Skip to content

sched_ext: Add dispatch transaction API

Cheng-Yang Chou sched-ext 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest3 patches 1 replyIn review
  2. v32 patches 8 repliesSuperseded

Changes in v4 from the cover letter

- Separate the internal rename into its own commit with no functional change; use scx_dsq_insert_stage() instead of scx_dsq_insert_buf() (Tejun Heo)
- Restrict scx_bpf_dsq_insert_commit() to ops.dispatch() only by moving it from scx_kfunc_ids_enqueue_dispatch to scx_kfunc_ids_dispatch (Tejun Heo)
- Restrict scx_bpf_dsq_insert_commit() to local DSQs at runtime:
targeting a non-local DSQ aborts the scheduler (Tejun Heo)
- Expand kdoc for both kfuncs with the full problem statement and use-case context (Tejun Heo)
- Add scx_bpf_dsq_insert_commit_args struct to stay within the BPF verifier's five-argument limit imposed by KF_IMPLICIT_ARGS (Andrea Righi)
- Selftest: pass slice=0 in commit_args to preserve the task's existing slice rather than zeroing it (Andrea Righi)
- Selftest: use the first online CPU from sched_getaffinity() instead of hardcoded CPU 0 (Andrea Righi)
- Selftest: clarify that nr_tx_stale counts attempted stale commits, not rejections detected by finish_dispatch() (Andrea Righi)
- Selftest: skip dispatch transaction path for CPU-pinned tasks (e.g. per-CPU kworkers) that cannot be inserted into the current CPU's local DSQ; fall back to the global DSQ instead
- Add comment to mark_direct_dispatch() explaining why qseq validation is not needed on the direct dispatch path (Andrea Righi)
- Remove duplicate scx_bpf_dsq_insert_begin BTF entry from scx_kfunc_ids_enqueue_dispatch (sashiko-bot)
- Use u64 consistently for qseq in the dispatch buffer path (scx_dsp_buf_ent, scx_dsq_insert_stage, finish_dispatch)
- Link to v3:
https://lore.kernel.org/r/[email protected]/

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3sched_ext: Rename scx_dsq_insert_commit() to scx_dsq_insert_stage()kernel/sched/ext.c+3 -30 replies
2/3sched_ext: Add dispatch transaction APIkernel/sched/ext.c kernel/sched/ext_internal.h tools/sched_ext/include/scx/common.bpf.h+108 -60 replies
3/3selftests/sched_ext: Add dispatch_cookie testtools/testing/selftests/sched_ext/Makefile tools/testing/selftests/sched_ext/dispatch_cookie.bpf.c tools/testing/selftests/sched_ext/dispatch_cookie.c+260 -01 reply

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v4 yet.

Read the v4 thread

Cover letter [PATCH v4 sched_ext/for-7.2 0/3] sched_ext: Add dispatch transaction API

scx_bpf_dsq_insert() captures the task's sequence number at insert time, so any pre-dispatch validity checks a BPF scheduler performs before the insert fall outside the race detection window.

Patch 1: Rename the internal scx_dsq_insert_commit() to scx_dsq_insert_stage() to better reflect its role as a staging step before the actual DSQ insertion. No functional change.

Patch 2: Introduce a dispatch transaction API with two new kfuncs.
- scx_bpf_dsq_insert_begin() captures the sequence number before any pre-dispatch checks, opening the transaction.
- scx_bpf_dsq_insert_commit() inserts the task using the early-captured token, extending the race detection window. If the token has gone stale (task was dequeued or claimed by another CPU), finish_dispatch() silently discards the entry. Both kfuncs are restricted to local DSQs, and commit() is callable only from ops.dispatch(). This is intended for schedulers that do not implement properly synchronized dequeue.

Patch 3: Add a selftest exercising the dispatch transaction API. The BPF scheduler captures the token in ops.dispatch() and flips CPU affinity on child tasks to trigger dequeue and re-enqueue, verifying that both successful and stale-token dispatch paths are exercised.

Test plan:
- Applied this series and ran selftests with vng, all tests pass.
- Confirmed selftest skips on older kernel.

This series is discussed in [1].
[1]: https://lore.kernel.org/r/[email protected]/

Changes in v4:
- Separate the internal rename into its own commit with no functional change; use scx_dsq_insert_stage() instead of scx_dsq_insert_buf() (Tejun Heo)
- Restrict scx_bpf_dsq_insert_commit() to ops.dispatch() only by moving it from scx_kfunc_ids_enqueue_dispatch to scx_kfunc_ids_dispatch (Tejun Heo)
- Restrict scx_bpf_dsq_insert_commit() to local DSQs at runtime:
targeting a non-local DSQ aborts the scheduler (Tejun Heo)
- Expand kdoc for both kfuncs with the full problem statement and use-case context (Tejun Heo)
- Add scx_bpf_dsq_insert_commit_args struct to stay within the BPF verifier's five-argument limit imposed by KF_IMPLICIT_ARGS (Andrea Righi)
- Selftest: pass slice=0 in commit_args to preserve the task's existing slice rather than zeroing it (Andrea Righi)
- Selftest: use the first online CPU from sched_getaffinity() instead of hardcoded CPU 0 (Andrea Righi)
- Selftest: clarify that nr_tx_stale counts attempted stale commits, not rejections detected by finish_dispatch() (Andrea Righi)
- Selftest: skip dispatch transaction path for CPU-pinned tasks (e.g. per-CPU kworkers) that cannot be inserted into the current CPU's local DSQ; fall back to the global DSQ instead
- Add comment to mark_direct_dispatch() explaining why qseq validation is not needed on the direct dispatch path (Andrea Righi)
- Remove duplicate scx_bpf_dsq_insert_begin BTF entry from scx_kfunc_ids_enqueue_dispatch (sashiko-bot)
- Use u64 consistently for qseq in the dispatch buffer path (scx_dsp_buf_ent, scx_dsq_insert_stage, finish_dispatch)
- Link to v3:
https://lore.kernel.org/r/[email protected]/

Changes in v3:
- Rename the API from cookie to dispatch transaction: kfuncs renamed from scx_bpf_task_get_cookie/scx_bpf_dsq_insert_with_cookie to scx_bpf_dsq_insert_begin/scx_bpf_dsq_insert_commit (Tejun Heo)
- Rename internal scx_dsq_insert_commit() to avoid collision with the new public kfunc (Tejun Heo)
- Move __ksym __weak declarations in common.bpf.h into the API commit
- Redesign selftest to call insert from ops.dispatch() with CPU affinity flips to actually exercise the race detection window (Tejun Heo)
- Skip selftest on older kernels lacking the dispatch transaction API
- Apply the same fix logic as commit e18002d2c791 to the selftest
- Link to v2:
https://lore.kernel.org/r/[email protected]/

Changes in v2:
- Reword kdoc for both kfuncs to avoid exposing qseq and ops_state as implementation details (Kuba Piecuch)
- Add __ksym __weak declarations for new kfuncs in common.bpf.h
- Add dispatch_cookie selftest (Kuba Piecuch)
- Link to v1:
https://lore.kernel.org/r/[email protected]/

Thanks,
Cheng-Yang

---

Cheng-Yang Chou (3):
sched_ext: Rename scx_dsq_insert_commit() to scx_dsq_insert_stage()
sched_ext: Add dispatch transaction API selftests/sched_ext: Add dispatch_cookie test

 kernel/sched/ext.c                            | 112 +++++++++++++-
 kernel/sched/ext_internal.h                   |   2 +-
 tools/sched_ext/include/scx/common.bpf.h      |   2 +
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/dispatch_cookie.bpf.c | 120 +++++++++++++++
 .../selftests/sched_ext/dispatch_cookie.c     | 139 ++++++++++++++++++
 6 files changed, 369 insertions(+), 7 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/dispatch_cookie.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/dispatch_cookie.c
Signature · 1 line
2.48.1