xref: /linux/kernel/sched/ext/sub.h (revision 947dd3eb80df8e83683182271d2782867befd333)
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 
15 #ifdef CONFIG_EXT_SUB_SCHED
16 
17 extern s32 scx_rescue_bw_1024;
18 
19 struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root);
20 struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root);
21 void scx_cid_sched_update(struct rq *rq, struct scx_sched *sch);
22 void scx_ops_cid_sched_updated_enable(struct scx_sched *sch);
23 void scx_ops_cid_sched_updated_disable(struct scx_sched *sch);
24 void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch);
25 struct cgroup *sch_cgroup(struct scx_sched *sch);
26 void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch);
27 void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog);
28 void scx_pstack_recursion_on_caps_updated(struct bpf_prog *prog);
29 void drain_descendants(struct scx_sched *sch);
30 void scx_sub_disable(struct scx_sched *sch);
31 void scx_sub_enable_workfn(struct kthread_work *work);
32 bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux);
33 void scx_free_pshards(struct scx_sched *sch);
34 s32 scx_alloc_pshards(struct scx_sched *sch);
35 void scx_init_root_caps(struct scx_sched *sch);
36 void __scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev);
37 void scx_sub_pm_begin(void);
38 void scx_update_frozen_cpus(s32 cpu, bool online);
39 void scx_sub_pm_end(void);
40 void scx_init_rq_ops_online(void);
41 void scx_online_ecaps(struct rq *rq);
42 void scx_offline_ecaps(struct rq *rq);
43 void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu);
44 void scx_discard_stale_ecaps_syncs(void);
45 struct scx_dispatch_q *__scx_resolve_local_dsq(struct scx_sched *sch, struct rq *rq,
46 					       struct task_struct *p, u64 *enq_flags);
47 bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p);
48 void scx_rescue_charge(struct rq *rq, s64 delta_exec);
49 void scx_rescue_end(struct rq *rq);
50 bool scx_rescue_keep(struct rq *rq, struct task_struct *p);
51 void scx_rescue_flush(struct rq *rq);
52 void scx_rescue_dump(struct seq_buf *s, struct rq *rq);
53 void scx_rescue_set_knobs(struct scx_sched *sch);
54 void scx_rescue_init(struct rq *rq);
55 
56 /* false only when the root disabled rescue, see scx_rescue_set_knobs() */
57 static inline bool scx_rescue_enabled(void)
58 {
59 	return likely(scx_rescue_bw_1024);
60 }
61 
62 /*
63  * cgrp->scx_sched is written by root/sub enable/disable under all of
64  * scx_enable_mutex, scx_fork_rwsem and cgroup_mutex. A new cgroup inherits the
65  * parent's sched under just cgroup_mutex but is not yet reachable by the other
66  * two lock holders. Any one of the three locks stabilizes the association.
67  */
68 static inline struct scx_sched *scx_cgroup_sched(struct cgroup *cgrp)
69 {
70 	return rcu_dereference_check(cgrp->scx_sched,
71 				     lockdep_is_held(&cgroup_mutex) ||
72 				     percpu_rwsem_is_held(&scx_fork_rwsem) ||
73 				     lockdep_is_held(&scx_enable_mutex));
74 }
75 
76 static inline const char *sch_cgrp_path(struct scx_sched *sch)
77 {
78 	return sch->cgrp_path;
79 }
80 
81 /* a dying sub's hot-path influence ends in scx_sched_free_rcu_work() */
82 static inline void scx_dec_has_subs(struct scx_sched *sch)
83 {
84 	if (sch->level)
85 		static_branch_dec(&__scx_has_subs);
86 }
87 
88 /* hot-path hooks, gated inline so that a root-only system skips the calls */
89 static inline void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev)
90 {
91 	lockdep_assert_rq_held(rq);
92 
93 	if (scx_has_subs())
94 		__scx_process_sync_ecaps(rq, prev);
95 }
96 
97 static inline struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch,
98 							   struct rq *rq,
99 							   struct task_struct *p,
100 							   u64 *enq_flags)
101 {
102 	if (!scx_has_subs())
103 		return &rq->scx.local_dsq;
104 	return __scx_resolve_local_dsq(sch, rq, p, enq_flags);
105 }
106 
107 #else	/* CONFIG_EXT_SUB_SCHED */
108 
109 static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; }
110 static inline struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root) { return NULL; }
111 static inline void scx_cid_sched_update(struct rq *rq, struct scx_sched *sch) {}
112 static inline void scx_ops_cid_sched_updated_enable(struct scx_sched *sch) {}
113 static inline void scx_ops_cid_sched_updated_disable(struct scx_sched *sch) {}
114 static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {}
115 static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; }
116 static inline const char *sch_cgrp_path(struct scx_sched *sch) { return "/"; }
117 static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {}
118 static inline void drain_descendants(struct scx_sched *sch) { }
119 static inline void scx_sub_disable(struct scx_sched *sch) { }
120 static inline void scx_free_pshards(struct scx_sched *sch) {}
121 static inline s32 scx_alloc_pshards(struct scx_sched *sch) { return 0; }
122 static inline void scx_init_root_caps(struct scx_sched *sch) {}
123 static inline void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev) {}
124 static inline void scx_sub_pm_begin(void) {}
125 static inline void scx_update_frozen_cpus(s32 cpu, bool online) {}
126 static inline void scx_sub_pm_end(void) {}
127 static inline void scx_init_rq_ops_online(void) {}
128 static inline void scx_online_ecaps(struct rq *rq) {}
129 static inline void scx_offline_ecaps(struct rq *rq) {}
130 static inline void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu) {}
131 static inline void scx_discard_stale_ecaps_syncs(void) {}
132 static inline struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *rq, struct task_struct *p, u64 *enq_flags) { return &rq->scx.local_dsq; }
133 static inline bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p) { return false; }
134 static inline void scx_rescue_charge(struct rq *rq, s64 delta_exec) {}
135 static inline void scx_rescue_end(struct rq *rq) {}
136 static inline bool scx_rescue_keep(struct rq *rq, struct task_struct *p) { return false; }
137 static inline void scx_rescue_flush(struct rq *rq) {}
138 static inline void scx_rescue_dump(struct seq_buf *s, struct rq *rq) {}
139 static inline bool scx_rescue_enabled(void) { return false; }
140 static inline void scx_rescue_set_knobs(struct scx_sched *sch) {}
141 static inline void scx_rescue_init(struct rq *rq) {}
142 static inline void scx_dec_has_subs(struct scx_sched *sch) {}
143 
144 #endif	/* CONFIG_EXT_SUB_SCHED */
145 
146 /**
147  * scx_for_each_descendant_pre - pre-order walk of a sched's descendants
148  * @pos: iteration cursor
149  * @root: sched to walk the descendants of
150  *
151  * Walk @root's descendants. @root is included in the iteration and the first
152  * node to be visited. Must be called with scx_enable_mutex, scx_sched_lock, or
153  * RCU read lock.
154  */
155 #define scx_for_each_descendant_pre(pos, root)					\
156 	for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos);		\
157 	     (pos) = scx_next_descendant_pre((pos), (root)))
158 
159 #ifdef CONFIG_EXT_SUB_SCHED
160 
161 /**
162  * scx_missing_caps - The caps in @needed that @sch lacks on @cpu
163  * @sch: sched to test
164  * @cpu: cpu to test on
165  * @needed: bitmask of SCX_CAP_* values
166  *
167  * Return the caps in @needed that @sch lacks for @cpu, 0 if it holds them all.
168  */
169 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed)
170 {
171 	u64 ecaps;
172 
173 	/* no sub-scheds, no missing caps */
174 	if (!scx_has_subs())
175 		return 0;
176 
177 	/* root holds every cap on every cpu */
178 	if (!sch->level)
179 		return 0;
180 
181 	ecaps = READ_ONCE(per_cpu_ptr(sch->pcpu, cpu)->ecaps);
182 
183 	return needed & ~ecaps;
184 }
185 
186 /*
187  * Cap semantics: which caps an action requires, and which caps a cap implies.
188  * Keep all such mappings collected here.
189  */
190 
191 /* map @enq_flags to the SCX_CAP_* bit required for the local-DSQ insert */
192 static inline u64 scx_caps_for_enq(u64 enq_flags)
193 {
194 	/* a restored task must be put into the local DSQ regardless of caps */
195 	if (unlikely(enq_flags & SCX_ENQ_IGNORE_CAPS))
196 		return 0;
197 	if (enq_flags & SCX_ENQ_IMMED)
198 		return SCX_CAP_ENQ_IMMED;
199 	return SCX_CAP_ENQ;
200 }
201 
202 /* map queued @p to the SCX_CAP_* bit required to stay on its local DSQ */
203 static inline u64 scx_caps_for_task(struct task_struct *p)
204 {
205 	if (p->scx.flags & SCX_TASK_IMMED)
206 		return SCX_CAP_ENQ_IMMED;
207 	return SCX_CAP_ENQ;
208 }
209 
210 /* the cap @sch needs to preempt @rq's current scheduling context, 0 if none */
211 static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq,
212 				       u64 enq_flags)
213 {
214 	struct task_struct *donor = rq->donor;
215 
216 	/* a kernel-forced placement preempts regardless of caps */
217 	if (unlikely(enq_flags & SCX_ENQ_IGNORE_CAPS))
218 		return 0;
219 	/* a non-ext task can't be preempted by ext, own-subtree needs no cap */
220 	if (donor->sched_class != &ext_sched_class ||
221 	    scx_is_descendant(scx_task_sched(donor), sch))
222 		return 0;
223 	return SCX_CAP_PREEMPT;
224 }
225 
226 /* caps implied by holding @cap */
227 static inline u64 scx_caps_implied(u64 cap)
228 {
229 	switch (cap) {
230 	case SCX_CAP_PREEMPT:
231 		return SCX_CAP_ENQ | SCX_CAP_ENQ_IMMED;
232 	case SCX_CAP_ENQ:
233 		return SCX_CAP_ENQ_IMMED;
234 	}
235 	return 0;
236 }
237 
238 /* may @p keep running on @rq's cpu? requires baseline cpu access */
239 static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p)
240 {
241 	s32 cpu = cpu_of(rq);
242 
243 	if (!scx_has_subs())
244 		return true;
245 
246 	/* a migration-disabled task is let in without caps, keep it likewise */
247 	if (unlikely(is_migration_disabled(p)))
248 		return true;
249 
250 	/* check caps against the sched running a bypassed task */
251 	return likely(!scx_missing_caps(scx_exec_sched(scx_task_sched(p), cpu), cpu,
252 					SCX_CAP_BASE));
253 }
254 
255 /* the task admitted for rescue on @rq, NULL if none */
256 static inline struct task_struct *scx_rescuee(struct rq *rq)
257 {
258 	lockdep_assert_rq_held(rq);
259 
260 	if (!scx_has_subs())
261 		return NULL;
262 
263 	return rq->scx.rescue.curr;
264 }
265 
266 /* does the kernel own @p's slice? see %SCX_TASK_KERNEL_SLICE */
267 static inline bool scx_task_has_kernel_slice(const struct task_struct *p)
268 {
269 	return scx_has_subs() && (p->scx.flags & SCX_TASK_KERNEL_SLICE);
270 }
271 
272 #else	/* CONFIG_EXT_SUB_SCHED */
273 
274 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) { return 0; }
275 static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq, u64 enq_flags) { return 0; }
276 static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p) { return true; }
277 static inline struct task_struct *scx_rescuee(struct rq *rq) { return NULL; }
278 static inline bool scx_task_has_kernel_slice(const struct task_struct *p) { return false; }
279 
280 #endif	/* CONFIG_EXT_SUB_SCHED */
281 
282 #endif /* _KERNEL_SCHED_EXT_SUB_H */
283