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

From: netdev-bot+sashiko

Date: Fri Dec 04 2026 - 06:54:48 EST


Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] Incomplete fix: the TCP BPF iterator keeps the same
kvmalloc(GFP_NOWAIT)-under-BH-spinlock deadlock that this patch…

--

bpf: udp: Do not call kvmalloc while holding BH spin lock

With this patch, bpf_iter_udp_realloc_batch() uses kmalloc instead of
kvmalloc when it is called with GFP_NOWAIT. That call happens while the
UDP bucket lock is held with BH disabled. The vmalloc fallback in kvmalloc
takes plain spinlocks, so it can deadlock against softirq users of the
same bucket lock.

> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index d784faf939c3b..613ea92e95b18 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);

[Severity: High]
Does the TCP BPF iterator need the same change? The TCP twin of this
helper in net/ipv4/tcp_ipv4.c still calls kvmalloc unconditionally:

net/ipv4/tcp_ipv4.c:bpf_iter_tcp_realloc_batch() {
...
new_batch = kvmalloc_objs(*new_batch, new_batch_sz,
flags | __GFP_NOWARN);
...
}

bpf_iter_tcp_batch() calls it with GFP_NOWAIT while it still holds the
bucket lock:

net/ipv4/tcp_ipv4.c:bpf_iter_tcp_batch() {
...
/* Batch size was still too small. Hold onto the lock while we try
* again with a larger batch to make sure the current bucket's size
* does not change in the meantime.
*/
err = bpf_iter_tcp_realloc_batch(iter, expected, GFP_NOWAIT);
...
}

For established and request sockets, that lock is the ehash bucket lock.
It is taken with BH disabled, as bpf_iter_tcp_unlock_bucket() shows:

spin_unlock_bh(inet_ehash_lockp(hinfo, st->bucket));

The softirq path takes the same ehash lock with a plain spin_lock(). One
example is inet_ehash_insert(), which runs from tcp_v4_syn_recv_sock() and
from request socket hashing.

Suppose the batch is larger than PAGE_SIZE (more than 512 items) and the
GFP_NOWAIT kmalloc fails. Then __kvmalloc_node_noprof() falls back to
__vmalloc_node_range_noprof() with the same flags. vmalloc takes plain
spinlocks such as free_vmap_area_lock without disabling BH.

Could this deadlock the same way as the UDP case described in the commit
message?

CPU0
bpf_iter_tcp_batch()
spin_lock_bh(ehash bucket lock)
bpf_iter_tcp_realloc_batch(iter, expected, GFP_NOWAIT)
kvmalloc -> __vmalloc_node_range_noprof()
spin_lock(&free_vmap_area_lock) <- spins

CPU1
holds free_vmap_area_lock with BH enabled
<irq, softirq runs on irq_exit>
tcp_v4_syn_recv_sock()
inet_ehash_insert()
spin_lock(same ehash bucket lock) <- spins

As far as I can tell, TCP is the only other caller under net/ that passes
GFP_NOWAIT to a realloc_batch helper. The Fixes tag points at
c6307674ed82, which made both iterators reach this path.

Would it make sense to fix bpf_iter_tcp_realloc_batch() in the same patch?
If not, could the commit message mention TCP, so that backports don't
treat the regression as fully closed?

> if (!new_batch)
> return -ENOMEM;

[ ... ]

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/aw6KwbtmeFFSf_MS%40gondor.apana.org.au