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 1/3] tcp: avoid spurious leftover copy in tcp_zerocopy_receive()
Date: Fri,  4 Dec 2026 05:41:05 +0000	[thread overview]
Message-ID: <20261204054107.96092-2-tavip@google.com> (raw)
In-Reply-To: <20261204054107.96092-1-tavip@google.com>

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


  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 ` Octavian Purdila [this message]
2026-12-04  5:41 ` [PATCH net 2/3] tcp: set exact recv_skip_hint on mid-frag in tcp_zerocopy_receive() Octavian Purdila
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-2-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