[PATCH net] bpf: udp: Do not call kvmalloc while holding BH spin lock

From: Herbert Xu

Date: Tue Dec 01 2026 - 03:52:50 EST


On Thu, Nov 05, 2026 at 07:44:30PM +0100, Uladzislau Rezki wrote:
> On Thu, Nov 05, 2026 at 04:51:39PM +0800, Herbert Xu wrote:
> > On Wed, Nov 04, 2026 at 12:11:24PM +0100, Uladzislau Rezki wrote:
>
> > > net/ipv4/udp.c:
> > > err = bpf_iter_udp_realloc_batch(iter, batch_sks, GFP_NOWAIT);
> >
> > This looks buggy in exactly the same way as rhashtable. It's
> > called under spin_lock_bh, and the same lock can be taken on
> > the softirq path on another CPU.

Since we're not making any progress on resolving this within kvmalloc,
let's fix UDP to not call kvmalloc with GFP_NOWAIT.

---8<---
Even though kvmalloc was modified to support being called with
non-blocking flags such as GFP_NOWAIT and GFP_ATOMIC, it cannot
do so safely when used inside spin locks that may be taken from
soft or hard IRQs. Doing so can lead to dead-lock because the
kvmalloc code takes naked spin locks without disabling soft or
hard IRQs.

Fix the UDP path to not call kvmalloc when BH spin locks are held.

Fixes: c6307674ed82 ("mm: kvmalloc: add non-blocking support for vmalloc")
Signed-off-by: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>

diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index d784faf939c3..613ea92e95b1 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -3886,8 +3886,12 @@ static int bpf_iter_udp_realloc_batch(struct bpf_udp_iter_state *iter,
{
union bpf_udp_iter_batch_item *new_batch;

- new_batch = kvmalloc_objs(*new_batch, new_batch_sz,
- flags | __GFP_NOWARN);
+ if (flags != GFP_NOWAIT)
+ new_batch = kvmalloc_objs(*new_batch, new_batch_sz,
+ flags | __GFP_NOWARN);
+ else
+ new_batch = kmalloc_objs(*new_batch, new_batch_sz,
+ flags | __GFP_NOWARN);
if (!new_batch)
return -ENOMEM;

--
Email: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt