diff mbox series

[bpf-next,1/2] bpf: use proper target btf when exporting attach_btf_obj_id

Message ID 20220730000809.312891-1-sdf@google.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next,1/2] bpf: use proper target btf when exporting attach_btf_obj_id | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 13 this patch: 13
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 13 this patch: 13
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 19 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Stanislav Fomichev July 30, 2022, 12:08 a.m. UTC
When attaching to program, the program itself might not be attached
to anything (and, hence, might not have attach_btf), so we can't
unconditionally use 'prog->aux->dst_prog->aux->attach_btf'.
Instead, use bpf_prog_get_target_btf to pick proper target btf:

* when attached to dst_prog, use dst_prog->aux->btf
* when attached to kernel btf, use prog->aux->attach_btf

Fixes: b79c9fc9551b ("bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP")
Signed-off-by: Stanislav Fomichev <sdf@google.com>
---
 kernel/bpf/syscall.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Comments

Hao Luo July 30, 2022, 12:24 a.m. UTC | #1
On Fri, Jul 29, 2022 at 5:08 PM Stanislav Fomichev <sdf@google.com> wrote:
>
> When attaching to program, the program itself might not be attached
> to anything (and, hence, might not have attach_btf), so we can't
> unconditionally use 'prog->aux->dst_prog->aux->attach_btf'.
> Instead, use bpf_prog_get_target_btf to pick proper target btf:
>
> * when attached to dst_prog, use dst_prog->aux->btf
> * when attached to kernel btf, use prog->aux->attach_btf
>
> Fixes: b79c9fc9551b ("bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP")
> Signed-off-by: Stanislav Fomichev <sdf@google.com>

Looks good to me.

Acked-by: Hao Luo <haoluo@google.com>

Hao
Martin KaFai Lau Aug. 1, 2022, 4:57 p.m. UTC | #2
On Fri, Jul 29, 2022 at 05:08:08PM -0700, Stanislav Fomichev wrote:
> When attaching to program, the program itself might not be attached
> to anything (and, hence, might not have attach_btf), so we can't
> unconditionally use 'prog->aux->dst_prog->aux->attach_btf'.
> Instead, use bpf_prog_get_target_btf to pick proper target btf:
> 
> * when attached to dst_prog, use dst_prog->aux->btf
> * when attached to kernel btf, use prog->aux->attach_btf
> 
> Fixes: b79c9fc9551b ("bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP")
> Signed-off-by: Stanislav Fomichev <sdf@google.com>
> ---
>  kernel/bpf/syscall.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 83c7136c5788..7dc3f8003631 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3886,6 +3886,7 @@ static int bpf_prog_get_info_by_fd(struct file *file,
>  				   union bpf_attr __user *uattr)
>  {
>  	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
> +	struct btf *attach_btf = bpf_prog_get_target_btf(prog);
>  	struct bpf_prog_info info;
>  	u32 info_len = attr->info.info_len;
>  	struct bpf_prog_kstats stats;
> @@ -4088,10 +4089,8 @@ static int bpf_prog_get_info_by_fd(struct file *file,
>  	if (prog->aux->btf)
>  		info.btf_id = btf_obj_id(prog->aux->btf);
>  	info.attach_btf_id = prog->aux->attach_btf_id;
> -	if (prog->aux->attach_btf)
> -		info.attach_btf_obj_id = btf_obj_id(prog->aux->attach_btf);
> -	else if (prog->aux->dst_prog)
> -		info.attach_btf_obj_id = btf_obj_id(prog->aux->dst_prog->aux->attach_btf);
> +	if (attach_btf)
> +		info.attach_btf_obj_id = btf_obj_id(attach_btf);
Nice.

Acked-by: Martin KaFai Lau <kafai@fb.com>
diff mbox series

Patch

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 83c7136c5788..7dc3f8003631 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3886,6 +3886,7 @@  static int bpf_prog_get_info_by_fd(struct file *file,
 				   union bpf_attr __user *uattr)
 {
 	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
+	struct btf *attach_btf = bpf_prog_get_target_btf(prog);
 	struct bpf_prog_info info;
 	u32 info_len = attr->info.info_len;
 	struct bpf_prog_kstats stats;
@@ -4088,10 +4089,8 @@  static int bpf_prog_get_info_by_fd(struct file *file,
 	if (prog->aux->btf)
 		info.btf_id = btf_obj_id(prog->aux->btf);
 	info.attach_btf_id = prog->aux->attach_btf_id;
-	if (prog->aux->attach_btf)
-		info.attach_btf_obj_id = btf_obj_id(prog->aux->attach_btf);
-	else if (prog->aux->dst_prog)
-		info.attach_btf_obj_id = btf_obj_id(prog->aux->dst_prog->aux->attach_btf);
+	if (attach_btf)
+		info.attach_btf_obj_id = btf_obj_id(attach_btf);
 
 	ulen = info.nr_func_info;
 	info.nr_func_info = prog->aux->func_info_cnt;