sched_ext/tools: Add compat layer for kfuncs using struct *_args
Andrea Righi
reviewerPATCH v3 1/1 sched_ext/tools: Add compat layer for kfuncs using struct *_args
Commit c0d630ba347c7 ("sched_ext: Wrap kfunc args in struct to prepare for aux__prog") introduced new kfuncs that take their scalar arguments packed into dedicated *_args structs.
However, these new kfuncs need to coexist with the previous scalar variants, which causes build failures in sched_ext schedulers when building against older kernels.
To address this, mirror the struct definitions adding a __COMPAT prefix, ensuring schedulers can still build also on old kernels that don't have the BTF definitions for these structs.
Moreover, add a __COMPAT_bpf_ksym_exists() helper, which extends bpf_ksym_exists() to handle static inline kfunc wrappers and fall back to the *___compat() variants, where available.
---
Changes v2 -> v3:
- Drop the ___v2() suffix from the new kfuncs (Tejun)
- Add __COMPAT_bpf_ksym_exists() to handle kfunc detection with static inline wrappers
- Link to v2: https://lore.kernel.org/all/[email protected]/
ChangeLog v1 -> v2:
- Introduce __COMPAT_* struct to fix build error with v6.19 (thanks Emil)
tools/sched_ext/include/scx/common.bpf.h | 3 -- tools/sched_ext/include/scx/compat.bpf.h | 36 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-)
tools/sched_ext/include/scx/common.bpf.h +0 -3
@@ -60,9 +60,6 @@ static inline void ___vmlinux_h_sanity_check___(void) s32 scx_bpf_create_dsq(u64 dsq_id, s32 node) __ksym; s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags, bool *is_idle) __ksym; -s32 __scx_bpf_select_cpu_and(struct task_struct *p, const struct cpumask *cpus_allowed, - struct scx_bpf_select_cpu_and_args *args) __ksym __weak; -bool __scx_bpf_dsq_insert_vtime(struct task_struct *p, struct scx_bpf_dsq_insert_vtime_args *args) __ksym __weak; u32 scx_bpf_dispatch_nr_slots(void) __ksym; void scx_bpf_dispatch_cancel(void) __ksym; bool scx_bpf_dsq_move_to_local(u64 dsq_id) __ksym __weak;
tools/sched_ext/include/scx/compat.bpf.h +35 -1
@@ -15,6 +15,13 @@ __ret; \ }) +/* +* Extended version of bpf_ksym_exists() that handles static inline kfunc +* wrappers, falling back to the *___compat versions. +*/ +#define __COMPAT_bpf_ksym_exists (sym) \ + (__builtin_constant_p(!!sym) ? bpf_ksym_exists(sym##___compat) : !!sym) +/* * v6.15: 950ad93df2fc ("bpf: add kfunc for populating cpumask bits") *
@@ -161,6 +168,27 @@ static inline struct task_struct *__COMPAT_scx_bpf_cpu_curr(int cpu)return rq ? rq->curr :NULL ; } +/* +* v6.19: Mirror the following _args structs, to prevent build errors in +* kernels that don't have these structs defined yet. +* +* The kernel will carry these __COMPAT_* structs until v6.23 (see below). +*/ +#define scx_bpf_select_cpu_and_args __COMPAT_scx_bpf_select_cpu_and_args +struct __COMPAT_scx_bpf_select_cpu_and_args { +s32 prev_cpu; +u64 wake_flags; +u64 flags; +}; + +#define scx_bpf_dsq_insert_vtime_args __COMPAT_scx_bpf_dsq_insert_vtime_args +struct __COMPAT_scx_bpf_dsq_insert_vtime_args { +u64 dsq_id; +u64 slice; +u64 vtime; +u64 enq_flags; +}; +/* * v6.19: To work around BPF maximum parameter limit, the following kfuncs are * replaced with variants that pack scalar arguments in a struct. Wrappers are
@@ -170,9 +198,15 @@ static inline struct task_struct *__COMPAT_scx_bpf_cpu_curr(int cpu)* compatibility. After v6.23 release, remove the compat handling and move the * wrappers to common.bpf.h. */ +s32 __scx_bpf_select_cpu_and(struct task_struct *p, const struct cpumask *cpus_allowed, + struct scx_bpf_select_cpu_and_args *args) __ksym __weak; s32 scx_bpf_select_cpu_and___compat(struct task_struct *p, s32 prev_cpu, u64 wake_flags, const struct cpumask *cpus_allowed, u64 flags) __ksym __weak; -void scx_bpf_dsq_insert_vtime___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak; + +bool __scx_bpf_dsq_insert_vtime(struct task_struct *p, + struct scx_bpf_dsq_insert_vtime_args *args) __ksym __weak; +void scx_bpf_dsq_insert_vtime___compat(struct task_struct *p, + u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak;/** * scx_bpf_select_cpu_and - Pick an idle CPU usable by task @p
Signature · 1 line
2.51.1
Quoted 13 lines · Andrea Righi
Commit
c0d630ba347c7("sched_ext: Wrap kfunc args in struct to prepare foraux__prog") introduced new kfuncs that take their scalar arguments packed into dedicated*_argsstructs.However, these new kfuncs need to coexist with the previous scalar variants, which causes build failures in
sched_extschedulers when building against older kernels.To address this, mirror the struct definitions adding a
__COMPATprefix, ensuring schedulers can still build also on old kernels that don't have the BTF definitions for these structs.
Quoted 7 lines
Moreover, add a
__COMPAT_bpf_ksym_exists()helper, which extendsbpf_ksym_exists()to handle static inline kfunc wrappers and fall back to the*___compat()variants, where available.Fixes c0d630ba347c7 ("sched_ext: Wrap kfunc args in struct to prepare for aux__prog")Signed-off-by Andrea Righi <[email protected]>---
Tested the patch on a 6.17 and a 6.19 system, binaries run on both when compiled on either.
Quoted 93 lines
Changes v2 -> v3:
- Drop the___v2()suffix from the new kfuncs (Tejun)
- Add__COMPAT_bpf_ksym_exists()to handle kfunc detection with static inline wrappers
- Link to v2: https://lore.kernel.org/all/[email protected]/ChangeLog v1 -> v2:
- Introduce __COMPAT_* struct to fix build error with v6.19 (thanks Emil)tools/sched_ext/include/scx/common.bpf.h | 3 -- tools/sched_ext/include/scx/compat.bpf.h | 36 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-)tools/sched_ext/include/scx/common.bpf.h +0
-3@@ -60,9 +60,6 @@ static inline void ___vmlinux_h_sanity_check___(void) s32 scx_bpf_create_dsq(u64 dsq_id, s32 node) __ksym; s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags, bool *is_idle) __ksym; -s32 __scx_bpf_select_cpu_and(struct task_struct *p, const struct cpumask *cpus_allowed, - struct scx_bpf_select_cpu_and_args *args) __ksym __weak; -bool __scx_bpf_dsq_insert_vtime(struct task_struct *p, struct scx_bpf_dsq_insert_vtime_args *args) __ksym __weak; u32 scx_bpf_dispatch_nr_slots(void) __ksym; void scx_bpf_dispatch_cancel(void) __ksym; bool scx_bpf_dsq_move_to_local(u64 dsq_id) __ksym __weak;tools/sched_ext/include/scx/compat.bpf.h +35
-2@@ -15,6 +15,13 @@ __ret; \ }) +/* +* Extended version of bpf_ksym_exists() that handles static inline kfunc +* wrappers, falling back to the *___compat versions. +*/ +#define __COMPAT_bpf_ksym_exists (sym) \ + (__builtin_constant_p(!!sym) ? bpf_ksym_exists(sym##___compat) : !!sym) +/* * v6.15: 950ad93df2fc ("bpf: add kfunc for populating cpumask bits") * @@ -161,6 +168,27 @@ static inline struct task_struct *__COMPAT_scx_bpf_cpu_curr(int cpu)return rq ? rq->curr :NULL ; } +/* +* v6.19: Mirror the following _args structs, to prevent build errors in +* kernels that don't have these structs defined yet. +* +* The kernel will carry these __COMPAT_* structs until v6.23 (see below). +*/ +#define scx_bpf_select_cpu_and_args __COMPAT_scx_bpf_select_cpu_and_args +struct __COMPAT_scx_bpf_select_cpu_and_args { +s32 prev_cpu; +u64 wake_flags; +u64 flags; +}; + +#define scx_bpf_dsq_insert_vtime_args __COMPAT_scx_bpf_dsq_insert_vtime_args +struct __COMPAT_scx_bpf_dsq_insert_vtime_args { +u64 dsq_id; +u64 slice; +u64 vtime; +u64 enq_flags; +}; +/* * v6.19: To work around BPF maximum parameter limit, the following kfuncs are * replaced with variants that pack scalar arguments in a struct. Wrappers are @@ -170,9 +198,15 @@ static inline struct task_struct *__COMPAT_scx_bpf_cpu_curr(int cpu)* compatibility. After v6.23 release, remove the compat handling and move the * wrappers to common.bpf.h. */ +s32 __scx_bpf_select_cpu_and(struct task_struct *p, const struct cpumask *cpus_allowed, + struct scx_bpf_select_cpu_and_args *args) __ksym __weak; s32 scx_bpf_select_cpu_and___compat(struct task_struct *p, s32 prev_cpu, u64 wake_flags, const struct cpumask *cpus_allowed, u64 flags) __ksym __weak; -void scx_bpf_dsq_insert_vtime___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak; + +bool __scx_bpf_dsq_insert_vtime(struct task_struct *p, + struct scx_bpf_dsq_insert_vtime_args *args) __ksym __weak; +void scx_bpf_dsq_insert_vtime___compat(struct task_struct *p, + u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak;/** * scx_bpf_select_cpu_and - Pick an idle CPU usable by task @p -- 2.51.1
Tejun Heo
reviewerHello,
Quoted 4 lines · Andrea Righi
tools/sched_ext/include/scx/compat.bpf.h +4
-0+#define scx_bpf_select_cpu_and_args __COMPAT_scx_bpf_select_cpu_and_args +struct __COMPAT_scx_bpf_select_cpu_and_args { +s32 prev_cpu; +u64 wake_flags;
tools/sched_ext/include/scx/compat.bpf.h static inline struct task_struct *__COMPAT_scx_bpf_cpu_curr(int cpu) · line 190
Quoted 10 lines
tools/sched_ext/include/scx/compat.bpf.h +10
-0+u64 flags; +}; + +#define scx_bpf_dsq_insert_vtime_args __COMPAT_scx_bpf_dsq_insert_vtime_args +struct __COMPAT_scx_bpf_dsq_insert_vtime_args { +u64 dsq_id; +u64 slice; +u64 vtime; +u64 enq_flags; +};
I think this makes the inline compat wrapper to always pick the compat interface as it'd do bpf_core_type_exists() on the __COMPAT prefixed type name which doesn't exist in the kernel.
Thanks.
Signature · 1 line
tejun