sched/pelt: Skip dormant cfs util_sum under scx_switched_all
Subsystems (3)
PATCH 1/1 sched/pelt: Skip dormant cfs util_sum under scx_switched_all
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().
--- kernel/sched/pelt.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/pelt.h +6 -1
@@ -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;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Vincent Guittot
maintainerQuoted 5 lines · Peng Huang
When
scx_switched_all()is enabled, all fair-class tasks move tosched_ext,scheduler_tick()skipssched_balance_trigger(), andfair_rqscheduling is bypassed. As a result,rq->cfs.avg.util_sumstops 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
Quoted 10 lines
When a CPU later goes idle,
update_idle_rq_clock_pelt()still adds this dormantrq->cfs.avg.util_sum. If stale cfs.avg.util_sum plus active RT and DLutil_sumreaches divider,update_idle_rq_clock_pelt()treats the rq as overutilized, continuously incrementsrq->lost_idle_time, and stallsrq_clock_pelt(), which in turn freezes RT and DL PELT updates.Exclude
rq->cfs.avg.util_suminupdate_idle_rq_clock_pelt()whilescx_switched_all()is active so dormant CFS load cannot stall idlerq_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
Quoted 29 lines
Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")Signed-off-by Peng Huang <[email protected]>--- kernel/sched/pelt.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)kernel/sched/pelt.h +6
-2@@ -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
Quoted 10 lines · Vincent Guittot
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_sumshoudl 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
Vincent Guittot
maintainerQuoted 54 lines · Peng Huang
Quoted 10 lines · Vincent Guittot
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_sumshoudl 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 treeThanks 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->removedand unvisited sibling task groups:
Tasks that exited (task_dead_fair()) or migrated while sleeping (migrate_task_rq_fair()) beforescx_enable()queue their load/util incfs_rq->removedviaremove_entity_load_avg()rather than deducting and propagating immediately.During
scx_enable(),switched_from_fair()->detach_task_cfs_rq()only visits and propagates alongcfs_rqchains that still have at least one live attached fair task. Any CPU or sibling cgroupcfs_rqwith no remaining live fair task is never visited, so itscfs_rq->removedstays undrained and itstg_secontribution remains inrq->cfs.avg. Once__scx_switched_allis enabled,scheduler_tick()skipssched_balance_trigger()andfair_sched_classis bypassed, so__update_blocked_fair()never runs to drain and propagate those remaining leafcfs_rqs.2. Sub-divider
util_sumresiduals:
Even on visited chains, iftg_se->avg.util_avghas 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()seesdelta_avg== 0 and returns early without subtractingtg_se->avg.util_sumfrom the parentcfs_rq. Independent PELT decay truncation and integer removed.util_avg accounting can also leave a < divider remainder incfs_rq->avg.util_sumafterutil_avghits 0.For v2, rather than touching
update_idle_rq_clock_pelt(), how about adding a fair helper (similar in spirit torq_offline_fair()->clear_tg_offline_cfs_rqs()) and calling it fromscx_enable()in the existing per-CPUscx_switched_all()loop (whererq->lockis held,rq->clockis updated, andrq->cfs.h_nr_queued== 0)? Specifically, it would walkleaf_cfs_rq_listbottom-up (plus&rq->cfs) to:
- flushcfs_rq->removed,
- clear load/runnable/util_sumand_avgon the emptycfs_rqsand theirtg_seswhile preservinglast_update_timeandperiod_contrib,
- clear tg contributions viaclear_tg_load_avg(), remove the emptycfs_rqsfromleaf_cfs_rq_list, and updaterq->has_blocked_load.
Quoted 1 line
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
Quoted 3 lines
Thanks,
Peng
Quoted 7 lines · Vincent Guittot
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 withutil_avg== 0 whereas we still have autil_sum!= 0, we can usedelta_sum=gcfs_rq->avg.util_sum-se->avg.util_sum; inupdate_tg_cfs_util()instead. Whenutil_avg== 0 we will be sure to clearutil_sumtoo
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
Tejun Heo
reviewerHello, Peng.
Quoted 11 lines · Peng Huang
For v2, rather than touching
update_idle_rq_clock_pelt(), how about adding a fair helper (similar in spirit torq_offline_fair()->clear_tg_offline_cfs_rqs()) and calling it fromscx_enable()in the existing per-CPUscx_switched_all()loop (whererq->lockis held,rq->clockis updated, andrq->cfs.h_nr_queued== 0)? Specifically, it would walkleaf_cfs_rq_listbottom-up (plus&rq->cfs) to:
- flushcfs_rq->removed,
- clear load/runnable/util_sumand_avgon the emptycfs_rqsand theirtg_seswhile preservinglast_update_timeandperiod_contrib,
- clear tg contributions viaclear_tg_load_avg(), remove the emptycfs_rqsfromleaf_cfs_rq_list, and updaterq->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
Hi Tejun,
Quoted 7 lines · Tejun Heo
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 walksleaf_cfs_rq_listbottom-up, flushescfs_rq->removedand propagates the result to the parent. It just stops running oncescx_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/[email protected]
Quoted 3 lines
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
v2 cover sched_ext, sched/fair: Drain blocked fair load on scx_switched_all
In reply to Peng Huangv1 [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/[email protected]
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(-)
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH v2 1/2 sched/fair: Clear residual sums on zero task-group avg propagation
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.
--- kernel/sched/fair.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-)
kernel/sched/fair.c +27 -8
@@ -5516,9 +5516,15 @@ update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rqlong 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 cfu64 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;
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH v2 2/2 sched_ext, sched/fair: Drain blocked load on scx_switched_all
In reply to Peng HuangTasks 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.
--- kernel/sched/ext/ext.c | 9 ++++----- kernel/sched/fair.c | 7 ++++++- kernel/sched/sched.h | 1 + 3 files changed, 11 insertions(+), 6 deletions(-)
kernel/sched/ext/ext.c +4 -5
@@ -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); } }
kernel/sched/fair.c +6 -1
@@ -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);
kernel/sched/sched.h +1 -0
@@ -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);
Signature · 1 line
2.56.1.831.g720f3f5937-goog