* Re: [PATCH net 1/2] net: do not run out of frags in alloc_skb_with_frags()
2026-11-08 10:06 ` [PATCH net 1/2] " Haishuang Yan
@ 2026-11-08 8:20 ` Narcisa Vasile
0 siblings, 0 replies; 5+ messages in thread
From: Narcisa Vasile @ 2026-11-08 8:20 UTC (permalink / raw)
To: Haishuang Yan
Cc: netdev, davem, edumazet, kuba, pabeni, horms, willemb, mst,
jasowangio, eperezma, willemdebruijn.kernel, linux-kernel
On Sun, Nov 08, 2026 at 07:06:16PM +0900, Haishuang Yan wrote:
> alloc_skb_with_frags() rejects data_len above
> MAX_SKB_FRAGS * (PAGE_SIZE << order) up front, but then rounds the order
> down for the tail, so a tail of N pages is split into one frag per bit
> set in N. A data_len that passed the check can then need more than
> MAX_SKB_FRAGS frags, and the allocation fails with -ENOBUFS although
> every page allocation succeeded.
>
> With order 3 and 4K pages, a 516536 byte packet is 15 * 32K plus a 25016
> byte tail, which becomes 16K + 8K + 4K, so 18 frags are needed. Sizes
> between about 480K and 540K can be affected.
>
> tun hits this when vhost-net hands it a GSO packet of such a size from a
> guest. Nothing on the host limits the size of a guest's GSO packet to
> 64K: virtio_net_hdr has no total length, and tun_get_user() sizes the
> skb from the descriptor chain. Today's virtio-net drivers stay at 64K,
> but a guest can send bigger packets by raising its own TSO limit, and
> the host must not depend on the guest for that. Worse, vhost-net treats
> -ENOBUFS from sendmsg() as transient: handle_tx_copy() puts the
> descriptor back and retries it, so the vhost worker spins at 100% on
> the same packet and the device stops passing traffic in both
> directions. On an unmodified host, tun_get_user() returned -ENOBUFS
> about 620,000 times per second, the tap's rx_dropped counter climbed at
> the same rate, and this went on with no traffic until the VM was
> destroyed.
>
> Keep rounding the tail down when the remaining frags allow it, so no
> allocation that works today changes, and use one page rounded up for
> the whole tail otherwise. This is never larger than the full-size
> chunks already used.
>
> This was triggered while prototyping >64K TSO for virtio-net, with a
> one-line guest virtio_net change raising its TSO limit; the host was
> unmodified. The fix was tested with that reproducer and with the KUnit
> test in the next patch.
>
> Fixes: 09c2c90705bb ("net: allow alloc_skb_with_frags() to allocate bigger packets")
> Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
> ---
> Base: net 30c71476d842 ("ipv6: Check the Home Address option again before its
> address is used"). The reproducer below was run on net-next 303d91a15925,
> where alloc_skb_with_frags() is identical. On net: KUnit net_core 20/20
> (kunit.py, UML x86_64), and allmodconfig W=1 build of net/core/ (arm64)
> with no warnings.
>
> Reproducer: x86_64 KVM host (i7-14700) running net-next 303d91a15925 with no
> local changes; vhost-net, a tap NIC with mrg_rxbuf=on and rx_queue_size=1024.
> The guest (net-next too) has a local one-line virtio_net change setting
> netif_set_tso_max_size(dev, GSO_MAX_SIZE), plus
> `ip link set dev ethX gso_max_size 524280 gso_ipv4_max_size 524280`; then
> iperf3 guest -> host.
>
> Without this patch: iperf3 fails, one vhost worker stays at 99.9% CPU,
> tun_get_user() returns -105 (31014 times in 50 ms, kretprobe), the tap's
> rx_dropped goes up by 34M during the run and keeps climbing at ~690k/s with
> no traffic, and the guest can no longer ping the host until the VM is
> destroyed. With this patch (same setup plus this patch on the host): 81
> Gbit/s, no drops, and the vhost worker is idle afterwards.
>
> net/core/skbuff.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)
>
I think we should decouple the two parts you've pointed out:
1. For valid packets that pass the initial data_len size check,
this patch avoids running out of frags by putting the tail into one
larger block of pages instead of splitting it into several smaller blocks.
2. The vhost cpu spin: this patch fixes it for the sizes in (1), but a
modified guest can still trigger it by sending a larger frame (above
~4.75 MB on x86).
In that case, tun_alloc_skb() asks for a linear part above the
kmalloc limit. The alloc_skb() allocation in this function can never
succeed and returns -ENOBUFS, and vhost-net retries the same frame
indefinitely.
Should we also return -EMSGSIZE when header_len
is above the kmalloc limit, like it already does for an oversized
data_len? I'm not sure whether the errno change is acceptable?.
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index 8da53d5b76fc..93e7b04ddfe6 100644
> --- a/net/core/skbuff.c
> +++ b/net/core/skbuff.c
> @@ -6902,8 +6902,18 @@ struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
> while (data_len) {
> if (nr_frags == MAX_SKB_FRAGS)
> goto failure;
> - while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order))
> - order--;
> + if (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order)) {
> + unsigned long pages = PAGE_ALIGN(data_len) >> PAGE_SHIFT;
> +
> + /* Rounding the tail down to smaller pages takes one
> + * frag per bit set in its page count. If not enough
> + * frags are left for that, use one bigger page.
> + */
nit, feel free to ignore, this might read more simply as:
/* If splitting the rest into smaller pages would need
* more frags than we have left, use one larger page.
*/
> + if (hweight_long(pages) > MAX_SKB_FRAGS - nr_frags)
> + order = order_base_2(pages);
> + else
> + order = ilog2(pages);
> + }
>
> if (order) {
> page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split
2026-11-08 10:06 ` [PATCH net 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split Haishuang Yan
@ 2026-11-08 8:24 ` Narcisa Vasile
0 siblings, 0 replies; 5+ messages in thread
From: Narcisa Vasile @ 2026-11-08 8:24 UTC (permalink / raw)
To: Haishuang Yan
Cc: netdev, davem, edumazet, kuba, pabeni, horms, willemb, mst,
jasowangio, eperezma, willemdebruijn.kernel, linux-kernel
On Sun, Nov 08, 2026 at 07:06:17PM +0900, Haishuang Yan wrote:
> Allocate MAX_SKB_FRAGS - 2 full chunks of PAGE_ALLOC_COSTLY_ORDER plus
> a 7 page tail. Rounding the tail down to 4 + 2 + 1 pages would need
> one frag more than MAX_SKB_FRAGS, which made the allocation fail with
> -ENOBUFS before the previous patch.
>
> Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
> ---
> net/core/net_test.c | 24 ++++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/net/core/net_test.c b/net/core/net_test.c
Thanks, I tried it locally.
Reviewed-by: Narcisa Vasile <narcisav.kernel@gmail.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 0/2] net: do not run out of frags in alloc_skb_with_frags()
@ 2026-11-08 10:06 Haishuang Yan
2026-11-08 10:06 ` [PATCH net 1/2] " Haishuang Yan
2026-11-08 10:06 ` [PATCH net 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split Haishuang Yan
0 siblings, 2 replies; 5+ messages in thread
From: Haishuang Yan @ 2026-11-08 10:06 UTC (permalink / raw)
To: netdev
Cc: davem, edumazet, kuba, pabeni, horms, willemb, mst, jasowangio,
eperezma, willemdebruijn.kernel, linux-kernel, Haishuang Yan
alloc_skb_with_frags() can fail with -ENOBUFS for a data_len that passed
its own size check, because it splits the tail into one frag per bit set
in its page count. With vhost-net + tun, a guest that sends a ~480-540K
GSO packet makes the vhost worker retry that packet forever (100% CPU,
device dead). Patch 1 fixes the split without changing any allocation
that works today; patch 2 adds a KUnit test that fails before patch 1.
Haishuang Yan (2):
net: do not run out of frags in alloc_skb_with_frags()
net: add a KUnit test for the alloc_skb_with_frags() tail split
net/core/net_test.c | 24 ++++++++++++++++++++++++
net/core/skbuff.c | 14 ++++++++++++--
2 files changed, 36 insertions(+), 2 deletions(-)
base-commit: 30c71476d842b094651bd7712f6c8ee8716147d0
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 1/2] net: do not run out of frags in alloc_skb_with_frags()
2026-11-08 10:06 [PATCH net 0/2] net: do not run out of frags in alloc_skb_with_frags() Haishuang Yan
@ 2026-11-08 10:06 ` Haishuang Yan
2026-11-08 8:20 ` Narcisa Vasile
2026-11-08 10:06 ` [PATCH net 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split Haishuang Yan
1 sibling, 1 reply; 5+ messages in thread
From: Haishuang Yan @ 2026-11-08 10:06 UTC (permalink / raw)
To: netdev
Cc: davem, edumazet, kuba, pabeni, horms, willemb, mst, jasowangio,
eperezma, willemdebruijn.kernel, linux-kernel, Haishuang Yan
alloc_skb_with_frags() rejects data_len above
MAX_SKB_FRAGS * (PAGE_SIZE << order) up front, but then rounds the order
down for the tail, so a tail of N pages is split into one frag per bit
set in N. A data_len that passed the check can then need more than
MAX_SKB_FRAGS frags, and the allocation fails with -ENOBUFS although
every page allocation succeeded.
With order 3 and 4K pages, a 516536 byte packet is 15 * 32K plus a 25016
byte tail, which becomes 16K + 8K + 4K, so 18 frags are needed. Sizes
between about 480K and 540K can be affected.
tun hits this when vhost-net hands it a GSO packet of such a size from a
guest. Nothing on the host limits the size of a guest's GSO packet to
64K: virtio_net_hdr has no total length, and tun_get_user() sizes the
skb from the descriptor chain. Today's virtio-net drivers stay at 64K,
but a guest can send bigger packets by raising its own TSO limit, and
the host must not depend on the guest for that. Worse, vhost-net treats
-ENOBUFS from sendmsg() as transient: handle_tx_copy() puts the
descriptor back and retries it, so the vhost worker spins at 100% on
the same packet and the device stops passing traffic in both
directions. On an unmodified host, tun_get_user() returned -ENOBUFS
about 620,000 times per second, the tap's rx_dropped counter climbed at
the same rate, and this went on with no traffic until the VM was
destroyed.
Keep rounding the tail down when the remaining frags allow it, so no
allocation that works today changes, and use one page rounded up for
the whole tail otherwise. This is never larger than the full-size
chunks already used.
This was triggered while prototyping >64K TSO for virtio-net, with a
one-line guest virtio_net change raising its TSO limit; the host was
unmodified. The fix was tested with that reproducer and with the KUnit
test in the next patch.
Fixes: 09c2c90705bb ("net: allow alloc_skb_with_frags() to allocate bigger packets")
Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
---
Base: net 30c71476d842 ("ipv6: Check the Home Address option again before its
address is used"). The reproducer below was run on net-next 303d91a15925,
where alloc_skb_with_frags() is identical. On net: KUnit net_core 20/20
(kunit.py, UML x86_64), and allmodconfig W=1 build of net/core/ (arm64)
with no warnings.
Reproducer: x86_64 KVM host (i7-14700) running net-next 303d91a15925 with no
local changes; vhost-net, a tap NIC with mrg_rxbuf=on and rx_queue_size=1024.
The guest (net-next too) has a local one-line virtio_net change setting
netif_set_tso_max_size(dev, GSO_MAX_SIZE), plus
`ip link set dev ethX gso_max_size 524280 gso_ipv4_max_size 524280`; then
iperf3 guest -> host.
Without this patch: iperf3 fails, one vhost worker stays at 99.9% CPU,
tun_get_user() returns -105 (31014 times in 50 ms, kretprobe), the tap's
rx_dropped goes up by 34M during the run and keeps climbing at ~690k/s with
no traffic, and the guest can no longer ping the host until the VM is
destroyed. With this patch (same setup plus this patch on the host): 81
Gbit/s, no drops, and the vhost worker is idle afterwards.
net/core/skbuff.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 8da53d5b76fc..93e7b04ddfe6 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -6902,8 +6902,18 @@ struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
while (data_len) {
if (nr_frags == MAX_SKB_FRAGS)
goto failure;
- while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order))
- order--;
+ if (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order)) {
+ unsigned long pages = PAGE_ALIGN(data_len) >> PAGE_SHIFT;
+
+ /* Rounding the tail down to smaller pages takes one
+ * frag per bit set in its page count. If not enough
+ * frags are left for that, use one bigger page.
+ */
+ if (hweight_long(pages) > MAX_SKB_FRAGS - nr_frags)
+ order = order_base_2(pages);
+ else
+ order = ilog2(pages);
+ }
if (order) {
page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split
2026-11-08 10:06 [PATCH net 0/2] net: do not run out of frags in alloc_skb_with_frags() Haishuang Yan
2026-11-08 10:06 ` [PATCH net 1/2] " Haishuang Yan
@ 2026-11-08 10:06 ` Haishuang Yan
2026-11-08 8:24 ` Narcisa Vasile
1 sibling, 1 reply; 5+ messages in thread
From: Haishuang Yan @ 2026-11-08 10:06 UTC (permalink / raw)
To: netdev
Cc: davem, edumazet, kuba, pabeni, horms, willemb, mst, jasowangio,
eperezma, willemdebruijn.kernel, linux-kernel, Haishuang Yan
Allocate MAX_SKB_FRAGS - 2 full chunks of PAGE_ALLOC_COSTLY_ORDER plus
a 7 page tail. Rounding the tail down to 4 + 2 + 1 pages would need
one frag more than MAX_SKB_FRAGS, which made the allocation fail with
-ENOBUFS before the previous patch.
Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
---
net/core/net_test.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/net/core/net_test.c b/net/core/net_test.c
index 2b5080c22663..5a451aaf37c9 100644
--- a/net/core/net_test.c
+++ b/net/core/net_test.c
@@ -1721,7 +1721,31 @@ static void netmem_ref_test_tcp_devmem_recv(struct kunit *test)
#endif
#endif
+static void alloc_skb_with_frags_test_tail(struct kunit *test)
+{
+ /* MAX_SKB_FRAGS - 2 full chunks plus a 7 page tail: rounding the
+ * tail down to 4 + 2 + 1 pages would need one frag too many.
+ */
+ unsigned long len = (MAX_SKB_FRAGS - 2) *
+ (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) +
+ 7 * PAGE_SIZE;
+ unsigned long frags_len = 0;
+ struct sk_buff *skb;
+ int err, i;
+
+ skb = alloc_skb_with_frags(0, len, PAGE_ALLOC_COSTLY_ORDER, &err,
+ GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL_MSG(test, skb, "err %d", err);
+
+ for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+ frags_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
+ KUNIT_EXPECT_EQ(test, frags_len, len);
+
+ kfree_skb(skb);
+}
+
static struct kunit_case net_test_cases[] = {
+ KUNIT_CASE(alloc_skb_with_frags_test_tail),
KUNIT_CASE_PARAM(gso_test_func, gso_test_gen_params),
KUNIT_CASE_PARAM(ip_tunnel_flags_test_run,
ip_tunnel_flags_test_gen_params),
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-08 8:24 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-08 10:06 [PATCH net 0/2] net: do not run out of frags in alloc_skb_with_frags() Haishuang Yan
2026-11-08 10:06 ` [PATCH net 1/2] " Haishuang Yan
2026-11-08 8:20 ` Narcisa Vasile
2026-11-08 10:06 ` [PATCH net 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split Haishuang Yan
2026-11-08 8:24 ` Narcisa Vasile
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®