mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] sched/core: Skip rq->avg_idle update without a valid idle_stamp
@ 2026-07-28 23:24 Shubhang Kaushik (Ampere)
  2026-07-29  6:19 ` K Prateek Nayak
  0 siblings, 1 reply; 2+ messages in thread
From: Shubhang Kaushik (Ampere) @ 2026-07-28 23:24 UTC (permalink / raw)
  To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, Christopher Lameter,
	Shubhang Kaushik
  Cc: linux-kernel, Shubhang Kaushik (Ampere)

Commit 4b603f1551a73 ("sched: Update rq->avg_idle when a task is moved
to an idle CPU") moved rq->avg_idle accounting out of the wakeup path and
into put_prev_task_idle(), so that the idle interval is consumed whenever
the idle task is switched out.

The wakeup-side accounting that it replaced only updated rq->avg_idle
when rq->idle_stamp was non-zero. The new helper lost that validity
check and unconditionally computes:

	rq_clock(rq) - rq->idle_stamp

If rq->idle_stamp is zero, this uses rq_clock(rq) as the sample. That is
not a valid idle duration and can immediately drive rq->avg_idle to its
clamp.

Restore the idle_stamp validity check in update_rq_avg_idle() and skip
the rq->avg_idle update when there is no measured idle interval.

Fixes: 4b603f1551a73 ("sched: Update rq->avg_idle when a task is moved to an idle CPU")
Signed-off-by: Shubhang Kaushik (Ampere) <sh@gentwo.org>
---
Temporary tracing under hackbench load confirmed that
update_rq_avg_idle() can be reached with rq->idle_stamp == 0.
Hackbench showed no material regression versus v7.2-rc5 mainline.

Related discussion:
  https://lore.kernel.org/r/20260423023322.1293923-1-firelzrd@gmail.com

This is a narrower variant of the earlier proposal.  It keeps the
rq->idle_stamp guard in update_rq_avg_idle(), but intentionally does not
stamp idle entry from set_next_task_idle(), preserving the existing
newidle accounting model and avoiding forced/proxy idle accounting
concerns.
---
 kernel/sched/core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 96226707c2f6135341aa779b8262f113e103d8ad..d8c9a80ffa83ac680bb479f67357ac1ec4a6d55e 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3732,11 +3732,17 @@ static inline void ttwu_do_wakeup(struct task_struct *p)
 
 void update_rq_avg_idle(struct rq *rq)
 {
-	u64 delta = rq_clock(rq) - rq->idle_stamp;
-	u64 max = 2*rq->max_idle_balance_cost;
+	u64 idle_stamp = rq->idle_stamp;
+	u64 delta, max;
+
+	if (unlikely(!idle_stamp))
+		return;
+
+	delta = rq_clock(rq) - idle_stamp;
 
 	update_avg(&rq->avg_idle, delta);
 
+	max = 2 * rq->max_idle_balance_cost;
 	if (rq->avg_idle > max)
 		rq->avg_idle = max;
 	rq->idle_stamp = 0;

---
base-commit: 3b5f4b83c4abc0c9b0a7b9e2b44e816611b7f2ec
change-id: 20260728-master-55cd7cc13290

Best regards,
-- 
Shubhang Kaushik (Ampere) <sh@gentwo.org>


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

* Re: [PATCH] sched/core: Skip rq->avg_idle update without a valid idle_stamp
  2026-07-28 23:24 [PATCH] sched/core: Skip rq->avg_idle update without a valid idle_stamp Shubhang Kaushik (Ampere)
@ 2026-07-29  6:19 ` K Prateek Nayak
  0 siblings, 0 replies; 2+ messages in thread
From: K Prateek Nayak @ 2026-07-29  6:19 UTC (permalink / raw)
  To: Shubhang Kaushik (Ampere),
	Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, Christopher Lameter, Shubhang Kaushik,
	John Stultz
  Cc: linux-kernel

(+John for awareness)

Hello Shubhang,

On 7/29/2026 4:54 AM, Shubhang Kaushik (Ampere) wrote:
> Commit 4b603f1551a73 ("sched: Update rq->avg_idle when a task is moved
> to an idle CPU") moved rq->avg_idle accounting out of the wakeup path and
> into put_prev_task_idle(), so that the idle interval is consumed whenever
> the idle task is switched out.
> 
> The wakeup-side accounting that it replaced only updated rq->avg_idle
> when rq->idle_stamp was non-zero. The new helper lost that validity
> check and unconditionally computes:
> 
> 	rq_clock(rq) - rq->idle_stamp
> 
> If rq->idle_stamp is zero, this uses rq_clock(rq) as the sample. That is
> not a valid idle duration and can immediately drive rq->avg_idle to its
> clamp.
> 
> Restore the idle_stamp validity check in update_rq_avg_idle() and skip
> the rq->avg_idle update when there is no measured idle interval.
> 
> Fixes: 4b603f1551a73 ("sched: Update rq->avg_idle when a task is moved to an idle CPU")
> Signed-off-by: Shubhang Kaushik (Ampere) <sh@gentwo.org>
> ---
> Temporary tracing under hackbench load confirmed that
> update_rq_avg_idle() can be reached with rq->idle_stamp == 0.
> Hackbench showed no material regression versus v7.2-rc5 mainline.
> 
> Related discussion:
>   https://lore.kernel.org/r/20260423023322.1293923-1-firelzrd@gmail.com
> 
> This is a narrower variant of the earlier proposal.  It keeps the
> rq->idle_stamp guard in update_rq_avg_idle(), but intentionally does not
> stamp idle entry from set_next_task_idle(), preserving the existing
> newidle accounting model and avoiding forced/proxy idle accounting
> concerns.

Makes sense since we switch to idle context without a newidle balance
during find_proxy_task() and also during force-idling. Feel free to
include:

Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>

> ---
>  kernel/sched/core.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 96226707c2f6135341aa779b8262f113e103d8ad..d8c9a80ffa83ac680bb479f67357ac1ec4a6d55e 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3732,11 +3732,17 @@ static inline void ttwu_do_wakeup(struct task_struct *p)
>  
>  void update_rq_avg_idle(struct rq *rq)
>  {
> -	u64 delta = rq_clock(rq) - rq->idle_stamp;
> -	u64 max = 2*rq->max_idle_balance_cost;
> +	u64 idle_stamp = rq->idle_stamp;
> +	u64 delta, max;
> +
> +	if (unlikely(!idle_stamp))
> +		return;
> +
> +	delta = rq_clock(rq) - idle_stamp;
>  
>  	update_avg(&rq->avg_idle, delta);
>  
> +	max = 2 * rq->max_idle_balance_cost;
>  	if (rq->avg_idle > max)
>  		rq->avg_idle = max;
>  	rq->idle_stamp = 0;
> 
> ---
> base-commit: 3b5f4b83c4abc0c9b0a7b9e2b44e816611b7f2ec
> change-id: 20260728-master-55cd7cc13290
> 
> Best regards,

-- 
Thanks and Regards,
Prateek


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

end of thread, other threads:[~2026-07-29  6:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-28 23:24 [PATCH] sched/core: Skip rq->avg_idle update without a valid idle_stamp Shubhang Kaushik (Ampere)
2026-07-29  6:19 ` K Prateek Nayak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome