]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
tools/sched_ext/include: Add libbpf version guard for assoc_struct_ops
authorTejun Heo <tj@kernel.org>
Sun, 8 Mar 2026 02:45:18 +0000 (16:45 -1000)
committerTejun Heo <tj@kernel.org>
Sun, 8 Mar 2026 08:45:12 +0000 (22:45 -1000)
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
<bpf/libbpf.h> include needed by the helper, and fix "assumming" typo in
a nearby comment.

Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Andrea Righi <arighi@nvidia.com>
tools/sched_ext/include/scx/compat.h

index 9b6df13b187bd1e37bcc24a09724812fd2b67536..50297d4b953341701d3f60baac44c68a339868b9 100644 (file)
@@ -8,6 +8,7 @@
 #define __SCX_COMPAT_H
 
 #include <bpf/btf.h>
+#include <bpf/libbpf.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -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