The attach of kprobe multi link with BPF_F_KPROBE_MULTI_INLINE flag fails for several reasons with the same error. Pass the link_create log to bpf_kprobe_multi_link_attach and use it to explain the failure, including the function and the inlined instance that can't be attached. Signed-off-by: Jiri Olsa --- include/linux/trace_events.h | 7 +++- kernel/bpf/syscall.c | 2 +- kernel/trace/bpf_trace.c | 78 ++++++++++++++++++++++++++++-------- 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h index dff8a209875c..a6bd86be8ef5 100644 --- a/include/linux/trace_events.h +++ b/include/linux/trace_events.h @@ -15,6 +15,7 @@ struct array_buffer; struct tracer; struct dentry; struct bpf_prog; +struct bpf_verifier_log; union bpf_attr; struct bpf_verifier_log; @@ -795,7 +796,8 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, u32 *fd_type, const char **buf, u64 *probe_offset, u64 *probe_addr, unsigned long *missed); -int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog); +int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog, + struct bpf_verifier_log *log); int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog); int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr, struct bpf_verifier_log *log); @@ -847,7 +849,8 @@ static inline int bpf_get_perf_event_info(const struct perf_event *event, return -EOPNOTSUPP; } static inline int -bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) +bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog, + struct bpf_verifier_log *log) { return -EOPNOTSUPP; } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0bdb4288beb2..28797c5fd9d2 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6017,7 +6017,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a ret = bpf_perf_link_attach(attr, prog); else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) - ret = bpf_kprobe_multi_link_attach(attr, prog); + ret = bpf_kprobe_multi_link_attach(attr, prog, log); else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) ret = bpf_uprobe_multi_link_attach(attr, prog); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index af7d258d7357..7c0a22971d7a 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3149,6 +3149,7 @@ struct inline_lookup { u64 *cookies; u8 *args_cnt; u32 syms_cnt; + struct bpf_verifier_log *log; /* Output */ struct inline_sites *inlines; @@ -3168,8 +3169,11 @@ static int inline_lookup_add(struct inline_lookup *lookup, const struct btf *btf struct inline_site *sites; u32 cap; - if (lookup->cnt == MAX_KPROBE_MULTI_CNT) + if (lookup->cnt == MAX_KPROBE_MULTI_CNT) { + bpf_log(lookup->log, "Number of inlined instances is above the limit %u.\n", + MAX_KPROBE_MULTI_CNT); return -E2BIG; + } if (lookup->cnt == lookup->cap) { cap = max(16, lookup->cap * 2); @@ -3354,6 +3358,7 @@ static bool inline_site_traceable(unsigned long addr) static int inline_lookup_symbols(struct inline_lookup *lookup) { struct inline_sites *inlines = lookup->inlines; + struct bpf_verifier_log *log = lookup->log; unsigned long *addrs = lookup->addrs; u32 i, j, k, n, cnt = lookup->syms_cnt; u64 *cookies = lookup->cookies; @@ -3363,13 +3368,17 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) s32 id; btf = bpf_get_btf_vmlinux_inline(); - if (IS_ERR_OR_NULL(btf)) + if (IS_ERR_OR_NULL(btf)) { + bpf_log(log, "Kernel inline BTF is not available.\n"); return -EOPNOTSUPP; + } /* The offsets of inline.text locations are relative to .text section. */ id = btf_find_by_name_kind(btf, "inline.text", BTF_KIND_LOCSEC); - if (id < 0) + if (id < 0) { + bpf_log(log, "Kernel inline BTF has no inline.text section.\n"); return -EOPNOTSUPP; + } err = inline_lookup_modules(lookup); if (err) @@ -3405,6 +3414,8 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) if (!addrs[i]) { if (k < n && sites[k].sym_idx == i) continue; + bpf_log(log, "Function %s has no entry and no inlined instance.\n", + lookup->syms[i]); err = -ESRCH; goto error; } @@ -3430,8 +3441,10 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) * Register inlined instances of the @link and skip the ones that fail. * Fail if function has no entry and none of its instances registered, * with @strict fail on the first instance that fails to register. + * The instance that failed is returned in *@failed. */ -static int inline_sites_register(struct bpf_kprobe_multi_link *link, bool strict) +static int inline_sites_register(struct bpf_kprobe_multi_link *link, bool strict, + struct inline_site **failed) { struct inline_sites *inlines = &link->inlines; struct inline_site *site, *first = NULL; @@ -3457,8 +3470,10 @@ static int inline_sites_register(struct bpf_kprobe_multi_link *link, bool strict err = site->traceable ? register_kprobe(&site->kp) : -EINVAL; if (err == -ENOMEM) goto error; - if (err && strict) + if (err && strict) { + *failed = site; goto error; + } if (err) { first_err = first_err ?: err; } else { @@ -3470,6 +3485,7 @@ static int inline_sites_register(struct bpf_kprobe_multi_link *link, bool strict if (i + 1 < inlines->sites_cnt && site[1].sym_idx == site->sym_idx) continue; if (!attached) { + *failed = first; err = first_err; goto error; } @@ -3490,9 +3506,12 @@ static int inline_sites_register(struct bpf_kprobe_multi_link *link, bool strict return err; } -int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) +int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog, + struct bpf_verifier_log *log) { struct inline_sites inlines = {}; + struct inline_site *failed = NULL; + struct user_syms us = {}; struct modules_array mods = {}; struct bpf_kprobe_multi_link *link = NULL; struct bpf_link_primer link_primer; @@ -3535,8 +3554,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr if (!!uaddrs == !!usyms) return -EINVAL; - if (strict && !(flags & BPF_F_KPROBE_MULTI_INLINE)) + if (strict && !(flags & BPF_F_KPROBE_MULTI_INLINE)) { + bpf_log(log, "Strict flag needs to be used with inline flag.\n"); return -EINVAL; + } if (flags & BPF_F_KPROBE_MULTI_INLINE) { /* @@ -3544,16 +3565,28 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr * have just entry probe, so there's no return probe, session * or override support. */ - if (flags & BPF_F_KPROBE_MULTI_RETURN) + if (flags & BPF_F_KPROBE_MULTI_RETURN) { + bpf_log(log, "Inlined functions can't be attached with return probe.\n"); return -EINVAL; - if (is_kprobe_session(prog) || prog->kprobe_override) + } + if (is_kprobe_session(prog) || prog->kprobe_override) { + bpf_log(log, + "Inlined functions can't be attached with session or override program.\n"); return -EINVAL; - if (uaddrs) + } + if (uaddrs) { + bpf_log(log, + "Inlined functions need to be specified with kprobe_multi.syms.\n"); return -EINVAL; - if (!IS_ENABLED(CONFIG_KPROBES)) + } + if (!IS_ENABLED(CONFIG_KPROBES)) { + bpf_log(log, "Inlined functions need kprobes support.\n"); return -EOPNOTSUPP; - if (!IS_ENABLED(CONFIG_HAVE_KPROBE_MULTI_INLINE)) + } + if (!IS_ENABLED(CONFIG_HAVE_KPROBE_MULTI_INLINE)) { + bpf_log(log, "Inlined functions are not supported on this architecture.\n"); return -EOPNOTSUPP; + } } cnt = attr->link_create.kprobe_multi.cnt; @@ -3604,7 +3637,6 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr struct multi_symbols_sort data = { .cookies = cookies, }; - struct user_syms us; err = copy_user_syms(&us, usyms, cnt); if (err) @@ -3635,6 +3667,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr .addrs = addrs, .cookies = cookies, .args_cnt = args_cnt, + .log = log, }; err = inline_lookup_symbols(&lookup); @@ -3643,7 +3676,6 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr cnt = lookup.addrs_cnt; } - free_user_syms(&us); if (err) goto error; } @@ -3701,7 +3733,16 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr link->mods_cnt = mods.mods_cnt; if (inlines.sites_cnt) { - err = inline_sites_register(link, strict); + err = inline_sites_register(link, strict, &failed); + if (failed && strict) { + bpf_log(log, + "Function %s has inlined instance that can't be attached, %pS failed with %d.\n", + us.syms[failed->sym_idx], failed->kp.addr, err); + } else if (failed) { + bpf_log(log, + "Function %s has no entry and none of its inlined instances can be attached, %pS failed with %d.\n", + us.syms[failed->sym_idx], failed->kp.addr, err); + } if (err) goto error_modules; } @@ -3713,6 +3754,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr goto error_inlines; } + free_user_syms(&us); return bpf_link_settle(&link_primer); error_inlines: @@ -3721,9 +3763,11 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr kprobe_multi_put_modules(link->mods, link->mods_cnt); error_cleanup: bpf_link_cleanup(&link_primer); + free_user_syms(&us); return err; error: + free_user_syms(&us); kprobe_multi_put_modules(mods.mods, mods.mods_cnt); kfree(mods.mods); kfree(link); @@ -3734,8 +3778,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr return err; } #else /* !CONFIG_FPROBE */ -int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) +int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog, + struct bpf_verifier_log *log) { + bpf_log(log, "This kernel does not support kprobe multi link.\n"); return -EOPNOTSUPP; } static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx) -- 2.56.1