Skip to contentSkip to conversation

selftests/bpf: Add uprobe pid filter test

PATCH Superseded Jiri Olsa 3 participants 5 messages Aug 29, 2024 – Aug 30, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover selftests/bpf: Add uprobe pid filter test

hi,
in response to [1] patch, I'm adding bpf selftest that confirms the change fixes problem for bpf programs trigered by return uprobe created over perf event.

Oleg pointed out other issues with uprobe_multi pid filter, I plan to send another patchset for that.

thanks,
jirka

[1] https://lore.kernel.org/linux-trace-kernel/ME0P300MB0416034322B9915ECD3888649D882@ME0P300MB0416.AUSP300.PROD.OUTLOOK.COM/
---
Jiri Olsa (2):
selftests/bpf: Add child argument to spawn_child function
selftests/bpf: Add uprobe pid filter test for multiple processes

 tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c  | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
 tools/testing/selftests/bpf/progs/uprobe_multi_pid_filter.c |  61 ++++++++++++++++++++++++++++++
 2 files changed, 203 insertions(+), 46 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_pid_filter.c
author reviewer
#1 lore raw

PATCH 1/2 selftests/bpf: Add child argument to spawn_child function

Adding child argument to spawn_child function to allow to create multiple children in following change.

---
 .../bpf/prog_tests/uprobe_multi_test.c        | 85 +++++++++----------
 1 file changed, 39 insertions(+), 46 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +39 -46
index acb62675ff65..250eb47c68f9 100644
@@ -68,29 +68,27 @@ static void kick_child(struct child *child)
 	fflush(NULL);
 }
 
-static struct child *spawn_child(void)
+static int spawn_child(struct child *child)
 {
-	static struct child child;
-	int err;
-	int c;
+	int err, c;
 
 	/* pipe to notify child to execute the trigger functions */
-	if (pipe(child.go))
-		return NULL;
+	if (pipe(child->go))
+		return -1;
 
-	child.pid = child.tid = fork();
-	if (child.pid < 0) {
-		release_child(&child);
+	child->pid = child->tid = fork();
+	if (child->pid < 0) {
+		release_child(child);
 		errno = EINVAL;
-		return NULL;
+		return -1;
 	}
 
 	/* child */
-	if (child.pid == 0) {
-		close(child.go[1]);
+	if (child->pid == 0) {
+		close(child->go[1]);
 
 		/* wait for parent's kick */
-		err = read(child.go[0], &c, 1);
+		err = read(child->go[0], &c, 1);
 		if (err != 1)
 			exit(err);
 
@@ -102,7 +100,7 @@ static struct child *spawn_child(void)
 		exit(errno);
 	}
 
-	return &child;
+	return 0;
 }
 
 static void *child_thread(void *ctx)
@@ -131,39 +129,38 @@ static void *child_thread(void *ctx)
 	pthread_exit(&err);
 }
 
-static struct child *spawn_thread(void)
+static int spawn_thread(struct child *child)
 {
-	static struct child child;
 	int c, err;
 
 	/* pipe to notify child to execute the trigger functions */
-	if (pipe(child.go))
-		return NULL;
+	if (pipe(child->go))
+		return -1;
 	/* pipe to notify parent that child thread is ready */
-	if (pipe(child.c2p)) {
-		close(child.go[0]);
-		close(child.go[1]);
-		return NULL;
+	if (pipe(child->c2p)) {
+		close(child->go[0]);
+		close(child->go[1]);
+		return -1;
 	}
 
-	child.pid = getpid();
+	child->pid = getpid();
 
-	err = pthread_create(&child.thread, NULL, child_thread, &child);
+	err = pthread_create(&child->thread, NULL, child_thread, child);
 	if (err) {
 		err = -errno;
-		close(child.go[0]);
-		close(child.go[1]);
-		close(child.c2p[0]);
-		close(child.c2p[1]);
+		close(child->go[0]);
+		close(child->go[1]);
+		close(child->c2p[0]);
+		close(child->c2p[1]);
 		errno = -err;
-		return NULL;
+		return -1;
 	}
 
-	err = read(child.c2p[0], &c, 1);
+	err = read(child->c2p[0], &c, 1);
 	if (!ASSERT_EQ(err, 1, "child_thread_ready"))
-		return NULL;
+		return -1;
 
-	return &child;
+	return 0;
 }
 
 static void uprobe_multi_test_run(struct uprobe_multi *skel, struct child *child)
@@ -304,24 +301,22 @@ __test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_mul
 static void
 test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_multi_opts *opts)
 {
-	struct child *child;
+	static struct child child;
 
 	/* no pid filter */
 	__test_attach_api(binary, pattern, opts, NULL);
 
 	/* pid filter */
-	child = spawn_child();
-	if (!ASSERT_OK_PTR(child, "spawn_child"))
+	if (!ASSERT_OK(spawn_child(&child), "spawn_child"))
 		return;
 
-	__test_attach_api(binary, pattern, opts, child);
+	__test_attach_api(binary, pattern, opts, &child);
 
 	/* pid filter (thread) */
-	child = spawn_thread();
-	if (!ASSERT_OK_PTR(child, "spawn_thread"))
+	if (!ASSERT_OK(spawn_thread(&child), "spawn_thread"))
 		return;
 
-	__test_attach_api(binary, pattern, opts, child);
+	__test_attach_api(binary, pattern, opts, &child);
 }
 
 static void test_attach_api_pattern(void)
@@ -712,24 +707,22 @@ static void __test_link_api(struct child *child)
 
 static void test_link_api(void)
 {
-	struct child *child;
+	static struct child child;
 
 	/* no pid filter */
 	__test_link_api(NULL);
 
 	/* pid filter */
-	child = spawn_child();
-	if (!ASSERT_OK_PTR(child, "spawn_child"))
+	if (!ASSERT_OK(spawn_child(&child), "spawn_child"))
 		return;
 
-	__test_link_api(child);
+	__test_link_api(&child);
 
 	/* pid filter (thread) */
-	child = spawn_thread();
-	if (!ASSERT_OK_PTR(child, "spawn_thread"))
+	if (!ASSERT_OK(spawn_thread(&child), "spawn_thread"))
 		return;
 
-	__test_link_api(child);
+	__test_link_api(&child);
 }
 
 static struct bpf_program *
Signature · 1 line
2.46.0
author reviewer
#2 lore raw

PATCH 2/2 selftests/bpf: Add uprobe pid filter test for multiple processes

In reply to Jiri Olsa

The idea is to create and monitor 3 uprobes, each trigered in separate process and make sure the bpf program gets executed just for the proper PID specified via pid filter.

---
 .../bpf/prog_tests/uprobe_multi_test.c        | 103 ++++++++++++++++++
 .../bpf/progs/uprobe_multi_pid_filter.c       |  61 +++++++++++
 2 files changed, 164 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_pid_filter.c
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +103 -0
index 250eb47c68f9..59c460675af9 100644
@@ -7,6 +7,7 @@
 #include "uprobe_multi_bench.skel.h"
 #include "uprobe_multi_usdt.skel.h"
 #include "uprobe_multi_consumers.skel.h"
+#include "uprobe_multi_pid_filter.skel.h"
 #include "bpf/libbpf_internal.h"
 #include "testing_helpers.h"
 #include "../sdt.h"
@@ -935,6 +936,106 @@ static void test_consumers(void)
 	uprobe_multi_consumers__destroy(skel);
 }
 
