From: Tejun Heo Date: Sun, 8 Mar 2026 02:45:18 +0000 (-1000) Subject: tools/sched_ext/include: Add libbpf version guard for assoc_struct_ops X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=93ac9b150e2fba3a5e94e0b20d954a12e4b0907f;p=thirdparty%2Flinux.git tools/sched_ext/include: Add libbpf version guard for assoc_struct_ops Extract the inline bpf_program__assoc_struct_ops() call in SCX_OPS_LOAD() into a __scx_ops_assoc_prog() helper and wrap it with a libbpf >= 1.7 version guard. bpf_program__assoc_struct_ops() was added in libbpf 1.7; the guard provides a no-op fallback for older versions. Add the include needed by the helper, and fix "assumming" typo in a nearby comment. Signed-off-by: Tejun Heo Acked-by: Andrea Righi --- diff --git a/tools/sched_ext/include/scx/compat.h b/tools/sched_ext/include/scx/compat.h index 9b6df13b187bd..50297d4b95334 100644 --- a/tools/sched_ext/include/scx/compat.h +++ b/tools/sched_ext/include/scx/compat.h @@ -8,6 +8,7 @@ #define __SCX_COMPAT_H #include +#include #include #include #include @@ -182,6 +183,31 @@ static inline long scx_hotplug_seq(void) __skel; \ }) +/* + * Associate non-struct_ops BPF programs with the scheduler's struct_ops map so + * that scx_prog_sched() can determine which scheduler a BPF program belongs + * to. Requires libbpf >= 1.7. + */ +#if LIBBPF_MAJOR_VERSION > 1 || \ + (LIBBPF_MAJOR_VERSION == 1 && LIBBPF_MINOR_VERSION >= 7) +static inline void __scx_ops_assoc_prog(struct bpf_program *prog, + struct bpf_map *map, + const char *ops_name) +{ + s32 err = bpf_program__assoc_struct_ops(prog, map, NULL); + if (err) + fprintf(stderr, + "ERROR: Failed to associate %s with %s: %d\n", + bpf_program__name(prog), ops_name, err); +} +#else +static inline void __scx_ops_assoc_prog(struct bpf_program *prog, + struct bpf_map *map, + const char *ops_name) +{ +} +#endif + #define SCX_OPS_LOAD(__skel, __ops_name, __scx_name, __uei_name) ({ \ struct bpf_program *__prog; \ UEI_SET_SIZE(__skel, __ops_name, __uei_name); \ @@ -189,18 +215,15 @@ static inline long scx_hotplug_seq(void) bpf_object__for_each_program(__prog, (__skel)->obj) { \ if (bpf_program__type(__prog) == BPF_PROG_TYPE_STRUCT_OPS) \ continue; \ - s32 err = bpf_program__assoc_struct_ops(__prog, \ - (__skel)->maps.__ops_name, NULL); \ - if (err) \ - fprintf(stderr, "ERROR: Failed to associate %s with %s: %d\n", \ - bpf_program__name(__prog), #__ops_name, err); \ + __scx_ops_assoc_prog(__prog, (__skel)->maps.__ops_name, \ + #__ops_name); \ } \ }) /* * New versions of bpftool now emit additional link placeholders for BPF maps, * and set up BPF skeleton in such a way that libbpf will auto-attach BPF maps - * automatically, assumming libbpf is recent enough (v1.5+). Old libbpf will do + * automatically, assuming libbpf is recent enough (v1.5+). Old libbpf will do * nothing with those links and won't attempt to auto-attach maps. * * To maintain compatibility with older libbpf while avoiding trying to attach