[PATCH net 2/3] tcp: set exact recv_skip_hint on mid-frag in tcp_zerocopy_receive()
From: Octavian Purdila
Date: Fri Dec 04 2026 - 00:42:03 EST
In tcp_zerocopy_receive(), when copied_seq lands in the middle of a page
frag (offset_frag != 0), the loop breaks with zc->recv_skip_hint still
set to skb->len - offset, the entire remaining length of the skb.
For multi-frag skbs (such as GRO or BIG TCP skbs), this overstates the
number of unmappable bytes: after skipping only the remainder of the
current frag (and any subsequent unmappable frags), the remaining full
page frags in the same skb are mappable. Returning skb->len - offset
causes callers to read() the whole skb instead of realigning to the next
mappable frag, and causes tcp_zc_handle_leftover() to over-copy past the
frag boundary when copybuf_len exceeds the remaining bytes of the frag.
Call tcp_zerocopy_set_hint_for_skb() before breaking when offset_frag is
non-zero so zc->recv_skip_hint covers only the bytes up to the next
mappable frag.
Discovered by code inspection while investigating low TCP receive
zerocopy rates with BIG TCP and copybuf enabled. Reproduced on an
unmodified kernel with the new tcp_zerocopy_receive selftest added in
this series, and tested against it and tcp_mmap.
Fixes: 0c3936d32f75 ("net-zerocopy: Set zerocopy hint when data is copied")
Assisted-by: LLM
Signed-off-by: Octavian Purdila <tavip@xxxxxxxxxx>
---
net/ipv4/tcp.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index b94d5b2d77daf..bc9548c4212b7 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2291,8 +2291,13 @@ static int tcp_zerocopy_receive(struct sock *sk,
}
zc->recv_skip_hint = skb->len - offset;
frags = skb_advance_to_frag(skb, offset, &offset_frag);
- if (!frags || offset_frag)
+ if (!frags)
break;
+ if (offset_frag) {
+ tcp_zerocopy_set_hint_for_skb(sk, zc, skb,
+ offset);
+ break;
+ }
}
mappable_offset = find_next_mappable_frag(frags,
--
2.56.1.915.g69d5ec7298-goog