Skip to content

Commit 974ef9f

Browse files
mykyta5anakryiko
authored andcommitted
libbpf: Pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID
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@meta.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/bpf/20250317174039.161275-4-mykyta.yatsenko5@gmail.com
1 parent 07651cc commit 974ef9f

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

tools/lib/bpf/bpf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ int bpf_map_get_fd_by_id(__u32 id)
10971097
int bpf_btf_get_fd_by_id_opts(__u32 id,
10981098
const struct bpf_get_fd_by_id_opts *opts)
10991099
{
1100-
const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1100+
const size_t attr_sz = offsetofend(union bpf_attr, fd_by_id_token_fd);
11011101
union bpf_attr attr;
11021102
int fd;
11031103

@@ -1107,6 +1107,7 @@ int bpf_btf_get_fd_by_id_opts(__u32 id,
11071107
memset(&attr, 0, attr_sz);
11081108
attr.btf_id = id;
11091109
attr.open_flags = OPTS_GET(opts, open_flags, 0);
1110+
attr.fd_by_id_token_fd = OPTS_GET(opts, token_fd, 0);
11101111

11111112
fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
11121113
return libbpf_err_errno(fd);

tools/lib/bpf/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,10 @@ LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
487487
struct bpf_get_fd_by_id_opts {
488488
size_t sz; /* size of this struct for forward/backward compatibility */
489489
__u32 open_flags; /* permissions requested for the operation on fd */
490+
__u32 token_fd;
490491
size_t :0;
491492
};
492-
#define bpf_get_fd_by_id_opts__last_field open_flags
493+
#define bpf_get_fd_by_id_opts__last_field token_fd
493494

494495
LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
495496
LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,

tools/lib/bpf/btf.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,12 +1619,18 @@ struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf)
16191619
return btf;
16201620
}
16211621

1622-
struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
1622+
struct btf *btf_load_from_kernel(__u32 id, struct btf *base_btf, int token_fd)
16231623
{
16241624
struct btf *btf;
16251625
int btf_fd;
1626+
LIBBPF_OPTS(bpf_get_fd_by_id_opts, opts);
1627+
1628+
if (token_fd) {
1629+
opts.open_flags |= BPF_F_TOKEN_FD;
1630+
opts.token_fd = token_fd;
1631+
}
16261632

1627-
btf_fd = bpf_btf_get_fd_by_id(id);
1633+
btf_fd = bpf_btf_get_fd_by_id_opts(id, &opts);
16281634
if (btf_fd < 0)
16291635
return libbpf_err_ptr(-errno);
16301636

@@ -1634,6 +1640,11 @@ struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
16341640
return libbpf_ptr(btf);
16351641
}
16361642

1643+
struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
1644+
{
1645+
return btf_load_from_kernel(id, base_btf, 0);
1646+
}
1647+
16371648
struct btf *btf__load_from_kernel_by_id(__u32 id)
16381649
{
16391650
return btf__load_from_kernel_by_id_split(id, NULL);

tools/lib/bpf/libbpf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10024,7 +10024,7 @@ int libbpf_find_vmlinux_btf_id(const char *name,
1002410024
return libbpf_err(err);
1002510025
}
1002610026

10027-
static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
10027+
static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int token_fd)
1002810028
{
1002910029
struct bpf_prog_info info;
1003010030
__u32 info_len = sizeof(info);
@@ -10044,7 +10044,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
1004410044
pr_warn("The target program doesn't have BTF\n");
1004510045
goto out;
1004610046
}
10047-
btf = btf__load_from_kernel_by_id(info.btf_id);
10047+
btf = btf_load_from_kernel(info.btf_id, NULL, token_fd);
1004810048
err = libbpf_get_error(btf);
1004910049
if (err) {
1005010050
pr_warn("Failed to get BTF %d of the program: %s\n", info.btf_id, errstr(err));
@@ -10127,7 +10127,7 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attac
1012710127
pr_warn("prog '%s': attach program FD is not set\n", prog->name);
1012810128
return -EINVAL;
1012910129
}
10130-
err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
10130+
err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd, prog->obj->token_fd);
1013110131
if (err < 0) {
1013210132
pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %s\n",
1013310133
prog->name, attach_prog_fd, attach_name, errstr(err));
@@ -12923,7 +12923,7 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
1292312923
if (target_fd) {
1292412924
LIBBPF_OPTS(bpf_link_create_opts, target_opts);
1292512925

12926-
btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
12926+
btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd, prog->obj->token_fd);
1292712927
if (btf_id < 0)
1292812928
return libbpf_err_ptr(btf_id);
1292912929

@@ -13744,7 +13744,7 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
1374413744

1374513745
if (attach_prog_fd) {
1374613746
btf_id = libbpf_find_prog_btf_id(attach_func_name,
13747-
attach_prog_fd);
13747+
attach_prog_fd, prog->obj->token_fd);
1374813748
if (btf_id < 0)
1374913749
return libbpf_err(btf_id);
1375013750
} else {

tools/lib/bpf/libbpf_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
409409
int btf_load_into_kernel(struct btf *btf,
410410
char *log_buf, size_t log_sz, __u32 log_level,
411411
int token_fd);
412+
struct btf *btf_load_from_kernel(__u32 id, struct btf *base_btf, int token_fd);
412413

413414
struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf);
414415
void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,

0 commit comments

Comments
 (0)