Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <test_progs.h>
3
4static volatile int sigusr1_received = 0;
5
6static void sigusr1_handler(int signum)
7{
8 sigusr1_received++;
9}
10
11static void test_send_signal_common(struct perf_event_attr *attr,
12 int prog_type,
13 const char *test_name)
14{
15 int err = -1, pmu_fd, prog_fd, info_map_fd, status_map_fd;
16 const char *file = "./test_send_signal_kern.o";
17 struct bpf_object *obj = NULL;
18 int pipe_c2p[2], pipe_p2c[2];
19 __u32 key = 0, duration = 0;
20 char buf[256];
21 pid_t pid;
22 __u64 val;
23
24 if (CHECK(pipe(pipe_c2p), test_name,
25 "pipe pipe_c2p error: %s\n", strerror(errno)))
26 return;
27
28 if (CHECK(pipe(pipe_p2c), test_name,
29 "pipe pipe_p2c error: %s\n", strerror(errno))) {
30 close(pipe_c2p[0]);
31 close(pipe_c2p[1]);
32 return;
33 }
34
35 pid = fork();
36 if (CHECK(pid < 0, test_name, "fork error: %s\n", strerror(errno))) {
37 close(pipe_c2p[0]);
38 close(pipe_c2p[1]);
39 close(pipe_p2c[0]);
40 close(pipe_p2c[1]);
41 return;
42 }
43
44 if (pid == 0) {
45 /* install signal handler and notify parent */
46 signal(SIGUSR1, sigusr1_handler);
47
48 close(pipe_c2p[0]); /* close read */
49 close(pipe_p2c[1]); /* close write */
50
51 /* notify parent signal handler is installed */
52 write(pipe_c2p[1], buf, 1);
53
54 /* make sure parent enabled bpf program to send_signal */
55 read(pipe_p2c[0], buf, 1);
56
57 /* wait a little for signal handler */
58 sleep(1);
59
60 if (sigusr1_received)
61 write(pipe_c2p[1], "2", 1);
62 else
63 write(pipe_c2p[1], "0", 1);
64
65 /* wait for parent notification and exit */
66 read(pipe_p2c[0], buf, 1);
67
68 close(pipe_c2p[1]);
69 close(pipe_p2c[0]);
70 exit(0);
71 }
72
73 close(pipe_c2p[1]); /* close write */
74 close(pipe_p2c[0]); /* close read */
75
76 err = bpf_prog_load(file, prog_type, &obj, &prog_fd);
77 if (CHECK(err < 0, test_name, "bpf_prog_load error: %s\n",
78 strerror(errno)))
79 goto prog_load_failure;
80
81 pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1,
82 -1 /* group id */, 0 /* flags */);
83 if (CHECK(pmu_fd < 0, test_name, "perf_event_open error: %s\n",
84 strerror(errno))) {
85 err = -1;
86 goto close_prog;
87 }
88
89 err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
90 if (CHECK(err < 0, test_name, "ioctl perf_event_ioc_enable error: %s\n",
91 strerror(errno)))
92 goto disable_pmu;
93
94 err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
95 if (CHECK(err < 0, test_name, "ioctl perf_event_ioc_set_bpf error: %s\n",
96 strerror(errno)))
97 goto disable_pmu;
98
99 err = -1;
100 info_map_fd = bpf_object__find_map_fd_by_name(obj, "info_map");
101 if (CHECK(info_map_fd < 0, test_name, "find map %s error\n", "info_map"))
102 goto disable_pmu;
103
104 status_map_fd = bpf_object__find_map_fd_by_name(obj, "status_map");
105 if (CHECK(status_map_fd < 0, test_name, "find map %s error\n", "status_map"))
106 goto disable_pmu;
107
108 /* wait until child signal handler installed */
109 read(pipe_c2p[0], buf, 1);
110
111 /* trigger the bpf send_signal */
112 key = 0;
113 val = (((__u64)(SIGUSR1)) << 32) | pid;
114 bpf_map_update_elem(info_map_fd, &key, &val, 0);
115
116 /* notify child that bpf program can send_signal now */
117 write(pipe_p2c[1], buf, 1);
118
119 /* wait for result */
120 err = read(pipe_c2p[0], buf, 1);
121 if (CHECK(err < 0, test_name, "reading pipe error: %s\n", strerror(errno)))
122 goto disable_pmu;
123 if (CHECK(err == 0, test_name, "reading pipe error: size 0\n")) {
124 err = -1;
125 goto disable_pmu;
126 }
127
128 CHECK(buf[0] != '2', test_name, "incorrect result\n");
129
130 /* notify child safe to exit */
131 write(pipe_p2c[1], buf, 1);
132
133disable_pmu:
134 close(pmu_fd);
135close_prog:
136 bpf_object__close(obj);
137prog_load_failure:
138 close(pipe_c2p[0]);
139 close(pipe_p2c[1]);
140 wait(NULL);
141}
142
143static void test_send_signal_tracepoint(void)
144{
145 const char *id_path = "/sys/kernel/debug/tracing/events/syscalls/sys_enter_nanosleep/id";
146 struct perf_event_attr attr = {
147 .type = PERF_TYPE_TRACEPOINT,
148 .sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN,
149 .sample_period = 1,
150 .wakeup_events = 1,
151 };
152 __u32 duration = 0;
153 int bytes, efd;
154 char buf[256];
155
156 efd = open(id_path, O_RDONLY, 0);
157 if (CHECK(efd < 0, "tracepoint",
158 "open syscalls/sys_enter_nanosleep/id failure: %s\n",
159 strerror(errno)))
160 return;
161
162 bytes = read(efd, buf, sizeof(buf));
163 close(efd);
164 if (CHECK(bytes <= 0 || bytes >= sizeof(buf), "tracepoint",
165 "read syscalls/sys_enter_nanosleep/id failure: %s\n",
166 strerror(errno)))
167 return;
168
169 attr.config = strtol(buf, NULL, 0);
170
171 test_send_signal_common(&attr, BPF_PROG_TYPE_TRACEPOINT, "tracepoint");
172}
173
174static void test_send_signal_perf(void)
175{
176 struct perf_event_attr attr = {
177 .sample_period = 1,
178 .type = PERF_TYPE_SOFTWARE,
179 .config = PERF_COUNT_SW_CPU_CLOCK,
180 };
181
182 test_send_signal_common(&attr, BPF_PROG_TYPE_PERF_EVENT,
183 "perf_sw_event");
184}
185
186static void test_send_signal_nmi(void)
187{
188 struct perf_event_attr attr = {
189 .sample_freq = 50,
190 .freq = 1,
191 .type = PERF_TYPE_HARDWARE,
192 .config = PERF_COUNT_HW_CPU_CYCLES,
193 };
194 int pmu_fd;
195
196 /* Some setups (e.g. virtual machines) might run with hardware
197 * perf events disabled. If this is the case, skip this test.
198 */
199 pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
200 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
201 if (pmu_fd == -1) {
202 if (errno == ENOENT) {
203 printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
204 __func__);
205 test__skip();
206 return;
207 }
208 /* Let the test fail with a more informative message */
209 } else {
210 close(pmu_fd);
211 }
212
213 test_send_signal_common(&attr, BPF_PROG_TYPE_PERF_EVENT,
214 "perf_hw_event");
215}
216
217void test_send_signal(void)
218{
219 if (test__start_subtest("send_signal_tracepoint"))
220 test_send_signal_tracepoint();
221 if (test__start_subtest("send_signal_perf"))
222 test_send_signal_perf();
223 if (test__start_subtest("send_signal_nmi"))
224 test_send_signal_nmi();
225}
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
7int sigusr1_received = 0;
8
9static void sigusr1_handler(int signum)
10{
11 sigusr1_received++;
12}
13
14static void test_send_signal_common(struct perf_event_attr *attr,
15 bool signal_thread,
16 const char *test_name)
17{
18 struct test_send_signal_kern *skel;
19 int pipe_c2p[2], pipe_p2c[2];
20 int err = -1, pmu_fd = -1;
21 __u32 duration = 0;
22 char buf[256];
23 pid_t pid;
24
25 if (CHECK(pipe(pipe_c2p), test_name,
26 "pipe pipe_c2p error: %s\n", strerror(errno)))
27 return;
28
29 if (CHECK(pipe(pipe_p2c), test_name,
30 "pipe pipe_p2c error: %s\n", strerror(errno))) {
31 close(pipe_c2p[0]);
32 close(pipe_c2p[1]);
33 return;
34 }
35
36 pid = fork();
37 if (CHECK(pid < 0, test_name, "fork error: %s\n", strerror(errno))) {
38 close(pipe_c2p[0]);
39 close(pipe_c2p[1]);
40 close(pipe_p2c[0]);
41 close(pipe_p2c[1]);
42 return;
43 }
44
45 if (pid == 0) {
46 int old_prio;
47
48 /* install signal handler and notify parent */
49 signal(SIGUSR1, sigusr1_handler);
50
51 close(pipe_c2p[0]); /* close read */
52 close(pipe_p2c[1]); /* close write */
53
54 /* boost with a high priority so we got a higher chance
55 * that if an interrupt happens, the underlying task
56 * is this process.
57 */
58 errno = 0;
59 old_prio = getpriority(PRIO_PROCESS, 0);
60 ASSERT_OK(errno, "getpriority");
61 ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
62
63 /* notify parent signal handler is installed */
64 CHECK(write(pipe_c2p[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
65
66 /* make sure parent enabled bpf program to send_signal */
67 CHECK(read(pipe_p2c[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
68
69 /* wait a little for signal handler */
70 sleep(1);
71
72 buf[0] = sigusr1_received ? '2' : '0';
73 CHECK(write(pipe_c2p[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
74
75 /* wait for parent notification and exit */
76 CHECK(read(pipe_p2c[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
77
78 /* restore the old priority */
79 ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");
80
81 close(pipe_c2p[1]);
82 close(pipe_p2c[0]);
83 exit(0);
84 }
85
86 close(pipe_c2p[1]); /* close write */
87 close(pipe_p2c[0]); /* close read */
88
89 skel = test_send_signal_kern__open_and_load();
90 if (CHECK(!skel, "skel_open_and_load", "skeleton open_and_load failed\n"))
91 goto skel_open_load_failure;
92
93 if (!attr) {
94 err = test_send_signal_kern__attach(skel);
95 if (CHECK(err, "skel_attach", "skeleton attach failed\n")) {
96 err = -1;
97 goto destroy_skel;
98 }
99 } else {
100 pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1,
101 -1 /* group id */, 0 /* flags */);
102 if (CHECK(pmu_fd < 0, test_name, "perf_event_open error: %s\n",
103 strerror(errno))) {
104 err = -1;
105 goto destroy_skel;
106 }
107
108 skel->links.send_signal_perf =
109 bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd);
110 if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event"))
111 goto disable_pmu;
112 }
113
114 /* wait until child signal handler installed */
115 CHECK(read(pipe_c2p[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
116
117 /* trigger the bpf send_signal */
118 skel->bss->pid = pid;
119 skel->bss->sig = SIGUSR1;
120 skel->bss->signal_thread = signal_thread;
121
122 /* notify child that bpf program can send_signal now */
123 CHECK(write(pipe_p2c[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
124
125 /* wait for result */
126 err = read(pipe_c2p[0], buf, 1);
127 if (CHECK(err < 0, test_name, "reading pipe error: %s\n", strerror(errno)))
128 goto disable_pmu;
129 if (CHECK(err == 0, test_name, "reading pipe error: size 0\n")) {
130 err = -1;
131 goto disable_pmu;
132 }
133
134 CHECK(buf[0] != '2', test_name, "incorrect result\n");
135
136 /* notify child safe to exit */
137 CHECK(write(pipe_p2c[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
138
139disable_pmu:
140 close(pmu_fd);
141destroy_skel:
142 test_send_signal_kern__destroy(skel);
143skel_open_load_failure:
144 close(pipe_c2p[0]);
145 close(pipe_p2c[1]);
146 wait(NULL);
147}
148
149static void test_send_signal_tracepoint(bool signal_thread)
150{
151 test_send_signal_common(NULL, signal_thread, "tracepoint");
152}
153
154static void test_send_signal_perf(bool signal_thread)
155{
156 struct perf_event_attr attr = {
157 .sample_period = 1,
158 .type = PERF_TYPE_SOFTWARE,
159 .config = PERF_COUNT_SW_CPU_CLOCK,
160 };
161
162 test_send_signal_common(&attr, signal_thread, "perf_sw_event");
163}
164
165static void test_send_signal_nmi(bool signal_thread)
166{
167 struct perf_event_attr attr = {
168 .sample_period = 1,
169 .type = PERF_TYPE_HARDWARE,
170 .config = PERF_COUNT_HW_CPU_CYCLES,
171 };
172 int pmu_fd;
173
174 /* Some setups (e.g. virtual machines) might run with hardware
175 * perf events disabled. If this is the case, skip this test.
176 */
177 pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
178 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
179 if (pmu_fd == -1) {
180 if (errno == ENOENT) {
181 printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
182 __func__);
183 test__skip();
184 return;
185 }
186 /* Let the test fail with a more informative message */
187 } else {
188 close(pmu_fd);
189 }
190
191 test_send_signal_common(&attr, signal_thread, "perf_hw_event");
192}
193
194void test_send_signal(void)
195{
196 if (test__start_subtest("send_signal_tracepoint"))
197 test_send_signal_tracepoint(false);
198 if (test__start_subtest("send_signal_perf"))
199 test_send_signal_perf(false);
200 if (test__start_subtest("send_signal_nmi"))
201 test_send_signal_nmi(false);
202 if (test__start_subtest("send_signal_tracepoint_thread"))
203 test_send_signal_tracepoint(true);
204 if (test__start_subtest("send_signal_perf_thread"))
205 test_send_signal_perf(true);
206 if (test__start_subtest("send_signal_nmi_thread"))
207 test_send_signal_nmi(true);
208}