* [PATCH net-next] tcp: use order-0 page frags in tcp_collapse()
@ 2026-11-23 15:47 Eric Dumazet
2026-11-24 16:27 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Eric Dumazet @ 2026-11-23 15:47 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni, Kuniyuki Iwashima,
Neal Cardwell
Cc: Simon Horman, netdev, Eric Dumazet
tcp_collapse() copies payload into linear skbs of SKB_MAX_ORDER(0, 0)
bytes (3776 bytes on x86_64), each of them with its own sk_buff and
its own skb_shared_info.
The truesize of such an skb is 4352 bytes: 13% of the memory is
overhead, and a collapsed range can not be denser than that.
Store the payload in order-0 page frags instead. One sk_buff can
hold up to MAX_SKB_FRAGS pages, and the overhead of a collapsed
range is now about one sk_buff and its small head.
What does not fill a page is stored in the linear part of the first
new skb, if this is not bigger than SKB_MAX_ORDER(0, 0) bytes: this
is never more expensive than a partially used page. Otherwise, the
last page frag is partially used.
A range of up to SKB_MAX_ORDER(0, 0) bytes is still collapsed into
a linear skb.
All the pages are allocated before the copy, like skbs were, so
that tcp_collapse() can still give up without side effect if an
allocation fails. The truesize of the new skbs is still known
before they are allocated, to skip the ranges that would not
shrink.
In tcp_rcv_collapse_bloated.pkt, the 30 skbs of 1000 bytes
(truesize 69120) were collapsed into 8 skbs (truesize 34816).
They are now collapsed into a single skb with 7 pages
(truesize 30976).
Other benefits:
- Fewer skbs in the receive queue and in the out-of-order rbtree.
- Collapsed data is stored in page frags which are full pages,
except possibly the last one: they can be used by
TCP_ZEROCOPY_RECEIVE.
TCP_COLLAPSE_SCALING_RATIO is not changed: skbs with a density
above 86% are still not collapsed.
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
net/ipv4/tcp_input.c | 109 ++++++++++++++----
.../packetdrill/tcp_rcv_collapse_bloated.pkt | 2 +-
2 files changed, 90 insertions(+), 21 deletions(-)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 801c76744ed8..975e6501aef8 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5837,7 +5837,7 @@ static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *li
#define TCP_COLLAPSE_SCALING_RATIO \
((TCP_COLLAPSE_MAX_SIZE << TCP_RMEM_TO_WIN_SCALE) / \
(SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE)))
-/* Cap pool preallocation per sub-range to at most 16 skbs in seq
+/* Cap pool preallocation per sub-range to about 15 pages in seq
* space. A single first skb exceeding this is still collapsed,
* whereas an exceeding overlapping ofo range is skipped.
*/
@@ -5886,6 +5886,57 @@ static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
return next;
}
+/* Allocate an skb with @linear bytes in its head, and @size bytes
+ * in order-0 page frags. All frags are full pages, but possibly the last one.
+ */
+static struct sk_buff *tcp_collapse_alloc_skb(int linear, int size)
+{
+ struct sk_buff *nskb = alloc_skb(linear, GFP_ATOMIC);
+
+ if (!nskb)
+ return NULL;
+
+ __skb_put(nskb, linear);
+
+ while (size > 0) {
+ int len = min_t(int, size, PAGE_SIZE);
+ struct page *page;
+
+ /* No __GFP_HIGHMEM: the page is directly mapped. */
+ page = alloc_page(GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN);
+ if (!page) {
+ __kfree_skb(nskb);
+ return NULL;
+ }
+ skb_add_rx_frag(nskb, skb_shinfo(nskb)->nr_frags, page, 0, len,
+ PAGE_SIZE);
+ size -= len;
+ }
+
+ return nskb;
+}
+
+/* Return the address of the byte at @offset in nskb, and the room
+ * left in the linear part or in the page frag containing this byte.
+ */
+static void *tcp_collapse_dst(const struct sk_buff *nskb, int offset, int *room)
+{
+ int hlen = skb_headlen(nskb);
+ const skb_frag_t *frag;
+
+ if (offset < hlen) {
+ *room = hlen - offset;
+ return nskb->data + offset;
+ }
+
+ offset -= hlen;
+ frag = &skb_shinfo(nskb)->frags[offset >> PAGE_SHIFT];
+ offset &= ~PAGE_MASK;
+ *room = skb_frag_size(frag) - offset;
+
+ return skb_frag_address(frag) + offset;
+}
+
/* Collapse bloated sub-ranges in contiguous sequence of skbs
* head..tail starting from sequence number start.
*
@@ -5901,7 +5952,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
unsigned int old_truesize, new_truesize;
struct sk_buff *skb = head, *n;
struct sk_buff_head tmp, pool;
- int remainder, nskbs;
+ int size, linear, npages;
struct sk_buff *nskb;
u8 scaling_ratio;
int released = 0;
@@ -5983,34 +6034,47 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
sub_end - start >= TCP_COLLAPSE_MAX_TOTAL_SIZE)
goto skip;
- /* Third, check if the new skbs have high density. */
- nskbs = (sub_end - start) / TCP_COLLAPSE_MAX_SIZE;
- new_truesize = nskbs * SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE);
-
- remainder = (sub_end - start) % TCP_COLLAPSE_MAX_SIZE;
- if (remainder) {
- nskbs++;
- new_truesize += skb_head_truesize(remainder, GFP_ATOMIC);
- } else {
- remainder = TCP_COLLAPSE_MAX_SIZE;
- }
+ /* Third, check if the new skbs have high density.
+ *
+ * Payload is stored in order-0 page frags: one sk_buff can hold
+ * up to MAX_SKB_FRAGS pages, and truesize stays close to the
+ * payload size.
+ *
+ * What does not fill a page is stored in the linear part of
+ * the first new skb, unless a page is cheaper.
+ */
+ size = sub_end - start;
+ linear = size & ~PAGE_MASK;
+ if (linear > TCP_COLLAPSE_MAX_SIZE)
+ linear = 0;
+
+ npages = DIV_ROUND_UP(size - linear, PAGE_SIZE);
+ new_truesize = skb_head_truesize(linear, GFP_ATOMIC) +
+ npages * PAGE_SIZE;
+ if (npages > MAX_SKB_FRAGS)
+ new_truesize += (DIV_ROUND_UP(npages, MAX_SKB_FRAGS) - 1) *
+ skb_head_truesize(0, GFP_ATOMIC);
if (old_truesize <= new_truesize)
goto skip;
- while (nskbs--) {
- nskb = alloc_skb(nskbs ? TCP_COLLAPSE_MAX_SIZE : remainder,
- GFP_ATOMIC);
+ while (size > 0) {
+ int len = min_t(int, size - linear, MAX_SKB_FRAGS * PAGE_SIZE);
+
+ nskb = tcp_collapse_alloc_skb(linear, len);
if (!nskb)
goto end;
__skb_queue_tail(&pool, nskb);
+ size -= linear + len;
+ linear = 0;
}
while (before(start, sub_end)) {
- int copy = min_t(int, TCP_COLLAPSE_MAX_SIZE, sub_end - start);
+ int copy;
nskb = __skb_dequeue(&pool);
+ copy = nskb->len;
memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
TCP_SKB_CB(nskb)->has_rxtstamp = false;
skb_copy_decrypted(nskb, skb);
@@ -6026,12 +6090,17 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
/* Copy data, releasing collapsed skbs. */
while (copy > 0) {
int offset = start - TCP_SKB_CB(skb)->seq;
- int size = TCP_SKB_CB(skb)->end_seq - start;
+ int room;
+ void *dst;
+
+ size = TCP_SKB_CB(skb)->end_seq - start;
BUG_ON(offset < 0);
if (size > 0) {
- size = min(copy, size);
- if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
+ dst = tcp_collapse_dst(nskb, nskb->len - copy,
+ &room);
+ size = min3(copy, size, room);
+ if (skb_copy_bits(skb, offset, dst, size))
BUG();
TCP_SKB_CB(nskb)->end_seq += size;
if (TCP_SKB_CB(skb)->has_rxtstamp) {
diff --git a/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
index 90e8be86968f..486a33bcddd3 100644
--- a/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
+++ b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
@@ -113,7 +113,7 @@
// Shrink SO_RCVBUF so sk_rcvbuf (320,000) is smaller than current
// sk_rmem_alloc (334,592 on x86_64 default), but larger than sk_rmem_alloc
-// after collapsing the 30 bloated skbs (300,288 on x86_64 default).
+// after collapsing the 30 bloated skbs (296,448 on x86_64 default).
+0 setsockopt(4, SOL_SOCKET, SO_RCVBUF, [160000], 4) = 0
// Inject the next 1,000 B segment with a changed window to bypass
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH net-next] tcp: use order-0 page frags in tcp_collapse()
2026-11-23 15:47 [PATCH net-next] tcp: use order-0 page frags in tcp_collapse() Eric Dumazet
@ 2026-11-24 16:27 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 16:27 UTC (permalink / raw)
To: edumazet; +Cc: davem, kuba, pabeni, kuniyu, ncardwell, horms, netdev
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] tcp: linear head in tcp_collapse() prevents
TCP_ZEROCOPY_RECEIVE
--
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -5983,34 +6034,47 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> sub_end - start >= TCP_COLLAPSE_MAX_TOTAL_SIZE)
> goto skip;
>
> - /* Third, check if the new skbs have high density. */
> - nskbs = (sub_end - start) / TCP_COLLAPSE_MAX_SIZE;
> - new_truesize = nskbs * SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE);
> -
> - remainder = (sub_end - start) % TCP_COLLAPSE_MAX_SIZE;
> - if (remainder) {
> - nskbs++;
> - new_truesize += skb_head_truesize(remainder, GFP_ATOMIC);
> - } else {
> - remainder = TCP_COLLAPSE_MAX_SIZE;
> - }
> + /* Third, check if the new skbs have high density.
> + *
> + * Payload is stored in order-0 page frags: one sk_buff can hold
> + * up to MAX_SKB_FRAGS pages, and truesize stays close to the
> + * payload size.
> + *
> + * What does not fill a page is stored in the linear part of
> + * the first new skb, unless a page is cheaper.
> + */
> + size = sub_end - start;
> + linear = size & ~PAGE_MASK;
> + if (linear > TCP_COLLAPSE_MAX_SIZE)
> + linear = 0;
> +
> + npages = DIV_ROUND_UP(size - linear, PAGE_SIZE);
> + new_truesize = skb_head_truesize(linear, GFP_ATOMIC) +
> + npages * PAGE_SIZE;
> + if (npages > MAX_SKB_FRAGS)
> + new_truesize += (DIV_ROUND_UP(npages, MAX_SKB_FRAGS) - 1) *
> + skb_head_truesize(0, GFP_ATOMIC);
>
> if (old_truesize <= new_truesize)
> goto skip;
>
> - while (nskbs--) {
> - nskb = alloc_skb(nskbs ? TCP_COLLAPSE_MAX_SIZE : remainder,
> - GFP_ATOMIC);
> + while (size > 0) {
> + int len = min_t(int, size - linear, MAX_SKB_FRAGS * PAGE_SIZE);
> +
> + nskb = tcp_collapse_alloc_skb(linear, len);
> if (!nskb)
> goto end;
>
> __skb_queue_tail(&pool, nskb);
> + size -= linear + len;
> + linear = 0;
> }
>
> while (before(start, sub_end)) {
> - int copy = min_t(int, TCP_COLLAPSE_MAX_SIZE, sub_end - start);
> + int copy;
>
> nskb = __skb_dequeue(&pool);
> + copy = nskb->len;
> memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
> TCP_SKB_CB(nskb)->has_rxtstamp = false;
> skb_copy_decrypted(nskb, skb);
[Severity: Medium]
The changelog says the collapsed page frags can be used by
TCP_ZEROCOPY_RECEIVE. Putting the sub-page remainder in the linear
part of the first new skb seems to defeat that for the whole skb.
When 1 <= linear <= TCP_COLLAPSE_MAX_SIZE, tcp_collapse_alloc_skb()
builds an skb with skb_headlen(nskb) == linear, followed by up to
MAX_SKB_FRAGS full pages.
In tcp_zerocopy_receive(), a read offset inside the head makes
skb_advance_to_frag() return NULL. The loop then breaks with nothing
mapped, and zc->recv_skip_hint is left at skb->len - offset. That is
the entire collapsed skb, not just skb_headlen(skb) - offset.
Without a copybuf, userspace is told to copy the whole skb. With a
copybuf, tcp_zc_handle_leftover() copies min(copybuf_len,
recv_skip_hint). That copy runs past the head into frags[0] and
leaves copied_seq at a non-zero offset_frag. Every later
TCP_ZEROCOPY_RECEIVE call on that skb then bails out on the
offset_frag check as well.
The tcp_rcv_collapse_bloated.pkt example from the changelog hits
exactly this case. 30000 bytes gives linear == 1328 ahead of the 7
pages, so none of those pages can be mapped. Any multi-page range
whose remainder fits in TCP_COLLAPSE_MAX_SIZE behaves the same way,
which is most of the non page-aligned ones.
This is not a regression compared to the old all-linear skbs. But
the zerocopy benefit claimed in the changelog mostly does not
materialise.
Would it be better to keep the remainder out of the head of a paged
skb? For example, it could go in a partially used last page frag, or
in its own small linear skb. That trades a little truesize for a
layout zerocopy can use. Or is zerocopy not a goal here, so the
changelog claim should be dropped?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123154708.360226-1-edumazet%40kernel.org
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 16:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 15:47 [PATCH net-next] tcp: use order-0 page frags in tcp_collapse() Eric Dumazet
2026-11-24 16:27 ` 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