mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/3] sched: update the rq->avg_idle when a task is moved to an idle CPU
@ 2025-11-27  5:03 Huang Shijie
  2025-11-27  5:03 ` [PATCH v2 1/3] sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle Huang Shijie
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Huang Shijie @ 2025-11-27  5:03 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot
  Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
	mgorman, linux-kernel, vschneid, vineethr, 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 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(-)

-- 
2.40.1


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

* [PATCH v2 1/3] sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle
  2025-11-27  5:03 [PATCH v2 0/3] sched: update the rq->avg_idle when a task is moved to an idle CPU Huang Shijie
@ 2025-11-27  5:03 ` Huang Shijie
  2025-11-27  5:03 ` [PATCH v2 2/3] sched: update the rq->avg_idle when a task is moved to an idle CPU Huang Shijie
  2025-11-27  5:03 ` [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task Huang Shijie
  2 siblings, 0 replies; 6+ messages in thread
From: Huang Shijie @ 2025-11-27  5:03 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot
  Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
	mgorman, linux-kernel, vschneid, vineethr, Huang Shijie

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.

Signed-off-by: Huang Shijie <shijie@os.amperecomputing.com>
---
 kernel/sched/fair.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 769d7b7990df..c1a8fa043156 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -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);
 
-- 
2.40.1


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

* [PATCH v2 2/3] sched: update the rq->avg_idle when a task is moved to an idle CPU
  2025-11-27  5:03 [PATCH v2 0/3] sched: update the rq->avg_idle when a task is moved to an idle CPU Huang Shijie
  2025-11-27  5:03 ` [PATCH v2 1/3] sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle Huang Shijie
@ 2025-11-27  5:03 ` Huang Shijie
  2025-11-27  5:03 ` [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task Huang Shijie
  2 siblings, 0 replies; 6+ messages in thread
From: Huang Shijie @ 2025-11-27  5:03 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot
  Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
	mgorman, linux-kernel, vschneid, vineethr, 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

Signed-off-by: Huang Shijie <shijie@os.amperecomputing.com>
---
 kernel/sched/core.c  | 28 ++++++++++++++++------------
 kernel/sched/sched.h |  2 ++
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 0c4ff93eeb78..a946f3604548 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -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;
-	}
 }
 
 /*
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index b419a4d98461..0e8aef1cef96 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -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.
-- 
2.40.1


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

* [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task
  2025-11-27  5:03 [PATCH v2 0/3] sched: update the rq->avg_idle when a task is moved to an idle CPU Huang Shijie
  2025-11-27  5:03 ` [PATCH v2 1/3] sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle Huang Shijie
  2025-11-27  5:03 ` [PATCH v2 2/3] sched: update the rq->avg_idle when a task is moved to an idle CPU Huang Shijie
@ 2025-11-27  5:03 ` Huang Shijie
  2025-11-27  5:51   ` K Prateek Nayak
  2 siblings, 1 reply; 6+ messages in thread
From: Huang Shijie @ 2025-11-27  5:03 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot
  Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
	mgorman, linux-kernel, vschneid, vineethr, 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.

Signed-off-by: Huang Shijie <shijie@os.amperecomputing.com>
---
 kernel/sched/fair.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index c1a8fa043156..2ac630ecdbd5 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -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);
-- 
2.40.1


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

* Re: [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task
  2025-11-27  5:03 ` [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task Huang Shijie
@ 2025-11-27  5:51   ` K Prateek Nayak
  2025-11-27  7:59     ` Shijie Huang
  0 siblings, 1 reply; 6+ messages in thread
From: K Prateek Nayak @ 2025-11-27  5:51 UTC (permalink / raw)
  To: Huang Shijie, mingo, peterz, juri.lelli, vincent.guittot
  Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
	mgorman, linux-kernel, vschneid, vineethr

On 11/27/2025 10:33 AM, Huang Shijie wrote:
> 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.

-- 
Thanks and Regards,
Prateek


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

* Re: [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task
  2025-11-27  5:51   ` K Prateek Nayak
@ 2025-11-27  7:59     ` Shijie Huang
  0 siblings, 0 replies; 6+ messages in thread
From: Shijie Huang @ 2025-11-27  7:59 UTC (permalink / raw)
  To: K Prateek Nayak, Huang Shijie, mingo, peterz, juri.lelli,
	vincent.guittot
  Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
	mgorman, linux-kernel, vschneid, vineethr


On 27/11/2025 13:51, K Prateek Nayak wrote:
> 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


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

end of thread, other threads:[~2025-11-27  8:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-27  5:03 [PATCH v2 0/3] sched: update the rq->avg_idle when a task is moved to an idle CPU Huang Shijie
2025-11-27  5:03 ` [PATCH v2 1/3] sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle Huang Shijie
2025-11-27  5:03 ` [PATCH v2 2/3] sched: update the rq->avg_idle when a task is moved to an idle CPU Huang Shijie
2025-11-27  5:03 ` [PATCH v2 3/3] sched/fair: update rq->avg_idle for the delay-dequeue task Huang Shijie
2025-11-27  5:51   ` K Prateek Nayak
2025-11-27  7:59     ` Shijie Huang

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®