From: Jianyue Wu <wujianyue000@gmail.com>
To: Johannes Weiner <hannes@cmpxchg.org>,
Yosry Ahmed <yosry@kernel.org>, Nhat Pham <nphamcs@gmail.com>,
Chengming Zhou <chengming.zhou@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Chris Li <chrisl@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Jianyue Wu <wujianyue000@gmail.com>
Subject: [PATCH v6 3/3] mm/zswap: reference the pool by id to shrink struct zswap_entry
Date: Sun, 06 Sep 2026 15:47:19 +0800 [thread overview]
Message-ID: <20260906-shrink_zswap_entry_v6-v6-3-ac4cf61565fb@gmail.com> (raw)
In-Reply-To: <20260906-shrink_zswap_entry_v6-v6-0-ac4cf61565fb@gmail.com>
struct zswap_entry is one allocation per stored page, so its size is
pure overhead. It currently embeds an 8-byte pool pointer, even though
the live pools now sit in an allocating xarray keyed by a small integer
id that fits in a u8.
Replace the per-entry pool pointer with that u8 id and resolve it
through the xarray with xa_load(). xa_load() does its own RCU-protected
lookup, so the caller needs no rcu_read_lock() section of its own. The
resolved pool stays valid because a live entry pins it via percpu_ref
(taken in zswap_store_page()), so its id cannot be reused. A live entry
never uses the reserved id 0, so a zeroed id resolves to NULL and trips
a WARN rather than aliasing a live pool.
The u8 fits in the padding after the bool referenced field, shrinking
the entry from 56 to 48 bytes on 64-bit. This raises objs_per_slab from
73 to 85 and saves about 2MiB of metadata per 1GiB of data held in
zswap.
Suggested-by: Chris Li <chrisl@kernel.org>
Signed-off-by: Jianyue Wu <wujianyue000@gmail.com>
---
mm/zswap.c | 37 ++++++++++++++++++++++++++++++-------
1 file changed, 30 insertions(+), 7 deletions(-)
diff --git a/mm/zswap.c b/mm/zswap.c
index 86db023d63ed..253eebb971b9 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -194,7 +194,7 @@ static struct shrinker *zswap_shrinker;
* writeback logic. The entry is only reclaimed by the writeback
* logic if referenced is unset. See comments in the shrinker
* section for context.
- * pool - the zswap_pool the entry's data is in
+ * pool_idx - id of the zswap_pool that the entry's data is in.
* 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.
@@ -203,12 +203,22 @@ struct zswap_entry {
swp_entry_t swpentry;
unsigned int length;
bool referenced;
- struct zswap_pool *pool;
+ u8 pool_idx;
unsigned long handle;
struct obj_cgroup *objcg;
struct list_head lru;
};
+/*
+ * No RCU section is needed around the returned pointer: a stored entry pins
+ * its pool via percpu_ref (taken in zswap_store_page()), so the id cannot be
+ * reused under us. Callers WARN and handle a NULL from a corrupt pool_idx.
+ */
+static struct zswap_pool *zswap_entry_pool(struct zswap_entry *entry)
+{
+ return xa_load(&zswap_pools, entry->pool_idx);
+}
+
static struct xarray *zswap_trees[MAX_SWAPFILES];
static unsigned int nr_zswap_trees[MAX_SWAPFILES];
@@ -759,9 +769,13 @@ static void zswap_entry_cache_free(struct zswap_entry *entry)
*/
static void zswap_entry_free(struct zswap_entry *entry)
{
+ struct zswap_pool *pool = zswap_entry_pool(entry);
+
zswap_lru_del(entry);
- zs_free(entry->pool->zs_pool, entry->handle);
- zswap_pool_put(entry->pool);
+ if (!WARN_ON_ONCE(!pool)) {
+ zs_free(pool->zs_pool, entry->handle);
+ zswap_pool_put(pool);
+ }
if (entry->objcg) {
obj_cgroup_uncharge_zswap(entry->objcg, entry->length);
obj_cgroup_put(entry->objcg);
@@ -918,12 +932,15 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
static bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)
{
- struct zswap_pool *pool = entry->pool;
+ struct zswap_pool *pool = zswap_entry_pool(entry);
struct scatterlist input[2]; /* zsmalloc returns an SG list 1-2 entries */
struct scatterlist output;
struct crypto_acomp_ctx *acomp_ctx;
int ret = 0, dlen;
+ if (WARN_ON_ONCE(!pool))
+ return false;
+
acomp_ctx = raw_cpu_ptr(pool->acomp_ctx);
mutex_lock(&acomp_ctx->mutex);
zs_obj_read_sg_begin(pool->zs_pool, entry->handle, input, entry->length);
@@ -959,7 +976,7 @@ static bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)
pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",
swp_type(entry->swpentry),
swp_offset(entry->swpentry),
- entry->pool->tfm_name,
+ pool->tfm_name,
entry->length, dlen);
return false;
}
@@ -1417,6 +1434,13 @@ static bool zswap_store_page(struct page *page,
if (!zswap_compress(page, entry, pool))
goto compress_failed;
+ /*
+ * Set pool_idx before the xa_store() below publishes the entry, or a
+ * concurrent reader could resolve a stale pool_idx left by slab reuse
+ * to an unrelated live pool.
+ */
+ entry->pool_idx = pool->idx;
+
old = xa_store(swap_zswap_tree(page_swpentry),
swp_offset(page_swpentry),
entry, GFP_KERNEL);
@@ -1462,7 +1486,6 @@ static bool zswap_store_page(struct page *page,
* The publishing order matters to prevent writeback from seeing
* an incoherent entry.
*/
- entry->pool = pool;
entry->swpentry = page_swpentry;
entry->objcg = objcg;
entry->referenced = true;
--
2.43.0
next prev parent reply other threads:[~2026-09-06 7:47 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-30 11:47 [RFC PATCH v4 0/3] mm/zswap: shrink zswap_entry via a fixed pool index Jianyue Wu
2026-08-30 11:47 ` [RFC PATCH v4 1/3] mm/zswap: release retired pools via queue_rcu_work() instead of synchronize_rcu() Jianyue Wu
2026-08-31 15:20 ` Yosry Ahmed
2026-09-01 14:33 ` Jianyue Wu
2026-09-01 15:38 ` Johannes Weiner
2026-09-02 0:53 ` Jianyue Wu
2026-08-30 11:47 ` [RFC PATCH v4 2/3] mm/zswap: replace the zswap_pools list with a fixed pools array Jianyue Wu
2026-08-31 15:28 ` Yosry Ahmed
2026-09-01 16:13 ` Johannes Weiner
2026-09-02 0:50 ` Jianyue Wu
2026-09-03 12:59 ` Jianyue Wu
2026-08-30 11:47 ` [RFC PATCH v4 3/3] mm/zswap: reference the pool by index to shrink struct zswap_entry Jianyue Wu
2026-08-31 15:30 ` Yosry Ahmed
2026-09-04 13:24 ` [PATCH v5 0/3] mm/zswap: shrink zswap_entry via a pool id Jianyue Wu
2026-09-04 13:24 ` [PATCH v5 1/3] mm/zswap: release retired pools via queue_rcu_work() instead of synchronize_rcu() Jianyue Wu
2026-09-04 13:24 ` [PATCH v5 2/3] mm/zswap: replace the zswap_pools list with an allocating xarray Jianyue Wu
2026-09-04 16:04 ` Yosry Ahmed
2026-09-05 13:15 ` Jianyue Wu
2026-09-04 13:24 ` [PATCH v5 3/3] mm/zswap: reference the pool by id to shrink struct zswap_entry Jianyue Wu
2026-09-04 15:38 ` Yosry Ahmed
2026-09-05 13:20 ` Jianyue Wu
2026-09-06 7:47 ` [PATCH v6 0/3] mm/zswap: shrink zswap_entry via a pool id Jianyue Wu
2026-09-06 7:47 ` [PATCH v6 1/3] mm/zswap: release retired pools via queue_rcu_work() instead of synchronize_rcu() Jianyue Wu
2026-09-06 7:47 ` [PATCH v6 2/3] mm/zswap: replace the zswap_pools list with an allocating xarray Jianyue Wu
2026-09-06 9:32 ` Yosry Ahmed
2026-09-06 7:47 ` Jianyue Wu [this message]
2026-09-06 9:33 ` [PATCH v6 3/3] mm/zswap: reference the pool by id to shrink struct zswap_entry Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260906-shrink_zswap_entry_v6-v6-3-ac4cf61565fb@gmail.com \
--to=wujianyue000@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®