Skip to content

Commit 0dea227

Browse files
sidchintamaneniKernel Patches Daemon
authored andcommitted
selftests/bpf: Adds selftests to check termination of long running nested bpf loops
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 <[email protected]> Signed-off-by: Siddharth Chintamaneni <[email protected]>
1 parent 8167865 commit 0dea227

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include <sys/socket.h>
5+
6+
#include "bpf_termination.skel.h"
7+
8+
void test_loop_termination(void)
9+
{
10+
struct bpf_termination *skel;
11+
int err;
12+
13+
skel = bpf_termination__open();
14+
if (!ASSERT_OK_PTR(skel, "bpf_termination__open"))
15+
return;
16+
17+
err = bpf_termination__load(skel);
18+
if (!ASSERT_OK(err, "bpf_termination__load"))
19+
goto out;
20+
21+
skel->bss->pid = getpid();
22+
err = bpf_termination__attach(skel);
23+
if (!ASSERT_OK(err, "bpf_termination__attach"))
24+
goto out;
25+
26+
/* Triggers long running BPF program */
27+
socket(AF_UNSPEC, SOCK_DGRAM, 0);
28+
29+
/* If the program is not terminated, it doesn't reach this point */
30+
ASSERT_TRUE(true, "Program is terminated");
31+
out:
32+
bpf_termination__destroy(skel);
33+
}
34+
35+
void test_bpf_termination(void)
36+
{
37+
if (test__start_subtest("bpf_termination"))
38+
test_loop_termination();
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stddef.h>
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
6+
int pid;
7+
8+
#define LOOPS_CNT 1 << 10
9+
10+
static int callback_fn4(void *ctx) {
11+
return 0;
12+
}
13+
14+
static int callback_fn3(void *ctx) {
15+
16+
bpf_loop(LOOPS_CNT, callback_fn4, NULL, 0);
17+
return 0;
18+
19+
}
20+
21+
22+
static int callback_fn2(void *ctx) {
23+
24+
bpf_loop(LOOPS_CNT, callback_fn3, NULL, 0);
25+
return 0;
26+
27+
}
28+
29+
static int callback_fn(void *ctx) {
30+
31+
bpf_loop(LOOPS_CNT, callback_fn2, NULL, 0);
32+
return 0;
33+
34+
}
35+
36+
SEC("tp/syscalls/sys_enter_socket")
37+
int bpf_loop_lr(void *ctx) {
38+
39+
if ((bpf_get_current_pid_tgid() >> 32) != pid)
40+
return 0;
41+
42+
bpf_loop(LOOPS_CNT, callback_fn, NULL, 0);
43+
44+
return 0;
45+
}
46+
47+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)