* [PATCH 1/3] sched_ext: Fix ops_state and ops.dequeue() locking races in dispatch
2026-11-23 17:10 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix ops_state and ops.dequeue() locking in dispatch Kuba Piecuch
@ 2026-11-23 17:10 ` Kuba Piecuch
2026-11-23 17:10 ` [PATCH 2/3] sched_ext: Remove scx_wait_dispatching() Kuba Piecuch
2026-11-23 17:10 ` [PATCH 3/3] selftests/sched_ext: Test that ops.dequeue() runs with the task's rq locked Kuba Piecuch
2 siblings, 0 replies; 4+ messages in thread
From: Kuba Piecuch @ 2026-11-23 17:10 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, fangqiurong, sched-ext, linux-kernel
When finish_dispatch() inserts a task into a non-local DSQ, the task
becomes visible on the DSQ while still SCX_OPSS_DISPATCHING: ops_state
is only cleared after the DSQ is unlocked and, for terminal DSQs, after
ops.dequeue() returns. A CPU that takes the task off the DSQ in that
window can requeue it, and the late store then overwrites the new
QUEUED state, so the task is never dispatched again. Commit 7de9a6fb44ea
("sched_ext: Wait for SCX_OPSS_DISPATCHING before reenqueueing a task")
and commit 2935062be771 ("sched_ext: Wait for SCX_OPSS_DISPATCHING before
moving tasks off DSQs") work around this by spinning wherever a task is
taken off a DSQ.
Also, for a remote task dispatched to SCX_DSQ_GLOBAL (or to a destroyed
user DSQ, which falls back to the global DSQ), scx_dispatch_enqueue()
runs with only the dispatching CPU's rq locked. task_leave_custody() then
does an unlocked RMW of p->scx.flags, which can lose concurrent updates
made under the task's rq lock (e.g. SCX_TASK_SUB_INIT). ops.dequeue()
also runs with scx_locked_rq() set to the wrong rq, so for example
scx_bpf_cpuperf_set(scx_bpf_task_cpu(p), ...) aborts the scheduler with
"Invalid target CPU".
Fix both:
- Clear ops_state while the DSQ (or rq) is still locked and before
ops.dequeue().
- Insert a remote task into the global DSQ under its rq lock, using the
holding_cpu handoff from dispatch_to_local_dsq(), factored out into
release_dispatching_and_lock_task_rq(). scx_dispatch_enqueue() now
returns false on a destroyed user DSQ if the caller doesn't hold the
task's rq lock, so finish_dispatch() can take the same path.
- Only set SCX_TASK_IN_CUSTODY with the task's rq locked, and add
lockdep assertions where p->scx.flags is updated and ops.dequeue() is
called. Drop the now impossible QUEUED && !IN_CUSTODY retry from
ops_dequeue() and the scx_bypass() comment about unlocked
p->scx.flags writes by an in-flight dispatch.
Fixes: ebf1ccff79c4 ("sched_ext: Fix ops.dequeue() semantics")
Fixes: cb86607ada73 ("sched_ext: Don't run ops.dequeue() with a DSQ lock held")
Cc: stable@vger.kernel.org # v7.1+
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
kernel/sched/ext/ext.c | 237 +++++++++++++++++++++++++----------------
1 file changed, 145 insertions(+), 92 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index e8c30a21e03e..6d8483fb8304 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -1778,6 +1778,8 @@ 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)
{
+ lockdep_assert_rq_held(rq);
+
if (task_leave_custody(p) && SCX_HAS_OP(sch, dequeue))
SCX_CALL_OP_TASK(sch, dequeue, rq, p, 0);
@@ -1852,11 +1854,22 @@ static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq,
}
}
-static void scx_dispatch_enqueue(struct scx_sched *sch, struct rq *rq,
+/*
+ * Insert @p into @dsq with @rq locked. Unless finish_dispatch() is inserting a
+ * remote task into a user DSQ, @rq must be @p's rq.
+ *
+ * If @dsq was destroyed and @rq is not @p's rq, leave @p untouched and return
+ * %false. Falling back to the global DSQ ends custody and requires @p's rq
+ * lock, so the caller must switch to @p's rq lock and fall back there. Return
+ * %true otherwise.
+ */
+static bool scx_dispatch_enqueue(struct scx_sched *sch, struct rq *rq,
struct scx_dispatch_q *dsq, struct task_struct *p,
u64 slice, u64 vtime, u64 enq_flags)
{
- bool is_rq_owned = dsq_is_rq_owned(dsq);
+ bool is_rq_owned = dsq_is_rq_owned(dsq), call_dequeue = false;
+
+ lockdep_assert_rq_held(rq);
if (dsq->id == SCX_DSQ_LOCAL)
dsq = scx_resolve_local_dsq(sch, rq, p, &enq_flags);
@@ -1871,8 +1884,10 @@ static void scx_dispatch_enqueue(struct scx_sched *sch, struct rq *rq,
if (unlikely(dsq->id == SCX_DSQ_INVALID)) {
scx_error(sch, "attempting to dispatch to a destroyed dsq");
- /* fall back to the global dsq */
raw_spin_unlock(&dsq->lock);
+ if (rq != task_rq(p))
+ return false;
+ /* fall back to the global dsq */
dsq = find_global_dsq(sch, task_cpu(p));
raw_spin_lock(&dsq->lock);
}
@@ -1960,45 +1975,49 @@ static void scx_dispatch_enqueue(struct scx_sched *sch, struct rq *rq,
p->scx.dsq = dsq;
/*
- * Update custody and call ops.dequeue() before clearing ops_state:
- * once ops_state is cleared, waiters in ops_dequeue() can proceed
- * and dequeue_task_scx() will RMW p->scx.flags. If we clear
- * ops_state first, both sides would modify p->scx.flags
- * concurrently in a non-atomic way.
+ * Global and bypass DSQs are terminal - the task leaves the scheduler's
+ * custody, so ops.dequeue() fires. A non-terminal DSQ keeps the task in
+ * custody. The custody transfer happens under @dsq->lock so that later
+ * consumers see the flag clear; the callback runs after @dsq->lock is
+ * dropped because it may lock a DSQ itself.
+ *
+ * @p->scx.flags is protected by @p's rq lock. finish_dispatch() may
+ * insert a remote task without it but only into a user DSQ and only
+ * with %SCX_TASK_IN_CUSTODY already set, so @p->scx.flags is updated
+ * only when @p's rq is locked.
*/
- if (is_rq_owned) {
- rq_owned_post_enq(sch, rq, dsq, p, enq_flags);
- } else {
- bool call_dequeue = false;
-
- /*
- * Global and bypass DSQs are terminal - the task leaves the
- * scheduler's custody, so ops.dequeue() fires. It can run
- * without @p's rq lock (finish_dispatch() passes the dispatch
- * rq); that's safe because dequeue_task_scx() waits on
- * SCX_OPSS_DISPATCHING (see the ops_state note above) and so
- * can't race it. A non-terminal DSQ keeps the task in custody.
- * The custody transfer happens under @dsq->lock so that later
- * consumers see the flag clear; the callback runs after
- * @dsq->lock is dropped because it may lock a DSQ itself.
- */
- if (dsq->id == SCX_DSQ_GLOBAL || dsq->id == SCX_DSQ_BYPASS)
+ if (!is_rq_owned) {
+ if (dsq->id == SCX_DSQ_GLOBAL || dsq->id == SCX_DSQ_BYPASS) {
+ lockdep_assert_rq_held(task_rq(p));
call_dequeue = task_leave_custody(p);
- else
+ } else if (rq == task_rq(p)) {
p->scx.flags |= SCX_TASK_IN_CUSTODY;
-
- raw_spin_unlock(&dsq->lock);
-
- if (call_dequeue && SCX_HAS_OP(sch, dequeue))
- SCX_CALL_OP_TASK(sch, dequeue, rq, p, 0);
+ }
}
/*
* We're transitioning out of QUEUEING or DISPATCHING. store_release to
* match waiters' load_acquire.
+ *
+ * Do it while @dsq is still locked and before ops.dequeue() so that @p
+ * is never visible on a DSQ while still owned by the dispatcher.
+ * Anyone who finds @p on @dsq can take it off without waiting on
+ * DISPATCHING. Updating @p->scx.flags and calling ops.dequeue() after
+ * the transition is safe as both are done under @p's rq lock.
*/
if (enq_flags & SCX_ENQ_CLEAR_OPSS)
atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE);
+
+ if (is_rq_owned) {
+ rq_owned_post_enq(sch, rq, dsq, p, enq_flags);
+ } else {
+ raw_spin_unlock(&dsq->lock);
+
+ if (call_dequeue && SCX_HAS_OP(sch, dequeue))
+ SCX_CALL_OP_TASK(sch, dequeue, rq, p, 0);
+ }
+
+ return true;
}
void scx_task_unlink_from_dsq(struct task_struct *p, struct scx_dispatch_q *dsq)
@@ -2528,7 +2547,6 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
*/
clr_task_runnable(p, false);
-retry:
/* acquire ensures that we see the preceding updates on QUEUED */
opss = atomic_long_read_acquire(&p->scx.ops_state);
@@ -2542,20 +2560,6 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
*/
BUG();
case SCX_OPSS_QUEUED:
- /*
- * A queued task must always be in BPF scheduler's custody. If
- * SCX_TASK_IN_CUSTODY is clear, finish_dispatch() on another
- * CPU has already passed task_leave_custody() (which clears the
- * flag), but has not yet written SCX_OPSS_NONE. That final
- * store does not require this rq's lock, so retrying with
- * cpu_relax() is bounded: we will observe NONE (or DISPATCHING,
- * handled by the fallthrough) on a subsequent iteration.
- */
- if (unlikely(!(READ_ONCE(p->scx.flags) & SCX_TASK_IN_CUSTODY))) {
- cpu_relax();
- goto retry;
- }
-
if (atomic_long_try_cmpxchg(&p->scx.ops_state, &opss,
SCX_OPSS_NONE))
break;
@@ -2580,18 +2584,13 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
}
/*
- * Call ops.dequeue() if the task is still in BPF custody.
+ * Call ops.dequeue() if @p is still in BPF custody. @p can be in
+ * %SCX_OPSS_NONE with %SCX_TASK_IN_CUSTODY set in two cases:
*
- * The code that clears ops_state to %SCX_OPSS_NONE does not always
- * clear %SCX_TASK_IN_CUSTODY: in dispatch_to_local_dsq(), when
- * we're moving a task that was in %SCX_OPSS_DISPATCHING to a
- * remote CPU's local DSQ, we only set ops_state to %SCX_OPSS_NONE
- * so that a concurrent dequeue can proceed, but we clear
- * %SCX_TASK_IN_CUSTODY only when we later enqueue or move the
- * task. So we can see NONE + IN_CUSTODY here and we must handle
- * it. Similarly, after waiting on %SCX_OPSS_DISPATCHING we see
- * NONE but the task may still have %SCX_TASK_IN_CUSTODY set until
- * it is enqueued on the destination.
+ * - @p is on, or being moved off, a user DSQ.
+ * - release_dispatching_and_lock_task_rq() cleared ops_state to let a
+ * racing dequeue proceed, before locking @p's rq to finish the
+ * dispatch.
*/
if (task_leave_custody(p) && SCX_HAS_OP(sch, dequeue))
SCX_CALL_OP_TASK(sch, dequeue, rq, p, deq_flags);
@@ -3166,6 +3165,48 @@ bool scx_consume_global_dsq(struct scx_sched *sch, struct rq *rq)
return scx_consume_dispatch_q(sch, rq, &sch->pnode[node]->global_dsq, 0);
}
+/**
+ * release_dispatching_and_lock_task_rq - Release DISPATCHING and lock task_rq
+ * @p: task being dispatched, owned by the caller (e.g. through
+ * %SCX_OPSS_DISPATCHING)
+ * @locked_rq: currently locked rq
+ * @src_rq: @p's rq, stable while the caller owns @p
+ *
+ * @p is on a possibly remote @src_rq which we need to lock. If dequeue is in
+ * progress, it'd be locking @src_rq and waiting on DISPATCHING, so we can't
+ * grab @src_rq lock while holding DISPATCHING.
+ *
+ * As @p is wholly ours, we can pretend that we're moving from a DSQ and use the
+ * same mechanism - mark the task under transfer with holding_cpu, release
+ * DISPATCHING and then follow the same protocol. See
+ * unlink_dsq_and_switch_rq_lock().
+ *
+ * On return, @src_rq is locked. Return %true if @p is still ours, with
+ * holding_cpu cleared. Return %false if @p was lost to a dequeue.
+ */
+static bool release_dispatching_and_lock_task_rq(struct task_struct *p,
+ struct rq *locked_rq,
+ struct rq *src_rq)
+{
+ p->scx.holding_cpu = raw_smp_processor_id();
+
+ /* store_release ensures that dequeue sees the above */
+ atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE);
+
+ if (locked_rq != src_rq)
+ switch_rq_lock(locked_rq, src_rq);
+
+ /* task_rq couldn't have changed if we're still the holding cpu */
+ if (likely(p->scx.holding_cpu == raw_smp_processor_id()) &&
+ !WARN_ON_ONCE(src_rq != task_rq(p))) {
+ /* the handshake is over, see unlink_dsq_and_switch_rq_lock() */
+ p->scx.holding_cpu = -1;
+ return true;
+ }
+
+ return false;
+}
+
/**
* dispatch_to_local_dsq - Dispatch a task to a local dsq
* @sch: scx_sched being operated on
@@ -3203,36 +3244,11 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq,
return;
}
- /*
- * @p is on a possibly remote @src_rq which we need to lock to move the
- * task. If dequeue is in progress, it'd be locking @src_rq and waiting
- * on DISPATCHING, so we can't grab @src_rq lock while holding
- * DISPATCHING.
- *
- * As DISPATCHING guarantees that @p is wholly ours, we can pretend that
- * we're moving from a DSQ and use the same mechanism - mark the task
- * under transfer with holding_cpu, release DISPATCHING and then follow
- * the same protocol. See unlink_dsq_and_switch_rq_lock().
- */
- p->scx.holding_cpu = raw_smp_processor_id();
-
- /* store_release ensures that dequeue sees the above */
- atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE);
-
/* switch to @src_rq lock */
- if (locked_rq != src_rq) {
- switch_rq_lock(locked_rq, src_rq);
- locked_rq = src_rq;
- }
-
- /* task_rq couldn't have changed if we're still the holding cpu */
- if (likely(p->scx.holding_cpu == raw_smp_processor_id()) &&
- !WARN_ON_ONCE(src_rq != task_rq(p))) {
+ locked_rq = src_rq;
+ if (release_dispatching_and_lock_task_rq(p, rq, src_rq)) {
bool fallback = false;
- /* the handshake is over, see unlink_dsq_and_switch_rq_lock() */
- p->scx.holding_cpu = -1;
-
/*
* If @p is staying on the same rq, there's no need to go
* through the full deactivate/activate cycle. Optimize by
@@ -3268,6 +3284,36 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq,
switch_rq_lock(locked_rq, rq);
}
+/**
+ * dispatch_remote_task_to_global_dsq - Dispatch a remote task to the global DSQ
+ * @sch: scx_sched being operated on
+ * @rq: current rq which is locked, not @p's rq
+ * @p: task to dispatch
+ * @slice: slice carried by the insert verdict, 0 keeps the current value
+ * @vtime: vtime carried by the insert verdict
+ * @enq_flags: %SCX_ENQ_*
+ *
+ * Inserting @p into the global DSQ takes @p out of the BPF scheduler's custody,
+ * which clears %SCX_TASK_IN_CUSTODY and calls ops.dequeue(). Both must be done
+ * under @p's rq lock. Switch to it the same way dispatch_to_local_dsq() does
+ * and switch back to @rq lock afterwards.
+ *
+ * The caller must have exclusive ownership of @p through %SCX_OPSS_DISPATCHING.
+ */
+static void dispatch_remote_task_to_global_dsq(struct scx_sched *sch,
+ struct rq *rq,
+ struct task_struct *p, u64 slice,
+ u64 vtime, u64 enq_flags)
+{
+ struct rq *src_rq = task_rq(p);
+
+ if (release_dispatching_and_lock_task_rq(p, rq, src_rq))
+ scx_dispatch_enqueue(sch, src_rq, find_global_dsq(sch, task_cpu(p)),
+ p, slice, vtime, enq_flags | SCX_ENQ_APPLY_SLICE);
+
+ switch_rq_lock(src_rq, rq);
+}
+
/**
* finish_dispatch - Asynchronously finish dispatching a task
* @sch: the scheduler
@@ -3350,11 +3396,22 @@ static void finish_dispatch(struct scx_sched *sch, struct rq *rq, struct task_st
dsq = find_dsq_for_dispatch(sch, rq, dsq_id, task_cpu(p));
- if (dsq->id == SCX_DSQ_LOCAL)
+ if (dsq->id == SCX_DSQ_LOCAL) {
dispatch_to_local_dsq(sch, rq, dsq, p, slice, vtime, enq_flags);
- else
- scx_dispatch_enqueue(sch, rq, dsq, p, slice, vtime,
- enq_flags | SCX_ENQ_APPLY_SLICE | SCX_ENQ_CLEAR_OPSS);
+ return;
+ }
+
+ /*
+ * A remote task can enter a user DSQ without its rq lock. Inserting
+ * into the global DSQ (including the destroyed-DSQ fallback) ends
+ * custody and requires @p's rq lock.
+ */
+ if ((dsq->id != SCX_DSQ_GLOBAL || rq == task_rq(p)) &&
+ scx_dispatch_enqueue(sch, rq, dsq, p, slice, vtime,
+ enq_flags | SCX_ENQ_APPLY_SLICE | SCX_ENQ_CLEAR_OPSS))
+ return;
+
+ dispatch_remote_task_to_global_dsq(sch, rq, p, slice, vtime, enq_flags);
}
void scx_flush_dispatch_buf(struct scx_sched *sch, struct rq *rq)
@@ -7316,10 +7373,6 @@ void scx_bypass(struct scx_sched *sch, bool bypass)
* that sched. Restart the wait unless @owner is
* the root, which runs its own tasks bypassing
* or not.
- *
- * A dispatch in flight writes p->scx.flags
- * without @p's rq lock. The dequeue waits it
- * out, so set the flag after it.
*/
if (scx_parent(owner))
p->scx.flags |= SCX_TASK_RESET_RUNNABLE_AT;
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/3] sched_ext: Remove scx_wait_dispatching()
2026-11-23 17:10 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix ops_state and ops.dequeue() locking in dispatch Kuba Piecuch
2026-11-23 17:10 ` [PATCH 1/3] sched_ext: Fix ops_state and ops.dequeue() locking races " Kuba Piecuch
@ 2026-11-23 17:10 ` Kuba Piecuch
2026-11-23 17:10 ` [PATCH 3/3] selftests/sched_ext: Test that ops.dequeue() runs with the task's rq locked Kuba Piecuch
2 siblings, 0 replies; 4+ messages in thread
From: Kuba Piecuch @ 2026-11-23 17:10 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, fangqiurong, sched-ext, linux-kernel
scx_wait_dispatching() waits for a task found on a DSQ to leave
SCX_OPSS_DISPATCHING. scx_dispatch_enqueue() now clears ops_state before
a dispatched task becomes visible on any DSQ, and the remote dispatch
paths release DISPATCHING before they lock the task's rq, so the wait
can no longer trigger. Remove it and all its callers.
ops_dequeue() still waits on DISPATCHING, as a dequeue can race a
dispatcher that hasn't inserted the task yet.
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
kernel/sched/ext/ext.c | 24 +-----------------------
kernel/sched/ext/internal.h | 1 -
2 files changed, 1 insertion(+), 24 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 6d8483fb8304..0b7e9c52fa92 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -2727,14 +2727,11 @@ static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl
void scx_move_local_task_to_local_dsq(struct scx_sched *sch, struct task_struct *p,
u64 enq_flags, struct rq *dst_rq)
{
- struct scx_dispatch_q *dst_dsq;
+ struct scx_dispatch_q *dst_dsq = scx_resolve_local_dsq(sch, dst_rq, p, &enq_flags);
lockdep_assert_rq_held(dst_rq);
WARN_ON_ONCE(p->scx.holding_cpu >= 0);
- scx_wait_dispatching(p);
-
- dst_dsq = scx_resolve_local_dsq(sch, dst_rq, p, &enq_flags);
if (enq_flags & (SCX_ENQ_HEAD | SCX_ENQ_PREEMPT | SCX_ENQ_PREEMPT_LAZY))
dsq_insert_head(dst_dsq, p);
@@ -2994,7 +2991,6 @@ static bool unlink_dsq_and_switch_rq_lock(struct task_struct *p,
*/
p->scx.dsq = NULL;
p->scx.holding_cpu = -1;
- scx_wait_dispatching(p);
return true;
}
@@ -3054,8 +3050,6 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch,
lockdep_assert_held(&src_dsq->lock);
lockdep_assert_rq_held(src_rq);
- scx_wait_dispatching(p);
-
if (dst_dsq->id == SCX_DSQ_LOCAL) {
dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq);
/*
@@ -5256,18 +5250,6 @@ static bool local_task_should_reenq(struct rq *rq, struct task_struct *p,
return *reenq_flags & SCX_REENQ_ANY;
}
-/*
- * The dispatcher runs ops.dequeue() and stores the final ops_state after
- * dropping the DSQ lock, so @p can be found on a DSQ while still
- * %SCX_OPSS_DISPATCHING. Wait for the dispatch to finish before operating on
- * @p.
- */
-void scx_wait_dispatching(struct task_struct *p)
-{
- if (unlikely(atomic_long_read_acquire(&p->scx.ops_state) == SCX_OPSS_DISPATCHING))
- wait_ops_state(p, SCX_OPSS_DISPATCHING);
-}
-
static u32 reenq_local(struct scx_sched *sch, struct rq *rq, u64 reenq_flags)
{
LIST_HEAD(tasks);
@@ -5311,7 +5293,6 @@ static u32 reenq_local(struct scx_sched *sch, struct rq *rq, u64 reenq_flags)
if (!local_task_should_reenq(rq, p, &reenq_flags, &reason))
continue;
- scx_wait_dispatching(p);
scx_dispatch_dequeue(rq, p);
WARN_ON_ONCE(p->scx.flags & SCX_TASK_REENQ_REASON_MASK);
@@ -5450,7 +5431,6 @@ static void reenq_user(struct rq *rq, struct scx_dispatch_q *dsq, u64 reenq_flag
}
/* @p is on @dsq, its rq and @dsq are locked */
- scx_wait_dispatching(p);
dispatch_dequeue_locked(p, dsq);
raw_spin_unlock(&dsq->lock);
@@ -5555,7 +5535,6 @@ static void scx_reenq_reject(struct rq *rq)
WARN_ON_ONCE(p->migration_pending);
}
- scx_wait_dispatching(p);
scx_dispatch_dequeue(rq, p);
list_add_tail(&p->scx.dsq_list.node, &tasks);
@@ -6819,7 +6798,6 @@ static s32 bypass_lb_move(struct scx_sched *sch, struct rq *donor_rq,
lockdep_assert_held(&donor_dsq->lock);
if (rq == donor_rq) {
- scx_wait_dispatching(p);
dispatch_dequeue_locked(p, donor_dsq);
raw_spin_unlock(&donor_dsq->lock);
} else if (!unlink_dsq_and_switch_rq_lock(p, donor_dsq, donor_rq, rq)) {
diff --git a/kernel/sched/ext/internal.h b/kernel/sched/ext/internal.h
index 58652370da5a..c77f62cd7bc9 100644
--- a/kernel/sched/ext/internal.h
+++ b/kernel/sched/ext/internal.h
@@ -2322,7 +2322,6 @@ void scx_resched_curr_deferred(struct rq *rq);
u64 __scx_bpf_now(struct rq *rq);
void schedule_dsq_reenq(struct scx_sched *sch, struct scx_dispatch_q *dsq,
u64 reenq_flags, struct rq *locked_rq);
-void scx_wait_dispatching(struct task_struct *p);
int __scx_init_task(struct scx_sched *sch, struct task_struct *p,
struct cgroup *cgrp, bool fork);
void scx_enable_task(struct scx_sched *sch, struct task_struct *p);
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 3/3] selftests/sched_ext: Test that ops.dequeue() runs with the task's rq locked
2026-11-23 17:10 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix ops_state and ops.dequeue() locking in dispatch Kuba Piecuch
2026-11-23 17:10 ` [PATCH 1/3] sched_ext: Fix ops_state and ops.dequeue() locking races " Kuba Piecuch
2026-11-23 17:10 ` [PATCH 2/3] sched_ext: Remove scx_wait_dispatching() Kuba Piecuch
@ 2026-11-23 17:10 ` Kuba Piecuch
2 siblings, 0 replies; 4+ messages in thread
From: Kuba Piecuch @ 2026-11-23 17:10 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, fangqiurong, sched-ext, linux-kernel
Check in dequeue_remote's ops.dequeue() that scx_bpf_locked_rq() is the
task's rq, and add a scenario that inserts tasks into SCX_DSQ_GLOBAL from
ops.dispatch(), mostly from CPUs other than the task's.
dequeue_remote fails without "sched_ext: Fix ops_state and ops.dequeue()
locking races in dispatch".
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
.../selftests/sched_ext/dequeue_remote.bpf.c | 34 +++++++++++++++--
.../selftests/sched_ext/dequeue_remote.c | 37 +++++++++++++++----
2 files changed, 61 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c b/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
index 983590c6d498..8996bcc96e1d 100644
--- a/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
+++ b/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
@@ -8,7 +8,11 @@
* the dispatching CPU's local DSQ, so most moves cross CPUs. With
* @test_use_move_to_local, tasks are queued on a user DSQ and consumed with
* scx_bpf_dsq_move_to_local(). Otherwise, they are queued in a BPF queue and
- * dispatched with SCX_DSQ_LOCAL_ON.
+ * dispatched with SCX_DSQ_LOCAL_ON or, with @test_use_global, inserted into
+ * SCX_DSQ_GLOBAL, often from a CPU other than the task's.
+ *
+ * ops.dequeue() also verifies that it's always called with the task's rq
+ * locked.
*
* Copyright (c) 2026 Google LLC.
*/
@@ -53,10 +57,11 @@ struct task_struct___core_sched {
} __attribute__((preserve_access_index));
bool test_use_move_to_local;
+bool test_use_global;
u64 enqueue_cnt, dequeue_cnt, dispatch_dequeue_cnt, change_dequeue_cnt;
u64 remote_dispatch_cnt, remote_running_cnt, missed_dequeue_cnt;
-u64 core_sched_exec_dequeue_cnt;
+u64 core_sched_exec_dequeue_cnt, remote_global_cnt, wrong_rq_dequeue_cnt;
static struct task_ctx *lookup_task_ctx(struct task_struct *p)
{
@@ -121,9 +126,22 @@ void BPF_STRUCT_OPS(dequeue_remote_enqueue, struct task_struct *p, u64 enq_flags
void BPF_STRUCT_OPS(dequeue_remote_dequeue, struct task_struct *p, u64 deq_flags)
{
struct task_ctx *tctx;
+ struct rq *locked_rq;
__sync_fetch_and_add(&dequeue_cnt, 1);
+ /*
+ * ops.dequeue() must always run with @p's rq locked, even when @p is
+ * inserted into SCX_DSQ_GLOBAL by ops.dispatch() on another CPU.
+ */
+ locked_rq = scx_bpf_locked_rq();
+ if (!locked_rq || locked_rq->cpu != scx_bpf_task_cpu(p)) {
+ __sync_fetch_and_add(&wrong_rq_dequeue_cnt, 1);
+ scx_bpf_error("%d (%s): dequeue without task rq lock (locked=%d cpu=%d)",
+ p->pid, p->comm, locked_rq ? locked_rq->cpu : -1,
+ scx_bpf_task_cpu(p));
+ }
+
tctx = lookup_task_ctx(p);
if (!tctx)
return;
@@ -187,7 +205,17 @@ void BPF_STRUCT_OPS(dequeue_remote_dispatch, s32 cpu, struct task_struct *prev)
continue;
}
- if (bpf_cpumask_test_cpu(cpu, p->cpus_ptr)) {
+ if (test_use_global) {
+ /*
+ * @p goes on the global DSQ of its own node, which this
+ * CPU may not consume. Kick @p's CPU in case it's idle.
+ */
+ if (scx_bpf_task_cpu(p) != cpu) {
+ __sync_fetch_and_add(&remote_global_cnt, 1);
+ scx_bpf_kick_cpu(scx_bpf_task_cpu(p), SCX_KICK_IDLE);
+ }
+ scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, 0);
+ } else if (bpf_cpumask_test_cpu(cpu, p->cpus_ptr)) {
if (scx_bpf_task_cpu(p) != cpu)
__sync_fetch_and_add(&remote_dispatch_cnt, 1);
scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu,
diff --git a/tools/testing/selftests/sched_ext/dequeue_remote.c b/tools/testing/selftests/sched_ext/dequeue_remote.c
index af344581d5f1..aab6dfb2d64f 100644
--- a/tools/testing/selftests/sched_ext/dequeue_remote.c
+++ b/tools/testing/selftests/sched_ext/dequeue_remote.c
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Verify that ops.dequeue() is called for tasks leaving BPF custody through
- * an SCX-internal cross-CPU migration (move_remote_task_to_local_dsq()).
+ * an SCX-internal cross-CPU migration (move_remote_task_to_local_dsq()) and
+ * that it always runs with the task's rq locked, including when the task is
+ * inserted into SCX_DSQ_GLOBAL from another CPU.
*
* Copyright (c) 2026 Google LLC.
*/
@@ -54,7 +56,7 @@ static void worker_fn(int id)
}
static enum scx_test_status run_scenario(struct dequeue_remote *skel,
- bool use_move_to_local,
+ bool use_move_to_local, bool use_global,
const char *name)
{
enum scx_test_status ret = SCX_TEST_PASS;
@@ -69,6 +71,7 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
nr_workers = MAX_WORKERS;
skel->bss->test_use_move_to_local = use_move_to_local;
+ skel->bss->test_use_global = use_global;
skel->bss->enqueue_cnt = 0;
skel->bss->dequeue_cnt = 0;
skel->bss->dispatch_dequeue_cnt = 0;
@@ -77,6 +80,8 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
skel->bss->remote_running_cnt = 0;
skel->bss->missed_dequeue_cnt = 0;
skel->bss->core_sched_exec_dequeue_cnt = 0;
+ skel->bss->remote_global_cnt = 0;
+ skel->bss->wrong_rq_dequeue_cnt = 0;
memset(&skel->data->uei, 0, sizeof(skel->data->uei));
link = bpf_map__attach_struct_ops(skel->maps.dequeue_remote_ops);
@@ -126,7 +131,10 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
(unsigned long)skel->bss->dequeue_cnt,
(unsigned long)skel->bss->dispatch_dequeue_cnt,
(unsigned long)skel->bss->change_dequeue_cnt);
- if (!use_move_to_local)
+ if (use_global)
+ printf(" remote SCX_DSQ_GLOBAL dispatches: %lu\n",
+ (unsigned long)skel->bss->remote_global_cnt);
+ else if (!use_move_to_local)
printf(" remote SCX_DSQ_LOCAL_ON dispatches: %lu\n",
(unsigned long)skel->bss->remote_dispatch_cnt);
printf(" ran on a CPU other than the enqueue CPU: %lu\n",
@@ -135,15 +143,25 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
(unsigned long)skel->bss->missed_dequeue_cnt);
printf(" late SCX_DEQ_CORE_SCHED_EXEC dequeues: %lu\n",
(unsigned long)skel->bss->core_sched_exec_dequeue_cnt);
+ printf(" dequeues without the task's rq locked: %lu\n",
+ (unsigned long)skel->bss->wrong_rq_dequeue_cnt);
if (skel->data->uei.kind != EXIT_KIND(SCX_EXIT_UNREG))
SCX_ERR("Scheduler exited with kind=%lld: %s",
(long long)skel->data->uei.kind, skel->data->uei.msg);
SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
- /* the test is meaningless if no task was moved across CPUs */
- SCX_GT(skel->bss->remote_running_cnt, 0);
+ /*
+ * The test is meaningless if no task was moved across CPUs. Global DSQs
+ * are per node, so the global scenario may not migrate tasks at all and
+ * needs remote global dispatches instead.
+ */
+ if (use_global)
+ SCX_GT(skel->bss->remote_global_cnt, 0);
+ else
+ SCX_GT(skel->bss->remote_running_cnt, 0);
SCX_EQ(skel->bss->enqueue_cnt, skel->bss->dequeue_cnt);
+ SCX_EQ(skel->bss->wrong_rq_dequeue_cnt, 0);
return SCX_TEST_PASS;
}
@@ -174,16 +192,21 @@ static enum scx_test_status run(void *ctx)
struct dequeue_remote *skel = ctx;
enum scx_test_status status;
- status = run_scenario(skel, false,
+ status = run_scenario(skel, false, false,
"BPF queue -> SCX_DSQ_LOCAL_ON | cpu");
if (status != SCX_TEST_PASS)
return status;
- status = run_scenario(skel, true,
+ status = run_scenario(skel, true, false,
"user DSQ -> scx_bpf_dsq_move_to_local()");
if (status != SCX_TEST_PASS)
return status;
+ status = run_scenario(skel, false, true,
+ "BPF queue -> SCX_DSQ_GLOBAL");
+ if (status != SCX_TEST_PASS)
+ return status;
+
return SCX_TEST_PASS;
}
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 4+ messages in thread