Skip to content

Commit 3d9c463

Browse files
bkammerdAlexei Starovoitov
authored andcommitted
selftests/bpf: add test for softlock when modifying hashmap while iterating
Add test that modifies the map while it's being iterated in such a way that hangs the kernel thread unless the _safe fix is applied to bpf_for_each_hash_elem. Signed-off-by: Brandon Kammerdiener <brandon.kammerdiener@intel.com> Link: https://lore.kernel.org/r/20250424153246.141677-3-brandon.kammerdiener@intel.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Hou Tao <houtao1@huawei.com>
1 parent 75673fd commit 3d9c463

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "for_each_array_map_elem.skel.h"
77
#include "for_each_map_elem_write_key.skel.h"
88
#include "for_each_multi_maps.skel.h"
9+
#include "for_each_hash_modify.skel.h"
910

1011
static unsigned int duration;
1112

@@ -203,6 +204,40 @@ static void test_multi_maps(void)
203204
for_each_multi_maps__destroy(skel);
204205
}
205206

207+
static void test_hash_modify(void)
208+
{
209+
struct for_each_hash_modify *skel;
210+
int max_entries, i, err;
211+
__u64 key, val;
212+
213+
LIBBPF_OPTS(bpf_test_run_opts, topts,
214+
.data_in = &pkt_v4,
215+
.data_size_in = sizeof(pkt_v4),
216+
.repeat = 1
217+
);
218+
219+
skel = for_each_hash_modify__open_and_load();
220+
if (!ASSERT_OK_PTR(skel, "for_each_hash_modify__open_and_load"))
221+
return;
222+
223+
max_entries = bpf_map__max_entries(skel->maps.hashmap);
224+
for (i = 0; i < max_entries; i++) {
225+
key = i;
226+
val = i;
227+
err = bpf_map__update_elem(skel->maps.hashmap, &key, sizeof(key),
228+
&val, sizeof(val), BPF_ANY);
229+
if (!ASSERT_OK(err, "map_update"))
230+
goto out;
231+
}
232+
233+
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.test_pkt_access), &topts);
234+
ASSERT_OK(err, "bpf_prog_test_run_opts");
235+
ASSERT_OK(topts.retval, "retval");
236+
237+
out:
238+
for_each_hash_modify__destroy(skel);
239+
}
240+
206241
void test_for_each(void)
207242
{
208243
if (test__start_subtest("hash_map"))
@@ -213,4 +248,6 @@ void test_for_each(void)
213248
test_write_map_key();
214249
if (test__start_subtest("multi_maps"))
215250
test_multi_maps();
251+
if (test__start_subtest("hash_modify"))
252+
test_hash_modify();
216253
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Intel Corporation */
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
struct {
9+
__uint(type, BPF_MAP_TYPE_HASH);
10+
__uint(max_entries, 128);
11+
__type(key, __u64);
12+
__type(value, __u64);
13+
} hashmap SEC(".maps");
14+
15+
static int cb(struct bpf_map *map, __u64 *key, __u64 *val, void *arg)
16+
{
17+
bpf_map_delete_elem(map, key);
18+
bpf_map_update_elem(map, key, val, 0);
19+
return 0;
20+
}
21+
22+
SEC("tc")
23+
int test_pkt_access(struct __sk_buff *skb)
24+
{
25+
(void)skb;
26+
27+
bpf_for_each_map_elem(&hashmap, cb, NULL, 0);
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)