Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2
3#define _GNU_SOURCE
4#include <sched.h>
5#include <test_progs.h>
6#include <pthread.h>
7#include <network_helpers.h>
8#include <sys/sysinfo.h>
9
10#include "timer_lockup.skel.h"
11
12static long cpu;
13static int *timer1_err;
14static int *timer2_err;
15static bool skip;
16
17volatile int k = 0;
18
19static void *timer_lockup_thread(void *arg)
20{
21 LIBBPF_OPTS(bpf_test_run_opts, opts,
22 .data_in = &pkt_v4,
23 .data_size_in = sizeof(pkt_v4),
24 .repeat = 1000,
25 );
26 int i, prog_fd = *(int *)arg;
27 cpu_set_t cpuset;
28
29 CPU_ZERO(&cpuset);
30 CPU_SET(__sync_fetch_and_add(&cpu, 1), &cpuset);
31 ASSERT_OK(pthread_setaffinity_np(pthread_self(), sizeof(cpuset),
32 &cpuset),
33 "cpu affinity");
34
35 for (i = 0; !READ_ONCE(*timer1_err) && !READ_ONCE(*timer2_err); i++) {
36 bpf_prog_test_run_opts(prog_fd, &opts);
37 /* Skip the test if we can't reproduce the race in a reasonable
38 * amount of time.
39 */
40 if (i > 50) {
41 WRITE_ONCE(skip, true);
42 break;
43 }
44 }
45
46 return NULL;
47}
48
49void test_timer_lockup(void)
50{
51 int timer1_prog, timer2_prog;
52 struct timer_lockup *skel;
53 pthread_t thrds[2];
54 void *ret;
55
56 if (get_nprocs() < 2) {
57 test__skip();
58 return;
59 }
60
61 skel = timer_lockup__open_and_load();
62 if (!skel && errno == EOPNOTSUPP) {
63 test__skip();
64 return;
65 }
66 if (!ASSERT_OK_PTR(skel, "timer_lockup__open_and_load"))
67 return;
68
69 timer1_prog = bpf_program__fd(skel->progs.timer1_prog);
70 timer2_prog = bpf_program__fd(skel->progs.timer2_prog);
71
72 timer1_err = &skel->bss->timer1_err;
73 timer2_err = &skel->bss->timer2_err;
74
75 if (!ASSERT_OK(pthread_create(&thrds[0], NULL, timer_lockup_thread,
76 &timer1_prog),
77 "pthread_create thread1"))
78 goto out;
79 if (!ASSERT_OK(pthread_create(&thrds[1], NULL, timer_lockup_thread,
80 &timer2_prog),
81 "pthread_create thread2")) {
82 pthread_exit(&thrds[0]);
83 goto out;
84 }
85
86 pthread_join(thrds[1], &ret);
87 pthread_join(thrds[0], &ret);
88
89 if (skip) {
90 test__skip();
91 goto out;
92 }
93
94 if (*timer1_err != -EDEADLK && *timer1_err != 0)
95 ASSERT_FAIL("timer1_err bad value");
96 if (*timer2_err != -EDEADLK && *timer2_err != 0)
97 ASSERT_FAIL("timer2_err bad value");
98out:
99 timer_lockup__destroy(skel);
100 return;
101}