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>
Cc: bpf@vger.kernel.org, Martin KaFai Lau <kafai@fb.com>,
	Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@chromium.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>
Subject: [PATCHv4 bpf-next 19/28] selftests/bpf: Add uprobe_multi skel test
Date: Thu, 20 Jul 2023 13:35:41 +0200	[thread overview]
Message-ID: <20230720113550.369257-20-jolsa@kernel.org> (raw)
In-Reply-To: <20230720113550.369257-1-jolsa@kernel.org>

Adding uprobe_multi test for skeleton load/attach functions,
to test skeleton auto attach for uprobe_multi link.

Test that bpf_get_func_ip works properly for uprobe_multi
attachment.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../bpf/prog_tests/uprobe_multi_test.c        | 76 ++++++++++++++++
 .../selftests/bpf/progs/uprobe_multi.c        | 91 +++++++++++++++++++
 2 files changed, 167 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi.c

diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
new file mode 100644
index 000000000000..5cd1116bbb62
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <unistd.h>
+#include <test_progs.h>
+#include "uprobe_multi.skel.h"
+
+static char test_data[] = "test_data";
+
+noinline void uprobe_multi_func_1(void)
+{
+	asm volatile ("");
+}
+
+noinline void uprobe_multi_func_2(void)
+{
+	asm volatile ("");
+}
+
+noinline void uprobe_multi_func_3(void)
+{
+	asm volatile ("");
+}
+
+static void uprobe_multi_test_run(struct uprobe_multi *skel)
+{
+	skel->bss->uprobe_multi_func_1_addr = (__u64) uprobe_multi_func_1;
+	skel->bss->uprobe_multi_func_2_addr = (__u64) uprobe_multi_func_2;
+	skel->bss->uprobe_multi_func_3_addr = (__u64) uprobe_multi_func_3;
+
+	skel->bss->user_ptr = test_data;
+	skel->bss->pid = getpid();
+
+	/* trigger all probes */
+	uprobe_multi_func_1();
+	uprobe_multi_func_2();
+	uprobe_multi_func_3();
+
+	/*
+	 * There are 2 entry and 2 exit probe called for each uprobe_multi_func_[123]
+	 * function and each slepable probe (6) increments uprobe_multi_sleep_result.
+	 */
+	ASSERT_EQ(skel->bss->uprobe_multi_func_1_result, 2, "uprobe_multi_func_1_result");
+	ASSERT_EQ(skel->bss->uprobe_multi_func_2_result, 2, "uprobe_multi_func_2_result");
+	ASSERT_EQ(skel->bss->uprobe_multi_func_3_result, 2, "uprobe_multi_func_3_result");
+
+	ASSERT_EQ(skel->bss->uretprobe_multi_func_1_result, 2, "uretprobe_multi_func_1_result");
+	ASSERT_EQ(skel->bss->uretprobe_multi_func_2_result, 2, "uretprobe_multi_func_2_result");
+	ASSERT_EQ(skel->bss->uretprobe_multi_func_3_result, 2, "uretprobe_multi_func_3_result");
+
+	ASSERT_EQ(skel->bss->uprobe_multi_sleep_result, 6, "uprobe_multi_sleep_result");
+}
+
+static void test_skel_api(void)
+{
+	struct uprobe_multi *skel = NULL;
+	int err;
+
+	skel = uprobe_multi__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_multi__open_and_load"))
+		goto cleanup;
+
+	err = uprobe_multi__attach(skel);
+	if (!ASSERT_OK(err, "uprobe_multi__attach"))
+		goto cleanup;
+
+	uprobe_multi_test_run(skel);
+
+cleanup:
+	uprobe_multi__destroy(skel);
+}
+
+void test_uprobe_multi_test(void)
+{
+	if (test__start_subtest("skel_api"))
+		test_skel_api();
+}
diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
new file mode 100644
index 000000000000..ab467970256a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <stdbool.h>
+
+char _license[] SEC("license") = "GPL";
+
+__u64 uprobe_multi_func_1_addr = 0;
+__u64 uprobe_multi_func_2_addr = 0;
+__u64 uprobe_multi_func_3_addr = 0;
+
+__u64 uprobe_multi_func_1_result = 0;
+__u64 uprobe_multi_func_2_result = 0;
+__u64 uprobe_multi_func_3_result = 0;
+
+__u64 uretprobe_multi_func_1_result = 0;
+__u64 uretprobe_multi_func_2_result = 0;
+__u64 uretprobe_multi_func_3_result = 0;
+
+__u64 uprobe_multi_sleep_result = 0;
+
+int pid = 0;
+bool test_cookie = false;
+void *user_ptr = 0;
+
+static __always_inline bool verify_sleepable_user_copy(void)
+{
+	char data[9];
+
+	bpf_copy_from_user(data, sizeof(data), user_ptr);
+	return bpf_strncmp(data, sizeof(data), "test_data") == 0;
+}
+
+static void uprobe_multi_check(void *ctx, bool is_return, bool is_sleep)
+{
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return;
+
+	__u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0;
+	__u64 addr = bpf_get_func_ip(ctx);
+
+#define SET(__var, __addr, __cookie) ({			\
+	if (addr == __addr &&				\
+	   (!test_cookie || (cookie == __cookie)))	\
+		__var += 1;				\
+})
+
+	if (is_return) {
+		SET(uretprobe_multi_func_1_result, uprobe_multi_func_1_addr, 2);
+		SET(uretprobe_multi_func_2_result, uprobe_multi_func_2_addr, 3);
+		SET(uretprobe_multi_func_3_result, uprobe_multi_func_3_addr, 1);
+	} else {
+		SET(uprobe_multi_func_1_result, uprobe_multi_func_1_addr, 3);
+		SET(uprobe_multi_func_2_result, uprobe_multi_func_2_addr, 1);
+		SET(uprobe_multi_func_3_result, uprobe_multi_func_3_addr, 2);
+	}
+
+#undef SET
+
+	if (is_sleep && verify_sleepable_user_copy())
+		uprobe_multi_sleep_result += 1;
+}
+
+SEC("uprobe.multi//proc/self/exe:uprobe_multi_func_*")
+int uprobe(struct pt_regs *ctx)
+{
+	uprobe_multi_check(ctx, false, false);
+	return 0;
+}
+
+SEC("uretprobe.multi//proc/self/exe:uprobe_multi_func_*")
+int uretprobe(struct pt_regs *ctx)
+{
+	uprobe_multi_check(ctx, true, false);
+	return 0;
+}
+
+SEC("uprobe.multi.s//proc/self/exe:uprobe_multi_func_*")
+int uprobe_sleep(struct pt_regs *ctx)
+{
+	uprobe_multi_check(ctx, false, true);
+	return 0;
+}
+
+SEC("uretprobe.multi.s//proc/self/exe:uprobe_multi_func_*")
+int uretprobe_sleep(struct pt_regs *ctx)
+{
+	uprobe_multi_check(ctx, true, true);
+	return 0;
+}
-- 
2.41.0


  parent reply	other threads:[~2023-07-20 11:39 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-20 11:35 [PATCHv4 bpf-next 00/28] bpf: Add multi uprobe link Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 01/28] bpf: Switch BPF_F_KPROBE_MULTI_RETURN macro to enum Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 02/28] bpf: Add attach_type checks under bpf_prog_attach_check_attach_type Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 03/28] bpf: Add multi uprobe link Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 04/28] bpf: Add cookies support for uprobe_multi link Jiri Olsa
