mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] sched: Account cgroup CPU time to the execution context
@ 2026-09-04  3:47 Hui Su
  2026-09-04  6:37 ` Tejun Heo
  0 siblings, 1 reply; 2+ messages in thread
From: Hui Su @ 2026-09-04  3:47 UTC (permalink / raw)
  To: Tejun Heo, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Johannes Weiner, Michal Koutny
  Cc: Hui Su, Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, John Stultz, cgroups,
	linux-kernel

Proxy execution separates the scheduling context from the execution
context. Commit aa4f74dfd42b ("sched: Fix runtime accounting w/ split
exec & sched contexts") made per-task and thread-group runtime
accounting follow the task that actually executes, while cgroup CPU
usage is charged to the donor.

When the donor and execution task belong to different cgroups, this
makes a task's execution time count against a different cgroup from the
one the task belongs to.

Cgroup CPU usage should follow the execution context, matching the
per-task, thread-group, and cgroup user/system accounting. Keep
scheduling state associated with the donor, but charge cgroup CPU
usage to rq->curr.

A reproducer with the donor and execution task in separate cgroups
showed the execution task accumulating runtime while cgroup CPU usage
was charged to the donor's cgroup. With this change, the execution
task's cgroup accumulates the CPU usage instead. The same behavior was
verified with an RT donor and with legacy cpuacct accounting.

Fixes: aa4f74dfd42b ("sched: Fix runtime accounting w/ split exec & sched contexts")
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Hui Su <sh_def@163.com>
---
Changes since v1:
- Rework the fix following Tejun Heo's review: make cgroup CPU usage
  follow the execution context instead of moving cgroup user/system
  accounting to the scheduling context.
- Drop the per-rq cputime owner, deferred boundary handling, and donor
  lifetime tracking, which are no longer needed.
- Rework the reproducer expectation around execution-context cgroup
  CPU usage.

Testing:
- cgroup v2 proxy reproducer: executor runtime +1.483s, donor cgroup A
  usage +0, and executor cgroup B usage +1.629s.
- RT donor reproducer: executor runtime +0.991s, donor cgroup A usage
  +125us, and executor cgroup B usage +0.991s.
- cgroup v1 cpuacct: usage charged to the executor cgroup.
- sched_proxy_exec=off control passed.
- CONFIG_SCHED_PROXY_EXEC / CONFIG_CGROUPS /
  CONFIG_CGROUP_CPUACCT build matrix passed.
- W=1 kernel/sched/fair.o passed.
- checkpatch --strict and git diff --check passed.

v1: https://lore.kernel.org/lkml/5733b51108eda90c1bda98a68d58b5e6ccbc24ec.1788433334.git.sh_def@163.com/

 kernel/sched/fair.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 8dff37059faf..6fcf67d741e8 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1404,7 +1404,6 @@ static s64 update_se(struct rq *rq, struct sched_entity *se)
 
 	se->exec_start = now;
 	if (entity_is_task(se)) {
-		struct task_struct *donor = task_of(se);
 		struct task_struct *running = rq->curr;
 		/*
 		 * If se is a task, we account the time against the running
@@ -1417,8 +1416,7 @@ static s64 update_se(struct rq *rq, struct sched_entity *se)
 		account_group_exec_runtime(running, delta_exec);
 		account_mm_sched(rq, running, delta_exec);
 
-		/* cgroup time is always accounted against the donor */
-		cgroup_account_cputime(donor, delta_exec);
+		cgroup_account_cputime(running, delta_exec);
 	} else {
 		/* If not task, account the time against donor se  */
 		se->sum_exec_runtime += delta_exec;
-- 
2.54.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] sched: Account cgroup CPU time to the execution context
  2026-09-04  3:47 [PATCH v2] sched: Account cgroup CPU time to the execution context Hui Su
@ 2026-09-04  6:37 ` Tejun Heo
  0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2026-09-04  6:37 UTC (permalink / raw)
  To: Hui Su
  Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Johannes Weiner, Michal Koutny, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
	John Stultz, cgroups, linux-kernel

On Fri, Sep 04, 2026 at 11:47:07AM +0800, Hui Su wrote:
> Proxy execution separates the scheduling context from the execution
> context. Commit aa4f74dfd42b ("sched: Fix runtime accounting w/ split
> exec & sched contexts") made per-task and thread-group runtime
> accounting follow the task that actually executes, while cgroup CPU
> usage is charged to the donor.
> 
> When the donor and execution task belong to different cgroups, this
> makes a task's execution time count against a different cgroup from the
> one the task belongs to.
> 
> Cgroup CPU usage should follow the execution context, matching the
> per-task, thread-group, and cgroup user/system accounting. Keep
> scheduling state associated with the donor, but charge cgroup CPU
> usage to rq->curr.
> 
> A reproducer with the donor and execution task in separate cgroups
> showed the execution task accumulating runtime while cgroup CPU usage
> was charged to the donor's cgroup. With this change, the execution
> task's cgroup accumulates the CPU usage instead. The same behavior was
> verified with an RT donor and with legacy cpuacct accounting.
> 
> Fixes: aa4f74dfd42b ("sched: Fix runtime accounting w/ split exec & sched contexts")
> Suggested-by: Tejun Heo <tj@kernel.org>
> Signed-off-by: Hui Su <sh_def@163.com>

Provided John is okay with going this way:

Acked-by: Tejun Heo <tj@kernel.org>

> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 8dff37059faf..6fcf67d741e8 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -1404,7 +1404,6 @@ static s64 update_se(struct rq *rq, struct sched_entity *se)
>  
>  	se->exec_start = now;
>  	if (entity_is_task(se)) {
> -		struct task_struct *donor = task_of(se);
>  		struct task_struct *running = rq->curr;
>  		/*
>  		 * If se is a task, we account the time against the running
> @@ -1417,8 +1416,7 @@ static s64 update_se(struct rq *rq, struct sched_entity *se)
>  		account_group_exec_runtime(running, delta_exec);
>  		account_mm_sched(rq, running, delta_exec);
>  
> -		/* cgroup time is always accounted against the donor */
> -		cgroup_account_cputime(donor, delta_exec);
> +		cgroup_account_cputime(running, delta_exec);

The diff looks so much better.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-04  6:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-04  3:47 [PATCH v2] sched: Account cgroup CPU time to the execution context Hui Su
2026-09-04  6:37 ` 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®