The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2] mm: memcg: reset zswap settings in css_reset
@ 2026-07-02  2:48 Jiayuan Chen
  2026-07-02  4:07 ` Tao Cui
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jiayuan Chen @ 2026-07-02  2:48 UTC (permalink / raw)
  To: linux-mm
  Cc: jiayuan.chen, Jiayuan Chen, Johannes Weiner, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, Andrew Morton, cgroups,
	linux-kernel

From: Jiayuan Chen <jiayuan.chen@shopee.com>

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.zswam.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

Cc: Jiayuan Chen <jiayuan.chen@linux.dev>
Signed-off-by: Jiayuan Chen <jiayuan.chen@shopee.com>
---
v1 -> v2 : add WRITE_ONCE.
https://lore.kernel.org/linux-mm/20260630100832.107062-1-jiayuan.chen@linux.dev/
---
 mm/memcontrol.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index d20ffc827306..c20ef3c1d6fe 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -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);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] mm: memcg: reset zswap settings in css_reset
  2026-07-02  2:48 [PATCH v2] mm: memcg: reset zswap settings in css_reset Jiayuan Chen
@ 2026-07-02  4:07 ` Tao Cui
  2026-07-05 21:02   ` Andrew Morton
  2026-07-05 21:01 ` Andrew Morton
  2026-07-06  6:28 ` Muchun Song
  2 siblings, 1 reply; 5+ messages in thread
From: Tao Cui @ 2026-07-02  4:07 UTC (permalink / raw)
  To: Jiayuan Chen, linux-mm
  Cc: cui.tao, Jiayuan Chen, Johannes Weiner, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, Andrew Morton, cgroups,
	linux-kernel

在 2026/7/2 10:48, Jiayuan Chen 写道:
> From: Jiayuan Chen <jiayuan.chen@shopee.com>
> 
> 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.zswam.max disappear
> 	echo "-memory" > /sys/fs/cgroup/test/cgroup.subtree_control
> 

Looks good to me.

One trivial nit on the commit message (not the patch):
  # child/memory.swap.max and child/memory.zswam.max disappear
  zswam  -> zswap

Reviewed-by: Tao Cui <cuitao@kylinos.cn>

> 	# 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
> 
> Cc: Jiayuan Chen <jiayuan.chen@linux.dev>
> Signed-off-by: Jiayuan Chen <jiayuan.chen@shopee.com>
> ---
> v1 -> v2 : add WRITE_ONCE.
> https://lore.kernel.org/linux-mm/20260630100832.107062-1-jiayuan.chen@linux.dev/
> ---
>  mm/memcontrol.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index d20ffc827306..c20ef3c1d6fe 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -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);


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] mm: memcg: reset zswap settings in css_reset
  2026-07-02  2:48 [PATCH v2] mm: memcg: reset zswap settings in css_reset Jiayuan Chen
  2026-07-02  4:07 ` Tao Cui
@ 2026-07-05 21:01 ` Andrew Morton
  2026-07-06  6:28 ` Muchun Song
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-07-05 21:01 UTC (permalink / raw)
  To: Jiayuan Chen
  Cc: linux-mm, Jiayuan Chen, Johannes Weiner, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, cgroups, linux-kernel,
	Yosry Ahmed, Nhat Pham, Chengming Zhou

On Thu,  2 Jul 2026 10:48:25 +0800 Jiayuan Chen <jiayuan.chen@linux.dev> wrote:

> From: Jiayuan Chen <jiayuan.chen@shopee.com>
> 
> 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.zswam.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

Thanks.

You convinced me, so I'll queue this for testing.  

The problem is old and doesn't sound serious, so I'll target 7.3-rc1,
no cc:stable.  If people disagree, please speak up.

AI review suggest that memcg->oom_group is missing similar treatment:
	https://sashiko.dev/#/patchset/20260702024827.353185-1-jiayuan.chen@linux.dev


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] mm: memcg: reset zswap settings in css_reset
  2026-07-02  4:07 ` Tao Cui
@ 2026-07-05 21:02   ` Andrew Morton
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-07-05 21:02 UTC (permalink / raw)
  To: Tao Cui
  Cc: Jiayuan Chen, linux-mm, Jiayuan Chen, Johannes Weiner,
	Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song, cgroups,
	linux-kernel

On Thu, 2 Jul 2026 12:07:23 +0800 Tao Cui <cui.tao@linux.dev> wrote:

> > 	# child/memory.swap.max and child/memory.zswam.max disappear
> > 	echo "-memory" > /sys/fs/cgroup/test/cgroup.subtree_control
> > 
> 
> Looks good to me.
> 
> One trivial nit on the commit message (not the patch):
>   # child/memory.swap.max and child/memory.zswam.max disappear
>   zswam  -> zswap

I fixed that in the mm.git copy of this patch.

> Reviewed-by: Tao Cui <cuitao@kylinos.cn>

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] mm: memcg: reset zswap settings in css_reset
  2026-07-02  2:48 [PATCH v2] mm: memcg: reset zswap settings in css_reset Jiayuan Chen
  2026-07-02  4:07 ` Tao Cui
  2026-07-05 21:01 ` Andrew Morton
@ 2026-07-06  6:28 ` Muchun Song
  2 siblings, 0 replies; 5+ messages in thread
From: Muchun Song @ 2026-07-06  6:28 UTC (permalink / raw)
  To: Jiayuan Chen
  Cc: linux-mm, Jiayuan Chen, Johannes Weiner, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Andrew Morton, cgroups,
	linux-kernel



> On Jul 2, 2026, at 10:48, Jiayuan Chen <jiayuan.chen@linux.dev> wrote:
> 
> From: Jiayuan Chen <jiayuan.chen@shopee.com>
> 
> 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.zswam.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
> 
> Cc: Jiayuan Chen <jiayuan.chen@linux.dev>
> Signed-off-by: Jiayuan Chen <jiayuan.chen@shopee.com>

Reviewed-by: Muchun Song <muchun.song@linux.dev>

Thanks.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-06  6:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-02  2:48 [PATCH v2] mm: memcg: reset zswap settings in css_reset Jiayuan Chen
2026-07-02  4:07 ` Tao Cui
2026-07-05 21:02   ` Andrew Morton
2026-07-05 21:01 ` Andrew Morton
2026-07-06  6:28 ` Muchun Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox