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 31/31] selftests/bpf: Add kprobe multi inline link info test
Date: Wed,  2 Dec 2026 13:30:35 +0100	[thread overview]
Message-ID: <20261202123035.10335-32-jolsa@kernel.org> (raw)
In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org>

Adding fill_link_info tests for kprobe multi link with inlined
instances attached:

  - kprobe_multi_inline_link_info checks the addresses, cookies and
    BTF ids of the inlined instances and the number of attached and
    skipped instances
  - kprobe_multi_inline_invalid_ubuff checks the errors returned for
    wrong setup of the inline link info fields

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../selftests/bpf/prog_tests/fill_link_info.c | 251 ++++++++++++++++++
 1 file changed, 251 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/fill_link_info.c b/tools/testing/selftests/bpf/prog_tests/fill_link_info.c
index 0918321c8e63..e4ad210b40b8 100644
--- a/tools/testing/selftests/bpf/prog_tests/fill_link_info.c
+++ b/tools/testing/selftests/bpf/prog_tests/fill_link_info.c
@@ -413,6 +413,253 @@ static void test_kprobe_multi_fill_link_info(struct test_fill_link_info *skel,
 	bpf_link__destroy(link);
 }
 
+/*
+ * The first function is attached on its entry, the rest through inlined
+ * instances: outer is inlined in one caller, inner in two and partial has
+ * one instance that is attached and one that can't be attached and is
+ * skipped.
+ */
+static const char *kmulti_inline_syms[] = {
+	"bpf_fentry_test1",
+	"bpf_kprobe_multi_inline_outer",
+	"bpf_kprobe_multi_inline_inner",
+	"bpf_kprobe_multi_inline_partial",
+};
+static __u64 kmulti_inline_cookies[] = { 1, 2, 3, 4 };
+static int kmulti_inline_cnts[] = { 0, 1, 2, 1 };
+#define KMULTI_INLINE_CNT 4
+
+/*
+ * Check the inline.text LOCSEC of @btf has inlined instance of function
+ * with BTF id @id on address @addr.
+ */
+static bool kmulti_inline_has_loc(const struct btf *btf, __u32 id, __u64 addr)
+{
+	/* LOCSEC offsets are relative to the .text section base. */
+	__u64 base = ksym_get_addr("_stext");
+	const struct btf_type *t;
+	struct btf_loc *loc;
+	int i, n, locsec;
+
+	locsec = btf__find_by_name_kind(btf, "inline.text", BTF_KIND_LOCSEC);
+	if (!ASSERT_GT(locsec, 0, "inline_locsec"))
+		return false;
+
+	t = btf__type_by_id(btf, locsec);
+	n = btf_vlen(t);
+	for (i = 0, loc = btf_locsec_locs(t); i < n; i++, loc++) {
+		if (loc->func == id && base + loc->offset == addr)
+			return true;
+	}
+	return false;
+}
+
+static int verify_kmulti_inline_link_info(int fd, const struct btf *btf)
+{
+	__u64 inline_addrs[KMULTI_INLINE_CNT], inline_cookies[KMULTI_INLINE_CNT];
+	int cnts[ARRAY_SIZE(kmulti_inline_syms)] = {}, i, j, err;
+	__u32 inline_ids[KMULTI_INLINE_CNT];
+	const struct btf_type *t;
+	struct bpf_link_info info;
+	__u32 len = sizeof(info);
+	__u64 addr = 0, cookie = 0;
+	struct ksym *ksym;
+	const char *name;
+
+	memset(&info, 0, sizeof(info));
+
+again:
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	if (!ASSERT_OK(err, "get_link_info"))
+		return -1;
+
+	if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_KPROBE_MULTI, "kmulti_type"))
+		return -1;
+
+	ASSERT_EQ(info.kprobe_multi.flags, BPF_F_KPROBE_MULTI_INLINE, "kmulti_flags");
+
+	/* Function with entry probe is not counted in inlined instances. */
+	if (!ASSERT_EQ(info.kprobe_multi.count, 1, "func_cnt") ||
+	    !ASSERT_EQ(info.kprobe_multi.inline_count, KMULTI_INLINE_CNT, "inline_cnt"))
+		return -1;
+
+	/* Instances that could not be attached are counted as skipped. */
+	ASSERT_EQ(info.kprobe_multi.inline_skipped, 1, "inline_skipped");
+
+	if (!info.kprobe_multi.addrs) {
+		info.kprobe_multi.addrs = ptr_to_u64(&addr);
+		info.kprobe_multi.cookies = ptr_to_u64(&cookie);
+		info.kprobe_multi.inline_addrs = ptr_to_u64(inline_addrs);
+		info.kprobe_multi.inline_cookies = ptr_to_u64(inline_cookies);
+		info.kprobe_multi.inline_ids = ptr_to_u64(inline_ids);
+		goto again;
+	}
+
+	ASSERT_EQ(addr, ksym_get_addr(kmulti_inline_syms[0]), "kmulti_addr");
+	ASSERT_EQ(cookie, kmulti_inline_cookies[0], "kmulti_cookie");
+
+	for (i = 0; i < KMULTI_INLINE_CNT; i++) {
+		/* The id is BTF id of the inlined function .. */
+		t = btf__type_by_id(btf, inline_ids[i]);
+		if (!ASSERT_OK_PTR(t, "inline_id"))
+			continue;
+		name = btf__name_by_offset(btf, t->name_off);
+
+		/* .. the instance has cookie of that function .. */
+		for (j = 0; j < ARRAY_SIZE(kmulti_inline_syms); j++) {
+			if (!strcmp(name, kmulti_inline_syms[j]))
+				break;
+		}
+		if (!ASSERT_LT(j, ARRAY_SIZE(kmulti_inline_syms), "inline_func"))
+			continue;
+		ASSERT_EQ(inline_cookies[i], kmulti_inline_cookies[j], "inline_cookie");
+		cnts[j]++;
+
+		/* .. and its address is the one from inline BTF, inside the caller. */
+		ASSERT_TRUE(kmulti_inline_has_loc(btf, inline_ids[i], inline_addrs[i]),
+			    "inline_addr");
+		ksym = ksym_search(inline_addrs[i]);
+		if (!ASSERT_OK_PTR(ksym, "ksym_search"))
+			continue;
+		ASSERT_OK(strncmp(ksym->name, "bpf_kprobe_multi_inline_test", 28), "inline_caller");
+	}
+
+	for (j = 0; j < ARRAY_SIZE(kmulti_inline_syms); j++)
+		ASSERT_EQ(cnts[j], kmulti_inline_cnts[j], "inline_func_cnt");
+	return 0;
+}
+
+static void verify_kmulti_inline_invalid_user_buffer(int fd)
+{
+	__u64 addrs[KMULTI_INLINE_CNT + 1], cookies[KMULTI_INLINE_CNT];
+	__u32 ids[KMULTI_INLINE_CNT];
+	struct bpf_link_info info;
+	__u32 len = sizeof(info);
+	int err, i;
+
+	/* Wrong info setup (inline_addrs == NULL and inline_count != 0) -> EINVAL */
+	memset(&info, 0, sizeof(info));
+	info.kprobe_multi.inline_count = KMULTI_INLINE_CNT;
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -EINVAL, "no_addr");
+
+	/* Wrong info setup (inline_addrs != NULL and inline_count == 0) -> EINVAL */
+	memset(&info, 0, sizeof(info));
+	info.kprobe_multi.inline_addrs = ptr_to_u64(addrs);
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -EINVAL, "no_cnt");
+
+	/* Wrong info setup (inline_cookies != NULL and inline_count == 0) -> EINVAL */
+	memset(&info, 0, sizeof(info));
+	info.kprobe_multi.inline_cookies = ptr_to_u64(cookies);
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -EINVAL, "cookies_no_cnt");
+
+	/* Wrong info setup (inline_ids != NULL and inline_count == 0) -> EINVAL */
+	memset(&info, 0, sizeof(info));
+	info.kprobe_multi.inline_ids = ptr_to_u64(ids);
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -EINVAL, "ids_no_cnt");
+
+	/* Smaller than actual count provided -> ENOSPC */
+	memset(&info, 0, sizeof(info));
+	memset(addrs, 0, sizeof(addrs));
+	info.kprobe_multi.inline_addrs = ptr_to_u64(addrs);
+	info.kprobe_multi.inline_count = KMULTI_INLINE_CNT - 1;
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -ENOSPC, "smaller_cnt");
+	for (i = 0; i < KMULTI_INLINE_CNT - 1; i++)
+		ASSERT_NEQ(addrs[i], 0, "inline_addrs");
+	/* check that the last entry is not populated */
+	ASSERT_EQ(addrs[i], 0, "inline_addrs");
+
+	/* Bigger than actual count provided -> OK */
+	memset(&info, 0, sizeof(info));
+	memset(addrs, 0, sizeof(addrs));
+	info.kprobe_multi.inline_addrs = ptr_to_u64(addrs);
+	info.kprobe_multi.inline_count = KMULTI_INLINE_CNT + 1;
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, 0, "bigger_cnt");
+	ASSERT_EQ(info.kprobe_multi.inline_count, KMULTI_INLINE_CNT, "bigger_cnt_inline_cnt");
+	for (i = 0; i < KMULTI_INLINE_CNT; i++)
+		ASSERT_NEQ(addrs[i], 0, "inline_addrs");
+	ASSERT_EQ(addrs[i], 0, "inline_addrs");
+
+	/* Invalid inline_addrs pointer -> EFAULT */
+	memset(&info, 0, sizeof(info));
+	info.kprobe_multi.inline_addrs = 0x1;
+	info.kprobe_multi.inline_count = KMULTI_INLINE_CNT;
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -EFAULT, "invalid_buff_addrs");
+
+	/* Invalid inline_cookies pointer -> EFAULT */
+	memset(&info, 0, sizeof(info));
+	info.kprobe_multi.inline_addrs = ptr_to_u64(addrs);
+	info.kprobe_multi.inline_cookies = 0x1;
+	info.kprobe_multi.inline_count = KMULTI_INLINE_CNT;
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -EFAULT, "invalid_buff_cookies");
+
+	/* Invalid inline_ids pointer -> EFAULT */
+	memset(&info, 0, sizeof(info));
+	info.kprobe_multi.inline_addrs = ptr_to_u64(addrs);
+	info.kprobe_multi.inline_ids = 0x1;
+	info.kprobe_multi.inline_count = KMULTI_INLINE_CNT;
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	ASSERT_EQ(err, -EFAULT, "invalid_buff_ids");
+}
+
+static void test_kprobe_multi_inline_fill_link_info(struct test_fill_link_info *skel,
+						    bool invalid)
+{
+	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
+	struct btf *vmlinux_btf, *inline_btf = NULL;
+	struct bpf_link *link;
+	int link_fd, err;
+
+	/* Attaching to inlined functions is supported only on x86_64. */
+#ifndef __x86_64__
+	test__skip();
+	return;
+#endif
+
+	vmlinux_btf = btf__load_vmlinux_btf();
+	if (!ASSERT_OK_PTR(vmlinux_btf, "vmlinux_btf"))
+		return;
+
+	inline_btf = btf__parse_split("/sys/kernel/btf/vmlinux.inline", vmlinux_btf);
+	err = libbpf_get_error(inline_btf);
+	if (err) {
+		inline_btf = NULL;
+		/* pahole may not have inline BTF feature support. */
+		if (err == -ENOENT)
+			test__skip();
+		else
+			ASSERT_OK(err, "parse_inline_btf");
+		goto out;
+	}
+
+	opts.syms = kmulti_inline_syms;
+	opts.cookies = kmulti_inline_cookies;
+	opts.cnt = ARRAY_SIZE(kmulti_inline_syms);
+	opts.inlined = true;
+	link = bpf_program__attach_kprobe_multi_opts(skel->progs.kmulti_run, NULL, &opts);
+	if (!ASSERT_OK_PTR(link, "attach_kprobe_multi_inline"))
+		goto out;
+
+	link_fd = bpf_link__fd(link);
+	if (!invalid) {
+		err = verify_kmulti_inline_link_info(link_fd, inline_btf);
+		ASSERT_OK(err, "verify_kmulti_inline_link_info");
+	} else {
+		verify_kmulti_inline_invalid_user_buffer(link_fd);
+	}
+	bpf_link__destroy(link);
+out:
+	btf__free(inline_btf);
+	btf__free(vmlinux_btf);
+}
+
 static int tmulti_target_cmp(const void *a, const void *b)
 {
 	const struct tmulti_target *ta = a;
@@ -874,6 +1121,10 @@ void test_fill_link_info(void)
 	}
 	if (test__start_subtest("kprobe_multi_invalid_ubuff"))
 		test_kprobe_multi_fill_link_info(skel, true, true, true);
+	if (test__start_subtest("kprobe_multi_inline_link_info"))
+		test_kprobe_multi_inline_fill_link_info(skel, false);
+	if (test__start_subtest("kprobe_multi_inline_invalid_ubuff"))
+		test_kprobe_multi_inline_fill_link_info(skel, true);
 
 	if (test__start_subtest("tracing_multi_link_info")) {
 		test_tracing_multi_fill_link_info(skel, false, false);
-- 
2.56.1


      parent reply	other threads:[~2026-12-02 12:35 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 ` [PATCH RFC bpf-next 23/31] selftests/bpf: Add kprobe multi inline attach test Jiri Olsa
2026-12-02 12:50   ` 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 ` Jiri Olsa [this message]

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-32-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.