1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_send_signal_kern.skel.h"
5 static volatile int sigusr1_received
= 0;
7 static void sigusr1_handler(int signum
)
12 static void test_send_signal_common(struct perf_event_attr
*attr
,
14 const char *test_name
)
16 struct test_send_signal_kern
*skel
;
17 int pipe_c2p
[2], pipe_p2c
[2];
18 int err
= -1, pmu_fd
= -1;
23 if (CHECK(pipe(pipe_c2p
), test_name
,
24 "pipe pipe_c2p error: %s\n", strerror(errno
)))
27 if (CHECK(pipe(pipe_p2c
), test_name
,
28 "pipe pipe_p2c error: %s\n", strerror(errno
))) {
35 if (CHECK(pid
< 0, test_name
, "fork error: %s\n", strerror(errno
))) {
44 /* install signal handler and notify parent */
45 signal(SIGUSR1
, sigusr1_handler
);
47 close(pipe_c2p
[0]); /* close read */
48 close(pipe_p2c
[1]); /* close write */
50 /* notify parent signal handler is installed */
51 write(pipe_c2p
[1], buf
, 1);
53 /* make sure parent enabled bpf program to send_signal */
54 read(pipe_p2c
[0], buf
, 1);
56 /* wait a little for signal handler */
60 write(pipe_c2p
[1], "2", 1);
62 write(pipe_c2p
[1], "0", 1);
64 /* wait for parent notification and exit */
65 read(pipe_p2c
[0], buf
, 1);
72 close(pipe_c2p
[1]); /* close write */
73 close(pipe_p2c
[0]); /* close read */
75 skel
= test_send_signal_kern__open_and_load();
76 if (CHECK(!skel
, "skel_open_and_load", "skeleton open_and_load failed\n"))
77 goto skel_open_load_failure
;
80 err
= test_send_signal_kern__attach(skel
);
81 if (CHECK(err
, "skel_attach", "skeleton attach failed\n")) {
86 pmu_fd
= syscall(__NR_perf_event_open
, attr
, pid
, -1,
87 -1 /* group id */, 0 /* flags */);
88 if (CHECK(pmu_fd
< 0, test_name
, "perf_event_open error: %s\n",
94 skel
->links
.send_signal_perf
=
95 bpf_program__attach_perf_event(skel
->progs
.send_signal_perf
, pmu_fd
);
96 if (CHECK(IS_ERR(skel
->links
.send_signal_perf
), "attach_perf_event",
97 "err %ld\n", PTR_ERR(skel
->links
.send_signal_perf
)))
101 /* wait until child signal handler installed */
102 read(pipe_c2p
[0], buf
, 1);
104 /* trigger the bpf send_signal */
105 skel
->bss
->pid
= pid
;
106 skel
->bss
->sig
= SIGUSR1
;
107 skel
->bss
->signal_thread
= signal_thread
;
109 /* notify child that bpf program can send_signal now */
110 write(pipe_p2c
[1], buf
, 1);
112 /* wait for result */
113 err
= read(pipe_c2p
[0], buf
, 1);
114 if (CHECK(err
< 0, test_name
, "reading pipe error: %s\n", strerror(errno
)))
116 if (CHECK(err
== 0, test_name
, "reading pipe error: size 0\n")) {
121 CHECK(buf
[0] != '2', test_name
, "incorrect result\n");
123 /* notify child safe to exit */
124 write(pipe_p2c
[1], buf
, 1);
129 test_send_signal_kern__destroy(skel
);
130 skel_open_load_failure
:
136 static void test_send_signal_tracepoint(bool signal_thread
)
138 test_send_signal_common(NULL
, signal_thread
, "tracepoint");
141 static void test_send_signal_perf(bool signal_thread
)
143 struct perf_event_attr attr
= {
145 .type
= PERF_TYPE_SOFTWARE
,
146 .config
= PERF_COUNT_SW_CPU_CLOCK
,
149 test_send_signal_common(&attr
, signal_thread
, "perf_sw_event");
152 static void test_send_signal_nmi(bool signal_thread
)
154 struct perf_event_attr attr
= {
156 .type
= PERF_TYPE_HARDWARE
,
157 .config
= PERF_COUNT_HW_CPU_CYCLES
,
161 /* Some setups (e.g. virtual machines) might run with hardware
162 * perf events disabled. If this is the case, skip this test.
164 pmu_fd
= syscall(__NR_perf_event_open
, &attr
, 0 /* pid */,
165 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
167 if (errno
== ENOENT
) {
168 printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
173 /* Let the test fail with a more informative message */
178 test_send_signal_common(&attr
, signal_thread
, "perf_hw_event");
181 void test_send_signal(void)
183 if (test__start_subtest("send_signal_tracepoint"))
184 test_send_signal_tracepoint(false);
185 if (test__start_subtest("send_signal_perf"))
186 test_send_signal_perf(false);
187 if (test__start_subtest("send_signal_nmi"))
188 test_send_signal_nmi(false);
189 if (test__start_subtest("send_signal_tracepoint_thread"))
190 test_send_signal_tracepoint(true);
191 if (test__start_subtest("send_signal_perf_thread"))
192 test_send_signal_perf(true);
193 if (test__start_subtest("send_signal_nmi_thread"))
194 test_send_signal_nmi(true);