Skip to content

Commit 8df43e8

Browse files
olsajirianakryiko
authored andcommitted
selftests/bpf: Add uprobe multi pid filter test for fork-ed processes
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. Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0b0bb45 commit 8df43e8

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "uprobe_multi_bench.skel.h"
88
#include "uprobe_multi_usdt.skel.h"
99
#include "uprobe_multi_consumers.skel.h"
10+
#include "uprobe_multi_pid_filter.skel.h"
1011
#include "bpf/libbpf_internal.h"
1112
#include "testing_helpers.h"
1213
#include "../sdt.h"
@@ -935,6 +936,70 @@ static void test_consumers(void)
935936
uprobe_multi_consumers__destroy(skel);
936937
}
937938

939+
static struct bpf_program *uprobe_multi_program(struct uprobe_multi_pid_filter *skel, int idx)
940+
{
941+
switch (idx) {
942+
case 0: return skel->progs.uprobe_multi_0;
943+
case 1: return skel->progs.uprobe_multi_1;
944+
case 2: return skel->progs.uprobe_multi_2;
945+
}
946+
return NULL;
947+
}
948+
949+
#define TASKS 3
950+
951+
static void run_pid_filter(struct uprobe_multi_pid_filter *skel, bool retprobe)
952+
{
953+
LIBBPF_OPTS(bpf_uprobe_multi_opts, opts, .retprobe = retprobe);
954+
struct bpf_link *link[TASKS] = {};
955+
struct child child[TASKS] = {};
956+
int i;
957+
958+
memset(skel->bss->test, 0, sizeof(skel->bss->test));
959+
960+
for (i = 0; i < TASKS; i++) {
961+
if (!ASSERT_OK(spawn_child(&child[i]), "spawn_child"))
962+
goto cleanup;
963+
skel->bss->pids[i] = child[i].pid;
964+
}
965+
966+
for (i = 0; i < TASKS; i++) {
967+
link[i] = bpf_program__attach_uprobe_multi(uprobe_multi_program(skel, i),
968+
child[i].pid, "/proc/self/exe",
969+
"uprobe_multi_func_1", &opts);
970+
if (!ASSERT_OK_PTR(link[i], "bpf_program__attach_uprobe_multi"))
971+
goto cleanup;
972+
}
973+
974+
for (i = 0; i < TASKS; i++)
975+
kick_child(&child[i]);
976+
977+
for (i = 0; i < TASKS; i++) {
978+
ASSERT_EQ(skel->bss->test[i][0], 1, "pid");
979+
ASSERT_EQ(skel->bss->test[i][1], 0, "unknown");
980+
}
981+
982+
cleanup:
983+
for (i = 0; i < TASKS; i++)
984+
bpf_link__destroy(link[i]);
985+
for (i = 0; i < TASKS; i++)
986+
release_child(&child[i]);
987+
}
988+
989+
static void test_pid_filter_process(void)
990+
{
991+
struct uprobe_multi_pid_filter *skel;
992+
993+
skel = uprobe_multi_pid_filter__open_and_load();
994+
if (!ASSERT_OK_PTR(skel, "uprobe_multi_pid_filter__open_and_load"))
995+
return;
996+
997+
run_pid_filter(skel, false);
998+
run_pid_filter(skel, true);
999+
1000+
uprobe_multi_pid_filter__destroy(skel);
1001+
}
1002+
9381003
static void test_bench_attach_uprobe(void)
9391004
{
9401005
long attach_start_ns = 0, attach_end_ns = 0;
@@ -1027,4 +1092,6 @@ void test_uprobe_multi_test(void)
10271092
test_attach_uprobe_fails();
10281093
if (test__start_subtest("consumers"))
10291094
test_consumers();
1095+
if (test__start_subtest("filter_fork"))
1096+
test_pid_filter_process();
10301097
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
__u32 pids[3];
9+
__u32 test[3][2];
10+
11+
static void update_pid(int idx)
12+
{
13+
__u32 pid = bpf_get_current_pid_tgid() >> 32;
14+
15+
if (pid == pids[idx])
16+
test[idx][0]++;
17+
else
18+
test[idx][1]++;
19+
}
20+
21+
SEC("uprobe.multi")
22+
int uprobe_multi_0(struct pt_regs *ctx)
23+
{
24+
update_pid(0);
25+
return 0;
26+
}
27+
28+
SEC("uprobe.multi")
29+
int uprobe_multi_1(struct pt_regs *ctx)
30+
{
31+
update_pid(1);
32+
return 0;
33+
}
34+
35+
SEC("uprobe.multi")
36+
int uprobe_multi_2(struct pt_regs *ctx)
37+
{
38+
update_pid(2);
39+
return 0;
40+
}

0 commit comments

Comments
 (0)