Re: [PATCH bpf-next v13 14/14] bpf: pass btf object id in bpf_map_info.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 12/14/23 23:46, Martin KaFai Lau wrote:
On 12/8/23 4:27 PM, thinker.li@xxxxxxxxx wrote:
From: Kui-Feng Lee <thinker.li@xxxxxxxxx>

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@xxxxxxxxx>
---
  include/linux/bpf.h            | 1 +
  include/uapi/linux/bpf.h       | 2 +-
  kernel/bpf/bpf_struct_ops.c    | 7 +++++++
  kernel/bpf/syscall.c           | 2 ++
  tools/include/uapi/linux/bpf.h | 2 +-
  5 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index c881befa35f5..26103d8a4374 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3350,5 +3350,6 @@ struct bpf_struct_ops_##_name {                    \
  int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
                   struct btf *btf,
                   struct bpf_verifier_log *log);
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);

This needs to be in the CONFIG_BPF_JIT guard also.

Got it!

  #endif /* _LINUX_BPF_H */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 5c3838a97554..716c6b28764d 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
      __u32 btf_id;
      __u32 btf_key_type_id;
      __u32 btf_value_type_id;
-    __u32 :32;    /* alignment pad */
+    __u32 btf_obj_id;

may be "btf_vmlinux_id" to make it clear it is a kernel btf and should be used with map_info->btf_vmlinux_value_type_id.

Sure!


      __u64 map_extra;
  } __attribute__((aligned(8)));
diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
index fd26716fa0f9..51c0de75aa85 100644
--- a/kernel/bpf/bpf_struct_ops.c
+++ b/kernel/bpf/bpf_struct_ops.c
@@ -979,3 +979,10 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
      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_obj_id = btf_obj_id(st_map->btf);
+}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 4aced7e58904..3cab56cd02ff 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -4715,6 +4715,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);

This patch should be moved earlier in the set instead of after the selftest patch 13. May be after patch 5 where st_map->btf is added.

No problem


and where is the test for this?

I use bpf_map_info as a part of calling bpf_map_create() while
testmod.ko is unloaded. It check if this change work as well.


      if (bpf_map_is_offloaded(map)) {
          err = bpf_map_offload_info_fill(&info, map);
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 5c3838a97554..716c6b28764d 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
      __u32 btf_id;
      __u32 btf_key_type_id;
      __u32 btf_value_type_id;
-    __u32 :32;    /* alignment pad */
+    __u32 btf_obj_id;
      __u64 map_extra;
  } __attribute__((aligned(8)));





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux