All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	linux-kernel@vger.kernel.org, x86@kernel.org,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Quentin Monnet <qmo@kernel.org>,
	Alan Maguire <almagui@kernel.org>
Subject: [PATCH RFC bpf-next 23/31] selftests/bpf: Add kprobe multi inline attach test
Date: Wed,  2 Dec 2026 13:30:27 +0100	[thread overview]
Message-ID: <20261202123035.10335-24-jolsa@kernel.org> (raw)
In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org>

Add inline_attach test that attaches inlined functions with
BPF_F_KPROBE_MULTI_INLINE flag and checks each inlined instance is
hit once with its function cookie, including instances sharing the
same address, and that the probes are gone after detach.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../bpf/prog_tests/kprobe_multi_test.c        | 213 ++++++++++++++++++
 .../selftests/bpf/progs/kprobe_multi_inline.c |  41 ++++
 2 files changed, 254 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_inline.c

diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
index 651123cd8602..e54db60427c3 100644
--- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
@@ -11,6 +11,7 @@
 #include "kprobe_multi_verifier.skel.h"
 #include "kprobe_write_ctx.skel.h"
 #include "kprobe_multi_sleepable.skel.h"
+#include "kprobe_multi_inline.skel.h"
 #include "bpf/libbpf_internal.h"
 #include "bpf/hashmap.h"
 
@@ -536,6 +537,16 @@ static void test_kprobe_multi_bench_attach(bool kernel)
 	free_kallsyms_local(ksyms);
 }
 
+/* Attaching to inlined functions is supported only on x86_64. */
+static bool inline_attach_supported(void)
+{
+#ifdef __x86_64__
+	return true;
+#else
+	return false;
+#endif
+}
+
 static void test_kprobe_multi_bench_attach_addr(bool kernel)
 {
 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
@@ -703,6 +714,207 @@ static void test_attach_probe_dup_sym(void)
 	kprobe_multi__destroy(skel);
 }
 
