[PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all

Peng Huang posted 1 patch 2 days, 9 hours ago
kernel/sched/pelt.h | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Peng Huang 2 days, 9 hours ago
When scx_switched_all() is enabled, all fair-class tasks move to
sched_ext, scheduler_tick() skips sched_balance_trigger(), and fair_rq
scheduling is bypassed. As a result, rq->cfs.avg.util_sum stops updating
and retains whatever stale load was present prior to switching to SCX.

When a CPU later goes idle, update_idle_rq_clock_pelt() still adds this
dormant rq->cfs.avg.util_sum. If stale cfs.avg.util_sum plus active RT
and DL util_sum reaches divider, update_idle_rq_clock_pelt() treats the
rq as overutilized, continuously increments rq->lost_idle_time, and
stalls rq_clock_pelt(), which in turn freezes RT and DL PELT updates.

Exclude rq->cfs.avg.util_sum in update_idle_rq_clock_pelt() while
scx_switched_all() is active so dormant CFS load cannot stall idle
rq_clock_pelt().

Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
Cc: stable@vger.kernel.org
Signed-off-by: Peng Huang <penghuang@google.com>
---
 kernel/sched/pelt.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/pelt.h b/kernel/sched/pelt.h
index f921302dc40f..6d31e1fbd094 100644
--- a/kernel/sched/pelt.h
+++ b/kernel/sched/pelt.h
@@ -138,7 +138,12 @@ static inline void update_rq_clock_pelt(struct rq *rq, s64 delta)
 static inline void update_idle_rq_clock_pelt(struct rq *rq)
 {
 	u32 divider = ((LOAD_AVG_MAX - 1024) << SCHED_CAPACITY_SHIFT) - LOAD_AVG_MAX;
-	u32 util_sum = rq->cfs.avg.util_sum;
+	/*
+	 * Under scx_switched_all(), CFS PELT is dormant and may stay stale;
+	 * exclude it so idle CPUs do not trip lost_idle_time and freeze RT
+	 * PELT.
+	 */
+	u32 util_sum = scx_switched_all() ? 0 : rq->cfs.avg.util_sum;
 	util_sum += rq->avg_rt.util_sum;
 	util_sum += rq->avg_dl.util_sum;
 
-- 
2.56.1.750.gf70dc682cf-goog
[PATCH v2 0/2] sched_ext, sched/fair: Drain blocked fair load on scx_switched_all
Posted by Peng Huang 15 hours ago
v1 [1] skipped rq->cfs.avg.util_sum in update_idle_rq_clock_pelt() when
scx_switched_all() was active. As Vincent Guittot pointed out, that hid
the underlying issue: once all fair tasks switch to sched_ext, fair
blocked load should be properly drained and propagated rather than left
stale in rq->cfs.avg.

Investigating why rq->cfs.avg.util_sum remained non-zero showed two
causes:

1. In update_tg_cfs_{util,runnable,load}(), if the group entity @se's
   _avg had already decayed to 0 while its _sum still held a sub-divider
   residual when the child gcfs_rq's _avg also reached 0, delta_avg == 0
   caused an early return without clearing @se's residual _sum from the
   parent cfs_rq.
2. Tasks that exited or migrated while sleeping before scx_enable()
   leave deferred removals in cfs_rq->removed, and switched_from_fair()
   only visits cfs_rq chains with live fair tasks. Once
   __scx_switched_all is enabled, periodic blocked-load updates stop,
   stranding unvisited leaf cfs_rqs in rq->cfs.avg.

Patch 1 clears residual task-group _sum values on zero-avg propagation
while keeping parent period_contrib divider scaling for non-zero
averages. Patch 2 reuses __sched_balance_update_blocked_averages() in
scx_enable()'s per-CPU scx_switched_all() loop to drain cfs_rq->removed
and propagate blocked averages across all leaf cfs_rqs.

Changes in v2:
- Drop the pelt.h workaround from v1 (Vincent Guittot).
- Clear residual sums in update_tg_cfs_{util,runnable,load}() when
  propagating a zero average (Vincent Guittot).
- Reuse __sched_balance_update_blocked_averages() in scx_enable() under
  scx_switched_all() to drain remaining blocked fair load (Vincent
  Guittot).

[1] https://lore.kernel.org/r/20261122015729.2512645-1-penghuang@google.com

Peng Huang (2):
  sched/fair: Clear residual sums on zero task-group avg propagation
  sched_ext, sched/fair: Drain blocked load on scx_switched_all

 kernel/sched/ext/ext.c |  9 ++++-----
 kernel/sched/fair.c    | 42 +++++++++++++++++++++++++++++++++---------
 kernel/sched/sched.h   |  1 +
 3 files changed, 38 insertions(+), 14 deletions(-)

-- 
2.56.1.831.g720f3f5937-goog
[PATCH v2 1/2] sched/fair: Clear residual sums on zero task-group avg propagation
Posted by Peng Huang 15 hours ago
In update_load_avg(), __update_load_avg_se() ages the group entity @se
before propagate_entity_load_avg() runs. If @se's _avg has already
decayed to 0 while its _sum still holds a sub-divider residual, and the
child gcfs_rq's _avg also reaches 0 (e.g. when its last task detaches),
update_tg_cfs_{util,runnable,load}() see delta_avg == 0 and return early
without subtracting @se's remaining _sum from the parent cfs_rq.

Keep scaling non-zero averages by the parent cfs_rq's divider to
preserve period_contrib alignment, while letting zero-avg propagations
proceed whenever @se still holds a non-zero _sum so the residual is
cleared from both @se and its parent cfs_rq.

Fixes: 7ceb77103001 ("sched/pelt: Continue to relax the sync of util_sum with util_avg")
Suggested-by: Vincent Guittot <vincent.guittot@linaro.org>
Cc: stable@vger.kernel.org
Signed-off-by: Peng Huang <penghuang@google.com>
---
 kernel/sched/fair.c | 35 +++++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 0c8af1cac2d5..f6c3bbb19557 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -5516,9 +5516,15 @@ update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq
 	long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg;
 	u32 new_sum, divider;
 
-	/* Nothing to update */
-	if (!delta_avg)
-		return;
+	if (delta_avg == 0) {
+		/* Keep util_sum as-is for unchanged non-zero avg */
+		if (gcfs_rq->avg.util_avg != 0)
+			return;
+
+		/* Nothing to clear once se->avg.util_sum is also 0 */
+		if (se->avg.util_sum == 0)
+			return;
+	}
 
 	/*
 	 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
@@ -5543,9 +5549,15 @@ update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cf
 	u64 new_sum;
 	u32 divider;
 
-	/* Nothing to update */
-	if (!delta_avg)
-		return;
+	if (delta_avg == 0) {
+		/* Keep runnable_sum as-is for unchanged non-zero avg */
+		if (gcfs_rq->avg.runnable_avg != 0)
+			return;
+
+		/* Nothing to clear once se->avg.runnable_sum is 0 */
+		if (se->avg.runnable_sum == 0)
+			return;
+	}
 
 	/*
 	 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
@@ -5617,8 +5629,15 @@ update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq
 	load_avg = div_u64(load_sum, divider);
 
 	delta_avg = load_avg - se->avg.load_avg;
-	if (!delta_avg)
-		return;
+	if (delta_avg == 0) {
+		/* Keep load_sum as-is while child load is non-zero */
+		if (load_sum != 0)
+			return;
+
+		/* Nothing to clear once se->avg.load_sum is also 0 */
+		if (se->avg.load_sum == 0)
+			return;
+	}
 
 	delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum;
 
-- 
2.56.1.831.g720f3f5937-goog
Re: [PATCH v2 1/2] sched/fair: Clear residual sums on zero task-group avg propagation
Posted by Vincent Guittot 52 minutes ago
On Mon, 23 Nov 2026 at 21:13, Peng Huang <penghuang@google.com> wrote:
>
> In update_load_avg(), __update_load_avg_se() ages the group entity @se
> before propagate_entity_load_avg() runs. If @se's _avg has already
> decayed to 0 while its _sum still holds a sub-divider residual, and the
> child gcfs_rq's _avg also reaches 0 (e.g. when its last task detaches),
> update_tg_cfs_{util,runnable,load}() see delta_avg == 0 and return early
> without subtracting @se's remaining _sum from the parent cfs_rq.
>
> Keep scaling non-zero averages by the parent cfs_rq's divider to
> preserve period_contrib alignment, while letting zero-avg propagations
> proceed whenever @se still holds a non-zero _sum so the residual is
> cleared from both @se and its parent cfs_rq.

When fair tasks are involved, the next decays will clear the *_sum
anyway so I suggest to skip this patch if it's not needed for
scx_switched_all() case

>
> Fixes: 7ceb77103001 ("sched/pelt: Continue to relax the sync of util_sum with util_avg")
> Suggested-by: Vincent Guittot <vincent.guittot@linaro.org>
> Cc: stable@vger.kernel.org
> Signed-off-by: Peng Huang <penghuang@google.com>
> ---
>  kernel/sched/fair.c | 35 +++++++++++++++++++++++++++--------
>  1 file changed, 27 insertions(+), 8 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 0c8af1cac2d5..f6c3bbb19557 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -5516,9 +5516,15 @@ update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq
>         long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg;
>         u32 new_sum, divider;
>
> -       /* Nothing to update */
> -       if (!delta_avg)
> -               return;
> +       if (delta_avg == 0) {
> +               /* Keep util_sum as-is for unchanged non-zero avg */
> +               if (gcfs_rq->avg.util_avg != 0)
> +                       return;
> +
> +               /* Nothing to clear once se->avg.util_sum is also 0 */
> +               if (se->avg.util_sum == 0)
> +                       return;
> +       }
>
>         /*
>          * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
> @@ -5543,9 +5549,15 @@ update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cf
>         u64 new_sum;
>         u32 divider;
>
> -       /* Nothing to update */
> -       if (!delta_avg)
> -               return;
> +       if (delta_avg == 0) {
> +               /* Keep runnable_sum as-is for unchanged non-zero avg */
> +               if (gcfs_rq->avg.runnable_avg != 0)
> +                       return;
> +
> +               /* Nothing to clear once se->avg.runnable_sum is 0 */
> +               if (se->avg.runnable_sum == 0)
> +                       return;
> +       }
>
>         /*
>          * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
> @@ -5617,8 +5629,15 @@ update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq
>         load_avg = div_u64(load_sum, divider);
>
>         delta_avg = load_avg - se->avg.load_avg;
> -       if (!delta_avg)
> -               return;
> +       if (delta_avg == 0) {
> +               /* Keep load_sum as-is while child load is non-zero */
> +               if (load_sum != 0)
> +                       return;
> +
> +               /* Nothing to clear once se->avg.load_sum is also 0 */
> +               if (se->avg.load_sum == 0)
> +                       return;
> +       }
>
>         delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum;
>
> --
> 2.56.1.831.g720f3f5937-goog
>
[PATCH v2 2/2] sched_ext, sched/fair: Drain blocked load on scx_switched_all
Posted by Peng Huang 15 hours ago
Tasks that exit or migrate while sleeping queue their PELT load and
utilization in cfs_rq->removed rather than deducting and propagating
immediately. When scx_enable() switches all fair tasks to sched_ext,
switched_from_fair() only visits cfs_rq chains that still hold live
fair tasks, leaving unvisited leaf cfs_rqs undrained in rq->cfs.avg.

Once __scx_switched_all is enabled, scheduler_tick() skips
sched_balance_trigger() and fair_sched_class is bypassed, freezing
blocked-load updates. Stale rq->cfs.avg.util_sum can then combine with
active RT/DL util_sum in update_idle_rq_clock_pelt() to exceed divider,
inflating rq->lost_idle_time and stalling rq_clock_pelt().

Export __sched_balance_update_blocked_averages() and call it on each
CPU in scx_enable()'s per-CPU scx_switched_all() loop so pending
cfs_rq->removed entries and task-group propagations are drained cleanly.

Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
Suggested-by: Vincent Guittot <vincent.guittot@linaro.org>
Cc: stable@vger.kernel.org
Signed-off-by: Peng Huang <penghuang@google.com>
---
 kernel/sched/ext/ext.c | 9 ++++-----
 kernel/sched/fair.c    | 7 ++++++-
 kernel/sched/sched.h   | 1 +
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 10de6bd865e8..f7c573092e17 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -9013,11 +9013,9 @@ static void scx_root_enable_workfn(struct kthread_work *work)
 		static_branch_enable(&__scx_switched_all);
 
 	/*
-	 * Detach the fair_server bandwidth reservation now that the switch
-	 * is fully committed. In full mode (!SCX_OPS_SWITCH_PARTIAL) no
-	 * task will ever run in the fair class, so give that bandwidth
-	 * back to the RT class. The matching ext_server attach already
-	 * happened earlier; this only releases bandwidth and cannot fail.
+	 * Detach the fair_server bandwidth reservation and drain any
+	 * remaining blocked or removed fair load now that the switch is
+	 * fully committed and periodic fair blocked-load updates stop.
 	 *
 	 * In partial mode keep fair_server attached.
 	 */
@@ -9028,6 +9026,7 @@ static void scx_root_enable_workfn(struct kthread_work *work)
 			guard(rq_lock_irqsave)(rq);
 			update_rq_clock(rq);
 			dl_server_detach_bw(&rq->fair_server);
+			__sched_balance_update_blocked_averages(rq);
 		}
 	}
 
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index f6c3bbb19557..f19f498e01a4 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11655,10 +11655,15 @@ static unsigned long task_h_load(struct task_struct *p)
 }
 #endif /* !CONFIG_FAIR_GROUP_SCHED */
 
-static void __sched_balance_update_blocked_averages(struct rq *rq)
+/*
+ * Update blocked PELT averages for RT/DL/IRQ and fair cfs_rqs on @rq.
+ * Caller must hold @rq's lock and have updated @rq's clock.
+ */
+void __sched_balance_update_blocked_averages(struct rq *rq)
 {
 	bool decayed = false, done = true;
 
+	lockdep_assert_rq_held(rq);
 	update_blocked_load_tick(rq);
 
 	decayed |= __update_blocked_others(rq, &done);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 220927453a88..71a69f90e5df 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2977,6 +2977,7 @@ extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);
 extern void update_group_capacity(struct sched_domain *sd, int cpu);
 
 extern void sched_balance_trigger(struct rq *rq);
+void __sched_balance_update_blocked_averages(struct rq *rq);
 
 extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx);
 extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx);
-- 
2.56.1.831.g720f3f5937-goog
Re: [PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Vincent Guittot 1 day ago
On Sun, 22 Nov 2026 at 02:57, Peng Huang <penghuang@google.com> wrote:
>
> When scx_switched_all() is enabled, all fair-class tasks move to
> sched_ext, scheduler_tick() skips sched_balance_trigger(), and fair_rq
> scheduling is bypassed. As a result, rq->cfs.avg.util_sum stops updating
> and retains whatever stale load was present prior to switching to SCX.

When all tasks are moved to sched_ext, fair utilization should be 0

>
> When a CPU later goes idle, update_idle_rq_clock_pelt() still adds this
> dormant rq->cfs.avg.util_sum. If stale cfs.avg.util_sum plus active RT
> and DL util_sum reaches divider, update_idle_rq_clock_pelt() treats the
> rq as overutilized, continuously increments rq->lost_idle_time, and
> stalls rq_clock_pelt(), which in turn freezes RT and DL PELT updates.
>
> Exclude rq->cfs.avg.util_sum in update_idle_rq_clock_pelt() while
> scx_switched_all() is active so dormant CFS load cannot stall idle
> rq_clock_pelt().

This is not the right solution but a bandaid to hide the issue. When
there is not fair task,  rq->cfs.avg.util_sum shoudl become null.
Please look at why it is not null.
It could be that the update stops before everything has been
propagated correctly in the task group tree

>
> Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
> Cc: stable@vger.kernel.org
> Signed-off-by: Peng Huang <penghuang@google.com>
> ---
>  kernel/sched/pelt.h | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/sched/pelt.h b/kernel/sched/pelt.h
> index f921302dc40f..6d31e1fbd094 100644
> --- a/kernel/sched/pelt.h
> +++ b/kernel/sched/pelt.h
> @@ -138,7 +138,12 @@ static inline void update_rq_clock_pelt(struct rq *rq, s64 delta)
>  static inline void update_idle_rq_clock_pelt(struct rq *rq)
>  {
>         u32 divider = ((LOAD_AVG_MAX - 1024) << SCHED_CAPACITY_SHIFT) - LOAD_AVG_MAX;
> -       u32 util_sum = rq->cfs.avg.util_sum;
> +       /*
> +        * Under scx_switched_all(), CFS PELT is dormant and may stay stale;
> +        * exclude it so idle CPUs do not trip lost_idle_time and freeze RT
> +        * PELT.
> +        */
> +       u32 util_sum = scx_switched_all() ? 0 : rq->cfs.avg.util_sum;
>         util_sum += rq->avg_rt.util_sum;
>         util_sum += rq->avg_dl.util_sum;
>
> --
> 2.56.1.750.gf70dc682cf-goog
>
Re: [PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Peng Huang 19 hours ago
On Mon, 23 Nov 2026 at 11:34, Vincent Guittot <vincent.guittot@linaro.org> wrote:
>
> When all tasks are moved to sched_ext, fair utilization should be 0
>
> ...
>
> This is not the right solution but a bandaid to hide the issue. When
> there is not fair task,  rq->cfs.avg.util_sum shoudl become null.
> Please look at why it is not null.
> It could be that the update stops before everything has been
> propagated correctly in the task group tree

Thanks for pointing this out -- you are right, and looking into the
code confirms your suspicion about stopped updates and incomplete task
group propagation:

1. Deferred cfs_rq->removed and unvisited sibling task groups:
   Tasks that exited (task_dead_fair()) or migrated while sleeping
   (migrate_task_rq_fair()) before scx_enable() queue their load/util
   in cfs_rq->removed via remove_entity_load_avg() rather than
   deducting and propagating immediately.

   During scx_enable(), switched_from_fair() -> detach_task_cfs_rq()
   only visits and propagates along cfs_rq chains that still have at
   least one live attached fair task. Any CPU or sibling cgroup cfs_rq
   with no remaining live fair task is never visited, so its
   cfs_rq->removed stays undrained and its tg_se contribution remains
   in rq->cfs.avg. Once __scx_switched_all is enabled,
   scheduler_tick() skips sched_balance_trigger() and fair_sched_class
   is bypassed, so __update_blocked_fair() never runs to drain and
   propagate those remaining leaf cfs_rqs.

2. Sub-divider util_sum residuals:
   Even on visited chains, if tg_se->avg.util_avg has already decayed
   to 0 (with 0 < tg_se->avg.util_sum < divider) when the child
   gcfs_rq's last task detaches, update_tg_cfs_util() sees
   delta_avg == 0 and returns early without subtracting
   tg_se->avg.util_sum from the parent cfs_rq. Independent PELT decay
   truncation and integer removed.util_avg accounting can also leave a
   < divider remainder in cfs_rq->avg.util_sum after util_avg hits 0.

For v2, rather than touching update_idle_rq_clock_pelt(), how about
adding a fair helper (similar in spirit to rq_offline_fair() ->
clear_tg_offline_cfs_rqs()) and calling it from scx_enable() in the
existing per-CPU scx_switched_all() loop (where rq->lock is held,
rq->clock is updated, and rq->cfs.h_nr_queued == 0)? Specifically, it
would walk leaf_cfs_rq_list bottom-up (plus &rq->cfs) to:
- flush cfs_rq->removed,
- clear load/runnable/util _sum and _avg on the empty cfs_rqs and
  their tg_ses while preserving last_update_time and period_contrib,
- clear tg contributions via clear_tg_load_avg(), remove the empty
  cfs_rqs from leaf_cfs_rq_list, and update rq->has_blocked_load.

Does this direction look reasonable to you?

Thanks,
Peng
Re: [PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Tejun Heo 11 hours ago
Hello, Peng.

On Mon, Nov 23, 2026 at 08:09:34AM -0800, Peng Huang wrote:
> For v2, rather than touching update_idle_rq_clock_pelt(), how about
> adding a fair helper (similar in spirit to rq_offline_fair() ->
> clear_tg_offline_cfs_rqs()) and calling it from scx_enable() in the
> existing per-CPU scx_switched_all() loop (where rq->lock is held,
> rq->clock is updated, and rq->cfs.h_nr_queued == 0)? Specifically, it
> would walk leaf_cfs_rq_list bottom-up (plus &rq->cfs) to:
> - flush cfs_rq->removed,
> - clear load/runnable/util _sum and _avg on the empty cfs_rqs and
>   their tg_ses while preserving last_update_time and period_contrib,
> - clear tg contributions via clear_tg_load_avg(), remove the empty
>   cfs_rqs from leaf_cfs_rq_list, and update rq->has_blocked_load.

I'm not familiar with this code, so I may be missing something, but
doesn't __update_blocked_fair() already do most of this? It walks
leaf_cfs_rq_list bottom-up, flushes cfs_rq->removed and propagates the
result to the parent. It just stops running once scx_switched_all() is
set. Maybe the helper can run the blocked load update once on each rq
from that loop instead of open-coding the clearing? If I'm reading it
right, one pass after all the tasks have switched would take care of #1.

As for #2, are the leftover sums big enough to matter for
update_idle_rq_clock_pelt()? It looks like fair can leave similar
leftovers behind once the avgs reach zero.

Thanks.

--
tejun
Re: [PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Peng Huang 10 hours ago
Hi Tejun,

On Mon, Nov 23, 2026 at 01:48:44PM -1000, Tejun Heo wrote:
> I'm not familiar with this code, so I may be missing something, but
> doesn't __update_blocked_fair() already do most of this? It walks
> leaf_cfs_rq_list bottom-up, flushes cfs_rq->removed and propagates the
> result to the parent. It just stops running once scx_switched_all() is
> set. Maybe the helper can run the blocked load update once on each rq
> from that loop instead of open-coding the clearing? If I'm reading it
> right, one pass after all the tasks have switched would take care of #1.

Yes, Vincent suggested the same thing, and I sent v2 earlier today doing
that one-pass drain via __sched_balance_update_blocked_averages(rq) in
scx_enable():

  https://lore.kernel.org/r/cover.1795462673.git.penghuang@google.com

> As for #2, are the leftover sums big enough to matter for
> update_idle_rq_clock_pelt()? It looks like fair can leave similar
> leftovers behind once the avgs reach zero.

You're right -- for update_idle_rq_clock_pelt(), #2 on its own is tiny
(< divider, i.e. < 1/1024 capacity per recently-decayed group) and is
not nearly enough to cause an issue once #1 is drained. #1 was the real
culprit in our reproduction.

I only split #2 out as patch 1/2 in v2 per Vincent's suggestion to clean
up the util_sum != 0 residual in update_tg_cfs_*() when _avg == 0 (which
otherwise keeps cfs_rq_is_decayed() false and leaves empty parent
cfs_rqs on leaf_cfs_rq_list in fair). Patch 2/2 alone already resolves
the sched_ext issue.

Thanks,
Peng
Re: [PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Vincent Guittot 54 minutes ago
On Tue, 24 Nov 2026 at 01:47, Peng Huang <penghuang@google.com> wrote:
>
> Hi Tejun,
>
> On Mon, Nov 23, 2026 at 01:48:44PM -1000, Tejun Heo wrote:
> > I'm not familiar with this code, so I may be missing something, but
> > doesn't __update_blocked_fair() already do most of this? It walks
> > leaf_cfs_rq_list bottom-up, flushes cfs_rq->removed and propagates the
> > result to the parent. It just stops running once scx_switched_all() is
> > set. Maybe the helper can run the blocked load update once on each rq
> > from that loop instead of open-coding the clearing? If I'm reading it
> > right, one pass after all the tasks have switched would take care of #1.
>
> Yes, Vincent suggested the same thing, and I sent v2 earlier today doing
> that one-pass drain via __sched_balance_update_blocked_averages(rq) in
> scx_enable():
>
>   https://lore.kernel.org/r/cover.1795462673.git.penghuang@google.com
>
> > As for #2, are the leftover sums big enough to matter for
> > update_idle_rq_clock_pelt()? It looks like fair can leave similar
> > leftovers behind once the avgs reach zero.
>
> You're right -- for update_idle_rq_clock_pelt(), #2 on its own is tiny
> (< divider, i.e. < 1/1024 capacity per recently-decayed group) and is
> not nearly enough to cause an issue once #1 is drained. #1 was the real
> culprit in our reproduction.
>
> I only split #2 out as patch 1/2 in v2 per Vincent's suggestion to clean
> up the util_sum != 0 residual in update_tg_cfs_*() when _avg == 0 (which
> otherwise keeps cfs_rq_is_decayed() false and leaves empty parent
> cfs_rqs on leaf_cfs_rq_list in fair). Patch 2/2 alone already resolves
> the sched_ext issue.

If patch 1/2 is not needed for scx_switched_all() case, I suggest to
skip it. When fair tasks are involved, the *_sum will decay to 0 at
during update

>
> Thanks,
> Peng
Re: [PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Vincent Guittot 18 hours ago
On Mon, 23 Nov 2026 at 17:09, Peng Huang <penghuang@google.com> wrote:
>
> On Mon, 23 Nov 2026 at 11:34, Vincent Guittot <vincent.guittot@linaro.org> wrote:
> >
> > When all tasks are moved to sched_ext, fair utilization should be 0
> >
> > ...
> >
> > This is not the right solution but a bandaid to hide the issue. When
> > there is not fair task,  rq->cfs.avg.util_sum shoudl become null.
> > Please look at why it is not null.
> > It could be that the update stops before everything has been
> > propagated correctly in the task group tree
>
> Thanks for pointing this out -- you are right, and looking into the
> code confirms your suspicion about stopped updates and incomplete task
> group propagation:
>
> 1. Deferred cfs_rq->removed and unvisited sibling task groups:
>    Tasks that exited (task_dead_fair()) or migrated while sleeping
>    (migrate_task_rq_fair()) before scx_enable() queue their load/util
>    in cfs_rq->removed via remove_entity_load_avg() rather than
>    deducting and propagating immediately.
>
>    During scx_enable(), switched_from_fair() -> detach_task_cfs_rq()
>    only visits and propagates along cfs_rq chains that still have at
>    least one live attached fair task. Any CPU or sibling cgroup cfs_rq
>    with no remaining live fair task is never visited, so its
>    cfs_rq->removed stays undrained and its tg_se contribution remains
>    in rq->cfs.avg. Once __scx_switched_all is enabled,
>    scheduler_tick() skips sched_balance_trigger() and fair_sched_class
>    is bypassed, so __update_blocked_fair() never runs to drain and
>    propagate those remaining leaf cfs_rqs.
>
> 2. Sub-divider util_sum residuals:
>    Even on visited chains, if tg_se->avg.util_avg has already decayed
>    to 0 (with 0 < tg_se->avg.util_sum < divider) when the child
>    gcfs_rq's last task detaches, update_tg_cfs_util() sees
>    delta_avg == 0 and returns early without subtracting
>    tg_se->avg.util_sum from the parent cfs_rq. Independent PELT decay
>    truncation and integer removed.util_avg accounting can also leave a
>    < divider remainder in cfs_rq->avg.util_sum after util_avg hits 0.
>
> For v2, rather than touching update_idle_rq_clock_pelt(), how about
> adding a fair helper (similar in spirit to rq_offline_fair() ->
> clear_tg_offline_cfs_rqs()) and calling it from scx_enable() in the
> existing per-CPU scx_switched_all() loop (where rq->lock is held,
> rq->clock is updated, and rq->cfs.h_nr_queued == 0)? Specifically, it
> would walk leaf_cfs_rq_list bottom-up (plus &rq->cfs) to:
> - flush cfs_rq->removed,
> - clear load/runnable/util _sum and _avg on the empty cfs_rqs and
>   their tg_ses while preserving last_update_time and period_contrib,
> - clear tg contributions via clear_tg_load_avg(), remove the empty
>   cfs_rqs from leaf_cfs_rq_list, and update rq->has_blocked_load.
>
> Does this direction look reasonable to you?

Yes this seems a better solution but It would be good to reuse
__update_blocked_fair() which already does the work for pelt signal.
Regarding your issue with util_avg == 0 whereas we still have a
util_sum != 0, we can use delta_sum = gcfs_rq->avg.util_sum -
se->avg.util_sum; in update_tg_cfs_util() instead. When util_avg == 0
we will be sure to clear util_sum too

Thanks,
Vincent
>
> Thanks,
> Peng
Re: [PATCH] sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Posted by Peng Huang 15 hours ago
On Mon, 23 Nov 2026 at 18:07, Vincent Guittot
<vincent.guittot@linaro.org> wrote:
>
> Yes this seems a better solution but It would be good to reuse
> __update_blocked_fair() which already does the work for pelt signal.
> Regarding your issue with util_avg == 0 whereas we still have a
> util_sum != 0, we can use delta_sum = gcfs_rq->avg.util_sum -
> se->avg.util_sum; in update_tg_cfs_util() instead. When util_avg == 0
> we will be sure to clear util_sum too

Thanks for the suggestions, Vincent!

One small detail in update_tg_cfs_util(): under CONFIG_CFS_BANDWIDTH,
gcfs_rq and its parent cfs_rq can have different period_contrib, so for
non-zero util_avg we still want new_sum = se->avg.util_avg * divider
using the parent cfs_rq's divider. Keeping that calculation while only
skipping the update when gcfs_rq->avg.util_avg != 0 or
se->avg.util_sum == 0 preserves period_contrib alignment and clears
residual _sum whenever util_avg reaches 0 (and similarly in
update_tg_cfs_{runnable,load}()).

I have sent out a 2-patch v2 series implementing this along with reusing
__sched_balance_update_blocked_averages() in scx_enable():
  [PATCH v2 0/2] sched_ext, sched/fair: Drain blocked fair load on scx_switched_all

Thanks,
Peng