sched: update the rq->avg_idle when a task is moved to an idle CPU
v7 cover sched: update the rq->avg_idle when a task is moved to an idle CPU
In the newidle balance, the rq->idle_stamp may set to a non-zero value if it cannot pull any task.
In the wakeup, it will detect the rq->idle_stamp, and updates the rq->avg_idle, then ends the CPU idle status by setting rq->idle_stamp to zero.
Besides the wakeup, current code does not end the CPU idle status when a task is moved to the idle CPU, such as fork/clone, execve, or other cases.
This patch set tries to resolve it.
v6--> v7:
Call the update_rq_avg_idle() in the put_prev_task_idle(). Remove the patch 1 in the original patch set.
--v6:https://lkml.org/lkml/2025/12/9/377
v5--> v6:
Remove "this_rq->idle_stamp = 0;" in patch 1.
Update the test result with Specjbb.
--v5:https://lkml.org/lkml/2025/12/3/179
v4--> v5:
Modify the changelog.
--v4:https://lkml.org/lkml/2025/11/28/300
v3--> v4:
Remove the code for delayed task.
--v3: https://lkml.org/lkml/2025/11/27/456
v2--> v3:
-- merge patch 3 into patch 2:
move update_rq_avg_idle() to enqueue_task().
--v2: https://lkml.org/lkml/2025/11/27/214
v1--> v2:
-- Put update_rq_avg_idle() to activate_task()
-- Add Delay-dequeue task check.
--v1: https://lkml.org/lkml/2025/11/24/97
Huang Shijie (1):
sched: update the rq->avg_idle when a task is moved to an idle CPU
kernel/sched/core.c | 27 +++++++++++++++------------ kernel/sched/idle.c | 1 + kernel/sched/sched.h | 1 + 3 files changed, 17 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0
PATCH v7 1/1 sched: update the rq->avg_idle when a task is moved to an idle CPU
In the newidle balance, the rq->idle_stamp may set to a non-zero value if it cannot pull any task.
In the wakeup, it will detect the rq->idle_stamp, and updates the rq->avg_idle, then ends the CPU idle status by setting rq->idle_stamp to zero.
Besides the wakeup, current code does not end the CPU idle status when a task is moved to the idle CPU, such as fork/clone, execve, or other cases. In order to get more accurate rq->avg_idle, we need to update it at more places(not only the wakeup).
This patch introduces a helper: update_rq_avg_idle().
And uses it in put_prev_task_idle(), so it will update the rq->avg_idle when a task is moved to an idle CPU at:
-- wakeup
-- fork/clone
-- execve
-- idle balance
-- other cases
--- kernel/sched/core.c | 27 +++++++++++++++------------ kernel/sched/idle.c | 1 + kernel/sched/sched.h | 1 + 3 files changed, 17 insertions(+), 12 deletions(-)
kernel/sched/core.c +15 -12
@@ -3609,6 +3609,21 @@ static inline void ttwu_do_wakeup(struct task_struct *p)trace_sched_wakeup (p); } +void update_rq_avg_idle (struct rq *rq) +{ +if (rq->idle_stamp) { +u64 delta =rq_clock (rq) - rq->idle_stamp; +u64 max =2 *rq->max_idle_balance_cost; + +update_avg (&rq->avg_idle, delta); + +if (rq->avg_idle > max) + rq->avg_idle = max; + + rq->idle_stamp =0 ; + } +} +static void ttwu_do_activate (struct rq *rq,struct task_struct *p,int wake_flags,struct rq_flags *rf)
@@ -3644,18 +3659,6 @@ ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, p->sched_class->task_woken (rq, p);rq_repin_lock (rq, rf); } - -if (rq->idle_stamp) { -u64 delta =rq_clock (rq) - rq->idle_stamp; -u64 max =2 *rq->max_idle_balance_cost; - -update_avg (&rq->avg_idle, delta); - -if (rq->avg_idle > max) - rq->avg_idle = max; - - rq->idle_stamp =0 ; - } }/*
kernel/sched/idle.c +1 -0
@@ -460,6 +460,7 @@ static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct t {update_curr_idle (rq);scx_update_idle (rq, false, true); +update_rq_avg_idle (rq); }static void set_next_task_idle (struct rq *rq,struct task_struct *next,bool first)
kernel/sched/sched.h +1 -0
@@ -1651,6 +1651,7 @@ static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)#endif /* !CONFIG_FAIR_GROUP_SCHED */ +extern void update_rq_avg_idle (struct rq *rq);extern void update_rq_clock (struct rq *rq);/*
Signature · 1 line
2.43.0
Valentin Schneider
reviewerQuoted 11 lines · Huang Shijie
kernel/sched/core.c +5
-0@@ -3609,6 +3609,21 @@ static inline void ttwu_do_wakeup(struct task_struct *p)trace_sched_wakeup (p); } +void update_rq_avg_idle (struct rq *rq) +{ +if (rq->idle_stamp) { +u64 delta =rq_clock (rq) - rq->idle_stamp; +u64 max =2 *rq->max_idle_balance_cost;
kernel/sched/core.c static inline void ttwu_do_wakeup(struct task_struct *p) · line 3626
Quoted 10 lines
kernel/sched/core.c +10
-0+ +update_avg (&rq->avg_idle, delta); + +if (rq->avg_idle > max) + rq->avg_idle = max; + + rq->idle_stamp =0 ; + } +} +
So if we have this invoked every time we switch to the idle task via put_prev_task_idle(), do we want to move sched_balance_newidle()'s update of rq->idle_stamp() to set_next_task_idle()?
That does change the behaviour as we'd now record any idle duration as opposed to only idle-from-fair duration, but that would mean we'd unconditionally record a rq->idle_stamp and could thus ditch the if{} clause.
Quoted 27 lines
kernel/sched/core.c +0
-12static void ttwu_do_activate (struct rq *rq,struct task_struct *p,int wake_flags,struct rq_flags *rf)@@ -3644,18 +3659,6 @@ ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, p->sched_class->task_woken (rq, p);rq_repin_lock (rq, rf); } - -if (rq->idle_stamp) { -u64 delta =rq_clock (rq) - rq->idle_stamp; -u64 max =2 *rq->max_idle_balance_cost; - -update_avg (&rq->avg_idle, delta); - -if (rq->avg_idle > max) - rq->avg_idle = max; - - rq->idle_stamp =0 ; - } }/* kernel/sched/idle.c +0
-0
kernel/sched/idle.c static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct t · line 463
Quoted 4 lines
kernel/sched/idle.c +1
-0@@ -460,6 +460,7 @@ static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct t {update_curr_idle (rq);scx_update_idle (rq, false, true); +update_rq_avg_idle (rq);
AFAICT we can't have put_prev_task_idle() immediately followed by set_next_task_idle(); put_prev_set_next_task() especially already handles this, so I think we're good, but maybe worth mentioning in the changelog?
Quoted 17 lines
kernel/sched/idle.c +0
-0}static void set_next_task_idle (struct rq *rq,struct task_struct *next,bool first)kernel/sched/sched.h +1
-1@@ -1651,6 +1651,7 @@ static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)#endif /* !CONFIG_FAIR_GROUP_SCHED */ +extern void update_rq_avg_idle (struct rq *rq);extern void update_rq_clock (struct rq *rq);/* -- 2.43.0
K Prateek Nayak
reviewerHello Valentin,
Quoted 26 lines · Valentin Schneider
Quoted 11 lines · Huang Shijie
kernel/sched/core.c +5
-0@@ -3609,6 +3609,21 @@ static inline void ttwu_do_wakeup(struct task_struct *p)trace_sched_wakeup (p); } +void update_rq_avg_idle (struct rq *rq) +{ +if (rq->idle_stamp) { +u64 delta =rq_clock (rq) - rq->idle_stamp; +u64 max =2 *rq->max_idle_balance_cost;kernel/sched/core.c static inline void ttwu_do_wakeup(struct task_struct *p) · line 3626
Quoted 10 lines
kernel/sched/core.c +10
-0+ +update_avg (&rq->avg_idle, delta); + +if (rq->avg_idle > max) + rq->avg_idle = max; + + rq->idle_stamp =0 ; + } +} +So if we have this invoked every time we switch to the idle task via
put_prev_task_idle(), do we want to move sched_balance_newidle()'s update ofrq->idle_stamp()toset_next_task_idle()?
Quoted 3 lines
Quoted 1 line
That does change the behaviour as we'd now record any idle duration as
opposed to only idle-from-fair duration, but that would mean we'd unconditionally record a
rq->idle_stampand could thus ditch the if{} clause.
So I'm a wee bit skeptical of this - the avg_idle also serves as a bailout for newidle_balance(). If a tasks keeps waking up during newidle balance, we would like to discourage further attempts of newidle balance for a while to avoid CPU being stuck doing newidle balance while having runnable tasks waken up on it.
There is no bailout past should_we_balance(), and for large domains, it can take a while to get out of balancing.
If we move this to {put_prev,set_next}_task_idle(), we'll completely fail to capture that part of newidle balance bailout and I'm afraid we'll start doing newidle balance more aggressively.
I'll get some data over the weekend for the different variants being discussed here - if it doesn't reveal anything drastic, we can consider moving this accounting to idle task's switch.
Signature · 2 lines
Thanks and Regards, Prateek
Valentin Schneider
reviewerQuoted 39 lines · K Prateek Nayak
Hello Valentin,
Quoted 26 lines · Valentin Schneider
Quoted 11 lines · Huang Shijie
kernel/sched/core.c +5
-0@@ -3609,6 +3609,21 @@ static inline void ttwu_do_wakeup(struct task_struct *p)trace_sched_wakeup (p); } +void update_rq_avg_idle (struct rq *rq) +{ +if (rq->idle_stamp) { +u64 delta =rq_clock (rq) - rq->idle_stamp; +u64 max =2 *rq->max_idle_balance_cost;kernel/sched/core.c static inline void ttwu_do_wakeup(struct task_struct *p) · line 3626
Quoted 10 lines
kernel/sched/core.c +10
-0+ +update_avg (&rq->avg_idle, delta); + +if (rq->avg_idle > max) + rq->avg_idle = max; + + rq->idle_stamp =0 ; + } +} +So if we have this invoked every time we switch to the idle task via
put_prev_task_idle(), do we want to move sched_balance_newidle()'s update ofrq->idle_stamp()toset_next_task_idle()?Quoted 3 lines
Quoted 1 line
That does change the behaviour as we'd now record any idle duration as
opposed to only idle-from-fair duration, but that would mean we'd unconditionally record a
rq->idle_stampand could thus ditch the if{} clause.So I'm a wee bit skeptical of this - the
avg_idlealso serves as a bailout fornewidle_balance(). If a tasks keeps waking up during newidle balance, we would like to discourage further attempts of newidle balance for a while to avoid CPU being stuck doing newidle balance while having runnable tasks waken up on it.
Quoted 7 lines
There is no bailout past
should_we_balance(), and for large domains, it can take a while to get out of balancing.If we move this to {put_prev,set_next}_task_idle(), we'll completely fail to capture that part of newidle balance bailout and I'm afraid we'll start doing newidle balance more aggressively.
Ah you're right, I'd forgotten about this, there's even a comment above the idle_stamp update pointing this out... Although AFAICT that means we'd end up with smaller rq->avg_idle and thus would newidle_balance() less often; regardless that is a behaviour change.
Quoted 7 lines
I'll get some data over the weekend for the different variants being discussed here - if it doesn't reveal anything drastic, we can consider moving this accounting to idle task's switch.
--
Thanks and Regards,
Prateek
Vincent Guittot
maintainerQuoted 41 lines · Valentin Schneider
Quoted 39 lines · K Prateek Nayak
Hello Valentin,
Quoted 26 lines · Valentin Schneider
Quoted 11 lines · Huang Shijie
kernel/sched/core.c +5
-0@@ -3609,6 +3609,21 @@ static inline void ttwu_do_wakeup(struct task_struct *p)trace_sched_wakeup (p); } +void update_rq_avg_idle (struct rq *rq) +{ +if (rq->idle_stamp) { +u64 delta =rq_clock (rq) - rq->idle_stamp; +u64 max =2 *rq->max_idle_balance_cost;kernel/sched/core.c static inline void ttwu_do_wakeup(struct task_struct *p) · line 3626
Quoted 10 lines
kernel/sched/core.c +10
-0+ +update_avg (&rq->avg_idle, delta); + +if (rq->avg_idle > max) + rq->avg_idle = max; + + rq->idle_stamp =0 ; + } +} +So if we have this invoked every time we switch to the idle task via
put_prev_task_idle(), do we want to move sched_balance_newidle()'s update ofrq->idle_stamp()toset_next_task_idle()?Quoted 3 lines
Quoted 1 line
That does change the behaviour as we'd now record any idle duration as
opposed to only idle-from-fair duration, but that would mean we'd unconditionally record a
rq->idle_stampand could thus ditch the if{} clause.So I'm a wee bit skeptical of this - the
avg_idlealso serves as a bailout fornewidle_balance(). If a tasks keeps waking up during newidle balance, we would like to discourage further attempts of newidle balance for a while to avoid CPU being stuck doing newidle balance while having runnable tasks waken up on it.
Quoted 6 lines
Quoted 6 lines
There is no bailout past
should_we_balance(), and for large domains, it can take a while to get out of balancing.If we move this to {put_prev,set_next}_task_idle(), we'll completely fail to capture that part of newidle balance bailout and I'm afraid we'll start doing newidle balance more aggressively.
That would be the opposite; We will do less newidle balance because avg_idle will be shorter because the time spent in newidle balance will not be accounted as idle time whereas it should
Quoted 15 lines
Quoted 1 line
Ah you're right, I'd forgotten about this, there's even a comment above the
idle_stampupdate pointing this out... Although AFAICT that means we'd end up with smallerrq->avg_idleand thus wouldnewidle_balance()less often; regardless that is a behaviour change.Quoted 7 lines
I'll get some data over the weekend for the different variants being discussed here - if it doesn't reveal anything drastic, we can consider moving this accounting to idle task's switch.
--
Thanks and Regards,
Prateek
Vincent Guittot
maintainerQuoted 19 lines · Valentin Schneider
Quoted 17 lines · Huang Shijie
kernel/sched/core.c +11
-0@@ -3609,6 +3609,21 @@ static inline void ttwu_do_wakeup(struct task_struct *p)trace_sched_wakeup (p); } +void update_rq_avg_idle (struct rq *rq) +{ +if (rq->idle_stamp) { +u64 delta =rq_clock (rq) - rq->idle_stamp; +u64 max =2 *rq->max_idle_balance_cost; + +update_avg (&rq->avg_idle, delta); + +if (rq->avg_idle > max) + rq->avg_idle = max; +
Quoted 8 lines
kernel/sched/core.c static inline void ttwu_do_wakeup(struct task_struct *p) · line 3626
Quoted 4 lines
kernel/sched/core.c +4
-0+ rq->idle_stamp =0 ; + } +} +So if we have this invoked every time we switch to the idle task via
put_prev_task_idle(), do we want to move sched_balance_newidle()'s update ofrq->idle_stamp()toset_next_task_idle()?
I don't think that this is necessary. In worst case we will set idle_stamp in sched_balance_newidle() but a sched_ext task will be picked instead of going idle and the idle_stamp will not be used and will be overwritten next time we try to pick next task
Quoted 4 lines
That does change the behaviour as we'd now record any idle duration as opposed to only idle-from-fair duration, but that would mean we'd unconditionally record a
rq->idle_stampand could thus ditch the if{} clause.
yes the if test is probably not necessary anymore
Quoted 55 lines
Quoted 28 lines
kernel/sched/core.c +0
-12static void ttwu_do_activate (struct rq *rq,struct task_struct *p,int wake_flags,struct rq_flags *rf)@@ -3644,18 +3659,6 @@ ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, p->sched_class->task_woken (rq, p);rq_repin_lock (rq, rf); } - -if (rq->idle_stamp) { -u64 delta =rq_clock (rq) - rq->idle_stamp; -u64 max =2 *rq->max_idle_balance_cost; - -update_avg (&rq->avg_idle, delta); - -if (rq->avg_idle > max) - rq->avg_idle = max; - - rq->idle_stamp =0 ; - } }/* kernel/sched/idle.c +0
-0kernel/sched/idle.c static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct t · line 463
Quoted 4 lines
kernel/sched/idle.c +1
-0@@ -460,6 +460,7 @@ static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct t {update_curr_idle (rq);scx_update_idle (rq, false, true); +update_rq_avg_idle (rq);AFAICT we can't have
put_prev_task_idle()immediately followed byset_next_task_idle();put_prev_set_next_task()especially already handles this, so I think we're good, but maybe worth mentioning in the changelog?Quoted 17 lines
kernel/sched/idle.c +0
-0}static void set_next_task_idle (struct rq *rq,struct task_struct *next,bool first)kernel/sched/sched.h +1
-1@@ -1651,6 +1651,7 @@ static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)#endif /* !CONFIG_FAIR_GROUP_SCHED */ +extern void update_rq_avg_idle (struct rq *rq);extern void update_rq_clock (struct rq *rq);/* -- 2.43.0