mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 v2 sched_ext/for-7.4-fixes] sched_ext: Fix ops_state and ops.dequeue() locking in dispatch
Date: Wed, 25 Nov 2026 11:28:34 +0000	[thread overview]
Message-ID: <20261125112553.1539758-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 factors the holding_cpu + SCX_OPSS_NONE + switch_rq_lock()
handoff out of dispatch_to_local_dsq() into
release_dispatching_and_lock_task_rq() with no functional change.

Patch 2 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 via
dispatch_remote_task_to_global_dsq(), using
release_dispatching_and_lock_task_rq(). On a destroyed user DSQ,
scx_dispatch_enqueue() clears ops_state after scx_error() and returns
without falling back to the global DSQ, leaving scx_bypass() to recover
the task. 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 3 removes scx_wait_dispatching() and all its callers, as it can no
longer find a DISPATCHING task. ops_dequeue() keeps its own wait.

Patch 4 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 2.

The patches were written with the help of an AI coding assistant.

Changes in v2:
- Split release_dispatching_and_lock_task_rq() extraction out into a
  separate preparatory patch (Patch 1) (Tejun).
- Patch 2: On a destroyed user DSQ in scx_dispatch_enqueue(), clear
  ops_state and return instead of falling back to the global DSQ,
  letting scx_bypass() recover the task and dropping the bool return and
  finish_dispatch() retry (Tejun).

v1: https://lore.kernel.org/all/20261123170846.3165648-1-jpiecuch@google.com/

Kuba Piecuch (4):
  sched_ext: Factor out release_dispatching_and_lock_task_rq()
  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                        | 264 ++++++++++--------
 kernel/sched/ext/internal.h                   |   1 -
 .../selftests/sched_ext/dequeue_remote.bpf.c  |  34 ++-
 .../selftests/sched_ext/dequeue_remote.c      |  37 ++-
 4 files changed, 208 insertions(+), 128 deletions(-)


base-commit: 97e1c7d467d07734f8f5c8c64730e1f7c415bd46
-- 
2.56.1.839.g22a8290db9-goog


             reply	other threads:[~2026-11-25 11:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 11:28 Kuba Piecuch [this message]
2026-11-25 11:28 ` [PATCH v2 1/4] sched_ext: Factor out release_dispatching_and_lock_task_rq() Kuba Piecuch
2026-11-25 11:28 ` [PATCH v2 2/4] sched_ext: Fix ops_state and ops.dequeue() locking races in dispatch Kuba Piecuch
2026-11-25 11:28 ` [PATCH v2 3/4] sched_ext: Remove scx_wait_dispatching() Kuba Piecuch
2026-11-25 11:28 ` [PATCH v2 4/4] 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=20261125112553.1539758-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®