116f0efc3SYonghong Song // SPDX-License-Identifier: GPL-2.0
216f0efc3SYonghong Song #include <test_progs.h>
3b16ac5bfSYonghong Song #include <sys/time.h>
4b16ac5bfSYonghong Song #include <sys/resource.h>
5ab8b7f0cSYonghong Song #include "test_send_signal_kern.skel.h"
616f0efc3SYonghong Song 
71fd49864SMykola Lysenko static int sigusr1_received;
816f0efc3SYonghong Song 
sigusr1_handler(int signum)916f0efc3SYonghong Song static void sigusr1_handler(int signum)
1016f0efc3SYonghong Song {
111fd49864SMykola Lysenko 	sigusr1_received = 1;
1216f0efc3SYonghong Song }
1316f0efc3SYonghong Song 
test_send_signal_common(struct perf_event_attr * attr,bool signal_thread)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) {
42b16ac5bfSYonghong Song 		int old_prio;
431fd49864SMykola Lysenko 		volatile int j = 0;
44b16ac5bfSYonghong Song 
4516f0efc3SYonghong Song 		/* install signal handler and notify parent */
461fd49864SMykola Lysenko 		ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal");
4716f0efc3SYonghong Song 
4816f0efc3SYonghong Song 		close(pipe_c2p[0]); /* close read */
4916f0efc3SYonghong Song 		close(pipe_p2c[1]); /* close write */
5016f0efc3SYonghong Song 
51b16ac5bfSYonghong Song 		/* boost with a high priority so we got a higher chance
52b16ac5bfSYonghong Song 		 * that if an interrupt happens, the underlying task
53b16ac5bfSYonghong Song 		 * is this process.
54b16ac5bfSYonghong Song 		 */
55b16ac5bfSYonghong Song 		errno = 0;
56b16ac5bfSYonghong Song 		old_prio = getpriority(PRIO_PROCESS, 0);
57b16ac5bfSYonghong Song 		ASSERT_OK(errno, "getpriority");
58b16ac5bfSYonghong Song 		ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
59b16ac5bfSYonghong Song 
6016f0efc3SYonghong Song 		/* notify parent signal handler is installed */
616f6cc426SYonghong Song 		ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
6216f0efc3SYonghong Song 
6316f0efc3SYonghong Song 		/* make sure parent enabled bpf program to send_signal */
646f6cc426SYonghong Song 		ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
6516f0efc3SYonghong Song 
6616f0efc3SYonghong Song 		/* wait a little for signal handler */
67*4a54de65SDavid Vernet 		for (int i = 0; i < 1000000000 && !sigusr1_received; i++) {
68d3b351f6SYonghong Song 			j /= i + j + 1;
69*4a54de65SDavid Vernet 			if (!attr)
70*4a54de65SDavid Vernet 				/* trigger the nanosleep tracepoint program. */
71*4a54de65SDavid Vernet 				usleep(1);
72*4a54de65SDavid Vernet 		}
7316f0efc3SYonghong Song 
74929e54a9SJianlin Lv 		buf[0] = sigusr1_received ? '2' : '0';
751fd49864SMykola Lysenko 		ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
766f6cc426SYonghong Song 		ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
7716f0efc3SYonghong Song 
7816f0efc3SYonghong Song 		/* wait for parent notification and exit */
796f6cc426SYonghong Song 		ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
8016f0efc3SYonghong Song 
81b16ac5bfSYonghong Song 		/* restore the old priority */
82b16ac5bfSYonghong Song 		ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");
83b16ac5bfSYonghong Song 
8416f0efc3SYonghong Song 		close(pipe_c2p[1]);
8516f0efc3SYonghong Song 		close(pipe_p2c[0]);
8616f0efc3SYonghong Song 		exit(0);
8716f0efc3SYonghong Song 	}
8816f0efc3SYonghong Song 
8916f0efc3SYonghong Song 	close(pipe_c2p[1]); /* close write */
9016f0efc3SYonghong Song 	close(pipe_p2c[0]); /* close read */
9116f0efc3SYonghong Song 
92ab8b7f0cSYonghong Song 	skel = test_send_signal_kern__open_and_load();
936f6cc426SYonghong Song 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
94ab8b7f0cSYonghong Song 		goto skel_open_load_failure;
9516f0efc3SYonghong Song 
96ab8b7f0cSYonghong Song 	if (!attr) {
97ab8b7f0cSYonghong Song 		err = test_send_signal_kern__attach(skel);
986f6cc426SYonghong Song 		if (!ASSERT_OK(err, "skel_attach")) {
99ab8b7f0cSYonghong Song 			err = -1;
100ab8b7f0cSYonghong Song 			goto destroy_skel;
101ab8b7f0cSYonghong Song 		}
102ab8b7f0cSYonghong Song 	} else {
1031fd49864SMykola Lysenko 		pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */,
10416f0efc3SYonghong Song 				 -1 /* group id */, 0 /* flags */);
1056f6cc426SYonghong Song 		if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
10616f0efc3SYonghong Song 			err = -1;
107ab8b7f0cSYonghong Song 			goto destroy_skel;
10816f0efc3SYonghong Song 		}
10916f0efc3SYonghong Song 
110ab8b7f0cSYonghong Song 		skel->links.send_signal_perf =
111ab8b7f0cSYonghong Song 			bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd);
112bad2e478SAndrii Nakryiko 		if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event"))
11316f0efc3SYonghong Song 			goto disable_pmu;
114ab8b7f0cSYonghong Song 	}
11516f0efc3SYonghong Song 
11616f0efc3SYonghong Song 	/* wait until child signal handler installed */
1176f6cc426SYonghong Song 	ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
11816f0efc3SYonghong Song 
11916f0efc3SYonghong Song 	/* trigger the bpf send_signal */
120ab8b7f0cSYonghong Song 	skel->bss->signal_thread = signal_thread;
1211fd49864SMykola Lysenko 	skel->bss->sig = SIGUSR1;
1221fd49864SMykola Lysenko 	skel->bss->pid = pid;
12316f0efc3SYonghong Song 
12416f0efc3SYonghong Song 	/* notify child that bpf program can send_signal now */
1256f6cc426SYonghong Song 	ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
12616f0efc3SYonghong Song 
12716f0efc3SYonghong Song 	/* wait for result */
12816f0efc3SYonghong Song 	err = read(pipe_c2p[0], buf, 1);
1296f6cc426SYonghong Song 	if (!ASSERT_GE(err, 0, "reading pipe"))
13016f0efc3SYonghong Song 		goto disable_pmu;
1316f6cc426SYonghong Song 	if (!ASSERT_GT(err, 0, "reading pipe error: size 0")) {
13216f0efc3SYonghong Song 		err = -1;
13316f0efc3SYonghong Song 		goto disable_pmu;
13416f0efc3SYonghong Song 	}
13516f0efc3SYonghong Song 
1366f6cc426SYonghong Song 	ASSERT_EQ(buf[0], '2', "incorrect result");
13716f0efc3SYonghong Song 
13816f0efc3SYonghong Song 	/* notify child safe to exit */
1396f6cc426SYonghong Song 	ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
14016f0efc3SYonghong Song 
14116f0efc3SYonghong Song disable_pmu:
14216f0efc3SYonghong Song 	close(pmu_fd);
143ab8b7f0cSYonghong Song destroy_skel:
144ab8b7f0cSYonghong Song 	test_send_signal_kern__destroy(skel);
145ab8b7f0cSYonghong Song skel_open_load_failure:
14616f0efc3SYonghong Song 	close(pipe_c2p[0]);
14716f0efc3SYonghong Song 	close(pipe_p2c[1]);
14816f0efc3SYonghong Song 	wait(NULL);
14916f0efc3SYonghong Song }
15016f0efc3SYonghong Song 
test_send_signal_tracepoint(bool signal_thread)151ab8b7f0cSYonghong Song static void test_send_signal_tracepoint(bool signal_thread)
15216f0efc3SYonghong Song {
1536f6cc426SYonghong Song 	test_send_signal_common(NULL, signal_thread);
15416f0efc3SYonghong Song }
15516f0efc3SYonghong Song 
test_send_signal_perf(bool signal_thread)156ab8b7f0cSYonghong Song static void test_send_signal_perf(bool signal_thread)
1574e59afbbSIlya Leoshkevich {
1584e59afbbSIlya Leoshkevich 	struct perf_event_attr attr = {
1594e59afbbSIlya Leoshkevich 		.sample_period = 1,
1604e59afbbSIlya Leoshkevich 		.type = PERF_TYPE_SOFTWARE,
1614e59afbbSIlya Leoshkevich 		.config = PERF_COUNT_SW_CPU_CLOCK,
1624e59afbbSIlya Leoshkevich 	};
1634e59afbbSIlya Leoshkevich 
1646f6cc426SYonghong Song 	test_send_signal_common(&attr, signal_thread);
1654e59afbbSIlya Leoshkevich }
1664e59afbbSIlya Leoshkevich 
test_send_signal_nmi(bool signal_thread)167ab8b7f0cSYonghong Song static void test_send_signal_nmi(bool signal_thread)
16816f0efc3SYonghong Song {
16916f0efc3SYonghong Song 	struct perf_event_attr attr = {
17035697c12SYonghong Song 		.sample_period = 1,
17116f0efc3SYonghong Song 		.type = PERF_TYPE_HARDWARE,
17216f0efc3SYonghong Song 		.config = PERF_COUNT_HW_CPU_CYCLES,
17316f0efc3SYonghong Song 	};
1744e59afbbSIlya Leoshkevich 	int pmu_fd;
17516f0efc3SYonghong Song 
1764e59afbbSIlya Leoshkevich 	/* Some setups (e.g. virtual machines) might run with hardware
1774e59afbbSIlya Leoshkevich 	 * perf events disabled. If this is the case, skip this test.
1784e59afbbSIlya Leoshkevich 	 */
1794e59afbbSIlya Leoshkevich 	pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
1804e59afbbSIlya Leoshkevich 			 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
1814e59afbbSIlya Leoshkevich 	if (pmu_fd == -1) {
1824e59afbbSIlya Leoshkevich 		if (errno == ENOENT) {
18366bd2ec1SStanislav Fomichev 			printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
1844e59afbbSIlya Leoshkevich 			       __func__);
185cd9c21d7SStanislav Fomichev 			test__skip();
18686ccc384SStanislav Fomichev 			return;
1874e59afbbSIlya Leoshkevich 		}
1884e59afbbSIlya Leoshkevich 		/* Let the test fail with a more informative message */
1894e59afbbSIlya Leoshkevich 	} else {
1904e59afbbSIlya Leoshkevich 		close(pmu_fd);
1914e59afbbSIlya Leoshkevich 	}
1924e59afbbSIlya Leoshkevich 
1936f6cc426SYonghong Song 	test_send_signal_common(&attr, signal_thread);
19416f0efc3SYonghong Song }
19516f0efc3SYonghong Song 
test_send_signal(void)19616f0efc3SYonghong Song void test_send_signal(void)
19716f0efc3SYonghong Song {
198b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_tracepoint"))
199ab8b7f0cSYonghong Song 		test_send_signal_tracepoint(false);
200b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_perf"))
201ab8b7f0cSYonghong Song 		test_send_signal_perf(false);
202b207edfeSAndrii Nakryiko 	if (test__start_subtest("send_signal_nmi"))
203ab8b7f0cSYonghong Song 		test_send_signal_nmi(false);
204ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_tracepoint_thread"))
205ab8b7f0cSYonghong Song 		test_send_signal_tracepoint(true);
206ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_perf_thread"))
207ab8b7f0cSYonghong Song 		test_send_signal_perf(true);
208ab8b7f0cSYonghong Song 	if (test__start_subtest("send_signal_nmi_thread"))
209ab8b7f0cSYonghong Song 		test_send_signal_nmi(true);
21016f0efc3SYonghong Song }
211