🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #19092

[PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter()

#19092
From: Eric Dumazet
Date: Thu, 19 Nov 2026 10:15
86 lines
10816 bytes
zerocopy_fill_skb_from_iter() adds PAGE_ALIGN(copied + start) to
skb->truesize for each iov_iter_get_pages2() call.

Since commit 394fcd8a8134 ("net: zerocopy: combine pages in
zerocopy_sg_from_iter()"), a chunk that directly follows the last frag
in the same page is merged into this frag, and the extra page reference
is released. The page is still accounted again in skb->truesize.

With iovecs describing small contiguous chunks of the same page, each
iovec charges a full page while a single page is pinned. For TCP,
1024 one-byte iovecs sent with MSG_ZEROCOPY add 4 MB to sk_wmem_queued
and charge 4 MB to the socket, for 1024 bytes of payload sitting in
one page. Appending to the skb at the tail of the write queue does not
check sk_sndbuf, so a single sendmsg() can get sk_wmem_queued above it.

Account pages while building the frags instead:

- A new frag accounts one page, as before.
- A chunk merged into the last frag only accounts the pages this frag
  did not already cover.

The accounting is unchanged when no chunk is merged in the middle
of a page.

Fixes: 394fcd8a8134 ("net: zerocopy: combine pages in zerocopy_sg_from_iter()")
Link: https://lore.kernel.org/netdev/20261119090828.343748-1-jiayuan.chen@linux.dev/
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
This was found with an AI coding assistant, while reviewing
the patch in the Link above, which mentions 8 one-byte iovecs being
charged 32768 bytes.

It was validated with a userspace reproducer, on an unmodified kernel,
without special privileges:

- TCP connection over loopback, SO_ZEROCOPY and TCP_CORK set
- send() of one byte, so that the tail skb is not a pure zerocopy one
- sendmsg(MSG_ZEROCOPY) of 1024 bytes of a single page, either
  in one iovec or in 1024 one-byte iovecs
- compare SK_MEMINFO_WMEM_QUEUED (SO_MEMINFO) before and after

  1 iovec        sk_wmem_queued += 4096
  1024 iovecs    sk_wmem_queued += 4194304

With this patch, both cases add 4096 bytes. Kernel selftests were
not run.

 net/core/datagram.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/core/datagram.c b/net/core/datagram.c
index 489d92fe6dae8952da08bc5cb2a58f9863938fe0..8c418c3adf1c97b41ee7999146d09aacd8cf29d4 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -659,7 +659,6 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb,
 
 		skb->data_len += copied;
 		skb->len += copied;
-		skb->truesize += PAGE_ALIGN(copied + start);
 
 		head = compound_head(pages[n]);
 		order = compound_order(head);
@@ -680,6 +679,11 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb,
 
 				if (head == skb_frag_page(last) &&
 				    start == skb_frag_off(last) + skb_frag_size(last)) {
+					/* Only account the pages this frag
+					 * did not already cover.
+					 */
+					skb->truesize += PAGE_ALIGN(start + size) -
+							 PAGE_ALIGN(start);
 					skb_frag_size_add(last, size);
 					/* We combined this page, we need to release
 					 * a reference. Since compound pages refcount
@@ -695,6 +699,7 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb,
 				page_ref_sub(last_head, refs);
 				refs = 0;
 			}
+			skb->truesize += PAGE_SIZE;
 			skb_fill_page_desc_noacc(skb, frag++, head, start, size);
 		}
 		if (refs)
-- 
2.53.0
Technical Details
Message-ID: <20261119101538.3229466-1-edumazet@kernel.org>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366934-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366934-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter()
Date: Thu, 19 Nov 2026 11:15:38 +0100
Organization: Usenet Network
Message-ID: <20261119101538.3229466-1-edumazet@kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2846111"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:c7R/MZfU5fk+uMQveQz8ZK9Vsaw= sha256:PZqoGW+beUO0Zb9Z69bdCDAcrgdjrjyX6/BR71ih6j0=
	sha1:6V7V87AJ7hDeliEyNXQJzB9Ng5Q= sha256:FTYZzFBD4uywls7Hr86o2TPYBfj3o6WRHCkO8ve7GK4=
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 E77D5115552; Thu, 19 Nov 2026 13:16:06 +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 LupG6_dtL9t1;
	Thu, 19 Nov 2026 13:16:01 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-366934-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 70138115579; Thu, 19 Nov 2026 13:15:51 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 26A82102D85; Thu, 19 Nov 2026 10:15:46 +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=1795083345;
	b=Tya6DgGIjDjN1gPNRy3ZGobcfghcV009J0BWrrQWOJ0gvZjSP0Bujbi1ftITrFEYrG7L
	 m9SeKntdlMyN2ZF1n9v0vGyiftFH5mkt5pkZgbwJ2dNHZ4bh1xCBMftLTBHrE9RzwOaLu
	 QMXS50s3K/y28aKSWry2gP2aYT6cgYxLx6invk4YG0LljAHTyywviI35yLrnNxOVYwbeQ
	 s5rmOR0p8xWnh+mkAeEk12GWvLZcGYuSSYmQpnmJzy8WoRpI2e5DBIBRU12K4V85ksra4
	 oOmiKiQLguflnFXWGS2Tppro5lvEU6l5CFUsLwpzmufbAiq6h9H+yTJu9nyzlXaikbw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795083345;
	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=VMbM0n0fGOb5WRoEQogOfM6SY5XoKlXZke9k2tIlH4c=;
	b=ijSsGJHQmIzTnctnh8bBrBmaq+DLoWH24W9I2F1xK7+euELr3AQYcKGZY5gH0Xq6dKwU
	 jPqLEqOKf7BaN2ykk/jk+aB73ajbZsRtEmXPDcJaIWe93tjaaBN0e2qogPC5ZHGRctSIE
	 Iw5WvJWCsfPeFJNzPt3PWsyx784Kh+u6TYqwnHnkb4RyC2gvFrI+FkuN4OZH34Qa2ay7/
	 qS8gZMzEjmpNfRqgkMfTlKayjFNC/viPV5Psd9PQ2qgblpFZ00GrzP4kjXg3nP0Mw/CYa
	 ZDEji/p12UEBNevYARFtX6ENlsIoMxEOsMbkj9B2pVVToRabrAZ4LWVlnwzrT2yNPRg==
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=OvmMBpyC;
	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 68197606A32F;
	Thu, 19 Nov 2026 10:15:45 +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=OvmMBpyC
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=1795083344;
	b=11nXj5vJyVG52kw+2qz9TlBxqDNJzCyhlr2tFt2yNYM4VD6dHUyHfLoeTZIg1OV8n1NJ
	 CSt2wFpih4Dd+iiZO5LuIVDRNEAMBGxmbe4HThUzYsPr/TusnpBF7ks09kpd/MHF8NBJr
	 shNZUeWI2aoE3XnSBppqH8qhCuvIFRDOd0q11ev69TKTIBeFrLthClnjy6I7CDWQoXzdJ
	 d/I8hV35me9/Ptt2x6trko00pUeTKJkROvk5gOmhP8XgO9YkkppUFaU8IVQYgoSqwai5V
	 HK4EYcIBO2x0vOjl/lTxqNd2pEBYhvXyrVQ/JYNb8EPl0Nvm049zM7GaiZFfxMGCvbw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795083344;
	h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 MIME-Version:Content-Transfer-Encoding;
	bh=VMbM0n0fGOb5WRoEQogOfM6SY5XoKlXZke9k2tIlH4c=;
	b=2lAlzA17eR4XVoI+v9aswraWYO6w55aXp4705gD6uCbC3PgFxhpFOadGix2uVhmeWBNh
	 KoIpICNuRx1A9noBFf65fxnnrdeJvWl4mib/1z+s0mV/DzCenQe9b4WIz5F4oAeNE+WA8
	 /arkwVmioOW2ZxtAcDZ6BtvPyswTzlWrqrP0cnLsLRle7ljXJjWWjhwKBBXwNyx8BUA/b
	 n6uPE8Y8G8QWPSk56jBTqL5JNNlnYrmCOElPhvwGammzj2Pr12as5fSKDsg1Jho5PWywt
	 YPhaoOuX39GkzQ4Rspwtfp6LsfSeHZkSl9N3DcqdeTyiIv0diKS226sNxY6IrbezwLA==
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=OvmMBpyC;
	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 86957606A324
	for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 10:15:44 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63C391F000C6;
	Thu, 19 Nov 2026 10:15:42 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795083344;
	bh=VMbM0n0fGOb5WRoEQogOfM6SY5XoKlXZke9k2tIlH4c=;
	h=From:To:Cc:Subject:Date;
	b=OvmMBpyCVARurStHfKKF3MGmg/2z9XlnSvsHrN40mucddEki76LCWkijCoBZzsUoS
	 B36Y4B9fIgHjiDzP9JEnu1lLPR/RmeOoLwmB+v/NtfczB3Z3w79hzC/9uvJRB0ZgAY
	 Kt1O2a7oFh4zWAoEz+u2daADPM7jWSGM/vUpxdC3az3sxYA+lHkAxd5vrQYCpPq2sR
	 dHItFKUVuSHbPGpUYIrJbA9fjUoYkuqrMt/gKpLPaioj1fmFwn4orh71/J8SL2FD5u
	 yJo3qGxtiANU8xF1IuFMuUj04YKhHB5C7ybYJoiq1r5LfhMFgAYLMnXVpXDdU0MQXV
	 KsHmlG99A51+A==
X-Cc: Kuniyuki Iwashima <kuniyu@google.com>,
	Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org,
	Eric Dumazet <edumazet@kernel.org>,
	Jiayuan Chen <jiayuan.chen@linux.dev>
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