mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net 0/3] tcp: fix zerocopy leftover copy and mid-frag recv_skip_hint
@ 2026-12-04  5:41 Octavian Purdila
  2026-12-04  5:41 ` [PATCH net 1/3] tcp: avoid spurious leftover copy in tcp_zerocopy_receive() Octavian Purdila
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Octavian Purdila @ 2026-12-04  5:41 UTC (permalink / raw)
  To: netdev, Eric Dumazet, David S. Miller, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn
  Cc: Neal Cardwell, Kuniyuki Iwashima, Simon Horman, Arjun Roy,
	Soheil Hassas Yeganeh, Shuah Khan, linux-kselftest, linux-kernel,
	Octavian Purdila

When TCP receive zerocopy is used with copybuf enabled on skbs larger
than the caller's mapping buffer (such as GRO, coalesced, or BIG TCP
skbs), two bugs in tcp_zerocopy_receive() cause sustained fallback from
zerocopy to copying:

1. After the mapping loop fills zc->length while >= PAGE_SIZE bytes
   remain in the current skb, tcp_zc_handle_leftover() still runs using
   zc->recv_skip_hint (the remaining skb length) and copies copybuf_len
   bytes out of the next mappable page, misaligning copied_seq mid-frag.

2. When copied_seq is mid-frag (offset_frag != 0), tcp_zerocopy_receive()
   breaks with zc->recv_skip_hint left as skb->len - offset instead of
   computing the exact bytes to the next mappable frag, forcing callers
   or copybuf to copy the remainder of the skb instead of realigning at
   the next page boundary.

Patch 1 skips tcp_zc_handle_leftover() when length == zc->length and at
least PAGE_SIZE bytes remain in the skb, preserving sub-page tail
straggler copying. Patch 2 calls tcp_zerocopy_set_hint_for_skb() when
offset_frag is non-zero. Patch 3 adds a kselftest covering both fixes
and sub-page tail straggler copying for IPv4 and IPv6.

Octavian Purdila (3):
  tcp: avoid spurious leftover copy in tcp_zerocopy_receive()
  tcp: set exact recv_skip_hint on mid-frag in tcp_zerocopy_receive()
  selftests: net: add TCP_ZEROCOPY_RECEIVE leftover and hint tests

 net/ipv4/tcp.c                                |   9 +-
 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/tcp_zerocopy_receive.c      | 428 ++++++++++++++++++
 4 files changed, 437 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/net/tcp_zerocopy_receive.c


base-commit: 90ae4042bd9f35a9098d14b1a12a43df9b326e2b
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH net 1/3] tcp: avoid spurious leftover copy in tcp_zerocopy_receive()
  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
  2026-12-04  5:41 ` [PATCH net 2/3] tcp: set exact recv_skip_hint on mid-frag " Octavian Purdila
  2026-12-04  5:41 ` [PATCH net 3/3] selftests: net: add TCP_ZEROCOPY_RECEIVE leftover and hint tests Octavian Purdila
  2 siblings, 0 replies; 4+ messages in thread
From: Octavian Purdila @ 2026-12-04  5:41 UTC (permalink / raw)
  To: netdev, Eric Dumazet, David S. Miller, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn
  Cc: Neal Cardwell, Kuniyuki Iwashima, Simon Horman, Arjun Roy,
	Soheil Hassas Yeganeh, Shuah Khan, linux-kselftest, linux-kernel,
	Octavian Purdila

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH net 2/3] tcp: set exact recv_skip_hint on mid-frag in tcp_zerocopy_receive()
  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
  2026-12-04  5:41 ` [PATCH net 3/3] selftests: net: add TCP_ZEROCOPY_RECEIVE leftover and hint tests Octavian Purdila
  2 siblings, 0 replies; 4+ messages in thread
From: Octavian Purdila @ 2026-12-04  5:41 UTC (permalink / raw)
  To: netdev, Eric Dumazet, David S. Miller, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn
  Cc: Neal Cardwell, Kuniyuki Iwashima, Simon Horman, Arjun Roy,
	Soheil Hassas Yeganeh, Shuah Khan, linux-kselftest, linux-kernel,
	Octavian Purdila

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH net 3/3] selftests: net: add TCP_ZEROCOPY_RECEIVE leftover and hint tests
  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 ` [PATCH net 2/3] tcp: set exact recv_skip_hint on mid-frag " Octavian Purdila
@ 2026-12-04  5:41 ` Octavian Purdila
  2 siblings, 0 replies; 4+ messages in thread
From: Octavian Purdila @ 2026-12-04  5:41 UTC (permalink / raw)
  To: netdev, Eric Dumazet, David S. Miller, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn
  Cc: Neal Cardwell, Kuniyuki Iwashima, Simon Horman, Arjun Roy,
	Soheil Hassas Yeganeh, Shuah Khan, linux-kselftest, linux-kernel,
	Octavian Purdila

Add a kselftest covering TCP_ZEROCOPY_RECEIVE leftover copybuf handling
and recv_skip_hint computation for both IPv4 and IPv6 multi-frag skbs:

- full_mapping_with_copybuf: filling the requested VMA length mid-skb
  must not copy into copybuf or misalign subsequent zerocopy calls.
- unaligned_vma_len_with_copybuf: unaligned zc.length values map
  floor(len / PAGE_SIZE) pages without triggering spurious leftover
  copies, including when exactly one page remains in the skb.
- mid_frag_skip_hint_no_copybuf: when copied_seq is mid-frag,
  recv_skip_hint covers only the remainder of the partial frag rather
  than the entire skb.
- mid_frag_skip_hint_with_copybuf: with a large copybuf, mid-frag
  leftover copy stops at the next page boundary so remaining frags map
  via zerocopy.
- tail_straggler_copybuf: legitimate sub-page tail straggler copying
  continues to work.

Assisted-by: LLM
Signed-off-by: Octavian Purdila <tavip@google.com>
---
 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/tcp_zerocopy_receive.c      | 428 ++++++++++++++++++
 3 files changed, 430 insertions(+)
 create mode 100644 tools/testing/selftests/net/tcp_zerocopy_receive.c

diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore
index 27761239d0699..1684241c97087 100644
--- a/tools/testing/selftests/net/.gitignore
+++ b/tools/testing/selftests/net/.gitignore
@@ -51,6 +51,7 @@ tcp_fastopen_backup_key
 tcp_inq
 tcp_mmap
 tcp_port_share
+tcp_zerocopy_receive
 tfo
 timestamping
 tls
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 875ce04438149..a78816c3bc303 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -206,6 +206,7 @@ TEST_GEN_PROGS := \
 	so_reserve_mem \
 	tap \
 	tcp_port_share \
+	tcp_zerocopy_receive \
 	tls \
 	udp_splice_checksum \
 # end of TEST_GEN_PROGS
diff --git a/tools/testing/selftests/net/tcp_zerocopy_receive.c b/tools/testing/selftests/net/tcp_zerocopy_receive.c
new file mode 100644
index 0000000000000..92b4f5eac1293
--- /dev/null
+++ b/tools/testing/selftests/net/tcp_zerocopy_receive.c
@@ -0,0 +1,428 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Tests for TCP_ZEROCOPY_RECEIVE leftover copy and recv_skip_hint handling.
+ */
+#define _GNU_SOURCE
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <linux/tcp.h>
+#include <netinet/in.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#ifndef SO_ZEROCOPY
+#define SO_ZEROCOPY 60
+#endif
+
+#ifndef MSG_ZEROCOPY
+#define MSG_ZEROCOPY 0x4000000
+#endif
+
+#ifndef TCP_ZEROCOPY_RECEIVE
+#define TCP_ZEROCOPY_RECEIVE 35
+#endif
+
+#ifndef TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT
+#define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1
+#endif
+
+#define NUM_PAGES 8
+#define PARTIAL_OFFSET 100
+
+static bool tcp_timestamps_enabled(void)
+{
+	char buf[16] = {};
+	FILE *f;
+
+	f = fopen("/proc/sys/net/ipv4/tcp_timestamps", "r");
+	if (!f)
+		return true;
+	if (!fgets(buf, sizeof(buf), f)) {
+		fclose(f);
+		return true;
+	}
+	fclose(f);
+	return atoi(buf) != 0;
+}
+
+static void drain_errqueue(int fd)
+{
+	char cbuf[256];
+	struct msghdr msg = {
+		.msg_control = cbuf,
+		.msg_controllen = sizeof(cbuf),
+	};
+
+	while (recvmsg(fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT) >= 0)
+		msg.msg_controllen = sizeof(cbuf);
+}
+
+static void fill_pattern(uint8_t *buf, size_t len)
+{
+	size_t i;
+
+	for (i = 0; i < len; i++)
+		buf[i] = (uint8_t)((i * 31 + (i >> 12) * 17 + 1) & 0xff);
+}
+
+FIXTURE(tcp_zerocopy_receive)
+{
+	int cfd;
+	int sfd;
+	size_t page_size;
+	size_t total_len;
+	uint8_t *send_buf;
+	uint8_t *zc_buf;
+	uint8_t *copy_buf;
+};
+
+FIXTURE_VARIANT(tcp_zerocopy_receive)
+{
+	int family;
+};
+
+FIXTURE_VARIANT_ADD(tcp_zerocopy_receive, ipv4)
+{
+	.family = AF_INET,
+};
+
+FIXTURE_VARIANT_ADD(tcp_zerocopy_receive, ipv6)
+{
+	.family = AF_INET6,
+};
+
+FIXTURE_SETUP(tcp_zerocopy_receive)
+{
+	struct sockaddr_storage ss = {};
+	struct tcp_info info = {};
+	socklen_t slen, info_len;
+	int lfd, mss, buf_sz;
+	int one = 1;
+	int ret;
+
+	self->cfd = -1;
+	self->sfd = -1;
+	self->send_buf = MAP_FAILED;
+	self->zc_buf = MAP_FAILED;
+	self->copy_buf = NULL;
+
+	self->page_size = sysconf(_SC_PAGESIZE);
+	self->total_len = NUM_PAGES * self->page_size;
+
+	lfd = socket(variant->family, SOCK_STREAM, 0);
+	if (lfd < 0 && errno == EAFNOSUPPORT)
+		SKIP(return, "address family %d not supported",
+		     variant->family);
+	ASSERT_GE(lfd, 0);
+
+	ASSERT_EQ(setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,
+			     &one, sizeof(one)), 0);
+
+	mss = self->page_size + (tcp_timestamps_enabled() ? 12 : 0);
+	ret = setsockopt(lfd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss));
+	if (ret < 0 && errno == EINVAL) {
+		close(lfd);
+		SKIP(return, "TCP_MAXSEG %d not supported for page_size %zu",
+		     mss, self->page_size);
+	}
+	ASSERT_EQ(ret, 0);
+
+	buf_sz = 4 * 1024 * 1024;
+	ASSERT_EQ(setsockopt(lfd, SOL_SOCKET, SO_RCVBUF,
+			     &buf_sz, sizeof(buf_sz)), 0);
+
+	if (variant->family == AF_INET) {
+		struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
+
+		sin->sin_family = AF_INET;
+		sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+		slen = sizeof(*sin);
+	} else {
+		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
+
+		sin6->sin6_family = AF_INET6;
+		sin6->sin6_addr = in6addr_loopback;
+		slen = sizeof(*sin6);
+	}
+
+	ret = bind(lfd, (struct sockaddr *)&ss, slen);
+	if (ret < 0 && errno == EADDRNOTAVAIL) {
+		close(lfd);
+		SKIP(return, "loopback address unavailable for family %d",
+		     variant->family);
+	}
+	ASSERT_EQ(ret, 0);
+	ASSERT_EQ(getsockname(lfd, (struct sockaddr *)&ss, &slen), 0);
+	ASSERT_EQ(listen(lfd, 1), 0);
+
+	self->cfd = socket(variant->family, SOCK_STREAM, 0);
+	ASSERT_GE(self->cfd, 0);
+
+	ASSERT_EQ(setsockopt(self->cfd, SOL_SOCKET, SO_ZEROCOPY,
+			     &one, sizeof(one)), 0);
+	ASSERT_EQ(setsockopt(self->cfd, IPPROTO_TCP, TCP_NODELAY,
+			     &one, sizeof(one)), 0);
+	ASSERT_EQ(setsockopt(self->cfd, IPPROTO_TCP, TCP_MAXSEG,
+			     &mss, sizeof(mss)), 0);
+	ASSERT_EQ(setsockopt(self->cfd, SOL_SOCKET, SO_SNDBUF,
+			     &buf_sz, sizeof(buf_sz)), 0);
+
+	ASSERT_EQ(connect(self->cfd, (struct sockaddr *)&ss, slen), 0);
+
+	self->sfd = accept(lfd, NULL, NULL);
+	close(lfd);
+	ASSERT_GE(self->sfd, 0);
+
+	info_len = sizeof(info);
+	ASSERT_EQ(getsockopt(self->cfd, IPPROTO_TCP, TCP_INFO,
+			     &info, &info_len), 0);
+	if (info.tcpi_snd_mss != self->page_size)
+		SKIP(return, "tcpi_snd_mss %u != page_size %zu",
+		     info.tcpi_snd_mss, self->page_size);
+
+	self->send_buf = mmap(NULL, self->total_len,
+			      PROT_READ | PROT_WRITE,
+			      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	ASSERT_NE(self->send_buf, MAP_FAILED);
+	fill_pattern(self->send_buf, self->total_len);
+
+	self->zc_buf = mmap(NULL, self->total_len, PROT_READ, MAP_SHARED,
+			    self->sfd, 0);
+	ASSERT_NE(self->zc_buf, MAP_FAILED);
+
+	self->copy_buf = malloc(self->total_len);
+	ASSERT_NE(self->copy_buf, NULL);
+}
+
+FIXTURE_TEARDOWN(tcp_zerocopy_receive)
+{
+	if (self->cfd >= 0) {
+		drain_errqueue(self->cfd);
+		close(self->cfd);
+	}
+	if (self->zc_buf != MAP_FAILED)
+		munmap(self->zc_buf, self->total_len);
+	if (self->send_buf != MAP_FAILED)
+		munmap(self->send_buf, self->total_len);
+	free(self->copy_buf);
+	if (self->sfd >= 0)
+		close(self->sfd);
+}
+
+/*
+ * MSG_ZEROCOPY over loopback triggers skb_copy_ubufs() on receive, producing
+ * order-0, offset-0 PAGE_SIZE frags that satisfy TCP_ZEROCOPY_RECEIVE mapping
+ * requirements (same setup as tcp_mmap.c).
+ */
+static void send_and_wait(struct __test_metadata *_metadata,
+			  FIXTURE_DATA(tcp_zerocopy_receive) *self,
+			  size_t len)
+{
+	size_t sent = 0;
+	int inq = 0;
+	int tries;
+
+	while (sent < len) {
+		ssize_t n = send(self->cfd, self->send_buf + sent,
+				 len - sent, MSG_ZEROCOPY);
+
+		ASSERT_GT(n, 0);
+		sent += n;
+	}
+
+	for (tries = 0; tries < 200; tries++) {
+		ASSERT_EQ(ioctl(self->sfd, FIONREAD, &inq), 0);
+		if ((size_t)inq >= len)
+			break;
+		usleep(10000);
+	}
+	ASSERT_EQ((size_t)inq, len);
+}
+
+static int do_zc_recv(int fd, void *addr, uint32_t len, void *copybuf,
+		      int32_t copybuf_len, struct tcp_zerocopy_receive *zc)
+{
+	socklen_t zc_len = sizeof(*zc);
+
+	memset(zc, 0, sizeof(*zc));
+	zc->address = (uint64_t)(uintptr_t)addr;
+	zc->length = len;
+	zc->copybuf_address = (uint64_t)(uintptr_t)copybuf;
+	zc->copybuf_len = copybuf_len;
+	zc->flags = TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT;
+
+	return getsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, zc, &zc_len);
+}
+
+/*
+ * Filling the requested VMA mapping mid-skb must not trigger a leftover copy
+ * into copybuf, which would misalign copied_seq for subsequent zerocopy calls.
+ */
+TEST_F(tcp_zerocopy_receive, full_mapping_with_copybuf)
+{
+	size_t half = self->total_len / 2;
+	struct tcp_zerocopy_receive zc;
+
+	send_and_wait(_metadata, self, self->total_len);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, half,
+			     self->copy_buf, self->page_size - 1, &zc), 0);
+	ASSERT_EQ(zc.length, half);
+	EXPECT_EQ(zc.copybuf_len, 0);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf, half), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, half,
+			     self->copy_buf, self->page_size - 1, &zc), 0);
+	ASSERT_EQ(zc.length, half);
+	EXPECT_EQ(zc.copybuf_len, 0);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf + half, half), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+}
+
+/*
+ * Passing an unaligned zc.length maps floor(len / PAGE_SIZE) pages and must
+ * likewise avoid an unaligned copybuf read mid-skb.
+ */
+TEST_F(tcp_zerocopy_receive, unaligned_vma_len_with_copybuf)
+{
+	size_t req_len = 4 * self->page_size - 1;
+	size_t aligned = 3 * self->page_size;
+	size_t total = 7 * self->page_size;
+	struct tcp_zerocopy_receive zc;
+
+	send_and_wait(_metadata, self, total);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, req_len,
+			     self->copy_buf, self->page_size - 1, &zc), 0);
+	ASSERT_EQ(zc.length, aligned);
+	EXPECT_EQ(zc.copybuf_len, 0);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf, aligned), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, req_len,
+			     self->copy_buf, self->page_size - 1, &zc), 0);
+	ASSERT_EQ(zc.length, aligned);
+	EXPECT_EQ(zc.copybuf_len, 0);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf + aligned, aligned), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, req_len,
+			     self->copy_buf, self->page_size - 1, &zc), 0);
+	ASSERT_EQ(zc.length, self->page_size);
+	EXPECT_EQ(zc.copybuf_len, 0);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf + 2 * aligned,
+			 self->page_size), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+}
+
+/*
+ * When copied_seq lands mid-frag inside a multi-frag skb, recv_skip_hint
+ * should cover only the remainder of the partial frag, not the entire skb.
+ */
+TEST_F(tcp_zerocopy_receive, mid_frag_skip_hint_no_copybuf)
+{
+	size_t expected_hint = self->page_size - PARTIAL_OFFSET;
+	size_t rem = self->total_len - self->page_size;
+	struct tcp_zerocopy_receive zc;
+	ssize_t n;
+
+	send_and_wait(_metadata, self, self->total_len);
+
+	n = read(self->sfd, self->copy_buf, PARTIAL_OFFSET);
+	ASSERT_EQ(n, PARTIAL_OFFSET);
+	EXPECT_EQ(memcmp(self->copy_buf, self->send_buf, PARTIAL_OFFSET), 0);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, self->total_len,
+			     NULL, 0, &zc), 0);
+	EXPECT_EQ(zc.length, 0);
+	ASSERT_EQ(zc.recv_skip_hint, expected_hint);
+
+	n = read(self->sfd, self->copy_buf, zc.recv_skip_hint);
+	ASSERT_EQ(n, (ssize_t)expected_hint);
+	EXPECT_EQ(memcmp(self->copy_buf, self->send_buf + PARTIAL_OFFSET,
+			 expected_hint), 0);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, self->total_len,
+			     NULL, 0, &zc), 0);
+	ASSERT_EQ(zc.length, rem);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf + self->page_size,
+			 rem), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+}
+
+/*
+ * With copybuf provided (even larger than a page), mid-frag leftover copy
+ * should stop at the page boundary so the next call maps the remaining pages.
+ */
+TEST_F(tcp_zerocopy_receive, mid_frag_skip_hint_with_copybuf)
+{
+	size_t expected_copy = self->page_size - PARTIAL_OFFSET;
+	size_t rem = self->total_len - self->page_size;
+	struct tcp_zerocopy_receive zc;
+	ssize_t n;
+
+	send_and_wait(_metadata, self, self->total_len);
+
+	n = read(self->sfd, self->copy_buf, PARTIAL_OFFSET);
+	ASSERT_EQ(n, PARTIAL_OFFSET);
+	EXPECT_EQ(memcmp(self->copy_buf, self->send_buf, PARTIAL_OFFSET), 0);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, self->total_len,
+			     self->copy_buf, 2 * self->page_size, &zc), 0);
+	EXPECT_EQ(zc.length, 0);
+	ASSERT_EQ(zc.copybuf_len, (int32_t)expected_copy);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->copy_buf, self->send_buf + PARTIAL_OFFSET,
+			 expected_copy), 0);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, self->total_len,
+			     self->copy_buf, self->page_size - 1, &zc), 0);
+	ASSERT_EQ(zc.length, rem);
+	EXPECT_EQ(zc.copybuf_len, 0);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf + self->page_size,
+			 rem), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+}
+
+/*
+ * Legitimate sub-page tail straggler copying must continue to work when the
+ * mapping did not fill zc.length.
+ */
+TEST_F(tcp_zerocopy_receive, tail_straggler_copybuf)
+{
+	size_t total = 4 * self->page_size + PARTIAL_OFFSET;
+	size_t aligned = 4 * self->page_size;
+	struct tcp_zerocopy_receive zc;
+
+	send_and_wait(_metadata, self, total);
+
+	ASSERT_EQ(do_zc_recv(self->sfd, self->zc_buf, self->total_len,
+			     self->copy_buf, self->page_size - 1, &zc), 0);
+	ASSERT_EQ(zc.length, aligned);
+	ASSERT_EQ(zc.copybuf_len, PARTIAL_OFFSET);
+	EXPECT_EQ(zc.recv_skip_hint, 0);
+	EXPECT_EQ(memcmp(self->zc_buf, self->send_buf, aligned), 0);
+	EXPECT_EQ(memcmp(self->copy_buf, self->send_buf + aligned,
+			 PARTIAL_OFFSET), 0);
+	ASSERT_EQ(madvise(self->zc_buf, zc.length, MADV_DONTNEED), 0);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04  5:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH net 2/3] tcp: set exact recv_skip_hint on mid-frag " Octavian Purdila
2026-12-04  5:41 ` [PATCH net 3/3] selftests: net: add TCP_ZEROCOPY_RECEIVE leftover and hint tests Octavian Purdila

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®