Skip to contentSkip to conversation

sched_ext: Add lazy preemption support

PATCH Superseded Andrea Righi 3 participants 9 messages Sep 17 – Sep 18

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH v6 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. 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 slice-expiry 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.

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 (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                            | 152 ++++-
 kernel/sched/ext/internal.h                       |  40 +-
 kernel/sched/ext/sub.c                            |  15 +-
 tools/sched_ext/include/scx/compat.bpf.h          |  17 +
 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      | 191 +++++++
 tools/testing/selftests/sched_ext/kick.c          | 655 ++++++++++++++++++++++
 tools/testing/selftests/sched_ext/nohz_tick.bpf.c |  62 +-
 tools/testing/selftests/sched_ext/nohz_tick.c     | 280 ++++++++-
 14 files changed, 1372 insertions(+), 65 deletions(-)
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_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.

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                        | 152 +++++++++++++++---
 kernel/sched/ext/internal.h                   |  40 ++++-
 kernel/sched/ext/sub.c                        |  15 +-
 tools/sched_ext/include/scx/compat.bpf.h      |  17 ++
 .../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, 210 insertions(+), 38 deletions(-)
include/linux/sched/ext.h +8 -0
index 8de69843c2150..7c759baf4813f 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_SLICE_EXPIRY immediately before ops.enable() and may be
+	 * modified afterwards with scx_bpf_task_set_slice_expiry().
+	 */
+	bool			slice_expires_lazy;
+
 	/* cold fields */
 #ifdef CONFIG_EXT_GROUP_SCHED
 	struct cgroup		*cgrp_moving_from;
kernel/sched/ext/ext.c +126 -26

309 lines not on this page · raw message

kernel/sched/ext/internal.h +36 -4
index e1eb3a0d456cb..bb527e891c8cd 100644
@@ -215,6 +215,20 @@ 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. When lazy preemption is disabled
+	 * at runtime, the request behaves like an immediate reschedule.
+	 * Rescheduling while bypassing 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 +237,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 +1342,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 +1423,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 insert
+ *            - SCX_KICK_PREEMPT and SCX_KICK_PREEMPT_LAZY kick
  *
  * PERF       control the cid's cpu power/perf management state, currently the
  *            cpufreq target set through scx_bpf_cidperf_set(). Hardware
@@ -1685,6 +1702,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 +1837,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
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 +17 -0
index 6944221f96cc0..9491acb8d059f 100644
@@ -403,6 +403,23 @@ static inline void scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime)
 		p->scx.dsq_vtime = vtime;
 }
 
+/*
+ * v7.4: Add per-task control over whether slice expiry requests lazy or
+ * immediate rescheduling. Older kernels have no equivalent operation, so the
+ * compatibility wrapper is a no-op when the kfunc is unavailable. As with the
+ * setters above, discard the kfunc's authority result for consistency.
+ *
+ * Keep the wrapper until pre-v7.4 kernels fall out of the sched_ext scheduler
+ * support window.
+ */
+bool scx_bpf_task_set_slice_expiry___new(struct task_struct *p, bool lazy) __ksym __weak;
+
+static inline void scx_bpf_task_set_slice_expiry(struct task_struct *p, bool lazy)
+{
+	if (bpf_ksym_exists(scx_bpf_task_set_slice_expiry___new))
+		scx_bpf_task_set_slice_expiry___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 +3 -0
index 63b6b14b19bd4..5d25a29ab9782 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,7 @@
 #define HAVE_SCX_KICK_IDLE
 #define HAVE_SCX_KICK_PREEMPT
 #define HAVE_SCX_KICK_WAIT
+#define HAVE_SCX_KICK_PREEMPT_LAZY
 #define HAVE_SCX_OPI_BEGIN
 #define HAVE_SCX_OPI_NORMAL_BEGIN
 #define HAVE_SCX_OPI_NORMAL_END
@@ -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
index 7268131010de3..6afd4179cce50 100644
@@ -112,6 +112,9 @@ 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_ENQ_WAKEUP __weak;
 #define SCX_ENQ_WAKEUP __SCX_ENQ_WAKEUP
 
@@ -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
index e616326545172..16e500b10fc65 100644
@@ -41,9 +41,11 @@
 	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_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
index d53899764f5ac..d6f38bebd8134 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,7 @@ 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_opi", "SCX_OPI_BEGIN", 0x0LLU },
 	{ "scx_opi", "SCX_OPI_NORMAL_BEGIN", 0x0LLU },
 	{ "scx_opi", "SCX_OPI_NORMAL_END", 0x21LLU },
@@ -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
reviewer
#2 lore raw

Hello, Andrea.

Looks good overall. A few things:

kernel/sched/ext/ext.c static void kick_cpus_irq_workfn(struct irq_work *irq_work) · line 8630

Quoted 7 lines · Andrea Righi
kernel/sched/ext/ext.c +7 -0
+		/*
+		 * kick_one_cpu() clears the lazy bit of every cpu it visited
+		 * above; visit the remaining requests which contain lazy
+		 * preemption, see scx_kick_cpu().
+		 */
+		for_each_cpu(cpu, pcpu->cpus_to_preempt_lazy)
+			should_wait |= kick_one_cpu(cpu, pcpu, this_rq, ksyncs);

Can this fold into the loop above with for_each_cpu_or()? As is, a lazy-only target keeps its cpus_to_kick_if_idle bit and gets locked a second time by the idle loop.

kernel/sched/ext/ext.c __bpf_kfunc bool scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime, · line 9609

Quoted 2 lines
kernel/sched/ext/ext.c +2 -0
+__bpf_kfunc bool scx_bpf_task_set_slice_expiry(struct task_struct *p, bool lazy,
+					       const struct bpf_prog_aux *aux)

The name reads a bit like it turns slice expiry on and off. Maybe something like scx_bpf_task_set_lazy_resched(), with the ops flag and the task field named to match?

kernel/sched/ext/ext.c void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) · line 9634

Quoted 5 lines
kernel/sched/ext/ext.c +5 -0
+	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;
+	}

Maybe add an enum with all the valid bits and test against that?

kernel/sched/ext/ext.c void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) · line 9639

Quoted 5 lines
kernel/sched/ext/ext.c +5 -0
+	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;
+	}

Does this need to move out of the IDLE branch? It's two tests on IDLE instead of one, and as scx_error() aborts the scheduler anyway, the early return doesn't buy much.

kernel/sched/ext/internal.h enum scx_ops_flags { · line 224

Quoted 3 lines
kernel/sched/ext/internal.h +3 -0
+	 * 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

Can you add a blank line between the paragraphs?

Quoted 6 lines

+ * compatibility wrapper is a no-op when the kfunc is unavailable. As with the
+ * setters above, discard the kfunc's authority result for consistency.
+ *
+ * Keep the wrapper until pre-v7.4 kernels fall out of the sched_ext scheduler
+ * support window.
+ */

Just noting the version it was added in is enough.

Thanks.

Signature · 1 line
tejun
author reviewer
#3 lore raw

Hi Tejun,

Quoted 5 lines · Tejun Heo

Hello, Andrea.

Looks good overall. A few things:

On Thu, Sep 17, 2026 at 09:02:52AM +0200, Andrea Righi wrote:

Quoted 11 lines

kernel/sched/ext/ext.c static void kick_cpus_irq_workfn(struct irq_work *irq_work) · line 8630

Quoted 7 lines
kernel/sched/ext/ext.c +7 -0
+		/*
+		 * kick_one_cpu() clears the lazy bit of every cpu it visited
+		 * above; visit the remaining requests which contain lazy
+		 * preemption, see scx_kick_cpu().
+		 */
+		for_each_cpu(cpu, pcpu->cpus_to_preempt_lazy)
+			should_wait |= kick_one_cpu(cpu, pcpu, this_rq, ksyncs);

Can this fold into the loop above with for_each_cpu_or()? As is, a lazy-only target keeps its cpus_to_kick_if_idle bit and gets locked a second time by the idle loop.

Good point, this also clears cpus_to_kick_if_idle for lazy-only targets and we can avoid locking the rq again in the idle loop.

Quoted 7 lines

kernel/sched/ext/ext.c __bpf_kfunc bool scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime, · line 9609

Quoted 3 lines
kernel/sched/ext/ext.c +2 -0
+__bpf_kfunc bool scx_bpf_task_set_slice_expiry(struct task_struct *p, bool lazy,
+					       const struct bpf_prog_aux *aux)

The name reads a bit like it turns slice expiry on and off. Maybe something like scx_bpf_task_set_lazy_resched(), with the ops flag and the task field named to match?

Agreed, I like scx_bpf_task_set_lazy_resched() and at this point I'd also rename the ops flag to SCX_OPS_LAZY_RESCHED.

Quoted 8 lines

kernel/sched/ext/ext.c void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) · line 9634

