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