MPTCP Linux Development
 help / color / mirror / Atom feed
From: Geliang Tang <geliang@kernel.org>
To: sashiko-reviews@lists.linux.dev,
	"Matthieu Baerts (NGI0)" <matttbe@kernel.org>,
	MPTCP Linux <mptcp@lists.linux.dev>
Subject: Re: [PATCH mptcp-next mptcp-net v7 6/6] mptcp: count the DATA_FIN in mptcp_inq_hint
Date: Fri, 04 Dec 2026 18:55:52 +0800	[thread overview]
Message-ID: <0ac04e420bb265f1f57e8ecf0961a309ace105ff.camel@kernel.org> (raw)
In-Reply-To: <sashiko-outbox-248428@kernel.org>

Hi Matt,

Thank you very much for helping me send this v7.

On Thu, 2026-12-03 at 18:50 +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential
> issues to consider:
> - [Medium] mptcp: mptcp_inq_hint() omits pending FIN byte on fallback
>   sockets
> - [Medium] mptcp: race in mptcp_inq_hint() counts spurious FIN on
>   fallback RST
> - [Low] mptcp: mptcp_inq_hint() omits TCP_CLOSE check for rcv_data_fin
> --

I plan to address all three points by replacing the open-coded check in
mptcp_inq_hint() with a new helper:

        static bool mptcp_pending_eof(const struct sock *sk, u64 *seq)
        {
                const struct mptcp_sock *msk = mptcp_sk(sk);

                if (!mptcp_pending_data_fin(sk, seq))
                        return false;

                return !__mptcp_check_fallback(msk) ||
                       (msk->first && sock_flag(msk->first, SOCK_DONE));
        }

and calling it from mptcp_inq_hint():

        if (sock_flag(sk, SOCK_DONE) || mptcp_pending_eof(sk, NULL))
                hint_val += 1;

Also, the mptcp_rcv_data_fin_in_seq helper can be deleted.

> 
> commit 3d490bb2690ae1fefdf4a528196f8efa6be2b028
> Author: Geliang Tang <geliang@kernel.org>
> 
> mptcp: count the DATA_FIN in mptcp_inq_hint
> 
> This commit updates mptcp_inq_hint() to add the FIN byte to the available
> data count when SOCK_DONE is set or when a non-fallback connection has an
> in-sequence DATA_FIN pending.
> 
> > diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
> > index 1d330c5d09f42..e0e6266e11c85 100644
> > --- a/net/mptcp/protocol.c
> > +++ b/net/mptcp/protocol.c
> > @@ -2487,22 +2487,17 @@ 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 = mptcp_data_avail(msk);
> > -
> > -		if (hint_val >= INT_MAX)
> > -			return INT_MAX;
> > -
> > -		return (unsigned int)hint_val;
> > -	}
> > +	if (hint_val >= INT_MAX)
> > +		return INT_MAX;
> >  
> > -	if (sock_flag(sk, SOCK_DONE))
> > -		return 1;
> > +	if (sock_flag(sk, SOCK_DONE) ||
> > +	    (!__mptcp_check_fallback(msk) &&
> > +	     mptcp_rcv_data_fin_in_seq(sk, NULL)))
> > +		hint_val += 1;
> 
> [Severity: Medium]
> Can excluding __mptcp_check_fallback(msk) here omit the pending FIN byte
> on fallback sockets before mptcp_worker() sets SOCK_DONE on sk?
> 
> When a fallen-back socket receives data and a TCP FIN in softirq,
> tcp_fin() sets SOCK_DONE on msk->first, move_skbs_to_msk() queues the
> data directly on sk->sk_receive_queue, and mptcp_data_ready() wakes
> userspace while SOCK_DONE propagation waits on mptcp_worker():
> 
> CPU0 (softirq / kworker)          CPU1 (userspace)
> tcp_fin()
>   sock_set_flag(ssk, SOCK_DONE)
> mptcp_data_ready()
>   move_skbs_to_msk()
>   sk->sk_data_ready(sk)
> subflow_sched_work_if_closed()
>   mptcp_update_rcv_data_fin()
>   mptcp_schedule_work(sk)
>                                   mptcp_recvmsg()
>                                     lock_sock(sk)
>                                     ... (backlog_list empty)
> mptcp_worker()
>   lock_sock(sk)  (blocks)
>                                     mptcp_inq_hint()  <-- omits +1
> 
> With msk->backlog_list empty, mptcp_recvmsg() skips mptcp_move_skbs()
> and mptcp_check_data_fin(), so TCP_CM_INQ under-reports by 1 byte (or
> returns 0 instead of 1 after draining the last data bytes).
> 
> Since callers of mptcp_inq_hint() hold lock_sock(sk), this could mirror
> mptcp_check_data_fin() and check (!__mptcp_check_fallback(msk) ||
> (msk->first && sock_flag(msk->first, SOCK_DONE))) to count fallback FINs
> while still excluding fallback RSTs.

The helper counts the fallback FIN once msk->first has SOCK_DONE set,
as in mptcp_check_data_fin(), without waiting for mptcp_worker().

