* [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter()
@ 2026-11-19 10:15 Eric Dumazet
2026-11-21 19:11 ` Kuniyuki Iwashima
2026-11-21 19:35 ` netdev-bot+sashiko
0 siblings, 2 replies; 4+ messages in thread
From: Eric Dumazet @ 2026-11-19 10:15 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Kuniyuki Iwashima, Simon Horman, netdev, Eric Dumazet,
Jiayuan Chen
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() 2026-11-19 10:15 [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() Eric Dumazet @ 2026-11-21 19:11 ` Kuniyuki Iwashima 2026-11-21 19:35 ` netdev-bot+sashiko 1 sibling, 0 replies; 4+ messages in thread From: Kuniyuki Iwashima @ 2026-11-21 19:11 UTC (permalink / raw) To: Eric Dumazet Cc: David S . Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman, netdev, Jiayuan Chen On Thu, 19 Nov 2026 11:15:38 +0100, Eric Dumazet wrote: > 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> Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com> ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() 2026-11-19 10:15 [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() Eric Dumazet 2026-11-21 19:11 ` Kuniyuki Iwashima @ 2026-11-21 19:35 ` netdev-bot+sashiko 2026-11-23 9:02 ` Eric Dumazet 1 sibling, 1 reply; 4+ messages in thread From: netdev-bot+sashiko @ 2026-11-21 19:35 UTC (permalink / raw) To: edumazet Cc: davem, kuba, pabeni, willemb, kuniyu, horms, netdev, jiayuan.chen Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider. Critical: 0 · High: 1 · Medium: 0 · Low: 1 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). - [High] The new merge accounting in zerocopy_fill_skb_from_iter() (net/core/datagram.c) assumes that this skb's truesize already paid… - [Low] The patch says the problem was found with an AI coding assistant, but only in the text after the '---' separator. -- net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() This commit changes how zerocopy_fill_skb_from_iter() accounts truesize. Each new frag now charges one page. A chunk merged into the last frag only charges the pages that frag did not already cover. Before, the code charged PAGE_ALIGN(copied + start) for every iov_iter_get_pages2() batch. Link: https://lore.kernel.org/netdev/20261119090828.343748-1-jiayuan.chen@linux.dev/ > --- > 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. [ ... ] > With this patch, both cases add 4096 bytes. Kernel selftests were > not run. [Severity: Low] This isn't a bug, but this text is below the --- separator, so git am will drop it. The committed changelog would then not say that the problem was found with an AI coding assistant. The reproducer results and the note that selftests were not run would also be lost. Documentation/process/generated-content.rst says: if a tool was used to find a problem addressed by a change, that should be noted in the changelog. Could the tool attribution, and maybe the testing note, move above the Signed-off-by block? > diff --git a/net/core/datagram.c b/net/core/datagram.c > index 489d92fe6dae8..8c418c3adf1c9 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); [Severity: High] This seems to assume that this skb's truesize already pays for every page the last frag covers. Is that always true? It holds for frags created earlier by zerocopy_fill_skb_from_iter() itself. But TCP tail skbs can end with a frag that was charged per byte, and the frag's geometry alone does not show which pages this skb paid for. tso_fragment() in net/ipv4/tcp_output.c moves exactly nlen bytes of truesize to the remainder skb: buff->truesize += nlen; skb->truesize -= nlen; ... skb_split(skb, buff, len); skb_split() usually leaves buff with a last frag that ends mid-page. skb_zerocopy_clone() gives buff the same uarg, and msg_zerocopy_realloc() reuses that uarg for contiguous sends. buff then becomes tcp_write_queue_tail(), and tcp_sendmsg_locked() appends the next MSG_ZEROCOPY chunk to it via skb_zerocopy_iter_stream(). The MSG_SPLICE_PAGES path in tcp_sendmsg_locked() is similar. skb_splice_from_iter() charges only the spliced bytes: out: skb_len_add(skb, spliced); Suppose a following MSG_ZEROCOPY sendmsg continues in the same page. It matches the merge condition with a mid-page start, and the chunk ends inside the page. PAGE_ALIGN(start + size) - PAGE_ALIGN(start) is then 0, while skb->len grows by up to PAGE_SIZE - 1. For example: - a zerocopy send of 2048 bytes from page P is charged 4096 - tso_fragment() splits at 1000 bytes, and buff gets frag [P, 1000, 1048] with truesize 1048 - a zerocopy send of bytes 2048..4095 of P merges into that frag and is charged PAGE_ALIGN(4096) - PAGE_ALIGN(2048) = 0 - buff now holds 3096 bytes of frag data against 1048 bytes of truesize Before this patch, the same send was charged PAGE_ALIGN(2048 + 2048) = 4096, so frag truesize never dropped below len. This under-charges sk_wmem_queued and sk_forward_alloc. Over loopback, can it also hit the WARN in skb_try_coalesce()? skb_orphan_frags_rx()->skb_copy_ubufs() copies the payload into new kernel pages without touching truesize. tcp_add_backlog() or tcp_try_coalesce() then reach: net/core/skbuff.c:skb_try_coalesce() { ... delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); ... if (WARN_ON_ONCE(delta < 0 || delta < len)) return false; ... } The receiver's rmem accounting would also under-count the pages that skb_copy_ubufs() allocated. This looks reachable without privileges. One way is SO_ZEROCOPY on a TCP socket with MSG_ZEROCOPY sends of a contiguous buffer in sizes that are not page multiples, while cwnd or rwnd forces tso_fragment() on the tail skb. Another is splice or sendfile of part of a page, followed by a MSG_ZEROCOPY send of the rest of that page. Would it help to charge at least the merged bytes here, for example the larger of the page delta and size? Another option is to track which pages this skb has actually charged. > /* We combined this page, we need to release > * a reference. Since compound pages refcount [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119101538.3229466-1-edumazet%40kernel.org ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() 2026-11-21 19:35 ` netdev-bot+sashiko @ 2026-11-23 9:02 ` Eric Dumazet 0 siblings, 0 replies; 4+ messages in thread From: Eric Dumazet @ 2026-11-23 9:02 UTC (permalink / raw) To: netdev-bot+sashiko Cc: davem, kuba, pabeni, willemb, kuniyu, horms, netdev, jiayuan.chen Le sam. 21 nov. 2026 à 20:35, <netdev-bot+sashiko@kernel.org> a écrit : > > Thank you for your contribution! Sashiko AI review found 2 potential > issue(s) to consider. > > Critical: 0 · High: 1 · Medium: 0 · Low: 1 > > 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). > > - [High] The new merge accounting in zerocopy_fill_skb_from_iter() > (net/core/datagram.c) assumes that this skb's truesize already paid… > - [Low] The patch says the problem was found with an AI coding assistant, > but only in the text after the '---' separator. > > -- > > net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() > > This commit changes how zerocopy_fill_skb_from_iter() accounts truesize. > Each new frag now charges one page. A chunk merged into the last frag > only charges the pages that frag did not already cover. Before, the code > charged PAGE_ALIGN(copied + start) for every iov_iter_get_pages2() batch. > > Link: https://lore.kernel.org/netdev/20261119090828.343748-1-jiayuan.chen@linux.dev/ > > > --- > > 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. > > [ ... ] > > > With this patch, both cases add 4096 bytes. Kernel selftests were > > not run. > > [Severity: Low] > This isn't a bug, but this text is below the --- separator, so git am > will drop it. The committed changelog would then not say that the problem > was found with an AI coding assistant. The reproducer results and the > note that selftests were not run would also be lost. > > Documentation/process/generated-content.rst says: > > if a tool was used to find a problem addressed by a change, that should > be noted in the changelog. > > Could the tool attribution, and maybe the testing note, move above the > Signed-off-by block? Yes, v2 will have the discovery and testing statements in the fat changelog. > > > diff --git a/net/core/datagram.c b/net/core/datagram.c > > index 489d92fe6dae8..8c418c3adf1c9 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); > > [Severity: High] > This seems to assume that this skb's truesize already pays for every page > the last frag covers. Is that always true? It is not, this is a real issue. I reproduced the MSG_SPLICE_PAGES case on a kernel with v1 applied, without privileges, over loopback: - send() of one byte, so that the receive queue holds one skb - TCP_CORK, send() of one byte - sendfile() of the first half of a memfd page - sendmsg(MSG_ZEROCOPY) of the second half of the same page, from a shared mapping - uncork sendfile 2048 bytes: sk_wmem_queued += 2048 MSG_ZEROCOPY 2048 bytes: sk_wmem_queued += 0 The receiver then hits the warning you mentioned: WARNING: net/core/skbuff.c:6655 at skb_try_coalesce+0x5ac/0x5c0 tcp_try_coalesce+0xed/0x1b0 tcp_queue_rcv+0x5b/0x200 tcp_rcv_established+0xa13/0xcd0 tcp_v4_do_rcv+0x189/0x3e0 tcp_v4_rcv+0xcf6/0x1640 I did not try the tso_fragment() case, but I agree it looks similar. > > It holds for frags created earlier by zerocopy_fill_skb_from_iter() > itself. But TCP tail skbs can end with a frag that was charged per byte, > and the frag's geometry alone does not show which pages this skb paid > for. > > tso_fragment() in net/ipv4/tcp_output.c moves exactly nlen bytes of > truesize to the remainder skb: > > buff->truesize += nlen; > skb->truesize -= nlen; > ... > skb_split(skb, buff, len); > > skb_split() usually leaves buff with a last frag that ends mid-page. > skb_zerocopy_clone() gives buff the same uarg, and msg_zerocopy_realloc() > reuses that uarg for contiguous sends. buff then becomes > tcp_write_queue_tail(), and tcp_sendmsg_locked() appends the next > MSG_ZEROCOPY chunk to it via skb_zerocopy_iter_stream(). > > The MSG_SPLICE_PAGES path in tcp_sendmsg_locked() is similar. > skb_splice_from_iter() charges only the spliced bytes: > > out: > skb_len_add(skb, spliced); > > Suppose a following MSG_ZEROCOPY sendmsg continues in the same page. It > matches the merge condition with a mid-page start, and the chunk ends > inside the page. PAGE_ALIGN(start + size) - PAGE_ALIGN(start) is then 0, > while skb->len grows by up to PAGE_SIZE - 1. > > For example: > > - a zerocopy send of 2048 bytes from page P is charged 4096 > - tso_fragment() splits at 1000 bytes, and buff gets frag [P, 1000, 1048] > with truesize 1048 > - a zerocopy send of bytes 2048..4095 of P merges into that frag and is > charged PAGE_ALIGN(4096) - PAGE_ALIGN(2048) = 0 > - buff now holds 3096 bytes of frag data against 1048 bytes of truesize > > Before this patch, the same send was charged PAGE_ALIGN(2048 + 2048) = > 4096, so frag truesize never dropped below len. > > This under-charges sk_wmem_queued and sk_forward_alloc. Over loopback, > can it also hit the WARN in skb_try_coalesce()? > > skb_orphan_frags_rx()->skb_copy_ubufs() copies the payload into new > kernel pages without touching truesize. tcp_add_backlog() or > tcp_try_coalesce() then reach: > > net/core/skbuff.c:skb_try_coalesce() { > ... > delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); > ... > if (WARN_ON_ONCE(delta < 0 || delta < len)) > return false; > ... > } > > The receiver's rmem accounting would also under-count the pages that > skb_copy_ubufs() allocated. > > This looks reachable without privileges. One way is SO_ZEROCOPY on a TCP > socket with MSG_ZEROCOPY sends of a contiguous buffer in sizes that are > not page multiples, while cwnd or rwnd forces tso_fragment() on the tail > skb. Another is splice or sendfile of part of a page, followed by a > MSG_ZEROCOPY send of the rest of that page. > > Would it help to charge at least the merged bytes here, for example the > larger of the page delta and size? Yes. In this loop a chunk never crosses a page boundary, so the page delta is PAGE_SIZE if the chunk starts a page, and 0 otherwise. v2 will use: skb->truesize += offset_in_page(start) ? size : PAGE_SIZE; With this, the sequence above adds 2048 bytes for each half and the warning is gone. The 1024 one-byte iovecs case adds 5119 bytes (instead of 4 MB before the patch, and 4096 with v1). Thanks. pw-bot: cr > Another option is to track which pages > this skb has actually charged. > > > /* We combined this page, we need to release > > * a reference. Since compound pages refcount > > [ ... ] > > -- > Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119101538.3229466-1-edumazet%40kernel.org ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 9:02 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-19 10:15 [PATCH net-next] net: do not over-estimate truesize in zerocopy_fill_skb_from_iter() Eric Dumazet 2026-11-21 19:11 ` Kuniyuki Iwashima 2026-11-21 19:35 ` netdev-bot+sashiko 2026-11-23 9:02 ` Eric Dumazet
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.