Skip to contentSkip to conversation

memcg: free the private ID of a memcg that never comes online

PATCH In review Shakeel Butt 4 participants 10 messages Nov 24

Subsystems (4)
lore
Share & tools
lore

Shakeel Butt

author maintainer
#0 lore raw

PATCH 1/1 memcg: free the private ID of a memcg that never comes online

mem_cgroup_alloc() reserves the private ID of a memcg when its css is allocated. The ID is erased when the last reference to it is put, and that reference is first taken in mem_cgroup_css_online(). The failure paths of mem_cgroup_alloc() and mem_cgroup_css_online() erase the ID themselves.

But css_create() can also fail after css_alloc() and before online_css(): in percpu_ref_init(), cgroup_idr_alloc() or css_rstat_init(). The memcg is then freed through mem_cgroup_css_free() without ever coming online, and nothing erases its ID. The reserved entry stays in mem_cgroup_private_ids for good, and each such failure takes one of the 65,535 IDs away. With failslab failing radix tree node allocations during mkdir, 1,500 mkdirs leaked 6 IDs this way.

Erase the ID in mem_cgroup_css_free() if the memcg never came online, which is when (memcg->css.flags & CSS_RELEASED) will be false: it is only set when an fully onlined memcg is released.

Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
---
 include/linux/cgroup.h | 5 +++++
 mm/memcontrol.c        | 7 +++++++
 2 files changed, 12 insertions(+)
include/linux/cgroup.h +5 -0
index 2afb4cb2bb4f..123c713dcc91 100644
@@ -449,6 +449,11 @@ static inline bool css_is_online(struct cgroup_subsys_state *css)
 	return css->flags & CSS_ONLINE;
 }
 
+static inline bool css_is_released(struct cgroup_subsys_state *css)
+{
+	return css->flags & CSS_RELEASED;
+}
+
 static inline bool css_is_self(struct cgroup_subsys_state *css)
 {
 	if (css == &css->cgroup->self) {
mm/memcontrol.c +7 -0
index 091401cecf39..e7dfc96fd1b9 100644
@@ -4545,6 +4545,13 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
 	irq_work_sync(&memcg->high_irq_work);
 	cancel_work_sync(&memcg->high_work);
 	free_shrinker_info(memcg);
+
+	/*
+	 * The css_create() error path bypasses normal CSS release, leaving
+	 * CSS_RELEASED clear. Drop any private ID that is still reserved.
+	 */
+	if (!css_is_released(css))
+		mem_cgroup_private_id_remove(memcg);
 	mem_cgroup_free(memcg);
 }
 
Signature · 1 line
2.53.1-Meta
maintainer
#1 lore raw
Quoted 14 lines · On Mon, 23 Nov 2026 16:04:39 -0800 Shakeel Butt <[email protected]> wrote:

mem_cgroup_alloc() reserves the private ID of a memcg when its css is allocated. The ID is erased when the last reference to it is put, and that reference is first taken in mem_cgroup_css_online(). The failure paths of mem_cgroup_alloc() and mem_cgroup_css_online() erase the ID themselves.

But css_create() can also fail after css_alloc() and before online_css(): in percpu_ref_init(), cgroup_idr_alloc() or css_rstat_init(). The memcg is then freed through mem_cgroup_css_free() without ever coming online, and nothing erases its ID. The reserved entry stays in mem_cgroup_private_ids for good, and each such failure takes one of the 65,535 IDs away. With failslab failing radix tree node allocations during mkdir, 1,500 mkdirs leaked 6 IDs this way.

Quoted 6 lines

Erase the ID in mem_cgroup_css_free() if the memcg never came online, which is when (memcg->css.flags & CSS_RELEASED) will be false: it is only set when an fully onlined memcg is released.

Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")

hm. These allocations are all small and they're GFP_KERNEL so
cc:stable seems a stretch? But whatever - stable people have bigger problems
than this ;)

include/linux/cgroup.h static inline bool css_is_online(struct cgroup_subsys_state *css) · line 455

Quoted 10 lines
include/linux/cgroup.h +4 -0
@@ -449,6 +449,11 @@ static inline bool css_is_online(struct cgroup_subsys_state *css)
 	return css->flags & CSS_ONLINE;
 }
 
+static inline bool css_is_released(struct cgroup_subsys_state *css)
+{
+	return css->flags & CSS_RELEASED;
+}

todo: switch css_next_child() to use this new helper.

author maintainer
#2 lore raw
Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Mon, 23 Nov 2026 16:04:39 -0800 Shakeel Butt <[email protected]> wrote:

mem_cgroup_alloc() reserves the private ID of a memcg when its css is allocated. The ID is erased when the last reference to it is put, and that reference is first taken in mem_cgroup_css_online(). The failure paths of mem_cgroup_alloc() and mem_cgroup_css_online() erase the ID themselves.

But css_create() can also fail after css_alloc() and before online_css(): in percpu_ref_init(), cgroup_idr_alloc() or css_rstat_init(). The memcg is then freed through mem_cgroup_css_free() without ever coming online, and nothing erases its ID. The reserved entry stays in mem_cgroup_private_ids for good, and each such failure takes one of the 65,535 IDs away. With failslab failing radix tree node allocations during mkdir, 1,500 mkdirs leaked 6 IDs this way.

Erase the ID in mem_cgroup_css_free() if the memcg never came online, which is when (memcg->css.flags & CSS_RELEASED) will be false: it is only set when an fully onlined memcg is released.

Quoted 7 lines
Quoted 2 lines
Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")

hm. These allocations are all small and they're GFP_KERNEL so
cc:stable seems a stretch? But whatever - stable people have bigger problems
than this ;)

Sorry, I should have explain better the user visible impact. The memory leak is not really a big issues, the memcg ID leak is greater issue. We have upper limit of 65535 IDs for memcgs and if we keep leaking the IDs, we can start seeing memcg creation failures.

Quoted 6 lines
Quoted 6 lines
include/linux/cgroup.h +0 -0
@@ -449,6 +449,11 @@ static inline bool css_is_online(struct cgroup_subsys_state *css)
 	return css->flags & CSS_ONLINE;
 }
 
Quoted 6 lines

include/linux/cgroup.h static inline bool css_is_online(struct cgroup_subsys_state *css) · line 455

Quoted 4 lines
include/linux/cgroup.h +4 -0
+static inline bool css_is_released(struct cgroup_subsys_state *css)
+{
+	return css->flags & CSS_RELEASED;
+}

todo: switch css_next_child() to use this new helper.

Ack.

maintainer tree notice
#3 lore raw

+ memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch added to mm-hotfixes-unstable branch

In reply to Shakeel Butt
The patch titled Subject: memcg: free the private ID of a memcg that never comes online has been added to the -mm mm-hotfixes-unstable branch. Its filename is…

The patch titled
Subject: memcg: free the private ID of a memcg that never comes online
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch

This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Shakeel Butt <[email protected]>
Subject: memcg: free the private ID of a memcg that never comes online
Date: Mon, 23 Nov 2026 16:04:39 -0800

mem_cgroup_alloc() reserves the private ID of a memcg when its css is allocated. The ID is erased when the last reference to it is put, and that reference is first taken in mem_cgroup_css_online(). The failure paths of mem_cgroup_alloc() and mem_cgroup_css_online() erase the ID themselves.

But css_create() can also fail after css_alloc() and before online_css():
in percpu_ref_init(), cgroup_idr_alloc() or css_rstat_init(). The memcg is then freed through mem_cgroup_css_free() without ever coming online, and nothing erases its ID. The reserved entry stays in mem_cgroup_private_ids for good, and each such failure takes one of the 65,535 IDs away. With failslab failing radix tree node allocations during mkdir, 1,500 mkdirs leaked 6 IDs this way.

Erase the ID in mem_cgroup_css_free() if the memcg never came online, which is when (memcg->css.flags & CSS_RELEASED) will be false: it is only set when an fully onlined memcg is released.

Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
Cc Tejun Heo <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Koutný <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Muchun Song <[email protected]>
 include/linux/cgroup.h |    5 +++++
 mm/memcontrol.c        |    7 +++++++
 2 files changed, 12 insertions(+)
include/linux/cgroup.h renamed from include/linux/cgroup.h~memcg-free-the-private-id-of-a-memcg-that-never-comes-online +5 -0
@@ -449,6 +449,11 @@ static inline bool css_is_online(struct
 	return css->flags & CSS_ONLINE;
 }
 
+static inline bool css_is_released(struct cgroup_subsys_state *css)
+{
+	return css->flags & CSS_RELEASED;
+}
+
 static inline bool css_is_self(struct cgroup_subsys_state *css)
 {
 	if (css == &css->cgroup->self) {
mm/memcontrol.c renamed from mm/memcontrol.c~memcg-free-the-private-id-of-a-memcg-that-never-comes-online +7 -0
@@ -4525,6 +4525,13 @@ static void mem_cgroup_css_free(struct c
 	irq_work_sync(&memcg->high_irq_work);
 	cancel_work_sync(&memcg->high_work);
 	free_shrinker_info(memcg);
+
+	/*
+	 * The css_create() error path bypasses normal CSS release, leaving
+	 * CSS_RELEASED clear. Drop any private ID that is still reserved.
+	 */
+	if (!css_is_released(css))
+		mem_cgroup_private_id_remove(memcg);
 	mem_cgroup_free(memcg);
 }
 

_

Patches currently in -mm which might be from [email protected] are

memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch memcg-move-memoryhigh-enforcement-out-of-try_charge_memcg.patch memcg-force-charges-from-reclaim-before-trying-the-limits.patch memcg-use-high_work-for-kernel-threads.patch memcg-use-high_work-for-remote-charges.patch memcg-simplify-high-limit-handling-after-a-charge.patch

reviewer
#4 lore raw
In reply to Shakeel Butt
Quoted 51 lines · Shakeel Butt

mem_cgroup_alloc() reserves the private ID of a memcg when its css is allocated. The ID is erased when the last reference to it is put, and that reference is first taken in mem_cgroup_css_online(). The failure paths of mem_cgroup_alloc() and mem_cgroup_css_online() erase the ID themselves.

But css_create() can also fail after css_alloc() and before online_css(): in percpu_ref_init(), cgroup_idr_alloc() or css_rstat_init(). The memcg is then freed through mem_cgroup_css_free() without ever coming online, and nothing erases its ID. The reserved entry stays in mem_cgroup_private_ids for good, and each such failure takes one of the 65,535 IDs away. With failslab failing radix tree node allocations during mkdir, 1,500 mkdirs leaked 6 IDs this way.

Erase the ID in mem_cgroup_css_free() if the memcg never came online, which is when (memcg->css.flags & CSS_RELEASED) will be false: it is only set when an fully onlined memcg is released.

Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")

---
include/linux/cgroup.h | 5 +++++
mm/memcontrol.c | 7 +++++++

2 files changed, 12 insertions(+)
include/linux/cgroup.h +5 -0
index 2afb4cb2bb4f..123c713dcc91 100644
@@ -449,6 +449,11 @@ static inline bool css_is_online(struct cgroup_subsys_state *css)
 	return css->flags & CSS_ONLINE;
 }
 
+static inline bool css_is_released(struct cgroup_subsys_state *css)
+{
+	return css->flags & CSS_RELEASED;
+}
+
 static inline bool css_is_self(struct cgroup_subsys_state *css)
 {
 	if (css == &css->cgroup->self) {
mm/memcontrol.c +0 -0
index 091401cecf39..e7dfc96fd1b9 100644

mm/memcontrol.c static void mem_cgroup_css_free(struct cgroup_subsys_state *css) · line 4553

Quoted 9 lines
mm/memcontrol.c +6 -0
@@ -4545,6 +4545,13 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
 	irq_work_sync(&memcg->high_irq_work);
 	cancel_work_sync(&memcg->high_work);
 	free_shrinker_info(memcg);
+
+	/*
+	 * The css_create() error path bypasses normal CSS release, leaving
+	 * CSS_RELEASED clear. Drop any private ID that is still reserved.
+	 */
+	if (!css_is_released(css))

The leak analysis and the fix look correct to me. However, using CSS_RELEASED here makes the private ID cleanup depend on a generic CSS teardown state, and adds an accessor solely for this failure path.

The private ID is not needed until mem_cgroup_css_online(), so could we instead reserve it at the beginning of that callback? Failures in css_create() before ->css_online() would then never own a private ID, while failures after the reservation would continue to use the existing mem_cgroup_private_id_remove() cleanup.

This would also make the private ID lifecycle more explicit: reserve and publish it during online, then drop the initial reference during offline. It would avoid both the new css_is_released() helper and special handling in mem_cgroup_css_free().

Thanks,
Muchun

Quoted 7 lines
mm/memcontrol.c +1 -0
+		mem_cgroup_private_id_remove(memcg);
 	mem_cgroup_free(memcg);
 }
 
Signature · 1 line
2.53.1-Meta
author maintainer
#5 lore raw
Quoted 4 lines · Muchun Song
Quoted 4 lines · Shakeel Butt

[...]

Quoted 11 lines

The leak analysis and the fix look correct to me. However, using CSS_RELEASED here makes the private ID cleanup depend on a generic CSS teardown state, and adds an accessor solely for this failure path.

The private ID is not needed until mem_cgroup_css_online(), so could we instead reserve it at the beginning of that callback? Failures in css_create() before ->css_online() would then never own a private ID, while failures after the reservation would continue to use the existing mem_cgroup_private_id_remove() cleanup.

Quoted 4 lines

This would also make the private ID lifecycle more explicit: reserve and publish it during online, then drop the initial reference during offline. It would avoid both the new css_is_released() helper and special handling in mem_cgroup_css_free().

Thanks Muchun. Nothing needs the private ID before ->css_online(), so reserving it there would work. But it moves the out-of-IDs failure from ->css_alloc() to ->css_online(). By then css_create() has linked the css into the parent's children list and css_idr, so mem_cgroup_iter() can css_tryget() it and cache it in iter->position. A css that fails online is freed without ->css_released(), so invalidate_reclaim_iterators() never runs, and the free does not wait for those references. Today memcg's ->css_online() fails only when a small allocation fails. With the move, every mkdir on a system that is out of IDs would take that path. I would rather keep failing in ->css_alloc(), before anything is visible.

To drop the CSS_RELEASED dependency, I can test the ID's own state:
!memcg->private_id_objcg means the ID was never published and the memcg still owns the reservation. Let me know if that works for you.

reviewer
#6 lore raw
Quoted 38 lines · Shakeel Butt
Quoted 4 lines · Muchun Song
Quoted 4 lines · Shakeel Butt

[...]

Quoted 11 lines

The leak analysis and the fix look correct to me. However, using CSS_RELEASED here makes the private ID cleanup depend on a generic CSS teardown state, and adds an accessor solely for this failure path.

The private ID is not needed until mem_cgroup_css_online(), so could we instead reserve it at the beginning of that callback? Failures in css_create() before ->css_online() would then never own a private ID, while failures after the reservation would continue to use the existing mem_cgroup_private_id_remove() cleanup.

Quoted 4 lines

This would also make the private ID lifecycle more explicit: reserve and publish it during online, then drop the initial reference during offline. It would avoid both the new css_is_released() helper and special handling in mem_cgroup_css_free().

Thanks Muchun. Nothing needs the private ID before ->css_online(), so reserving it there would work. But it moves the out-of-IDs failure from ->css_alloc() to ->css_online(). By then css_create() has linked the css into the parent's children list and css_idr, so mem_cgroup_iter() can css_tryget() it and cache it in iter->position. A css that fails online is freed without ->css_released(), so invalidate_reclaim_iterators() never runs, and the free does not wait for those references. Today memcg's ->css_online() fails only when a small allocation fails. With the move, every mkdir on a system that is out of IDs would take that path. I would rather keep failing in ->css_alloc(), before anything is visible.

Quoted 3 lines

To drop the CSS_RELEASED dependency, I can test the ID's own state:
!memcg->private_id_objcg means the ID was never published and the memcg still owns the reservation. Let me know if that works for you.

You're right. I missed that the css is visible to mem_cgroup_iter() before ->css_online() completes. Using private_id_objcg to track whether the reservation was published makes sense to me. Thanks Shakeel!

author maintainer
#7 lore raw
Quoted 38 lines · Muchun Song
Quoted 38 lines · Shakeel Butt
Quoted 4 lines · Muchun Song
Quoted 4 lines · Shakeel Butt

[...]

Quoted 11 lines

The leak analysis and the fix look correct to me. However, using CSS_RELEASED here makes the private ID cleanup depend on a generic CSS teardown state, and adds an accessor solely for this failure path.

The private ID is not needed until mem_cgroup_css_online(), so could we instead reserve it at the beginning of that callback? Failures in css_create() before ->css_online() would then never own a private ID, while failures after the reservation would continue to use the existing mem_cgroup_private_id_remove() cleanup.

Quoted 4 lines

This would also make the private ID lifecycle more explicit: reserve and publish it during online, then drop the initial reference during offline. It would avoid both the new css_is_released() helper and special handling in mem_cgroup_css_free().

Thanks Muchun. Nothing needs the private ID before ->css_online(), so reserving it there would work. But it moves the out-of-IDs failure from ->css_alloc() to ->css_online(). By then css_create() has linked the css into the parent's children list and css_idr, so mem_cgroup_iter() can css_tryget() it and cache it in iter->position. A css that fails online is freed without ->css_released(), so invalidate_reclaim_iterators() never runs, and the free does not wait for those references. Today memcg's ->css_online() fails only when a small allocation fails. With the move, every mkdir on a system that is out of IDs would take that path. I would rather keep failing in ->css_alloc(), before anything is visible.

Quoted 8 lines
Quoted 3 lines

To drop the CSS_RELEASED dependency, I can test the ID's own state:
!memcg->private_id_objcg means the ID was never published and the memcg still owns the reservation. Let me know if that works for you.

You're right. I missed that the css is visible to mem_cgroup_iter() before ->css_online() completes. Using private_id_objcg to track whether the reservation was published makes sense to me. Thanks Shakeel!

Oh I just noticed that private_id_objcg is introduced in 7.4, so to backport the fix to previous stable kernels, we will still need to use CSS_RELEASED or something else there.

reviewer Acked-by
#8 lore raw
Quoted 47 lines · Shakeel Butt
Quoted 42 lines · Muchun Song
Quoted 41 lines · Shakeel Butt
Quoted 4 lines · Muchun Song
Quoted 4 lines · Shakeel Butt

[...]

Quoted 11 lines

The leak analysis and the fix look correct to me. However, using CSS_RELEASED here makes the private ID cleanup depend on a generic CSS teardown state, and adds an accessor solely for this failure path.

The private ID is not needed until mem_cgroup_css_online(), so could we instead reserve it at the beginning of that callback? Failures in css_create() before ->css_online() would then never own a private ID, while failures after the reservation would continue to use the existing mem_cgroup_private_id_remove() cleanup.

Quoted 4 lines

This would also make the private ID lifecycle more explicit: reserve and publish it during online, then drop the initial reference during offline. It would avoid both the new css_is_released() helper and special handling in mem_cgroup_css_free().

Thanks Muchun. Nothing needs the private ID before ->css_online(), so reserving it there would work. But it moves the out-of-IDs failure from ->css_alloc() to ->css_online(). By then css_create() has linked the css into the parent's children list and css_idr, so mem_cgroup_iter() can css_tryget() it and cache it in iter->position. A css that fails online is freed without ->css_released(), so invalidate_reclaim_iterators() never runs, and the free does not wait for those references. Today memcg's ->css_online() fails only when a small allocation fails. With the move, every mkdir on a system that is out of IDs would take that path. I would rather keep failing in ->css_alloc(), before anything is visible.

To drop the CSS_RELEASED dependency, I can test the ID's own state:
!memcg->private_id_objcg means the ID was never published and the memcg still owns the reservation. Let me know if that works for you.

Quoted 8 lines
Quoted 4 lines

You're right. I missed that the css is visible to mem_cgroup_iter() before ->css_online() completes. Using private_id_objcg to track whether the reservation was published makes sense to me. Thanks Shakeel!

Oh I just noticed that private_id_objcg is introduced in 7.4, so to backport the fix to previous stable kernels, we will still need to use CSS_RELEASED or something else there.

Agreed. Keeping CSS_RELEASED in the fix makes sense for backporting.

So:

Acked-by on v1 1/1
#9 lore raw
In reply to Shakeel Butt
Quoted 6 lines · Shakeel Butt

mem_cgroup_alloc() reserves the private ID of a memcg when its css is allocated. The ID is erased when the last reference to it is put, and that reference is first taken in mem_cgroup_css_online(). The failure paths of mem_cgroup_alloc() and mem_cgroup_css_online() erase the ID themselves.

Quoted 7 lines

But css_create() can also fail after css_alloc() and before online_css(): in percpu_ref_init(), cgroup_idr_alloc() or css_rstat_init(). The memcg is then freed through mem_cgroup_css_free() without ever coming online, and nothing erases its ID. The reserved entry stays in mem_cgroup_private_ids for good, and each such failure takes one of the 65,535 IDs away. With failslab failing radix tree node allocations during mkdir, 1,500 mkdirs leaked 6 IDs this way.

Considering these error paths they should be pretty impossible in practice (and without fault injection), right?

Quoted 7 lines

Erase the ID in mem_cgroup_css_free() if the memcg never came online, which is when (memcg->css.flags & CSS_RELEASED) will be false: it is only set when an fully onlined memcg is released.

Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")

Thanks!

Quoted 42 lines
---
 include/linux/cgroup.h | 5 +++++
 mm/memcontrol.c        | 7 +++++++
 2 files changed, 12 insertions(+)
include/linux/cgroup.h +5 -0
index 2afb4cb2bb4f..123c713dcc91 100644
@@ -449,6 +449,11 @@ static inline bool css_is_online(struct cgroup_subsys_state *css)
 	return css->flags & CSS_ONLINE;
 }
 
+static inline bool css_is_released(struct cgroup_subsys_state *css)
+{
+	return css->flags & CSS_RELEASED;
+}
+
 static inline bool css_is_self(struct cgroup_subsys_state *css)
 {
 	if (css == &css->cgroup->self) {
mm/memcontrol.c +7 -0
index 091401cecf39..e7dfc96fd1b9 100644
@@ -4545,6 +4545,13 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
 	irq_work_sync(&memcg->high_irq_work);
 	cancel_work_sync(&memcg->high_work);
 	free_shrinker_info(memcg);
+
+	/*
+	 * The css_create() error path bypasses normal CSS release, leaving
+	 * CSS_RELEASED clear. Drop any private ID that is still reserved.
+	 */
+	if (!css_is_released(css))
+		mem_cgroup_private_id_remove(memcg);
 	mem_cgroup_free(memcg);
 }
 
Signature · 1 line
2.53.1-Meta
Signature · 2 lines
Michal Hocko
SUSE Labs