Add support to use cookies together with BPF_F_KPROBE_MULTI_INLINE flag. Each inlined instance carries the cookie configured for its function and the instance is passed to the program through the run context, so bpf_get_attach_cookie returns the cookie of the instance's function. When instances of several functions share the same address (nested inlines), the handler of each kprobe is called separately, so each instance runs the program with its own cookie. Functions without the entry are removed from the addrs array, so remove them from the cookies array as well to keep both arrays in sync. Signed-off-by: Jiri Olsa --- kernel/trace/bpf_trace.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 89388a140b48..15430d72c2a0 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2371,6 +2371,7 @@ struct bpf_kprobe_multi_link; struct inline_site { struct kprobe kp; struct bpf_kprobe_multi_link *link; + u64 cookie; u32 sym_idx; bool has_entry; bool traceable; @@ -2402,6 +2403,7 @@ struct bpf_kprobe_multi_run_ctx { struct bpf_session_run_ctx session_ctx; struct bpf_kprobe_multi_link *link; unsigned long entry_ip; + struct inline_site *site; }; struct user_syms { @@ -2629,6 +2631,8 @@ static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx) return 0; run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, session_ctx.run_ctx); + if (run_ctx->site) + return run_ctx->site->cookie; link = run_ctx->link; if (!link->cookies) return 0; @@ -2652,6 +2656,7 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) static __always_inline int kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, + struct inline_site *site, unsigned long entry_ip, struct ftrace_regs *fregs, struct pt_regs *regs, bool is_return, void *data) { @@ -2662,6 +2667,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, }, .link = link, .entry_ip = entry_ip, + .site = site, }; struct bpf_run_ctx *old_run_ctx; int err; @@ -2704,7 +2710,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - err = kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip), + err = kprobe_multi_link_prog_run(link, NULL, ftrace_get_entry_ip(fentry_ip), fregs, NULL, false, data); return is_kprobe_session(link->link.prog) ? err : 0; } @@ -2717,7 +2723,7 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, struct bpf_kprobe_multi_link *link; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip), + kprobe_multi_link_prog_run(link, NULL, ftrace_get_entry_ip(fentry_ip), fregs, NULL, true, data); } @@ -2727,7 +2733,7 @@ kprobe_multi_inline_handler(struct kprobe *kp, struct pt_regs *regs) struct inline_site *site; site = container_of(kp, struct inline_site, kp); - kprobe_multi_link_prog_run(site->link, (unsigned long)kp->addr, + kprobe_multi_link_prog_run(site->link, site, (unsigned long)kp->addr, NULL, regs, false, NULL); return 0; } @@ -2885,6 +2891,7 @@ struct inline_lookup { /* Input: functions to look for, @syms is alphabetically sorted. */ const char **syms; unsigned long *addrs; + u64 *cookies; u32 syms_cnt; /* Output */ @@ -2988,6 +2995,7 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) struct inline_sites *inlines = lookup->inlines; unsigned long *addrs = lookup->addrs; u32 i, j, k, n, cnt = lookup->syms_cnt; + u64 *cookies = lookup->cookies; struct inline_site *sites; struct btf *btf; int err; @@ -3015,13 +3023,14 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) sites[i].kp.pre_handler = kprobe_multi_inline_handler; /* Registered disabled and enabled all at once. */ sites[i].kp.flags = KPROBE_FLAG_DISABLED; + sites[i].cookie = cookies ? cookies[sites[i].sym_idx] : 0; sites[i].has_entry = !!addrs[sites[i].sym_idx]; sites[i].traceable = inline_site_traceable((unsigned long)sites[i].kp.addr); } /* * Each symbol needs to have either entry or inlined instance, - * keep only symbols with entry in @addrs array. + * keep only symbols with entry in @addrs and @cookies arrays. * The instances are sorted by symbol name, so we walk them together. */ for (i = 0, j = 0, k = 0; i < cnt; i++) { @@ -3034,6 +3043,8 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) goto error; } addrs[j] = addrs[i]; + if (cookies) + cookies[j] = cookies[i]; j++; } @@ -3224,6 +3235,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr .syms = us.syms, .syms_cnt = cnt, .addrs = addrs, + .cookies = cookies, }; err = inline_lookup_symbols(&lookup); -- 2.56.1