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