Quoted 6 lines
kernel/sched/ext/ext.c +5 -0
+	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;
+	}

Maybe add an enum with all the valid bits and test against that?

Ack.

Quoted 10 lines

kernel/sched/ext/ext.c void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) · line 9639

Quoted 6 lines
kernel/sched/ext/ext.c +5 -0
+	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;
+	}

Does this need to move out of the IDLE branch? It's two tests on IDLE instead of one, and as scx_error() aborts the scheduler anyway, the early return doesn't buy much.

Ok.

Quoted 6 lines

kernel/sched/ext/internal.h enum scx_ops_flags { · line 224

Quoted 4 lines
kernel/sched/ext/internal.h +3 -0
+	 * 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

Can you add a blank line between the paragraphs?

Ok.

Quoted 9 lines
Quoted 7 lines

+ * compatibility wrapper is a no-op when the kfunc is unavailable. As with the
+ * setters above, discard the kfunc's authority result for consistency.
+ *
+ * Keep the wrapper until pre-v7.4 kernels fall out of the sched_ext scheduler
+ * support window.
+ */

Just noting the version it was added in is enough.

Ok.

Thanks for looking at this!
-Andrea

Tested-by on v1 1/2
#4 lore raw
In reply to Andrea Righi

Hi Andrea,

Quoted 24 lines · Andrea Righi

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.

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.

Quoted 4 lines

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.

I tested v6 on sched_ext/for-7.4 in a 4-CPU KVM guest: wakeup latency of a 1ms-period timer task under a scx_simple-based probe scheduler (SCX_OPS_LAZY_SLICE_EXPIRY) with a busy loop on the same CPU.

avg p50 p99
CFS (no scx) 60 59 66
no wake preempt 20919 20982 21002

  SCX_ENQ_PREEMPT     59      58      64
  SCX_ENQ_PREEMPT_LAZY 59     59      64
  CFS (control)       60      59      68

(usecs, 2000 iterations each)

Lazy and immediate preemption are identical here and on par with CFS, i.e. no wakeup latency regression in the common case, matching the intended semantics. SCX_OPS_LAZY_SLICE_EXPIRY also cut the context switch rate of 6 busy hogs from ~1000/s (CFS) to ~215/s. Repeated cgroup knob churn (cpu.weight/cpu.idle) ran clean, no warnings in dmesg.

Overall the series looks well-behaved in these scenarios. Thanks for working on this!

Quoted 631 lines
---
 include/linux/sched/ext.h                     |   8 +
 kernel/sched/ext/ext.c                        | 152 +++++++++++++++---
 kernel/sched/ext/internal.h                   |  40 ++++-
 kernel/sched/ext/sub.c                        |  15 +-
 tools/sched_ext/include/scx/compat.bpf.h      |  17 ++
 .../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, 210 insertions(+), 38 deletions(-)
include/linux/sched/ext.h +8 -0
index 8de69843c2150..7c759baf4813f 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_SLICE_EXPIRY immediately before ops.enable() and may be
+	 * modified afterwards with scx_bpf_task_set_slice_expiry().
+	 */
+	bool			slice_expires_lazy;
+
 	/* cold fields */
 #ifdef CONFIG_EXT_GROUP_SCHED
 	struct cgroup		*cgrp_moving_from;
kernel/sched/ext/ext.c +126 -26
index 4080902a528cd..e1d6fddda1815 100644
@@ -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;
 
 		/*
@@ -1515,6 +1515,26 @@ static void call_task_dequeue(struct scx_sched *sch, struct rq *rq,
 	p->scx.flags &= ~SCX_TASK_IN_CUSTODY;
 }
 
+/*
+ * 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 to
+ * guarantee that the lazy request is promoted by a real scheduler tick. Set
+ * the lazy request first so that the dependency update's IPI can also serve it
+ * on return to user space.
+ */
+static void scx_resched_curr_lazy(struct rq *rq)
+{
+	resched_curr_lazy(rq);
+
+	if (rq->scx.flags & SCX_RQ_CAN_STOP_TICK) {
+		rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
+		if (rq->clock_update_flags < RQCF_UPDATED)
+			update_rq_clock(rq);
+		update_other_load_avgs(rq);
+		sched_update_tick_dependency(rq);
+	}
+}
+
 static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq,
 			      struct scx_dispatch_q *dsq, struct task_struct *p,
 			      u64 enq_flags)
