The kprobe multi link with BPF_F_KPROBE_MULTI_INLINE flag skips inlined instances that fail to attach, so the link can end up attached just to some of the inlined instances of the function. Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for users that need all the inlined instances attached. With the flag the attach fails on the first inlined instance that fails to register and returns its error. The flag needs to be used together with BPF_F_KPROBE_MULTI_INLINE flag, the attach fails with -EINVAL otherwise. Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 3 +++ kernel/trace/bpf_trace.c | 17 +++++++++++++---- tools/include/uapi/linux/bpf.h | 3 +++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index eee02f62f98b..3b97904851f4 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1373,6 +1373,9 @@ enum { /* Attach also to inlined function instances, skip those that fail. */ BPF_F_KPROBE_MULTI_INLINE = (1U << 1), + + /* With BPF_F_KPROBE_MULTI_INLINE, fail if inlined instance fails. */ + BPF_F_KPROBE_MULTI_INLINE_STRICT = (1U << 2), }; /* link_create.uprobe_multi.flags used in LINK_CREATE command for diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 71da54a6ea20..552a7420fb8c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3184,9 +3184,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. + * Fail if function has no entry and none of its instances registered, + * with @strict fail on the first instance that fails to register. */ -static int inline_sites_register(struct bpf_kprobe_multi_link *link) +static int inline_sites_register(struct bpf_kprobe_multi_link *link, bool strict) { struct inline_sites *inlines = &link->inlines; struct inline_site *site, *first = NULL; @@ -3212,6 +3213,8 @@ static int inline_sites_register(struct bpf_kprobe_multi_link *link) err = site->traceable ? register_kprobe(&site->kp) : -EINVAL; if (err == -ENOMEM) goto error; + if (err && strict) + goto error; if (err) { first_err = first_err ?: err; } else { @@ -3255,6 +3258,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr void __user *uaddrs; u64 *cookies = NULL; void __user *usyms; + bool strict; int err; /* no support for 32bit archs yet */ @@ -3276,14 +3280,19 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr return -EINVAL; flags = attr->link_create.kprobe_multi.flags; - if (flags & ~(BPF_F_KPROBE_MULTI_RETURN | BPF_F_KPROBE_MULTI_INLINE)) + if (flags & ~(BPF_F_KPROBE_MULTI_RETURN | BPF_F_KPROBE_MULTI_INLINE | + BPF_F_KPROBE_MULTI_INLINE_STRICT)) return -EINVAL; + strict = flags & BPF_F_KPROBE_MULTI_INLINE_STRICT; uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs); usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms); if (!!uaddrs == !!usyms) return -EINVAL; + if (strict && !(flags & BPF_F_KPROBE_MULTI_INLINE)) + return -EINVAL; + if (flags & BPF_F_KPROBE_MULTI_INLINE) { /* * Inlined instances are resolved from function names and @@ -3426,7 +3435,7 @@ 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); + err = inline_sites_register(link, strict); if (err) goto error_modules; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index eee02f62f98b..3b97904851f4 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1373,6 +1373,9 @@ enum { /* Attach also to inlined function instances, skip those that fail. */ BPF_F_KPROBE_MULTI_INLINE = (1U << 1), + + /* With BPF_F_KPROBE_MULTI_INLINE, fail if inlined instance fails. */ + BPF_F_KPROBE_MULTI_INLINE_STRICT = (1U << 2), }; /* link_create.uprobe_multi.flags used in LINK_CREATE command for -- 2.56.1