Skip to contentSkip to conversation

sched_ext: Update other_load_avgs on set_next_task_scx

PATCH Superseded Peng Huang 3 participants 5 messages Nov 22 – Nov 23

Subsystems (2)
lore
Share & tools
lore

Peng Huang

author
#0 lore raw

PATCH 1/1 sched_ext: Update other_load_avgs on set_next_task_scx

When scx_switched_all() is enabled, scheduler_tick() skips sched_balance_trigger(), so __update_blocked_others() no longer runs to decay RT, DL, IRQ, and HW pressure load averages. Other-class load averages then depend on scx_tick() and SCX_RQ_CAN_STOP_TICK transitions in set_next_task_scx().

If SCX_RQ_CAN_STOP_TICK stays unchanged (e.g., remaining set across an idle or RT interval on tickless CPUs, or remaining clear on finite-slice switches before a tick fires or before going idle), stale RT/DL PELT values can remain elevated when consumers such as effective_cpu_util() or update_idle_rq_clock_pelt() read them.

Track load-average updates explicitly with @update_load_avgs in set_next_task_scx() and refresh update_other_load_avgs() on SNT_PICK as well as when SCX_RQ_CAN_STOP_TICK toggles.

Fixes 22a920209ab6 ("sched_ext: Implement tickless support")
---
 kernel/sched/ext/ext.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
