17f480f34STejun Heo /* SPDX-License-Identifier: GPL-2.0 */
27f480f34STejun Heo /*
37f480f34STejun Heo * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst
47f480f34STejun Heo *
57f480f34STejun Heo * Inline definitions layered on top of internal.h and cid.h.
67f480f34STejun Heo *
77f480f34STejun Heo * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
87f480f34STejun Heo * Copyright (c) 2026 Tejun Heo <tj@kernel.org>
97f480f34STejun Heo */
107f480f34STejun Heo #ifndef _KERNEL_SCHED_EXT_INLINES_H
117f480f34STejun Heo #define _KERNEL_SCHED_EXT_INLINES_H
127f480f34STejun Heo
137f480f34STejun Heo #include "internal.h"
147f480f34STejun Heo #include "cid.h"
157f480f34STejun Heo
161be10bb0STejun Heo /* what dispatch concluded, consumed by the pick that follows */
171be10bb0STejun Heo enum scx_dsp_verdict {
181be10bb0STejun Heo SCX_DSP_NONE, /* nothing to run */
191be10bb0STejun Heo SCX_DSP_LOCAL, /* local DSQ has tasks */
201be10bb0STejun Heo SCX_DSP_PREV, /* keep running @prev */
211be10bb0STejun Heo SCX_DSP_RETRY, /* pick helpers only: restart the pick */
221be10bb0STejun Heo };
231be10bb0STejun Heo
247f480f34STejun Heo /*
251d0a73ddSTao Cui * One user of this function is scx_bpf_sub_dispatch() which can be called
267f480f34STejun Heo * recursively as sub-sched dispatches nest. Always inline to reduce stack usage
277f480f34STejun Heo * from the call frame.
287f480f34STejun Heo */
291be10bb0STejun Heo static __always_inline enum scx_dsp_verdict
scx_dispatch_sched(struct scx_sched * sch,struct rq * rq,struct task_struct * prev,bool nested)307f480f34STejun Heo scx_dispatch_sched(struct scx_sched *sch, struct rq *rq,
317f480f34STejun Heo struct task_struct *prev, bool nested)
327f480f34STejun Heo {
337f480f34STejun Heo struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx;
347f480f34STejun Heo int nr_loops = SCX_DSP_MAX_LOOPS;
357f480f34STejun Heo s32 cpu = cpu_of(rq);
367f480f34STejun Heo bool prev_on_sch = (prev->sched_class == &ext_sched_class) &&
377f480f34STejun Heo scx_task_on_sched(sch, prev);
387f480f34STejun Heo
397f480f34STejun Heo if (scx_consume_global_dsq(sch, rq))
401be10bb0STejun Heo return SCX_DSP_LOCAL;
417f480f34STejun Heo
427f480f34STejun Heo if (scx_bypass_dsp_enabled(sch)) {
437f480f34STejun Heo /* if @sch is bypassing, only the bypass DSQs are active */
441be10bb0STejun Heo if (scx_bypassing(sch, cpu)) {
451be10bb0STejun Heo if (scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0))
461be10bb0STejun Heo return SCX_DSP_LOCAL;
471be10bb0STejun Heo return SCX_DSP_NONE;
481be10bb0STejun Heo }
497f480f34STejun Heo
507f480f34STejun Heo #ifdef CONFIG_EXT_SUB_SCHED
517f480f34STejun Heo /*
527f480f34STejun Heo * If @sch isn't bypassing but its children are, @sch is
537f480f34STejun Heo * responsible for making forward progress for both its own
547f480f34STejun Heo * tasks that aren't bypassing and the bypassing descendants'
557f480f34STejun Heo * tasks. The following implements a simple built-in behavior -
567f480f34STejun Heo * let each CPU try to run the bypass DSQ every Nth time.
577f480f34STejun Heo *
587f480f34STejun Heo * Later, if necessary, we can add an ops flag to suppress the
597f480f34STejun Heo * auto-consumption and a kfunc to consume the bypass DSQ and,
607f480f34STejun Heo * so that the BPF scheduler can fully control scheduling of
617f480f34STejun Heo * bypassed tasks.
627f480f34STejun Heo */
637f480f34STejun Heo struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu);
647f480f34STejun Heo
657f480f34STejun Heo if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) &&
667f480f34STejun Heo scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0)) {
677f480f34STejun Heo __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1);
681be10bb0STejun Heo return SCX_DSP_LOCAL;
697f480f34STejun Heo }
707f480f34STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */
717f480f34STejun Heo }
727f480f34STejun Heo
737f480f34STejun Heo if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !scx_rq_online(rq))
741be10bb0STejun Heo return SCX_DSP_NONE;
757f480f34STejun Heo
767f480f34STejun Heo dspc->rq = rq;
777f480f34STejun Heo
787f480f34STejun Heo /*
797f480f34STejun Heo * The dispatch loop. Because scx_flush_dispatch_buf() may drop the rq
807f480f34STejun Heo * lock, the local DSQ might still end up empty after a successful
817f480f34STejun Heo * ops.dispatch(). If the local DSQ is empty even after ops.dispatch()
827f480f34STejun Heo * produced some tasks, retry. The BPF scheduler may depend on this
837f480f34STejun Heo * looping behavior to simplify its implementation.
847f480f34STejun Heo */
857f480f34STejun Heo do {
867f480f34STejun Heo dspc->nr_tasks = 0;
877f480f34STejun Heo
886ba3bd6fSTejun Heo #ifdef CONFIG_EXT_SUB_SCHED
897f480f34STejun Heo /* stash @prev so that nested invocations can access it */
906ba3bd6fSTejun Heo if (!nested)
917f480f34STejun Heo rq->scx.sub_dispatch_prev = prev;
926ba3bd6fSTejun Heo #endif
936ba3bd6fSTejun Heo
947f480f34STejun Heo SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu),
957f480f34STejun Heo prev_on_sch ? prev : NULL);
966ba3bd6fSTejun Heo
976ba3bd6fSTejun Heo #ifdef CONFIG_EXT_SUB_SCHED
986ba3bd6fSTejun Heo if (!nested)
997f480f34STejun Heo rq->scx.sub_dispatch_prev = NULL;
1006ba3bd6fSTejun Heo #endif
1017f480f34STejun Heo
1027f480f34STejun Heo scx_flush_dispatch_buf(sch, rq);
1037f480f34STejun Heo
1041be10bb0STejun Heo if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice)
1051be10bb0STejun Heo return SCX_DSP_PREV;
1067f480f34STejun Heo if (rq->scx.local_dsq.nr)
1071be10bb0STejun Heo return SCX_DSP_LOCAL;
1087f480f34STejun Heo if (scx_consume_global_dsq(sch, rq))
1091be10bb0STejun Heo return SCX_DSP_LOCAL;
1107f480f34STejun Heo
1117f480f34STejun Heo /*
1127f480f34STejun Heo * ops.dispatch() can trap us in this loop by repeatedly
1137f480f34STejun Heo * dispatching ineligible tasks. Break out once in a while to
1147f480f34STejun Heo * allow the watchdog to run. As IRQ can't be enabled in
115*3167bd3eSTejun Heo * dispatch, we want to complete this scheduling cycle and then
1167f480f34STejun Heo * start a new one. IOW, we want to call resched_curr() on the
1177f480f34STejun Heo * next, most likely idle, task, not the current one. Use
1187f480f34STejun Heo * __scx_bpf_kick_cpu() for deferred kicking.
1197f480f34STejun Heo */
1207f480f34STejun Heo if (unlikely(!--nr_loops)) {
1217f480f34STejun Heo scx_kick_cpu(sch, cpu, 0);
1227f480f34STejun Heo break;
1237f480f34STejun Heo }
1247f480f34STejun Heo } while (dspc->nr_tasks);
1257f480f34STejun Heo
1267f480f34STejun Heo /*
1277f480f34STejun Heo * Prevent the CPU from going idle while bypassed descendants have tasks
1287f480f34STejun Heo * queued. Without this fallback, bypassed tasks could stall if the host
1297f480f34STejun Heo * scheduler's ops.dispatch() doesn't yield any tasks.
1307f480f34STejun Heo */
1311be10bb0STejun Heo if (scx_bypass_dsp_enabled(sch) &&
1321be10bb0STejun Heo scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0))
1331be10bb0STejun Heo return SCX_DSP_LOCAL;
1347f480f34STejun Heo
1351be10bb0STejun Heo return SCX_DSP_NONE;
1367f480f34STejun Heo }
1377f480f34STejun Heo
1387f480f34STejun Heo #endif /* _KERNEL_SCHED_EXT_INLINES_H */
139