Improve send_signal BPF test stability
authorMykola Lysenko <mykolal@fb.com>
Tue, 8 Mar 2022 20:04:48 +0000 (12:04 -0800)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 9 Mar 2022 01:39:29 +0000 (17:39 -0800)
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
tools/testing/selftests/bpf/prog_tests/send_signal.c
tools/testing/selftests/bpf/progs/test_send_signal_kern.c

index 776916b61c4067880dbb30da4995c65b8d8674b2..def50f1c5c31e5b316314ab60b0342d383e0b8b8 100644 (file)
@@ -4,11 +4,11 @@
 #include <sys/resource.h>
 #include "test_send_signal_kern.skel.h"
 
-int sigusr1_received = 0;
+static int sigusr1_received;
 
 static void sigusr1_handler(int signum)
 {
-       sigusr1_received++;
+       sigusr1_received = 1;
 }
 
 static void test_send_signal_common(struct perf_event_attr *attr,
@@ -40,9 +40,10 @@ static void test_send_signal_common(struct perf_event_attr *attr,
 
        if (pid == 0) {
                int old_prio;
+               volatile int j = 0;
 
                /* install signal handler and notify parent */
-               signal(SIGUSR1, sigusr1_handler);
+               ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal");
 
                close(pipe_c2p[0]); /* close read */
                close(pipe_p2c[1]); /* close write */
@@ -63,9 +64,11 @@ static void test_send_signal_common(struct perf_event_attr *attr,
                ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
 
                /* wait a little for signal handler */
-               sleep(1);
+               for (int i = 0; i < 100000000 && !sigusr1_received; i++)
+                       j /= i + 1;
 
                buf[0] = sigusr1_received ? '2' : '0';
+               ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
                ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
 
                /* wait for parent notification and exit */
@@ -93,7 +96,7 @@ static void test_send_signal_common(struct perf_event_attr *attr,
                        goto destroy_skel;
                }
        } else {
-               pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1,
+               pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */,
                                 -1 /* group id */, 0 /* flags */);
                if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
                        err = -1;
@@ -110,9 +113,9 @@ static void test_send_signal_common(struct perf_event_attr *attr,
        ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
 
        /* trigger the bpf send_signal */
-       skel->bss->pid = pid;
-       skel->bss->sig = SIGUSR1;
        skel->bss->signal_thread = signal_thread;
+       skel->bss->sig = SIGUSR1;
+       skel->bss->pid = pid;
 
        /* notify child that bpf program can send_signal now */
        ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
index b4233d3efac2deb8b56c123ac9e87ec1a133a5d7..92354cd7204407475b4d5594d8e69512ebcd3b67 100644 (file)
@@ -10,7 +10,7 @@ static __always_inline int bpf_send_signal_test(void *ctx)
 {
        int ret;
 
-       if (status != 0 || sig == 0 || pid == 0)
+       if (status != 0 || pid == 0)
                return 0;
 
        if ((bpf_get_current_pid_tgid() >> 32) == pid) {