sched_ext: Invalidate dispatch decisions on CPU affinity changes
Subsystems (2)
Andrea Righi
reviewerPATCH v2 1/1 sched_ext: Invalidate dispatch decisions on CPU affinity changes
A BPF scheduler may rely on p->cpus_ptr from ops.dispatch() to select a target CPU. However, task affinity can change between the dispatch decision and its finalization in finish_dispatch(). When this happens, the scheduler may attempt to dispatch a task to a CPU that is no longer allowed, resulting in fatal errors such as:
EXIT: runtime error (SCX_DSQ_LOCAL[_ON] target CPU 10 not allowed for stress-ng-race-[13565])
This race exists because ops.dispatch() runs without holding the task's run queue lock, allowing a concurrent set_cpus_allowed() to update p->cpus_ptr while the BPF scheduler is still using it. The dispatch is then finalized using stale affinity information.
Example timeline:
CPU0 CPU1
---- ----
task_rq_lock(p)
if (cpumask_test_cpu(cpu, p->cpus_ptr))
set_cpus_allowed_scx(p, new_mask)
task_rq_unlock(p)
scx_bpf_dsq_insert(p,
SCX_DSQ_LOCAL_ON | cpu, 0)With commit ebf1ccff79c4 ("sched_ext: Fix ops.dequeue() semantics"), BPF schedulers can avoid the affinity race by tracking task state and handling %SCX_DEQ_SCHED_CHANGE in ops.dequeue(): when a task is dequeued due to a property change, the scheduler can update the task state and skip the direct dispatch from ops.dispatch() for non-queued tasks.
However, schedulers that do not implement task state tracking and dispatch directly to a local DSQ directly from ops.dispatch() may trigger the scx_error() condition when the kernel validates the destination in dispatch_to_local_dsq().
Improve this by shooting down in-flight dispatches from the dequeue path in the sched_ext core, instead of using the global DSQ as a fallback. When a QUEUED task is dequeued, increment the runqueue's ops_qseq before transitioning the task's ops_state to NONE. A finish_dispatch() that runs after the transition sees NONE and drops the dispatch, one that runs later, after the task has been re-enqueued (with the new qseq), sees a qseq mismatch and also drops. Either way the stale dispatch is discarded and the task is already, or will be, handled by the scheduler again.
Since this change removes the global DSQ fallback, also drop %SCX_ENQ_GDSQ_FALLBACK, which is now unused.
This allows reducing boilerplate in BPF schedulers for task state tracking and simplifies their implementation.
---
Changes in v2:
- Rework the patch based on the new ops.dequeue() semantic
- Drop SCX_ENQ_GDSQ_FALLBACK
- Link to v1: https://lore.kernel.org/all/[email protected]
kernel/sched/ext.c | 55 +++++++++++++++++++++++++++++-------- kernel/sched/ext_internal.h | 1 - 2 files changed, 43 insertions(+), 13 deletions(-)
kernel/sched/ext.c +43 -12
@@ -1382,10 +1382,8 @@ static void dsq_inc_nr(struct scx_dispatch_q *dsq, struct task_struct *p, u64 en* e.g. SAVE/RESTORE cycles and slice extensions. */ if (enq_flags &SCX_ENQ_IMMED ) { -if (unlikely (dsq->id !=SCX_DSQ_LOCAL )) { -WARN_ON_ONCE (!(enq_flags &SCX_ENQ_GDSQ_FALLBACK )); +if (unlikely (dsq->id !=SCX_DSQ_LOCAL ))return ; - } p->scx.flags |=SCX_TASK_IMMED ; }
@@ -2043,6 +2041,13 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)*/ BUG ();case SCX_OPSS_QUEUED : +/* +* Invalidate any in-flight dispatches for this task. The +* task is leaving the runqueue, so any dispatch decision +* made while it was queued is stale. +*/ + rq->scx.ops_qseq++; +/* A queued task must always be in BPF scheduler's custody */ WARN_ON_ONCE (!(p->scx.flags &SCX_TASK_IN_CUSTODY )); if (atomic_long_try_cmpxchg(&p->scx.ops_state, &opss,
@@ -2390,8 +2395,10 @@ static bool consume_remote_task(struct rq *this_rq,* will change. As @p's task_rq is locked, this function doesn't need to use the * holding_cpu mechanism. * -* On return, @src_dsq is unlocked and only @p's new task_rq, which is the -* return value, is locked. +* On success, @src_dsq is unlocked and only @p's new task_rq, which is the +* return value, is locked. On failure (affinity change invalidated the +* move), returns NULL with @src_dsq still locked and task remaining in +* @src_dsq. */ static struct rq *move_task_between_dsqs(struct scx_sched *sch, struct task_struct *p, u64 enq_flags,
@@ -2408,9 +2415,11 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch, dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq);if (src_rq != dst_rq &&unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { - dst_dsq =find_global_dsq (sch,task_cpu (p)); - dst_rq = src_rq; - enq_flags |=SCX_ENQ_GDSQ_FALLBACK ; +/* +* Affinity changed after dispatch: abort the move, +* task stays on src_dsq. +*/ +return NULL ; } }else {/* no need to migrate if destination is a non-local DSQ */
@@ -2537,9 +2546,26 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, }if (src_rq != dst_rq && -unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { -dispatch_enqueue (sch, rq,find_global_dsq (sch,task_cpu (p)), p, - enq_flags |SCX_ENQ_CLEAR_OPSS |SCX_ENQ_GDSQ_FALLBACK ); +unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, false))) { +/* +* Affinity changed after dispatch decision and the task +* can't run anymore on the destination rq. +* +* Drop the dispatch, the task will be re-enqueued. Set the +* task back to QUEUED so dequeue (if waiting) can proceed +* using current qseq from the task's rq. +*/ +if (src_rq != rq) { +raw_spin_rq_unlock (rq); +raw_spin_rq_lock (src_rq); + } +atomic_long_set_release (&p->scx.ops_state, +SCX_OPSS_QUEUED | + (src_rq->scx.ops_qseq <<SCX_OPSS_QSEQ_SHIFT )); +if (src_rq != rq) { +raw_spin_rq_unlock (src_rq); +raw_spin_rq_lock (rq); + }return ; }
@@ -8112,7 +8138,12 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,/* execute move */ locked_rq =move_task_between_dsqs (sch, p, enq_flags, src_dsq, dst_dsq); - dispatched = true; +if (locked_rq) { + dispatched = true; + }else { +raw_spin_unlock (&src_dsq->lock); + locked_rq = src_rq; + } out:if (in_balance) {if (this_rq != locked_rq) {
kernel/sched/ext_internal.h +0 -1
@@ -1145,7 +1145,6 @@ enum scx_enq_flags {SCX_ENQ_CLEAR_OPSS =1LLU <<56 ,SCX_ENQ_DSQ_PRIQ =1LLU <<57 , SCX_ENQ_NESTED = 1LLU << 58, - SCX_ENQ_GDSQ_FALLBACK = 1LLU << 59, /* fell back to global DSQ */ }; enum scx_deq_flags {
Signature · 1 line
2.53.0
Hi Andrea,
kernel/sched/ext.c static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags) · line 2049
Quoted 10 lines · Andrea Righi
kernel/sched/ext.c +6
-0@@ -2043,6 +2041,13 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)*/ BUG ();case SCX_OPSS_QUEUED : +/* +* Invalidate any in-flight dispatches for this task. The +* task is leaving the runqueue, so any dispatch decision +* made while it was queued is stale. +*/ + rq->scx.ops_qseq++;
I'm not sure why this is necessary. Isn't setting the ops_state to SCX_OPSS_NONE enough to invalidate in-flight dispatches? Could you describe a scenario where incrementing qseq on dequeue is necessary?
kernel/sched/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2552
Quoted 11 lines
kernel/sched/ext.c +4
-3@@ -2537,9 +2546,26 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, }if (src_rq != dst_rq && -unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { -dispatch_enqueue (sch, rq,find_global_dsq (sch,task_cpu (p)), p, - enq_flags |SCX_ENQ_CLEAR_OPSS |SCX_ENQ_GDSQ_FALLBACK ); +unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, false))) { +/* +* Affinity changed after dispatch decision and the task +* can't run anymore on the destination rq.
More of a nitpick, but this doesn't necessarily mean that the affinity changed. The scheduler could have also issued an invalid dispatch to a CPU outside of the task's cpumask (e.g. due to a bug), in which case the task won't be re-enqueued if we simply drop the dispatch, correct?
kernel/sched/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2554
Quoted 2 lines
kernel/sched/ext.c +2
-0+* +* Drop the dispatch, the task will be re-enqueued. Set the
Just to clarify, is this referring to the enqueue that happens in do_set_cpus_allowed(), immediately after the actual cpumask change?
Quoted 6 lines
kernel/sched/ext.c +6
-0+* task back to QUEUED so dequeue (if waiting) can proceed +* using current qseq from the task's rq. +*/ +if (src_rq != rq) { +raw_spin_rq_unlock (rq); +raw_spin_rq_lock (src_rq);
kernel/sched/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2570
Quoted 10 lines
kernel/sched/ext.c +8
-0+ } +atomic_long_set_release (&p->scx.ops_state, +SCX_OPSS_QUEUED | + (src_rq->scx.ops_qseq <<SCX_OPSS_QSEQ_SHIFT )); +if (src_rq != rq) { +raw_spin_rq_unlock (src_rq); +raw_spin_rq_lock (rq); + }return ; }
My understanding is that task_can_run_on_remote_rq() can run without src_rq locked, so it's possible that @p's cpumask changes after the check, isn't it? In that case, I think it's still possible to move the task to the local DSQ of a CPU that is outside of its cpumask, triggering a warning in move_remote_task_to_local_dsq().
Thanks,
Kuba
Quoted 27 lines · Kuba Piecuch
Quoted 27 lines
kernel/sched/ext.c +20
-3@@ -2537,9 +2546,26 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, }if (src_rq != dst_rq && -unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { -dispatch_enqueue (sch, rq,find_global_dsq (sch,task_cpu (p)), p, - enq_flags |SCX_ENQ_CLEAR_OPSS |SCX_ENQ_GDSQ_FALLBACK ); +unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, false))) { +/* +* Affinity changed after dispatch decision and the task +* can't run anymore on the destination rq. +* +* Drop the dispatch, the task will be re-enqueued. Set the +* task back to QUEUED so dequeue (if waiting) can proceed +* using current qseq from the task's rq. +*/ +if (src_rq != rq) { +raw_spin_rq_unlock (rq); +raw_spin_rq_lock (src_rq); + } +atomic_long_set_release (&p->scx.ops_state, +SCX_OPSS_QUEUED | + (src_rq->scx.ops_qseq <<SCX_OPSS_QSEQ_SHIFT )); +if (src_rq != rq) { +raw_spin_rq_unlock (src_rq); +raw_spin_rq_lock (rq); + }
Quoted 8 lines
Review static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2570
Quoted 2 lines
(quoted diff) +0
-0return; }My understanding is that
task_can_run_on_remote_rq()can run withoutsrc_rqlocked, so it's possible that @p's cpumask changes after the check, isn't it? In that case, I think it's still possible to move the task to the local DSQ of a CPU that is outside of its cpumask, triggering a warning inmove_remote_task_to_local_dsq().
I've looked at the code more carefully and I don't think this is an issue. It's true that task_can_run_on_remote_rq() can run without src_rq locked, and it's possible that the cpumask changes after the check, but then the dequeue preceding the cpumask change must have waited for ops_state to change from SCX_OPSS_DISPATCHING to SCX_OPSS_NONE and it must have reset holding_cpu to -1, so thanks to the holding_cpu check later we won't insert the task into the DSQ.
Apologies for the confusion.
Andrea Righi
reviewerQuoted 17 lines · Kuba Piecuch
Hi Andrea,
kernel/sched/ext.c static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags) · line 2049
Quoted 10 lines · Andrea Righi
kernel/sched/ext.c +6
-0@@ -2043,6 +2041,13 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)*/ BUG ();case SCX_OPSS_QUEUED : +/* +* Invalidate any in-flight dispatches for this task. The +* task is leaving the runqueue, so any dispatch decision +* made while it was queued is stale. +*/ + rq->scx.ops_qseq++;I'm not sure why this is necessary. Isn't setting the
ops_statetoSCX_OPSS_NONEenough to invalidate in-flight dispatches? Could you describe a scenario where incrementing qseq on dequeue is necessary?
I'm looking back at the code and I think you're right, ops_qseq is already incremented by the new enqueue, so setting the state to NONE on dequeue should be enough to drop the in-flight dispatch.
I did some quick tests and everything seems to work also without this increment. Thanks for noticing this!
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -2537,9 +2546,26 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, }
Quoted 13 lines
kernel/sched/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2552
Quoted 8 lines
kernel/sched/ext.c +4
-3if (src_rq != dst_rq && -unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { -dispatch_enqueue (sch, rq,find_global_dsq (sch,task_cpu (p)), p, - enq_flags |SCX_ENQ_CLEAR_OPSS |SCX_ENQ_GDSQ_FALLBACK ); +unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, false))) { +/* +* Affinity changed after dispatch decision and the task +* can't run anymore on the destination rq. More of a nitpick, but this doesn't necessarily mean that the affinity changed. The scheduler could have also issued an invalid dispatch to a CPU outside of the task's cpumask (e.g. due to a bug), in which case the task won't be re-enqueued if we simply drop the dispatch, correct?
That's right, the scheduler could have issues an invalid dispatch and in that case we would just drop the task on the floor, which is not really nice, it'd be better to immediately error in this case. And we don't need the global DSQ fallback, since we're erroring anyway.
I need to rethink this part...
Quoted 6 lines
kernel/sched/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2554
Quoted 3 lines
kernel/sched/ext.c +2
-0+* +* Drop the dispatch, the task will be re-enqueued. Set the Just to clarify, is this referring to the enqueue that happens in
do_set_cpus_allowed(), immediately after the actual cpumask change?
Correct, it's the enqueue that happens from sched_change_end() in do_set_cpus_allowed().
Thanks,
-Andrea
Quoted 19 lines · Andrea Righi
Quoted 4 lines · Kuba Piecuch
Quoted 3 lines · Andrea Righi
(quoted diff) +0
-0@@ -2537,9 +2546,26 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, }Quoted 13 lines
kernel/sched/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2552
Quoted 8 lines
kernel/sched/ext.c +4
-3if (src_rq != dst_rq && -unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { -dispatch_enqueue (sch, rq,find_global_dsq (sch,task_cpu (p)), p, - enq_flags |SCX_ENQ_CLEAR_OPSS |SCX_ENQ_GDSQ_FALLBACK ); +unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, false))) { +/* +* Affinity changed after dispatch decision and the task +* can't run anymore on the destination rq. More of a nitpick, but this doesn't necessarily mean that the affinity changed. The scheduler could have also issued an invalid dispatch to a CPU outside of the task's cpumask (e.g. due to a bug), in which case the task won't be re-enqueued if we simply drop the dispatch, correct?
Quoted 6 lines
That's right, the scheduler could have issues an invalid dispatch and in that case we would just drop the task on the floor, which is not really nice, it'd be better to immediately error in this case. And we don't need the global DSQ fallback, since we're erroring anyway.
I need to rethink this part...
The fundamental problem here is differentiating between buggy dispatches that should have never been issued and dispatches that were valid at the moment the BPF scheduler was preparing the task for dispatch, but became invalid due to racing cpumask changes.
The crucial observation is that SCX will only detect racing dequeues/enqueues if they race with the window between scx_bpf_dsq_insert() and finish_dispatch(). That's because scx_bpf_dsq_insert() stores a snapshot of the task's current qseq value, which is compared in finish_dispatch().
The BPF-side cpumask checks traditionally happen outside of this window, making finish_dispatch() incapable of detecting cpumask changes that raced with the BPF-side check but happened strictly before scx_bpf_dsq_insert().
To resolve this, we need to extend the race detection window so that it includes the BPF-side checks.
The simple way to do this is to do scx_bpf_dsq_insert() at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch via scx_bpf_dispatch_cancel() if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.
With this solution, we can throw an error if task_can_run_on_remote_rq() is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, in finish_dispatch()).
Tejun Heo
reviewerHello,
On Fri, Mar 20, 2026 at 09:18:20AM +0000, Kuba Piecuch wrote:
...
Quoted 10 lines
The simple way to do this is to do
scx_bpf_dsq_insert()at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch viascx_bpf_dispatch_cancel()if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.With this solution, we can throw an error if
task_can_run_on_remote_rq()is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, infinish_dispatch()).
Yeah, I think this makes more sense. qseq is already there to provide protection against these events. It's just that the capturing of qseq is too late. If insert/cancel is too ugly, we can introduce another kfunc to capture the qseq - scx_bpf_dsq_insert_begin() or something like that - and stash it in a per-cpu variable. That way, qseq would be cover the "current" queued instance and the existing qseq mechanism would be able to reliably ignore the ones that lost race to dequeue.
Thanks.
Signature · 1 line
tejun
Hi Tejun, Andrea, and Kuba
Quoted 11 lines · Tejun Heo
Quoted 10 lines
The simple way to do this is to do
scx_bpf_dsq_insert()at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch viascx_bpf_dispatch_cancel()if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.With this solution, we can throw an error if
task_can_run_on_remote_rq()is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, infinish_dispatch()).
Quoted 7 lines
Yeah, I think this makes more sense. qseq is already there to provide protection against these events. It's just that the capturing of qseq is too late. If insert/cancel is too ugly, we can introduce another kfunc to capture the qseq -
scx_bpf_dsq_insert_begin()or something like that - and stash it in a per-cpu variable. That way, qseq would be cover the "current" queued instance and the existing qseq mechanism would be able to reliably ignore the ones that lost race to dequeue.
Since this has been stale for a while, I prepared a patch to implement scx_bpf_dsq_insert_begin() as suggested.
Is anyone else working on this? If not, I'm happy to send the formal patch to fix this.
Signature · 2 lines
Cheers, Cheng-Yang
kernel/sched/ext.c +43 -1
@@ -7933,6 +7933,7 @@ static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p, {struct scx_dsp_ctx *dspc = &this_cpu_ptr (sch->pcpu)->dsp_ctx;struct task_struct *ddsp_task; +unsigned long qseq; ddsp_task =__this_cpu_read (direct_dispatch_task);if (ddsp_task) {
@@ -7945,9 +7946,16 @@ static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p,return ; } +if (dspc->insert_begin_valid) { + qseq = dspc->insert_begin_qseq; + dspc->insert_begin_valid = false; + }else { + qseq =atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK ; + } + 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, };
@@ -7955,6 +7963,39 @@ static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p,__bpf_kfunc_start_defs (); +/** +* scx_bpf_dsq_insert_begin - Snapshot qseq before a dispatch decision +* @p: task_struct being considered for dispatch +* @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs +* +* Capture @p's qseq before the BPF scheduler reads @p's properties (e.g. +* cpus_ptr) to make a dispatch decision. The snapshot is used by the +* subsequent scx_bpf_dsq_insert() call, extending the race detection window +* to cover any BPF-side checks between this call and the insert. If a +* concurrent dequeue/re-enqueue races within this window, finish_dispatch() +* detects the qseq mismatch and discards the stale dispatch. +*/ +__bpf_kfunc void scx_bpf_dsq_insert_begin(struct task_struct *p, +const struct bpf_prog_aux *aux) +{ +struct scx_sched *sch; +struct scx_dsp_ctx *dspc; + +guard (rcu)(); + + sch =scx_prog_sched (aux); +if (unlikely (!sch)) +return ; + +if (!scx_kf_allowed (sch,SCX_KF_ENQUEUE |SCX_KF_DISPATCH )) +return ; + + dspc = &this_cpu_ptr (sch->pcpu)->dsp_ctx; + dspc->insert_begin_qseq =atomic_long_read (&p->scx.ops_state) & +SCX_OPSS_QSEQ_MASK ; + dspc->insert_begin_valid = true; +} +/** * scx_bpf_dsq_insert - Insert a task into the FIFO queue of a DSQ * @p: task_struct to insert
@@ -8134,6 +8175,7 @@ __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id,__bpf_kfunc_end_defs (); BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch) +BTF_ID_FLAGS(func, scx_bpf_dsq_insert_begin, KF_IMPLICIT_ARGS | KF_RCU) 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_vtime, KF_IMPLICIT_ARGS | KF_RCU)
kernel/sched/ext_internal.h +2 -0
@@ -989,6 +989,8 @@ struct scx_dsp_ctx {struct rq *rq;u32 cursor;u32 nr_tasks; +unsigned long insert_begin_qseq; +bool insert_begin_valid;struct scx_dsp_buf_ent buf[]; };
tools/sched_ext/scx_central.bpf.c +2 -0
@@ -155,6 +155,8 @@ static bool dispatch_to_cpu(s32 cpu)* reflect the migration-disabled state yet if * migrate_disable_switch() hasn't run. */ +scx_bpf_dsq_insert_begin (p); +if (!bpf_cpumask_test_cpu (cpu, p->cpus_ptr) || (is_migration_disabled (p) &&scx_bpf_task_cpu (p) != cpu)) {__sync_fetch_and_add (&nr_mismatches,1 );
Signature · 2 lines
-- 2.48.1
Andrea Righi
reviewerHi Cheng-Yang,
Quoted 22 lines · Cheng-Yang Chou
Hi Tejun, Andrea, and Kuba
Quoted 11 lines · Tejun Heo
Quoted 10 lines
The simple way to do this is to do
scx_bpf_dsq_insert()at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch viascx_bpf_dispatch_cancel()if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.With this solution, we can throw an error if
task_can_run_on_remote_rq()is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, infinish_dispatch()).Quoted 7 lines
Yeah, I think this makes more sense. qseq is already there to provide protection against these events. It's just that the capturing of qseq is too late. If insert/cancel is too ugly, we can introduce another kfunc to capture the qseq -
scx_bpf_dsq_insert_begin()or something like that - and stash it in a per-cpu variable. That way, qseq would be cover the "current" queued instance and the existing qseq mechanism would be able to reliably ignore the ones that lost race to dequeue.
Quoted 5 lines
Since this has been stale for a while, I prepared a patch to implement
scx_bpf_dsq_insert_begin()as suggested.Is anyone else working on this? If not, I'm happy to send the formal patch to fix this.
It's sitting in my TODO list. If you have time to work on this, go ahead and send the updated patch. I'll take a look at it ASAP.
Thanks,
-Andrea
Quoted 112 lines
Signature · 3 lines
-- Cheers, Cheng-Yangkernel/sched/ext.c +43
-1@@ -7933,6 +7933,7 @@ static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p, {struct scx_dsp_ctx *dspc = &this_cpu_ptr (sch->pcpu)->dsp_ctx;struct task_struct *ddsp_task; +unsigned long qseq; ddsp_task =__this_cpu_read (direct_dispatch_task);if (ddsp_task) {@@ -7945,9 +7946,16 @@ static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p,return ; } +if (dspc->insert_begin_valid) { + qseq = dspc->insert_begin_qseq; + dspc->insert_begin_valid = false; + }else { + qseq =atomic_long_read (&p->scx.ops_state) &SCX_OPSS_QSEQ_MASK ; + } + 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, };@@ -7955,6 +7963,39 @@ static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p,__bpf_kfunc_start_defs (); +/** +* scx_bpf_dsq_insert_begin - Snapshot qseq before a dispatch decision +* @p: task_struct being considered for dispatch +* @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs +* +* Capture @p's qseq before the BPF scheduler reads @p's properties (e.g. +* cpus_ptr) to make a dispatch decision. The snapshot is used by the +* subsequent scx_bpf_dsq_insert() call, extending the race detection window +* to cover any BPF-side checks between this call and the insert. If a +* concurrent dequeue/re-enqueue races within this window, finish_dispatch() +* detects the qseq mismatch and discards the stale dispatch. +*/ +__bpf_kfunc void scx_bpf_dsq_insert_begin(struct task_struct *p, +const struct bpf_prog_aux *aux) +{ +struct scx_sched *sch; +struct scx_dsp_ctx *dspc; + +guard (rcu)(); + + sch =scx_prog_sched (aux); +if (unlikely (!sch)) +return ; + +if (!scx_kf_allowed (sch,SCX_KF_ENQUEUE |SCX_KF_DISPATCH )) +return ; + + dspc = &this_cpu_ptr (sch->pcpu)->dsp_ctx; + dspc->insert_begin_qseq =atomic_long_read (&p->scx.ops_state) & +SCX_OPSS_QSEQ_MASK ; + dspc->insert_begin_valid = true; +} +/** * scx_bpf_dsq_insert - Insert a task into the FIFO queue of a DSQ * @p: task_struct to insert @@ -8134,6 +8175,7 @@ __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id,__bpf_kfunc_end_defs (); BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch) +BTF_ID_FLAGS(func, scx_bpf_dsq_insert_begin, KF_IMPLICIT_ARGS | KF_RCU) 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_vtime, KF_IMPLICIT_ARGS | KF_RCU)kernel/sched/ext_internal.h +2
-0@@ -989,6 +989,8 @@ struct scx_dsp_ctx {struct rq *rq;u32 cursor;u32 nr_tasks; +unsigned long insert_begin_qseq; +bool insert_begin_valid;struct scx_dsp_buf_ent buf[]; };tools/sched_ext/scx_central.bpf.c +2
-0@@ -155,6 +155,8 @@ static bool dispatch_to_cpu(s32 cpu)* reflect the migration-disabled state yet if * migrate_disable_switch() hasn't run. */ +scx_bpf_dsq_insert_begin (p); +if (!bpf_cpumask_test_cpu (cpu, p->cpus_ptr) || (is_migration_disabled (p) &&scx_bpf_task_cpu (p) != cpu)) {__sync_fetch_and_add (&nr_mismatches,1 );Signature · 2 lines
-- 2.48.1
Hi Cheng-Yang,
Quoted 22 lines · Cheng-Yang Chou
Hi Tejun, Andrea, and Kuba
Quoted 11 lines · Tejun Heo
Quoted 10 lines
The simple way to do this is to do
scx_bpf_dsq_insert()at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch viascx_bpf_dispatch_cancel()if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.With this solution, we can throw an error if
task_can_run_on_remote_rq()is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, infinish_dispatch()).Quoted 7 lines
Yeah, I think this makes more sense. qseq is already there to provide protection against these events. It's just that the capturing of qseq is too late. If insert/cancel is too ugly, we can introduce another kfunc to capture the qseq -
scx_bpf_dsq_insert_begin()or something like that - and stash it in a per-cpu variable. That way, qseq would be cover the "current" queued instance and the existing qseq mechanism would be able to reliably ignore the ones that lost race to dequeue.
Quoted 5 lines
Since this has been stale for a while, I prepared a patch to implement
scx_bpf_dsq_insert_begin()as suggested.Is anyone else working on this? If not, I'm happy to send the formal patch to fix this.
Thanks for creating the patch. A couple of thoughts:
1. Do we have a use case that requires dsq_insert_begin() that isn't satisfied using the "insert and then cancel if needed" approach?
2. Do we want to restrict ourselves through the one qseq slot provided by dsq_insert_begin()? The most flexible approach IMO would be to simply allow BPF to read the qseq directly via a kfunc and then supply it to dsq_insert() later. With this, we can have multiple qseqs saved at the same time, and we can even pass them between CPUs, e.g. if one CPU dequeues a task for a sibling CPU, but we want the checks to be made inside the sibling's ops.dispatch() (I just made this use case it up, it may not be practical.)
That said, exposing an internal thing like qseq to BPF may be a step too far. Let me know what you think.
Thanks,
Kuba
Hi Kuba,
Quoted 20 lines · Kuba Piecuch
Quoted 20 lines · Tejun Heo
Quoted 18 lines
Quoted 10 lines
The simple way to do this is to do
scx_bpf_dsq_insert()at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch viascx_bpf_dispatch_cancel()if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.With this solution, we can throw an error if
task_can_run_on_remote_rq()is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, infinish_dispatch()).Yeah, I think this makes more sense. qseq is already there to provide protection against these events. It's just that the capturing of qseq is too late. If insert/cancel is too ugly, we can introduce another kfunc to capture the qseq -
scx_bpf_dsq_insert_begin()or something like that - and stash it in a per-cpu variable. That way, qseq would be cover the "current" queued instance and the existing qseq mechanism would be able to reliably ignore the ones that lost race to dequeue.
Quoted 7 lines
Quoted 2 lines
Since this has been stale for a while, I prepared a patch to implement
scx_bpf_dsq_insert_begin()as suggested.Thanks for creating the patch. A couple of thoughts:
1. Do we have a use case that requires
dsq_insert_begin()that isn't satisfied using the "insert and then cancel if needed" approach?
IIUC, yes. scx_bpf_dispatch_cancel() is only registered in scx_kfunc_ids_dispatch, so it is only callable from ops.dispatch(). dsq_insert_begin(), on the other hand, is available from both ops.enqueue() and ops.dispatch() (SCX_KF_ENQUEUE | SCX_KF_DISPATCH). Since there is nothing to cancel in ops.enqueue(), the insert-and-cancel approach simply doesn't work there.
Quoted 10 lines
2. Do we want to restrict ourselves through the one qseq slot provided by
dsq_insert_begin()? The most flexible approach IMO would be to simply allow BPF to read the qseq directly via a kfunc and then supply it todsq_insert()later. With this, we can have multiple qseqs saved at the same time, and we can even pass them between CPUs, e.g. if one CPU dequeues a task for a sibling CPU, but we want the checks to be made inside the sibling'sops.dispatch()(I just made this use case it up, it may not be practical.)
That said, exposing an internal thing like qseq to BPF may be a step too far.
In Tejun's reply back in [1], he suggested dsq_insert_begin() precisely to avoid promoting qseq into the BPF ABI — which matches your own concern. The single per-CPU slot is sufficient for the one-task-per-iteration dispatch loops used by existing schedulers (e.g., scx_central). If a concrete cross-CPU use case materializes later, we can always extend dsq_insert() to accept an explicit qseq without breaking the current, simpler path.
[1]: https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Let me know what you think.
Please correct me if I'm missing something, thanks! ^0^
Signature · 2 lines
Cheers, Cheng-Yang
Hi Cheng-Yang,
Quoted 31 lines · Cheng-Yang Chou
Hi Kuba,
Quoted 20 lines · Kuba Piecuch
Quoted 20 lines · Tejun Heo
Quoted 18 lines
Quoted 10 lines
The simple way to do this is to do
scx_bpf_dsq_insert()at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch viascx_bpf_dispatch_cancel()if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.With this solution, we can throw an error if
task_can_run_on_remote_rq()is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, infinish_dispatch()).Yeah, I think this makes more sense. qseq is already there to provide protection against these events. It's just that the capturing of qseq is too late. If insert/cancel is too ugly, we can introduce another kfunc to capture the qseq -
scx_bpf_dsq_insert_begin()or something like that - and stash it in a per-cpu variable. That way, qseq would be cover the "current" queued instance and the existing qseq mechanism would be able to reliably ignore the ones that lost race to dequeue.Quoted 7 lines
Quoted 2 lines
Since this has been stale for a while, I prepared a patch to implement
scx_bpf_dsq_insert_begin()as suggested.Thanks for creating the patch. A couple of thoughts:
1. Do we have a use case that requires
dsq_insert_begin()that isn't satisfied using the "insert and then cancel if needed" approach?
Quoted 6 lines
IIUC, yes.
scx_bpf_dispatch_cancel()is only registered inscx_kfunc_ids_dispatch, so it is only callable fromops.dispatch().dsq_insert_begin(), on the other hand, is available from bothops.enqueue()andops.dispatch()(SCX_KF_ENQUEUE|SCX_KF_DISPATCH). Since there is nothing to cancel inops.enqueue(), the insert-and-cancel approach simply doesn't work there.
Wouldn't the natural thing then be to extend scx_bpf_dispatch_cancel() to work for direct dispatch? Instead of introducing a whole new mechanism, let's extend the one we have by functionality that it (arguably) should have had from the beginning.
Quoted 20 lines
Quoted 11 lines
2. Do we want to restrict ourselves through the one qseq slot provided by
dsq_insert_begin()? The most flexible approach IMO would be to simply allow BPF to read the qseq directly via a kfunc and then supply it todsq_insert()later. With this, we can have multiple qseqs saved at the same time, and we can even pass them between CPUs, e.g. if one CPU dequeues a task for a sibling CPU, but we want the checks to be made inside the sibling'sops.dispatch()(I just made this use case it up, it may not be practical.)
That said, exposing an internal thing like qseq to BPF may be a step too far.In Tejun's reply back in [1], he suggested
dsq_insert_begin()precisely to avoid promoting qseq into the BPF ABI — which matches your own concern. The single per-CPU slot is sufficient for the one-task-per-iteration dispatch loops used by existing schedulers (e.g.,scx_central). If a concrete cross-CPU use case materializes later, we can always extenddsq_insert()to accept an explicit qseq without breaking the current, simpler path.
Quoted 2 lines
Well, Tejun doesn't explicitly say there that he's against exposing qseq, but I won't be surprised if he is.
FWIW, ghOSt (our Google-internal BPF scheduling solution) uses exactly this approach to guard the dispatch path against racing dequeues/enqueues. Every task has a seqnum that gets incremented on each "event" pertaining to the task. In the dispatch path, the BPF scheduler reads the task seqnum, does whatever checks it needs to do, and passes the seqnum to ghOSt at the end.
Admittedly, what works downstream doesn't have to work upstream, but I still wanted to provide this data point :-)
Thanks,
Kuba
Hi Kuba,
Quoted 36 lines · Kuba Piecuch
Quoted 29 lines · Kuba Piecuch
Quoted 20 lines
Quoted 20 lines · Tejun Heo
Quoted 18 lines
Quoted 10 lines
The simple way to do this is to do
scx_bpf_dsq_insert()at the very beginning, once we know which task we would like to dispatch, and cancel the pending dispatch viascx_bpf_dispatch_cancel()if any of the pre-dispatch checks fail on the BPF side. This way, the "critical section" includes BPF-side checks, and SCX will ignore the dispatch if there was a dequeue/enqueue racing with the critical section.With this solution, we can throw an error if
task_can_run_on_remote_rq()is false, because we know that there was no racing cpumask change (if there was, it would have been caught earlier, infinish_dispatch()).Yeah, I think this makes more sense. qseq is already there to provide protection against these events. It's just that the capturing of qseq is too late. If insert/cancel is too ugly, we can introduce another kfunc to capture the qseq -
scx_bpf_dsq_insert_begin()or something like that - and stash it in a per-cpu variable. That way, qseq would be cover the "current" queued instance and the existing qseq mechanism would be able to reliably ignore the ones that lost race to dequeue.Quoted 7 lines
Quoted 2 lines
Since this has been stale for a while, I prepared a patch to implement
scx_bpf_dsq_insert_begin()as suggested.Thanks for creating the patch. A couple of thoughts:
1. Do we have a use case that requires
dsq_insert_begin()that isn't satisfied using the "insert and then cancel if needed" approach?Quoted 6 lines
IIUC, yes.
scx_bpf_dispatch_cancel()is only registered inscx_kfunc_ids_dispatch, so it is only callable fromops.dispatch().dsq_insert_begin(), on the other hand, is available from bothops.enqueue()andops.dispatch()(SCX_KF_ENQUEUE|SCX_KF_DISPATCH). Since there is nothing to cancel inops.enqueue(), the insert-and-cancel approach simply doesn't work there.
Quoted 4 lines
Wouldn't the natural thing then be to extend
scx_bpf_dispatch_cancel()to work for direct dispatch? Instead of introducing a whole new mechanism, let's extend the one we have by functionality that it (arguably) should have had from the beginning.
I see. You're right that dispatch_cancel() could be extended to work in the enqueue context.
I'm happy to go either direction, your approach or Tejun's suggestion. Tejun, Andrea, sched-ext folks, thoughts?
Quoted 27 lines
Quoted 21 lines
Quoted 12 lines
2. Do we want to restrict ourselves through the one qseq slot provided by
dsq_insert_begin()? The most flexible approach IMO would be to simply allow BPF to read the qseq directly via a kfunc and then supply it todsq_insert()later. With this, we can have multiple qseqs saved at the same time, and we can even pass them between CPUs, e.g. if one CPU dequeues a task for a sibling CPU, but we want the checks to be made inside the sibling'sops.dispatch()(I just made this use case it up, it may not be practical.)
That said, exposing an internal thing like qseq to BPF may be a step too far.In Tejun's reply back in [1], he suggested
dsq_insert_begin()precisely to avoid promoting qseq into the BPF ABI — which matches your own concern. The single per-CPU slot is sufficient for the one-task-per-iteration dispatch loops used by existing schedulers (e.g.,scx_central). If a concrete cross-CPU use case materializes later, we can always extenddsq_insert()to accept an explicit qseq without breaking the current, simpler path.Quoted 2 lines
Well, Tejun doesn't explicitly say there that he's against exposing qseq, but I won't be surprised if he is.
Quoted 8 lines
FWIW, ghOSt (our Google-internal BPF scheduling solution) uses exactly this approach to guard the dispatch path against racing dequeues/enqueues. Every task has a seqnum that gets incremented on each "event" pertaining to the task. In the dispatch path, the BPF scheduler reads the task seqnum, does whatever checks it needs to do, and passes the seqnum to ghOSt at the end.
Admittedly, what works downstream doesn't have to work upstream, but I still wanted to provide this data point :-)
The ghOSt data point is appreciated. If a concrete use case emerges where the single-slot approach falls short, extending dsq_insert() to accept an explicit qseq seems like a natural next step.
Tejun, Andrea, sched-ext folks, any preferences?
Signature · 2 lines
Cheers, Cheng-Yang
Hi Cheng-Yang,
Quoted 30 lines · Cheng-Yang Chou
Quoted 30 lines
Quoted 18 lines
Quoted 9 lines
2. Do we want to restrict ourselves through the one qseq slot provided by
dsq_insert_begin()? The most flexible approach IMO would be to simply allow BPF to read the qseq directly via a kfunc and then supply it todsq_insert()later. With this, we can have multiple qseqs saved at the same time, and we can even pass them between CPUs, e.g. if one CPU dequeues a task for a sibling CPU, but we want the checks to be made inside the sibling'sops.dispatch()(I just made this use case it up, it may not be practical.)
That said, exposing an internal thing like qseq to BPF may be a step too far.In Tejun's reply back in [1], he suggested
dsq_insert_begin()precisely to avoid promoting qseq into the BPF ABI — which matches your own concern. The single per-CPU slot is sufficient for the one-task-per-iteration dispatch loops used by existing schedulers (e.g.,scx_central). If a concrete cross-CPU use case materializes later, we can always extenddsq_insert()to accept an explicit qseq without breaking the current, simpler path.Quoted 2 lines
Well, Tejun doesn't explicitly say there that he's against exposing qseq, but I won't be surprised if he is.
FWIW, ghOSt (our Google-internal BPF scheduling solution) uses exactly this approach to guard the dispatch path against racing dequeues/enqueues. Every task has a seqnum that gets incremented on each "event" pertaining to the task. In the dispatch path, the BPF scheduler reads the task seqnum, does whatever checks it needs to do, and passes the seqnum to ghOSt at the end.
Quoted 8 lines
Quoted 2 lines
Admittedly, what works downstream doesn't have to work upstream, but I still wanted to provide this data point :-)
The ghOSt data point is appreciated. If a concrete use case emerges where the single-slot approach falls short, extending
dsq_insert()to accept an explicit qseq seems like a natural next step.Tejun, Andrea, sched-ext folks, any preferences?
Random thought: If exposing qseq values to BPF directly is undesirable, then perhaps a less objectionable approach would be to expose them as opaque cookie/token values? Same semantics, but fewer SCX internals leaking to BPF.
Thanks,
Kuba
Tejun Heo
reviewerQuoted 3 lines · Kuba Piecuch
Random thought: If exposing qseq values to BPF directly is undesirable, then perhaps a less objectionable approach would be to expose them as opaque cookie/token values? Same semantics, but fewer SCX internals leaking to BPF.
Yeah, cookie sounds fine to me and let's clearly note that this is for schedulers that don't implement properly synchronized dequeue.
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerQuoted 7 lines · Tejun Heo
Quoted 3 lines · Kuba Piecuch
Random thought: If exposing qseq values to BPF directly is undesirable, then perhaps a less objectionable approach would be to expose them as opaque cookie/token values? Same semantics, but fewer SCX internals leaking to BPF.
Yeah, cookie sounds fine to me and let's clearly note that this is for schedulers that don't implement properly synchronized dequeue.
I also like the idea of exposing cookies and extending scx_bpf_dispatch_cancel() to support direct dispatches, it it doesn't introduce too much complexity (it shouldn't be too painful).
Cheng-Yang, just to make sure we're not doing the work twice, are you planning to work on this?
Thanks,
-Andrea
Hi Andrea,
Quoted 9 lines · Andrea Righi
Quoted 7 lines · Tejun Heo
Quoted 3 lines · Kuba Piecuch
Random thought: If exposing qseq values to BPF directly is undesirable, then perhaps a less objectionable approach would be to expose them as opaque cookie/token values? Same semantics, but fewer SCX internals leaking to BPF.
Yeah, cookie sounds fine to me and let's clearly note that this is for schedulers that don't implement properly synchronized dequeue.
Quoted 6 lines
I also like the idea of exposing cookies and extending
scx_bpf_dispatch_cancel()to support direct dispatches, it it doesn't introduce too much complexity (it shouldn't be too painful).Cheng-Yang, just to make sure we're not doing the work twice, are you planning to work on this?
If you are already working on this, I can help with testing after your patches! Otherwise, I'm happy to help.
FYI, I'm already working on deferring ext_server bandwidth to SCX enable and releasing it when disabled, but it still needs more testing!
Signature · 2 lines
Cheers, Cheng-Yang
Hi Tejun,
Quoted 7 lines · Tejun Heo
Quoted 3 lines · Kuba Piecuch
Random thought: If exposing qseq values to BPF directly is undesirable, then perhaps a less objectionable approach would be to expose them as opaque cookie/token values? Same semantics, but fewer SCX internals leaking to BPF.
Yeah, cookie sounds fine to me and let's clearly note that this is for schedulers that don't implement properly synchronized dequeue.
Could you elaborate a bit on what you mean by "properly synchronized" here?
To me, introducing cookies is primarily about adding flexibility around managing the "dispatch window" between the qseq being probed and the actual dispatch attempt in finish_dispatch(). For example, a CPU can get a cookie and pass it to another CPU to perform the dispatch, which is not possible with the current interface.
On another, slightly related note: I'm considering making scx_bpf_dsq_insert() and other dispatch-related kfuncs that manipulate only CPU-local state callable while holding BPF spinlocks. This is something that the comment above scx_bpf_dsq_insert() explicitly mentions:
This function doesn't have any locking restrictions and may be called under BPF locks (in the future when BPF introduces more flexible locking).
I'm not sure what "more flexible locking" means here, but this can be accomplished by simply adding the kfuncs to the list of kfuncs callable under spinlocks in the BPF verifier.
Are you aware of any previous work on this? Any pushback from BPF folks?
Thanks,
Kuba
Tejun Heo
reviewerHello, Kuba.
Quoted 1 line · Kuba Piecuch
Could you elaborate a bit on what you mean by "properly synchronized" here?
If ops.dequeue() synchronizes with the dispatch path so that the task being dequeued is either dequeued or dispatched, there's nothing else to protect. If ops.dequeue() wins, the task won't be dispatched. If ops.dequeue() loses, the task should already be in either the dispatch buffer or local DSQ and the kernel dequeue code will shoot them down. In the former case, at the dispatch buffer flush time, the task would either be already dequeued or re-enqueued with a different qseq and ignored. In the latter, dispatch_dequeue() would remove it from the local DSQ.
Quoted 5 lines
To me, introducing cookies is primarily about adding flexibility around managing the "dispatch window" between the qseq being probed and the actual dispatch attempt in
finish_dispatch(). For example, a CPU can get a cookie and pass it to another CPU to perform the dispatch, which is not possible with the current interface.
So, this is mostly for schedulers that don't want to or for some reason can't implement proper synchronization between dequeue and dispatch paths. A convenient thing to make life a bit easier.
Quoted 5 lines
On another, slightly related note: I'm considering making
scx_bpf_dsq_insert()and other dispatch-related kfuncs that manipulate only CPU-local state callable while holding BPF spinlocks. This is something that the comment abovescx_bpf_dsq_insert()explicitly mentions:
Quoted 8 lines
This function doesn't have any locking restrictions and may be called under BPF locks (in the future when BPF introduces more flexible locking).
I'm not sure what "more flexible locking" means here, but this can be accomplished by simply adding the kfuncs to the list of kfuncs callable under spinlocks in the BPF verifier.
Are you aware of any previous work on this? Any pushback from BPF folks?
That comment was written before bpf_spinlock was introduced. Please feel free to allow thoes functions under bpf spinlocks. BTW, there's also arena spinlock that is implemented in BPF proper, which is already used by multiple schedulers and likely to be the default option in the future:
https://github.com/sched-ext/scx/blob/main/scheds/include/bpf_arena_spin_lock.h
Thanks.
Signature · 1 line
tejun
Quoted 5 lines · Tejun Heo
Hello, Kuba.
Quoted 1 line · Kuba Piecuch
Could you elaborate a bit on what you mean by "properly synchronized" here?
Quoted 8 lines
If
ops.dequeue()synchronizes with the dispatch path so that the task being dequeued is either dequeued or dispatched, there's nothing else to protect. Ifops.dequeue()wins, the task won't be dispatched. Ifops.dequeue()loses, the task should already be in either the dispatch buffer or local DSQ and the kernel dequeue code will shoot them down. In the former case, at the dispatch buffer flush time, the task would either be already dequeued or re-enqueued with a different qseq and ignored. In the latter,dispatch_dequeue()would remove it from the local DSQ.
I see, makes sense. Thanks!
Quoted 12 lines
Quoted 12 lines
On another, slightly related note: I'm considering making
scx_bpf_dsq_insert()and other dispatch-related kfuncs that manipulate only CPU-local state callable while holding BPF spinlocks. This is something that the comment abovescx_bpf_dsq_insert()explicitly mentions:This function doesn't have any locking restrictions and may be called under BPF locks (in the future when BPF introduces more flexible locking).
I'm not sure what "more flexible locking" means here, but this can be accomplished by simply adding the kfuncs to the list of kfuncs callable under spinlocks in the BPF verifier.
Quoted 8 lines
Quoted 1 line
Are you aware of any previous work on this? Any pushback from BPF folks?
That comment was written before
bpf_spinlockwas introduced. Please feel free to allow thoes functions under bpf spinlocks. BTW, there's also arena spinlock that is implemented in BPF proper, which is already used by multiple schedulers and likely to be the default option in the future:https://github.com/sched-ext/scx/blob/main/scheds/include/bpf_arena_spin_lock.h
Ah, interesting, I didn't know there's a pure-BPF implementation of spinlocks! I haven't really had the chance to play around with arenas yet, looks like I'm missing out ;-)
Thanks,
Kuba
Tejun Heo
reviewerHello,
Quoted 3 lines · Kuba Piecuch
Ah, interesting, I didn't know there's a pure-BPF implementation of spinlocks! I haven't really had the chance to play around with arenas yet, looks like I'm missing out ;-)
Yeah, I'm pretty excited about it. If you have a moment, take a look at the qmap code in the following RFC branch:
- Native FIFO data structure in arena. (SCX repo already has atq and other usages but this is a simplistic example)
- qmap_set_cmask() (the RFC part) shows kernel <-> BPF communication through arena memory where kernel directly writes to arena and pass the arena pointer to ops.set_cmask().
Thanks.
Signature · 1 line
tejun
Hi Tejun, Kuba, Andrea,
Quoted 17 lines · Tejun Heo
Quoted 1 line
Could you elaborate a bit on what you mean by "properly synchronized" here?
If
ops.dequeue()synchronizes with the dispatch path so that the task being dequeued is either dequeued or dispatched, there's nothing else to protect. Ifops.dequeue()wins, the task won't be dispatched. Ifops.dequeue()loses, the task should already be in either the dispatch buffer or local DSQ and the kernel dequeue code will shoot them down. In the former case, at the dispatch buffer flush time, the task would either be already dequeued or re-enqueued with a different qseq and ignored. In the latter,dispatch_dequeue()would remove it from the local DSQ.Quoted 5 lines
To me, introducing cookies is primarily about adding flexibility around managing the "dispatch window" between the qseq being probed and the actual dispatch attempt in
finish_dispatch(). For example, a CPU can get a cookie and pass it to another CPU to perform the dispatch, which is not possible with the current interface.
Quoted 3 lines
So, this is mostly for schedulers that don't want to or for some reason can't implement proper synchronization between dequeue and dispatch paths. A convenient thing to make life a bit easier.
I think we are on the same page! I'd like to take ownership of the cookie implementation.
Andrea, as we discussed offline, please feel free to send the deferred SCX bandwidth implementation, since you're already working on that. I want to focus on the cookie implementation first, since the design has been discussed for a while and I want to move it forward.
Here's the API I'm planning to propose:
- u64 scx_bpf_task_get_cookie(struct task_struct *p)
Returns an opaque cookie representing @p's current qseq. The caller can store this in a BPF map and pass it to another CPU.
- bool scx_bpf_dsq_insert_with_cookie(struct task_struct *p, u64 dsq_id,
u64 enq_flags, u64 cookie)
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, which is safe.
Will send a follow-up patch soon, thanks!
Signature · 2 lines
Cheers, Cheng-Yang
Hi Andrea,
Quoted 30 lines · Andrea Righi
A BPF scheduler may rely on
p->cpus_ptrfromops.dispatch()to select a target CPU. However, task affinity can change between the dispatch decision and its finalization infinish_dispatch(). When this happens, the scheduler may attempt to dispatch a task to a CPU that is no longer allowed, resulting in fatal errors such as:EXIT: runtime error (SCX_DSQ_LOCAL[_ON] target CPU 10 not allowed for stress-ng-race-[13565])
This race exists because
ops.dispatch()runs without holding the task's run queue lock, allowing a concurrentset_cpus_allowed()to updatep->cpus_ptrwhile the BPF scheduler is still using it. The dispatch is then finalized using stale affinity information.Example timeline:
CPU0 CPU1
---- ----task_rq_lock(p) if (cpumask_test_cpu(cpu, p->cpus_ptr)) set_cpus_allowed_scx(p, new_mask) task_rq_unlock(p) scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu, 0)With commit
ebf1ccff79c4("sched_ext: Fixops.dequeue()semantics"), BPF schedulers can avoid the affinity race by tracking task state and handling %SCX_DEQ_SCHED_CHANGE inops.dequeue(): when a task is dequeued due to a property change, the scheduler can update the task state and skip the direct dispatch fromops.dispatch()for non-queued tasks.
Quoted 4 lines
However, schedulers that do not implement task state tracking and dispatch directly to a local DSQ directly from
ops.dispatch()may trigger thescx_error()condition when the kernel validates the destination indispatch_to_local_dsq().
The two paragraphs above mention "direct dispatch from ops.dispatch()" and "dispatch directly to a local DSQ directly from ops.dispatch()". My understanding is that a "direct dispatch" can only happen from ops.select_cpu() or ops.enqueue(), not from ops.dispatch(). Is this just an unfortunate choice of words?
Would "dispatch to a local DSQ" be a more accurate phrase here?
Thanks,
Kuba
Andrea Righi
reviewerHi Kuba,
Quoted 38 lines · Kuba Piecuch
Hi Andrea,
Quoted 30 lines · Andrea Righi
A BPF scheduler may rely on
p->cpus_ptrfromops.dispatch()to select a target CPU. However, task affinity can change between the dispatch decision and its finalization infinish_dispatch(). When this happens, the scheduler may attempt to dispatch a task to a CPU that is no longer allowed, resulting in fatal errors such as:EXIT: runtime error (SCX_DSQ_LOCAL[_ON] target CPU 10 not allowed for stress-ng-race-[13565])
This race exists because
ops.dispatch()runs without holding the task's run queue lock, allowing a concurrentset_cpus_allowed()to updatep->cpus_ptrwhile the BPF scheduler is still using it. The dispatch is then finalized using stale affinity information.Example timeline:
CPU0 CPU1
---- ----task_rq_lock(p) if (cpumask_test_cpu(cpu, p->cpus_ptr)) set_cpus_allowed_scx(p, new_mask) task_rq_unlock(p) scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu, 0)With commit
ebf1ccff79c4("sched_ext: Fixops.dequeue()semantics"), BPF schedulers can avoid the affinity race by tracking task state and handling %SCX_DEQ_SCHED_CHANGE inops.dequeue(): when a task is dequeued due to a property change, the scheduler can update the task state and skip the direct dispatch fromops.dispatch()for non-queued tasks.Quoted 4 lines
However, schedulers that do not implement task state tracking and dispatch directly to a local DSQ directly from
ops.dispatch()may trigger thescx_error()condition when the kernel validates the destination indispatch_to_local_dsq().
Quoted 6 lines
The two paragraphs above mention "direct dispatch from
ops.dispatch()" and "dispatch directly to a local DSQ directly fromops.dispatch()". My understanding is that a "direct dispatch" can only happen fromops.select_cpu()orops.enqueue(), not fromops.dispatch(). Is this just an unfortunate choice of words?
Would "dispatch to a local DSQ" be a more accurate phrase here?
Oh yes, poor wording on my side. What I mean is scx_bpf_dsq_insert(SCX_DSQ_LOCAL_ON | cpu) from ops.dispatch(), so "dispatch to a local DSQ" is definitely better, thanks!
-Andrea