Skip to contentSkip to conversation

mm: memcg-v1: fix memory.memsw.failcnt accounting

PATCH Superseded Guopeng Zhang 4 participants 6 messages Aug 10 – Aug 11

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: memcg-v1: fix memory.memsw.failcnt accounting

From: Guopeng Zhang <[email protected]>

Commit 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups") made failcnt accounting conditional on track_failcnt. It enabled the flag for memcg->memory, but not for memcg->memsw.

Consequently, memory.memsw.failcnt remains zero when the memory+swap limit is hit. Enable failcnt accounting for the v1 memsw counter.

Reproducer:

    CG=/sys/fs/cgroup/memory/memsw-test
    LIMIT=33554432
    mkdir "$CG"
    echo "$LIMIT" > "$CG/memory.limit_in_bytes"
    echo "$LIMIT" > "$CG/memory.memsw.limit_in_bytes"

Start a child process in the cgroup and make it allocate and touch 96 MiB of memory, causing a memcg OOM.

cat "$CG/memory.memsw.failcnt"

Without the patch, memory.memsw.failcnt is 0. With the patch, memory.memsw.failcnt is greater than 0.

Fixes 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups")
---
 mm/memcontrol.c | 1 +
 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -0
index 6939a4fbb991..4ffe5b3733d9 100644
@@ -4235,6 +4235,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 #ifdef CONFIG_MEMCG_V1
 		WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
 		memcg->memory.track_failcnt = !memcg_on_dfl;
+		memcg->memsw.track_failcnt = !memcg_on_dfl;
 		WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
 		page_counter_init(&memcg->kmem, &parent->kmem, false);
 		page_counter_init(&memcg->tcpmem, &parent->tcpmem, false);
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · Guopeng Zhang

From: Guopeng Zhang <[email protected]>

Commit 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups") made failcnt accounting conditional on track_failcnt. It enabled the flag for memcg->memory, but not for memcg->memsw.

Consequently, memory.memsw.failcnt remains zero when the memory+swap limit is hit. Enable failcnt accounting for the v1 memsw counter.

Reproducer:

    CG=/sys/fs/cgroup/memory/memsw-test
    LIMIT=33554432
    mkdir "$CG"
    echo "$LIMIT" > "$CG/memory.limit_in_bytes"
    echo "$LIMIT" > "$CG/memory.memsw.limit_in_bytes"

Start a child process in the cgroup and make it allocate and touch 96 MiB of memory, causing a memcg OOM.

Quoted 7 lines

cat "$CG/memory.memsw.failcnt"

Without the patch, memory.memsw.failcnt is 0. With the patch, memory.memsw.failcnt is greater than 0.

Fixes 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups")

Thanks!

Quoted 18 lines
---
 mm/memcontrol.c | 1 +
 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -0
index 6939a4fbb991..4ffe5b3733d9 100644
@@ -4235,6 +4235,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 #ifdef CONFIG_MEMCG_V1
 		WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
 		memcg->memory.track_failcnt = !memcg_on_dfl;
+		memcg->memsw.track_failcnt = !memcg_on_dfl;
 		WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
 		page_counter_init(&memcg->kmem, &parent->kmem, false);
 		page_counter_init(&memcg->tcpmem, &parent->tcpmem, false);
Signature · 1 line
2.43.0
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#2 lore raw
In reply to Guopeng Zhang
Quoted 15 lines · On Mon, 10 Aug 2026 15:42:47 +0800 Guopeng Zhang <[email protected]>…

Commit 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups") made failcnt accounting conditional on track_failcnt. It enabled the flag for memcg->memory, but not for memcg->memsw.

Consequently, memory.memsw.failcnt remains zero when the memory+swap limit is hit. Enable failcnt accounting for the v1 memsw counter.

Reproducer:

    CG=/sys/fs/cgroup/memory/memsw-test
    LIMIT=33554432
    mkdir "$CG"
    echo "$LIMIT" > "$CG/memory.limit_in_bytes"
    echo "$LIMIT" > "$CG/memory.memsw.limit_in_bytes"
Quoted 7 lines

Start a child process in the cgroup and make it allocate and touch 96 MiB of memory, causing a memcg OOM.

cat "$CG/memory.memsw.failcnt"

Without the patch, memory.memsw.failcnt is 0. With the patch, memory.memsw.failcnt is greater than 0.

Thanks. Nice changelog.

Quoted 1 line
Fixes 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups")

I'll add cc:stable to this.

AI review thinks that memcg->tcpmem.track_failcnt should receive the same treatment:

	https://sashiko.dev/#/patchset/[email protected]
maintainer Acked-by on v1 1/1
#4 lore raw
In reply to Guopeng Zhang
Quoted 20 lines · Guopeng Zhang

From: Guopeng Zhang <[email protected]>

Commit 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups") made failcnt accounting conditional on track_failcnt. It enabled the flag for memcg->memory, but not for memcg->memsw.

Consequently, memory.memsw.failcnt remains zero when the memory+swap limit is hit. Enable failcnt accounting for the v1 memsw counter.