@@ -1577,12 +1597,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
+				scx_resched_curr_lazy(rq);
+		} else {
 			__scx_add_event(sch, SCX_EV_SLICE_DENIED, 1);
+		}
 	}
 }
 
@@ -1672,7 +1696,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 +2410,7 @@ void scx_move_local_task_to_local_dsq(struct scx_sched *sch, struct task_struct
 
 	WARN_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 +3828,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) {
+		/* the slice can't be trusted while bypassing */
+		if (READ_ONCE(curr->scx.slice_expires_lazy) &&
+		    !scx_bypassing(sch, cpu_of(rq)))
+			scx_resched_curr_lazy(rq);
+		else
+			resched_curr(rq);
+	}
 }
 
 #ifdef CONFIG_EXT_GROUP_SCHED
@@ -3921,6 +3951,7 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p)
 		weight = sched_prio_to_weight[p->static_prio - MAX_RT_PRIO];
 
 	p->scx.weight = sched_weight_to_cgroup(weight);
+	p->scx.slice_expires_lazy = sch->ops.flags & SCX_OPS_LAZY_SLICE_EXPIRY;
 
 	if (SCX_HAS_OP(sch, enable))
 		SCX_CALL_OP_TASK(sch, enable, rq, p);
@@ -5371,6 +5402,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 +6915,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 +7235,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 +7371,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) {
@@ -8459,13 +8496,23 @@ static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_r
 {
 	struct rq *rq = cpu_rq(cpu);
 	struct scx_rq *this_scx = &this_rq->scx;
+	struct rq_flags rf;
 	const struct sched_class *cur_class;
 	bool should_wait = false;
 	bool kickable;
-	unsigned long flags;
+	bool preempt, preempt_lazy, wait, immediate;
 
-	raw_spin_rq_lock_irqsave(rq, flags);
+	rq_lock_irqsave(rq, &rf);
 	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);
+	wait = cpumask_test_cpu(cpu, pcpu->cpus_to_wait);
+	/*
+	 * Immediate preemption, waiting and a plain kick take precedence over
+	 * lazy preemption. The lazy request still clears the slice, so all
+	 * accumulated requests are served.
+	 */
+	immediate = preempt || wait || cpumask_test_cpu(cpu, pcpu->cpus_to_kick);
 
 	/*
 	 * During CPU hotplug, a CPU may depend on kicking itself to make
@@ -8479,19 +8526,23 @@ 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);
 
-				if (unlikely(scx_missing_caps(pcpu->sch, cpu, caps)))
+				if (unlikely(scx_missing_caps(pcpu->sch, cpu, caps))) {
 					__scx_add_event(pcpu->sch, SCX_EV_SUB_PREEMPT_DENIED, 1);
-				else if (unlikely(!scx_set_task_slice(rq->curr, 0)))
+					/* degrade to a plain, immediate kick */
+					immediate = true;
+				} else 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);
+			cpumask_clear_cpu(cpu, pcpu->cpus_to_preempt_lazy);
 		}
 
-		if (cpumask_test_cpu(cpu, pcpu->cpus_to_wait)) {
+		if (wait) {
 			if (cur_class == &ext_sched_class) {
 				cpumask_set_cpu(cpu, this_scx->cpus_to_sync);
 				ksyncs[cpu] = rq->scx.kick_sync;
@@ -8500,17 +8551,21 @@ static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_r
 			cpumask_clear_cpu(cpu, pcpu->cpus_to_wait);
 		}
 
-		resched_curr(rq);
+		if (immediate)
+			resched_curr(rq);
+		else
+			scx_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_preempt_lazy);
 		cpumask_clear_cpu(cpu, pcpu->cpus_to_wait);
 	}
 
 	scx_rq_lock_drop(rq);
-	raw_spin_rq_unlock_irqrestore(rq, flags);
+	rq_unlock_irqrestore(rq, &rf);
 
 	return should_wait;
 }
@@ -8566,6 +8621,14 @@ 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; visit the remaining requests which contain lazy
+		 * preemption, see scx_kick_cpu().
+		 */
+		for_each_cpu(cpu, pcpu->cpus_to_preempt_lazy)
+			should_wait |= 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);
@@ -9531,6 +9594,31 @@ __bpf_kfunc bool scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime,
 	return true;
 }
 
+/**
+ * scx_bpf_task_set_slice_expiry - Set task's slice expiry policy
+ * @p: task of interest
+ * @lazy: whether slice expiry should request lazy rescheduling
+ * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
+ *
+ * Choose whether depletion of @p's slice at the scheduler tick requests lazy
+ * or immediate rescheduling. @p must be on the calling scheduler.
+ *
+ * Return %true on success, %false if @p is not on the calling scheduler.
+ */
+__bpf_kfunc bool scx_bpf_task_set_slice_expiry(struct task_struct *p, bool lazy,
+					       const struct bpf_prog_aux *aux)
+{
+	struct scx_sched *sch;
+
+	guard(rcu)();
+	sch = scx_prog_sched(aux);
+	if (unlikely(!sch || !scx_task_on_sched(sch, p)))
+		return false;
+
+	WRITE_ONCE(p->scx.slice_expires_lazy, lazy);
+	return true;
+}
+
 void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)
 {
 	struct scx_sched_pcpu *pcpu;
@@ -9539,6 +9627,16 @@ 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_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 +9662,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);
@@ -9578,12 +9673,15 @@ void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)
 		}
 		cpumask_set_cpu(cpu, pcpu->cpus_to_kick_if_idle);
 	} else {
-		cpumask_set_cpu(cpu, pcpu->cpus_to_kick);
-
+		/* Accumulate requests and resolve their precedence at delivery. */
 		if (flags & SCX_KICK_PREEMPT)
 			cpumask_set_cpu(cpu, pcpu->cpus_to_preempt);
+		if (flags & SCX_KICK_PREEMPT_LAZY)
+			cpumask_set_cpu(cpu, pcpu->cpus_to_preempt_lazy);
 		if (flags & SCX_KICK_WAIT)
 			cpumask_set_cpu(cpu, pcpu->cpus_to_wait);
+		if (!(flags & SCX_KICK_PREEMPT_LAZY))
+			cpumask_set_cpu(cpu, pcpu->cpus_to_kick);
 	}
 
 	if (list_empty(&pcpu->to_kick_node))
@@ -9623,8 +9721,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)
 {
@@ -10691,6 +10790,7 @@ __bpf_kfunc_end_defs();
 BTF_KFUNCS_START(scx_kfunc_ids_any)
 BTF_ID_FLAGS(func, scx_bpf_task_set_slice, KF_IMPLICIT_ARGS | KF_RCU);
 BTF_ID_FLAGS(func, scx_bpf_task_set_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU);
+BTF_ID_FLAGS(func, scx_bpf_task_set_slice_expiry, KF_IMPLICIT_ARGS | KF_RCU);
 BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS)
 BTF_ID_FLAGS(func, scx_bpf_kick_cid, KF_IMPLICIT_ARGS)
 BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued, KF_IMPLICIT_ARGS)
kernel/sched/ext/internal.h +36 -4
index e1eb3a0d456cb..bb527e891c8cd 100644
@@ -215,6 +215,20 @@ 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. When lazy preemption is disabled
+	 * at runtime, the request behaves like an immediate reschedule.
+	 * Rescheduling while bypassing 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 +237,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 +1342,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 +1423,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 insert
+ *            - SCX_KICK_PREEMPT and SCX_KICK_PREEMPT_LAZY kick
  *
  * PERF       control the cid's cpu power/perf management state, currently the
  *            cpufreq target set through scx_bpf_cidperf_set(). Hardware
@@ -1685,6 +1702,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 +1837,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
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 +17 -0
index 6944221f96cc0..9491acb8d059f 100644
@@ -403,6 +403,23 @@ static inline void scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime)
 		p->scx.dsq_vtime = vtime;
 }
 
