🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #21136

[PATCH net-next] tcp: use order-0 page frags in tcp_collapse()

#21136
From: Eric Dumazet
Date: Mon, 23 Nov 2026 15:47
227 lines
15354 bytes
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
Technical Details
Message-ID: <20261123154708.360226-1-edumazet@kernel.org>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-369549-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-369549-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next] tcp: use order-0 page frags in tcp_collapse()
Date: Mon, 23 Nov 2026 16:47:08 +0100
Organization: Usenet Network
Message-ID: <20261123154708.360226-1-edumazet@kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3918207"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:Q77OTamFsVHGBNFUI9nfXN5VXrM= sha256:tFY85g4ip/VipkG+DWzjgu7PcCv4/ssbWwk9RMeVnbM=
	sha1:xqPiS3Wa+OzGBj9dsNuj35mUIxY= sha256:g6q/J6rkWj1A/Xn3TPqFjTELcupagLITrUFEZy6nF+g=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 85A06125DBD; Mon, 23 Nov 2026 18:47:35 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id CVG8o-oUfZwL;
	Mon, 23 Nov 2026 18:47:28 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-369549-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id B27C7125DBC; Mon, 23 Nov 2026 18:47:19 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id B538329536; Mon, 23 Nov 2026 15:47:17 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795448837;
	b=YRHNQPqNt8GmcA5qqTPXaEJPvqT1QKgvJYz/cQPNMKbCv691EYavwtWw+zeXvL4WY3+g
	 iYpAQxsVkSLYYBsCxLhkE3hOcpoSZQG2TDYEUdlVKw/jiWwAQ1IOhiJg00LwAkBHiIW1n
	 /B4Q/AMp9vrohwo4qGkFJkR3FAkJTWcu/mfguUvWmfG0a2EWkAurMmJhFSHAKr1mD4+3Q
	 zrw39qqsF9gTIUjl8KZWfUlI/E9J03if2bqtYLoSEYqdEtuHVoNGk7jdX7NN6OjFdJxXz
	 9+guSgjF50E6UDczWYVL24IIg6zNjWe/eA4CH+1KymAhn7wodxj0NuL+CRjQk+6DOkA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795448837;
	h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:List-Help:
	 List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=2RRGa+6xAqYwGcrdChLln3M+CJbvF/ihltQfYAefwKY=;
	b=jaX7rKAUrFzYR/Lc9iYjfhx2PRiZaReouVE8Df6Uzi99pTCO+u8vxDz9CzlVvCmouFmi
	 1WuWdXH2oaZJgBio88Jjgzvd49ymPwWSqNNR6ux+yCZBc0VC6Z5N2n0xLH4X1wQFRKcBS
	 V9wx/pH84uS4O6bIyZobHtYdoEMCws8BdtCafIPH4YCPiz6Ecok2S3sNMsf1dwYzkPGYK
	 VSX0HvQ/9C5zfPNlgC09RiNKlSfgWaMRcAeQsxmvomPG5yFcMG8vFQCvm/lZPH7lF4Irx
	 CJsLY0ymaPfxtwgqsXcIptgCS3F7BW7MRLlSH78jrnRoALS68tn7G/tXrikrq+44NUw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="Kai04/5i";
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 8D021606A31A;
	Mon, 23 Nov 2026 15:47:17 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Kai04/5i
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795448836;
	b=v/AsnIGeEfYmmGoRsc2hF+4wh6NTN5EGxkD5P2NJxQzjVkYlp8ICBUK5vE0OZW7P/PTh
	 kGgcfqV48yEhflP1L1QwFuOF1TwqJqjHYnR5k+O7EfCxemTwQ+luWCOHPrOBdsTovRgC0
	 vnqhJGNqi3ykvh0SxyIyCLzOF/RiJRt7ALVFloIzgfr6g8anM8G7nhM3+fd7J/S5CTis3
	 /LFO6X9R4dOd4sVKkIvWVvTnAukIkNOPDvKUZ083lSTFbzCQc/agWtDq18+OjullutKQT
	 NgomxSPgXYBXXh+1M0ajoYny312eoMxb9S0ah5O6/nDOOugetPEhth7+7szJ7me0eSA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795448836;
	h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 MIME-Version:Content-Transfer-Encoding;
	bh=2RRGa+6xAqYwGcrdChLln3M+CJbvF/ihltQfYAefwKY=;
	b=Ersamjq6HMNaC4l9//g+0ft8KeoX/tcDkGp4OeFSMVi6WyTKC3LD8jkOhcCuvjYWGWcD
	 DrLRo9aK4+htHz+veG9xh5Y/gbJC8cEKuFZnmDG87JNBwgKvBKgl3DVeXOpUQETQMn4qB
	 waJUe7wlFzQCoo5woMpEq7/zGg200Cb+nn65NskHA73olg/+1v6ncTIeMyfPPLaouvUPx
	 33zms11A0ZXjKCFLKVxeA/2ADwVBDjg8FtJ0o8/UDRsNKqo2FJHbvhz9PTdnyVD4Pr5bq
	 7Hir/3DGe85UjzMIkAd23TBg/XUblQkz46W5IKlBK8cX+D+H2NtxuLeuRzFdURWkuJA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="Kai04/5i";
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id A93D9606A330
	for <netdev@vger.kernel.org>; Mon, 23 Nov 2026 15:47:16 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E6721F000E6;
	Mon, 23 Nov 2026 15:47:14 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795448836;
	bh=2RRGa+6xAqYwGcrdChLln3M+CJbvF/ihltQfYAefwKY=;
	h=From:To:Cc:Subject:Date;
	b=Kai04/5idHNmf//X4Fd73Bp2azWGMqBNhmiH1CpLbg+gNt/j2d+fvbzjo9H4UKTb6
	 6JCoYeQk+RKWHN34gJF5d533By0d6Nh4PDxDY9fTxH0ps+OCsoq96DD09cb3O7GHrB
	 FyfDDCfl3WUWfMrBPQ0L/GyoMzUdxr440VyYcEA8LjaOFZlgX3jENDiXsv1O5JwVRm
	 0BNfl/v+u8rAutETY+JhQoB1rJ5m8M6LeKzY5hi/mK+LdKfom9ThEiJw5bb1Uwhpcw
	 i+9y1cNim2nsiMlYVXKYbEqJx8yCnW8yjWs/ZumSI6qMNNvsADm8/WeCU0UsudOwhz
	 F1ljdzcI6tcCA==
X-Cc: Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org,
	Eric Dumazet <edumazet@kernel.org>
X-Mailer: git-send-email 2.53.0
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list