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 struct scx_dispatch_q *scx_local_or_reject_dsq(struct scx_sched *sch, struct rq *rq, 38 struct task_struct *p, u64 *enq_flags); 39 bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p); 40 void scx_reenq_reject(struct rq *rq); 41 42 static inline const char *sch_cgrp_path(struct scx_sched *sch) 43 { 44 return sch->cgrp_path; 45 } 46 47 #else /* CONFIG_EXT_SUB_SCHED */ 48 49 static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; } 50 static inline struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root) { return NULL; } 51 static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {} 52 static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; } 53 static inline const char *sch_cgrp_path(struct scx_sched *sch) { return "/"; } 54 static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {} 55 static inline void drain_descendants(struct scx_sched *sch) { } 56 static inline void scx_sub_disable(struct scx_sched *sch) { } 57 static inline void scx_free_pshards(struct scx_sched *sch) {} 58 static inline s32 scx_alloc_pshards(struct scx_sched *sch) { return 0; } 59 static inline void scx_init_root_caps(struct scx_sched *sch) {} 60 static inline void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev) {} 61 static inline void scx_online_ecaps(struct rq *rq) {} 62 static inline void scx_offline_ecaps(struct rq *rq) {} 63 static inline void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu) {} 64 static inline void scx_discard_stale_ecaps_syncs(void) {} 65 static inline struct scx_dispatch_q *scx_local_or_reject_dsq(struct scx_sched *sch, struct rq *rq, struct task_struct *p, u64 *enq_flags) { return &rq->scx.local_dsq; } 66 static inline bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p) { return false; } 67 static inline void scx_reenq_reject(struct rq *rq) {} 68 69 #endif /* CONFIG_EXT_SUB_SCHED */ 70 71 /** 72 * scx_for_each_descendant_pre - pre-order walk of a sched's descendants 73 * @pos: iteration cursor 74 * @root: sched to walk the descendants of 75 * 76 * Walk @root's descendants. @root is included in the iteration and the first 77 * node to be visited. Must be called with scx_enable_mutex, scx_sched_lock, or 78 * RCU read lock. 79 */ 80 #define scx_for_each_descendant_pre(pos, root) \ 81 for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos); \ 82 (pos) = scx_next_descendant_pre((pos), (root))) 83 84 #ifdef CONFIG_EXT_SUB_SCHED 85 86 /** 87 * scx_missing_caps - The caps in @needed that @sch lacks on @cpu 88 * @sch: sched to test 89 * @cpu: cpu to test on 90 * @needed: bitmask of SCX_CAP_* values 91 * 92 * Return the caps in @needed that @sch lacks for @cpu, 0 if it holds them all. 93 */ 94 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) 95 { 96 u64 ecaps; 97 98 /* root holds every cap on every cpu */ 99 if (!sch->level) 100 return 0; 101 102 ecaps = READ_ONCE(per_cpu_ptr(sch->pcpu, cpu)->ecaps); 103 104 return needed & ~ecaps; 105 } 106 107 /* 108 * Cap semantics: which caps an action requires, and which caps a cap implies. 109 * Keep all such mappings collected here. 110 */ 111 112 /* map @enq_flags to the SCX_CAP_* bit required for the local-DSQ insert */ 113 static inline u64 scx_caps_for_enq(u64 enq_flags) 114 { 115 return 0; 116 } 117 118 /* map queued @p to the SCX_CAP_* bit required to stay on its local DSQ */ 119 static inline u64 scx_caps_for_task(struct task_struct *p) 120 { 121 return 0; 122 } 123 124 /* caps implied by holding @cap */ 125 static inline u64 scx_caps_implied(u64 cap) 126 { 127 return 0; 128 } 129 130 #else /* CONFIG_EXT_SUB_SCHED */ 131 132 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) { return 0; } 133 134 #endif /* CONFIG_EXT_SUB_SCHED */ 135 136 /* 137 * One user of this function is scx_bpf_dispatch() which can be called 138 * recursively as sub-sched dispatches nest. Always inline to reduce stack usage 139 * from the call frame. 140 */ 141 static __always_inline bool 142 scx_dispatch_sched(struct scx_sched *sch, struct rq *rq, 143 struct task_struct *prev, bool nested) 144 { 145 struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 146 int nr_loops = SCX_DSP_MAX_LOOPS; 147 s32 cpu = cpu_of(rq); 148 bool prev_on_sch = (prev->sched_class == &ext_sched_class) && 149 scx_task_on_sched(sch, prev); 150 151 if (scx_consume_global_dsq(sch, rq)) 152 return true; 153 154 if (scx_bypass_dsp_enabled(sch)) { 155 /* if @sch is bypassing, only the bypass DSQs are active */ 156 if (scx_bypassing(sch, cpu)) 157 return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 158 159 #ifdef CONFIG_EXT_SUB_SCHED 160 /* 161 * If @sch isn't bypassing but its children are, @sch is 162 * responsible for making forward progress for both its own 163 * tasks that aren't bypassing and the bypassing descendants' 164 * tasks. The following implements a simple built-in behavior - 165 * let each CPU try to run the bypass DSQ every Nth time. 166 * 167 * Later, if necessary, we can add an ops flag to suppress the 168 * auto-consumption and a kfunc to consume the bypass DSQ and, 169 * so that the BPF scheduler can fully control scheduling of 170 * bypassed tasks. 171 */ 172 struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 173 174 if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) && 175 scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0)) { 176 __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1); 177 return true; 178 } 179 #endif /* CONFIG_EXT_SUB_SCHED */ 180 } 181 182 if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !scx_rq_online(rq)) 183 return false; 184 185 dspc->rq = rq; 186 187 /* 188 * The dispatch loop. Because scx_flush_dispatch_buf() may drop the rq 189 * lock, the local DSQ might still end up empty after a successful 190 * ops.dispatch(). If the local DSQ is empty even after ops.dispatch() 191 * produced some tasks, retry. The BPF scheduler may depend on this 192 * looping behavior to simplify its implementation. 193 */ 194 do { 195 dspc->nr_tasks = 0; 196 197 if (nested) { 198 SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 199 prev_on_sch ? prev : NULL); 200 } else { 201 /* stash @prev so that nested invocations can access it */ 202 rq->scx.sub_dispatch_prev = prev; 203 SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 204 prev_on_sch ? prev : NULL); 205 rq->scx.sub_dispatch_prev = NULL; 206 } 207 208 scx_flush_dispatch_buf(sch, rq); 209 210 if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice) { 211 rq->scx.flags |= SCX_RQ_BAL_KEEP; 212 return true; 213 } 214 if (rq->scx.local_dsq.nr) 215 return true; 216 if (scx_consume_global_dsq(sch, rq)) 217 return true; 218 219 /* 220 * ops.dispatch() can trap us in this loop by repeatedly 221 * dispatching ineligible tasks. Break out once in a while to 222 * allow the watchdog to run. As IRQ can't be enabled in 223 * balance(), we want to complete this scheduling cycle and then 224 * start a new one. IOW, we want to call resched_curr() on the 225 * next, most likely idle, task, not the current one. Use 226 * __scx_bpf_kick_cpu() for deferred kicking. 227 */ 228 if (unlikely(!--nr_loops)) { 229 scx_kick_cpu(sch, cpu, 0); 230 break; 231 } 232 } while (dspc->nr_tasks); 233 234 /* 235 * Prevent the CPU from going idle while bypassed descendants have tasks 236 * queued. Without this fallback, bypassed tasks could stall if the host 237 * scheduler's ops.dispatch() doesn't yield any tasks. 238 */ 239 if (scx_bypass_dsp_enabled(sch)) 240 return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 241 242 return false; 243 } 244 245 #endif /* _KERNEL_SCHED_EXT_SUB_H */ 246