xref: /linux/tools/sched_ext/include/scx/compat.bpf.h (revision bf1079577a116f0685e7025b9ee2547345ee1c63)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
4  * Copyright (c) 2024 Tejun Heo <tj@kernel.org>
5  * Copyright (c) 2024 David Vernet <dvernet@meta.com>
6  */
7 #ifndef __SCX_COMPAT_BPF_H
8 #define __SCX_COMPAT_BPF_H
9 
10 #define __COMPAT_ENUM_OR_ZERO(__type, __ent)					\
11 ({										\
12 	__type __ret = 0;							\
13 	if (bpf_core_enum_value_exists(__type, __ent))				\
14 		__ret = __ent;							\
15 	__ret;									\
16 })
17 
18 /* v6.12: 819513666966 ("sched_ext: Add cgroup support") */
19 struct cgroup *scx_bpf_task_cgroup___new(struct task_struct *p) __ksym __weak;
20 
21 #define scx_bpf_task_cgroup(p)							\
22 	(bpf_ksym_exists(scx_bpf_task_cgroup___new) ?				\
23 	 scx_bpf_task_cgroup___new((p)) : NULL)
24 
25 /*
26  * v6.13: The verb `dispatch` was too overloaded and confusing. kfuncs are
27  * renamed to unload the verb.
28  *
29  * scx_bpf_dispatch_from_dsq() and friends were added during v6.12 by
30  * 4c30f5ce4f7a ("sched_ext: Implement scx_bpf_dispatch[_vtime]_from_dsq()").
31  *
32  * v7.1: scx_bpf_dsq_move_to_local___v2() to add @enq_flags.
33  */
34 bool scx_bpf_dsq_move_to_local___v2___compat(u64 dsq_id, u64 enq_flags) __ksym __weak;
35 bool scx_bpf_dsq_move_to_local___v1(u64 dsq_id) __ksym __weak;
36 void scx_bpf_dsq_move_set_slice___new(struct bpf_iter_scx_dsq *it__iter, u64 slice) __ksym __weak;
37 void scx_bpf_dsq_move_set_vtime___new(struct bpf_iter_scx_dsq *it__iter, u64 vtime) __ksym __weak;
38 bool scx_bpf_dsq_move___new(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;
39 bool scx_bpf_dsq_move_vtime___new(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;
40 
41 bool scx_bpf_consume___old(u64 dsq_id) __ksym __weak;
42 void scx_bpf_dispatch_from_dsq_set_slice___old(struct bpf_iter_scx_dsq *it__iter, u64 slice) __ksym __weak;
43 void scx_bpf_dispatch_from_dsq_set_vtime___old(struct bpf_iter_scx_dsq *it__iter, u64 vtime) __ksym __weak;
44 bool scx_bpf_dispatch_from_dsq___old(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;
45 bool scx_bpf_dispatch_vtime_from_dsq___old(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;
46 
47 #define scx_bpf_dsq_move_to_local(dsq_id, enq_flags)				\
48 	(bpf_ksym_exists(scx_bpf_dsq_move_to_local___v2___compat) ?		\
49 	 scx_bpf_dsq_move_to_local___v2___compat((dsq_id), (enq_flags)) :	\
50 	 (bpf_ksym_exists(scx_bpf_dsq_move_to_local___v1) ?			\
51 	  scx_bpf_dsq_move_to_local___v1((dsq_id)) :				\
52 	  scx_bpf_consume___old((dsq_id))))
53 
54 #define scx_bpf_dsq_move_set_slice(it__iter, slice)				\
55 	(bpf_ksym_exists(scx_bpf_dsq_move_set_slice___new) ?			\
56 	 scx_bpf_dsq_move_set_slice___new((it__iter), (slice)) :		\
57 	 (bpf_ksym_exists(scx_bpf_dispatch_from_dsq_set_slice___old) ?		\
58 	  scx_bpf_dispatch_from_dsq_set_slice___old((it__iter), (slice)) :	\
59 	  (void)0))
60 
61 #define scx_bpf_dsq_move_set_vtime(it__iter, vtime)				\
62 	(bpf_ksym_exists(scx_bpf_dsq_move_set_vtime___new) ?			\
63 	 scx_bpf_dsq_move_set_vtime___new((it__iter), (vtime)) :		\
64 	 (bpf_ksym_exists(scx_bpf_dispatch_from_dsq_set_vtime___old) ?		\
65 	  scx_bpf_dispatch_from_dsq_set_vtime___old((it__iter), (vtime)) :	\
66 	  (void)0))
67 
68 #define scx_bpf_dsq_move(it__iter, p, dsq_id, enq_flags)			\
69 	(bpf_ksym_exists(scx_bpf_dsq_move___new) ?				\
70 	 scx_bpf_dsq_move___new((it__iter), (p), (dsq_id), (enq_flags)) :	\
71 	 (bpf_ksym_exists(scx_bpf_dispatch_from_dsq___old) ?			\
72 	  scx_bpf_dispatch_from_dsq___old((it__iter), (p), (dsq_id), (enq_flags)) : \
73 	  false))
74 
75 #define scx_bpf_dsq_move_vtime(it__iter, p, dsq_id, enq_flags)			\
76 	(bpf_ksym_exists(scx_bpf_dsq_move_vtime___new) ?			\
77 	 scx_bpf_dsq_move_vtime___new((it__iter), (p), (dsq_id), (enq_flags)) : \
78 	 (bpf_ksym_exists(scx_bpf_dispatch_vtime_from_dsq___old) ?		\
79 	  scx_bpf_dispatch_vtime_from_dsq___old((it__iter), (p), (dsq_id), (enq_flags)) : \
80 	  false))
81 
82 /*
83  * v6.15: 950ad93df2fc ("bpf: add kfunc for populating cpumask bits")
84  *
85  * Compat macro will be dropped on v6.19 release.
86  */
87 int bpf_cpumask_populate(struct bpf_cpumask *dst, void *src, size_t src__sz) __ksym __weak;
88 
89 #define __COMPAT_bpf_cpumask_populate(cpumask, src, size__sz)		\
90 	(bpf_ksym_exists(bpf_cpumask_populate) ?			\
91 	 (bpf_cpumask_populate(cpumask, src, size__sz)) : -EOPNOTSUPP)
92 
93 /*
94  * v6.19: Introduce lockless peek API for user DSQs.
95  * v7.1:  Fix scx_bpf_dsq_peek() spuriously returning NULL on non-empty
96  *        FIFO DSQs (2f2ea7709266).
97  *
98  * The kfunc exists from v6.19 but can return NULL for a non-empty FIFO DSQ
99  * before the v7.1 fix. Require kernel version >= 7.1.0 before calling it;
100  * otherwise fall through to the bpf_iter_scx_dsq fallback below.
101  */
__COMPAT_scx_bpf_dsq_peek(u64 dsq_id)102 static inline struct task_struct *__COMPAT_scx_bpf_dsq_peek(u64 dsq_id)
103 {
104 	struct task_struct *p = NULL;
105 	struct bpf_iter_scx_dsq it;
106 
107 	if (bpf_ksym_exists(scx_bpf_dsq_peek) &&
108 	    LINUX_KERNEL_VERSION >= KERNEL_VERSION(7, 1, 0))
109 		return scx_bpf_dsq_peek(dsq_id);
110 	if (!bpf_iter_scx_dsq_new(&it, dsq_id, 0))
111 		p = bpf_iter_scx_dsq_next(&it);
112 	bpf_iter_scx_dsq_destroy(&it);
113 	return p;
114 }
115 
116 /*
117  * v7.1: scx_bpf_sub_dispatch() for sub-sched dispatch. Preserve until
118  * we drop the compat layer for older kernels that lack the kfunc.
119  */
120 bool scx_bpf_sub_dispatch___compat(u64 cgroup_id) __ksym __weak;
121 
scx_bpf_sub_dispatch(u64 cgroup_id)122 static inline bool scx_bpf_sub_dispatch(u64 cgroup_id)
123 {
124 	if (bpf_ksym_exists(scx_bpf_sub_dispatch___compat))
125 		return scx_bpf_sub_dispatch___compat(cgroup_id);
126 	return false;
127 }
128 
129 /*
130  * v7.3: scx_bpf_cid_override() for explicit cid and shard mapping. Ignore if
131  * missing.
132  */
133 void scx_bpf_cid_override___compat(const s32 __arena *cpu_to_cid__arena,
134 				   u32 cpu_to_cid_cnt,
135 				   const s32 __arena *shard_start__arena,
136 				   u32 shard_start_cnt) __ksym __weak;
137 
scx_bpf_cid_override(const s32 __arena * cpu_to_cid,u32 cpu_to_cid_cnt,const s32 __arena * shard_start,u32 shard_start_cnt)138 static inline void scx_bpf_cid_override(const s32 __arena *cpu_to_cid, u32 cpu_to_cid_cnt,
139 					const s32 __arena *shard_start, u32 shard_start_cnt)
140 {
141 	if (bpf_ksym_exists(scx_bpf_cid_override___compat))
142 		scx_bpf_cid_override___compat(cpu_to_cid, cpu_to_cid_cnt,
143 					      shard_start, shard_start_cnt);
144 }
145 
146 /**
147  * __COMPAT_is_enq_cpu_selected - Test if SCX_ENQ_CPU_SELECTED is on
148  * in a compatible way. We will preserve this __COMPAT helper until v6.16.
149  *
150  * @enq_flags: enqueue flags from ops.enqueue()
151  *
152  * Return: True if SCX_ENQ_CPU_SELECTED is turned on in @enq_flags
153  */
__COMPAT_is_enq_cpu_selected(u64 enq_flags)154 static inline bool __COMPAT_is_enq_cpu_selected(u64 enq_flags)
155 {
156 #ifdef HAVE_SCX_ENQ_CPU_SELECTED
157 	/*
158 	 * This is the case that a BPF code compiled against vmlinux.h
159 	 * where the enum SCX_ENQ_CPU_SELECTED exists.
160 	 */
161 
162 	/*
163 	 * We should temporarily suspend the macro expansion of
164 	 * 'SCX_ENQ_CPU_SELECTED'. This avoids 'SCX_ENQ_CPU_SELECTED' being
165 	 * rewritten to '__SCX_ENQ_CPU_SELECTED' when 'SCX_ENQ_CPU_SELECTED'
166 	 * is defined in 'scripts/gen_enums.py'.
167 	 */
168 #pragma push_macro("SCX_ENQ_CPU_SELECTED")
169 #undef SCX_ENQ_CPU_SELECTED
170 	u64 flag;
171 
172 	/*
173 	 * When the kernel did not have SCX_ENQ_CPU_SELECTED,
174 	 * select_task_rq_scx() has never been skipped. Thus, this case
175 	 * should be considered that the CPU has already been selected.
176 	 */
177 	if (!bpf_core_enum_value_exists(enum scx_enq_flags,
178 					SCX_ENQ_CPU_SELECTED))
179 		return true;
180 
181 	flag = bpf_core_enum_value(enum scx_enq_flags, SCX_ENQ_CPU_SELECTED);
182 	return enq_flags & flag;
183 
184 	/*
185 	 * Once done, resume the macro expansion of 'SCX_ENQ_CPU_SELECTED'.
186 	 */
187 #pragma pop_macro("SCX_ENQ_CPU_SELECTED")
188 #else
189 	/*
190 	 * This is the case that a BPF code compiled against vmlinux.h
191 	 * where the enum SCX_ENQ_CPU_SELECTED does NOT exist.
192 	 */
193 	return true;
194 #endif /* HAVE_SCX_ENQ_CPU_SELECTED */
195 }
196 
197 
198 #define scx_bpf_now()								\
199 	(bpf_ksym_exists(scx_bpf_now) ?						\
200 	 scx_bpf_now() :							\
201 	 bpf_ktime_get_ns())
202 
203 /*
204  * v6.15: Introduce event counters.
205  *
206  * Preserve the following macro until v6.17.
207  */
208 #define __COMPAT_scx_bpf_events(events, size)					\
209 	(bpf_ksym_exists(scx_bpf_events) ?					\
210 	 scx_bpf_events(events, size) : ({}))
211 
212 /*
213  * v6.15: Introduce NUMA-aware kfuncs to operate with per-node idle
214  * cpumasks.
215  *
216  * Preserve the following __COMPAT_scx_*_node macros until v6.17.
217  */
218 #define __COMPAT_scx_bpf_nr_node_ids()						\
219 	(bpf_ksym_exists(scx_bpf_nr_node_ids) ?					\
220 	 scx_bpf_nr_node_ids() : 1U)
221 
222 #define __COMPAT_scx_bpf_cpu_node(cpu)						\
223 	(bpf_ksym_exists(scx_bpf_cpu_node) ?					\
224 	 scx_bpf_cpu_node(cpu) : 0)
225 
226 #define __COMPAT_scx_bpf_get_idle_cpumask_node(node)				\
227 	(bpf_ksym_exists(scx_bpf_get_idle_cpumask_node) ?			\
228 	 scx_bpf_get_idle_cpumask_node(node) :					\
229 	 scx_bpf_get_idle_cpumask())						\
230 
231 #define __COMPAT_scx_bpf_get_idle_smtmask_node(node)				\
232 	(bpf_ksym_exists(scx_bpf_get_idle_smtmask_node) ?			\
233 	 scx_bpf_get_idle_smtmask_node(node) :					\
234 	 scx_bpf_get_idle_smtmask())
235 
236 #define __COMPAT_scx_bpf_pick_idle_cpu_node(cpus_allowed, node, flags)		\
237 	(bpf_ksym_exists(scx_bpf_pick_idle_cpu_node) ?				\
238 	 scx_bpf_pick_idle_cpu_node(cpus_allowed, node, flags) :		\
239 	 scx_bpf_pick_idle_cpu(cpus_allowed, flags))
240 
241 #define __COMPAT_scx_bpf_pick_any_cpu_node(cpus_allowed, node, flags)		\
242 	(bpf_ksym_exists(scx_bpf_pick_any_cpu_node) ?				\
243 	 scx_bpf_pick_any_cpu_node(cpus_allowed, node, flags) :			\
244 	 scx_bpf_pick_any_cpu(cpus_allowed, flags))
245 
246 /*
247  * v6.18: Add a helper to retrieve the current task running on a CPU.
248  *
249  * The kernel tree dropped this helper and scx_bpf_cpu_rq(), but schedulers in
250  * this tree still support pre-v6.18 kernels where scx_bpf_cpu_curr() doesn't
251  * resolve and the scx_bpf_cpu_rq() fallback still exists. Keep it until
252  * pre-v6.18 kernels fall out of the support window.
253  */
__COMPAT_scx_bpf_cpu_curr(int cpu)254 static inline struct task_struct *__COMPAT_scx_bpf_cpu_curr(int cpu)
255 {
256 	struct rq *rq;
257 
258 	if (bpf_ksym_exists(scx_bpf_cpu_curr))
259 		return scx_bpf_cpu_curr(cpu);
260 
261 	rq = scx_bpf_cpu_rq(cpu);
262 
263 	return rq ? rq->curr : NULL;
264 }
265 
266 /*
267  * v6.19: To work around BPF maximum parameter limit, the following kfuncs are
268  * replaced with variants that pack scalar arguments in a struct. Wrappers are
269  * provided to maintain source compatibility.
270  *
271  * v6.13: scx_bpf_dsq_insert_vtime() renaming is also handled here. See the
272  * block on dispatch renaming above for more details.
273  *
274  * The kernel will carry the compat variants until v6.23 to maintain binary
275  * compatibility. After v6.23 release, remove the compat handling and move the
276  * wrappers to common.bpf.h.
277  */
278 s32 scx_bpf_select_cpu_and___compat(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
279 				    const struct cpumask *cpus_allowed, u64 flags) __ksym __weak;
280 void scx_bpf_dispatch_vtime___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak;
281 void scx_bpf_dsq_insert_vtime___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak;
282 
283 /**
284  * scx_bpf_select_cpu_and - Pick an idle CPU usable by task @p
285  * @p: task_struct to select a CPU for
286  * @prev_cpu: CPU @p was on previously
287  * @wake_flags: %SCX_WAKE_* flags
288  * @cpus_allowed: cpumask of allowed CPUs
289  * @flags: %SCX_PICK_IDLE* flags
290  *
291  * Inline wrapper that packs scalar arguments into a struct and calls
292  * __scx_bpf_select_cpu_and(). See __scx_bpf_select_cpu_and() for details.
293  */
294 static inline s32
scx_bpf_select_cpu_and(struct task_struct * p,s32 prev_cpu,u64 wake_flags,const struct cpumask * cpus_allowed,u64 flags)295 scx_bpf_select_cpu_and(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
296 		       const struct cpumask *cpus_allowed, u64 flags)
297 {
298 	if (bpf_core_type_exists(struct scx_bpf_select_cpu_and_args)) {
299 		struct scx_bpf_select_cpu_and_args args = {
300 			.prev_cpu = prev_cpu,
301 			.wake_flags = wake_flags,
302 			.flags = flags,
303 		};
304 
305 		return __scx_bpf_select_cpu_and(p, cpus_allowed, &args);
306 	} else {
307 		return scx_bpf_select_cpu_and___compat(p, prev_cpu, wake_flags,
308 						       cpus_allowed, flags);
309 	}
310 }
311 
312 /*
313  * scx_bpf_select_cpu_and() is now an inline wrapper. Use this instead of
314  * bpf_ksym_exists(scx_bpf_select_cpu_and) to test availability.
315  */
316 #define __COMPAT_HAS_scx_bpf_select_cpu_and				\
317 	(bpf_core_type_exists(struct scx_bpf_select_cpu_and_args) ||	\
318 	 bpf_ksym_exists(scx_bpf_select_cpu_and___compat))
319 
320 /**
321  * scx_bpf_dsq_insert_vtime - Insert a task into the vtime priority queue of a DSQ
322  * @p: task_struct to insert
323  * @dsq_id: DSQ to insert into
324  * @slice: duration @p can run for in nsecs, 0 to keep the current value
325  * @vtime: @p's ordering inside the vtime-sorted queue of the target DSQ
326  * @enq_flags: SCX_ENQ_*
327  *
328  * Inline wrapper that packs scalar arguments into a struct and calls
329  * __scx_bpf_dsq_insert_vtime(). See __scx_bpf_dsq_insert_vtime() for details.
330  */
331 static inline bool
scx_bpf_dsq_insert_vtime(struct task_struct * p,u64 dsq_id,u64 slice,u64 vtime,u64 enq_flags)332 scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime,
333 			 u64 enq_flags)
334 {
335 	if (bpf_core_type_exists(struct scx_bpf_dsq_insert_vtime_args)) {
336 		struct scx_bpf_dsq_insert_vtime_args args = {
337 			.dsq_id = dsq_id,
338 			.slice = slice,
339 			.vtime = vtime,
340 			.enq_flags = enq_flags,
341 		};
342 
343 		return __scx_bpf_dsq_insert_vtime(p, &args);
344 	} else if (bpf_ksym_exists(scx_bpf_dsq_insert_vtime___compat)) {
345 		scx_bpf_dsq_insert_vtime___compat(p, dsq_id, slice, vtime,
346 						  enq_flags);
347 		return true;
348 	} else {
349 		scx_bpf_dispatch_vtime___compat(p, dsq_id, slice, vtime,
350 						enq_flags);
351 		return true;
352 	}
353 }
354 
355 /*
356  * v6.19: scx_bpf_dsq_insert() now returns bool instead of void. Move
357  * scx_bpf_dsq_insert() decl to common.bpf.h and drop compat helper after v6.22.
358  * The extra ___compat suffix is to work around libbpf not ignoring __SUFFIX on
359  * kernel side. The entire suffix can be dropped later.
360  *
361  * v6.13: scx_bpf_dsq_insert() renaming is also handled here. See the block on
362  * dispatch renaming above for more details.
363  */
364 bool scx_bpf_dsq_insert___v2___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak;
365 void scx_bpf_dsq_insert___v1(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak;
366 void scx_bpf_dispatch___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak;
367 
368 static inline bool
scx_bpf_dsq_insert(struct task_struct * p,u64 dsq_id,u64 slice,u64 enq_flags)369 scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags)
370 {
371 	if (bpf_ksym_exists(scx_bpf_dsq_insert___v2___compat)) {
372 		return scx_bpf_dsq_insert___v2___compat(p, dsq_id, slice, enq_flags);
373 	} else if (bpf_ksym_exists(scx_bpf_dsq_insert___v1)) {
374 		scx_bpf_dsq_insert___v1(p, dsq_id, slice, enq_flags);
375 		return true;
376 	} else {
377 		scx_bpf_dispatch___compat(p, dsq_id, slice, enq_flags);
378 		return true;
379 	}
380 }
381 
382 /*
383  * v6.19: scx_bpf_task_set_slice() and scx_bpf_task_set_dsq_vtime() added to for
384  * sub-sched authority checks. Drop the wrappers and move the decls to
385  * common.bpf.h after v6.22.
386  */
387 bool scx_bpf_task_set_slice___new(struct task_struct *p, u64 slice) __ksym __weak;
388 bool scx_bpf_task_set_dsq_vtime___new(struct task_struct *p, u64 vtime) __ksym __weak;
389 
scx_bpf_task_set_slice(struct task_struct * p,u64 slice)390 static inline void scx_bpf_task_set_slice(struct task_struct *p, u64 slice)
391 {
392 	if (bpf_ksym_exists(scx_bpf_task_set_slice___new))
393 		scx_bpf_task_set_slice___new(p, slice);
394 	else
395 		p->scx.slice = slice;
396 }
397 
scx_bpf_task_set_dsq_vtime(struct task_struct * p,u64 vtime)398 static inline void scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime)
399 {
400 	if (bpf_ksym_exists(scx_bpf_task_set_dsq_vtime___new))
401 		scx_bpf_task_set_dsq_vtime___new(p, vtime);
402 	else
403 		p->scx.dsq_vtime = vtime;
404 }
405 
406 /*
407  * v7.1: New scx_bpf_dsq_reenq() that allows re-enqueues on more DSQs. This
408  * will eventually deprecate scx_bpf_reenqueue_local().
409  */
410 void scx_bpf_dsq_reenq___compat(u64 dsq_id, u64 reenq_flags) __ksym __weak;
411 
__COMPAT_has_generic_reenq(void)412 static inline bool __COMPAT_has_generic_reenq(void)
413 {
414 	return bpf_ksym_exists(scx_bpf_dsq_reenq___compat);
415 }
416 
417 /*
418  * v6.19: The new void variant can be called from anywhere while the older v1
419  * variant can only be called from ops.cpu_release(). The double ___ prefixes on
420  * the v2 variant need to be removed once libbpf is updated to ignore ___ prefix
421  * on kernel side. Drop the wrapper and move the decl to common.bpf.h after
422  * v6.22.
423  */
424 u32 scx_bpf_reenqueue_local___v1(void) __ksym __weak;
425 void scx_bpf_reenqueue_local___v2___compat(void) __ksym __weak;
426 
__COMPAT_scx_bpf_reenqueue_local_from_anywhere(void)427 static inline bool __COMPAT_scx_bpf_reenqueue_local_from_anywhere(void)
428 {
429 	return bpf_ksym_exists(scx_bpf_reenqueue_local___v2___compat);
430 }
431 
scx_bpf_reenqueue_local(void)432 static inline void scx_bpf_reenqueue_local(void)
433 {
434 	if (__COMPAT_has_generic_reenq())
435 		scx_bpf_dsq_reenq___compat(SCX_DSQ_LOCAL, 0);
436 	else if (__COMPAT_scx_bpf_reenqueue_local_from_anywhere())
437 		scx_bpf_reenqueue_local___v2___compat();
438 	else
439 		scx_bpf_reenqueue_local___v1();
440 }
441 
scx_bpf_reenqueue_local_from_anywhere(void)442 static inline int scx_bpf_reenqueue_local_from_anywhere(void)
443 {
444 	/*
445 	 * The generic reenq kfunc and the v2 reenqueue-local variant can both be
446 	 * called from anywhere; v1 cannot. Test each ksym in its own branch with a
447 	 * distinct call: combining them with || would fold into a bitwise OR of the
448 	 * two ksym addresses, which the verifier rejects.
449 	 */
450 	if (__COMPAT_has_generic_reenq()) {
451 		scx_bpf_dsq_reenq___compat(SCX_DSQ_LOCAL, 0);
452 		return 0;
453 	}
454 	if (__COMPAT_scx_bpf_reenqueue_local_from_anywhere()) {
455 		scx_bpf_reenqueue_local___v2___compat();
456 		return 0;
457 	}
458 	return -EOPNOTSUPP;
459 }
460 
scx_bpf_dsq_reenq(u64 dsq_id,u64 reenq_flags)461 static inline void scx_bpf_dsq_reenq(u64 dsq_id, u64 reenq_flags)
462 {
463 	if (bpf_ksym_exists(scx_bpf_dsq_reenq___compat))
464 		scx_bpf_dsq_reenq___compat(dsq_id, reenq_flags);
465 	else if (dsq_id == SCX_DSQ_LOCAL && reenq_flags == 0)
466 		scx_bpf_reenqueue_local();
467 	else
468 		scx_bpf_error("kernel too old to reenqueue foreign local or user DSQs");
469 }
470 
471 /*
472  * Define sched_ext_ops. See compat.h::SCX_OPS_OPEN() for how backward
473  * compatibility is handled (this macro can be expanded to emit multiple
474  * variants for incompatible op changes; SCX_OPS_OPEN() handles purely
475  * additive changes at load time).
476  */
477 #define SCX_OPS_DEFINE(__name, ...)						\
478 	SEC(".struct_ops.link")							\
479 	struct sched_ext_ops __name = {						\
480 		__VA_ARGS__,							\
481 	};
482 
483 /*
484  * Define a cid-form sched_ext_ops. Programs targeting this struct_ops type
485  * use cid-form callback signatures (select_cid, set_cmask, cid_online/offline,
486  * dispatch with cid arg, etc.) and may only call the cid-form scx_bpf_*
487  * kfuncs (kick_cid, task_cid, this_cid, ...).
488  */
489 #define SCX_OPS_CID_DEFINE(__name, ...)						\
490 	SEC(".struct_ops.link")							\
491 	struct sched_ext_ops_cid __name = {					\
492 		__VA_ARGS__,							\
493 	};
494 
495 #endif	/* __SCX_COMPAT_BPF_H */
496