From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 4C93A33D4EE for ; Mon, 25 May 2026 12:23:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779711785; cv=none; b=k0TWe4OrAkVuQE9yMhK2QImGOpkUBoXC6aY/nXTTf76/nFwiLNF4m6OtxUhnjIeDoOgp9WCmIUHwft3h+5c9fRfIebl7atcjKopuxeEQZPeJ7YGaky8amn37Sk03QXt68ByCt0IebGgmpBPqBTb7gcbPtRkHqo26mim5lMVlODw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779711785; c=relaxed/simple; bh=UBB195Ozt5QE0T4ary3zwUYu4vqp88ETqIcNr3qHkjE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fFFr/Y8fH+SEghfY0G/QKL214PtbzJAfbVg89cqBQuOKKcPUAo9V3qMTMe18J8XfUYIC74PbeRHsnNeP4J+2XnyCzayyt6Tc4qCA7zr4hKlZqRMKxI8AeYoJLeops1mZOQegdQa14BLlO6dctSpliwFcL4aOlgSetJc6c54vNhw= 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=rLYw8XLH; arc=none smtp.client-ip=209.85.216.47 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="rLYw8XLH" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-36a35e4eefeso2977016a91.1 for ; Mon, 25 May 2026 05:23:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779711784; x=1780316584; 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=QtMgcDN043IrMH4z5Dydpux33DpRRQAjJ0E9E7yR9Fo=; b=rLYw8XLHNNa5aV53Jeh3nr3yqZW91pi6RrJ9EoHsvjiP0EMaN+tS/rnZtZ2pdiHWN+ +DHjmK3ib04Bko4VaGOoCR4PA2XSdQFS/8KSwPSNi7t/8xXSQnyvt5n0QKZ1pqP2za+3 5ad8hDUUeuO3DH6AkC49ZywIK3RxducnDCPAcN+feJaHfq0FpzuF2hRxk/DPH+s4tav9 r3/ftj+SGUSqBlusNkQwDSoiZXXt/IP8RYa8XMN6AAVkTHjuISVeZ0g1lzHgeIJ6m/kC Pv+wOLmcUTsrkeeD5sv3NWWXc5Pyth1BDheuWtLjsax3H5XoTfLj6kochEaoXqM4Ilph vikA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779711784; x=1780316584; 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=QtMgcDN043IrMH4z5Dydpux33DpRRQAjJ0E9E7yR9Fo=; b=Y/1/1BISG7514AYyNgdHWHErCnF0+VubmwwInWJo7k8kyBjnfymqzNnar/wW8IbBQy Rxjinm+8uTwInpsrPle1E2eKsFncVEKe/1K6/QxjHBU+gwcGy3eubhZwv3hx5c1lEor2 U9G88OdzTJp5MVAbT0leykMLMw5cuaLmD0pOXI6v1oX+YOBUopU2agCJsj33VnG5TfPk LY2Oiq1zNpDrMmOfzvGAZdUbAuVcnOyS1yxGCTk6XspRiXaklcNVfdfSvGg7VfEZx7Qk xjmUSVufIyC/CDo4biTRHN91y20MIDTSFIcgF60X0P3iFY0Avy1Jxne2AKZ6nztAZH4c z6RQ== X-Forwarded-Encrypted: i=1; AFNElJ+B6KbyWRLWdHEOgCltKNDWRHmCuE5fj+XmePrZiKB4ZIgD3X3D7079tDUUZKsl6XJaZH+7hBiMfFk=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2Qjcl4nJae9KmNJeAiOOpZRmaW+DoW7WKWiZAEAN4QoQ2JVS2 n67O5OPqq4QU6MaC1TpznblI0SGwoKDwRdgm5skMZGasTJmhhI2WB+Cx X-Gm-Gg: Acq92OGo9fWo3ZxP6Sai2yaZ1vRVDgBZJSwjcbdKyOvKrMP8Myu2DQ0FJmD+zWIbyvJ C+Y93XodtW/k2dwBavMTCaVXnh+3nG3kHkxlez9VvcIcO8s0oWdJo4TQRJpdgAXxjO+Dy/XVCCJ kFRzvz1GBag6EiOnSbNcEmAeXIxpWOu/zmbElyfYt+QFom/lqYw2fBVWN+E2OeQDfl1lSJ2yXPu T6UJ3buKe4DhU/y3LDo83ZeU1ccbMzFFacs1S4vDVU4RjyCQFBL975Vf1WZm/1vul0vB0SBUKTQ hMiXcOT6O9759HpRbfhMgvGzsIeHNUs4XukyDPw5wtF0px6PdS9PEal8PyaKBNkZgs/Tuwjf4qA l59fdftpmDskhQ/a1dP1H6OoIckSZdqkep+akFRo3OSyIgHUXm/XEVrtnUlpD0xOc+WIHCSFM43 aQXhboC7wqEPzcnbFdfvV3ovD7PCHpKBDi1XOChKLO6f4chWZNsPB/cb33TBMHmw== X-Received: by 2002:a17:90a:d44f:b0:368:763a:17b8 with SMTP id 98e67ed59e1d1-36a6741dd0bmr9210428a91.2.1779711783550; Mon, 25 May 2026 05:23:03 -0700 (PDT) Received: from localhost.localdomain ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36a72c913a1sm8999131a91.15.2026.05.25.05.22.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 25 May 2026 05:23:03 -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 v2 1/4] mm/zswap: Make shrink_worker writeback cursor per-memcg Date: Mon, 25 May 2026 20:22:39 +0800 Message-Id: <20260525122242.36127-2-jiahao.kernel@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260525122242.36127-1-jiahao.kernel@gmail.com> References: <20260525122242.36127-1-jiahao.kernel@gmail.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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 | 3 + include/linux/zswap.h | 9 +++ mm/memcontrol.c | 3 + mm/zswap.c | 119 ++++++++++++++++++++++++++----------- 4 files changed, 98 insertions(+), 36 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index dc3fa687759b..b8323c8d6565 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -228,6 +228,9 @@ struct mem_cgroup { * swap, and from being swapped out on zswap store failures. */ bool zswap_writeback; + + /* Per-memcg writeback cursor */ + struct zswap_wb_iter zswap_wb_iter; #endif /* 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; #ifdef CONFIG_ZSWAP +/* 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 = 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..6519f646b496 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; -/* 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; @@ -717,28 +714,88 @@ void zswap_folio_swapin(struct folio *folio) } } -/* - * 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. * - * 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 cursor is retained across invocations, so successive calls walk + * @root's subtree cyclically in pre-order and, after %NULL, restart + * from the beginning. * - * shrink_worker() must handle the case where this function releases - * the reference of memcg being shrunk. + * The returned memcg carries an extra reference; release it with + * mem_cgroup_put(). + * + * 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 == memcg) { + struct mem_cgroup *memcg; + + if (mem_cgroup_disabled()) + return NULL; + + spin_lock(&root->zswap_wb_iter.lock); + do { + memcg = mem_cgroup_iter(root, root->zswap_wb_iter.pos, NULL); + root->zswap_wb_iter.pos = 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 == dead_memcg) { do { - zswap_next_shrink = mem_cgroup_iter(NULL, zswap_next_shrink, NULL); - } while (zswap_next_shrink && !mem_cgroup_online(zswap_next_shrink)); + root->zswap_wb_iter.pos = + 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); +} + +/* + * 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 = memcg; + + do { + __zswap_memcg_offline_cleanup(parent, memcg); + } while ((parent = 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 +1385,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 = mem_cgroup_iter(NULL, zswap_next_shrink, NULL); - zswap_next_shrink = memcg; - } while (memcg && !mem_cgroup_tryget_online(memcg)); - spin_unlock(&zswap_shrink_lock); + memcg = zswap_mem_cgroup_iter(root_mem_cgroup); if (!memcg) { /* -- 2.34.1