1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include <sched.h>
#include <test_progs.h>
#include <linux/perf_event.h>
#include <sys/syscall.h>
#include "timer.skel.h"
#include "timer_failure.skel.h"
#include "timer_interrupt.skel.h"
#define NUM_THR 8
static int perf_event_open(__u32 type, __u64 config, int pid, int cpu)
{
struct perf_event_attr attr = {
.type = type,
.config = config,
.size = sizeof(struct perf_event_attr),
.sample_period = 10000,
};
return syscall(__NR_perf_event_open, &attr, pid, cpu, -1, 0);
}
static void *spin_lock_thread(void *arg)
{
int i, err, prog_fd = *(int *)arg;
LIBBPF_OPTS(bpf_test_run_opts, topts);
for (i = 0; i < 10000; i++) {
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err") ||
!ASSERT_OK(topts.retval, "test_run_opts retval"))
break;
}
pthread_exit(arg);
}
static int timer_stress_runner(struct timer *timer_skel, bool async_cancel)
{
int i, err = 1, prog_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);
pthread_t thread_id[NUM_THR];
void *ret;
timer_skel->bss->async_cancel = async_cancel;
prog_fd = bpf_program__fd(timer_skel->progs.race);
for (i = 0; i < NUM_THR; i++) {
err = pthread_create(&thread_id[i], NULL,
&spin_lock_thread, &prog_fd);
if (!ASSERT_OK(err, "pthread_create"))
break;
}
while (i) {
err = pthread_join(thread_id[--i], &ret);
if (ASSERT_OK(err, "pthread_join"))
ASSERT_EQ(ret, (void *)&prog_fd, "pthread_join");
}
return err;
}
static int timer_stress(struct timer *timer_skel)
{
return timer_stress_runner(timer_skel, false);
}
static int timer_stress_async_cancel(struct timer *timer_skel)
{
return timer_stress_runner(timer_skel, true);
}
static void *nmi_cpu_worker(void *arg)
{
volatile __u64 num = 1;
int i;
for (i = 0; i < 500000000; ++i)
num *= (i % 7) + 1;
(void)num;
return NULL;
}
static int run_nmi_test(struct timer *timer_skel, struct bpf_program *prog)
{
struct bpf_link *link = NULL;
int pe_fd = -1, pipefd[2] = {-1, -1}, pid = 0, status;
char buf = 0;
int ret = -1;
if (!ASSERT_OK(pipe(pipefd), "pipe"))
goto cleanup;
pid = fork();
if (pid == 0) {
/* Child: spawn multiple threads to consume multiple CPUs */
pthread_t threads[NUM_THR];
int i;
close(pipefd[1]);
read(pipefd[0], &buf, 1);
close(pipefd[0]);
for (i = 0; i < NUM_THR; i++)
pthread_create(&threads[i], NULL, nmi_cpu_worker, NULL);
for (i = 0; i < NUM_THR; i++)
pthread_join(threads[i], NULL);
exit(0);
}
if (!ASSERT_GE(pid, 0, "fork"))
goto cleanup;
/* Open perf event for child process across all CPUs */
pe_fd = perf_event_open(PERF_TYPE_HARDWARE,
PERF_COUNT_HW_CPU_CYCLES,
pid, /* measure child process */
-1); /* on any CPU */
if (pe_fd < 0) {
if (errno == ENOENT || errno == EOPNOTSUPP) {
printf("SKIP:no PERF_COUNT_HW_CPU_CYCLES\n");
test__skip();
ret = EOPNOTSUPP;
goto cleanup;
}
ASSERT_GE(pe_fd, 0, "perf_event_open");
goto cleanup;
}
link = bpf_program__attach_perf_event(prog, pe_fd);
if (!ASSERT_OK_PTR(link, "attach_perf_event"))
goto cleanup;
pe_fd = -1; /* Ownership transferred to link */
/* Signal child to start CPU work */
close(pipefd[0]);
pipefd[0] = -1;
write(pipefd[1], &buf, 1);
close(pipefd[1]);
pipefd[1] = -1;
waitpid(pid, &status, 0);
pid = 0;
/* Verify NMI context was hit */
ASSERT_GT(timer_skel->bss->test_hits, 0, "test_hits");
ret = 0;
cleanup:
bpf_link__destroy(link);
if (pe_fd >= 0)
close(pe_fd);
if (pid > 0) {
write(pipefd[1], &buf, 1);
waitpid(pid, &status, 0);
}
if (pipefd[0] >= 0)
close(pipefd[0]);
if (pipefd[1] >= 0)
close(pipefd[1]);
return ret;
}
static int timer_stress_nmi_race(struct timer *timer_skel)
{
int err;
err = run_nmi_test(timer_skel, timer_skel->progs.nmi_race);
if (err == EOPNOTSUPP)
return 0;
return err;
}
static int timer_stress_nmi_update(struct timer *timer_skel)
{
int err;
err = run_nmi_test(timer_skel, timer_skel->progs.nmi_update);
if (err == EOPNOTSUPP)
return 0;
if (err)
return err;
ASSERT_GT(timer_skel->bss->update_hits, 0, "update_hits");
return 0;
}
static int timer_stress_nmi_cancel(struct timer *timer_skel)
{
int err;
err = run_nmi_test(timer_skel, timer_skel->progs.nmi_cancel);
if (err == EOPNOTSUPP)
return 0;
if (err)
return err;
ASSERT_GT(timer_skel->bss->cancel_hits, 0, "cancel_hits");
return 0;
}
static int timer(struct timer *timer_skel)
{
int err, prog_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);
err = timer__attach(timer_skel);
if (!ASSERT_OK(err, "timer_attach"))
return err;
ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
ASSERT_EQ(timer_skel->bss->pinned_callback_check, 0, "pinned_callback_check1");
prog_fd = bpf_program__fd(timer_skel->progs.test1);
err = bpf_prog_test_run_opts(prog_fd, &topts);
ASSERT_OK(err, "test_run");
ASSERT_EQ(topts.retval, 0, "test_run");
timer__detach(timer_skel);
usleep(50); /* 10 usecs should be enough, but give it extra */
/* check that timer_cb1() was executed 10+10 times */
ASSERT_EQ(timer_skel->data->callback_check, 42, "callback_check2");
ASSERT_EQ(timer_skel->data->callback2_check, 42, "callback2_check2");
/* check that timer_cb2() was executed twice */
ASSERT_EQ(timer_skel->bss->bss_data, 10, "bss_data");
/* check that timer_cb3() was executed twice */
ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data");
/* check that timer_cb_pinned() was executed twice */
ASSERT_EQ(timer_skel->bss->pinned_callback_check, 2, "pinned_callback_check");
/* check that there were no errors in timer execution */
ASSERT_EQ(timer_skel->bss->err, 0, "err");
/* check that code paths completed */
ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
return 0;
}
static int timer_cancel_async(struct timer *timer_skel)
{
int err, prog_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);
prog_fd = bpf_program__fd(timer_skel->progs.test_async_cancel_succeed);
err = bpf_prog_test_run_opts(prog_fd, &topts);
ASSERT_OK(err, "test_run");
ASSERT_EQ(topts.retval, 0, "test_run");
usleep(500);
/* check that there were no errors in timer execution */
ASSERT_EQ(timer_skel->bss->err, 0, "err");
/* check that code paths completed */
ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
return 0;
}
static void test_timer(int (*timer_test_fn)(struct timer *timer_skel))
{
struct timer *timer_skel = NULL;
int err;
timer_skel = timer__open_and_load();
if (!timer_skel && errno == EOPNOTSUPP) {
test__skip();
return;
}
if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
return;
err = timer_test_fn(timer_skel);
ASSERT_OK(err, "timer");
timer__destroy(timer_skel);
}
void serial_test_timer(void)
{
test_timer(timer);
RUN_TESTS(timer_failure);
}
void serial_test_timer_stress(void)
{
test_timer(timer_stress);
}
void serial_test_timer_stress_async_cancel(void)
{
test_timer(timer_stress_async_cancel);
}
void serial_test_timer_async_cancel(void)
{
test_timer(timer_cancel_async);
}
void serial_test_timer_stress_nmi_race(void)
{
test_timer(timer_stress_nmi_race);
}
void serial_test_timer_stress_nmi_update(void)
{
test_timer(timer_stress_nmi_update);
}
void serial_test_timer_stress_nmi_cancel(void)
{
test_timer(timer_stress_nmi_cancel);
}
void test_timer_interrupt(void)
{
struct timer_interrupt *skel = NULL;
int err, prog_fd;
LIBBPF_OPTS(bpf_test_run_opts, opts);
skel = timer_interrupt__open_and_load();
if (!skel && errno == EOPNOTSUPP) {
test__skip();
return;
}
if (!ASSERT_OK_PTR(skel, "timer_interrupt__open_and_load"))
return;
err = timer_interrupt__attach(skel);
if (!ASSERT_OK(err, "timer_interrupt__attach"))
goto out;
prog_fd = bpf_program__fd(skel->progs.test_timer_interrupt);
err = bpf_prog_test_run_opts(prog_fd, &opts);
if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
goto out;
usleep(50);
ASSERT_EQ(skel->bss->in_interrupt, 0, "in_interrupt");
if (skel->bss->preempt_count)
ASSERT_NEQ(skel->bss->in_interrupt_cb, 0, "in_interrupt_cb");
out:
timer_interrupt__destroy(skel);
}
|