When loading module BTF, libbpf will iterate through the available BTF IDs in the kernel and try to load them. This fails inside a user namespace because of missing permissions. The kernel now supports passing in a bpf_token to BPF_BTF_GET_NEXT_ID, so amend the iteration to pass in a token if it exists. Passing in the token doesn't work on older kernels, so if we get an EINVAL back when trying to iterate, we reset any tokens and try again. Signed-off-by: Toke Høiland-Jørgensen --- tools/lib/bpf/libbpf.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2ede3081a20e..65c2af92db2f 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -6014,6 +6014,10 @@ static bool is_btf_mod_allowed(const struct bpf_object *obj, const char *name) static int load_module_btfs(struct bpf_object *obj) { + DECLARE_LIBBPF_OPTS(bpf_get_next_id_opts, next_opts, + .token_fd = obj->token_fd); + DECLARE_LIBBPF_OPTS(bpf_get_fd_by_id_opts, fd_opts, + .token_fd = obj->token_fd); struct bpf_btf_info info; struct module_btf *mod_btf; struct btf *btf; @@ -6038,20 +6042,26 @@ static int load_module_btfs(struct bpf_object *obj) return 0; while (true) { - err = bpf_btf_get_next_id(id, &id); + err = bpf_btf_get_next_id_opts(id, &id, &next_opts); if (err && errno == ENOENT) return 0; if (err && errno == EPERM) { pr_debug("skipping module BTFs loading, missing privileges\n"); return 0; } + if (err && errno == EINVAL && next_opts.token_fd) { + pr_debug("retrying module BTF load without token fd\n"); + next_opts.token_fd = 0; + fd_opts.token_fd = 0; + err = bpf_btf_get_next_id_opts(id, &id, &next_opts); + } if (err) { err = -errno; pr_warn("failed to iterate BTF objects: %s\n", errstr(err)); return err; } - fd = bpf_btf_get_fd_by_id(id); + fd = bpf_btf_get_fd_by_id_opts(id, &fd_opts); if (fd < 0) { if (errno == ENOENT) continue; /* expected race: BTF was unloaded */ -- 2.56.1