Skip to contentSkip to conversation

mptcp: add MSG_ZEROCOPY support

PATCH Superseded Geliang Tang 4 participants 19 messages Nov 25 – Nov 26

Subsystems (4)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v9 cover mptcp: add MSG_ZEROCOPY support

From: Geliang Tang <[email protected]>

This series adds MSG_ZEROCOPY support for MPTCP sockets, allowing userspace to transmit data without intermediate kernel copies, and provides corresponding selftests to exercise the new path.

v9:
- drop "ipv4: fix uninitialized memory in zerocopy cmsg".
- patch 3:
The dfrag memory accounting now uses PAGE_SIZE for zerocopy dfrags instead of the actual data length, and sk_wmem_schedule checks are skipped for zerocopy paths.
A page reference leak was fixed when iov_iter_get_pages2 returns zero. mptcp_sendmsg_fastopen helper now accepts and forwards the ubuf parameter.
Error handling has been improved by using uarg_to_msgzc()->zerocopy = 0 instead of net_zcopy_put_abort, and the success path now checks ubuf directly rather than the zc flag.
Additionally, MSG_FASTOPEN flag clearing is handled separately from the SOCK_ZEROCOPY check.
- patch 4:
mptcp_setsockopt_sol_socket_zerocopy helper now increments the setsockopt sequence counter and iterates through all subflows, setting SOCK_ZEROCOPY on each one and updating their setsockopt_seq values. Additionally, sync_socket_options has been updated to synchronize the SOCK_ZEROCOPY flag to keep subflows consistent with the msk.
- patch 5:
Documentation has been updated to reflect MPTCP support in the msg_zerocopy guide.
- patch 6:
add mptcp support for selftests msg_zerocopy.c.

v8:
- Add pg_off + pg_len overflow check in mptcp_sendmsg_zerocopy_iter() to
cap the sum at U16_MAX, preventing silent wraparound in dfrag->offset
during partial ACK processing.
- Reorder kzalloc_obj() before sk_wmem_schedule() to eliminate erroneous sk_forward_alloc_add() on allocation failure path.
- Update caller to handle -EAGAIN/-ENOMEM by jumping to wait_for_memory, preserving blocking socket semantics.
- Refactor wait_for_completions_zc() to use clock_gettime(CLOCK_MONOTONIC) instead of loop counter, preventing premature exit when poll() returns immediately after shut_wr().
- Add retry loop in do_sendmsg_zc() to handle short writes from sendmsg(MSG_ZEROCOPY).
- Skip wait_for_completions_zc() when size is 0 to avoid 5-second stall on
MPTFO transfers.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v7:
- Add the pg_len / pg_off defensive truncation directly inside mptcp_sendmsg_zerocopy_iter() (the new helper in patch 5). When iov_iter_get_pages2() returns more than U16_MAX bytes, the excess
is reverted from the iov_iter so the next iteration picks it up
cleanly. When the page offset exceeds U16_MAX (on 256KB-page
kernels like PowerPC and Hexagon), the call returns -EFAULT
instead of silently truncating. The non-zerocopy path is left
untouched.
- Refactor mptcp_sendmsg_zerocopy_iter() to return the per-iteration
byte count as ssize_t so the caller can simply accumulate via
copied += ret; this drops a ssize_t *copied out-parameter.
- Clean up and fix the zerocopy completion path in mptcp_connect.c
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v6:
- Add patch 2 to widen offset field in struct mptcp_data_frag from u16 to
u32 to support page sizes up to 4GB on architectures with large page
sizes (256KB on PowerPC and Hexagon). This prevents offset wrap-around
when page fragment offset exceeds 65535.
- Add patch 5 to fix uninitialized memory leak in IPv4 error queue
handling for zerocopy completion notifications. Add early check for SO_EE_ORIGIN_ZEROCOPY in ipv4_datagram_support_cmsg() to prevent
reading uninitialized memory from ip_hdr(skb)->saddr.
- Refactor zerocopy completion notification draining logic in selftests.
Extract process_zc_cmsg() helper to process individual control messages. Extract drain_errqueue() helper to handle error queue draining.
Simplify wait_for_zc_completions() main loop from 6 levels to 3 levels.
Move variables to their actual usage scope for better maintainability.
- Use batched polling (200ms intervals with 5s total timeout) to drain completion notifications for large transfers that may generate multiple completion events.
- Parse error queue messages to verify all sent data has been ACKed before breaking the completion loop.
- Reset msg_controllen each iteration of the inner reap loop to prevent truncation of control messages.
- Use >= instead of > in the MPTFO size adjustment so file_size reaches 0
when the whole file is sent via fast open.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v5:
- A new patch to align struct mptcp_data_frag to 8 bytes on 32-bit.
- Prevent coalescing of dfrags with different ubuf instances into the same subflow skb to avoid premature completion notifications.
- Return -EFAULT instead of -EAGAIN when iov_iter_get_pages2() returns 0
to properly signal iterator exhaustion.
- Account for dfrag metadata overhead in sk_wmem_queued_add() to prevent bypassing socket memory limits.
- Widen data_len field from u16 to u32 to prevent truncation on 64KB-page kernels (ARM64, PPC64).
- In selftests, use batched polling (200ms intervals with 5s total timeout)
to drain completion notifications for large transfers that may generate multiple completion events.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v4:
- two more cleanups, patch 2 and patch 7.
- net/mptcp/protocol.c, mptcp_sendmsg_frag(): pull the type-mismatch
check out of the if (can_coalesce) block and use the standard
skb_zcopy_pure() helper; mptcp_sendmsg_zerocopy_iter() now
calls iov_iter_revert() before put_page() in the kzalloc failure
path so a retry starts from the same iterator offset.
- tools/testing/selftests/net/mptcp/mptcp_connect.c, copyfd_io_zc():
reset msg_controllen each iteration of the inner reap loop using a
do {} while block; use >= instead of > in the MPTFO size
adjustment so file_size reaches 0 when the whole file is sent via
fast open.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v3:
- Force fresh skb when mixing zerocopy and kernel-copy fragments.
- Simplify zero-copy availability check by using SOCK_ZEROCOPY flag
directly.
- Validate SO_ZEROCOPY value in setsockopt and avoid affecting
fallback subflow.
- In selftests, drain MPTFO partial data, use poll+recvmsg for completion notifications with timeout, and verify getsockopt round-trip.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v2:
- patch 2, never mix PURE_ZEROCOPY frags with kernel-copy frags in the
same skb; handle fallback, return values.
- patch 3, a new patch to handle SO_ZEROCOPY in setsockopt.
- patch 4, set SO_ZEROCOPY, handle listen_mode, error queue.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v1:
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

Geliang Tang (7):
mptcp: use local variable tp in sendmsg_frag
mptcp: remove redundant orig_offset in carve_data_frag
mptcp: add MSG_ZEROCOPY support
mptcp: handle SO_ZEROCOPY in setsockopt
Documentation: msg_zerocopy: add MPTCP support
selftests: net: msg_zerocopy: add MPTCP support
selftests: mptcp: cover msg_zerocopy test

 Documentation/networking/msg_zerocopy.rst     |   4 +-
 net/mptcp/protocol.c                          | 173 ++++++++++++++++--
 net/mptcp/protocol.h                          |   1 +
 net/mptcp/sockopt.c                           |  30 ++-
 tools/testing/selftests/net/mptcp/.gitignore  |   1 +
 tools/testing/selftests/net/mptcp/Makefile    |   5 +
 .../selftests/net/mptcp/msg_zerocopy.sh       |   1 +
 tools/testing/selftests/net/msg_zerocopy.c    |   6 +
 tools/testing/selftests/net/msg_zerocopy.sh   |   8 +-
 9 files changed, 206 insertions(+), 23 deletions(-)
 create mode 120000 tools/testing/selftests/net/mptcp/msg_zerocopy.sh
Signature · 1 line
2.55.0
author reviewer
#1 lore raw

PATCH v9 1/7 mptcp: use local variable tp in sendmsg_frag

From: Geliang Tang <[email protected]>

In mptcp_sendmsg_frag(), tcp_sk(ssk) is called multiple times. Introduce a local variable 'tp' to hold the tcp_sock pointer and use it throughout the function. This improves code readability, with no functional change.

---
 net/mptcp/protocol.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +4 -3
index d64e04245963..b9ae52caf47e 100644
@@ -1447,6 +1447,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	u64 data_seq = dfrag->data_seq + info->sent;
 	int offset = dfrag->offset + info->sent;
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct tcp_sock *tp = tcp_sk(ssk);
 	bool zero_window_probe = false;
 	struct mptcp_ext *mpext = NULL;
 	bool can_coalesce = false;
@@ -1483,14 +1484,14 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 		mpext = mptcp_get_ext(skb);
 		if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
 			TCP_SKB_CB(skb)->eor = 1;
-			tcp_mark_push(tcp_sk(ssk), skb);
+			tcp_mark_push(tp, skb);
 			goto alloc_skb;
 		}
 
 		i = skb_shinfo(skb)->nr_frags;
 		can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
 		if (!can_coalesce && i >= READ_ONCE(net_hotdata.sysctl_max_skb_frags)) {
-			tcp_mark_push(tcp_sk(ssk), skb);
+			tcp_mark_push(tp, skb);
 			goto alloc_skb;
 		}
 
@@ -1548,7 +1549,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	skb->truesize += copy;
 	sk_wmem_queued_add(ssk, copy);
 	sk_mem_charge(ssk, copy);
-	WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
+	WRITE_ONCE(tp->write_seq, tp->write_seq + copy);
 	TCP_SKB_CB(skb)->end_seq += copy;
 	tcp_skb_pcount_set(skb, 0);
 
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH mptcp-next v9 1/7] mptcp: use local variable tp in sendmsg_frag

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The From:/Signed-off-by: email addresses do not match.

--

mptcp: use local variable tp in sendmsg_frag

