bpf: pass btf object id in bpf_map_info.
authorKui-Feng Lee <thinker.li@gmail.com>
Fri, 19 Jan 2024 22:49:57 +0000 (14:49 -0800)
committerMartin KaFai Lau <martin.lau@kernel.org>
Wed, 24 Jan 2024 00:37:44 +0000 (16:37 -0800)
Include btf object id (btf_obj_id) in bpf_map_info so that tools (ex:
bpftools struct_ops dump) know the correct btf from the kernel to look up
type information of struct_ops types.

Since struct_ops types can be defined and registered in a module. The
type information of a struct_ops type are defined in the btf of the
module defining it.  The userspace tools need to know which btf is for
the module defining a struct_ops type.

Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
Link: https://lore.kernel.org/r/20240119225005.668602-7-thinker.li@gmail.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/bpf_struct_ops.c
kernel/bpf/syscall.c
tools/include/uapi/linux/bpf.h

index 7fc95e7..29fcae9 100644 (file)
@@ -1732,6 +1732,7 @@ struct bpf_dummy_ops {
 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
                            union bpf_attr __user *uattr);
 #endif
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);
 #else
 static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(u32 type_id)
 {
@@ -1759,6 +1760,9 @@ static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
 {
        return -EOPNOTSUPP;
 }
+static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
+{
+}
 
 #endif
 
index 287d057..a380047 100644 (file)
@@ -6487,7 +6487,7 @@ struct bpf_map_info {
        __u32 btf_id;
        __u32 btf_key_type_id;
        __u32 btf_value_type_id;
-       __u32 :32;      /* alignment pad */
+       __u32 btf_vmlinux_id;
        __u64 map_extra;
 } __attribute__((aligned(8)));
 
index 5ddcca4..5e98af4 100644 (file)
@@ -947,3 +947,10 @@ err_out:
        kfree(link);
        return err;
 }
+
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
+{
+       struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+
+       info->btf_vmlinux_id = btf_obj_id(st_map->btf);
+}
index 13193aa..55b4584 100644 (file)
@@ -4691,6 +4691,8 @@ static int bpf_map_get_info_by_fd(struct file *file,
                info.btf_value_type_id = map->btf_value_type_id;
        }
        info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
+       if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS)
+               bpf_map_struct_ops_info_fill(&info, map);
 
        if (bpf_map_is_offloaded(map)) {
                err = bpf_map_offload_info_fill(&info, map);
index 287d057..a380047 100644 (file)
@@ -6487,7 +6487,7 @@ struct bpf_map_info {
        __u32 btf_id;
        __u32 btf_key_type_id;
        __u32 btf_value_type_id;
-       __u32 :32;      /* alignment pad */
+       __u32 btf_vmlinux_id;
        __u64 map_extra;
 } __attribute__((aligned(8)));