Skip to content

Commit 0d7ae06

Browse files
Byte-Labborkmann
authored andcommitted
selftests/bpf: Test pinning bpf timer to a core
Now that we support pinning a BPF timer to the current core, we should test it with some selftests. This patch adds two new testcases to the timer suite, which verifies that a BPF timer both with and without BPF_F_TIMER_ABS, can be pinned to the calling core with BPF_F_TIMER_CPU_PIN. Signed-off-by: David Vernet <void@manifault.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Song Liu <song@kernel.org> Acked-by: Hou Tao <houtao1@huawei.com> Link: https://lore.kernel.org/bpf/20231004162339.200702-3-void@manifault.com
1 parent d6247ec commit 0d7ae06

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static int timer(struct timer *timer_skel)
1414

1515
ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
1616
ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
17+
ASSERT_EQ(timer_skel->bss->pinned_callback_check, 0, "pinned_callback_check1");
1718

1819
prog_fd = bpf_program__fd(timer_skel->progs.test1);
1920
err = bpf_prog_test_run_opts(prog_fd, &topts);
@@ -32,6 +33,9 @@ static int timer(struct timer *timer_skel)
3233
/* check that timer_cb3() was executed twice */
3334
ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data");
3435

36+
/* check that timer_cb_pinned() was executed twice */
37+
ASSERT_EQ(timer_skel->bss->pinned_callback_check, 2, "pinned_callback_check");
38+
3539
/* check that there were no errors in timer execution */
3640
ASSERT_EQ(timer_skel->bss->err, 0, "err");
3741

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ struct {
5151
__uint(max_entries, 1);
5252
__type(key, int);
5353
__type(value, struct elem);
54-
} abs_timer SEC(".maps");
54+
} abs_timer SEC(".maps"), soft_timer_pinned SEC(".maps"), abs_timer_pinned SEC(".maps");
5555

5656
__u64 bss_data;
5757
__u64 abs_data;
5858
__u64 err;
5959
__u64 ok;
6060
__u64 callback_check = 52;
6161
__u64 callback2_check = 52;
62+
__u64 pinned_callback_check;
63+
__s32 pinned_cpu;
6264

6365
#define ARRAY 1
6466
#define HTAB 2
@@ -329,3 +331,62 @@ int BPF_PROG2(test3, int, a)
329331

330332
return 0;
331333
}
334+
335+
/* callback for pinned timer */
336+
static int timer_cb_pinned(void *map, int *key, struct bpf_timer *timer)
337+
{
338+
__s32 cpu = bpf_get_smp_processor_id();
339+
340+
if (cpu != pinned_cpu)
341+
err |= 16384;
342+
343+
pinned_callback_check++;
344+
return 0;
345+
}
346+
347+
static void test_pinned_timer(bool soft)
348+
{
349+
int key = 0;
350+
void *map;
351+
struct bpf_timer *timer;
352+
__u64 flags = BPF_F_TIMER_CPU_PIN;
353+
__u64 start_time;
354+
355+
if (soft) {
356+
map = &soft_timer_pinned;
357+
start_time = 0;
358+
} else {
359+
map = &abs_timer_pinned;
360+
start_time = bpf_ktime_get_boot_ns();
361+
flags |= BPF_F_TIMER_ABS;
362+
}
363+
364+
timer = bpf_map_lookup_elem(map, &key);
365+
if (timer) {
366+
if (bpf_timer_init(timer, map, CLOCK_BOOTTIME) != 0)
367+
err |= 4096;
368+
bpf_timer_set_callback(timer, timer_cb_pinned);
369+
pinned_cpu = bpf_get_smp_processor_id();
370+
bpf_timer_start(timer, start_time + 1000, flags);
371+
} else {
372+
err |= 8192;
373+
}
374+
}
375+
376+
SEC("fentry/bpf_fentry_test4")
377+
int BPF_PROG2(test4, int, a)
378+
{
379+
bpf_printk("test4");
380+
test_pinned_timer(true);
381+
382+
return 0;
383+
}
384+
385+
SEC("fentry/bpf_fentry_test5")
386+
int BPF_PROG2(test5, int, a)
387+
{
388+
bpf_printk("test5");
389+
test_pinned_timer(false);
390+
391+
return 0;
392+
}

0 commit comments

Comments
 (0)