Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Improve send_signal BPF test stability

Substitute sleep with dummy CPU intensive computation.
Finish aforemention computation as soon as signal was
delivered to the test process. Make the BPF code to
only execute when PID global variable is set

Signed-off-by: Mykola Lysenko <mykolal@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220308200449.1757478-3-mykolal@fb.com

authored by

Mykola Lysenko and committed by
Andrii Nakryiko
1fd49864 d4b54054

+11 -8
+10 -7
tools/testing/selftests/bpf/prog_tests/send_signal.c
··· 4 4 #include <sys/resource.h> 5 5 #include "test_send_signal_kern.skel.h" 6 6 7 - int sigusr1_received = 0; 7 + static int sigusr1_received; 8 8 9 9 static void sigusr1_handler(int signum) 10 10 { 11 - sigusr1_received++; 11 + sigusr1_received = 1; 12 12 } 13 13 14 14 static void test_send_signal_common(struct perf_event_attr *attr, ··· 40 40 41 41 if (pid == 0) { 42 42 int old_prio; 43 + volatile int j = 0; 43 44 44 45 /* install signal handler and notify parent */ 45 - signal(SIGUSR1, sigusr1_handler); 46 + ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal"); 46 47 47 48 close(pipe_c2p[0]); /* close read */ 48 49 close(pipe_p2c[1]); /* close write */ ··· 64 63 ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read"); 65 64 66 65 /* wait a little for signal handler */ 67 - sleep(1); 66 + for (int i = 0; i < 100000000 && !sigusr1_received; i++) 67 + j /= i + 1; 68 68 69 69 buf[0] = sigusr1_received ? '2' : '0'; 70 + ASSERT_EQ(sigusr1_received, 1, "sigusr1_received"); 70 71 ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write"); 71 72 72 73 /* wait for parent notification and exit */ ··· 96 93 goto destroy_skel; 97 94 } 98 95 } else { 99 - pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1, 96 + pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */, 100 97 -1 /* group id */, 0 /* flags */); 101 98 if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) { 102 99 err = -1; ··· 113 110 ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read"); 114 111 115 112 /* trigger the bpf send_signal */ 116 - skel->bss->pid = pid; 117 - skel->bss->sig = SIGUSR1; 118 113 skel->bss->signal_thread = signal_thread; 114 + skel->bss->sig = SIGUSR1; 115 + skel->bss->pid = pid; 119 116 120 117 /* notify child that bpf program can send_signal now */ 121 118 ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
+1 -1
tools/testing/selftests/bpf/progs/test_send_signal_kern.c
··· 10 10 { 11 11 int ret; 12 12 13 - if (status != 0 || sig == 0 || pid == 0) 13 + if (status != 0 || pid == 0) 14 14 return 0; 15 15 16 16 if ((bpf_get_current_pid_tgid() >> 32) == pid) {