Skip to contentSkip to conversation

sched_ext: lockless peek operation for DSQs

PATCH Applied Ryan Newton 2 participants 4 messages Oct 15, 2025

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

v5 cover sched_ext: lockless peek operation for DSQs

This allows sched_ext schedulers an inexpensive operation to peek at the first element in a queue (DSQ), without creating an iterator and acquiring the lock on that queue.

Note that manual testing has thus far included a modified version of the example qmap scheduler that exercises peek, as well as a modified modified LAVD (from the SCX repo) that exercises peek. The attached test passes >1000 stress tests when run in concurrent VMs, and when run sequentially on the host kernel. Presently, tested on the below workstation and server processors.
- AMD Ryzen Threadripper PRO 7975WX 32-Cores
- AMD EPYC 9D64 88-Core Processor

Initial experiments indicate a substantial speedup (on schbench) when running an SCX scheduler with per-cpu DSQs and peeking each queue to retrieve the task with the minimum vruntime across all the CPUs.

---
Changes in v5:
- minor comment tweak requested in review
- add Reviewed-bys [email protected]

Changes in v4:
- review comments (from [email protected]) addressed, add Reviewed-by
- make the test much lighter weight with 4 rather than 100 workers
- link: https://lore.kernel.org/lkml/[email protected]/

Changes in v3:
- inline helpers and simplify
- coding style tweaks
- link: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- make peek() only work for user DSQs and error otherwise
- added a stress test component to the selftest that performs many peeks
- responded to review comments from [email protected] and [email protected]
- link: https://lore.kernel.org/lkml/[email protected]/

v1 link: https://lore.kernel.org/lkml/[email protected]/

Ryan Newton (2):
sched_ext: Add lockless peek operation for DSQs
sched_ext: Add a selftest for scx_bpf_dsq_peek

 include/linux/sched/ext.h                     |   1 +
 kernel/sched/ext.c                            |  58 +++-
 tools/sched_ext/include/scx/common.bpf.h      |   1 +
 tools/sched_ext/include/scx/compat.bpf.h      |  18 ++
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/peek_dsq.bpf.c        | 251 ++++++++++++++++++
 tools/testing/selftests/sched_ext/peek_dsq.c  | 224 ++++++++++++++++
 7 files changed, 552 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/peek_dsq.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/peek_dsq.c
Signature · 1 line
2.51.0
#1 lore raw

PATCH v5 1/2 sched_ext: Add lockless peek operation for DSQs

From: Ryan Newton <[email protected]>

The builtin DSQ queue data structures are meant to be used by a wide range of different sched_ext schedulers with different demands on these data structures. They might be per-cpu with low-contention, or high-contention shared queues. Unfortunately, DSQs have a coarse-grained lock around the whole data structure. Without going all the way to a lock-free, more scalable implementation, a small step we can take to reduce lock contention is to allow a lockless, small-fixed-cost peek at the head of the queue.

This change allows certain custom SCX schedulers to cheaply peek at queues, e.g. during load balancing, before locking them. But it represents a few extra memory operations to update the pointer each time the DSQ is modified, including a memory barrier on ARM so the write appears correctly ordered.

This commit adds a first_task pointer field which is updated atomically when the DSQ is modified, and allows any thread to peek at the head of the queue without holding the lock.

---
 include/linux/sched/ext.h                |  1 +
 kernel/sched/ext.c                       | 58 +++++++++++++++++++++++-
 tools/sched_ext/include/scx/common.bpf.h |  1 +
 tools/sched_ext/include/scx/compat.bpf.h | 18 ++++++++
 4 files changed, 76 insertions(+), 2 deletions(-)
include/linux/sched/ext.h +1 -0
index d82b7a9b0658..81478d4ae782 100644
@@ -58,6 +58,7 @@ enum scx_dsq_id_flags {
  */
 struct scx_dispatch_q {
 	raw_spinlock_t		lock;
+	struct task_struct __rcu *first_task; /* lockless peek at head */
 	struct list_head	list;	/* tasks in dispatch order */
 	struct rb_root		priq;	/* used to order by p->scx.dsq_vtime */
 	u32			nr;
kernel/sched/ext.c +56 -2

92 lines not on this page · raw message

tools/sched_ext/include/scx/common.bpf.h +1 -0
index eb3c99445cb3..e65b1eb668ea 100644
@@ -74,6 +74,7 @@ u32 scx_bpf_reenqueue_local(void) __ksym;
 void scx_bpf_kick_cpu(s32 cpu, u64 flags) __ksym;
 s32 scx_bpf_dsq_nr_queued(u64 dsq_id) __ksym;
 void scx_bpf_destroy_dsq(u64 dsq_id) __ksym;
+struct task_struct *scx_bpf_dsq_peek(u64 dsq_id) __ksym __weak;
 int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __ksym __weak;
 struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) __ksym __weak;
 void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak;
tools/sched_ext/include/scx/compat.bpf.h +18 -0
index e487c10b5e07..619a16f0d39a 100644
@@ -26,6 +26,24 @@ int bpf_cpumask_populate(struct cpumask *dst, void *src, size_t src__sz) __ksym
 	(bpf_ksym_exists(bpf_cpumask_populate) ?			\
 	 (bpf_cpumask_populate(cpumask, src, size__sz)) : -EOPNOTSUPP)
 
+/*
+ * v6.19: Introduce lockless peek API for user DSQs.
+ *
+ * Preserve the following macro until v6.21.
+ */
+static inline struct task_struct *__COMPAT_scx_bpf_dsq_peek(u64 dsq_id)
+{
+	struct task_struct *p = NULL;
+	struct bpf_iter_scx_dsq it;
+
+	if (bpf_ksym_exists(scx_bpf_dsq_peek))
+		return scx_bpf_dsq_peek(dsq_id);
+	if (!bpf_iter_scx_dsq_new(&it, dsq_id, 0))
+		p = bpf_iter_scx_dsq_next(&it);
+	bpf_iter_scx_dsq_destroy(&it);
+	return p;
+}
+
 /**
  * __COMPAT_is_enq_cpu_selected - Test if SCX_ENQ_CPU_SELECTED is on
  * in a compatible way. We will preserve this __COMPAT helper until v6.16.
Signature · 1 line
2.51.0
#2 lore raw

PATCH v5 2/2 sched_ext: Add a selftest for scx_bpf_dsq_peek

In reply to Ryan Newton

From: Ryan Newton <[email protected]>

This commit adds two tests. The first is the most basic unit test:
make sure an empty queue peeks as empty, and when we put one element in the queue, make sure peek returns that element.

However, even this simple test is a little complicated by the different behavior of scx_bpf_dsq_insert in different calling contexts:
- insert is for direct dispatch in enqueue
- insert is delayed when called from select_cpu

In this case we split the insert and the peek that verifies the result between enqueue/dispatch.

Note: An alternative would be to call scx_bpf_dsq_move_to_local on an empty queue, which in turn calls flush_dispatch_buf, in order to flush the buffered insert. Unfortunately, this is not viable within the enqueue path, as it attempts a voluntary context switch within an RCU read-side critical section.

The second test is a stress test that performs many peeks on all DSQs and records the observed tasks.

---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/peek_dsq.bpf.c        | 251 ++++++++++++++++++
 tools/testing/selftests/sched_ext/peek_dsq.c  | 224 ++++++++++++++++
 3 files changed, 476 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/peek_dsq.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/peek_dsq.c
tools/testing/selftests/sched_ext/Makefile +1 -0
index 9d9d6b4c38b0..5fe45f9c5f8f 100644
@@ -174,6 +174,7 @@ auto-test-targets :=			\
 	minimal				\
 	numa				\
 	allowed_cpus			\
+	peek_dsq			\
 	prog_run			\
 	reload_loop			\
 	select_cpu_dfl			\
tools/testing/selftests/sched_ext/peek_dsq.bpf.c new file +251 -0

251 lines not on this page · raw message

tools/testing/selftests/sched_ext/peek_dsq.c new file +224 -0

224 lines not on this page · raw message

Signature · 1 line
2.51.0
reviewer Applied
#3 lore raw
In reply to Ryan Newton
Quoted 3 lines · Ryan Newton

Ryan Newton (2):
sched_ext: Add lockless peek operation for DSQs
sched_ext: Add a selftest for scx_bpf_dsq_peek

Applied to sched_ext/for-6.19.

Thanks.

Signature · 1 line
tejun