+typedef struct bpf_link *(create_link_t)(struct uprobe_multi_pid_filter *, int, int, bool);
+
+static struct bpf_program *uprobe_program(struct uprobe_multi_pid_filter *skel, int idx)
+{
+	switch (idx) {
+	case 0: return skel->progs.uprobe_0;
+	case 1: return skel->progs.uprobe_1;
+	case 2: return skel->progs.uprobe_2;
+	}
+	return NULL;
+}
+
+static struct bpf_link *create_link_uprobe(struct uprobe_multi_pid_filter *skel,
+					   int idx, int pid, bool retprobe)
+{
+	LIBBPF_OPTS(bpf_uprobe_opts, opts,
+		.retprobe  = retprobe,
+		.func_name = "uprobe_multi_func_1",
+	);
+
+	return bpf_program__attach_uprobe_opts(uprobe_program(skel, idx), pid,
+					       "/proc/self/exe", 0, &opts);
+}
+
+static struct bpf_program *uprobe_multi_program(struct uprobe_multi_pid_filter *skel, int idx)
+{
+	switch (idx) {
+	case 0: return skel->progs.uprobe_multi_0;
+	case 1: return skel->progs.uprobe_multi_1;
+	case 2: return skel->progs.uprobe_multi_2;
+	}
+	return NULL;
+}
+
+static struct bpf_link *create_link_uprobe_multi(struct uprobe_multi_pid_filter *skel,
+						 int idx, int pid, bool retprobe)
+{
+	LIBBPF_OPTS(bpf_uprobe_multi_opts, opts, .retprobe = retprobe);
+
+	return bpf_program__attach_uprobe_multi(uprobe_multi_program(skel, idx), pid,
+						"/proc/self/exe", "uprobe_multi_func_1", &opts);
+}
+
+#define TASKS 3
+
+static void run_pid_filter(struct uprobe_multi_pid_filter *skel,
+			   create_link_t create_link, bool retprobe)
+{
+	struct bpf_link *link[TASKS] = {};
+	struct child child[TASKS] = {};
+	int i;
+
+	printf("%s retprobe %d\n", create_link == create_link_uprobe ? "uprobe" : "uprobe_multi",
+		retprobe);
+
+	memset(skel->bss->test, 0, sizeof(skel->bss->test));
+
+	for (i = 0; i < TASKS; i++) {
+		if (!ASSERT_OK(spawn_child(&child[i]), "spawn_child"))
+			goto cleanup;
+		skel->bss->pids[i] = child[i].pid;
+	}
+
+	for (i = 0; i < TASKS; i++) {
+		link[i] = create_link(skel, i, child[i].pid, retprobe);
+		if (!ASSERT_OK_PTR(link[i], "create_link"))
+			goto cleanup;
+	}
+
+	for (i = 0; i < TASKS; i++)
+		kick_child(&child[i]);
+
+	for (i = 0; i < TASKS; i++) {
+		ASSERT_EQ(skel->bss->test[i][0], 1, "pid");
+		ASSERT_EQ(skel->bss->test[i][1], 0, "unknown");
+	}
+
+cleanup:
+	for (i = 0; i < TASKS; i++)
+		bpf_link__destroy(link[i]);
+	for (i = 0; i < TASKS; i++)
+		release_child(&child[i]);
+}
+
+static void test_pid_filter_process(void)
+{
+	struct uprobe_multi_pid_filter *skel;
+
+	skel = uprobe_multi_pid_filter__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_multi_pid_filter__open_and_load"))
+		return;
+
+	run_pid_filter(skel, create_link_uprobe, false);
+	run_pid_filter(skel, create_link_uprobe, true);
+	run_pid_filter(skel, create_link_uprobe_multi, false);
+	run_pid_filter(skel, create_link_uprobe_multi, true);
+
+	uprobe_multi_pid_filter__destroy(skel);
+}
+
 static void test_bench_attach_uprobe(void)
 {
 	long attach_start_ns = 0, attach_end_ns = 0;
@@ -1027,4 +1128,6 @@ void test_uprobe_multi_test(void)
 		test_attach_uprobe_fails();
 	if (test__start_subtest("consumers"))
 		test_consumers();
+	if (test__start_subtest("filter_process"))
+		test_pid_filter_process();
 }