This commit adds a local variable tp to hold tcp_sk(ssk) in mptcp_sendmsg_frag() in net/mptcp/protocol.c. It then uses tp for the tcp_mark_push() calls and the write_seq update. No functional change is intended.

Quoted 1 line

Author: Geliang Tang <[email protected]>

[ ... ]

Quoted 1 line

[Severity: Low]
This isn't a bug, but the From: and Signed-off-by: email addresses don't match. The author is [email protected], and the only sign-off uses [email protected].

The .mailmap file maps both addresses to the same person:

Geliang Tang <[email protected]> <[email protected]>

The author sign-off check in scripts/checkpatch.pl does not read .mailmap, so it still warns:

    WARN("FROM_SIGN_OFF_MISMATCH",
         "From:/Signed-off-by: email address mismatch: $sob_msg\n");

The netdev patchwork CI will probably flag this too.

Could the author be set to the kylinos.cn address, or could a Signed-off-by with the kernel.org address be added?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795591278.git.tanggeliang%40kylinos.cn
author reviewer
#3 lore raw

PATCH v9 2/7 mptcp: remove redundant orig_offset in carve_data_frag

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

mptcp_carve_data_frag() already receives struct page_frag via its 'pfrag' parameter, making the separate 'orig_offset' parameter redundant since it merely duplicates pfrag->offset.

Simplify the function signature by removing this unnecessary parameter and directly accessing the offset from struct pfrag internally.

---
 net/mptcp/protocol.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +3 -3
index b9ae52caf47e..504d0b196d84 100644
@@ -1302,9 +1302,9 @@ static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
 }
 
 static struct mptcp_data_frag *
-mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
-		      int orig_offset)
+mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag)
 {
+	int orig_offset = pfrag->offset;
 	int offset = ALIGN(orig_offset, sizeof(long));
 	struct mptcp_data_frag *dfrag;
 
@@ -2111,7 +2111,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 			if (!mptcp_page_frag_refill(sk, pfrag))
 				goto wait_for_memory;
 
-			dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
+			dfrag = mptcp_carve_data_frag(msk, pfrag);
 			frag_truesize = dfrag->overhead;
 		}
 
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH mptcp-next v9 2/7] mptcp: remove redundant orig_offset in carve_data_frag

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The authorship and sign-off identities don't match.

--

mptcp: remove redundant orig_offset in carve_data_frag

This commit removes the orig_offset parameter from mptcp_carve_data_frag() in net/mptcp/protocol.c. The function now reads pfrag->offset itself, and the only caller, mptcp_sendmsg(), is updated to match.

Quoted 3 lines

Author: Geliang Tang <[email protected]>
...

[Severity: Low]
This isn't a bug, but the author address and the Signed-off-by address are different. No Signed-off-by line uses [email protected].

checkpatch reports this as FROM_SIGN_OFF_MISMATCH:

scripts/checkpatch.pl:

    "From:/Signed-off-by: email address mismatch: $sob_msg\n");

.mailmap maps [email protected] to [email protected], but checkpatch does not read .mailmap, so the warning still fires. The other six patches in the series have the same mismatch.

Could this be resent with an in-body "From: Geliang Tang <[email protected]>" line, or with a Signed-off-by that uses the [email protected] address?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795591278.git.tanggeliang%40kylinos.cn
author reviewer
#5 lore raw

PATCH v9 3/7 mptcp: add MSG_ZEROCOPY support

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

This patch enables MSG_ZEROCOPY support for MPTCP sockets, allowing user-space to transmit data without intermediate kernel copies.

The implementation follows the same lifecycle model as TCP:

A single ubuf_info_msgzc is allocated per sendmsg via msg_zerocopy_realloc(). Its reference count consists of three parts:
one 'caller' reference held until sendmsg returns, one reference per MPTCP dfrag in the retransmission queue (released when the dfrag is acked and cleared), and one reference per subflow skb that consumes dfrag bytes (released by TCP's skb_zcopy_clear() when the skb is acked). Completion is reported only after all bytes are acknowledged at both MPTCP and subflow levels.

User pages are pinned via iov_iter_get_pages2() and stored directly in a newly allocated mptcp_data_frag. Subflow skbs take additional page references via get_page() and are marked with skb_zcopy_set() and SKBFL_PURE_ZEROCOPY.

The feature silently downgrades to regular copy when MSG_FASTOPEN is set or the application has not opted in via setsockopt(SO_ZEROCOPY).

To prevent premature completion notifications, dfrags with different ubuf instances cannot be coalesced into the same subflow skb. The new mptcp_sendmsg_zerocopy_iter() helper handles page pinning and ensures offsets stay within u16 limits.

---
 net/mptcp/protocol.c | 160 ++++++++++++++++++++++++++++++++++++++++---
 net/mptcp/protocol.h |   1 +
 2 files changed, 150 insertions(+), 11 deletions(-)
net/mptcp/protocol.c +149 -11
index 504d0b196d84..6bd10a4e1e93 100644
@@ -1186,18 +1186,27 @@ static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
 		df->data_seq + df->data_len == msk->write_seq;
 }
 
-static void dfrag_uncharge(struct sock *sk, int len)
+static void dfrag_uncharge(struct sock *sk, int len,
+			   struct mptcp_data_frag *dfrag)
 {
-	sk_mem_uncharge(sk, len);
+	if (!dfrag->ubuf)
+		sk_mem_uncharge(sk, len);
 	sk_wmem_queued_add(sk, -len);
 }
 
 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
 {
-	int len = dfrag->data_len + dfrag->overhead;
+	int len = dfrag->ubuf ? PAGE_SIZE + dfrag->overhead :
+				dfrag->data_len + dfrag->overhead;
 
 	list_del(&dfrag->list);
-	dfrag_uncharge(sk, len);
+	dfrag_uncharge(sk, len, dfrag);
+	if (dfrag->ubuf) {
+		net_zcopy_put(dfrag->ubuf);
+		put_page(dfrag->page);
+		kfree(dfrag);
+		return;
+	}
 	put_page(dfrag->page);
 }
 
@@ -1242,7 +1251,8 @@ static void __mptcp_clean_una(struct sock *sk)
 		dfrag->data_len -= delta;
 		dfrag->already_sent -= delta;
 
-		dfrag_uncharge(sk, delta);
+		if (!dfrag->ubuf)
+			dfrag_uncharge(sk, delta, dfrag);
 	}
 
 	/* all retransmitted data acked, recovery completed */
@@ -1316,6 +1326,7 @@ mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag)
 	dfrag->already_sent = 0;
 	dfrag->page = pfrag->page;
 	dfrag->eor = 0;
+	dfrag->ubuf = NULL;
 
 	return dfrag;
 }
@@ -1488,6 +1499,16 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 			goto alloc_skb;
 		}
 
+		if (skb_zcopy_pure(skb) != !!dfrag->ubuf) {
+			tcp_mark_push(tp, skb);
+			goto alloc_skb;
+		}
+		if (dfrag->ubuf && skb_zcopy(skb) &&
+		    skb_zcopy(skb) != dfrag->ubuf) {
+			tcp_mark_push(tp, skb);
+			goto alloc_skb;
+		}
+
 		i = skb_shinfo(skb)->nr_frags;
 		can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
 		if (!can_coalesce && i >= READ_ONCE(net_hotdata.sysctl_max_skb_frags)) {
@@ -1531,7 +1552,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 
 	/* like TCP, allow some progress up to tcp_wmem[0] */
 	avail = tcp_wmem_schedule(ssk, copy);
-	if (avail <= 0) {
+	if (!dfrag->ubuf && avail <= 0) {
 		tcp_remove_empty_skb(ssk);
 		return -ENOMEM;
 	}
@@ -1542,13 +1563,19 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	} else {
 		get_page(dfrag->page);
 		skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
+
+		if (dfrag->ubuf && !skb_zcopy(skb)) {
+			skb_zcopy_set(skb, dfrag->ubuf, NULL);
+			skb_shinfo(skb)->flags |= SKBFL_PURE_ZEROCOPY;
+		}
 	}
 
 	skb->len += copy;
 	skb->data_len += copy;
 	skb->truesize += copy;
 	sk_wmem_queued_add(ssk, copy);
-	sk_mem_charge(ssk, copy);
+	if (!skb_zcopy_pure(skb))
+		sk_mem_charge(ssk, copy);
 	WRITE_ONCE(tp->write_seq, tp->write_seq + copy);
 	TCP_SKB_CB(skb)->end_seq += copy;
 	tcp_skb_pcount_set(skb, 0);
@@ -1940,7 +1967,8 @@ static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool
 static int mptcp_disconnect(struct sock *sk, int flags);
 
 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
-				  size_t len, int *copied_syn)
+				  size_t len, int *copied_syn,
+				  struct ubuf_info *ubuf)
 {
 	unsigned int saved_flags = msg->msg_flags;
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -1966,7 +1994,7 @@ static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
 	lock_sock(ssk);
 	msg->msg_flags |= MSG_DONTWAIT;
 	msk->fastopening = 1;
-	ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
+	ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, ubuf);
 	msk->fastopening = 0;
 	msg->msg_flags = saved_flags;
 	release_sock(ssk);
@@ -2048,27 +2076,113 @@ static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
 	}
 }
 
+static ssize_t mptcp_sendmsg_zerocopy_iter(struct sock *sk, struct msghdr *msg,
+					   struct ubuf_info *ubuf,
+					   u32 copy_limit)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct mptcp_data_frag *dfrag;
+	struct page *pages[1] = {};
+	size_t pg_off = 0;
+	ssize_t pg_len;
+
+	pg_len = iov_iter_get_pages2(&msg->msg_iter, pages,
+				     copy_limit, 1, &pg_off);
+	if (pg_len < 0)
+		return pg_len;
+	if (pg_len == 0) {
+		if (pages[0])
+			put_page(pages[0]);
+		return -EFAULT;
+	}
+
+	if (pg_len > U16_MAX) {
+		iov_iter_revert(&msg->msg_iter, pg_len - U16_MAX);
+		pg_len = U16_MAX;
+	}
+
+	if (pg_off > U16_MAX) {
+		iov_iter_revert(&msg->msg_iter, pg_len);
+		put_page(pages[0]);
+		return -EFAULT;
+	}
+
+	if (pg_off + pg_len > U16_MAX + 1) {
+		size_t len = U16_MAX + 1 - pg_off;
+
+		iov_iter_revert(&msg->msg_iter, pg_len - len);
+		pg_len = len;
+	}
+
+	dfrag = kzalloc_obj(*dfrag, GFP_KERNEL_ACCOUNT);
+	if (!dfrag) {
+		iov_iter_revert(&msg->msg_iter, pg_len);
+		put_page(pages[0]);
+		return -ENOMEM;
+	}
+
+	if (!ubuf && !sk_wmem_schedule(sk, pg_len + sizeof(*dfrag))) {
+		kfree(dfrag);
+		iov_iter_revert(&msg->msg_iter, pg_len);
+		put_page(pages[0]);
+		return -EAGAIN;
+	}
+
+	dfrag->data_len = pg_len;
+	dfrag->data_seq = msk->write_seq;
+	dfrag->offset = pg_off;
+	dfrag->page = pages[0];
+	dfrag->ubuf = ubuf;
+	dfrag->overhead = sizeof(struct mptcp_data_frag);
+
+	/* one ref for the dfrag; released in dfrag_clear */
+	net_zcopy_get(ubuf);
+
+	WRITE_ONCE(msk->write_seq, msk->write_seq + pg_len);
+	sk_wmem_queued_add(sk, PAGE_SIZE + dfrag->overhead);
+
+	list_add_tail(&dfrag->list, &msk->rtx_queue);
+	if (!msk->first_pending)
+		msk->first_pending = dfrag;
+
+	return pg_len;
+}
+
 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct ubuf_info *ubuf = NULL;
 	struct page_frag *pfrag;
 	size_t copied = 0;
+	bool zc = false;
 	int ret = 0;
 	long timeo;
 
 	/* silently ignore everything else */
 	msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
-			  MSG_FASTOPEN | MSG_EOR;
+			  MSG_FASTOPEN | MSG_EOR | MSG_ZEROCOPY;
 
 	lock_sock(sk);
 
 	mptcp_rps_record_subflows(msk);
 
+	if (!sock_flag(sk, SOCK_ZEROCOPY))
+		msg->msg_flags &= ~MSG_ZEROCOPY;
+
+	if ((msg->msg_flags & MSG_ZEROCOPY) && len) {
+		ubuf = msg_zerocopy_realloc(sk, len, NULL, false);
+		if (!ubuf) {
+			ret = -ENOBUFS;
+			goto do_error;
+		}
+		zc = true;
+	}
+
 	if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
 		     msg->msg_flags & MSG_FASTOPEN)) {
 		int copied_syn = 0;
 
-		ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
+		ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn, ubuf);
 		copied += copied_syn;
 		if (ret == -EINPROGRESS && copied_syn > 0)
 			goto out;
@@ -2102,6 +2216,24 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		if (!copy_limit)
 			goto wait_for_memory;
 
+		if (zc) {
+			ret = mptcp_sendmsg_zerocopy_iter(sk, msg, ubuf,
+							  copy_limit);
+			if (ret == -EAGAIN || ret == -ENOMEM)
+				goto wait_for_memory;
+			if (ret == -EFAULT) {
+				if (copied)
+					goto do_error;
+				uarg_to_msgzc(ubuf)->zerocopy = 0;
+				zc = false;
+			} else if (ret < 0) {
+				goto do_error;
+			} else {
+				copied += ret;
+				continue;
+			}
+		}
+
 		/* reuse tail pfrag, if possible, or carve a new one from the
 		 * page allocator
 		 */
@@ -2177,6 +2309,8 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	}
 
 out:
+	if (ubuf)
+		net_zcopy_put(ubuf);
 	release_sock(sk);
 	return copied;
 
@@ -2187,6 +2321,10 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	}
 
 	copied = sk_stream_error(sk, msg->msg_flags, ret);
+	if (ubuf) {
+		net_zcopy_put_abort(ubuf, true);
+		ubuf = NULL;
+	}
 	goto out;
 }
 
net/mptcp/protocol.h +1 -0
index 2c9a442dacd6..5378321ae58a 100644
@@ -285,6 +285,7 @@ struct mptcp_data_frag {
 	u8 eor;			/* currently using 1 bit */
 	u16 already_sent;
 	struct page *page;
+	struct ubuf_info *ubuf;
 };
 
 /* Arbitrary compromise between as low as possible to react timely to subflow
Signature · 1 line
2.55.0
#6 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] mptcp: corrupted copy length and wmem accounting in mptcp_sendmsg_frag() - [Medium] mptcp: missing…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] mptcp: corrupted copy length and wmem accounting in mptcp_sendmsg_frag()
- [Medium] mptcp: missing zerocopy downgrade on fastopen in mptcp_sendmsg()
- [Low] mptcp: dead !ubuf check before net_zcopy_get() in mptcp_sendmsg_zerocopy_iter()
--

commit 5139e983df0c81d1f761fa0ac9e35eeb08cfa459
Author: Geliang Tang <[email protected]>

mptcp: add MSG_ZEROCOPY support

This commit adds MSG_ZEROCOPY support for MPTCP sockets by pinning user pages into mptcp_data_frag structures and tracking completion across both the MPTCP retransmission queue and subflow skbs via ubuf_info_msgzc.

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index 504d0b196d84b..6bd10a4e1e938 100644

[ ... ]

Quoted 20 lines
net/mptcp/protocol.c +6 -1
@@ -1531,7 +1552,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 
 	/* like TCP, allow some progress up to tcp_wmem[0] */
 	avail = tcp_wmem_schedule(ssk, copy);
-	if (avail <= 0) {
+	if (!dfrag->ubuf && avail <= 0) {
 		tcp_remove_empty_skb(ssk);
 		return -ENOMEM;
 	}
@@ -1542,13 +1563,19 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	} else {
 		get_page(dfrag->page);
 		skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
+
+		if (dfrag->ubuf && !skb_zcopy(skb)) {
+			skb_zcopy_set(skb, dfrag->ubuf, NULL);
+			skb_shinfo(skb)->flags |= SKBFL_PURE_ZEROCOPY;
+		}
 	}
 

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 1581

Quoted 10 lines
net/mptcp/protocol.c +2 -1
 	skb->len += copy;
 	skb->data_len += copy;
 	skb->truesize += copy;
 	sk_wmem_queued_add(ssk, copy);
-	sk_mem_charge(ssk, copy);
+	if (!skb_zcopy_pure(skb))
+		sk_mem_charge(ssk, copy);
 	WRITE_ONCE(tp->write_seq, tp->write_seq + copy);
 	TCP_SKB_CB(skb)->end_seq += copy;
 	tcp_skb_pcount_set(skb, 0);

[Severity: High]
Should tcp_wmem_schedule(ssk, copy) and copy = avail be skipped entirely when dfrag->ubuf is non-NULL, matching tcp_sendmsg_locked()?

Right now tcp_wmem_schedule(ssk, copy) and copy = avail still run for zerocopy dfrags while only the avail <= 0 error return and sk_mem_charge(ssk, copy) are skipped. When tcp_wmem_schedule() succeeds, it raises ssk->sk_forward_alloc without a matching sk_mem_charge() or payload uncharge in tcp_wmem_free_skb(). When ssk hits write memory pressure and tcp_wmem_schedule() returns avail == 0 (or avail < 0 if ssk->sk_forward_alloc is negative), copy is overwritten with 0 (or wraps to a large size_t), leaving an empty 0-length skb on ssk->sk_write_queue that stalls tcp_write_xmit() or corrupting the skb lengths and sequence numbers.

[ ... ]

Quoted 8 lines
net/mptcp/protocol.c +4 -0
@@ -2048,27 +2076,113 @@ static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
 	}
 }
 
+static ssize_t mptcp_sendmsg_zerocopy_iter(struct sock *sk, struct msghdr *msg,
+					   struct ubuf_info *ubuf,
+					   u32 copy_limit)
+{

[ ... ]

Quoted 13 lines
net/mptcp/protocol.c +13 -0
+	dfrag = kzalloc_obj(*dfrag, GFP_KERNEL_ACCOUNT);
+	if (!dfrag) {
+		iov_iter_revert(&msg->msg_iter, pg_len);
+		put_page(pages[0]);
+		return -ENOMEM;
+	}
+
+	if (!ubuf && !sk_wmem_schedule(sk, pg_len + sizeof(*dfrag))) {
+		kfree(dfrag);
+		iov_iter_revert(&msg->msg_iter, pg_len);
+		put_page(pages[0]);
+		return -EAGAIN;
+	}

net/mptcp/protocol.c static void mptcp_rps_record_subflows(const struct mptcp_sock *msk) · line 2139

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+
+	dfrag->data_len = pg_len;
+	dfrag->data_seq = msk->write_seq;
+	dfrag->offset = pg_off;
+	dfrag->page = pages[0];
+	dfrag->ubuf = ubuf;
+	dfrag->overhead = sizeof(struct mptcp_data_frag);
+
+	/* one ref for the dfrag; released in dfrag_clear */
+	net_zcopy_get(ubuf);

[Severity: Low]
Is the !ubuf check in mptcp_sendmsg_zerocopy_iter() dead code? mptcp_sendmsg() only calls this helper when zc is true (so ubuf is non-NULL), and net_zcopy_get(ubuf) unconditionally dereferences ubuf a few lines below.

Quoted 38 lines
net/mptcp/protocol.c +22 -1
+
+	WRITE_ONCE(msk->write_seq, msk->write_seq + pg_len);
+	sk_wmem_queued_add(sk, PAGE_SIZE + dfrag->overhead);
+
+	list_add_tail(&dfrag->list, &msk->rtx_queue);
+	if (!msk->first_pending)
+		msk->first_pending = dfrag;
+
+	return pg_len;
+}
+
 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct ubuf_info *ubuf = NULL;
 	struct page_frag *pfrag;
 	size_t copied = 0;
