[PATCH v2 1/4] sched_ext: Factor out release_dispatching_and_lock_task_rq()

From: Kuba Piecuch

Date: Wed Nov 25 2026 - 06:28:48 EST


Factor the holding_cpu + SCX_OPSS_NONE + switch_rq_lock() handoff out of
dispatch_to_local_dsq() into release_dispatching_and_lock_task_rq() so
that other remote dispatch paths can share it.

No functional change.

Cc: stable@xxxxxxxxxxxxxxx # v7.1+
Signed-off-by: Kuba Piecuch <jpiecuch@xxxxxxxxxx>
---
kernel/sched/ext/ext.c | 73 ++++++++++++++++++++++++++----------------
1 file changed, 45 insertions(+), 28 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index efa1d01d03af..d6505d90e2c6 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -3166,6 +3166,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
@@ -3189,7 +3231,7 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq,
{
struct rq *src_rq = task_rq(p);
struct rq *dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq);
- struct rq *locked_rq = rq;
+ struct rq *locked_rq;

/*
* We're synchronized against dequeue through DISPATCHING. As @p can't
@@ -3203,36 +3245,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
--
2.56.1.839.g22a8290db9-goog