bpf: Adjust free target to avoid global starvation of LRU map
authorWillem de Bruijn <willemb@google.com>
Wed, 18 Jun 2025 21:57:40 +0000 (17:57 -0400)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 19 Jun 2025 01:50:14 +0000 (18:50 -0700)
commitd4adf1c9ee7722545450608bcb095fb31512f0c6
treefb176387cac4f2831053edfe8e24f5fd878d084f
parenta766cfbbeb3a74397965a8fa2e9a402026d3e1d8
bpf: Adjust free target to avoid global starvation of LRU map

BPF_MAP_TYPE_LRU_HASH can recycle most recent elements well before the
map is full, due to percpu reservations and force shrink before
neighbor stealing. Once a CPU is unable to borrow from the global map,
it will once steal one elem from a neighbor and after that each time
flush this one element to the global list and immediately recycle it.

Batch value LOCAL_FREE_TARGET (128) will exhaust a 10K element map
with 79 CPUs. CPU 79 will observe this behavior even while its
neighbors hold 78 * 127 + 1 * 15 == 9921 free elements (99%).

CPUs need not be active concurrently. The issue can appear with
affinity migration, e.g., irqbalance. Each CPU can reserve and then
hold onto its 128 elements indefinitely.

Avoid global list exhaustion by limiting aggregate percpu caches to
half of map size, by adjusting LOCAL_FREE_TARGET based on cpu count.
This change has no effect on sufficiently large tables.

Similar to LOCAL_NR_SCANS and lru->nr_scans, introduce a map variable
lru->free_target. The extra field fits in a hole in struct bpf_lru.
The cacheline is already warm where read in the hot path. The field is
only accessed with the lru lock held.

Tested-by: Anton Protopopov <a.s.protopopov@gmail.com>
Signed-off-by: Willem de Bruijn <willemb@google.com>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://lore.kernel.org/r/20250618215803.3587312-1-willemdebruijn.kernel@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Documentation/bpf/map_hash.rst
Documentation/bpf/map_lru_hash_update.dot
kernel/bpf/bpf_lru_list.c
kernel/bpf/bpf_lru_list.h
tools/testing/selftests/bpf/test_lru_map.c