sched_ext: Add lazy preemption support
Subsystems (3)
Andrea Righi
reviewerPATCH v2 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.
This 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.
SCX_OPS_LAZY_SLICE_EXPIRY selects lazy slice expiry by default. Schedulers can override that default in either direction for individual tasks through scx_bpf_task_set_slice_expiry(). The helper preserves sub-scheduler task ownership boundaries. Bypass continues to force immediate rescheduling.
Lazy enqueue and kick requests also restore the scheduler tick when targeting an infinite-slice task on a NO_HZ_FULL CPU, ensuring forward progress even when the target's tick has stopped.
The kselftests cover immediate and lazy preemption, coalescing and precedence between kick modes, enqueue preemption, per-task slice-expiry overrides, invalid flags and lazy preemption of infinite-slice tasks on NO_HZ_FULL CPUs.
Tested in separate virtme-ng runs, covering preempt=lazy and nohz_full:
$ vng -a "preempt=lazy" -- tools/testing/selftests/sched_ext/runner -t kick $ vng -a "preempt=lazy nohz_full=8-15" -- tools/testing/selftests/sched_ext/runner -t nohz_tick
kick:
PASSED: 6
SKIPPED: 0
FAILED: 0
nohz_tick:
CPU 8 received 7 finite-slice ticks
PASSED: 1
SKIPPED: 0
FAILED: 0
Changes in v2:
- Make lazy slice expiry a per-task property (Tejun Heo).
- Add scx_bpf_task_set_slice_expiry() for per-task overrides from any sched_ext callback (preserving sub-scheduler task ownership).
- Restore the scheduler tick dependency for lazy enqueue and kick preemption of infinite-slice tasks on NO_HZ_FULL CPUs (Tejun Heo).
- Accumulate immediate and lazy kick requests independently and resolve their precedence in kick_one_cpu() (Tejun Heo).
- Allow PREEMPT|PREEMPT_LAZY and WAIT|PREEMPT_LAZY, immediate preemption, WAIT and plain kicks take precedence over lazy preemption (Tejun Heo).
- Clear both preemption masks together when processing or skipping a preemption request (Tejun Heo).
- Extend the kselftests with combined and ordered kick requests, per-task expiry overrides and NO_HZ_FULL coverage.
- Link to v1: https://lore.kernel.org/r/[email protected]
Andrea Righi (2):
sched_ext: Add lazy preemption support
selftests/sched_ext: Add lazy preemption tests
include/linux/sched/ext.h | 8 + kernel/sched/ext/ext.c | 146 +++++- kernel/sched/ext/internal.h | 39 +- kernel/sched/ext/sub.c | 15 +- tools/sched_ext/include/scx/compat.bpf.h | 13 + 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 | 166 +++++++ tools/testing/selftests/sched_ext/kick.c | 526 ++++++++++++++++++++++ tools/testing/selftests/sched_ext/nohz_tick.bpf.c | 54 ++- tools/testing/selftests/sched_ext/nohz_tick.c | 189 +++++++- 14 files changed, 1129 insertions(+), 44 deletions(-)
Andrea Righi
reviewerPATCH 1/2 sched_ext: Add lazy preemption support
The fair scheduling class can request lazy rescheduling, deferring an in-kernel scheduling boundary until returning to user space or until the next scheduler tick. sched_ext only exposes immediate preemption, preventing BPF schedulers from making the same trade-off.
Add SCX_ENQ_PREEMPT_LAZY and SCX_KICK_PREEMPT_LAZY. Both expire the current sched_ext task slice but request lazy rescheduling. Immediate preemption, WAIT and plain kicks take precedence when requests are combined, while a lazy enqueue to a non-local DSQ retains the head-insertion semantics of SCX_ENQ_PREEMPT.
Add SCX_OPS_LAZY_SLICE_EXPIRY as the default expiry policy for newly enabled tasks and initialize it before ops.enable(). Add scx_bpf_task_set_slice_expiry() so the owning scheduler can override the policy per task from any callback while preserving sub-scheduler task ownership boundaries. Bypass continues to force immediate expiry.
Restore the scheduler tick dependency before lazily rescheduling a task whose infinite slice allowed a NO_HZ_FULL CPU to stop its tick. Accumulate kick requests independently and resolve precedence while holding the target rq lock. Reject unknown kick flags and invalid SCX_KICK_IDLE combinations.
--- include/linux/sched/ext.h | 8 + kernel/sched/ext/ext.c | 146 +++++++++++++++--- kernel/sched/ext/internal.h | 39 ++++- kernel/sched/ext/sub.c | 15 +- tools/sched_ext/include/scx/compat.bpf.h | 13 ++ .../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 +- 9 files changed, 200 insertions(+), 37 deletions(-)
include/linux/sched/ext.h +8 -0
@@ -249,6 +249,14 @@ struct sched_ext_entity {*/ u64 dsq_vtime; +/* +* If set, depletion of this task's slice at the scheduler tick requests +* lazy instead of immediate rescheduling. Initialized from +* %SCX_OPS_LAZY_SLICE_EXPIRY immediately before ops.enable() and may be +* modified afterwards with scx_bpf_task_set_slice_expiry(). +*/ +bool slice_expires_lazy; +/* * Out-of-band slice request from scx_bpf_task_set_slice() when the * caller does not hold the rq lock, applied under the rq lock at the
kernel/sched/ext/ext.c +121 -25
310 lines not on this page · raw message
kernel/sched/ext/internal.h +35 -4
@@ -215,6 +215,19 @@ enum scx_ops_flags {*/ SCX_OPS_TID_TO_TASK =1LLU <<8 , +/* +* If set, tasks default to requesting lazy rescheduling when their slice +* runs out at the tick, the way fair.c expires a slice from update_curr(). +* The default is copied to p->scx.slice_expires_lazy immediately before +* ops.enable(), after which scx_bpf_task_set_slice_expiry() may override +* it per task. +* 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 disabling 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 +236,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 +1341,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 +1422,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 +1701,15 @@ 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. %SCX_ENQ_PREEMPT takes precedence +* if both are specified. 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 +1836,12 @@ enum scx_kick_flags {* is not on SCX. */ SCX_KICK_WAIT =1LLU <<2 , + +/* +* Like %SCX_KICK_PREEMPT, but request lazy rescheduling. If combined +* with %SCX_KICK_PREEMPT or %SCX_KICK_WAIT, rescheduling is immediate. +*/ + 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/compat.bpf.h +13 -0
@@ -403,6 +403,19 @@ static inline void scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime) p->scx.dsq_vtime = vtime; } +/* +* v7.4: scx_bpf_task_set_slice_expiry() added to enforce sub-scheduler task +* ownership. Preserve until v7.7. +*/ +bool scx_bpf_task_set_slice_expiry___new(struct task_struct *p, bool lazy) __ksym __weak; + +static inline bool scx_bpf_task_set_slice_expiry (struct task_struct *p,bool lazy) +{ +if (bpf_ksym_exists (scx_bpf_task_set_slice_expiry___new)) +return scx_bpf_task_set_slice_expiry___new (p, lazy); +return false; +} +/* * v7.1: New scx_bpf_dsq_reenq() that allows re-enqueues on more DSQs. This * will eventually deprecate scx_bpf_reenqueue_local().
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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Calling update_rq_clock(rq) inside scx_resched_curr_lazy() triggers a WARN_DOUBLE_CLOCK warning…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Calling update_rq_clock(rq) inside scx_resched_curr_lazy() triggers a WARN_DOUBLE_CLOCK warning because the runqueue clock has already been updated in the calling contexts. --
commit 6fc03e981449ffc8b39d02362a88e51ad755b27e
Author: Andrea Righi <[email protected]>
sched_ext: Add lazy preemption support
The fair scheduling class can request lazy rescheduling, deferring an in-kernel scheduling boundary until returning to user space or until the next scheduler tick. sched_ext only exposes immediate preemption, preventing BPF schedulers from making the same trade-off.
Quoted 4 lines
kernel/sched/ext/ext.c +0
-0
[ ... ]
Quoted 5 lines
kernel/sched/ext/ext.c +1
-0@@ -1515,6 +1515,23 @@ static void call_task_dequeue(struct scx_sched *sch, struct rq *rq, p->scx.flags &= ~SCX_TASK_IN_CUSTODY ; } +/*
kernel/sched/ext/ext.c static void call_task_dequeue(struct scx_sched *sch, struct rq *rq, · line 1528
Quoted 10 lines
kernel/sched/ext/ext.c +10
-0+* A task with an infinite slice may be running with its tick stopped. Lazy +* rescheduling doesn't send an IPI, so restore the tick dependency first to +* guarantee that the lazy request is promoted by a real scheduler tick. +*/ +static void scx_resched_curr_lazy (struct rq *rq) +{ +if (rq->scx.flags &SCX_RQ_CAN_STOP_TICK ) { + rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK ; +update_rq_clock (rq); +update_other_load_avgs (rq);
[Severity: Medium]
Does this trigger a SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED) double-clock update warning?
It looks like update_rq_clock() can be called from paths where the clock has already been updated or the rq lock was taken without pinning.
For example, during a task enqueue, enqueue_task() updates the clock and sets RQCF_UPDATED. The call chain then reaches rq_owned_post_enq() -> scx_resched_curr_lazy(), calling update_rq_clock() while RQCF_UPDATED is still set.
It also appears reachable via kick_one_cpu(), which acquires the runqueue lock using raw_spin_rq_lock_irqsave() rather than rq_pin_lock(). This means RQCF_UPDATED is not cleared from the previous lock hold, and calling scx_resched_curr_lazy() here updates the clock without a valid pin context.
Quoted 6 lines
kernel/sched/ext/ext.c +6
-0+sched_update_tick_dependency (rq); + } + +resched_curr_lazy (rq); +} +
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Andrea Righi
reviewerOn Mon, Sep 14, 2026 at 09:06:40AM +0000, [email protected] wrote:
...
Quoted 28 lines
Quoted 5 lines
kernel/sched/ext/ext.c +1
-0@@ -1515,6 +1515,23 @@ static void call_task_dequeue(struct scx_sched *sch, struct rq *rq, p->scx.flags &= ~SCX_TASK_IN_CUSTODY ; } +/* kernel/sched/ext/ext.c static void call_task_dequeue(struct scx_sched *sch, struct rq *rq, · line 1528
Quoted 10 lines
kernel/sched/ext/ext.c +10
-0+* A task with an infinite slice may be running with its tick stopped. Lazy +* rescheduling doesn't send an IPI, so restore the tick dependency first to +* guarantee that the lazy request is promoted by a real scheduler tick. +*/ +static void scx_resched_curr_lazy (struct rq *rq) +{ +if (rq->scx.flags &SCX_RQ_CAN_STOP_TICK ) { + rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK ; +update_rq_clock (rq); +update_other_load_avgs (rq);[Severity: Medium]
Does this trigger aSCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED)double-clock update warning?It looks like
update_rq_clock()can be called from paths where the clock has already been updated or the rq lock was taken without pinning.For example, during a task enqueue,
enqueue_task()updates the clock and setsRQCF_UPDATED. The call chain then reachesrq_owned_post_enq()->scx_resched_curr_lazy(), callingupdate_rq_clock()whileRQCF_UPDATEDis still set.
Quoted 4 lines
It also appears reachable via
kick_one_cpu(), which acquires the runqueue lock usingraw_spin_rq_lock_irqsave()rather thanrq_pin_lock(). This meansRQCF_UPDATEDis not cleared from the previous lock hold, and callingscx_resched_curr_lazy()here updates the clock without a valid pin context.
This seems legit. The enqueue path may have already updated the rq clock before reaching sched_resched_curr_lazy(), while the kick path doesn't currently establish new rq pin context. I think we can avoid the redundant clock update when the clock is already current and using a pinned rq lock helpers in kick_one_cpu().
I'll fix this in v3.
-Andrea
Andrea Righi
reviewerPATCH 2/2 selftests/sched_ext: Add lazy preemption tests
In reply to Andrea RighiAdd trace-based coverage for immediate and lazy preemption. An fexit probe on __resched_curr() records the requested TIF and the current task slice, while the stopping callback verifies that the task reaches a scheduling boundary.
Exercise kick requests individually, in both orders and combined in one call. Verify that immediate preemption and WAIT take precedence over lazy preemption, and that combining immediate and lazy enqueue preemption follows the same rule. Also test overriding the expiry default in both directions through scx_bpf_task_set_slice_expiry().
Extend the NO_HZ_FULL test with infinite-slice victims. Verify that lazy enqueue and kick requests restart a stopped tick and make forward progress. Keep invalid kick flag coverage and skip modes not exposed by the running kernel.
--- tools/testing/selftests/sched_ext/Makefile | 1 + tools/testing/selftests/sched_ext/kick.bpf.c | 166 ++++++ tools/testing/selftests/sched_ext/kick.c | 526 ++++++++++++++++++ .../selftests/sched_ext/nohz_tick.bpf.c | 54 +- tools/testing/selftests/sched_ext/nohz_tick.c | 189 ++++++- 5 files changed, 929 insertions(+), 7 deletions(-) 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 +166 -0
166 lines not on this page · raw message
tools/testing/selftests/sched_ext/kick.c new file +526 -0
526 lines not on this page · raw message
tools/testing/selftests/sched_ext/nohz_tick.bpf.c +48 -6
82 lines not on this page · raw message
tools/testing/selftests/sched_ext/nohz_tick.c +188 -1
238 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 2/2] selftests/sched_ext: Add lazy preemption tests
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Missing PR_SET_PDEATHSIG in spawned victim tasks leads to orphaned tasks spinning at 100% CPU.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing PR_SET_PDEATHSIG in spawned victim tasks leads to orphaned tasks spinning at 100% CPU.
- [Medium] The wait condition for nr_lazy_victim_running hardcodes a target value without accounting for intermediate preemptions, causing test flakes. --
commit 027a94072433d755727d58cbb128013210dcb223
Author: Andrea Righi <[email protected]>
selftests/sched_ext: Add lazy preemption tests
This commit adds trace-based coverage for immediate and lazy preemption. It tests kick requests individually and combined, and extends the NO_HZ_FULL test with infinite-slice victims.
Quoted 5 lines
tools/testing/selftests/sched_ext/kick.c new file +0
-0
[ ... ]
Quoted 2 lines
tools/testing/selftests/sched_ext/kick.c new file +2
-0+static struct victim spawn_victim (int cpu) +{
[ ... ]
Quoted 8 lines
tools/testing/selftests/sched_ext/kick.c new file +8
-0+ victim.pid =fork (); +if (!victim.pid) { +cpu_set_t mask; + +close (ready[0 ]); +close (start[1 ]); +CPU_ZERO (&mask); +CPU_SET (cpu, &mask);
tools/testing/selftests/sched_ext/kick.c line 108
Quoted 10 lines
tools/testing/selftests/sched_ext/kick.c new file +10
-0+if (sched_setaffinity (0 ,sizeof (mask), &mask)) +_exit (1 ); +if (write (ready[1 ], &byte,1 ) !=1 ) +_exit (1 ); +close (ready[1 ]); +if (read (start[0 ], &byte,1 ) !=1 ) +_exit (1 ); +close (start[0 ]); +for (;;) + asmvolatile ("" :::"memory" );
[Severity: Medium]
Will this lead to orphaned tasks spinning at 100% CPU if the test runner crashes?
If the test is forcefully terminated or crashes before stop_victim() is called, the child processes will be orphaned and continue their infinite loop indefinitely.
Should prctl(PR_SET_PDEATHSIG, SIGKILL) be used here, similar to how it is used in spawn_gated_worker() elsewhere in this patchset?
Quoted 1 line
tools/testing/selftests/sched_ext/kick.c new file +1
-0+ }
[ ... ]
Quoted 4 lines
tools/testing/selftests/sched_ext/nohz_tick.c +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/sched_ext/nohz_tick.c +6
-0+/* Repeat with a lazy kick delivered from a housekeeping CPU. */ +__atomic_store_n (&skel->bss->phase,NOHZ_PHASE_LAZY_KICK , + __ATOMIC_RELEASE); + victim =spawn_gated_worker (ctx->test_cpu); + challenger =spawn_gated_worker (ctx->test_cpu); + trigger =spawn_gated_worker (ctx->housekeeping_cpu);
tools/testing/selftests/sched_ext/nohz_tick.c static enum scx_test_status run(void *ctx_ptr) · line 471
Quoted 10 lines
tools/testing/selftests/sched_ext/nohz_tick.c +10
-0+if (victim.pid <0 || challenger.pid <0 || trigger.pid <0 ) { +SCX_ERR ("Failed to spawn lazy-kick workers" ); + goto out; + } + skel->bss->victim_pid = victim.pid; + skel->bss->challenger_pid = challenger.pid; + skel->bss->trigger_pid = trigger.pid; +if (!start_gated_worker (&victim) || + !wait_for_counter (&skel->bss->nr_lazy_victim_running,2 , +PHASE_TIMEOUT_MS )) {
[Severity: Medium]
Could this wait condition cause the test to flake by relying on an absolute counter value?
During the teardown of the previous phase (NOHZ_PHASE_LAZY_ENQ), stopping the challenger can allow the original victim to be rescheduled, incrementing nr_lazy_victim_running to 2 before this new phase begins.
If that happens, wait_for_counter() will return immediately here, and the test will proceed to launch the new challenger before the new victim is running. If the new challenger is scheduled before the new victim, the subsequent check on nr_lazy_kick_running can fail.
Should this wait condition account for intermediate preemptions instead of hardcoding 2?
Quoted 3 lines
tools/testing/selftests/sched_ext/nohz_tick.c +3
-0+SCX_ERR ("Lazy-kick victim was not scheduled" ); + goto out; + }
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Andrea Righi
reviewerRe: [PATCH 2/2] selftests/sched_ext: Add lazy preemption tests
On Mon, Sep 14, 2026 at 09:01:55AM +0000, [email protected] wrote:
...
Quoted 23 lines
Quoted 8 lines
tools/testing/selftests/sched_ext/kick.c new file +8
-0+ victim.pid =fork (); +if (!victim.pid) { +cpu_set_t mask; + +close (ready[0 ]); +close (start[1 ]); +CPU_ZERO (&mask); +CPU_SET (cpu, &mask);tools/testing/selftests/sched_ext/kick.c line 108
Quoted 10 lines
tools/testing/selftests/sched_ext/kick.c new file +10
-0+if (sched_setaffinity (0 ,sizeof (mask), &mask)) +_exit (1 ); +if (write (ready[1 ], &byte,1 ) !=1 ) +_exit (1 ); +close (ready[1 ]); +if (read (start[0 ], &byte,1 ) !=1 ) +_exit (1 ); +close (start[0 ]); +for (;;) + asmvolatile ("" :::"memory" );[Severity: Medium]
Will this lead to orphaned tasks spinning at 100% CPU if the test runner crashes?
Quoted 6 lines
If the test is forcefully terminated or crashes before
stop_victim()is called, the child processes will be orphaned and continue their infinite loop indefinitely.Should
prctl(PR_SET_PDEATHSIG, SIGKILL)be used here, similar to how it is used inspawn_gated_worker()elsewhere in this patchset?
Although this is a kselftest intended for a test environment, it may still run on a persistent test host, and these workers spin indefinitely. Since I'm already planning to send a v3, I'll fix this as well.
Quoted 33 lines
Quoted 2 lines
(quoted diff) +1
-0+ }[ ... ]
Quoted 4 lines
tools/testing/selftests/sched_ext/nohz_tick.c +0
-0[ ... ]
Quoted 6 lines
tools/testing/selftests/sched_ext/nohz_tick.c +6
-0+/* Repeat with a lazy kick delivered from a housekeeping CPU. */ +__atomic_store_n (&skel->bss->phase,NOHZ_PHASE_LAZY_KICK , + __ATOMIC_RELEASE); + victim =spawn_gated_worker (ctx->test_cpu); + challenger =spawn_gated_worker (ctx->test_cpu); + trigger =spawn_gated_worker (ctx->housekeeping_cpu);tools/testing/selftests/sched_ext/nohz_tick.c static enum scx_test_status run(void *ctx_ptr) · line 471
Quoted 10 lines
tools/testing/selftests/sched_ext/nohz_tick.c +10
-0+if (victim.pid <0 || challenger.pid <0 || trigger.pid <0 ) { +SCX_ERR ("Failed to spawn lazy-kick workers" ); + goto out; + } + skel->bss->victim_pid = victim.pid; + skel->bss->challenger_pid = challenger.pid; + skel->bss->trigger_pid = trigger.pid; +if (!start_gated_worker (&victim) || + !wait_for_counter (&skel->bss->nr_lazy_victim_running,2 , +PHASE_TIMEOUT_MS )) {[Severity: Medium]
Could this wait condition cause the test to flake by relying on an absolute counter value?During the teardown of the previous phase (
NOHZ_PHASE_LAZY_ENQ), stopping the challenger can allow the original victim to be rescheduled, incrementingnr_lazy_victim_runningto 2 before this new phase begins.
Quoted 7 lines
If that happens,
wait_for_counter()will return immediately here, and the test will proceed to launch the new challenger before the new victim is running. If the new challenger is scheduled before the new victim, the subsequent check onnr_lazy_kick_runningcan fail.Should this wait condition account for intermediate preemptions instead of hardcoding 2?
We can snapshot nr_lazy_kick_running before starting the new victim and wait for the counter to increase by one. I'll also fix this on v3.
-Andrea
Re: [PATCH 2/2] selftests/sched_ext: Add lazy preemption tests
In reply to Andrea RighiHi Andrea,
Quoted 11 lines · Andrea Righi
Add trace-based coverage for immediate and lazy preemption. An fexit probe on
__resched_curr()records the requested TIF and the current task slice, while the stopping callback verifies that the task reaches a scheduling boundary.Exercise kick requests individually, in both orders and combined in one call. Verify that immediate preemption and WAIT take precedence over lazy preemption, and that combining immediate and lazy enqueue preemption follows the same rule. Also test overriding the expiry default in both directions through
scx_bpf_task_set_slice_expiry().
Quoted 6 lines
Extend the
NO_HZ_FULLtest with infinite-slice victims. Verify that lazy enqueue and kick requests restart a stopped tick and make forward progress. Keep invalid kick flag coverage and skip modes not exposed by the running kernel.Signed-off-by Andrea Righi <[email protected]>
[...]
Didn't have time to go through the whole series, but I applied it on for-next and built with PREEMPT_LAZY=y and PREEMPT_DYNAMIC=n:
INFO: preemption mode unavailable; lazy TIF was 5, immediate TIF was 4 ok 2 kick_lazy #
INFO: preemption mode unavailable; lazy TIF was 5, immediate TIF was 4 ok 4 kick_tick #
PASSED: 6
tools/testing/selftests/sched_ext/kick.c line 269
Quoted 8 lines
tools/testing/selftests/sched_ext/kick.c new file +8
-0+static int active_lazy_mode (void ) +{ +char buf[128 ]; +FILE *file; + + file =fopen ("/sys/kernel/debug/sched/preempt" ,"r" ); +if (!file) +return -1 ;
sched_init_debug() only creates that file under CONFIG_PREEMPT_DYNAMIC, and PREEMPT_LAZY can be built without it ("select PREEMPT_BUILD if !PREEMPT_DYNAMIC"), so the two tests pass without comparing the TIFs.
Would something like this work?
if (!file) {
#if defined(CONFIG_PREEMPT_LAZY) && !defined(CONFIG_PREEMPT_DYNAMIC)
return 1;
#else
return -1;
#endif
}Thanks.
Quoted 13 lines
tools/testing/selftests/sched_ext/kick.c new file +13
-0+if (!fgets (buf,sizeof (buf), file)) { +fclose (file); +return -1 ; + } +fclose (file); + +if (strstr (buf,"(lazy)" )) +return 1 ; +if (strstr (buf,"(full)" )) +return 0 ; +return -1 ; +} +
Signature · 2 lines
Cheers, Cheng-Yang
Andrea Righi
reviewerRe: [PATCH 2/2] selftests/sched_ext: Add lazy preemption tests
Quoted 45 lines · Cheng-Yang Chou
Hi Andrea,
Quoted 11 lines · Andrea Righi
Add trace-based coverage for immediate and lazy preemption. An fexit probe on
__resched_curr()records the requested TIF and the current task slice, while the stopping callback verifies that the task reaches a scheduling boundary.Exercise kick requests individually, in both orders and combined in one call. Verify that immediate preemption and WAIT take precedence over lazy preemption, and that combining immediate and lazy enqueue preemption follows the same rule. Also test overriding the expiry default in both directions through
scx_bpf_task_set_slice_expiry().Quoted 6 lines
Extend the
NO_HZ_FULLtest with infinite-slice victims. Verify that lazy enqueue and kick requests restart a stopped tick and make forward progress. Keep invalid kick flag coverage and skip modes not exposed by the running kernel.Signed-off-by Andrea Righi <[email protected]>[...]
Didn't have time to go through the whole series, but I applied it on for-next and built with PREEMPT_LAZY=y and PREEMPT_DYNAMIC=n:
INFO: preemption mode unavailable; lazy TIF was 5, immediate TIF was 4 ok 2
kick_lazy#
INFO: preemption mode unavailable; lazy TIF was 5, immediate TIF was 4 ok 4kick_tick#
PASSED: 6tools/testing/selftests/sched_ext/kick.c line 269
Quoted 8 lines
tools/testing/selftests/sched_ext/kick.c new file +8
-0+static int active_lazy_mode (void ) +{ +char buf[128 ]; +FILE *file; + + file =fopen ("/sys/kernel/debug/sched/preempt" ,"r" ); +if (!file) +return -1 ;
sched_init_debug()only creates that file underCONFIG_PREEMPT_DYNAMIC, andPREEMPT_LAZYcan be built without it ("selectPREEMPT_BUILDif !PREEMPT_DYNAMIC"), so the two tests pass without comparing the TIFs.
Quoted 8 lines
Would something like this work?
if (!file) { #if defined(CONFIG_PREEMPT_LAZY) && !defined(CONFIG_PREEMPT_DYNAMIC) return 1; #else return -1;#endif
Ah yes, makes sense, we can include autoconf.h and check that. I'll include this fix in the next version.
Thanks,
-Andrea
Quoted 21 lines
}Thanks.
Quoted 13 lines
tools/testing/selftests/sched_ext/kick.c new file +13
-0+if (!fgets (buf,sizeof (buf), file)) { +fclose (file); +return -1 ; + } +fclose (file); + +if (strstr (buf,"(lazy)" )) +return 1 ; +if (strstr (buf,"(full)" )) +return 0 ; +return -1 ; +} +Signature · 2 lines
Cheers, Cheng-Yang