+/*
+ * v7.4: Add per-task control over whether slice expiry requests lazy or
+ * immediate rescheduling. Older kernels have no equivalent operation, so the
+ * compatibility wrapper is a no-op when the kfunc is unavailable. As with the
+ * setters above, discard the kfunc's authority result for consistency.
+ *
+ * Keep the wrapper until pre-v7.4 kernels fall out of the sched_ext scheduler
+ * support window.
+ */
+bool scx_bpf_task_set_slice_expiry___new(struct task_struct *p, bool lazy) __ksym __weak;
+
+static inline void scx_bpf_task_set_slice_expiry(struct task_struct *p, bool lazy)
+{
+	if (bpf_ksym_exists(scx_bpf_task_set_slice_expiry___new))
+		scx_bpf_task_set_slice_expiry___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 +3 -0
index 63b6b14b19bd4..5d25a29ab9782 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,7 @@
 #define HAVE_SCX_KICK_IDLE
 #define HAVE_SCX_KICK_PREEMPT
 #define HAVE_SCX_KICK_WAIT
+#define HAVE_SCX_KICK_PREEMPT_LAZY
 #define HAVE_SCX_OPI_BEGIN
 #define HAVE_SCX_OPI_NORMAL_BEGIN
 #define HAVE_SCX_OPI_NORMAL_END
@@ -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
index 7268131010de3..6afd4179cce50 100644
@@ -112,6 +112,9 @@ 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_ENQ_WAKEUP __weak;
 #define SCX_ENQ_WAKEUP __SCX_ENQ_WAKEUP
 
@@ -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
index e616326545172..16e500b10fc65 100644
@@ -41,9 +41,11 @@
 	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_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
index d53899764f5ac..d6f38bebd8134 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,7 @@ 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_opi", "SCX_OPI_BEGIN", 0x0LLU },
 	{ "scx_opi", "SCX_OPI_NORMAL_BEGIN", 0x0LLU },
 	{ "scx_opi", "SCX_OPI_NORMAL_END", 0x21LLU },
@@ -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 },
author reviewer
#5 lore raw

Hi Tao,

Quoted 52 lines · Tao Cui

Hi Andrea,

Quoted 24 lines · Andrea Righi

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.

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.

Quoted 4 lines

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.

I tested v6 on sched_ext/for-7.4 in a 4-CPU KVM guest: wakeup latency of a 1ms-period timer task under a scx_simple-based probe scheduler (SCX_OPS_LAZY_SLICE_EXPIRY) with a busy loop on the same CPU.

avg p50 p99
CFS (no scx) 60 59 66
no wake preempt 20919 20982 21002

  SCX_ENQ_PREEMPT     59      58      64
  SCX_ENQ_PREEMPT_LAZY 59     59      64
  CFS (control)       60      59      68

(usecs, 2000 iterations each)

Lazy and immediate preemption are identical here and on par with CFS, i.e. no wakeup latency regression in the common case, matching the intended semantics. SCX_OPS_LAZY_SLICE_EXPIRY also cut the context switch rate of 6 busy hogs from ~1000/s (CFS) to ~215/s. Repeated cgroup knob churn (cpu.weight/cpu.idle) ran clean, no warnings in dmesg.

Quoted 4 lines

Overall the series looks well-behaved in these scenarios. Thanks for working on this!

Thanks for testing and sharing the results!

-Andrea

author reviewer
#6 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 expiry default in both directions through scx_bpf_task_set_slice_expiry(), 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.

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  | 191 +++++
 tools/testing/selftests/sched_ext/kick.c      | 655 ++++++++++++++++++
 .../selftests/sched_ext/nohz_tick.bpf.c       |  62 +-
 tools/testing/selftests/sched_ext/nohz_tick.c | 280 +++++++-
 5 files changed, 1162 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
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 +191 -0

191 lines not on this page · raw message

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

655 lines not on this page · raw message

tools/testing/selftests/sched_ext/nohz_tick.bpf.c +55 -7

95 lines not on this page · raw message

tools/testing/selftests/sched_ext/nohz_tick.c +260 -20

386 lines not on this page · raw message

Signature · 1 line
2.55.0
reviewer
#7 lore raw

Re: [PATCH 2/2] selftests/sched_ext: Add lazy preemption tests

Hello, Andrea.

This is an AI review. The series was built and the tests pass under vng here in lazy and full modes, with nohz_full on 2-3 and on 1-3. Three things worth fixing and some leftovers.

Quoted 1 line · Andrea Righi

