[PATCH net v3 1/2] net: do not run out of frags in alloc_skb_with_frags()
From: Haishuang Yan
Date: Sun Nov 15 2026 - 22:57:08 EST
alloc_skb_with_frags() can need more than MAX_SKB_FRAGS frags for a
data_len that passed its size check, and then fails with -ENOBUFS:
- the tail is split into one frag per bit set in its page count;
- after a failed high-order allocation, the order is lowered without
checking that the frags left can hold the rest.
tun hits this with large GSO packets from a vhost-net guest.
Use one larger page for the tail when splitting it would not fit. Only
lower the order if the rest still fits, else retry the same order with
direct reclaim. If that retry fails too, the allocation still fails.
Found while prototyping >64K TSO in a virtio-net guest. Tested with that
guest, page allocation fault injection and the KUnit test in patch 2.
Fixes: 09c2c90705bb ("net: allow alloc_skb_with_frags() to allocate bigger packets")
Signed-off-by: Haishuang Yan <yanhaishuang@xxxxxxxxxxxxxxxxxxxx>
---
Tested on an x86_64 vhost-net host (net-next 303d91a15925, same
function) with a guest sending 512K GSO packets:
- without this patch: -ENOBUFS, the vhost worker spins, traffic stops;
- with it: ~100 Gbit/s, no drops, none of 354872 packets failed;
- fault injection on order-3 allocations (5-100%): no failures;
- not tested: the direct reclaim retry failing too (real memory
pressure); it only runs where the allocation fails today;
- KUnit net_core 20/20; arm64 W=1 build of net/core/ clean.
net/core/skbuff.c | 33 ++++++++++++++++++++++++++++-----
1 file changed, 28 insertions(+), 5 deletions(-)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index a7d555b3d2f28..43581ca020243 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -6906,17 +6906,40 @@ 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 = DIV_ROUND_UP(data_len, PAGE_SIZE);
+
+ /* 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) |
__GFP_COMP |
__GFP_NOWARN,
order);
- if (!page) {
- order--;
- continue;
+ if (unlikely(!page)) {
+ unsigned int shift = PAGE_SHIFT + order - 1;
+
+ /* Can smaller pages hold the rest? */
+ if (((data_len - 1) >> shift) + 1 <=
+ MAX_SKB_FRAGS - nr_frags) {
+ order--;
+ continue;
+ }
+ /* No, retry this order with direct reclaim. */
+ if (!gfpflags_allow_blocking(gfp_mask))
+ goto failure;
+ page = alloc_pages(gfp_mask | __GFP_COMP |
+ __GFP_NOWARN | __GFP_NORETRY,
+ order);
+ if (!page)
+ goto failure;
}
} else {
page = alloc_page(gfp_mask);
--
2.43.0