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: [PATCHv3 bpf-next 25/26] selftests/bpf: Add uprobe_multi pid filter tests
Date: Fri, 30 Jun 2023 10:33:43 +0200	[thread overview]
Message-ID: <20230630083344.984305-26-jolsa@kernel.org> (raw)
In-Reply-To: <20230630083344.984305-1-jolsa@kernel.org>

Running api and link tests also with pid filter and checking
the probe gets executed only for specific pid.

Spawning extra process to trigger attached uprobes and checking
we get correct counts from executed programs.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../bpf/prog_tests/uprobe_multi_test.c        | 131 ++++++++++++++++--
 .../selftests/bpf/progs/uprobe_multi.c        |   6 +-
 2 files changed, 125 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
index b12dc1f992e5..8ca7a45e21e6 100644
--- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
@@ -23,14 +23,86 @@ noinline void uprobe_multi_func_3(void)
 	asm volatile ("");
 }
 
-static void uprobe_multi_test_run(struct uprobe_multi *skel)
+struct child {
+	int go[2];
+	int pid;
+};
+
+static void release_child(struct child *child)
+{
+	int child_status;
+
+	if (!child)
+		return;
+	close(child->go[1]);
+	close(child->go[0]);
+	if (child->pid > 0)
+		waitpid(child->pid, &child_status, 0);
+}
+
+static void kick_child(struct child *child)
+{
+	char c = 1;
+
+	if (child) {
+		write(child->go[1], &c, 1);
+		release_child(child);
+	}
+	fflush(NULL);
+}
+
+static struct child *spawn_child(void)
+{
+	static struct child child;
+	int err;
+	int c;
+
+	/* pid filter */
+	if (!ASSERT_OK(pipe(child.go), "pipe"))
+		return NULL;
+
+	child.pid = fork();
+	if (child.pid < 0) {
+		release_child(&child);
+		return NULL;
+	}
+
+	/* child */
+	if (child.pid == 0) {
+		close(child.go[1]);
+		fflush(NULL);
+		/* wait for parent's kick */
+		err = read(child.go[0], &c, 1);
+		if (!ASSERT_EQ(err, 1, "child_read_pipe"))
+			exit(err);
+
+		uprobe_multi_func_1();
+		uprobe_multi_func_2();
+		uprobe_multi_func_3();
+
+		exit(errno);
+	}
+
+	return &child;
+}
+
+static void uprobe_multi_test_run(struct uprobe_multi *skel, struct child *child)
 {
 	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();
+
+	/*
+	 * Disable pid check in bpf program if we are pid filter test,
+	 * because the probe should be executed only by child->pid
+	 * passed at the probe attach.
+	 */
+	skel->bss->pid = child ? 0 : getpid();
+
+	if (child)
+		kick_child(child);
 
 	/* trigger all probes */
 	uprobe_multi_func_1();
@@ -50,6 +122,9 @@ static void uprobe_multi_test_run(struct uprobe_multi *skel)
 	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");
+
+	if (child)
+		ASSERT_EQ(skel->bss->child_pid, child->pid, "uprobe_multi_child_pid");
 }
 
 static void test_skel_api(void)
@@ -65,17 +140,19 @@ static void test_skel_api(void)
 	if (!ASSERT_OK(err, "uprobe_multi__attach"))
 		goto cleanup;
 
-	uprobe_multi_test_run(skel);
+	uprobe_multi_test_run(skel, NULL);
 
 cleanup:
 	uprobe_multi__destroy(skel);
 }
 
 static void
-test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_multi_opts *opts)
+__test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_multi_opts *opts,
+		  struct child *child)
 {
 	struct bpf_link *link1 = NULL, *link2 = NULL;
 	struct bpf_link *link3 = NULL, *link4 = NULL;
+	pid_t pid = child ? child->pid : -1;
 	struct uprobe_multi *skel = NULL;
 
 	skel = uprobe_multi__open_and_load();
@@ -83,30 +160,30 @@ test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_multi
 		goto cleanup;
 
 	opts->retprobe = false;
-	link1 = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe, -1,
+	link1 = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe, pid,
 						      binary, pattern, opts);
 	if (!ASSERT_OK_PTR(link1, "bpf_program__attach_uprobe_multi"))
 		goto cleanup;
 
 	opts->retprobe = true;
-	link2 = bpf_program__attach_uprobe_multi(skel->progs.test_uretprobe, -1,
+	link2 = bpf_program__attach_uprobe_multi(skel->progs.test_uretprobe, pid,
 						      binary, pattern, opts);
 	if (!ASSERT_OK_PTR(link2, "bpf_program__attach_uprobe_multi_retprobe"))
 		goto cleanup;
 
 	opts->retprobe = false;
-	link3 = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe_sleep, -1,
+	link3 = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe_sleep, pid,
 						      binary, pattern, opts);
 	if (!ASSERT_OK_PTR(link1, "bpf_program__attach_uprobe_multi"))
 		goto cleanup;
 
 	opts->retprobe = true;
