From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796214725; b=MeaXuYwP/JrqoBezhAsRcvIIDXyMMco0YnRdHA7kdcFNbXMLCI4xZNiLQ1eLhVO3iqK0 oSydjFkYW6gsnDbm0RZkD2fhU2RuVN5CLc63zLKQ038K5HkJITJozei9XdpL+9Q5QIAo7 TS64kPGmnNmWXhS2UUQOi2eu8jPvrV+e/Afr53kzMnHwKgFugGFLF949jj7jTAHDROt6U d7TwmZ+SuzfRFtQMN1d1gSCvyPvc5WGMu/dtGkv+aF8UVW7fMAFxUI0i6oFbmRzf1UaNt aoh/9iXa0hwKv5I+dK67sqyMTQkSyimbWZYucElDiaR5h8SX/xDk0dR7qguf5I8J1kA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214725; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=en4rG6uJEfKF29nz+6nVW9TQKHkRBzX7SYPguij7QMw=; b=kLwRJjTRlFDxZHCCIiUij40GKrNHrdxZ5kkG+LssaFW+5oRgta8zsjj8uvURGMZnHZVy BsiZD482y5ByfCAyQ4jr8rJSQVdi3D/ZSNTujdUOKz5BCPpDr0TxO1w+t1t6MIl3KXcV0 8X2ijBF3Hx/IJfiAmmIfblXZMJwiI5qINikpGcc5aOos6ODiOJfguPguNaNMS8JV0lbaW lm0sgrnoF++G9iX2b7o408k9RceZqTTwIxsWgR++RUMcmSfdbUBYkVu6rWT++3O0k9mP8 vjcCvV+dHjAcc2k8KfN9SXq6SPIqhku2EhuUOZJg41dYa9pWf+PUJTW/77qevmmCmLw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=iLHazLBp; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 792B3606A330; Wed, 2 Dec 2026 12:32:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=iLHazLBp Received: by smtp.kernel.org (Postfix) with ESMTPSA id F1D411F000C1; Wed, 2 Dec 2026 12:32:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214725; bh=en4rG6uJEfKF29nz+6nVW9TQKHkRBzX7SYPguij7QMw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=iLHazLBpQIatGacwcnG9qzTdtbWfq/55PrUqUWgB9HbKlv4/aimquvIkck614sxZM eJUO8doZDe6smxLZ3F0TDIGvfxfuYAPExmh+mnSnJBBddE7Aj2YN8Rr+XItKZvAisS ew9ea1EhY3NzVhOBsvjWG2QYQteUzF2flDia4AEn9+/CAhVr7jA4hsOeFqMC85scyX EmAen90guE65FtQ3k2XbOhYWiC6rLRaig/bYkgOeC5huZB/9oazM13GAc8Gi8y/XIl OCC853GayvyEOegr4zDVPI3oPFI5q+fIIPp9XDqvBL518SmGzjQLE8/KlDaIprWyRi 82gZPeMHhemOg== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , Quentin Monnet , Alan Maguire Subject: [PATCH RFC bpf-next 08/31] bpf: Add cookie support for kprobe multi inlined functions Date: Wed, 2 Dec 2026 13:30:12 +0100 Message-ID: <20261202123035.10335-9-jolsa@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org> References: <20261202123035.10335-1-jolsa@kernel.org> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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