MPTCP Linux Development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Geliang Tang" <geliang@kernel.org>, mptcp@lists.linux.dev
Subject: Re: [PATCH mptcp-next v9 3/7] mptcp: add MSG_ZEROCOPY support
Date: Wed, 25 Nov 2026 07:52:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-232845@kernel.org> (raw)
In-Reply-To: <fef46d879cfe64c34ee1d792a5fd5713981f9ea4.1795591278.git.tanggeliang@kylinos.cn>

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 <geliang@kernel.org>

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.

> diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
> index 504d0b196d84b..6bd10a4e1e938 100644
> --- a/net/mptcp/protocol.c
> +++ b/net/mptcp/protocol.c
[ ... ]
> @@ -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);

[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.

[ ... ]

> @@ -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)
> +{
[ ... ]
> +	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);

[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.

> +
> +	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);

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795591278.git.tanggeliang@kylinos.cn?part=3

  reply	other threads:[~2026-11-25  7:52 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  7:22 [PATCH mptcp-next v9 0/7] mptcp: add MSG_ZEROCOPY support Geliang Tang
2026-11-25  7:22 ` [PATCH mptcp-next v9 1/7] mptcp: use local variable tp in sendmsg_frag Geliang Tang
2026-11-25  7:23 ` [PATCH mptcp-next v9 2/7] mptcp: remove redundant orig_offset in carve_data_frag Geliang Tang
2026-11-25  7:23 ` [PATCH mptcp-next v9 3/7] mptcp: add MSG_ZEROCOPY support Geliang Tang
2026-11-25  7:52   ` sashiko-bot [this message]
2026-11-25  7:23 ` [PATCH mptcp-next v9 4/7] mptcp: handle SO_ZEROCOPY in setsockopt Geliang Tang
2026-11-25  7:44   ` sashiko-bot
2026-11-25  7:23 ` [PATCH mptcp-next v9 5/7] Documentation: msg_zerocopy: add MPTCP support Geliang Tang
2026-11-25  7:23 ` [PATCH mptcp-next v9 6/7] selftests: net: " Geliang Tang
2026-11-25  7:23 ` [PATCH mptcp-next v9 7/7] selftests: mptcp: cover msg_zerocopy test Geliang Tang
2026-11-25  7:34   ` sashiko-bot
2026-11-25  9:39 ` [PATCH mptcp-next v9 0/7] mptcp: add MSG_ZEROCOPY support MPTCP CI

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=sashiko-outbox-232845@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=geliang@kernel.org \
    --cc=mptcp@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

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

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