From: sashiko-bot@kernel.org
To: "Kuba Piecuch" <jpiecuch@google.com>
Cc: sched-ext@lists.linux.dev
Subject: Re: [PATCH 6.18.y] sched_ext: Fix this_rq() assumptions in dispatch kfuncs
Date: Tue, 01 Dec 2026 17:54:47 +0000 [thread overview]
Message-ID: <sashiko-outbox-243916@kernel.org> (raw)
In-Reply-To: <20261201173537.1103264-1-jpiecuch@google.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] sched_ext: deadlock in scx_dsq_move() due to missing
scx_locked_rq() prereqs
--
commit f1df95fec5f50329ce0dbe3284b81e578b28a649
Author: Tejun Heo <tj@kernel.org>
sched_ext: Fix this_rq() assumptions in dispatch kfuncs
This commit replaces this_rq() assumptions in finish_dispatch(),
scx_dsq_move(), and scx_bpf_dsq_nr_queued() with the runqueue tracked by
scx_locked_rq() so operations targeting an SMT sibling under core
scheduling resolve SCX_DSQ_LOCAL and rq lock ownership against the
dispatched rq.
> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> index b3a6d60709403..8a378a50c3444 100644
> --- a/kernel/sched/ext.c
> +++ b/kernel/sched/ext.c
[ ... ]
> @@ -5762,9 +5762,8 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,
> {
> struct scx_sched *sch = scx_root;
> struct scx_dispatch_q *src_dsq = kit->dsq, *dst_dsq;
> - struct rq *this_rq, *src_rq, *locked_rq;
> + struct rq *p_rq, *src_rq, *locked_rq;
> bool dispatched = false;
> - bool in_balance;
> unsigned long flags;
>
> /*
> @@ -5780,19 +5779,23 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,
> return false;
>
> /*
> - * Can be called from either ops.dispatch() locking this_rq() or any
> - * context where no rq lock is held. If latter, lock @p's task_rq which
> - * we'll likely need anyway.
> + * Can be called from either ops.dispatch() holding the dispatched rq's
> + * lock or any context where no rq lock is held. If latter, lock @p's
> + * task_rq which we'll likely need anyway.
> */
> src_rq = task_rq(p);
>
> local_irq_save(flags);
> - this_rq = this_rq();
> - in_balance = this_rq->scx.flags & SCX_RQ_IN_BALANCE;
>
> - if (in_balance) {
> - if (this_rq != src_rq) {
> - raw_spin_rq_unlock(this_rq);
> + /*
> + * Under core scheduling, dispatch can run for a sibling rq, so the
> + * locked rq is not necessarily this CPU's.
> + */
> + locked_rq = scx_locked_rq();
> +
> + if (locked_rq) {
> + if (locked_rq != src_rq) {
> + raw_spin_rq_unlock(locked_rq);
> raw_spin_rq_lock(src_rq);
> }
> } else {
> @@ -5806,7 +5809,7 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,
> */
> scx_breather(src_rq);
>
> - locked_rq = src_rq;
> + p_rq = src_rq;
> raw_spin_lock(&src_dsq->lock);
>
> /*
> @@ -5822,7 +5825,7 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,
> }
>
> /* @p is still on $src_dsq and stable, determine the destination */
> - dst_dsq = find_dsq_for_dispatch(sch, this_rq, dsq_id, p);
> + dst_dsq = find_dsq_for_dispatch(sch, locked_rq ?: this_rq(), dsq_id, p);
>
> /*
> * Apply vtime and slice updates before moving so that the new time is
> @@ -5835,16 +5838,16 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,
> p->scx.slice = kit->slice;
>
> /* execute move */
> - locked_rq = move_task_between_dsqs(sch, p, enq_flags, src_dsq, dst_dsq);
> + p_rq = move_task_between_dsqs(sch, p, enq_flags, src_dsq, dst_dsq);
> dispatched = true;
> out:
> - if (in_balance) {
> - if (this_rq != locked_rq) {
> - raw_spin_rq_unlock(locked_rq);
> - raw_spin_rq_lock(this_rq);
> + if (locked_rq) {
> + if (locked_rq != p_rq) {
> + raw_spin_rq_unlock(p_rq);
> + raw_spin_rq_lock(locked_rq);
> }
> } else {
> - raw_spin_rq_unlock_irqrestore(locked_rq, flags);
> + raw_spin_rq_unlock_irqrestore(p_rq, flags);
> }
[Severity: High]
Can a second scx_dsq_move() call inside ops.dispatch() see
scx_locked_rq() == NULL and deadlock on raw_spin_rq_lock(src_rq)?
In 6.18.y, SCX_CALL_OP_RET() clears scx_locked_rq_state to NULL on exit
instead of restoring the outer operation's locked rq:
kernel/sched/ext.c:SCX_CALL_OP_RET() {
if (rq)
update_locked_rq(rq);
...
if (rq)
update_locked_rq(NULL);
}
When ops.dispatch() migrates a core-scheduled task to a remote local DSQ,
activate_task() invokes ops.core_sched_before() via SCX_CALL_OP_RET(),
wiping scx_locked_rq_state while ops.dispatch() still holds the
dispatched rq lock:
balance_one(rq)
SCX_CALL_OP(..., dispatch, rq, ...)
update_locked_rq(rq)
ops.dispatch()
scx_dsq_move()
move_task_between_dsqs()
move_remote_task_to_local_dsq()
activate_task(dst_rq, p, 0)
...
scx_prio_less()
SCX_CALL_OP_2TASKS_RET(..., core_sched_before, ...)
update_locked_rq(NULL)
raw_spin_rq_lock(locked_rq)
scx_dsq_move()
locked_rq = scx_locked_rq() /* NULL */
raw_spin_rq_lock(src_rq) <-- deadlocks if src_rq shares rq lock
If src_rq is on another core, scx_dsq_move() instead acquires src_rq on
top of the held rq lock and then unlocks p_rq and enables IRQs at out:
without restoring the dispatched rq lock. In addition, replacing
switch_rq_lock() with raw_spin_rq_unlock() and raw_spin_rq_lock() skips
updating scx_locked_rq_state across rq lock handoffs.
Backporting prerequisite commits 7fb39e4eb4c3 ("sched_ext: Save and
restore scx_locked_rq across SCX_CALL_OP") and 18d62044cda7 ("sched_ext:
Preserve rq tracking across local DSQ dispatch") would preserve
scx_locked_rq() across nested callbacks and rq lock switches.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201173537.1103264-1-jpiecuch@google.com?part=1
next prev parent reply other threads:[~2026-12-01 17:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 17:36 [PATCH 6.18.y] sched_ext: Fix this_rq() assumptions in dispatch kfuncs Kuba Piecuch
2026-12-01 17:54 ` sashiko-bot [this message]
2026-12-01 18:46 ` 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=sashiko-outbox-243916@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=jpiecuch@google.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=sched-ext@lists.linux.dev \
/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