* [PATCH] cgroup: Publish css ID after online_css() succeeds in css_create()
@ 2026-12-01 9:10 Hu Xinyao
2026-12-01 14:15 ` Tejun Heo
0 siblings, 1 reply; 3+ messages in thread
From: Hu Xinyao @ 2026-12-01 9:10 UTC (permalink / raw)
To: Tejun Heo, Johannes Weiner, Michal Koutny
Cc: cgroups, linux-kernel, Hu Xinyao
In css_create(), cgroup_idr_replace(&ss->css_idr, css, css->id)
publishes the new css pointer into ss->css_idr before online_css(css)
is called.
Commit b00c52dae6d9 ("cgroup: remove redundant cleanup in css_create")
removed cgroup_idr_remove() and percpu_ref_exit() from err_list_del so
css_free_rwork_fn() would not remove css->id twice. However, when
online_css(css) fails, that leaves the live css pointer published in
ss->css_idr with an active percpu_ref until css_free_rwork_fn() runs
after an RCU grace period. Concurrent ID-based lookups that use
css_from_id() + css_tryget(), such as cgroup_writeback_by_id(), can
acquire a reference to the never-onlined css while css_free_rwork_fn()
exits its percpu_ref and frees css via ss->css_free().
None of the subsystem ->css_online() callbacks look up the new css via
ss->css_idr, and css->id itself is already assigned by
cgroup_idr_alloc(). Move cgroup_idr_replace(&ss->css_idr, css, css->id)
to after online_css(css) succeeds so a failed css_create() keeps the
IDR slot as NULL until css_free_rwork_fn() removes the ID reservation.
Tested the online_css() -ENOMEM unwind path in QEMU with
CONFIG_KASAN=y, CONFIG_PROVE_LOCKING=y, and CONFIG_FAILSLAB=y injecting
allocation failures in mem_cgroup_css_online() during cgroup mkdir.
Fixes: b00c52dae6d9 ("cgroup: remove redundant cleanup in css_create")
Cc: stable@vger.kernel.org
Signed-off-by: Hu Xinyao <huxinyao@kylinsec.com.cn>
---
kernel/cgroup/cgroup.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index b3657e7..9af266b 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -5978,12 +5978,13 @@ static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
/* @css is ready to be brought online now, make it visible */
list_add_tail_rcu(&css->sibling, &parent_css->children);
- cgroup_idr_replace(&ss->css_idr, css, css->id);
err = online_css(css);
if (err)
goto err_list_del;
+ cgroup_idr_replace(&ss->css_idr, css, css->id);
+
return css;
err_list_del:
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] cgroup: Publish css ID after online_css() succeeds in css_create()
2026-12-01 9:10 [PATCH] cgroup: Publish css ID after online_css() succeeds in css_create() Hu Xinyao
@ 2026-12-01 14:15 ` Tejun Heo
2026-12-02 3:29 ` Hu Xinyao
0 siblings, 1 reply; 3+ messages in thread
From: Tejun Heo @ 2026-12-01 14:15 UTC (permalink / raw)
To: Hu Xinyao; +Cc: Johannes Weiner, Michal Koutny, cgroups, linux-kernel
Hello, Xinyao.
On Tue, Dec 01, 2026 at 06:10:22PM +0900, Hu Xinyao wrote:
> None of the subsystem ->css_online() callbacks look up the new css via
> ss->css_idr, and css->id itself is already assigned by
> cgroup_idr_alloc(). Move cgroup_idr_replace(&ss->css_idr, css, css->id)
> to after online_css(css) succeeds so a failed css_create() keeps the
> IDR slot as NULL until css_free_rwork_fn() removes the ID reservation.
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?
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] cgroup: Publish css ID after online_css() succeeds in css_create()
2026-12-01 14:15 ` Tejun Heo
@ 2026-12-02 3:29 ` Hu Xinyao
0 siblings, 0 replies; 3+ messages in thread
From: Hu Xinyao @ 2026-12-02 3:29 UTC (permalink / raw)
To: Tejun Heo
Cc: Johannes Weiner, Michal Koutny, cgroups, linux-kernel, Hu Xinyao
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 3:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 9:10 [PATCH] cgroup: Publish css ID after online_css() succeeds in css_create() Hu Xinyao
2026-12-01 14:15 ` Tejun Heo
2026-12-02 3:29 ` Hu Xinyao
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.