+#define INLINE_MAX	8
+
+/*
+ * Get addresses of inlined instances of @func from inline.text LOCSEC
+ * of @inline_btf, the offsets are relative to @base. Returns number of
+ * instances or -ENOENT if there's no such LOCSEC.
+ */
+static int find_inline_sites(struct btf *inline_btf, unsigned long base, const char *func,
+			     unsigned long *addrs, int max)
+{
+	const struct btf_type *t, *ft;
+	struct btf_loc *l;
+	int id, i, n, cnt = 0;
+
+	id = btf__find_by_name_kind(inline_btf, "inline.text", BTF_KIND_LOCSEC);
+	if (id < 0)
+		return -ENOENT;
+
+	t = btf__type_by_id(inline_btf, id);
+	n = btf_vlen(t);
+	for (i = 0, l = btf_locsec_locs(t); i < n; i++, l++) {
+		ft = btf__type_by_id(inline_btf, l->func);
+		if (!ft || strcmp(btf__name_by_offset(inline_btf, ft->name_off), func))
+			continue;
+		if (!ASSERT_LT(cnt, max, "inline_sites_max"))
+			return -EINVAL;
+		addrs[cnt++] = base + l->offset;
+	}
+	return cnt;
+}
+
+/* BTF used to find inlined instances, loaded once on first use. */
+static struct btf *vmlinux_btf;
+static struct btf *vmlinux_inline_btf;
+
+static struct btf *get_vmlinux_btf(void)
+{
+	if (!vmlinux_btf)
+		vmlinux_btf = btf__load_vmlinux_btf();
+	return vmlinux_btf;
+}
+
+/*
+ * Get split BTF from @path on top of @base, it's parsed just once and
+ * stored in @cache. Returns 0 on success or negative error.
+ */
+static int get_split_btf(struct btf **cache, const char *path, struct btf *base)
+{
+	struct btf *btf;
+	int err;
+
+	if (*cache)
+		return 0;
+
+	btf = btf__parse_split(path, base);
+	err = libbpf_get_error(btf);
+	if (err)
+		return err;
+	*cache = btf;
+	return 0;
+}
+
+static void free_inline_btf(void)
+{
+	btf__free(vmlinux_inline_btf);
+	btf__free(vmlinux_btf);
+	vmlinux_inline_btf = vmlinux_btf = NULL;
+}
+
+/*
+ * Get addresses of inlined instances of @func from vmlinux inline BTF.
+ * Returns number of instances or -ENOENT if there's no inline BTF.
+ */
+static int get_inline_sites(const char *func, unsigned long *addrs, int max)
+{
+	static unsigned long base;
+	int err;
+
+	/* LOCSEC offsets are relative to the .text section base. */
+	if (!base)
+		base = ksym_get_addr("_stext");
+	if (!ASSERT_NEQ(base, 0, "stext_addr"))
+		return -EINVAL;
+
+	if (!ASSERT_OK_PTR(get_vmlinux_btf(), "vmlinux_btf"))
+		return -EINVAL;
+
+	err = get_split_btf(&vmlinux_inline_btf, "/sys/kernel/btf/vmlinux.inline", vmlinux_btf);
+	if (err) {
+		/* pahole may not have inline BTF feature support. */
+		if (err != -ENOENT)
+			ASSERT_OK(err, "parse_inline_btf");
+		return err;
+	}
+
+	return find_inline_sites(vmlinux_inline_btf, base, func, addrs, max);
+}
+
+static int inline_hits(struct kprobe_multi_inline *skel, unsigned long ip, __u64 cookie)
+{
+	int i, cnt = 0;
+
+	for (i = 0; i < skel->bss->hits_cnt; i++) {
+		if (skel->bss->hits_ip[i] == ip && skel->bss->hits_cookie[i] == cookie)
+			cnt++;
+	}
+	return cnt;
+}
+
+static void inline_trigger(struct kprobe_multi_inline *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err;
+
+	skel->bss->hits_cnt = 0;
+	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.trigger), &topts);
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(topts.retval, 0, "test_run");
+}
+
+/* Each inlined instance must be hit just once with its function cookie. */
+static void inline_check_sites(struct kprobe_multi_inline *skel, unsigned long *addrs,
+			       int cnt, __u64 cookie)
+{
+	int i;
+
+	for (i = 0; i < cnt; i++)
+		ASSERT_EQ(inline_hits(skel, addrs[i], cookie), 1, "inline_site_hit");
+}
+
+static void test_inline_attach(void)
+{
+	const char *syms[2] = { "bpf_kprobe_multi_inline_outer",
+				"bpf_kprobe_multi_inline_inner" };
+	unsigned long inner[INLINE_MAX], outer[INLINE_MAX];
+	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
+	struct kprobe_multi_inline *skel = NULL;
+	__u64 cookies[2] = { 2, 1 };
+	int inner_cnt, outer_cnt;
+
+	inner_cnt = get_inline_sites(syms[1], inner, INLINE_MAX);
+	outer_cnt = get_inline_sites(syms[0], outer, INLINE_MAX);
+	if (inner_cnt == -ENOENT || outer_cnt == -ENOENT) {
+		test__skip();
+		return;
+	}
+
+	/* inner is inlined in both callers, outer just in the first one */
+	if (!ASSERT_EQ(inner_cnt, 2, "inner_cnt") || !ASSERT_EQ(outer_cnt, 1, "outer_cnt"))
+		return;
+
+	skel = kprobe_multi_inline__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "kprobe_multi_inline__open_and_load"))
+		return;
+
+	skel->bss->pid = getpid();
+
+	opts.syms = syms;
+	opts.cookies = cookies;
+	opts.cnt = ARRAY_SIZE(syms);
+	opts.inlined = true;
+
+	skel->links.test_kprobe_inline =
+		bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_inline, NULL, &opts);
+	if (!ASSERT_OK_PTR(skel->links.test_kprobe_inline, "attach_kprobe_multi_opts"))
+		goto cleanup;
+
+	inline_trigger(skel);
+
+	/*
+	 * The outer instance is usually on the same address as one of
+	 * the inner instances, each of them needs to get its own cookie.
+	 */
+	ASSERT_EQ(skel->bss->hits_cnt, inner_cnt + outer_cnt, "hits_cnt");
+	inline_check_sites(skel, inner, inner_cnt, 1);
+	inline_check_sites(skel, outer, outer_cnt, 2);
+
+	/* Make sure the probes are gone after detach. */
+	kprobe_multi_inline__detach(skel);
+
+	inline_trigger(skel);
+	ASSERT_EQ(skel->bss->hits_cnt, 0, "hits_cnt_detached");
+
+cleanup:
+	kprobe_multi_inline__destroy(skel);
+}
+
+/* Run all the tests for attaching to inlined functions. */
+static void test_inline_subtests(void)
+{
+	if (!inline_attach_supported()) {
+		test__skip();
+		return;
+	}
+
+	if (test__start_subtest("inline_attach"))
+		test_inline_attach();
+
+	free_inline_btf();
+}
+
 void serial_test_kprobe_multi_bench_attach(void)
 {
 	if (test__start_subtest("kernel"))
@@ -748,5 +960,6 @@ void test_kprobe_multi_test(void)
 		test_attach_write_ctx();
 	if (test__start_subtest("dup_sym"))
 		test_attach_probe_dup_sym();
+	test_inline_subtests();
 	RUN_TESTS(kprobe_multi_verifier);
 }
diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_inline.c b/tools/testing/selftests/bpf/progs/kprobe_multi_inline.c
new file mode 100644
index 000000000000..f23f79f540fd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/kprobe_multi_inline.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define MAX_HITS 16
+
+int pid = 0;
+
+__u64 hits_ip[MAX_HITS];
+__u64 hits_cookie[MAX_HITS];
+__u32 hits_cnt = 0;
+
+/*
+ * No tests in here, just to trigger 'bpf_kprobe_multi_inline_test*'
+ * through tracing test_run
+ */
+SEC("fentry/bpf_modify_return_test")
+int BPF_PROG(trigger)
+{
+	return 0;
+}
+
+SEC("kprobe.multi")
+int test_kprobe_inline(struct pt_regs *ctx)
+{
+	__u32 idx = hits_cnt;
+
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 0;
+
+	if (idx >= MAX_HITS)
+		return 0;
+
+	hits_ip[idx] = bpf_get_func_ip(ctx);
+	hits_cookie[idx] = bpf_get_attach_cookie(ctx);
+	hits_cnt = idx + 1;
+	return 0;
+}
-- 
2.56.1


  parent reply	other threads:[~2026-12-02 12:34 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:30 [PATCH RFC bpf-next 00/31] bpf: Attach kprobe multi link to inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 01/31] x86/ptrace: Add regs_get_dwarf_register function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 02/31] kprobes: Make the kprobe hash table size configurable Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 03/31] kprobes: Add enable_kprobes to enable multiple kprobes at once Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 04/31] module: Add module_sect_addr function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 05/31] bpf: Add bpf_get_btf_vmlinux_inline function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 06/31] bpf: Use goto labels for error path in bpf_kprobe_multi_link_attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 07/31] bpf: Add support to attach inlined functions with kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 08/31] bpf: Add cookie support for kprobe multi inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 09/31] bpf: Add link info " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 10/31] bpf: Add fdinfo " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 11/31] bpf: Add btf_for_each_module_inline function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 12/31] bpf: Change get_modules_for_addrs to use struct modules_array Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 13/31] tbpf: Add support to attach inlined functions in modules with kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 14/31] bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 15/31] bpf: Add get_func_arg[_cnt] support for kprobe multi functions Jiri Olsa
2026-12-02 13:06   ` sashiko-bot
2026-12-02 12:30 ` [PATCH RFC bpf-next 16/31] bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions Jiri Olsa
2026-12-02 12:55   ` sashiko-bot
2026-12-02 12:30 ` [PATCH RFC bpf-next 17/31] bpf: Add log messages for kprobe multi inline attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 18/31] bpf: Add inlined functions for kprobe multi inline tests Jiri Olsa
2026-12-02 12:51   ` sashiko-bot
2026-12-02 12:30 ` [PATCH RFC bpf-next 19/31] libbpf: Add inline options to bpf_kprobe_multi_opts Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 20/31] libbpf: Print kernel log for failed kprobe multi attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 21/31] bpftool: Display inlined functions for kprobe multi link Jiri Olsa
2026-12-02 13:00   ` sashiko-bot
2026-12-02 12:30 ` [PATCH RFC bpf-next 22/31] selftests/bpf: Add inlined functions to bpf_testmod Jiri Olsa
2026-12-02 12:30 ` Jiri Olsa [this message]
2026-12-02 12:50   ` [PATCH RFC bpf-next 23/31] selftests/bpf: Add kprobe multi inline attach test sashiko-bot
2026-12-02 12:30 ` [PATCH RFC bpf-next 24/31] selftests/bpf: Add kprobe multi inline mixed test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 25/31] selftests/bpf: Add kprobe multi inline skip test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 26/31] selftests/bpf: Add kprobe multi inline fails test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 27/31] selftests/bpf: Add kprobe multi inline module test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 28/31] selftests/bpf: Add kprobe multi inline strict test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 29/31] selftests/bpf: Add kprobe multi inline args test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 30/31] selftests/bpf: Add kprobe multi inline bench attach test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 31/31] selftests/bpf: Add kprobe multi inline link info test Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202123035.10335-24-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=almagui@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=qmo@kernel.org \
    --cc=song@kernel.org \
    --cc=x86@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.