1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 #include <sys/resource.h>
5 #include "test_send_signal_kern.skel.h"
7 static int sigusr1_received;
9 static void sigusr1_handler(int signum)
14 static void test_send_signal_common(struct perf_event_attr *attr,
17 struct test_send_signal_kern *skel;
18 int pipe_c2p[2], pipe_p2c[2];
19 int err = -1, pmu_fd = -1;
23 if (!ASSERT_OK(pipe(pipe_c2p), "pipe_c2p"))
26 if (!ASSERT_OK(pipe(pipe_p2c), "pipe_p2c")) {
33 if (!ASSERT_GE(pid, 0, "fork")) {
45 /* install signal handler and notify parent */
46 ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal");
48 close(pipe_c2p[0]); /* close read */
49 close(pipe_p2c[1]); /* close write */
51 /* boost with a high priority so we got a higher chance
52 * that if an interrupt happens, the underlying task
56 old_prio = getpriority(PRIO_PROCESS, 0);
57 ASSERT_OK(errno, "getpriority");
58 ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
60 /* notify parent signal handler is installed */
61 ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
63 /* make sure parent enabled bpf program to send_signal */
64 ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
66 /* wait a little for signal handler */
67 for (int i = 0; i < 1000000000 && !sigusr1_received; i++)
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");
74 /* wait for parent notification and exit */
75 ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
77 /* restore the old priority */
78 ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");
85 close(pipe_c2p[1]); /* close write */
86 close(pipe_p2c[0]); /* close read */
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;
93 err = test_send_signal_kern__attach(skel);
94 if (!ASSERT_OK(err, "skel_attach")) {
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")) {
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"))
112 /* wait until child signal handler installed */
113 ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
115 /* trigger the bpf send_signal */
116 skel->bss->signal_thread = signal_thread;
117 skel->bss->sig = SIGUSR1;
118 skel->bss->pid = pid;
120 /* notify child that bpf program can send_signal now */
121 ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
123 /* wait for result */
124 err = read(pipe_c2p[0], buf, 1);
125 if (!ASSERT_GE(err, 0, "reading pipe"))
127 if (!ASSERT_GT(err, 0, "reading pipe error: size 0")) {
132 ASSERT_EQ(buf[0], '2', "incorrect result");
134 /* notify child safe to exit */
135 ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
140 test_send_signal_kern__destroy(skel);
141 skel_open_load_failure:
147 static void test_send_signal_tracepoint(bool signal_thread)
149 test_send_signal_common(NULL, signal_thread);
152 static void test_send_signal_perf(bool signal_thread)
154 struct perf_event_attr attr = {
156 .type = PERF_TYPE_SOFTWARE,
157 .config = PERF_COUNT_SW_CPU_CLOCK,
160 test_send_signal_common(&attr, signal_thread);
163 static void test_send_signal_nmi(bool signal_thread)
165 struct perf_event_attr attr = {
167 .type = PERF_TYPE_HARDWARE,
168 .config = PERF_COUNT_HW_CPU_CYCLES,
172 /* Some setups (e.g. virtual machines) might run with hardware
173 * perf events disabled. If this is the case, skip this test.
175 pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
176 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
178 if (errno == ENOENT) {
179 printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
184 /* Let the test fail with a more informative message */
189 test_send_signal_common(&attr, signal_thread);
192 void test_send_signal(void)
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);