+	bool zc = false;
 	int ret = 0;
 	long timeo;
 
 	/* silently ignore everything else */
 	msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
-			  MSG_FASTOPEN | MSG_EOR;
+			  MSG_FASTOPEN | MSG_EOR | MSG_ZEROCOPY;
 
 	lock_sock(sk);
 
 	mptcp_rps_record_subflows(msk);
 
+	if (!sock_flag(sk, SOCK_ZEROCOPY))
+		msg->msg_flags &= ~MSG_ZEROCOPY;
+
+	if ((msg->msg_flags & MSG_ZEROCOPY) && len) {
+		ubuf = msg_zerocopy_realloc(sk, len, NULL, false);
+		if (!ubuf) {
+			ret = -ENOBUFS;
+			goto do_error;

net/mptcp/protocol.c static void mptcp_rps_record_subflows(const struct mptcp_sock *msk) · line 2185

Quoted 10 lines
net/mptcp/protocol.c +5 -1
+		}
+		zc = true;
+	}
+
 	if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
 		     msg->msg_flags & MSG_FASTOPEN)) {
 		int copied_syn = 0;
 
-		ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
+		ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn, ubuf);

[Severity: Medium]
Does the fastopen path miss downgrading ubuf and zc to regular copy?

The commit message notes that MSG_ZEROCOPY silently downgrades to regular copy when MSG_FASTOPEN is set, and tcp_send_syn_data() copies the SYN payload into a kernel page_frag via copy_page_from_iter() before calling skb_zcopy_set(syn_data, fo->uarg, NULL). However, msg_zerocopy_realloc() initializes uarg_to_msgzc(ubuf)->zerocopy = 1 and mptcp_sendmsg() sets zc = true before entering the DEFER_CONNECT / MSG_FASTOPEN branch. Without clearing uarg_to_msgzc(ubuf)->zerocopy = 0 and zc = false here, completion notifications for the copied SYN data will omit SO_EE_CODE_ZEROCOPY_COPIED, and any remaining bytes after mptcp_sendmsg_fastopen() will still take the mptcp_sendmsg_zerocopy_iter() path.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw
In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 5 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In mptcp_sendmsg_frag() (net/mptcp/protocol.c), zerocopy dfrags no longer bail out when tcp_wmem_schedule() returns <= 0.
- [Medium] mptcp_sendmsg_frag() pins whole user pages into subflow skbs (get_page(dfrag->page)) but charges the subflow only copy bytes…
- [Medium] mptcp_sendmsg_zerocopy_iter() returns -EFAULT when pg_off > U16_MAX.
- [Medium] The commit message says the feature "silently downgrades to regular copy when MSG_FASTOPEN is set", but the code does not do that.
- [Medium] mptcp_sendmsg() turns on zerocopy for the whole msk whenever SOCK_ZEROCOPY and MSG_ZEROCOPY are set.
- [Low] In mptcp_sendmsg_zerocopy_iter(), the socket-memory check if (!ubuf && !sk_wmem_schedule(sk, pg_len + sizeof(*dfrag))) is dead…

--

mptcp: add MSG_ZEROCOPY support

This commit adds MSG_ZEROCOPY support to MPTCP sockets. User pages are pinned with iov_iter_get_pages2() into separately allocated mptcp_data_frag entries that hold a ubuf_info reference. Subflow skbs built from those dfrags are marked with skb_zcopy_set() and SKBFL_PURE_ZEROCOPY.

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index 504d0b196d84b..6bd10a4e1e938 100644

[ ... ]

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 1558

Quoted 9 lines
net/mptcp/protocol.c +1 -1
@@ -1531,7 +1552,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 
 	/* like TCP, allow some progress up to tcp_wmem[0] */
 	avail = tcp_wmem_schedule(ssk, copy);
-	if (avail <= 0) {
+	if (!dfrag->ubuf && avail <= 0) {
 		tcp_remove_empty_skb(ssk);
 		return -ENOMEM;
 	}

[Severity: Medium]

For zerocopy dfrags this no longer bails out when tcp_wmem_schedule() returns 0, but the code right after still does copy = avail. What happens when the subflow is under memory pressure?

When sk_wmem_schedule() fails and sk_wmem_queued is already above tcp_wmem[0], tcp_wmem_schedule() returns min(copy, sk->sk_forward_alloc). That looks easy to hit here, because zerocopy bytes are added to ssk->sk_wmem_queued but are never sk_mem_charge()d.

With copy == 0, mptcp_sendmsg_frag() still does:

    get_page(dfrag->page);
    skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
    skb_zcopy_set(skb, dfrag->ubuf, NULL);

For a fresh skb it also fills an mpext with use_map = 1 and data_len = 0. It then returns 0 without calling tcp_remove_empty_skb(). The zero window probe branch has the same problem: copy is forced to 1, then overwritten with 0 on a frozen mpext.

__mptcp_alloc_tx_skb() has already done tcp_skb_entail(). Doesn't this leave an empty skb (seq == end_seq) in the subflow write queue?

Later, a different data_seq may be sent on this subflow (another subflow took the dfrag, or a retransmission). In that case mptcp_skb_can_collapse_to() fails and a new skb is queued behind the empty one. tcp_write_xmit() stops at an empty send head:

    if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq)
        break;

The subflow would then stall until the probe0 timer sends the empty skb as a data-less segment with a zero-length DSS mapping.

There is also a smaller issue. When the schedule succeeds, forward memory is reserved for copy bytes but never used, because sk_mem_charge() is skipped for pure zerocopy skbs.

For comparison, tcp_sendmsg_locked() never calls tcp_wmem_schedule() for pure zerocopy skbs, and never uses a 0 result as a frag length:

    if (!skb_zcopy_pure(skb)) {
        copy = tcp_wmem_schedule(sk, copy);
        if (!copy)
            goto wait_for_space;
    }

Should the zerocopy path here follow the same pattern?

Quoted 8 lines
net/mptcp/protocol.c +4 -0
@@ -1542,13 +1563,19 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	} else {
 		get_page(dfrag->page);
 		skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
+
+		if (dfrag->ubuf && !skb_zcopy(skb)) {
+			skb_zcopy_set(skb, dfrag->ubuf, NULL);
+			skb_shinfo(skb)->flags |= SKBFL_PURE_ZEROCOPY;

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 1578

Quoted 10 lines
net/mptcp/protocol.c +3 -1
+		}
 	}
 
 	skb->len += copy;
 	skb->data_len += copy;
 	skb->truesize += copy;
 	sk_wmem_queued_add(ssk, copy);
-	sk_mem_charge(ssk, copy);
+	if (!skb_zcopy_pure(skb))
+		sk_mem_charge(ssk, copy);

[Severity: Medium]

For zerocopy dfrags, get_page(dfrag->page) pins a whole user page into the subflow skb. The subflow is only charged copy bytes in truesize and sk_wmem_queued, though. Can a sender use this to pin far more pages than sndbuf allows?

TCP's own MSG_ZEROCOPY path charges the full page span of each pinned chunk in zerocopy_fill_skb_from_iter():

    skb->truesize += PAGE_ALIGN(copied + start);

Here, the only page-sized charge is the PAGE_SIZE + overhead that mptcp_sendmsg_zerocopy_iter() adds to the msk. dfrag_clear() drops that charge as soon as the MPTCP DATA_ACK covers the data. ack_update_msk() accepts a DATA_ACK up to msk->snd_nxt, independent of subflow level TCP ACKs.

Consider a sender using 1-byte-per-page iovecs and a peer that sends DATA_ACKs but withholds subflow ACKs:

- Each subflow skb could pin up to sysctl_max_skb_frags full pages for roughly 1-1.5KB of accounted truesize.
- sk_stream_memory_free(ssk) would keep admitting more data.
- The pages stay pinned until the subflow times out or resets.

mm_account_pinned_pages() counts bytes rather than pages, so RLIMIT_MEMLOCK would not catch this either.

[ ... ]

net/mptcp/protocol.c static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, · line 1997

Quoted 6 lines
net/mptcp/protocol.c +1 -1
@@ -1966,7 +1994,7 @@ static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
 	lock_sock(ssk);
 	msg->msg_flags |= MSG_DONTWAIT;
 	msk->fastopening = 1;
-	ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
+	ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, ubuf);

[Severity: Medium]

The commit message says:

The feature silently downgrades to regular copy when MSG_FASTOPEN is set or the application has not opted in via setsockopt(SO_ZEROCOPY).

Is that accurate for MSG_FASTOPEN? mptcp_sendmsg() allocates the ubuf and sets zc = true before the DEFER_CONNECT/MSG_FASTOPEN branch. This hunk then passes that ubuf to tcp_sendmsg_fastopen() instead of NULL, and tcp_send_syn_data() attaches it to the SYN skb:

    skb_zcopy_set(syn_data, fo->uarg, NULL);

If mptcp_sendmsg_fastopen() returns 0, the main loop continues with zc still true. The remaining bytes then go through mptcp_sendmsg_zerocopy_iter(), because nothing clears zc or ubuf for MSG_FASTOPEN.

The SYN payload is copied, but the completion is not marked SO_EE_CODE_ZEROCOPY_COPIED.

Should the code or the commit message be updated so that they match?

[ ... ]

Quoted 8 lines
net/mptcp/protocol.c +4 -0
@@ -2048,27 +2076,113 @@ static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
 	}
 }
 
+static ssize_t mptcp_sendmsg_zerocopy_iter(struct sock *sk, struct msghdr *msg,
+					   struct ubuf_info *ubuf,
+					   u32 copy_limit)
+{

[ ... ]

net/mptcp/protocol.c static void mptcp_rps_record_subflows(const struct mptcp_sock *msk) · line 2108

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+	if (pg_len > U16_MAX) {
+		iov_iter_revert(&msg->msg_iter, pg_len - U16_MAX);
+		pg_len = U16_MAX;
+	}
+
+	if (pg_off > U16_MAX) {
+		iov_iter_revert(&msg->msg_iter, pg_len);
+		put_page(pages[0]);
+		return -EFAULT;
+	}

[Severity: Medium]

Is -EFAULT the right error here? The user memory is valid; the offset just doesn't fit in the u16 dfrag->offset field. mptcp_sendmsg() still treats it as a real fault:

    if (ret == -EFAULT) {
        if (copied)
            goto do_error;
        uarg_to_msgzc(ubuf)->zerocopy = 0;
        zc = false;

Take PAGE_SIZE >= 128K (for example 256K page powerpc or hexagon configs) and a buffer starting at page offset 0:

chunk 1 is clamped to 65535 bytes
chunk 2 starts at pg_off 65535 and is clamped to 1 byte by the
pg_off + pg_len > U16_MAX + 1 check below
chunk 3 has pg_off 65536 > U16_MAX and returns -EFAULT

A blocking sendmsg() would then return a short count of 65536 bytes even though no error occurred. A buffer that starts at page offset >= 65536 would silently lose zerocopy for the whole message.

On 64K pages the same clamping produces alternating 65535-byte and 1-byte dfrags, each charged PAGE_SIZE + overhead.

Quoted 11 lines
net/mptcp/protocol.c +11 -0
+
+	if (pg_off + pg_len > U16_MAX + 1) {
+		size_t len = U16_MAX + 1 - pg_off;
+
+		iov_iter_revert(&msg->msg_iter, pg_len - len);
+		pg_len = len;
+	}
+
+	dfrag = kzalloc_obj(*dfrag, GFP_KERNEL_ACCOUNT);
+	if (!dfrag) {
+		iov_iter_revert(&msg->msg_iter, pg_len);

net/mptcp/protocol.c static void mptcp_rps_record_subflows(const struct mptcp_sock *msk) · line 2129

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		put_page(pages[0]);
+		return -ENOMEM;
+	}
+
+	if (!ubuf && !sk_wmem_schedule(sk, pg_len + sizeof(*dfrag))) {
+		kfree(dfrag);
+		iov_iter_revert(&msg->msg_iter, pg_len);
+		put_page(pages[0]);
+		return -EAGAIN;
+	}

[Severity: Low]

Can ubuf ever be NULL here? mptcp_sendmsg_zerocopy_iter() is only called when zc is true, and zc is only set after msg_zerocopy_realloc() returns a non-NULL ubuf.

If not, this sk_wmem_schedule() check and its -EAGAIN path are dead code. Zerocopy dfrag metadata is then never charged to the msk's sk_forward_alloc.

Instead, the dfrag above is allocated with a hard-coded GFP_KERNEL_ACCOUNT. That charges the current task's memcg rather than sk->sk_memcg, and it ignores sk->sk_allocation, unlike the rest of the MPTCP tx path.

Only sk_wmem_queued is increased, by PAGE_SIZE + overhead. The free in dfrag_clear() is symmetric, so nothing leaks.

[ ... ]

Quoted 2 lines

static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) {

[ ... ]

net/mptcp/protocol.c static void mptcp_rps_record_subflows(const struct mptcp_sock *msk) · line 2179

Quoted 11 lines
net/mptcp/protocol.c +11 -0
+	if (!sock_flag(sk, SOCK_ZEROCOPY))
+		msg->msg_flags &= ~MSG_ZEROCOPY;
+
+	if ((msg->msg_flags & MSG_ZEROCOPY) && len) {
+		ubuf = msg_zerocopy_realloc(sk, len, NULL, false);
+		if (!ubuf) {
+			ret = -ENOBUFS;
+			goto do_error;
+		}
+		zc = true;
+	}

[Severity: Medium]

Should there be a NETIF_F_SG check before zerocopy is enabled here? tcp_sendmsg_locked() does:

    if (sk->sk_route_caps & NETIF_F_SG)
        zc = MSG_ZEROCOPY;
    else
        uarg_to_msgzc(uarg)->zerocopy = 0;

mptcp_sendmsg() enables zerocopy for the whole msk without checking the subflow routes, and it never sets zerocopy = 0.

On a subflow without SG, the pinned page frags would be linearized or copied before transmit. The completion would still report real zerocopy instead of SO_EE_CODE_ZEROCOPY_COPIED.

TCP subflows usually get NETIF_F_SG through sk_setup_caps(). So this seems limited to devices without SG where GSO is masked, for example non-offloaded xfrm with dst->header_len, or sk_gso_disabled.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795591278.git.tanggeliang%40kylinos.cn
author reviewer
#8 lore raw

PATCH v9 4/7 mptcp: handle SO_ZEROCOPY in setsockopt

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

SO_ZEROCOPY is currently rejected by MPTCP setsockopt with -EOPNOTSUPP, even though the matching MSG_ZEROCOPY path in mptcp_sendmsg() is wired up. As a result MSG_ZEROCOPY silently falls back to a copy on MPTCP sockets because the opt-in flag is never set.

Expose SO_ZEROCOPY through the SOL_SOCKET setsockopt dispatcher. The new mptcp_setsockopt_sol_socket_zerocopy() helper validates that val is 0 or 1 (matching standard TCP sk_setsockopt), flips SOCK_ZEROCOPY on the msk, and propagates it to all subflows. sync_socket_options() also synchronizes the flag to keep subflows consistent.

---
 net/mptcp/sockopt.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
net/mptcp/sockopt.c +29 -1
index 922f6ae5c80c..3391f7a9d983 100644
@@ -184,6 +184,31 @@ static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optnam
 	return 0;
 }
 
+static int mptcp_setsockopt_sol_socket_zerocopy(struct mptcp_sock *msk, int val)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *sk = (struct sock *)msk;
+
+	if (val != 0 && val != 1)
+		return -EINVAL;
+
+	lock_sock(sk);
+	sock_valbool_flag(sk, SOCK_ZEROCOPY, val);
+	sockopt_seq_inc(msk);
+
+	mptcp_for_each_subflow(msk, subflow) {
+		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
+		bool slow = lock_sock_fast(ssk);
+
+		sock_valbool_flag(ssk, SOCK_ZEROCOPY, val);
+		subflow->setsockopt_seq = msk->setsockopt_seq;
+		unlock_sock_fast(ssk, slow);
+	}
+
+	release_sock(sk);
+	return 0;
+}
+
 static int mptcp_setsockopt_sol_socket_int(struct mptcp_sock *msk, int optname,
 					   sockptr_t optval,
 					   unsigned int optlen)
@@ -212,6 +237,8 @@ static int mptcp_setsockopt_sol_socket_int(struct mptcp_sock *msk, int optname,
 	case SO_TIMESTAMPNS_OLD:
 	case SO_TIMESTAMPNS_NEW:
 		return mptcp_setsockopt_sol_socket_tstamp(msk, optname, val);
+	case SO_ZEROCOPY:
+		return mptcp_setsockopt_sol_socket_zerocopy(msk, val);
 	}
 
 	return -ENOPROTOOPT;
@@ -351,6 +378,7 @@ static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname,
 	case SO_TIMESTAMP_NEW:
 	case SO_TIMESTAMPNS_OLD:
 	case SO_TIMESTAMPNS_NEW:
+	case SO_ZEROCOPY:
 		return mptcp_setsockopt_sol_socket_int(msk, optname, optval,
 						       optlen);
 	case SO_TIMESTAMPING_OLD:
@@ -396,7 +424,6 @@ static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname,
 	 * SO_CNX_ADVICE is currently unsupported, could possibly be relevant,
 	 * but likely needs careful design
 	 *
-	 * SO_ZEROCOPY is currently unsupported, TODO in sndmsg
 	 * SO_TXTIME is currently unsupported
 	 */
 
@@ -1682,6 +1709,7 @@ static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
 	}
 
 	sock_valbool_flag(ssk, SOCK_DBG, sock_flag(sk, SOCK_DBG));
+	sock_valbool_flag(ssk, SOCK_ZEROCOPY, sock_flag(sk, SOCK_ZEROCOPY));
 
 	if (inet_csk(sk)->icsk_ca_ops != inet_csk(ssk)->icsk_ca_ops)
 		tcp_set_congestion_control(ssk, msk->ca_name, false, true);
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH mptcp-next v9 4/7] mptcp: handle SO_ZEROCOPY in setsockopt

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mptcp: 0-length skb queued in mptcp_sendmsg_frag() on zerocopy wmem failure --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mptcp: 0-length skb queued in mptcp_sendmsg_frag() on zerocopy wmem failure
--

commit c40bf75d2e840f9a30a3c937060e37a47f1aa32c
Author: Geliang Tang <[email protected]>

mptcp: handle SO_ZEROCOPY in setsockopt

This commit enables SO_ZEROCOPY in MPTCP setsockopt by setting SOCK_ZEROCOPY on the MPTCP socket and propagating it to all subflows.

Quoted 8 lines
net/mptcp/sockopt.c +0 -0
index 922f6ae5c80cb..3391f7a9d9834 100644
@@ -184,6 +184,31 @@ static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optnam
 	return 0;
 }
 

net/mptcp/sockopt.c static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optnam · line 196

Quoted 10 lines
net/mptcp/sockopt.c +10 -0
+static int mptcp_setsockopt_sol_socket_zerocopy(struct mptcp_sock *msk, int val)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *sk = (struct sock *)msk;
+
+	if (val != 0 && val != 1)
+		return -EINVAL;
+
+	lock_sock(sk);
+	sock_valbool_flag(sk, SOCK_ZEROCOPY, val);

[Severity: Medium]
Can enabling SOCK_ZEROCOPY cause mptcp_sendmsg_frag() to leave a 0-length skb on ssk->sk_write_queue when tcp_wmem_schedule() returns 0?

With SOCK_ZEROCOPY set, mptcp_sendmsg() queues mptcp_data_frag entries with dfrag->ubuf set. When __subflow_push_pending() pushes such a fragment under subflow wmem pressure, mptcp_sendmsg_frag() skips the avail <= 0 check but still overwrites copy with avail:

net/mptcp/protocol.c:mptcp_sendmsg_frag() {
    ...
	avail = tcp_wmem_schedule(ssk, copy);
	if (!dfrag->ubuf && avail <= 0) {
		tcp_remove_empty_skb(ssk);
		return -ENOMEM;
	}
	copy = avail;
    ...
}

When tcp_wmem_schedule() returns 0 on a newly allocated skb, execution continues with copy == 0, attaches a 0-byte page fragment and a dfrag->ubuf reference via skb_zcopy_set(), and leaves the empty skb (end_seq == seq) on ssk->sk_write_queue. Once a non-coalescing skb is queued behind it, tcp_write_xmit() stalls at tcp_send_head(ssk), wedging transmission on the subflow and holding the ubuf reference until the subflow is closed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#10 lore raw

Re: [PATCH mptcp-next v9 4/7] mptcp: handle SO_ZEROCOPY in setsockopt

In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] This patch adds mptcp_setsockopt_sol_socket_zerocopy() (net/mptcp/sockopt.c), the first way to set SOCK_ZEROCOPY on an MPTCP…

--

mptcp: handle SO_ZEROCOPY in setsockopt

Until now MPTCP setsockopt() rejected SO_ZEROCOPY with -EOPNOTSUPP, so on MPTCP sockets MSG_ZEROCOPY quietly fell back to copying. This patch adds mptcp_setsockopt_sol_socket_zerocopy(), which checks that val is 0 or 1, sets or clears SOCK_ZEROCOPY on the msk and pushes the flag to every subflow. sync_socket_options() also copies the flag to new subflows.

Quoted 8 lines
net/mptcp/sockopt.c +0 -0
index 922f6ae5c80cb..3391f7a9d9834 100644
@@ -184,6 +184,31 @@ static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optnam
 	return 0;
 }
 

net/mptcp/sockopt.c static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optnam · line 196

Quoted 10 lines
net/mptcp/sockopt.c +10 -0
+static int mptcp_setsockopt_sol_socket_zerocopy(struct mptcp_sock *msk, int val)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *sk = (struct sock *)msk;
+
+	if (val != 0 && val != 1)
+		return -EINVAL;
+
+	lock_sock(sk);
+	sock_valbool_flag(sk, SOCK_ZEROCOPY, val);

[Severity: Medium]
This is the first way to set SOCK_ZEROCOPY on an msk. That means unprivileged userspace can now reach the ubuf dfrag path in mptcp_sendmsg_frag(), which the earlier patch in this series, "mptcp: add MSG_ZEROCOPY support", added. Does that path handle memory pressure correctly?

In net/mptcp/protocol.c:mptcp_sendmsg_frag(), every dfrag goes through tcp_wmem_schedule(), including pure zero-copy ones. The early exit is then skipped for ubuf dfrags:

net/mptcp/protocol.c:mptcp_sendmsg_frag() {
    ...
	avail = tcp_wmem_schedule(ssk, copy);
	if (!dfrag->ubuf && avail <= 0) {
		tcp_remove_empty_skb(ssk);
		return -ENOMEM;
	}
	copy = avail;
    ...
}

tcp_wmem_schedule() can return 0 in two cases: when sk_wmem_schedule() fails with sk_forward_alloc at 0, and when the subflow already has at least tcp_wmem[0] queued.

Plain TCP avoids this by skipping the schedule for pure zero-copy skbs, and by treating 0 as wait_for_space:

net/ipv4/tcp.c:tcp_sendmsg_locked() {
    ...
			if (!skb_zcopy_pure(skb)) {
				copy = tcp_wmem_schedule(sk, copy);
				if (!copy)
					goto wait_for_space;
			}
    ...
}

When copy is 0 on the MPTCP side, mptcp_sendmsg_frag() still does this:

		get_page(dfrag->page);
		skb_fill_page_desc(skb, i, dfrag->page, offset, copy);

		if (dfrag->ubuf && !skb_zcopy(skb)) {
			skb_zcopy_set(skb, dfrag->ubuf, NULL);
			skb_shinfo(skb)->flags |= SKBFL_PURE_ZEROCOPY;
		}

So a zero-size frag is added, and page and uarg references are taken for zero bytes.

On the alloc_skb path, __mptcp_alloc_tx_skb() has already called tcp_skb_entail(), so the skb is on the subflow write queue. The function then writes the mapping:

	mpext->data_len = copy;
	mpext->use_map = 1;

and returns 0 without calling tcp_remove_empty_skb(). RFC 8684 reserves data_len 0 for the infinite mapping.

The other two early returns in this function call tcp_remove_empty_skb() to avoid exactly this state. __subflow_push_pending() treats ret <= 0 as "stop pushing" and does not remove the skb.

Could mptcp_push_release()->tcp_push() then send a zero-length data segment carrying a data_len 0 DSS mapping? That segment would also be counted in packets_out and could be retransmitted by RTO.

The triggering sequence would be:

setsockopt(SOL_SOCKET, SO_ZEROCOPY, 1) sendmsg(MSG_ZEROCOPY)
mptcp_sendmsg_zerocopy_iter() /* dfrag->ubuf != NULL */ __mptcp_push_pending()
mptcp_sendmsg_frag()

This needs tcp_wmem[0] or more queued on the subflow, sk_forward_alloc at 0, and __sk_mem_schedule() failing. The last condition can happen under global TCP memory pressure above tcp_mem[2], or with a tight memcg.

On the reuse path with !can_coalesce, the same thing appends a zero-size frag to an existing skb. If sk_forward_alloc could go negative here, copy = avail would also wrap to a huge size_t value. I couldn't confirm that sk_forward_alloc goes negative on this path, though.

The code is still the same at the end of the series.

Would it make sense to fix the ubuf handling in mptcp_sendmsg_frag() before exposing SO_ZEROCOPY? For example, skip tcp_wmem_schedule() for pure zero-copy dfrags as TCP does, or handle a 0 result with tcp_remove_empty_skb().

Quoted 3 lines
net/mptcp/sockopt.c +3 -0
+	sockopt_seq_inc(msk);
+
+	mptcp_for_each_subflow(msk, subflow) {

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795591278.git.tanggeliang%40kylinos.cn
author reviewer
#11 lore raw

PATCH v9 5/7 Documentation: msg_zerocopy: add MPTCP support

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Update the MSG_ZEROCOPY documentation to list MPTCP as one of the protocols that support this feature, alongside TCP, UDP and VSOCK.

---
 Documentation/networking/msg_zerocopy.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/networking/msg_zerocopy.rst +2 -2
index 78fb70e748b7..0b91beb2f5be 100644
@@ -7,8 +7,8 @@ Intro
 =====
 
 The MSG_ZEROCOPY flag enables copy avoidance for socket send calls.
-The feature is currently implemented for TCP, UDP and VSOCK (with
-virtio transport) sockets.
+The feature is currently implemented for TCP, MPTCP, UDP and VSOCK
+(with virtio transport) sockets.
 
 
 Opportunity and Caveats
Signature · 1 line
2.55.0
#12 lore raw

Re: [PATCH mptcp-next v9 5/7] Documentation: msg_zerocopy: add MPTCP support

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] Commit "Documentation: msg_zerocopy: add MPTCP support" adds MPTCP to the protocol list in the Intro of…

--

Documentation: msg_zerocopy: add MPTCP support

This commit adds MPTCP to the list of protocols that support MSG_ZEROCOPY in the Intro section of Documentation/networking/msg_zerocopy.rst.

Documentation/networking/msg_zerocopy.rst Intro · line 11

Quoted 12 lines
Documentation/networking/msg_zerocopy.rst +2 -2
index 78fb70e748b77..0b91beb2f5bea 100644
@@ -7,8 +7,8 @@ Intro
 =====
 
 The MSG_ZEROCOPY flag enables copy avoidance for socket send calls.
-The feature is currently implemented for TCP, UDP and VSOCK (with
-virtio transport) sockets.
+The feature is currently implemented for TCP, MPTCP, UDP and VSOCK
+(with virtio transport) sockets.

[Severity: Low]
Should the protocol-specific sections later in msg_zerocopy.rst also mention MPTCP? After this change they still only name TCP/UDP and VSOCK.

The Notification Parsing section says:

The level and type fields in the control data are protocol family specific, IP_RECVERR or IPV6_RECVERR (for TCP or UDP socket). For VSOCK socket, cmsg_level will be SOL_VSOCK and cmsg_type will be VSOCK_RECVERR.

MPTCP error queue reads go through inet_recv_error():

net/mptcp/protocol.c:mptcp_recvmsg() {
    ...
	if (unlikely(flags & MSG_ERRQUEUE))
		return inet_recv_error(sk, msg, len);
    ...
}

So MPTCP sockets also get IP_RECVERR/IPV6_RECVERR. The text doesn't say so.

The Implementation/Loopback section has the same gap:

For TCP and UDP:
Data sent to local sockets can be queued indefinitely if the receive process does not read its socket. ...

For VSOCK:
Data path sent to local sockets is the same as for non-local sockets.

MPTCP subflows are TCP, so the TCP deferred copy behaviour should apply. The document doesn't say that MPTCP is covered there either.

Later patches in this series don't change msg_zerocopy.rst again.

Could these two sections be updated to include MPTCP alongside TCP?

Quoted 3 lines
Documentation/networking/msg_zerocopy.rst +0 -0
 Opportunity and Caveats
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795591278.git.tanggeliang%40kylinos.cn
author reviewer
#13 lore raw

PATCH v9 6/7 selftests: net: msg_zerocopy: add MPTCP support

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Add support for testing MSG_ZEROCOPY over MPTCP sockets. This enables the existing msg_zerocopy test to exercise the zerocopy send path on MPTCP connections by specifying "mptcp" as the test mode.

A fallback definition for IPPROTO_MPTCP (262) is provided for systems with older headers that do not include it.

Usage example:

    ./msg_zerocopy -4 -t 1 -D 127.0.0.1 mptcp
    ./msg_zerocopy -4 -t 1 -D 127.0.0.1 -z mptcp

msg_zerocopy.sh script is also updated to automatically run IPv4 and IPv6 MPTCP tests in the default test suite.

---
 tools/testing/selftests/net/msg_zerocopy.c  | 6 ++++++
 tools/testing/selftests/net/msg_zerocopy.sh | 8 +++++---
 2 files changed, 11 insertions(+), 3 deletions(-)
tools/testing/selftests/net/msg_zerocopy.c +6 -0
index 1d5d3c4e7e87..3a130f80d662 100644
@@ -74,6 +74,10 @@
 #define MSG_ZEROCOPY	0x4000000
 #endif
 
+#ifndef IPPROTO_MPTCP
+#define IPPROTO_MPTCP	262
+#endif
+
 static int  cfg_cork;
 static bool cfg_cork_mixed;
 static int  cfg_cpu		= -1;		/* default: pin to last cpu */
@@ -816,6 +820,8 @@ int main(int argc, char **argv)
 		do_test(cfg_family, SOCK_RAW, IPPROTO_RAW);
 	else if (!strcmp(cfg_test, "tcp"))
 		do_test(cfg_family, SOCK_STREAM, 0);
+	else if (!strcmp(cfg_test, "mptcp"))
+		do_test(cfg_family, SOCK_STREAM, IPPROTO_MPTCP);
 	else if (!strcmp(cfg_test, "udp"))
 		do_test(cfg_family, SOCK_DGRAM, 0);
 	else if (!strcmp(cfg_test, "rds"))
tools/testing/selftests/net/msg_zerocopy.sh +5 -3
index 28178a38a4e7..83424758c452 100755
@@ -31,6 +31,8 @@ if [[ "$#" -eq "0" ]]; then
 	$0 6 tcp -t 1 || ret=1
 	$0 4 udp -t 1 || ret=1
 	$0 6 udp -t 1 || ret=1
+	$0 4 mptcp -t 1 || ret=1
+	$0 6 mptcp -t 1 || ret=1
 
 	[[ "$ret" == "0" ]] && echo "OK. All tests passed"
 	exit $ret
@@ -38,7 +40,7 @@ fi
 
 # Argument parsing
 if [[ "$#" -lt "2" ]]; then
-	echo "Usage: $0 [4|6] [tcp|udp|raw|raw_hdrincl|packet|packet_dgram] <args>"
+	echo "Usage: $0 [4|6] [tcp|udp|mptcp|raw|raw_hdrincl|packet|packet_dgram] <args>"
 	exit 1
 fi
 
@@ -143,8 +145,8 @@ do_test() {
 		-S "${SADDR}" -D "${DADDR}" ${ARGS} "${TXMODE}" -Z 0 || ret=1
 	wait
 
-	# next test is unconnected tx to dummy0, cannot exercise with tcp
-	[[ "${TXMODE}" == "tcp" ]] && return
+	# next test is unconnected tx to dummy0, cannot exercise with tcp/mptcp
+	[[ "${TXMODE}" == "tcp" || "${TXMODE}" == "mptcp" ]] && return
 
 	# tx-only test: send out dummy0
 	# packets leaving the host are not copied,
Signature · 1 line
2.55.0
#14 lore raw

Re: [PATCH mptcp-next v9 6/7] selftests: net: msg_zerocopy: add MPTCP support

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch adds $0 4 mptcp -t 1 and $0 6 mptcp -t 1 to the default (no-argument) suite in…
- [Low] The header comment of tools/testing/selftests/net/msg_zerocopy.c lists the 'supported protocols and modes', including protocol variants…

--

selftests: net: msg_zerocopy: add MPTCP support

This patch adds an "mptcp" test mode to msg_zerocopy.c. The new mode opens SOCK_STREAM sockets with IPPROTO_MPTCP, with a fallback definition for older headers. msg_zerocopy.sh is also changed to run IPv4 and IPv6 MPTCP tests in the default test suite.

Quoted 4 lines
tools/testing/selftests/net/msg_zerocopy.c +0 -0
index 1d5d3c4e7e872..3a130f80d662f 100644

[ ... ]

tools/testing/selftests/net/msg_zerocopy.c int main(int argc, char **argv) · line 824

Quoted 6 lines
tools/testing/selftests/net/msg_zerocopy.c +2 -0
@@ -816,6 +820,8 @@ int main(int argc, char **argv)
 		do_test(cfg_family, SOCK_RAW, IPPROTO_RAW);
 	else if (!strcmp(cfg_test, "tcp"))
 		do_test(cfg_family, SOCK_STREAM, 0);
+	else if (!strcmp(cfg_test, "mptcp"))
+		do_test(cfg_family, SOCK_STREAM, IPPROTO_MPTCP);

[Severity: Low]
This isn't a bug, but should the header comment at the top of msg_zerocopy.c also list the new mode? It currently says:

* Send traffic between two processes over one of the supported
* protocols and modes:
*
* PF_INET/PF_INET6
* - SOCK_STREAM
* - SOCK_DGRAM
* - SOCK_DGRAM with UDP_CORK
* - SOCK_RAW
* - SOCK_RAW with IP_HDRINCL

Other protocol variants such as "SOCK_RAW with IP_HDRINCL" are listed there, but SOCK_STREAM with IPPROTO_MPTCP is not. The usage string in msg_zerocopy.sh was updated to include mptcp.

tools/testing/selftests/net/msg_zerocopy.sh if [[ "$#" -eq "0" ]]; then · line 35

Quoted 13 lines
tools/testing/selftests/net/msg_zerocopy.c +0 -0
 	else if (!strcmp(cfg_test, "udp"))
 		do_test(cfg_family, SOCK_DGRAM, 0);
 	else if (!strcmp(cfg_test, "rds"))
tools/testing/selftests/net/msg_zerocopy.sh +2 -0
index 28178a38a4e7b..83424758c452a 100755
@@ -31,6 +31,8 @@ if [[ "$#" -eq "0" ]]; then
 	$0 6 tcp -t 1 || ret=1
 	$0 4 udp -t 1 || ret=1
 	$0 6 udp -t 1 || ret=1
+	$0 4 mptcp -t 1 || ret=1
+	$0 6 mptcp -t 1 || ret=1

[Severity: Medium]
Will this make the whole default suite fail on kernels where MPTCP, or MPTCP SO_ZEROCOPY, is not available? Previously only tcp and udp ran here. These new cases run unconditionally, and there is no feature check or kselftest skip (exit 4).

On kernels without CONFIG_MPTCP, or for the IPv6 run without CONFIG_MPTCP_IPV6, socket() fails. The tx side hits a hard error in do_setup_tx():

	fd = socket(domain, type, protocol);
	if (fd == -1)
		error(1, errno, "socket t");

do_setup_rx() fails the same way with "socket r".

On kernels that have MPTCP but not "mptcp: handle SO_ZEROCOPY in setsockopt", the "-z" run calls do_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, 1). In that case mptcp_setsockopt_sol_socket() ends with:

	 * SO_ZEROCOPY is currently unsupported, TODO in sndmsg
	 * SO_TXTIME is currently unsupported
	 */

	return -EOPNOTSUPP;

do_setsockopt() then calls error(1, ...), and the script sets ret=1.

msg_zerocopy.sh is in the net TEST_PROGS. The last patch in the series, "selftests: mptcp: cover msg_zerocopy test", also links this script into the MPTCP selftests. Those are often run on stable and older kernels, and that patch does not add a feature check either.

Could the mptcp cases detect missing MPTCP or SO_ZEROCOPY support and report a skip instead of a failure?

Quoted 3 lines
tools/testing/selftests/net/msg_zerocopy.sh +0 -0
 	[[ "$ret" == "0" ]] && echo "OK. All tests passed"
 	exit $ret

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795591278.git.tanggeliang%40kylinos.cn
author reviewer
#15 lore raw

PATCH v9 7/7 selftests: mptcp: cover msg_zerocopy test

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Add the msg_zerocopy test to the MPTCP selftest suite by creating a symlink to the existing msg_zerocopy.sh script in the net selftests directory.

Update the Makefile to add msg_zerocopy.sh to TEST_PROGS, add msg_zerocopy binary to TEST_GEN_FILES, and add build rule to compile msg_zerocopy from ../msg_zerocopy.c. Also add msg_zerocopy binary to .gitignore.

---
 tools/testing/selftests/net/mptcp/.gitignore      | 1 +
 tools/testing/selftests/net/mptcp/Makefile        | 5 +++++
 tools/testing/selftests/net/mptcp/msg_zerocopy.sh | 1 +
 3 files changed, 7 insertions(+)
 create mode 120000 tools/testing/selftests/net/mptcp/msg_zerocopy.sh
tools/testing/selftests/net/mptcp/.gitignore +1 -0
index 833279fb34e2..1f318a297f70 100644
@@ -3,5 +3,6 @@ mptcp_connect
 mptcp_diag
 mptcp_inq
 mptcp_sockopt
+msg_zerocopy
 pm_nl_ctl
 *.pcap
