From nobody Sat Jun 13 02:54:54 2026 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2C063BD63C for ; Mon, 11 May 2026 10:52:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778496732; cv=none; b=YHib5XACVbadPHhyn+ojjDtmzfSwH/dopY+n6cq2Db6fahk4NPtT4dZpXqOKjvkKt4wBl/uUDOxJZEV0FPZNnU6CC+PNlnwy2NN22ajBOslhtU1f8NMwcobHV3KThWreORgWQn01i+eDMtJKiJjZ/o38+dR5CnYnnxCMGamsnIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778496732; c=relaxed/simple; bh=FyIqTq7KzjIEluRyUgLgG60I4S5apuZAqf2XLTtBDBA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oGzPzWRjS0o+1Pv+oXLanQQ/ufjYhb9eHxJX4LfuDq1IVNC44GdD5vWAn0ZY4lFnLw0NaN04xp+lu3s/tomklku7dOSH5TwovvNRPUThBLvovbhhti5H/t9Fj6czaRXFKerbW8/lUhEOoEqLUEeIKbRf9avwKV7Ly7s2K2OePaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nOs54a9c; arc=none smtp.client-ip=209.85.215.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nOs54a9c" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-c798fc1a28cso1761622a12.3 for ; Mon, 11 May 2026 03:52:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778496730; x=1779101530; darn=vger.kernel.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=XtZHWbxmSq2itef1lwgRS73YF1vAkySaw+AaQooe+e0=; b=nOs54a9cajWZxVMtjR1BL60keAekpVs5vXqXHwaFJU2UWmXQ+GnfIDcMGNhnuXAzoP 8uR/QsZ9g4JCM1CTaM3QmE92Ffm7C6xsAg0I9dFxrgcAn7AJXGWOp6lDuPH90JKflxXN Af/Exfye5sCb2y7Pu3vnGio2vk6Mk+KJGWLR0yIKgjSqO75w6aB4cLd/3h/ImO70Ne9u iH6WAn6AU2P8hFdFHGhxfYKuNz/OoXSfIavN4RZ3YJv4P+dQRKmLwfwrl3A4ealJV3w+ xMPqkIO+mqqn5gLJdefPcOsO5C9xvTuLTucB2Xy5Z28LU6OPEx1TgmMw7aWt0ooexQKa LVLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778496730; x=1779101530; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=XtZHWbxmSq2itef1lwgRS73YF1vAkySaw+AaQooe+e0=; b=CBA2RN677ks2P0pglHmSu1Xwd1tISBW/uz7j3OR92yvBSMRnKGbKNFWFdSGJCYosLs lsD2Zq0Woe1hq8WOrjrlKu9LqnCLEREAI5hVUcAzRg4/fknPa0Oc369FniQEKEbt++0U Gcv2Dz5oXakk7Qu0EowCZl/CwxksEmEgiZEBWETn9PS5tzQGXfBRQVHBqxHwMVdlKY2a HXmaw/Rg5uwlxghNtZhcOxkP4XiU0gakciAV1yvIrDrlV+/VGREDdkmkeUlt0SgxZP5J 74pcJsbFTC++hECwFQYTG5V5NGiyHZ+pJg+LkbFbUXHsWv3CRDfgSCuHXsbN02mC5/TZ Po2g== X-Forwarded-Encrypted: i=1; AFNElJ/soTaBGfWBlpSCbmox0ydzPVrYE9lPidKJNzE3iat1KMvMhV7MZtOl8atPfnqz1z+kZSYhhmiIKlNJGjc=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1u7fJaDKC7YjSyhHJj+1bhGMbZboTjZv3fR4nrPC3WAUKQJya Pje+HgPmlVBPW3wc7iT3HMuQ7s7sYQtviJzRmYZlq7FCAYvzz2w+NCO8 X-Gm-Gg: Acq92OFYRGs3vG9I4ApLjhynVoz6zG6ZocHjSRBabIbRDM2vstJN+WzJqdHEzDDDG0Q i1VMDg0VTWLP4qrlSQ5N4AUR1L0a1EyvngyZpduFHB0eLGvNFw+TZfHGbYas2LGPs3IZpaNcuYR SQ+z85V1AwzFB49VnmbDhXME2WEcXbwucGNf+ZFy3IrD/x9gEzez+/Yj70INTeIVFc+D71U5ndQ w812nntdywxQKnttHS9xyi/BcbIn9fUUMDTCnVPS+7khQ2zgL6Zb1iC3FtinnC4+LKebyaRf/0o tyBw1ZD0IvubCOUGRWHia6g557+kumxEtwEQpUp+11+CiXLEEhOY0cuu64f4fBFr1NY6fl/hSjs ZXCB6rzQ/BfUDTW1Bmd2bRSAJESGWlEz1w82i8k5CX6XPHePPkq6LFXmm7FUhXW3eZ4E9FtHDdY JQqGjfArvt/IbOM26UkrHhbnMy+W1dcv08faHdIEUkzE0FZ86EmpD5eyKMknEtaw== X-Received: by 2002:a17:902:f78d:b0:2b0:606b:6fd3 with SMTP id d9443c01a7336-2ba78b30a89mr253673435ad.5.1778496729907; Mon, 11 May 2026 03:52:09 -0700 (PDT) Received: from localhost.localdomain ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2baf1d409eesm98571745ad.32.2026.05.11.03.52.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 11 May 2026 03:52:09 -0700 (PDT) From: Hao Jia To: akpm@linux-foundation.org, tj@kernel.org, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@kernel.org, yosry@kernel.org, mkoutny@suse.com, nphamcs@gmail.com, chengming.zhou@linux.dev, muchun.song@linux.dev, roman.gushchin@linux.dev Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Hao Jia Subject: [PATCH 1/3] mm/zswap: Make shrink_worker writeback cursor per-memcg Date: Mon, 11 May 2026 18:51:47 +0800 Message-Id: <20260511105149.75584-2-jiahao.kernel@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260511105149.75584-1-jiahao.kernel@gmail.com> References: <20260511105149.75584-1-jiahao.kernel@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hao Jia The zswap background writeback worker shrink_worker() uses a global cursor zswap_next_shrink, protected by zswap_shrink_lock, to round-robin across the online memcgs under root_mem_cgroup. Proactive writeback, about to be introduced by memory.zswap.proactive_writeback, also wants a similar per-memcg cursor that is scoped to the specified memcg, so that repeated invocations against the same memcg make forward progress across its descendant memcgs instead of restarting from the first child memcg each time. Naturally, group the cursor and its protecting spinlock into a zswap_wb_iter struct, and make it a member of struct mem_cgroup to realize per-memcg cursor management. Accordingly, shrink_worker() now uses the lock and cursor in root_mem_cgroup->zswap_wb_iter. Because the cursor is now per-memcg, the offline cleanup must visit every ancestor that could be holding a reference to the dying memcg. Factor out __zswap_memcg_offline_cleanup() and walk from dead_memcg up to the root. No functional change intended for shrink_worker(). Signed-off-by: Hao Jia --- include/linux/memcontrol.h | 6 ++ include/linux/zswap.h | 9 +++ mm/memcontrol.c | 3 + mm/zswap.c | 116 +++++++++++++++++++++++++------------ 4 files changed, 98 insertions(+), 36 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index dc3fa687759b..00ae646a3a15 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -228,6 +228,12 @@ struct mem_cgroup { * swap, and from being swapped out on zswap store failures. */ bool zswap_writeback; + + /* + * Per-memcg writeback cursor: root by shrink_worker, non-root by + * proactive writeback. + */ + struct zswap_wb_iter zswap_wb_iter; #endif =20 /* vmpressure notifications */ diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 30c193a1207e..efa6b551217e 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -11,6 +11,15 @@ extern atomic_long_t zswap_stored_pages; =20 #ifdef CONFIG_ZSWAP =20 +/* Iteration cursor for zswap writeback over a memcg's subtree. */ +struct zswap_wb_iter { + /* protects @pos against concurrent advances */ + spinlock_t lock; + struct mem_cgroup *pos; +}; + +void zswap_wb_iter_init(struct zswap_wb_iter *iter); + struct zswap_lruvec_state { /* * Number of swapped in pages from disk, i.e not found in the zswap pool. diff --git a/mm/memcontrol.c b/mm/memcontrol.c index c03d4787d466..409c41359dc8 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4022,6 +4022,9 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem= _cgroup *parent) INIT_LIST_HEAD(&memcg->memory_peaks); INIT_LIST_HEAD(&memcg->swap_peaks); spin_lock_init(&memcg->peaks_lock); +#ifdef CONFIG_ZSWAP + zswap_wb_iter_init(&memcg->zswap_wb_iter); +#endif memcg->socket_pressure =3D get_jiffies_64(); #if BITS_PER_LONG < 64 seqlock_init(&memcg->socket_pressure_seqlock); diff --git a/mm/zswap.c b/mm/zswap.c index 4b5149173b0e..19538d6f169a 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -163,9 +163,6 @@ struct zswap_pool { /* Global LRU lists shared by all zswap pools. */ static struct list_lru zswap_list_lru; =20 -/* The lock protects zswap_next_shrink updates. */ -static DEFINE_SPINLOCK(zswap_shrink_lock); -static struct mem_cgroup *zswap_next_shrink; static struct work_struct zswap_shrink_work; static struct shrinker *zswap_shrinker; =20 @@ -717,28 +714,85 @@ void zswap_folio_swapin(struct folio *folio) } } =20 -/* - * This function should be called when a memcg is being offlined. +void zswap_wb_iter_init(struct zswap_wb_iter *iter) +{ + spin_lock_init(&iter->lock); +} + +#ifdef CONFIG_MEMCG +/** + * zswap_mem_cgroup_iter - advance the writeback cursor + * @root: subtree root whose cursor to advance + * + * Advance @root->zswap_wb_iter.pos to @root itself or the next online + * descendant. Passing root_mem_cgroup yields a global walk. + * + * The cursor is retained across invocations, so successive calls walk + * @root's subtree cyclically in pre-order and, after %NULL, restart + * from the beginning. * - * Since the global shrinker shrink_worker() may hold a reference - * of the memcg, we must check and release the reference in - * zswap_next_shrink. + * The returned memcg carries an extra reference; release it with + * mem_cgroup_put(). * - * shrink_worker() must handle the case where this function releases - * the reference of memcg being shrunk. + * Return: the next online memcg in @root's subtree, or @root itself, + * with an extra reference, or %NULL after a full round-trip. */ -void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) +static struct mem_cgroup *zswap_mem_cgroup_iter(struct mem_cgroup *root) { - /* lock out zswap shrinker walking memcg tree */ - spin_lock(&zswap_shrink_lock); - if (zswap_next_shrink =3D=3D memcg) { + struct mem_cgroup *memcg; + + spin_lock(&root->zswap_wb_iter.lock); + do { + memcg =3D mem_cgroup_iter(root, root->zswap_wb_iter.pos, NULL); + root->zswap_wb_iter.pos =3D memcg; + } while (memcg && !mem_cgroup_tryget_online(memcg)); + spin_unlock(&root->zswap_wb_iter.lock); + + return memcg; +} + +/* + * If @root's cursor currently points at @dead_memcg, advance it to the + * next online descendant so @dead_memcg can be freed. + */ +static void __zswap_memcg_offline_cleanup(struct mem_cgroup *root, + struct mem_cgroup *dead_memcg) +{ + spin_lock(&root->zswap_wb_iter.lock); + if (root->zswap_wb_iter.pos =3D=3D dead_memcg) { do { - zswap_next_shrink =3D mem_cgroup_iter(NULL, zswap_next_shrink, NULL); - } while (zswap_next_shrink && !mem_cgroup_online(zswap_next_shrink)); + root->zswap_wb_iter.pos =3D + mem_cgroup_iter(root, + root->zswap_wb_iter.pos, NULL); + } while (root->zswap_wb_iter.pos && + !mem_cgroup_online(root->zswap_wb_iter.pos)); } - spin_unlock(&zswap_shrink_lock); + spin_unlock(&root->zswap_wb_iter.lock); } =20 +/* + * Called when a memcg is being offlined. If @memcg or any of its + * ancestors has a cursor pointing at @memcg, it must be advanced + * past @memcg before @memcg can be freed. Walk the chain and + * release such references. + */ +void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) +{ + struct mem_cgroup *parent =3D memcg; + + do { + __zswap_memcg_offline_cleanup(parent, memcg); + } while ((parent =3D parent_mem_cgroup(parent))); +} +#else /* !CONFIG_MEMCG */ +static struct mem_cgroup *zswap_mem_cgroup_iter(struct mem_cgroup *root) +{ + return NULL; +} + +void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) { } +#endif /* CONFIG_MEMCG */ + /********************************* * zswap entry functions **********************************/ @@ -1328,38 +1382,28 @@ static void shrink_worker(struct work_struct *w) * - No writeback-candidate memcgs found in a memcg tree walk. * - Shrinking a writeback-candidate memcg failed. * - * We save iteration cursor memcg into zswap_next_shrink, + * We save the iteration cursor in root_mem_cgroup->zswap_wb_iter.pos, * which can be modified by the offline memcg cleaner * zswap_memcg_offline_cleanup(). * * Since the offline cleaner is called only once, we cannot leave an - * offline memcg reference in zswap_next_shrink. + * offline memcg reference in root_mem_cgroup->zswap_wb_iter.pos. * We can rely on the cleaner only if we get online memcg under lock. * * If we get an offline memcg, we cannot determine if the cleaner has * already been called or will be called later. We must put back the * reference before returning from this function. Otherwise, the - * offline memcg left in zswap_next_shrink will hold the reference - * until the next run of shrink_worker(). + * offline memcg left in root_mem_cgroup->zswap_wb_iter.pos will hold + * the reference until the next run of shrink_worker(). */ do { /* - * Start shrinking from the next memcg after zswap_next_shrink. - * When the offline cleaner has already advanced the cursor, - * advancing the cursor here overlooks one memcg, but this - * should be negligibly rare. - * - * If we get an online memcg, keep the extra reference in case - * the original one obtained by mem_cgroup_iter() is dropped by - * zswap_memcg_offline_cleanup() while we are shrinking the - * memcg. + * Start shrinking from the next memcg after + * root_mem_cgroup->zswap_wb_iter.pos. When the offline cleaner + * has already advanced the cursor, advancing the cursor here + * overlooks one memcg, but this should be negligibly rare. */ - spin_lock(&zswap_shrink_lock); - do { - memcg =3D mem_cgroup_iter(NULL, zswap_next_shrink, NULL); - zswap_next_shrink =3D memcg; - } while (memcg && !mem_cgroup_tryget_online(memcg)); - spin_unlock(&zswap_shrink_lock); + memcg =3D zswap_mem_cgroup_iter(root_mem_cgroup); =20 if (!memcg) { /* --=20 2.34.1 From nobody Sat Jun 13 02:54:54 2026 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD3D03D812D for ; Mon, 11 May 2026 10:52:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778496739; cv=none; b=RpUacp7Uxjudzy9hdu2YyMswdQsCFhTGQHffu5GEKvjCo+4mODF7NFOw86y2JOPFG2jGsFv+NVl4zBiZEKKSABue0RnnpyukYs2MEDBZs3lsctNMxQjhcBtmqMTp0fsS5c7wik1ghyQMajyUylIHX1ekt2R8tEAepUY52VhgVt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778496739; c=relaxed/simple; bh=zcW9l857uawFCXHEiacEiugUR4qFm/g0jRguMBzUXGQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ScYRcpWdLOXWKr6q1wjC6tSMgptApA4EMGpQrdk00fOFU3etY2kFAtKAUqMQ7mCVETq5YoPET+s/GBIbnNAxuUEyBluI3rk19VYRKM09hirI2M758+r6rXkhgQ2tTKMz/xTLqInY3iY0KV9Eub5uA1VPcfMK7c9gGciKLZds1q8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XQoQZmIz; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XQoQZmIz" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-366070f71adso3678330a91.2 for ; Mon, 11 May 2026 03:52:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778496737; x=1779101537; darn=vger.kernel.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=9fjW4dLeqWiYQdk+4k4SbCTrPHj2YrRsMiHdQFzx+K0=; b=XQoQZmIzHIzom6+33oKV5qAzXF3xOrSBjV7bx2WHu60V5nqLn6AAJTK5uDiYL2KB2q kKxqSnQK37ardhkuKRn7zVw6sQCzDror1sMpPA7QPaGj4FdM/jIKzNtVE5NBAipsFsjp Bj6o+3n2RxaQ/lMAcrQhomEjKJr/fB4mJRi70aL4a060xasJ7bYwSbcSL4YVev/XOTTf u/mW/s5lvar85VHpIP/s9Y8pQiw9/pwaVOhGQ3vQ1fAUmXtXk4slSM1aQ1+bZia5FaAS i5ChahXfdR1lftEjU8JBXpYDpJIxW9f/Gi71JPOdZFX6LvvizvnYG3aug5QeypYwW1Oe gH5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778496737; x=1779101537; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=9fjW4dLeqWiYQdk+4k4SbCTrPHj2YrRsMiHdQFzx+K0=; b=MwRODWYIn3HbshCKlvj1i3y8ib7gHfd2qDmxMStymX10wlGvgsb/6cHb+HcanTiIfD DGNBWdSSutRQkhXEONwPV8B1Ei5pOFhsZUGpde8vmSSOYNz5ETjJQ5nO2ZHaSZkQ/dLM KHmZqs4m9OW+g5KOu/eI0HZeXd1BT3r7W7MrntIIawwETKr5gtWZ/EzW4SFFkWfzaOfo Whxvs9RF/HqE0SZhV1g94UK/Hwj0WXRbUJMy8nEtc5rK1DqZSakpTLtM+2KgowYJIJxx mssf1CXm8pJG9XdpSXAQYTxkVapv+8vd+TvC2S9NjNJJdSwMsLkUZD5qPJNzptD11koo +L7w== X-Forwarded-Encrypted: i=1; AFNElJ9oFFWHtyVHWfYq7O5mOxwErfWStZIf2gl/UIh/G1XXWzlV5/g9zy2R5ek6N8MBHsKmhef5Vq5/CzL/sgw=@vger.kernel.org X-Gm-Message-State: AOJu0YwelUUPzoRc9lpZXDX6eZlf2oKPMpREZdts26QK98Rxn6bDvIHU Uvlw03RVk86xoosaJ8kdr0/WRR/86788kqjxir1wNEU5q47BlpZfuAey X-Gm-Gg: Acq92OHXmEsVU7h0IsPuliIbiwr2OeEDr07J//N+x3aFmIfOFzI5Wurg48sbA1CjGlg e8TmJkgklzXr7qTKOuvF7RjxVfiaFSasIsdssGI2I5vjVAv51Sj1RdLbiV5R9YlKwSjQI3KvzmL 0SRi5C0a1P2rPo2Jdc1HZjUOVJ6HN6hJaKw5fBXcOWOt/gDKhs1hhfm4X16Cl13W2U03BLPCPJU 5s3YK+GsuQjHx0/M9YLjPRNNKmvlJhPkMypdQpiVdWuIArxauCnxMdBoBpKncgCcXmAKRf6Hjh9 9gfDukMoFYymNBCEzECurIFHw6bLwWhFxEiTWgVJmF4mtTfHvNorT6ZH6xDiKd+kNF8hGxIa26l u0GjxCnKDbNN/iDzAdwpjMqWOxQO2TiXsGXqNPQmdXATBiaepv88uTIG5ig6EsIkbvb4tV48z08 m1mPls3bRV2Uv82gDkPNv3tI9REx1yawr+pHSomggXjhSu1F9D/dxEcqNvHAcvjQ== X-Received: by 2002:a17:90b:2e0e:b0:366:469d:98ec with SMTP id 98e67ed59e1d1-366469d99a0mr14958354a91.13.1778496737121; Mon, 11 May 2026 03:52:17 -0700 (PDT) Received: from localhost.localdomain ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2baf1d409eesm98571745ad.32.2026.05.11.03.52.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 11 May 2026 03:52:16 -0700 (PDT) From: Hao Jia To: akpm@linux-foundation.org, tj@kernel.org, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@kernel.org, yosry@kernel.org, mkoutny@suse.com, nphamcs@gmail.com, chengming.zhou@linux.dev, muchun.song@linux.dev, roman.gushchin@linux.dev Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Hao Jia Subject: [PATCH 2/3] mm/zswap: Implement proactive writeback Date: Mon, 11 May 2026 18:51:48 +0800 Message-Id: <20260511105149.75584-3-jiahao.kernel@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260511105149.75584-1-jiahao.kernel@gmail.com> References: <20260511105149.75584-1-jiahao.kernel@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hao Jia Zswap currently writes back pages to backing swap devices reactively, triggered either by memory pressure via the shrinker or by the pool reaching its size limit. This reactive approach offers no precise control over when writeback happens, which can disturb latency-sensitive workloads, and it cannot direct writeback at a specific memory cgroup. However, there are scenarios where users might want to proactively write back cold pages from zswap to the backing swap device, for example, to free up memory for other applications or to prepare for upcoming memory-intensive workloads. Therefore, implement a proactive writeback mechanism for zswap by adding a new cgroup interface file memory.zswap.proactive_writeback within the memory controller. Users can trigger writeback by writing to this file with the following parameters: - max=3D: The maximum amount of memory to write back (optional, default: unlimited). - : The minimum age of the pages to write back. Only pages that have been in zswap for at least this duration will be written back. Example usage: # Write back pages older than 1 hour (3600 seconds), max 10MB echo "max=3D10M 3600" > memory.zswap.proactive_writeback The implementation consists of: 1. Add store_time to struct zswap_entry to record when each entry was inserted into zswap, used for proactive writeback age comparison. 2. Introduce struct zswap_shrink_walk_arg, passed as the cb_arg to list_lru_walk_one() in both the shrinker and proactive paths. It carries the per-invocation cutoff_time and proactive flag down to shrink_memcg_cb(), and propagates the encountered_page_in_swapcache out-signal from the callback back to the caller. 3. Modify the callback function shrink_memcg_cb() to proactively writeback zswap_entries that meet the time threshold. 4. Add zswap_proactive_writeback() as the proactive writeback driver: a per-node batched list_lru_walk_one() loop bounded by the writeback budget. Signed-off-by: Hao Jia --- Documentation/admin-guide/cgroup-v2.rst | 24 ++++ include/linux/zswap.h | 8 ++ mm/memcontrol.c | 76 ++++++++++ mm/zswap.c | 176 ++++++++++++++++++++++-- 4 files changed, 276 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-= guide/cgroup-v2.rst index 6efd0095ed99..05b664b3b3e8 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1908,6 +1908,30 @@ The following nested keys are defined. This setting has no effect if zswap is disabled, and swapping is allowed unless memory.swap.max is set to 0. =20 + memory.zswap.proactive_writeback + A write-only nested-keyed file which exists in non-root cgroups. + + This interface allows proactive writeback of pages from the zswap + pool to the backing swap device. This is useful to offload cold + pages from the zswap pool to the slower swap device. It is only + available if zswap writeback is enabled. + + Users can trigger writeback by writing to this file with the following + parameters: + + - "max=3D" : Optional. The maximum amount of data to write back. + (default: unlimited). Please note that the kernel can over or under + writeback this value. + + - "" : Required. The minimum age of the pages to write back + (in seconds). Only pages that have been in the zswap pool for at + least this amount of time will be written back. + + Example:: + + # Write back pages older than 1 hour (3600 seconds), max 10MB + echo "max=3D10M 3600" > memory.zswap.proactive_writeback + memory.pressure A read-only nested-keyed file. =20 diff --git a/include/linux/zswap.h b/include/linux/zswap.h index efa6b551217e..7a51b4f95017 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -44,6 +44,8 @@ void zswap_lruvec_state_init(struct lruvec *lruvec); void zswap_folio_swapin(struct folio *folio); bool zswap_is_enabled(void); bool zswap_never_enabled(void); +int zswap_proactive_writeback(struct mem_cgroup *root, unsigned long nr_ma= x_writeback, + ktime_t cutoff); #else =20 struct zswap_lruvec_state {}; @@ -78,6 +80,12 @@ static inline bool zswap_never_enabled(void) return true; } =20 +static inline int zswap_proactive_writeback(struct mem_cgroup *root, + unsigned long nr_max_writeback, ktime_t cutoff) +{ + return 0; +} + #endif =20 #endif /* _LINUX_ZSWAP_H */ diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 409c41359dc8..ba7f7b1954a8 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -70,6 +70,7 @@ #include "memcontrol-v1.h" =20 #include +#include =20 #define CREATE_TRACE_POINTS #include @@ -5891,6 +5892,76 @@ static ssize_t zswap_writeback_write(struct kernfs_o= pen_file *of, return nbytes; } =20 +enum { + ZSWAP_WRITEBACK_MAX, + ZSWAP_WRITEBACK_AGE, + ZSWAP_WRITEBACK_ERR, +}; + +static const match_table_t zswap_writeback_tokens =3D { + { ZSWAP_WRITEBACK_MAX, "max=3D%s" }, + { ZSWAP_WRITEBACK_AGE, "%u" }, + { ZSWAP_WRITEBACK_ERR, NULL }, +}; + +static ssize_t zswap_proactive_writeback_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, + loff_t off) +{ + struct mem_cgroup *memcg =3D mem_cgroup_from_css(of_css(of)); + unsigned long nr_max_writeback =3D ULONG_MAX; + substring_t args[MAX_OPT_ARGS]; + unsigned int age_sec; + bool age_set =3D false; + ktime_t cutoff_time; + char *token, *end; + int err; + + if (!mem_cgroup_zswap_writeback_enabled(memcg)) + return -EINVAL; + + buf =3D strstrip(buf); + + while ((token =3D strsep(&buf, " ")) !=3D NULL) { + if (!strlen(token)) + continue; + + switch (match_token(token, zswap_writeback_tokens, args)) { + case ZSWAP_WRITEBACK_MAX: + nr_max_writeback =3D memparse(args[0].from, &end); + if (*end !=3D '\0') + return -EINVAL; + nr_max_writeback >>=3D PAGE_SHIFT; + break; + case ZSWAP_WRITEBACK_AGE: + if (age_set) + return -EINVAL; + + if (match_uint(&args[0], &age_sec)) + return -EINVAL; + age_set =3D true; + break; + default: + return -EINVAL; + } + } + + if (!age_set || !age_sec || !nr_max_writeback) + return -EINVAL; + + cutoff_time =3D ktime_sub(ktime_get_boottime(), + ns_to_ktime((u64)age_sec * NSEC_PER_SEC)); + /* age_sec >=3D uptime: no entry can be that old, skip the walk. */ + if (ktime_to_ns(cutoff_time) <=3D 0) + return nbytes; + + err =3D zswap_proactive_writeback(memcg, nr_max_writeback, cutoff_time); + if (err) + return err; + + return nbytes; +} + static struct cftype zswap_files[] =3D { { .name =3D "zswap.current", @@ -5908,6 +5979,11 @@ static struct cftype zswap_files[] =3D { .seq_show =3D zswap_writeback_show, .write =3D zswap_writeback_write, }, + { + .name =3D "zswap.proactive_writeback", + .flags =3D CFTYPE_NOT_ON_ROOT, + .write =3D zswap_proactive_writeback_write, + }, { } /* terminate */ }; #endif /* CONFIG_ZSWAP */ diff --git a/mm/zswap.c b/mm/zswap.c index 19538d6f169a..1173ac6836fa 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -36,6 +36,7 @@ #include #include #include +#include =20 #include "swap.h" #include "internal.h" @@ -160,6 +161,12 @@ struct zswap_pool { char tfm_name[CRYPTO_MAX_ALG_NAME]; }; =20 +struct zswap_shrink_walk_arg { + ktime_t cutoff_time; + bool proactive; + bool encountered_page_in_swapcache; +}; + /* Global LRU lists shared by all zswap pools. */ static struct list_lru zswap_list_lru; =20 @@ -183,6 +190,7 @@ static struct shrinker *zswap_shrinker; * handle - zsmalloc allocation handle that stores the compressed page data * objcg - the obj_cgroup that the compressed memory is charged to * lru - handle to the pool's lru used to evict pages. + * store_time - Time when the entry was stored, for proactive writeback. */ struct zswap_entry { swp_entry_t swpentry; @@ -192,6 +200,7 @@ struct zswap_entry { unsigned long handle; struct obj_cgroup *objcg; struct list_head lru; + ktime_t store_time; }; =20 static struct xarray *zswap_trees[MAX_SWAPFILES]; @@ -1148,10 +1157,19 @@ static enum lru_status shrink_memcg_cb(struct list_= head *item, struct list_lru_o void *arg) { struct zswap_entry *entry =3D container_of(item, struct zswap_entry, lru); - bool *encountered_page_in_swapcache =3D (bool *)arg; - swp_entry_t swpentry; + struct zswap_shrink_walk_arg *walk_arg =3D arg; + bool proactive_wb =3D walk_arg && walk_arg->proactive; enum lru_status ret =3D LRU_REMOVED_RETRY; int writeback_result; + swp_entry_t swpentry; + + /* + * For proactive writeback, rotate young entries to the LRU tail + * so that subsequent list_lru_walk_one() batches start past + * them. + */ + if (proactive_wb && ktime_after(entry->store_time, walk_arg->cutoff_time)) + return LRU_ROTATE; =20 /* * Second chance algorithm: if the entry has its referenced bit set, give= it @@ -1160,7 +1178,9 @@ static enum lru_status shrink_memcg_cb(struct list_he= ad *item, struct list_lru_o */ if (entry->referenced) { entry->referenced =3D false; - return LRU_ROTATE; + /* Proactive writeback is an explicit hint; don't rotate. */ + if (!proactive_wb) + return LRU_ROTATE; } =20 /* @@ -1214,9 +1234,9 @@ static enum lru_status shrink_memcg_cb(struct list_he= ad *item, struct list_lru_o * into the warmer region. We should terminate shrinking (if we're in th= e dynamic * shrinker context). */ - if (writeback_result =3D=3D -EEXIST && encountered_page_in_swapcache) { + if (writeback_result =3D=3D -EEXIST && walk_arg) { ret =3D LRU_STOP; - *encountered_page_in_swapcache =3D true; + walk_arg->encountered_page_in_swapcache =3D true; } } else { zswap_written_back_pages++; @@ -1228,8 +1248,12 @@ static enum lru_status shrink_memcg_cb(struct list_h= ead *item, struct list_lru_o static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc) { + struct zswap_shrink_walk_arg walk_arg =3D { + .cutoff_time =3D KTIME_MAX, + .proactive =3D false, + .encountered_page_in_swapcache =3D false, + }; unsigned long shrink_ret; - bool encountered_page_in_swapcache =3D false; =20 if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(sc->memcg)) { @@ -1238,9 +1262,9 @@ static unsigned long zswap_shrinker_scan(struct shrin= ker *shrinker, } =20 shrink_ret =3D list_lru_shrink_walk(&zswap_list_lru, sc, &shrink_memcg_cb, - &encountered_page_in_swapcache); + &walk_arg); =20 - if (encountered_page_in_swapcache) + if (walk_arg.encountered_page_in_swapcache) return SHRINK_STOP; =20 return shrink_ret ? shrink_ret : SHRINK_STOP; @@ -1508,6 +1532,7 @@ static bool zswap_store_page(struct page *page, entry->swpentry =3D page_swpentry; entry->objcg =3D objcg; entry->referenced =3D true; + entry->store_time =3D ktime_get_boottime(); if (entry->length) { INIT_LIST_HEAD(&entry->lru); zswap_lru_add(&zswap_list_lru, entry); @@ -1681,6 +1706,141 @@ int zswap_load(struct folio *folio) return 0; } =20 +/* Cap LRU scan to this many entries per page of remaining budget. */ +#define ZSWAP_PROACTIVE_WB_SCAN_RATIO 16UL +/* + * Batch size for proactive writeback, used both as the per-memcg + * writeback target in the outer memcg loop and as the per-walk budget + * for list_lru_walk_one(). + */ +#define ZSWAP_PROACTIVE_WB_BATCH 128UL + +/* + * Walk @memcg's per-node LRUs, writing back entries older than @cutoff + * up to @nr_to_write pages. Returns the number of pages written back, + * or -ENOENT if @memcg is a zombie or has writeback disabled. + */ +static long zswap_proactive_shrink_memcg(struct mem_cgroup *memcg, + ktime_t cutoff, + unsigned long nr_to_write) +{ + unsigned long nr_written =3D 0; + int nid; + + if (!mem_cgroup_zswap_writeback_enabled(memcg)) + return -ENOENT; + + if (!mem_cgroup_online(memcg)) + return -ENOENT; + + for_each_node_state(nid, N_NORMAL_MEMORY) { + struct zswap_shrink_walk_arg walk_arg =3D { + .cutoff_time =3D cutoff, + .proactive =3D true, + .encountered_page_in_swapcache =3D false, + }; + unsigned long nr_to_scan, nr_scanned =3D 0; + + /* + * Cap by LRU length: bounds rewalks when entries keep + * rotating (young or referenced). + */ + nr_to_scan =3D list_lru_count_one(&zswap_list_lru, nid, memcg); + if (!nr_to_scan) + continue; + + /* + * Cap by SCAN_RATIO * remaining budget: bounds scan cost + * to the remaining writeback budget. + */ + nr_to_scan =3D min(nr_to_scan, + (nr_to_write - nr_written) * ZSWAP_PROACTIVE_WB_SCAN_RATIO); + + while (nr_scanned < nr_to_scan) { + unsigned long nr_to_walk =3D min(ZSWAP_PROACTIVE_WB_BATCH, + nr_to_scan - nr_scanned); + + if (signal_pending(current)) + return nr_written; + + /* + * Account the committed budget rather than the walker's + * actual delta: if the list empties under us the walker + * visits nothing and nr_scanned would never advance. + */ + nr_scanned +=3D nr_to_walk; + + nr_written +=3D list_lru_walk_one(&zswap_list_lru, nid, memcg, + &shrink_memcg_cb, &walk_arg, + &nr_to_walk); + + if (nr_written >=3D nr_to_write) + return nr_written; + if (walk_arg.encountered_page_in_swapcache) + break; + + cond_resched(); + } + } + + return nr_written; +} + +int zswap_proactive_writeback(struct mem_cgroup *root, + unsigned long nr_max_writeback, + ktime_t cutoff) +{ + struct mem_cgroup *memcg; + unsigned long nr_written =3D 0; + int failures =3D 0, attempts =3D 0; + + /* + * Writeback will be aborted with -EAGAIN if @nr_written is still + * zero and we encounter the following MAX_RECLAIM_RETRIES times: + * - No writeback-candidate memcgs found in a subtree walk. + * - A writeback-candidate memcg wrote back zero pages. + */ + while (nr_written < nr_max_writeback) { + unsigned long nr_to_write; + long shrunk; + + if (signal_pending(current)) + return -EINTR; + + memcg =3D zswap_mem_cgroup_iter(root); + + if (!memcg) { + /* + * Continue without incrementing failures if we found + * candidate memcgs in the last subtree walk. + */ + if (!attempts && ++failures =3D=3D MAX_RECLAIM_RETRIES) + goto out; + attempts =3D 0; + continue; + } + + nr_to_write =3D min(nr_max_writeback - nr_written, + ZSWAP_PROACTIVE_WB_BATCH); + shrunk =3D zswap_proactive_shrink_memcg(memcg, cutoff, nr_to_write); + mem_cgroup_put(memcg); + + /* Writeback-disabled or offline: skip without counting. */ + if (shrunk =3D=3D -ENOENT) + continue; + + ++attempts; + if (shrunk > 0) + nr_written +=3D shrunk; + else if (++failures =3D=3D MAX_RECLAIM_RETRIES) + goto out; + + cond_resched(); + } +out: + return nr_written ? 0 : -EAGAIN; +} + void zswap_invalidate(swp_entry_t swp) { pgoff_t offset =3D swp_offset(swp); --=20 2.34.1 From nobody Sat Jun 13 02:54:54 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 567593DB626 for ; Mon, 11 May 2026 10:52:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778496746; cv=none; b=Ktw/XZ++IgOfEuXCGxy2FJACTl9GA3rMxRzPvZWHwyg1m4SwZGx33Tbsimp//GP+sbVynBf+BN2+W890amR8mWvdM9HeFKUOQHRrYX3BVKwnjQAwIsnnBuXZ4pAE443muzjVjZBgfSThPRjxVyQUx4m9pTSZxRH/l28tx43z84o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778496746; c=relaxed/simple; bh=Leh1MEBHBCTSKNFe7cTJpIBEvX0LcJ1AgIVCFnqVjmE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FR2mDWE7YSDg9Rr+ZpTGlO0h0iTtUzs51RUxSIK4pneG+BATi1sPQaO8wRo3GY8SqyWA0+JXdkK2m55gCWL4gc5F19xW8bS7cuebbeW2xQTP+F/WOES7eb+5MPwoCatgjeLKw139OnbfIlW0YzXJBLENGRNcUTeX9fYDmtikLWo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Gw796Hjk; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Gw796Hjk" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2b4650d5f5cso15501805ad.0 for ; Mon, 11 May 2026 03:52:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778496745; x=1779101545; darn=vger.kernel.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=n6WtgpEfVE+ktty/21d7K0edFsjegj7CB2gJFywRNtg=; b=Gw796HjkLoA35cR6fFwk8989XD1QCyOm5rl0t6e1HLhIbJSeGfbso0SecXLqqUMeqT tpXGoFDJPpl09luRmkccF6LKJTl/vVJM4yzMzISPOo3V7kn2/O5sEalxReVDshyy53Y8 MwoL2hew/cNeevylvP3hGyCXti8fEXBqOtlVWDnfATRs9oYfelV4GE5qnMWQ8dysDoIG 2m1D2csGZLeuVUuriS9lIOCqfFk2zA9hxRdzDe5EKXITo1pnZXFvDe++tTtRVeCwLLvH H+vRjSddq4z9iClNRs2cFNkJQ1PLmErgjpBx4pmJXFEX4ykVTYgZSzr3YONrGRzgeCr4 k+/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778496745; x=1779101545; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=n6WtgpEfVE+ktty/21d7K0edFsjegj7CB2gJFywRNtg=; b=gYBJYKaTyWUzTD4QZ8wm7KHbq/vMM4gOQAj/2GIm5LEcHg3a0HPgCK6xOyC+uC39vP gD2nbNnr1IBSX0yq/hDN7Av4qSOA3zVy5f6rXv4Qr9aqXIy3UwOkcrlGiYYg98+KKI/r tQ9lEYorvH4u2zcOvmrp6gJMrSs7I5Cd91yub9vBSgY5NLpErA6QEh6WCEbjAVGvp8Lk VNBYEfsenwgTYoOb1223JTpi3q9VhdCp4AtSEu5D4a916BalqfAHlg3QGq+2Tx7CghFZ UbDt2WnxScOqLRw8onIwvxYMqWxY0L+z1m9/A/93OuQrdXL9kFM0VkroI4X33uUXZhzs 2RXw== X-Forwarded-Encrypted: i=1; AFNElJ8ts2C1UMTAzvLXmyo/SC+5G+G4xYBJrbT12FdiIoETlE0YhdD0/LH9MLAz/mIpN0raSiDf3Cmsl5Pq8D8=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6b+tPcVgsRVI9jDn/R/D+vkB19WR4L/wTtTsvZVSVWHn1BiPL Hytc3S8GOs1tonS2oirxpY6MhfFGxV4l9X8KpLQKkw7um9gI37rm6iP/ X-Gm-Gg: Acq92OGkYXcDOzi8ed1xF41Fo1BQZMZtZ2mDuCm8dcpQdzqdNMvbDlm0FoAf6+H66Yn tHYvQPgdKXoDz54QTB/DbUo1IRReD9aGo63Ii+qbRN9YYosr6IijyxClFxtiqOdvYFn0jVqt4l2 Q2HgE01OM3f5OU2sBtWNuSUx1JEUyL2vm8nheRN3zOfBMScRZwKBfgR7w2yB1/paXTqCsgPrLG+ sI6UeNj6kFUyAxovfmrzXuGTYcMgnIZNL3jFUNmWW/2fuRXvvtIOTMOQ9DMWwuvR8VIY5c5gfLu hrp8mTRjZDGK5vruVvcPiJ06UAJNyMbmnhppcDpDyJQkJUFhXx9FiMnBekzYiN9lgsW/TcW7yC/ KgnDnV24shMQ23Xi0RCVmEBBa12QWW/mEAFiULgEpeIrxx9RJpL0+LzJfQR3pLy3MfDLSxxwLx2 ADvYzjnSzo3RbZbEV7AgXrOLnV61HN/rsPBIQcwWL6xmcQbFB358pfbzc29+7ckQ== X-Received: by 2002:a17:902:ee92:b0:2b4:59bf:5728 with SMTP id d9443c01a7336-2ba798c279emr168316355ad.25.1778496744462; Mon, 11 May 2026 03:52:24 -0700 (PDT) Received: from localhost.localdomain ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2baf1d409eesm98571745ad.32.2026.05.11.03.52.18 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 11 May 2026 03:52:23 -0700 (PDT) From: Hao Jia To: akpm@linux-foundation.org, tj@kernel.org, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@kernel.org, yosry@kernel.org, mkoutny@suse.com, nphamcs@gmail.com, chengming.zhou@linux.dev, muchun.song@linux.dev, roman.gushchin@linux.dev Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Hao Jia Subject: [PATCH 3/3] mm/zswap: Add per-memcg stat for proactive writeback Date: Mon, 11 May 2026 18:51:49 +0800 Message-Id: <20260511105149.75584-4-jiahao.kernel@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260511105149.75584-1-jiahao.kernel@gmail.com> References: <20260511105149.75584-1-jiahao.kernel@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hao Jia Currently, zswap writeback can be triggered by either the pool limit being hit or by the proactive writeback mechanism. However, the existing 'zswpwb' metric in memory.stat and /proc/vmstat counts all written back pages, making it difficult to distinguish between pages written back due to the pool limit and those written back proactively. Add a new statistic 'zswpwb_proactive' to memory.stat and /proc/vmstat. This counter tracks the number of pages written back due to proactive writeback. This allows users to better monitor and tune the proactive writeback mechanism. Signed-off-by: Hao Jia --- Documentation/admin-guide/cgroup-v2.rst | 4 ++++ include/linux/vm_event_item.h | 1 + mm/memcontrol.c | 1 + mm/vmstat.c | 1 + mm/zswap.c | 11 +++++++++-- 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-= guide/cgroup-v2.rst index 05b664b3b3e8..29a189b18efc 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1734,6 +1734,10 @@ The following nested keys are defined. zswpwb Number of pages written from zswap to swap. =20 + zswpwb_proactive + Number of pages written from zswap to swap by proactive + writeback. This is a subset of zswpwb. + zswap_incomp Number of incompressible pages currently stored in zswap without compression. These pages could not be compressed to diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index 03fe95f5a020..7a5bee0a20b6 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -138,6 +138,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, ZSWPIN, ZSWPOUT, ZSWPWB, + ZSWPWB_PROACTIVE, #endif #ifdef CONFIG_X86 DIRECT_MAP_LEVEL2_SPLIT, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ba7f7b1954a8..830d895e77c3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -572,6 +572,7 @@ static const unsigned int memcg_vm_event_stat[] =3D { ZSWPIN, ZSWPOUT, ZSWPWB, + ZSWPWB_PROACTIVE, #endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE THP_FAULT_ALLOC, diff --git a/mm/vmstat.c b/mm/vmstat.c index f534972f517d..66fd06d1bb01 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1452,6 +1452,7 @@ const char * const vmstat_text[] =3D { [I(ZSWPIN)] =3D "zswpin", [I(ZSWPOUT)] =3D "zswpout", [I(ZSWPWB)] =3D "zswpwb", + [I(ZSWPWB_PROACTIVE)] =3D "zswpwb_proactive", #endif #ifdef CONFIG_X86 [I(DIRECT_MAP_LEVEL2_SPLIT)] =3D "direct_map_level2_splits", diff --git a/mm/zswap.c b/mm/zswap.c index 1173ac6836fa..bf23c46e838e 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1048,7 +1048,8 @@ static bool zswap_decompress(struct zswap_entry *entr= y, struct folio *folio) * freed. */ static int zswap_writeback_entry(struct zswap_entry *entry, - swp_entry_t swpentry) + swp_entry_t swpentry, + bool proactive) { struct xarray *tree; pgoff_t offset =3D swp_offset(swpentry); @@ -1108,6 +1109,12 @@ static int zswap_writeback_entry(struct zswap_entry = *entry, if (entry->objcg) count_objcg_events(entry->objcg, ZSWPWB, 1); =20 + if (proactive) { + count_vm_event(ZSWPWB_PROACTIVE); + if (entry->objcg) + count_objcg_events(entry->objcg, ZSWPWB_PROACTIVE, 1); + } + zswap_entry_free(entry); =20 /* folio is up to date */ @@ -1223,7 +1230,7 @@ static enum lru_status shrink_memcg_cb(struct list_he= ad *item, struct list_lru_o */ spin_unlock(&l->lock); =20 - writeback_result =3D zswap_writeback_entry(entry, swpentry); + writeback_result =3D zswap_writeback_entry(entry, swpentry, proactive_wb); =20 if (writeback_result) { zswap_reject_reclaim_fail++; --=20 2.34.1