tools/testing/selftests/bpf/progs/uprobe_multi_pid_filter.c new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.46.0
#3 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Add uprobe pid filter test for multiple processes

Quoted 2 lines · Jiri Olsa

The idea is to create and monitor 3 uprobes, each trigered in separate

typo: triggered

Quoted 10 lines

process and make sure the bpf program gets executed just for the proper PID specified via pid filter.

---
 .../bpf/prog_tests/uprobe_multi_test.c        | 103 ++++++++++++++++++
 .../bpf/progs/uprobe_multi_pid_filter.c       |  61 +++++++++++
 2 files changed, 164 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_pid_filter.c

It's good to have a test, thanks for adding it! But we should couple it with the fix in multi-uprobe and land together, right? I'm not exactly sure why we can't just use task->signal-based check, but let's try to converge on something and fix it.

pw-bot: cr

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c static void test_consumers(void) · line 992

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +11 -0
+#define TASKS 3
+
+static void run_pid_filter(struct uprobe_multi_pid_filter *skel,
+                          create_link_t create_link, bool retprobe)
+{
+       struct bpf_link *link[TASKS] = {};
+       struct child child[TASKS] = {};
+       int i;
+
+       printf("%s retprobe %d\n", create_link == create_link_uprobe ? "uprobe" : "uprobe_multi",
+               retprobe);

leftovers

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +8 -0
+
+       memset(skel->bss->test, 0, sizeof(skel->bss->test));
+
+       for (i = 0; i < TASKS; i++) {
+               if (!ASSERT_OK(spawn_child(&child[i]), "spawn_child"))
+                       goto cleanup;
+               skel->bss->pids[i] = child[i].pid;
+       }

[...]

Acked-by on the v1 series
#4 lore raw
In reply to Jiri Olsa
Quoted 7 lines · Jiri Olsa

hi,
in response to [1] patch, I'm adding bpf selftest that confirms the change fixes problem for bpf programs trigered by return uprobe created over perf event.

Oleg pointed out other issues with uprobe_multi pid filter, I plan to send another patchset for that.

Not sure whether Oleg or Andrii plan to take a look, but LGTM: