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=1796214957; b=TBfHs08MWPibXD88WHbNiawLT61t6yDL5VqonTyh46JoflUc7lHt/pAS4Rw5rI3cQVWH Gn4HaytBNnGm968TMVP30OrwVrS3MyCahKLKjXjs1xzNMZ1jPzdw4DUxliTVwhqWEKLJg 3Zzy/Fdlvy1BFx6bc2+c6ZN6Dv4CxWz1i+jFNtJ46xYtOk7YPSzKotPuF0npGVnQG/gnS 3lOdCOHtB8kpswr3ExozTD4xMfWTN8p0yOKgUBSh8mI5nXv4klo+qNUBGIuxQ+vimNgJh 7ToUbwMMseQYKTOvlKWjASr2MIaY8WbB2gnxrswYNsx0HqWtGiGxEsl6Ix+sC4li2rA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214957; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=iSjc4yyMMD6RE+2Bq+wAq2A0IDIYSiBH7hxgIbQhGeo=; b=WHZ6aeekGjvCrVHBDHbDi+uIngRx6AHbqMBC81v3AxiBpr3vdvKtzvyc8tQ2WAkUWRVl b1mUEeEn6dT6BG4TawuyfaPLzanwMXsjfsLxlIjSlytJZVGhlvyOfvI+lQkc6dAgZL62G 1R/uCFT57Kn7y2O4qJSUC7y2XHbfGaO0eNGXINcj2yWIZ5/zTNy/EkhEebCp19Y/cDWAd 9hWORZpTIgzkosE5fgfofoBHSxC5sJ0981pJFsYNcAeOA4S1OQdv08ZOFrxAJ3bKsxRYB IDPsLN/rPKp06QMXr8vARfV3/x0mKxHHGfwpaPeFpvE7gqMjzoXzqbW5lfbMicPBkog== 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=VmnANDlc; 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 7B209606A31A; Wed, 2 Dec 2026 12:35:57 +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=VmnANDlc Received: by smtp.kernel.org (Postfix) with ESMTPSA id 673F21F000C1; Wed, 2 Dec 2026 12:35:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214957; bh=iSjc4yyMMD6RE+2Bq+wAq2A0IDIYSiBH7hxgIbQhGeo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VmnANDlceJDwlVOiOkHYDmYYEF6+oInrzSNd2/patmcglP4A+Reo9yJJMRFCBzqlj lMu7E4bKtM1suw/fpLoIGlcj9BkPt6XaOwCFKbDiBkuus8Nk9Cg3bBmGW8qn8DjpMS XmurrZAykCpixGt6LGyv+GrdsDJCzoBQmq2zTb5ekVMuVTP1Nz8huRHmqirIeLIDBJ /uFqGG0V7aN/YI7fatHRhA4/5Z57rW1CAfUc0DOJMS+NMHRUZ/jZNjDCqcGeIuiLBg UzC9YufqYAX9Gqn7Viks+b9+OqZmWN2ibKu9p6Tw6GvpqBwGpfVMgECPAXMpJz4uqV qGnika+hH/eiA== 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 31/31] selftests/bpf: Add kprobe multi inline link info test Date: Wed, 2 Dec 2026 13:30:35 +0100 Message-ID: <20261202123035.10335-32-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 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 --- .../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