mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v3 0/2] net: do not run out of frags in alloc_skb_with_frags()
@ 2026-11-16 11:56 Haishuang Yan
  2026-11-16 11:56 ` [PATCH net v3 1/2] " Haishuang Yan
  2026-11-16 11:56 ` [PATCH net v3 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-16 11:56 UTC (permalink / raw)
  To: netdev
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Mina Almasry, Daniel Borkmann, Willem de Bruijn,
	Willem de Bruijn, Michael S. Tsirkin, Jason Wang,
	Eugenio Pérez, Narcisa Vasile, linux-kernel

alloc_skb_with_frags() can fail with -ENOBUFS for a data_len that passed
its size check. Patch 1 fixes it, patch 2 adds a KUnit test.

The issues were found with the help of an AI coding assistant.

Changes in v3:
- Patch 1: check the frags left only after a failed allocation, with a
  shift (Eric).
- Patch 1: say that the allocation still fails if the retry fails.
- Patch 2: skip only if MAX_SKB_FRAGS - 1 order-3 pages can't be held
  at once; one page was nearly always available (netdev AI review).
- Shorter changelogs (Eric).

v2: https://lore.kernel.org/netdev/20261110162418.28322-1-yanhaishuang@cmss.chinamobile.com/


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 | 51 +++++++++++++++++++++++++++++++++++++++++++++
 net/core/skbuff.c   | 33 ++++++++++++++++++++++++-----
 2 files changed, 79 insertions(+), 5 deletions(-)


base-commit: a45dbf182137275f44ad7c8bc8acd23f7af6b2cf
-- 
2.43.0




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net v3 1/2] net: do not run out of frags in alloc_skb_with_frags()
  2026-11-16 11:56 [PATCH net v3 0/2] net: do not run out of frags in alloc_skb_with_frags() Haishuang Yan
@ 2026-11-16 11:56 ` Haishuang Yan
  2026-11-19 14:56   ` netdev-bot+sashiko
  2026-11-16 11:56 ` [PATCH net v3 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-16 11:56 UTC (permalink / raw)
  To: netdev
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Mina Almasry, Daniel Borkmann, Willem de Bruijn,
	Willem de Bruijn, Michael S. Tsirkin, Jason Wang,
	Eugenio Pérez, Narcisa Vasile, linux-kernel

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@cmss.chinamobile.com>
---
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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net v3 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split
  2026-11-16 11:56 [PATCH net v3 0/2] net: do not run out of frags in alloc_skb_with_frags() Haishuang Yan
  2026-11-16 11:56 ` [PATCH net v3 1/2] " Haishuang Yan
@ 2026-11-16 11:56 ` Haishuang Yan
  2026-11-19 14:56   ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ messages in thread
From: Haishuang Yan @ 2026-11-16 11:56 UTC (permalink / raw)
  To: netdev
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Mina Almasry, Daniel Borkmann, Willem de Bruijn,
	Willem de Bruijn, Michael S. Tsirkin, Jason Wang,
	Eugenio Pérez, Narcisa Vasile, linux-kernel

Allocate MAX_SKB_FRAGS - 2 full order-3 chunks plus a 7 page tail, which
needs one frag too many if the tail is rounded down to 4 + 2 + 1 pages.

If the allocation fails, skip the test unless MAX_SKB_FRAGS - 1 order-3
pages can be held at once, so that fragmented memory is not reported as
a failure.

Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
---
 net/core/net_test.c | 51 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/net/core/net_test.c b/net/core/net_test.c
index 2b5080c226636..118866f3e28c3 100644
--- a/net/core/net_test.c
+++ b/net/core/net_test.c
@@ -1721,7 +1721,58 @@ static void netmem_ref_test_tcp_devmem_recv(struct kunit *test)
 #endif
 #endif
 
+/* Can @nr pages of @order be allocated and held at the same time? */
+static bool alloc_skb_with_frags_test_have_pages(unsigned int nr,
+						 unsigned int order)
+{
+	struct page *pages[MAX_SKB_FRAGS];
+	unsigned int i, n;
+
+	for (n = 0; n < nr; n++) {
+		pages[n] = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
+				       __GFP_NORETRY, order);
+		if (!pages[n])
+			break;
+	}
+	for (i = 0; i < n; i++)
+		__free_pages(pages[i], order);
+
+	return n == nr;
+}
+
+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);
+	/* The full chunks can't fall back to smaller pages here, so the
+	 * call needs up to MAX_SKB_FRAGS - 1 order-3 pages at once. Don't
+	 * report a lack of them as a failure.
+	 */
+	if (!skb && !alloc_skb_with_frags_test_have_pages(MAX_SKB_FRAGS - 1,
+							  PAGE_ALLOC_COSTLY_ORDER))
+		kunit_skip(test, "not enough order-%d pages",
+			   PAGE_ALLOC_COSTLY_ORDER);
+	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

* Re: [PATCH net v3 1/2] net: do not run out of frags in alloc_skb_with_frags()
  2026-11-16 11:56 ` [PATCH net v3 1/2] " Haishuang Yan
