bpf_load_and_run loads only the loader insns and the metadata map. To match the kernel's extended signature scope (insns || btf), the loader needs to BPF_BTF_LOAD the prog BTF before BPF_PROG_LOAD so attr->prog_btf_fd can be filled in. Add btf and btf_sz to bpf_load_and_run_opts; when set, do the BPF_BTF_LOAD before BPF_PROG_LOAD and pass the resulting fd as attr->prog_btf_fd. Signed-off-by: KP Singh --- tools/lib/bpf/skel_internal.h | 44 ++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/skel_internal.h b/tools/lib/bpf/skel_internal.h index 6a8f5c7a02eb..0b6b1ecedd45 100644 --- a/tools/lib/bpf/skel_internal.h +++ b/tools/lib/bpf/skel_internal.h @@ -11,6 +11,8 @@ #include #else #include +#include +#include #include #include #include @@ -66,8 +68,10 @@ struct bpf_load_and_run_opts { struct bpf_loader_ctx *ctx; const void *data; const void *insns; + const void *btf; __u32 data_sz; __u32 insns_sz; + __u32 btf_sz; const char *errstr; void *signature; __u32 signature_sz; @@ -88,6 +92,22 @@ static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, #endif } +#ifndef __KERNEL__ +static inline int skel_sys_btf_load(union bpf_attr *attr, unsigned int size) +{ + int fd; + + fd = skel_sys_bpf(BPF_BTF_LOAD, attr, size); + if (fd >= 0 && fd < 3) { + int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); + + close(fd); + fd = new_fd; + } + return fd; +} +#endif + #ifdef __KERNEL__ static inline int close(int fd) { @@ -353,8 +373,9 @@ static inline int skel_map_freeze(int fd) static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) { const size_t prog_load_attr_sz = offsetofend(union bpf_attr, keyring_id); + const size_t btf_load_attr_sz = offsetofend(union bpf_attr, btf_token_fd); const size_t test_run_attr_sz = offsetofend(union bpf_attr, test); - int map_fd = -1, prog_fd = -1, key = 0, err; + int map_fd = -1, prog_fd = -1, btf_fd = -1, key = 0, err; union bpf_attr attr; err = map_fd = skel_map_create(BPF_MAP_TYPE_ARRAY, "__loader.map", 4, opts->data_sz, 1, @@ -387,11 +408,30 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) } #endif +#ifndef __KERNEL__ + if (opts->btf && opts->btf_sz) { + memset(&attr, 0, btf_load_attr_sz); + attr.btf = (long) opts->btf; + attr.btf_size = opts->btf_sz; + attr.btf_log_level = opts->ctx->log_level; + attr.btf_log_size = opts->ctx->log_size; + attr.btf_log_buf = opts->ctx->log_buf; + err = btf_fd = skel_sys_btf_load(&attr, btf_load_attr_sz); + if (btf_fd < 0) { + opts->errstr = "failed to load loader BTF"; + set_err; + goto out; + } + } +#endif + memset(&attr, 0, prog_load_attr_sz); attr.prog_type = BPF_PROG_TYPE_SYSCALL; attr.insns = (long) opts->insns; attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn); attr.license = (long) "Dual BSD/GPL"; + if (btf_fd >= 0) + attr.prog_btf_fd = btf_fd; #ifndef __KERNEL__ attr.signature = (long) opts->signature; attr.signature_size = opts->signature_sz; @@ -437,6 +477,8 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) close(map_fd); if (prog_fd >= 0) close(prog_fd); + if (btf_fd >= 0) + close(btf_fd); return err; } -- 2.53.0