Netdev List
 help / color / mirror / Atom feed
From: Octavian Purdila <tavip@google.com>
To: netdev@vger.kernel.org, Eric Dumazet <edumazet@kernel.org>,
	 "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	 Willem de Bruijn <willemb@google.com>
Cc: Neal Cardwell <ncardwell@google.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	 Simon Horman <horms@kernel.org>, Arjun Roy <arjunroy@google.com>,
	 Soheil Hassas Yeganeh <soheil@google.com>,
	Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org,  linux-kernel@vger.kernel.org,
	Octavian Purdila <tavip@google.com>
Subject: [PATCH net 2/3] tcp: set exact recv_skip_hint on mid-frag in tcp_zerocopy_receive()
Date: Fri,  4 Dec 2026 05:41:06 +0000	[thread overview]
Message-ID: <20261204054107.96092-3-tavip@google.com> (raw)
In-Reply-To: <20261204054107.96092-1-tavip@google.com>

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@google.com>
---
 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


  parent reply	other threads:[~2026-12-04  5:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  5:41 [PATCH net 0/3] tcp: fix zerocopy leftover copy and mid-frag recv_skip_hint Octavian Purdila
2026-12-04  5:41 ` [PATCH net 1/3] tcp: avoid spurious leftover copy in tcp_zerocopy_receive() Octavian Purdila
2026-12-04  5:41 ` Octavian Purdila [this message]
2026-12-04  5:41 ` [PATCH net 3/3] selftests: net: add TCP_ZEROCOPY_RECEIVE leftover and hint tests Octavian Purdila

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204054107.96092-3-tavip@google.com \
    --to=tavip@google.com \
    --cc=arjunroy@google.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=soheil@google.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox