1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst 4 * 5 * Inline definitions layered on top of internal.h and cid.h. 6 * 7 * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. 8 * Copyright (c) 2026 Tejun Heo <tj@kernel.org> 9 */ 10 #ifndef _KERNEL_SCHED_EXT_INLINES_H 11 #define _KERNEL_SCHED_EXT_INLINES_H 12 13 #include "internal.h" 14 #include "cid.h" 15 16 /* what dispatch concluded, consumed by the pick that follows */ 17 enum scx_dsp_verdict { 18 SCX_DSP_NONE, /* nothing to run */ 19 SCX_DSP_LOCAL, /* local DSQ has tasks */ 20 SCX_DSP_PREV, /* keep running @prev */ 21 SCX_DSP_RETRY, /* restart the pick */ 22 }; 23 24 /** 25 * scx_dsp_prev_stale - Test whether dispatch's @prev went stale 26 * @rq: rq being dispatched for 27 * @prev: @rq's donor when dispatch started 28 * 29 * @prev can stop being @rq's donor while dispatch has the rq lock dropped. With 30 * proxy execution, @prev may not be the running task, and another CPU can reset 31 * the donor to rq->curr. With core scheduling, the core pick runs dispatch for 32 * the sibling rqs too, and a sibling can switch @prev out. Dispatch restarts 33 * the pick when that happened and doesn't hand the old donor to the BPF 34 * scheduler. 35 * 36 * With proxy execution alone, the donor only ever changes to rq->curr during a 37 * pick. Only __schedule() on this CPU sets a different donor, after the pick, 38 * so the restarted pick takes rq->curr as @prev and can't go stale again. Under 39 * core scheduling, the restart goes through the core pick, which already 40 * retries whenever dispatch dropped the lock. 41 */ 42 static inline bool scx_dsp_prev_stale(struct rq *rq, struct task_struct *prev) 43 { 44 return (sched_proxy_exec() || sched_core_enabled(rq)) && unlikely(prev != rq->donor); 45 } 46 47 /* 48 * One user of this function is scx_bpf_sub_dispatch() which can be called 49 * recursively as sub-sched dispatches nest. Always inline to reduce stack usage 50 * from the call frame. 51 */ 52 static __always_inline enum scx_dsp_verdict 53 scx_dispatch_sched(struct scx_sched *sch, struct rq *rq, 54 struct task_struct *prev, bool nested) 55 { 56 struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 57 int nr_loops = SCX_DSP_MAX_LOOPS; 58 s32 cpu = cpu_of(rq); 59 bool prev_on_sch; 60 61 if (scx_consume_global_dsq(sch, rq)) 62 return SCX_DSP_LOCAL; 63 64 /* 65 * A bypass consume puts a task on the cpu being dispatched for, to run 66 * right away. That is an IMMED insert and needs only 67 * %SCX_CAP_ENQ_IMMED, the base cap. Dispatch descends into @sch only on 68 * a cpu where @sch holds the base cap, so @sch holds it here. The same 69 * holds for the consume at the end of this function. 70 */ 71 if (scx_bypass_dsp_enabled(sch)) { 72 /* if @sch is bypassing, only the bypass DSQs are active */ 73 if (scx_bypassing(sch, cpu)) { 74 if (scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 75 SCX_ENQ_IMMED)) 76 return SCX_DSP_LOCAL; 77 return SCX_DSP_NONE; 78 } 79 80 #ifdef CONFIG_EXT_SUB_SCHED 81 /* 82 * If @sch isn't bypassing but its children are, @sch is 83 * responsible for making forward progress for both its own 84 * tasks that aren't bypassing and the bypassing descendants' 85 * tasks. The following implements a simple built-in behavior - 86 * let each CPU try to run the bypass DSQ every Nth time. 87 * 88 * Later, if necessary, we can add an ops flag to suppress the 89 * auto-consumption and a kfunc to consume the bypass DSQ and, 90 * so that the BPF scheduler can fully control scheduling of 91 * bypassed tasks. 92 */ 93 struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 94 95 if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) && 96 scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 97 SCX_ENQ_IMMED)) { 98 __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1); 99 return SCX_DSP_LOCAL; 100 } 101 #endif /* CONFIG_EXT_SUB_SCHED */ 102 } 103 104 /* 105 * scx_rq_online() can't be used. Its cpu_active() test goes false 106 * before CPU hotplug waits for an RCU grace period, and 107 * handle_hotplug() moves this CPU's tasks to the local DSQ only after 108 * the wait. The grace period can depend on those tasks running. 109 */ 110 if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !(rq->scx.flags & SCX_RQ_ONLINE)) 111 return SCX_DSP_NONE; 112 113 dspc->rq = rq; 114 115 /* 116 * The dispatch loop. Because scx_flush_dispatch_buf() may drop the rq 117 * lock, the local DSQ might still end up empty after a successful 118 * ops.dispatch(). If the local DSQ is empty even after ops.dispatch() 119 * produced some tasks, retry. The BPF scheduler may depend on this 120 * looping behavior to simplify its implementation. 121 */ 122 do { 123 dspc->nr_tasks = 0; 124 125 #ifdef CONFIG_EXT_SUB_SCHED 126 /* stash @prev so that nested invocations can access it */ 127 if (!nested) 128 rq->scx.sub_dispatch_prev = prev; 129 #endif 130 131 /* the dispatch so far may have dropped the rq lock */ 132 prev_on_sch = !scx_dsp_prev_stale(rq, prev) && 133 prev->sched_class == &ext_sched_class && 134 scx_task_on_sched(sch, prev); 135 136 SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 137 prev_on_sch ? prev : NULL); 138 139 #ifdef CONFIG_EXT_SUB_SCHED 140 if (!nested) 141 rq->scx.sub_dispatch_prev = NULL; 142 #endif 143 144 scx_flush_dispatch_buf(sch, rq); 145 146 /* ops.dispatch() and the flush may have dropped the rq lock */ 147 if (!scx_dsp_prev_stale(rq, prev) && 148 (prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice) 149 return SCX_DSP_PREV; 150 if (rq->scx.local_dsq.nr) 151 return SCX_DSP_LOCAL; 152 if (scx_consume_global_dsq(sch, rq)) 153 return SCX_DSP_LOCAL; 154 155 /* 156 * ops.dispatch() can trap us in this loop by repeatedly 157 * dispatching ineligible tasks. Break out once in a while to 158 * allow the watchdog to run. As IRQ can't be enabled in 159 * dispatch, we want to complete this scheduling cycle and then 160 * start a new one. IOW, we want to call resched_curr() on the 161 * next, most likely idle, task, not the current one. Use 162 * __scx_bpf_kick_cpu() for deferred kicking. 163 */ 164 if (unlikely(!--nr_loops)) { 165 scx_kick_cpu(sch, cpu, 0); 166 break; 167 } 168 } while (dspc->nr_tasks); 169 170 /* 171 * Prevent the CPU from going idle while bypassed descendants have tasks 172 * queued. Without this fallback, bypassed tasks could stall if the host 173 * scheduler's ops.dispatch() doesn't yield any tasks. 174 */ 175 if (scx_bypass_dsp_enabled(sch) && 176 scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), SCX_ENQ_IMMED)) { 177 __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1); 178 return SCX_DSP_LOCAL; 179 } 180 181 return SCX_DSP_NONE; 182 } 183 184 #endif /* _KERNEL_SCHED_EXT_INLINES_H */ 185