Commit 1fd49864 authored by Mykola Lysenko's avatar Mykola Lysenko Committed by Andrii Nakryiko

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: default avatarMykola Lysenko <mykolal@fb.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220308200449.1757478-3-mykolal@fb.com
parent d4b54054
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
#include <sys/resource.h> #include <sys/resource.h>
#include "test_send_signal_kern.skel.h" #include "test_send_signal_kern.skel.h"
int sigusr1_received = 0; static int sigusr1_received;
static void sigusr1_handler(int signum) static void sigusr1_handler(int signum)
{ {
sigusr1_received++; sigusr1_received = 1;
} }
static void test_send_signal_common(struct perf_event_attr *attr, 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, ...@@ -40,9 +40,10 @@ static void test_send_signal_common(struct perf_event_attr *attr,
if (pid == 0) { if (pid == 0) {
int old_prio; int old_prio;
volatile int j = 0;
/* install signal handler and notify parent */ /* 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_c2p[0]); /* close read */
close(pipe_p2c[1]); /* close write */ close(pipe_p2c[1]); /* close write */
...@@ -63,9 +64,11 @@ static void test_send_signal_common(struct perf_event_attr *attr, ...@@ -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"); ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
/* wait a little for signal handler */ /* 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'; buf[0] = sigusr1_received ? '2' : '0';
ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write"); ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
/* wait for parent notification and exit */ /* wait for parent notification and exit */
...@@ -93,7 +96,7 @@ static void test_send_signal_common(struct perf_event_attr *attr, ...@@ -93,7 +96,7 @@ static void test_send_signal_common(struct perf_event_attr *attr,
goto destroy_skel; goto destroy_skel;
} }
} else { } 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 */); -1 /* group id */, 0 /* flags */);
if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) { if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
err = -1; err = -1;
...@@ -110,9 +113,9 @@ static void test_send_signal_common(struct perf_event_attr *attr, ...@@ -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"); ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
/* trigger the bpf send_signal */ /* trigger the bpf send_signal */
skel->bss->pid = pid;
skel->bss->sig = SIGUSR1;
skel->bss->signal_thread = signal_thread; skel->bss->signal_thread = signal_thread;
skel->bss->sig = SIGUSR1;
skel->bss->pid = pid;
/* notify child that bpf program can send_signal now */ /* notify child that bpf program can send_signal now */
ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write"); ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
......
...@@ -10,7 +10,7 @@ static __always_inline int bpf_send_signal_test(void *ctx) ...@@ -10,7 +10,7 @@ static __always_inline int bpf_send_signal_test(void *ctx)
{ {
int ret; int ret;
if (status != 0 || sig == 0 || pid == 0) if (status != 0 || pid == 0)
return 0; return 0;
if ((bpf_get_current_pid_tgid() >> 32) == pid) { if ((bpf_get_current_pid_tgid() >> 32) == pid) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment