[PATCH v5 5/6] mm/zswap: Add per-memcg stat for proactive writeback
From: Hao Jia
Date: Mon Jun 29 2026 - 07:33:57 EST
From: Hao Jia <jiahao1@xxxxxxxxxxx>
Add a new stat zswpwb_proactive_b to memory.stat. This counter is
incremented by entry->length during proactive writebacks triggered
via the source=zswap key in memory.reclaim. It tracks the
compressed size (in bytes) of pages proactively written back from
zswap to swap, allowing users to better monitor and tune the
proactive writeback mechanism.
Signed-off-by: Hao Jia <jiahao1@xxxxxxxxxxx>
---
Documentation/admin-guide/cgroup-v2.rst | 4 ++++
include/linux/memcontrol.h | 1 +
mm/memcontrol.c | 3 +++
mm/zswap.c | 4 +++-
4 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index bbcc9695aa8d..e1f6a4729a65 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1748,6 +1748,10 @@ The following nested keys are defined.
zswpwb
Number of pages written from zswap to swap.
+ zswpwb_proactive_b
+ Bytes of compressed data proactively written back from
+ zswap to swap via the memory.reclaim source=zswap key.
+
zswap_incomp
Number of incompressible pages currently stored in zswap
without compression. These pages could not be compressed to
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index e1f46a0016fc..56580b264dc4 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -40,6 +40,7 @@ enum memcg_stat_item {
MEMCG_ZSWAP_B,
MEMCG_ZSWAPPED,
MEMCG_ZSWAP_INCOMP,
+ MEMCG_ZSWPWB_PROACTIVE_B,
MEMCG_NR_STAT,
};
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index d20ffc827306..d81c34484bca 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -433,6 +433,7 @@ static const unsigned int memcg_stat_items[] = {
MEMCG_ZSWAP_B,
MEMCG_ZSWAPPED,
MEMCG_ZSWAP_INCOMP,
+ MEMCG_ZSWPWB_PROACTIVE_B,
};
#define NR_MEMCG_NODE_STAT_ITEMS ARRAY_SIZE(memcg_node_stat_items)
@@ -1558,6 +1559,7 @@ static const struct memory_stat memory_stats[] = {
{ "zswap", MEMCG_ZSWAP_B },
{ "zswapped", MEMCG_ZSWAPPED },
{ "zswap_incomp", MEMCG_ZSWAP_INCOMP },
+ { "zswpwb_proactive_b", MEMCG_ZSWPWB_PROACTIVE_B },
#endif
{ "file_mapped", NR_FILE_MAPPED },
{ "file_dirty", NR_FILE_DIRTY },
@@ -1614,6 +1616,7 @@ static int memcg_page_state_unit(int item)
switch (item) {
case MEMCG_PERCPU_B:
case MEMCG_ZSWAP_B:
+ case MEMCG_ZSWPWB_PROACTIVE_B:
case NR_SLAB_RECLAIMABLE_B:
case NR_SLAB_UNRECLAIMABLE_B:
return 1;
diff --git a/mm/zswap.c b/mm/zswap.c
index 9cda96f05508..d356c1739c68 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1747,8 +1747,10 @@ int zswap_proactive_writeback(struct mem_cgroup *memcg, u64 bytes_to_writeback)
} while (iter && !mem_cgroup_tryget_online(iter));
shrunk = zswap_shrink_one_memcg(iter, &s);
- if (shrunk > 0)
+ if (shrunk > 0) {
bytes_written += shrunk;
+ mod_memcg_state(iter, MEMCG_ZSWPWB_PROACTIVE_B, shrunk);
+ }
/* drop the extra reference taken by mem_cgroup_tryget_online() */
mem_cgroup_put(iter);
--
2.34.1