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