Re: [PATCH] cgroup: Publish css ID after online_css() succeeds in css_create()

From: Hu Xinyao

Date: Tue Dec 01 2026 - 22:30:16 EST


Hello, Tejun.

On Tue, Dec 01, 2026 at 04:15:12AM -1000, Tejun Heo wrote:
> The ordering is intentional. A css should become visible to iteration and
> ID lookups before it goes online and stay visible while it's online. With
> this change, a css which is already online can't be found by its ID for a
> while.
>
> This means that both iteration and ID lookups can yield a css which isn't
> online, and the users need to be able to handle that. AFAICS, the problem
> here is that the failure path of css_create() frees the css without
> honoring its refcnt. Maybe the failure path should honor the refcnt like
> the regular destruction path does?

Thank you for the explanation. That makes complete sense.

In css_create(), keeping cgroup_idr_replace() before online_css() and
calling percpu_ref_kill(&css->refcnt) directly when online_css() fails
routes teardown through css_release_work_fn() -> css_free_rwork_fn(),
which honors active references while unlinking css->sibling with
CSS_RELEASED set and clearing the ss->css_idr slot.

One subtlety on that path is that a failed css from css_create() was
never online and never passed through kill_css():
- Calling ss->css_released() unconditionally would crash subsystems whose
->css_online() failed partway through (e.g., cpu_cgroup_css_online()
failing before sched_online_group() initializes tg->list).
- Decrementing nr_dying_subsys unconditionally would underflow cgrp and
all ancestors.

Guarding css_rstat_flush(), ss->css_released(), and the nr_dying_subsys
decrements in css_release_work_fn() with (css->flags & CSS_DYING) avoids
both issues while keeping CSS_RELEASED list removal and css_idr cleanup
intact.

I tested this approach in QEMU with CONFIG_KASAN=y,
CONFIG_PROVE_LOCKING=y, CONFIG_DEBUG_LIST=y, and CONFIG_FAILSLAB=y
injecting -ENOMEM into mem_cgroup_css_online() under concurrent
cgroup.stat reads and confirmed nr_dying_subsys remains 0 with no warnings.

I am sending [PATCH v2] separately with your Suggested-by.

Thanks,
Hu Xinyao