Skip to contentSkip to conversation

sched_ext: Add lazy preemption support

PATCH Superseded Andrea Righi 3 participants 7 messages Sep 15 – Sep 16

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH v5 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 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                            | 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          | 617 ++++++++++++++++++++++
 tools/testing/selftests/sched_ext/nohz_tick.bpf.c |  56 +-
 tools/testing/selftests/sched_ext/nohz_tick.c     | 249 ++++++++-
 14 files changed, 1282 insertions(+), 67 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/kick.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/kick.c
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.

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
index 8de69843c2150..685f1846aa386 100644
@@ -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
index e1eb3a0d456cb..d73b10d692d21 100644
@@ -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
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 +13 -0
index 6944221f96cc0..4a9bf1a4bb4a8 100644
@@ -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
index 63b6b14b19bd4..f1554c22071ff 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
@@ -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
index 7268131010de3..2cec24beb2d80 100644
@@ -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
index e616326545172..dd762ee0ddb36 100644
@@ -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
index d53899764f5ac..672c02a497d14 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 },
@@ -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
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] sched_ext: redundant idle kick processing for lazy preemptions --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] sched_ext: redundant idle kick processing for lazy preemptions
--

commit 42fa214a424ea8999256e25137769c2bfce81537
Author: Andrea Righi <[email protected]>
sched_ext: Add lazy preemption support

This commit adds lazy preemption support to sched_ext, allowing BPF schedulers to request deferred rescheduling. It introduces new enqueue and kick flags for lazy preemption, and updates the task slice expiry logic.

Quoted 4 lines
kernel/sched/ext/ext.c +0 -0
index f875c50f00bbe..819995667cc26 100644

[ ... ]

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

Quoted 12 lines
kernel/sched/ext/ext.c +8 -0
@@ -8574,6 +8626,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);
+

[Severity: Low]
Does this loop in kick_cpus_irq_workfn() intentionally skip clearing cpus_to_kick_if_idle for the CPUs it processes?

If a BPF scheduler issues both an idle kick and a lazy preemption kick on the same target within a narrow window, but without a non-lazy kick, the target CPU is serviced by this loop.

Because this loop fails to clear cpus_to_kick_if_idle for the visited CPU, the subsequent idle loop immediately below it will redundantly lock and evaluate the same CPU again.

Could this cause a minor performance inefficiency due to the redundant evaluation, and should cpumask_clear_cpu(cpu, pcpu->cpus_to_kick_if_idle) be added here as well?

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

}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw
Quoted 38 lines · On Tue, Sep 15, 2026 at 07:57:52PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] sched_ext: redundant idle kick processing for lazy preemptions
--

commit 42fa214a424ea8999256e25137769c2bfce81537
Author: Andrea Righi <[email protected]>
sched_ext: Add lazy preemption support

This commit adds lazy preemption support to sched_ext, allowing BPF schedulers to request deferred rescheduling. It introduces new enqueue and kick flags for lazy preemption, and updates the task slice expiry logic.

Quoted 4 lines
kernel/sched/ext/ext.c +0 -0
index f875c50f00bbe..819995667cc26 100644

[ ... ]

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

Quoted 12 lines
kernel/sched/ext/ext.c +8 -0
@@ -8574,6 +8626,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);
+

[Severity: Low]
Does this loop in kick_cpus_irq_workfn() intentionally skip clearing cpus_to_kick_if_idle for the CPUs it processes?

If a BPF scheduler issues both an idle kick and a lazy preemption kick on the same target within a narrow window, but without a non-lazy kick, the target CPU is serviced by this loop.

Quoted 7 lines

Because this loop fails to clear cpus_to_kick_if_idle for the visited CPU, the subsequent idle loop immediately below it will redundantly lock and evaluate the same CPU again.

Could this cause a minor performance inefficiency due to the redundant evaluation, and should cpumask_clear_cpu(cpu, pcpu->cpus_to_kick_if_idle) be added here as well?

