bpf: Do not allow deleting local storage in NMI
PATCH v2 1/1 bpf: Do not allow deleting local storage in NMI
Currently, local storage may deadlock when deferring freeing selem or local storage through kfree_rcu(), call_rcu() or call_rcu_tasks_trace() in NMI or reentrant. Since deleting selem in NMI is an unlikely use case, partially mitigate it by returning error when calling from bpf_xxx_storage_delete() helpers in NMI. Note that, it is still possible to deadlock through reentrant. A full mitigation requires returning error when irqs_disabled() is true, which, however is too heavy-handed for bpf_xxx_storage_delete().
The long-term solution requires _nolock versions of call_rcu. Another possible solution is to defer the free through irq_work [0], but it would grow the size of selem, which is non-ideal.
The check is only needed in bpf_selem_unlink(), which is used by helpers and syscalls. bpf_selem_unlink_nofail() is fine as it is called during map and owner tear down that never run in NMI or reentrant.
[0] https://lore.kernel.org/bpf/[email protected]/
--- kernel/bpf/bpf_local_storage.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/bpf/bpf_local_storage.c +3 -0
@@ -393,6 +393,9 @@ int bpf_selem_unlink(struct bpf_local_storage_elem *selem)unsigned long flags;int err; +if (in_nmi ()) +return -EOPNOTSUPP ; +if (unlikely (!selem_linked_to_storage_lockless (selem)))/* selem has already been unlinked from sk */ return 0 ;
Signature · 1 line
2.52.0
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Quoted 10 lines · On Wed, 18 Mar 2026 19:57:16 -0700 you wrote:
Currently, local storage may deadlock when deferring freeing selem or local storage through
kfree_rcu(),call_rcu()orcall_rcu_tasks_trace()in NMI or reentrant. Since deleting selem in NMI is an unlikely use case, partially mitigate it by returning error when calling frombpf_xxx_storage_delete()helpers in NMI. Note that, it is still possible to deadlock through reentrant. A full mitigation requires returning error whenirqs_disabled()is true, which, however is too heavy-handed forbpf_xxx_storage_delete().[...]
Here is the summary with links:
- [bpf-next,v2,1/1] bpf: Do not allow deleting local storage in NMI
https://git.kernel.org/bpf/bpf-next/c/350de5b8a9be
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html