From patchwork Mon Nov 27 23:45: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: 13470416 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 2F913C4167B for ; Mon, 27 Nov 2023 23:46:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF11B6B0254; Mon, 27 Nov 2023 18:46:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA2096B0256; Mon, 27 Nov 2023 18:46:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A426A6B0257; Mon, 27 Nov 2023 18:46:05 -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 924946B0254 for ; Mon, 27 Nov 2023 18:46:05 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 48EF24012D for ; Mon, 27 Nov 2023 23:46:05 +0000 (UTC) X-FDA: 81505369890.24.0B6E7A8 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf12.hostedemail.com (Postfix) with ESMTP id 19C8D4049B for ; Mon, 27 Nov 2023 23:46:02 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MFQC4TCr; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.175 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=1701128763; 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=fNiCSzvNV1ziGwJa7LtT47VBl/50ZnVtPtDE/RNEfyE=; b=3Ehn31wkOrCQNbS93FJKujaYDiNi8sCJ4pQK7ATGPepOkd0xnr6C2l3GOydknSDicXPh9L DHDqmfu5DJRy53nYKVUKoy3JlhQWXXmNi4vvwJ/D2oSPRIOHLE0XU1ykpwsNbfIZGE9PMs JuMEKg3xS5pSrrKx3cknFv4eQIchDvw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MFQC4TCr; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701128763; a=rsa-sha256; cv=none; b=MoUMvCa4jNVmUw9ZjWeQmBCV2BAejvXj60Ftuc5BeJYxifICXpRHJZwOMetjaE9zgSPgLY C4y/DiGtOztr0wXFKtuswf/Bw3z2jaBhIC38sPdIITWhHnl+zQUOExVUYykl78DTx/XKvp 1jZJUcNk1UTmYbR4yzE+W4HaDY0psM0= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1ce627400f6so38737975ad.2 for ; Mon, 27 Nov 2023 15:46:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701128762; x=1701733562; 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=fNiCSzvNV1ziGwJa7LtT47VBl/50ZnVtPtDE/RNEfyE=; b=MFQC4TCrc5ZlHwEGyWp0ta8YUMPyBCIziUtpeMSvIvHQpzOdOdGKQ4APlSOlKVrLQS L0HLW7EcO2J4BjUJrA5J1MeMcoECPS3kNhSPHKbAmaUavhiteTvgY6gc3A9YecQv1pro SLj9naM5wioN7BRBM+jAPHdNWxm2JVNjNWnNgiD59rPW9AUPaCwFL0V7wHXUPE13Y3cR CSLc+nBcs43xlut1CaUupqVUtMsFPUE1dXOlEwCjqGfzqV7IZWfeF/ND5ocYHdA/jqja sNkhQpcp3B7WJ3M6kEIMOlY3OH1xnldhb/NDBbGS47BIZ5D1m1bPESMkQSvR+3zYcKQV KaSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701128762; x=1701733562; 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=fNiCSzvNV1ziGwJa7LtT47VBl/50ZnVtPtDE/RNEfyE=; b=Kf0yvLuwQwEia3et8kC91rc/2XoIvcETYc8tT4fJFrEURdqy5rbphmF1znlPnz1MhF Yhvv2+t+B9hWC6TuyfDyr6d4BLJYZZuKa/EPHXXax9KYbtOekxZXzk0VdDrX0s+Wi8nR rOK0JkZGgEPRwHpI9gmYJx7liC/onvKupFeUZpgEY5ihVwXsBX3ZWLbUoiT0iHgEXs8e fGkuXJQ/DcQpvNYPR75lncFakVeYJ4Lsiztm4/6SCNzzigiP3yAfmcXKVrqfxlqTeSKB bM1ODRt7dAr2C8fgq7V3iYb3jRJeIQxhFuyc0NbN4BOaTLgUNixD/J/yGHx7e9fQ1Y/z o3Fg== X-Gm-Message-State: AOJu0YyQMv7LF3f5/M0W17ugsFMesZspiiMUq9XAqsq3Xh31xGhUOGKT SuSQKvXGQLreM4f0LnZY7Eo= X-Google-Smtp-Source: AGHT+IGg0b2BurabLi4x78Yv3t0y3lEvWfl/iyf/uPgTp5xjo5vBmJ99c7oNP5NvYSoCjl7FotlyHQ== X-Received: by 2002:a17:902:ab94:b0:1cf:ba11:1adf with SMTP id f20-20020a170902ab9400b001cfba111adfmr7677488plr.67.1701128761804; Mon, 27 Nov 2023 15:46:01 -0800 (PST) Received: from localhost (fwdproxy-prn-024.fbsv.net. [2a03:2880:ff:18::face:b00c]) by smtp.gmail.com with ESMTPSA id p18-20020a170902a41200b001c74df14e6fsm8818313plq.284.2023.11.27.15.46.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 15:46: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 v7 1/6] list_lru: allows explicit memcg and NUMA node selection Date: Mon, 27 Nov 2023 15:45:55 -0800 Message-Id: <20231127234600.2971029-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127234600.2971029-1-nphamcs@gmail.com> References: <20231127234600.2971029-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 19C8D4049B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: poqcg7531an7cshjwujnqhayape1r3x9 X-HE-Tag: 1701128762-18620 X-HE-Meta: U2FsdGVkX1/JIdL6opb/y4Q9cZ7Z93ZFogmKBf3mDdAF1mDn++1iYNQbMx9tpAtxa5yH7Yd0TUMy+xfeIckXr0O8Qf6V3W7iIQxTwV/lLPjzVsez2iz8ZPuBpF0Y/v9Ew0Gf4/239TE6fyVq53GTVu5+3aP4FsCIou8GxVW8HeWH2X0NrJE/V8cDzgMzpaJP4FhEqb3hlxO43y+kuvkJU4ZvAF8lec402x5zv//aGaHZGXVQcrz1ctQOsU4diDC2JCSUuvQnLkhOENzcU9RxhtoIVwZeMWvQDKU3/5aVgfFZV9LO/jWKvMff2MgIl6p45mX3fc4XBBJLV7R67VxSuezyl0eBrcR4oFpwcZ7QFwkMQdPi6xlb/2ILUUh5eMYH89NuxTo2F6HbuSgFV1mk0lWhIXZL9KSTb9hY0+yHKsxqnBm0+SqheoXo+XtY6RpiDngAhlJz5SHYlxJMQMyiK7CMsFNCAP4yYq5j8kISaT6IR7CZwhYrP3CcFp0ruPclJ4avLgi2PjMHUNCc3SLWU60p8y9a5qnhWOMJ09OZGwP3s3hKr82DBRpLLVu2IUznMimBZUP/sDu3eG9a/NVdZnCa6jpUq3nee+J0zbUQxQH26dh0cHWm2nESIfdSRIqY3FlohF0Ul84PNaWkZZ7Y9cS6UE1GCGhr9oOqAlW6+YBl2h319Bq3hULGOe30iWWm/EMo2atT4saV8dw1TgWF4UMIOAkIasTFotRNg7IsYittf8VhqbCUyrDO/tazDN3wK/VzWXC0rhyIyJFPahalKc+2azRIdw+movLnRQ6hXni2owHzKGdNe7408F84nPh6uYj2luUhdFJb+rGS5impSPV06fFfIavDzVSIoEl36r2AmgTxcbzxLAHyOkhxxdz+6w5j88+mlvMqf35VdRa0tLEKWnJ3GknsbNt1CKA76Otg65K0gb0hOGJGgPCm2E9dfvQW0D2D39OB3zy2aJ3 f78GQqjr IAUJ/Mhu1lkLd6dqUZpFo+rIuyS0ur6C0iN+R/1ju0sTVpNIEfeE1VUbetVBg1fp6ia5uLY9kURA2Mf5f95vwDIQQhIjJ7MuItePTcluIkaPvrsvkLmoq66bMJUpoZCApTI8JplW92ca6dHFyujtAnMF8ka/ItwPOgZvVQLbMKitscTJrmVvC9FgRbWxDa/XHW8fH46ORXHr//8PWI6WkF1QUwFQ0bH+MqDuY2/dYfzdGnE2hasnoan37Wa97FQBDVO3adqvhBHdrM1jZ6z0M76H5E+bUCs/omAmlQSk7ylmz5UlMvIxiLQ4IHC5tDBczro3kvp6ojFje0egj+og79DxckcdeUgqMQEFf8T1exB1nKJWw3LR5jkn5z9vmH8KybKQ7801jamQq4trmTY6hky//cNn+TsXa+jyBfE1AfOpEMHlXMVR8+2an6Avr22J441jlsY+zinK/3wY+mPOumKyXsKWZ1kw2GbN49ktfWbT2JQoFs1da3C5qWk1AdGc8OgzMN5DXaPVTR7SQoscVWiMvvsROMwh4TvcX3uiYt6OcAR3an2BNJjA2BL+kR7cbq64kcGRaNjxaPbk= 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 Acked-by: Johannes Weiner --- drivers/android/binder_alloc.c | 7 ++--- 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 | 54 ++++++++++++++++++++++++++++++++-- mm/list_lru.c | 48 +++++++++++++++++++++++++----- mm/workingset.c | 4 +-- 12 files changed, 117 insertions(+), 36 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 138f6d43d13b..f69d30c9f50f 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -234,7 +234,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate, if (page->page_ptr) { trace_binder_alloc_lru_start(alloc, index); - on_lru = list_lru_del(&binder_alloc_lru, &page->lru); + on_lru = list_lru_del_obj(&binder_alloc_lru, &page->lru); WARN_ON(!on_lru); trace_binder_alloc_lru_end(alloc, index); @@ -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 c82ae731df9a..2ba37643b9c5 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 95dae7838b4e..b57f8c7b35be 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); } @@ -1517,7 +1517,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 f238d987dec9..ef2034a985e0 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -464,7 +464,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; @@ -482,7 +482,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 ef063f93fde9..6c2decfdeb4b 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 545c7991b9b5..669332849680 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 db86ad78d428..7675a48a0701 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 * @lru: the lru pointer * @item: the item to be added. + * @nid: the node id of the sublist to add the item to. + * @memcg: the cgroup 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: 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 + * @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 * @lru: the lru pointer * @item: the item to be deleted. + * @nid: the node id of the sublist to delete the item from. + * @memcg: the cgroup 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: 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 + * @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 @@ -138,6 +170,22 @@ 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 + * @lru: the lru pointer. + * @item: the item to put back. + * @nid: the node id of the sublist to put the item back to. + * @memcg: the cgroup of the sublist to put the item back to. + * + * Put back an isolated item into its original LRU. Note that unlike + * list_lru_add, this does not increment the node LRU count (as + * list_lru_isolate does not originally decrement this count). + * + * 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 b192e44a0e7c..c17d45c6f29b 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -615,12 +615,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 27 23:45: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: 13470417 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 8F0F5C07CB1 for ; Mon, 27 Nov 2023 23:46:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 933A76B0256; Mon, 27 Nov 2023 18:46:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E3556B0257; Mon, 27 Nov 2023 18:46:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C08D6B0258; Mon, 27 Nov 2023 18:46:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 531146B0256 for ; Mon, 27 Nov 2023 18:46:06 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 09CF4C0124 for ; Mon, 27 Nov 2023 23:46:06 +0000 (UTC) X-FDA: 81505369932.20.88B58A6 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf21.hostedemail.com (Postfix) with ESMTP id ECAC21C0457 for ; Mon, 27 Nov 2023 23:46:03 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZK5ah0aa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701128764; a=rsa-sha256; cv=none; b=n6622kM2qfUD1GkYMhzjXL8FIXdAfsByCedi4yNhFfFcGUQr5DN2X3xRdLmIO6Px/Vo5hF uT6d92zyqvNf0fksykyasQI/JGSpjxNibfLtD1tc0Sd1Ch0NnS7dZZg51IN8EfsiLssOmo jg/TA3FOL/omehMpojbtlxZy8Pv3kkM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZK5ah0aa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.175 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=1701128764; 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=jvxvLBlp9fV8n96zZT2Ez0ns0pnZxld9OCN8MtepcpQ=; b=4jqprPvS7UIJwXbf/e5W+nzpjKbip2+M44kqzvHEJZ5WfL/A5PX6fiaNiAMh0daeJEyFvY uE0hyBArJPk6q3dL2h9b16vo+LLjdg5xznMfBL1QLX7NSA/6vxdT5akS33Zo9Uj0wsTKpE EDJ5Or/yRlUH+zzoqh2X8wIg9RFpLok= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1cfae5ca719so23360485ad.0 for ; Mon, 27 Nov 2023 15:46:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701128763; x=1701733563; 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=jvxvLBlp9fV8n96zZT2Ez0ns0pnZxld9OCN8MtepcpQ=; b=ZK5ah0aaoIWtPtcOjA34xcoMTTrQokVvBGUufQPT0ChV0p35YSnj+OeBuNXthy3sMj t/EBnms2eU9G03+eDDA9r8GDbdknWBKu1iOg+SOSG62S6BK32zSLsJeEZS92zraT3MKp daK/eD5IOn/IoI9NR7/rEnfI0QwVWPrTADSJzUphY3+wa76R20N+odSlpAZN63WSZZXR iajrvVIR8y9/7kiuNvmlF90LW/aFmXWDTqJS8Emn/YZ5fsA6ofWaVdkqA3VKAN4L1pWa ZARroYe6rYDk+XGl6sfJzgSRsRlgVTiyGA1rwj1KSkb6zTA4qdAAnPocWrlFWbkeuStx rJXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701128763; x=1701733563; 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=jvxvLBlp9fV8n96zZT2Ez0ns0pnZxld9OCN8MtepcpQ=; b=s84ofYGTXQk/bmWI89kWy0SsM4iCTTj+08pL6SecSIjUpLGI3bumQ1te5gKEEM75r3 piS552WwOfYW8hGCwI4gQtijQx5LFwzQ+dOgtXbaL2nesQqFUfSfrkb8JdJdDhG3oE0A BcO9DcD5gviKxQuDcR/FfEyMJ4q/WovSwHKY9RreNRB26wKKHwfq/o1rOe5Pvc5vaGIB soDUicRVTuwFHCHAvQdivhJ4j1SSSjBI0tKyQvFjjEiCBxeL6eTixwVAXJMmNj7TaNX3 pWOLs+QuDz4fN/9mUcYrmozfvm+KcYAgqWosi7XiYi0eglU7yNubqQrncDZUC93R/Xau Yq3A== X-Gm-Message-State: AOJu0YwnCzWCnon+wU13EQC2FxMQ6la5mIHWn0PP/3at94gVtj+XCqaS hDWSbhBwBSIQnAvX7xexvik= X-Google-Smtp-Source: AGHT+IFWjPsb6Vx9KPkJs2Zvxm9Rkb4xtPIG7tu2HUUPcSPyQQtnOlcCh2kiefVKGXuca/ewhsPVLw== X-Received: by 2002:a17:902:b28c:b0:1cc:3fc9:1802 with SMTP id u12-20020a170902b28c00b001cc3fc91802mr11577380plr.61.1701128762852; Mon, 27 Nov 2023 15:46:02 -0800 (PST) Received: from localhost (fwdproxy-prn-011.fbsv.net. [2a03:2880:ff:b::face:b00c]) by smtp.gmail.com with ESMTPSA id u13-20020a170902e80d00b001cfb14a09a4sm5507314plg.126.2023.11.27.15.46.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 15:46: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 v7 2/6] memcontrol: add a new function to traverse online-only memcg hierarchy Date: Mon, 27 Nov 2023 15:45:56 -0800 Message-Id: <20231127234600.2971029-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127234600.2971029-1-nphamcs@gmail.com> References: <20231127234600.2971029-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: ECAC21C0457 X-Stat-Signature: 8qhfdzt7cthn13jzksrajkh8jowqozxo X-HE-Tag: 1701128763-925245 X-HE-Meta: U2FsdGVkX18OuiQhEQnDiV/6FgZnPzcohJr44NhYiEucxTQaalTyWWRNEbVxiXDZ5k2JOQFVuXqhqa3Oepg65OcXipgAIhcxy1zNYu2jRA1xM6iOimzxGK/P6GufPNTpKJDKdJxghJUSpTg1l6PkziquHsiPjCi/CkBVT9PeqA214nKghGHDp+6gSgn7/Ii2N+sLpmcQpWvVpHKIQBCLetTmMy+DuanwJZ8xNpvdHDmCbiK/zh+tgOG/ZQyqS5ty9SorAsrfPi1gHCwbFxNCep/thIHTvlZnQmtWPLo0RCu0h9NDB9L5r2CY6Cy7iQcgEqMbR+tw8fR+heYOjqmkcyk3ZlaMwGvl9l8sV8A5qJu2fgVMdTpfyxma1m9xl80A0c5/zpRa01m35s3nHoGkdrdkao27Qc9e0Y/K4shb/mWZBvC2OsLg0K08rBygOTtPsqLSZQ1mHc9LTfvTXXEAF/iVh3mafPXOdxbTVMRKCuZEsR26MBgnZlHd9ECfRBPtLCLD+9L96pWYmvE0mFNWTso98ELfukgqlD+vR7uMeZMSee7KSXIhN2kIpZaQvo3CTPvSQNzc6JfJeoSSKUAMOj7f2uJiwLycTu+3eTrF1CKR50zCHO3fx8r9VPhza4HzHyq/Y/qR4ADpLJpGiVJVenrliUa/4ZZbOCL45ejy3V9oTq+9CACB7FvSMTJ6od/e7S4DaWBzYCkDJ8Koo7crij0A5pECLo7nn7dBUkbrlrKsuP5LFL8I8Mg7lDWk4uzky043tlRRbys9tPE84bJ5M1/gHYqDUHqGnfxCBIuORE9S1c6TY3hCy3AqH4EuzbaFcHKzu5EFUoHWV2RN+8OX8765yidsxXsI/KzOEhsCOKyHbNfXOQKAXwKLUsKnqWN8SIHSx+NmoSVqXuiFumM704qK1mPzjsG9YLc9IzmCATtkE5IHVBLjIZdxMENzLYX34ALL95Ymnj18cXlnYy6 nb8LMxtg XGzuTzALVmhyDLMn4A4sWE3q8dED1OZnM35fXxtpfgrY8mlR7VovCBe/h01A/s0lyHEIz/uzXEa84oMmH4QUApA/NInkUW7IgJK5m7Ctqhq2lKMXvu9Wt3Z+A5zqmNM2vZHJU0Vq2N4a1ggnMYYHfS2YsEeDR/FQe3T/zmjibJ0NUe4UVNGZSOVRiItGEn3CKG6vTjgagUmiLhHQ2X9+pVbXShEE+fCoKIXe2VhMj7jDsTdiC0zWr5M8Dt7tWBMyQkVHwMG+3G3OSa8YBt3mjGqr3sg4InwUKjttHltXde0RBblLolGOMai9IBNZHGbWPPwZ0jfyfXh48ZI4KeP/m08827oWmffqOr0L1Yngsz97f2zeNsg2vCrBB8zt8ZepgwP7+Tu6IyNn9q/yRtBjd9ELLZ2AJtFVpNSuyFqpuiK+f8fvL/KxwsXuSs8UwIjB17MHqGkiSod4jtDtRDYaoRObzxCSqvVLjItLMBozqzChUO7c= 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 this functionality via the new mem_cgroup_iter_online() function - the old mem_cgroup_iter() is a special case of this new function. Suggested-by: Andrew Morton Signed-off-by: Nhat Pham Acked-by: Johannes Weiner --- include/linux/memcontrol.h | 13 +++++++++++++ mm/memcontrol.c | 29 +++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 7bdcf3020d7a..5bfe41840e80 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -833,6 +833,10 @@ 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 *mem_cgroup_iter_online(struct mem_cgroup *root, + struct mem_cgroup *prev, + struct mem_cgroup_reclaim_cookie *reclaim, + 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); @@ -1386,6 +1390,15 @@ mem_cgroup_iter(struct mem_cgroup *root, return NULL; } +static inline struct mem_cgroup * +mem_cgroup_iter_online(struct mem_cgroup *root, + struct mem_cgroup *prev, + struct mem_cgroup_reclaim_cookie *reclaim, + bool online) +{ + return NULL; +} + static inline void mem_cgroup_iter_break(struct mem_cgroup *root, struct mem_cgroup *prev) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 470821d1ba1a..b1fb96233fa1 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1111,10 +1111,11 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) } /** - * mem_cgroup_iter - iterate over memory cgroup hierarchy + * mem_cgroup_iter_online - iterate over memory cgroup hierarchy * @root: hierarchy root * @prev: previously returned memcg, NULL on first invocation * @reclaim: cookie for shared reclaim walks, NULL for full walks + * @online: whether to skip offline memcgs * * Returns references to children of the hierarchy below @root, or * @root itself, or %NULL after a full round-trip. @@ -1123,13 +1124,16 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) * invocations for reference counting, or use mem_cgroup_iter_break() * to cancel a hierarchy walk before the round-trip is complete. * + * Caller can skip offline memcgs by passing true for @online. + * * Reclaimers can specify a node in @reclaim to divide up the memcgs * in the hierarchy among all concurrent reclaimers operating on the * same node. */ -struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, +struct mem_cgroup *mem_cgroup_iter_online(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; @@ -1199,7 +1203,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; } @@ -1228,6 +1233,22 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, return memcg; } +/** + * mem_cgroup_iter - iterate over memory cgroup hierarchy + * @root: hierarchy root + * @prev: previously returned memcg, NULL on first invocation + * @reclaim: cookie for shared reclaim walks, NULL for full walks + * + * Perform an iteration on the memory cgroup hierarchy without skipping + * offline memcgs. + */ +struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, + struct mem_cgroup *prev, + struct mem_cgroup_reclaim_cookie *reclaim) +{ + return mem_cgroup_iter_online(root, prev, reclaim, false); +} + /** * mem_cgroup_iter_break - abort a hierarchy walk prematurely * @root: hierarchy root From patchwork Mon Nov 27 23:45: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: 13470419 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 8B9E6C07CB1 for ; Mon, 27 Nov 2023 23:46:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C7A26B0258; Mon, 27 Nov 2023 18:46:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 450446B025A; Mon, 27 Nov 2023 18:46:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2061F6B025F; Mon, 27 Nov 2023 18:46:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 097506B0258 for ; Mon, 27 Nov 2023 18:46:08 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B0C701A012D for ; Mon, 27 Nov 2023 23:46:07 +0000 (UTC) X-FDA: 81505369974.29.A94398E Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf11.hostedemail.com (Postfix) with ESMTP id 16D7840485 for ; Mon, 27 Nov 2023 23:46:04 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=d6AlSt9f; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.179 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=1701128765; 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=bWOipUHj9Dmf0Z2DLTB1Rx67tTniBw2zqpEESxZom8o=; b=SVn1IJvxFXL5VcWRTolLw3LP/j0qtMm8FnyCPvKjoLzqrOvaZowHUjLipVHWhYF/8WnUyr wO2xm1gC3icc1n+aqzgtqASt2DKiI7zCFMWMpV7dTA+Z+SErw42mV1MSNKVAGc1sblqdbr JExv7a7qsylL0u2lX8wGm9lSRfiKvlc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=d6AlSt9f; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701128765; a=rsa-sha256; cv=none; b=BTUaUNeqxlFz0BG06lqb9pFygXbwvnr4CvyWwdzvKY6BB8cxETml4EFadwiJV8NJZ1K3eV 0JQRuXlOg0uy5hkcRiBoJwcJGHeMVGAO14mL9dDQUrXQjNyUTKFS8yoXffbX1uitG2FNzP bOh5bCUHL4uOpOkx0Uv5xV+bvvBqUA0= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6cd97c135e8so995588b3a.0 for ; Mon, 27 Nov 2023 15:46:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701128764; x=1701733564; 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=bWOipUHj9Dmf0Z2DLTB1Rx67tTniBw2zqpEESxZom8o=; b=d6AlSt9f8JvVEs+yOVMQilBPSC1wN0UuVQmQlu0t6Be1h4NSiWqVHBnWVF1eKIEmnL MQbSGoBuvlLBNa4EU0hZTtk4D7bT9hoZWjrMXd6wTCg8jDmiuMZd2SQWzZ1LlzR8VXNc sTy6eTm+KWMOHhMG/i8l7BMVybVaTFYPGRnIEQ+5rxug38CJcDX/rsnnxSduMqMelerL lcIR7mz0gAoOh7K0rhiwyaAe2infBA0kNdjRJ4K+lHTJzn/4cGJo4iDVFlOvoeBD+4xZ uqPe9Zt4NgWY8T3J0MLiyAbVPyR74hqoG/p3altAvUFrGYp7lHxQi6jOcwFJAjP0IY8e ttKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701128764; x=1701733564; 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=bWOipUHj9Dmf0Z2DLTB1Rx67tTniBw2zqpEESxZom8o=; b=OwqFhKgWmcuNQtzy/ZEApSB46XnYz7aOeJsjUOPoE4hUedQvUEsCOgc9qCiiEQ/xJz OnHPFi6xQkICgI2Vd1KBuGPvms1uzbqWnVL10hrl/JgiKxxPtThaJDqtVstpD+yiDKpD BJCnCPsunfWPQX6B1657CjfgkklQF2SpO251i2BT9VnMg/MDQ3moBShia9MyzD0T0fSQ LhpqdX+jWrZefCwAW6p22VCblBwe11GGJ4NDc3LSFHaxlQtxXcb/EE1IFBc0pqtLg0f9 9LCOEJwawZAUbDXLPteUHUNtrNXEFQ565qaze4I7EVMN9EH9Ej5WemL7N2fkJokIT/O9 /e+Q== X-Gm-Message-State: AOJu0YzaV958FA+lZJ2a7DwzbJWUZjoGv9TViurbdH6HwDFw7DZW0MwU aLqASrRwPolOQmbXIsN0NJw= X-Google-Smtp-Source: AGHT+IG53Y+4b9/tx63WaQc/soW/oe6BTGHF/9YP+H2NVQQsAKCCyAnoqhYfOg0JOJN+GQO7ctw/Dg== X-Received: by 2002:a05:6a00:984:b0:6cd:8870:bd1f with SMTP id u4-20020a056a00098400b006cd8870bd1fmr6937191pfg.0.1701128763715; Mon, 27 Nov 2023 15:46:03 -0800 (PST) Received: from localhost (fwdproxy-prn-007.fbsv.net. [2a03:2880:ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id r16-20020a62e410000000b006933f504111sm7940946pfh.145.2023.11.27.15.46.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 15:46:03 -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 v7 3/6] zswap: make shrinking memcg-aware Date: Mon, 27 Nov 2023 15:45:57 -0800 Message-Id: <20231127234600.2971029-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127234600.2971029-1-nphamcs@gmail.com> References: <20231127234600.2971029-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 16D7840485 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ncyg47cimzc7taqtyiozm57m1wka8cke X-HE-Tag: 1701128764-188993 X-HE-Meta: U2FsdGVkX19AJuWNrG+gztnqVGbYdy1Xgk4aO/NvX9jOTLR7kCiYC4qx43bsxpon9RLdINDKEbCMAj/UnHS3fHpc+IdXXLSh8e0Ue5FOZBNAz3XpdRMh7XmU3QCHsvAj2FYnHc8euHQg59q8ZER4b0Z15enUaGnn0XptQQZjyN3ozemksi8GVxfOZDymQUz2UH/e+HCmc776ryRgFmb3RPJNmZyEMHiQqFTzytMTi/SKjgrDOfjz0jtE8L3tRMP97CWZ6gnJ9Y52AA0kzInvRzBSBMJTBwfRzgGNGi/MlaxgJX9DmROOQvq+Zw7sG9CKHBX10OQquRrdxRCbfQJiuT5PPqdagkJqJKhgQ/tMJMHIHyv1F37gt6r0J5mch3sM5ABGwiUGAPKanf7KWuJThe+PNkRRnEcZu8WtBIZCrZSnj0FKkbXnlveT2zYpxy3YxYrIuiveztJBPJXqbn638gO9AJdvNXq3FJCnRxYeir/W+JhwBjbJFZo6p9LelbUk5bWWvCnOmJFo68DtgRsxzCqjqZfh9iK5TzcSVWANDniHNriJE/k17M9i6JbuLv8SJ9WQWHQiE/uXpCUnksJiudhAz8Tl9Dz1o9V5tFic5nFGxPG15CVnyirFETPbY0DfVTzY+Mqpl4O7JIGZGCH3ek5IvS6g0IzlUmd7DD/3Z6c3e48/ZWbBWD+EUFopiz+fCLrIomGFKRX7T6eFTAiJQlaxk3cYbJS5i4ZJUbENlKw3b6E34fZVXB6GCEldv+fZ2Gen/pcFnQdhhUeicY2+o7zIhIBOHw/+bPvhEu1pf0nYp3//1mo6LEtaUgFdyRr/3NAQHjFM06VBHjIenSTq5Bd0AIiJ6lqCdzwgAu5zWkO/M33dwGcJVNdyvORdlreOCagNWHTen4Cy3X5wBT5q4rCxb8bOOMc21SyobgGI4di2Ken9xvV+ahUnsaSklvZBgJ+hsXP6GqnOenE7xp1 di3b9heP nhd2Q32SBY+iWzTPYGYLfoyVp2veUZOcTGz+y/t13zZst/Fhu4Hjp/EwZZe56lBjmFcL1r2mbDcTOAVCV/tS09NYb5s0XDqqzQyqYg63DBy9G9GdjOes6QrQttrzRkgnyOCBH0BViw2PDBZN81v8BWMVyqJKjS2qrsGqpxJQ2NzGH4vbqkfqGOGDx/wqHPICKSMwohdisLJRkXiK5okgkOlEUQUiy2Noch+W5WkqbeppS5e0RXeKnR8VWwh/haykQiBNMV3Ce34lHmABD/1ctOP8+VY/WBsT+0yekZMRAJxXvOGevg72CGICFEyU6g/B6T0/4fpOOrpX9fk5TBabxsILOh55JYhyTddNTJbzZXCqLRV50195KSwdmlzCtFAfuigb7A2ClAfl3A44VTPj25kdXOU3CihOTd+VytQnQkpuVD4i13zDXixcpqyGoO9aBMCQMXnmnXXHG0JMHRogcvwWPl9FTpaTv/7a8ZiKP4EN7pII= 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 Acked-by: Johannes Weiner --- 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 | 248 +++++++++++++++++++++++++++++-------- 6 files changed, 223 insertions(+), 61 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 5bfe41840e80..a568f70a2677 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1191,6 +1191,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 b1fb96233fa1..8c0f3f971179 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5635,6 +5635,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 4bdb2d83bb0d..5e397fc1f375 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "swap.h" #include "internal.h" @@ -174,8 +175,8 @@ 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; + struct mem_cgroup *next_shrink; }; /* @@ -291,15 +292,40 @@ static void zswap_update_total_size(void) zswap_pool_total_size = total; } +/* should be called under RCU */ +static inline struct mem_cgroup *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) { + if (pool->next_shrink == memcg) + pool->next_shrink = + mem_cgroup_iter_online(NULL, pool->next_shrink, NULL, true); + } + 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; @@ -312,6 +338,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 = 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 = 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 = 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 **********************************/ @@ -396,9 +477,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); } @@ -632,21 +711,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 @@ -654,28 +727,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 @@ -689,27 +766,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(&zswap_pools_lock); + memcg = pool->next_shrink = + mem_cgroup_iter_online(NULL, pool->next_shrink, NULL, true); + + /* full round trip */ + if (!memcg) { + spin_unlock(&zswap_pools_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(&zswap_pools_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) @@ -767,8 +893,7 @@ 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); @@ -834,6 +959,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(&zswap_pools_lock); + mem_cgroup_put(pool->next_shrink); + pool->next_shrink = NULL; + spin_unlock(&zswap_pools_lock); + for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) zpool_destroy_pool(pool->zpools[i]); kfree(pool); @@ -1081,7 +1213,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; @@ -1152,7 +1284,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; @@ -1209,6 +1340,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; @@ -1240,15 +1372,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()) { @@ -1265,7 +1397,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; @@ -1292,6 +1424,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); @@ -1370,9 +1511,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); @@ -1385,6 +1525,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); @@ -1479,9 +1620,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 27 23:45: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: 13470420 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 347BDC4167B for ; Mon, 27 Nov 2023 23:46:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3E1066B025F; Mon, 27 Nov 2023 18:46:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 366816B029C; Mon, 27 Nov 2023 18:46:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 193066B02E5; Mon, 27 Nov 2023 18:46:09 -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 052C26B025F for ; Mon, 27 Nov 2023 18:46:09 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D2BB3A0111 for ; Mon, 27 Nov 2023 23:46:08 +0000 (UTC) X-FDA: 81505370016.24.C1A15D3 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf18.hostedemail.com (Postfix) with ESMTP id 56FD61C04BD for ; Mon, 27 Nov 2023 23:46:06 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UaPF9q5G; spf=pass (imf18.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.43 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=1701128766; 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=+QqTttvrK+ydHCThErTWQGU78pwuFOyKaqUvf3Lt6j8=; b=zZGmPkEVAxZne7YaxH789ig/ovm7lguCTFOs60xN90Uqadu2fU/x47uHmoqQ33AUd2+NFB KO3d8NggcyYeZbflze5e4mWCJbxSc9nCxec9hyZL5oX7lNP6sdJkN+JsM2rrbvyG2SzFs2 f13WWACf4l3+mudUuqCy9jPnbG2GLXE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UaPF9q5G; spf=pass (imf18.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.43 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=1701128766; a=rsa-sha256; cv=none; b=PCweqpuJDx9QhYxhmaG7ELMw/ATiZEn1TcW+gW4DcKlduYDUhyssWepejADZ5F2g4TLfPY NF7H/eIpB0y6j7QeSXHMF4t+8Po3mMKXAnDbGTv6l9dCMBdhQa2Kuo2qvm4aghTlUKLQHQ O0EJkFwQEDKcx+aucyM75sRTPLRYvNU= Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-285d32c6f07so1221243a91.1 for ; Mon, 27 Nov 2023 15:46:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701128765; x=1701733565; 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=+QqTttvrK+ydHCThErTWQGU78pwuFOyKaqUvf3Lt6j8=; b=UaPF9q5Gub7KIpVCk8ybYnofzM0bN3d/2X2087ER+32bEoM+O1jwEOyuhvrgwwDtH6 lDtMZzyVPIJX3jwzNEY+A7T1au0F1g7v2FZ9h22X8G9pw5gWKTAnAFVFv8TrwkQmF04d 0rpK9jJ1hYD9GBf391Fa60FEDuwbtZf+IDawBmreC0wTWtbXRCNVBTTzUkJE2FoAdQPC 2miH/H1Lj0VaX65UoNXsvU67k7+apguSD79bge3deTsANx09xMNiESIDnu+3WPOZFu1d PY8MBQoyDoTeY8vQ8bWXhy41/mZrtNCDAgLQM/Gix/RL9ukPQ2fdyP5YJQndv3hwtZOi +bIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701128765; x=1701733565; 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=+QqTttvrK+ydHCThErTWQGU78pwuFOyKaqUvf3Lt6j8=; b=Vj+GQLs7aBwpI0WbyflQPLfDph1PcLVBhMLCgHAPMT01iVEVGe2TSqsKBLu66WoYHF ofXPT9OmslM3E+DqgdSHTLY+7wKyWaVpBgiGa6Upc2uIZdIcjnhnTWox49SfYQJZVXpK +ybUycqW9erm+lu3xEhYMQh+NfpDQBRMFzvhs3e5QYztCk4t4jv0TRD9oHvKmVNdVDOL aifuiJI7xhJQKfV4LkS9LsCbI6YZMVOoxiINym/BWVJa80pAJrf9BBjfnYAJ2UCvXf4/ XETZ3FB0xnomj3s0WSAX33y9n6sbsKHGUwFikY4qqAysXJ7rg4lmd6YPNKbViaaHio42 XJMQ== X-Gm-Message-State: AOJu0Yxb5pCKpnJwlMZiwKLjyFaRg9UYEfGyFUgbS9c08Br38CK3c0ez FOZmSy99WJ4xyA646dmj/E0= X-Google-Smtp-Source: AGHT+IExkmTDKAzglcJvC3UaGVQZ3+W9qPh/6zFr3PtHuO4ykPcK8P5QG1OBTphSE0yUc3YsgNVUPQ== X-Received: by 2002:a17:90b:4d06:b0:27c:f9e7:30fd with SMTP id mw6-20020a17090b4d0600b0027cf9e730fdmr12623338pjb.7.1701128765215; Mon, 27 Nov 2023 15:46:05 -0800 (PST) Received: from localhost (fwdproxy-prn-116.fbsv.net. [2a03:2880:ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id 25-20020a17090a031900b0027d0af2e9c3sm9103232pje.40.2023.11.27.15.46.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 15:46: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 v7 4/6] mm: memcg: add per-memcg zswap writeback stat Date: Mon, 27 Nov 2023 15:45:58 -0800 Message-Id: <20231127234600.2971029-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127234600.2971029-1-nphamcs@gmail.com> References: <20231127234600.2971029-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 56FD61C04BD X-Rspam-User: X-Stat-Signature: zm9sjkqxgh3d4x63gpzp7t4ju1bq8dph X-Rspamd-Server: rspam01 X-HE-Tag: 1701128766-470868 X-HE-Meta: U2FsdGVkX1+5NKi8MIDbx4QFNjPVVe/V/NI+ZZsp7qu5/AkWG9ZW4rV7Al3G2PDy28d2S9PWKYcIwuvDLdjVl4qA8PTbbgmBdm+eHAjZSMbg7K5hQvtq/WuPstzid6hLCdRVi8SjVlgFzjbIsBaNnxshRj0yz8HEyGlZ4iAZ4ezJK/kUyIenNCrs+QRWS+f15dlVkDIPSNkaIT5J5Op9QZtCLUZLBx5eiPvKNqBbPyPVB8iYFnX0nnT98SXL3Aca2xMA6KIzfyVWe/ybqxoRFSzmCvmII5FaPpEJk4EIyJRjC7zdM4fdeJW5+4EJvzS3h41LXxQR4DHapTbiRyg+otXXSv5680cflZ0MbUuOj5GLX/mDrBOsGceRHV/hJl/mU4IEfL+8Om3wsRTiLaEa37nYXugm1GqtznBLZwrOT6sBl2BWnv8vC7bqbW14ePGNwdi+kmBux69yEivFXTqV99Mew8aT8ll/YiP6RKl5qN7CoyydiWlVD1RUHzz7xihbUlfbwEh7SXHGX7UNYPg2rWHciqa3Re85v1X0CjXF4rF15d0kzIt/vSwGnx4dAwq2NP3irv39IJ7pNjIVADw6/XxIZJnzxrj+4o2iLadrexYV/e8mjYF4SKv1HoS9SDaBrmFxNPqdeOxBonY8O4ZypV1jRvm0lqiKusqVe0q9+3ZXwyLn1pZGKhXL32OFQAuPv3JGaredtiHfFv1fgWenQUUjrSpwwhRe1PFbclEbReaNZA5eEFFZEWIxGbZpMlTooMWVRRFqZ6TigBMp2yGjmUN/nEa323GLUTRxzr+A7XHisIESQU2wuejVCz9APYBubMxx/yWrM8VwCcXYKlTFucMtJYvWU+u/goaymjC1y+BBkHoYb9bPFVRKDW71IISmbzuG8uhzLhCBoMUlovPEsgYwoha/ARusLIGjYa2i+YjhVTnV+aBP8VWVV52fagXfutp4vvDmYd3+XXc/Ljc ISw+5Lhl VE1IsQgikgIEoznhHfdMeKanCK+zlpsqpKnfJhWAENQ8biUqeCbv+AmocdWtqjdLjtX7iG1snX+qU8w0QL6597TITcaCKICeiCtuAIJmiTc0EjXPSnkVab9IWseKAPjmjMHJUpChzlrZVhFG38KuINhfFgpTaoXffzwzvn/yUeXetxSEklU2cztjodoZGYn0VddVcVfXzJeOdalxvLE56zN9KV4O87IsJ+5fV9+Z9RUOfjj9KCF/oNQbx6r9x7ZggcldDsE7+mS/NGpJZ8JbWc9BG+S8ebU0/WXwVeWK9IoSYcmILrxAO5eaBTgln+sqXtZXW5Ygj/uMJO76ENmqMNbZi4o5otkM3XGwVrjTSrSPlWxxKR2fzS1G9R7iFF96T4rxg6K0tYi8vC9X7E0M4unQVoJvBUcBsF0RlobyLqxrFCswpK2dxmMjp8lnVjAsdleKYNTN3ZCoCetg+MkBx06DO8w== 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 d1b847502f09..f4569ad98edf 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -142,6 +142,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 8c0f3f971179..f88c8fd03689 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -703,6 +703,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 afa5a38fcc9c..2249f85e4a87 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 5e397fc1f375..6a761753f979 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -754,6 +754,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 27 23:45: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: 13470421 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 D131EC07CB1 for ; Mon, 27 Nov 2023 23:46:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D04166B029C; Mon, 27 Nov 2023 18:46:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB4496B02E5; Mon, 27 Nov 2023 18:46:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B0AFA6B02E9; Mon, 27 Nov 2023 18:46:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 98FC66B029C for ; Mon, 27 Nov 2023 18:46:09 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 74DE01C0041 for ; Mon, 27 Nov 2023 23:46:09 +0000 (UTC) X-FDA: 81505370058.05.AA2B4D8 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf06.hostedemail.com (Postfix) with ESMTP id 4A8411804A3 for ; Mon, 27 Nov 2023 23:46:07 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QdsoZita; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.174 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=1701128767; 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=qpgmz+lE50DZaA/oBKT5EnJWyAwPJO9gmArvQZM8Xwo=; b=iwbnkrB48YnfCyCs4LJjS1BY2L5ItsOK9KJGLp28sxdUTWt5n+OOr1/p6MvzQ39ojvDF4V wMusbfAKDqru/9CAI+tTZDvWaknnYl5F3+es/1yKEM5CUWyII/Uq9Domc8TsCu0yiZuMc7 QClne+JKvV3daJ8HhXYu3jYHycC276Q= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701128767; a=rsa-sha256; cv=none; b=zrKQFDGyIPYtizDFu0F8IxRWIeSGBj6Ren/dDiDUXK5kkGXk04iHDYbOY274INBcKb6Dye AGcNzT6MPDc/NGTLYmrsvalgB/CQk0jtHQd0MJ3AMk/RUw/pmrbEA2Ln5Q+r3yV7Bc3ArU 6nkNLtTzCBjrEWIffXOSRwL69z3b01k= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QdsoZita; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1cf8c462766so33955315ad.1 for ; Mon, 27 Nov 2023 15:46:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701128766; x=1701733566; 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=qpgmz+lE50DZaA/oBKT5EnJWyAwPJO9gmArvQZM8Xwo=; b=QdsoZitaWFkAKzxUgsitZqEP+jcbWiZnea0UZVScN7XS3fIssxMryw7W3gX6XzxRqw RV/D405c+UzbBOq/1TYqLYGQ3JpEbXVuzzwuUHw7uSzc1qBWFDFpusoZajl/hMIyK2v7 H1F9r8/KWHh6ncFkpUmrOi//ubdP56HnzM8UzlpyghXdo/mmJAR7vJX1NNCU0xtgS1gA BnEwseGAJONZIdgcP63chgD73ssIdtGUsN8DkNV8gqEomjLCXCgs1i8xikhOWVrnx6xd mJ5zw4ZLfxDLte2yVYvSPIoZbkH1+VZkwD7mXnkh8GU8h+5Eccw+f8wrm7wvGJ027C61 TXgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701128766; x=1701733566; 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=qpgmz+lE50DZaA/oBKT5EnJWyAwPJO9gmArvQZM8Xwo=; b=Rerv09/z17DywPPhZTJApzt2xbdA8BMJbC4Pj7pqIJVu9Ukl215Xd5dFrjeE1hQk6U yoshymUnZAbIGQARaALYM93OqNzNzkfFy+zhmNnyVvi1YsktiPULSbwTGRkSegvYikZi Vv/S+XqWsIH1ncD2GTXHld4nMb+AsvS62q4mgA5ehq2gjLNR45R1w9Jc9DSVCgjvKinq mvvX4GqF9sFV+xggus7Z2PMbi/BlDv9yFePQShT6608LEtx+I/132fPKulB3mt4cD7DX SFyd7tNfmUrIrVR6GSGRDBFZKFS3rUSpheWtLZRRWNs9uoP6q+7AhlC1E5QOQjPgkzl2 AjnA== X-Gm-Message-State: AOJu0YwV6gzMgT3bcd150KMMDB9EjN/tCeWi79/YLuLTxTkx8ItmUII2 GLhTdaKuvXO93Zhbqodb4ak= X-Google-Smtp-Source: AGHT+IGu6efuRIiSCPKCUDwRobB76N4xJJlnLFjZnsosc5Yzt4lkGdloLxXAga7HCdsLQ7YjiKz6pw== X-Received: by 2002:a17:902:c411:b0:1cf:a0b1:ec06 with SMTP id k17-20020a170902c41100b001cfa0b1ec06mr15743491plk.55.1701128766074; Mon, 27 Nov 2023 15:46:06 -0800 (PST) Received: from localhost (fwdproxy-prn-011.fbsv.net. [2a03:2880:ff:b::face:b00c]) by smtp.gmail.com with ESMTPSA id c12-20020a170902c1cc00b001cf96a0e4e6sm8166994plc.242.2023.11.27.15.46.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 15:46: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 v7 5/6] selftests: cgroup: update per-memcg zswap writeback selftest Date: Mon, 27 Nov 2023 15:45:59 -0800 Message-Id: <20231127234600.2971029-6-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127234600.2971029-1-nphamcs@gmail.com> References: <20231127234600.2971029-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4A8411804A3 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: fmohuf4seftdp6h95u3dza76mccpehqr X-HE-Tag: 1701128767-566241 X-HE-Meta: U2FsdGVkX1/wfKjsVkhhirwgHWKF7qbeRvcaIKzNbJLZke8WMjnpsR/8Ii8FiWv5ntP/FZr5APt7yyW7tgsr7JrGZIbkcJ/1wgiHIGbNPQ257efAYGJvvmzdaUTJvK7dlQlKr67LHI/tAOy+DZxoZnN2km9QdLbQoCxCCaCmr/pPGjyOKHw+cspS+Bkz3XvySjgb1/rNKsbwr3FNa9LY/YYFcnxOfplSCICuVv2ZLxAA1v38+GQQgATc7swGiax36lAOetT/9StRsljQyANJwB87cXnubQXYJ258hi8lAMo7KrHkS/tcOPoQMD7qZll/XnTZrFhWFqPFiCmsbpCruH+Efhe8CUhWvTnKkSH0naL4DB3nNA1WniTcd0Re0YJGdKXReUy9LgiAKhckjFi4DV8NxjaYjJ/s/XrQX8hVvs4voQgJWafHn38ogL4NzXj114A5DE6HI/hNZqSWkbooxuYHN3kkVQ84uVB/kFY9/hBVCn9tK3L4+Bm08uUEVCteG3jpZK6maN+m03wiakZwca/GZgtCox1vhplblC8yGK3NO2uHTS6yCtOK1h8/lg1ZRADljFbRjupiv+9JBnfUELccYMyH2jpBvY11FtuJX6F+3OXoP4GEUxw1YSSdVtfyRnyHaX4L6RlS0v0HqJojKacAgqtkdvce6THy/L1njNkej5pOms9bxjUDqoycJTP6z7crgSEo+1qYAugQL8/DTQ3ugN3RKOvj8rwRrNxhQFs5thsm28zbbt2T8FdFeZ345LJSpwtAnPNCbv1c1bpMywzeEyhW0gCeryHH4EVZbbIWrOKiTvPCNNsY+NNhmvmGsFCpUI/qVj03ycNg9InH0Albd1D9jmuBCGALbepUHGk9EXopNUSrS7ppc0xjlILBFp2Qq69XUStnzhRl23T+F3vuwoYnNGZewhOkHCNx7PAaC85jw3FWDsffvvgEGaFguqPEM+wKIMnlSmFu3KD DdjTCCie FGeOz5QN1TjIVILYaf3sJZWPYAQsviklksCNODKuDN3n8Rb6ru3BEmwUXsnrUa4EfdWFUuLI6C2c2PZSiEkU5bu9twjNV0bMgcH4WGSkMRMG5NAawP+Fk8+xxlGFVIMBdOLZglY/4ACB4aHbXdHw+MH0NhxQXdET0m5ONzu4SFbtPnHJz0lI3hpVVtT1kGb2fTBiM9il/r8mBlS/6VcEXCaGIuEp8EArpdgw4MpsVWBJa1ALNlRLAbz4+5FO+9o8wremg+jP4hCEnJOFMiOm/BtlP+YXkjdcBGZLX/klsQUAyA7bEbgtCkWc7dNTke0gO3WtZhApC/4Lj+sFGJcv2Ukw5FjqVzLtb2bgVJAJGn7AWrEwkARWBelgAKCfvoLvFk9TqNrPi6qSi6pWPnDpVpP9/bXQCS0O3xKjNz3L1ilwmUZ8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.121858, 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 c99d2adaca3f..47fdaa146443 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 long get_zswpout(const char *cgroup) @@ -73,6 +73,24 @@ 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; +} + /* * Sanity test to check that pages are written into zswap. */ @@ -117,43 +135,51 @@ static int test_zswap_usage(const char *root) /* * 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 27 23:46:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13470422 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 B0A87C07CB1 for ; Mon, 27 Nov 2023 23:46:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F017E6B02E5; Mon, 27 Nov 2023 18:46:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EB06D6B02E9; Mon, 27 Nov 2023 18:46:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBE656B02EB; Mon, 27 Nov 2023 18:46:10 -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 B41B76B02E5 for ; Mon, 27 Nov 2023 18:46:10 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6D946120128 for ; Mon, 27 Nov 2023 23:46:10 +0000 (UTC) X-FDA: 81505370100.03.FB58F65 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf07.hostedemail.com (Postfix) with ESMTP id 61D6F404A8 for ; Mon, 27 Nov 2023 23:46:08 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CRgCMyic; spf=pass (imf07.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.172 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=1701128768; a=rsa-sha256; cv=none; b=Y58lm1aPRE7K2i7oylHGqJJuVSJ7nQsFDRsTHazNwCCYRxXY+oehny8It5P7+eMnvldL5A UUTv5zyYSMAQFrOzIHOHJjpknnk6QpyN4um7p9xkmUN2DHIy83ZIoCnPQuvZT4gZXUfkDf GNAnxNjZE4hVvFs905CB3VQmhe9qNZU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CRgCMyic; spf=pass (imf07.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.215.172 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=1701128768; 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=P/4683J/JG8SNpYaZrS5y5voLFVVr47Tv9KGfGpsiro=; b=ZMXFVmy+atU9auUp/ewxfYX9IUrqTxW9eCfgFFCWuskGdO2y7J55Fen2gIHDSEP9xFQAE/ YIpa5xrNVUD5qy/TemdyFi1Jo4fFqwKOsVsdSu6BGgR07VgTAhPmaKQrbKHDWeIeU8K3N9 3XbjaNckq+/ngvy2ZmxM/mq2GtfaOkE= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-5bcfc508d14so3517124a12.3 for ; Mon, 27 Nov 2023 15:46:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701128767; x=1701733567; 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=P/4683J/JG8SNpYaZrS5y5voLFVVr47Tv9KGfGpsiro=; b=CRgCMyicAPXwDDgvx4m+bLR5RcRC2pd3hZ0VuHjxU/72DNhp1w7kJqDE4lSwwNZu5A Atqb+wtsrE98XF6XCPToxs1txla43MT9x8GAgXhbr42B90r3Q4/l9RX9ndcyYitlJtZl hinNdlD5RhfVLp63XSqxDbYmyU7voGfP+7/Cv9DsLVpovT1pAzyjHxbmHPXZT/NNVOoh ttVC4XgO7yiohMdxi0iKLafCyHxcE89nbXjKcmmQv8JZgKsSD3a0fxvuhhwGyBA+GESQ Ycq0zfBtrT6sGUXR4sX7FQoeqrR7vwdwwVTTpiSEIB6Dxu8inDWjy7SxCvwiNFQJVr83 ORIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701128767; x=1701733567; 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=P/4683J/JG8SNpYaZrS5y5voLFVVr47Tv9KGfGpsiro=; b=ZAFsGHCrDrls729w49oMQzyleE4hbsswLiwvhjuAedLDlAxKkspIykDQxFQRmwuvx4 h+Xs5Vj0ndHzSDJsL+UUm6HBNeYpCgTk9pjPPOqPhQAFNsn7f1Cb+eY++ZOTZa2MUSNZ fNG9lbrATxkzOuaR3AZciGcna+Apay86VIWznWc8HsC/aJZxBonrj/3lU//ICUSSGcGq E7CHBbL/XmlqGt7sECRNuQjHdgoqwkaUe1jdwQyuVoW48xS+NLFoarqiSwBEsdStu6EF 7rZM7hjaDATcKfI1FloAUQhgyUK3t8Y6D/PEqyGnQCaZDQYSTJfbuKU2dF9yyGKZmIsC 1F3g== X-Gm-Message-State: AOJu0YyeqlJZMJvXtuKi/LdLRnHOIkrsO1ailPJzGw/zL0u8pBe243H2 mtu7sAcp8m+K0FOzqPZSLKg= X-Google-Smtp-Source: AGHT+IEne8hhtprZKN5WUYV1dS5IKz/iVlrZs21P7rSblrBwIt7TbNVRdH0qudR1TXG3O3iB2lOkCw== X-Received: by 2002:a05:6a21:33a4:b0:18c:726:920a with SMTP id yy36-20020a056a2133a400b0018c0726920amr15422194pzb.26.1701128767109; Mon, 27 Nov 2023 15:46:07 -0800 (PST) Received: from localhost (fwdproxy-prn-003.fbsv.net. [2a03:2880:ff:3::face:b00c]) by smtp.gmail.com with ESMTPSA id b2-20020a056a00114200b0068fece22469sm7687675pfm.4.2023.11.27.15.46.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 15:46: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 v7 6/6] zswap: shrinks zswap pool based on memory pressure Date: Mon, 27 Nov 2023 15:46:00 -0800 Message-Id: <20231127234600.2971029-7-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127234600.2971029-1-nphamcs@gmail.com> References: <20231127234600.2971029-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 61D6F404A8 X-Stat-Signature: ozu5u8tn179wh5jcn3sih9bkns7n7cag X-Rspam-User: X-HE-Tag: 1701128768-836841 X-HE-Meta: U2FsdGVkX1/mB//1vkKWVn3AuC1psatIWbb9ptMsWYronmePzvB4YRFxotLC6L2LwgFMIM9SX0SrB0SLGytEl0owoO2v3rymmz9v9j4pUzjL8SfGfPP1A1aL76ap82UohfPBoIQUhf28POviE3rOezKiuRr20kcjbvqjs/7f1cHqZ7zqPcQ0ytoFTX85aVprYUHBj3jhDfxQlPdKaL8TCcNTTtz4Lro1Egg0DtdH0HwqUCip5oKJZtlVYgk2VSQ8zahKR9G6TCZpBvZ0NFIfIYcRkkuLkTlkSVqpXgLqht4kdlzOVKso7YjodOWnah2s1jHM2dZHBVc9MnPXdYvTrIiZnaI96KRzMt5oolV3JffkAxGe87cX1um2x1FqNaWWxJ1CNtqZZNObxxiknLg24CVlCHJlePMkBmotoqD5dOPoLZOySPwcR4AWEtlIWV+NQCIlWqOcG8YPPnX+ixmCJL+njjC/5Nz0OGHhtWj5haM65DFhnYngn93HEBuVsKof9tvalTqKOMphOD+W1zgqBAaEwPHcXjNYRhZ+hoI8PLIk2N7Fz18N05d+Ewmijj1Z8RjcfkCePKWpQpFDKkD0ychV2NbBhzpccX7EG5PusiLc+frpLOMo/0UEZpJ28vw5W8qpi31ajoASSIe+9gJcjioc+kQm7RWrQCTpO/nKEN6OAiycsvup173p22LX/gc2emJxqTLHnf9obAQVd1YL++U/De1iurPgkXUCl9oR5xeEfHCKWE+VYG91Vz4ZO2oz2h+vQeLKlryiFYABndnTB1a9Ijw50tRL77awSnhD2E8naAqI5mwCc0txyzhjY7Bjc7/dvrSHqFo1rgtWHPD1ykZ0sbyA/fxXLVtu2rK2g7y1T8fimnXXGxfbVfTxuTUYJz73ybsHeWyVuOK8UHw6oy4OPEOKbPifAgN/uTtHHuZKHGnEgZyCfo++PjjLbaflXdflfqDEKGwGa9nlYm6 EvF38ni+ J0I5zuQZgjXGHPSjQy1A+gZM/0iDjOR3b0mDfilJK87H5fUV+69pgtL9VMU9kjJpHenNubLyUAY3au4D2XW1YeU8ABX4ylwhDfM6oaJ1r0dJJ1ms1qg+5rvzYIYyNn9FCrHC4NIpu9sBPl7Ikz2Q6zsUyGeDKsCrGRCpC7n/+Qkrmv8EH36dAuYpfT2tBvRUOmiUz4u7gA7imh3Km3Ln8SMPs9wA55c3WR3VtKKD9LXC48G/pdtvEOEpjiQ2PB0fqmbU9GRLNqKJPy/P+5Ug2KwR8ZHG9+DtjwOfYsua/vIRHj1srx3bUu+u8sQCspy9Une2BOVDvgFrjCBRhGrljeDApvELAE9Vg2ZN+N5VDlF+JHUtlru+Qg8t+2qmCgQuIfaoZuiSd3by84WnZz7SmvbBlHW1y68pr03/gnjYQKuL7EGEMFLmvf8fRYg== 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 Acked-by: Johannes Weiner --- 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 7b1816450bfc..b23bc5390240 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. */ @@ -641,6 +642,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..03253a5965bb 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_state_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 6a761753f979..a3378b4c8e6e 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -148,6 +148,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 **********************************/ @@ -177,6 +181,8 @@ struct zswap_pool { char tfm_name[CRYPTO_MAX_ALG_NAME]; struct list_lru list_lru; struct mem_cgroup *next_shrink; + struct shrinker *shrinker; + atomic_t nr_stored; }; /* @@ -275,17 +281,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(); @@ -338,13 +353,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 @@ -362,6 +398,19 @@ static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) memcg = 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(); } @@ -383,6 +432,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 = mem_cgroup_from_entry(entry); @@ -390,6 +440,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(); } @@ -479,6 +533,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); @@ -520,6 +575,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 **********************************/ @@ -715,6 +859,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; @@ -750,6 +895,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++; @@ -889,6 +1045,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 @@ -896,13 +1057,19 @@ 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); return pool; +lru_fail: + list_lru_destroy(&pool->list_lru); + shrinker_free(pool->shrinker); error: if (pool->acomp_ctx) free_percpu(pool->acomp_ctx); @@ -960,6 +1127,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); @@ -1516,6 +1684,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);