[PATCH RFC bpf-next 31/31] selftests/bpf: Add kprobe multi inline link info test
From: Jiri Olsa
Date: Wed Dec 02 2026 - 07:36:02 EST
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@xxxxxxxxxx>
---
.../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