Re: [PATCH] memcg: free the private ID of a memcg that never comes online

From: Shakeel Butt

Date: Tue Nov 24 2026 - 00:24:51 EST


On Tue, Nov 24, 2026 at 12:52:41PM +0800, Muchun Song wrote:
>
>
> > On Nov 24, 2026, at 12:33, Shakeel Butt <shakeel.butt@xxxxxxxxx> 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@xxxxxxxxx> 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.