Skip to contentSkip to conversation

sched_ext: lockless peek operation for DSQs

PATCH Superseded Ryan Newton 2 participants 5 messages Oct 3, 2025

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 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 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 to v1: https://lkml.org/lkml/2025/10/1/1375

Ryan Newton (3):
sched_ext: Add lockless peek operation for DSQs
sched_ext: optimize first_task update logic
sched_ext: Add a selftest for scx_bpf_dsq_peek

 include/linux/sched/ext.h                     |   1 +
 kernel/sched/ext.c                            |  73 ++++-
 tools/sched_ext/include/scx/common.bpf.h      |   1 +
 tools/sched_ext/include/scx/compat.bpf.h      |  19 ++
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/peek_dsq.bpf.c        | 268 ++++++++++++++++++
 tools/testing/selftests/sched_ext/peek_dsq.c  | 235 +++++++++++++++
 7 files changed, 596 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 v2 1/3 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                       | 59 ++++++++++++++++++++++++
 tools/sched_ext/include/scx/common.bpf.h |  1 +
 tools/sched_ext/include/scx/compat.bpf.h | 19 ++++++++
 4 files changed, 80 insertions(+)
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 +59 -0

89 lines not on this page · raw message

tools/sched_ext/include/scx/common.bpf.h +1 -0
index 06e2551033cb..fbf3e7f9526c 100644
@@ -75,6 +75,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 +19 -0
index dd9144624dc9..97b10c184b2c 100644
@@ -130,6 +130,25 @@ int bpf_cpumask_populate(struct cpumask *dst, void *src, size_t src__sz) __ksym
 	false;									\
 })
 
+
+/*
+ * 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
reviewer
#2 lore raw

Re: [PATCH v2 1/3] sched_ext: Add lockless peek operation for DSQs

Hi Ryan,

Quoted 46 lines · Ryan Newton

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                       | 59 ++++++++++++++++++++++++
 tools/sched_ext/include/scx/common.bpf.h |  1 +
 tools/sched_ext/include/scx/compat.bpf.h | 19 ++++++++
 4 files changed, 80 insertions(+)
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 +0 -0
index 2b0e88206d07..ea0fe4019eca 100644

kernel/sched/ext.c static void refill_task_slice_dfl(struct scx_sched *sch, struct task_struct *p) · line 894

Quoted 10 lines
kernel/sched/ext.c +7 -0
@@ -885,6 +885,27 @@ static void refill_task_slice_dfl(struct scx_sched *sch, struct task_struct *p)
 	__scx_add_event(sch, SCX_EV_REFILL_SLICE_DFL, 1);
 }
 
+/* while holding dsq->lock and does nothing on builtin DSQs */
+static inline void dsq_set_first_task(struct scx_dispatch_q *dsq,
+				      struct task_struct *p)
+{
+	if (dsq->id & SCX_DSQ_FLAG_BUILTIN)
+		return;
+	rcu_assign_pointer(dsq->first_task, p);

Do we need this helper? Maybe we can just do this in the caller:

	if (!(dsq->id & SCX_DSQ_FLAG_BUILTIN))
		rcu_assign_pointer(dsq->first_task, p);

kernel/sched/ext.c static void refill_task_slice_dfl(struct scx_sched *sch, struct task_struct *p) · line 902

Quoted 8 lines
kernel/sched/ext.c +8 -0
+}
+
+/* while holding dsq->lock and does nothing on builtin DSQs */
+static void dsq_update_first_task(struct scx_dispatch_q *dsq)
+{
+	struct task_struct *first_task;
+
+	if ((dsq->id & SCX_DSQ_FLAG_BUILTIN))

nit: remove the extra ().

Quoted 20 lines
kernel/sched/ext.c +9 -0
+		return;
+
+	first_task = nldsq_next_task(dsq, NULL, false);
+	rcu_assign_pointer(dsq->first_task, first_task);
+}
+
 static void dispatch_enqueue(struct scx_sched *sch, struct scx_dispatch_q *dsq,
 			     struct task_struct *p, u64 enq_flags)
 {
@@ -959,6 +980,9 @@ static void dispatch_enqueue(struct scx_sched *sch, struct scx_dispatch_q *dsq,
 			list_add_tail(&p->scx.dsq_list.node, &dsq->list);
 	}
 
+	/* even the add_tail code path may have changed the first element */
+	dsq_update_first_task(dsq);
+
 	/* seq records the order tasks are queued, used by BPF DSQ iterator */
 	dsq->seq++;
 	p->scx.dsq_seq = dsq->seq;

kernel/sched/ext.c static void task_unlink_from_dsq(struct task_struct *p, · line 1040

Quoted 4 lines
kernel/sched/ext.c +1 -0
@@ -1013,6 +1037,7 @@ static void task_unlink_from_dsq(struct task_struct *p,
 
 	list_del_init(&p->scx.dsq_list.node);
 	dsq_mod_nr(dsq, -1);
+	dsq_update_first_task(dsq);

Same here:

	if (!(dsq->id & SCX_DSQ_FLAG_BUILTIN))
		dsq_update_first_task(dsq);

And remove the check inside dsq_update_first_task() - feel free to ignore this, just a stylish comment.

Quoted 15 lines
kernel/sched/ext.c +8 -0
 }
 
 static void dispatch_dequeue(struct rq *rq, struct task_struct *p)
@@ -6084,6 +6109,39 @@ __bpf_kfunc void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it)
 	kit->dsq = NULL;
 }
 
+/**
+ * scx_bpf_dsq_peek - Lockless peek at the first element.
+ * @dsq_id: DSQ to examine.
+ *
+ * Read the first element in the DSQ. This is semantically equivalent to using
+ * the DSQ iterator, but is lockfree.
+ *
+ * Returns the pointer, or NULL indicates an empty queue OR internal error.

kernel/sched/ext.c __bpf_kfunc void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) · line 6129

Quoted 10 lines
kernel/sched/ext.c +10 -0
+ */
+__bpf_kfunc struct task_struct *scx_bpf_dsq_peek(u64 dsq_id)
+{
+	struct scx_sched *sch;
+	struct scx_dispatch_q *dsq;
+
+	sch = rcu_dereference(scx_root);
+	/* Rather than return ERR_PTR(-ENODEV), we follow the precedent of
+	 * other functions and let the peek fail but we continue on.
+	 */

Well, if sch == NULL the root scheduler doesn't exist, so it's not too unreasonable to return NULL (meaning that the DSQ is empty).

What I'm saying is that we either return ENODEV (but then we need to force the caller to handle that) or maybe we can just drop this comment, otherwise I think it's a bit confusing mentioning about ENODEV without returning it.

kernel/sched/ext.c __bpf_kfunc void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) · line 6139

Quoted 10 lines
kernel/sched/ext.c +10 -0
+	if (unlikely(!sch))
+		return NULL;
+
+	dsq = find_user_dsq(sch, dsq_id);
+	if (unlikely(!dsq)) {
+		scx_error(sch, "peek on non-existent DSQ 0x%llx", dsq_id);
+		return NULL;
+	}
+	if (unlikely((dsq->id & SCX_DSQ_FLAG_BUILTIN))) {
+		scx_error(NULL, "peek disallowed on builtin DSQ 0x%llx", dsq_id);

This should be sch instead of NULL, right?

Moreover, I think we can move this check before find_user_dsq(), so we can save a call to find_user_dsq() that would fail.

Quoted 61 lines
kernel/sched/ext.c +6 -0
+		return NULL;
+	}
+	return rcu_dereference(dsq->first_task);
+}
+
 __bpf_kfunc_end_defs();
 
 static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
@@ -6641,6 +6699,7 @@ BTF_KFUNCS_START(scx_kfunc_ids_any)
 BTF_ID_FLAGS(func, scx_bpf_kick_cpu)
 BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued)
 BTF_ID_FLAGS(func, scx_bpf_destroy_dsq)
+BTF_ID_FLAGS(func, scx_bpf_dsq_peek, KF_RCU_PROTECTED | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_scx_dsq_new, KF_ITER_NEW | KF_RCU_PROTECTED)
 BTF_ID_FLAGS(func, bpf_iter_scx_dsq_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_scx_dsq_destroy, KF_ITER_DESTROY)
tools/sched_ext/include/scx/common.bpf.h +1 -0
index 06e2551033cb..fbf3e7f9526c 100644
@@ -75,6 +75,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 +19 -0
index dd9144624dc9..97b10c184b2c 100644
@@ -130,6 +130,25 @@ int bpf_cpumask_populate(struct cpumask *dst, void *src, size_t src__sz) __ksym
 	false;									\
 })
 
+
+/*
+ * 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

Thanks,
-Andrea

#3 lore raw

PATCH v2 2/3 sched_ext: optimize first_task update logic

In reply to Ryan Newton

From: Ryan Newton <[email protected]>

This is a follow-on optimization to the prior commit which added a lockless peek operation on DSQs. That implementation is correct and simple, but elides several optimizations.

Previously, we read the first_task using the same slowpath, irrespective of where we enqueue the task. With this change, we instead base the update on what we know about the calling context. On both insert and removal we can break down whether the change (1) definitely, (2) never, or (3) sometimes changes first task. In some cases we know what the new first task will be, and can set it more directly.

---
 kernel/sched/ext.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
kernel/sched/ext.c +16 -6
index ea0fe4019eca..53d7ba64a483 100644
@@ -965,8 +965,11 @@ static void dispatch_enqueue(struct scx_sched *sch, struct scx_dispatch_q *dsq,
 				container_of(rbp, struct task_struct,
 					     scx.dsq_priq);
 			list_add(&p->scx.dsq_list.node, &prev->scx.dsq_list.node);
+			/* first task unchanged - no update needed */
 		} else {
 			list_add(&p->scx.dsq_list.node, &dsq->list);
+			/* not builtin and new task is at head - use fastpath */
+			rcu_assign_pointer(dsq->first_task, p);
 		}
 	} else {
 		/* a FIFO DSQ shouldn't be using PRIQ enqueuing */
@@ -974,15 +977,20 @@ static void dispatch_enqueue(struct scx_sched *sch, struct scx_dispatch_q *dsq,
 			scx_error(sch, "DSQ ID 0x%016llx already had PRIQ-enqueued tasks",
 				  dsq->id);
 
-		if (enq_flags & (SCX_ENQ_HEAD | SCX_ENQ_PREEMPT))
+		if (enq_flags & (SCX_ENQ_HEAD | SCX_ENQ_PREEMPT)) {
 			list_add(&p->scx.dsq_list.node, &dsq->list);
-		else
+			/* new task inserted at head - use fastpath */
+			dsq_set_first_task(dsq, p);
+		} else {
+			bool was_empty;
+
+			was_empty = list_empty(&dsq->list);
 			list_add_tail(&p->scx.dsq_list.node, &dsq->list);
+			if (was_empty)
+				dsq_set_first_task(dsq, p);
+		}
 	}
 
-	/* even the add_tail code path may have changed the first element */
-	dsq_update_first_task(dsq);
-
 	/* seq records the order tasks are queued, used by BPF DSQ iterator */
 	dsq->seq++;
 	p->scx.dsq_seq = dsq->seq;
@@ -1035,9 +1043,11 @@ static void task_unlink_from_dsq(struct task_struct *p,
 		p->scx.dsq_flags &= ~SCX_TASK_DSQ_ON_PRIQ;
 	}
 
+	if (dsq->first_task == p)
+		dsq_update_first_task(dsq);
+
 	list_del_init(&p->scx.dsq_list.node);
 	dsq_mod_nr(dsq, -1);
-	dsq_update_first_task(dsq);
 }
 
 static void dispatch_dequeue(struct rq *rq, struct task_struct *p)
Signature · 1 line
2.51.0
#4 lore raw

PATCH v2 3/3 sched_ext: Add a selftest for scx_bpf_dsq_peek

In reply to Ryan Newton

From: Ryan Newton <[email protected]>

This 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.

---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/peek_dsq.bpf.c        | 268 ++++++++++++++++++
 tools/testing/selftests/sched_ext/peek_dsq.c  | 235 +++++++++++++++
 3 files changed, 504 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 +268 -0

268 lines not on this page · raw message

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

235 lines not on this page · raw message

Signature · 1 line
2.51.0