Skip to content

Commit e8e1b5b

Browse files
Hou TaoNobody
Hou Tao
authored and
Nobody
committed
selftests/bpf: use raw_tp program for atomic test
Now atomic tests will attach fentry program and run it through bpf_prog_test_run_opts(), but attaching fentry program depends on bpf trampoline which is only available under x86-64. Considering many archs have atomic support, using raw_tp program instead. Signed-off-by: Hou Tao <[email protected]> Acked-by: Andrii Nakryiko <[email protected]>
1 parent b4acc47 commit e8e1b5b

File tree

2 files changed

+36
-83
lines changed

2 files changed

+36
-83
lines changed

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

Lines changed: 22 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@
77
static void test_add(struct atomics_lskel *skel)
88
{
99
int err, prog_fd;
10-
int link_fd;
1110
LIBBPF_OPTS(bpf_test_run_opts, topts);
1211

13-
link_fd = atomics_lskel__add__attach(skel);
14-
if (!ASSERT_GT(link_fd, 0, "attach(add)"))
15-
return;
16-
12+
/* No need to attach it, just run it directly */
1713
prog_fd = skel->progs.add.prog_fd;
1814
err = bpf_prog_test_run_opts(prog_fd, &topts);
1915
if (!ASSERT_OK(err, "test_run_opts err"))
20-
goto cleanup;
16+
return;
2117
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
22-
goto cleanup;
18+
return;
2319

2420
ASSERT_EQ(skel->data->add64_value, 3, "add64_value");
2521
ASSERT_EQ(skel->bss->add64_result, 1, "add64_result");
@@ -31,27 +27,20 @@ static void test_add(struct atomics_lskel *skel)
3127
ASSERT_EQ(skel->bss->add_stack_result, 1, "add_stack_result");
3228

3329
ASSERT_EQ(skel->data->add_noreturn_value, 3, "add_noreturn_value");
34-
35-
cleanup:
36-
close(link_fd);
3730
}
3831

3932
static void test_sub(struct atomics_lskel *skel)
4033
{
4134
int err, prog_fd;
42-
int link_fd;
4335
LIBBPF_OPTS(bpf_test_run_opts, topts);
4436

45-
link_fd = atomics_lskel__sub__attach(skel);
46-
if (!ASSERT_GT(link_fd, 0, "attach(sub)"))
47-
return;
48-
37+
/* No need to attach it, just run it directly */
4938
prog_fd = skel->progs.sub.prog_fd;
5039
err = bpf_prog_test_run_opts(prog_fd, &topts);
5140
if (!ASSERT_OK(err, "test_run_opts err"))
52-
goto cleanup;
41+
return;
5342
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
54-
goto cleanup;
43+
return;
5544

5645
ASSERT_EQ(skel->data->sub64_value, -1, "sub64_value");
5746
ASSERT_EQ(skel->bss->sub64_result, 1, "sub64_result");
@@ -63,27 +52,20 @@ static void test_sub(struct atomics_lskel *skel)
6352
ASSERT_EQ(skel->bss->sub_stack_result, 1, "sub_stack_result");
6453

6554
ASSERT_EQ(skel->data->sub_noreturn_value, -1, "sub_noreturn_value");
66-
67-
cleanup:
68-
close(link_fd);
6955
}
7056

7157
static void test_and(struct atomics_lskel *skel)
7258
{
7359
int err, prog_fd;
74-
int link_fd;
7560
LIBBPF_OPTS(bpf_test_run_opts, topts);
7661

77-
link_fd = atomics_lskel__and__attach(skel);
78-
if (!ASSERT_GT(link_fd, 0, "attach(and)"))
79-
return;
80-
62+
/* No need to attach it, just run it directly */
8163
prog_fd = skel->progs.and.prog_fd;
8264
err = bpf_prog_test_run_opts(prog_fd, &topts);
8365
if (!ASSERT_OK(err, "test_run_opts err"))
84-
goto cleanup;
66+
return;
8567
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
86-
goto cleanup;
68+
return;
8769

8870
ASSERT_EQ(skel->data->and64_value, 0x010ull << 32, "and64_value");
8971
ASSERT_EQ(skel->bss->and64_result, 0x110ull << 32, "and64_result");
@@ -92,26 +74,20 @@ static void test_and(struct atomics_lskel *skel)
9274
ASSERT_EQ(skel->bss->and32_result, 0x110, "and32_result");
9375

9476
ASSERT_EQ(skel->data->and_noreturn_value, 0x010ull << 32, "and_noreturn_value");
95-
cleanup:
96-
close(link_fd);
9777
}
9878

