116f0efc3SYonghong Song // SPDX-License-Identifier: GPL-2.0
216f0efc3SYonghong Song #include <test_progs.h>
3ab8b7f0cSYonghong Song #include "test_send_signal_kern.skel.h"
416f0efc3SYonghong Song 
5256eab48SAndrii Nakryiko int sigusr1_received = 0;
616f0efc3SYonghong Song 
716f0efc3SYonghong Song static void sigusr1_handler(int signum)
816f0efc3SYonghong Song {
916f0efc3SYonghong Song 	sigusr1_received++;
1016f0efc3SYonghong Song }
1116f0efc3SYonghong Song 
1286ccc384SStanislav Fomichev static void test_send_signal_common(struct perf_event_attr *attr,
13ab8b7f0cSYonghong Song 				    bool signal_thread,
1416f0efc3SYonghong Song 				    const char *test_name)
1516f0efc3SYonghong Song {
16ab8b7f0cSYonghong Song 	struct test_send_signal_kern *skel;
1716f0efc3SYonghong Song 	int pipe_c2p[2], pipe_p2c[2];
18ab8b7f0cSYonghong Song 	int err = -1, pmu_fd = -1;
19ab8b7f0cSYonghong Song 	__u32 duration = 0;
2016f0efc3SYonghong Song 	char buf[256];
2116f0efc3SYonghong Song 	pid_t pid;
2216f0efc3SYonghong Song 
2316f0efc3SYonghong Song 	if (CHECK(pipe(pipe_c2p), test_name,
2416f0efc3SYonghong Song 		  "pipe pipe_c2p error: %s\n", strerror(errno)))
2586ccc384SStanislav Fomichev 		return;
2616f0efc3SYonghong Song 
2716f0efc3SYonghong Song 	if (CHECK(pipe(pipe_p2c), test_name,
2816f0efc3SYonghong Song 		  "pipe pipe_p2c error: %s\n", strerror(errno))) {
2916f0efc3SYonghong Song 		close(pipe_c2p[0]);
3016f0efc3SYonghong Song 		close(pipe_c2p[1]);
3186ccc384SStanislav Fomichev 		return;
3216f0efc3SYonghong Song 	}
3316f0efc3SYonghong Song 
3416f0efc3SYonghong Song 	pid = fork();
3516f0efc3SYonghong Song 	if (CHECK(pid < 0, test_name, "fork error: %s\n", strerror(errno))) {
3616f0efc3SYonghong Song 		close(pipe_c2p[0]);
3716f0efc3SYonghong Song 		close(pipe_c2p[1]);
3816f0efc3SYonghong Song 		close(pipe_p2c[0]);
3916f0efc3SYonghong Song 		close(pipe_p2c[1]);
4086ccc384SStanislav Fomichev 		return;
4116f0efc3SYonghong Song 	}
4216f0efc3SYonghong Song 
4316f0efc3SYonghong Song 	if (pid == 0) {
4416f0efc3SYonghong Song 		/* install signal handler and notify parent */
4516f0efc3SYonghong Song 		signal(SIGUSR1, sigusr1_handler);
4616f0efc3SYonghong Song 
4716f0efc3SYonghong Song 		close(pipe_c2p[0]); /* close read */
4816f0efc3SYonghong Song 		close(pipe_p2c[1]); /* close write */
4916f0efc3SYonghong Song 
5016f0efc3SYonghong Song 		/* notify parent signal handler is installed */
51929e54a9SJianlin Lv 		CHECK(write(pipe_c2p[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
5216f0efc3SYonghong Song 
5316f0efc3SYonghong Song 		/* make sure parent enabled bpf program to send_signal */
54929e54a9SJianlin Lv 		CHECK(read(pipe_p2c[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
5516f0efc3SYonghong Song 
5616f0efc3SYonghong Song 		/* wait a little for signal handler */
5716f0efc3SYonghong Song 		sleep(1);
5816f0efc3SYonghong Song 
59929e54a9SJianlin Lv 		buf[0] = sigusr1_received ? '2' : '0';
60929e54a9SJianlin Lv 		CHECK(write(pipe_c2p[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
6116f0efc3SYonghong Song 
6216f0efc3SYonghong Song 		/* wait for parent notification and exit */
63929e54a9SJianlin Lv 		CHECK(read(pipe_p2c[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
6416f0efc3SYonghong Song 
6516f0efc3SYonghong Song 		close(pipe_c2p[1]);
6616f0efc3SYonghong Song 		close(pipe_p2c[0]);
6716f0efc3SYonghong Song 		exit(0);
6816f0efc3SYonghong Song 	}
6916f0efc3SYonghong Song 
7016f0efc3SYonghong Song 	close(pipe_c2p[1]); /* close write */
7116f0efc3SYonghong Song 	close(pipe_p2c[0]); /* close read */
7216f0efc3SYonghong Song 
73ab8b7f0cSYonghong Song 	skel = test_send_signal_kern__open_and_load();
74ab8b7f0cSYonghong Song 	if (CHECK(!skel, "skel_open_and_load", "skeleton open_and_load failed\n"))
75ab8b7f0cSYonghong Song 		goto skel_open_load_failure;
7616f0efc3SYonghong Song 
77ab8b7f0cSYonghong Song 	if (!attr) {
78ab8b7f0cSYonghong Song 		err = test_send_signal_kern__attach(skel);
79ab8b7f0cSYonghong Song 		if (CHECK(err, "skel_attach", "skeleton attach failed\n")) {
80ab8b7f0cSYonghong Song 			err = -1;
81ab8b7f0cSYonghong Song 			goto destroy_skel;
82ab8b7f0cSYonghong Song 		}
83ab8b7f0cSYonghong Song 	} else {
8416f0efc3SYonghong Song 		pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1,
8516f0efc3SYonghong Song 				 -1 /* group id */, 0 /* flags */);
8616f0efc3SYonghong Song 		if (CHECK(pmu_fd < 0, test_name, "perf_event_open error: %s\n",
8716f0efc3SYonghong Song 			strerror(errno))) {
8816f0efc3SYonghong Song 			err = -1;
89ab8b7f0cSYonghong Song 			goto destroy_skel;
9016f0efc3SYonghong Song 		}
9116f0efc3SYonghong Song 
92ab8b7f0cSYonghong Song 		skel->links.send_signal_perf =
93ab8b7f0cSYonghong Song 			bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd);
94*bad2e478SAndrii Nakryiko 		if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event"))
9516f0efc3SYonghong Song 			goto disable_pmu;
96ab8b7f0cSYonghong Song 	}
9716f0efc3SYonghong Song 
9816f0efc3SYonghong Song 	/* wait until child signal handler installed */
99929e54a9SJianlin Lv 	CHECK(read(pipe_c2p[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
10016f0efc3SYonghong Song 
10116f0efc3SYonghong Song 	/* trigger the bpf send_signal */
102ab8b7f0cSYonghong Song 	skel->bss->pid = pid;
103ab8b7f0cSYonghong Song 	skel->bss->sig = SIGUSR1;
104ab8b7f0cSYonghong Song 	skel->bss->signal_thread = signal_thread;
10516f0efc3SYonghong Song 
10616f0efc3SYonghong Song 	/* notify child that bpf program can send_signal now */
107929e54a9SJianlin Lv 	CHECK(write(pipe_p2c[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
10816f0efc3SYonghong Song 
10916f0efc3SYonghong Song 	/* wait for result */
11016f0efc3SYonghong Song 	err = read(pipe_c2p[0], buf, 1);
11116f0efc3SYonghong Song 	if (CHECK(err < 0, test_name, "reading pipe error: %s\n", strerror(errno)))
11216f0efc3SYonghong Song 		goto disable_pmu;
11316f0efc3SYonghong Song 	if (CHECK(err == 0, test_name, "reading pipe error: size 0\n")) {
11416f0efc3SYonghong Song 		err = -1;
11516f0efc3SYonghong Song 		goto disable_pmu;
11616f0efc3SYonghong Song 	}
11716f0efc3SYonghong Song 
11886ccc384SStanislav Fomichev 	CHECK(buf[0] != '2', test_name, "incorrect result\n");
11916f0efc3SYonghong Song 
12016f0efc3SYonghong Song 	/* notify child safe to exit */
121929e54a9SJianlin Lv 	CHECK(write(pipe_p2c[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
12216f0efc3SYonghong Song 
12316f0efc3SYonghong Song disable_pmu:
12416f0efc3SYonghong Song 	close(pmu_fd);
125ab8b7f0cSYonghong Song destroy_skel:
126ab8b7f0cSYonghong Song 	test_send_signal_kern__destroy(skel);
127ab8b7f0cSYonghong Song skel_open_load_failure:
12816f0efc3SYonghong Song 	close(pipe_c2p[0]);
12916f0efc3SYonghong Song 	close(pipe_p2c[1]);
13016f0efc3SYonghong Song 	wait(NULL);
13116f0efc3SYonghong Song }
13216f0efc3SYonghong Song 
133ab8b7f0cSYonghong Song static void test_send_signal_tracepoint(bool signal_thread)
13416f0efc3SYonghong Song {
135ab8b7f0cSYonghong Song 	test_send_signal_common(NULL, signal_thread, "tracepoint");
13616f0efc3SYonghong Song }
13716f0efc3SYonghong Song 
138ab8b7f0cSYonghong Song static void test_send_signal_perf(bool signal_thread)
1394e59afbbSIlya Leoshkevich {
1404e59afbbSIlya Leoshkevich 	struct perf_event_attr attr = {
1414e59afbbSIlya Leoshkevich 		.sample_period = 1,
1424e59afbbSIlya Leoshkevich 		.type = PERF_TYPE_SOFTWARE,
1434e59afbbSIlya Leoshkevich 		.config = PERF_COUNT_SW_CPU_CLOCK,
1444e59afbbSIlya Leoshkevich 	};
1454e59afbbSIlya Leoshkevich 
146ab8b7f0cSYonghong Song 	test_send_signal_common(&attr, signal_thread, "perf_sw_event");
1474e59afbbSIlya Leoshkevich }
1484e59afbbSIlya Leoshkevich 
149ab8b7f0cSYonghong Song static void test_send_signal_nmi(bool signal_thread)
15016f0efc3SYonghong Song {
15116f0efc3SYonghong Song 	struct perf_event_attr attr = {
15235697c12SYonghong Song 		.sample_period = 1,
15316f0efc3SYonghong Song 		.type = PERF_TYPE_HARDWARE,
15416f0efc3SYonghong Song 		.config = PERF_COUNT_HW_CPU_CYCLES,
15516f0efc3SYonghong Song 	};
1564e59afbbSIlya Leoshkevich 	int pmu_fd;
15716f0efc3SYonghong Song 
1584e59afbbSIlya Leoshkevich 	/* Some setups (e.g. virtual machines) might run with hardware
1594e59afbbSIlya Leoshkevich 	 * perf events disabled. If this is the case, skip this test.
1604e59afbbSIlya Leoshkevich 	 */
1614e59afbbSIlya Leoshkevich 	pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
1624e59afbbSIlya Leoshkevich 			 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
1634e59afbbSIlya Leoshkevich 	if (pmu_fd == -1) {
1644e59afbbSIlya Leoshkevich 		if (errno == ENOENT) {
16566bd2ec1SStanislav Fomichev 			printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
1664e59afbbSIlya Leoshkevich 			       __func__);
167cd9c21d7SStanislav Fomichev 			test__skip();
16886ccc384SStanislav Fomichev 			return;
1694e59afbbSIlya Leoshkevich 		}
1704e59afbbSIlya Leoshkevich 		/* Let the test fail with a more informative message */
1714e59afbbSIlya Leoshkevich 	} else {
1724e59afbbSIlya Leoshkevich 		close(pmu_fd);
1734e59afbbSIlya Leoshkevich 	}
1744e59afbbSIlya Leoshkevich 
175ab8b7f0cSYonghong Song 	test_send_signal_common(&attr, signal_thread, "perf_hw_event");
17616f0efc3SYonghong Song }
17716f0efc3SYonghong Song 
17816f0efc3SYonghong Song void test_send_signal(void)
17916f0efc3SYonghong Song {
180b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_tracepoint"))
181ab8b7f0cSYonghong Song 		test_send_signal_tracepoint(false);
182b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_perf"))
183ab8b7f0cSYonghong Song 		test_send_signal_perf(false);
184b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_nmi"))
185ab8b7f0cSYonghong Song 		test_send_signal_nmi(false);
186ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_tracepoint_thread"))
187ab8b7f0cSYonghong Song 		test_send_signal_tracepoint(true);
188ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_perf_thread"))
189ab8b7f0cSYonghong Song 		test_send_signal_perf(true);
190ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_nmi_thread"))
191ab8b7f0cSYonghong Song 		test_send_signal_nmi(true);
19216f0efc3SYonghong Song }
193