[PATCH v5 6/9] mm: zswap: Obtain folio's wb_enabled status once in zswap_store().
From: Kanchana P. Sridhar
Date: Tue Dec 01 2026 - 00:37:54 EST
Instead of querying mem_cgroup_zswap_writeback_enabled() in each call to
zswap_compress() for large folios, obtain the folio memcg's writeback
enabled status once in zswap_store(), and pass it to zswap_store_page(),
which passes it to zswap_compress(), thus eliminating redundant
computes.
Signed-off-by: Kanchana P. Sridhar <kanchanapsridhar2026@xxxxxxxxx>
---
mm/zswap.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/mm/zswap.c b/mm/zswap.c
index cdca497ef8db..c0c00696599b 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -890,7 +890,8 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
}
static bool zswap_compress(struct folio *folio, long index,
- struct zswap_entry *entry, struct zswap_pool *pool)
+ struct zswap_entry *entry, struct zswap_pool *pool,
+ bool wb_enabled)
{
struct zswap_comp_ctx *comp_ctx;
struct scatterlist input, output;
@@ -942,12 +943,8 @@ static bool zswap_compress(struct folio *folio, long index,
* to the active LRU list in the case.
*/
if (comp_ret) {
- rcu_read_lock();
- if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) {
- rcu_read_unlock();
+ if (!wb_enabled)
goto unlock;
- }
- rcu_read_unlock();
comp_ret = 0;
dlen = PAGE_SIZE;
dst = kmap_local_folio(folio, index * PAGE_SIZE);
@@ -1532,7 +1529,8 @@ static void shrink_worker(struct work_struct *w)
static bool zswap_store_page(struct folio *folio, long index,
struct obj_cgroup *objcg,
- struct zswap_pool *pool)
+ struct zswap_pool *pool,
+ bool wb_enabled)
{
swp_entry_t page_swpentry = folio_swap_entry(folio, index);
struct zswap_entry *entry, *old;
@@ -1545,7 +1543,7 @@ static bool zswap_store_page(struct folio *folio, long index,
return false;
}
- if (!zswap_compress(folio, index, entry, pool))
+ if (!zswap_compress(folio, index, entry, pool, wb_enabled))
goto compress_failed;
/*
@@ -1624,6 +1622,7 @@ bool zswap_store(struct folio *folio)
struct mem_cgroup *memcg = NULL;
struct zswap_pool *pool;
bool ret = false;
+ bool wb_enabled;
long index;
VM_WARN_ON_ONCE(!folio_test_locked(folio));
@@ -1658,8 +1657,12 @@ bool zswap_store(struct folio *folio)
mem_cgroup_put(memcg);
}
+ rcu_read_lock();
+ wb_enabled = mem_cgroup_zswap_writeback_enabled(folio_memcg(folio));
+ rcu_read_unlock();
+
for (index = 0; index < nr_pages; ++index) {
- if (!zswap_store_page(folio, index, objcg, pool))
+ if (!zswap_store_page(folio, index, objcg, pool, wb_enabled))
goto put_pool;
}
--
2.39.5