[PATCH] cgroup: Publish css ID after online_css() succeeds in css_create()
From: Hu Xinyao
Date: Tue Dec 01 2026 - 04:11:08 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Hu Xinyao <huxinyao@xxxxxxxxxxxxxxx>
---
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