[PATCH 06/12] sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors

From: Andrea Righi

Date: Thu Jul 02 2026 - 13:25:34 EST


With proxy-exec, pick_next_task() can return a task with blocked_on set
(a proxy donor); put_prev_set_next_task() then calls set_next_task_scx()
on this "ghost" task, which fires ops.running(). However, the task never
actually runs.

If we simply short-circuit set_next_task_scx() for blocked tasks, we
break DSQ bookkeeping. If we only skip ops.running(), we create an
ops.enqueue() -> ops.stopping() pair without running, because
ops.stopping() is still called in put_prev_task_scx().

Fix this by introducing a new flag SCX_TASK_IS_RUNNING to track whether
ops.running() was actually called. Skip ops.running() for blocked tasks,
and only call ops.stopping() if SCX_TASK_IS_RUNNING is set. This ensures
that running and stopping callbacks are perfectly paired even when a
blocked task is picked as a proxy donor.

Signed-off-by: Andrea Righi <arighi@xxxxxxxxxx>
---
include/linux/sched/ext.h | 2 ++
kernel/sched/core.c | 2 +-
kernel/sched/ext/ext.c | 14 +++++++++++---
kernel/sched/ext/ext.h | 6 ++++++
4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
index 75cb8b119fb79..e599bb86f8acd 100644
--- a/include/linux/sched/ext.h
+++ b/include/linux/sched/ext.h
@@ -102,6 +102,8 @@ enum scx_ent_flags {
SCX_TASK_SUB_INIT = 1 << 4, /* task being initialized for a sub sched */
SCX_TASK_IMMED = 1 << 5, /* task is on local DSQ with %SCX_ENQ_IMMED */

+ SCX_TASK_IS_RUNNING = 1 << 6, /* ops.running() has been called */
+
/*
* Bits 8 to 10 are used to carry task state:
*
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 2e4c98b4ea6b0..6aedb26c08ee7 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -7189,7 +7189,7 @@ static void __sched notrace __schedule(int sched_mode)
* sched_ext tracks curr/donor itself; re-entering set_next_task_scx
* here dispatches through a stale/NULL BPF ops vtable.
*/
- if (donor->sched_class != &ext_sched_class) {
+ if (!is_ext_class(donor)) {
donor->sched_class->put_prev_task(rq, donor, donor);
donor->sched_class->set_next_task(rq, donor, true);
}
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 189ba9c42043a..b0ec579e3a3ef 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -1985,9 +1985,11 @@ static bool dequeue_task_scx(struct rq *rq, struct task_struct *p, int core_deq_
* information meaningful to the BPF scheduler and can be suppressed by
* skipping the callbacks if the task is !QUEUED.
*/
- if (SCX_HAS_OP(sch, stopping) && task_current(rq, p)) {
+ if (SCX_HAS_OP(sch, stopping) && task_current(rq, p) &&
+ (p->scx.flags & SCX_TASK_IS_RUNNING)) {
update_curr_scx(rq);
SCX_CALL_OP_TASK(sch, stopping, rq, p, false);
+ p->scx.flags &= ~SCX_TASK_IS_RUNNING;
}

if (SCX_HAS_OP(sch, quiescent) && !task_on_rq_migrating(p))
@@ -2725,8 +2727,11 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
p->se.exec_start = rq_clock_task(rq);

/* see dequeue_task_scx() on why we skip when !QUEUED */
- if (SCX_HAS_OP(sch, running) && (p->scx.flags & SCX_TASK_QUEUED))
+ if (SCX_HAS_OP(sch, running) && (p->scx.flags & SCX_TASK_QUEUED) &&
+ !task_is_blocked(p)) {
SCX_CALL_OP_TASK(sch, running, rq, p);
+ p->scx.flags |= SCX_TASK_IS_RUNNING;
+ }

clr_task_runnable(p, true);

@@ -2815,8 +2820,11 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,
update_curr_scx(rq);

/* see dequeue_task_scx() on why we skip when !QUEUED */
- if (SCX_HAS_OP(sch, stopping) && (p->scx.flags & SCX_TASK_QUEUED))
+ if (SCX_HAS_OP(sch, stopping) && (p->scx.flags & SCX_TASK_QUEUED) &&
+ (p->scx.flags & SCX_TASK_IS_RUNNING)) {
SCX_CALL_OP_TASK(sch, stopping, rq, p, true);
+ p->scx.flags &= ~SCX_TASK_IS_RUNNING;
+ }

if (p->scx.flags & SCX_TASK_QUEUED) {
set_task_runnable(rq, p);
diff --git a/kernel/sched/ext/ext.h b/kernel/sched/ext/ext.h
index 0b7fc46aee08c..c7fa4d06ac7d3 100644
--- a/kernel/sched/ext/ext.h
+++ b/kernel/sched/ext/ext.h
@@ -35,6 +35,11 @@ static inline bool task_on_scx(const struct task_struct *p)
return scx_enabled() && p->sched_class == &ext_sched_class;
}

+static inline bool is_ext_class(const struct task_struct *p)
+{
+ return p->sched_class == &ext_sched_class;
+}
+
#ifdef CONFIG_SCHED_CORE
bool scx_prio_less(const struct task_struct *a, const struct task_struct *b,
bool in_fi);
@@ -53,6 +58,7 @@ static inline void scx_rq_activate(struct rq *rq) {}
static inline void scx_rq_deactivate(struct rq *rq) {}
static inline int scx_check_setscheduler(struct task_struct *p, int policy) { return 0; }
static inline bool task_on_scx(const struct task_struct *p) { return false; }
+static inline bool is_ext_class(const struct task_struct *p) { return false; }
static inline bool scx_allow_ttwu_queue(const struct task_struct *p) { return true; }
static inline void init_sched_ext_class(void) {}

--
2.55.0