2023-07-21  2:18   ` Yafang Shao
2023-07-21  7:39     ` Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 05/28] bpf: Add pid filter " Jiri Olsa
2023-07-21  2:55   ` Yafang Shao
2023-07-21  8:31     ` Oleg Nesterov
2023-07-21  8:54       ` Oleg Nesterov
2023-07-21  9:05         ` Yafang Shao
2023-07-21 10:41           ` Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 06/28] bpf: Add bpf_get_func_ip helper support for uprobe link Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 07/28] libbpf: Add uprobe_multi attach type and link names Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 08/28] libbpf: Move elf_find_func_offset* functions to elf object Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 09/28] libbpf: Add elf_open/elf_close functions Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 10/28] libbpf: Add elf symbol iterator Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 11/28] libbpf: Add elf_resolve_syms_offsets function Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 12/28] libbpf: Add elf_resolve_pattern_offsets function Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 13/28] libbpf: Add bpf_link_create support for multi uprobes Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 14/28] libbpf: Add bpf_program__attach_uprobe_multi function Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 15/28] libbpf: Add support for u[ret]probe.multi[.s] program sections Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 16/28] libbpf: Add uprobe multi link detection Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 17/28] libbpf: Add uprobe multi link support to bpf_program__attach_usdt Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 18/28] selftests/bpf: Move get_time_ns to testing_helpers.h Jiri Olsa
2023-07-20 11:35 ` Jiri Olsa [this message]
2023-07-20 11:35 ` [PATCHv4 bpf-next 20/28] selftests/bpf: Add uprobe_multi api test Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 21/28] selftests/bpf: Add uprobe_multi link test Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 22/28] selftests/bpf: Add uprobe_multi test program Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 23/28] selftests/bpf: Add uprobe_multi bench test Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 24/28] selftests/bpf: Add uprobe_multi usdt test code Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 25/28] selftests/bpf: Add uprobe_multi usdt bench test Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 26/28] selftests/bpf: Add uprobe_multi cookie test Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 27/28] selftests/bpf: Add uprobe_multi pid filter tests Jiri Olsa
2023-07-20 11:35 ` [PATCHv4 bpf-next 28/28] selftests/bpf: Add extra link to uprobe_multi tests 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=20230720113550.369257-20-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=sdf@google.com \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    /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.