sched_ext: Add lazy preemption support
Subsystems (3)
Andrea Righi
reviewerPATCH v4 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 selftests 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
kick:
PASSED: 6
SKIPPED: 0
FAILED: 0
$ vng -a "preempt=lazy nohz_full=8-15" -- tools/testing/selftests/sched_ext/runner -t nohz_tick
nohz_tick:
CPU 8 received 7 finite-slice ticks
PASSED: 1
SKIPPED: 0
FAILED: 0
Changes in v4:
- Preserve SCX_KICK_WAIT synchronization when it is combined with SCX_KICK_PREEMPT_LAZY (Sashiko).
- Restore the scheduler tick dependency when ops.tick() lazily expires an infinite slice, ensuring the request is promoted by a later tick (Sashiko).
- kselftest: verify that the deferred SCX_KICK_WAIT callback runs (Sashiko).
- kselftest: keep the kick test controller off the victim CPU and skip cleanly when a second allowed CPU is unavailable (Sashiko).
- kselftest: select the nohz_tick controller from allowed housekeeping CPUs outside the NOHZ_FULL mask and skip when the required topology is unavailable.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Avoid redundant rq clock updates when restoring the tick dependency and use the pinned rq lock helpers for lazy kick delivery (Sashiko).
- Set PR_SET_PDEATHSIG for the indefinitely spinning kick test workers so they don't survive unexpected test-runner exit (Sashiko).
- Wait for a delta in nr_lazy_victim_running instead of an absolute count that may include reschedules from the previous phase (Sashiko).
- Increase the nohz_tick scheduler watchdog and phase timeouts to tolerate scheduling delays in oversubscribed test VMs.
- Link to v2: https://lore.kernel.org/r/[email protected]
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 | 153 +++++- 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 | 182 +++++++ tools/testing/selftests/sched_ext/kick.c | 608 ++++++++++++++++++++++ tools/testing/selftests/sched_ext/nohz_tick.bpf.c | 56 +- tools/testing/selftests/sched_ext/nohz_tick.c | 249 ++++++++- 14 files changed, 1273 insertions(+), 67 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 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 | 153 ++++++++++++++---- 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, 204 insertions(+), 40 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 +125 -28
322 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
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 | 182 ++++++ tools/testing/selftests/sched_ext/kick.c | 608 ++++++++++++++++++ .../selftests/sched_ext/nohz_tick.bpf.c | 56 +- tools/testing/selftests/sched_ext/nohz_tick.c | 249 ++++++- 5 files changed, 1069 insertions(+), 27 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 +182 -0
182 lines not on this page · raw message
tools/testing/selftests/sched_ext/kick.c new file +608 -0
608 lines not on this page · raw message
tools/testing/selftests/sched_ext/nohz_tick.bpf.c +49 -7
88 lines not on this page · raw message
tools/testing/selftests/sched_ext/nohz_tick.c +229 -20
352 lines not on this page · raw message
Signature · 1 line
2.55.0