mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: JP Kobryn <inwardvessel@gmail.com>
To: tj@kernel.org, shakeel.butt@linux.dev, mkoutny@suse.com,
	yosryahmed@google.com, hannes@cmpxchg.org,
	akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
	kernel-team@meta.com
Subject: [PATCH 5/5 cgroup/for-6.16-fixes] cgroup: break up the internal rstat init/exit logic by subsys and base
Date: Mon, 21 Jul 2025 18:40:30 -0700	[thread overview]
Message-ID: <20250722014030.297537-6-inwardvessel@gmail.com> (raw)
In-Reply-To: <20250722014030.297537-1-inwardvessel@gmail.com>

The __css_rstat_{base_,}init/exit() functions have complexity in how they
distinguish between base stats and formal subsystem stats. Eliminate this
complexity by breaking up these functions and moving the logic for base and
subsystem directly into their respective public API functions.

Signed-off-by: JP Kobryn <inwardvessel@gmail.com>
---
 kernel/cgroup/rstat.c | 72 ++++++++++++++++++-------------------------
 1 file changed, 30 insertions(+), 42 deletions(-)

diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c
index ba656a53136a..30fdf92a21a4 100644
--- a/kernel/cgroup/rstat.c
+++ b/kernel/cgroup/rstat.c
@@ -437,29 +437,15 @@ __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)
 	}
 }
 
-static int __css_rstat_init(struct cgroup_subsys_state *css, bool is_self)
+int css_rstat_init(struct cgroup_subsys_state *css)
 {
-	struct cgroup *cgrp = css->cgroup;
 	int cpu;
 
-	if (is_self) {
-		/* the root cgrp has rstat_base_cpu preallocated */
-		if (!cgrp->rstat_base_cpu) {
-			cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu);
-			if (!cgrp->rstat_base_cpu)
-				return -ENOMEM;
-		}
-	}
-
 	/* the root cgrp's self css has rstat_cpu preallocated */
 	if (!css->rstat_cpu) {
 		css->rstat_cpu = alloc_percpu(struct css_rstat_cpu);
-		if (!css->rstat_cpu) {
-			if (is_self)
-				free_percpu(cgrp->rstat_base_cpu);
-
+		if (!css->rstat_cpu)
 			return -ENOMEM;
-		}
 	}
 
 	/* ->updated_children list is self terminated */
@@ -467,19 +453,12 @@ static int __css_rstat_init(struct cgroup_subsys_state *css, bool is_self)
 		struct css_rstat_cpu *rstatc = css_rstat_cpu(css, cpu);
 
 		rstatc->updated_children = css;
-
-		if (is_self) {
-			struct cgroup_rstat_base_cpu *rstatbc;
-
-			rstatbc = cgroup_rstat_base_cpu(cgrp, cpu);
-			u64_stats_init(&rstatbc->bsync);
-		}
 	}
 
 	return 0;
 }
 
-static void __css_rstat_exit(struct cgroup_subsys_state *css, bool is_self)
+void css_rstat_exit(struct cgroup_subsys_state *css)
 {
 	int cpu;
 
@@ -494,35 +473,44 @@ static void __css_rstat_exit(struct cgroup_subsys_state *css, bool is_self)
 			return;
 	}
 
-	if (is_self) {
-		struct cgroup *cgrp = css->cgroup;
-
-		free_percpu(cgrp->rstat_base_cpu);
-		cgrp->rstat_base_cpu = NULL;
-	}
-
 	free_percpu(css->rstat_cpu);
 	css->rstat_cpu = NULL;
 }
 
-int css_rstat_init(struct cgroup_subsys_state *css)
+int cgroup_rstat_base_init(struct cgroup *cgrp)
 {
-	return __css_rstat_init(css, false);
-}
+	int ret, cpu;
 
-void css_rstat_exit(struct cgroup_subsys_state *css)
-{
-	return __css_rstat_exit(css, false);
-}
+	/* the root cgrp has rstat_base_cpu preallocated */
+	if (!cgrp->rstat_base_cpu) {
+		cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu);
+		if (!cgrp->rstat_base_cpu)
+			return -ENOMEM;
+	}
 
-int cgroup_rstat_base_init(struct cgroup *cgrp)
-{
-	return __css_rstat_init(&cgrp->self, true);
+	ret = css_rstat_init(&cgrp->self);
+	if (ret) {
+		free_percpu(cgrp->rstat_base_cpu);
+		return ret;
+	}
+
+	/* ->updated_children list is self terminated */
+	for_each_possible_cpu(cpu) {
+		struct cgroup_rstat_base_cpu *rstatbc;
+
+		rstatbc = cgroup_rstat_base_cpu(cgrp, cpu);
+		u64_stats_init(&rstatbc->bsync);
+	}
+
+	return ret;
 }
 
 void cgroup_rstat_base_exit(struct cgroup *cgrp)
 {
-	__css_rstat_exit(&cgrp->self, true);
+	css_rstat_exit(&cgrp->self);
+
+	free_percpu(cgrp->rstat_base_cpu);
+	cgrp->rstat_base_cpu = NULL;
 }
 
 /**
-- 
2.47.1


  parent reply	other threads:[~2025-07-22  1:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-22  1:40 [PATCH 0/5 cgroup/for-6.16-fixes] harden css_create() for safe placement of call to css_rstat_init() JP Kobryn
2025-07-22  1:40 ` [PATCH 1/5 cgroup/for-6.16-fixes] cgroup: add exclusive css rstat init/exit api for base stats JP Kobryn
2025-07-22  1:40 ` [PATCH 2/5 cgroup/for-6.16-fixes] cgroup: check for rstat flush callback at css rstat init/exit call sites JP Kobryn
2025-07-22  1:40 ` [PATCH 3/5 cgroup/for-6.16-fixes] cgroup: split init_and_link_css() JP Kobryn
2025-07-22  1:40 ` [PATCH 4/5 cgroup/for-6.16-fixes] cgroup: initialize css rstat before linking to cgroups in css_create() JP Kobryn
2025-07-22  1:40 ` JP Kobryn [this message]
2025-07-25 17:23 ` [PATCH 0/5 cgroup/for-6.16-fixes] harden css_create() for safe placement of call to css_rstat_init() Michal Koutný
2025-07-28 18:04   ` JP Kobryn
2025-07-29  9:42     ` Michal Koutný
2025-07-29 23:53       ` JP Kobryn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250722014030.297537-6-inwardvessel@gmail.com \
    --to=inwardvessel@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkoutny@suse.com \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    --cc=yosryahmed@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®