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

Add inline_fails test for attach failures of kprobe multi link with
BPF_F_KPROBE_MULTI_INLINE flag: used with addrs, with return probe,
with session program, with wildcard pattern and for function that
has nothing to attach.

Most of the setups are rejected by libbpf already, so they are
tested also through bpf_link_create to check the kernel error and
the message in the link_create log.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../bpf/prog_tests/kprobe_multi_test.c        | 168 ++++++++++++++++++
 .../selftests/bpf/progs/kprobe_multi_inline.c |   6 +
 2 files changed, 174 insertions(+)

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 f6556bd9f2d0..3f5d5b5198e6 100644
--- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
@@ -1049,6 +1049,172 @@ static void test_inline_skip(void)
 	kprobe_multi_inline__destroy(skel);
 }
 
+/*
+ * Make sure the link create fails with @error and kernel explains
+ * the failure with @msg in the log.
+ */
+static bool inline_link_fails(int prog_fd, enum bpf_attach_type type,
+			      struct bpf_link_create_opts *lopts,
+			      int error, const char *msg)
+{
+	LIBBPF_OPTS(bpf_log_opts, log_opts);
+	char log_buf[4096] = {};
+	int saved_error, link_fd;
+
+	log_opts.buf = log_buf;
+	log_opts.size = sizeof(log_buf);
+	log_opts.level = 1;
+	lopts->log_opts = &log_opts;
+
+	link_fd = bpf_link_create(prog_fd, 0, type, lopts);
+	saved_error = -errno;
+	if (!ASSERT_LT(link_fd, 0, "link_create")) {
+		close(link_fd);
+		return false;
+	}
+	ASSERT_EQ(saved_error, error, "link_create_error");
+	ASSERT_HAS_SUBSTR(log_buf, msg, "link_create_log");
+	return true;
+}
+
+/*
+ * Most of the invalid inline setups are rejected by libbpf before
+ * it gets to the kernel, so each setup is tested twice:
+ *
+ *   - through bpf_program__attach_kprobe_multi_opts to test the libbpf check,
+ *   - through bpf_link_create (inline_link_fails) to test the kernel check
+ *     together with the failure message in the kernel log, which libbpf
+ *     does not pass to the caller.
+ */
+static void test_inline_fails(void)
+{
+	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
+	LIBBPF_OPTS(bpf_link_create_opts, lopts);
+	struct kprobe_multi_inline *skel = NULL;
+	const char *syms[1] = { "bpf_kprobe_multi_inline_inner" };
+	const char *nosyms[1] = { "bpf_kprobe_multi_inline_nonexistent" };
+	const char *nokprobe[1] = { "bpf_kprobe_multi_inline_nokprobe" };
+	unsigned long inner[INLINE_MAX], addrs[1];
+	int saved_error, prog_fd;
+
+	if (get_inline_sites(syms[0], inner, INLINE_MAX) == -ENOENT) {
+		test__skip();
+		return;
+	}
+
+	addrs[0] = ksym_get_addr("bpf_fentry_test1");
+	if (!ASSERT_NEQ(addrs[0], 0, "bpf_fentry_test1_addr"))
+		return;
+
+	skel = kprobe_multi_inline__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "kprobe_multi_inline__open_and_load"))
+		return;
+
+	prog_fd = bpf_program__fd(skel->progs.test_kprobe_inline);
+
+	/* fail_1 - inlined with addrs */
+	opts.addrs = addrs;
+	opts.cnt = 1;
+	opts.inlined = true;
+
+	skel->links.test_kprobe_inline =
+		bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_inline, NULL, &opts);
+	saved_error = -errno;
+	if (!ASSERT_ERR_PTR(skel->links.test_kprobe_inline, "fail_1"))
+		goto cleanup;
+	ASSERT_EQ(saved_error, -EINVAL, "fail_1_error");
+
+	lopts.kprobe_multi.addrs = addrs;
+	lopts.kprobe_multi.cnt = 1;
+	lopts.kprobe_multi.flags = BPF_F_KPROBE_MULTI_INLINE;
+
+	if (!inline_link_fails(prog_fd, BPF_TRACE_KPROBE_MULTI, &lopts, -EINVAL,
+			       "Inlined functions need to be specified with kprobe_multi.syms."))
+		goto cleanup;
+
+	/* fail_2 - inlined with return probe */
+	LIBBPF_OPTS_RESET(opts,
+		.syms = syms,
+		.cnt = 1,
+		.inlined = true,
+		.retprobe = true,
+	);
+
+	skel->links.test_kprobe_inline =
+		bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_inline, NULL, &opts);
+	saved_error = -errno;
+	if (!ASSERT_ERR_PTR(skel->links.test_kprobe_inline, "fail_2"))
+		goto cleanup;
+	ASSERT_EQ(saved_error, -EINVAL, "fail_2_error");
+
+	LIBBPF_OPTS_RESET(lopts,
+		.kprobe_multi.syms = syms,
+		.kprobe_multi.cnt = 1,
+		.kprobe_multi.flags = BPF_F_KPROBE_MULTI_INLINE | BPF_F_KPROBE_MULTI_RETURN,
+	);
+
+	if (!inline_link_fails(prog_fd, BPF_TRACE_KPROBE_MULTI, &lopts, -EINVAL,
+			       "Inlined functions can't be attached with return probe."))
+		goto cleanup;
+
+	/* fail_3 - inlined with session program */
+	lopts.kprobe_multi.flags = BPF_F_KPROBE_MULTI_INLINE;
+
+	if (!inline_link_fails(bpf_program__fd(skel->progs.test_session_inline),
+			       BPF_TRACE_KPROBE_SESSION, &lopts, -EINVAL,
+			       "Inlined functions can't be attached with session or override "
+			       "program."))
+		goto cleanup;
+
+	/* fail_4 - inlined with wildcard pattern */
+	LIBBPF_OPTS_RESET(opts,
+		.inlined = true,
+	);
+
+	skel->links.test_kprobe_inline =
+		bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_inline,
+						      "bpf_kprobe_multi_inline_*", &opts);
+	saved_error = -errno;
+	if (!ASSERT_ERR_PTR(skel->links.test_kprobe_inline, "fail_4"))
+		goto cleanup;
+	ASSERT_EQ(saved_error, -EINVAL, "fail_4_error");
+
+	/* fail_5 - function with no entry and no inlined instance */
+	LIBBPF_OPTS_RESET(opts,
+		.syms = nosyms,
+		.cnt = 1,
+		.inlined = true,
+	);
+
+	skel->links.test_kprobe_inline =
+		bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_inline, NULL, &opts);
+	saved_error = -errno;
+	if (!ASSERT_ERR_PTR(skel->links.test_kprobe_inline, "fail_5"))
+		goto cleanup;
+	ASSERT_EQ(saved_error, -ENOENT, "fail_5_error");
+
+	LIBBPF_OPTS_RESET(lopts,
+		.kprobe_multi.syms = nosyms,
+		.kprobe_multi.cnt = 1,
+		.kprobe_multi.flags = BPF_F_KPROBE_MULTI_INLINE,
+	);
+
+	if (!inline_link_fails(prog_fd, BPF_TRACE_KPROBE_MULTI, &lopts, -ESRCH,
+			       "Function bpf_kprobe_multi_inline_nonexistent has no entry and "
+			       "no inlined instance."))
+		goto cleanup;
+
+	/* fail_6 - function with no entry and no inlined instance that can be attached */
+	lopts.kprobe_multi.syms = nokprobe;
+
+	inline_link_fails(prog_fd, BPF_TRACE_KPROBE_MULTI, &lopts, -EINVAL,
+			  "Function bpf_kprobe_multi_inline_nokprobe has no entry and none of its "
+			  "inlined instances can be attached, bpf_kprobe_multi_inline_test4+");
+
+cleanup:
+	kprobe_multi_inline__destroy(skel);
+}
+
 /* Run all the tests for attaching to inlined functions. */
 static void test_inline_subtests(void)
 {
@@ -1063,6 +1229,8 @@ static void test_inline_subtests(void)
 		test_inline_mixed();
 	if (test__start_subtest("inline_skip"))
 		test_inline_skip();
+	if (test__start_subtest("inline_fails"))
+		test_inline_fails();
 
 	free_inline_btf();
 }
diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_inline.c b/tools/testing/selftests/bpf/progs/kprobe_multi_inline.c
index f23f79f540fd..f2b428292544 100644
--- a/tools/testing/selftests/bpf/progs/kprobe_multi_inline.c
+++ b/tools/testing/selftests/bpf/progs/kprobe_multi_inline.c
@@ -39,3 +39,9 @@ int test_kprobe_inline(struct pt_regs *ctx)
 	hits_cnt = idx + 1;
 	return 0;
 }
+
+SEC("kprobe.session")
+int test_session_inline(struct pt_regs *ctx)
+{
+	return 0;
+}
-- 
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 ` Jiri Olsa [this message]
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 ` [PATCH RFC bpf-next 31/31] selftests/bpf: Add kprobe multi inline link info test Jiri Olsa

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-27-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox