Skip to contentSkip to conversation

sched_ext: Add lazy preemption support

PATCH In review Andrea Righi 2 participants 4 messages Sep 18 – Sep 19

Subsystems (3)
lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH v7 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_RESCHED selects lazy rescheduling on slice expiry by default. Schedulers can override that persistent policy in either direction for individual tasks through scx_bpf_task_set_lazy_resched(). Unlike the one-shot SCX_ENQ_PREEMPT_LAZY and SCX_KICK_PREEMPT_LAZY operations, the kfunc does not request rescheduling itself; it controls what happens when a later scheduler tick finds the task's slice depleted. A compatibility wrapper makes the call a no-op on older kernels that lack the kfunc. 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. The reschedule request is set first so that the dependency update's IPI can serve it on return to user space, while the restored tick guarantees eventual promotion otherwise.

The selftests cover immediate and lazy preemption, coalescing and precedence between kick modes, enqueue preemption, per-task lazy rescheduling policy overrides, invalid flags, and lazy preemption of infinite-slice tasks on NO_HZ_FULL CPUs. The NO_HZ_FULL cases verify that the tick has stopped before issuing the lazy request.

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 --cpus 4 -a "nohz_full=1-3" -- \
     tools/testing/selftests/sched_ext/runner -t nohz_tick

nohz_tick:
CPU 1 received 6 finite-slice ticks
PASSED: 1
SKIPPED: 0
FAILED: 0

The mode-sensitive kick_lazy and kick_tick cases also passed after switching /sys/kernel/debug/sched/preempt between lazy and full. Tejun Heo additionally reported successful vng runs in lazy and full modes, with nohz_full on CPUs 2-3 and on CPUs 1-3.

Tao Cui tested v6 in a 4-CPU KVM guest. Lazy and immediate wakeup preemption both averaged 59 usecs, compared to 60 usecs for CFS, over 2,000 iterations. Lazy slice expiry reduced the context-switch rate of six busy hogs from about 1,000/s with CFS to about 215/s.

Changes in v7:
- Fold normal and lazy kick processing into one CPU loop, avoiding a second rq lock and clearing idle-kick state for lazy-only targets (Tejun Heo).
- Rename scx_bpf_task_set_slice_expiry() to scx_bpf_task_set_lazy_resched(), together with its task field and default ops flag (Tejun Heo).
- Clarify that scx_bpf_task_set_lazy_resched() sets a persistent per-task policy, while SCX_ENQ_PREEMPT_LAZY and SCX_KICK_PREEMPT_LAZY are one-shot requests.
- Add SCX_KICK_ALL_FLAGS and simplify validation of SCX_KICK_IDLE combinations (Tejun Heo).
- Fix the compatibility-wrapper comment and separate the ops flag documentation paragraphs (Tejun Heo).
- Share test enums between BPF and userspace, consolidate gated-worker helpers in util.c and remove an unused generated configuration include (Tejun Heo).
- Detect static preemption modes through /proc/config.gz when debugfs does not expose sched/preempt, and skip mode-sensitive assertions when the active mode cannot be determined (Tejun Heo).
- Restrict coalescing, per-task override and lazy NO_HZ_FULL checks to kernels running with lazy preemption enabled (Tejun Heo).
- Stop infinite-slice victims before their challengers and check the scheduler exit state while polling (Tejun Heo).
- Run the NO_HZ_FULL lazy phases with SCX_OPS_LAZY_RESCHED so that a remote tick cannot turn the test request into immediate preemption (Tejun Heo).
- Link to v6:
https://lore.kernel.org/r/[email protected]

Changes in v6:
- Pack slice_expires_lazy next to scx.disallow instead of growing task_struct by eight bytes (Tejun Heo).
- Correct the SCX_OPS_LAZY_SLICE_EXPIRY documentation for PREEMPT_DYNAMIC and bypass (Tejun Heo).
- Set the lazy reschedule request before restoring the tick dependency, so the dependency update's IPI can serve it on return to user space (Tejun Heo).
- Explain the pinned rq locking and the newly rejected unknown kick flags in the patch description (Tejun Heo).
- Regenerate the enum headers in definition order (Tejun Heo).
- kselftest: use SCX_OPS_ENQ_LAST to drive kick-test victims through ops.stopping() (Tejun Heo).
- kselftest: poll for the SCX_KICK_WAIT callback after ops.stopping() (Tejun Heo).
- kselftest: skip trace-based tests when their BTF targets are unavailable (Tejun Heo).
- kselftest: verify that the NO_HZ_FULL tick has stopped before testing lazy enqueue and kick requests (Tejun Heo).
- Link to v5:
https://lore.kernel.org/r/[email protected]

