mirror of https://lore.kernel.org/lkml/
 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	[flat|nested] 2+ 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
  0 siblings, 0 replies; 2+ 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] 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

all inboxes | Powered by JetHome®