From: Kuba Piecuch <jpiecuch@google.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
Andrea Righi <arighi@nvidia.com>,
Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
fangqiurong <fangqiurong@kylinos.cn>,
sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH 1/3] sched_ext: Fix ops_state and ops.dequeue() locking races in dispatch
Date: Mon, 23 Nov 2026 17:10:32 +0000 [thread overview]
Message-ID: <20261123170846.3165648-2-jpiecuch@google.com> (raw)
In-Reply-To: <20261123170846.3165648-1-jpiecuch@google.com>
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
next prev parent reply other threads:[~2026-11-23 17:10 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 17:10 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix ops_state and ops.dequeue() locking " Kuba Piecuch
2026-11-23 17:10 ` Kuba Piecuch [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123170846.3165648-2-jpiecuch@google.com \
--to=jpiecuch@google.com \
--cc=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=fangqiurong@kylinos.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®