@ 2026-11-19 14:56   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-19 14:56 UTC (permalink / raw)
  To: yanhaishuang
  Cc: netdev, davem, edumazet, kuba, pabeni, horms, almasrymina,
	daniel, willemb, willemdebruijn.kernel, mst, jasowangio,
	eperezma, narcisav.kernel, linux-kernel

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message says only tun is affected ("tun hits this with
  large GSO packets from a vhost-net guest").
- [Low] In alloc_skb_with_frags() (net/core/skbuff.c), when a non-
  blocking high-order allocation fails and order-1 pages cannot hold the…
- [Low] The commit body (above the '---' line, so it stays in git
  history) says "Tested with that guest, page allocation fault injection…

--

net: do not run out of frags in alloc_skb_with_frags()

This patch changes how alloc_skb_with_frags() sizes its frags. When
splitting the tail into smaller pages would need more than MAX_SKB_FRAGS
frags, it uses one larger page instead. When a non-blocking high-order
allocation fails, the order is lowered only if the rest still fits.
Otherwise the same order is retried with direct reclaim.

> tun hits this with large GSO packets from a vhost-net guest.

[Severity: Low]
Is tun the only affected caller? tap_alloc_skb() in drivers/net/tap.c and
packet_alloc_skb() in net/packet/af_packet.c use the same clamp as
tun_alloc_skb():

drivers/net/tap.c:tap_alloc_skb() {
    ...
	if (len - linear > MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
		linear = len - MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER);
	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
				   err, PAGE_ALLOC_COSTLY_ORDER);
    ...
}

vhost-net also drives macvtap/ipvtap through tap_get_socket(), and
AF_PACKET vnet_hdr GSO senders reach the same path. All of them could hit
the same -ENOBUFS failure before this patch. The fix in the shared helper
covers all of these callers, so only the message is incomplete.

The visible symptom is only described below the '---' line, and that part
is dropped when the patch is applied. In that symptom, handle_tx_copy()
treats -ENOBUFS as retryable:

drivers/vhost/net.c:handle_tx_copy() {
    ...
			if (err == -EAGAIN || err == -ENOMEM || err == -ENOBUFS) {
				vhost_discard_vq_desc(vq, 1, ndesc);
				vhost_net_enable_vq(net, vq);
				break;
			}
    ...
}

It then retries the same descriptor forever.

The message also doesn't say that the trigger needs data_len above roughly
68K. At 64K the order can drop to 0 and the data still fits in the
available frags, so stock 64K virtio-net guests are not affected.

Could the commit message list tap and AF_PACKET as affected, describe the
vhost retry symptom, and give the size threshold? That would help anyone
deciding on a stable backport.

> 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.

[Severity: Low]
This isn't a bug, but "patch 2" is a position in the posted series and
will mean nothing once the commit is in git history. This fix also has a
Fixes: tag and may be backported on its own.

Could this sentence name the test commit instead ("net: add a KUnit test
for the alloc_skb_with_frags() tail split"), or move below the '---'
line?

> 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,

[ ... ]

>  		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;

[Severity: Low]
Does this fallback bring back the order-3 direct reclaim and compaction
that commit fb05e7a89f50 ("net: don't wait for order-3 page allocation")
removed from this function on purpose? That commit noted that
"alloc_skb_with_frags is the same".

tun_alloc_skb(), tap_alloc_skb() and packet_alloc_skb() set data_len to
exactly MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER). With 4K
pages and the default MAX_SKB_FRAGS of 17, the check at nr_frags == 0 is:

  ((557056 - 1) >> 14) + 1 = 34 > 17

The left side stays larger than the remaining frag count for every later
frag too. Under fragmentation, could a single packet then cause up to
MAX_SKB_FRAGS direct reclaim/compaction attempts? Those would run either
in the vhost worker while holding vq->mutex, or in the AF_PACKET sendmsg
path.

This retry only runs where the old code would already have failed with
-ENOBUFS, so the behaviour itself is not a regression. If the order-3
retry also fails, though, handle_tx_copy() retries the same descriptor
again, and each pass now includes a reclaim attempt.

Could the commit message mention fb05e7a89f50 and explain why the
reclaim cost is acceptable here?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116115620.18151-1-yanhaishuang%40cmss.chinamobile.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net v3 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split
  2026-11-16 11:56 ` [PATCH net v3 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split Haishuang Yan
@ 2026-11-19 14:56   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-19 14:56 UTC (permalink / raw)
  To: yanhaishuang
  Cc: netdev, davem, edumazet, kuba, pabeni, horms, almasrymina,
	daniel, willemb, willemdebruijn.kernel, mst, jasowangio,
	eperezma, narcisav.kernel, linux-kernel

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] The skip-or-fail decision in alloc_skb_with_frags_test_tail()
  (net/core/net_test.c) depends on a probe that only runs after…

--

net: add a KUnit test for the alloc_skb_with_frags() tail split

This adds a KUnit test that asks alloc_skb_with_frags() for
MAX_SKB_FRAGS - 2 full order-3 chunks plus a 7 page tail. It then checks
that the frags cover the full length. If the allocation fails, the test
checks whether MAX_SKB_FRAGS - 1 order-3 pages can be held at once, and
skips if they cannot.

> diff --git a/net/core/net_test.c b/net/core/net_test.c
> index 2b5080c226636..118866f3e28c3 100644
> --- a/net/core/net_test.c
> +++ b/net/core/net_test.c
> @@ -1721,7 +1721,58 @@ static void netmem_ref_test_tcp_devmem_recv(struct kunit *test)
>  #endif
>  #endif
>  
> +/* Can @nr pages of @order be allocated and held at the same time? */
> +static bool alloc_skb_with_frags_test_have_pages(unsigned int nr,
> +						 unsigned int order)
> +{
> +	struct page *pages[MAX_SKB_FRAGS];
> +	unsigned int i, n;
> +
> +	for (n = 0; n < nr; n++) {
> +		pages[n] = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
> +				       __GFP_NORETRY, order);
> +		if (!pages[n])
> +			break;
> +	}

[ ... ]

> +static void alloc_skb_with_frags_test_tail(struct kunit *test)
> +{

[ ... ]

> +	skb = alloc_skb_with_frags(0, len, PAGE_ALLOC_COSTLY_ORDER, &err,
> +				   GFP_KERNEL);
> +	/* The full chunks can't fall back to smaller pages here, so the
> +	 * call needs up to MAX_SKB_FRAGS - 1 order-3 pages at once. Don't
> +	 * report a lack of them as a failure.
> +	 */
> +	if (!skb && !alloc_skb_with_frags_test_have_pages(MAX_SKB_FRAGS - 1,
> +							  PAGE_ALLOC_COSTLY_ORDER))
> +		kunit_skip(test, "not enough order-%d pages",
> +			   PAGE_ALLOC_COSTLY_ORDER);
> +	KUNIT_ASSERT_NOT_NULL_MSG(test, skb, "err %d", err);

[Severity: Medium]

Can this report a FAIL when alloc_skb_with_frags() only ran into a
temporary shortage of high-order pages?

The probe in alloc_skb_with_frags_test_have_pages() runs only after the
real allocation has failed, and by then memory is in a different state.
For this len, the full order-3 chunks can't drop to a lower order. So a
single failed __GFP_NORETRY retry ends the call:

net/core/skbuff.c:alloc_skb_with_frags() {
    ...
				page = alloc_pages(gfp_mask | __GFP_COMP |
						   __GFP_NOWARN | __GFP_NORETRY,
						   order);
				if (!page)
					goto failure;
    ...
failure:
	kfree_skb(skb);
	return NULL;
}

By the time the probe runs, three things have changed:

  - kfree_skb() has given the order-3 pages already taken back to the
    allocator.
  - The failed call has already done one round of direct reclaim and
    compaction.
  - kswapd and kcompactd have been woken for order 3.

The probe's GFP_KERNEL | __GFP_NORETRY order-3 allocations can then
succeed. In that case the test does not skip, and
KUNIT_ASSERT_NOT_NULL_MSG() fails.

The commit message says:

  so that fragmented memory is not reported as a failure.

Does this ordering guarantee that?

Also, the failure message prints err. That value is -ENOBUFS both for a
real frag exhaustion regression and for a page allocation failure, so the
log can't tell the two apart.

Would it be more robust to call alloc_skb_with_frags() again when the
probe succeeds, and fail only if the second call also returns NULL?

> +
> +	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);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116115620.18151-1-yanhaishuang%40cmss.chinamobile.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-19 14:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 11:56 [PATCH net v3 0/2] net: do not run out of frags in alloc_skb_with_frags() Haishuang Yan
2026-11-16 11:56 ` [PATCH net v3 1/2] " Haishuang Yan
2026-11-19 14:56   ` netdev-bot+sashiko
2026-11-16 11:56 ` [PATCH net v3 2/2] net: add a KUnit test for the alloc_skb_with_frags() tail split Haishuang Yan
2026-11-19 14:56   ` netdev-bot+sashiko

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®