116f0efc3SYonghong Song // SPDX-License-Identifier: GPL-2.0
216f0efc3SYonghong Song #include <test_progs.h>
3*b16ac5bfSYonghong Song #include <sys/time.h>
4*b16ac5bfSYonghong Song #include <sys/resource.h>
5ab8b7f0cSYonghong Song #include "test_send_signal_kern.skel.h"
616f0efc3SYonghong Song 
7256eab48SAndrii Nakryiko int sigusr1_received = 0;
816f0efc3SYonghong Song 
916f0efc3SYonghong Song static void sigusr1_handler(int signum)
1016f0efc3SYonghong Song {
1116f0efc3SYonghong Song 	sigusr1_received++;
1216f0efc3SYonghong Song }
1316f0efc3SYonghong Song 
1486ccc384SStanislav Fomichev static void test_send_signal_common(struct perf_event_attr *attr,
156f6cc426SYonghong Song 				    bool signal_thread)
1616f0efc3SYonghong Song {
17ab8b7f0cSYonghong Song 	struct test_send_signal_kern *skel;
1816f0efc3SYonghong Song 	int pipe_c2p[2], pipe_p2c[2];
19ab8b7f0cSYonghong Song 	int err = -1, pmu_fd = -1;
2016f0efc3SYonghong Song 	char buf[256];
2116f0efc3SYonghong Song 	pid_t pid;
2216f0efc3SYonghong Song 
236f6cc426SYonghong Song 	if (!ASSERT_OK(pipe(pipe_c2p), "pipe_c2p"))
2486ccc384SStanislav Fomichev 		return;
2516f0efc3SYonghong Song 
266f6cc426SYonghong Song 	if (!ASSERT_OK(pipe(pipe_p2c), "pipe_p2c")) {
2716f0efc3SYonghong Song 		close(pipe_c2p[0]);
2816f0efc3SYonghong Song 		close(pipe_c2p[1]);
2986ccc384SStanislav Fomichev 		return;
3016f0efc3SYonghong Song 	}
3116f0efc3SYonghong Song 
3216f0efc3SYonghong Song 	pid = fork();
336f6cc426SYonghong Song 	if (!ASSERT_GE(pid, 0, "fork")) {
3416f0efc3SYonghong Song 		close(pipe_c2p[0]);
3516f0efc3SYonghong Song 		close(pipe_c2p[1]);
3616f0efc3SYonghong Song 		close(pipe_p2c[0]);
3716f0efc3SYonghong Song 		close(pipe_p2c[1]);
3886ccc384SStanislav Fomichev 		return;
3916f0efc3SYonghong Song 	}
4016f0efc3SYonghong Song 
4116f0efc3SYonghong Song 	if (pid == 0) {
42*b16ac5bfSYonghong Song 		int old_prio;
43*b16ac5bfSYonghong Song 
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 
50*b16ac5bfSYonghong Song 		/* boost with a high priority so we got a higher chance
51*b16ac5bfSYonghong Song 		 * that if an interrupt happens, the underlying task
52*b16ac5bfSYonghong Song 		 * is this process.
53*b16ac5bfSYonghong Song 		 */
54*b16ac5bfSYonghong Song 		errno = 0;
55*b16ac5bfSYonghong Song 		old_prio = getpriority(PRIO_PROCESS, 0);
56*b16ac5bfSYonghong Song 		ASSERT_OK(errno, "getpriority");
57*b16ac5bfSYonghong Song 		ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
58*b16ac5bfSYonghong Song 
5916f0efc3SYonghong Song 		/* notify parent signal handler is installed */
606f6cc426SYonghong Song 		ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
6116f0efc3SYonghong Song 
6216f0efc3SYonghong Song 		/* make sure parent enabled bpf program to send_signal */
636f6cc426SYonghong Song 		ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
6416f0efc3SYonghong Song 
6516f0efc3SYonghong Song 		/* wait a little for signal handler */
6616f0efc3SYonghong Song 		sleep(1);
6716f0efc3SYonghong Song 
68929e54a9SJianlin Lv 		buf[0] = sigusr1_received ? '2' : '0';
696f6cc426SYonghong Song 		ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
7016f0efc3SYonghong Song 
7116f0efc3SYonghong Song 		/* wait for parent notification and exit */
726f6cc426SYonghong Song 		ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
7316f0efc3SYonghong Song 
74*b16ac5bfSYonghong Song 		/* restore the old priority */
75*b16ac5bfSYonghong Song 		ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");
76*b16ac5bfSYonghong Song 
7716f0efc3SYonghong Song 		close(pipe_c2p[1]);
7816f0efc3SYonghong Song 		close(pipe_p2c[0]);
7916f0efc3SYonghong Song 		exit(0);
8016f0efc3SYonghong Song 	}
8116f0efc3SYonghong Song 
8216f0efc3SYonghong Song 	close(pipe_c2p[1]); /* close write */
8316f0efc3SYonghong Song 	close(pipe_p2c[0]); /* close read */
8416f0efc3SYonghong Song 
85ab8b7f0cSYonghong Song 	skel = test_send_signal_kern__open_and_load();
866f6cc426SYonghong Song 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
87ab8b7f0cSYonghong Song 		goto skel_open_load_failure;
8816f0efc3SYonghong Song 
89ab8b7f0cSYonghong Song 	if (!attr) {
90ab8b7f0cSYonghong Song 		err = test_send_signal_kern__attach(skel);
916f6cc426SYonghong Song 		if (!ASSERT_OK(err, "skel_attach")) {
92ab8b7f0cSYonghong Song 			err = -1;
93ab8b7f0cSYonghong Song 			goto destroy_skel;
94ab8b7f0cSYonghong Song 		}
95ab8b7f0cSYonghong Song 	} else {
9616f0efc3SYonghong Song 		pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1,
9716f0efc3SYonghong Song 				 -1 /* group id */, 0 /* flags */);
986f6cc426SYonghong Song 		if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
9916f0efc3SYonghong Song 			err = -1;
100ab8b7f0cSYonghong Song 			goto destroy_skel;
10116f0efc3SYonghong Song 		}
10216f0efc3SYonghong Song 
103ab8b7f0cSYonghong Song 		skel->links.send_signal_perf =
104ab8b7f0cSYonghong Song 			bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd);
105bad2e478SAndrii Nakryiko 		if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event"))
10616f0efc3SYonghong Song 			goto disable_pmu;
107ab8b7f0cSYonghong Song 	}
10816f0efc3SYonghong Song 
10916f0efc3SYonghong Song 	/* wait until child signal handler installed */
1106f6cc426SYonghong Song 	ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
11116f0efc3SYonghong Song 
11216f0efc3SYonghong Song 	/* trigger the bpf send_signal */
113ab8b7f0cSYonghong Song 	skel->bss->pid = pid;
114ab8b7f0cSYonghong Song 	skel->bss->sig = SIGUSR1;
115ab8b7f0cSYonghong Song 	skel->bss->signal_thread = signal_thread;
11616f0efc3SYonghong Song 
11716f0efc3SYonghong Song 	/* notify child that bpf program can send_signal now */
1186f6cc426SYonghong Song 	ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
11916f0efc3SYonghong Song 
12016f0efc3SYonghong Song 	/* wait for result */
12116f0efc3SYonghong Song 	err = read(pipe_c2p[0], buf, 1);
1226f6cc426SYonghong Song 	if (!ASSERT_GE(err, 0, "reading pipe"))
12316f0efc3SYonghong Song 		goto disable_pmu;
1246f6cc426SYonghong Song 	if (!ASSERT_GT(err, 0, "reading pipe error: size 0")) {
12516f0efc3SYonghong Song 		err = -1;
12616f0efc3SYonghong Song 		goto disable_pmu;
12716f0efc3SYonghong Song 	}
12816f0efc3SYonghong Song 
1296f6cc426SYonghong Song 	ASSERT_EQ(buf[0], '2', "incorrect result");
13016f0efc3SYonghong Song 
13116f0efc3SYonghong Song 	/* notify child safe to exit */
1326f6cc426SYonghong Song 	ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
13316f0efc3SYonghong Song 
13416f0efc3SYonghong Song disable_pmu:
13516f0efc3SYonghong Song 	close(pmu_fd);
136ab8b7f0cSYonghong Song destroy_skel:
137ab8b7f0cSYonghong Song 	test_send_signal_kern__destroy(skel);
138ab8b7f0cSYonghong Song skel_open_load_failure:
13916f0efc3SYonghong Song 	close(pipe_c2p[0]);
14016f0efc3SYonghong Song 	close(pipe_p2c[1]);
14116f0efc3SYonghong Song 	wait(NULL);
14216f0efc3SYonghong Song }
14316f0efc3SYonghong Song 
144ab8b7f0cSYonghong Song static void test_send_signal_tracepoint(bool signal_thread)
14516f0efc3SYonghong Song {
1466f6cc426SYonghong Song 	test_send_signal_common(NULL, signal_thread);
14716f0efc3SYonghong Song }
14816f0efc3SYonghong Song 
149ab8b7f0cSYonghong Song static void test_send_signal_perf(bool signal_thread)
1504e59afbbSIlya Leoshkevich {
1514e59afbbSIlya Leoshkevich 	struct perf_event_attr attr = {
1524e59afbbSIlya Leoshkevich 		.sample_period = 1,
1534e59afbbSIlya Leoshkevich 		.type = PERF_TYPE_SOFTWARE,
1544e59afbbSIlya Leoshkevich 		.config = PERF_COUNT_SW_CPU_CLOCK,
1554e59afbbSIlya Leoshkevich 	};
1564e59afbbSIlya Leoshkevich 
1576f6cc426SYonghong Song 	test_send_signal_common(&attr, signal_thread);
1584e59afbbSIlya Leoshkevich }
1594e59afbbSIlya Leoshkevich 
160ab8b7f0cSYonghong Song static void test_send_signal_nmi(bool signal_thread)
16116f0efc3SYonghong Song {
16216f0efc3SYonghong Song 	struct perf_event_attr attr = {
16335697c12SYonghong Song 		.sample_period = 1,
16416f0efc3SYonghong Song 		.type = PERF_TYPE_HARDWARE,
16516f0efc3SYonghong Song 		.config = PERF_COUNT_HW_CPU_CYCLES,
16616f0efc3SYonghong Song 	};
1674e59afbbSIlya Leoshkevich 	int pmu_fd;
16816f0efc3SYonghong Song 
1694e59afbbSIlya Leoshkevich 	/* Some setups (e.g. virtual machines) might run with hardware
1704e59afbbSIlya Leoshkevich 	 * perf events disabled. If this is the case, skip this test.
1714e59afbbSIlya Leoshkevich 	 */
1724e59afbbSIlya Leoshkevich 	pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
1734e59afbbSIlya Leoshkevich 			 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
1744e59afbbSIlya Leoshkevich 	if (pmu_fd == -1) {
1754e59afbbSIlya Leoshkevich 		if (errno == ENOENT) {
17666bd2ec1SStanislav Fomichev 			printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
1774e59afbbSIlya Leoshkevich 			       __func__);
178cd9c21d7SStanislav Fomichev 			test__skip();
17986ccc384SStanislav Fomichev 			return;
1804e59afbbSIlya Leoshkevich 		}
1814e59afbbSIlya Leoshkevich 		/* Let the test fail with a more informative message */
1824e59afbbSIlya Leoshkevich 	} else {
1834e59afbbSIlya Leoshkevich 		close(pmu_fd);
1844e59afbbSIlya Leoshkevich 	}
1854e59afbbSIlya Leoshkevich 
1866f6cc426SYonghong Song 	test_send_signal_common(&attr, signal_thread);
18716f0efc3SYonghong Song }
18816f0efc3SYonghong Song 
18916f0efc3SYonghong Song void test_send_signal(void)
19016f0efc3SYonghong Song {
191b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_tracepoint"))
192ab8b7f0cSYonghong Song 		test_send_signal_tracepoint(false);
193b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_perf"))
194ab8b7f0cSYonghong Song 		test_send_signal_perf(false);
195b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_nmi"))
196ab8b7f0cSYonghong Song 		test_send_signal_nmi(false);
197ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_tracepoint_thread"))
198ab8b7f0cSYonghong Song 		test_send_signal_tracepoint(true);
199ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_perf_thread"))
200ab8b7f0cSYonghong Song 		test_send_signal_perf(true);
201ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_nmi_thread"))
202ab8b7f0cSYonghong Song 		test_send_signal_nmi(true);
20316f0efc3SYonghong Song }
204