1*daf8e166STejun Heo /* SPDX-License-Identifier: GPL-2.0 */ 2*daf8e166STejun Heo /* 3*daf8e166STejun Heo * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst 4*daf8e166STejun Heo * 5*daf8e166STejun Heo * Sub-scheduler hierarchy support. 6*daf8e166STejun Heo * 7*daf8e166STejun Heo * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. 8*daf8e166STejun Heo * Copyright (c) 2026 Tejun Heo <tj@kernel.org> 9*daf8e166STejun Heo */ 10*daf8e166STejun Heo #ifndef _KERNEL_SCHED_EXT_SUB_H 11*daf8e166STejun Heo #define _KERNEL_SCHED_EXT_SUB_H 12*daf8e166STejun Heo 13*daf8e166STejun Heo #include "internal.h" 14*daf8e166STejun Heo #include "cid.h" 15*daf8e166STejun Heo 16*daf8e166STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 17*daf8e166STejun Heo 18*daf8e166STejun Heo struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root); 19*daf8e166STejun Heo void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch); 20*daf8e166STejun Heo struct cgroup *sch_cgroup(struct scx_sched *sch); 21*daf8e166STejun Heo void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch); 22*daf8e166STejun Heo void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog); 23*daf8e166STejun Heo void drain_descendants(struct scx_sched *sch); 24*daf8e166STejun Heo void scx_sub_disable(struct scx_sched *sch); 25*daf8e166STejun Heo void scx_sub_enable_workfn(struct kthread_work *work); 26*daf8e166STejun Heo bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux); 27*daf8e166STejun Heo 28*daf8e166STejun Heo #else /* CONFIG_EXT_SUB_SCHED */ 29*daf8e166STejun Heo 30*daf8e166STejun Heo static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; } 31*daf8e166STejun Heo static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {} 32*daf8e166STejun Heo static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; } 33*daf8e166STejun Heo static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {} 34*daf8e166STejun Heo static inline void drain_descendants(struct scx_sched *sch) { } 35*daf8e166STejun Heo static inline void scx_sub_disable(struct scx_sched *sch) { } 36*daf8e166STejun Heo 37*daf8e166STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 38*daf8e166STejun Heo 39*daf8e166STejun Heo /** 40*daf8e166STejun Heo * scx_for_each_descendant_pre - pre-order walk of a sched's descendants 41*daf8e166STejun Heo * @pos: iteration cursor 42*daf8e166STejun Heo * @root: sched to walk the descendants of 43*daf8e166STejun Heo * 44*daf8e166STejun Heo * Walk @root's descendants. @root is included in the iteration and the first 45*daf8e166STejun Heo * node to be visited. Must be called with either scx_enable_mutex or 46*daf8e166STejun Heo * scx_sched_lock held. 47*daf8e166STejun Heo */ 48*daf8e166STejun Heo #define scx_for_each_descendant_pre(pos, root) \ 49*daf8e166STejun Heo for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos); \ 50*daf8e166STejun Heo (pos) = scx_next_descendant_pre((pos), (root))) 51*daf8e166STejun Heo 52*daf8e166STejun Heo /* 53*daf8e166STejun Heo * One user of this function is scx_bpf_dispatch() which can be called 54*daf8e166STejun Heo * recursively as sub-sched dispatches nest. Always inline to reduce stack usage 55*daf8e166STejun Heo * from the call frame. 56*daf8e166STejun Heo */ 57*daf8e166STejun Heo static __always_inline bool 58*daf8e166STejun Heo scx_dispatch_sched(struct scx_sched *sch, struct rq *rq, 59*daf8e166STejun Heo struct task_struct *prev, bool nested) 60*daf8e166STejun Heo { 61*daf8e166STejun Heo struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 62*daf8e166STejun Heo int nr_loops = SCX_DSP_MAX_LOOPS; 63*daf8e166STejun Heo s32 cpu = cpu_of(rq); 64*daf8e166STejun Heo bool prev_on_sch = (prev->sched_class == &ext_sched_class) && 65*daf8e166STejun Heo scx_task_on_sched(sch, prev); 66*daf8e166STejun Heo 67*daf8e166STejun Heo if (scx_consume_global_dsq(sch, rq)) 68*daf8e166STejun Heo return true; 69*daf8e166STejun Heo 70*daf8e166STejun Heo if (scx_bypass_dsp_enabled(sch)) { 71*daf8e166STejun Heo /* if @sch is bypassing, only the bypass DSQs are active */ 72*daf8e166STejun Heo if (scx_bypassing(sch, cpu)) 73*daf8e166STejun Heo return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 74*daf8e166STejun Heo 75*daf8e166STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 76*daf8e166STejun Heo /* 77*daf8e166STejun Heo * If @sch isn't bypassing but its children are, @sch is 78*daf8e166STejun Heo * responsible for making forward progress for both its own 79*daf8e166STejun Heo * tasks that aren't bypassing and the bypassing descendants' 80*daf8e166STejun Heo * tasks. The following implements a simple built-in behavior - 81*daf8e166STejun Heo * let each CPU try to run the bypass DSQ every Nth time. 82*daf8e166STejun Heo * 83*daf8e166STejun Heo * Later, if necessary, we can add an ops flag to suppress the 84*daf8e166STejun Heo * auto-consumption and a kfunc to consume the bypass DSQ and, 85*daf8e166STejun Heo * so that the BPF scheduler can fully control scheduling of 86*daf8e166STejun Heo * bypassed tasks. 87*daf8e166STejun Heo */ 88*daf8e166STejun Heo struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 89*daf8e166STejun Heo 90*daf8e166STejun Heo if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) && 91*daf8e166STejun Heo scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0)) { 92*daf8e166STejun Heo __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1); 93*daf8e166STejun Heo return true; 94*daf8e166STejun Heo } 95*daf8e166STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 96*daf8e166STejun Heo } 97*daf8e166STejun Heo 98*daf8e166STejun Heo if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !scx_rq_online(rq)) 99*daf8e166STejun Heo return false; 100*daf8e166STejun Heo 101*daf8e166STejun Heo dspc->rq = rq; 102*daf8e166STejun Heo 103*daf8e166STejun Heo /* 104*daf8e166STejun Heo * The dispatch loop. Because scx_flush_dispatch_buf() may drop the rq 105*daf8e166STejun Heo * lock, the local DSQ might still end up empty after a successful 106*daf8e166STejun Heo * ops.dispatch(). If the local DSQ is empty even after ops.dispatch() 107*daf8e166STejun Heo * produced some tasks, retry. The BPF scheduler may depend on this 108*daf8e166STejun Heo * looping behavior to simplify its implementation. 109*daf8e166STejun Heo */ 110*daf8e166STejun Heo do { 111*daf8e166STejun Heo dspc->nr_tasks = 0; 112*daf8e166STejun Heo 113*daf8e166STejun Heo if (nested) { 114*daf8e166STejun Heo SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 115*daf8e166STejun Heo prev_on_sch ? prev : NULL); 116*daf8e166STejun Heo } else { 117*daf8e166STejun Heo /* stash @prev so that nested invocations can access it */ 118*daf8e166STejun Heo rq->scx.sub_dispatch_prev = prev; 119*daf8e166STejun Heo SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 120*daf8e166STejun Heo prev_on_sch ? prev : NULL); 121*daf8e166STejun Heo rq->scx.sub_dispatch_prev = NULL; 122*daf8e166STejun Heo } 123*daf8e166STejun Heo 124*daf8e166STejun Heo scx_flush_dispatch_buf(sch, rq); 125*daf8e166STejun Heo 126*daf8e166STejun Heo if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice) { 127*daf8e166STejun Heo rq->scx.flags |= SCX_RQ_BAL_KEEP; 128*daf8e166STejun Heo return true; 129*daf8e166STejun Heo } 130*daf8e166STejun Heo if (rq->scx.local_dsq.nr) 131*daf8e166STejun Heo return true; 132*daf8e166STejun Heo if (scx_consume_global_dsq(sch, rq)) 133*daf8e166STejun Heo return true; 134*daf8e166STejun Heo 135*daf8e166STejun Heo /* 136*daf8e166STejun Heo * ops.dispatch() can trap us in this loop by repeatedly 137*daf8e166STejun Heo * dispatching ineligible tasks. Break out once in a while to 138*daf8e166STejun Heo * allow the watchdog to run. As IRQ can't be enabled in 139*daf8e166STejun Heo * balance(), we want to complete this scheduling cycle and then 140*daf8e166STejun Heo * start a new one. IOW, we want to call resched_curr() on the 141*daf8e166STejun Heo * next, most likely idle, task, not the current one. Use 142*daf8e166STejun Heo * __scx_bpf_kick_cpu() for deferred kicking. 143*daf8e166STejun Heo */ 144*daf8e166STejun Heo if (unlikely(!--nr_loops)) { 145*daf8e166STejun Heo scx_kick_cpu(sch, cpu, 0); 146*daf8e166STejun Heo break; 147*daf8e166STejun Heo } 148*daf8e166STejun Heo } while (dspc->nr_tasks); 149*daf8e166STejun Heo 150*daf8e166STejun Heo /* 151*daf8e166STejun Heo * Prevent the CPU from going idle while bypassed descendants have tasks 152*daf8e166STejun Heo * queued. Without this fallback, bypassed tasks could stall if the host 153*daf8e166STejun Heo * scheduler's ops.dispatch() doesn't yield any tasks. 154*daf8e166STejun Heo */ 155*daf8e166STejun Heo if (scx_bypass_dsp_enabled(sch)) 156*daf8e166STejun Heo return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 157*daf8e166STejun Heo 158*daf8e166STejun Heo return false; 159*daf8e166STejun Heo } 160*daf8e166STejun Heo 161*daf8e166STejun Heo #endif /* _KERNEL_SCHED_EXT_SUB_H */ 162