kernel/sched/ext/ext.c +12 -7
index 10de6bd865e8..e29c9c18e1ab 100644
@@ -3553,6 +3553,7 @@ static void kick_sync_wait_bal_cb(struct rq *rq);
 static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e type)
 {
 	bool first = type == SNT_PICK;
+	bool update_load_avgs = first;
 	bool can_stop_tick;
 
 	/*
@@ -3619,10 +3620,6 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 	 * @p is getting newly scheduled or got kicked after someone updated its
 	 * slice. Update SCX_RQ_CAN_STOP_TICK to reflect whether the tick can be
 	 * stopped. See scx_can_stop_tick().
-	 *
-	 * Moreover, refresh the load_avgs just when transitioning in and out of
-	 * nohz. In the future, we might want to add a mechanism to update
-	 * load_avgs periodically on tick-stopped CPUs.
 	 */
 	if (can_stop_tick) {
 		if (!(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) {
@@ -3634,13 +3631,12 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 			 */
 			rq->scx.flags |= SCX_RQ_CAN_STOP_TICK;
 			sched_update_tick_dependency(rq);
-
-			update_other_load_avgs(rq);
+			update_load_avgs = true;
 		}
 	} else {
 		if (rq->scx.flags & SCX_RQ_CAN_STOP_TICK) {
 			rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
-			update_other_load_avgs(rq);
+			update_load_avgs = true;
 		}
 
 		/*
@@ -3651,6 +3647,15 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 		if (tick_nohz_full_cpu(cpu_of(rq)))
 			tick_nohz_dep_set_cpu(cpu_of(rq), TICK_DEP_BIT_SCHED);
 	}
+
+	/*
+	 * Refresh other-class (RT/DL/IRQ/HW) load averages when picking a new
+	 * task or toggling nohz tick dependency while CFS PELT is dormant.
+	 * In the future, we might want to add a mechanism to update load_avgs
+	 * periodically on tick-stopped CPUs.
+	 */
+	if (update_load_avgs)
+		update_other_load_avgs(rq);
 }
 
 void scx_proxy_donor_start(struct rq *rq)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched_ext: set_next_task_scx() misses PELT decay on idle and in ops.running() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched_ext: set_next_task_scx() misses PELT decay on idle and in ops.running()
--

commit 06ffe41f90ea50855b518361481cd90702ee1876
Author: Peng Huang <[email protected]>

sched_ext: Update other_load_avgs on set_next_task_scx

This commit updates RT, DL, IRQ, and HW pressure load averages in set_next_task_scx() when picking a new SCX task (SNT_PICK) in addition to when SCX_RQ_CAN_STOP_TICK toggles.

Quoted 43 lines
kernel/sched/ext/ext.c +3 -7
index a9e36b5bb0386..f46553d8eb3b9 100644
@@ -3553,6 +3553,7 @@ static void kick_sync_wait_bal_cb(struct rq *rq);
 static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e type)
 {
 	bool first = type == SNT_PICK;
+	bool update_load_avgs = first;
 	bool can_stop_tick;
 
 	/*
@@ -3619,10 +3620,6 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 	 * @p is getting newly scheduled or got kicked after someone updated its
 	 * slice. Update SCX_RQ_CAN_STOP_TICK to reflect whether the tick can be
 	 * stopped. See scx_can_stop_tick().
-	 *
-	 * Moreover, refresh the load_avgs just when transitioning in and out of
-	 * nohz. In the future, we might want to add a mechanism to update
-	 * load_avgs periodically on tick-stopped CPUs.
 	 */
 	if (can_stop_tick) {
 		if (!(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) {
@@ -3634,13 +3631,12 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 			 */
 			rq->scx.flags |= SCX_RQ_CAN_STOP_TICK;
 			sched_update_tick_dependency(rq);
-
-			update_other_load_avgs(rq);
+			update_load_avgs = true;
 		}
 	} else {
 		if (rq->scx.flags & SCX_RQ_CAN_STOP_TICK) {
 			rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
-			update_other_load_avgs(rq);
+			update_load_avgs = true;
 		}
 
 		/*
@@ -3651,6 +3647,15 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 		if (tick_nohz_full_cpu(cpu_of(rq)))
 			tick_nohz_dep_set_cpu(cpu_of(rq), TICK_DEP_BIT_SCHED);
 	}

kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t · line 3659

Quoted 10 lines
kernel/sched/ext/ext.c +9 -0
+
+	/*
+	 * Refresh other-class (RT/DL/IRQ/HW) load averages when picking a new
+	 * task or toggling nohz tick dependency while CFS PELT is dormant.
+	 * In the future, we might want to add a mechanism to update load_avgs
+	 * periodically on tick-stopped CPUs.
+	 */
+	if (update_load_avgs)
+		update_other_load_avgs(rq);
 }

[Severity: Medium]
Does calling update_other_load_avgs() only in set_next_task_scx() leave rq->avg_rt, rq->avg_dl, rq->avg_irq, and rq->avg_hw undecayed when an SCX task goes idle after an RT or DL interval?

On an RT/DL -> SCX switch, put_prev_set_next_task() calls put_prev_task_rt() or put_prev_task_dl(), which updates avg_rt or avg_dl at rq_clock_pelt(rq), and then immediately calls set_next_task_scx() at the same clock timestamp where no time has elapsed to decay those averages. When the SCX task later blocks and the CPU goes idle without an intervening tick, put_prev_set_next_task() calls put_prev_task_scx() and set_next_task_idle() without calling set_next_task_scx(), so update_idle_rq_clock_pelt() and effective_cpu_util() still see the undecayed utilization from before the SCX task ran.

In addition, within set_next_task_scx(), update_other_load_avgs() runs after scx_start_task_running() invokes ops.running(), so a BPF scheduler calling scx_bpf_cpuperf_set() or scx_bpf_cidperf_set() from ops.running() triggers effective_cpu_util() before the averages are refreshed, and same-task repicks (SNT_REPICK) return early without updating them.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw

PATCH v2 1/1 sched_ext: Update other_load_avgs on set_next_task_scx and idle entry

In reply to Peng Huang

When scx_switched_all() is enabled, scheduler_tick() skips sched_balance_trigger(), so __update_blocked_others() no longer runs to decay RT, DL, IRQ, and HW pressure load averages. Other-class load averages then depend on scx_tick() and SCX_RQ_CAN_STOP_TICK transitions in set_next_task_scx().

If SCX_RQ_CAN_STOP_TICK stays unchanged (e.g., remaining set across an idle or RT interval on tickless CPUs, or remaining clear on finite-slice switches before a tick fires), or when an SCX task goes idle after an RT/DL interval without an intervening tick, stale RT/DL PELT values can remain elevated when consumers such as effective_cpu_util() or update_idle_rq_clock_pelt() read them.

Refresh update_other_load_avgs() once at the entry of set_next_task_scx() (before SNT_REPICK early return and before ops.running() for SNT_PICK / SNT_NORMAL), and in put_prev_task_scx() when transitioning to the idle class.

Fixes 22a920209ab6 ("sched_ext: Implement tickless support")

---
Changes in v2:
- Call update_other_load_avgs() once before SNT_REPICK return and before ops.running() in set_next_task_scx() so BPF callbacks such as scx_bpf_cpuperf_set() see updated PELT values, and cover SNT_REPICK.
- Refresh other-class load_avgs in put_prev_task_scx() when transitioning to idle_sched_class before ops.update_idle() and update_idle_rq_clock_pelt().
- Link to v1: https://lore.kernel.org/r/[email protected]

 kernel/sched/ext/ext.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
kernel/sched/ext/ext.c +18 -9
index 10de6bd865e8..523f86be4d26 100644
@@ -3579,6 +3579,15 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 	if (unlikely(rq->scx.kick_sync_pending) && (type == SNT_PICK || type == SNT_REPICK))
 		queue_balance_callback(rq, &rq->scx.kick_sync_bal_cb, kick_sync_wait_bal_cb);
 
+	/*
+	 * Refresh other-class (RT/DL/IRQ/HW) load averages before ops.running()
+	 * (or returning on SNT_REPICK) so BPF callbacks such as
+	 * scx_bpf_cpuperf_set() observe fresh utilization while CFS PELT is
+	 * dormant. In the future, we might want to add a mechanism to update
+	 * load_avgs periodically on tick-stopped CPUs.
+	 */
+	update_other_load_avgs(rq);
+
 	if (type == SNT_REPICK)
 		return;
 
@@ -3619,10 +3628,6 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 	 * @p is getting newly scheduled or got kicked after someone updated its
 	 * slice. Update SCX_RQ_CAN_STOP_TICK to reflect whether the tick can be
 	 * stopped. See scx_can_stop_tick().
-	 *
-	 * Moreover, refresh the load_avgs just when transitioning in and out of
-	 * nohz. In the future, we might want to add a mechanism to update
-	 * load_avgs periodically on tick-stopped CPUs.
 	 */
 	if (can_stop_tick) {
 		if (!(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) {
@@ -3634,14 +3639,10 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, enum snt_e t
 			 */
 			rq->scx.flags |= SCX_RQ_CAN_STOP_TICK;
 			sched_update_tick_dependency(rq);
-
-			update_other_load_avgs(rq);
 		}
 	} else {
-		if (rq->scx.flags & SCX_RQ_CAN_STOP_TICK) {
+		if (rq->scx.flags & SCX_RQ_CAN_STOP_TICK)
 			rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
-			update_other_load_avgs(rq);
-		}
 
 		/*
 		 * @rq still references the outgoing scheduling context. A finite
@@ -3863,6 +3864,14 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,
 	if (next && next->sched_class != &ext_sched_class) {
 		scx_cid_sched_update(rq, NULL);
 		switch_class(rq, next);
+
+		/*
+		 * Refresh other-class load averages before entering idle so
+		 * ops.update_idle() and update_idle_rq_clock_pelt() observe
+		 * decayed RT/DL/IRQ/HW metrics while CFS PELT is dormant.
+		 */
+		if (sched_class_above(&ext_sched_class, next->sched_class))
+			update_other_load_avgs(rq);
 	}
 }
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
reviewer
#3 lore raw

Re: [PATCH v2] sched_ext: Update other_load_avgs on set_next_task_scx and idle entry

Hello, Peng.

Quoted 6 lines · Peng Huang

If SCX_RQ_CAN_STOP_TICK stays unchanged (e.g., remaining set across an idle or RT interval on tickless CPUs, or remaining clear on finite-slice switches before a tick fires), or when an SCX task goes idle after an RT/DL interval without an intervening tick, stale RT/DL PELT values can remain elevated when consumers such as effective_cpu_util() or update_idle_rq_clock_pelt() read them.

I think this overstates the staleness. While the tick is running, scx_tick() updates them on every tick, so they're at most a tick old, at idle entry too. That's already a lot more often than fair, which updates them at load balancing intervals.

Quoted 4 lines

Refresh update_other_load_avgs() once at the entry of set_next_task_scx() (before SNT_REPICK early return and before ops.running() for SNT_PICK / SNT_NORMAL), and in put_prev_task_scx() when transitioning to the idle class.

update_other_load_avgs() isn't necessarily cheap and I don't think we want to call it from scheduling hot paths. If anything, sched_ext is already updating them too often and we should pace it down.

The actual hole is CPUs with the tick stopped, where nothing updates them, and this patch doesn't address that.

Also, how was this tested? Is this from reading the code, or was the effect observed or tested in some way?

Thanks.

--
tejun

author
#4 lore raw

Re: [PATCH v2] sched_ext: Update other_load_avgs on set_next_task_scx and idle entry

Hello Tejun,

Quoted 9 lines · Tejun Heo

I think this overstates the staleness. While the tick is running, scx_tick() updates them on every tick, so they're at most a tick old, at idle entry too. That's already a lot more often than fair, which updates them at load balancing intervals.

update_other_load_avgs() isn't necessarily cheap and I don't think we want to call it from scheduling hot paths. If anything, sched_ext is already updating them too often and we should pace it down.

Quoted 5 lines

The actual hole is CPUs with the tick stopped, where nothing updates them, and this patch doesn't address that.

Also, how was this tested? Is this from reading the code, or was the effect observed or tested in some way?

Fair points on all counts -- calling update_other_load_avgs() from set_next_task_scx() and put_prev_task_scx() adds unnecessary hot-path cost when scx_tick() already covers active-tick CPUs every tick.

To answer your question on testing: on an Android 6.18 setup running sched_ext, we observed frozen/elevated avg_rt in Perfetto traces, but the actual root cause there turned out to be idle rq_clock_pelt() stalling due to non-zero dormant rq->cfs.avg.util_sum (discussed in the separate sched/pelt thread). This upstream patch came from reading the SCX_RQ_CAN_STOP_TICK code paths afterwards rather than an isolated reproducer on mainline.

Let's drop this patch. If we later observe a concrete issue on tick-stopped CPUs, we can come back with a targeted fix and benchmark numbers.

Thanks,
Peng