mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Shubhang Kaushik (Ampere)" <sh@gentwo.org>
To: Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	 Juri Lelli <juri.lelli@redhat.com>,
	 Vincent Guittot <vincent.guittot@linaro.org>,
	 Dietmar Eggemann <dietmar.eggemann@arm.com>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>,  Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	 K Prateek Nayak <kprateek.nayak@amd.com>,
	John Stultz <jstultz@google.com>,
	 Christopher Lameter <cl@os.amperecomputing.com>,
	 Shubhang Kaushik <shubhang@os.amperecomputing.com>
Cc: linux-kernel@vger.kernel.org,
	 "Shubhang Kaushik (Ampere)" <sh@gentwo.org>
Subject: [PATCH v2] sched/core: Skip rq->avg_idle update without a valid idle_stamp
Date: Thu, 06 Aug 2026 17:26:27 -0700	[thread overview]
Message-ID: <20260806-master-v2-1-e1f3a1a0c903@gentwo.org> (raw)

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.

This can happen when the scheduler switches to the idle task through a
path that did not set rq->idle_stamp via newidle_balance(), for example
during find_proxy_task() or force-idling.

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")
Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>
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.
---
Changes in v2:
  - Add Reviewed-by from Prateek.
  - Mention find_proxy_task() and force-idling as examples of paths that
    can switch to the idle task without a valid rq->idle_stamp.
  - Cc John Stultz.

Link to v1: https://lore.kernel.org/r/20260728-master-v1-1-f95d9b0147d2@gentwo.org
---
 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: c0a27675eaf08255017b3cabc28c99c0cd71f468
change-id: 20260728-master-55cd7cc13290

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


             reply	other threads:[~2026-08-07  0:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07  0:26 Shubhang Kaushik (Ampere) [this message]
2026-08-07  1:01 ` John Stultz
2026-08-07  2:45 ` Zhan Xusheng
2026-08-07 20:24   ` Shubhang

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=20260806-master-v2-1-e1f3a1a0c903@gentwo.org \
    --to=sh@gentwo.org \
    --cc=bsegall@google.com \
    --cc=cl@os.amperecomputing.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jstultz@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shubhang@os.amperecomputing.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.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®