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=1796214907; b=foEcK5IIZg36WaDWMnYY7ODHyB+Ly01mcVR2Z/s+XSn+g7Z/u9tVxempzElmILtXXXxB OwyqrARbZAl5ClIiO4enQJsYkPrevCOCQ1AgHTIqBOMgRoF8J14Q+5pzZPrDK/+1OpbUE WlQQ76UTOfFoSzNplcX4U2OSVy62Vx1bS1V1YE7uUMDnZ9DhHhtzgmVSNAMg70HErmUT2 WuQckR/dtbigbrAaunk3ZdZ62Mrpylj0W/4LhYpMEFobDX1HQp9HSJ0fF+ZLrd/YJx4WZ p2xGtR0190sTwlCs1qbo4BYa3pGRTjAu3YYq0CNEWXQX8R30FvRqDxeJlzUeNJ7WEwA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214907; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=B7S1nBz1W0gv0cTdrLW+nlYO41V0pUcxbUNzAUpDwnQ=; b=QAP4PEjrTVmZOjWopdaUT9Q9vOTikC/xK2c/LHhcUrGZYSjU1YpkAqhGHEVEdgWC15s9 pDteB/fLjXQfmebnmjEAzeVqlJzflc/ETqKIf4JjydsgmtrmKIyIm/5zpFfeGwZZt/Pw5 jmgLUgiUlLD/xpfXJGj36zeBGdTbYCYOg2hjG/rbia5i1iH0RJxEaMw3zqTs8HTWjFaHc mPpVuGtdEgDAq2IytinHOWwrkQjg2KJgsJJ+RqfBvMqbsS4beM1Nt0B6DTjoFSO57PiJx qMQY1v2D4Pjo3CbjiEI5EQL94KQ0IGovmZsELj9vYXGsh/N3lKhLRc8xtI5JmxS/IEQ== 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=QwknX5qB; 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 91602606A31A; Wed, 2 Dec 2026 12:35:07 +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=QwknX5qB Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3F641F000C1; Wed, 2 Dec 2026 12:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214907; bh=B7S1nBz1W0gv0cTdrLW+nlYO41V0pUcxbUNzAUpDwnQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QwknX5qBIC9iSYzCUkVLAHG4qVQb4i8Umd5hCEYDe6aM2nYh8fgQFiZaysfrwuVM3 W57gl+jc9a5KYL4urGHd+IJZVp3+cohoYVoFIioR3p5iYuSIX1O2EjccXhjkPdhXrc v5/Fjg1WE3Agi1wnLJTMW9zXWnWtk0o9UFnNFjSu4YRiFSECuw1ASGr1wjhFbxJEz6 k0i0OGkrd/hUMLNW3Eu9ijKKKEVPa7LEEQOy/bd5Ws9KxsfvSl/SoIdVsuzLAHopHT p22WREeYiOmtp5mPCjON5RlCQ/KyHZ+ucV/MlZ9lf0gGkRvoi13AtKN2qrVj9gVt8l y39PJPgp3/UYw== 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 26/31] selftests/bpf: Add kprobe multi inline fails test Date: Wed, 2 Dec 2026 13:30:30 +0100 Message-ID: <20261202123035.10335-27-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: bpf@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_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 --- .../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