Skip to content

Commit 92a86b7

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
selftests/bpf: Add tests to verify no unintended eviction when updating lru hash maps
Add two tests to verify that updating an existing element in LRU hash maps does not cause unintended eviction of other elements. The test creates lru_hash/lru_percpu_hash maps with max_entries slots and populates all of them. It then updates an existing key and verifies that: 1. The update succeeds without error 2. The updated key has the new value 3. All other keys still exist with their original values This validates the fix that prevents unnecessary LRU eviction when updating existing elements in full LRU hash maps. Signed-off-by: Leon Hwang <[email protected]>
1 parent bf941b9 commit 92a86b7

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,76 @@ void test_htab_update(void)
143143
if (test__start_subtest("concurrent_update"))
144144
test_concurrent_update();
145145
}
146+
147+
static void test_lru_hash_map_update_elem(enum bpf_map_type map_type)
148+
{
149+
int err, map_fd, i, key, nr_cpus, max_entries = 128;
150+
u64 *values, value = 0xDEADC0DE;
151+
152+
nr_cpus = libbpf_num_possible_cpus();
153+
if (!ASSERT_GT(nr_cpus, 0, "libbpf_num_possible_cpus"))
154+
return;
155+
156+
values = calloc(nr_cpus, sizeof(u64));
157+
if (!ASSERT_OK_PTR(values, "calloc values"))
158+
return;
159+
for (i = 0; i < nr_cpus; i++)
160+
values[i] = value;
161+
162+
map_fd = bpf_map_create(map_type, "test_lru", sizeof(int), sizeof(u64), max_entries, NULL);
163+
if (!ASSERT_GE(map_fd, 0, "bpf_map_create")) {
164+
free(values);
165+
return;
166+
}
167+
168+
/* populate all slots */
169+
for (key = 0; key < max_entries; key++) {
170+
err = bpf_map_update_elem(map_fd, &key, values, 0);
171+
if (!ASSERT_OK(err, "bpf_map_update_elem"))
172+
goto out;
173+
}
174+
175+
/* LRU eviction should not happen */
176+
177+
key = 0;
178+
memset(values, 0, nr_cpus * sizeof(u64));
179+
err = bpf_map_update_elem(map_fd, &key, values, 0);
180+
if (!ASSERT_OK(err, "bpf_map_update_elem"))
181+
goto out;
182+
183+
err = bpf_map_lookup_elem(map_fd, &key, values);
184+
if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
185+
goto out;
186+
if (!ASSERT_EQ(*values, 0, "bpf_map_lookup_elem value"))
187+
goto out;
188+
189+
for (key = 1; key < max_entries; key++) {
190+
err = bpf_map_lookup_elem(map_fd, &key, values);
191+
if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
192+
goto out;
193+
if (!ASSERT_EQ(*values, value, "bpf_map_lookup_elem value"))
194+
goto out;
195+
}
196+
197+
out:
198+
close(map_fd);
199+
free(values);
200+
}
201+
202+
static void test_update_lru_hash_map(void)
203+
{
204+
test_lru_hash_map_update_elem(BPF_MAP_TYPE_LRU_HASH);
205+
}
206+
207+
static void test_update_lru_percpu_hash_map(void)
208+
{
209+
test_lru_hash_map_update_elem(BPF_MAP_TYPE_LRU_PERCPU_HASH);
210+
}
211+
212+
void test_update_lru_hash_maps(void)
213+
{
214+
if (test__start_subtest("lru_hash"))
215+
test_update_lru_hash_map();
216+
if (test__start_subtest("lru_percpu_hash"))
217+
test_update_lru_percpu_hash_map();
218+
}

0 commit comments

Comments
 (0)