This looks like a false positive. SCX_KICK_IDLE and SCX_KICK_PREEMPT_LAZY may accumulate from separate calls. Processing the lazy request does not always satisfy the idle request: if the CPU is in SCX_RQ_IN_DISPATCH, the subsequent idle pass intentionally upgrades it to an immediate reschedule. Unconditionally clearing cpus_to_kick_if_idle in the lazy loop could lose that request.

-Andrea

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

}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#4 lore raw
In reply to Andrea Righi

Hello, Andrea.

This is an AI review. The series was built and the selftests pass under vng here, and the kick and tick paths were traced and look correct. A few things to fix up.

include/linux/sched/ext.h struct sched_ext_entity { · line 258

Quoted 3 lines · Andrea Righi
include/linux/sched/ext.h +3 -0
+	 * modified afterwards with scx_bpf_task_set_slice_expiry().
+	 */
+	bool			slice_expires_lazy;

This adds 8 bytes to task_struct for a bool. Put it next to @disallow, there are three bytes of padding after it.

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

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

Lazy is a runtime mode under PREEMPT_DYNAMIC, so "kernels without lazy preemption" isn't right. And what forces immediate rescheduling is bypassing, not disabling.

kernel/sched/ext/ext.c static void call_task_dequeue(struct scx_sched *sch, struct rq *rq, · line 1534

Quoted 12 lines
kernel/sched/ext/ext.c +12 -0
+static void scx_resched_curr_lazy(struct rq *rq)
+{
+	if (rq->scx.flags & SCX_RQ_CAN_STOP_TICK) {
+		rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
+		if (rq->clock_update_flags < RQCF_UPDATED)
+			update_rq_clock(rq);
+		update_other_load_avgs(rq);
+		sched_update_tick_dependency(rq);
+	}
+
+	resched_curr_lazy(rq);
+}

Setting the TIF bit before restoring the tick dependency would let the tick IPI's return to user space serve the reschedule instead of waiting for the next tick. Not critical.

kernel/sched/ext/ext.c static bool bpf_scx_is_valid_access(int off, int size, · line 7991

Quoted 2 lines
kernel/sched/ext/ext.c +1 -1
-/* common to both forms: only scx.disallow is writable */
+/* common to both forms: only the fields below are writable */

Nothing else became writable. Drop the hunk.

kernel/sched/ext/ext.c static bool kick_one_cpu(s32 cpu, struct scx_sched_pcpu *pcpu, struct rq *this_r · line 8502

Quoted 2 lines
kernel/sched/ext/ext.c +1 -1
-	raw_spin_rq_lock_irqsave(rq, flags);
+	rq_lock_irqsave(rq, &rf);

Please explain this in the description. Pinning clears RQCF_UPDATED, which is what makes the clock_update_flags test in scx_resched_curr_lazy() do anything on this path.

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

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;
+	}

Unknown flags were silently ignored before, so this is a behavior change and the description should say so. The SCX_KICK_IDLE check already existed, it just returns early now.

Quoted 2 lines

+ * - SCX_ENQ_PREEMPT and SCX_ENQ_PREEMPT_LAZY inserts
+ * - SCX_KICK_PREEMPT and SCX_KICK_PREEMPT_LAZY kicks

insert, kick.

Quoted 4 lines

+/*
+ * v7.4: scx_bpf_task_set_slice_expiry() added to enforce sub-scheduler task
+ * ownership. Preserve until v7.7.
+ */

This is copied from the block above and doesn't apply. The kfunc is new with the feature, there was no field write to replace.

Quoted 2 lines

+#define HAVE_SCX_KICK_PREEMPT_LAZY
#define HAVE_SCX_KICK_WAIT

The autogen headers follow definition order, so SCX_KICK_PREEMPT_LAZY goes after SCX_KICK_WAIT in all four. Please regenerate them instead of editing.

Thanks.

--
tejun

author reviewer
#5 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 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.

Cc Cheng-Yang Chou <[email protected]>
---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 tools/testing/selftests/sched_ext/kick.bpf.c  | 182 ++++++
 tools/testing/selftests/sched_ext/kick.c      | 617 ++++++++++++++++++
 .../selftests/sched_ext/nohz_tick.bpf.c       |  56 +-
 tools/testing/selftests/sched_ext/nohz_tick.c | 249 ++++++-
 5 files changed, 1078 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 +182 -0

182 lines not on this page · raw message

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

617 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
reviewer
#6 lore raw

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

Hello, Andrea.

This is an AI review. The tests pass here under vng in lazy, full and none modes. The timing below was measured with the target CPU on nohz_full.

tools/testing/selftests/sched_ext/kick.bpf.c line 168

Quoted 5 lines · Andrea Righi
tools/testing/selftests/sched_ext/kick.bpf.c new file +5 -0
+void BPF_STRUCT_OPS(kick_stopping, struct task_struct *p, bool runnable)
+{
+	if (p->pid == victim_pid && state == KICK_STATE_RESCHED)
+		state = KICK_STATE_DONE;
+}

In the kick and tick scenarios the victim is the only runnable task on the target CPU, so when its slice is cleared the kernel keeps running it with a refilled slice and ops.stopping() is never called. DONE only arrives when some other task pinned to that CPU wakes up. With the target CPU on nohz_full here, the first such wakeup was a kworker 2.86s after the victim started, against the 3s wait. Without nohz_full the scenarios still spend about half a second each waiting for one. SCX_OPS_ENQ_LAST with the SCX_ENQ_LAST kick as in nohz_tick.bpf.c would put the victim through ops.stopping() on its own.

tools/testing/selftests/sched_ext/kick.bpf.c line 145

Quoted 8 lines
tools/testing/selftests/sched_ext/kick.bpf.c new file +8 -0
+SEC("fexit/__resched_curr")
+int BPF_PROG(kick_need_resched, struct rq *rq, int tif)
+{
+	struct task_struct *task = BPF_CORE_READ(rq, curr);
+
+	if (!task || BPF_CORE_READ(task, pid) != victim_pid ||
+	    BPF_CORE_READ(rq, cpu) != target_cpu || state != KICK_STATE_QUEUED)
+		return 0;

This latches the first resched of the victim whatever the cause. A higher class wakeup on the target CPU inside the window, which is the whole 20ms slice in TICK_EXPIRY, records a full slice and fails observation_valid(). Skipping observations with a non-zero slice would be more robust.

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

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

DONE is set from ops.stopping() before the context switch and the balance callback runs after it, so this read can race the increment. Poll for it.

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

Quoted 7 lines
tools/testing/selftests/sched_ext/kick.c new file +7 -0
+	file = fopen("/sys/kernel/debug/sched/preempt", "r");
+	if (!file) {
+#if defined(CONFIG_PREEMPT_LAZY) && !defined(CONFIG_PREEMPT_DYNAMIC)
+		return 1;
+#else
+		return -1;
+#endif

This reflects the build tree, not the running kernel. Also, none and voluntary return -1 and skip the comparison, but the TIFs must match in those modes too.

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

Quoted 3 lines
tools/testing/selftests/sched_ext/kick.c new file +3 -0
+	skel->rodata->scenario = scenario;
+	if (kick__load(skel))
+		goto out;

If __resched_curr() isn't in BTF, this fails too and kick_invalid FAILs along with the other four although it doesn't use the tracer. Skipping when the target is missing would be better.

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

Quoted 7 lines
tools/testing/selftests/sched_ext/nohz_tick.c +7 -0
+	if (!start_gated_worker(&victim) ||
+	    !wait_for_counter(&skel->bss->nr_lazy_victim_running, 1,
+			      PHASE_TIMEOUT_MS)) {
+		SCX_ERR("Lazy-enqueue victim was not scheduled");
+		goto out;
+	}
+	usleep(100000);

Neither lazy phase checks that the tick actually stopped before the request, so they pass with the tick running the whole time.

Quoted 4 lines

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.

kick.c is new, nothing is kept. The controller mask and timeout changes to nohz_tick aren't mentioned either.

Thanks.

--
tejun