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 7*1fd49864SMykola Lysenko static int sigusr1_received; 816f0efc3SYonghong Song 916f0efc3SYonghong Song static void sigusr1_handler(int signum) 1016f0efc3SYonghong Song { 11*1fd49864SMykola Lysenko sigusr1_received = 1; 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) { 42b16ac5bfSYonghong Song int old_prio; 43*1fd49864SMykola Lysenko volatile int j = 0; 44b16ac5bfSYonghong Song 4516f0efc3SYonghong Song /* install signal handler and notify parent */ 46*1fd49864SMykola 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*1fd49864SMykola Lysenko for (int i = 0; i < 100000000 && !sigusr1_received; i++) 68*1fd49864SMykola Lysenko j /= i + 1; 6916f0efc3SYonghong Song 70929e54a9SJianlin Lv buf[0] = sigusr1_received ? '2' : '0'; 71*1fd49864SMykola Lysenko ASSERT_EQ(sigusr1_received, 1, "sigusr1_received"); 726f6cc426SYonghong Song ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write"); 7316f0efc3SYonghong Song 7416f0efc3SYonghong Song /* wait for parent notification and exit */ 756f6cc426SYonghong Song ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read"); 7616f0efc3SYonghong Song 77b16ac5bfSYonghong Song /* restore the old priority */ 78b16ac5bfSYonghong Song ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority"); 79b16ac5bfSYonghong Song 8016f0efc3SYonghong Song close(pipe_c2p[1]); 8116f0efc3SYonghong Song close(pipe_p2c[0]); 8216f0efc3SYonghong Song exit(0); 8316f0efc3SYonghong Song } 8416f0efc3SYonghong Song 8516f0efc3SYonghong Song close(pipe_c2p[1]); /* close write */ 8616f0efc3SYonghong Song close(pipe_p2c[0]); /* close read */ 8716f0efc3SYonghong Song 88ab8b7f0cSYonghong Song skel = test_send_signal_kern__open_and_load(); 896f6cc426SYonghong Song if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) 90ab8b7f0cSYonghong Song goto skel_open_load_failure; 9116f0efc3SYonghong Song 92ab8b7f0cSYonghong Song if (!attr) { 93ab8b7f0cSYonghong Song err = test_send_signal_kern__attach(skel); 946f6cc426SYonghong Song if (!ASSERT_OK(err, "skel_attach")) { 95ab8b7f0cSYonghong Song err = -1; 96ab8b7f0cSYonghong Song goto destroy_skel; 97ab8b7f0cSYonghong Song } 98ab8b7f0cSYonghong Song } else { 99*1fd49864SMykola Lysenko pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */, 10016f0efc3SYonghong Song -1 /* group id */, 0 /* flags */); 1016f6cc426SYonghong Song if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) { 10216f0efc3SYonghong Song err = -1; 103ab8b7f0cSYonghong Song goto destroy_skel; 10416f0efc3SYonghong Song } 10516f0efc3SYonghong Song 106ab8b7f0cSYonghong Song skel->links.send_signal_perf = 107ab8b7f0cSYonghong Song bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd); 108bad2e478SAndrii Nakryiko if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event")) 10916f0efc3SYonghong Song goto disable_pmu; 110ab8b7f0cSYonghong Song } 11116f0efc3SYonghong Song 11216f0efc3SYonghong Song /* wait until child signal handler installed */ 1136f6cc426SYonghong Song ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read"); 11416f0efc3SYonghong Song 11516f0efc3SYonghong Song /* trigger the bpf send_signal */ 116ab8b7f0cSYonghong Song skel->bss->signal_thread = signal_thread; 117*1fd49864SMykola Lysenko skel->bss->sig = SIGUSR1; 118*1fd49864SMykola Lysenko skel->bss->pid = pid; 11916f0efc3SYonghong Song 12016f0efc3SYonghong Song /* notify child that bpf program can send_signal now */ 1216f6cc426SYonghong Song ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write"); 12216f0efc3SYonghong Song 12316f0efc3SYonghong Song /* wait for result */ 12416f0efc3SYonghong Song err = read(pipe_c2p[0], buf, 1); 1256f6cc426SYonghong Song if (!ASSERT_GE(err, 0, "reading pipe")) 12616f0efc3SYonghong Song goto disable_pmu; 1276f6cc426SYonghong Song if (!ASSERT_GT(err, 0, "reading pipe error: size 0")) { 12816f0efc3SYonghong Song err = -1; 12916f0efc3SYonghong Song goto disable_pmu; 13016f0efc3SYonghong Song } 13116f0efc3SYonghong Song 1326f6cc426SYonghong Song ASSERT_EQ(buf[0], '2', "incorrect result"); 13316f0efc3SYonghong Song 13416f0efc3SYonghong Song /* notify child safe to exit */ 1356f6cc426SYonghong Song ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write"); 13616f0efc3SYonghong Song 13716f0efc3SYonghong Song disable_pmu: 13816f0efc3SYonghong Song close(pmu_fd); 139ab8b7f0cSYonghong Song destroy_skel: 140ab8b7f0cSYonghong Song test_send_signal_kern__destroy(skel); 141ab8b7f0cSYonghong Song skel_open_load_failure: 14216f0efc3SYonghong Song close(pipe_c2p[0]); 14316f0efc3SYonghong Song close(pipe_p2c[1]); 14416f0efc3SYonghong Song wait(NULL); 14516f0efc3SYonghong Song } 14616f0efc3SYonghong Song 147ab8b7f0cSYonghong Song static void test_send_signal_tracepoint(bool signal_thread) 14816f0efc3SYonghong Song { 1496f6cc426SYonghong Song test_send_signal_common(NULL, signal_thread); 15016f0efc3SYonghong Song } 15116f0efc3SYonghong Song 152ab8b7f0cSYonghong Song static void test_send_signal_perf(bool signal_thread) 1534e59afbbSIlya Leoshkevich { 1544e59afbbSIlya Leoshkevich struct perf_event_attr attr = { 1554e59afbbSIlya Leoshkevich .sample_period = 1, 1564e59afbbSIlya Leoshkevich .type = PERF_TYPE_SOFTWARE, 1574e59afbbSIlya Leoshkevich .config = PERF_COUNT_SW_CPU_CLOCK, 1584e59afbbSIlya Leoshkevich }; 1594e59afbbSIlya Leoshkevich 1606f6cc426SYonghong Song test_send_signal_common(&attr, signal_thread); 1614e59afbbSIlya Leoshkevich } 1624e59afbbSIlya Leoshkevich 163ab8b7f0cSYonghong Song static void test_send_signal_nmi(bool signal_thread) 16416f0efc3SYonghong Song { 16516f0efc3SYonghong Song struct perf_event_attr attr = { 16635697c12SYonghong Song .sample_period = 1, 16716f0efc3SYonghong Song .type = PERF_TYPE_HARDWARE, 16816f0efc3SYonghong Song .config = PERF_COUNT_HW_CPU_CYCLES, 16916f0efc3SYonghong Song }; 1704e59afbbSIlya Leoshkevich int pmu_fd; 17116f0efc3SYonghong Song 1724e59afbbSIlya Leoshkevich /* Some setups (e.g. virtual machines) might run with hardware 1734e59afbbSIlya Leoshkevich * perf events disabled. If this is the case, skip this test. 1744e59afbbSIlya Leoshkevich */ 1754e59afbbSIlya Leoshkevich pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */, 1764e59afbbSIlya Leoshkevich -1 /* cpu */, -1 /* group_fd */, 0 /* flags */); 1774e59afbbSIlya Leoshkevich if (pmu_fd == -1) { 1784e59afbbSIlya Leoshkevich if (errno == ENOENT) { 17966bd2ec1SStanislav Fomichev printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", 1804e59afbbSIlya Leoshkevich __func__); 181cd9c21d7SStanislav Fomichev test__skip(); 18286ccc384SStanislav Fomichev return; 1834e59afbbSIlya Leoshkevich } 1844e59afbbSIlya Leoshkevich /* Let the test fail with a more informative message */ 1854e59afbbSIlya Leoshkevich } else { 1864e59afbbSIlya Leoshkevich close(pmu_fd); 1874e59afbbSIlya Leoshkevich } 1884e59afbbSIlya Leoshkevich 1896f6cc426SYonghong Song test_send_signal_common(&attr, signal_thread); 19016f0efc3SYonghong Song } 19116f0efc3SYonghong Song 19216f0efc3SYonghong Song void test_send_signal(void) 19316f0efc3SYonghong Song { 194b207edfeSAndrii Nakryiko if (test__start_subtest("send_signal_tracepoint")) 195ab8b7f0cSYonghong Song test_send_signal_tracepoint(false); 196b207edfeSAndrii Nakryiko if (test__start_subtest("send_signal_perf")) 197ab8b7f0cSYonghong Song test_send_signal_perf(false); 198b207edfeSAndrii Nakryiko if (test__start_subtest("send_signal_nmi")) 199ab8b7f0cSYonghong Song test_send_signal_nmi(false); 200ab8b7f0cSYonghong Song if (test__start_subtest("send_signal_tracepoint_thread")) 201ab8b7f0cSYonghong Song test_send_signal_tracepoint(true); 202ab8b7f0cSYonghong Song if (test__start_subtest("send_signal_perf_thread")) 203ab8b7f0cSYonghong Song test_send_signal_perf(true); 204ab8b7f0cSYonghong Song if (test__start_subtest("send_signal_nmi_thread")) 205ab8b7f0cSYonghong Song test_send_signal_nmi(true); 20616f0efc3SYonghong Song } 207