[PATCH RFC bpf-next 23/31] selftests/bpf: Add kprobe multi inline attach test

From: Jiri Olsa

Date: Wed Dec 02 2026 - 07:34:42 EST


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@xxxxxxxxxx>
---
.../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