summaryrefslogtreecommitdiff
path: root/scripts/basic/split-include.c
diff options
context:
space:
mode:
authorPuranjay Mohan <puranjay@kernel.org>2026-10-21 11:51:16 -0700
committerAlexei Starovoitov <ast@kernel.org>2026-10-30 22:18:50 +0000
commit1cd28b5354ab77c727ed87255bf289a87178b415 (patch)
tree93aefd9676ed7548f435f5ed5f9805890a0e431a /scripts/basic/split-include.c
parent3e4279dc5144a5afcda101df23792b13e94c3859 (diff)
downloadlinux-1cd28b5354ab77c727ed87255bf289a87178b415.tar.gz
linux-1cd28b5354ab77c727ed87255bf289a87178b415.zip
bpf: Drop percpu_counter from the htab element count
The element count of a large non-preallocated hash map is a percpu_counter, whose slow path takes fbc->lock with raw_spin_lock_irqsave(). That is not safe against BPF programs that run while the lock is held or contended: NMI programs and, as queued_spin_lock_slowpath() calls trace_contention_end() with the lock already held, programs attached to the contention_end tracepoint. If such a program updates the same map it spins on the lock its own CPU holds: WARNING: possible recursive locking detected cend/250 is trying to acquire lock: (key#5){....}-{2:2}, at: _raw_spin_lock_irqsave+0x18/0x30 but task is already holding lock: (key#5){....}-{2:2}, at: _raw_spin_lock_irqsave+0x18/0x30 percpu_counter_add_batch+0xd8/0x160 alloc_htab_elem+0x408/0x480 htab_map_update_elem+0x1e8/0x6a0 bpf_prog_7a29a1ae68aedf07_cend+0x14c/0x1f8 __bpf_trace_contention_end+0x14/0x20 queued_spin_lock_slowpath+0x2e8/0x468 percpu_counter_add_batch+0xd8/0x160 alloc_htab_elem+0x408/0x480 htab_map_update_elem+0x1e8/0x6a0 is_map_full() is affected too, as __percpu_counter_compare() falls back to percpu_counter_sum(), which takes the same lock. map.elem_count already is a lockless per-cpu count of the same elements. Make htab->count follow it in steps of PERCPU_COUNTER_BATCH and let is_map_full() sum elem_count once count is within nr_cpu_ids * batch of max_entries. Each cpu's elem_count is ahead of count by less than the batch, so that is exact, in NMI as well. Fixes: 86fe28f7692d ("bpf: Optimize element count in non-preallocated hash map.") Suggested-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Puranjay Mohan <puranjay@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://patch.msgid.link/20261021185117.650403-1-puranjay@kernel.org
Diffstat (limited to 'scripts/basic/split-include.c')
0 files changed, 0 insertions, 0 deletions