[PATCH net 1/3] tcp: avoid spurious leftover copy in tcp_zerocopy_receive()

From: Octavian Purdila

Date: Fri Dec 04 2026 - 00:41:48 EST


In tcp_zerocopy_receive(), zc->recv_skip_hint tracks the remaining unread
bytes in the current skb while mapping pages into the user VMA. When the
mapping loop finishes because length reached zc->length while at least
PAGE_SIZE bytes remain in the current skb, zc->recv_skip_hint still holds
those remaining bytes rather than an unmappable skip length.

Because tcp_zc_handle_leftover() runs before zc->recv_skip_hint is
cleared for length == zc->length, a caller that supplies a copybuf gets
copybuf_len bytes copied out of the next mappable page. That advances
copied_seq into the middle of a page frag, forcing subsequent
TCP_ZEROCOPY_RECEIVE calls on the same skb to fall back to copying. This
commonly triggers when GRO, coalescing, or BIG TCP produces skbs larger
than the user's receive mapping buffer.

Skip tcp_zc_handle_leftover() when the requested mapping length was
filled and at least PAGE_SIZE bytes remain in the skb, while preserving
sub-page tail straggler copying when fewer than PAGE_SIZE bytes remain.

Observed while investigating low TCP receive zerocopy rates in
throughput benchmarks with BIG TCP and copybuf enabled, and confirmed
via code inspection. Reproduced on an unmodified kernel with a BIG TCP
loopback reproducer and the new tcp_zerocopy_receive selftest added in
this series, and tested against both as well as tcp_mmap.

Fixes: 18fb76ed5386 ("net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.")
Assisted-by: LLM
Signed-off-by: Octavian Purdila <tavip@xxxxxxxxxx>
---
net/ipv4/tcp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 80de376a9d2ef..b94d5b2d77daf 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2333,7 +2333,7 @@ static int tcp_zerocopy_receive(struct sock *sk,
out:
vma_end_read(vma);
/* Try to copy straggler data. */
- if (!ret)
+ if (!ret && (length != zc->length || zc->recv_skip_hint < PAGE_SIZE))
copylen = tcp_zc_handle_leftover(zc, sk, skb, &seq, copybuf_len, tss);

if (length + copylen) {
--
2.56.1.915.g69d5ec7298-goog