selftests/bpf: Bump internal send_signal/send_signal_tracepoint timeout
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / prog_tests / send_signal.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <sys/time.h>
4 #include <sys/resource.h>
5 #include "test_send_signal_kern.skel.h"
6
7 static int sigusr1_received;
8
9 static void sigusr1_handler(int signum)
10 {
11         sigusr1_received = 1;
12 }
13
14 static void test_send_signal_common(struct perf_event_attr *attr,
15                                     bool signal_thread)
16 {
17         struct test_send_signal_kern *skel;
18         int pipe_c2p[2], pipe_p2c[2];
19         int err = -1, pmu_fd = -1;
20         char buf[256];
21         pid_t pid;
22
23         if (!ASSERT_OK(pipe(pipe_c2p), "pipe_c2p"))
24                 return;
25
26         if (!ASSERT_OK(pipe(pipe_p2c), "pipe_p2c")) {
27                 close(pipe_c2p[0]);
28                 close(pipe_c2p[1]);
29                 return;
30         }
31
32         pid = fork();
33         if (!ASSERT_GE(pid, 0, "fork")) {
34                 close(pipe_c2p[0]);
35                 close(pipe_c2p[1]);
36                 close(pipe_p2c[0]);
37                 close(pipe_p2c[1]);
38                 return;
39         }
40
41         if (pid == 0) {
42                 int old_prio;
43                 volatile int j = 0;
44
45                 /* install signal handler and notify parent */
46                 ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal");
47
48                 close(pipe_c2p[0]); /* close read */
49                 close(pipe_p2c[1]); /* close write */
50
51                 /* boost with a high priority so we got a higher chance
52                  * that if an interrupt happens, the underlying task
53                  * is this process.
54                  */
55                 errno = 0;
56                 old_prio = getpriority(PRIO_PROCESS, 0);
57                 ASSERT_OK(errno, "getpriority");
58                 ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
59
60                 /* notify parent signal handler is installed */
61                 ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
62
63                 /* make sure parent enabled bpf program to send_signal */
64                 ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
65
66                 /* wait a little for signal handler */
67                 for (int i = 0; i < 1000000000 && !sigusr1_received; i++)
68                         j /= i + j + 1;
69
70                 buf[0] = sigusr1_received ? '2' : '0';
71                 ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
72                 ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
73
74                 /* wait for parent notification and exit */
75                 ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
76
77                 /* restore the old priority */
78                 ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");
79
80                 close(pipe_c2p[1]);
81                 close(pipe_p2c[0]);
82                 exit(0);
83         }
84
85         close(pipe_c2p[1]); /* close write */
86         close(pipe_p2c[0]); /* close read */
87
88         skel = test_send_signal_kern__open_and_load();
89         if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
90                 goto skel_open_load_failure;
91
92         if (!attr) {
93                 err = test_send_signal_kern__attach(skel);
94                 if (!ASSERT_OK(err, "skel_attach")) {
95                         err = -1;
96                         goto destroy_skel;
97                 }
98         } else {
99                 pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */,
100                                  -1 /* group id */, 0 /* flags */);
101                 if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
102                         err = -1;
103                         goto destroy_skel;
104                 }
105
106                 skel->links.send_signal_perf =
107                         bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd);
108                 if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event"))
109                         goto disable_pmu;
110         }
111
112         /* wait until child signal handler installed */
113         ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
114
115         /* trigger the bpf send_signal */
116         skel->bss->signal_thread = signal_thread;
117         skel->bss->sig = SIGUSR1;
118         skel->bss->pid = pid;
119
120         /* notify child that bpf program can send_signal now */
121         ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
122
123         /* wait for result */
124         err = read(pipe_c2p[0], buf, 1);
125         if (!ASSERT_GE(err, 0, "reading pipe"))
126                 goto disable_pmu;
127         if (!ASSERT_GT(err, 0, "reading pipe error: size 0")) {
128                 err = -1;
129                 goto disable_pmu;
130         }
131
132         ASSERT_EQ(buf[0], '2', "incorrect result");
133
134         /* notify child safe to exit */
135         ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
136
137 disable_pmu:
138         close(pmu_fd);
139 destroy_skel:
140         test_send_signal_kern__destroy(skel);
141 skel_open_load_failure:
142         close(pipe_c2p[0]);
143         close(pipe_p2c[1]);
144         wait(NULL);
145 }
146
147 static void test_send_signal_tracepoint(bool signal_thread)
148 {
149         test_send_signal_common(NULL, signal_thread);
150 }
151
152 static void test_send_signal_perf(bool signal_thread)
153 {
154         struct perf_event_attr attr = {
155                 .sample_period = 1,
156                 .type = PERF_TYPE_SOFTWARE,
157                 .config = PERF_COUNT_SW_CPU_CLOCK,
158         };
159
160         test_send_signal_common(&attr, signal_thread);
161 }
162
163 static void test_send_signal_nmi(bool signal_thread)
164 {
165         struct perf_event_attr attr = {
166                 .sample_period = 1,
167                 .type = PERF_TYPE_HARDWARE,
168                 .config = PERF_COUNT_HW_CPU_CYCLES,
169         };
170         int pmu_fd;
171
172         /* Some setups (e.g. virtual machines) might run with hardware
173          * perf events disabled. If this is the case, skip this test.
174          */
175         pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
176                          -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
177         if (pmu_fd == -1) {
178                 if (errno == ENOENT) {
179                         printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
180                                __func__);
181                         test__skip();
182                         return;
183                 }
184                 /* Let the test fail with a more informative message */
185         } else {
186                 close(pmu_fd);
187         }
188
189         test_send_signal_common(&attr, signal_thread);
190 }
191
192 void test_send_signal(void)
193 {
194         if (test__start_subtest("send_signal_tracepoint"))
195                 test_send_signal_tracepoint(false);
196         if (test__start_subtest("send_signal_perf"))
197                 test_send_signal_perf(false);
198         if (test__start_subtest("send_signal_nmi"))
199                 test_send_signal_nmi(false);
200         if (test__start_subtest("send_signal_tracepoint_thread"))
201                 test_send_signal_tracepoint(true);
202         if (test__start_subtest("send_signal_perf_thread"))
203                 test_send_signal_perf(true);
204         if (test__start_subtest("send_signal_nmi_thread"))
205                 test_send_signal_nmi(true);
206 }