]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
libbpf: improve old BPF skeleton handling for map auto-attach
authorAndrii Nakryiko <andrii@kernel.org>
Mon, 8 Jul 2024 20:45:40 +0000 (13:45 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 10 Jul 2024 02:03:36 +0000 (19:03 -0700)
Improve how we handle old BPF skeletons when it comes to BPF map
auto-attachment. Emit one warn-level message per each struct_ops map
that could have been auto-attached, if user provided recent enough BPF
skeleton version. Don't spam log if there are no relevant struct_ops
maps, though.

This should help users realize that they probably need to regenerate BPF
skeleton header with more recent bpftool/libbpf-cargo (or whatever other
means of BPF skeleton generation).

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20240708204540.4188946-4-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/libbpf.c

index 8625c948f60a803f9033a098e154c46c0acd3973..a3be6f8fac09ea25983f3619c5094c5dde9ef4ae 100644 (file)
@@ -13967,32 +13967,34 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
                 */
        }
 
-       /* Skeleton is created with earlier version of bpftool
-        * which does not support auto-attachment
-        */
-       if (s->map_skel_sz < sizeof(struct bpf_map_skeleton))
-               return 0;
 
        for (i = 0; i < s->map_cnt; i++) {
                struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz;
                struct bpf_map *map = *map_skel->map;
-               struct bpf_link **link = map_skel->link;
+               struct bpf_link **link;
 
                if (!map->autocreate || !map->autoattach)
                        continue;
 
-               if (*link)
-                       continue;
-
                /* only struct_ops maps can be attached */
                if (!bpf_map__is_struct_ops(map))
                        continue;
-               *link = bpf_map__attach_struct_ops(map);
 
+               /* skeleton is created with earlier version of bpftool, notify user */
+               if (s->map_skel_sz < offsetofend(struct bpf_map_skeleton, link)) {
+                       pr_warn("map '%s': BPF skeleton version is old, skipping map auto-attachment...\n",
+                               bpf_map__name(map));
+                       continue;
+               }
+
+               link = map_skel->link;
+               if (*link)
+                       continue;
+
+               *link = bpf_map__attach_struct_ops(map);
                if (!*link) {
                        err = -errno;
-                       pr_warn("map '%s': failed to auto-attach: %d\n",
-                               bpf_map__name(map), err);
+                       pr_warn("map '%s': failed to auto-attach: %d\n", bpf_map__name(map), err);
                        return libbpf_err(err);
                }
        }