[PATCH v4 1/2] cgroup/rstat: Fix forceidle time in cpu.stat

Abel Wu posted 2 patches 10 months, 1 week ago
[PATCH v4 1/2] cgroup/rstat: Fix forceidle time in cpu.stat
Posted by Abel Wu 10 months, 1 week ago
The commit b824766504e4 ("cgroup/rstat: add force idle show helper")
retrieves forceidle_time outside cgroup_rstat_lock for non-root cgroups
which can be potentially inconsistent with other stats.

Rather than reverting that commit, fix it in a way that retains the
effort of cleaning up the ifdef-messes.

Fixes: b824766504e4 ("cgroup/rstat: add force idle show helper")
Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>
---
 kernel/cgroup/rstat.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c
index 5877974ece92..c2784c317cdd 100644
--- a/kernel/cgroup/rstat.c
+++ b/kernel/cgroup/rstat.c
@@ -613,36 +613,33 @@ static void cgroup_force_idle_show(struct seq_file *seq, struct cgroup_base_stat
 void cgroup_base_stat_cputime_show(struct seq_file *seq)
 {
 	struct cgroup *cgrp = seq_css(seq)->cgroup;
-	u64 usage, utime, stime, ntime;
+	struct cgroup_base_stat bstat;
 
 	if (cgroup_parent(cgrp)) {
 		cgroup_rstat_flush_hold(cgrp);
-		usage = cgrp->bstat.cputime.sum_exec_runtime;
+		bstat = cgrp->bstat;
 		cputime_adjust(&cgrp->bstat.cputime, &cgrp->prev_cputime,
-			       &utime, &stime);
-		ntime = cgrp->bstat.ntime;
+			       &bstat.cputime.utime, &bstat.cputime.stime);
 		cgroup_rstat_flush_release(cgrp);
 	} else {
-		/* cgrp->bstat of root is not actually used, reuse it */
-		root_cgroup_cputime(&cgrp->bstat);
-		usage = cgrp->bstat.cputime.sum_exec_runtime;
-		utime = cgrp->bstat.cputime.utime;
-		stime = cgrp->bstat.cputime.stime;
-		ntime = cgrp->bstat.ntime;
+		root_cgroup_cputime(&bstat);
 	}
 
-	do_div(usage, NSEC_PER_USEC);
-	do_div(utime, NSEC_PER_USEC);
-	do_div(stime, NSEC_PER_USEC);
-	do_div(ntime, NSEC_PER_USEC);
+	do_div(bstat.cputime.sum_exec_runtime, NSEC_PER_USEC);
+	do_div(bstat.cputime.utime, NSEC_PER_USEC);
+	do_div(bstat.cputime.stime, NSEC_PER_USEC);
+	do_div(bstat.ntime, NSEC_PER_USEC);
 
 	seq_printf(seq, "usage_usec %llu\n"
 			"user_usec %llu\n"
 			"system_usec %llu\n"
 			"nice_usec %llu\n",
-			usage, utime, stime, ntime);
+			bstat.cputime.sum_exec_runtime,
+			bstat.cputime.utime,
+			bstat.cputime.stime,
+			bstat.ntime);
 
-	cgroup_force_idle_show(seq, &cgrp->bstat);
+	cgroup_force_idle_show(seq, &bstat);
 }
 
 /* Add bpf kfuncs for cgroup_rstat_updated() and cgroup_rstat_flush() */
-- 
2.37.3
Re: [PATCH v4 1/2] cgroup/rstat: Fix forceidle time in cpu.stat
Posted by Tejun Heo 9 months, 3 weeks ago
On Sun, Feb 09, 2025 at 02:13:11PM +0800, Abel Wu wrote:
> The commit b824766504e4 ("cgroup/rstat: add force idle show helper")
> retrieves forceidle_time outside cgroup_rstat_lock for non-root cgroups
> which can be potentially inconsistent with other stats.
> 
> Rather than reverting that commit, fix it in a way that retains the
> effort of cleaning up the ifdef-messes.
> 
> Fixes: b824766504e4 ("cgroup/rstat: add force idle show helper")
> Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>

