sched_ext: Add cookie API for early qseq capture
Subsystems (2)
PATCH 1/1 sched_ext: Add cookie API for early qseq capture
scx_bpf_dsq_insert() captures qseq at insert time. Any BPF-side pre-dispatch checks (e.g. CPU affinity validation) performed before the insert are outside the qseq protection window: a dequeue/re-enqueue race occurring between the check and the insert goes undetected by finish_dispatch(), which sees a matching qseq and proceeds with stale assumptions.
Introduce two new kfuncs to extend the qseq protection window:
- scx_bpf_task_get_cookie(p)
Reads @p's current qseq from ops_state and returns it as an opaque u64 cookie. The BPF scheduler should call this before performing pre-dispatch validity checks. The cookie may be stored in BPF maps to support cross-CPU dispatch patterns.
- scx_bpf_dsq_insert_with_cookie(p, dsq_id, enq_flags, cookie) Like scx_bpf_dsq_insert() with slice=0, but uses the cookie's qseq instead of re-reading ops_state at insert time. If @p was dequeued and re-enqueued between get_cookie() and here, qseq will have changed and finish_dispatch() will silently discard the stale dispatch. Use scx_bpf_task_set_slice() to set a non-default slice.
To support explicit qseq passing, refactor scx_dsq_insert_commit() to take qseq as a parameter; all existing callers capture ops_state at their call site, preserving the original behavior.
This mechanism is intended for schedulers that do not implement properly synchronized dequeue. A scheduler whose ops.dequeue() synchronizes atomically with the dispatch path does not need cookies.
---
As discussed in [1].
[1]: https://lore.kernel.org/r/[email protected]/
kernel/sched/ext.c | 65 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-)
kernel/sched/ext.c +61 -4
@@ -8303,7 +8303,7 @@ static bool scx_dsq_insert_preamble(struct scx_sched *sch, struct task_struct *p }static void scx_dsq_insert_commit (struct scx_sched *sch,struct task_struct *p, -u64 dsq_id,u64 enq_flags) +u64 dsq_id,u64 enq_flags,unsigned long qseq) {struct scx_dsp_ctx *dspc = &this_cpu_ptr (sch->pcpu)->dsp_ctx;struct task_struct *ddsp_task;
@@ -8321,7 +8321,7 @@ static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p, dspc->buf[dspc->cursor++] = (struct scx_dsp_buf_ent ){ .task = p, - .qseq =atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK , + .qseq = qseq, .dsq_id = dsq_id, .enq_flags = enq_flags, };
@@ -8388,7 +8388,8 @@ __bpf_kfunc bool scx_bpf_dsq_insert___v2(struct task_struct *p, u64 dsq_id,else p->scx.slice = p->scx.slice ?:1 ; -scx_dsq_insert_commit (sch, p, dsq_id, enq_flags); +scx_dsq_insert_commit (sch, p, dsq_id, enq_flags, +atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK );return true; }
@@ -8416,7 +8417,8 @@ static bool scx_dsq_insert_vtime(struct scx_sched *sch, struct task_struct *p, p->scx.dsq_vtime = vtime; -scx_dsq_insert_commit (sch, p, dsq_id, enq_flags |SCX_ENQ_DSQ_PRIQ ); +scx_dsq_insert_commit (sch, p, dsq_id, enq_flags |SCX_ENQ_DSQ_PRIQ , +atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK );return true; }
@@ -8505,13 +8507,67 @@ __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id,scx_dsq_insert_vtime (sch, p, dsq_id, slice, vtime, enq_flags); } +/** +* scx_bpf_task_get_cookie - Get an opaque dispatch cookie for a task +* @p: task_struct to read cookie from +* +* Returns an opaque u64 cookie encoding @p's current qseq. Call this +* before pre-dispatch validity checks and pass the result to +* scx_bpf_dsq_insert_with_cookie() to extend the qseq protection window. +* +* For schedulers that do not implement properly synchronized dequeue only. +*/ +__bpf_kfunc u64 scx_bpf_task_get_cookie(struct task_struct *p) +{ +return atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK ; +} + +/** +* scx_bpf_dsq_insert_with_cookie - Insert a task using an early-captured cookie +* @p: task_struct to insert +* @dsq_id: DSQ to insert into +* @enq_flags: SCX_ENQ_* +* @cookie: cookie from scx_bpf_task_get_cookie() +* @aux: implicit BPF argument +* +* Like scx_bpf_dsq_insert() with slice=0, but uses @cookie's qseq instead +* of re-reading ops_state at insert time. A stale cookie causes +* finish_dispatch() to silently discard the dispatch. Use +* scx_bpf_task_set_slice() to set a non-default slice. +* +* Returns %true on success, %false on failure. +*/ +__bpf_kfunc bool scx_bpf_dsq_insert_with_cookie(struct task_struct *p, +u64 dsq_id,u64 enq_flags, +u64 cookie, +const struct bpf_prog_aux *aux) +{ +struct scx_sched *sch; + +guard (rcu)(); + sch =scx_prog_sched (aux); +if (unlikely (!sch)) +return false; + +if (!scx_dsq_insert_preamble (sch, p, dsq_id, &enq_flags)) +return false; + + p->scx.slice = p->scx.slice ?:1 ; + +scx_dsq_insert_commit (sch, p, dsq_id, enq_flags, (unsigned long )cookie); + +return true; +} +__bpf_kfunc_end_defs (); BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch) BTF_ID_FLAGS(func, scx_bpf_dsq_insert, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, scx_bpf_dsq_insert___v2, KF_IMPLICIT_ARGS | KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_dsq_insert_with_cookie, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, __scx_bpf_dsq_insert_vtime, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, scx_bpf_dsq_insert_vtime, KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU) BTF_KFUNCS_END(scx_kfunc_ids_enqueue_dispatch) static const struct btf_kfunc_id_set scx_kfunc_set_enqueue_dispatch = {
@@ -10181,6 +10237,7 @@ BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_cpu_rq, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL)BTF_ID_FLAGS (func, scx_bpf_cpu_curr,KF_IMPLICIT_ARGS |KF_RET_NULL |KF_RCU_PROTECTED )
Signature · 1 line
2.48.1
Hi Cheng-Yang,
kernel/sched/ext.c __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, · line 8516
Quoted 11 lines · Cheng-Yang Chou
kernel/sched/ext.c +7
-0@@ -8505,13 +8507,67 @@ __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id,scx_dsq_insert_vtime (sch, p, dsq_id, slice, vtime, enq_flags); } +/** +* scx_bpf_task_get_cookie - Get an opaque dispatch cookie for a task +* @p: task_struct to read cookie from +* +* Returns an opaque u64 cookie encoding @p's current qseq. Call this +* before pre-dispatch validity checks and pass the result to +* scx_bpf_dsq_insert_with_cookie() to extend the qseq protection window.
This is just my opinion, but I thought the point of introducing cookies is to not have to expose qseq as a concept to the BPF schedulers (or people writing them). This includes comments documenting the kfuncs.
Wouldn't it be better if we just focus on describing the semantics without mentioning qseq? Perhaps something like:
Returns an opaque u64 dispatch cookie. Pass the cookie to scx_bpf_dsq_insert_with_cookie() to extend the time window during which sched_ext will detect racing dequeues/enqueues of the task being dispatched. The extended time window begins with the call to scx_bpf_task_get_cookie() and ends at the same point as for dispatches without cookies, i.e. at the point where sched_ext attempts to finish the dispatch.
Review __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, · line 8518
Quoted 2 lines
(quoted diff) +2
-0+ * + * For schedulers that do not implement properly synchronized dequeue only.
"only" seems quite strong here. How about "This API is intended for schedulers that do not implement properly synchronized dequeue"?
Quoted 56 lines
kernel/sched/ext.c +45
-0+*/ +__bpf_kfunc u64 scx_bpf_task_get_cookie(struct task_struct *p) +{ +return atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK ; +} + +/** +* scx_bpf_dsq_insert_with_cookie - Insert a task using an early-captured cookie +* @p: task_struct to insert +* @dsq_id: DSQ to insert into +* @enq_flags: SCX_ENQ_* +* @cookie: cookie from scx_bpf_task_get_cookie() +* @aux: implicit BPF argument +* +* Like scx_bpf_dsq_insert() with slice=0, but uses @cookie's qseq instead +* of re-reading ops_state at insert time. A stale cookie causes +* finish_dispatch() to silently discard the dispatch. Use +* scx_bpf_task_set_slice() to set a non-default slice. +* +* Returns %true on success, %false on failure. +*/ +__bpf_kfunc bool scx_bpf_dsq_insert_with_cookie(struct task_struct *p, +u64 dsq_id,u64 enq_flags, +u64 cookie, +const struct bpf_prog_aux *aux) +{ +struct scx_sched *sch; + +guard (rcu)(); + sch =scx_prog_sched (aux); +if (unlikely (!sch)) +return false; + +if (!scx_dsq_insert_preamble (sch, p, dsq_id, &enq_flags)) +return false; + + p->scx.slice = p->scx.slice ?:1 ; + +scx_dsq_insert_commit (sch, p, dsq_id, enq_flags, (unsigned long )cookie); + +return true; +} +__bpf_kfunc_end_defs (); BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch) BTF_ID_FLAGS(func, scx_bpf_dsq_insert, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, scx_bpf_dsq_insert___v2, KF_IMPLICIT_ARGS | KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_dsq_insert_with_cookie, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, __scx_bpf_dsq_insert_vtime, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, scx_bpf_dsq_insert_vtime, KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU) BTF_KFUNCS_END(scx_kfunc_ids_enqueue_dispatch) static const struct btf_kfunc_id_set scx_kfunc_set_enqueue_dispatch = {
kernel/sched/ext.c BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) · line 10243
Quoted 7 lines
kernel/sched/ext.c +1
-0@@ -10181,6 +10237,7 @@ BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_cpu_rq, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL)BTF_ID_FLAGS (func, scx_bpf_cpu_curr,KF_IMPLICIT_ARGS |KF_RET_NULL |KF_RCU_PROTECTED )
Thank you for the patch!
Could you add a simple selftest for the two new kfuncs?
Thanks,
Kuba
Hi Kuba,
Quoted 20 lines · Kuba Piecuch
kernel/sched/ext.c __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, · line 8516
Quoted 11 lines · Cheng-Yang Chou
kernel/sched/ext.c +7
-0@@ -8505,13 +8507,67 @@ __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id,scx_dsq_insert_vtime (sch, p, dsq_id, slice, vtime, enq_flags); } +/** +* scx_bpf_task_get_cookie - Get an opaque dispatch cookie for a task +* @p: task_struct to read cookie from +* +* Returns an opaque u64 cookie encoding @p's current qseq. Call this +* before pre-dispatch validity checks and pass the result to +* scx_bpf_dsq_insert_with_cookie() to extend the qseq protection window. This is just my opinion, but I thought the point of introducing cookies is to not have to expose qseq as a concept to the BPF schedulers (or people writing them). This includes comments documenting the kfuncs.
Wouldn't it be better if we just focus on describing the semantics without mentioning qseq? Perhaps something like:
Quoted 6 lines
Returns an opaque u64 dispatch cookie. Pass the cookie to
scx_bpf_dsq_insert_with_cookie()to extend the time window during whichsched_extwill detect racing dequeues/enqueues of the task being dispatched. The extended time window begins with the call toscx_bpf_task_get_cookie()and ends at the same point as for dispatches without cookies, i.e. at the point wheresched_extattempts to finish the dispatch.
Agreed, will update in v2 patch.
Quoted 7 lines
Review __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, · line 8518
Quoted 3 lines
(quoted diff) +2
-0+ * + * For schedulers that do not implement properly synchronized dequeue only."only" seems quite strong here. How about "This API is intended for schedulers that do not implement properly synchronized dequeue"?
Ack.
Quoted 66 lines
Quoted 56 lines
kernel/sched/ext.c +45
-0+*/ +__bpf_kfunc u64 scx_bpf_task_get_cookie(struct task_struct *p) +{ +return atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK ; +} + +/** +* scx_bpf_dsq_insert_with_cookie - Insert a task using an early-captured cookie +* @p: task_struct to insert +* @dsq_id: DSQ to insert into +* @enq_flags: SCX_ENQ_* +* @cookie: cookie from scx_bpf_task_get_cookie() +* @aux: implicit BPF argument +* +* Like scx_bpf_dsq_insert() with slice=0, but uses @cookie's qseq instead +* of re-reading ops_state at insert time. A stale cookie causes +* finish_dispatch() to silently discard the dispatch. Use +* scx_bpf_task_set_slice() to set a non-default slice. +* +* Returns %true on success, %false on failure. +*/ +__bpf_kfunc bool scx_bpf_dsq_insert_with_cookie(struct task_struct *p, +u64 dsq_id,u64 enq_flags, +u64 cookie, +const struct bpf_prog_aux *aux) +{ +struct scx_sched *sch; + +guard (rcu)(); + sch =scx_prog_sched (aux); +if (unlikely (!sch)) +return false; + +if (!scx_dsq_insert_preamble (sch, p, dsq_id, &enq_flags)) +return false; + + p->scx.slice = p->scx.slice ?:1 ; + +scx_dsq_insert_commit (sch, p, dsq_id, enq_flags, (unsigned long )cookie); + +return true; +} +__bpf_kfunc_end_defs (); BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch) BTF_ID_FLAGS(func, scx_bpf_dsq_insert, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, scx_bpf_dsq_insert___v2, KF_IMPLICIT_ARGS | KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_dsq_insert_with_cookie, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, __scx_bpf_dsq_insert_vtime, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, scx_bpf_dsq_insert_vtime, KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU) BTF_KFUNCS_END(scx_kfunc_ids_enqueue_dispatch) static const struct btf_kfunc_id_set scx_kfunc_set_enqueue_dispatch = {kernel/sched/ext.c BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) · line 10243
Quoted 7 lines
kernel/sched/ext.c +1
-0@@ -10181,6 +10237,7 @@ BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_cpu_rq, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL)BTF_ID_FLAGS (func, scx_bpf_cpu_curr,KF_IMPLICIT_ARGS |KF_RET_NULL |KF_RCU_PROTECTED )Thank you for the patch!
Quoted 1 line
Could you add a simple selftest for the two new kfuncs?
Sure, will add a selftest in v2, thanks! ^_^
Signature · 2 lines
Cheers, Cheng-Yang
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Unprotected task state mutation causing data race on slice - [High] Missing acquire memory barrier…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Unprotected task state mutation causing data race on slice
- [High] Missing acquire memory barrier in scx_bpf_task_get_cookie() defeats sequence-lock protection
- [Medium] Missing API variant for PRIQ (vtime-ordered) DSQ insertion
--
commit c9d30798b70b7101ea880a0f906790c5875384ac
Author: Cheng-Yang Chou <[email protected]>
sched_ext: Add cookie API for early qseq capture
This commit introduces two new kfuncs, scx_bpf_task_get_cookie() and scx_bpf_dsq_insert_with_cookie(), to allow BPF schedulers to capture a task's qseq before performing pre-dispatch checks and safely insert it if the sequence hasn't changed.
Quoted 4 lines
kernel/sched/ext.c +0
-0
[ ... ]
kernel/sched/ext.c __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, · line 8522
Quoted 13 lines
kernel/sched/ext.c +13
-0+/** +* scx_bpf_task_get_cookie - Get an opaque dispatch cookie for a task +* @p: task_struct to read cookie from +* +* Returns an opaque u64 cookie encoding @p's current qseq. Call this +* before pre-dispatch validity checks and pass the result to +* scx_bpf_dsq_insert_with_cookie() to extend the qseq protection window. +* +* For schedulers that do not implement properly synchronized dequeue only. +*/ +__bpf_kfunc u64 scx_bpf_task_get_cookie(struct task_struct *p) +{ +return atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK ;
Should this use atomic_long_read_acquire() instead of a relaxed atomic read?
Since this conceptually starts a sequence-lock read-side critical section, a relaxed read doesn't emit hardware memory barriers. On weakly-ordered architectures, the CPU could reorder the subsequent BPF memory loads (the validity checks) to execute before the ops_state read.
If a concurrent dequeue/re-enqueue race occurs, the BPF checks could be evaluated using stale task data, but the delayed ops_state read captures the new qseq. The protection window is defeated, and finish_dispatch() would accept the dispatch despite violated constraints.
Quoted 23 lines
kernel/sched/ext.c +23
-0+} + +/** +* scx_bpf_dsq_insert_with_cookie - Insert a task using an early-captured cookie +* @p: task_struct to insert +* @dsq_id: DSQ to insert into +* @enq_flags: SCX_ENQ_* +* @cookie: cookie from scx_bpf_task_get_cookie() +* @aux: implicit BPF argument +* +* Like scx_bpf_dsq_insert() with slice=0, but uses @cookie's qseq instead +* of re-reading ops_state at insert time. A stale cookie causes +* finish_dispatch() to silently discard the dispatch. Use +* scx_bpf_task_set_slice() to set a non-default slice. +* +* Returns %true on success, %false on failure. +*/ +__bpf_kfunc bool scx_bpf_dsq_insert_with_cookie(struct task_struct *p, +u64 dsq_id,u64 enq_flags, +u64 cookie, +const struct bpf_prog_aux *aux) +{ +struct scx_sched *sch;
kernel/sched/ext.c __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, · line 8555
Quoted 10 lines
kernel/sched/ext.c +10
-0+ +guard (rcu)(); + sch =scx_prog_sched (aux); +if (unlikely (!sch)) +return false; + +if (!scx_dsq_insert_preamble (sch, p, dsq_id, &enq_flags)) +return false; + + p->scx.slice = p->scx.slice ?:1 ;
Does modifying the slice here create a data race if the task is currently running on another CPU?
Because this kfunc is meant for scenarios where the task reference might be stale due to a concurrent dequeue/re-enqueue race, the task might be in the TASK_RUNNING state elsewhere.
Modifying p->scx.slice without holding the task's current rq->lock can race with update_curr_scx() on the other CPU, potentially overwriting the slice decrement from the timer tick.
kernel/sched/ext.c __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, · line 8560
Quoted 5 lines
kernel/sched/ext.c +5
-0+ +scx_dsq_insert_commit (sch, p, dsq_id, enq_flags, (unsigned long )cookie); + +return true; +}
Is there a missing API variant for inserting into PRIQ (vtime-ordered) DSQs with a cookie?
To insert a task into a PRIQ DSQ, SCX_ENQ_DSQ_PRIQ must be set. However, BPF schedulers cannot manually pass this flag in enq_flags because scx_dsq_insert_preamble() rejects it as part of __SCX_ENQ_INTERNAL_MASK. This prevents BPF schedulers from using the new cookie mechanism for vtime-ordered priority queues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1