Skip to contentSkip to conversation

sched: update the rq->avg_idle when a task is moved to an idle CPU

PATCH Superseded Huang Shijie 3 participants 6 messages Nov 27, 2025 Scheduler

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 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.

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 (3):
sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle
sched: update the rq->avg_idle when a task is moved to an idle CPU sched/fair: update rq->avg_idle for the delay-dequeue task

 kernel/sched/core.c  | 28 ++++++++++++++++------------
 kernel/sched/fair.c  | 24 ++++++++++++++++--------
 kernel/sched/sched.h |  2 ++
 3 files changed, 34 insertions(+), 20 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v2 1/3 sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle

Save the idle_stamp at the beginning of sched_balance_newidle(), if it cannot pull any task, set it for rq->idle_stamp.

This patch does not change the logic of rq->idle_stamp.

---
 kernel/sched/fair.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/sched/fair.c +8 -4
index 769d7b7990df..c1a8fa043156 100644
@@ -12862,6 +12862,7 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
 	u64 t0, t1, curr_cost = 0;
 	struct sched_domain *sd;
 	int pulled_task = 0;
+	u64 idle_stamp;
 
 	update_misfit_status(NULL, this_rq);
 
@@ -12877,7 +12878,9 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
 	 * for CPU_NEWLY_IDLE, such that we measure the this duration
 	 * as idle time.
 	 */
-	this_rq->idle_stamp = rq_clock(this_rq);
+	idle_stamp = rq_clock(this_rq);
+
+	this_rq->idle_stamp = 0;
 
 	/*
 	 * Do not pull tasks towards !active CPUs...
@@ -12989,10 +12992,11 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
 	if (time_after(this_rq->next_balance, next_balance))
 		this_rq->next_balance = next_balance;
 
-	if (pulled_task)
-		this_rq->idle_stamp = 0;
-	else
+	if (!pulled_task) {
+		/* Set it here on purpose. */
+		this_rq->idle_stamp = idle_stamp;
 		nohz_newidle_balance(this_rq);
+	}
 
 	rq_repin_lock(this_rq, rf);
 
Signature · 1 line
2.40.1
#2 lore raw

PATCH v2 2/3 sched: update the rq->avg_idle when a task is moved to an idle CPU

In reply to Huang Shijie

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 introduces a helper: update_rq_avg_idle(). And uses it in activate_task(), 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  | 28 ++++++++++++++++------------
 kernel/sched/sched.h |  2 ++
 2 files changed, 18 insertions(+), 12 deletions(-)
kernel/sched/core.c +16 -12
index 0c4ff93eeb78..a946f3604548 100644
@@ -2135,6 +2135,7 @@ void activate_task(struct rq *rq, struct task_struct *p, int flags)
 		sched_mm_cid_migrate_to(rq, p);
 
 	enqueue_task(rq, p, flags);
+	update_rq_avg_idle(rq);
 
 	WRITE_ONCE(p->on_rq, TASK_ON_RQ_QUEUED);
 	ASSERT_EXCLUSIVE_WRITER(p->on_rq);
@@ -2412,6 +2413,21 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
 	return cpu_online(cpu);
 }
 
+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;
+	}
+}
+
 /*
  * This is how migration works:
  *
@@ -3645,18 +3661,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/sched.h +2 -0
index b419a4d98461..0e8aef1cef96 100644
@@ -242,6 +242,8 @@ static inline void update_avg(u64 *avg, u64 sample)
 	*avg += diff / 8;
 }
 
+extern void update_rq_avg_idle(struct rq *);
+
 /*
  * Shifting a value by an exponent greater *or equal* to the size of said value
  * is UB; cap at size-1.
Signature · 1 line
2.40.1
#3 lore raw

PATCH v2 3/3 sched/fair: update rq->avg_idle for the delay-dequeue task

In reply to Huang Shijie

When a delay-dequeue task is woken and requeued back, it may becomes eligible, and changes the CPU idle status, so we need to update the rq->avg_idle for it.

This patch adds a new parameter for requeue_delayed_entity(), and updates the rq->avg_idle as needed.

---
 kernel/sched/fair.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/sched/fair.c +8 -4
index c1a8fa043156..2ac630ecdbd5 100644
@@ -5208,7 +5208,7 @@ static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq);
 
 static void
-requeue_delayed_entity(struct sched_entity *se);
+requeue_delayed_entity(struct rq *rq, struct sched_entity *se);
 
 static void
 enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
@@ -6835,7 +6835,7 @@ static int sched_idle_cpu(int cpu)
 }
 
 static void
-requeue_delayed_entity(struct sched_entity *se)
+requeue_delayed_entity(struct rq *rq, struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
@@ -6862,6 +6862,10 @@ requeue_delayed_entity(struct sched_entity *se)
 	}
 
 	update_load_avg(cfs_rq, se, 0);
+
+	if (entity_eligible(cfs_rq, se))
+		update_rq_avg_idle(rq);
+
 	clear_delayed(se);
 }
 
@@ -6894,7 +6898,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 		util_est_enqueue(&rq->cfs, p);
 
 	if (flags & ENQUEUE_DELAYED) {
-		requeue_delayed_entity(se);
+		requeue_delayed_entity(rq, se);
 		return;
 	}
 
@@ -6912,7 +6916,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	for_each_sched_entity(se) {
 		if (se->on_rq) {
 			if (se->sched_delayed)
-				requeue_delayed_entity(se);
+				requeue_delayed_entity(rq, se);
 			break;
 		}
 		cfs_rq = cfs_rq_of(se);
Signature · 1 line
2.40.1
#4 lore raw

Re: [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task

Quoted 6 lines · Huang Shijie

When a delay-dequeue task is woken and requeued back, it may becomes eligible, and changes the CPU idle status, so we need to update the rq->avg_idle for it.

This patch adds a new parameter for requeue_delayed_entity(), and updates the rq->avg_idle as needed.

Can't this be solved by moving update_rq_avg_idle() into enqueue_task() in the previous patch? The only path that can reenqueue a delayed task from ttwu_runnable() calls enqueue_task() with ENQUEUE_DELAYED.

Signature · 2 lines
Thanks and Regards,
Prateek
#5 lore raw

Re: [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task

Quoted 2 lines · K Prateek Nayak

Can't this be solved by moving update_rq_avg_idle() into enqueue_task() in the previous patch? The only path that can

No problem, I can merge this patch into previous patch.

Thanks

Huang Shijie