Re: [PATCH bpf-next 2/3] libbpf: pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID

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

 



On Tue, Mar 4, 2025 at 1:15 PM Mykyta Yatsenko
<mykyta.yatsenko5@xxxxxxxxx> wrote:
>
> From: Mykyta Yatsenko <yatsenko@xxxxxxxx>
>
> 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.
>
> Signed-off-by: Mykyta Yatsenko <yatsenko@xxxxxxxx>
> ---
>  tools/include/uapi/linux/bpf.h  |  1 +
>  tools/lib/bpf/bpf.c             |  3 ++-
>  tools/lib/bpf/bpf.h             |  4 +++-
>  tools/lib/bpf/btf.c             | 10 ++++++++--
>  tools/lib/bpf/libbpf.c          | 10 +++++-----
>  tools/lib/bpf/libbpf_internal.h |  1 +
>  6 files changed, 20 insertions(+), 9 deletions(-)
>
> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> index bb37897c0393..73c23daacabf 100644
> --- a/tools/include/uapi/linux/bpf.h
> +++ b/tools/include/uapi/linux/bpf.h

let's update tools/include/uapi/linux/bpf.h in the same patch as
include/uapi/linux/bpf.h itself

> @@ -1652,6 +1652,7 @@ union bpf_attr {
>                 };
>                 __u32           next_id;
>                 __u32           open_flags;
> +               __s32           token_fd;
>         };
>
>         struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
> 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..251071e1ce1d 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -1619,12 +1619,13 @@ 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_by_id_split(__u32 id, struct btf *base_btf, int token_fd)

that's quite a name we now have :) let's call it just "btf_load_from_kernel"

>  {
>         struct btf *btf;
>         int btf_fd;
> +       LIBBPF_OPTS(bpf_get_fd_by_id_opts, 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);
>

[...]





[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