Skip to content

Commit fe9d205

Browse files
mykyta5anakryiko
authored andcommitted
selftests/bpf: Verify bpf_timer_cancel_async works
Add test that verifies that bpf_timer_cancel_async works: can cancel callback successfully. Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20260201025403.66625-7-alexei.starovoitov@gmail.com
1 parent d02fdd7 commit fe9d205

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ static int timer(struct timer *timer_skel)
9999
return 0;
100100
}
101101

102+
static int timer_cancel_async(struct timer *timer_skel)
103+
{
104+
int err, prog_fd;
105+
LIBBPF_OPTS(bpf_test_run_opts, topts);
106+
107+
prog_fd = bpf_program__fd(timer_skel->progs.test_async_cancel_succeed);
108+
err = bpf_prog_test_run_opts(prog_fd, &topts);
109+
ASSERT_OK(err, "test_run");
110+
ASSERT_EQ(topts.retval, 0, "test_run");
111+
112+
usleep(500);
113+
/* check that there were no errors in timer execution */
114+
ASSERT_EQ(timer_skel->bss->err, 0, "err");
115+
116+
/* check that code paths completed */
117+
ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
118+
119+
return 0;
120+
}
121+
102122
static void test_timer(int (*timer_test_fn)(struct timer *timer_skel))
103123
{
104124
struct timer *timer_skel = NULL;
@@ -134,6 +154,11 @@ void serial_test_timer_stress_async_cancel(void)
134154
test_timer(timer_stress_async_cancel);
135155
}
136156

157+
void serial_test_timer_async_cancel(void)
158+
{
159+
test_timer(timer_cancel_async);
160+
}
161+
137162
void test_timer_interrupt(void)
138163
{
139164
struct timer_interrupt *skel = NULL;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,29 @@ int BPF_PROG2(test1, int, a)
169169
return 0;
170170
}
171171

172+
static int timer_error(void *map, int *key, struct bpf_timer *timer)
173+
{
174+
err = 42;
175+
return 0;
176+
}
177+
178+
SEC("syscall")
179+
int test_async_cancel_succeed(void *ctx)
180+
{
181+
struct bpf_timer *arr_timer;
182+
int array_key = ARRAY;
183+
184+
arr_timer = bpf_map_lookup_elem(&array, &array_key);
185+
if (!arr_timer)
186+
return 0;
187+
bpf_timer_init(arr_timer, &array, CLOCK_MONOTONIC);
188+
bpf_timer_set_callback(arr_timer, timer_error);
189+
bpf_timer_start(arr_timer, 100000 /* 100us */, 0);
190+
bpf_timer_cancel_async(arr_timer);
191+
ok = 7;
192+
return 0;
193+
}
194+
172195
/* callback for prealloc and non-prealloca hashtab timers */
173196
static int timer_cb2(void *map, int *key, struct hmap_elem *val)
174197
{

0 commit comments

Comments
 (0)