Skip to content

Commit accb9a7

Browse files
mannkafaiAlexei Starovoitov
authored andcommitted
selftests/bpf: Add test for unpinning htab with internal timer struct
Add test to verify that unpinning hash tables containing internal timer structures does not trigger context warnings. Each subtest (timer_prealloc and timer_no_prealloc) can trigger the context warning when unpinning, but the warning cannot be triggered twice within a short time interval (a HZ), which is expected behavior. Signed-off-by: KaFai Wan <kafai.wan@linux.dev> Acked-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20251008102628.808045-3-kafai.wan@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 4f375ad commit accb9a7

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "test_pinning_htab.skel.h"
5+
6+
static void unpin_map(const char *map_name, const char *pin_path)
7+
{
8+
struct test_pinning_htab *skel;
9+
struct bpf_map *map;
10+
int err;
11+
12+
skel = test_pinning_htab__open_and_load();
13+
if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
14+
return;
15+
16+
map = bpf_object__find_map_by_name(skel->obj, map_name);
17+
if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name"))
18+
goto out;
19+
20+
err = bpf_map__pin(map, pin_path);
21+
if (!ASSERT_OK(err, "bpf_map__pin"))
22+
goto out;
23+
24+
err = bpf_map__unpin(map, pin_path);
25+
ASSERT_OK(err, "bpf_map__unpin");
26+
out:
27+
test_pinning_htab__destroy(skel);
28+
}
29+
30+
void test_pinning_htab(void)
31+
{
32+
if (test__start_subtest("timer_prealloc"))
33+
unpin_map("timer_prealloc", "/sys/fs/bpf/timer_prealloc");
34+
if (test__start_subtest("timer_no_prealloc"))
35+
unpin_map("timer_no_prealloc", "/sys/fs/bpf/timer_no_prealloc");
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
struct timer_val {
9+
struct bpf_timer timer;
10+
};
11+
12+
struct {
13+
__uint(type, BPF_MAP_TYPE_HASH);
14+
__type(key, __u32);
15+
__type(value, struct timer_val);
16+
__uint(max_entries, 1);
17+
} timer_prealloc SEC(".maps");
18+
19+
struct {
20+
__uint(type, BPF_MAP_TYPE_HASH);
21+
__type(key, __u32);
22+
__type(value, struct timer_val);
23+
__uint(max_entries, 1);
24+
__uint(map_flags, BPF_F_NO_PREALLOC);
25+
} timer_no_prealloc SEC(".maps");

0 commit comments

Comments
 (0)