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=1796214878; b=fctYm+kF6Xk2dTqnyJSbAetW16mvtBDVb6WfJtqaBwwWlnVpQssZx2yHck1SNS4RuaAx qhvPQgpQgvEFGrTcwCi1ukDA8LwEa6ev1G/Qoo1orLWd0o/HNgbE3iz/0XUT8Zj9Zi+9x I6Ow3WitBIEwXg32Fx9hRuofz8D8J4g6aiRTNkEdV3xE+1dkE3kM+4eTS0f/ula8blBpu +6lqF05b0LzZmTS0TLsO1SWubkoRO76z/6VDA1KFjjCSezSSWb4SMyL+zdSDoQ6Rsc1YN XwnbpyGE3Y1lKoyLa7sBRhKaiGOWJA35JyO/0rn/beGH8OF4lvDxpT9bLHtz6AmNkKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214878; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=ZAAbRq6OvqWMZp49kw94s9M4HLheMemrnmFHHnxMkAA=; b=TLMYr00Xv7UbjZHeuF6tLAL2yMUW8KVD1wn7GEYHjqs2N2RDXjP8Lvtdh6uS7mbIOhqp DgV297aJd9Jw/sjxjDgxFYG66N8+wCASFJO73BrsEYo/ecNddCC7i99BXaiGFAPhzDMOb yGLEIzd2VDF8zsYnmnialkvdVO03fqDqOkqd7AF1OiVWgr4Yl7abukdb5CvC81zY8OOQQ +KKQVk5nF7jS4a1mUIBN7r084pNZouxl7gxkBDvI676FR1DNx95CoNq+X4uYFJrSaVeoA XdcRVEBxbrgNJqbA0lmr6TAoV8fImnRImQfHTJKqcIdmvgDYill7Cu2QiGAQkljzlNQ== 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=CUvwPLne; 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 C1263606A322; Wed, 2 Dec 2026 12:34:38 +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=CUvwPLne Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D0681F0089B; Wed, 2 Dec 2026 12:34:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214878; bh=ZAAbRq6OvqWMZp49kw94s9M4HLheMemrnmFHHnxMkAA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CUvwPLneaFP7JBIfhKTACejVA1YIpPRSJe+NEUrgwxI07Pzv6L6JScR/7g8iSKi+V K8WSmO99KQcHtJjJM5SNY7qFT+9RG70AzYTDJQevFqKz8RI44Pw7OJH4l29RrJIrlv 0kdYXWpyyvdFHAwL7UWutbU6WxJ5wCcuJb/G19HzXAC6FCxTuBdGrit3sogIcGQiZI emReyCg5+cj8Z4B5A56Hf8F3tmZb/lpllzaQimqzuWbXMPNaqyIknPJwvup3L9XRy1 bYnwbLMvUpwARtcAipxDqRR+tY8rQ3sWB2rKOeZ6mJVJqalaRs2Gw35MNAFs2rBwbN OVjof/JFXYCNw== 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 23/31] selftests/bpf: Add kprobe multi inline attach test Date: Wed, 2 Dec 2026 13:30:27 +0100 Message-ID: <20261202123035.10335-24-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: linux-kernel@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 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 --- .../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 +#include +#include + +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