Skip to content

Commit 6ae003a

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-fix-softlock-condition-in-bpf-hashmap-interation'
Brandon Kammerdiener says: ==================== This patchset fixes an endless loop condition that can occur in bpf_for_each_hash_elem, causing the core to softlock. My understanding is that a combination of RCU list deletion and insertion introduces the new element after the iteration cursor and that there is a chance that an RCU reader may in fact use this new element in iteration. The patch uses a _safe variant of the macro which gets the next element to iterate before executing the loop body for the current element. I have also added a subtest in the for_each selftest that can trigger this condition without the fix. Changes since v2: - Renaming and additional checks in selftests/bpf/prog_tests/for_each.c Changes since v1: - Added missing Signed-off-by lines to both patches ==================== Acked-by: Hou Tao <houtao1@huawei.com> Link: https://patch.msgid.link/20250424153246.141677-1-brandon.kammerdiener@intel.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents f2858f3 + 3d9c463 commit 6ae003a

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

kernel/bpf/hashtab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ static long bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_
21892189
b = &htab->buckets[i];
21902190
rcu_read_lock();
21912191
head = &b->head;
2192-
hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
2192+
hlist_nulls_for_each_entry_safe(elem, n, head, hash_node) {
21932193
key = elem->key;
21942194
if (is_percpu) {
21952195
/* current cpu value for percpu map */

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)