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 Signed-off-by: Eric Dumazet --- 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