tools/testing/selftests/net/mptcp/Makefile +5 -0
index 22ba0da2adb8..17d0d1051165 100644
@@ -14,6 +14,7 @@ TEST_PROGS := \
 	mptcp_connect_splice.sh \
 	mptcp_join.sh \
 	mptcp_sockopt.sh \
+	msg_zerocopy.sh \
 	pm_netlink.sh \
 	simult_flows.sh \
 	userspace_pm.sh \
@@ -24,6 +25,7 @@ TEST_GEN_FILES := \
 	mptcp_diag \
 	mptcp_inq \
 	mptcp_sockopt \
+	msg_zerocopy \
 	pm_nl_ctl \
 # end of TEST_GEN_FILES
 
@@ -37,3 +39,6 @@ TEST_INCLUDES := ../lib.sh $(wildcard ../lib/sh/*.sh)
 EXTRA_CLEAN := *.pcap
 
 include ../../lib.mk
+
+$(OUTPUT)/msg_zerocopy: ../msg_zerocopy.c
+	$(CC) $< $(CFLAGS) $(CPPFLAGS) -o $@
tools/testing/selftests/net/mptcp/msg_zerocopy.sh new file +1 -0
new file mode 120000
index 000000000000..b2f82b8fa5f2
@@ -0,0 +1 @@
+../msg_zerocopy.sh
\ No newline at end of file
Signature · 1 line
2.55.0
#16 lore raw

Re: [PATCH mptcp-next v9 7/7] selftests: mptcp: cover msg_zerocopy test

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests: mptcp: missing CONFIG_DUMMY in config for msg_zerocopy.sh --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests: mptcp: missing CONFIG_DUMMY in config for msg_zerocopy.sh
--

commit 5b3ee281d765d4c4bfa0409eafb89f384c870ea5
Author: Geliang Tang <[email protected]>

selftests: mptcp: cover msg_zerocopy test

This commit adds the msg_zerocopy test to the MPTCP selftest suite by symlinking ../msg_zerocopy.sh and building msg_zerocopy from ../msg_zerocopy.c.

tools/testing/selftests/net/mptcp/Makefile TEST_PROGS := \ · line 20

Quoted 12 lines
tools/testing/selftests/net/mptcp/Makefile +1 -0
index 22ba0da2adb85..17d0d1051165c 100644
@@ -14,6 +14,7 @@ TEST_PROGS := \
 	mptcp_connect_splice.sh \
 	mptcp_join.sh \
 	mptcp_sockopt.sh \
+	msg_zerocopy.sh \
 	pm_netlink.sh \
 	simult_flows.sh \
 	userspace_pm.sh \

[Severity: Medium]
Does tools/testing/selftests/net/mptcp/config also need CONFIG_DUMMY=y?

msg_zerocopy.sh runs under set -e and unconditionally creates a dummy network device with "ip link add ... type dummy" for every subtest. Unlike tools/testing/selftests/net/config, the mptcp config only enables CONFIG_VETH=y and omits CONFIG_DUMMY, so running the mptcp selftests on a kernel built from tools/testing/selftests/net/mptcp/config will fail when msg_zerocopy.sh tries to create dummy0.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#17 lore raw

Re: [PATCH mptcp-next v9 7/7] selftests: mptcp: cover msg_zerocopy test

In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 3

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] This patch adds msg_zerocopy.sh to the MPTCP selftest target (tools/testing/selftests/net/mptcp/Makefile, TEST_PROGS, line 17), but…
- [Low] The same test and binary are now registered twice, and the MPTCP- target copy cannot be limited to MPTCP cases.
- [Low] The symlinked msg_zerocopy.sh breaks the MPTCP suite's feature- detection convention.
- [Low] The custom recipe added for $(OUTPUT)/msg_zerocopy in tools/testing/selftests/net/mptcp/Makefile (lines 43-44) is `$(CC) $<…

--

selftests: mptcp: cover msg_zerocopy test

This commit adds the msg_zerocopy test to the MPTCP selftest suite through a symlink to the existing net/msg_zerocopy.sh script. It also adds the msg_zerocopy binary to TEST_GEN_FILES, with a custom rule that builds it from ../msg_zerocopy.c, and updates .gitignore.

tools/testing/selftests/net/mptcp/Makefile TEST_PROGS := \ · line 17

Quoted 9 lines
tools/testing/selftests/net/mptcp/Makefile +1 -0
index 22ba0da2adb85..17d0d1051165c 100644
@@ -14,6 +14,7 @@ TEST_PROGS := \
 	mptcp_connect_splice.sh \
 	mptcp_join.sh \
 	mptcp_sockopt.sh \
+	msg_zerocopy.sh \

[Severity: Medium]
The shared script runs under set -e. For every mode, including tcp and mptcp, it creates a dummy device before it reaches do_test():

tools/testing/selftests/net/msg_zerocopy.sh:

    set -e
    ...
    ip link add "${DUMMY_DEV}" mtu "${DEV_MTU}" netns "${NS2}" type dummy

tools/testing/selftests/net/mptcp/config only has CONFIG_VETH=y, and CONFIG_DUMMY has no default. Take a kernel built from defconfig plus the mptcp fragment. There this ip link add fails and set -e aborts each of the six child runs. The test then reports FAIL without exercising MPTCP zerocopy.

The net/ target only works because tools/testing/selftests/net/config has CONFIG_DUMMY=y.

Does net/mptcp/config need CONFIG_DUMMY=m as part of this change?

[Severity: Low]
Is this registration redundant with the existing one in tools/testing/selftests/net/Makefile?

net/Makefile already lists msg_zerocopy.sh in TEST_PROGS and msg_zerocopy in TEST_GEN_FILES, and net/config has CONFIG_MPTCP=y. Since the previous patch in this series ("selftests: net: msg_zerocopy: add MPTCP support"), the no-argument path already covers mptcp:

tools/testing/selftests/net/msg_zerocopy.sh:

    if [[ "$#" -eq "0" ]]; then
    	ret=0

    	$0 4 tcp -t 1 || ret=1
    	$0 6 tcp -t 1 || ret=1
    	$0 4 udp -t 1 || ret=1
    	$0 6 udp -t 1 || ret=1
    	$0 4 mptcp -t 1 || ret=1
    	$0 6 mptcp -t 1 || ret=1

kselftest runs TEST_PROGS with no arguments, so the mptcp target runs the full tcp/udp/mptcp matrix. That has two effects:

- A TCP or UDP zerocopy regression shows up as an MPTCP suite failure.
- With TARGETS="net net/mptcp", the script runs twice and the binary is built twice.

Would it be better to keep the test in net/ only? Another option is a small mptcp wrapper that calls the script with just "4 mptcp" and "6 mptcp".

[ ... ]

tools/testing/selftests/net/mptcp/Makefile TEST_INCLUDES := ../lib.sh $(wildcard ../lib/sh/*.sh) · line 44

Quoted 7 lines
tools/testing/selftests/net/mptcp/Makefile +3 -0
@@ -37,3 +39,6 @@ TEST_INCLUDES := ../lib.sh $(wildcard ../lib/sh/*.sh)
 EXTRA_CLEAN := *.pcap
 
 include ../../lib.mk
+
+$(OUTPUT)/msg_zerocopy: ../msg_zerocopy.c
+	$(CC) $< $(CFLAGS) $(CPPFLAGS) -o $@

[Severity: Low]
This recipe leaves out $(LDFLAGS), $(TARGET_ARCH) and $(LDLIBS). lib.mk uses all three for the other binaries in this directory:

tools/testing/selftests/lib.mk:

    LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
    ...
    $(OUTPUT)/%:%.c $(LOCAL_HDRS)
    	$(call msg,CC,,$@)
    	$(Q)$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@

With make TARGETS=net/mptcp LDFLAGS=-static, mptcp_connect and the other binaries would be linked statically. Would msg_zerocopy still end up dynamically linked?

Would $(LINK.c) $^ $(LDLIBS) -o $@, with $(Q) and the msg helper, be a closer match to lib.mk?

tools/testing/selftests/net/mptcp/msg_zerocopy.sh line 1

Quoted 8 lines
tools/testing/selftests/net/mptcp/msg_zerocopy.sh new file +1 -0
new file mode 120000
index 0000000000000..b2f82b8fa5f2b
@@ -0,0 +1 @@
+../msg_zerocopy.sh
\ No newline at end of file

[Severity: Low]
Every other script in the mptcp TEST_PROGS sources mptcp_lib.sh and calls mptcp_lib_check_mptcp(). That helper skips the test when MPTCP is not available:

tools/testing/selftests/net/mptcp/mptcp_lib.sh:

    mptcp_lib_check_mptcp() {
    	if ! mptcp_lib_has_file "/proc/sys/net/mptcp/enabled"; then
    		mptcp_lib_pr_skip "MPTCP support is not available"
    		exit ${KSFT_SKIP}
    	fi
    }

The symlinked script has no such check. On a kernel without CONFIG_MPTCP, socket(..., IPPROTO_MPTCP) fails in do_setup_tx() and do_setup_rx(), and msg_zerocopy calls error(1, errno, "socket t").

Some kernels have MPTCP but not the "mptcp: handle SO_ZEROCOPY in setsockopt" change. On those, do_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY,
1) fails and also calls error(1, ...).

The rest of the MPTCP suite reports SKIP for missing features when run against older or stable kernels. Should these cases report SKIP too, instead of FAIL?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795591278.git.tanggeliang%40kylinos.cn
#18 lore raw
In reply to Geliang Tang

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Unstable: 2 failed test(s): packetdrill_regressions selftest_msg_zerocopy ⚠️
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Unstable: 1 failed test(s): selftest_msg_zerocopy ⚠️
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Perf: Unstable: 5 failed test(s): regression:sched-baseline(7) regression:sched-bbr(5) regression:sched-clientlim regression:sched-mem-pressure validation:sched-loss(2) ⚠️ (3 improvements, 56 new outliers to review)
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/45570779969

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/95b50a4b8e72
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1221715

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)