Skip to content

Conversation

@puranjaymohan
Copy link
Contributor

No description provided.

BPF programs detect recursion by doing atomic inc/dec on a per-cpu
active counter from the trampoline. Create two helpers for operations on
this active counter, this makes it easy to make changes later.

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
BPF programs detect recursion by a per-cpu active flag in struct
bpf_prog. This flag is set/unset through the trampoline using atomic
operations.

Some arm64 platforms have slow per-CPU atomic operations, for example,
the Neoverse V2.  This commit therefore changes the recursion detection
mechanism to not use atomics at all.

Change active to a per-cpu array of four u8 values, one for each context
and use non-atomic increment/decrement on the value for the running
context. After the increment, check for inter-context / same-context
recursion by looking at the whole array as a u32 and checking if any
other array element other than the expected element has changed.

So, initally the array is {0,0,0,0} for {NMI, hard-irq, soft-irq, normal}

Let's say the bpf program is triggered on normal context so after the
increment this array looks like: {0,0,0,1} now let's say NMI is received
on this CPU and the bpf program is again triggered in NMI context, so
the increment happens again the array now looks like: {1,0,0,1}. Now
when the NMI context checks the u32 for the value 0x000001, it will find
0x01000001 and recursion will be detected. Similarly, recursion from the
same context can be detected.

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot force-pushed the bpf-next_base branch 3 times, most recently from baaa336 to b6555d5 Compare November 6, 2025 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant