public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>,
	bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
	daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com,
	eddyz87@gmail.com
Cc: Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH bpf-next v3 3/4] libbpf: pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID
Date: Fri, 7 Mar 2025 21:02:22 -0800	[thread overview]
Message-ID: <473d1583-4663-44da-93f9-82fa331ca16f@linux.dev> (raw)
In-Reply-To: <20250307212934.181996-4-mykyta.yatsenko5@gmail.com>



On 3/7/25 1:29 PM, Mykyta Yatsenko wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
>
> Pass BPF token from bpf_program__set_attach_target to
> BPF_BTF_GET_FD_BY_ID bpf command.
> When freplace program attaches to target program, it needs to look up
> for BTF of the target, this may require BPF token, if, for example,
> running from user namespace.

LGTM with small nit below.

Acked-by: Yonghong Song <yonghong.song@linux.dev>

>
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---
>   tools/lib/bpf/bpf.c             |  3 ++-
>   tools/lib/bpf/bpf.h             |  4 +++-
>   tools/lib/bpf/btf.c             | 14 ++++++++++++--
>   tools/lib/bpf/libbpf.c          | 10 +++++-----
>   tools/lib/bpf/libbpf_internal.h |  1 +
>   5 files changed, 23 insertions(+), 9 deletions(-)
>
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index 359f73ead613..783274172e56 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -1097,7 +1097,7 @@ int bpf_map_get_fd_by_id(__u32 id)
>   int bpf_btf_get_fd_by_id_opts(__u32 id,
>   			      const struct bpf_get_fd_by_id_opts *opts)
>   {
> -	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
> +	const size_t attr_sz = offsetofend(union bpf_attr, token_fd);
>   	union bpf_attr attr;
>   	int fd;
>   
> @@ -1107,6 +1107,7 @@ int bpf_btf_get_fd_by_id_opts(__u32 id,
>   	memset(&attr, 0, attr_sz);
>   	attr.btf_id = id;
>   	attr.open_flags = OPTS_GET(opts, open_flags, 0);
> +	attr.token_fd = OPTS_GET(opts, token_fd, 0);
>   
>   	fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
>   	return libbpf_err_errno(fd);
> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> index 435da95d2058..544215d7137c 100644
> --- a/tools/lib/bpf/bpf.h
> +++ b/tools/lib/bpf/bpf.h
> @@ -487,9 +487,11 @@ LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
>   struct bpf_get_fd_by_id_opts {
>   	size_t sz; /* size of this struct for forward/backward compatibility */
>   	__u32 open_flags; /* permissions requested for the operation on fd */
> +	__u32 token_fd;
>   	size_t :0;
>   };
> -#define bpf_get_fd_by_id_opts__last_field open_flags
> +
> +#define bpf_get_fd_by_id_opts__last_field token_fd
>   
>   LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
>   LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index eea99c766a20..466336f16134 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -1619,12 +1619,17 @@ struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf)
>   	return btf;
>   }
>   
> -struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
> +struct btf *btf_load_from_kernel(__u32 id, struct btf *base_btf, int token_fd)
>   {
>   	struct btf *btf;
>   	int btf_fd;
> +	LIBBPF_OPTS(bpf_get_fd_by_id_opts, opts);
> +
> +	opts.token_fd = token_fd;
> +	if (token_fd)
> +		opts.open_flags |= BPF_F_TOKEN_FD;

To 'move opts.token_fd = token_fd' under if condition? Something like

	if (token_fd) {
		opts.open_flags |= BPF_F_TOKEN_FD;
		opts.token_fd = token_fd;
	}
?

>   
> -	btf_fd = bpf_btf_get_fd_by_id(id);
> +	btf_fd = bpf_btf_get_fd_by_id_opts(id, &opts);
>   	if (btf_fd < 0)
>   		return libbpf_err_ptr(-errno);
>   
>
[...]

  reply	other threads:[~2025-03-08  5:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-07 21:29 [PATCH bpf-next v3 0/4] Support freplace prog from user namespace Mykyta Yatsenko
2025-03-07 21:29 ` [PATCH bpf-next v3 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID Mykyta Yatsenko
2025-03-08  4:53   ` Yonghong Song
2025-03-07 21:29 ` [PATCH bpf-next v3 2/4] bpf: return prog btf_id without capable check Mykyta Yatsenko
2025-03-08  4:54   ` Yonghong Song
2025-03-07 21:29 ` [PATCH bpf-next v3 3/4] libbpf: pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID Mykyta Yatsenko
2025-03-08  5:02   ` Yonghong Song [this message]
2025-03-07 21:29 ` [PATCH bpf-next v3 4/4] selftests/bpf: test freplace from user namespace Mykyta Yatsenko
2025-03-08  5:12   ` Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=473d1583-4663-44da-93f9-82fa331ca16f@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kafai@meta.com \
    --cc=kernel-team@meta.com \
    --cc=mykyta.yatsenko5@gmail.com \
    --cc=yatsenko@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox