mptcp: fixes for SIOCINQ
Subsystems (2)
Geliang Tang
reviewercover mptcp: fixes for SIOCINQ
From: Geliang Tang <[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 introduces a mptcp_data_fin_seq() helper 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: handle ack_seq advancing past rcv_data_fin_seq
mptcp: add missing memory barriers for rcv_data_fin_seq
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
mptcp: add mptcp_inq() for SIOCINQ
net/mptcp/options.c | 1 + net/mptcp/protocol.c | 55 +++++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 16 deletions(-)
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH 1/4 mptcp: handle ack_seq advancing past rcv_data_fin_seq
From: Geliang Tang <[email protected]>
Change mptcp_pending_data_fin() to check if ack_seq >= rcv_data_fin_seq instead of ack_seq == rcv_data_fin_seq. This handles the case where out-of-order segments in the OFO queue advance ack_seq past rcv_data_fin_seq before mptcp_pending_data_fin() runs.
With the strict equality check, if ack_seq advances past rcv_data_fin_seq due to OFO processing, the DATA_FIN would never be processed. In ESTABLISHED state, this causes read() to never return EOF since RCV_SHUTDOWN is not set. In FIN_WAIT1 or TCP_FIN_WAIT2, the connection hangs until the close timer expires.
The new check using !before64(ack_seq, rcv_data_fin_seq) ensures DATA_FIN is processed even when ack_seq has moved past it.
--- net/mptcp/protocol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/protocol.c +1 -1
@@ -569,7 +569,7 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {u64 rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); -if (READ_ONCE (msk->ack_seq) == rcv_data_fin_seq) { +if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) {if (seq) *seq = rcv_data_fin_seq;
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH 2/4 mptcp: add missing memory barriers for rcv_data_fin_seq
In reply to Geliang TangFrom: 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.
--- net/mptcp/options.c | 1 + net/mptcp/protocol.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
net/mptcp/options.c +1 -0
@@ -1192,6 +1192,7 @@ bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool usWRITE_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
@@ -567,8 +567,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 (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) {if (seq) *seq = rcv_data_fin_seq;
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH 3/4 mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
In reply to Geliang TangFrom: 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_seq() helper. The FIN byte (+1) is now guarded by it, so the hint only counts a FIN when a DATA_FIN was actually received. Without this guard, local shutdown(SHUT_RD) and MP_FASTCLOSE (which set RCV_SHUTDOWN or TCP_CLOSE without a DATA_FIN) would incorrectly inflate the hint by 1. This means with N bytes unread after DATA_FIN, both SIOCINQ and TCP_CM_INQ report N+1 when rcv_data_fin_seq is set, or N otherwise. This differs from TCP, which always reports N+1 after FIN because the FIN consumes a sequence number.
Also reset rcv_data_fin_seq to 0 in mptcp_disconnect().
--- net/mptcp/protocol.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-)
net/mptcp/protocol.c +19 -13
@@ -2196,6 +2196,18 @@ static void mptcp_eat_recv_skb(struct sock *sk, struct sk_buff *skb)skb_attempt_defer_free (skb); } +static u64 mptcp_data_fin_seq (const struct sock *sk) +{ +const struct mptcp_sock *msk =mptcp_sk (sk); + +if (!(READ_ONCE (msk->rcv_data_fin) || + sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN ))) +return 0 ; + +smp_rmb ();/* pair with smp_wmb() in mptcp_update_rcv_data_fin() */ +return READ_ONCE (msk->rcv_data_fin_seq); +} + static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg, size_t len, int flags, int copied_total, struct scm_timestamping_internal *tss,
@@ -2407,22 +2419,15 @@ 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; - - skb =skb_peek (&sk->sk_receive_queue); -if (skb) { -u64 hint_val =READ_ONCE (msk->ack_seq) -MPTCP_SKB_CB (skb)->map_seq; +u64 hint_val =mptcp_data_avail (msk); -if (hint_val >=INT_MAX ) -return INT_MAX ; +if (hint_val >=INT_MAX ) +return INT_MAX ; -return (unsigned int )hint_val; - } +if (mptcp_data_fin_seq (sk)) + hint_val +=1 ; -if (sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) -return 1 ; - -return 0 ; +return (unsigned int )hint_val; } static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
@@ -3682,6 +3687,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
[email protected]
AI reviewRe: [PATCH mptcp-net 3/4] mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mptcp: premature FIN byte accounting in mptcp_data_fin_seq() - [Medium] mptcp: zero…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mptcp: premature FIN byte accounting in mptcp_data_fin_seq()
- [Medium] mptcp: zero rcv_data_fin_seq missed in mptcp_inq_hint() on TCP fallback
--
commit 7f3583a035d8da6607d3d80aed0de39c52b747e0
Author: Geliang Tang <[email protected]>
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
This commit updates mptcp_inq_hint() to compute available bytes via mptcp_data_avail() and adds mptcp_data_fin_seq() to account for the received DATA_FIN byte.
Quoted 9 lines
net/mptcp/protocol.c +1
-0@@ -2154,6 +2154,18 @@ static void mptcp_eat_recv_skb(struct sock *sk, struct sk_buff *skb)skb_attempt_defer_free (skb); } +static u64 mptcp_data_fin_seq (const struct sock *sk)
net/mptcp/protocol.c static void mptcp_eat_recv_skb(struct sock *sk, struct sk_buff *skb) · line 2167
Quoted 10 lines
net/mptcp/protocol.c +10
-0+{ +const struct mptcp_sock *msk =mptcp_sk (sk); + +if (!(READ_ONCE (msk->rcv_data_fin) || + sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN ))) +return 0 ; + +smp_rmb ();/* pair with smp_wmb() in mptcp_update_rcv_data_fin() */ +return READ_ONCE (msk->rcv_data_fin_seq); +}
[Severity: Medium]
Does mptcp_data_fin_seq() report the FIN before all preceding data has arrived in order?
get_mapping_status() calls mptcp_update_rcv_data_fin() as soon as a subflow skb carrying DATA_FIN reaches the head of ssk->sk_receive_queue, even if that mapping arrived out of order while earlier DSN data is still in flight on another subflow (or on the first segment of a multi-segment DSS mapping carrying DATA_FIN). Unlike mptcp_pending_data_fin(), mptcp_data_fin_seq() does not check !before64(READ_ONCE(msk->ack_seq), rcv_data_fin_seq).
While msk->ack_seq is still behind msk->rcv_data_fin_seq, mptcp_inq_hint() adds 1 for the FIN before RCV_SHUTDOWN is set, so SIOCINQ and TCP_CM_INQ report N + 1 when only N in-order bytes are readable before the gap (or 1 when a non-blocking recvmsg() will return -EAGAIN).
Quoted 17 lines
net/mptcp/protocol.c +3
-8@@ -2364,22 +2376,15 @@ 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; - - skb =skb_peek (&sk->sk_receive_queue); -if (skb) { -u64 hint_val =READ_ONCE (msk->ack_seq) -MPTCP_SKB_CB (skb)->map_seq; +u64 hint_val =mptcp_data_avail (msk); -if (hint_val >=INT_MAX ) -return INT_MAX ; +if (hint_val >=INT_MAX ) +return INT_MAX ; -return (unsigned int )hint_val;
net/mptcp/protocol.c static bool mptcp_move_skbs(struct sock *sk) · line 2388
Quoted 10 lines
net/mptcp/protocol.c +3
-5- } +if (mptcp_data_fin_seq (sk)) + hint_val +=1 ; -if (sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) -return 1 ; - -return 0 ; +return (unsigned int )hint_val; }
[Severity: Medium]
Can mptcp_inq_hint() miss the FIN byte when msk->rcv_data_fin_seq is 0 on a TCP fallback connection?
When an active connection falls back to plain TCP in subflow_finish_connect(), subflow_set_remote_key() is not called, leaving subflow->iasn, subflow->map_seq and msk->ack_seq at 0. If the peer closes the connection without sending any payload, subflow_sched_work_if_closed() records the dummy DATA_FIN with sequence 0:
net/mptcp/subflow.c:subflow_sched_work_if_closed() {
...
if (__mptcp_check_fallback(msk) && subflow_is_done(ssk) &&
msk->first == ssk &&
mptcp_update_rcv_data_fin(msk, subflow->map_seq +
subflow->map_data_len, true))
mptcp_schedule_work(sk);
}Because msk->rcv_data_fin_seq is 0, mptcp_data_fin_seq(sk) returns 0 and mptcp_inq_hint() returns 0 instead of 1 after the peer's FIN.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Geliang Tang
reviewerPATCH 4/4 mptcp: add mptcp_inq() for SIOCINQ
In reply to Geliang TangFrom: 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.
--- net/mptcp/protocol.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +16 -1
@@ -4185,6 +4185,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);
@@ -4198,7 +4213,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
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): Success! ✅
- 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-bbr(36) regression:sched-mem-pressure regression:sched-rmem(3) regression:sched-serverlim(7) validation:sched-loss ⚠️ (7 improvements, 105 new outliers to review)
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/44983059580
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/ea481c424b80
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1218409
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-normalFor 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)