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