Skip to content

Commit bf941b9

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Avoid unintended eviction when updating lru_percpu_hash maps
Similar to the previous fix for lru_hash maps, the lru_percpu_hash map implementation also suffers from unnecessary eviction when updating existing elements. When updating a key that already exists in a full lru_percpu_hash map, the current code path calls prealloc_lru_pop() before checking for the existing key (unless map_flags is BPF_EXIST). This can evict an unrelated element even though the update is just modifying the per-CPU value of an existing entry. Fix this by looking up the key first. If found, update the per-CPU value in-place using pcpu_copy_value(), refresh the LRU reference, and return early. Only proceed with node allocation if the key does not exist. Fixes: 8f84493 ("bpf: Add BPF_MAP_TYPE_LRU_PERCPU_HASH") Signed-off-by: Leon Hwang <[email protected]>
1 parent 47f7984 commit bf941b9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

kernel/bpf/hashtab.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,28 @@ static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
13581358
b = __select_bucket(htab, hash);
13591359
head = &b->head;
13601360

1361+
ret = htab_lock_bucket(b, &flags);
1362+
if (ret)
1363+
goto err_lock_bucket;
1364+
1365+
l_old = lookup_elem_raw(head, hash, key, key_size);
1366+
1367+
ret = check_flags(htab, l_old, map_flags);
1368+
if (ret)
1369+
goto err;
1370+
1371+
if (l_old) {
1372+
bpf_lru_node_set_ref(&l_old->lru_node);
1373+
/* per-cpu hash map can update value in-place */
1374+
pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1375+
value, onallcpus);
1376+
}
1377+
1378+
htab_unlock_bucket(b, flags);
1379+
1380+
if (l_old)
1381+
return 0;
1382+
13611383
/* For LRU, we need to alloc before taking bucket's
13621384
* spinlock because LRU's elem alloc may need
13631385
* to remove older elem from htab and this removal

0 commit comments

Comments
 (0)