> 
> [Severity: Medium]
> Does testing !__mptcp_check_fallback(msk) before
> mptcp_rcv_data_fin_in_seq(sk, NULL) race with a concurrent fallback and
> TCP RST and count a spurious FIN byte?
> 
> mptcp_inq_hint() runs under lock_sock(sk) without msk->fallback_lock or
> the subflow lock.  Concurrently in softirq, a non-MP_CAPABLE RST before
> fully_established (or a fallback followed by RST) sets
> MPTCP_FALLBACK_DONE before smp_wmb() and msk->rcv_data_fin = 1:
> 
> CPU0 (userspace)                  CPU1 (softirq)
> mptcp_inq_hint()
>   !__mptcp_check_fallback(msk)
>     (true)
>                                   tcp_reset()
>                                     __mptcp_try_fallback()
>                                       set_bit(MPTCP_FALLBACK_DONE, ...)
>                                     subflow_sched_work_if_closed()
>                                       mptcp_update_rcv_data_fin()
>                                         smp_wmb()
>                                         msk->rcv_data_fin = 1
>   mptcp_rcv_data_fin_in_seq()
>     (true -> hint_val += 1)  <-- spurious FIN
> 
> Because the reader checks MPTCP_FALLBACK_DONE before msk->rcv_data_fin
> with no barrier in between, it can observe the emulated DATA_FIN from
> the RST and report an unread FIN byte on a reset connection.
> 
> Calling mptcp_rcv_data_fin_in_seq(sk, NULL) first, which executes the
> pairing smp_rmb() after reading msk->rcv_data_fin, and checking
> __mptcp_check_fallback(msk) afterward closes both the race window and
> load reordering.

The pending DATA_FIN is observed before the fallback flag; the
smp_rmb() in mptcp_pending_data_fin() pairs with the smp_wmb() in
mptcp_update_rcv_data_fin(), so a RST-induced DATA_FIN is excluded.

> 
> [Severity: Low]
> Should this check the socket state (like mptcp_pending_data_fin() does)
> so a stale rcv_data_fin on a TCP_CLOSE socket is not counted?
> 
> In mptcp_check_fastclose(), mptcp_check_data_fin(sk) runs before locking
> and closing the subflows.  A zero-length in-sequence DATA_FIN arriving on
> an open subflow in that window sets msk->rcv_data_fin = 1 just before sk
> transitions to TCP_CLOSE with ECONNRESET and SOCK_DONE unset:
> 
> CPU0 (kworker)                    CPU1 (softirq)
> mptcp_check_fastclose()
>   mptcp_check_data_fin(sk)
>                                   mptcp_incoming_options()
>                                     mptcp_update_rcv_data_fin()
>                                       msk->rcv_data_fin = 1
>   sk->sk_err = ECONNRESET
>   mptcp_set_state(sk, TCP_CLOSE)  <-- rcv_data_fin left stale
> 
> Once sk is in TCP_CLOSE, mptcp_pending_data_fin() returns false and
> mptcp_worker() exits early, leaving msk->rcv_data_fin permanently set
> without SOCK_DONE.  When userspace drains queued data, mptcp_inq_hint()
> still adds +1 to TCP_CM_INQ even though the next recvmsg() will return
> -ECONNRESET rather than EOF.
> 
> Using mptcp_pending_data_fin() (or excluding TCP_CLOSE) avoids counting
> an unprocessable DATA_FIN.

The ESTABLISHED/FIN_WAIT1/FIN_WAIT2 state check in
mptcp_pending_data_fin() keeps a stale rcv_data_fin on TCP_CLOSE from
being counted.

If you think this is okay, I'll send a v8.

Thanks,
-Geliang

  reply	other threads:[~2026-12-04 10:55 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 16:48 [PATCH mptcp-net v7 0/6] mptcp: fixes for SIOCINQ Matthieu Baerts (NGI0)
2026-12-03 16:48 ` [PATCH mptcp-net v7 1/6] mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint Matthieu Baerts (NGI0)
2026-12-03 16:48 ` [PATCH mptcp-next mptcp-net v7 2/6] mptcp: add missing memory barriers for rcv_data_fin_seq Matthieu Baerts (NGI0)
2026-12-03 16:48 ` [PATCH mptcp-next mptcp-net v7 3/6] mptcp: extract mptcp_rcv_data_fin_in_seq helper Matthieu Baerts (NGI0)
2026-12-03 16:48 ` [PATCH mptcp-next mptcp-net v7 4/6] mptcp: add mptcp_inq() for SIOCINQ Matthieu Baerts (NGI0)
2026-12-03 16:48 ` [PATCH mptcp-next mptcp-net v7 5/6] mptcp: use SOCK_DONE in mptcp_inq_hint Matthieu Baerts (NGI0)
2026-12-03 16:48 ` [PATCH mptcp-next mptcp-net v7 6/6] mptcp: count the DATA_FIN " Matthieu Baerts (NGI0)
2026-12-03 18:50   ` sashiko-bot
2026-12-04 10:55     ` Geliang Tang [this message]
2026-12-04 12:12       ` Matthieu Baerts
2026-12-04 12:14         ` Matthieu Baerts
2026-12-04 23:06         ` Geliang Tang
2026-12-05 14:28           ` Matthieu Baerts
2026-12-05  6:21   ` netdev-bot+sashiko
2026-12-03 18:33 ` [PATCH mptcp-net v7 0/6] mptcp: fixes for SIOCINQ 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=0ac04e420bb265f1f57e8ecf0961a309ace105ff.camel@kernel.org \
    --to=geliang@kernel.org \
    --cc=matttbe@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