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: [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix ops_state and ops.dequeue() locking in dispatch
Date: Mon, 23 Nov 2026 17:10:31 +0000 [thread overview]
Message-ID: <20261123170846.3165648-1-jpiecuch@google.com> (raw)
finish_dispatch() hands a task claimed in SCX_OPSS_DISPATCHING to
scx_dispatch_enqueue(), which inserts it into the target DSQ and only
then, after dropping the DSQ lock and (for SCX_DSQ_GLOBAL) after calling
ops.dequeue(), stores SCX_OPSS_NONE. When the task is remote,
finish_dispatch() holds the dispatching CPU's rq lock, not the task's.
This causes three problems.
1) The task is visible on the DSQ while still DISPATCHING. Another CPU
can consume and requeue it before the dispatcher's final store, which
then overwrites the new state. Without the waits described below:
CPU 0 (finish_dispatch(), rq0) CPU 1 (rq1 == task_rq(p))
------------------------------ ------------------------------
QUEUED -> DISPATCHING
insert p into global_dsq, unlock
ops.dequeue(p) ...
scx_consume_global_dsq()
moves p to local DSQ
reenq_local()
ops.enqueue(p)
ops_state = QUEUED(qseq')
ops_state = NONE
-> QUEUED(qseq') is lost, later
finish_dispatch()es of p see
NONE and drop it, p stalls
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") paper over this
with scx_wait_dispatching() at every place that takes a task off a
DSQ. Consuming a task from the global DSQ then busy-waits, with the
task's rq lock held and IRQs off, until the dispatching CPU returns
from ops.dequeue(). Lockdep can't see that dependency.
2) For a remote task dispatched to SCX_DSQ_GLOBAL (or to a destroyed
user DSQ, which falls back to the global DSQ), task_leave_custody()
does an RMW of p->scx.flags without the task's rq lock. DISPATCHING
keeps dequeues away, but other paths update p->scx.flags under the rq
lock without dequeueing:
CPU 0 (finish_dispatch(), rq0) CPU 1 (scx_sub_enable_workfn())
------------------------------ ------------------------------
QUEUED -> DISPATCHING
lock global_dsq, insert p
tmp = p->scx.flags
task_rq_lock(p)
p->scx.flags |= SUB_INIT
task_rq_unlock(p)
p->scx.flags = tmp & ~IN_CUSTODY
-> SCX_TASK_SUB_INIT is lost
3) In the same case ops.dequeue() runs with scx_locked_rq() pointing at
the dispatching CPU's rq, although SCX_CALL_OP_TASK() promises the
task's rq is locked. kfuncs that operate on the locked rq then act on
the wrong CPU, e.g.:
scx_dispatch_sched() /* CPU 0, rq0 locked */
ops.dispatch()
scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL) /* task_cpu(p) == 1 */
scx_flush_dispatch_buf()
finish_dispatch()
scx_dispatch_enqueue(rq0, global_dsq, p)
ops.dequeue(p)
scx_bpf_cpuperf_set(1, ...)
scx_error("Invalid target CPU 1")
Patch 1 clears ops_state in scx_dispatch_enqueue() while the target DSQ
(or rq) is still locked and before ops.dequeue(), so a task found on a
DSQ is never DISPATCHING. finish_dispatch() now inserts a remote task
into the global DSQ under the task's rq lock, using the holding_cpu
handoff that dispatch_to_local_dsq() already uses, factored out into
release_dispatching_and_lock_task_rq(). scx_dispatch_enqueue() returns
false on a destroyed user DSQ when the caller doesn't hold the task's rq
lock, so the fallback takes the same path. SCX_TASK_IN_CUSTODY is only
set with the task's rq locked, lockdep assertions are added, and the now
impossible QUEUED && !IN_CUSTODY retry in ops_dequeue() and a stale
comment in scx_bypass() are dropped.
Patch 2 removes scx_wait_dispatching() and all its callers, as it can no
longer find a DISPATCHING task. ops_dequeue() keeps its own wait.
Patch 3 makes dequeue_remote check in ops.dequeue() that
scx_bpf_locked_rq() is the task's rq, and adds a scenario that inserts
tasks into SCX_DSQ_GLOBAL from remote CPUs. dequeue_remote fails without
patch 1.
The patches were written with the help of an AI coding assistant.
Kuba Piecuch (3):
sched_ext: Fix ops_state and ops.dequeue() locking races in dispatch
sched_ext: Remove scx_wait_dispatching()
selftests/sched_ext: Test that ops.dequeue() runs with the task's rq
locked
kernel/sched/ext/ext.c | 261 ++++++++++--------
kernel/sched/ext/internal.h | 1 -
.../selftests/sched_ext/dequeue_remote.bpf.c | 34 ++-
.../selftests/sched_ext/dequeue_remote.c | 37 ++-
4 files changed, 207 insertions(+), 126 deletions(-)
base-commit: 2f7928516314f37d7cb334675656b4412edc8be3
--
2.56.1.750.gf70dc682cf-goog
next 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 Kuba Piecuch [this message]
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 ` [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-1-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®