9979
static void test_or(struct atomics_lskel *skel)
10080
{
10181
int err, prog_fd;
102-
int link_fd;
10382
LIBBPF_OPTS(bpf_test_run_opts, topts);
10483

105-
link_fd = atomics_lskel__or__attach(skel);
106-
if (!ASSERT_GT(link_fd, 0, "attach(or)"))
107-
return;
108-
84+
/* No need to attach it, just run it directly */
10985
prog_fd = skel->progs.or.prog_fd;
11086
err = bpf_prog_test_run_opts(prog_fd, &topts);
11187
if (!ASSERT_OK(err, "test_run_opts err"))
112-
goto cleanup;
88+
return;
11389
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
114-
goto cleanup;
90+
return;
11591

11692
ASSERT_EQ(skel->data->or64_value, 0x111ull << 32, "or64_value");
11793
ASSERT_EQ(skel->bss->or64_result, 0x110ull << 32, "or64_result");
@@ -120,26 +96,20 @@ static void test_or(struct atomics_lskel *skel)
12096
ASSERT_EQ(skel->bss->or32_result, 0x110, "or32_result");
12197

12298
ASSERT_EQ(skel->data->or_noreturn_value, 0x111ull << 32, "or_noreturn_value");
123-
cleanup:
124-
close(link_fd);
12599
}
126100

127101
static void test_xor(struct atomics_lskel *skel)
128102
{
129103
int err, prog_fd;
130-
int link_fd;
131104
LIBBPF_OPTS(bpf_test_run_opts, topts);
132105

133-
link_fd = atomics_lskel__xor__attach(skel);
134-
if (!ASSERT_GT(link_fd, 0, "attach(xor)"))
135-
return;
136-
106+
/* No need to attach it, just run it directly */
137107
prog_fd = skel->progs.xor.prog_fd;
138108
err = bpf_prog_test_run_opts(prog_fd, &topts);
139109
if (!ASSERT_OK(err, "test_run_opts err"))
140-
goto cleanup;
110+
return;
141111
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
142-
goto cleanup;
112+
return;
143113

144114
ASSERT_EQ(skel->data->xor64_value, 0x101ull << 32, "xor64_value");
145115
ASSERT_EQ(skel->bss->xor64_result, 0x110ull << 32, "xor64_result");
@@ -148,26 +118,20 @@ static void test_xor(struct atomics_lskel *skel)
148118
ASSERT_EQ(skel->bss->xor32_result, 0x110, "xor32_result");
149119

150120
ASSERT_EQ(skel->data->xor_noreturn_value, 0x101ull << 32, "xor_nxoreturn_value");
151-
cleanup:
152-
close(link_fd);
153121
}
154122

155123
static void test_cmpxchg(struct atomics_lskel *skel)
156124
{
157125
int err, prog_fd;
158-
int link_fd;
159126
LIBBPF_OPTS(bpf_test_run_opts, topts);
160127

161-
link_fd = atomics_lskel__cmpxchg__attach(skel);
162-
if (!ASSERT_GT(link_fd, 0, "attach(cmpxchg)"))
163-
return;
164-
128+
/* No need to attach it, just run it directly */
165129
prog_fd = skel->progs.cmpxchg.prog_fd;
166130
err = bpf_prog_test_run_opts(prog_fd, &topts);
167131
if (!ASSERT_OK(err, "test_run_opts err"))
168-
goto cleanup;
132+
return;
169133
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
170-
goto cleanup;
134+
return;
171135

172136
ASSERT_EQ(skel->data->cmpxchg64_value, 2, "cmpxchg64_value");
173137
ASSERT_EQ(skel->bss->cmpxchg64_result_fail, 1, "cmpxchg_result_fail");
@@ -176,45 +140,34 @@ static void test_cmpxchg(struct atomics_lskel *skel)
176140
ASSERT_EQ(skel->data->cmpxchg32_value, 2, "lcmpxchg32_value");
177141
ASSERT_EQ(skel->bss->cmpxchg32_result_fail, 1, "cmpxchg_result_fail");
178142
ASSERT_EQ(skel->bss->cmpxchg32_result_succeed, 1, "cmpxchg_result_succeed");
179-
180-
cleanup:
181-
close(link_fd);
182143
}
183144