-	link4 = bpf_program__attach_uprobe_multi(skel->progs.test_uretprobe_sleep, -1,
+	link4 = bpf_program__attach_uprobe_multi(skel->progs.test_uretprobe_sleep, pid,
 						      binary, pattern, opts);
 	if (!ASSERT_OK_PTR(link2, "bpf_program__attach_uprobe_multi_retprobe"))
 		goto cleanup;
 
-	uprobe_multi_test_run(skel);
+	uprobe_multi_test_run(skel, child);
 
 cleanup:
 	bpf_link__destroy(link4);
@@ -116,6 +193,22 @@ test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_multi
 	uprobe_multi__destroy(skel);
 }
 
+static void
+test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_multi_opts *opts)
+{
+	struct child *child;
+
+	/* no pid filter */
+	__test_attach_api(binary, pattern, opts, NULL);
+
+	/* pid filter */
+	child = spawn_child();
+	if (!child)
+		return;
+
+	__test_attach_api(binary, pattern, opts, child);
+}
+
 static void test_attach_api_pattern(void)
 {
 	LIBBPF_OPTS(bpf_uprobe_multi_opts, opts);
@@ -138,7 +231,7 @@ static void test_attach_api_syms(void)
 	test_attach_api("/proc/self/exe", NULL, &opts);
 }
 
-static void test_link_api(void)
+static void __test_link_api(struct child *child)
 {
 	int prog_fd, link1_fd = -1, link2_fd = -1, link3_fd = -1, link4_fd = -1;
 	LIBBPF_OPTS(bpf_link_create_opts, opts);
@@ -159,6 +252,7 @@ static void test_link_api(void)
 	opts.uprobe_multi.path = path;
 	opts.uprobe_multi.offsets = offsets;
 	opts.uprobe_multi.cnt = ARRAY_SIZE(syms);
+	opts.uprobe_multi.pid = child ? child->pid : 0;
 
 	skel = uprobe_multi__open_and_load();
 	if (!ASSERT_OK_PTR(skel, "uprobe_multi__open_and_load"))
@@ -187,7 +281,7 @@ static void test_link_api(void)
 	link4_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &opts);
 	if (!ASSERT_GE(link2_fd, 0, "link4_fd"))
 		goto cleanup;
-	uprobe_multi_test_run(skel);
+	uprobe_multi_test_run(skel, child);
 
 cleanup:
 	if (link1_fd >= 0)
@@ -203,6 +297,21 @@ static void test_link_api(void)
 	free(offsets);
 }
 
+void test_link_api(void)
+{
+	struct child *child;
+
+	/* no pid filter */
+	__test_link_api(NULL);
+
+	/* pid filter */
+	child = spawn_child();
+	if (!child)
+		return;
+
+	__test_link_api(child);
+}
+
 static inline __u64 get_time_ns(void)
 {
 	struct timespec t;
diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
index cd73139dc881..1086312b5d1e 100644
--- a/tools/testing/selftests/bpf/progs/uprobe_multi.c
+++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
@@ -21,6 +21,8 @@ __u64 uretprobe_multi_func_3_result = 0;
 __u64 uprobe_multi_sleep_result = 0;
 
 int pid = 0;
+int child_pid = 0;
+
 bool test_cookie = false;
 void *user_ptr = 0;
 
@@ -34,7 +36,9 @@ static __always_inline bool verify_sleepable_user_copy(void)
 
 static void uprobe_multi_check(void *ctx, bool is_return, bool is_sleep)
 {
-	if (bpf_get_current_pid_tgid() >> 32 != pid)
+	child_pid = bpf_get_current_pid_tgid() >> 32;
+
+	if (pid && child_pid != pid)
 		return;
 
 	__u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0;
-- 
2.41.0


  parent reply	other threads:[~2023-06-30  8:38 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-30  8:33 [PATCHv3 bpf-next 00/26] bpf: Add multi uprobe link Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 01/26] bpf: Add attach_type checks under bpf_prog_attach_check_attach_type Jiri Olsa
2023-07-06 22:34   ` Andrii Nakryiko
2023-06-30  8:33 ` [PATCHv3 bpf-next 02/26] bpf: Add multi uprobe link Jiri Olsa
2023-07-06 22:34   ` Andrii Nakryiko
2023-07-11  9:00     ` Jiri Olsa
2023-07-07  4:22   ` Andrii Nakryiko
2023-07-11  9:01     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 03/26] bpf: Add cookies support for uprobe_multi link Jiri Olsa
2023-07-01  3:40   ` Yafang Shao
2023-07-01  8:54     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 04/26] bpf: Add pid filter " Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 05/26] bpf: Add bpf_get_func_ip helper support for uprobe link Jiri Olsa
2023-07-06 22:29   ` Andrii Nakryiko
2023-07-10  7:24     ` Jiri Olsa
2023-07-10 17:55       ` Andrii Nakryiko
2023-07-11  8:28         ` Jiri Olsa
2023-07-11 16:57           ` Andrii Nakryiko
2023-06-30  8:33 ` [PATCHv3 bpf-next 06/26] libbpf: Add uprobe_multi attach type and link names Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 07/26] libbpf: Move elf_find_func_offset* functions to elf object Jiri Olsa
2023-07-06 23:02   ` Andrii Nakryiko
2023-07-11  9:05     ` Jiri Olsa
2023-07-11 17:01       ` Andrii Nakryiko
2023-07-06 23:03   ` Andrii Nakryiko
2023-07-11  9:05     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 08/26] libbpf: Add elf_open/elf_close functions Jiri Olsa
2023-07-06 23:09   ` Andrii Nakryiko
2023-07-11  9:01     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 09/26] libbpf: Add elf symbol iterator Jiri Olsa
2023-07-06 23:24   ` Andrii Nakryiko
2023-07-11  9:03     ` Jiri Olsa
2023-07-11 16:59       ` Andrii Nakryiko
2023-06-30  8:33 ` [PATCHv3 bpf-next 10/26] libbpf: Add elf_resolve_syms_offsets function Jiri Olsa
2023-07-07  3:48   ` Andrii Nakryiko
2023-07-11  9:04     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 11/26] libbpf: Add elf_resolve_pattern_offsets function Jiri Olsa
2023-07-07  3:52   ` Andrii Nakryiko
2023-07-11  9:04     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 12/26] libbpf: Add bpf_link_create support for multi uprobes Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 13/26] libbpf: Add bpf_program__attach_uprobe_multi function Jiri Olsa
2023-07-07  4:05   ` Andrii Nakryiko
2023-07-11  9:05     ` Jiri Olsa
2023-07-11 17:02       ` Andrii Nakryiko
2023-06-30  8:33 ` [PATCHv3 bpf-next 14/26] libbpf: Add support for u[ret]probe.multi[.s] program sections Jiri Olsa
2023-07-07  4:07   ` Andrii Nakryiko
2023-06-30  8:33 ` [PATCHv3 bpf-next 15/26] libbpf: Add uprobe multi link detection Jiri Olsa
2023-07-07  4:20   ` Andrii Nakryiko
2023-07-11  9:03     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 16/26] libbpf: Add uprobe multi link support to bpf_program__attach_usdt Jiri Olsa
2023-07-07  4:29   ` Andrii Nakryiko
2023-07-11  9:04     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 17/26] selftests/bpf: Add uprobe_multi skel test Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 18/26] selftests/bpf: Add uprobe_multi api test Jiri Olsa
2023-07-07  4:32   ` Andrii Nakryiko
2023-07-11  9:06     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 19/26] selftests/bpf: Add uprobe_multi link test Jiri Olsa
2023-07-07  4:33   ` Andrii Nakryiko
2023-07-11  9:06     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 20/26] selftests/bpf: Add uprobe_multi test program Jiri Olsa
2023-07-07  4:35   ` Andrii Nakryiko
2023-06-30  8:33 ` [PATCHv3 bpf-next 21/26] selftests/bpf: Add uprobe_multi bench test Jiri Olsa
2023-07-07  4:38   ` Andrii Nakryiko
2023-07-11  9:07     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 22/26] selftests/bpf: Add usdt_multi test program Jiri Olsa
2023-07-07  4:39   ` Andrii Nakryiko
2023-06-30  8:33 ` [PATCHv3 bpf-next 23/26] selftests/bpf: Add usdt_multi bench test Jiri Olsa
2023-07-07  4:42   ` Andrii Nakryiko
2023-07-11  9:07     ` Jiri Olsa
2023-06-30  8:33 ` [PATCHv3 bpf-next 24/26] selftests/bpf: Add uprobe_multi cookie test Jiri Olsa
2023-06-30  8:33 ` Jiri Olsa [this message]
2023-06-30  8:33 ` [PATCHv3 bpf-next 26/26] selftests/bpf: Add extra link to uprobe_multi tests Jiri Olsa
2023-07-05 12:45 ` [PATCHv3 bpf-next 00/26] bpf: Add multi uprobe link Daniel Borkmann
2023-07-05 19:10   ` 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=20230630083344.984305-26-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.