Skip to content

Commit 88a3bde

Browse files
AsphalttAlexei Starovoitov
authored andcommitted
selftests/bpf: Add case to test bpf_in_interrupt()
Add a timer test case to test 'bpf_in_interrupt()'. cd tools/testing/selftests/bpf ./test_progs -t timer_interrupt 462 timer_interrupt:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Leon Hwang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 4b69e31 commit 88a3bde

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

tools/testing/selftests/bpf/prog_tests/timer.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <test_progs.h>
44
#include "timer.skel.h"
55
#include "timer_failure.skel.h"
6+
#include "timer_interrupt.skel.h"
67

78
#define NUM_THR 8
89

@@ -95,3 +96,32 @@ void serial_test_timer(void)
9596

9697
RUN_TESTS(timer_failure);
9798
}
99+
100+
void test_timer_interrupt(void)
101+
{
102+
struct timer_interrupt *skel = NULL;
103+
int err, prog_fd;
104+
LIBBPF_OPTS(bpf_test_run_opts, opts);
105+
106+
skel = timer_interrupt__open_and_load();
107+
if (!ASSERT_OK_PTR(skel, "timer_interrupt__open_and_load"))
108+
return;
109+
110+
err = timer_interrupt__attach(skel);
111+
if (!ASSERT_OK(err, "timer_interrupt__attach"))
112+
goto out;
113+
114+
prog_fd = bpf_program__fd(skel->progs.test_timer_interrupt);
115+
err = bpf_prog_test_run_opts(prog_fd, &opts);
116+
if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
117+
goto out;
118+
119+
usleep(50);
120+
121+
ASSERT_EQ(skel->bss->in_interrupt, 0, "in_interrupt");
122+
if (skel->bss->preempt_count)
123+
ASSERT_NEQ(skel->bss->in_interrupt_cb, 0, "in_interrupt_cb");
124+
125+
out:
126+
timer_interrupt__destroy(skel);
127+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <vmlinux.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "bpf_experimental.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
#define CLOCK_MONOTONIC 1
10+
11+
int preempt_count;
12+
int in_interrupt;
13+
int in_interrupt_cb;
14+
15+
struct elem {
16+
struct bpf_timer t;
17+
};
18+
19+
struct {
20+
__uint(type, BPF_MAP_TYPE_ARRAY);
21+
__uint(max_entries, 1);
22+
__type(key, int);
23+
__type(value, struct elem);
24+
} array SEC(".maps");
25+
26+
static int timer_in_interrupt(void *map, int *key, struct bpf_timer *timer)
27+
{
28+
preempt_count = get_preempt_count();
29+
in_interrupt_cb = bpf_in_interrupt();
30+
return 0;
31+
}
32+
33+
SEC("fentry/bpf_fentry_test1")
34+
int BPF_PROG(test_timer_interrupt)
35+
{
36+
struct bpf_timer *timer;
37+
int key = 0;
38+
39+
timer = bpf_map_lookup_elem(&array, &key);
40+
if (!timer)
41+
return 0;
42+
43+
in_interrupt = bpf_in_interrupt();
44+
bpf_timer_init(timer, &array, CLOCK_MONOTONIC);
45+
bpf_timer_set_callback(timer, timer_in_interrupt);
46+
bpf_timer_start(timer, 0, 0);
47+
return 0;
48+
}

0 commit comments

Comments
 (0)