Changes in v5:
- kselftest: detect static lazy preemption when CONFIG_PREEMPT_DYNAMIC is disabled and the sched/preempt debugfs file is unavailable (Cheng-Yang Chou).
- Link to v4:
https://lore.kernel.org/r/[email protected]

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 while 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                             | 151 +++++-
 kernel/sched/ext/internal.h                        |  44 +-
 kernel/sched/ext/sub.c                             |  15 +-
 tools/sched_ext/include/scx/compat.bpf.h           |   9 +
 tools/sched_ext/include/scx/enum_defs.autogen.h    |   4 +
 tools/sched_ext/include/scx/enums.autogen.bpf.h    |   9 +
 tools/sched_ext/include/scx/enums.autogen.h        |   3 +
 tools/sched_ext/include/scx/enums_abi.autogen.h    |   6 +-
 tools/testing/selftests/sched_ext/Makefile         |   1 +
 tools/testing/selftests/sched_ext/kick.bpf.c       | 170 +++++++
 tools/testing/selftests/sched_ext/kick.c           | 531 +++++++++++++++++++++
 tools/testing/selftests/sched_ext/kick_test.h      |  29 ++
 tools/testing/selftests/sched_ext/nohz_tick.bpf.c  |  57 ++-
 tools/testing/selftests/sched_ext/nohz_tick.c      | 220 ++++++++-
 tools/testing/selftests/sched_ext/nohz_tick_test.h |  13 +
 tools/testing/selftests/sched_ext/util.c           | 144 ++++++
 tools/testing/selftests/sched_ext/util.h           |  13 +
 18 files changed, 1357 insertions(+), 70 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/kick.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/kick.c
 create mode 100644 tools/testing/selftests/sched_ext/kick_test.h
 create mode 100644 tools/testing/selftests/sched_ext/nohz_tick_test.h
author reviewer
#1 lore raw

PATCH 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_RESCHED as the default slice-expiry policy for newly enabled tasks and initialize it before ops.enable(). Add scx_bpf_task_set_lazy_resched() so the owning scheduler can override the persistent policy per task from any callback while preserving sub-scheduler task ownership boundaries. Bypass continues to force immediate rescheduling.

For an infinite-slice task whose tick is stopped, set the lazy reschedule request before restoring the scheduler tick dependency. This lets the dependency update's IPI serve the request on return to user space and guarantees that the next tick promotes it otherwise. Use pinned rq locking for remote kick delivery so RQCF_UPDATED reflects clock updates since lock acquisition.

Accumulate kick requests independently and resolve precedence while holding the target rq lock. Reject invalid SCX_KICK_IDLE combinations and unknown kick flags, which were previously silently ignored.

---
 include/linux/sched/ext.h                     |   8 +
 kernel/sched/ext/ext.c                        | 151 +++++++++++++++---
 kernel/sched/ext/internal.h                   |  44 ++++-
 kernel/sched/ext/sub.c                        |  15 +-
 tools/sched_ext/include/scx/compat.bpf.h      |   9 ++
 .../sched_ext/include/scx/enum_defs.autogen.h |   4 +
 .../sched_ext/include/scx/enums.autogen.bpf.h |   9 ++
 tools/sched_ext/include/scx/enums.autogen.h   |   3 +
 .../sched_ext/include/scx/enums_abi.autogen.h |   6 +-
 9 files changed, 212 insertions(+), 37 deletions(-)
include/linux/sched/ext.h +8 -0
index 8de69843c2150..1e1fc3312bc40 100644
@@ -278,6 +278,14 @@ struct sched_ext_entity {
 	 */
 	bool			disallow;	/* reject switching into SCX */
 
+	/*
+	 * If set, depletion of this task's slice at the scheduler tick requests
+	 * lazy instead of immediate rescheduling. Initialized from
+	 * %SCX_OPS_LAZY_RESCHED immediately before ops.enable() and may be
+	 * modified afterwards with scx_bpf_task_set_lazy_resched().
+	 */
+	bool			lazy_resched;
+
 	/* cold fields */
 #ifdef CONFIG_EXT_GROUP_SCHED
 	struct cgroup		*cgrp_moving_from;
kernel/sched/ext/ext.c +126 -25

308 lines not on this page · raw message

kernel/sched/ext/internal.h +40 -4

86 lines not on this page · raw message

kernel/sched/ext/sub.c +8 -7
index 380a5653dc529..a88b87614b55e 100644
@@ -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 *r
 	if (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 +9 -0
index 6944221f96cc0..7d870331b2a6b 100644
@@ -403,6 +403,15 @@ static inline void scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime)
 		p->scx.dsq_vtime = vtime;
 }
 
+/* v7.4: Add scx_bpf_task_set_lazy_resched(). */
+bool scx_bpf_task_set_lazy_resched___new(struct task_struct *p, bool lazy) __ksym __weak;
+
+static inline void scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy)
+{
+	if (bpf_ksym_exists(scx_bpf_task_set_lazy_resched___new))
+		scx_bpf_task_set_lazy_resched___new(p, lazy);
+}
+
 /*
  * 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 +4 -0
index 63b6b14b19bd4..92501498f6a36 100644
@@ -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
@@ -149,6 +150,8 @@
 #define HAVE_SCX_KICK_IDLE
 #define HAVE_SCX_KICK_PREEMPT
 #define HAVE_SCX_KICK_WAIT
+#define HAVE_SCX_KICK_PREEMPT_LAZY
+#define HAVE_SCX_KICK_ALL_FLAGS
 #define HAVE_SCX_OPI_BEGIN
 #define HAVE_SCX_OPI_NORMAL_BEGIN
 #define HAVE_SCX_OPI_NORMAL_END
@@ -164,6 +167,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_RESCHED
 #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 +9 -0
index 7268131010de3..21e1a79d5ecda 100644
@@ -112,6 +112,12 @@ const volatile u64 __SCX_KICK_PREEMPT __weak;
 const volatile u64 __SCX_KICK_WAIT __weak;
 #define SCX_KICK_WAIT __SCX_KICK_WAIT
 
+const volatile u64 __SCX_KICK_PREEMPT_LAZY __weak;
+#define SCX_KICK_PREEMPT_LAZY __SCX_KICK_PREEMPT_LAZY
+
+const volatile u64 __SCX_KICK_ALL_FLAGS __weak;
+#define SCX_KICK_ALL_FLAGS __SCX_KICK_ALL_FLAGS
+
 const volatile u64 __SCX_ENQ_WAKEUP __weak;
 #define SCX_ENQ_WAKEUP __SCX_ENQ_WAKEUP
 
@@ -121,6 +127,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 +3 -0
index e616326545172..6e0f6a3a219ac 100644
@@ -41,9 +41,12 @@
 	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_WAIT); \
+	SCX_ENUM_SET(skel, scx_kick_flags, SCX_KICK_PREEMPT_LAZY); \
+	SCX_ENUM_SET(skel, scx_kick_flags, SCX_KICK_ALL_FLAGS); \
 	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 +5 -1
index d53899764f5ac..f1bb6d70bf1d7 100644
@@ -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 },
@@ -161,6 +162,8 @@ static const struct __scx_enum_abi_val __scx_enum_abi_vals[]
 	{ "scx_kick_flags", "SCX_KICK_IDLE", 0x1LLU },
 	{ "scx_kick_flags", "SCX_KICK_PREEMPT", 0x2LLU },
 	{ "scx_kick_flags", "SCX_KICK_WAIT", 0x4LLU },
+	{ "scx_kick_flags", "SCX_KICK_PREEMPT_LAZY", 0x8LLU },
+	{ "scx_kick_flags", "SCX_KICK_ALL_FLAGS", 0xfLLU },
 	{ "scx_opi", "SCX_OPI_BEGIN", 0x0LLU },
 	{ "scx_opi", "SCX_OPI_NORMAL_BEGIN", 0x0LLU },
 	{ "scx_opi", "SCX_OPI_NORMAL_END", 0x21LLU },
@@ -176,7 +179,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_RESCHED", 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
author reviewer
#2 lore raw

PATCH 2/2 selftests/sched_ext: Add lazy preemption tests

In reply to Andrea Righi

Add trace-based coverage for immediate and lazy preemption. An fexit probe on __resched_curr() records the requested TIF after the current task's slice is cleared. SCX_OPS_ENQ_LAST drives the victim through ops.stopping() without relying on an unrelated wakeup.

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 persistent policy in both directions through scx_bpf_task_set_lazy_resched(), and add invalid kick flag coverage.

Extend the NO_HZ_FULL test with infinite-slice victims. Wait until the scheduler tick is observed to stop, then verify that lazy enqueue and kick requests restart it and make forward progress. Move the controller off all NO_HZ_FULL CPUs and increase the phase and watchdog timeouts to tolerate scheduling delays in oversubscribed test VMs.

Detect both dynamic and static kernel preemption modes and skip mode-sensitive assertions when the active mode is unavailable. Skip trace-based cases when their kernel BTF targets are unavailable, while allowing the independent invalid-flag cases to run.

---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 tools/testing/selftests/sched_ext/kick.bpf.c  | 170 ++++++
 tools/testing/selftests/sched_ext/kick.c      | 531 ++++++++++++++++++
 tools/testing/selftests/sched_ext/kick_test.h |  29 +
 .../selftests/sched_ext/nohz_tick.bpf.c       |  57 +-
 tools/testing/selftests/sched_ext/nohz_tick.c | 220 +++++++-
 .../selftests/sched_ext/nohz_tick_test.h      |  13 +
 tools/testing/selftests/sched_ext/util.c      | 144 +++++
 tools/testing/selftests/sched_ext/util.h      |  13 +
 9 files changed, 1145 insertions(+), 33 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/kick.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/kick.c
 create mode 100644 tools/testing/selftests/sched_ext/kick_test.h
 create mode 100644 tools/testing/selftests/sched_ext/nohz_tick_test.h
tools/testing/selftests/sched_ext/Makefile +1 -0
index 5f5dd9ab903ae..c4ec9b21a4c2a 100644
@@ -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 +170 -0

170 lines not on this page · raw message

tools/testing/selftests/sched_ext/kick.c new file +531 -0

531 lines not on this page · raw message

tools/testing/selftests/sched_ext/kick_test.h new file +29 -0
new file mode 100644
index 0000000000000..7d3c608c6b630
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES */
+#ifndef __KICK_TEST_H__
+#define __KICK_TEST_H__
+
+enum kick_scenario {
+	KICK_IMMEDIATE,
+	KICK_LAZY,
+	KICK_LAZY_THEN_IMMEDIATE,
+	KICK_IMMEDIATE_THEN_LAZY,
+	KICK_PLAIN_THEN_LAZY,
+	KICK_LAZY_THEN_PLAIN,
+	KICK_BOTH,
+	KICK_LAZY_WAIT,
+	ENQ_BOTH,
+	TICK_EXPIRY,
+	INVALID_KICK_IDLE,
+	INVALID_KICK_UNKNOWN,
+};
+
+enum kick_state {
+	KICK_STATE_IDLE,
+	KICK_STATE_ARMED,
+	KICK_STATE_QUEUED,
+	KICK_STATE_RESCHED,
+	KICK_STATE_DONE,
+};
+
+#endif /* __KICK_TEST_H__ */
tools/testing/selftests/sched_ext/nohz_tick.bpf.c +50 -7

93 lines not on this page · raw message

tools/testing/selftests/sched_ext/nohz_tick.c +194 -26

370 lines not on this page · raw message

tools/testing/selftests/sched_ext/nohz_tick_test.h new file +13 -0
new file mode 100644
index 0000000000000..b122d723cd8b4
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES */
+#ifndef __NOHZ_TICK_TEST_H__
+#define __NOHZ_TICK_TEST_H__
+
+enum nohz_phase {
+	NOHZ_PHASE_INF,
+	NOHZ_PHASE_FINITE,
+	NOHZ_PHASE_LAZY_ENQ,
+	NOHZ_PHASE_LAZY_KICK,
+};
+
+#endif /* __NOHZ_TICK_TEST_H__ */
tools/testing/selftests/sched_ext/util.c +144 -0

157 lines not on this page · raw message

tools/testing/selftests/sched_ext/util.h +13 -0
index 681cec04b4395..6a9bd92d42a3f 100644
@@ -7,7 +7,20 @@
 #ifndef __SCX_TEST_UTIL_H__
 #define __SCX_TEST_UTIL_H__
 
+#include <stdbool.h>
+#include <sys/types.h>
+
+struct scx_test_gated_worker {
+	pid_t pid;
+	int start_fd;
+};
+
 long file_read_long(const char *path);
 int file_write_long(const char *path, long val);
+struct scx_test_gated_worker scx_test_spawn_gated_worker(int cpu,
+							 bool set_sched_ext);
+bool scx_test_start_gated_worker(struct scx_test_gated_worker *worker);
+void scx_test_stop_gated_worker(struct scx_test_gated_worker *worker);
+int scx_test_preempt_lazy_mode(void);
 
 #endif // __SCX_TEST_UTIL_H__
Signature · 1 line
2.55.0
reviewer
#3 lore raw
In reply to Andrea Righi

Hello,

Quoted 3 lines · Andrea Righi

Andrea Righi (2):
sched_ext: Add lazy preemption support selftests/sched_ext: Add lazy preemption tests

Applied 1-2 to sched_ext/for-7.4. I refilled the code and comments.

Thanks.

--
tejun