Skip to contentSkip to conversation

mm: memcg: reset zswap settings in css_reset

PATCH Applied Jiayuan Chen 3 participants 4 messages Jul 3 – Jul 7

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 mm: memcg: reset zswap settings in css_reset

From: Jiayuan Chen <[email protected]>

mem_cgroup_css_reset() is called when the memory controller is disabled on a cgroup but the memcg cannot be destroyed because it is pinned by a subsystem dependency -- for example, the io controller declares .depends_on = 1 << memory_cgrp_id, so memory remains in the cgroup_ss_mask and the css is hidden rather than killed.

The purpose of css_reset is to revert the memcg to its vanilla state so that no policies are applied and the css can be safely made visible again later. Currently, all page counters (memory.max, swap.max, kmem.max, tcpmem.max) and other limits (soft_limit, memory.high, swap.high) are reset to their defaults, but zswap_max and zswap_writeback are not.

These fields are initialized in css_alloc (zswap_max = PAGE_COUNTER_MAX, zswap_writeback inherited from parent) but were missing from css_reset. As a result, stale zswap policies remain in effect after css_reset: the zswap charge path (obj_cgroup_may_zswap) continues to enforce the old zswap_max limit, and the writeback path continues to honor the old zswap_writeback setting, even though the memory controller has been "disabled" on this cgroup.

Reset zswap_max to PAGE_COUNTER_MAX and zswap_writeback to true, matching their defaults in css_alloc.

Test:
echo "+memory +io" > /sys/fs/cgroup/cgroup.subtree_control

mkdir /sys/fs/cgroup/test
mkdir /sys/fs/cgroup/test/child

echo "+memory +io" > /sys/fs/cgroup/test/cgroup.subtree_control echo 10000 > /sys/fs/cgroup/test/child/memory.zswap.max

# child/memory.swap.max and child/memory.zswap.max disappear echo "-memory" > /sys/fs/cgroup/test/cgroup.subtree_control

# re-enable memory control
echo "+memory" > /sys/fs/cgroup/test/cgroup.subtree_control

# before this patch
cat /sys/fs/cgroup/test/child/memory.zswap.max
8192

# after this patch, same as memory.swap.max cat /sys/fs/cgroup/test/child/memory.zswap.max
max

---

v2 -> v3 : typo in commit message and add Reviewed-by tag. v1 -> v2 : add WRITE_ONCE.
https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/memcontrol.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/memcontrol.c +4 -0
index d20ffc827306..c20ef3c1d6fe 100644
@@ -4362,6 +4362,10 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
 
 	page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
 	page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
+#ifdef CONFIG_ZSWAP
+	WRITE_ONCE(memcg->zswap_max, PAGE_COUNTER_MAX);
+	WRITE_ONCE(memcg->zswap_writeback, true);
+#endif
 #ifdef CONFIG_MEMCG_V1
 	page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
 	page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 2/2 mm: memcg: reset oom_group in css_reset

From: Jiayuan Chen <[email protected]>

memory.oom.group defaults to disabled, but css_reset did not clear memcg->oom_group when a disabled memory css is kept alive by another controller dependency.

Reset it with the other memory controller policies so a hidden memcg cannot keep applying stale group OOM kill policy.

---
It is found when Sashiko reviewed my previous patch.
https://sashiko.dev/#/patchset/20260702024827.353185-1-jiayuan.chen%40linux.dev

---
 mm/memcontrol.c | 1 +
 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -0
index c20ef3c1d6fe..c7de62c8f86f 100644
@@ -4362,6 +4362,7 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
 
 	page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
 	page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
+	WRITE_ONCE(memcg->oom_group, false);
 #ifdef CONFIG_ZSWAP
 	WRITE_ONCE(memcg->zswap_max, PAGE_COUNTER_MAX);
 	WRITE_ONCE(memcg->zswap_writeback, true);
Signature · 1 line
2.43.0
maintainer Acked-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] mm: memcg: reset oom_group in css_reset

Quoted 11 lines · Jiayuan Chen

From: Jiayuan Chen <[email protected]>

memory.oom.group defaults to disabled, but css_reset did not clear memcg->oom_group when a disabled memory css is kept alive by another controller dependency.

Reset it with the other memory controller policies so a hidden memcg cannot keep applying stale group OOM kill policy.

Thanks!

reviewer Reviewed-by on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] mm: memcg: reset oom_group in css_reset

In reply to Jiayuan Chen
Quoted 10 lines · Jiayuan Chen

From: Jiayuan Chen <[email protected]>

memory.oom.group defaults to disabled, but css_reset did not clear memcg->oom_group when a disabled memory css is kept alive by another controller dependency.

Quoted 5 lines

Reset it with the other memory controller policies so a hidden memcg cannot keep applying stale group OOM kill policy.

Thanks.