sched_ext: Report a sub-scheduler's ecaps on attach, detach and cpu hotplug
Subsystems (2)
Tejun Heo
reviewerPATCH v2 1/1 sched_ext: Report a sub-scheduler's ecaps on attach, detach and cpu hotplug
Hello,
v2 (detach and cpu hotplug):
- Report the caps on detach as well as attach, and deliver regardless of bypass, which drops the replay at unbypass (#3).
- Drop the sync_pcpu_ecaps() factoring. Add the cid_pshard() factoring and free a sub-scheduler a grace period after discarding its syncs (#1, #2).
- Deliver only while the root has the cpu online, report the loss when a cpu goes offline, and leave suspend and hibernation out (#4-#6).
v1: https://lore.kernel.org/r/[email protected]
A sub-scheduler learns which cids it holds through ops.sub_ecaps_updated(), but the grants its parent makes when it attaches are reported only after its enable bypass is lifted, which is also when its tasks are handed to it. So it gets its first ops.enqueue() calls before it has been notified of a single cid and has to hold the tasks somewhere until the grants arrive.
The losses aren't reported either. The caps go away on detach and when a cpu goes offline without a delivery to the sub-scheduler or its parent. The reseed of a cpu coming back can also run their ops before the root's ops.cpu_online().
With this patchset, a sub-scheduler's ecaps change only with the deliveries that report them. The attach-time caps are reported before bypass lifts, the loss on detach before any task goes back to the parent, and a cpu's loss and regain around the root's ops.cpu_offline() and ops.cpu_online(). Suspend and hibernation are left out: a cpu they take down keeps the caps last delivered, and the resume reports only the net change.
Verified with sub-schedulers attaching and detaching with tasks queued and caps granted and revoked, through cpu hotplug with the races widened by debug patches and grants flooding a cpu as it goes down, on PREEMPT_RT as well, and across suspend. The protocol between the reports, the per-cpu syncs, cpu hotplug, suspend and the free path was model checked with TLC for small cpu counts.
Based on sched_ext/for-7.4 (db87a8f530b6).
This patchset contains the following 6 patches.
0001 sched_ext: Factor out cid_pshard()
0002 sched_ext: Free a sub-scheduler a grace period after discarding its ecaps syncs 0003 sched_ext: Report a sub-scheduler's ecaps on attach and detach 0004 sched_ext: Deliver ecaps only while the root has the cpu online 0005 sched_ext: Report the ecaps loss when a cpu goes offline
0006 sched_ext: Leave suspend and hibernation out of the ecaps reports
The patches are organized as follows:
- 0001-0002 are prep. 0002 stops the free path from relying on a bypassing sched's syncs calling no op.
- 0003 reports the caps on attach and detach.
- 0004-0006 cover cpu hotplug.
The patchset is also available in the following git branch:
git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git sub-attach-ecaps-v2
diffstat follows. Thanks.
kernel/sched/ext/ext.c | 116 ++++++-- kernel/sched/ext/internal.h | 69 ++++- kernel/sched/ext/sub.c | 612 +++++++++++++++++++++++++++++++---------- kernel/sched/ext/sub.h | 10 +- kernel/sched/sched.h | 8 + tools/sched_ext/scx_qmap.bpf.c | 25 +- 6 files changed, 625 insertions(+), 215 deletions(-)
--
tejun
Tejun Heo
reviewerPATCH 1/6 sched_ext: Factor out cid_pshard()
Factor the cid to pshard lookup out of the ecaps sync paths into cid_pshard() so that new sync paths can share it. No functional change.
--- kernel/sched/ext/sub.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
kernel/sched/ext/sub.c +10 -6
@@ -1063,6 +1063,12 @@ static void discard_queued_syncs(struct rq *rq)init_llist_node (pos); } +/* the pshard holding @sch's caps on @cid */ +static struct scx_pshard *cid_pshard (struct scx_sched *sch,s32 cid) +{ +return sch->pshard[rcu_dereference_all (scx_cid_to_shard)[cid]]; +} +/** * __scx_process_sync_ecaps - Sync this cpu's ecaps to pshard->caps[] * @rq: the cid's cpu rq
@@ -1079,7 +1085,7 @@ static void discard_queued_syncs(struct rq *rq)void __scx_process_sync_ecaps (struct rq *rq,struct task_struct *prev) {s32 cpu =cpu_of (rq); -s32 cid, shard; +s32 cid;struct llist_node *batch, *pos, *tmp;u64 lost_all =0 ;
@@ -1101,13 +1107,12 @@ void __scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev)/* @cid is valid here: the cpu is active with queued syncs */ cid =__scx_cpu_to_cid (cpu); - shard =rcu_dereference_all (scx_cid_to_shard)[cid]; batch =llist_del_all (&rq->scx.ecaps_to_sync);llist_for_each_safe (pos, tmp, batch) {struct scx_sched_pcpu *pcpu = container_of(pos, struct scx_sched_pcpu, ecaps_to_sync_node); -struct scx_pshard *ps = pcpu->sch->pshard[shard]; +struct scx_pshard *ps =cid_pshard (pcpu->sch, cid);u64 old, ecaps, lost, gained;init_llist_node (pos);
@@ -1229,7 +1234,7 @@ void scx_unbypass_replay_ecaps(struct rq *rq, struct scx_sched *sch)void scx_online_ecaps (struct rq *rq) {struct scx_sched *root, *pos; -s32 cid, shard; +s32 cid;/* * Only a live hierarchy can have ecaps to reseed. This also keeps the
@@ -1244,7 +1249,6 @@ void scx_online_ecaps(struct rq *rq) root =scx_root_protected (); cid =__scx_cpu_to_cid (cpu_of (rq)); - shard =rcu_dereference_all (scx_cid_to_shard)[cid];scx_for_each_descendant_pre (pos, root) {struct scx_pshard *ps;
@@ -1253,7 +1257,7 @@ void scx_online_ecaps(struct rq *rq)if (!pos->level)continue ; - ps = pos->pshard[shard]; + ps =cid_pshard (pos, cid);guard (raw_spinlock)(&ps->lock);queue_sync_ecaps (pos, cid); }
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 2/6 sched_ext: Free a sub-scheduler a grace period after discarding its ecaps syncs
In reply to Tejun HeoThe free path removes a sub-scheduler's ecaps sync node from each cpu's list and then cycles the rq lock until the node reads off-list, as a sync batch can hold the node privately. Cycling the lock is correct only because the sched is bypassing and a bypassing sched's sync calls no op, so the batch touches the pcpu within one rq lock hold.
Tying the free path to what the sync does for a bypassing sched is unusual and brittle, and it gets in the way of changing the sync. Delivering ecaps to a bypassing sched or its parent would break the wait, as a delivery drops the rq lock.
Wait for the batches with RCU instead, the usual way to wait for lockless users before a free. Discard the nodes after the first grace period as before, and free the sched after a second one. The first grace period waits for the paths that found the sched before it was unlinked, such as the sub-cap kfuncs and the cpu hotplug hooks. Until they finish, they can queue its syncs, and a cpu can take a queued node into a sync batch that the first grace period doesn't wait for. After it, nothing queues the syncs, and the discards remove the nodes still queued.
A batch that took a node keeps irqs off from llist_del_all() to its last pcpu write, even across the rq lock drops of a delivery. The second grace period waits for it whether or not it called an op. The exit and disable irq_works, the helper kthread and the bypass load balancing timer are stopped ahead of the per-cpu checks as before, so they move into scx_sched_quiesce_rcu_work() with the checks.
--- kernel/sched/ext/ext.c | 76 ++++++++++++++++++++++++++++++------------ kernel/sched/ext/sub.c | 51 +++++++++++----------------- 2 files changed, 74 insertions(+), 53 deletions(-)
kernel/sched/ext/ext.c +55 -21
@@ -5983,10 +5983,6 @@ static void scx_sched_free_rcu_work(struct work_struct *work)struct scx_dispatch_q *dsq;int cpu, node; -irq_work_sync (&sch->propagate_exit_irq_work); -irq_work_sync (&sch->disable_irq_work); -kthread_destroy_worker (sch->helper); -timer_shutdown_sync (&sch->bypass_lb_timer);free_cpumask_var (sch->bypass_lb_donee_cpumask);free_cpumask_var (sch->bypass_lb_resched_cpumask);free_cpumask_var (sch->stall_cpus);
@@ -6004,22 +6000,6 @@ static void scx_sched_free_rcu_work(struct work_struct *work)for_each_possible_cpu (cpu) {struct scx_sched_pcpu *pcpu =per_cpu_ptr (sch->pcpu, cpu); -/* -* $sch would have entered bypass mode before the RCU grace -* period. As that blocks new deferrals, all -* deferred_reenq_local_node's must be off-list by now. -*/ -WARN_ON_ONCE (!list_empty (&pcpu->deferred_reenq_local.node)); - -/* remove the queued ecaps sync so the pcpu can be freed */ -scx_discard_ecaps_to_sync (cpu, pcpu); - -/* -* Bypass blocks new kicks. Flush the kick irq_work so this -* pcpu's to_kick_node is off the list before it is freed. -*/ -irq_work_sync (&cpu_rq (cpu)->scx.kick_cpus_irq_work); -WARN_ON_ONCE (!list_empty (&pcpu->to_kick_node));free_cpumask_var (pcpu->cpus_to_kick);free_cpumask_var (pcpu->cpus_to_kick_if_idle);free_cpumask_var (pcpu->cpus_to_preempt);
@@ -6061,11 +6041,65 @@ static void scx_sched_free_rcu_work(struct work_struct *work)kfree (sch); } +/** +* scx_sched_quiesce_rcu_work - Quiesce a sched and queue its free +* @work: &scx_sched.rcu_work +* +* $sch is freed after two RCU grace periods because of its ecaps sync nodes. +* +* The first grace period runs between scx_kobj_release() and this function. It +* waits for the paths that found $sch before scx_unlink_sched(), such as the +* sub-cap kfuncs and the cpu hotplug hooks. Until they finish, they can queue +* $sch's syncs, and a cpu can take a queued node into a sync batch. The first +* grace period doesn't wait for a batch that started after it began. +* +* Nothing queues $sch's syncs now. Remove the nodes still on the cpus' lists. A +* batch that took a node can still write $sch's pcpu, and a delivery in the +* batch can drop the rq lock before that write. The batch keeps irqs off from +* taking the node to its last pcpu write, so the second grace period waits for +* it before scx_sched_free_rcu_work() frees $sch. +*/ +static void scx_sched_quiesce_rcu_work (struct work_struct *work) +{ +struct rcu_work *rcu_work =to_rcu_work (work); + struct scx_sched *sch = container_of(rcu_work, struct scx_sched, rcu_work); +s32 cpu; + +irq_work_sync (&sch->propagate_exit_irq_work); +irq_work_sync (&sch->disable_irq_work); +kthread_destroy_worker (sch->helper); +timer_shutdown_sync (&sch->bypass_lb_timer); + +for_each_possible_cpu (cpu) { +struct scx_sched_pcpu *pcpu =per_cpu_ptr (sch->pcpu, cpu); + +/* +* $sch would have entered bypass mode before the first RCU +* grace period. As that blocks new deferrals, all +* deferred_reenq_local_node's must be off-list by now. +*/ +WARN_ON_ONCE (!list_empty (&pcpu->deferred_reenq_local.node)); + +/* remove the queued ecaps sync so the pcpu can be freed */ +scx_discard_ecaps_to_sync (cpu, pcpu); + +/* +* Bypass blocks new kicks. Flush the kick irq_work so this +* pcpu's to_kick_node is off the list before it is freed. +*/ +irq_work_sync (&cpu_rq (cpu)->scx.kick_cpus_irq_work); +WARN_ON_ONCE (!list_empty (&pcpu->to_kick_node)); + } + +INIT_RCU_WORK (&sch->rcu_work, scx_sched_free_rcu_work); +queue_rcu_work (system_dfl_wq, &sch->rcu_work); +} +static void scx_kobj_release (struct kobject *kobj) { struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj); -INIT_RCU_WORK (&sch->rcu_work, scx_sched_free_rcu_work); +INIT_RCU_WORK (&sch->rcu_work, scx_sched_quiesce_rcu_work);queue_rcu_work (system_dfl_wq, &sch->rcu_work); }
kernel/sched/ext/sub.c +19 -32
70 lines not on this page · raw message
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 3/6 sched_ext: Report a sub-scheduler's ecaps on attach and detach
In reply to Tejun HeoA sub-scheduler learns which cids it holds through ops.sub_ecaps_updated(), but the caps granted from ops.sub_attach() reach it only after its enable bypass lifts, when it is already scheduling its tasks.
Report the caps at the end of the enable, before bypass lifts. On disable, report their loss before any task goes back to the parent, so that the parent hears of the loss of every cap it was told of. ops.sub_detach() remains where the parent cleans up after the sub-scheduler. Deliveries no longer depend on bypass, so ops.sub_ecaps_updated() and ops.sub_child_ecaps_updated() also run while a scheduler bypasses. The replay at unbypass goes, and the enable's bypass returns to after ops.sub_attach().
A sub-scheduler's ecaps now change only with the deliveries that report them, so that whenever its ops run under the rq lock, its caps are the ones it was last told of. A sync before the attach report leaves them alone, and the detach report zeroes them as it delivers the loss. A cpu going offline still drops its caps without a report, and those are the only changes not delivered. clear_all_caps() drops that unreported loss on inactive cpus.
A report reschedules only the cpus the sub-scheduler was ever granted a cap on. Rescheduling every cpu would disrupt a large machine split many ways, and which cpus still owe a delivery can't be read off the caps.
The report waits on an rcuwait woken from hard irq work. On PREEMPT_RT, a regular irq_work queued by a cpu going down can be left behind when hotplug parks the irq_work thread, and a hard irq can't take a wait queue's lock.
--- kernel/sched/ext/ext.c | 7 +- kernel/sched/ext/internal.h | 50 ++++++- kernel/sched/ext/sub.c | 253 ++++++++++++++++++++++++++---------- kernel/sched/ext/sub.h | 2 - 4 files changed, 228 insertions(+), 84 deletions(-)
kernel/sched/ext/ext.c +4 -3
@@ -6746,6 +6746,9 @@ static void unbypass_renotify_idle(struct rq *rq, struct scx_sched *pos,* * - ops.dispatch() is ignored. * +* - ops.sub_ecaps_updated() and ops.sub_child_ecaps_updated() are still called. +* The bypassed tasks are on the bypass DSQs, out of their reach. +* * - dispatch_one() does not report %SCX_DSP_PREV on non-zero slice as slice * can't be trusted. Whenever a tick triggers, the running task is rotated to * the tail of the queue.
@@ -6825,10 +6828,8 @@ void scx_bypass(struct scx_sched *sch, bool bypass) pcpu->flags |=SCX_SCHED_PCPU_BYPASSING ; }else { pcpu->flags &= ~SCX_SCHED_PCPU_BYPASSING ; -if (was_bypassing) { +if (was_bypassing)unbypass_renotify_idle (rq, pos, pcpu); -scx_unbypass_replay_ecaps (rq, pos); - } } }
kernel/sched/ext/internal.h +43 -7
87 lines not on this page · raw message
kernel/sched/ext/sub.c +181 -72
403 lines not on this page · raw message
kernel/sched/ext/sub.h +0 -2
@@ -32,7 +32,6 @@ void scx_free_pshards(struct scx_sched *sch);s32 scx_alloc_pshards (struct scx_sched *sch);void scx_init_root_caps (struct scx_sched *sch);void __scx_process_sync_ecaps (struct rq *rq,struct task_struct *prev); -void scx_unbypass_replay_ecaps (struct rq *rq,struct scx_sched *sch);void scx_online_ecaps (struct rq *rq);void scx_offline_ecaps (struct rq *rq);void scx_discard_ecaps_to_sync (s32 cpu,struct scx_sched_pcpu *pcpu);
@@ -110,7 +109,6 @@ static inline void scx_free_pshards(struct scx_sched *sch) {}static inline s32 scx_alloc_pshards (struct scx_sched *sch) {return 0 ; }static inline void scx_init_root_caps (struct scx_sched *sch) {}static inline void scx_process_sync_ecaps (struct rq *rq,struct task_struct *prev) {} -static inline void scx_unbypass_replay_ecaps (struct rq *rq,struct scx_sched *sch) {}static inline void scx_online_ecaps (struct rq *rq) {}static inline void scx_offline_ecaps (struct rq *rq) {}static inline void scx_discard_ecaps_to_sync (s32 cpu,struct scx_sched_pcpu *pcpu) {}
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 4/6 sched_ext: Deliver ecaps only while the root has the cpu online
In reply to Tejun HeoA sub-scheduler's ecaps ops run in dispatch context, and ops.cpu_online() and ops.cpu_offline() document that the cpu doesn't call ops.dispatch() outside them. The reseed of a cpu coming back is queued before the root's ops.cpu_online(), so the ops of a sub-scheduler and its parent can run for a cpu the root still considers offline.
Add SCX_RQ_OPS_ONLINE, set while the root considers the cpu online, from its ops.cpu_online() or its enable until its ops.cpu_offline(). A sync outside that span leaves the ecaps alone, and the reseed, now queued after ops.cpu_online(), reports the net change. SCX_RQ_ONLINE can't be used, as the core changes it ahead of the root's view on both edges.
A cpu going offline still drops its caps without a report.
--- kernel/sched/ext/ext.c | 18 ++++++++---- kernel/sched/ext/internal.h | 6 ++++ kernel/sched/ext/sub.c | 57 ++++++++++++++++++++++++++++++------- kernel/sched/ext/sub.h | 2 ++ kernel/sched/sched.h | 7 +++++ 5 files changed, 74 insertions(+), 16 deletions(-)
kernel/sched/ext/ext.c +13 -5
@@ -4117,11 +4117,6 @@ static void handle_hotplug(struct rq *rq, bool online)if (scx_enabled ())scx_idle_update_selcpu_topology (&sch->ops); -if (online) -scx_online_ecaps (rq); -else -scx_offline_ecaps (rq); -/* * The tables can't be retired while this function is running as the * retirement is inside cpus_read_lock. However, scx_cpu_arg() is
@@ -4150,6 +4145,14 @@ static void handle_hotplug(struct rq *rq, bool online) } } +/* +* ecaps are delivered for @cpu only after ops.cpu_online() and before +* ops.cpu_offline(). The ecaps ops may read online_cmask, so +* scx_offline_ecaps() and scx_online_ecaps() run after its update. +*/ +if (!online) +scx_offline_ecaps (rq); +if (online &&SCX_HAS_OP (sch, cpu_online))SCX_CALL_OP (sch, cpu_online,NULL , cpu_or_cid);else if (!online &&SCX_HAS_OP (sch, cpu_offline))
@@ -4159,6 +4162,9 @@ static void handle_hotplug(struct rq *rq, bool online)SCX_ECODE_ACT_RESTART |SCX_ECODE_RSN_HOTPLUG ,"cpu %d going %s, exiting scheduler" , cpu, online ? "online" : "offline"); + +if (online) +scx_online_ecaps (rq); }void scx_rq_activate (struct rq *rq)
@@ -8293,6 +8299,8 @@ static void scx_root_enable_workfn(struct kthread_work *work) goto err_disable; } +scx_init_rq_ops_online (); +/* * Make the scheduler instance visible. Must be inside cpus_read_lock(). * See handle_hotplug().
kernel/sched/ext/internal.h +6 -0
@@ -926,6 +926,10 @@ struct sched_ext_ops {* Deliveries continue while this sched bypasses. Its tasks are then on * the bypass DSQs, where this op can't insert or move them. Deliveries * stop once this sched is being disabled. +* +* Deliveries for a cid happen only while the root considers the cid's +* cpu online, never before the root's ops.cpu_online() or after its +* ops.cpu_offline() for the cpu. */ void (*sub_ecaps_updated)(s32 cid,u64 before,u64 after);
@@ -979,6 +983,7 @@ struct sched_ext_ops {* * @cpu just came online. @cpu will not call ops.enqueue() or * ops.dispatch(), nor run tasks associated with other CPUs beforehand. +* ops.sub_child_ecaps_updated() runs for @cpu only after this op. */ void (*cpu_online)(s32 cpu);
@@ -988,6 +993,7 @@ struct sched_ext_ops {* * @cpu is going offline. @cpu will not call ops.enqueue() or * ops.dispatch(), nor run tasks associated with other CPUs afterwards. +* ops.sub_child_ecaps_updated() runs for @cpu only before this op. */ void (*cpu_offline)(s32 cpu);
kernel/sched/ext/sub.c +46 -11
102 lines not on this page · raw message
kernel/sched/ext/sub.h +2 -0
@@ -32,6 +32,7 @@ void scx_free_pshards(struct scx_sched *sch);s32 scx_alloc_pshards (struct scx_sched *sch);void scx_init_root_caps (struct scx_sched *sch);void __scx_process_sync_ecaps (struct rq *rq,struct task_struct *prev); +void scx_init_rq_ops_online (void );void scx_online_ecaps (struct rq *rq);void scx_offline_ecaps (struct rq *rq);void scx_discard_ecaps_to_sync (s32 cpu,struct scx_sched_pcpu *pcpu);
@@ -109,6 +110,7 @@ static inline void scx_free_pshards(struct scx_sched *sch) {}static inline s32 scx_alloc_pshards (struct scx_sched *sch) {return 0 ; }static inline void scx_init_root_caps (struct scx_sched *sch) {}static inline void scx_process_sync_ecaps (struct rq *rq,struct task_struct *prev) {} +static inline void scx_init_rq_ops_online (void ) {}static inline void scx_online_ecaps (struct rq *rq) {}static inline void scx_offline_ecaps (struct rq *rq) {}static inline void scx_discard_ecaps_to_sync (s32 cpu,struct scx_sched_pcpu *pcpu) {}
kernel/sched/sched.h +7 -0
@@ -787,6 +787,13 @@ enum scx_rq_flags {*/ SCX_RQ_ONLINE =1 <<0 ,SCX_RQ_CAN_STOP_TICK =1 <<1 , +/* +* Set while the root considers the cpu online, from its +* ops.cpu_online() until its ops.cpu_offline(). ecaps are delivered +* only while it is set. %SCX_RQ_ONLINE follows the core's rq state +* instead, which changes ahead of the root's view on both edges. +*/ +SCX_RQ_OPS_ONLINE =1 <<2 ,SCX_RQ_CLK_VALID =1 <<5 ,/* RQ clock is fresh and valid */ SCX_RQ_BAL_CB_PENDING =1 <<6 ,/* must queue a cb after dispatching */ SCX_RQ_SUB_IDLE_RENOTIFY = 1 << 7, /* sub-scheds are owed update_idle() */
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 5/6 sched_ext: Report the ecaps loss when a cpu goes offline
In reply to Tejun HeoWhen a cpu goes offline, a sub-scheduler's effective caps on its cid are cleared, but neither the sub-scheduler nor its parent is notified. The sub-scheduler keeps treating the cid as usable, and the parent can't act on the loss, e.g. reset the cpuperf target.
Report the loss before the root's ops.cpu_offline() and the regain after its ops.cpu_online(). The zeroing at offline was the last change to a sched's ecaps outside a delivery, so they now change only with one, and syncs on an inactive cpu are processed instead of discarded. Suspend and hibernation now report their cpu downs and ups as well. This is temporary, and a later patch leaves them out.
Two things need care. A cpu going down marks a sync for each sub-scheduler, and its hotplug thread waits until each marked sync has been processed. The cpu's own ext picks process them, and the ext server is started in case a fair task holds the cpu. Only the marked syncs count, so a flood of changes on the cid can't hold up the hotplug.
The detach report reaches only active cpus. It holds off cpu hotplug, so a cpu it skips has reported its loss as it went down, and no unreported loss is left to drop.
Drop the cpuperf reset from scx_qmap's ops.sub_detach(), as its ops.sub_child_ecaps_updated() now resets the targets on the loss reports.
--- kernel/sched/ext/internal.h | 22 ++-- kernel/sched/ext/sub.c | 232 ++++++++++++++++++++------------- kernel/sched/sched.h | 1 + tools/sched_ext/scx_qmap.bpf.c | 25 +--- 4 files changed, 160 insertions(+), 120 deletions(-)
kernel/sched/ext/internal.h +13 -9
@@ -927,9 +927,14 @@ struct sched_ext_ops {* the bypass DSQs, where this op can't insert or move them. Deliveries * stop once this sched is being disabled. * +* Until this sched is being disabled, whenever an op of this sched runs +* under a cid's rq lock, its effective caps on the cid are the @after +* of the last invocation for the cid. +* * Deliveries for a cid happen only while the root considers the cid's * cpu online, never before the root's ops.cpu_online() or after its -* ops.cpu_offline() for the cpu. +* ops.cpu_offline() for the cpu. A cpu going offline reports the loss +* of every cap on it, and a cpu coming back reports the regain. */ void (*sub_ecaps_updated)(s32 cid,u64 before,u64 after);
@@ -960,12 +965,14 @@ struct sched_ext_ops {* change is in effect on the cpu. From here on the parent can act on a * cap the child lost, e.g. reset the cpuperf target or schedule on the * cid. A disabling child gets no ops.sub_ecaps_updated(), so only this -* op is invoked for its loss. A cpu going offline drops caps without a -* report. +* op is invoked for its loss. On a cpu going offline, a parent other +* than the root loses its own caps on the cid too, possibly before this +* op is invoked. If so, it can't act on the cid when this op runs. * * Deliveries start as for the child's ops.sub_ecaps_updated(), continue * while either sched bypasses and stop once this sched is being -* disabled. +* disabled. A cpu going offline or coming back is reported as for the +* child's ops.sub_ecaps_updated(). * * Runs in dispatch context with rq lock held, and can perform all * operations allowed in ops.dispatch() including inserting/moving
@@ -1441,6 +1448,8 @@ enum scx_sched_pcpu_flags {SCX_SCHED_PCPU_ECAPS_ENABLED =1LLU <<1 ,/* a report is pending, see ecaps_report() */ SCX_SCHED_PCPU_ECAPS_REPORT =1LLU <<2 , +/* a cpu going down waits for this sync, see scx_offline_ecaps() */ + SCX_SCHED_PCPU_ECAPS_OFFLINE = 1LLU << 3, };/* dispatch buf */
@@ -1511,11 +1520,6 @@ struct scx_sched_pcpu {* under pshard->lock. */ bool had_caps; -/* -* ecaps as of the last sub_ecaps_updated() and -* sub_child_ecaps_updated() delivery. See __scx_process_sync_ecaps(). -*/ -u64 reported_ecaps;/* * Decaying rescue runtime consumed on this cpu, see
kernel/sched/ext/sub.c +143 -89
401 lines not on this page · raw message
kernel/sched/sched.h +1 -0
@@ -844,6 +844,7 @@ struct scx_rq {/* sched of the open running session, see scx_cid_sched_update() */ struct scx_sched *sched;struct llist_head ecaps_to_sync;/* pending ecaps syncs */ +u32 nr_ecaps_offline;/* marked syncs a down waits for */ struct task_struct *sub_dispatch_prev;#endif cpumask_var_t cpus_to_sync;
tools/sched_ext/scx_qmap.bpf.c +3 -22
@@ -2168,35 +2168,16 @@ s32 BPF_STRUCT_OPS(qmap_sub_attach, struct scx_sub_attach_args *args)void BPF_STRUCT_OPS (qmap_sub_detach ,struct scx_sub_detach_args *args) {u64 cgid = args->ops->sub_cgroup_id; -s32 nr_cids = qa.nr_cids; -s32 pos = qa.part.rr_pos; -u64 holder_cgid; -bool reset_pool; -s32 i, cid; - -if (nr_cids <0 || nr_cids >SCX_QMAP_MAX_CPUS || pos <0 || pos >=MAX_PARTS ) { -scx_bpf_error ("-ERANGE" ); -return ; - } +s32 i;/* -* The child's caps are gone without a report, see ops.sub_detach(). -* cpuperf targets persist until the next write. Reset the child's excl -* cids, and the pool unless another child holds it: that child's revoke -* report or its own detach resets it then. +* qmap_sub_child_ecaps_updated() has reset the cpuperf targets on the +* loss reports. */ - holder_cgid = qa.part.rr_slots[pos]; - reset_pool = !holder_cgid || holder_cgid == cgid; -for (i =0 ; i <MAX_SUB_SCHEDS ; i++) {if (qa.sub_sched_ctxs[i].cgroup_id != cgid)continue ; - bpf_arena_for(cid, 0, nr_cids) -if (cmask_test (cid, &qa.sub_sched_ctxs[i].granted_cids.mask) || - (reset_pool &&cmask_test (cid, &qa.rr_cids.mask))) -scx_bpf_cidperf_set (cid,SCX_CPUPERF_ONE ); - qa.sub_sched_ctxs[i].cgroup_id =0 ; qa.sub_sched_ctxs[i].weight =100 ;cmask_init (&qa.sub_sched_ctxs[i].granted_cids.mask,0 , qa.nr_cids);
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 6/6 sched_ext: Leave suspend and hibernation out of the ecaps reports
In reply to Tejun HeoSuspend and hibernation take down every non-boot cpu and bring them back. A loss and a regain per cpu to every sub-scheduler would be noise. Leave them
out: a cpu they take down reports no loss and keeps the caps last delivered, and the reseed after resume reports only the net change. They are the downs with cpuhp_tasks_frozen set between the PM notifications. A kexec jump sets the flag too but sends no PM notification.
Two things need care. A cpu that suspend or hibernation takes down doesn't wait for its syncs, and a pending ecaps_report() mark on it is processed by its last pick before the stopper takes it down.
The detach report reaches only active cpus, and the loss on a cpu that suspend or hibernation took down was never delivered. The detach waits for those cpus to come back, so every loss is delivered. They come back before the tasks thaw, so the wait doesn't hold up the freezer. If one fails to come back, the hierarchy is aborted at the end of the PM cycle.
--- kernel/sched/ext/ext.c | 15 ++++ kernel/sched/ext/internal.h | 5 +- kernel/sched/ext/sub.c | 143 +++++++++++++++++++++++++++++++++--- kernel/sched/ext/sub.h | 6 ++ 4 files changed, 156 insertions(+), 13 deletions(-)
kernel/sched/ext/ext.c +15 -0
@@ -4105,6 +4105,7 @@ static void handle_hotplug(struct rq *rq, bool online)s32 cpu_or_cid = cpu;atomic_long_inc (&scx_hotplug_seq); +scx_update_frozen_cpus (cpu, online);/* * scx_root updates are protected by cpus_read_lock() and will stay
@@ -9407,6 +9408,20 @@ static int scx_pm_handler(struct notifier_block *nb, unsigned long event, void * {struct scx_sched *sch; +/* track the PM cycle whether or not a root is loaded */ +switch (event) { +case PM_HIBERNATION_PREPARE : +case PM_SUSPEND_PREPARE : +case PM_RESTORE_PREPARE : +scx_sub_pm_begin (); +break ; +case PM_POST_HIBERNATION : +case PM_POST_SUSPEND : +case PM_POST_RESTORE : +scx_sub_pm_end (); +break ; + } +guard (rcu)(); sch =rcu_dereference (scx_root);
kernel/sched/ext/internal.h +4 -1
@@ -934,7 +934,10 @@ struct sched_ext_ops {* Deliveries for a cid happen only while the root considers the cid's * cpu online, never before the root's ops.cpu_online() or after its * ops.cpu_offline() for the cpu. A cpu going offline reports the loss -* of every cap on it, and a cpu coming back reports the regain. +* of every cap on it, and a cpu coming back reports the regain. A cpu +* that suspend or hibernation takes down reports no loss and keeps its +* caps while down. Only its net change, if any, is reported once it is +* back. */ void (*sub_ecaps_updated)(s32 cid,u64 before,u64 after);
kernel/sched/ext/sub.c +131 -12
200 lines not on this page · raw message
kernel/sched/ext/sub.h +6 -0
@@ -32,6 +32,9 @@ void scx_free_pshards(struct scx_sched *sch);s32 scx_alloc_pshards (struct scx_sched *sch);void scx_init_root_caps (struct scx_sched *sch);void __scx_process_sync_ecaps (struct rq *rq,struct task_struct *prev); +void scx_sub_pm_begin (void ); +void scx_update_frozen_cpus (s32 cpu,bool online); +void scx_sub_pm_end (void );void scx_init_rq_ops_online (void );void scx_online_ecaps (struct rq *rq);void scx_offline_ecaps (struct rq *rq);
@@ -110,6 +113,9 @@ static inline void scx_free_pshards(struct scx_sched *sch) {}static inline s32 scx_alloc_pshards (struct scx_sched *sch) {return 0 ; }static inline void scx_init_root_caps (struct scx_sched *sch) {}static inline void scx_process_sync_ecaps (struct rq *rq,struct task_struct *prev) {} +static inline void scx_sub_pm_begin (void ) {} +static inline void scx_update_frozen_cpus (s32 cpu,bool online) {} +static inline void scx_sub_pm_end (void ) {}static inline void scx_init_rq_ops_online (void ) {}static inline void scx_online_ecaps (struct rq *rq) {}static inline void scx_offline_ecaps (struct rq *rq) {}
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 3/6] sched_ext: Report a sub-scheduler's ecaps on attach and detach
In reply to Tejun HeoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] sched_ext: lost reschedule in ecaps_report() causes uninterruptible hang --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] sched_ext: lost reschedule in ecaps_report() causes uninterruptible hang --
commit 5335600717ec4a41d30d235361731881672efc03
Author: Tejun Heo <[email protected]>
sched_ext: Report a sub-scheduler's ecaps on attach and detach
This commit reports a sub-scheduler's effective capabilities synchronously on attach (before bypass lifts) and on detach (before tasks return to the parent) by queuing an ecaps sync on active CPUs that ever held capabilities and waiting on an rcuwait for dispatch to process them.
Quoted 4 lines
kernel/sched/ext/sub.c +0
-0
[ ... ]
Quoted 63 lines
kernel/sched/ext/sub.c +59
-0@@ -1654,6 +1678,76 @@ static inline s32 scx_cgroup_claim_subtree(struct scx_sched *sch) { return 0; }static inline void scx_cgroup_return_subtree (struct scx_sched *sch) {}#endif +/* +* Enable @sch's ecaps deliveries on every possible cpu, so that a cpu inactive +* during the attach report delivers from the reseed at its activation. +*/ +static void ecaps_enable (struct scx_sched *sch) +{ +s32 cpu; + +for_each_possible_cpu (cpu) { +guard (rq_lock_irqsave)(cpu_rq (cpu)); +per_cpu_ptr (sch->pcpu, cpu)->flags |=SCX_SCHED_PCPU_ECAPS_ENABLED ; + } + sch->ecaps_enabled = true; +} + +/** +* ecaps_report - Report @sch's ecaps and wait +* @sch: sub-scheduler to report +* +* Mark every cpu @sch was ever granted a cap on, deliver its pending change +* through the dispatch path and wait for it. An inactive cpu is skipped. A cpu +* going down meanwhile has its mark cleared by scx_offline_ecaps(). +* +* Called with scx_enable_mutex held. A sched's attach and detach reports must +* not overlap, as the mark is one bit per pcpu. +*/ +static void ecaps_report (struct scx_sched *sch) +{ +s32 cpu; + +lockdep_assert_held (&scx_enable_mutex); + +for_each_possible_cpu (cpu) { +struct scx_sched_pcpu *pcpu =per_cpu_ptr (sch->pcpu, cpu); +struct rq *rq =cpu_rq (cpu); +struct scx_pshard *ps; +s32 cid; + +guard (rq_lock_irqsave)(rq); + +/* syncs on an inactive cpu are discarded */ +if (!cpu_active (cpu)) +continue ; + + cid =__scx_cpu_to_cid (cpu); + ps =cid_pshard (sch, cid); +guard (raw_spinlock)(&ps->lock); + +if (!pcpu->had_caps) +continue ; + + pcpu->flags |=SCX_SCHED_PCPU_ECAPS_REPORT ; +atomic_inc (&sch->nr_ecaps_reports); +queue_sync_ecaps (sch, cid); + +/* +* The report needs an ext dispatch on this cpu. A kick is +* dropped while the root bypasses, e.g. for PM, so reschedule +* directly. A fair task can hold the cpu, so also start the ext
kernel/sched/ext/sub.c static inline s32 scx_cgroup_claim_subtree(struct scx_sched *sch) { return 0; } · line 1749
Quoted 10 lines
kernel/sched/ext/sub.c +10
-0+* server, which picks the ext class within its period. +*/ +update_rq_clock (rq); +dl_server_start (&rq->ext_server); +resched_curr (rq); + } + +rcuwait_wait_event (&ecaps_report_wait, !atomic_read (&sch->nr_ecaps_reports), +TASK_UNINTERRUPTIBLE ); +}
[Severity: High]
Can the reschedule issued by ecaps_report() be lost if the target CPU is already inside dispatch_one() and temporarily drops rq->lock, causing ecaps_report() to hang in rcuwait_wait_event() while holding scx_enable_mutex (and cpus_read_lock() in scx_sub_disable())?
On the target CPU, dispatch_one() drains rq->scx.ecaps_to_sync once at entry:
kernel/sched/ext/ext.c:dispatch_one() {
lockdep_assert_rq_held(rq);
rq->scx.flags |= SCX_RQ_IN_DISPATCH;
scx_process_sync_ecaps(rq, prev);
...
verdict = scx_dispatch_sched(root_sch, rq, prev, false);
}After __scx_process_sync_ecaps() calls llist_del_all(&rq->scx.ecaps_to_sync), rq->lock can be dropped and re-acquired via switch_rq_lock() during scx_flush_dispatch_buf() -> finish_dispatch() -> dispatch_to_local_dsq() or during scx_dispatch_sched() (for example in consume_remote_task(), dispatch_to_local_dsq(), or scx_dsq_move()).
If ecaps_report() acquires rq->lock during that window, it queues pcpu->ecaps_to_sync_node onto rq->scx.ecaps_to_sync and calls dl_server_start(&rq->ext_server) and resched_curr(rq). Because the target CPU is still in pick_next_task(), rq->curr is still prev. When dispatch_one() finishes without re-checking rq->scx.ecaps_to_sync and pick_next_task() returns to __schedule(), __schedule() clears the reschedule request:
kernel/sched/core.c:__schedule() {
picked:
clear_tsk_need_resched(prev); clear_preempt_need_resched(); }
Similarly, under CONFIG_SCHED_CORE, if an SMT sibling's core-wide pick in pick_next_task() already set rq->core_pick and rq->core->core_pick_seq == rq->core->core_task_seq before ecaps_report() runs on rq, ecaps_report() does not advance core_task_seq or clear rq->core_pick, so pick_next_task() can take the core_pick fastpath without calling dispatch_one() and then clear TIF_NEED_RESCHED at picked:.
In either case, starting rq->ext_server does not guarantee another dispatch_one() call: if the selected next task is rq->idle, dl_server_update_idle() sets dl_defer_idle and dl_server_timer() stops ext_server without calling resched_curr(rq); if the selected next task is an ext_sched_class task with SCX_SLICE_INF, task_tick_scx() does not reschedule it and update_curr_scx() -> dl_server_update() keeps pushing ext_server's timer forward. Could that leave sch->nr_ecaps_reports non-zero indefinitely?
([ ... ])
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH 5/6] sched_ext: Report the ecaps loss when a cpu goes offline
In reply to Tejun HeoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] sched_ext: deadlock in scx_offline_ecaps() when scx_root_disable() races hotplug --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] sched_ext: deadlock in scx_offline_ecaps() when scx_root_disable() races hotplug --
commit 7354181ddf2a55286ce8abbd0374e5b9d7fdb269
Author: Tejun Heo <[email protected]>
sched_ext: Report the ecaps loss when a cpu goes offline
This patch reports the loss of effective caps on a cid to the sub-scheduler and its parent before the root's ops.cpu_offline() runs, and reports the regain after ops.cpu_online(). To ensure the loss is reported before the CPU goes offline, scx_offline_ecaps() marks a sync for each sub-scheduler, starts the ext server, and waits for all marked syncs to be processed.
Quoted 4 lines
kernel/sched/ext/sub.c +0
-0
[ ... ]
Quoted 1 line
kernel/sched/ext/sub.c +0
-0@@ -1298,40 +1332,69 @@ void scx_online_ecaps(struct rq *rq)
[ ... ]
Quoted 46 lines
void
scx_offline_ecaps(struct rq *rq)
{kernel/sched/ext/sub.c +30
-7s32 cpu =cpu_of (rq);struct scx_sched *root, *pos; +u32 nr =0 ; +s32 cid; -guard (rq_lock_irqsave)(rq); +/* +* ecaps and the tables need a live root, see scx_online_ecaps(). The +* ext class isn't picked without one, so the syncs wouldn't be +* processed and the wait would never end. scx_enabled() can't change +* during hotplug. +*/ +if (scx_enabled ()) { + scoped_guard (rq_lock_irqsave, rq) { + root =scx_root_protected (); + cid =__scx_cpu_to_cid (cpu); - root =scx_root_protected (); +scx_for_each_descendant_pre (pos, root) { +struct scx_sched_pcpu *pcpu =per_cpu_ptr (pos->pcpu, cpu); +struct scx_pshard *ps; -scx_for_each_descendant_pre (pos, root) { -struct scx_sched_pcpu *pcpu =per_cpu_ptr (pos->pcpu, cpu); +/* root holds every cap and never uses ecaps */ +if (!pos->level) +continue ; -/* root holds every cap and never uses ecaps */ -if (!pos->level) -continue ; +/* +* A batch that has yet to reach the sync sees +* the mark. A sync already being processed has +* left the list and is queued again below. +*/ + pcpu->flags |=SCX_SCHED_PCPU_ECAPS_OFFLINE ; + nr++; + + ps =cid_pshard (pos, cid); +guard (raw_spinlock)(&ps->lock); +queue_sync_ecaps (pos, cid); + }
kernel/sched/ext/sub.c void scx_online_ecaps(struct rq *rq) · line 1398
Quoted 10 lines
kernel/sched/ext/sub.c +4
-4-WRITE_ONCE (pcpu->ecaps,0 ); +WRITE_ONCE (rq->scx.nr_ecaps_offline, nr); +resched_sync_ecaps (rq); + } -/* deliveries have stopped on this cpu, clear its mark */ -if (pcpu->flags &SCX_SCHED_PCPU_ECAPS_REPORT ) -ecaps_report_done (pcpu); +wait_event (ecaps_offline_waitq, !READ_ONCE (rq->scx.nr_ecaps_offline)); }
[Severity: High]
Can scx_offline_ecaps() deadlock here if a CPU hotplug offline operation races with scx_sub_disable() and scx_root_disable()?
The comment notes that scx_enabled() cannot change during hotplug, and that without a live root the ext class will not be picked and the wait would never end. However, static_branch_disable(&__scx_enabled) in scx_root_disable() runs before cpus_read_lock() is acquired.
In scx_sub_disable(), cpus_read_lock() is only held around clear_all_caps() and ecaps_report(), and is dropped while sch remains linked on parent->children until scx_unlink_sched(sch):
kernel/sched/ext/sub.c:scx_sub_disable() {
scoped_guard(cpus_read_lock) {
clear_all_caps(sch);
if (sch->ecaps_enabled)
ecaps_report(sch);
}
percpu_down_write(&scx_fork_rwsem);
scx_cgroup_lock();
...
scx_unlink_sched(sch);
}If the last sub-scheduler has passed that cpus_read_lock() block in scx_sub_disable() when a CPU begins going offline (holding cpus_write_lock), scx_offline_ecaps() sees scx_enabled() == true and visits sch via scx_for_each_descendant_pre(pos, root). It sets SCX_SCHED_PCPU_ECAPS_OFFLINE, sets rq->scx.nr_ecaps_offline = 1, starts rq->ext_server (which enters its dl_defer zero-laxity wait if a fair task is running), and sleeps in wait_event(ecaps_offline_waitq, ...).
Meanwhile, scx_sub_disable() does not take cpus_read_lock() again. It finishes scx_unlink_sched(sch) and wakes scx_unlink_waitq. If the root scheduler is also disabling, scx_root_disable() wakes from drain_descendants(), detaches rq->ext_server, and disables __scx_enabled before blocking on cpus_read_lock():
kernel/sched/ext/ext.c:scx_root_disable() {
drain_descendants();
...
for_each_possible_cpu(cpu) {
struct rq *rq = cpu_rq(cpu);
scoped_guard(rq_lock_irqsave, rq) {
update_rq_clock(rq);
if (was_switched_all) {
if (WARN_ON_ONCE(dl_server_swap_bw(&rq->ext_server,
&rq->fair_server)))
pr_warn("failed to re-attach fair_server on CPU %d\n", cpu);
} else {
dl_server_detach_bw(&rq->ext_server);
}
}
}
scx_ops_cid_sched_updated_disable(sch);
/* no task is on scx, turn off all the switches and flush in-progress calls */
static_branch_disable(&__scx_enabled);
...
scoped_guard(cpus_read_lock) {
...
}
}If the offline CPU had not yet picked ext_sched_class before __scx_enabled was disabled and ext_server was detached (for instance, while a fair or RT task was running during ext_server's dl_defer window), __scx_process_sync_ecaps() will never be called on that rq. Furthermore, scx_discard_ecaps_to_sync() is only invoked from scx_sched_quiesce_rcu_work() after scx_kobj_release() when the sub-scheduler's bpf_link is closed, so while the sub-scheduler's bpf_link remains open (or if userspace is blocked in bpf_scx_unreg(root) -> scx_flush_disable_work(root)), wouldn't the hotplug thread and scx_root_disable() wait on each other forever?
Quoted 4 lines
kernel/sched/ext/sub.c +1
-0+guard (rq_lock_irqsave)(rq); rq->scx.flags &= ~SCX_RQ_OPS_ONLINE ; }
[ ... ]
Quoted 13 lines
kernel/sched/ext/sub.c +3
-3@@ -1808,9 +1862,9 @@ void scx_sub_disable(struct scx_sched *sch)/* * Clear @sch's caps and report the loss before any task goes back to * the parent. A disabling sched is granted nothing, so the caps stay -* cleared. Hold off cpu hotplug across both. The report covers every -* active cpu and the clear drops the unreported loss on the rest, so -* that no loss reaches the parent after ops.sub_detach(). +* cleared. Hold off cpu hotplug across both so that a cpu the report +* skips has finished going down. Its loss was reported as it went down, +* see scx_offline_ecaps(). * * An enable that failed before its attach report has nothing to report. * If it failed before scx_link_sched(), the root's disable doesn't wait
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Tejun Heo
reviewerRe: [PATCH 5/6] sched_ext: Report the ecaps loss when a cpu goes offline
Hello,
Quoted 4 lines · On Sat, Oct 17, 2026 at 06:14:02PM +0000, [email protected] wrote:
The comment notes that
scx_enabled()cannot change during hotplug, and that without a live root the ext class will not be picked and the wait would never end. However,static_branch_disable(&__scx_enabled)inscx_root_disable()runs beforecpus_read_lock()is acquired.
static_branch_disable() takes cpus_read_lock() itself in static_key_disable(), so the flip waits until the cpu down releases cpus_write_lock(). scx_enabled() stays true across the down.
Quoted 4 lines
If the offline CPU had not yet picked
ext_sched_classbefore__scx_enabledwas disabled andext_serverwas detached (for instance, while a fair or RT task was running during ext_server'sdl_deferwindow),__scx_process_sync_ecaps()will never be called on that rq.
The drain doesn't depend on the ext server. scx_offline_ecaps() runs from sched_cpu_deactivate() after balance_push_set(), so once the hotplug thread sleeps in the wait, balance_push() moves every task but per-cpu kthreads and migration-disabled ones off the cpu. With scx_enabled() still true, the cpu's first pick with no fair task left runs the ext pick, whose dispatch_one() processes the marked sync.
Only a per-cpu kthread or a migration-disabled task that never sleeps could hold it off after the ext server is detached, and that would be a bug of its own.
Thanks.
--
tejun