[PATCH 4/5 cgroup/for-6.16-fixes] cgroup: initialize css rstat before linking to cgroups in css_create()
From: JP Kobryn
Date: Mon Jul 21 2025 - 21:41:43 EST
Calling css_rstat_init() after linking the css with its cgroup (and parent
cgroup) leaves the code vulnerable to async cleanup where css_rstat_exit()
is invoked on a fully initialized css. Avoid this by calling
css_rstat_init() before linking. The error can then be handled inline with
no async cleanup requirement.
Signed-off-by: JP Kobryn <inwardvessel@xxxxxxxxx>
---
kernel/cgroup/cgroup.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 1990c6113c7f..0f8c6ce21634 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -5675,6 +5675,15 @@ static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
return css;
init_css(css);
+
+ if (ss->css_rstat_flush) {
+ err = css_rstat_init(css);
+ if (err) {
+ ss->css_free(css);
+ goto err_out;
+ }
+ }
+
link_css(css, ss, cgrp);
err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
@@ -5686,12 +5695,6 @@ static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
goto err_free_css;
css->id = err;
- if (ss->css_rstat_flush) {
- err = css_rstat_init(css);
- if (err)
- goto err_free_css;
- }
-
/* @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);
@@ -5707,6 +5710,7 @@ static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
err_free_css:
INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
+err_out:
return ERR_PTR(err);
}
--
2.47.1