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 /* a restored task must be put into the local DSQ regardless of caps */ 116 if (enq_flags & SCX_ENQ_IGNORE_CAPS) 117 return 0; 118 return SCX_CAP_ENQ_IMMED; 119 } 120 121 /* map queued @p to the SCX_CAP_* bit required to stay on its local DSQ */ 122 static inline u64 scx_caps_for_task(struct task_struct *p) 123 { 124 return SCX_CAP_ENQ_IMMED; 125 } 126 127 /* caps implied by holding @cap */ 128 static inline u64 scx_caps_implied(u64 cap) 129 { 130 return 0; 131 } 132 133 #else /* CONFIG_EXT_SUB_SCHED */ 134 135 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) { return 0; } 136 137 #endif /* CONFIG_EXT_SUB_SCHED */ 138 139 /* 140 * One user of this function is scx_bpf_dispatch() which can be called 141 * recursively as sub-sched dispatches nest. Always inline to reduce stack usage 142 * from the call frame. 143 */ 144 static __always_inline bool 145 scx_dispatch_sched(struct scx_sched *sch, struct rq *rq, 146 struct task_struct *prev, bool nested) 147 { 148 struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 149 int nr_loops = SCX_DSP_MAX_LOOPS; 150 s32 cpu = cpu_of(rq); 151 bool prev_on_sch = (prev->sched_class == &ext_sched_class) && 152 scx_task_on_sched(sch, prev); 153 154 if (scx_consume_global_dsq(sch, rq)) 155 return true; 156 157 if (scx_bypass_dsp_enabled(sch)) { 158 /* if @sch is bypassing, only the bypass DSQs are active */ 159 if (scx_bypassing(sch, cpu)) 160 return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 161 162 #ifdef CONFIG_EXT_SUB_SCHED 163 /* 164 * If @sch isn't bypassing but its children are, @sch is 165 * responsible for making forward progress for both its own 166 * tasks that aren't bypassing and the bypassing descendants' 167 * tasks. The following implements a simple built-in behavior - 168 * let each CPU try to run the bypass DSQ every Nth time. 169 * 170 * Later, if necessary, we can add an ops flag to suppress the 171 * auto-consumption and a kfunc to consume the bypass DSQ and, 172 * so that the BPF scheduler can fully control scheduling of 173 * bypassed tasks. 174 */ 175 struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 176 177 if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) && 178 scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0)) { 179 __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1); 180 return true; 181 } 182 #endif /* CONFIG_EXT_SUB_SCHED */ 183 } 184 185 if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !scx_rq_online(rq)) 186 return false; 187 188 dspc->rq = rq; 189 190 /* 191 * The dispatch loop. Because scx_flush_dispatch_buf() may drop the rq 192 * lock, the local DSQ might still end up empty after a successful 193 * ops.dispatch(). If the local DSQ is empty even after ops.dispatch() 194 * produced some tasks, retry. The BPF scheduler may depend on this 195 * looping behavior to simplify its implementation. 196 */ 197 do { 198 dspc->nr_tasks = 0; 199 200 if (nested) { 201 SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 202 prev_on_sch ? prev : NULL); 203 } else { 204 /* stash @prev so that nested invocations can access it */ 205 rq->scx.sub_dispatch_prev = prev; 206 SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 207 prev_on_sch ? prev : NULL); 208 rq->scx.sub_dispatch_prev = NULL; 209 } 210 211 scx_flush_dispatch_buf(sch, rq); 212 213 if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice) { 214 rq->scx.flags |= SCX_RQ_BAL_KEEP; 215 return true; 216 } 217 if (rq->scx.local_dsq.nr) 218 return true; 219 if (scx_consume_global_dsq(sch, rq)) 220 return true; 221 222 /* 223 * ops.dispatch() can trap us in this loop by repeatedly 224 * dispatching ineligible tasks. Break out once in a while to 225 * allow the watchdog to run. As IRQ can't be enabled in 226 * balance(), we want to complete this scheduling cycle and then 227 * start a new one. IOW, we want to call resched_curr() on the 228 * next, most likely idle, task, not the current one. Use 229 * __scx_bpf_kick_cpu() for deferred kicking. 230 */ 231 if (unlikely(!--nr_loops)) { 232 scx_kick_cpu(sch, cpu, 0); 233 break; 234 } 235 } while (dspc->nr_tasks); 236 237 /* 238 * Prevent the CPU from going idle while bypassed descendants have tasks 239 * queued. Without this fallback, bypassed tasks could stall if the host 240 * scheduler's ops.dispatch() doesn't yield any tasks. 241 */ 242 if (scx_bypass_dsp_enabled(sch)) 243 return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 244 245 return false; 246 } 247 248 #endif /* _KERNEL_SCHED_EXT_SUB_H */ 249