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
sigusr1_handler(int signum)9 static void sigusr1_handler(int signum)
10 {
11 sigusr1_received = 1;
12 }
13
test_send_signal_common(struct perf_event_attr * attr,bool signal_thread)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 if (!attr)
70 /* trigger the nanosleep tracepoint program. */
71 usleep(1);
72 }
73
74 buf[0] = sigusr1_received ? '2' : '0';
75 ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
76 ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
77
78 /* wait for parent notification and exit */
79 ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
80
81 /* restore the old priority */
82 ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");
83
84 close(pipe_c2p[1]);
85 close(pipe_p2c[0]);
86 exit(0);
87 }
88
89 close(pipe_c2p[1]); /* close write */
90 close(pipe_p2c[0]); /* close read */
91
92 skel = test_send_signal_kern__open_and_load();
93 if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
94 goto skel_open_load_failure;
95
96 if (!attr) {
97 err = test_send_signal_kern__attach(skel);
98 if (!ASSERT_OK(err, "skel_attach")) {
99 err = -1;
100 goto destroy_skel;
101 }
102 } else {
103 pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */,
104 -1 /* group id */, 0 /* flags */);
105 if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
106 err = -1;
107 goto destroy_skel;
108 }
109
110 skel->links.send_signal_perf =
111 bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd);
112 if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event"))
113 goto disable_pmu;
114 }
115
116 /* wait until child signal handler installed */
117 ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
118
119 /* trigger the bpf send_signal */
120 skel->bss->signal_thread = signal_thread;
121 skel->bss->sig = SIGUSR1;
122 skel->bss->pid = pid;
123
124 /* notify child that bpf program can send_signal now */
125 ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
126
127 /* wait for result */
128 err = read(pipe_c2p[0], buf, 1);
129 if (!ASSERT_GE(err, 0, "reading pipe"))
130 goto disable_pmu;
131 if (!ASSERT_GT(err, 0, "reading pipe error: size 0")) {
132 err = -1;
133 goto disable_pmu;
134 }
135
136 ASSERT_EQ(buf[0], '2', "incorrect result");
137
138 /* notify child safe to exit */
139 ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
140
141 disable_pmu:
142 close(pmu_fd);
143 destroy_skel:
144 test_send_signal_kern__destroy(skel);
145 skel_open_load_failure:
146 close(pipe_c2p[0]);
147 close(pipe_p2c[1]);
148 wait(NULL);
149 }
150
test_send_signal_tracepoint(bool signal_thread)151 static void test_send_signal_tracepoint(bool signal_thread)
152 {
153 test_send_signal_common(NULL, signal_thread);
154 }
155
test_send_signal_perf(bool signal_thread)156 static void test_send_signal_perf(bool signal_thread)
157 {
158 struct perf_event_attr attr = {
159 .sample_period = 1,
160 .type = PERF_TYPE_SOFTWARE,
161 .config = PERF_COUNT_SW_CPU_CLOCK,
162 };
163
164 test_send_signal_common(&attr, signal_thread);
165 }
166
test_send_signal_nmi(bool signal_thread)167 static void test_send_signal_nmi(bool signal_thread)
168 {
169 struct perf_event_attr attr = {
170 .sample_period = 1,
171 .type = PERF_TYPE_HARDWARE,
172 .config = PERF_COUNT_HW_CPU_CYCLES,
173 };
174 int pmu_fd;
175
176 /* Some setups (e.g. virtual machines) might run with hardware
177 * perf events disabled. If this is the case, skip this test.
178 */
179 pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
180 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
181 if (pmu_fd == -1) {
182 if (errno == ENOENT) {
183 printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
184 __func__);
185 test__skip();
186 return;
187 }
188 /* Let the test fail with a more informative message */
189 } else {
190 close(pmu_fd);
191 }
192
193 test_send_signal_common(&attr, signal_thread);
194 }
195
test_send_signal(void)196 void test_send_signal(void)
197 {
198 if (test__start_subtest("send_signal_tracepoint"))
199 test_send_signal_tracepoint(false);
200 if (test__start_subtest("send_signal_perf"))
201 test_send_signal_perf(false);
202 if (test__start_subtest("send_signal_nmi"))
203 test_send_signal_nmi(false);
204 if (test__start_subtest("send_signal_tracepoint_thread"))
205 test_send_signal_tracepoint(true);
206 if (test__start_subtest("send_signal_perf_thread"))
207 test_send_signal_perf(true);
208 if (test__start_subtest("send_signal_nmi_thread"))
209 test_send_signal_nmi(true);
210 }
211