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