The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [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; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-12-01 14:15 UTC | newest]

Thread overview: 2+ 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox