Re: [PATCH 2/3] sched_ext: Implement SCX_ENQ_IMMED
From: Andrea Righi
Date: Mon Mar 09 2026 - 13:39:23 EST
Hi Tejun,
On Fri, Mar 06, 2026 at 02:28:16PM -1000, Tejun Heo wrote:
> Add SCX_ENQ_IMMED enqueue flag for inserting into local DSQs. It requests
> that the task be queued on the CPU's local DSQ only if it can execute
> immediately - the current task is done and no other tasks are waiting. If the
> CPU is busy, the task is re-enqueued back to the BPF scheduler with
> SCX_TASK_REENQ_IMMED so that it can be dispatched elsewhere. When multiple
> IMMED tasks are inserted, only the first one stays if the current task is
> done and the rest are re-enqueued.
>
> One intended use case is enabling opportunistic CPU sharing across multiple
> sub-schedulers. Without this, a sub-scheduler can stuff the local DSQ of a
> shared CPU, making it difficult for others to use. More generally, multiple
> tasks on a local DSQ can cause high latencies, and stricter control can help.
>
> Signed-off-by: Tejun Heo <tj@xxxxxxxxxx>
> ---
...
> @@ -3818,15 +3929,31 @@ static void process_deferred_reenq_locals(struct rq *rq)
> sch_pcpu = container_of(drl, struct scx_sched_pcpu,
> deferred_reenq_local);
> sch = sch_pcpu->sch;
> +
> reenq_flags = drl->flags;
> WRITE_ONCE(drl->flags, 0);
> list_del_init(&drl->node);
> +
> + if (likely(drl->seq != seq)) {
> + drl->seq = seq;
> + drl->cnt = 0;
> + } else {
> + if (unlikely(++drl->cnt > SCX_REENQ_LOCAL_MAX_REPEAT)) {
> + scx_error(sch, "SCX_ENQ_REENQ on SCX_DSQ_LOCAL repeated %u times",
> + drl->cnt);
Instead of triggering an error here, should we simply accept the task into
the local DSQ and ignore the fact that the CPU is busy?
I'm thinking at the SCX_OPS_ALWAYS_ENQ_IMMED scenario. In that case, the
scheduler can't clear the ENQ_IMMED flag, so it may hit this error loop
limitation, unless it explicitly bounces the task to a non-local DSQ at
some point.
> + skip = true;
> + }
> +
> + __scx_add_event(sch, SCX_EV_REENQ_LOCAL_REPEAT, 1);
> + }
> }
>
> - /* see schedule_dsq_reenq() */
> - smp_mb();
> + if (!skip) {
> + /* see schedule_dsq_reenq() */
> + smp_mb();
>
> - reenq_local(sch, rq, reenq_flags);
> + reenq_local(sch, rq, reenq_flags);
> + }
> }
> }
...
> diff --git a/kernel/sched/ext_internal.h b/kernel/sched/ext_internal.h
> index f8df73044515..cd4272117be4 100644
> --- a/kernel/sched/ext_internal.h
> +++ b/kernel/sched/ext_internal.h
> @@ -31,6 +31,8 @@ enum scx_consts {
> SCX_BYPASS_LB_MIN_DELTA_DIV = 4,
> SCX_BYPASS_LB_BATCH = 256,
>
> + SCX_REENQ_LOCAL_MAX_REPEAT = 256,
That's a lot of re-enqueues. What if we simply ignore SCX_ENQ_IMMED when
SCX_ENQ_REENQ is set?
This would solve the SCX_OPS_ALWAYS_ENQ_IMMED issue and naturally limit the
loop to a single retry:
- first attempt (IMMED) fails -> task re-enqueued with REENQ flag,
- second attempt sees REENQ -> ignores IMMED check -> queues normally on
local DSQ.
This approach seems more robust and would avoid the latency overhead of
repeated failures (the re-enqueues were actually the reason of the latency
issues that I was experiencing). If I don't use SCX_OPS_ALWAYS_ENQ_IMMED
and I selectively use SCX_ENQ_IMMED with just one retry I can actually see
some small, but consistent, benefits with scx_cosmos running some latency
benchmarks.
What do you think?
Thanks,
-Andrea