+enum kick_scenario {

exit_test.h and hotplug_test.h share such enums between the BPF and user sides. The same would work here and for nohz_phase. run_invalid() and is_trace_scenario() depend on the two copies staying in order.

tools/testing/selftests/sched_ext/kick.c line 21

Quoted 3 lines
tools/testing/selftests/sched_ext/kick.c new file +3 -0
+#ifdef HAVE_GENHDR
+#include "autoconf.h"
+#endif

Nothing uses CONFIG_ in kick.c anymore.

Quoted 1 line

+static struct victim spawn_victim(int cpu)

Same as spawn_gated_worker() in nohz_tick.c except for sched_setscheduler(). util.c could hold one copy.

tools/testing/selftests/sched_ext/kick.c line 341

Quoted 3 lines
tools/testing/selftests/sched_ext/kick.c new file +3 -0
+	file = fopen("/sys/kernel/debug/sched/preempt", "r");
+	if (!file)
+		return -1;

That file only exists with PREEMPT_DYNAMIC, and needs debugfs mounted. Every static kernel takes the INFO path and passes without comparing, including static PREEMPT_LAZY, which is the default on arm64, riscv, powerpc, s390 and loongarch where PREEMPT_DYNAMIC defaults off. uname can't tell either, as PREEMPT_LAZY selects PREEMPT_BUILD. /proc/config.gz covers static kernels when it's there. Otherwise skipping would be more honest than passing.

tools/testing/selftests/sched_ext/kick.c line 497

Quoted 2 lines
tools/testing/selftests/sched_ext/kick.c new file +2 -0
+	SCX_EQ(both.resched_tif, immediate.resched_tif);
+	SCX_EQ(lazy_wait.resched_tif, immediate.resched_tif);

Outside lazy mode the lazy TIF is the immediate one, so these and the override checks in run_tick() hold whatever kick_one_cpu() decided. Maybe skip or note it when the mode isn't lazy.

tools/testing/selftests/sched_ext/nohz_tick.bpf.c s32 BPF_STRUCT_OPS(nohz_tick_select_cpu, struct task_struct *p, s32 prev_cpu, · line 54

Quoted 4 lines
tools/testing/selftests/sched_ext/nohz_tick.bpf.c +4 -0
+	case NOHZ_PHASE_LAZY_ENQ:
+	case NOHZ_PHASE_LAZY_KICK:
+		dsq_id = SCX_DSQ_LOCAL;
+		slice = p->pid == victim_pid ? SCX_SLICE_INF : SCX_SLICE_DFL;

The victim gets an infinite slice on every enqueue, including the one after the lazy preemption puts it back. In the kick phase the challenger is stopped first; if its slice runs out before the SIGKILL lands, it's re-enqueued behind the victim and never runs again, waitpid() blocks, and the 5s watchdog aborts the scheduler, which then fails the test at check_exit. The enqueue phase is safe as the challenger re-inserts at the head. Stopping the victim first avoids it. ENQ_BOTH in kick.c has the same shape with the global DSQ and the default 30s watchdog, a stall rather than a failure there.

Quoted 1 line

+static bool wait_for_tick_stop(const u64 *counter, int timeout_ms)

Neither this nor wait_for_counter() checks uei.kind, so a watchdog abort mid-phase reports as no progress.

tools/testing/selftests/sched_ext/nohz_tick.c static enum scx_test_status run(void *ctx_ptr) · line 496

Quoted 4 lines
tools/testing/selftests/sched_ext/nohz_tick.c +4 -0
+	if (!start_gated_worker(&challenger) ||
+	    !wait_for_counter(&skel->bss->nr_lazy_enq_running, 1,
+			      PHASE_TIMEOUT_MS)) {
+		SCX_ERR("Lazy enqueue made no progress on CPU %d", ctx->test_cpu);

This passes even without the tick dependency restore. sched_tick_remote() runs task_tick_scx() once a second on the tick-stopped CPU, and as nohz_tick_ops doesn't set SCX_OPS_LAZY_SLICE_EXPIRY that path does resched_curr() with an IPI, so the challenger runs within a second either way. Setting the flag on nohz_tick_ops keeps the remote tick lazy, so a missing restore shows up as the timeout. Both phases only test anything in lazy mode.

Thanks.

Signature · 1 line
tejun
author reviewer
#8 lore raw

Re: [PATCH 2/2] selftests/sched_ext: Add lazy preemption tests

Hi Tejun,

Quoted 61 lines · Tejun Heo

Hello, Andrea.

This is an AI review. The series was built and the tests pass under vng here in lazy and full modes, with nohz_full on 2-3 and on 1-3. Three things worth fixing and some leftovers.

Quoted 1 line · Andrea Righi

+enum kick_scenario {

exit_test.h and hotplug_test.h share such enums between the BPF and user sides. The same would work here and for nohz_phase. run_invalid() and is_trace_scenario() depend on the two copies staying in order.

tools/testing/selftests/sched_ext/kick.c line 21

Quoted 3 lines
tools/testing/selftests/sched_ext/kick.c new file +3 -0
+#ifdef HAVE_GENHDR
+#include "autoconf.h"
+#endif

Nothing uses CONFIG_ in kick.c anymore.

Quoted 1 line

+static struct victim spawn_victim(int cpu)

Same as spawn_gated_worker() in nohz_tick.c except for sched_setscheduler(). util.c could hold one copy.

tools/testing/selftests/sched_ext/kick.c line 341

Quoted 3 lines
tools/testing/selftests/sched_ext/kick.c new file +3 -0
+	file = fopen("/sys/kernel/debug/sched/preempt", "r");
+	if (!file)
+		return -1;

That file only exists with PREEMPT_DYNAMIC, and needs debugfs mounted. Every static kernel takes the INFO path and passes without comparing, including static PREEMPT_LAZY, which is the default on arm64, riscv, powerpc, s390 and loongarch where PREEMPT_DYNAMIC defaults off. uname can't tell either, as PREEMPT_LAZY selects PREEMPT_BUILD. /proc/config.gz covers static kernels when it's there. Otherwise skipping would be more honest than passing.

tools/testing/selftests/sched_ext/kick.c line 497

Quoted 2 lines
tools/testing/selftests/sched_ext/kick.c new file +2 -0
+	SCX_EQ(both.resched_tif, immediate.resched_tif);
+	SCX_EQ(lazy_wait.resched_tif, immediate.resched_tif);

Outside lazy mode the lazy TIF is the immediate one, so these and the override checks in run_tick() hold whatever kick_one_cpu() decided. Maybe skip or note it when the mode isn't lazy.

tools/testing/selftests/sched_ext/nohz_tick.bpf.c s32 BPF_STRUCT_OPS(nohz_tick_select_cpu, struct task_struct *p, s32 prev_cpu, · line 54

Quoted 4 lines
tools/testing/selftests/sched_ext/nohz_tick.bpf.c +4 -0
+	case NOHZ_PHASE_LAZY_ENQ:
+	case NOHZ_PHASE_LAZY_KICK:
+		dsq_id = SCX_DSQ_LOCAL;
+		slice = p->pid == victim_pid ? SCX_SLICE_INF : SCX_SLICE_DFL;

The victim gets an infinite slice on every enqueue, including the one after the lazy preemption puts it back. In the kick phase the challenger is stopped first; if its slice runs out before the SIGKILL lands, it's re-enqueued behind the victim and never runs again, waitpid() blocks, and the 5s watchdog aborts the scheduler, which then fails the test at check_exit. The enqueue phase is safe as the challenger re-inserts at the head. Stopping the victim first avoids it. ENQ_BOTH in kick.c has the same shape with the global DSQ and the default 30s watchdog, a stall rather than a failure there.

Quoted 1 line

+static bool wait_for_tick_stop(const u64 *counter, int timeout_ms)

Neither this nor wait_for_counter() checks uei.kind, so a watchdog abort mid-phase reports as no progress.

Quoted 12 lines

tools/testing/selftests/sched_ext/nohz_tick.c static enum scx_test_status run(void *ctx_ptr) · line 496

Quoted 4 lines
tools/testing/selftests/sched_ext/nohz_tick.c +4 -0
+	if (!start_gated_worker(&challenger) ||
+	    !wait_for_counter(&skel->bss->nr_lazy_enq_running, 1,
+			      PHASE_TIMEOUT_MS)) {
+		SCX_ERR("Lazy enqueue made no progress on CPU %d", ctx->test_cpu);

This passes even without the tick dependency restore. sched_tick_remote() runs task_tick_scx() once a second on the tick-stopped CPU, and as nohz_tick_ops doesn't set SCX_OPS_LAZY_SLICE_EXPIRY that path does resched_curr() with an IPI, so the challenger runs within a second either way. Setting the flag on nohz_tick_ops keeps the remote tick lazy, so a missing restore shows up as the timeout. Both phases only test anything in lazy mode.

Agree to all of the above. I'll send a new version with all of these fixed.

Thanks,
-Andrea