Reproducer:

    CG=/sys/fs/cgroup/memory/memsw-test
    LIMIT=33554432
    mkdir "$CG"
    echo "$LIMIT" > "$CG/memory.limit_in_bytes"
    echo "$LIMIT" > "$CG/memory.memsw.limit_in_bytes"

Start a child process in the cgroup and make it allocate and touch 96 MiB of memory, causing a memcg OOM.

Quoted 7 lines

cat "$CG/memory.memsw.failcnt"

Without the patch, memory.memsw.failcnt is 0. With the patch, memory.memsw.failcnt is greater than 0.

Fixes 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups")

Sashiko raises a valid point. kmem.tcp.limit_in_bytes is deprecated and will warn if set, but the functionality is still there for the time being. No need to leave it broken until it's removed.

Could you add

		memcg->tcpmem.track_failcnt = !memcg_on_dfl;

as well?

Quoted 16 lines
---
 mm/memcontrol.c | 1 +
 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -0
index 6939a4fbb991..4ffe5b3733d9 100644
@@ -4235,6 +4235,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 #ifdef CONFIG_MEMCG_V1
 		WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
 		memcg->memory.track_failcnt = !memcg_on_dfl;
+		memcg->memsw.track_failcnt = !memcg_on_dfl;
 		WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
 		page_counter_init(&memcg->kmem, &parent->kmem, false);
 		page_counter_init(&memcg->tcpmem, &parent->tcpmem, false);
#3 lore raw
In reply to Andrew Morton
Quoted 27 lines · Andrew Morton
Quoted 15 lines · On Mon, 10 Aug 2026 15:42:47 +0800 Guopeng Zhang <[email protected]>…

Commit 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups") made failcnt accounting conditional on track_failcnt. It enabled the flag for memcg->memory, but not for memcg->memsw.

Consequently, memory.memsw.failcnt remains zero when the memory+swap limit is hit. Enable failcnt accounting for the v1 memsw counter.

Reproducer:

    CG=/sys/fs/cgroup/memory/memsw-test
    LIMIT=33554432
    mkdir "$CG"
    echo "$LIMIT" > "$CG/memory.limit_in_bytes"
    echo "$LIMIT" > "$CG/memory.memsw.limit_in_bytes"
Quoted 7 lines

Start a child process in the cgroup and make it allocate and touch 96 MiB of memory, causing a memcg OOM.

cat "$CG/memory.memsw.failcnt"

Without the patch, memory.memsw.failcnt is 0. With the patch, memory.memsw.failcnt is greater than 0.

Thanks. Nice changelog.

Quoted 8 lines
Quoted 1 line
Fixes 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups")

I'll add cc:stable to this.

AI review thinks that memcg->tcpmem.track_failcnt should receive the same treatment:

	https://sashiko.dev/#/patchset/[email protected]

Yeah, tcpmem has the same issue.

I'll add

    memcg->tcpmem.track_failcnt = !memcg_on_dfl;

and update the changelog accordingly in v2.

Thanks,
Guopeng

#5 lore raw
In reply to Johannes Weiner
Quoted 35 lines · Johannes Weiner
Quoted 20 lines · Guopeng Zhang

From: Guopeng Zhang <[email protected]>

Commit 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups") made failcnt accounting conditional on track_failcnt. It enabled the flag for memcg->memory, but not for memcg->memsw.

Consequently, memory.memsw.failcnt remains zero when the memory+swap limit is hit. Enable failcnt accounting for the v1 memsw counter.

Reproducer:

    CG=/sys/fs/cgroup/memory/memsw-test
    LIMIT=33554432
    mkdir "$CG"
    echo "$LIMIT" > "$CG/memory.limit_in_bytes"
    echo "$LIMIT" > "$CG/memory.memsw.limit_in_bytes"

Start a child process in the cgroup and make it allocate and touch 96 MiB of memory, causing a memcg OOM.

Quoted 7 lines

cat "$CG/memory.memsw.failcnt"

Without the patch, memory.memsw.failcnt is 0. With the patch, memory.memsw.failcnt is greater than 0.

Fixes 0e2759afcaf9 ("page_counter: track failcnt only for legacy cgroups")

Sashiko raises a valid point. kmem.tcp.limit_in_bytes is deprecated and will warn if set, but the functionality is still there for the time being. No need to leave it broken until it's removed.

Quoted 5 lines

Could you add

		memcg->tcpmem.track_failcnt = !memcg_on_dfl;

as well?

Thanks for the review and Ack.

Agreed. I'll add

    memcg->tcpmem.track_failcnt = !memcg_on_dfl;

and update the changelog accordingly in v2.

Thanks,
Guopeng

Quoted 17 lines
Quoted 17 lines
---
 mm/memcontrol.c | 1 +
 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -0
index 6939a4fbb991..4ffe5b3733d9 100644
@@ -4235,6 +4235,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 #ifdef CONFIG_MEMCG_V1
 		WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
 		memcg->memory.track_failcnt = !memcg_on_dfl;
+		memcg->memsw.track_failcnt = !memcg_on_dfl;
 		WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
 		page_counter_init(&memcg->kmem, &parent->kmem, false);
 		page_counter_init(&memcg->tcpmem, &parent->tcpmem, false);