184145
static void test_xchg(struct atomics_lskel *skel)
185146
{
186147
int err, prog_fd;
187-
int link_fd;
188148
LIBBPF_OPTS(bpf_test_run_opts, topts);
189149

190-
link_fd = atomics_lskel__xchg__attach(skel);
191-
if (!ASSERT_GT(link_fd, 0, "attach(xchg)"))
192-
return;
193-
150+
/* No need to attach it, just run it directly */
194151
prog_fd = skel->progs.xchg.prog_fd;
195152
err = bpf_prog_test_run_opts(prog_fd, &topts);
196153
if (!ASSERT_OK(err, "test_run_opts err"))
197-
goto cleanup;
154+
return;
198155
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
199-
goto cleanup;
156+
return;
200157

201158
ASSERT_EQ(skel->data->xchg64_value, 2, "xchg64_value");
202159
ASSERT_EQ(skel->bss->xchg64_result, 1, "xchg64_result");
203160

204161
ASSERT_EQ(skel->data->xchg32_value, 2, "xchg32_value");
205162
ASSERT_EQ(skel->bss->xchg32_result, 1, "xchg32_result");
206-
207-
cleanup:
208-
close(link_fd);
209163
}
210164

211165
void test_atomics(void)
212166
{
213167
struct atomics_lskel *skel;
214-
__u32 duration = 0;
215168

216169
skel = atomics_lskel__open_and_load();
217-
if (CHECK(!skel, "skel_load", "atomics skeleton failed\n"))
170+
if (!ASSERT_OK_PTR(skel, "atomics skeleton load"))
218171
return;
219172

220173
if (skel->data->skip_tests) {

tools/testing/selftests/bpf/progs/atomics.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ __u64 add_stack_value_copy = 0;
2020
__u64 add_stack_result = 0;
2121
__u64 add_noreturn_value = 1;
2222

23-
SEC("fentry/bpf_fentry_test1")
24-
int BPF_PROG(add, int a)
23+
SEC("raw_tp/sys_enter")
24+
int add(const void *ctx)
2525
{
2626
if (pid != (bpf_get_current_pid_tgid() >> 32))
2727
return 0;
@@ -46,8 +46,8 @@ __s64 sub_stack_value_copy = 0;
4646
__s64 sub_stack_result = 0;
4747
__s64 sub_noreturn_value = 1;
4848

49-
SEC("fentry/bpf_fentry_test1")
50-
int BPF_PROG(sub, int a)
49+
SEC("raw_tp/sys_enter")
50+
int sub(const void *ctx)
5151
{
5252
if (pid != (bpf_get_current_pid_tgid() >> 32))
5353
return 0;
@@ -70,8 +70,8 @@ __u32 and32_value = 0x110;
7070
__u32 and32_result = 0;
7171
__u64 and_noreturn_value = (0x110ull << 32);
7272

73-
SEC("fentry/bpf_fentry_test1")
74-
int BPF_PROG(and, int a)
73+
SEC("raw_tp/sys_enter")
74+
int and(const void *ctx)
7575
{
7676
if (pid != (bpf_get_current_pid_tgid() >> 32))
7777
return 0;
@@ -91,8 +91,8 @@ __u32 or32_value = 0x110;
9191
__u32 or32_result = 0;
9292
__u64 or_noreturn_value = (0x110ull << 32);
9393

94-
SEC("fentry/bpf_fentry_test1")
95-
int BPF_PROG(or, int a)
94+
SEC("raw_tp/sys_enter")
95+
int or(const void *ctx)
9696
{
9797
if (pid != (bpf_get_current_pid_tgid() >> 32))
9898
return 0;
@@ -111,8 +111,8 @@ __u32 xor32_value = 0x110;
111111
__u32 xor32_result = 0;
112112
__u64 xor_noreturn_value = (0x110ull << 32);
113113

114-
SEC("fentry/bpf_fentry_test1")
115-
int BPF_PROG(xor, int a)
114+
SEC("raw_tp/sys_enter")
115+
int xor(const void *ctx)
116116
{
117117
if (pid != (bpf_get_current_pid_tgid() >> 32))
118118
return 0;
@@ -132,8 +132,8 @@ __u32 cmpxchg32_value = 1;
132132
__u32 cmpxchg32_result_fail = 0;
133133
__u32 cmpxchg32_result_succeed = 0;
134134

135-
SEC("fentry/bpf_fentry_test1")
136-
int BPF_PROG(cmpxchg, int a)
135+
SEC("raw_tp/sys_enter")
136+
int cmpxchg(const void *ctx)
137137
{
138138
if (pid != (bpf_get_current_pid_tgid() >> 32))
139139
return 0;
@@ -153,8 +153,8 @@ __u64 xchg64_result = 0;
153153
__u32 xchg32_value = 1;
154154
__u32 xchg32_result = 0;
155155

156-
SEC("fentry/bpf_fentry_test1")
157-
int BPF_PROG(xchg, int a)
156+
SEC("raw_tp/sys_enter")
157+
int xchg(const void *ctx)
158158
{
159159
if (pid != (bpf_get_current_pid_tgid() >> 32))
160160
return 0;

0 commit comments

Comments
 (0)