From patchwork Mon Nov 6 18:31:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447285 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE9CFC41535 for ; Mon, 6 Nov 2023 18:32:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1A876B0272; Mon, 6 Nov 2023 13:32:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC88F6B0273; Mon, 6 Nov 2023 13:32:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1B796B0274; Mon, 6 Nov 2023 13:32:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BE9B86B0272 for ; Mon, 6 Nov 2023 13:32:04 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 85BBB1408A1 for ; Mon, 6 Nov 2023 18:32:04 +0000 (UTC) X-FDA: 81428373768.02.0C60EE0 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf27.hostedemail.com (Postfix) with ESMTP id 7058B40014 for ; Mon, 6 Nov 2023 18:32:02 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BVfoZprQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699295522; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5pFcPVumquKH2Gwm0GjVQh+sz7Zkp1krGgtTHD7g2j8=; b=4y3l7+7L6vDPtTmHHu2Dl14kyLFPiB2pG3DTK5PFqRdOEW3Siyapk2eghvIh0oYZj8+Xw7 HAE/5QOZs4yN5DfNLc+XG0cnLe5xB9puWdbwB8jvOhGkFjgGA/zK3HTDXvDX8jy7T7Tvlq LuObHCnWt3RN9o9PepQZizR+UBgMQMk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BVfoZprQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699295522; a=rsa-sha256; cv=none; b=QwnfihX7NyuJLv0n1sxO2m8HPNQN9Kh49vvFFvMGpKKVglsolK7CM7RIEAbY+IL5HN8OT9 VJOn/h6oq5vJWdnZ3A+Bg7FsR4H3+k/ppdNtD6A3/BjIvRBzbiy7HcNIxerCqFG3MICU/P vNWpLR/z4hSxRj4zJmdXaqrEDFwvlmc= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6b7f0170d7bso4830517b3a.2 for ; Mon, 06 Nov 2023 10:32:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699295521; x=1699900321; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5pFcPVumquKH2Gwm0GjVQh+sz7Zkp1krGgtTHD7g2j8=; b=BVfoZprQRX3gvbrpYoXkc5G18sEXJ6v7u6Q31ijJGRQbzwW9IW7HAIzXWwYA3TY/24 pmUt53bzu95t4na25fWxU8sv7uBqc0WqNkyR13cS3TTGC0gQRhe8iHlfBlGrOD/j3GoF 6S72e8Dgmzy6Oi+xhBWit2CDD7NYSltqus/TbzTo1QICRHzIWw05D8AKW5RSNejz2J6O DCpr2R8v2DLpGsO0+aY0rsxqzVkDLLxtlY6NNAxFYvVRV2ia8RMuk+liTMc/sudJtogT 2OdZn2Lifqtusc41Rj2uLk3ZJGhZ/j0mGRp43qkxi1CQMXuM9saTA3H3bj6OV8wNcnXn kQdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699295521; x=1699900321; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5pFcPVumquKH2Gwm0GjVQh+sz7Zkp1krGgtTHD7g2j8=; b=gSt44MoLpqqE0d4lvT2/sLtfSBMMqXFns0FsSraLZe0QGMjX/5YMVhN0BUN0CinhZl 1IQhzYrr9vTeST9g363c+JWB1AtBl5L+TDj9fk0qzpQ2SiAdHKP6Jbzz9O36UnpU2Cv6 EmD431UxOUswJ6wF698K1j8WL3GrqDPgZgIBg07DXIHnFDtKnWSG7nlhcAoou4ImuknI HEVzacUUpsCXUXI+EtuieuyxVPN9dbzcK3sOcW8lCXB8AtA2s8iOOB/xDqJiYmsSL5et jpc2CPmgrKUzuOaP/Bv3aN2Y2f9bL0LeqtImYNygCxMNozptmWHe5OI70AI10IxkuEGf TeRA== X-Gm-Message-State: AOJu0YzYO3n/w52UsR3BhaDBiBkSRGG9jiacr4NOfnXLmmwBL0pohI11 QlV1cfzpQgHZ19TJ+QRNTb8= X-Google-Smtp-Source: AGHT+IEdvmy59H9XmGUYA6zji1s0dbSLNLf3euz0dHvMncPHXmjFiDKaUu5rlouTtKERH+A68+5eVA== X-Received: by 2002:a05:6a00:b42:b0:6be:30f1:45f8 with SMTP id p2-20020a056a000b4200b006be30f145f8mr37139393pfo.20.1699295521198; Mon, 06 Nov 2023 10:32:01 -0800 (PST) Received: from localhost (fwdproxy-prn-007.fbsv.net. [2a03:2880:ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id p24-20020aa78618000000b006bf53a51e6dsm6119753pfn.179.2023.11.06.10.32.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 10:32:00 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v5 1/6] list_lru: allows explicit memcg and NUMA node selection Date: Mon, 6 Nov 2023 10:31:54 -0800 Message-Id: <20231106183159.3562879-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106183159.3562879-1-nphamcs@gmail.com> References: <20231106183159.3562879-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7058B40014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: oogogmd9djjpn83geor3r1d18azsb3d4 X-HE-Tag: 1699295522-672396 X-HE-Meta: U2FsdGVkX1/sZTwIYab9S7ciA59lBHu1MorOosE7bIeezsrB8Vqk9NpT1S3qYGGh0E4BY6t9plV1tKDIDvTMRD45dQSUQXtmcMMAFQc5WP6lx2jjBejPNjN4BRv1W+OHsH10B0u8kQggvey0O2TO4B5vHxSKJUL0cV4vf+BpZfVdzEp6wWxGY7gPEG1rmmbk/nH70Dst99hXh6Q5pkymuQNxm5QDT5n/ULCW41Petu3/YLVyHkYl2gq7vVIy285zczJMc2Nhn7L7Z9MDPU+V6xD4k/grkFhi3I/qHFSajtADOmOJetY0xQGN8uaEJZ88ilCcciSvEUL7NvpF9YG+t99IiSGQtB44i9bYexYh2KvoYaG62HVtKii5doX27NQpIk8Vf3wXw5G40FRLWpH2cM9HG/bfvJCujF8TmHo4ZQTwL+/4+gEKK7NdSMi5s5g73W6yCghSTR/A+DbWwqxirHkLxU7upXAan9m0e2x1wiW183JQQZSGNco125jmanVzW34qvPPXp+RZ42xw0OrpVAiWWNqAmYfehy/9sn+W0lenw4fxxHx8y9stXkOuRHbTiLFGx7SrRsoFLWHJ3dRcurt+P+2x2/Z3jlQZCvFtRe92nyVIoNqnMEh4B6Wdf00cvAiAJoau9PjiXAU+quS9ugascFTiqVVp4xCzzgTNx2fF+43O2ELiS2RrUskdkwsLeO83Lk+PU4bZlnzUFfZjG+mpFBPPHK9wHhgioXXvLbTlwl3Nk1NZg47ncKJBOZml+gP+GEJuZEwqvuI2wYNzQp/yqeLOmlpUQsDnApLEwqMCqkDoYb+acNvHudlnoJ+WLyA6iQl4B08b6KPnMVjMVOvIEEiZbhdMGEoW+iCkDNig9bgukC87R2oD3JDysbUiKGO4QwJmMfRs3WEMiDq5/p/HpXtrGhBqh8SijpY+hU3CfpIsXqpIe0RrVzuENdv9qI3Tace+gmJxkucNSgD c05Ft6Cu teeZC5/Ptfu+wNJLy9b85xQUUQdmsd1Qe5YdHn6RROaPxlUeKC/P0LAtpeEVcPhgCS2MMEe5c+hLsaS6Qvqz82vRfJxtGYv4S6rCumZ+xQioA1lHvu+GS6OXE7CItf/rNsNKypV+2xWTC6dhGw6sdDWa2aIJokwK+y6siULPkvWLMNQmAJJmsiTYd5T6yLMaTooQzdu3l3Qc9ngBFIYZuqY0rjSJOhZU/MsW4GlZeOx09hGMdBqmjsIzqzV0VJgdOC0wC2uzRtI/fxk7v7w7e/M1NJmO17Y9NvS17alFzCikrkAEzKiNxjQR2vphTthhHUfkOYoyZ7PP4UPPXNn4fsSXdYXKPJwy/P+r6ezXjhwe4USBmZC6Wr7XuknfjTiOMnFtep+wmcfIz6/+6JNdc565dLWmLsood9Qr1hxHSmOETbH1O59iLKsDtp4SiTdtAmsTzKv/HBnocBt+xvxpl2kzZo0T8kTuChJMQaB0zdFVh5cQCtW+xgrQ7o7x8SzdzB4O9SoN1MtUcxy24tCcAZ6X6nikzVjqAZ7ZKB0HiJ/AzrSUNaAOT8yakozAQ11TLpgTIzeJy4QgoFeE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The interface of list_lru is based on the assumption that the list node and the data it represents belong to the same allocated on the correct node/memcg. While this assumption is valid for existing slab objects LRU such as dentries and inodes, it is undocumented, and rather inflexible for certain potential list_lru users (such as the upcoming zswap shrinker and the THP shrinker). It has caused us a lot of issues during our development. This patch changes list_lru interface so that the caller must explicitly specify numa node and memcg when adding and removing objects. The old list_lru_add() and list_lru_del() are renamed to list_lru_add_obj() and list_lru_del_obj(), respectively. It also extends the list_lru API with a new function, list_lru_putback, which undoes a previous list_lru_isolate call. Unlike list_lru_add, it does not increment the LRU node count (as list_lru_isolate does not decrement the node count). list_lru_putback also allows for explicit memcg and NUMA node selection. Suggested-by: Johannes Weiner Signed-off-by: Nhat Pham --- drivers/android/binder_alloc.c | 5 ++-- fs/dcache.c | 8 +++--- fs/gfs2/quota.c | 6 ++--- fs/inode.c | 4 +-- fs/nfs/nfs42xattr.c | 8 +++--- fs/nfsd/filecache.c | 4 +-- fs/xfs/xfs_buf.c | 6 ++--- fs/xfs/xfs_dquot.c | 2 +- fs/xfs/xfs_qm.c | 2 +- include/linux/list_lru.h | 46 +++++++++++++++++++++++++++++--- mm/list_lru.c | 48 ++++++++++++++++++++++++++++------ mm/workingset.c | 4 +-- 12 files changed, 108 insertions(+), 35 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 138f6d43d13b..e80669d4e037 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -285,7 +285,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate, trace_binder_free_lru_start(alloc, index); - ret = list_lru_add(&binder_alloc_lru, &page->lru); + ret = list_lru_add_obj(&binder_alloc_lru, &page->lru); WARN_ON(!ret); trace_binder_free_lru_end(alloc, index); @@ -848,7 +848,7 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc) if (!alloc->pages[i].page_ptr) continue; - on_lru = list_lru_del(&binder_alloc_lru, + on_lru = list_lru_del_obj(&binder_alloc_lru, &alloc->pages[i].lru); page_addr = alloc->buffer + i * PAGE_SIZE; binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, @@ -1287,4 +1287,3 @@ int binder_alloc_copy_from_buffer(struct binder_alloc *alloc, return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset, dest, bytes); } - diff --git a/fs/dcache.c b/fs/dcache.c index 25ac74d30bff..482d1b34d88d 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -428,7 +428,8 @@ static void d_lru_add(struct dentry *dentry) this_cpu_inc(nr_dentry_unused); if (d_is_negative(dentry)) this_cpu_inc(nr_dentry_negative); - WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); + WARN_ON_ONCE(!list_lru_add_obj( + &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); } static void d_lru_del(struct dentry *dentry) @@ -438,7 +439,8 @@ static void d_lru_del(struct dentry *dentry) this_cpu_dec(nr_dentry_unused); if (d_is_negative(dentry)) this_cpu_dec(nr_dentry_negative); - WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); + WARN_ON_ONCE(!list_lru_del_obj( + &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); } static void d_shrink_del(struct dentry *dentry) @@ -1240,7 +1242,7 @@ static enum lru_status dentry_lru_isolate(struct list_head *item, * * This is guaranteed by the fact that all LRU management * functions are intermediated by the LRU API calls like - * list_lru_add and list_lru_del. List movement in this file + * list_lru_add_obj and list_lru_del_obj. List movement in this file * only ever occur through this functions or through callbacks * like this one, that are called from the LRU API. * diff --git a/fs/gfs2/quota.c b/fs/gfs2/quota.c index 2f1328af34f4..72015594bc83 100644 --- a/fs/gfs2/quota.c +++ b/fs/gfs2/quota.c @@ -271,7 +271,7 @@ static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash, if (qd->qd_sbd != sdp) continue; if (lockref_get_not_dead(&qd->qd_lockref)) { - list_lru_del(&gfs2_qd_lru, &qd->qd_lru); + list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); return qd; } } @@ -344,7 +344,7 @@ static void qd_put(struct gfs2_quota_data *qd) } qd->qd_lockref.count = 0; - list_lru_add(&gfs2_qd_lru, &qd->qd_lru); + list_lru_add_obj(&gfs2_qd_lru, &qd->qd_lru); spin_unlock(&qd->qd_lockref.lock); } @@ -1508,7 +1508,7 @@ void gfs2_quota_cleanup(struct gfs2_sbd *sdp) lockref_mark_dead(&qd->qd_lockref); spin_unlock(&qd->qd_lockref.lock); - list_lru_del(&gfs2_qd_lru, &qd->qd_lru); + list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); list_add(&qd->qd_lru, &dispose); } spin_unlock(&qd_lock); diff --git a/fs/inode.c b/fs/inode.c index 84bc3c76e5cc..f889ba8dccd9 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -462,7 +462,7 @@ static void __inode_add_lru(struct inode *inode, bool rotate) if (!mapping_shrinkable(&inode->i_data)) return; - if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru)) + if (list_lru_add_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) this_cpu_inc(nr_unused); else if (rotate) inode->i_state |= I_REFERENCED; @@ -480,7 +480,7 @@ void inode_add_lru(struct inode *inode) static void inode_lru_list_del(struct inode *inode) { - if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru)) + if (list_lru_del_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) this_cpu_dec(nr_unused); } diff --git a/fs/nfs/nfs42xattr.c b/fs/nfs/nfs42xattr.c index 2ad66a8922f4..49aaf28a6950 100644 --- a/fs/nfs/nfs42xattr.c +++ b/fs/nfs/nfs42xattr.c @@ -132,7 +132,7 @@ nfs4_xattr_entry_lru_add(struct nfs4_xattr_entry *entry) lru = (entry->flags & NFS4_XATTR_ENTRY_EXTVAL) ? &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru; - return list_lru_add(lru, &entry->lru); + return list_lru_add_obj(lru, &entry->lru); } static bool @@ -143,7 +143,7 @@ nfs4_xattr_entry_lru_del(struct nfs4_xattr_entry *entry) lru = (entry->flags & NFS4_XATTR_ENTRY_EXTVAL) ? &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru; - return list_lru_del(lru, &entry->lru); + return list_lru_del_obj(lru, &entry->lru); } /* @@ -349,7 +349,7 @@ nfs4_xattr_cache_unlink(struct inode *inode) oldcache = nfsi->xattr_cache; if (oldcache != NULL) { - list_lru_del(&nfs4_xattr_cache_lru, &oldcache->lru); + list_lru_del_obj(&nfs4_xattr_cache_lru, &oldcache->lru); oldcache->inode = NULL; } nfsi->xattr_cache = NULL; @@ -474,7 +474,7 @@ nfs4_xattr_get_cache(struct inode *inode, int add) kref_get(&cache->ref); nfsi->xattr_cache = cache; cache->inode = inode; - list_lru_add(&nfs4_xattr_cache_lru, &cache->lru); + list_lru_add_obj(&nfs4_xattr_cache_lru, &cache->lru); } spin_unlock(&inode->i_lock); diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c index 9c62b4502539..82352c100b49 100644 --- a/fs/nfsd/filecache.c +++ b/fs/nfsd/filecache.c @@ -322,7 +322,7 @@ nfsd_file_check_writeback(struct nfsd_file *nf) static bool nfsd_file_lru_add(struct nfsd_file *nf) { set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); - if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) { + if (list_lru_add_obj(&nfsd_file_lru, &nf->nf_lru)) { trace_nfsd_file_lru_add(nf); return true; } @@ -331,7 +331,7 @@ static bool nfsd_file_lru_add(struct nfsd_file *nf) static bool nfsd_file_lru_remove(struct nfsd_file *nf) { - if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) { + if (list_lru_del_obj(&nfsd_file_lru, &nf->nf_lru)) { trace_nfsd_file_lru_del(nf); return true; } diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c index 9e7ba04572db..9c2654a8d24b 100644 --- a/fs/xfs/xfs_buf.c +++ b/fs/xfs/xfs_buf.c @@ -169,7 +169,7 @@ xfs_buf_stale( atomic_set(&bp->b_lru_ref, 0); if (!(bp->b_state & XFS_BSTATE_DISPOSE) && - (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru))) + (list_lru_del_obj(&bp->b_target->bt_lru, &bp->b_lru))) atomic_dec(&bp->b_hold); ASSERT(atomic_read(&bp->b_hold) >= 1); @@ -1047,7 +1047,7 @@ xfs_buf_rele( * buffer for the LRU and clear the (now stale) dispose list * state flag */ - if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) { + if (list_lru_add_obj(&bp->b_target->bt_lru, &bp->b_lru)) { bp->b_state &= ~XFS_BSTATE_DISPOSE; atomic_inc(&bp->b_hold); } @@ -1060,7 +1060,7 @@ xfs_buf_rele( * was on was the disposal list */ if (!(bp->b_state & XFS_BSTATE_DISPOSE)) { - list_lru_del(&bp->b_target->bt_lru, &bp->b_lru); + list_lru_del_obj(&bp->b_target->bt_lru, &bp->b_lru); } else { ASSERT(list_empty(&bp->b_lru)); } diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c index ac6ba646624d..49f619f5aa96 100644 --- a/fs/xfs/xfs_dquot.c +++ b/fs/xfs/xfs_dquot.c @@ -1064,7 +1064,7 @@ xfs_qm_dqput( struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo; trace_xfs_dqput_free(dqp); - if (list_lru_add(&qi->qi_lru, &dqp->q_lru)) + if (list_lru_add_obj(&qi->qi_lru, &dqp->q_lru)) XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused); } xfs_dqunlock(dqp); diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c index 94a7932ac570..67d0a8564ff3 100644 --- a/fs/xfs/xfs_qm.c +++ b/fs/xfs/xfs_qm.c @@ -171,7 +171,7 @@ xfs_qm_dqpurge( * hits zero, so it really should be on the freelist here. */ ASSERT(!list_empty(&dqp->q_lru)); - list_lru_del(&qi->qi_lru, &dqp->q_lru); + list_lru_del_obj(&qi->qi_lru, &dqp->q_lru); XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused); xfs_qm_dqdestroy(dqp); diff --git a/include/linux/list_lru.h b/include/linux/list_lru.h index b35968ee9fb5..5ef217443299 100644 --- a/include/linux/list_lru.h +++ b/include/linux/list_lru.h @@ -75,6 +75,8 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren * list_lru_add: add an element to the lru list's tail * @list_lru: the lru pointer * @item: the item to be added. + * @memcg: the cgroup of the sublist to add the item to. + * @nid: the node id of the sublist to add the item to. * * If the element is already part of a list, this function returns doing * nothing. Therefore the caller does not need to keep state about whether or @@ -87,12 +89,28 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren * * Return value: true if the list was updated, false otherwise */ -bool list_lru_add(struct list_lru *lru, struct list_head *item); +bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg); /** - * list_lru_del: delete an element to the lru list + * list_lru_add_obj: add an element to the lru list's tail + * @list_lru: the lru pointer + * @item: the item to be added. + * + * This function is similar to list_lru_add(), but the NUMA node and the + * memcg of the sublist is determined by @item list_head. This assumption is + * valid for slab objects LRU such as dentries, inodes, etc. + * + * Return value: true if the list was updated, false otherwise + */ +bool list_lru_add_obj(struct list_lru *lru, struct list_head *item); + +/** + * list_lru_del: delete an element from the lru list * @list_lru: the lru pointer * @item: the item to be deleted. + * @memcg: the cgroup of the sublist to delete the item from. + * @nid: the node id of the sublist to delete the item from. * * This function works analogously as list_lru_add in terms of list * manipulation. The comments about an element already pertaining to @@ -100,7 +118,21 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item); * * Return value: true if the list was updated, false otherwise */ -bool list_lru_del(struct list_lru *lru, struct list_head *item); +bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg); + +/** + * list_lru_del_obj: delete an element from the lru list + * @list_lru: the lru pointer + * @item: the item to be deleted. + * + * This function is similar to list_lru_del(), but the NUMA node and the + * memcg of the sublist is determined by @item list_head. This assumption is + * valid for slab objects LRU such as dentries, inodes, etc. + * + * Return value: true if the list was updated, false otherwise. + */ +bool list_lru_del_obj(struct list_lru *lru, struct list_head *item); /** * list_lru_count_one: return the number of objects currently held by @lru @@ -136,6 +168,14 @@ static inline unsigned long list_lru_count(struct list_lru *lru) void list_lru_isolate(struct list_lru_one *list, struct list_head *item); void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, struct list_head *head); +/* + * list_lru_putback: undo list_lru_isolate. + * + * Since we might have dropped the LRU lock in between, recompute list_lru_one + * from the node's id and memcg. + */ +void list_lru_putback(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg); typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item, struct list_lru_one *list, spinlock_t *lock, void *cb_arg); diff --git a/mm/list_lru.c b/mm/list_lru.c index a05e5bef3b40..fcca67ac26ec 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -116,21 +116,19 @@ list_lru_from_kmem(struct list_lru *lru, int nid, void *ptr, } #endif /* CONFIG_MEMCG_KMEM */ -bool list_lru_add(struct list_lru *lru, struct list_head *item) +bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg) { - int nid = page_to_nid(virt_to_page(item)); struct list_lru_node *nlru = &lru->node[nid]; - struct mem_cgroup *memcg; struct list_lru_one *l; spin_lock(&nlru->lock); if (list_empty(item)) { - l = list_lru_from_kmem(lru, nid, item, &memcg); + l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg)); list_add_tail(item, &l->list); /* Set shrinker bit if the first element was added */ if (!l->nr_items++) - set_shrinker_bit(memcg, nid, - lru_shrinker_id(lru)); + set_shrinker_bit(memcg, nid, lru_shrinker_id(lru)); nlru->nr_items++; spin_unlock(&nlru->lock); return true; @@ -140,15 +138,25 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item) } EXPORT_SYMBOL_GPL(list_lru_add); -bool list_lru_del(struct list_lru *lru, struct list_head *item) +bool list_lru_add_obj(struct list_lru *lru, struct list_head *item) { int nid = page_to_nid(virt_to_page(item)); + struct mem_cgroup *memcg = list_lru_memcg_aware(lru) ? + mem_cgroup_from_slab_obj(item) : NULL; + + return list_lru_add(lru, item, nid, memcg); +} +EXPORT_SYMBOL_GPL(list_lru_add_obj); + +bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg) +{ struct list_lru_node *nlru = &lru->node[nid]; struct list_lru_one *l; spin_lock(&nlru->lock); if (!list_empty(item)) { - l = list_lru_from_kmem(lru, nid, item, NULL); + l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg)); list_del_init(item); l->nr_items--; nlru->nr_items--; @@ -160,6 +168,16 @@ bool list_lru_del(struct list_lru *lru, struct list_head *item) } EXPORT_SYMBOL_GPL(list_lru_del); +bool list_lru_del_obj(struct list_lru *lru, struct list_head *item) +{ + int nid = page_to_nid(virt_to_page(item)); + struct mem_cgroup *memcg = list_lru_memcg_aware(lru) ? + mem_cgroup_from_slab_obj(item) : NULL; + + return list_lru_del(lru, item, nid, memcg); +} +EXPORT_SYMBOL_GPL(list_lru_del_obj); + void list_lru_isolate(struct list_lru_one *list, struct list_head *item) { list_del_init(item); @@ -175,6 +193,20 @@ void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, } EXPORT_SYMBOL_GPL(list_lru_isolate_move); +void list_lru_putback(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg) +{ + struct list_lru_one *list = + list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg)); + + if (list_empty(item)) { + list_add_tail(item, &list->list); + if (!list->nr_items++) + set_shrinker_bit(memcg, nid, lru_shrinker_id(lru)); + } +} +EXPORT_SYMBOL_GPL(list_lru_putback); + unsigned long list_lru_count_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg) { diff --git a/mm/workingset.c b/mm/workingset.c index 11045febc383..7d3dacab8451 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -631,12 +631,12 @@ void workingset_update_node(struct xa_node *node) if (node->count && node->count == node->nr_values) { if (list_empty(&node->private_list)) { - list_lru_add(&shadow_nodes, &node->private_list); + list_lru_add_obj(&shadow_nodes, &node->private_list); __inc_lruvec_kmem_state(node, WORKINGSET_NODES); } } else { if (!list_empty(&node->private_list)) { - list_lru_del(&shadow_nodes, &node->private_list); + list_lru_del_obj(&shadow_nodes, &node->private_list); __dec_lruvec_kmem_state(node, WORKINGSET_NODES); } } From patchwork Mon Nov 6 18:31:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447286 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20DB2C001B2 for ; Mon, 6 Nov 2023 18:32:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1FEC26B0273; Mon, 6 Nov 2023 13:32:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 15D086B0274; Mon, 6 Nov 2023 13:32:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA2A56B0275; Mon, 6 Nov 2023 13:32:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D58A46B0273 for ; Mon, 6 Nov 2023 13:32:05 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9ACC840880 for ; Mon, 6 Nov 2023 18:32:05 +0000 (UTC) X-FDA: 81428373810.15.6B6D67C Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf11.hostedemail.com (Postfix) with ESMTP id B2B5840010 for ; Mon, 6 Nov 2023 18:32:03 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=le8IRabu; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699295523; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aeg4px5UubAcyDZsKdAVJT5jO+bA71pIdTPHzCJTR5Q=; b=W7nGUqmqcPO1xNE3KcVms+tF78HIEF64Ctjv7c2Xogju43lC4mjJz36MQ8gyNOaIV2S01E d2lh1EzjYfvPN+gGsYWzI8QE0f55y81LhdN2vHIN2s793uh2A8RyP9PYyLBAu9+94O032T l/1OIdA0qEml5P+dGcz0PsroXOInpFo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699295523; a=rsa-sha256; cv=none; b=iuEYH5l7mB0C5p2kVYwHpgAca7fJdSk4aN/IPT9ZsQVD7effW0bHJhTH+sN4sfSSICJsE8 abmkgrnUASj1kMmAs1zkDUtK9MGXxm/jrlAHzJMWNAtbg19Pz5h+XbNBuuIEFNPCb8ykNf o2cy6uwTiimN+uvpUojJ8jEbLZOeFVU= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=le8IRabu; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1cc131e52f1so45016925ad.0 for ; Mon, 06 Nov 2023 10:32:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699295522; x=1699900322; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aeg4px5UubAcyDZsKdAVJT5jO+bA71pIdTPHzCJTR5Q=; b=le8IRabuGMrSJmwbd23Q+oOobnnN5Ld4lm8QDAZmPGpjC4V6svoy0hl0gBTdvblCic kzmhcaa7oLRtdYAlUWwZr3HYe/rWhZ+tjYOR9b0CMsahWpOEcZlVTaqDhfGahTUWt+83 /4TId/slp8HDWetUyzfOT+IrZe7PqgTKPw1AOVZj7YTv6owsBpCI9WzwRewztAGfqDH6 zEB9ewn/yb4X/whp/qIuKldqAB2/pkUvFI5fGUblfMGkRMH1GX+DrXCbtkwDtDIRLXdM RlOlVUPVAzr2cxh0we2Q9OfFLgFnxkEMCSiMICbb6sAWOiI9vVkwz1AlmmuRg9oYRm4e XHfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699295522; x=1699900322; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aeg4px5UubAcyDZsKdAVJT5jO+bA71pIdTPHzCJTR5Q=; b=dJq81a2HbeelCrhInDeDV/FRNqgY8We+ri3V9P471CJ6npS5phP4NFBtJGHr3LYkmm 74mi9pe5XQ1vR2J5WkgC4uWRtDc0MtkDxfre51m3VH2xL6nMXqy/5jgLUtlIv2tWNd6H VIh3jROXrphsVPK8ykndtnvFMjXgaB0R2EDv8TKxKp1OW3cXhJVOca3Yj8vtrvdkgyHG FEFQIGFY6728i89fs4VMxEwRrCeFZ5Xb6AJeMTiZwjpw2MhWbZi76YAGcP8CWTlz36Ld llcZEh3oET0tJT4tRGcCU2M6y29wB02wWCyEPyNZiKR6n6/fXaeYRWjXBkVKPD+Aczfc m/3w== X-Gm-Message-State: AOJu0YwqFa9oBLKWwcBVTn+cUoCppPpVVmCj8+pX9iZFuxZVcbkqUTwP cINexl64tpChF9q2hd+QJuw= X-Google-Smtp-Source: AGHT+IH8J6dzOa+acwIoDEAwJcVL40/FtAfyj5lkxXosODWxLZGIgiyQlQw11vmAE3oLLfcbhjqGJg== X-Received: by 2002:a17:902:f54b:b0:1cc:5ce4:f657 with SMTP id h11-20020a170902f54b00b001cc5ce4f657mr336993plf.29.1699295522328; Mon, 06 Nov 2023 10:32:02 -0800 (PST) Received: from localhost (fwdproxy-prn-015.fbsv.net. [2a03:2880:ff:f::face:b00c]) by smtp.gmail.com with ESMTPSA id p9-20020a170902e74900b001c73701bd17sm6215465plf.4.2023.11.06.10.32.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 10:32:01 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v5 2/6] memcontrol: allows mem_cgroup_iter() to check for onlineness Date: Mon, 6 Nov 2023 10:31:55 -0800 Message-Id: <20231106183159.3562879-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106183159.3562879-1-nphamcs@gmail.com> References: <20231106183159.3562879-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Stat-Signature: bcormo98cpoarnp4ygxt3iccw8riotht X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B2B5840010 X-Rspam-User: X-HE-Tag: 1699295523-504224 X-HE-Meta: U2FsdGVkX1/xGkAd2Jh2qWZ2nnk7xBPqp/6EWeM4KVvMhYgKlVUF44LT4sl4K43T0fylhk77l1a6L/iMtYjLFSyudA5rQsBvb2PTfx8V+pBwEDe34flpHyFoc2GFR7aaSUAqbh9HzfMGxr2ZVARg32yB8SDn6S/jph8bYwC94ju+7/vFaQcFM90OGpv+lxa2+QyBrNie730NFP9mokhBr4mjDdBwTo4IndjpH4JNVszZ1N6mhHKuLAMXxIvFcwoJGfoj+aJqdbrPBT+8HJBerFwEVzYEFoYcvlURu71Rd6z3cG2fVJYyjTGJuEw9wD0hNwJqCtwZaFbVqgYb6Ud4rx4ODqq8QYRf6Ua1Iv3HMlO0BRvQfLO5nH3GTRktazVGGXUoLvmwKE/FEmt8R2qKP4YqI6kDPHXSXgYDreIxppxnT2Hlt6TNTbKcRL8knutKecSWHp4SQSOdtDoBpZL+k78hKg3dJnHgxIdW+Fs6G00TrNrsBS7aJfNhVsf0hdI77DlpDMZRF6KxDrh4LX0FopIBUDvU4Ll5GQkcO5+YD3JWTPekjW7cnEfIoZ6KYY7IaLqi4+UjW2pd3Hkn3z6sNf2k8bgdyEzOr7NTUFitcrshVbe7CgJzVIhls6APFwVJ/5xCdVqVuCfUsyiqv0skOGB789+lNrX+dG5IILEP/dbnTQqiIQxVI4LYskrstJ0QViVnjDiaqdRsXsIThRNp6rcAlMiX7yjT/31iJ0qUasVV/lbnijs9QN5AX7q0yIePo725zHvUNB/ZL6RaBhnqooFlqewLnAAFmdUOezaOwrC897qi8VhchLm7WT59r/6hMpr26e8110grTlS3vxgHhQmQY7fbMny0V53k6v3AJwFjiIx35e+Vsk6VYP5CYk64Qt2XgnAIby2fm/+Hz+rqfpb4l1eDESSE8HTZgs2RVY+d+zz4UOuwHtoINcU2hxUoxRzIWd2YD0BqJRd+0zX XPSFdbEv WUQ+zhEZZTxbzc/BBN06A8Zn2c2LA93Xz8SrqYxc8wUAAYCGnJ7BohRhBeLw6ziKnqNPPbivItGK3kY035lpIUwTde8jNrti03bhmsUMgl9zi6Bz4uiP7Y4yhroyiuGuZeJRtXs6MyDPaRAbsPP93I38BG05paxIaR5NkFRHR8lV8yIFI6uA47KJ9lt/rycqCZ5OPhWInTR8lUJTcNr2F2sLzFfDj9TDnIZBG6gZgROMka6pFKcGy+q5eGlkSh+kTfIcMECAiJ5Om6siMPe27DwJL0eN4X+NWItwXaYO3BbiCssigQNCnmBU6iKlGGPjxp/jlDsWjqeWT58irUWGF3lcvi53mx0Zd0/GhIOWgmmT8o7gI1ixsknA98O3D5PZD+8qIHVMt7xkr34oVeaDsR/WQCEYY4vfaxwBJ3Nolw479NMDxv0gkZffGy3EVTaqsjbsmAj943yxw8+xnYXwQHT5nOJhKncq5WeMO X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The new zswap writeback scheme requires an online-only memcg hierarchy traversal. Add a new parameter to mem_cgroup_iter() to check for onlineness before returning. Signed-off-by: Nhat Pham --- include/linux/memcontrol.h | 4 ++-- mm/memcontrol.c | 17 ++++++++++------- mm/shrinker.c | 4 ++-- mm/vmscan.c | 26 +++++++++++++------------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 6edd3ec4d8d5..55c85f952afd 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -832,7 +832,7 @@ static inline void mem_cgroup_put(struct mem_cgroup *memcg) struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *, struct mem_cgroup *, - struct mem_cgroup_reclaim_cookie *); + struct mem_cgroup_reclaim_cookie *, bool online); void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, int (*)(struct task_struct *, void *), void *arg); @@ -1381,7 +1381,7 @@ static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, static inline struct mem_cgroup * mem_cgroup_iter(struct mem_cgroup *root, struct mem_cgroup *prev, - struct mem_cgroup_reclaim_cookie *reclaim) + struct mem_cgroup_reclaim_cookie *reclaim, bool online) { return NULL; } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 61c0c46c2d62..6f7fc0101252 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -221,14 +221,14 @@ enum res_type { * be used for reference counting. */ #define for_each_mem_cgroup_tree(iter, root) \ - for (iter = mem_cgroup_iter(root, NULL, NULL); \ + for (iter = mem_cgroup_iter(root, NULL, NULL, false); \ iter != NULL; \ - iter = mem_cgroup_iter(root, iter, NULL)) + iter = mem_cgroup_iter(root, iter, NULL, false)) #define for_each_mem_cgroup(iter) \ - for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ + for (iter = mem_cgroup_iter(NULL, NULL, NULL, false); \ iter != NULL; \ - iter = mem_cgroup_iter(NULL, iter, NULL)) + iter = mem_cgroup_iter(NULL, iter, NULL, false)) static inline bool task_is_dying(void) { @@ -1139,6 +1139,7 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) * @root: hierarchy root * @prev: previously returned memcg, NULL on first invocation * @reclaim: cookie for shared reclaim walks, NULL for full walks + * @online: skip offline memcgs * * Returns references to children of the hierarchy below @root, or * @root itself, or %NULL after a full round-trip. @@ -1153,7 +1154,8 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) */ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, struct mem_cgroup *prev, - struct mem_cgroup_reclaim_cookie *reclaim) + struct mem_cgroup_reclaim_cookie *reclaim, + bool online) { struct mem_cgroup_reclaim_iter *iter; struct cgroup_subsys_state *css = NULL; @@ -1223,7 +1225,8 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, * is provided by the caller, so we know it's alive * and kicking, and don't take an extra reference. */ - if (css == &root->css || css_tryget(css)) { + if (css == &root->css || (!online && css_tryget(css)) || + css_tryget_online(css)) { memcg = mem_cgroup_from_css(css); break; } @@ -1836,7 +1839,7 @@ static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, excess = soft_limit_excess(root_memcg); while (1) { - victim = mem_cgroup_iter(root_memcg, victim, &reclaim); + victim = mem_cgroup_iter(root_memcg, victim, &reclaim, false); if (!victim) { loop++; if (loop >= 2) { diff --git a/mm/shrinker.c b/mm/shrinker.c index dd91eab43ed3..54f5d3aa4f27 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -160,7 +160,7 @@ static int expand_shrinker_info(int new_id) new_size = shrinker_unit_size(new_nr_max); old_size = shrinker_unit_size(shrinker_nr_max); - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { ret = expand_one_shrinker_info(memcg, new_size, old_size, new_nr_max); @@ -168,7 +168,7 @@ static int expand_shrinker_info(int new_id) mem_cgroup_iter_break(NULL, memcg); goto out; } - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false)) != NULL); out: if (!ret) shrinker_nr_max = new_nr_max; diff --git a/mm/vmscan.c b/mm/vmscan.c index 2cc0cb41fb32..065d29502580 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -397,10 +397,10 @@ static unsigned long drop_slab_node(int nid) unsigned long freed = 0; struct mem_cgroup *memcg = NULL; - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false)) != NULL); return freed; } @@ -3931,7 +3931,7 @@ static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY) return; - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); @@ -3941,7 +3941,7 @@ static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) } cond_resched(); - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false))); /* * The main goal is to OOM kill if every generation from all memcgs is @@ -5033,7 +5033,7 @@ static void lru_gen_change_state(bool enabled) else static_branch_disable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]); - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { int nid; @@ -5057,7 +5057,7 @@ static void lru_gen_change_state(bool enabled) } cond_resched(); - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false))); unlock: mutex_unlock(&state_mutex); put_online_mems(); @@ -5160,7 +5160,7 @@ static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos) if (!m->private) return ERR_PTR(-ENOMEM); - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { int nid; @@ -5168,7 +5168,7 @@ static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos) if (!nr_to_skip--) return get_lruvec(memcg, nid); } - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false))); return NULL; } @@ -5191,7 +5191,7 @@ static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos) nid = next_memory_node(nid); if (nid == MAX_NUMNODES) { - memcg = mem_cgroup_iter(NULL, memcg, NULL); + memcg = mem_cgroup_iter(NULL, memcg, NULL, false); if (!memcg) return NULL; @@ -5794,7 +5794,7 @@ static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) struct mem_cgroup *target_memcg = sc->target_mem_cgroup; struct mem_cgroup *memcg; - memcg = mem_cgroup_iter(target_memcg, NULL, NULL); + memcg = mem_cgroup_iter(target_memcg, NULL, NULL, false); do { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); unsigned long reclaimed; @@ -5844,7 +5844,7 @@ static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) sc->nr_scanned - scanned, sc->nr_reclaimed - reclaimed); - } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL, false))); } static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) @@ -6511,12 +6511,12 @@ static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc) if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) return; - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { lruvec = mem_cgroup_lruvec(memcg, pgdat); shrink_active_list(SWAP_CLUSTER_MAX, lruvec, sc, LRU_ACTIVE_ANON); - memcg = mem_cgroup_iter(NULL, memcg, NULL); + memcg = mem_cgroup_iter(NULL, memcg, NULL, false); } while (memcg); } From patchwork Mon Nov 6 18:31:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447294 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D19BC001B5 for ; Mon, 6 Nov 2023 18:32:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 138D36B0274; Mon, 6 Nov 2023 13:32:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C1416B0275; Mon, 6 Nov 2023 13:32:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E552B6B0276; Mon, 6 Nov 2023 13:32:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D1B8E6B0274 for ; Mon, 6 Nov 2023 13:32:06 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A202F14081A for ; Mon, 6 Nov 2023 18:32:06 +0000 (UTC) X-FDA: 81428373852.03.AD56D3C Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf05.hostedemail.com (Postfix) with ESMTP id AE05410001E for ; Mon, 6 Nov 2023 18:32:04 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=PIpSnKQ8; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699295524; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nLPnyP0RaynBJicEAOxae+atCQWtlTDNIjOFc7rQd7s=; b=TEsta06oZtKJg8EdWq3tKokiZKPe1505m3ehmGDU85u5HGSku/npuuc/x2HtnJyyycrPI2 vQw72ndZDUBG+fzrSOz7Rbt+lgM8PR9tCTpWwHvmmem021zRPzHzD8jBNVAzMnZli5niRM CRtIEu3K24KiS2lRdUXXRCkf2VXwhZo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699295524; a=rsa-sha256; cv=none; b=8AnB8Bk/GUMcpFWaHexP7k2XBD8RR/XvaR1XIAFmq895GQtHYycVoaMCYxaqfL2gomN1i5 e/j8y6ygB9pZZKIQk4omb7yBoMadLJ2oGgx6D/j4Fy1bkuO+GOplmcpsCfL0r9OG2MVLvd WgjTywQJaVpgzYSlta1QQ2M6vr5y9Oc= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=PIpSnKQ8; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1cc9b626a96so23594985ad.2 for ; Mon, 06 Nov 2023 10:32:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699295523; x=1699900323; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nLPnyP0RaynBJicEAOxae+atCQWtlTDNIjOFc7rQd7s=; b=PIpSnKQ8l925Xqxm0eh8c6k/iZVRiqThW/kZcBq+kzCro242Zll6bYUYeABAf6jVEE pZRM+TG7lnsRyaoFgSzvLAfaqoSzUOcgkRcYFieuNgj5WqVJIckjuosOJ3hjM+mhGNQu Xi1kUcmJ4Wo7DfFeW11N3SyBMHakR04P6V1KqQ6LVA9G5Qi57Er7HUOjtAiOfVWqqy1R QQlpdIDuhpAn0wuh02G/XOaRfGDZ4visu9nS8Cyohl15P2oSfzAGkQd6M/ZlaHoxjG3z wENhVQzQVskZFYpMsrDpaIBrfZoXKKWLDuQndoTHVDRxptr04qOsAklGT++lJoV1uKiU sqcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699295523; x=1699900323; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nLPnyP0RaynBJicEAOxae+atCQWtlTDNIjOFc7rQd7s=; b=fI9Q6S+pFfXZIKf+LqNiEUEIZJYnaYbF8QpFS11nA35VDAHodMgZ3KhxdswaMbHSKr 0kRs5dCJ1+qWXq7My88VHPS2nI1Z8LzgFw3v/B/mAdAdSogdc9DElcuAwniNKWjz/8JT 4F77eB5ETjj6rR2IwxUONcdwbcEdynP+0pNd/vuAlLVo0zaUXySTLialH3uHXsPSQNgk I/gOG6WUOg9Wu2DeIY4oLzCt7T9bwMfbnABhnEyWER7721f5f6MzHuOVe9/0vEm3MDZu +W0YC7Crpl2z/oOAwgohsk+vMLoOagRZn2FzgXETmuoX8DHfWR8d7wEHIDJazYeJgdhs myZw== X-Gm-Message-State: AOJu0YxXxDbLjH7+JtX2F6JPniNB3laFQ/e2CbM819/4gQJfuYFgj7PY 5qbZz5a49J5TNOi3DqRcbpI= X-Google-Smtp-Source: AGHT+IHLadiF672x0FUJEhI9uPXAzFqdH8ZA0VHMO8fhwfSJYioG6QuQm0IBydiy65qnzpPAPUU1IA== X-Received: by 2002:a17:902:d484:b0:1cc:361b:7b28 with SMTP id c4-20020a170902d48400b001cc361b7b28mr24335746plg.64.1699295523478; Mon, 06 Nov 2023 10:32:03 -0800 (PST) Received: from localhost (fwdproxy-prn-009.fbsv.net. [2a03:2880:ff:9::face:b00c]) by smtp.gmail.com with ESMTPSA id u22-20020a1709026e1600b001c5dea67c26sm6357160plk.233.2023.11.06.10.32.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 10:32:02 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v5 3/6] zswap: make shrinking memcg-aware Date: Mon, 6 Nov 2023 10:31:56 -0800 Message-Id: <20231106183159.3562879-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106183159.3562879-1-nphamcs@gmail.com> References: <20231106183159.3562879-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AE05410001E X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: i573jkfbc7ba5ws4hb1cbizzycjjmp8w X-HE-Tag: 1699295524-276146 X-HE-Meta: U2FsdGVkX19ii35ypMI7N+TgLqNKVQlQ96MsfHe3a1PFoDQit6oXUDpuhZT9sg7yqV5tQW7FO3gV9NRlZJQRnZy0OMhvdx7L4JyQx6X6HbfCFyTiZ8Jc7l1VmdDI5e7gSKTNg9yFCPtV6hojdwAcAbfNEgwc6DRuRp58QR6M5xFMiKj68sEn9nlyu1wTmXwjEw039GgErU3nCgX7Gcus9byQJ4OgYpk0QQthQbm0cHmxlbq6+ATwgJXyfsDXjpaUBfC3oh2Bmx8V1wSCOHCjC8pTvrpwvXEg5zhfLZWORl37blutpXaHXgswAQZc//FTaSbh+mg3GvILciZm3BokB2RlOBK3e+UdWuBF8gYpd8kbDlovuyQ9R+xJXqzC9Q43HOMvuDMV38oGqL5gO3Dtdw0dMZQMFKHf5Er66syvdH+aVhqqwgZ6Nr2kd+ZmAtLtDQF/mNg2qfHicu1Vo0sT/S0wzJmQU4krCF1eaO2X6ir+jLatXQFQJTQUaGkT5Pm8R4DyeqYWL9Y/VWJV88bzMLDLiLSkDG2ijtxmBYoPeYDd3QAJdLYmzTF1MVI6+wOO48m2+0d6CACU74Jmb3k9hthI1XQ+XScfNKdaPV9kFJy/ymL+SyylOcP7YJxU7l8ymJLilCMIBOzkcwENQnHp0xDF4QKhJ9ERfgZYTovJRX1WDbhRnD6xzYQvhqKzmgHTwzoJrSMlZQgofGBszBhUf+oMGZl1Bhk6TUVvOhUbmIttTRgZQTzujgMQkQUvq4WtX8no3UvLq8PP71js07B38gYQDnaICU1/VuVbJOJ+9pgycoJkM+p7QQzlCiyznp4amUuDfp4AmtBXi0thf3SaMAlNs5568veRMotoqKLtsxmk30qNE/H5aT3DSHjYHjJBjaENs+Cx1JkaP+lr6CN8Oe4bjcZ3H+PlSNiJ+TwCvBJsLovz3wNwewhrRa00zbZc74iM7386lITYeJveB3J Po2wzmwl +yQKHV1rzJnTA6TYUhBbk7tsT8tlq8umg7j+xskFhuIxCoqJY8K7A0YAojNjNoxVY33xC+cxe4Ixb8G3YhpC05W+WEUTJA+Qm5YpmfIqXQkYLYLwrtFD3Ymr0/c8dqIMlR1OncxYpkxYmQuUrdqMT7Gi8p+PKlNfLnJsyj2mbEte6gQKupf+c39UXzGkpakbWvr7VvrMPEQW52nUlPNMYdM0ARe/PBynplZE8KybZVuxHZ+vjEZjUeV2pzLNbkk702E4OlI15U45pI9Wpc6v+u0m7Vly25D2qLBMYi1M+h4kvlhJSrzTBlYFGib+jpLj70VEI5UzV4JvNrhny9t8PoHqrpkSvCl66/esRUZ2Xbk6yE6mjaSVLKOkQu7atXqXfeHTSWcQ1FYwpxyMotY8YtzQ2e22caN+5wdCOIXaOml9nhEMvC7jeN1sGWHx1K31ZRZloLnlcQCjooKjuRRSHrGXvuwSSQNF493Vi89/rCgOLveQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Domenico Cerasuolo Currently, we only have a single global LRU for zswap. This makes it impossible to perform worload-specific shrinking - an memcg cannot determine which pages in the pool it owns, and often ends up writing pages from other memcgs. This issue has been previously observed in practice and mitigated by simply disabling memcg-initiated shrinking: https://lore.kernel.org/all/20230530232435.3097106-1-nphamcs@gmail.com/T/#u This patch fully resolves the issue by replacing the global zswap LRU with memcg- and NUMA-specific LRUs, and modify the reclaim logic: a) When a store attempt hits an memcg limit, it now triggers a synchronous reclaim attempt that, if successful, allows the new hotter page to be accepted by zswap. b) If the store attempt instead hits the global zswap limit, it will trigger an asynchronous reclaim attempt, in which an memcg is selected for reclaim in a round-robin-like fashion. Signed-off-by: Domenico Cerasuolo Co-developed-by: Nhat Pham Signed-off-by: Nhat Pham --- include/linux/memcontrol.h | 5 + include/linux/zswap.h | 2 + mm/memcontrol.c | 2 + mm/swap.h | 3 +- mm/swap_state.c | 24 +++- mm/zswap.c | 252 +++++++++++++++++++++++++++++-------- 6 files changed, 227 insertions(+), 61 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 55c85f952afd..95f6c9e60ed1 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1187,6 +1187,11 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page) return NULL; } +static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg) +{ + return NULL; +} + static inline bool folio_memcg_kmem(struct folio *folio) { return false; diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 2a60ce39cfde..e571e393669b 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -15,6 +15,7 @@ bool zswap_load(struct folio *folio); void zswap_invalidate(int type, pgoff_t offset); void zswap_swapon(int type); void zswap_swapoff(int type); +void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); #else @@ -31,6 +32,7 @@ static inline bool zswap_load(struct folio *folio) static inline void zswap_invalidate(int type, pgoff_t offset) {} static inline void zswap_swapon(int type) {} static inline void zswap_swapoff(int type) {} +static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} #endif diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 6f7fc0101252..2ef49b471a16 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5640,6 +5640,8 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) page_counter_set_min(&memcg->memory, 0); page_counter_set_low(&memcg->memory, 0); + zswap_memcg_offline_cleanup(memcg); + memcg_offline_kmem(memcg); reparent_shrinker_deferred(memcg); wb_memcg_offline(memcg); diff --git a/mm/swap.h b/mm/swap.h index 73c332ee4d91..c0dc73e10e91 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -51,7 +51,8 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct swap_iocb **plug); struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx, - bool *new_page_allocated); + bool *new_page_allocated, + bool skip_if_exists); struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t flag, struct mempolicy *mpol, pgoff_t ilx); struct page *swapin_readahead(swp_entry_t entry, gfp_t flag, diff --git a/mm/swap_state.c b/mm/swap_state.c index 85d9e5806a6a..6c84236382f3 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -412,7 +412,8 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping, struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx, - bool *new_page_allocated) + bool *new_page_allocated, + bool skip_if_exists) { struct swap_info_struct *si; struct folio *folio; @@ -470,6 +471,17 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, if (err != -EEXIST) goto fail_put_swap; + /* + * Protect against a recursive call to __read_swap_cache_async() + * on the same entry waiting forever here because SWAP_HAS_CACHE + * is set but the folio is not the swap cache yet. This can + * happen today if mem_cgroup_swapin_charge_folio() below + * triggers reclaim through zswap, which may call + * __read_swap_cache_async() in the writeback path. + */ + if (skip_if_exists) + goto fail_put_swap; + /* * We might race against __delete_from_swap_cache(), and * stumble across a swap_map entry whose SWAP_HAS_CACHE @@ -537,7 +549,7 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, mpol = get_vma_policy(vma, addr, 0, &ilx); page = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false); mpol_cond_put(mpol); if (page_allocated) @@ -654,7 +666,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, /* Ok, do the async read-ahead now */ page = __read_swap_cache_async( swp_entry(swp_type(entry), offset), - gfp_mask, mpol, ilx, &page_allocated); + gfp_mask, mpol, ilx, &page_allocated, false); if (!page) continue; if (page_allocated) { @@ -672,7 +684,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, skip: /* The page was likely read above, so no need for plugging here */ page = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); return page; @@ -827,7 +839,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, pte_unmap(pte); pte = NULL; page = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false); if (!page) continue; if (page_allocated) { @@ -847,7 +859,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, skip: /* The page was likely read above, so no need for plugging here */ page = __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx, - &page_allocated); + &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); return page; diff --git a/mm/zswap.c b/mm/zswap.c index 2e691cd1a466..2654b0d214cc 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "swap.h" #include "internal.h" @@ -172,8 +173,9 @@ struct zswap_pool { struct work_struct shrink_work; struct hlist_node node; char tfm_name[CRYPTO_MAX_ALG_NAME]; - struct list_head lru; - spinlock_t lru_lock; + struct list_lru list_lru; + spinlock_t next_shrink_lock; + struct mem_cgroup *next_shrink; }; /* @@ -289,15 +291,42 @@ static void zswap_update_total_size(void) zswap_pool_total_size = total; } +/* should be called under RCU */ +static inline struct mem_cgroup *get_mem_cgroup_from_entry(struct zswap_entry *entry) +{ + return entry->objcg ? obj_cgroup_memcg(entry->objcg) : NULL; +} + +static inline int entry_to_nid(struct zswap_entry *entry) +{ + return page_to_nid(virt_to_page(entry)); +} + +void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) +{ + struct zswap_pool *pool; + + /* lock out zswap pools list modification */ + spin_lock(&zswap_pools_lock); + list_for_each_entry(pool, &zswap_pools, list) { + spin_lock(&pool->next_shrink_lock); + if (pool->next_shrink == memcg) + pool->next_shrink = + mem_cgroup_iter(NULL, pool->next_shrink, NULL, true); + spin_unlock(&pool->next_shrink_lock); + } + spin_unlock(&zswap_pools_lock); +} + /********************************* * zswap entry functions **********************************/ static struct kmem_cache *zswap_entry_cache; -static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp) +static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid) { struct zswap_entry *entry; - entry = kmem_cache_alloc(zswap_entry_cache, gfp); + entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid); if (!entry) return NULL; entry->refcount = 1; @@ -310,6 +339,61 @@ static void zswap_entry_cache_free(struct zswap_entry *entry) kmem_cache_free(zswap_entry_cache, entry); } +/********************************* +* lru functions +**********************************/ +static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) +{ + int nid = entry_to_nid(entry); + struct mem_cgroup *memcg; + + /* + * Note that it is safe to use rcu_read_lock() here, even in the face of + * concurrent memcg offlining. Thanks to the memcg->kmemcg_id indirection + * used in list_lru lookup, only two scenarios are possible: + * + * 1. list_lru_add() is called before memcg->kmemcg_id is updated. The + * new entry will be reparented to memcg's parent's list_lru. + * 2. list_lru_add() is called after memcg->kmemcg_id is updated. The + * new entry will be added directly to memcg's parent's list_lru. + * + * Similar reasoning holds for list_lru_del() and list_lru_putback(). + */ + rcu_read_lock(); + memcg = get_mem_cgroup_from_entry(entry); + /* will always succeed */ + list_lru_add(list_lru, &entry->lru, nid, memcg); + rcu_read_unlock(); +} + +static void zswap_lru_del(struct list_lru *list_lru, struct zswap_entry *entry) +{ + int nid = entry_to_nid(entry); + struct mem_cgroup *memcg; + + rcu_read_lock(); + memcg = get_mem_cgroup_from_entry(entry); + /* will always succeed */ + list_lru_del(list_lru, &entry->lru, nid, memcg); + rcu_read_unlock(); +} + +static void zswap_lru_putback(struct list_lru *list_lru, + struct zswap_entry *entry) +{ + int nid = entry_to_nid(entry); + spinlock_t *lock = &list_lru->node[nid].lock; + struct mem_cgroup *memcg; + + rcu_read_lock(); + memcg = get_mem_cgroup_from_entry(entry); + spin_lock(lock); + /* we cannot use list_lru_add here, because it increments node's lru count */ + list_lru_putback(list_lru, &entry->lru, nid, memcg); + spin_unlock(lock); + rcu_read_unlock(); +} + /********************************* * rbtree functions **********************************/ @@ -394,9 +478,7 @@ static void zswap_free_entry(struct zswap_entry *entry) if (!entry->length) atomic_dec(&zswap_same_filled_pages); else { - spin_lock(&entry->pool->lru_lock); - list_del(&entry->lru); - spin_unlock(&entry->pool->lru_lock); + zswap_lru_del(&entry->pool->list_lru, entry); zpool_free(zswap_find_zpool(entry), entry->handle); zswap_pool_put(entry->pool); } @@ -630,21 +712,15 @@ static void zswap_invalidate_entry(struct zswap_tree *tree, zswap_entry_put(tree, entry); } -static int zswap_reclaim_entry(struct zswap_pool *pool) +static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l, + spinlock_t *lock, void *arg) { - struct zswap_entry *entry; + struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); struct zswap_tree *tree; pgoff_t swpoffset; - int ret; + enum lru_status ret = LRU_REMOVED_RETRY; + int writeback_result; - /* Get an entry off the LRU */ - spin_lock(&pool->lru_lock); - if (list_empty(&pool->lru)) { - spin_unlock(&pool->lru_lock); - return -EINVAL; - } - entry = list_last_entry(&pool->lru, struct zswap_entry, lru); - list_del_init(&entry->lru); /* * Once the lru lock is dropped, the entry might get freed. The * swpoffset is copied to the stack, and entry isn't deref'd again @@ -652,28 +728,32 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) */ swpoffset = swp_offset(entry->swpentry); tree = zswap_trees[swp_type(entry->swpentry)]; - spin_unlock(&pool->lru_lock); + list_lru_isolate(l, item); + /* + * It's safe to drop the lock here because we return either + * LRU_REMOVED_RETRY or LRU_RETRY. + */ + spin_unlock(lock); /* Check for invalidate() race */ spin_lock(&tree->lock); - if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) { - ret = -EAGAIN; + if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) goto unlock; - } + /* Hold a reference to prevent a free during writeback */ zswap_entry_get(entry); spin_unlock(&tree->lock); - ret = zswap_writeback_entry(entry, tree); + writeback_result = zswap_writeback_entry(entry, tree); spin_lock(&tree->lock); - if (ret) { - /* Writeback failed, put entry back on LRU */ - spin_lock(&pool->lru_lock); - list_move(&entry->lru, &pool->lru); - spin_unlock(&pool->lru_lock); + if (writeback_result) { + zswap_reject_reclaim_fail++; + zswap_lru_putback(&entry->pool->list_lru, entry); + ret = LRU_RETRY; goto put_unlock; } + zswap_written_back_pages++; /* * Writeback started successfully, the page now belongs to the @@ -687,27 +767,76 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) zswap_entry_put(tree, entry); unlock: spin_unlock(&tree->lock); - return ret ? -EAGAIN : 0; + spin_lock(lock); + return ret; +} + +static int shrink_memcg(struct mem_cgroup *memcg) +{ + struct zswap_pool *pool; + int nid, shrunk = 0; + + /* + * Skip zombies because their LRUs are reparented and we would be + * reclaiming from the parent instead of the dead memcg. + */ + if (memcg && !mem_cgroup_online(memcg)) + return -ENOENT; + + pool = zswap_pool_current_get(); + if (!pool) + return -EINVAL; + + for_each_node_state(nid, N_NORMAL_MEMORY) { + unsigned long nr_to_walk = 1; + + shrunk += list_lru_walk_one(&pool->list_lru, nid, memcg, + &shrink_memcg_cb, NULL, &nr_to_walk); + } + zswap_pool_put(pool); + return shrunk ? 0 : -EAGAIN; } static void shrink_worker(struct work_struct *w) { struct zswap_pool *pool = container_of(w, typeof(*pool), shrink_work); + struct mem_cgroup *memcg; int ret, failures = 0; + /* global reclaim will select cgroup in a round-robin fashion. */ do { - ret = zswap_reclaim_entry(pool); - if (ret) { - zswap_reject_reclaim_fail++; - if (ret != -EAGAIN) - break; + spin_lock(&pool->next_shrink_lock); + memcg = pool->next_shrink = + mem_cgroup_iter(NULL, pool->next_shrink, NULL, true); + + /* full round trip */ + if (!memcg) { + spin_unlock(&pool->next_shrink_lock); if (++failures == MAX_RECLAIM_RETRIES) break; + + goto resched; } + + /* + * Acquire an extra reference to the iterated memcg in case the + * original reference is dropped by the zswap offlining callback. + */ + css_get(&memcg->css); + spin_unlock(&pool->next_shrink_lock); + + ret = shrink_memcg(memcg); + mem_cgroup_put(memcg); + + if (ret == -EINVAL) + break; + if (ret && ++failures == MAX_RECLAIM_RETRIES) + break; + +resched: cond_resched(); } while (!zswap_can_accept()); - zswap_pool_put(pool); } static struct zswap_pool *zswap_pool_create(char *type, char *compressor) @@ -765,11 +894,11 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) */ kref_init(&pool->kref); INIT_LIST_HEAD(&pool->list); - INIT_LIST_HEAD(&pool->lru); - spin_lock_init(&pool->lru_lock); + list_lru_init_memcg(&pool->list_lru, NULL); INIT_WORK(&pool->shrink_work, shrink_worker); zswap_pool_debug("created", pool); + spin_lock_init(&pool->next_shrink_lock); return pool; @@ -832,6 +961,13 @@ static void zswap_pool_destroy(struct zswap_pool *pool) cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); free_percpu(pool->acomp_ctx); + list_lru_destroy(&pool->list_lru); + + spin_lock(&pool->next_shrink_lock); + mem_cgroup_put(pool->next_shrink); + pool->next_shrink = NULL; + spin_unlock(&pool->next_shrink_lock); + for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) zpool_destroy_pool(pool->zpools[i]); kfree(pool); @@ -1079,7 +1215,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, /* try to allocate swap cache page */ mpol = get_task_policy(current); page = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol, - NO_INTERLEAVE_INDEX, &page_was_allocated); + NO_INTERLEAVE_INDEX, &page_was_allocated, true); if (!page) { ret = -ENOMEM; goto fail; @@ -1145,7 +1281,6 @@ static int zswap_writeback_entry(struct zswap_entry *entry, /* start writeback */ __swap_writepage(page, &wbc); put_page(page); - zswap_written_back_pages++; return ret; @@ -1202,6 +1337,7 @@ bool zswap_store(struct folio *folio) struct scatterlist input, output; struct crypto_acomp_ctx *acomp_ctx; struct obj_cgroup *objcg = NULL; + struct mem_cgroup *memcg = NULL; struct zswap_pool *pool; struct zpool *zpool; unsigned int dlen = PAGE_SIZE; @@ -1233,15 +1369,15 @@ bool zswap_store(struct folio *folio) zswap_invalidate_entry(tree, dupentry); } spin_unlock(&tree->lock); - - /* - * XXX: zswap reclaim does not work with cgroups yet. Without a - * cgroup-aware entry LRU, we will push out entries system-wide based on - * local cgroup limits. - */ objcg = get_obj_cgroup_from_folio(folio); - if (objcg && !obj_cgroup_may_zswap(objcg)) - goto reject; + if (objcg && !obj_cgroup_may_zswap(objcg)) { + memcg = get_mem_cgroup_from_objcg(objcg); + if (shrink_memcg(memcg)) { + mem_cgroup_put(memcg); + goto reject; + } + mem_cgroup_put(memcg); + } /* reclaim space if needed */ if (zswap_is_full()) { @@ -1258,7 +1394,7 @@ bool zswap_store(struct folio *folio) } /* allocate entry */ - entry = zswap_entry_cache_alloc(GFP_KERNEL); + entry = zswap_entry_cache_alloc(GFP_KERNEL, page_to_nid(page)); if (!entry) { zswap_reject_kmemcache_fail++; goto reject; @@ -1285,6 +1421,15 @@ bool zswap_store(struct folio *folio) if (!entry->pool) goto freepage; + if (objcg) { + memcg = get_mem_cgroup_from_objcg(objcg); + if (memcg_list_lru_alloc(memcg, &entry->pool->list_lru, GFP_KERNEL)) { + mem_cgroup_put(memcg); + goto put_pool; + } + mem_cgroup_put(memcg); + } + /* compress */ acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); @@ -1361,9 +1506,8 @@ bool zswap_store(struct folio *folio) zswap_invalidate_entry(tree, dupentry); } if (entry->length) { - spin_lock(&entry->pool->lru_lock); - list_add(&entry->lru, &entry->pool->lru); - spin_unlock(&entry->pool->lru_lock); + INIT_LIST_HEAD(&entry->lru); + zswap_lru_add(&entry->pool->list_lru, entry); } spin_unlock(&tree->lock); @@ -1376,6 +1520,7 @@ bool zswap_store(struct folio *folio) put_dstmem: mutex_unlock(acomp_ctx->mutex); +put_pool: zswap_pool_put(entry->pool); freepage: zswap_entry_cache_free(entry); @@ -1470,9 +1615,8 @@ bool zswap_load(struct folio *folio) zswap_invalidate_entry(tree, entry); folio_mark_dirty(folio); } else if (entry->length) { - spin_lock(&entry->pool->lru_lock); - list_move(&entry->lru, &entry->pool->lru); - spin_unlock(&entry->pool->lru_lock); + zswap_lru_del(&entry->pool->list_lru, entry); + zswap_lru_add(&entry->pool->list_lru, entry); } zswap_entry_put(tree, entry); spin_unlock(&tree->lock); From patchwork Mon Nov 6 18:31:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447295 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72128C0018A for ; Mon, 6 Nov 2023 18:32:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E9D86B0275; Mon, 6 Nov 2023 13:32:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1FA776B0277; Mon, 6 Nov 2023 13:32:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 023B86B0278; Mon, 6 Nov 2023 13:32:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D77F46B0275 for ; Mon, 6 Nov 2023 13:32:07 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AF03BA0819 for ; Mon, 6 Nov 2023 18:32:07 +0000 (UTC) X-FDA: 81428373894.18.E90D41B Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf27.hostedemail.com (Postfix) with ESMTP id CE7CE40015 for ; Mon, 6 Nov 2023 18:32:05 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bUZBp11s; spf=pass (imf27.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699295525; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CKTpKLeSBdUqUozne8dOLG7XHxiGHFR4EgqMwDuCKlY=; b=gbq5TFOd5d+jxpne4BmUc+Dwp5HFdB4oiAe7vEXVgjQ9Qsn1RP8GSrFkyeEzAlXz2Guz8q yHr5Da+9LiTVEIOdvaXHKL4tFVYlulWE+U6PFyVYltEA3l5RUQfMzljhdqeIoRXqoldm3v VBo7quE72Mt9yjphPgDBNRGqCDIgBZs= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bUZBp11s; spf=pass (imf27.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699295525; a=rsa-sha256; cv=none; b=7o9lcHUJksfpUq5lrnYDkyO+fVt4h7+5ISPQh0aWCdhpnk+GzneH2w5KdjgXz5lzJr5DNf x1GAwdfldOZNhLNAUMSizvXcOG4wjzJZjc/2ughL7YKH18EwJvi8paRQdZwfqSzQ/qERa2 vADI+RXaWlqAMth8p7GYTqJyqKqItjw= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6b20a48522fso4034558b3a.1 for ; Mon, 06 Nov 2023 10:32:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699295525; x=1699900325; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CKTpKLeSBdUqUozne8dOLG7XHxiGHFR4EgqMwDuCKlY=; b=bUZBp11s4KQ/wIxPfLzLiuu4s+7Axzag+D+V5y1wRLiKCF34e23gpTmniPiasaiN0q KIdeBwoIyFls+8yMUW/PlaLyWcCCK3ifhGmMAA51GCxOGSOG3UX60UwPxjOSlbExoZ4U CMSJdfFbmsRugXDW/tOO8JnMGduOjbPM0u/4sJ7wvmQmWlqXBHVS76WASFcdN5+sW028 /1Uw8jtSvXFLtoS1bl23HaukM93ss8bN0MlVWtgWBegwfWfkxtPPzi6rMXgZzZmF1nGf LXJSgI9MbAMtpzeY6T8QkAo2FYnLAz7+Y1+k+2COGa3IZXJsxvjjK+ZGDluwARw4UU6k OHBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699295525; x=1699900325; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CKTpKLeSBdUqUozne8dOLG7XHxiGHFR4EgqMwDuCKlY=; b=GkxN2iPcQkK/pKiqyfTCmsdis5HxNH2l6yqt1URRdFdWZzOj5pgQo21RkVwPw2Tg2v AOGGyxbtWSykIW2iwsXIOt17KkfIgaUMqfOlffHUFo4YPUO7HO1WLsKa+KLIiMOSYHs1 u9H+bdMckMDBAledHsWdlk8BnoxclBMMZo+oQnqxOThexidKftSs/CGC33U3ehiWHwSL eYITfYknpYPZ8rsKkfVqCI7Jvp2k+TDXFXV4uSy5jPFCXu0IRI70WbP5GoIn81UOrlue Xo1pPHkwhTbMFochbvkmGNnlVMEMas6rAvG2+iflc7705iugSWD11uEcdD1b+toKkEgi FWVQ== X-Gm-Message-State: AOJu0YydMphO2GWd8ujHhxeYZUw/GwaoR8+qhylOJpqCk1lpWN1dbKtl PoOclfx4uwUJ4y016gR+g8Y= X-Google-Smtp-Source: AGHT+IEajdjXne5TgvRI7wLvY5VaXI1+x16n9o7YmP2T+LGhqO+wFg1B1TlhqVvjHYOcgf56WSD/SA== X-Received: by 2002:a05:6a20:914e:b0:172:83b8:67f8 with SMTP id x14-20020a056a20914e00b0017283b867f8mr26904604pzc.29.1699295524682; Mon, 06 Nov 2023 10:32:04 -0800 (PST) Received: from localhost (fwdproxy-prn-119.fbsv.net. [2a03:2880:ff:77::face:b00c]) by smtp.gmail.com with ESMTPSA id j8-20020a170903024800b001c9d968563csm6161658plh.79.2023.11.06.10.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 10:32:04 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v5 4/6] mm: memcg: add per-memcg zswap writeback stat Date: Mon, 6 Nov 2023 10:31:57 -0800 Message-Id: <20231106183159.3562879-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106183159.3562879-1-nphamcs@gmail.com> References: <20231106183159.3562879-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: CE7CE40015 X-Rspam-User: X-Stat-Signature: d3uuar67er9wzsacdonyp1qanfs5h3xu X-Rspamd-Server: rspam01 X-HE-Tag: 1699295525-191251 X-HE-Meta: U2FsdGVkX19G+/iBqmWexSg76njWrH9heJbUKOT8nXVzXii4a+fBPpKSUtOfV9jeKoY2ht+ydSlVqTp0A0VBvRGzu5QXk2qW9r8xKhArQQOTvESE4MNRPgaH0CTXNKo+sq4gflNsUB4+cOWkuJiUSs9M/RyeQKauFNC3aRRyEZbDbRXRJ2ngOOD8++RpuNwsIj2m8QxR9327HA16nuwFmhgdbQnTXhgXBKQTAJcYKdU6/WOOTqPf+ZMDjMnryqhBykrnyZdNrS0zD2ikjoB1t+h6aH5B//C007/vvnj0EjVAg3jWpRIZAAiRI89PP4jv91mldy3miS71kDYMJnyj3g4zwmbPFA1u3ouisrBoC3IVw2kv+NfX/MOVPwGpSdb5LyFtYJrb1mgg1Faxb5ZS3yQYyHbnmuMDqETK63ebObtSdXvKh1rnh0/Stw2WnEgrdNXPfD4ZY6tBEwAIuhGNDv6TacCh/aQjJ6ba/wzup6mE6m+zoWF6nhNxj/2vbazlmEfJRWPgoZqD+ewccoNHy89J7KpgaPNKNI7/5G5Iruv3eNS3cktc/imNXiTx/8JFvg5mdxDQp37Lv0kieDtZNKf7kDGc4B3NxN3cBbB8TGW+WIA79f2sAQHgwWmaPHOLnCtIS+fxtoF09lC/ldU88r3OtQk17GwQp7XdmRSyuM5K5VkaqKXjO+/Nsf8Qa0BdrGlCd7rY6BCst3BlczjsZvpZteMzRd+rTL+paYsqseyw2CocepjF+pVPwjdd10rLrFord6I0/sjByN/iFYndLM9yLf07xIo0Fnm+93yVg1DaFrq2p1vIrwDGFnSnRL04AGJiTcHgPs+UtFIgEIyQtgWQeIiNCxeY/RHWFay6Wvy1YGnsv6Gc04mCVHus7YLScM0DezwNh73xLQd2v5LmhNmUAiYkPHjGjD+ITIj6qpY8PTwz/wBcNNui0ldvMVlLDWtIkn1eiGKZgK8+6UX KIsblyr0 8NIJp8JUxZ8xB77C1cZKTg9aWOcy/Zy3NCKqpjmGUc/XACDvmtJELgJs4wz5NDL/JQTvSZbMSc9gK1a13sOEE4wkY4XRYM1yuGannx3RivC+s6YP4dUr8lOqoLANhl6JCMxnfE+2zEqaS3DG8NECBnOWcYIgRLijx9B+sRDp3Y0elD5WSCv53O/F6kmz/tBkAF6Av8SxFkMXDQ0xNgsR8jFN2W7fAcXpIzALZNJID6TkpGYr0sFrbMzCAEC0IviwvZ3lsX2xy+xwdQgFCVVSd5q3H6x8/hCU5S0X3NQBXd5ReNNNWLl1ZX9mPZeA7sgbBTxhx+BzdYXm96Y854MmG6cirkgNExyNFLHEAAGcp5aPMRgWsqF4+AWQTxq52q4LUQpYZrcLHUL4rIFEIAiWh50ZDYFf3GRgQzP4LbD5ZLXQO+2zZGGgQH5n/f6IuT6cY/pqFtOjruQWzzw11SIrbAC3Opw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Domenico Cerasuolo Since zswap now writes back pages from memcg-specific LRUs, we now need a new stat to show writebacks count for each memcg. Suggested-by: Nhat Pham Signed-off-by: Domenico Cerasuolo Signed-off-by: Nhat Pham --- include/linux/vm_event_item.h | 1 + mm/memcontrol.c | 1 + mm/vmstat.c | 1 + mm/zswap.c | 3 +++ 4 files changed, 6 insertions(+) diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index 8abfa1240040..3153359c3841 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -145,6 +145,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, #ifdef CONFIG_ZSWAP ZSWPIN, ZSWPOUT, + ZSWP_WB, #endif #ifdef CONFIG_X86 DIRECT_MAP_LEVEL2_SPLIT, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2ef49b471a16..e43b5aba8efc 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -593,6 +593,7 @@ static const unsigned int memcg_vm_event_stat[] = { #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) ZSWPIN, ZSWPOUT, + ZSWP_WB, #endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE THP_FAULT_ALLOC, diff --git a/mm/vmstat.c b/mm/vmstat.c index 359460deb377..5e5572f3b456 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1401,6 +1401,7 @@ const char * const vmstat_text[] = { #ifdef CONFIG_ZSWAP "zswpin", "zswpout", + "zswp_wb", #endif #ifdef CONFIG_X86 "direct_map_level2_splits", diff --git a/mm/zswap.c b/mm/zswap.c index 2654b0d214cc..03ee41a8b884 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -755,6 +755,9 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o } zswap_written_back_pages++; + if (entry->objcg) + count_objcg_event(entry->objcg, ZSWP_WB); + /* * Writeback started successfully, the page now belongs to the * swapcache. Drop the entry from zswap - unless invalidate already From patchwork Mon Nov 6 18:31:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447296 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBDD1C4167B for ; Mon, 6 Nov 2023 18:32:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E5CD6B0278; Mon, 6 Nov 2023 13:32:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 26F156B027A; Mon, 6 Nov 2023 13:32:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09C256B0279; Mon, 6 Nov 2023 13:32:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DA06C6B0277 for ; Mon, 6 Nov 2023 13:32:08 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B29371CACA7 for ; Mon, 6 Nov 2023 18:32:08 +0000 (UTC) X-FDA: 81428373936.09.F32C11D Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf01.hostedemail.com (Postfix) with ESMTP id CB43A40023 for ; Mon, 6 Nov 2023 18:32:06 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VxREms7B; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699295526; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fQKZ6uKAgGpieDKxwUlilWEMw/a+RaeqsurH8iiKhxA=; b=fjrcE2//0TT43Kg99E3yhxiKxv0oDZrRcDKdeNIpYFJUi79p6jAOpW34++SLBwqA0eh3jF /F3/sQsCWgjyxQ1bIB3HTvPAyRQLGRO3HMpF0OVz00+aP0F1vEnPe2IxTdcOSXdul4q5YN rdIBMuwVi5Pd6qRcK/kF7z1/nGWd4hU= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VxREms7B; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699295526; a=rsa-sha256; cv=none; b=GBJ8EM//3uJFUwZ+JC9Rqf8EZjRW35o6z8XcW4VuxLEt9YLfokOTVwzz9zF3TuMJoxtprs xGOUGnJIN6zT1WI4WyJ8IL8RJiyiGSpsvdfzFJuZmm4Nvm6N1qSDL1C4OwH+7T+OLGMhuv ZkSFuoAEdoiMRcggHixXmBA8Yug4abE= Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-5bd306f86a8so2764380a12.0 for ; Mon, 06 Nov 2023 10:32:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699295525; x=1699900325; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fQKZ6uKAgGpieDKxwUlilWEMw/a+RaeqsurH8iiKhxA=; b=VxREms7BEXZ7lhuvWGuq/VxJGwCY2iYHj/AvXMqSRGb9Auc5y4jR70082BzRxtjWCZ uf7J3KuNpYQ2c+03j1Qdvas+DRhbD6PfSGF/PxBzJglQurwjWt+o+Ywy/YHZbIEsFbY/ dl9CT1qsNEmcwBU6ZiZOzAvwC0ev3CB2+bqwLaWbj4mrgS1uQkAnyKWr09o1hOcCXyTN 99Kvwng1b8eNNZ6yMnmkelaytQtmqR3rwp8rj34BTtJ3Z0A168Z8MGnGvF/xz27m8TIU csFmZ65eCqMJNck2Gv5Ko4VzW1uiX+/y4IxAa7rO6lZz8ERheo9Iiu7Vz4Emb3hs2LiI tvFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699295525; x=1699900325; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fQKZ6uKAgGpieDKxwUlilWEMw/a+RaeqsurH8iiKhxA=; b=SnlBnJj7kiAw3XlaZ6Q7mapYgbY40UEe+BEnoRhyNuyl9SwsxNTiwtYSJOTmt7gAJk W2byszluQXNLs62GZSD7BDv/w3DKOuMYbhuNBdfnsp/8KZuW6/W7xQDbsKv0HLFOSk16 p5FM4YdG4khCN5zcQcpt5wryg5kLETRL7OwMrHqUuTlSoZuR9VDx785DQQpwfmtGcUej QiuVcN3Y/5+Wnky08zwgIyaPnj18QyswGTICuDF/AGX0XCGaJQ5qL086eJF5WvYwFmUc mHJw5hiv8JyGATWVKi8I6Dy0pLxxh99pJTq2aDhtHxT9J8w59c9ZdY+NqQkWUMf9Qi6K X9ng== X-Gm-Message-State: AOJu0YxnqEe8i6WtyhPkbR8k+esgdZtbJhDG+3iLr/V64a9zT+fQAp7i 3MfyitdU54uQRiK7NJDiQJs= X-Google-Smtp-Source: AGHT+IE3472nXfqmh8O2FIqo+TXEJJPgsaUljC9xCLaQyRQ4pk9qek0sRUdOVNhr8wM4dIBARewIvQ== X-Received: by 2002:a05:6a20:7487:b0:15e:b8a1:57b0 with SMTP id p7-20020a056a20748700b0015eb8a157b0mr25302832pzd.39.1699295525583; Mon, 06 Nov 2023 10:32:05 -0800 (PST) Received: from localhost (fwdproxy-prn-007.fbsv.net. [2a03:2880:ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id r17-20020a62e411000000b006bc189378a7sm5859887pfh.196.2023.11.06.10.32.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 10:32:05 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v5 5/6] selftests: cgroup: update per-memcg zswap writeback selftest Date: Mon, 6 Nov 2023 10:31:58 -0800 Message-Id: <20231106183159.3562879-6-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106183159.3562879-1-nphamcs@gmail.com> References: <20231106183159.3562879-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: CB43A40023 X-Stat-Signature: 3oytx1mabwif1r4e6q76zo1j3j9pic5i X-Rspam-User: X-HE-Tag: 1699295526-976287 X-HE-Meta: U2FsdGVkX19AsdcC8El717bv9fvxEx1+L80N3+GcsjzCP+AG2xV6ibda80ZLSs5V6c6v3oqyNE/3/x5GIv04j8Op9nv5D79P/AJ79KTNPhS+D3wwXRa+cYul5dH3Lm5qL+HwbEmHm5Co7/tMoPaU1rCff+nwo99UgX8Y2S+qHJidjjeoKgG0w0wnKs1NHTShiw7N89nhOmqjq/td3KDY494Rvp64ZoishyWEpaly6emML+WQesFhb0W/AprPGdA5SFJ9tgx9CFyejgzQWAO/pYVjg71TUaq8z4Zqqjt4thQp0f1HO2HNH9jBcnqvoG3Tk5OB2csBa2Uklv4IdDC+L6JLPI3zNgxlWi/g+w5X2MmI15lItjWIpeeXKLhxnRILXAkIr2aH6PrctoE7VHXBvMVt4hCmpeyJM53RFB+6vM56oDJjd/i5rNf3JKj2jqs1QJbPnmikuuQO26jq85kd7y3KlG6X/5+FsRhHbpYUZS+BlE19CCwyGFw49LVV/NX6ZWHjjeHLQ/f4EPU2H4bGK1qDrN9fyr3koogIr2UgigyUHAjFJp/LOtkdaJnqgUb2eH3bpvArXHTuR+muBHJy+RDe2EHb5L6DEqliSWi+lyWfrR3NTIiUZTvOioYOiML7nCj0UP4PQdpSTtYJBqHkwRfwskED2GT4dKfaSYYqxE7PXxYwq4uvaiUXog8MYYDECTpLMeMK6DDUKOD42A1CKtDCs6le94RC6akldcWi/FhApplU+CxPAv6fHinu2T1JSM1xn3YHbGfFnPhZMhl9vmCHVaJQ4mEyhnDpBFLynJaMYbJm+Bi0UmiI/aP8RKWhFzG5pWqYPiqHqqfzCd9JmmL+0Aw6+/hU+8Lvs4T8m7HveMatJmX4lLH4dUudUyQzp/YKrBP65CKnYvfHgENF8RxQvtmUZnoR2TpHZvMnCzsQG7LpheHeUeKs7Gl4vw4+BvLM5ayR8dRbW4Hdblf 4OTBxpQb S+UFnGrRJcoHdC/rGPZcKSL/mcJm51cZsFGNcqpb3VbbIn0XBKEsqu8LxCnh1lJLUNhNsNUXBuJa0Ncp/AMecWPJmmzffTK0lPuOjwc/IjwbQx3SYv/eX3a4r0n+iXntzEtFjEtP9L5eqYxZoXsSk+fTr61Vv4kqlDKDT4LXTMkDI5SC5xHl1cOMebMOIx9hTaCHxZQ4rH987mzoBYA2CLPqyqb42znxHgB5mKiuG43Y/7qNXhyQfX2/PuE561SrzddU5U9P3s7hwjpjoa4gmZkr3mvLpyEWKOj2sSY4DMi+9onHWpjQBd3UGlfhm5qtyhrosAe4HQVImZratjd56vpvhqITFrN4jur3+vZnYa1VveTvT6P6eSWkqCKu0gMhFNAB/qFkOPIb+MDdJSyFQtHaBf7kG2blYULnDjkTzRxumpfw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.203550, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Domenico Cerasuolo The memcg-zswap self test is updated to adjust to the behavior change implemented by commit 87730b165089 ("zswap: make shrinking memcg-aware"), where zswap performs writeback for specific memcg. Signed-off-by: Domenico Cerasuolo Signed-off-by: Nhat Pham --- tools/testing/selftests/cgroup/test_zswap.c | 74 ++++++++++++++------- 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c index 49def87a909b..753a3b9de1ad 100644 --- a/tools/testing/selftests/cgroup/test_zswap.c +++ b/tools/testing/selftests/cgroup/test_zswap.c @@ -50,9 +50,9 @@ static int get_zswap_stored_pages(size_t *value) return read_int("/sys/kernel/debug/zswap/stored_pages", value); } -static int get_zswap_written_back_pages(size_t *value) +static int get_cg_wb_count(const char *cg) { - return read_int("/sys/kernel/debug/zswap/written_back_pages", value); + return cg_read_key_long(cg, "memory.stat", "zswp_wb"); } static int allocate_bytes(const char *cgroup, void *arg) @@ -68,45 +68,71 @@ static int allocate_bytes(const char *cgroup, void *arg) return 0; } +static char *setup_test_group_1M(const char *root, const char *name) +{ + char *group_name = cg_name(root, name); + + if (!group_name) + return NULL; + if (cg_create(group_name)) + goto fail; + if (cg_write(group_name, "memory.max", "1M")) { + cg_destroy(group_name); + goto fail; + } + return group_name; +fail: + free(group_name); + return NULL; +} + /* * When trying to store a memcg page in zswap, if the memcg hits its memory - * limit in zswap, writeback should not be triggered. - * - * This was fixed with commit 0bdf0efa180a("zswap: do not shrink if cgroup may - * not zswap"). Needs to be revised when a per memcg writeback mechanism is - * implemented. + * limit in zswap, writeback should affect only the zswapped pages of that + * memcg. */ static int test_no_invasive_cgroup_shrink(const char *root) { - size_t written_back_before, written_back_after; int ret = KSFT_FAIL; - char *test_group; + size_t control_allocation_size = MB(10); + char *control_allocation, *wb_group = NULL, *control_group = NULL; /* Set up */ - test_group = cg_name(root, "no_shrink_test"); - if (!test_group) - goto out; - if (cg_create(test_group)) + wb_group = setup_test_group_1M(root, "per_memcg_wb_test1"); + if (!wb_group) + return KSFT_FAIL; + if (cg_write(wb_group, "memory.zswap.max", "10K")) goto out; - if (cg_write(test_group, "memory.max", "1M")) + control_group = setup_test_group_1M(root, "per_memcg_wb_test2"); + if (!control_group) goto out; - if (cg_write(test_group, "memory.zswap.max", "10K")) + + /* Push some test_group2 memory into zswap */ + if (cg_enter_current(control_group)) goto out; - if (get_zswap_written_back_pages(&written_back_before)) + control_allocation = malloc(control_allocation_size); + for (int i = 0; i < control_allocation_size; i += 4095) + control_allocation[i] = 'a'; + if (cg_read_key_long(control_group, "memory.stat", "zswapped") < 1) goto out; - /* Allocate 10x memory.max to push memory into zswap */ - if (cg_run(test_group, allocate_bytes, (void *)MB(10))) + /* Allocate 10x memory.max to push wb_group memory into zswap and trigger wb */ + if (cg_run(wb_group, allocate_bytes, (void *)MB(10))) goto out; - /* Verify that no writeback happened because of the memcg allocation */ - if (get_zswap_written_back_pages(&written_back_after)) - goto out; - if (written_back_after == written_back_before) + /* Verify that only zswapped memory from gwb_group has been written back */ + if (get_cg_wb_count(wb_group) > 0 && get_cg_wb_count(control_group) == 0) ret = KSFT_PASS; out: - cg_destroy(test_group); - free(test_group); + cg_enter_current(root); + if (control_group) { + cg_destroy(control_group); + free(control_group); + } + cg_destroy(wb_group); + free(wb_group); + if (control_allocation) + free(control_allocation); return ret; } From patchwork Mon Nov 6 18:31:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447297 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01A84C41535 for ; Mon, 6 Nov 2023 18:32:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61A076B0279; Mon, 6 Nov 2023 13:32:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A0E26B027A; Mon, 6 Nov 2023 13:32:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 37FDA6B027B; Mon, 6 Nov 2023 13:32:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 16AE06B0279 for ; Mon, 6 Nov 2023 13:32:10 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E7E1D4084A for ; Mon, 6 Nov 2023 18:32:09 +0000 (UTC) X-FDA: 81428373978.22.2339A9B Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf08.hostedemail.com (Postfix) with ESMTP id 0862C160004 for ; Mon, 6 Nov 2023 18:32:07 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G3B6RFKl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699295528; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=N8ph0qQQ5H5bJLHYkI6jb4RvQ0Lcli/vqoPRF5tf8vc=; b=trqjnDvBBaVzsNTRLS84mmQOxsCVOTGGIutFaAGIM13izx+xxJ0bgyLLZf/cXYT0ZK7LHM cJ9ogVdchCiYy26FDo2aUxR1E1ZCmTbrQG1KXkrTYwOZ5fCIXyx3hhAWfemvdGvRUXF/NH 0qJ6D4adN0Udx/lBDMEIf6rKwtP1ZAI= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G3B6RFKl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699295528; a=rsa-sha256; cv=none; b=Z3wknmjuWydHQTdPZgkv9drnwA6K7o1tCbMrDeh/k71qAi51PVumVovI2ZnbjyqkXiZKai APn27WXqxzJvnMnUD+YFVnfJCQGegAVbVFX3X9+Eb7BQJP6UUtm2Zkc0C1fsLVmCNgEpCH NCxO6bNNmjSTqs+rHXU6VdfoAWyhL0A= Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6bd73395bceso3855932b3a.0 for ; Mon, 06 Nov 2023 10:32:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699295527; x=1699900327; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N8ph0qQQ5H5bJLHYkI6jb4RvQ0Lcli/vqoPRF5tf8vc=; b=G3B6RFKl+NEYOVc1MvbaUuy37MldExS7RgCsZ1kaESOwUPraTIJ9+cmI5xm4kZmwvl jMDNp+L01WHgbx8RFDi5dZV6hdTJ2FxUUBYXN3FyILwblUh/TKmtHPASlWGud2XA4ddw R5+91+a9qFSvN30kOLqlgmqOJTu1+YWKv4msQojHGsSzD7nCDM7DSOT1iU8UXYBm1y9a PwPczYhRv6DND4B3PbAJldJEadZ9zZeF1nGRkCQqZGRK22YmDNRaSxe2htO4RCPXWWFP zOV0eYGR+XRxigx2sA27JlyE6phXIRYzycmHmYw1ZDv4m4SiKd4rexJa+Mk0acHX9kvk WQ1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699295527; x=1699900327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N8ph0qQQ5H5bJLHYkI6jb4RvQ0Lcli/vqoPRF5tf8vc=; b=qE9wVgR6EsXJp2C5w635nqJ1nPY1rT9hMjeJIH1gryOD91eG9tgNP3azeuk/eSe+4B VXQKv5NPZpl8cqGJCMilccPByA4n8J8qwE7eTBspQXQt9XKUCdL2/SBcLN8eseL0/AP6 1RqoWdbkC6si5C6w5njaSjnHVKk/rGbbRcyExKtJeVIld2tJG9plOU/eAXxBc8dbigGH qV1ommGoGNAT+Mmb+wrK6aZ/WM5ycdWqgU3xUL5/T1TangKGQQz7XNiLvh2HIEL4LOJW HkGosy3/Ye+EgFHCWmjSCRVvAgknSzcQF11fSQmkt87KZDVNLakvI1lbhtpeHeYRqr8X 2JlA== X-Gm-Message-State: AOJu0YwJPuRnmaUUyPnk1HnPIcRz7eXs58TaJXyNNOr+HiEarVElzZEZ GXhAsP4X2gZ9sima7fGAM+Y= X-Google-Smtp-Source: AGHT+IH2T1QbVRMdMtauNrsjzt3Q4b+qJkYfrV6f0fnZ1Hn1uZuzo56kDPFZIEaY+sGMiHJuYXoV1w== X-Received: by 2002:a05:6a20:4406:b0:151:35ad:f331 with SMTP id ce6-20020a056a20440600b0015135adf331mr324995pzb.14.1699295526484; Mon, 06 Nov 2023 10:32:06 -0800 (PST) Received: from localhost (fwdproxy-prn-016.fbsv.net. [2a03:2880:ff:10::face:b00c]) by smtp.gmail.com with ESMTPSA id m11-20020a62f20b000000b006887be16675sm5829072pfh.205.2023.11.06.10.32.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 10:32:06 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v5 6/6] zswap: shrinks zswap pool based on memory pressure Date: Mon, 6 Nov 2023 10:31:59 -0800 Message-Id: <20231106183159.3562879-7-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106183159.3562879-1-nphamcs@gmail.com> References: <20231106183159.3562879-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0862C160004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 9p8y99b7xbda4mwpc67gd3rxhr9yaoab X-HE-Tag: 1699295527-363389 X-HE-Meta: U2FsdGVkX1+XdcPdHc6qGFvFHRxRvXcGKXTJ15+9EbOgL/cJ88OesWI70fbUalAaKC+8HJ11jsMmA7lBbNXGP8FEtigFuJRY2yu9ZlN8wNkFTgwH+8lI8OOWBHLT/KAycQkL3o5QpVimmJj8QJML7ySjjOs6ZOP6d44nEHOjOBp4wJiHL/f+NM7shvFjXCCQnkMwp6tbXUwJEIShzrDv/Yi9huET/mKRLi4ybEEUAI7ZxgNNfeQT9erAdD344dlB1MdNQ4+Cl2tUe0nQAxKrmcUX2zGTcwf/MyMW1rX8orzcNKyL7kplMNx9AwmPgUB/YWZlqGcJS97uAhLwVv75/IqbfSPkmqlmgu9yFBXFrn3/SlkQfQDlTYEKIzEv/g+ubc96nmQYGBvqRFSPPHpb7cwEmbQqmCqKp/xr1prVXa1mOJImfESUqu+U1AnaejVs7r2MD02XdJuY7D+uKFVmCs1iUud9tI7GWiMB/zM4YII+YoHS7sCt5NAk4AUhYWlC2Ac10KFIuunkIhI/d4pgTL2yiOzsPe44iA1cAMbY8/Gtn3PMRx9vsR75lvM3xcP6T5FI9TuGaYFt8Jy9gLO4MMoKWP9tsEL1tFT+JErx+FXn8sEkpxtmoahvMSCASvvA/kuyEWs6n5jDu8C8Fa3U2yrR4pTWd022kJSDvDdeYP5KBsTcGUXXPgGqMld8Wt1zAkJVW6bKdR9u4eul1P3tCM66Bn2N1HHz7d57iYGOt0xAhdyioQ1xLBnv2pjkOD8tOOQ9k1HAXNWPSdBYtegwOVmYvKxZZCjrwg4DxnT2off0ulZCCdrdJfi7zRw28YTsuyKM5+IkLyb0UgP1yMj4Nn4sv+74G4n5W3dLAv2p6AMZbLgb7WTVCQiM5ScPsBkVrhVtuTQzRjG90PA9wH5ReVA54Ppt3CQfdNpDkOuO75VU5O6+iUxvAHovEhA1w2q7APoP9SZGilCGVn1+rHD ClBbpEGc 6vpUTwFxMHyRL4VZRKJk+j+kRkDvN2B8rkkOrmLD4606dNFwA8LquB6pLevaY4qktSO9mXbgXlcvI8ZmSlqTjJZ7kQ96dmCiI4T65pn8yBv4b0G2J4dhtd6y6t9U4SdLgrTN5hg8OkXApem6w5vkb7eFIYMlTzAnqN2fb4k/jVBynf1wM22zHP9PFdI4cBC6oJuY2VsGJJdJabIO5FBROdDLcfFG2GN7QAbBytiGGbSmKsH7J4M6ZubIIwkbSZgHznUGZ9zuO+1jJtF3mL7cXL2oUdJ/FhXq9hLPVKj279Hc86SOfZhloiUt3iZn16BbaDMEO3TuKP5pWjfQKEhpzvDBV7Y52LAMPc9VDmYTtdwh4W4RFqrxgejB7PYNNaSHImZpYSRBGg9lAmq/68rPJoRsI0A1EER47dxwVtoIk05YAj3vnqQ0Z4cdRCQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Currently, we only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages). This patch implements a memcg- and NUMA-aware shrinker for zswap, that is initiated when there is memory pressure. The shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis. Furthermore, to make it more robust for many workloads and prevent overshrinking (i.e evicting warm pages that might be refaulted into memory), we build in the following heuristics: * Estimate the number of warm pages residing in zswap, and attempt to protect this region of the zswap LRU. * Scale the number of freeable objects by an estimate of the memory saving factor. The better zswap compresses the data, the fewer pages we will evict to swap (as we will otherwise incur IO for relatively small memory saving). * During reclaim, if the shrinker encounters a page that is also being brought into memory, the shrinker will cautiously terminate its shrinking action, as this is a sign that it is touching the warmer region of the zswap LRU. As a proof of concept, we ran the following synthetic benchmark: build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds. Signed-off-by: Nhat Pham --- Documentation/admin-guide/mm/zswap.rst | 7 + include/linux/mmzone.h | 2 + include/linux/zswap.h | 25 +++- mm/mmzone.c | 1 + mm/swap_state.c | 2 + mm/zswap.c | 177 ++++++++++++++++++++++++- 6 files changed, 208 insertions(+), 6 deletions(-) diff --git a/Documentation/admin-guide/mm/zswap.rst b/Documentation/admin-guide/mm/zswap.rst index 45b98390e938..522ae22ccb84 100644 --- a/Documentation/admin-guide/mm/zswap.rst +++ b/Documentation/admin-guide/mm/zswap.rst @@ -153,6 +153,13 @@ attribute, e. g.:: Setting this parameter to 100 will disable the hysteresis. +When there is a sizable amount of cold memory residing in the zswap pool, it +can be advantageous to proactively write these cold pages to swap and reclaim +the memory for other use cases. By default, the zswap shrinker is disabled. +User can enable it as follows: + + echo Y > /sys/module/zswap/parameters/shrinker_enabled + A debugfs interface is provided for various statistic about pool size, number of pages stored, same-value filled pages and various counters for the reasons pages are rejected. diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 12f31633be05..633afdb96c40 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -22,6 +22,7 @@ #include #include #include +#include #include /* Free memory management - zoned buddy allocator. */ @@ -637,6 +638,7 @@ struct lruvec { #ifdef CONFIG_MEMCG struct pglist_data *pgdat; #endif + struct zswap_lruvec_state zswap_lruvec_state; }; /* Isolate for asynchronous migration */ diff --git a/include/linux/zswap.h b/include/linux/zswap.h index e571e393669b..cbd373ba88d2 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -5,20 +5,40 @@ #include #include +struct lruvec; + extern u64 zswap_pool_total_size; extern atomic_t zswap_stored_pages; #ifdef CONFIG_ZSWAP +struct zswap_lruvec_state { + /* + * Number of pages in zswap that should be protected from the shrinker. + * This number is an estimate of the following counts: + * + * a) Recent page faults. + * b) Recent insertion to the zswap LRU. This includes new zswap stores, + * as well as recent zswap LRU rotations. + * + * These pages are likely to be warm, and might incur IO if the are written + * to swap. + */ + atomic_long_t nr_zswap_protected; +}; + bool zswap_store(struct folio *folio); bool zswap_load(struct folio *folio); void zswap_invalidate(int type, pgoff_t offset); void zswap_swapon(int type); void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); - +void zswap_lruvec_state_init(struct lruvec *lruvec); +void zswap_lruvec_swapin(struct page *page); #else +struct zswap_lruvec_state {}; + static inline bool zswap_store(struct folio *folio) { return false; @@ -33,7 +53,8 @@ static inline void zswap_invalidate(int type, pgoff_t offset) {} static inline void zswap_swapon(int type) {} static inline void zswap_swapoff(int type) {} static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} - +static inline void zswap_lruvec_init(struct lruvec *lruvec) {} +static inline void zswap_lruvec_swapin(struct page *page) {} #endif #endif /* _LINUX_ZSWAP_H */ diff --git a/mm/mmzone.c b/mm/mmzone.c index b594d3f268fe..c01896eca736 100644 --- a/mm/mmzone.c +++ b/mm/mmzone.c @@ -78,6 +78,7 @@ void lruvec_init(struct lruvec *lruvec) memset(lruvec, 0, sizeof(struct lruvec)); spin_lock_init(&lruvec->lru_lock); + zswap_lruvec_state_init(lruvec); for_each_lru(lru) INIT_LIST_HEAD(&lruvec->lists[lru]); diff --git a/mm/swap_state.c b/mm/swap_state.c index 6c84236382f3..94ed2d508db0 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -687,6 +687,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); + zswap_lruvec_swapin(page); return page; } @@ -862,6 +863,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); + zswap_lruvec_swapin(page); return page; } diff --git a/mm/zswap.c b/mm/zswap.c index 03ee41a8b884..260e01180ee0 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -146,6 +146,10 @@ module_param_named(exclusive_loads, zswap_exclusive_loads_enabled, bool, 0644); /* Number of zpools in zswap_pool (empirically determined for scalability) */ #define ZSWAP_NR_ZPOOLS 32 +/* Enable/disable memory pressure-based shrinker. */ +static bool zswap_shrinker_enabled; +module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644); + /********************************* * data structures **********************************/ @@ -176,6 +180,8 @@ struct zswap_pool { struct list_lru list_lru; spinlock_t next_shrink_lock; struct mem_cgroup *next_shrink; + struct shrinker *shrinker; + atomic_t nr_stored; }; /* @@ -274,17 +280,26 @@ static bool zswap_can_accept(void) DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE); } +static u64 get_zswap_pool_size(struct zswap_pool *pool) +{ + u64 pool_size = 0; + int i; + + for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) + pool_size += zpool_get_total_size(pool->zpools[i]); + + return pool_size; +} + static void zswap_update_total_size(void) { struct zswap_pool *pool; u64 total = 0; - int i; rcu_read_lock(); list_for_each_entry_rcu(pool, &zswap_pools, list) - for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) - total += zpool_get_total_size(pool->zpools[i]); + total += get_zswap_pool_size(pool); rcu_read_unlock(); @@ -339,13 +354,34 @@ static void zswap_entry_cache_free(struct zswap_entry *entry) kmem_cache_free(zswap_entry_cache, entry); } +/********************************* +* zswap lruvec functions +**********************************/ +void zswap_lruvec_state_init(struct lruvec *lruvec) +{ + atomic_long_set(&lruvec->zswap_lruvec_state.nr_zswap_protected, 0); +} + +void zswap_lruvec_swapin(struct page *page) +{ + struct lruvec *lruvec; + + if (page) { + lruvec = folio_lruvec(page_folio(page)); + atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); + } +} + /********************************* * lru functions **********************************/ static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) { + atomic_long_t *nr_zswap_protected; + unsigned long lru_size, old, new; int nid = entry_to_nid(entry); struct mem_cgroup *memcg; + struct lruvec *lruvec; /* * Note that it is safe to use rcu_read_lock() here, even in the face of @@ -363,6 +399,19 @@ static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) memcg = get_mem_cgroup_from_entry(entry); /* will always succeed */ list_lru_add(list_lru, &entry->lru, nid, memcg); + + /* Update the protection area */ + lru_size = list_lru_count_one(list_lru, nid, memcg); + lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); + nr_zswap_protected = &lruvec->zswap_lruvec_state.nr_zswap_protected; + old = atomic_long_inc_return(nr_zswap_protected); + /* + * Decay to avoid overflow and adapt to changing workloads. + * This is based on LRU reclaim cost decaying heuristics. + */ + do { + new = old > lru_size / 4 ? old / 2 : old; + } while (!atomic_long_try_cmpxchg(nr_zswap_protected, &old, new)); rcu_read_unlock(); } @@ -384,6 +433,7 @@ static void zswap_lru_putback(struct list_lru *list_lru, int nid = entry_to_nid(entry); spinlock_t *lock = &list_lru->node[nid].lock; struct mem_cgroup *memcg; + struct lruvec *lruvec; rcu_read_lock(); memcg = get_mem_cgroup_from_entry(entry); @@ -391,6 +441,10 @@ static void zswap_lru_putback(struct list_lru *list_lru, /* we cannot use list_lru_add here, because it increments node's lru count */ list_lru_putback(list_lru, &entry->lru, nid, memcg); spin_unlock(lock); + + lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(entry_to_nid(entry))); + /* increment the protection area to account for the LRU rotation. */ + atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); rcu_read_unlock(); } @@ -480,6 +534,7 @@ static void zswap_free_entry(struct zswap_entry *entry) else { zswap_lru_del(&entry->pool->list_lru, entry); zpool_free(zswap_find_zpool(entry), entry->handle); + atomic_dec(&entry->pool->nr_stored); zswap_pool_put(entry->pool); } zswap_entry_cache_free(entry); @@ -521,6 +576,95 @@ static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, return entry; } +/********************************* +* shrinker functions +**********************************/ +static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l, + spinlock_t *lock, void *arg); + +static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, + struct shrink_control *sc) +{ + struct lruvec *lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid)); + unsigned long shrink_ret, nr_protected, lru_size; + struct zswap_pool *pool = shrinker->private_data; + bool encountered_page_in_swapcache = false; + + nr_protected = + atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); + lru_size = list_lru_shrink_count(&pool->list_lru, sc); + + /* + * Abort if the shrinker is disabled or if we are shrinking into the + * protected region. + */ + if (!zswap_shrinker_enabled || nr_protected >= lru_size - sc->nr_to_scan) { + sc->nr_scanned = 0; + return SHRINK_STOP; + } + + shrink_ret = list_lru_shrink_walk(&pool->list_lru, sc, &shrink_memcg_cb, + &encountered_page_in_swapcache); + + if (encountered_page_in_swapcache) + return SHRINK_STOP; + + return shrink_ret ? shrink_ret : SHRINK_STOP; +} + +static unsigned long zswap_shrinker_count(struct shrinker *shrinker, + struct shrink_control *sc) +{ + struct zswap_pool *pool = shrinker->private_data; + struct mem_cgroup *memcg = sc->memcg; + struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid)); + unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; + +#ifdef CONFIG_MEMCG_KMEM + cgroup_rstat_flush(memcg->css.cgroup); + nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT; + nr_stored = memcg_page_state(memcg, MEMCG_ZSWAPPED); +#else + /* use pool stats instead of memcg stats */ + nr_backing = get_zswap_pool_size(pool) >> PAGE_SHIFT; + nr_stored = atomic_read(&pool->nr_stored); +#endif + + if (!zswap_shrinker_enabled || !nr_stored) + return 0; + + nr_protected = + atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); + nr_freeable = list_lru_shrink_count(&pool->list_lru, sc); + /* + * Subtract the lru size by an estimate of the number of pages + * that should be protected. + */ + nr_freeable = nr_freeable > nr_protected ? nr_freeable - nr_protected : 0; + + /* + * Scale the number of freeable pages by the memory saving factor. + * This ensures that the better zswap compresses memory, the fewer + * pages we will evict to swap (as it will otherwise incur IO for + * relatively small memory saving). + */ + return mult_frac(nr_freeable, nr_backing, nr_stored); +} + +static void zswap_alloc_shrinker(struct zswap_pool *pool) +{ + pool->shrinker = + shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE, "mm-zswap"); + if (!pool->shrinker) + return; + + pool->shrinker->private_data = pool; + pool->shrinker->scan_objects = zswap_shrinker_scan; + pool->shrinker->count_objects = zswap_shrinker_count; + pool->shrinker->batch = 0; + pool->shrinker->seeks = DEFAULT_SEEKS; +} + /********************************* * per-cpu code **********************************/ @@ -716,6 +860,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o spinlock_t *lock, void *arg) { struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); + bool *encountered_page_in_swapcache = (bool *)arg; struct zswap_tree *tree; pgoff_t swpoffset; enum lru_status ret = LRU_REMOVED_RETRY; @@ -751,6 +896,17 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o zswap_reject_reclaim_fail++; zswap_lru_putback(&entry->pool->list_lru, entry); ret = LRU_RETRY; + + /* + * Encountering a page already in swap cache is a sign that we are shrinking + * into the warmer region. We should terminate shrinking (if we're in the dynamic + * shrinker context). + */ + if (writeback_result == -EEXIST && encountered_page_in_swapcache) { + ret = LRU_SKIP; + *encountered_page_in_swapcache = true; + } + goto put_unlock; } zswap_written_back_pages++; @@ -890,6 +1046,11 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) &pool->node); if (ret) goto error; + + zswap_alloc_shrinker(pool); + if (!pool->shrinker) + goto error; + pr_debug("using %s compressor\n", pool->tfm_name); /* being the current pool takes 1 ref; this func expects the @@ -897,14 +1058,20 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) */ kref_init(&pool->kref); INIT_LIST_HEAD(&pool->list); - list_lru_init_memcg(&pool->list_lru, NULL); + if (list_lru_init_memcg(&pool->list_lru, pool->shrinker)) + goto lru_fail; + shrinker_register(pool->shrinker); INIT_WORK(&pool->shrink_work, shrink_worker); + atomic_set(&pool->nr_stored, 0); zswap_pool_debug("created", pool); spin_lock_init(&pool->next_shrink_lock); return pool; +lru_fail: + list_lru_destroy(&pool->list_lru); + shrinker_free(pool->shrinker); error: if (pool->acomp_ctx) free_percpu(pool->acomp_ctx); @@ -962,6 +1129,7 @@ static void zswap_pool_destroy(struct zswap_pool *pool) zswap_pool_debug("destroying", pool); + shrinker_free(pool->shrinker); cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); free_percpu(pool->acomp_ctx); list_lru_destroy(&pool->list_lru); @@ -1511,6 +1679,7 @@ bool zswap_store(struct folio *folio) if (entry->length) { INIT_LIST_HEAD(&entry->lru); zswap_lru_add(&entry->pool->list_lru, entry); + atomic_inc(&entry->pool->nr_stored); } spin_unlock(&tree->lock);