Skip to content

Commit 80b1219

Browse files
olsajiriNobody
authored andcommitted
selftest/bpf: Add kprobe_multi test for bpf_cookie values
Adding bpf_cookie test for programs attached by kprobe_multi links. Signed-off-by: Jiri Olsa <[email protected]>
1 parent 4b8db78 commit 80b1219

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <unistd.h>
88
#include <test_progs.h>
99
#include "test_bpf_cookie.skel.h"
10+
#include "kprobe_multi_bpf_cookie.skel.h"
1011

1112
/* uprobe attach point */
1213
static void trigger_func(void)
@@ -63,6 +64,75 @@ static void kprobe_subtest(struct test_bpf_cookie *skel)
6364
bpf_link__destroy(retlink2);
6465
}
6566

67+
static void kprobe_multi_subtest(void)
68+
{
69+
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
70+
int err, prog_fd, link1_fd = -1, link2_fd = -1;
71+
LIBBPF_OPTS(bpf_test_run_opts, topts);
72+
struct kprobe_multi_bpf_cookie *skel = NULL;
73+
__u64 addrs[8], cookies[8];
74+
75+
skel = kprobe_multi_bpf_cookie__open_and_load();
76+
if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
77+
goto cleanup;
78+
79+
kallsyms_find("bpf_fentry_test1", &addrs[0]);
80+
kallsyms_find("bpf_fentry_test2", &addrs[1]);
81+
kallsyms_find("bpf_fentry_test3", &addrs[2]);
82+
kallsyms_find("bpf_fentry_test4", &addrs[3]);
83+
kallsyms_find("bpf_fentry_test5", &addrs[4]);
84+
kallsyms_find("bpf_fentry_test6", &addrs[5]);
85+
kallsyms_find("bpf_fentry_test7", &addrs[6]);
86+
kallsyms_find("bpf_fentry_test8", &addrs[7]);
87+
88+
cookies[0] = 1;
89+
cookies[1] = 2;
90+
cookies[2] = 3;
91+
cookies[3] = 4;
92+
cookies[4] = 5;
93+
cookies[5] = 6;
94+
cookies[6] = 7;
95+
cookies[7] = 8;
96+
97+
opts.kprobe_multi.addrs = ptr_to_u64(&addrs);
98+
opts.kprobe_multi.cnt = 8;
99+
opts.kprobe_multi.cookies = ptr_to_u64(&cookies);
100+
prog_fd = bpf_program__fd(skel->progs.test2);
101+
102+
link1_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &opts);
103+
if (!ASSERT_GE(link1_fd, 0, "link1_fd"))
104+
return;
105+
106+
cookies[0] = 8;
107+
cookies[1] = 7;
108+
cookies[2] = 6;
109+
cookies[3] = 5;
110+
cookies[4] = 4;
111+
cookies[5] = 3;
112+
cookies[6] = 2;
113+
cookies[7] = 1;
114+
115+
opts.flags = BPF_F_KPROBE_MULTI_RETURN;
116+
prog_fd = bpf_program__fd(skel->progs.test3);
117+
118+
link2_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &opts);
119+
if (!ASSERT_GE(link2_fd, 0, "link2_fd"))
120+
goto cleanup;
121+
122+
prog_fd = bpf_program__fd(skel->progs.test1);
123+
err = bpf_prog_test_run_opts(prog_fd, &topts);
124+
ASSERT_OK(err, "test_run");
125+
ASSERT_EQ(topts.retval, 0, "test_run");
126+
127+
ASSERT_EQ(skel->bss->test2_result, 8, "test2_result");
128+
ASSERT_EQ(skel->bss->test3_result, 8, "test3_result");
129+
130+
cleanup:
131+
close(link1_fd);
132+
close(link2_fd);
133+
kprobe_multi_bpf_cookie__destroy(skel);
134+
}
135+
66136
static void uprobe_subtest(struct test_bpf_cookie *skel)
67137
{
68138
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
@@ -249,6 +319,8 @@ void test_bpf_cookie(void)
249319

250320
if (test__start_subtest("kprobe"))
251321
kprobe_subtest(skel);
322+
if (test__start_subtest("multi_kprobe"))
323+
kprobe_multi_subtest();
252324
if (test__start_subtest("uprobe"))
253325
uprobe_subtest(skel);
254326
if (test__start_subtest("tracepoint"))
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
extern const void bpf_fentry_test1 __ksym;
9+
extern const void bpf_fentry_test2 __ksym;
10+
extern const void bpf_fentry_test3 __ksym;
11+
extern const void bpf_fentry_test4 __ksym;
12+
extern const void bpf_fentry_test5 __ksym;
13+
extern const void bpf_fentry_test6 __ksym;
14+
extern const void bpf_fentry_test7 __ksym;
15+
extern const void bpf_fentry_test8 __ksym;
16+
17+
/* No tests, just to trigger bpf_fentry_test* through tracing test_run */
18+
SEC("fentry/bpf_modify_return_test")
19+
int BPF_PROG(test1)
20+
{
21+
return 0;
22+
}
23+
24+
__u64 test2_result = 0;
25+
26+
SEC("kprobe.multi/bpf_fentry_tes??")
27+
int test2(struct pt_regs *ctx)
28+
{
29+
__u64 cookie = bpf_get_attach_cookie(ctx);
30+
__u64 addr = bpf_get_func_ip(ctx);
31+
32+
test2_result += (const void *) addr == &bpf_fentry_test1 && cookie == 1;
33+
test2_result += (const void *) addr == &bpf_fentry_test2 && cookie == 2;
34+
test2_result += (const void *) addr == &bpf_fentry_test3 && cookie == 3;
35+
test2_result += (const void *) addr == &bpf_fentry_test4 && cookie == 4;
36+
test2_result += (const void *) addr == &bpf_fentry_test5 && cookie == 5;
37+
test2_result += (const void *) addr == &bpf_fentry_test6 && cookie == 6;
38+
test2_result += (const void *) addr == &bpf_fentry_test7 && cookie == 7;
39+
test2_result += (const void *) addr == &bpf_fentry_test8 && cookie == 8;
40+
41+
return 0;
42+
}
43+
44+
__u64 test3_result = 0;
45+
46+
SEC("kretprobe.multi/bpf_fentry_test*")
47+
int test3(struct pt_regs *ctx)
48+
{
49+
__u64 cookie = bpf_get_attach_cookie(ctx);
50+
__u64 addr = bpf_get_func_ip(ctx);
51+
52+
test3_result += (const void *) addr == &bpf_fentry_test1 && cookie == 8;
53+
test3_result += (const void *) addr == &bpf_fentry_test2 && cookie == 7;
54+
test3_result += (const void *) addr == &bpf_fentry_test3 && cookie == 6;
55+
test3_result += (const void *) addr == &bpf_fentry_test4 && cookie == 5;
56+
test3_result += (const void *) addr == &bpf_fentry_test5 && cookie == 4;
57+
test3_result += (const void *) addr == &bpf_fentry_test6 && cookie == 3;
58+
test3_result += (const void *) addr == &bpf_fentry_test7 && cookie == 2;
59+
test3_result += (const void *) addr == &bpf_fentry_test8 && cookie == 1;
60+
61+
return 0;
62+
}

0 commit comments

Comments
 (0)