Re: [PATCH] memcg: free the private ID of a memcg that never comes online
From: Muchun Song
Date: Mon Nov 23 2026 - 21:50:26 EST
> On Nov 24, 2026, at 08:04, Shakeel Butt <shakeel.butt@xxxxxxxxx> 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@xxxxxxxxxxxxxxx
> Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
> ---
> 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
>