* [PATCH] memcg: free the private ID of a memcg that never comes online
@ 2026-11-24 0:04 Shakeel Butt
2026-11-24 0:54 ` Andrew Morton
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: Shakeel Butt @ 2026-11-24 0:04 UTC (permalink / raw)
To: Andrew Morton
Cc: Tejun Heo, Johannes Weiner, Michal Koutny, Michal Hocko,
Roman Gushchin, Muchun Song, Meta kernel team, cgroups, linux-mm,
linux-kernel, stable
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: stable@vger.kernel.org
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
include/linux/cgroup.h | 5 +++++
mm/memcontrol.c | 7 +++++++
2 files changed, 12 insertions(+)
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 2afb4cb2bb4f..123c713dcc91 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -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) {
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 091401cecf39..e7dfc96fd1b9 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -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);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 0:04 [PATCH] memcg: free the private ID of a memcg that never comes online Shakeel Butt
@ 2026-11-24 0:54 ` Andrew Morton
2026-11-24 1:09 ` Shakeel Butt
2026-11-24 0:55 ` + memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch added to mm-hotfixes-unstable branch Andrew Morton
` (2 subsequent siblings)
3 siblings, 1 reply; 11+ messages in thread
From: Andrew Morton @ 2026-11-24 0:54 UTC (permalink / raw)
To: Shakeel Butt
Cc: Tejun Heo, Johannes Weiner, Michal Koutny, Michal Hocko,
Roman Gushchin, Muchun Song, Meta kernel team, cgroups, linux-mm,
linux-kernel, stable
On Mon, 23 Nov 2026 16:04:39 -0800 Shakeel Butt <shakeel.butt@linux.dev> 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.
>
> Fixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
> Cc: stable@vger.kernel.org
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 ;)
> --- a/include/linux/cgroup.h
> +++ b/include/linux/cgroup.h
> @@ -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.
^ permalink raw reply [flat|nested] 11+ messages in thread
* + memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch added to mm-hotfixes-unstable branch
2026-11-24 0:04 [PATCH] memcg: free the private ID of a memcg that never comes online Shakeel Butt
2026-11-24 0:54 ` Andrew Morton
@ 2026-11-24 0:55 ` Andrew Morton
2026-11-24 2:50 ` [PATCH] memcg: free the private ID of a memcg that never comes online Muchun Song
2026-11-24 9:02 ` Michal Hocko
3 siblings, 0 replies; 11+ messages in thread
From: Andrew Morton @ 2026-11-24 0:55 UTC (permalink / raw)
To: mm-commits, tj, stable, roman.gushchin, muchun.song, mkoutny,
mhocko, hannes, shakeel.butt, akpm
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 4248 bytes --]
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 <shakeel.butt@linux.dev>
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.
Link: https://patch.msgid.link/20261124000439.967403-1-shakeel.butt@linux.dev
Fixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Koutný <mkoutny@suse.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: <stable@vger.kernel.org>
include/linux/cgroup.h | 5 +++++
mm/memcontrol.c | 7 +++++++
2 files changed, 12 insertions(+)
--- a/include/linux/cgroup.h~memcg-free-the-private-id-of-a-memcg-that-never-comes-online
+++ a/include/linux/cgroup.h
@@ -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) {
--- a/mm/memcontrol.c~memcg-free-the-private-id-of-a-memcg-that-never-comes-online
+++ a/mm/memcontrol.c
@@ -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 shakeel.butt@linux.dev 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
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 0:54 ` Andrew Morton
@ 2026-11-24 1:09 ` Shakeel Butt
0 siblings, 0 replies; 11+ messages in thread
From: Shakeel Butt @ 2026-11-24 1:09 UTC (permalink / raw)
To: Andrew Morton
Cc: Tejun Heo, Johannes Weiner, Michal Koutny, Michal Hocko,
Roman Gushchin, Muchun Song, Meta kernel team, cgroups, linux-mm,
linux-kernel, stable
On Mon, Nov 23, 2026 at 04:54:51PM -0800, Andrew Morton wrote:
> On Mon, 23 Nov 2026 16:04:39 -0800 Shakeel Butt <shakeel.butt@linux.dev> 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.
> >
> > Fixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
> > Cc: stable@vger.kernel.org
>
> 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.
> > --- a/include/linux/cgroup.h
> > +++ b/include/linux/cgroup.h
> > @@ -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.
Ack.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 0:04 [PATCH] memcg: free the private ID of a memcg that never comes online Shakeel Butt
2026-11-24 0:54 ` Andrew Morton
2026-11-24 0:55 ` + memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch added to mm-hotfixes-unstable branch Andrew Morton
@ 2026-11-24 2:50 ` Muchun Song
2026-11-24 4:33 ` Shakeel Butt
2026-11-24 9:02 ` Michal Hocko
3 siblings, 1 reply; 11+ messages in thread
From: Muchun Song @ 2026-11-24 2:50 UTC (permalink / raw)
To: Shakeel Butt
Cc: Muchun Song, Andrew Morton, Tejun Heo, Johannes Weiner,
Michal Koutny, Michal Hocko, Roman Gushchin, Meta kernel team,
cgroups, linux-mm, linux-kernel, stable
> On Nov 24, 2026, at 08:04, Shakeel Butt <shakeel.butt@linux.dev> 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.
>
> Fixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
> Cc: stable@vger.kernel.org
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
> ---
> include/linux/cgroup.h | 5 +++++
> mm/memcontrol.c | 7 +++++++
> 2 files changed, 12 insertions(+)
>
> diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
> index 2afb4cb2bb4f..123c713dcc91 100644
> --- a/include/linux/cgroup.h
> +++ b/include/linux/cgroup.h
> @@ -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) {
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 091401cecf39..e7dfc96fd1b9 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -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
> + mem_cgroup_private_id_remove(memcg);
> mem_cgroup_free(memcg);
> }
>
> --
> 2.53.1-Meta
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 2:50 ` [PATCH] memcg: free the private ID of a memcg that never comes online Muchun Song
@ 2026-11-24 4:33 ` Shakeel Butt
2026-11-24 4:52 ` Muchun Song
0 siblings, 1 reply; 11+ messages in thread
From: Shakeel Butt @ 2026-11-24 4:33 UTC (permalink / raw)
To: Muchun Song
Cc: Andrew Morton, Tejun Heo, Johannes Weiner, Michal Koutny,
Michal Hocko, Roman Gushchin, Meta kernel team, cgroups, linux-mm,
linux-kernel, stable
On Tue, Nov 24, 2026 at 10:50:00AM +0800, Muchun Song wrote:
>
>
> > On Nov 24, 2026, at 08:04, Shakeel Butt <shakeel.butt@linux.dev> wrote:
> >
[...]
>
> 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. 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.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 4:33 ` Shakeel Butt
@ 2026-11-24 4:52 ` Muchun Song
2026-11-24 5:24 ` Shakeel Butt
0 siblings, 1 reply; 11+ messages in thread
From: Muchun Song @ 2026-11-24 4:52 UTC (permalink / raw)
To: Shakeel Butt
Cc: Muchun Song, Andrew Morton, Tejun Heo, Johannes Weiner,
Michal Koutny, Michal Hocko, Roman Gushchin, Meta kernel team,
cgroups, linux-mm, linux-kernel, stable
> On Nov 24, 2026, at 12:33, Shakeel Butt <shakeel.butt@linux.dev> wrote:
>
> On Tue, Nov 24, 2026 at 10:50:00AM +0800, Muchun Song wrote:
>>
>>
>>> On Nov 24, 2026, at 08:04, Shakeel Butt <shakeel.butt@linux.dev> wrote:
>>>
> [...]
>>
>> 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. 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.
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!
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 4:52 ` Muchun Song
@ 2026-11-24 5:24 ` Shakeel Butt
2026-11-24 6:34 ` Muchun Song
0 siblings, 1 reply; 11+ messages in thread
From: Shakeel Butt @ 2026-11-24 5:24 UTC (permalink / raw)
To: Muchun Song
Cc: Andrew Morton, Tejun Heo, Johannes Weiner, Michal Koutny,
Michal Hocko, Roman Gushchin, Meta kernel team, cgroups, linux-mm,
linux-kernel, stable
On Tue, Nov 24, 2026 at 12:52:41PM +0800, Muchun Song wrote:
>
>
> > On Nov 24, 2026, at 12:33, Shakeel Butt <shakeel.butt@linux.dev> wrote:
> >
> > On Tue, Nov 24, 2026 at 10:50:00AM +0800, Muchun Song wrote:
> >>
> >>
> >>> On Nov 24, 2026, at 08:04, Shakeel Butt <shakeel.butt@linux.dev> wrote:
> >>>
> > [...]
> >>
> >> 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. 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.
>
> 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.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 5:24 ` Shakeel Butt
@ 2026-11-24 6:34 ` Muchun Song
0 siblings, 0 replies; 11+ messages in thread
From: Muchun Song @ 2026-11-24 6:34 UTC (permalink / raw)
To: Shakeel Butt
Cc: Muchun Song, Andrew Morton, Tejun Heo, Johannes Weiner,
Michal Koutny, Michal Hocko, Roman Gushchin, Meta kernel team,
cgroups, linux-mm, linux-kernel, stable
> On Nov 24, 2026, at 13:24, Shakeel Butt <shakeel.butt@linux.dev> wrote:
>
> On Tue, Nov 24, 2026 at 12:52:41PM +0800, Muchun Song wrote:
>>
>>
>>> On Nov 24, 2026, at 12:33, Shakeel Butt <shakeel.butt@linux.dev> wrote:
>>>
>>> On Tue, Nov 24, 2026 at 10:50:00AM +0800, Muchun Song wrote:
>>>>
>>>>
>>>>> On Nov 24, 2026, at 08:04, Shakeel Butt <shakeel.butt@linux.dev> wrote:
>>>>>
>>> [...]
>>>>
>>>> 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. 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.
>>
>> 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: Muchun Song (ByteDance) <muchun.song@linux.dev>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 0:04 [PATCH] memcg: free the private ID of a memcg that never comes online Shakeel Butt
` (2 preceding siblings ...)
2026-11-24 2:50 ` [PATCH] memcg: free the private ID of a memcg that never comes online Muchun Song
@ 2026-11-24 9:02 ` Michal Hocko
2026-11-24 17:54 ` Shakeel Butt
3 siblings, 1 reply; 11+ messages in thread
From: Michal Hocko @ 2026-11-24 9:02 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Tejun Heo, Johannes Weiner, Michal Koutny,
Roman Gushchin, Muchun Song, Meta kernel team, cgroups, linux-mm,
linux-kernel, stable
On Mon 23-11-26 16:04:39, Shakeel Butt 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.
Considering these error paths they should be pretty impossible in
practice (and without fault injection), right?
> 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: stable@vger.kernel.org
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
> ---
> include/linux/cgroup.h | 5 +++++
> mm/memcontrol.c | 7 +++++++
> 2 files changed, 12 insertions(+)
>
> diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
> index 2afb4cb2bb4f..123c713dcc91 100644
> --- a/include/linux/cgroup.h
> +++ b/include/linux/cgroup.h
> @@ -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) {
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 091401cecf39..e7dfc96fd1b9 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -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);
> }
>
> --
> 2.53.1-Meta
>
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] memcg: free the private ID of a memcg that never comes online
2026-11-24 9:02 ` Michal Hocko
@ 2026-11-24 17:54 ` Shakeel Butt
0 siblings, 0 replies; 11+ messages in thread
From: Shakeel Butt @ 2026-11-24 17:54 UTC (permalink / raw)
To: Michal Hocko
Cc: Andrew Morton, Tejun Heo, Johannes Weiner, Michal Koutny,
Roman Gushchin, Muchun Song, Meta kernel team, cgroups, linux-mm,
linux-kernel, stable
On Tue, Nov 24, 2026 at 10:02:37AM +0100, Michal Hocko wrote:
> On Mon 23-11-26 16:04:39, Shakeel Butt 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.
>
> Considering these error paths they should be pretty impossible in
> practice (and without fault injection), right?
>
Yes, this one is found by code inspection while I was debugging the memcg ID
leaks happening in the Meta fleet. One of the biggest source I found is the
orphaned swap cache of offlined memcg. I am still working on the fix for that.
> > 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: stable@vger.kernel.org
> > Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
>
> Acked-by: Michal Hocko <mhocko@suse.com>
Thanks.
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-11-24 17:55 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 0:04 [PATCH] memcg: free the private ID of a memcg that never comes online Shakeel Butt
2026-11-24 0:54 ` Andrew Morton
2026-11-24 1:09 ` Shakeel Butt
2026-11-24 0:55 ` + memcg-free-the-private-id-of-a-memcg-that-never-comes-online.patch added to mm-hotfixes-unstable branch Andrew Morton
2026-11-24 2:50 ` [PATCH] memcg: free the private ID of a memcg that never comes online Muchun Song
2026-11-24 4:33 ` Shakeel Butt
2026-11-24 4:52 ` Muchun Song
2026-11-24 5:24 ` Shakeel Butt
2026-11-24 6:34 ` Muchun Song
2026-11-24 9:02 ` Michal Hocko
2026-11-24 17:54 ` Shakeel Butt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox