Skip to content

Commit 740f8a6

Browse files
fomichevborkmann
authored andcommitted
selftests/bpf: make sure signal interrupts BPF_PROG_TEST_RUN
Simple test that I used to reproduce the issue in the previous commit: Do BPF_PROG_TEST_RUN with max iterations, each program is 4096 simple move instructions. File alarm in 0.1 second and check that bpf_prog_test_run is interrupted (i.e. test doesn't hang). Note: reposting this for bpf-next to avoid linux-next conflict. In this version I test both BPF_PROG_TYPE_SOCKET_FILTER (which uses generic bpf_test_run implementation) and BPF_PROG_TYPE_FLOW_DISSECTOR (which has it own loop with preempt handling in bpf_prog_test_run_flow_dissector). Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent a439184 commit 740f8a6

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tools/testing/selftests/bpf/test_progs.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stdlib.h>
1313
#include <stdarg.h>
1414
#include <time.h>
15+
#include <signal.h>
1516

1617
#include <linux/types.h>
1718
typedef __u16 __sum16;
@@ -28,6 +29,7 @@ typedef __u16 __sum16;
2829
#include <sys/ioctl.h>
2930
#include <sys/wait.h>
3031
#include <sys/types.h>
32+
#include <sys/time.h>
3133
#include <fcntl.h>
3234
#include <pthread.h>
3335
#include <linux/bpf.h>
@@ -2108,6 +2110,46 @@ static void test_map_lock(void)
21082110
bpf_object__close(obj);
21092111
}
21102112

2113+
static void sigalrm_handler(int s) {}
2114+
static struct sigaction sigalrm_action = {
2115+
.sa_handler = sigalrm_handler,
2116+
};
2117+
2118+
static void test_signal_pending(enum bpf_prog_type prog_type)
2119+
{
2120+
struct bpf_insn prog[4096];
2121+
struct itimerval timeo = {
2122+
.it_value.tv_usec = 100000, /* 100ms */
2123+
};
2124+
__u32 duration, retval;
2125+
int prog_fd;
2126+
int err;
2127+
int i;
2128+
2129+
for (i = 0; i < ARRAY_SIZE(prog); i++)
2130+
prog[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0);
2131+
prog[ARRAY_SIZE(prog) - 1] = BPF_EXIT_INSN();
2132+
2133+
prog_fd = bpf_load_program(prog_type, prog, ARRAY_SIZE(prog),
2134+
"GPL", 0, NULL, 0);
2135+
CHECK(prog_fd < 0, "test-run", "errno %d\n", errno);
2136+
2137+
err = sigaction(SIGALRM, &sigalrm_action, NULL);
2138+
CHECK(err, "test-run-signal-sigaction", "errno %d\n", errno);
2139+
2140+
err = setitimer(ITIMER_REAL, &timeo, NULL);
2141+
CHECK(err, "test-run-signal-timer", "errno %d\n", errno);
2142+
2143+
err = bpf_prog_test_run(prog_fd, 0xffffffff, &pkt_v4, sizeof(pkt_v4),
2144+
NULL, NULL, &retval, &duration);
2145+
CHECK(duration > 500000000, /* 500ms */
2146+
"test-run-signal-duration",
2147+
"duration %dns > 500ms\n",
2148+
duration);
2149+
2150+
signal(SIGALRM, SIG_DFL);
2151+
}
2152+
21112153
int main(void)
21122154
{
21132155
srand(time(NULL));
@@ -2138,6 +2180,8 @@ int main(void)
21382180
test_flow_dissector();
21392181
test_spinlock();
21402182
test_map_lock();
2183+
test_signal_pending(BPF_PROG_TYPE_SOCKET_FILTER);
2184+
test_signal_pending(BPF_PROG_TYPE_FLOW_DISSECTOR);
21412185

21422186
printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
21432187
return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;

0 commit comments

Comments
 (0)