Adds tests checks for loops termination which are nested. 32/1 bpf_termination/bpf_termination:OK 32 bpf_termination:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Raj Sahu Signed-off-by: Siddharth Chintamaneni --- .../bpf/prog_tests/bpf_termination.c | 39 +++++++++++++++ .../selftests/bpf/progs/bpf_termination.c | 47 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_termination.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_termination.c diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_termination.c b/tools/testing/selftests/bpf/prog_tests/bpf_termination.c new file mode 100644 index 000000000000..d060073db8f9 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/bpf_termination.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +#include "bpf_termination.skel.h" + +void test_loop_termination(void) +{ + struct bpf_termination *skel; + int err; + + skel = bpf_termination__open(); + if (!ASSERT_OK_PTR(skel, "bpf_termination__open")) + return; + + err = bpf_termination__load(skel); + if (!ASSERT_OK(err, "bpf_termination__load")) + goto out; + + skel->bss->pid = getpid(); + err = bpf_termination__attach(skel); + if (!ASSERT_OK(err, "bpf_termination__attach")) + goto out; + + /* Triggers long running BPF program */ + socket(AF_UNSPEC, SOCK_DGRAM, 0); + + /* If the program is not terminated, it doesn't reach this point */ + ASSERT_TRUE(true, "Program is terminated"); +out: + bpf_termination__destroy(skel); +} + +void test_bpf_termination(void) +{ + if (test__start_subtest("bpf_termination")) + test_loop_termination(); +} diff --git a/tools/testing/selftests/bpf/progs/bpf_termination.c b/tools/testing/selftests/bpf/progs/bpf_termination.c new file mode 100644 index 000000000000..36e97d84750b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_termination.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +int pid; + +#define LOOPS_CNT 1 << 10 + +static int callback_fn4(void *ctx) { + return 0; +} + +static int callback_fn3(void *ctx) { + + bpf_loop(LOOPS_CNT, callback_fn4, NULL, 0); + return 0; + +} + + +static int callback_fn2(void *ctx) { + + bpf_loop(LOOPS_CNT, callback_fn3, NULL, 0); + return 0; + +} + +static int callback_fn(void *ctx) { + + bpf_loop(LOOPS_CNT, callback_fn2, NULL, 0); + return 0; + +} + +SEC("tp/syscalls/sys_enter_socket") +int bpf_loop_lr(void *ctx) { + + if ((bpf_get_current_pid_tgid() >> 32) != pid) + return 0; + + bpf_loop(LOOPS_CNT, callback_fn, NULL, 0); + + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.43.0