Applied to cgroup/for-6.15.

Thanks.

-- 
tejun
Re: [PATCH v4 1/2] cgroup/rstat: Fix forceidle time in cpu.stat
Posted by Abel Wu 9 months, 3 weeks ago
Hi Tejun, can you please pick this fix? Since there is no objections..

Thanks!

On 2/9/25 2:13 PM, Abel Wu wrote:
> The commit b824766504e4 ("cgroup/rstat: add force idle show helper")
> retrieves forceidle_time outside cgroup_rstat_lock for non-root cgroups
> which can be potentially inconsistent with other stats.
> 
> Rather than reverting that commit, fix it in a way that retains the
> effort of cleaning up the ifdef-messes.
> 
> Fixes: b824766504e4 ("cgroup/rstat: add force idle show helper")
> Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>
> ---
>   kernel/cgroup/rstat.c | 29 +++++++++++++----------------
>   1 file changed, 13 insertions(+), 16 deletions(-)
> 
> diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c
> index 5877974ece92..c2784c317cdd 100644
> --- a/kernel/cgroup/rstat.c
> +++ b/kernel/cgroup/rstat.c
> @@ -613,36 +613,33 @@ static void cgroup_force_idle_show(struct seq_file *seq, struct cgroup_base_stat
>   void cgroup_base_stat_cputime_show(struct seq_file *seq)
>   {
>   	struct cgroup *cgrp = seq_css(seq)->cgroup;
> -	u64 usage, utime, stime, ntime;
> +	struct cgroup_base_stat bstat;
>   
>   	if (cgroup_parent(cgrp)) {
>   		cgroup_rstat_flush_hold(cgrp);
> -		usage = cgrp->bstat.cputime.sum_exec_runtime;
> +		bstat = cgrp->bstat;
>   		cputime_adjust(&cgrp->bstat.cputime, &cgrp->prev_cputime,
> -			       &utime, &stime);
> -		ntime = cgrp->bstat.ntime;
> +			       &bstat.cputime.utime, &bstat.cputime.stime);
>   		cgroup_rstat_flush_release(cgrp);
>   	} else {
> -		/* cgrp->bstat of root is not actually used, reuse it */
> -		root_cgroup_cputime(&cgrp->bstat);
> -		usage = cgrp->bstat.cputime.sum_exec_runtime;
> -		utime = cgrp->bstat.cputime.utime;
> -		stime = cgrp->bstat.cputime.stime;
> -		ntime = cgrp->bstat.ntime;
> +		root_cgroup_cputime(&bstat);
>   	}
>   
> -	do_div(usage, NSEC_PER_USEC);
> -	do_div(utime, NSEC_PER_USEC);
> -	do_div(stime, NSEC_PER_USEC);
> -	do_div(ntime, NSEC_PER_USEC);
> +	do_div(bstat.cputime.sum_exec_runtime, NSEC_PER_USEC);
> +	do_div(bstat.cputime.utime, NSEC_PER_USEC);
> +	do_div(bstat.cputime.stime, NSEC_PER_USEC);
> +	do_div(bstat.ntime, NSEC_PER_USEC);
>   
>   	seq_printf(seq, "usage_usec %llu\n"
>   			"user_usec %llu\n"
>   			"system_usec %llu\n"
>   			"nice_usec %llu\n",
> -			usage, utime, stime, ntime);
> +			bstat.cputime.sum_exec_runtime,
> +			bstat.cputime.utime,
> +			bstat.cputime.stime,
> +			bstat.ntime);
>   
> -	cgroup_force_idle_show(seq, &cgrp->bstat);
> +	cgroup_force_idle_show(seq, &bstat);
>   }
>   
>   /* Add bpf kfuncs for cgroup_rstat_updated() and cgroup_rstat_flush() */