diff mbox series

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

Message ID 20231209002709.535966-15-thinker.li@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Registrating struct_ops types from modules | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/ynl success SINGLE THREAD; Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 7874 this patch: 7874
netdev/cc_maintainers warning 8 maintainers not CCed: kpsingh@kernel.org daniel@iogearbox.net netdev@vger.kernel.org yonghong.song@linux.dev sdf@google.com haoluo@google.com jolsa@kernel.org john.fastabend@gmail.com
netdev/build_clang success Errors and warnings before: 2577 this patch: 2577
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8409 this patch: 8409
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 82 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 1 this patch: 1
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-13 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-15 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18

Commit Message

Kui-Feng Lee Dec. 9, 2023, 12:27 a.m. UTC
From: Kui-Feng Lee <thinker.li@gmail.com>

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>
---
 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(-)

Comments

Martin KaFai Lau Dec. 15, 2023, 7:46 a.m. UTC | #1
On 12/8/23 4:27 PM, thinker.li@gmail.com wrote:
> From: Kui-Feng Lee <thinker.li@gmail.com>
> 
> 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>
> ---
>   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.

>   
>   #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.

>   	__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.

and where is the test for this?

>   
>   	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)));
>
Kui-Feng Lee Dec. 17, 2023, 7:35 a.m. UTC | #2
On 12/14/23 23:46, Martin KaFai Lau wrote:
> On 12/8/23 4:27 PM, thinker.li@gmail.com wrote:
>> From: Kui-Feng Lee <thinker.li@gmail.com>
>>
>> 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>
>> ---
>>   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)));
>
diff mbox series

Patch

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);
 
 #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;
 	__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);
 
 	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)));