Skip to contentSkip to conversation

sched_ext: Fix missing ops.dequeue() on remote local DSQ moves

PATCH In review Kuba Piecuch 2 participants 4 messages Sep 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 sched_ext: Fix missing ops.dequeue() on remote local DSQ moves

Hi,

When a task in the BPF scheduler's custody is moved to another CPU's local DSQ, ops.dequeue() is deferred until the task is picked and then reported with SCX_DEQ_CORE_SCHED_EXEC, instead of being called without flags when the task is inserted into the destination DSQ. Patch 1 fixes this by reverting the enqueue side of b75aaea24c9f ("sched_ext: Properly mark SCX-internal migrations via sticky_cpu"). Patch 2 adds a selftest.

For 7.1.y, patch 1 also needs 18d62044cda7 ("sched_ext: Preserve rq tracking across local DSQ dispatch"), as noted in its stable tags.

This is based on sched_ext/for-7.3-fixes (d35a535d3e3e). Merging it into sched_ext/for-next gives a trivial conflict in the selftests Makefile, where enq_blocked was added next to dequeue_remote; keep both.

Testing was done on x86_64 in virtme-ng with 4 vCPUs in an SMT topology (2 cores x 2 threads) and CONFIG_SCHED_CORE=y. Without patch 1, dequeue_remote fails in every run (30/30), e.g.:

sched_ext: dequeue_remote: dequeue_remote.bpf.c:141: 15 (rcu_preempt): late ops.dequeue() with SCX_DEQ_CORE_SCHED_EXEC (enq_cpu=3 cpu=2 seq=1)

     ...
     ops_dequeue+0x114/0x170
     set_next_task_scx+0x104/0x1e0
     __pick_next_task+0xc7/0x180
     __schedule+0x154/0x1870

and the full sched_ext selftest suite reports 31 passed, 1 skipped (nohz_tick), 1 failed (dequeue_remote).

With patch 1, dequeue_remote passes in every run (30/30), with ~140k custody enqueues per run, ~90k of them followed by the task running on another CPU. The full suite reports 32 passed, 1 skipped (nohz_tick), 0 failed. dequeue_remote also passes 10/10 with the runner and its workers sharing a core cookie, where legitimate core-sched picks out of custody do happen.

v3:
- Test: kick the dispatching CPU if ops.dispatch() runs out of pops while the queue isn't empty, reset enqueue_seq in ops.init_task() and clear the exit record before each scenario (Andrea). Added Andrea's Reviewed-by.

v2:
- Reordered to put the fix first and folded the Makefile entry into the test patch (Tejun, Andrea).
- Fix: clear p->scx.sticky_cpu right after reading it, making the fix a revert of the enqueue side of b75aaea24c9f (Andrea). Reworded the comment and shortened the description (Tejun). Noted 18d62044cda7 as a 7.1.y prerequisite (Tejun, Andrea). Added Andrea's Reviewed-by.
- Test: check p->core_cookie on SCX_DEQ_CORE_SCHED_EXEC instead of skipping when core scheduling is in use (Tejun).
- Test: count CPUs with sched_getaffinity() (Tejun), pop past stale queue entries in ops.dispatch(), reset and print all counters per scenario (Tejun), destroy the struct_ops link and reap workers on error paths (Sashiko).
- Test: deduplicated the descriptions and lowercased single-line comments (Tejun).

v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

Thanks,
Kuba

Assisted-by Claude:claude-opus-5.5

Kuba Piecuch (2):
sched_ext: Call ops.dequeue() when a task arrives on a remote local
DSQ
selftests/sched_ext: Add a test for ops.dequeue() on remote local DSQ
moves

 kernel/sched/ext/ext.c                        |  10 +-
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/dequeue_remote.bpf.c  | 270 ++++++++++++++++++
 .../selftests/sched_ext/dequeue_remote.c      | 204 +++++++++++++
 4 files changed, 482 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/dequeue_remote.c

base-commit: d35a535d3e3e71f92a051d94e415f43612c4edfc

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#2 lore raw

PATCH v3 2/2 selftests/sched_ext: Add a test for ops.dequeue() on remote local DSQ moves

Add a dequeue_remote test that makes moves of tasks in the BPF scheduler's custody to another CPU's local DSQ the common case, both via SCX_DSQ_LOCAL_ON dispatch and via scx_bpf_dsq_move_to_local(). The BPF scheduler tracks each task's custody state and triggers scx_bpf_error() if a custody period doesn't end with exactly one ops.dequeue() before the task runs, or if it ends with an SCX_DEQ_CORE_SCHED_EXEC dequeue of a task without a core cookie.

Without the previous patch, the test fails with:

sched_ext: dequeue_remote: dequeue_remote.bpf.c:141: 15 (rcu_preempt): late ops.dequeue() with SCX_DEQ_CORE_SCHED_EXEC (enq_cpu=3 cpu=2 seq=1)

     ...
     ops_dequeue+0x114/0x170
     set_next_task_scx+0x104/0x1e0
     __pick_next_task+0xc7/0x180
     __schedule+0x154/0x1870
Assisted-by Claude:claude-opus-5.5
---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/dequeue_remote.bpf.c  | 270 ++++++++++++++++++
 .../selftests/sched_ext/dequeue_remote.c      | 204 +++++++++++++
 3 files changed, 475 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/dequeue_remote.c
tools/testing/selftests/sched_ext/Makefile +1 -0
index 4e06d0baaeec..af919a56a4f4 100644
@@ -165,6 +165,7 @@ auto-test-targets :=			\
 	create_dsq			\
 	dequeue				\
 	dequeue_iter			\
+	dequeue_remote			\
 	enq_last_no_enq_fails		\
 	ddsp_bogus_dsq_fail		\
 	ddsp_vtimelocal_fail		\
tools/testing/selftests/sched_ext/dequeue_remote.bpf.c new file +270 -0

270 lines not on this page · raw message

tools/testing/selftests/sched_ext/dequeue_remote.c new file +204 -0

204 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#1 lore raw

PATCH v3 1/2 sched_ext: Call ops.dequeue() when a task arrives on a remote local DSQ

In reply to Kuba Piecuch

When a task in the BPF scheduler's custody is moved to another CPU's local DSQ, ops.dequeue() is only called once the task is picked for execution, from set_next_task_scx() with SCX_DEQ_CORE_SCHED_EXEC, even though no core-sched pick took place. It should instead be called without flags when the task is inserted into the destination DSQ, as it is for same-rq dispatches.

move_remote_task_to_local_dsq() sets p->scx.sticky_cpu across the migration so that deactivate_task() on the source rq doesn't end custody. Since commit b75aaea24c9f ("sched_ext: Properly mark SCX-internal migrations via sticky_cpu"), enqueue_task_scx() only clears it after inserting the task, so task_leave_custody() still sees the migration in progress and skips the custody exit.

Clear p->scx.sticky_cpu as soon as enqueue_task_scx() has read it, as was done before that commit. The source side is unaffected.

Fixes ebf1ccff79c4 ("sched_ext: Fix ops.dequeue() semantics")
Cc [email protected] # 7.1.x: 18d62044cda7: sched_ext: Preserve rq tracking across local DSQ dispatch
Assisted-by Claude:claude-opus-5.5
---
 kernel/sched/ext/ext.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/sched/ext/ext.c +7 -3
index 493b7aac7087..5209510d6a5e 100644
@@ -2152,6 +2152,13 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_
 	int sticky_cpu = p->scx.sticky_cpu;
 	u64 enq_flags = core_enq_flags | rq->scx.remote_activate_enq_flags;
 
+	/*
+	 * An SCX-internal migration ends on arrival. Clear sticky_cpu so @p can
+	 * leave custody when inserted into the destination DSQ.
+	 */
+	if (sticky_cpu >= 0)
+		p->scx.sticky_cpu = -1;
+
 	/*
 	 * SCX_RQ_IN_WAKEUP promises a task_woken_scx() call once this enqueue
 	 * returns. Only the core's wakeup path delivers one. The flags stashed
@@ -2190,9 +2197,6 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_
 		dl_server_start(&rq->ext_server);
 
 	scx_do_enqueue_task(rq, p, enq_flags, sticky_cpu);
-
-	if (sticky_cpu >= 0)
-		p->scx.sticky_cpu = -1;
 out:
 	rq->scx.flags &= ~SCX_RQ_IN_WAKEUP;
 
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog