sched_ext: Report a sub-scheduler's ecaps on attach, detach and cpu hotplug
Subsystems (2)
Tejun Heo
reviewerPATCH v3 1/1 sched_ext: Report a sub-scheduler's ecaps on attach, detach and cpu hotplug
Hello,
v3 (resched from irq work):
- Reschedule the cpu for its queued ecaps syncs from hard irq work on that cpu instead of a kick. dispatch_one() processes the syncs only at its start, and a kick that lands while the pick has the rq lock dropped is cleared with the pick (#3, #5, sashiko).
- Fix the SCX_RQ_ONLINE comments (#4) and why no report mark is pending at a suspend down (#6), and update comments and descriptions.
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).
v2: https://lore.kernel.org/r/[email protected]
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 (9c0af54cb49c).
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-v3
diffstat follows. Thanks.
kernel/sched/ext/ext.c | 157 ++++++++--- kernel/sched/ext/internal.h | 71 ++++- kernel/sched/ext/sub.c | 625 +++++++++++++++++++++++++++++++---------- kernel/sched/ext/sub.h | 10 +- kernel/sched/sched.h | 17 +- tools/sched_ext/scx_qmap.bpf.c | 25 +- 6 files changed, 683 insertions(+), 222 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
@@ -6013,10 +6013,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);
@@ -6034,22 +6030,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);
@@ -6091,11 +6071,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 on an active cpu. 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.
The following things need care. 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 and a queued sync reschedule the cpu from hard irq work on that cpu, not with a kick or a direct resched. dispatch_one() processes the syncs only at its start, and a kick only guarantees that a scheduling event completes after it, which can be the pick in progress. That pick has already processed the syncs and clears the resched, and a cpu that then idles or runs a task with an infinite slice leaves the report waiting. A pick runs with IRQs off, so the irq work runs after any pick in progress on that cpu.
Under core scheduling, a sibling's committed pick can switch the cpu without dispatch_one(), and a sync queued after that pick is processed only at the cpu's next scheduling event. A cpu running a task of a higher class isn't rescheduled, as its pick would return that task again. While the root bypasses, a queued sync doesn't reschedule the cpu, as a grant or revoke can run in the PM syscore phase, where irq work can malfunction. The report never runs there and reschedules the cpu itself.
The report waits on an rcuwait woken from hard irq work. Under core scheduling, a cpu going down can process a sibling's marked sync after hotplug has parked its irq_work thread. On PREEMPT_RT, a regular irq_work queued there would be left behind, and a hard irq can't take a wait queue's lock.
v2: Reschedule from hard irq work on the cpu instead of a kick (sashiko).
--- kernel/sched/ext/ext.c | 38 ++++- kernel/sched/ext/internal.h | 52 ++++++- kernel/sched/ext/sub.c | 272 ++++++++++++++++++++++++++---------- kernel/sched/ext/sub.h | 2 - kernel/sched/sched.h | 1 + 5 files changed, 277 insertions(+), 88 deletions(-)
kernel/sched/ext/ext.c +35 -3
@@ -1150,6 +1150,36 @@ static void schedule_deferred_locked(struct rq *rq)schedule_deferred (rq); } +static void resched_irq_workfn (struct irq_work *irq_work) +{ + struct rq *rq = container_of(irq_work, struct rq, scx.resched_irq_work); + +guard (rq_lock)(rq); +/* a higher class task would only be picked again, see kick_one_cpu() */ +if (!sched_class_above (rq->donor->sched_class, &ext_sched_class)) +resched_curr (rq); +scx_rq_lock_drop (rq); +} + +/** +* scx_resched_curr_deferred - Reschedule @rq's cpu from hard irq work on it +* @rq: rq to reschedule +* +* A kick, like a direct resched, only guarantees that a scheduling event on the +* cpu completes after it, and that can be the one in progress. Work that a pick +* processes only at the start of its dispatch, such as the ecaps syncs, can be +* queued after that point and miss the pick. A pick on @rq's cpu runs with IRQs +* off, so the irq work runs after it, and the scheduling event its resched +* triggers starts after this call. A sibling's core scheduling pick for @rq +* isn't covered. A cpu running a task of a higher class isn't rescheduled, as +* its pick would return that task again. @rq's cpu must stay online until the +* irq work is queued. +*/ +void scx_resched_curr_deferred (struct rq *rq) +{ +irq_work_queue_on (&rq->scx.resched_irq_work,cpu_of (rq)); +} +#ifdef CONFIG_NO_HZ_FULL static void scx_proxy_tick_bal_cb (struct rq *rq) {
@@ -6776,6 +6806,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.
@@ -6855,10 +6888,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); - } } }
@@ -9495,6 +9526,7 @@ void __init init_sched_ext_class(void)INIT_LIST_HEAD (&rq->scx.deferred_reenq_users); rq->scx.deferred_irq_work =IRQ_WORK_INIT_HARD (deferred_irq_workfn); rq->scx.kick_cpus_irq_work =IRQ_WORK_INIT_HARD (kick_cpus_irq_workfn); + rq->scx.resched_irq_work =IRQ_WORK_INIT_HARD (resched_irq_workfn);if (cpu_online (cpu))cpu_rq (cpu)->scx.flags |=SCX_RQ_ONLINE ;
kernel/sched/ext/internal.h +45 -7
95 lines not on this page · raw message
kernel/sched/ext/sub.c +196 -76
434 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) {}
kernel/sched/sched.h +1 -0
@@ -855,6 +855,7 @@ struct scx_rq {#endif struct irq_work deferred_irq_work;struct irq_work kick_cpus_irq_work; + struct irq_work resched_irq_work; };#endif /* CONFIG_SCHED_CLASS_EXT */
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.
While at it, fix the SCX_RQ_ONLINE comments, which claimed that the flag tracks ops.cpu_online() and ops.cpu_offline().
v2: Fix the SCX_RQ_ONLINE comments.
--- kernel/sched/ext/ext.c | 28 +++++++++++------- kernel/sched/ext/internal.h | 6 ++++ kernel/sched/ext/sub.c | 57 ++++++++++++++++++++++++++++++------- kernel/sched/ext/sub.h | 2 ++ kernel/sched/sched.h | 15 ++++++++-- 5 files changed, 84 insertions(+), 24 deletions(-)
kernel/sched/ext/ext.c +18 -10
@@ -2173,11 +2173,11 @@ static void direct_dispatch(struct scx_sched *sch, struct task_struct *p,bool scx_rq_online (struct rq *rq) {/* -* Test both cpu_active() and %SCX_RQ_ONLINE. %SCX_RQ_ONLINE indicates -* the online state as seen from the BPF scheduler. cpu_active() test -* guarantees that, if this function returns %true, %SCX_RQ_ONLINE will -* stay set until the current scheduling operation is complete even if -* we aren't locking @rq. +* Test both cpu_active() and %SCX_RQ_ONLINE. %SCX_RQ_ONLINE follows the +* core's rq online state. cpu_active() test guarantees that, if this +* function returns %true, %SCX_RQ_ONLINE will stay set until the +* current scheduling operation is complete even if we aren't locking +* @rq. */ return likely ((rq->scx.flags &SCX_RQ_ONLINE ) &&cpu_active (cpu_of (rq))); }
@@ -4177,11 +4177,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
@@ -4210,6 +4205,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))
@@ -4219,6 +4222,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)
@@ -8353,6 +8359,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
@@ -927,6 +927,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);
@@ -980,6 +984,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);
@@ -989,6 +994,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 +12 -3
@@ -781,12 +781,21 @@ struct cfs_rq {/* scx_rq->flags, protected by the rq lock */ enum scx_rq_flags {/* -* A hotplugged CPU starts scheduling before rq_online_scx(). Track -* ops.cpu_on/offline() state so that ops.enqueue/dispatch() are called -* only while the BPF scheduler considers the CPU to be online. +* Set while the core has the rq online, see rq_online_scx() and +* rq_offline_scx(). A hotplugged cpu starts scheduling before +* rq_online_scx(). ops.enqueue() and ops.dispatch() are called for the +* cpu only while it is set, see scx_rq_online() and +* scx_dispatch_sched(). */ 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. Syncs on an inactive cpu are processed instead of discarded, and the reseed reschedules the cpu itself, as they got no resched when queued. 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.
v2: Reschedule from the irq work and document the regain order.
--- kernel/sched/ext/internal.h | 34 +++-- kernel/sched/ext/sub.c | 245 ++++++++++++++++++++------------- kernel/sched/sched.h | 1 + tools/sched_ext/scx_qmap.bpf.c | 25 +--- 4 files changed, 173 insertions(+), 132 deletions(-)
kernel/sched/ext/internal.h +19 -15
@@ -880,12 +880,11 @@ struct sched_ext_ops {* The sub-scheduler holds no caps by this point and can no longer * affect any cid. Every cap reported through * ops.sub_child_ecaps_updated() was also reported lost there before the -* sub-scheduler's tasks came back to this sched, except on a cpu that -* went offline. Nothing is reported for a sub-scheduler whose enable -* failed before its tasks were handed over. If this sched is being -* disabled too, it can miss these reports. On a cpu whose loss wasn't -* reported, whatever was delegated, e.g. cpuperf targets, is the -* parent's to restore here. +* sub-scheduler's tasks came back to this sched. Nothing is reported +* for a sub-scheduler whose enable failed before its tasks were handed +* over. If this sched is being disabled too, it can miss these reports. +* On a cpu whose loss wasn't reported, whatever was delegated, e.g. +* cpuperf targets, is the parent's to restore here. */ void (*sub_detach)(struct scx_sub_detach_args *args);
@@ -928,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);
@@ -961,12 +965,15 @@ 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. A parent other than the root loses its +* own caps on the cid too when the cpu goes offline, possibly before +* this op is invoked, and regains them when the cpu comes back, +* possibly after. It can't act on the cid while it holds no caps there. * * 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
@@ -1442,6 +1449,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 */
@@ -1512,11 +1521,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 +150 -95
418 lines not on this page · raw message
kernel/sched/sched.h +1 -0
@@ -846,6 +846,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. Their downs are the ones 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 no ecaps_report() mark can be pending on it then. The attach report runs while the attaching task waits uninterruptibly, which holds off the freezer, and the detach report holds cpus_read_lock().
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.
v2: Fix why no report mark is pending at a PM down, add a data_race().
--- kernel/sched/ext/ext.c | 15 ++++ kernel/sched/ext/internal.h | 5 +- kernel/sched/ext/sub.c | 148 ++++++++++++++++++++++++++++++++---- kernel/sched/ext/sub.h | 6 ++ 4 files changed, 160 insertions(+), 14 deletions(-)
kernel/sched/ext/ext.c +15 -0
@@ -4165,6 +4165,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
@@ -9467,6 +9468,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 +135 -13
204 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
Andrea Righi
reviewerRe: [PATCH 5/6] sched_ext: Report the ecaps loss when a cpu goes offline
In reply to Tejun HeoHi Tejun,
Quoted 30 lines · Tejun Heo
When 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 itsops.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. Syncs on an inactive cpu are processed instead of discarded, and the reseed reschedules the cpu itself, as they got no resched when queued. 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 itsops.sub_child_ecaps_updated()now resets the targets on the loss reports.v2: Reschedule from the irq work and document the regain order.
Signed-off-by Tejun Heo <[email protected]>---
...
kernel/sched/sched.h struct scx_rq { · line 852
Quoted 12 lines
kernel/sched/sched.h +1
-0@@ -846,6 +846,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;
nit: nr_ecaps_offline lands between two 8-byte aligned fields, which adds a 4-byte hole and shifts everything after it (kick_sync included) by 8 bytes. Can we move it next to kick_sync_pending instead? Something like:
#ifdef CONFIG_EXT_SUB_SCHED /* sched of the open running session, see scx_cid_sched_update() */ struct scx_sched *sched; struct llist_head ecaps_to_sync; /* pending ecaps syncs */ struct task_struct *sub_dispatch_prev; u32 nr_ecaps_offline; /* marked syncs a down waits for */ #endif bool kick_sync_pending; cpumask_var_t cpus_to_sync; unsigned long kick_sync;
This way the u32 and the bool share the existing hole, and kick_sync and the following fields keep their current offsets.
Thanks,
-Andrea
Andrea Righi
reviewerRe: [PATCH 2/6] sched_ext: Free a sub-scheduler a grace period after discarding its ecaps syncs
In reply to Tejun HeoQuoted 29 lines · Tejun Heo
The 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 disableirq_works, the helper kthread and the bypass load balancing timer are stopped ahead of the per-cpu checks as before, so they move intoscx_sched_quiesce_rcu_work()with the checks.Signed-off-by Tejun Heo <[email protected]>---
...
Quoted 18 lines
kernel/sched/ext/sub.c +4
-2@@ -1318,8 +1318,10 @@ static void clear_all_caps(struct scx_sched *sch) }/* -* @pcpu's sched was unhashed before the grace period, so nothing re-queues its -* sync node. Remove the node from @rq's pending list so the pcpu can be freed. +* Remove @pcpu's ecaps sync node from @cpu's pending list. The first grace +* period has passed, so nothing queues the node again. The second grace period +* waits for a sync batch that took the node earlier, see +* scx_sched_quiesce_rcu_work(). */ void scx_discard_ecaps_to_sync (s32 cpu,struct scx_sched_pcpu *pcpu) {
kernel/sched/ext/sub.c void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu) · line 1336
Quoted 8 lines
kernel/sched/ext/sub.c +5
-0@@ -1327,43 +1329,28 @@ void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu)struct llist_node *head =NULL , *tail =NULL ;struct llist_node *pos, *tmp; +guard (rq_lock_irqsave)(rq); + +if (!llist_on_list (&pcpu->ecaps_to_sync_node)) +return ; +
This is called for every possible CPU from the free path of every sched, including a root without sub-scheds, whose nodes are never queued. With the lock moved ahead of the test, every scheduler unload now takes all the rq locks with irqs off, which on a large machine can be a lot of lock traffic.
The first grace period has passed by now, so nothing queues the node again, and a node taken by an in-flight batch still reads as on-list. So, can we keep the lockless test in front?
if (!llist_on_list(&pcpu->ecaps_to_sync_node)) return; guard(rq_lock_irqsave)(rq);
And the WARN_ON_ONCE() on SCX_SCHED_PCPU_ECAPS_REPORT added in 3/6 could go above the lockless test.
Thanks,
-Andrea
Quoted 53 lines
kernel/sched/ext/sub.c +10
-30/* * llist can't unlink a single node. Take all queued nodes, drop @pcpu's * and resplice the rest. Nodes in the taken batch read as on-list * throughout, so queue_sync_ecaps() stays correct. */ -if (llist_on_list (&pcpu->ecaps_to_sync_node)) { - scoped_guard (rq_lock_irqsave, rq) { -llist_for_each_safe (pos, tmp,llist_del_all (&rq->scx.ecaps_to_sync)) { -if (pos == &pcpu->ecaps_to_sync_node) { -init_llist_node (pos); - }else { - pos->next = head; - head = pos; -if (!tail) - tail = pos; - } - } -if (head) -llist_add_batch (head, tail, &rq->scx.ecaps_to_sync); - } - } - -/* -* An in-flight scx_process_sync_ecaps() batch may still hold the node -* privately across dispatch-induced rq unlocks, reading as on-list. -* -* Because a bypassing sched gets no op call, init_llist_node() and all -* @pcpu accesses share one contiguous lock hold, off-list under the rq -* lock means @pcpu won't be accessed again. -*/ -while (true) { - scoped_guard (rq_lock_irqsave, rq) { -if (!llist_on_list (&pcpu->ecaps_to_sync_node)) -return ; +llist_for_each_safe (pos, tmp,llist_del_all (&rq->scx.ecaps_to_sync)) { +if (pos == &pcpu->ecaps_to_sync_node) { +init_llist_node (pos); + }else { + pos->next = head; + head = pos; +if (!tail) + tail = pos; } -cpu_relax (); } +if (head) +llist_add_batch (head, tail, &rq->scx.ecaps_to_sync); }/** Signature · 1 line
2.56.0
Andrea Righi
reviewerRe: [PATCH 5/6] sched_ext: Report the ecaps loss when a cpu goes offline
In reply to Tejun HeoQuoted 29 lines · Tejun Heo
When 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 itsops.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. Syncs on an inactive cpu are processed instead of discarded, and the reseed reschedules the cpu itself, as they got no resched when queued. 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 itsops.sub_child_ecaps_updated()now resets the targets on the loss reports.v2: Reschedule from the irq work and document the regain order.
Signed-off-by Tejun Heo <[email protected]>
...
kernel/sched/ext/sub.c void scx_online_ecaps(struct rq *rq) · line 1352
Quoted 7 lines
kernel/sched/ext/sub.c +2
-0@@ -1313,40 +1347,69 @@ void scx_online_ecaps(struct rq *rq)guard (raw_spinlock)(&ps->lock);queue_sync_ecaps (pos, cid); } + +resched_sync_ecaps (rq); }
With a root-only scheduler the walk skips the root and queues nothing, but resched_sync_ecaps() still runs on every online CPU, starting the ext_server and queueing the resched irq work for no reason. Can we do it only if something was actually queued?
Something like:
bool queued = false; ... queue_sync_ecaps(pos, cid); queued = true; } if (queued) resched_sync_ecaps(rq);
Quoted 66 lines
kernel/sched/ext/sub.c +39
-15/* -* A cpu is going down, and the root's ops.cpu_offline() follows. Stop the cpu's -* ecaps deliveries and zero each sub-sched's in-effect ecaps so cap checks -* treat the cpu as capless while offline. Pending and late-queued syncs are -* discarded at consumption by scx_process_sync_ecaps() while the cpu is -* inactive. Leave reported_ecaps. Ownership is unchanged, so the -* scx_online_ecaps() reseed reports only a genuine delta. No callback fires -* here. +* A cpu is going down, and the root's ops.cpu_offline() follows. Queue a marked +* sync for each sub-sched to report the loss and wait until each has been +* processed. Then stop the cpu's ecaps deliveries, so that none comes after +* ops.cpu_offline(). +* +* This runs on the cpu's hotplug thread. While it sleeps, the cpu keeps +* scheduling, and a pick through the ext class processes the syncs, see +* resched_sync_ecaps(). Only the marked syncs count, so a flood of changes on +* the cid can't hold up the hotplug. After the wait, every sub-sched's ecaps on +* the cpu are zero, and the later syncs have nothing to deliver. */ void scx_offline_ecaps (struct rq *rq) {s32 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++; -WRITE_ONCE (pcpu->ecaps,0 ); + 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 1410
Quoted 10 lines
kernel/sched/ext/sub.c +5
-3-/* deliveries have stopped on this cpu, clear its mark */ -if (pcpu->flags &SCX_SCHED_PCPU_ECAPS_REPORT ) -ecaps_report_done (pcpu); +WRITE_ONCE (rq->scx.nr_ecaps_offline, nr); +resched_sync_ecaps (rq); + } + +wait_event (ecaps_offline_waitq, !READ_ONCE (rq->scx.nr_ecaps_offline)); }
ame on the offline side: with no sub-sched nr is 0, but we still do update_rq_clock(), dl_server_start(&rq->ext_server) and the irq work on a CPU that's going down and the ext_server stays enqueued there until sched_cpu_dying() stops it. How about:
WRITE_ONCE(rq->scx.nr_ecaps_offline, nr); if (nr) resched_sync_ecaps(rq);
The wait_event() returns right away when nr is 0, so nothing else changes for root-only schedulers.
Thanks,
-Andrea
Tejun Heo
reviewerRe: [PATCH 5/6] sched_ext: Report the ecaps loss when a cpu goes offline
In reply to Andrea RighiHello, Andrea.
Quoted 3 lines · Andrea Righi
nit:
nr_ecaps_offlinelands between two 8-byte aligned fields, which adds a 4-byte hole and shifts everything after it (kick_syncincluded) by 8 bytes. Can we move it next tokick_sync_pendinginstead? Something like:
Will do.
Thanks.
--
tejun
Tejun Heo
reviewerRe: [PATCH 5/6] sched_ext: Report the ecaps loss when a cpu goes offline
In reply to Andrea RighiHello, Andrea.
Quoted 4 lines · Andrea Righi
With a root-only scheduler the walk skips the root and queues nothing, but
resched_sync_ecaps()still runs on every online CPU, starting theext_serverand queueing the resched irq work for no reason. Can we do it only if something was actually queued?
...
Quoted 4 lines
ame on the offline side: with no sub-sched nr is 0, but we still do
update_rq_clock(),dl_server_start(&rq->ext_server)and the irq work on a CPU that's going down and theext_serverstays enqueued there untilsched_cpu_dying()stops it. How about:
These are on the cpu hotplug path, which is really cold, and the cost is a resched on one cpu. It's minuscule and doesn't matter, so I don't think it makes sense to add any complexity to avoid it.
Thanks.
--
tejun
Tejun Heo
reviewerRe: [PATCH 2/6] sched_ext: Free a sub-scheduler a grace period after discarding its ecaps syncs
In reply to Andrea RighiHello, Andrea.
Quoted 4 lines · Andrea Righi
This is called for every possible CPU from the free path of every sched, including a root without sub-scheds, whose nodes are never queued. With the lock moved ahead of the test, every scheduler unload now takes all the rq locks with irqs off, which on a large machine can be a lot of lock traffic.
The loop this replaces also took each rq lock once, and attach and detach already cycle through all the rq locks several times, e.g. in scx_bypass() and ecaps_report(). Cycling through the rq locks isn't that expensive.
Quoted 3 lines
The first grace period has passed by now, so nothing queues the node again, and a node taken by an in-flight batch still reads as on-list. So, can we keep the lockless test in front?
A node taken by an in-flight batch doesn't read as on-list. __scx_process_sync_ecaps() calls init_llist_node() on each node before it writes the pcpu and calls the ops, so the node being processed reads as off-list while the batch is still using it. The lockless test would still be safe, as the second grace period is what waits for the batch, but the rationale is incorrect, which is kinda par for the course given the complexity of this path. I'd much rather keep it as dumb as possible.
Thanks.
--
tejun
Tejun Heo
reviewerPATCH v3 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. Syncs on an inactive cpu are processed instead of discarded, and the reseed reschedules the cpu itself, as they got no resched when queued. 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.
v2: Reschedule from the irq work and document the regain order.
v3: Pack nr_ecaps_offline with kick_sync_pending to avoid a hole (Andrea).
--- kernel/sched/ext/internal.h | 34 +++-- kernel/sched/ext/sub.c | 245 ++++++++++++++++++++------------- kernel/sched/sched.h | 3 +- tools/sched_ext/scx_qmap.bpf.c | 25 +--- 4 files changed, 174 insertions(+), 133 deletions(-)
kernel/sched/ext/internal.h +19 -15
@@ -880,12 +880,11 @@ struct sched_ext_ops {* The sub-scheduler holds no caps by this point and can no longer * affect any cid. Every cap reported through * ops.sub_child_ecaps_updated() was also reported lost there before the -* sub-scheduler's tasks came back to this sched, except on a cpu that -* went offline. Nothing is reported for a sub-scheduler whose enable -* failed before its tasks were handed over. If this sched is being -* disabled too, it can miss these reports. On a cpu whose loss wasn't -* reported, whatever was delegated, e.g. cpuperf targets, is the -* parent's to restore here. +* sub-scheduler's tasks came back to this sched. Nothing is reported +* for a sub-scheduler whose enable failed before its tasks were handed +* over. If this sched is being disabled too, it can miss these reports. +* On a cpu whose loss wasn't reported, whatever was delegated, e.g. +* cpuperf targets, is the parent's to restore here. */ void (*sub_detach)(struct scx_sub_detach_args *args);
@@ -928,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);
@@ -961,12 +965,15 @@ 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. A parent other than the root loses its +* own caps on the cid too when the cpu goes offline, possibly before +* this op is invoked, and regains them when the cpu comes back, +* possibly after. It can't act on the cid while it holds no caps there. * * 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
@@ -1442,6 +1449,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 */
@@ -1512,11 +1521,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 +150 -95
418 lines not on this page · raw message
kernel/sched/sched.h +2 -1
@@ -847,9 +847,10 @@ struct scx_rq {struct scx_sched *sched;struct llist_head ecaps_to_sync;/* pending ecaps syncs */ struct task_struct *sub_dispatch_prev; +u32 nr_ecaps_offline;/* marked syncs a down waits for */ #endif -cpumask_var_t cpus_to_sync;bool kick_sync_pending; +cpumask_var_t cpus_to_sync;unsigned long kick_sync;struct list_head sched_pcpus_to_kick;/* see kick_cpus_irq_workfn() */
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
Andrea Righi
reviewerRe: [PATCH 2/6] sched_ext: Free a sub-scheduler a grace period after discarding its ecaps syncs
In reply to Tejun HeoQuoted 16 lines · Tejun Heo
Hello, Andrea.
Quoted 4 lines · Andrea Righi
This is called for every possible CPU from the free path of every sched, including a root without sub-scheds, whose nodes are never queued. With the lock moved ahead of the test, every scheduler unload now takes all the rq locks with irqs off, which on a large machine can be a lot of lock traffic.
The loop this replaces also took each rq lock once, and attach and detach already cycle through all the rq locks several times, e.g. in
scx_bypass()andecaps_report(). Cycling through the rq locks isn't that expensive.Quoted 3 lines
The first grace period has passed by now, so nothing queues the node again, and a node taken by an in-flight batch still reads as on-list. So, can we keep the lockless test in front?
Quoted 7 lines
A node taken by an in-flight batch doesn't read as on-list.
__scx_process_sync_ecaps()callsinit_llist_node()on each node before it writes the pcpu and calls the ops, so the node being processed reads as off-list while the batch is still using it. The lockless test would still be safe, as the second grace period is what waits for the batch, but the rationale is incorrect, which is kinda par for the course given the complexity of this path. I'd much rather keep it as dumb as possible.
Makes sense, thanks for clarifying.
-Andrea
Andrea Righi
reviewerRe: [PATCH 5/6] sched_ext: Report the ecaps loss when a cpu goes offline
In reply to Tejun HeoQuoted 8 lines · Tejun Heo
Hello, Andrea.
Quoted 4 lines · Andrea Righi
With a root-only scheduler the walk skips the root and queues nothing, but
resched_sync_ecaps()still runs on every online CPU, starting theext_serverand queueing the resched irq work for no reason. Can we do it only if something was actually queued?...
Quoted 8 lines
Quoted 4 lines
ame on the offline side: with no sub-sched nr is 0, but we still do
update_rq_clock(),dl_server_start(&rq->ext_server)and the irq work on a CPU that's going down and theext_serverstays enqueued there untilsched_cpu_dying()stops it. How about:These are on the cpu hotplug path, which is really cold, and the cost is a resched on one cpu. It's minuscule and doesn't matter, so I don't think it makes sense to add any complexity to avoid it.
Right, fair enough.
-Andrea
Hi Tejun,
Quoted 39 lines · Tejun Heo
Hello,
v3 (resched from irq work):
- Reschedule the cpu for its queued ecaps syncs from hard irq work on that cpu instead of a kick.dispatch_one()processes the syncs only at its start, and a kick that lands while the pick has the rq lock dropped is cleared with the pick (#3, #5, sashiko).
- Fix theSCX_RQ_ONLINEcomments (#4) and why no report mark is pending at a suspend down (#6), and update comments and descriptions.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 thesync_pcpu_ecaps()factoring. Add thecid_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).v2: https://lore.kernel.org/r/[email protected]
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 firstops.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()andops.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.
Quoted 6 lines
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_RTas 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.
For the whole series:
Thanks,
-Andrea
Quoted 6 lines
0001
sched_ext: Factor outcid_pshard()
0002sched_ext: Free a sub-scheduler a grace period after discarding its ecaps syncs 0003sched_ext: Report a sub-scheduler's ecaps on attach and detach 0004sched_ext: Deliver ecaps only while the root has the cpu online 0005sched_ext: Report the ecaps loss when a cpu goes offline
0006sched_ext: Leave suspend and hibernation out of the ecaps reports
Applied 1-6 to sched_ext/for-7.4-fixes with the following changes:
- 5 is the update posted as a reply to the original.
- Added to all:
Thanks.
--
tejun