Skip to contentSkip to conversation

mptcp: fixes for SIOCINQ

PATCH In review Geliang Tang 3 participants 8 messages Nov 28

Subsystems (2)
lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v4 cover mptcp: fixes for SIOCINQ

From: Geliang Tang <[email protected]>

v4:
- drop patch 1 "handle ack_seq advancing past rcv_data_fin_seq": revert '!before64()' back to '==' in mptcp_pending_data_fin(), the OFO queue edge case is not a real issue
- add patch 2 "use SOCK_DONE in mptcp_inq_hint": replace the TCP_CLOSE/RCV_SHUTDOWN check with sock_flag(sk, SOCK_DONE), aligning with tcp_inq_hint()
- patch 3: use sock_flag(sk, SOCK_DONE) to add FIN byte instead of checking TCP_CLOSE/RCV_SHUTDOWN and rcv_fastclose, simplifying the condition.

v3:
- patch 3: fix Sashiko AI review comment
avoid comparing ack_seq with unset rcv_data_fin_seq (0)
check TCP_CLOSE/RCV_SHUTDOWN directly, only call mptcp_data_fin() when rcv_data_fin is true, exclude MP_FASTCLOSE from +1
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v2:
- patch 3: drop mptcp_data_fin_seq(), reuse mptcp_data_fin()
- u64 return couldn't distinguish "no DATA_FIN" from "DATA_FIN at seq 0"
- mptcp_inq_hint() checks rcv_data_fin/TCP_CLOSE/RCV_SHUTDOWN first
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

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

The goal of this series is to make MPTCP's SIOCINQ fully consistent with TCP's: both report the number of readable data bytes, excluding the FIN byte.

Patch 1 fixes mptcp_pending_data_fin() to use >= instead of == so that DATA_FIN is processed when ack_seq advances past rcv_data_fin_seq due to OFO queue processing.

Patch 2 adds missing smp_wmb/smp_rmb barriers for rcv_data_fin_seq on weakly-ordered architectures.

Patch 3 fixes mptcp_inq_hint() INT_MAX overflow on passive TFO by using mptcp_data_avail() instead of ack_seq minus map_seq, and reuses mptcp_data_fin() to guard the FIN byte (+1).

Patch 4 adds mptcp_inq() that excludes the FIN byte, matching TCP's tcp_inq() behavior, and switches SIOCINQ to it.

Geliang Tang (4):
mptcp: add missing memory barriers for rcv_data_fin_seq
mptcp: use SOCK_DONE in mptcp_inq_hint
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
mptcp: add mptcp_inq() for SIOCINQ

 net/mptcp/options.c  |  1 +
 net/mptcp/protocol.c | 62 ++++++++++++++++++++++++++++----------------
 2 files changed, 41 insertions(+), 22 deletions(-)
Signature · 1 line
2.55.0
author reviewer
#1 lore raw

PATCH v4 1/4 mptcp: add missing memory barriers for rcv_data_fin_seq

From: Geliang Tang <[email protected]>

mptcp_update_rcv_data_fin() writes rcv_data_fin_seq before setting rcv_data_fin, but there is no write barrier to ensure the ordering is visible to concurrent readers.

mptcp_pending_data_fin() checks rcv_data_fin and then reads rcv_data_fin_seq, but has no read barrier. On weakly-ordered architectures, the reader could observe rcv_data_fin == 1 with a stale (or zero) rcv_data_fin_seq, causing mptcp_check_data_fin() to either process the DATA_FIN prematurely or skip it entirely.

Add smp_wmb() in mptcp_update_rcv_data_fin() between the two writes and a matching smp_rmb() in mptcp_pending_data_fin() between the two reads.

Fixes 3721b9b64676 ("mptcp: Track received DATA_FIN sequence number and add related helpers")
---
 net/mptcp/options.c  | 1 +
 net/mptcp/protocol.c | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
net/mptcp/options.c +1 -0
index 05b9c66aaed6..7c5e748105ea 100644
@@ -1204,6 +1204,7 @@ bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool us
 
 	WRITE_ONCE(msk->rcv_data_fin_seq,
 		   mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
+	smp_wmb(); /* ensure rcv_data_fin_seq is visible before rcv_data_fin */
 	WRITE_ONCE(msk->rcv_data_fin, 1);
 
 	return true;
net/mptcp/protocol.c +3 -1
index e27eddcfa758..4dad5da12680 100644
@@ -573,8 +573,10 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
 	if (READ_ONCE(msk->rcv_data_fin) &&
 	    ((1 << inet_sk_state_load(sk)) &
 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
-		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
+		u64 rcv_data_fin_seq;
 
+		smp_rmb(); /* pair with smp_wmb in mptcp_update_rcv_data_fin */
+		rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
 		if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
 			if (seq)
 				*seq = rcv_data_fin_seq;
Signature · 1 line
2.55.0
author reviewer
#2 lore raw

PATCH v4 2/4 mptcp: use SOCK_DONE in mptcp_inq_hint

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

mptcp_inq_hint() checks sk_state == TCP_CLOSE and RCV_SHUTDOWN to decide whether to return a non-zero hint when the receive queue is empty. This is redundant: SOCK_DONE is the canonical indicator that no more data will arrive, and it is set in all the relevant paths - __mptcp_data_fin_incoming(), __mptcp_subflow_error_report(), and __mptcp_close().

Switch to sock_flag(sk, SOCK_DONE) to align with tcp_inq_hint() and simplify the check.

---
 net/mptcp/protocol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/protocol.c +1 -1
index 4dad5da12680..be08a0abd197 100644
@@ -2641,7 +2641,7 @@ static unsigned int mptcp_inq_hint(const struct sock *sk)
 		return (unsigned int)hint_val;
 	}
 
-	if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
+	if (sock_flag(sk, SOCK_DONE))
 		return 1;
 
 	return 0;
Signature · 1 line
2.55.0
author reviewer
#4 lore raw

PATCH v4 3/4 mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Fix mptcp_inq_hint() to compute available bytes using mptcp_data_avail() (bytes_received - bytes_consumed) instead of ack_seq minus the head skb's map_seq.

On passive TFO connections, the TFO skb has map_seq set to -skb->len. Without the removed __mptcp_fastopen_gen_msk_ackseq() to adjust the TFO skb's map_seq, mptcp_inq_hint() computes "ack_seq - map_seq = iasn + len", almost always >= INT_MAX, causing SIOCINQ/FIONREAD and TCP_CM_INQ to report INT_MAX. This also breaks receive buffer autotuning, as mptcp_rcv_space_adjust() uses mptcp_inq_hint() to measure consumed data. The bogus hint prevents mptcp_rcvbuf_grow() from being called until the entire TFO skb has been read.

Using mptcp_data_avail() avoids this by tracking bytes directly. Introduce a mptcp_data_fin() helper to check if ack_seq has passed rcv_data_fin_seq. The FIN byte (+1) is added for TCP_CLOSE or RCV_SHUTDOWN (excluding MP_FASTCLOSE), or for DATA_FIN when rcv_data_fin is set and ack_seq has caught up. Also reset rcv_data_fin_seq to 0 in mptcp_disconnect().

Fixes f03afb3aeb9d ("mptcp: drop __mptcp_fastopen_gen_msk_ackseq()")
---
 net/mptcp/protocol.c | 47 ++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 23 deletions(-)
net/mptcp/protocol.c +24 -23
index be08a0abd197..7d090ada5ebd 100644
@@ -565,6 +565,21 @@ static void mptcp_check_data_fin_ack(struct sock *sk)
 	}
 }
 
+static bool mptcp_data_fin(const struct mptcp_sock *msk, u64 *seq)
+{
+	u64 rcv_data_fin_seq;
+
+	smp_rmb(); /* pair with smp_wmb() in mptcp_update_rcv_data_fin() */
+	rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
+	if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
+		if (seq)
+			*seq = rcv_data_fin_seq;
+
+		return true;
+	}
+	return false;
+}
+
 /* can be called with no lock acquired */
 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
 {
@@ -573,16 +588,7 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
 	if (READ_ONCE(msk->rcv_data_fin) &&
 	    ((1 << inet_sk_state_load(sk)) &
 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
-		u64 rcv_data_fin_seq;
-
-		smp_rmb(); /* pair with smp_wmb in mptcp_update_rcv_data_fin */
-		rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
-		if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
-			if (seq)
-				*seq = rcv_data_fin_seq;
-
-			return true;
-		}
+		return mptcp_data_fin(msk, seq);
 	}
 
 	return false;
@@ -2629,22 +2635,16 @@ static bool mptcp_move_skbs(struct sock *sk)
 static unsigned int mptcp_inq_hint(const struct sock *sk)
 {
 	const struct mptcp_sock *msk = mptcp_sk(sk);
-	const struct sk_buff *skb;
+	u64 hint_val = mptcp_data_avail(msk);
 
-	skb = skb_peek(&sk->sk_receive_queue);
-	if (skb) {
-		u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
+	if (hint_val >= INT_MAX)
+		return INT_MAX;
 
-		if (hint_val >= INT_MAX)
-			return INT_MAX;
+	if (sock_flag(sk, SOCK_DONE) ||
+	    (READ_ONCE(msk->rcv_data_fin) && mptcp_data_fin(msk, NULL)))
+		hint_val += 1;
 
-		return (unsigned int)hint_val;
-	}
-
-	if (sock_flag(sk, SOCK_DONE))
-		return 1;
-
-	return 0;
+	return (unsigned int)hint_val;
 }
 
 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
@@ -4065,6 +4065,7 @@ static int mptcp_disconnect(struct sock *sk, int flags)
 	WRITE_ONCE(msk->can_ack, false);
 	WRITE_ONCE(msk->fully_established, false);
 	WRITE_ONCE(msk->rcv_data_fin, false);
+	WRITE_ONCE(msk->rcv_data_fin_seq, 0);
 	WRITE_ONCE(msk->snd_data_fin_enable, false);
 	WRITE_ONCE(msk->rcv_fastclose, false);
 	WRITE_ONCE(msk->use_64bit_ack, false);
Signature · 1 line
2.55.0
author reviewer
#6 lore raw

PATCH v4 4/4 mptcp: add mptcp_inq() for SIOCINQ

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Add mptcp_inq() to compute readable bytes at the MPTCP level using mptcp_data_avail() (bytes_received - bytes_consumed). It returns 0 during handshake (TCPF_SYN_SENT/TCPF_SYN_RECV), INT_MAX when avail >= INT_MAX, otherwise the actual byte count.

Unlike mptcp_inq_hint(), mptcp_inq() does not include the FIN byte. Switch SIOCINQ from mptcp_inq_hint() to mptcp_inq() so that FIONREAD reports N (not N+1) when N bytes are unread with DATA_FIN pending, matching TCP's tcp_ioctl() which subtracts the FIN byte.

Fixes 644807e3e462 ("mptcp: add SIOCINQ, OUTQ and OUTQNSD ioctls")
---
 net/mptcp/protocol.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +16 -1
index 7d090ada5ebd..bc61ac827c12 100644
@@ -4568,6 +4568,21 @@ static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
 	return (int)delta;
 }
 
+static int mptcp_inq(struct sock *sk)
+{
+	const struct mptcp_sock *msk = mptcp_sk(sk);
+	u64 avail;
+
+	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
+		return 0;
+
+	avail = mptcp_data_avail(msk);
+	if (avail >= INT_MAX)
+		return INT_MAX;
+
+	return (unsigned int)avail;
+}
+
 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -4581,7 +4596,7 @@ static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
 		lock_sock(sk);
 		if (mptcp_move_skbs(sk))
 			mptcp_cleanup_rbuf(msk, 0);
-		*karg = mptcp_inq_hint(sk);
+		*karg = mptcp_inq(sk);
 		release_sock(sk);
 		break;
 	case SIOCOUTQ:
Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH mptcp-net v4 2/4] mptcp: use SOCK_DONE in mptcp_inq_hint

In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mptcp: inaccurate SOCK_DONE setters and behavior description in commit message --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mptcp: inaccurate SOCK_DONE setters and behavior description in commit message
--

commit 29686dc34959b6d287ae3e6764bcdbaa2489bcad
Author: Geliang Tang <[email protected]>

mptcp: use SOCK_DONE in mptcp_inq_hint

This commit switches mptcp_inq_hint() from checking TCP_CLOSE and RCV_SHUTDOWN to checking SOCK_DONE when the receive queue is empty.

Quoted 6 lines

mptcp_inq_hint() checks sk_state == TCP_CLOSE and RCV_SHUTDOWN to decide whether to return a non-zero hint when the receive queue is empty. This is redundant: SOCK_DONE is the canonical indicator that no more data will arrive, and it is set in all the relevant paths - __mptcp_data_fin_incoming(), __mptcp_subflow_error_report(), and __mptcp_close().

[Severity: Low]
Is the commit message accurate about where SOCK_DONE is set and how this changes mptcp_inq_hint()?

There is no __mptcp_data_fin_incoming() function; incoming DATA_FIN (or fallback TCP FIN) sets SOCK_DONE in mptcp_check_data_fin(). Also, __mptcp_close() sets sk->sk_shutdown to SHUTDOWN_MASK and orphans the socket, but does not set SOCK_DONE.

Because SOCK_DONE is only set when a peer DATA_FIN or fallback FIN is received, this is not just removing a redundant check: on an empty receive queue after a local shutdown(SHUT_RD), on a closed or unconnected socket, or after an abortive MP_FASTCLOSE or fallback reset without a prior FIN, mptcp_inq_hint() now returns 0 instead of 1 (matching tcp_inq_hint()).

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

Re: [PATCH mptcp-net v4 3/4] mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint

In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mptcp: spurious FIN byte in mptcp_inq_hint() on fallback TCP reset --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mptcp: spurious FIN byte in mptcp_inq_hint() on fallback TCP reset
--

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

mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint

This commit updates mptcp_inq_hint() to compute readable bytes from mptcp_data_avail() and adds a mptcp_data_fin() helper to account for a pending FIN byte.

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index 52b5b6d2f1ce9..69facb6a86f04 100644

[ ... ]

Quoted 18 lines
net/mptcp/protocol.c +6 -6
@@ -2437,22 +2443,16 @@ static bool mptcp_move_skbs(struct sock *sk)
 static unsigned int mptcp_inq_hint(const struct sock *sk)
 {
 	const struct mptcp_sock *msk = mptcp_sk(sk);
-	const struct sk_buff *skb;
+	u64 hint_val = mptcp_data_avail(msk);
 
-	skb = skb_peek(&sk->sk_receive_queue);
-	if (skb) {
-		u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
+	if (hint_val >= INT_MAX)
+		return INT_MAX;
 
-		if (hint_val >= INT_MAX)
-			return INT_MAX;
+	if (sock_flag(sk, SOCK_DONE) ||
+	    (READ_ONCE(msk->rcv_data_fin) && mptcp_data_fin(msk, NULL)))
+		hint_val += 1;

net/mptcp/protocol.c static bool mptcp_move_skbs(struct sock *sk) · line 2456

Quoted 10 lines
net/mptcp/protocol.c +1 -7
 
-		return (unsigned int)hint_val;
-	}
-
-	if (sock_flag(sk, SOCK_DONE))
-		return 1;
-
-	return 0;
+	return (unsigned int)hint_val;
 }

[Severity: Medium]
Can mptcp_inq_hint() add a spurious FIN byte when a fallback connection is closed by a TCP RST?

On fallback, subflow_sched_work_if_closed() sets msk->rcv_data_fin whenever the subflow enters TCP_CLOSE, including after receiving a TCP RST without a prior FIN. mptcp_check_data_fin() and __mptcp_subflow_error_report() both check sock_flag(ssk, SOCK_DONE) before setting SOCK_DONE on sk, and __mptcp_subflow_error_report() moves sk to TCP_CLOSE without clearing msk->rcv_data_fin:

net/mptcp/protocol.c:mptcp_check_data_fin() {
    ...
		/* on fallback, also emulated after a reset: check for a FIN */
		if (!__mptcp_check_fallback(msk) ||
		    (msk->first && sock_flag(msk->first, SOCK_DONE)))
			sock_set_flag(sk, SOCK_DONE);
    ...
}

Because mptcp_inq_hint() checks msk->rcv_data_fin and mptcp_data_fin() directly without verifying the socket state or whether a fallback subflow actually received a FIN, TCP_CM_INQ (and mptcp_rcv_space_adjust()) will count an extra FIN byte that was never received.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 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): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Unstable: 1 failed test(s): selftest_diag ⚠️
- 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): error:sched-mem-pressure regression:sched-bbr regression:sched-rmem(3) regression:sched-serverlim(2) validation:sched-loss ⚠️ (5 improvements, 21 new outliers to review) | Success! ✅ (1 improvements, 15 new outliers to review) | Success! ✅ (10 new outliers to review) | Success! ✅ (7 new outliers to review)
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/46139517807

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

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)