]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
bpf: arm64: Optimize recursion detection by not using atomics
authorPuranjay Mohan <puranjay@kernel.org>
Fri, 19 Dec 2025 18:44:18 +0000 (10:44 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 21 Dec 2025 18:54:37 +0000 (10:54 -0800)
commitc3e34f88f9992866a1fb510850921a8fe299a97b
treee437974093abf3f2c4bd9f3c852e2b3fe9125744
parent93f0d09697613beba922a387d21a09a41eeefef5
bpf: arm64: Optimize recursion detection by not using atomics

BPF programs detect recursion using a per-CPU 'active' flag in struct
bpf_prog. The trampoline currently sets/clears this flag with atomic
operations.

On some arm64 platforms (e.g., Neoverse V2 with LSE), per-CPU atomic
operations are relatively slow. Unlike x86_64 - where per-CPU updates
can avoid cross-core atomicity, arm64 LSE atomics are always atomic
across all cores, which is unnecessary overhead for strictly per-CPU
state.

This patch removes atomics from the recursion detection path on arm64 by
changing 'active' to a per-CPU array of four u8 counters, one per
context: {NMI, hard-irq, soft-irq, normal}. The running context uses a
non-atomic increment/decrement on its element.  After increment,
recursion is detected by reading the array as a u32 and verifying that
only the expected element changed; any change in another element
indicates inter-context recursion, and a value > 1 in the same element
indicates same-context recursion.

For example, starting from {0,0,0,0}, a normal-context trigger changes
the array to {0,0,0,1}.  If an NMI arrives on the same CPU and triggers
the program, the array becomes {1,0,0,1}. When the NMI context checks
the u32 against the expected mask for normal (0x00000001), it observes
0x01000001 and correctly reports recursion. Same-context recursion is
detected analogously.

Acked-by: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20251219184422.2899902-3-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
kernel/bpf/core.c