sched_ext: Add lazy preemption support
Subsystems (3)
Andrea Righi
reviewerPATCH 1/1 sched_ext: Add lazy preemption support
The fair scheduling class can defer a scheduling boundary through lazy rescheduling, but sched_ext currently exposes only immediate preemption to BPF schedulers.
The series adds lazy variants of the enqueue and kick preemption flags. They expire the running sched_ext task's slice while allowing the scheduling boundary to be deferred until a return to user space or the next scheduler tick. Non-local lazy enqueue requests retain the head-insertion semantics of immediate preemption.
SCX_OPS_LAZY_SLICE_EXPIRY also lets a BPF scheduler request lazy rescheduling when a slice expires in task_tick_scx(), matching the choice available to the fair scheduling class. Immediate behavior remains the default and is used while sched_ext is being disabled.
An fexit probe on __resched_curr() records the exact reschedule TIF after the running task's slice is expired. Together with the stopping callback, the selftests cover immediate and lazy kicks, coalescing in both orders, tick-driven slice expiry and invalid flag combinations. Individual modes are skipped when the running kernel does not expose the corresponding flag.
Andrea Righi (2):
sched_ext: Add lazy preemption support
selftests/sched_ext: Add kick selftest
kernel/sched/ext/ext.c | 115 ++++-- kernel/sched/ext/internal.h | 36 +- kernel/sched/ext/sub.c | 15 +- tools/sched_ext/include/scx/enum_defs.autogen.h | 3 + tools/sched_ext/include/scx/enums.autogen.bpf.h | 6 + tools/sched_ext/include/scx/enums.autogen.h | 2 + tools/sched_ext/include/scx/enums_abi.autogen.h | 5 +- tools/testing/selftests/sched_ext/Makefile | 1 + tools/testing/selftests/sched_ext/kick.bpf.c | 144 +++++++ tools/testing/selftests/sched_ext/kick.c | 479 ++++++++++++++++++++++++ 10 files changed, 773 insertions(+), 33 deletions(-) create mode 100644 tools/testing/selftests/sched_ext/kick.bpf.c create mode 100644 tools/testing/selftests/sched_ext/kick.c
Andrea Righi
reviewerPATCH 1/2 sched_ext: Add lazy preemption support
The fair scheduling class supports lazy rescheduling: when lazy preemption is enabled, a lazy reschedule request does not preempt the current task immediately at the next kernel preemption point. Instead, it is serviced when returning to user space or promoted to an immediate reschedule by the next scheduler tick. This avoids unnecessary in-kernel preemption while still bounding the scheduling delay.
sched_ext only supports immediate preemption, so BPF schedulers cannot make the same trade-off.
Add lazy preemption support to sched_ext through SCX_ENQ_PREEMPT_LAZY and SCX_KICK_PREEMPT_LAZY. Both operations still expire the current sched_ext task's slice, but use resched_curr_lazy() so the scheduling boundary can be deferred when lazy preemption is available.
On a non-local DSQ %SCX_ENQ_PREEMPT_LAZY means a head insertion, as %SCX_ENQ_PREEMPT does there.
fair.c also expires a slice lazily: update_curr() calls resched_curr_lazy() when the slice has run out at the tick. Let a BPF scheduler opt into the same with SCX_OPS_LAZY_SLICE_EXPIRY, which makes task_tick_scx() request lazy rescheduling for a depleted slice. A task in user space still reschedules on the way back from the tick, a task in the kernel runs on to its next return to user space or to the next tick. The default stays immediate, as does rescheduling while the scheduler is being disabled.
Reject requests which combine immediate and lazy preemption. Also reject lazy kicks combined with SCX_KICK_WAIT, as waiting for a lazy scheduling boundary would have a potentially unbounded contract. Unknown kick flags are now rejected as well, where they used to be ignored.
Track lazy kicks in a separate mask, outside cpus_to_kick, so that a lazy-only request can be told from one that coincides with a plain or preempting kick. An immediate preemption supersedes a queued lazy one. A plain kick reschedules at once, but leaves the slice alone, so a lazy request next to it keeps clearing the slice while the reschedule stays
immediate: both are served.
--- kernel/sched/ext/ext.c | 115 ++++++++++++++---- kernel/sched/ext/internal.h | 36 +++++- kernel/sched/ext/sub.c | 15 +-- .../sched_ext/include/scx/enum_defs.autogen.h | 3 + .../sched_ext/include/scx/enums.autogen.bpf.h | 6 + tools/sched_ext/include/scx/enums.autogen.h | 2 + .../sched_ext/include/scx/enums_abi.autogen.h | 5 +- 7 files changed, 149 insertions(+), 33 deletions(-)
kernel/sched/ext/ext.c +94 -21
@@ -383,11 +383,11 @@ static bool rq_is_open(struct rq *rq, u64 enq_flags)return true;/* -* %SCX_ENQ_PREEMPT clears $curr's slice if on SCX and kicks dispatch, -* so allow it to avoid spuriously triggering reenq on a combined +* The preemption flags clear $curr's slice if on SCX and kick dispatch, +* so allow them to avoid spuriously triggering reenq on a combined * PREEMPT|IMMED insertion. */ -if (enq_flags &SCX_ENQ_PREEMPT ) { +if (enq_flags & (SCX_ENQ_PREEMPT |SCX_ENQ_PREEMPT_LAZY )) {struct task_struct *curr = rq->curr;/*
@@ -1577,12 +1577,16 @@ static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq,if (rq->scx.flags &SCX_RQ_IN_DISPATCH )return ; -if ((enq_flags &SCX_ENQ_PREEMPT ) && p != rq->curr && +if ((enq_flags & (SCX_ENQ_PREEMPT |SCX_ENQ_PREEMPT_LAZY )) && p != rq->curr && rq->curr->sched_class == &ext_sched_class) { -if (likely (scx_set_task_slice (rq->curr,0 ))) -resched_curr (rq); -else +if (likely (scx_set_task_slice (rq->curr,0 ))) { +if (enq_flags &SCX_ENQ_PREEMPT ) +resched_curr (rq); +else +resched_curr_lazy (rq); + }else {__scx_add_event (sch,SCX_EV_SLICE_DENIED ,1 ); + } } }
@@ -1672,7 +1676,7 @@ static void scx_dispatch_enqueue(struct scx_sched *sch, struct rq *rq,scx_error (sch,"DSQ ID 0x%016llx already had PRIQ-enqueued tasks" , dsq->id); -if (enq_flags & (SCX_ENQ_HEAD |SCX_ENQ_PREEMPT )) { +if (enq_flags & (SCX_ENQ_HEAD |SCX_ENQ_PREEMPT |SCX_ENQ_PREEMPT_LAZY )) {/* new task inserted at head - use fastpath */ if (dsq_insert_head (dsq, p) && !(dsq->id &SCX_DSQ_FLAG_BUILTIN ))rcu_assign_pointer (dsq->first_task, p);
@@ -2386,7 +2390,7 @@ void scx_move_local_task_to_local_dsq(struct scx_sched *sch, struct task_structWARN_ON_ONCE (p->scx.holding_cpu >=0 ); -if (enq_flags & (SCX_ENQ_HEAD |SCX_ENQ_PREEMPT )) +if (enq_flags & (SCX_ENQ_HEAD |SCX_ENQ_PREEMPT |SCX_ENQ_PREEMPT_LAZY ))dsq_insert_head (dst_dsq, p);else list_add_tail (&p->scx.dsq_list.node, &dst_dsq->list);
@@ -3804,8 +3808,14 @@ static void task_tick_scx(struct rq *rq, struct task_struct *curr, int queued)else if (SCX_HAS_OP (sch, tick))SCX_CALL_OP_TASK (sch, tick, rq, curr); -if (!curr->scx.slice) -resched_curr (rq); +if (!curr->scx.slice) { +/* see SCX_OPS_LAZY_SLICE_EXPIRY; the slice can't be trusted while bypassing */ +if ((sch->ops.flags &SCX_OPS_LAZY_SLICE_EXPIRY ) && + !scx_bypassing (sch,cpu_of (rq))) +resched_curr_lazy (rq); +else +resched_curr (rq); + } }#ifdef CONFIG_EXT_GROUP_SCHED
@@ -5371,6 +5381,7 @@ static void scx_sched_free_rcu_work(struct work_struct *work)free_cpumask_var (pcpu->cpus_to_kick);free_cpumask_var (pcpu->cpus_to_kick_if_idle);free_cpumask_var (pcpu->cpus_to_preempt); +free_cpumask_var (pcpu->cpus_to_preempt_lazy);free_cpumask_var (pcpu->cpus_to_wait);exit_dsq (scx_bypass_dsq (sch, cpu));
@@ -6883,6 +6894,9 @@ static void scx_dump_cpu(struct scx_sched *sch, struct seq_buf *s,if (!cpumask_empty (pcpu->cpus_to_preempt))scx_dump_line (&ns," cpus_to_preempt: %*pb" ,cpumask_pr_args (pcpu->cpus_to_preempt)); +if (!cpumask_empty (pcpu->cpus_to_preempt_lazy)) +scx_dump_line (&ns," preempt_lazy : %*pb" , +cpumask_pr_args (pcpu->cpus_to_preempt_lazy));if (!cpumask_empty (pcpu->cpus_to_wait))scx_dump_line (&ns," cpus_to_wait : %*pb" ,cpumask_pr_args (pcpu->cpus_to_wait));
@@ -7200,6 +7214,7 @@ struct scx_sched *scx_alloc_and_add_sched(struct scx_enable_cmd *cmd,if (!zalloc_cpumask_var_node (&pcpu->cpus_to_kick,GFP_KERNEL , node) || !zalloc_cpumask_var_node (&pcpu->cpus_to_kick_if_idle,GFP_KERNEL , node) || !zalloc_cpumask_var_node (&pcpu->cpus_to_preempt,GFP_KERNEL , node) || + !zalloc_cpumask_var_node (&pcpu->cpus_to_preempt_lazy,GFP_KERNEL , node) || !zalloc_cpumask_var_node (&pcpu->cpus_to_wait,GFP_KERNEL , node)) { ret = -ENOMEM ; goto err_free_pcpu;
@@ -7335,6 +7350,7 @@ struct scx_sched *scx_alloc_and_add_sched(struct scx_enable_cmd *cmd,free_cpumask_var (pcpu->cpus_to_kick);free_cpumask_var (pcpu->cpus_to_kick_if_idle);free_cpumask_var (pcpu->cpus_to_preempt); +free_cpumask_var (pcpu->cpus_to_preempt_lazy);free_cpumask_var (pcpu->cpus_to_wait); }for_each_possible_cpu (cpu) {
@@ -8462,10 +8478,20 @@ static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_rconst struct sched_class *cur_class;bool should_wait = false;bool kickable; +bool preempt, preempt_lazy, immediate;unsigned long flags;raw_spin_rq_lock_irqsave (rq, flags); cur_class = rq->curr->sched_class; + preempt =cpumask_test_cpu (cpu, pcpu->cpus_to_preempt); + preempt_lazy =cpumask_test_cpu (cpu, pcpu->cpus_to_preempt_lazy); +/* +* A lazy-only request sits in cpus_to_preempt_lazy alone. Any other +* kick for the same cpu, plain or preempting, is in cpus_to_kick and +* asks for an immediate reschedule; the lazy request still clears the +* slice, the immediate one still reschedules now, so both are served. +*/ + immediate = !preempt_lazy ||cpumask_test_cpu (cpu, pcpu->cpus_to_kick);/* * During CPU hotplug, a CPU may depend on kicking itself to make
@@ -8479,7 +8505,7 @@ static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_r !sched_class_above (cur_class, &ext_sched_class);if (kickable && !scx_missing_caps (pcpu->sch, cpu,SCX_CAP_BASE )) { -if (cpumask_test_cpu (cpu, pcpu->cpus_to_preempt)) { +if (preempt || preempt_lazy) {if (cur_class == &ext_sched_class) {u64 caps =scx_caps_for_preempt (pcpu->sch, rq,0 );
@@ -8488,7 +8514,6 @@ static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_relse if (unlikely (!scx_set_task_slice (rq->curr,0 )))__scx_add_event (pcpu->sch,SCX_EV_SLICE_DENIED ,1 ); } -cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt); }if (cpumask_test_cpu (cpu, pcpu->cpus_to_wait)) {
@@ -8500,14 +8525,20 @@ static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_rcpumask_clear_cpu (cpu, pcpu->cpus_to_wait); } -resched_curr (rq); +if (immediate) +resched_curr (rq); +else +resched_curr_lazy (rq); }else {/* a kickable cpu was skipped solely for the missing caps */ if (kickable)__scx_add_event (pcpu->sch,SCX_EV_SUB_KICK_DENIED ,1 ); -cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt);cpumask_clear_cpu (cpu, pcpu->cpus_to_wait); } +if (preempt) +cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt); +if (preempt_lazy) +cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt_lazy);scx_rq_lock_drop (rq);raw_spin_rq_unlock_irqrestore (rq, flags);
@@ -8566,6 +8597,13 @@ static void kick_cpus_irq_workfn(struct irq_work *irq_work)cpumask_clear_cpu (cpu, pcpu->cpus_to_kick_if_idle); } +/* +* kick_one_cpu() clears the lazy bit of every cpu it visited +* above; what remains are lazy-only requests, see scx_kick_cpu(). +*/ +for_each_cpu (cpu, pcpu->cpus_to_preempt_lazy) +kick_one_cpu (cpu, pcpu, this_rq, ksyncs); +for_each_cpu (cpu, pcpu->cpus_to_kick_if_idle) {kick_one_cpu_if_idle (cpu, pcpu, this_rq);cpumask_clear_cpu (cpu, pcpu->cpus_to_kick_if_idle);
@@ -8734,6 +8772,11 @@ static bool scx_vet_enq_flags(struct scx_sched *sch, u64 dsq_id, u64 *enq_flags)return false; } +if (unlikely ((*enq_flags &SCX_ENQ_PREEMPT ) && (*enq_flags &SCX_ENQ_PREEMPT_LAZY ))) { +scx_error (sch,"SCX_ENQ_PREEMPT and SCX_ENQ_PREEMPT_LAZY cannot be combined" ); +return false; + } +if (*enq_flags &SCX_ENQ_IMMED ) {if (unlikely (!is_local)) {scx_error (sch,"SCX_ENQ_IMMED on a non-local DSQ 0x%llx" , dsq_id);
@@ -9539,6 +9582,24 @@ void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)if (!scx_kf_allowed_ctx (sch))return ; +if (unlikely (flags & ~(SCX_KICK_IDLE |SCX_KICK_PREEMPT |SCX_KICK_WAIT | +SCX_KICK_PREEMPT_LAZY ))) { +scx_error (sch,"invalid kick flags 0x%llx" , flags); +return ; + } +if (unlikely ((flags &SCX_KICK_PREEMPT ) && (flags &SCX_KICK_PREEMPT_LAZY ))) { +scx_error (sch,"SCX_KICK_PREEMPT and SCX_KICK_PREEMPT_LAZY cannot be combined" ); +return ; + } +if (unlikely ((flags &SCX_KICK_PREEMPT_LAZY ) && (flags &SCX_KICK_WAIT ))) { +scx_error (sch,"SCX_KICK_PREEMPT_LAZY cannot be used with SCX_KICK_WAIT" ); +return ; + } +if (unlikely ((flags &SCX_KICK_IDLE ) && + (flags & (SCX_KICK_PREEMPT |SCX_KICK_PREEMPT_LAZY |SCX_KICK_WAIT )))) { +scx_error (sch,"PREEMPT/WAIT cannot be used with SCX_KICK_IDLE" ); +return ; + }local_irq_save (irq_flags);
@@ -9564,9 +9625,6 @@ void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)if (flags &SCX_KICK_IDLE ) {struct rq *target_rq =cpu_rq (cpu); -if (unlikely (flags & (SCX_KICK_PREEMPT |SCX_KICK_WAIT ))) -scx_error (sch,"PREEMPT/WAIT cannot be used with SCX_KICK_IDLE" ); -if (raw_spin_rq_trylock (target_rq)) {if (can_skip_idle_kick (target_rq)) {scx_rq_lock_drop (target_rq);
@@ -9577,11 +9635,25 @@ void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)raw_spin_rq_unlock (target_rq); }cpumask_set_cpu (cpu, pcpu->cpus_to_kick_if_idle); + }else if (flags &SCX_KICK_PREEMPT_LAZY ) { +/* +* Recorded in its own mask and not in cpus_to_kick, so that +* kick_one_cpu() can tell a lazy-only request from one that +* coincides with a plain or preempting kick. A queued immediate +* preemption already clears the slice and reschedules now, +* which is more than asked for; a plain kick doesn't touch the +* slice, so the lazy request must stay recorded next to it. +*/ +if (!cpumask_test_cpu (cpu, pcpu->cpus_to_preempt)) +cpumask_set_cpu (cpu, pcpu->cpus_to_preempt_lazy); }else {cpumask_set_cpu (cpu, pcpu->cpus_to_kick); -if (flags &SCX_KICK_PREEMPT ) +if (flags &SCX_KICK_PREEMPT ) { +/* an immediate preemption subsumes a queued lazy one */ cpumask_set_cpu (cpu, pcpu->cpus_to_preempt); +cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt_lazy); + }if (flags &SCX_KICK_WAIT )cpumask_set_cpu (cpu, pcpu->cpus_to_wait); }
@@ -9623,8 +9695,9 @@ __bpf_kfunc void scx_bpf_kick_cpu(s32 cpu, u64 flags, const struct bpf_prog_aux* cid-addressed equivalent of scx_bpf_kick_cpu(). An invalid @cid aborts the * scheduler via scx_cid_to_cpu(). Caps are enforced on the delivery path: a * kick is dropped if the caller lacks baseline access on @cid, and a -* %SCX_KICK_PREEMPT degrades to a plain reschedule if the caller lacks -* %SCX_CAP_PREEMPT for a task outside its subtree. +* %SCX_KICK_PREEMPT or %SCX_KICK_PREEMPT_LAZY request degrades to a plain +* reschedule if the caller lacks %SCX_CAP_PREEMPT for a task outside its +* subtree. */ __bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux) {
kernel/sched/ext/internal.h +32 -4
@@ -215,6 +215,17 @@ enum scx_ops_flags {*/ SCX_OPS_TID_TO_TASK =1LLU <<8 , +/* +* If set, a slice that runs out at the tick requests lazy rescheduling +* instead of an immediate one, the way fair.c expires a slice from +* update_curr(): a task in user space still reschedules on the way +* back from the tick, a task in the kernel runs on to its next return +* to user space or to the next tick, which promotes the request. No +* effect on kernels without lazy preemption. Rescheduling while the +* scheduler is being disabled stays immediate. +*/ +SCX_OPS_LAZY_SLICE_EXPIRY =1LLU <<9 , +SCX_OPS_ALL_FLAGS =SCX_OPS_KEEP_BUILTIN_IDLE |SCX_OPS_ENQ_LAST | SCX_OPS_ENQ_EXITING |
@@ -223,7 +234,8 @@ enum scx_ops_flags {SCX_OPS_SWITCH_PARTIAL |SCX_OPS_BUILTIN_IDLE_PER_NODE |SCX_OPS_ALWAYS_ENQ_IMMED | -SCX_OPS_TID_TO_TASK , +SCX_OPS_TID_TO_TASK | +SCX_OPS_LAZY_SLICE_EXPIRY ,/* high 8 bits are internal, don't include in SCX_OPS_ALL_FLAGS */ __SCX_OPS_INTERNAL_MASK = 0xffLLU << 56,
@@ -1327,6 +1339,7 @@ struct scx_sched_pcpu {cpumask_var_t cpus_to_kick;cpumask_var_t cpus_to_kick_if_idle;cpumask_var_t cpus_to_preempt; +cpumask_var_t cpus_to_preempt_lazy;cpumask_var_t cpus_to_wait;struct list_head to_kick_node;
@@ -1407,15 +1420,16 @@ struct scx_sched_pnode {* the allocation pattern. * * ENQ_IMMED insert an IMMED task onto the cid's local DSQ -* - kick the cid's cpu (except SCX_KICK_PREEMPT) +* - kick the cid's cpu (except SCX_KICK_PREEMPT and +* SCX_KICK_PREEMPT_LAZY) * * ENQ insert any task onto the cid's local DSQ (implies ENQ_IMMED) * * PREEMPT preempt any task running on the cid regardless of the owning * sched (implies ENQ). Preempting a task in the sched's own subtree * doesn't require any cap. -* - SCX_ENQ_PREEMPT inserts -* - SCX_KICK_PREEMPT kicks +* - SCX_ENQ_PREEMPT and SCX_ENQ_PREEMPT_LAZY inserts +* - SCX_KICK_PREEMPT and SCX_KICK_PREEMPT_LAZY kicks * * PERF control the cid's cpu power/perf management state, currently the * cpufreq target set through scx_bpf_cidperf_set(). Hardware
@@ -1685,6 +1699,14 @@ enum scx_enq_flags {*/ SCX_ENQ_PREEMPT =1LLU <<32 , +/* +* Like %SCX_ENQ_PREEMPT, but request lazy rescheduling. The current +* task's slice is still cleared immediately so that the next scheduling +* boundary observes the new ordering. Implies %SCX_ENQ_HEAD, which is +* all it means on a non-local DSQ, as with %SCX_ENQ_PREEMPT. +*/ + SCX_ENQ_PREEMPT_LAZY = 1LLU << 35, +/* * Only allowed on local DSQs. Guarantees that the task either gets * on the CPU immediately and stays on it, or gets reenqueued back
@@ -1811,6 +1833,12 @@ enum scx_kick_flags {* is not on SCX. */ SCX_KICK_WAIT =1LLU <<2 , + +/* +* Like %SCX_KICK_PREEMPT, but request lazy rescheduling. This cannot be +* combined with %SCX_KICK_WAIT. +*/ + SCX_KICK_PREEMPT_LAZY = 1LLU << 3, }; enum scx_tg_flags {
kernel/sched/ext/sub.c +8 -7
@@ -686,9 +686,10 @@ void scx_rescue_init(struct rq *rq)* rescue is enabled, or @rq's reject DSQ after recording the reenq reason on * @p. * -* %SCX_ENQ_IMMED, %SCX_ENQ_PREEMPT and %SCX_ENQ_HEAD are cleared when diverting -* to rescue or reject. %SCX_ENQ_PREEMPT is also cleared on a fallback -* migration-disabled admission. +* %SCX_ENQ_IMMED, %SCX_ENQ_PREEMPT, %SCX_ENQ_PREEMPT_LAZY and %SCX_ENQ_HEAD +* are cleared when diverting to rescue or reject. %SCX_ENQ_PREEMPT and +* %SCX_ENQ_PREEMPT_LAZY are also cleared on a fallback migration-disabled +* admission. * * Bypass doesn't need special-casing as a bypassing sched's tasks are enqueued * to and run by its nearest non-bypassing ancestor. If root is bypassing, it
@@ -709,7 +710,7 @@ struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *r* On a remote activation the scheduling sched (@asch) differs from * @p's owner (@sch). Check caps against the scheduling sched. */ -if (*enq_flags &SCX_ENQ_PREEMPT ) +if (*enq_flags & (SCX_ENQ_PREEMPT |SCX_ENQ_PREEMPT_LAZY )) needed |=scx_caps_for_preempt (asch, rq, *enq_flags); missing =scx_missing_caps (asch,cpu_of (rq), needed);
@@ -726,7 +727,7 @@ struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *rif (unlikely (!scx_rq_online (rq) ||is_migration_disabled (p) || p->migration_pending)) {__scx_add_event (sch,SCX_EV_SUB_FORCED_ADMIT ,1 ); - *enq_flags &= ~SCX_ENQ_PREEMPT ; + *enq_flags &= ~(SCX_ENQ_PREEMPT |SCX_ENQ_PREEMPT_LAZY );return &rq->scx.local_dsq; }
@@ -735,8 +736,8 @@ struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *r* or HEAD - a diversion has no priority and IMMED is not allowed on * non-local DSQs. Strip the enq and task flags along with the slice. */ - *enq_flags &= ~(SCX_ENQ_IMMED |SCX_ENQ_PREEMPT |SCX_ENQ_HEAD | -SCX_ENQ_APPLY_SLICE |SCX_ENQ_SLICE_DFL ); + *enq_flags &= ~(SCX_ENQ_IMMED |SCX_ENQ_PREEMPT |SCX_ENQ_PREEMPT_LAZY | +SCX_ENQ_HEAD |SCX_ENQ_APPLY_SLICE |SCX_ENQ_SLICE_DFL ); p->scx.flags &= ~SCX_TASK_IMMED ;/* the enqueuer opted for rescue instead of rejection and reenqueue */
tools/sched_ext/include/scx/enum_defs.autogen.h +3 -0
@@ -85,6 +85,7 @@#define HAVE_SCX_ENQ_HEAD #define HAVE_SCX_ENQ_CPU_SELECTED #define HAVE_SCX_ENQ_PREEMPT +#define HAVE_SCX_ENQ_PREEMPT_LAZY #define HAVE_SCX_ENQ_IMMED #define HAVE_SCX_ENQ_RESCUE #define HAVE_SCX_ENQ_REENQ
@@ -148,6 +149,7 @@#define HAVE_SCX_KF_ALLOW_SELECT_CPU #define HAVE_SCX_KICK_IDLE #define HAVE_SCX_KICK_PREEMPT +#define HAVE_SCX_KICK_PREEMPT_LAZY #define HAVE_SCX_KICK_WAIT #define HAVE_SCX_OPI_BEGIN #define HAVE_SCX_OPI_NORMAL_BEGIN
@@ -164,6 +166,7 @@#define HAVE_SCX_OPS_BUILTIN_IDLE_PER_NODE #define HAVE_SCX_OPS_ALWAYS_ENQ_IMMED #define HAVE_SCX_OPS_TID_TO_TASK +#define HAVE_SCX_OPS_LAZY_SLICE_EXPIRY #define HAVE_SCX_OPS_ALL_FLAGS #define HAVE___SCX_OPS_INTERNAL_MASK #define HAVE_SCX_OPS_HAS_CPU_PREEMPT
tools/sched_ext/include/scx/enums.autogen.bpf.h +6 -0
@@ -109,6 +109,9 @@ const volatile u64 __SCX_KICK_IDLE __weak;const volatile u64 __SCX_KICK_PREEMPT__weak ;#define SCX_KICK_PREEMPT __SCX_KICK_PREEMPT +const volatile u64 __SCX_KICK_PREEMPT_LAZY__weak ; +#define SCX_KICK_PREEMPT_LAZY __SCX_KICK_PREEMPT_LAZY +const volatile u64 __SCX_KICK_WAIT__weak ;#define SCX_KICK_WAIT __SCX_KICK_WAIT
@@ -121,6 +124,9 @@ const volatile u64 __SCX_ENQ_HEAD __weak;const volatile u64 __SCX_ENQ_PREEMPT__weak ;#define SCX_ENQ_PREEMPT __SCX_ENQ_PREEMPT +const volatile u64 __SCX_ENQ_PREEMPT_LAZY__weak ; +#define SCX_ENQ_PREEMPT_LAZY __SCX_ENQ_PREEMPT_LAZY +const volatile u64 __SCX_ENQ_IMMED__weak ;#define SCX_ENQ_IMMED __SCX_ENQ_IMMED
tools/sched_ext/include/scx/enums.autogen.h +2 -0
@@ -40,10 +40,12 @@SCX_ENUM_SET (skel, scx_ent_dsq_flags,SCX_TASK_DSQ_ON_PRIQ ); \SCX_ENUM_SET (skel, scx_kick_flags,SCX_KICK_IDLE ); \SCX_ENUM_SET (skel, scx_kick_flags,SCX_KICK_PREEMPT ); \ +SCX_ENUM_SET (skel, scx_kick_flags,SCX_KICK_PREEMPT_LAZY ); \SCX_ENUM_SET (skel, scx_kick_flags,SCX_KICK_WAIT ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_WAKEUP ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_HEAD ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_PREEMPT ); \ +SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_PREEMPT_LAZY ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_IMMED ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_RESCUE ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_REENQ ); \
tools/sched_ext/include/scx/enums_abi.autogen.h +4 -1
@@ -97,6 +97,7 @@ static const struct __scx_enum_abi_val __scx_enum_abi_vals[] { "scx_enq_flags", "SCX_ENQ_HEAD", 0x10000LLU }, { "scx_enq_flags", "SCX_ENQ_CPU_SELECTED", 0x100000LLU }, { "scx_enq_flags", "SCX_ENQ_PREEMPT", 0x100000000LLU }, + { "scx_enq_flags", "SCX_ENQ_PREEMPT_LAZY", 0x800000000LLU }, { "scx_enq_flags", "SCX_ENQ_IMMED", 0x200000000LLU }, { "scx_enq_flags", "SCX_ENQ_RESCUE", 0x400000000LLU }, { "scx_enq_flags", "SCX_ENQ_REENQ", 0x10000000000LLU },
@@ -160,6 +161,7 @@ static const struct __scx_enum_abi_val __scx_enum_abi_vals[] { "scx_kf_allow_flags", "SCX_KF_ALLOW_SELECT_CPU", 0x20LLU }, { "scx_kick_flags", "SCX_KICK_IDLE", 0x1LLU }, { "scx_kick_flags", "SCX_KICK_PREEMPT", 0x2LLU }, + { "scx_kick_flags", "SCX_KICK_PREEMPT_LAZY", 0x8LLU }, { "scx_kick_flags", "SCX_KICK_WAIT", 0x4LLU }, { "scx_opi", "SCX_OPI_BEGIN", 0x0LLU }, { "scx_opi", "SCX_OPI_NORMAL_BEGIN", 0x0LLU },
@@ -176,7 +178,8 @@ static const struct __scx_enum_abi_val __scx_enum_abi_vals[] { "scx_ops_flags", "SCX_OPS_BUILTIN_IDLE_PER_NODE", 0x40LLU }, { "scx_ops_flags", "SCX_OPS_ALWAYS_ENQ_IMMED", 0x80LLU }, { "scx_ops_flags", "SCX_OPS_TID_TO_TASK", 0x100LLU }, - { "scx_ops_flags", "SCX_OPS_ALL_FLAGS", 0x1ffLLU }, + { "scx_ops_flags", "SCX_OPS_LAZY_SLICE_EXPIRY", 0x200LLU }, + { "scx_ops_flags", "SCX_OPS_ALL_FLAGS", 0x3ffLLU }, { "scx_ops_flags", "__SCX_OPS_INTERNAL_MASK", 0xff00000000000000LLU }, { "scx_ops_flags", "SCX_OPS_HAS_CPU_PREEMPT", 0x100000000000000LLU }, { "scx_ops_state", "SCX_OPSS_NONE", 0x0LLU },
Signature · 1 line
2.55.0
Tejun Heo
reviewerHello, Andrea.
This mostly looks fine to me, but the kick path changes could be simpler.
kernel/sched/ext/ext.c static void task_tick_scx(struct rq *rq, struct task_struct *curr, int queued) · line 3815
Quoted 3 lines · Andrea Righi
kernel/sched/ext/ext.c +3
-0+if ((sch->ops.flags &SCX_OPS_LAZY_SLICE_EXPIRY ) && + !scx_bypassing (sch,cpu_of (rq))) +resched_curr_lazy (rq);
Could lazy slice expiry be a per-task flag, with the ops flag providing the default? That would let a scheduler choose immediate or lazy expiry for individual tasks. BPF should be able to override the default in either direction, with bypass still forcing immediate rescheduling.
There's also a NO_HZ_FULL corner case. If a remote target is running with SCX_SLICE_INF and its tick stopped, resched_curr_lazy() sends no IPI, and clearing the slice doesn't restart the tick. sched_tick_remote() calls task_tick_scx() directly, bypassing the lazy-to-immediate promotion in sched_tick(). With lazy expiry enabled, it keeps requesting lazy rescheduling. That leaves delivery dependent on another interrupt, such as the deadline server timer. Both the kick and enqueue paths need to arrange progress for a tick-stopped target.
kernel/sched/ext/ext.c static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_r · line 8541
Quoted 4 lines
kernel/sched/ext/ext.c +4
-0+if (preempt) +cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt); +if (preempt_lazy) +cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt_lazy);
Why not clear both masks where cpus_to_preempt was previously cleared, including the skipped-kick path? There's no need for the additional conditions here.
kernel/sched/ext/ext.c void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) · line 9597
Quoted 8 lines
kernel/sched/ext/ext.c +8
-0+if (unlikely ((flags &SCX_KICK_PREEMPT ) && (flags &SCX_KICK_PREEMPT_LAZY ))) { +scx_error (sch,"SCX_KICK_PREEMPT and SCX_KICK_PREEMPT_LAZY cannot be combined" ); +return ; + } +if (unlikely ((flags &SCX_KICK_PREEMPT_LAZY ) && (flags &SCX_KICK_WAIT ))) { +scx_error (sch,"SCX_KICK_PREEMPT_LAZY cannot be used with SCX_KICK_WAIT" ); +return ; + }
Do we need to reject all these combinations? PREEMPT should win over PREEMPT_LAZY, as it does when separate calls request both. WAIT can force immediate rescheduling. A plain kick combined with lazy preemption should still clear the slice and reschedule immediately.
Quoted 2 lines
kernel/sched/ext/ext.c +2
-0+if (!cpumask_test_cpu (cpu, pcpu->cpus_to_preempt)) +cpumask_set_cpu (cpu, pcpu->cpus_to_preempt_lazy);
[ ... ]
Review
Quoted 1 line
(quoted diff) +1
-0+ cpumask_clear_cpu(cpu, pcpu->cpus_to_preempt_lazy);
Can we just accumulate the requested bits and resolve precedence in kick_one_cpu()? That would remove both the guard against cpus_to_preempt and the clearing of cpus_to_preempt_lazy.
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerHi Tejun,
Quoted 5 lines · Tejun Heo
Hello, Andrea.
This mostly looks fine to me, but the kick path changes could be simpler.
On Fri, Sep 11, 2026 at 09:56:53PM +0200, Andrea Righi wrote:
Quoted 8 lines
kernel/sched/ext/ext.c static void task_tick_scx(struct rq *rq, struct task_struct *curr, int queued) · line 3815
Quoted 3 lines
kernel/sched/ext/ext.c +3
-0+if ((sch->ops.flags &SCX_OPS_LAZY_SLICE_EXPIRY ) && + !scx_bypassing (sch,cpu_of (rq))) +resched_curr_lazy (rq);Could lazy slice expiry be a per-task flag, with the ops flag providing the default? That would let a scheduler choose immediate or lazy expiry for individual tasks. BPF should be able to override the default in either direction, with bypass still forcing immediate rescheduling.
Yes, that makes sense. We can add a BPF-writable per-task slice-expiry setting (e.g., p->scx.slice_expires_lazy), initialize it from SCX_OPS_LAZY_SLICE_EXPIRY immediately before ops.enable() and the BPF scheduler may override it from ops.enable() or any subsequent callback.
Quoted 9 lines
There's also a
NO_HZ_FULLcorner case. If a remote target is running withSCX_SLICE_INFand its tick stopped,resched_curr_lazy()sends no IPI, and clearing the slice doesn't restart the tick.sched_tick_remote()callstask_tick_scx()directly, bypassing the lazy-to-immediate promotion insched_tick(). With lazy expiry enabled, it keeps requesting lazy rescheduling. That leaves delivery dependent on another interrupt, such as the deadline server timer. Both the kick and enqueue paths need to arrange progress for a tick-stopped target.
Good point. We can add a common helper for lazy SCX rescheduling, when the runqueue was allowed to stop its tick, the helper clears SCX_RQ_CAN_STOP_TICK and update the scheduler tick dependency before calling resched_curr_lazy(). And setting TICK_DEP_BIT_SCHED will restart/kick the tick as needed.
Both the lazy enqueue and lazy kick paths will use this helper after successfully clearing the current task's slice.
Quoted 9 lines
kernel/sched/ext/ext.c static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_r · line 8541
Quoted 5 lines
kernel/sched/ext/ext.c +4
-0+if (preempt) +cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt); +if (preempt_lazy) +cpumask_clear_cpu (cpu, pcpu->cpus_to_preempt_lazy);Why not clear both masks where
cpus_to_preemptwas previously cleared, including the skipped-kick path? There's no need for the additional conditions here.
Ack. I'll change this.
Quoted 14 lines
kernel/sched/ext/ext.c void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) · line 9597
Quoted 9 lines
kernel/sched/ext/ext.c +8
-0+if (unlikely ((flags &SCX_KICK_PREEMPT ) && (flags &SCX_KICK_PREEMPT_LAZY ))) { +scx_error (sch,"SCX_KICK_PREEMPT and SCX_KICK_PREEMPT_LAZY cannot be combined" ); +return ; + } +if (unlikely ((flags &SCX_KICK_PREEMPT_LAZY ) && (flags &SCX_KICK_WAIT ))) { +scx_error (sch,"SCX_KICK_PREEMPT_LAZY cannot be used with SCX_KICK_WAIT" ); +return ; + }Do we need to reject all these combinations? PREEMPT should win over
PREEMPT_LAZY, as it does when separate calls request both. WAIT can force immediate rescheduling. A plain kick combined with lazy preemption should still clear the slice and reschedule immediately.
Agreed. We can accept these combinations and apply the following precedence:
PREEMPT or WAIT > plain kick > PREEMPT_LAZY
A lazy request should still clear the slice when it is combined with an immediate request. And I'll make the enqueue interface consistent with kick.
Quoted 11 lines
Quoted 3 lines
kernel/sched/ext/ext.c +2
-0+if (!cpumask_test_cpu (cpu, pcpu->cpus_to_preempt)) +cpumask_set_cpu (cpu, pcpu->cpus_to_preempt_lazy);[ ... ]
Review
Quoted 1 line
(quoted diff) +1
-0+ cpumask_clear_cpu(cpu, pcpu->cpus_to_preempt_lazy);Can we just accumulate the requested bits and resolve precedence in
kick_one_cpu()? That would remove both the guard againstcpus_to_preemptand the clearing ofcpus_to_preempt_lazy.
Yep, makes sense.
Thanks,
-Andrea
Andrea Righi
reviewerPATCH 2/2 selftests/sched_ext: Add kick selftest
In reply to Andrea RighiAdd trace-based coverage for immediate and lazy preemption kicks. Run a CPU-bound sched_ext victim and issue each kick after the victim starts running.
Use an fexit probe on __resched_curr() and the stopping callback to verify that the victim starts with a positive slice, has a zero slice after the rescheduling request is processed and subsequently reaches a scheduling boundary. The probe also records the requested TIF for comparison against an immediate-kick baseline and the active runtime preemption mode.
Issue lazy and immediate kicks in both orders and verify that the recorded TIF matches the immediate baseline. Keep the invalid flag checks from the original test.
Register immediate, lazy, coalescing, tick-expiry and invalid-request coverage as separate tests. Probe the running kernel BTF before using each kick mode to skip unsupported modes instead of failing or accidentally issuing a zero-flag kick.
--- tools/testing/selftests/sched_ext/Makefile | 1 + tools/testing/selftests/sched_ext/kick.bpf.c | 144 ++++++ tools/testing/selftests/sched_ext/kick.c | 479 +++++++++++++++++++ 3 files changed, 624 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/kick.bpf.c create mode 100644 tools/testing/selftests/sched_ext/kick.c
tools/testing/selftests/sched_ext/Makefile +1 -0
@@ -172,6 +172,7 @@ auto-test-targets := \ exit \ hotplug \ init_enable_count \ + kick \ maximal \ maybe_null \ minimal \
tools/testing/selftests/sched_ext/kick.bpf.c new file +144 -0
144 lines not on this page · raw message
tools/testing/selftests/sched_ext/kick.c new file +479 -0
479 lines not on this page · raw message
Signature · 1 line
2.55.0