LCOV - code coverage report
Current view: top level - mptcp/protocol.c (source / functions) Coverage Total Hit
Test: export Lines: 92.4 % 2563 2368
Test Date: 2026-08-11 07:23:51 Functions: 95.5 % 157 150
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 67.9 % 2053 1394

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-2.0
       2                 :             : /* Multipath TCP
       3                 :             :  *
       4                 :             :  * Copyright (c) 2017 - 2019, Intel Corporation.
       5                 :             :  */
       6                 :             : 
       7                 :             : #define pr_fmt(fmt) "MPTCP: " fmt
       8                 :             : 
       9                 :             : #include <linux/kernel.h>
      10                 :             : #include <linux/module.h>
      11                 :             : #include <linux/netdevice.h>
      12                 :             : #include <linux/sched/signal.h>
      13                 :             : #include <linux/atomic.h>
      14                 :             : #include <linux/errqueue.h>
      15                 :             : #include <net/aligned_data.h>
      16                 :             : #include <net/rps.h>
      17                 :             : #include <net/sock.h>
      18                 :             : #include <net/inet_common.h>
      19                 :             : #include <net/inet_hashtables.h>
      20                 :             : #include <net/protocol.h>
      21                 :             : #include <net/tcp_states.h>
      22                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
      23                 :             : #include <net/transp_v6.h>
      24                 :             : #endif
      25                 :             : #include <net/mptcp.h>
      26                 :             : #include <net/hotdata.h>
      27                 :             : #include <net/xfrm.h>
      28                 :             : #include <asm/ioctls.h>
      29                 :             : #include "protocol.h"
      30                 :             : #include "mib.h"
      31                 :             : 
      32                 :             : static unsigned int mptcp_inq_hint(const struct sock *sk);
      33                 :             : 
      34                 :             : #define CREATE_TRACE_POINTS
      35                 :             : #include <trace/events/mptcp.h>
      36                 :             : 
      37                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
      38                 :             : struct mptcp6_sock {
      39                 :             :         struct mptcp_sock msk;
      40                 :             :         struct ipv6_pinfo np;
      41                 :             : };
      42                 :             : #endif
      43                 :             : 
      44                 :             : enum {
      45                 :             :         MPTCP_CMSG_TS = BIT(0),
      46                 :             :         MPTCP_CMSG_INQ = BIT(1),
      47                 :             : };
      48                 :             : 
      49                 :             : static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
      50                 :             : 
      51                 :             : static void __mptcp_destroy_sock(struct sock *sk);
      52                 :             : static void mptcp_check_send_data_fin(struct sock *sk);
      53                 :             : 
      54                 :             : DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions) = {
      55                 :             :         .bh_lock = INIT_LOCAL_LOCK(bh_lock),
      56                 :             : };
      57                 :             : static struct net_device *mptcp_napi_dev;
      58                 :             : 
      59                 :             : /* Returns end sequence number of the receiver's advertised window */
      60                 :      379824 : u64 mptcp_wnd_end(const struct mptcp_sock *msk)
      61                 :             : {
      62                 :      379824 :         return READ_ONCE(msk->wnd_end);
      63                 :             : }
      64                 :             : 
      65                 :         178 : static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk)
      66                 :             : {
      67                 :         178 :         unsigned short family = READ_ONCE(sk->sk_family);
      68                 :             : 
      69                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
      70         [ +  + ]:         178 :         if (family == AF_INET6)
      71                 :             :                 return &inet6_stream_ops;
      72                 :             : #endif
      73         [ -  + ]:          88 :         WARN_ON_ONCE(family != AF_INET);
      74                 :             :         return &inet_stream_ops;
      75                 :             : }
      76                 :             : 
      77                 :         214 : bool __mptcp_try_fallback(struct mptcp_sock *msk, int fb_mib)
      78                 :             : {
      79                 :         214 :         struct net *net = sock_net((struct sock *)msk);
      80                 :             : 
      81         [ -  + ]:         214 :         if (__mptcp_check_fallback(msk))
      82                 :             :                 return true;
      83                 :             : 
      84                 :             :         /* The caller possibly is not holding the msk socket lock, but
      85                 :             :          * in the fallback case only the current subflow is touching
      86                 :             :          * the OoO queue.
      87                 :             :          */
      88         [ -  + ]:         214 :         if (!RB_EMPTY_ROOT(&msk->out_of_order_queue))
      89                 :             :                 return false;
      90                 :             : 
      91                 :         214 :         spin_lock_bh(&msk->fallback_lock);
      92         [ +  + ]:         214 :         if (!msk->allow_infinite_fallback) {
           [ -  +  +  + ]
      93                 :          13 :                 spin_unlock_bh(&msk->fallback_lock);
      94                 :          13 :                 return false;
      95                 :             :         }
      96                 :             : 
      97                 :         201 :         msk->allow_subflows = false;
      98                 :         201 :         set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
      99         [ +  - ]:         201 :         __MPTCP_INC_STATS(net, fb_mib);
     100                 :         201 :         spin_unlock_bh(&msk->fallback_lock);
     101                 :         201 :         return true;
     102                 :             : }
     103                 :             : 
     104                 :        3520 : static int __mptcp_socket_create(struct mptcp_sock *msk)
     105                 :             : {
     106                 :        3520 :         struct mptcp_subflow_context *subflow;
     107                 :        3520 :         struct sock *sk = (struct sock *)msk;
     108                 :        3520 :         struct socket *ssock;
     109                 :        3520 :         int err;
     110                 :             : 
     111                 :        3520 :         err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
     112         [ +  - ]:        3520 :         if (err)
     113                 :             :                 return err;
     114                 :             : 
     115         [ -  + ]:        3520 :         msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
     116                 :        3520 :         WRITE_ONCE(msk->first, ssock->sk);
     117                 :        3520 :         subflow = mptcp_subflow_ctx(ssock->sk);
     118                 :        3520 :         list_add(&subflow->node, &msk->conn_list);
     119                 :        3520 :         sock_hold(ssock->sk);
     120                 :        3520 :         subflow->request_mptcp = 1;
     121                 :        3520 :         subflow->subflow_id = msk->subflow_id++;
     122                 :             : 
     123                 :             :         /* This is the first subflow, always with id 0 */
     124                 :        3520 :         WRITE_ONCE(subflow->local_id, 0);
     125                 :        3520 :         mptcp_sock_graft(msk->first, sk->sk_socket);
     126                 :        3520 :         iput(SOCK_INODE(ssock));
     127                 :             : 
     128                 :        3520 :         return 0;
     129                 :             : }
     130                 :             : 
     131                 :             : /* If the MPC handshake is not started, returns the first subflow,
     132                 :             :  * eventually allocating it.
     133                 :             :  */
     134                 :       10231 : struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
     135                 :             : {
     136                 :       10231 :         struct sock *sk = (struct sock *)msk;
     137                 :       10231 :         int ret;
     138                 :             : 
     139         [ +  + ]:       10231 :         if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
           [ -  +  +  + ]
     140                 :             :                 return ERR_PTR(-EINVAL);
     141                 :             : 
     142         [ +  + ]:       10213 :         if (!msk->first) {
     143                 :        3520 :                 ret = __mptcp_socket_create(msk);
     144         [ -  + ]:        3520 :                 if (ret)
     145                 :           0 :                         return ERR_PTR(ret);
     146                 :             :         }
     147                 :             : 
     148                 :       10213 :         return msk->first;
     149                 :             : }
     150                 :             : 
     151                 :        1577 : static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
     152                 :             : {
     153                 :             :         /* The skb forward memory was already transferred to sk by
     154                 :             :          * mptcp_borrow_fwdmem(), even before setting the destructor.
     155                 :             :          */
     156         [ +  + ]:        1577 :         if (!skb->destructor)
     157                 :        1300 :                 sk_mem_reclaim(sk);
     158                 :             : 
     159                 :        1577 :         sk_drops_skbadd(sk, skb);
     160                 :        1577 :         __kfree_skb(skb);
     161                 :        1577 : }
     162                 :             : 
     163                 :      626787 : static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
     164                 :             :                                  struct sk_buff *from, bool *fragstolen,
     165                 :             :                                  int *delta)
     166                 :             : {
     167                 :      626787 :         int limit = READ_ONCE(sk->sk_rcvbuf);
     168                 :             : 
     169         [ +  + ]:      626787 :         if (unlikely(MPTCP_SKB_CB(to)->cant_coalesce) ||
     170         [ +  + ]:      626762 :             MPTCP_SKB_CB(from)->offset ||
     171   [ +  +  +  + ]:     1179841 :             ((to->len + from->len) > (limit >> 3)) ||
     172                 :      553107 :             !skb_try_coalesce(to, from, fragstolen, delta))
     173                 :      206323 :                 return false;
     174                 :             : 
     175   [ -  +  -  - ]:      420464 :         pr_debug("colesced seq %llx into %llx new len %d new end seq %llx\n",
     176                 :             :                  MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
     177                 :             :                  to->len, MPTCP_SKB_CB(from)->end_seq);
     178                 :      420464 :         MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
     179                 :      420464 :         return true;
     180                 :             : }
     181                 :             : 
     182                 :      553675 : static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
     183                 :             :                                struct sk_buff *from)
     184                 :             : {
     185                 :      553675 :         bool fragstolen;
     186                 :      553675 :         int delta;
     187                 :             : 
     188         [ +  + ]:      553675 :         if (!__mptcp_try_coalesce(sk, to, from, &fragstolen, &delta))
     189                 :             :                 return false;
     190                 :             : 
     191                 :             :         /* note the fwd memory can reach a negative value after accounting
     192                 :             :          * for the delta, but the later skb free will restore a non
     193                 :             :          * negative one
     194                 :             :          */
     195                 :      381159 :         atomic_add(delta, &sk->sk_rmem_alloc);
     196         [ +  - ]:      381159 :         sk_mem_charge(sk, delta);
     197         [ -  + ]:      381159 :         kfree_skb_partial(from, fragstolen);
     198                 :             : 
     199                 :      381159 :         return true;
     200                 :             : }
     201                 :             : 
     202                 :        7892 : static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
     203                 :             :                                    struct sk_buff *from)
     204                 :             : {
     205         [ +  + ]:       44134 :         if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
     206                 :             :                 return false;
     207                 :             : 
     208                 :      227884 :         return mptcp_try_coalesce((struct sock *)msk, to, from);
     209                 :             : }
     210                 :             : 
     211                 :             : /* "inspired" by tcp_rcvbuf_grow(), main difference:
     212                 :             :  * - mptcp does not maintain a msk-level window clamp
     213                 :             :  * - returns true when  the receive buffer is actually updated
     214                 :             :  */
     215                 :        5026 : static bool mptcp_rcvbuf_grow(struct sock *sk, u32 newval)
     216                 :             : {
     217         [ -  + ]:        5026 :         struct mptcp_sock *msk = mptcp_sk(sk);
     218         [ +  - ]:        5026 :         const struct net *net = sock_net(sk);
     219                 :        5026 :         u32 rcvwin, rcvbuf, cap, oldval;
     220                 :        5026 :         u64 grow;
     221                 :             : 
     222                 :        5026 :         oldval = msk->rcvq_space.space;
     223                 :        5026 :         msk->rcvq_space.space = newval;
     224         [ +  - ]:        5026 :         if (!READ_ONCE(net->ipv4.sysctl_tcp_moderate_rcvbuf) ||
     225         [ -  + ]:        5026 :             (sk->sk_userlocks & SOCK_RCVBUF_LOCK))
     226                 :             :                 return false;
     227                 :             : 
     228                 :             :         /* DRS is always one RTT late. */
     229                 :        5026 :         rcvwin = newval << 1;
     230                 :             : 
     231                 :             :         /* slow start: allow the sender to double its rate. */
     232                 :        5026 :         grow = (u64)rcvwin * (newval - oldval);
     233                 :        5026 :         do_div(grow, oldval);
     234                 :        5026 :         rcvwin += grow << 1;
     235                 :             : 
     236                 :        5026 :         cap = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
     237                 :             : 
     238                 :        5026 :         rcvbuf = min_t(u32, mptcp_space_from_win(sk, rcvwin), cap);
     239         [ +  + ]:        5026 :         if (rcvbuf > sk->sk_rcvbuf) {
     240                 :        2468 :                 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
     241                 :        2468 :                 return true;
     242                 :             :         }
     243                 :             :         return false;
     244                 :             : }
     245                 :             : 
     246                 :             : /* "Inspired" from the TCP version; main difference: stop as soon as the MPTCP
     247                 :             :  * socket is under memory limit.
     248                 :             :  */
     249                 :           9 : static void mptcp_prune_ofo_queue(struct sock *sk,
     250                 :             :                                   const struct sk_buff *in_skb)
     251                 :             : {
     252         [ -  + ]:           9 :         struct mptcp_sock *msk = mptcp_sk(sk);
     253                 :           9 :         struct rb_node *node, *prev;
     254                 :           9 :         bool pruned = false;
     255                 :           9 :         u64 mem;
     256                 :             : 
     257         [ -  + ]:           9 :         if (RB_EMPTY_ROOT(&msk->out_of_order_queue))
     258                 :             :                 return;
     259                 :             : 
     260                 :           0 :         node = &msk->ooo_last_skb->rbnode;
     261                 :             : 
     262                 :           0 :         do {
     263                 :           0 :                 struct sk_buff *skb = rb_to_skb(node);
     264                 :             : 
     265                 :             :                 /* Stop pruning if the incoming skb would land in OoO tail. */
     266         [ #  # ]:           0 :                 if (after64(MPTCP_SKB_CB(in_skb)->map_seq,
     267                 :             :                             MPTCP_SKB_CB(skb)->map_seq))
     268                 :             :                         break;
     269                 :             : 
     270                 :           0 :                 pruned = true;
     271                 :           0 :                 prev = rb_prev(node);
     272                 :           0 :                 rb_erase(node, &msk->out_of_order_queue);
     273                 :           0 :                 mptcp_drop(sk, skb);
     274                 :           0 :                 msk->ooo_last_skb = rb_to_skb(prev);
     275                 :             : 
     276         [ #  # ]:           0 :                 mem = (unsigned int)sk_rmem_alloc_get(sk);
     277         [ #  # ]:           0 :                 if (mem <= sk->sk_rcvbuf)
     278                 :             :                         break;
     279                 :             : 
     280                 :           0 :                 node = prev;
     281         [ #  # ]:           0 :         } while (node);
     282                 :             : 
     283         [ #  # ]:           0 :         if (pruned)
     284         [ #  # ]:           0 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOPRUNED);
     285                 :             : }
     286                 :             : 
     287                 :             : /* The stack can't drop packets for fallback socket at the msk level, or the
     288                 :             :  * stream will break.
     289                 :             :  */
     290                 :     1249913 : static bool mptcp_can_ingest(const struct sock *sk)
     291                 :             : {
     292   [ +  +  -  + ]:     1249931 :         return likely(sk_rmem_alloc_get(sk) <= READ_ONCE(sk->sk_rcvbuf)) ||
     293         [ -  + ]:          18 :                __mptcp_check_fallback(mptcp_sk(sk));
     294                 :             : }
     295                 :             : 
     296                 :     1249904 : static bool mptcp_try_rmem_schedule(struct sock *sk, const struct sk_buff *skb)
     297                 :             : {
     298         [ +  + ]:     1249904 :         if (!mptcp_can_ingest(sk)) {
     299                 :           9 :                 mptcp_prune_ofo_queue(sk, skb);
     300                 :           9 :                 return mptcp_can_ingest(sk);
     301                 :             :         }
     302                 :             :         return true;
     303                 :             : }
     304                 :             : 
     305                 :             : /* "inspired" by tcp_data_queue_ofo(), main differences:
     306                 :             :  * - use mptcp seqs
     307                 :             :  * - don't cope with sacks
     308                 :             :  */
     309                 :      205560 : static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
     310                 :             : {
     311                 :      205560 :         struct sock *sk = (struct sock *)msk;
     312                 :      205560 :         struct rb_node **p, *parent;
     313                 :      205560 :         u64 seq, end_seq, max_seq;
     314                 :      205560 :         struct sk_buff *skb1;
     315                 :             : 
     316         [ +  + ]:      205560 :         if (!mptcp_try_rmem_schedule(sk, skb)) {
     317         [ +  - ]:           4 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
     318                 :           4 :                 mptcp_drop(sk, skb);
     319                 :           4 :                 return;
     320                 :             :         }
     321                 :             : 
     322                 :      205556 :         seq = MPTCP_SKB_CB(skb)->map_seq;
     323                 :      205556 :         end_seq = MPTCP_SKB_CB(skb)->end_seq;
     324         [ -  + ]:      205556 :         max_seq = atomic64_read(&msk->rcv_wnd_sent);
     325                 :             : 
     326   [ -  +  -  - ]:      205556 :         pr_debug("msk=%p seq=%llx limit=%llx empty=%d\n", msk, seq, max_seq,
     327                 :             :                  RB_EMPTY_ROOT(&msk->out_of_order_queue));
     328         [ +  + ]:      205556 :         if (after64(end_seq, max_seq)) {
     329                 :             :                 /* out of window */
     330                 :         416 :                 mptcp_drop(sk, skb);
     331   [ -  +  -  - ]:         416 :                 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
     332                 :             :                          (unsigned long long)end_seq - (unsigned long)max_seq,
     333                 :             :                          (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
     334         [ +  - ]:         416 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
     335                 :         416 :                 return;
     336                 :             :         }
     337                 :             : 
     338                 :      205140 :         p = &msk->out_of_order_queue.rb_node;
     339         [ +  - ]:      205140 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
     340         [ +  + ]:      205140 :         if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
     341                 :        9693 :                 rb_link_node(&skb->rbnode, NULL, p);
     342                 :        9693 :                 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
     343                 :        9693 :                 msk->ooo_last_skb = skb;
     344                 :        9693 :                 goto end;
     345                 :             :         }
     346                 :             : 
     347                 :             :         /* with 2 subflows, adding at end of ooo queue is quite likely
     348                 :             :          * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
     349                 :             :          */
     350   [ +  +  +  + ]:      360271 :         if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
     351         [ +  - ]:      145224 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
     352         [ +  - ]:      145224 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
     353                 :      145224 :                 return;
     354                 :             :         }
     355                 :             : 
     356                 :             :         /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
     357         [ +  + ]:       50223 :         if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
     358         [ +  - ]:       28164 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
     359                 :       28164 :                 parent = &msk->ooo_last_skb->rbnode;
     360                 :       28164 :                 p = &parent->rb_right;
     361                 :       28164 :                 goto insert;
     362                 :             :         }
     363                 :             : 
     364                 :             :         /* Find place to insert this segment. Handle overlaps on the way. */
     365                 :             :         parent = NULL;
     366         [ +  + ]:      122795 :         while (*p) {
     367                 :      116256 :                 parent = *p;
     368                 :      116256 :                 skb1 = rb_to_skb(parent);
     369         [ +  + ]:      116256 :                 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
     370                 :       30295 :                         p = &parent->rb_left;
     371                 :       30295 :                         continue;
     372                 :             :                 }
     373         [ +  + ]:       85961 :                 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
     374         [ +  + ]:         731 :                         if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
     375                 :             :                                 /* All the bits are present. Drop. */
     376                 :         458 :                                 mptcp_drop(sk, skb);
     377         [ +  - ]:         458 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     378                 :         458 :                                 return;
     379                 :             :                         }
     380         [ +  + ]:         273 :                         if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
     381                 :             :                                 /* partial overlap:
     382                 :             :                                  *     |     skb      |
     383                 :             :                                  *  |     skb1    |
     384                 :             :                                  * continue traversing
     385                 :             :                                  */
     386                 :             :                         } else {
     387                 :             :                                 /* skb's seq == skb1's seq and skb covers skb1.
     388                 :             :                                  * Replace skb1 with skb.
     389                 :             :                                  */
     390                 :          15 :                                 rb_replace_node(&skb1->rbnode, &skb->rbnode,
     391                 :             :                                                 &msk->out_of_order_queue);
     392                 :          15 :                                 mptcp_drop(sk, skb1);
     393         [ +  - ]:          15 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     394                 :          15 :                                 goto merge_right;
     395                 :             :                         }
     396   [ +  +  +  + ]:      104914 :                 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
     397         [ +  - ]:       15047 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
     398                 :       15047 :                         return;
     399                 :             :                 }
     400                 :       70441 :                 p = &parent->rb_right;
     401                 :             :         }
     402                 :             : 
     403                 :        6539 : insert:
     404                 :             :         /* Insert segment into RB tree. */
     405                 :       34703 :         rb_link_node(&skb->rbnode, parent, p);
     406                 :       34703 :         rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
     407                 :             : 
     408                 :             : merge_right:
     409                 :             :         /* Remove other segments covered by skb. */
     410         [ +  + ]:       34824 :         while ((skb1 = skb_rb_next(skb)) != NULL) {
     411         [ +  + ]:        6657 :                 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
     412                 :             :                         break;
     413                 :         106 :                 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
     414                 :         106 :                 mptcp_drop(sk, skb1);
     415         [ +  - ]:        1770 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     416                 :             :         }
     417                 :             :         /* If there is no skb after us, we are the last_skb ! */
     418         [ +  + ]:       34718 :         if (!skb1)
     419                 :       28167 :                 msk->ooo_last_skb = skb;
     420                 :             : 
     421                 :        6551 : end:
     422                 :       44411 :         skb_condense(skb);
     423                 :       44411 :         skb_set_owner_r(skb, sk);
     424                 :             : }
     425                 :             : 
     426                 :     1289628 : static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,
     427                 :             :                            int copy_len)
     428                 :             : {
     429                 :     1289628 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
     430                 :     1289628 :         bool has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
     431                 :             : 
     432                 :             :         /* the skb map_seq accounts for the skb offset:
     433                 :             :          * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
     434                 :             :          * value
     435                 :             :          */
     436                 :     1289628 :         MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
     437                 :     1289628 :         MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
     438                 :     1289628 :         MPTCP_SKB_CB(skb)->offset = offset;
     439                 :     1289628 :         MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
     440                 :     1289628 :         MPTCP_SKB_CB(skb)->cant_coalesce = 0;
     441                 :             : 
     442                 :     1289628 :         __skb_unlink(skb, &ssk->sk_receive_queue);
     443                 :             : 
     444                 :     1289628 :         skb_ext_reset(skb);
     445         [ -  + ]:     1289628 :         skb_dst_drop(skb);
     446                 :     1289628 : }
     447                 :             : 
     448                 :     1250321 : static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
     449                 :             : {
     450                 :     1250321 :         u64 copy_len = MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq;
     451         [ -  + ]:     1250321 :         struct mptcp_sock *msk = mptcp_sk(sk);
     452                 :     1250321 :         struct sk_buff *tail;
     453                 :             : 
     454                 :     1250321 :         mptcp_borrow_fwdmem(sk, skb);
     455                 :             : 
     456         [ +  + ]:     1250321 :         if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
     457                 :             :                 /* in sequence */
     458                 :     1044338 : insert:
     459         [ +  + ]:     1044344 :                 if (!mptcp_try_rmem_schedule(sk, skb)) {
     460         [ +  - ]:           5 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
     461                 :           5 :                         mptcp_drop(sk, skb);
     462                 :           5 :                         return false;
     463                 :             :                 }
     464                 :             : 
     465                 :     1044339 :                 msk->bytes_received += copy_len;
     466                 :     1044339 :                 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
     467         [ +  + ]:     1044339 :                 tail = skb_peek_tail(&sk->sk_receive_queue);
     468   [ +  -  +  + ]:      325791 :                 if (tail && mptcp_try_coalesce(sk, tail, skb))
     469                 :             :                         return true;
     470                 :             : 
     471                 :      839557 :                 skb_set_owner_r(skb, sk);
     472                 :      839557 :                 __skb_queue_tail(&sk->sk_receive_queue, skb);
     473                 :      839557 :                 return true;
     474         [ +  + ]:      205983 :         } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
     475                 :      205560 :                 mptcp_data_queue_ofo(msk, skb);
     476                 :      205560 :                 return false;
     477                 :             :         }
     478                 :             : 
     479                 :             :         /* Partial packet */
     480         [ +  + ]:         423 :         if (after64(MPTCP_SKB_CB(skb)->end_seq, msk->ack_seq)) {
     481                 :           6 :                 copy_len = MPTCP_SKB_CB(skb)->end_seq - msk->ack_seq;
     482                 :           6 :                 MPTCP_SKB_CB(skb)->offset += msk->ack_seq -
     483                 :             :                                              MPTCP_SKB_CB(skb)->map_seq;
     484                 :           6 :                 MPTCP_SKB_CB(skb)->map_seq += msk->ack_seq -
     485                 :             :                                               MPTCP_SKB_CB(skb)->map_seq;
     486                 :           6 :                 goto insert;
     487                 :             :         }
     488                 :             : 
     489                 :             :         /* Completely old data */
     490         [ +  - ]:         417 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     491                 :         417 :         mptcp_drop(sk, skb);
     492                 :         417 :         return false;
     493                 :             : }
     494                 :             : 
     495                 :       54963 : static void mptcp_stop_rtx_timer(struct sock *sk)
     496                 :             : {
     497                 :       54963 :         sk_stop_timer(sk, &sk->mptcp_retransmit_timer);
     498         [ -  + ]:       54963 :         mptcp_sk(sk)->timer_ival = 0;
     499                 :       54963 : }
     500                 :             : 
     501                 :        6936 : static void mptcp_close_wake_up(struct sock *sk)
     502                 :             : {
     503         [ +  + ]:        6936 :         if (sock_flag(sk, SOCK_DEAD))
     504                 :             :                 return;
     505                 :             : 
     506                 :        4495 :         sk->sk_state_change(sk);
     507         [ +  + ]:        4495 :         if (sk->sk_shutdown == SHUTDOWN_MASK ||
     508         [ +  + ]:        2184 :             sk->sk_state == TCP_CLOSE)
     509                 :        2368 :                 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
     510                 :             :         else
     511                 :        2127 :                 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
     512                 :             : }
     513                 :             : 
     514                 :        2421 : static void mptcp_shutdown_subflows(struct mptcp_sock *msk)
     515                 :             : {
     516                 :        2421 :         struct mptcp_subflow_context *subflow;
     517                 :             : 
     518         [ +  + ]:        5752 :         mptcp_for_each_subflow(msk, subflow) {
     519                 :        3331 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
     520                 :        3331 :                 bool slow;
     521                 :             : 
     522                 :        3331 :                 slow = lock_sock_fast(ssk);
     523                 :        3331 :                 tcp_shutdown(ssk, SEND_SHUTDOWN);
     524                 :        3331 :                 unlock_sock_fast(ssk, slow);
     525                 :             :         }
     526                 :        2421 : }
     527                 :             : 
     528                 :             : /* called under the msk socket lock */
     529                 :      486979 : static bool mptcp_pending_data_fin_ack(struct sock *sk)
     530                 :             : {
     531         [ -  + ]:      486979 :         struct mptcp_sock *msk = mptcp_sk(sk);
     532                 :             : 
     533                 :      247406 :         return ((1 << sk->sk_state) &
     534         [ +  + ]:      486979 :                 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
           [ -  +  +  + ]
     535         [ +  + ]:       68584 :                msk->write_seq == READ_ONCE(msk->snd_una);
     536                 :             : }
     537                 :             : 
     538                 :       12647 : static void mptcp_check_data_fin_ack(struct sock *sk)
     539                 :             : {
     540         [ -  + ]:       12647 :         struct mptcp_sock *msk = mptcp_sk(sk);
     541                 :             : 
     542                 :             :         /* Look for an acknowledged DATA_FIN */
     543         [ +  + ]:       12647 :         if (mptcp_pending_data_fin_ack(sk)) {
     544                 :        2588 :                 WRITE_ONCE(msk->snd_data_fin_enable, 0);
     545                 :             : 
     546      [ +  +  - ]:        2588 :                 switch (sk->sk_state) {
     547                 :        1367 :                 case TCP_FIN_WAIT1:
     548                 :        1367 :                         mptcp_set_state(sk, TCP_FIN_WAIT2);
     549                 :        1367 :                         break;
     550                 :        1221 :                 case TCP_CLOSING:
     551                 :             :                 case TCP_LAST_ACK:
     552                 :        1221 :                         mptcp_shutdown_subflows(msk);
     553                 :        1221 :                         mptcp_set_state(sk, TCP_CLOSE);
     554                 :        1221 :                         break;
     555                 :             :                 }
     556                 :             : 
     557                 :        2588 :                 mptcp_close_wake_up(sk);
     558                 :             :         }
     559                 :       12647 : }
     560                 :             : 
     561                 :             : /* can be called with no lock acquired */
     562                 :     1176454 : static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
     563                 :             : {
     564         [ -  + ]:     1176454 :         struct mptcp_sock *msk = mptcp_sk(sk);
     565                 :             : 
     566   [ +  +  +  + ]:     1212092 :         if (READ_ONCE(msk->rcv_data_fin) &&
           [ -  +  +  +  
           +  + ][ +  + ]
           [ -  +  +  +  
             -  +  +  + ]
     567         [ +  + ]:         172 :             ((1 << inet_sk_state_load(sk)) &
     568                 :             :              (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
     569                 :       34850 :                 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
     570                 :             : 
     571         [ +  + ]:       34850 :                 if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
     572         [ +  + ]:        3126 :                         if (seq)
     573                 :        2562 :                                 *seq = rcv_data_fin_seq;
     574                 :             : 
     575                 :        3126 :                         return true;
     576                 :             :                 }
     577                 :             :         }
     578                 :             : 
     579                 :             :         return false;
     580                 :             : }
     581                 :             : 
     582                 :        1379 : static void mptcp_set_datafin_timeout(struct sock *sk)
     583                 :             : {
     584                 :        1379 :         struct inet_connection_sock *icsk = inet_csk(sk);
     585                 :        1379 :         u32 rto_min = READ_ONCE(icsk->icsk_rto_min);
     586                 :        1379 :         u32 rto_max = READ_ONCE(icsk->icsk_rto_max);
     587                 :        1379 :         u32 retransmits;
     588                 :             : 
     589                 :             :         /* The sysctls are validated independently: rto_min > rto_max is
     590                 :             :          * possible, guard against ilog2(0).
     591                 :             :          */
     592         [ -  + ]:        1379 :         retransmits = min_t(u32, icsk->icsk_retransmits,
     593                 :             :                             ilog2(max_t(u32, rto_max / rto_min, 1)));
     594                 :             : 
     595         [ -  + ]:        1379 :         mptcp_sk(sk)->timer_ival = rto_min << retransmits;
           [ -  +  -  + ]
     596                 :        1379 : }
     597                 :             : 
     598                 :     1251378 : static void __mptcp_set_timeout(struct sock *sk, long tout)
     599                 :             : {
     600   [ +  +  -  + ]:     1613676 :         mptcp_sk(sk)->timer_ival = tout > 0 ? tout :
     601                 :      362298 :                                    READ_ONCE(inet_csk(sk)->icsk_rto_min);
     602                 :     1251378 : }
     603                 :             : 
     604                 :      249449 : static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
     605                 :             : {
     606                 :     1822384 :         const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
     607                 :             : 
     608   [ +  +  +  + ]:     1489680 :         return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
     609   [ +  +  +  + ]:     3089464 :                tcp_timeout_expires(ssk) - jiffies : 0;
     610                 :             : }
     611                 :             : 
     612                 :      458738 : void mptcp_set_timeout(struct sock *sk)
     613                 :             : {
     614                 :      458738 :         struct mptcp_subflow_context *subflow;
     615                 :      458738 :         long tout = 0;
     616                 :             : 
     617   [ -  +  -  +  :     1099781 :         mptcp_for_each_subflow(mptcp_sk(sk), subflow)
                   +  + ]
     618         [ +  + ]:     1139394 :                 tout = max(tout, mptcp_timeout_from_subflow(subflow));
     619                 :      458738 :         __mptcp_set_timeout(sk, tout);
     620                 :      458738 : }
     621                 :             : 
     622                 :      548990 : static inline bool tcp_can_send_ack(const struct sock *ssk)
     623                 :             : {
     624         [ -  + ]:     1228728 :         return !((1 << inet_sk_state_load(ssk)) &
     625                 :             :                (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
     626                 :             : }
     627                 :             : 
     628                 :        6474 : void __mptcp_subflow_send_ack(struct sock *ssk)
     629                 :             : {
     630         [ +  + ]:        6474 :         if (tcp_can_send_ack(ssk))
     631                 :        6264 :                 tcp_send_ack(ssk);
     632                 :        6474 : }
     633                 :             : 
     634                 :        5422 : static void mptcp_subflow_send_ack(struct sock *ssk)
     635                 :             : {
     636                 :        5422 :         bool slow;
     637                 :             : 
     638                 :        5422 :         slow = lock_sock_fast(ssk);
     639                 :        5422 :         __mptcp_subflow_send_ack(ssk);
     640                 :        5422 :         unlock_sock_fast(ssk, slow);
     641                 :        5422 : }
     642                 :             : 
     643                 :        3788 : static void mptcp_send_ack(struct mptcp_sock *msk)
     644                 :             : {
     645                 :        3788 :         struct mptcp_subflow_context *subflow;
     646                 :             : 
     647         [ +  + ]:        9210 :         mptcp_for_each_subflow(msk, subflow)
     648                 :        5422 :                 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
     649                 :        3788 : }
     650                 :             : 
     651                 :      831612 : static void mptcp_subflow_cleanup_rbuf(struct sock *ssk, int copied)
     652                 :             : {
     653                 :      831612 :         bool slow;
     654                 :             : 
     655                 :      831612 :         slow = lock_sock_fast(ssk);
     656         [ +  + ]:      831612 :         if (tcp_can_send_ack(ssk))
     657                 :      829220 :                 tcp_cleanup_rbuf(ssk, copied);
     658                 :      831612 :         unlock_sock_fast(ssk, slow);
     659                 :      831612 : }
     660                 :             : 
     661                 :     2188314 : static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
     662                 :             : {
     663                 :     2188314 :         const struct inet_connection_sock *icsk = inet_csk(ssk);
     664                 :     2188314 :         u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
     665         [ -  + ]:     2188314 :         const struct tcp_sock *tp = tcp_sk(ssk);
     666                 :             : 
     667         [ +  + ]:     2188314 :         return (ack_pending & ICSK_ACK_SCHED) &&
     668                 :     1086836 :                 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
     669   [ +  +  +  + ]:     1086836 :                   READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
     670         [ +  + ]:      274641 :                  (rx_empty && ack_pending &
     671                 :             :                               (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
     672                 :             : }
     673                 :             : 
     674                 :     1945013 : static void mptcp_cleanup_rbuf(struct mptcp_sock *msk, int copied)
     675                 :             : {
     676                 :     1945013 :         int old_space = READ_ONCE(msk->old_wspace);
     677                 :     1945013 :         struct mptcp_subflow_context *subflow;
     678                 :     1945013 :         struct sock *sk = (struct sock *)msk;
     679                 :     1945013 :         int space =  __mptcp_space(sk);
     680                 :     1945013 :         bool cleanup, rx_empty;
     681                 :             : 
     682   [ +  +  +  +  :     1945013 :         cleanup = (space > 0) && (space >= (old_space << 1)) && copied;
                   +  + ]
     683   [ +  +  +  + ]:     1945013 :         rx_empty = !sk_rmem_alloc_get(sk) && copied;
     684                 :             : 
     685         [ +  + ]:     4567165 :         mptcp_for_each_subflow(msk, subflow) {
     686         [ +  + ]:     2622152 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
     687                 :             : 
     688   [ +  +  +  + ]:     2622152 :                 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
     689                 :      831612 :                         mptcp_subflow_cleanup_rbuf(ssk, copied);
     690                 :             :         }
     691                 :     1945013 : }
     692                 :             : 
     693                 :      219634 : static void mptcp_check_data_fin(struct sock *sk)
     694                 :             : {
     695         [ -  + ]:      219634 :         struct mptcp_sock *msk = mptcp_sk(sk);
     696                 :      219634 :         u64 rcv_data_fin_seq;
     697                 :             : 
     698                 :             :         /* Need to ack a DATA_FIN received from a peer while this side
     699                 :             :          * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
     700                 :             :          * msk->rcv_data_fin was set when parsing the incoming options
     701                 :             :          * at the subflow level and the msk lock was not held, so this
     702                 :             :          * is the first opportunity to act on the DATA_FIN and change
     703                 :             :          * the msk state.
     704                 :             :          *
     705                 :             :          * If we are caught up to the sequence number of the incoming
     706                 :             :          * DATA_FIN, send the DATA_ACK now and do state transition.  If
     707                 :             :          * not caught up, do nothing and let the recv code send DATA_ACK
     708                 :             :          * when catching up.
     709                 :             :          */
     710                 :             : 
     711         [ +  + ]:      219634 :         if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
     712                 :        2562 :                 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
     713                 :        2562 :                 WRITE_ONCE(msk->rcv_data_fin, 0);
     714                 :             : 
     715                 :        2562 :                 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
     716                 :        2562 :                 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
     717                 :             : 
     718   [ +  +  +  - ]:        2562 :                 switch (sk->sk_state) {
     719                 :             :                 case TCP_ESTABLISHED:
     720                 :        1032 :                         mptcp_set_state(sk, TCP_CLOSE_WAIT);
     721                 :        1032 :                         break;
     722                 :         330 :                 case TCP_FIN_WAIT1:
     723                 :         330 :                         mptcp_set_state(sk, TCP_CLOSING);
     724                 :         330 :                         break;
     725                 :        1200 :                 case TCP_FIN_WAIT2:
     726                 :        1200 :                         mptcp_shutdown_subflows(msk);
     727                 :        1200 :                         mptcp_set_state(sk, TCP_CLOSE);
     728                 :        1200 :                         break;
     729                 :             :                 default:
     730                 :             :                         /* Other states not expected */
     731                 :           0 :                         WARN_ON_ONCE(1);
     732                 :           0 :                         break;
     733                 :             :                 }
     734                 :             : 
     735         [ +  + ]:        2562 :                 if (!__mptcp_check_fallback(msk))
     736                 :        2409 :                         mptcp_send_ack(msk);
     737                 :        2562 :                 mptcp_close_wake_up(sk);
     738                 :             :         }
     739                 :      219634 : }
     740                 :             : 
     741                 :           0 : static void mptcp_dss_corruption(struct mptcp_sock *msk, struct sock *ssk)
     742                 :             : {
     743         [ #  # ]:           0 :         if (!mptcp_try_fallback(ssk, MPTCP_MIB_DSSCORRUPTIONFALLBACK)) {
     744         [ #  # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSCORRUPTIONRESET);
     745                 :           0 :                 mptcp_subflow_reset(ssk);
     746                 :             :         }
     747                 :           0 : }
     748                 :             : 
     749                 :      295259 : static void __mptcp_add_backlog(struct sock *sk,
     750                 :             :                                 struct mptcp_subflow_context *subflow,
     751                 :             :                                 struct sk_buff *skb)
     752                 :             : {
     753         [ -  + ]:      295259 :         struct mptcp_sock *msk = mptcp_sk(sk);
     754                 :      295259 :         struct sk_buff *tail = NULL;
     755                 :      295259 :         struct sock *ssk = skb->sk;
     756                 :      295259 :         bool fragstolen;
     757                 :      295259 :         u64 limit;
     758                 :      295259 :         int delta;
     759                 :             : 
     760         [ +  + ]:      295259 :         if (unlikely(sk->sk_state == TCP_CLOSE)) {
     761                 :           2 :                 kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE);
     762                 :           4 :                 return;
     763                 :             :         }
     764                 :             : 
     765                 :             :         /* Similar additional allowance as plain TCP. */
     766                 :      295257 :         limit = READ_ONCE(sk->sk_rcvbuf);
     767                 :      295257 :         limit += (limit >> 1) + 64 * 1024;
     768                 :      295257 :         limit = min_t(u64, limit, UINT_MAX);
     769   [ -  +  -  - ]:      295257 :         if (msk->backlog_len > limit && !__mptcp_check_fallback(msk)) {
     770         [ #  # ]:           0 :                 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_BACKLOGDROP);
     771                 :           0 :                 kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_BACKLOG);
     772                 :           0 :                 return;
     773                 :             :         }
     774                 :             : 
     775                 :             :         /* Try to coalesce with the last skb in our backlog */
     776         [ +  + ]:      295257 :         if (!list_empty(&msk->backlog_list))
     777                 :       84115 :                 tail = list_last_entry(&msk->backlog_list, struct sk_buff, list);
     778                 :             : 
     779   [ +  -  +  + ]:       84115 :         if (tail && MPTCP_SKB_CB(skb)->map_seq == MPTCP_SKB_CB(tail)->end_seq &&
     780   [ +  +  +  + ]:      153481 :             ssk == tail->sk &&
     781                 :       73112 :             __mptcp_try_coalesce(sk, tail, skb, &fragstolen, &delta)) {
     782                 :       39305 :                 skb->truesize -= delta;
     783         [ -  + ]:       39305 :                 kfree_skb_partial(skb, fragstolen);
     784                 :       39305 :                 __mptcp_subflow_lend_fwdmem(subflow, delta);
     785                 :       39305 :                 goto account;
     786                 :             :         }
     787                 :             : 
     788                 :      255952 :         list_add_tail(&skb->list, &msk->backlog_list);
     789                 :      255952 :         mptcp_subflow_lend_fwdmem(subflow, skb);
     790                 :      255952 :         delta = skb->truesize;
     791                 :             : 
     792                 :      295257 : account:
     793                 :      295257 :         WRITE_ONCE(msk->backlog_len, msk->backlog_len + delta);
     794                 :             : 
     795                 :             :         /* Possibly not accept()ed yet, keep track of memory not CG
     796                 :             :          * accounted, mptcp_graft_subflows() will handle it.
     797                 :             :          */
     798         [ +  + ]:      295257 :         if (!mem_cgroup_from_sk(ssk))
     799                 :      248508 :                 msk->backlog_unaccounted += delta;
     800                 :             : }
     801                 :             : 
     802                 :     1241062 : static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
     803                 :             :                                            struct sock *ssk, bool own_msk)
     804                 :             : {
     805         [ -  + ]:     1241062 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
     806                 :     1241062 :         struct sock *sk = (struct sock *)msk;
     807                 :     1241062 :         bool more_data_avail;
     808                 :     1241062 :         struct tcp_sock *tp;
     809                 :     1241062 :         bool ret = false;
     810                 :             : 
     811   [ -  +  -  - ]:     1241062 :         pr_debug("msk=%p ssk=%p\n", msk, ssk);
     812         [ -  + ]:     1241062 :         tp = tcp_sk(ssk);
     813                 :     1289673 :         do {
     814                 :     1289673 :                 u32 map_remaining, offset;
     815                 :     1289673 :                 u32 seq = tp->copied_seq;
     816                 :     1289673 :                 struct sk_buff *skb;
     817                 :     1289673 :                 bool fin;
     818                 :             : 
     819                 :             :                 /* try to move as much data as available */
     820                 :     2579346 :                 map_remaining = subflow->map_data_len -
     821                 :     1289673 :                                 mptcp_subflow_get_map_offset(subflow);
     822                 :             : 
     823         [ +  - ]:     1289673 :                 skb = skb_peek(&ssk->sk_receive_queue);
     824         [ +  - ]:     1289673 :                 if (unlikely(!skb))
     825                 :             :                         break;
     826                 :             : 
     827         [ +  + ]:     1289673 :                 if (__mptcp_check_fallback(msk)) {
     828                 :             :                         /* Under fallback skbs have no MPTCP extension and TCP could
     829                 :             :                          * collapse them between the dummy map creation and the
     830                 :             :                          * current dequeue. Be sure to adjust the map size.
     831                 :             :                          */
     832                 :       13654 :                         map_remaining = skb->len;
     833                 :       13654 :                         subflow->map_data_len = skb->len;
     834                 :             :                 }
     835                 :             : 
     836                 :     1289673 :                 offset = seq - TCP_SKB_CB(skb)->seq;
     837                 :     1289673 :                 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
     838         [ +  + ]:     1289673 :                 if (fin)
     839                 :         154 :                         seq++;
     840                 :             : 
     841         [ +  + ]:     1289673 :                 if (offset < skb->len) {
     842                 :     1289628 :                         size_t len = skb->len - offset;
     843                 :             : 
     844                 :     1289628 :                         mptcp_init_skb(ssk, skb, offset, len);
     845                 :             : 
     846         [ +  + ]:     1289628 :                         if (own_msk) {
     847                 :      994369 :                                 mptcp_subflow_lend_fwdmem(subflow, skb);
     848                 :      994369 :                                 ret |= __mptcp_move_skb(sk, skb);
     849                 :             :                         } else {
     850                 :      295259 :                                 __mptcp_add_backlog(sk, subflow, skb);
     851                 :             :                         }
     852                 :     1289628 :                         seq += len;
     853                 :             : 
     854         [ -  + ]:     1289628 :                         if (unlikely(map_remaining < len)) {
     855                 :           0 :                                 DEBUG_NET_WARN_ON_ONCE(1);
     856                 :           0 :                                 mptcp_dss_corruption(msk, ssk);
     857                 :             :                         }
     858                 :             :                 } else {
     859         [ -  + ]:          45 :                         if (unlikely(!fin)) {
     860                 :           0 :                                 DEBUG_NET_WARN_ON_ONCE(1);
     861                 :           0 :                                 mptcp_dss_corruption(msk, ssk);
     862                 :             :                         }
     863                 :             : 
     864                 :          45 :                         sk_eat_skb(ssk, skb);
     865                 :             :                 }
     866                 :             : 
     867                 :     1289673 :                 WRITE_ONCE(tp->copied_seq, seq);
     868                 :     1289673 :                 more_data_avail = mptcp_subflow_data_available(ssk);
     869                 :             : 
     870         [ +  + ]:     1289673 :         } while (more_data_avail);
     871                 :             : 
     872         [ +  + ]:     1241062 :         if (ret)
     873                 :      763021 :                 msk->last_data_recv = tcp_jiffies32;
     874                 :     1241062 :         return ret;
     875                 :             : }
     876                 :             : 
     877                 :     1167962 : static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
     878                 :             : {
     879                 :     1167962 :         struct sock *sk = (struct sock *)msk;
     880                 :     1167962 :         struct sk_buff *skb, *tail;
     881                 :     1167962 :         bool moved = false;
     882                 :     1167962 :         struct rb_node *p;
     883                 :     1167962 :         u64 end_seq;
     884                 :             : 
     885         [ +  + ]:     1167962 :         p = rb_first(&msk->out_of_order_queue);
     886   [ -  +  -  - ]:     1167962 :         pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
     887         [ +  + ]:     1212252 :         while (p) {
     888                 :      357909 :                 skb = rb_to_skb(p);
     889         [ +  + ]:      357909 :                 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
     890                 :             :                         break;
     891                 :             : 
     892                 :       44290 :                 p = rb_next(p);
     893                 :       44290 :                 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
     894                 :             : 
     895         [ +  + ]:       44290 :                 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
     896                 :             :                                       msk->ack_seq))) {
     897                 :         156 :                         mptcp_drop(sk, skb);
     898         [ +  - ]:         156 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     899                 :         156 :                         continue;
     900                 :             :                 }
     901                 :             : 
     902                 :       44134 :                 end_seq = MPTCP_SKB_CB(skb)->end_seq;
     903         [ +  - ]:       44134 :                 tail = skb_peek_tail(&sk->sk_receive_queue);
     904   [ +  -  +  + ]:       87510 :                 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
     905                 :       28028 :                         int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
     906                 :             : 
     907                 :             :                         /* skip overlapping data, if any */
     908   [ -  +  -  - ]:       28028 :                         pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d\n",
     909                 :             :                                  MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
     910                 :             :                                  delta);
     911                 :       28028 :                         MPTCP_SKB_CB(skb)->offset += delta;
     912                 :       28028 :                         MPTCP_SKB_CB(skb)->map_seq += delta;
     913                 :       28028 :                         __skb_queue_tail(&sk->sk_receive_queue, skb);
     914                 :             :                 }
     915                 :       44134 :                 msk->bytes_received += end_seq - msk->ack_seq;
     916                 :       44134 :                 WRITE_ONCE(msk->ack_seq, end_seq);
     917                 :       44134 :                 moved = true;
     918                 :             :         }
     919                 :     1167962 :         return moved;
     920                 :             : }
     921                 :             : 
     922                 :           0 : static bool mptcp_errqueue_skb_forwardable(const struct sk_buff *skb)
     923                 :             : {
     924                 :             :         /* Subflow-level ICMP errors are dropped: the legacy RECVERR ABI
     925                 :             :          * cannot convey their per-subflow peer identity.
     926                 :             :          */
     927                 :          18 :         return SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING;
     928                 :             : }
     929                 :             : 
     930                 :        7798 : static bool __mptcp_subflow_splice_errqueue(struct sock *sk, struct sock *ssk)
     931                 :             : {
     932                 :        7798 :         struct sk_buff *skb;
     933                 :        7798 :         bool moved = false;
     934                 :             : 
     935         [ +  + ]:        7816 :         while ((skb = skb_dequeue(&ssk->sk_error_queue))) {
     936                 :             :                 /* sock_queue_err_skb() re-homes skb->sk onto the parent and
     937                 :             :                  * charges sk_rmem_alloc, bounding the queue by sk_rcvbuf.
     938                 :             :                  */
     939   [ -  +  -  - ]:          18 :                 if (!mptcp_errqueue_skb_forwardable(skb) ||
     940                 :           0 :                     sock_queue_err_skb(sk, skb)) {
     941                 :          18 :                         kfree_skb(skb);
     942                 :          18 :                         continue;
     943                 :             :                 }
     944                 :             :                 moved = true;
     945                 :             :         }
     946                 :             : 
     947                 :        7798 :         return moved;
     948                 :             : }
     949                 :             : 
     950                 :        7798 : static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
     951                 :             : {
     952                 :        7798 :         bool propagated = false;
     953                 :        7798 :         int ssk_state;
     954                 :        7798 :         bool report;
     955                 :        7798 :         int err;
     956                 :             : 
     957                 :        7798 :         report = __mptcp_subflow_splice_errqueue(sk, ssk);
     958                 :             : 
     959                 :             :         /* only propagate errors on fallen-back sockets or
     960                 :             :          * on MPC connect
     961                 :             :          */
     962   [ +  +  -  +  :        7798 :         if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
                   +  + ]
     963                 :        7558 :                 goto out;
     964                 :             : 
     965         [ +  + ]:         240 :         err = sock_error(ssk);
     966         [ +  + ]:         142 :         if (!err)
     967                 :         202 :                 goto out;
     968                 :             :         /* We need to propagate only transition to CLOSE state.
     969                 :             :          * Orphaned socket will see such state change via
     970                 :             :          * subflow_sched_work_if_closed() and that path will properly
     971                 :             :          * destroy the msk as needed.
     972                 :             :          */
     973                 :          38 :         ssk_state = inet_sk_state_load(ssk);
     974   [ +  -  +  + ]:          38 :         if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
     975                 :          26 :                 mptcp_set_state(sk, ssk_state);
     976                 :          38 :         WRITE_ONCE(sk->sk_err, -err);
     977                 :          38 :         report = true;
     978                 :          38 :         propagated = true;
     979                 :             : 
     980                 :        7760 : out:
     981         [ -  + ]:        7798 :         if (report) {
     982                 :             :                 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
     983                 :          38 :                 smp_wmb();
     984                 :          38 :                 sk_error_report(sk);
     985                 :             :         }
     986                 :        7798 :         return propagated;
     987                 :             : }
     988                 :             : 
     989                 :        1064 : void __mptcp_error_report(struct sock *sk)
     990                 :             : {
     991                 :        1064 :         struct mptcp_subflow_context *subflow;
     992         [ -  + ]:        1064 :         struct mptcp_sock *msk = mptcp_sk(sk);
     993                 :             : 
     994         [ +  + ]:        2449 :         mptcp_for_each_subflow(msk, subflow)
     995         [ +  + ]:        1411 :                 if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
     996                 :             :                         break;
     997                 :        1064 : }
     998                 :             : 
     999                 :             : /* In most cases we will be able to lock the mptcp socket.  If its already
    1000                 :             :  * owned, we need to defer to the work queue to avoid ABBA deadlock.
    1001                 :             :  */
    1002                 :      956820 : static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
    1003                 :             : {
    1004                 :      956820 :         struct sock *sk = (struct sock *)msk;
    1005                 :      956820 :         bool moved;
    1006                 :             : 
    1007                 :      956820 :         moved = __mptcp_move_skbs_from_subflow(msk, ssk, true);
    1008                 :      956820 :         __mptcp_ofo_queue(msk);
    1009         [ -  + ]:      956820 :         if (unlikely(ssk->sk_err))
    1010                 :           0 :                 __mptcp_subflow_error_report(sk, ssk);
    1011                 :             : 
    1012                 :             :         /* If the moves have caught up with the DATA_FIN sequence number
    1013                 :             :          * it's time to ack the DATA_FIN and change socket state, but
    1014                 :             :          * this is not a good place to change state. Let the workqueue
    1015                 :             :          * do it.
    1016                 :             :          */
    1017         [ +  + ]:      956820 :         if (mptcp_pending_data_fin(sk, NULL))
    1018                 :         564 :                 mptcp_schedule_work(sk);
    1019                 :      956820 :         return moved;
    1020                 :             : }
    1021                 :             : 
    1022                 :     1241062 : static void mptcp_rcv_rtt_update(struct mptcp_sock *msk,
    1023                 :             :                                  struct mptcp_subflow_context *subflow)
    1024                 :             : {
    1025         [ -  + ]:     1241062 :         const struct tcp_sock *tp = tcp_sk(subflow->tcp_sock);
    1026                 :     1241062 :         u32 rtt_us = tp->rcv_rtt_est.rtt_us;
    1027                 :     1241062 :         int id;
    1028                 :             : 
    1029                 :             :         /* Update once per subflow per rcvwnd to avoid touching the msk
    1030                 :             :          * too often.
    1031                 :             :          */
    1032   [ +  +  +  + ]:     1241062 :         if (!rtt_us || tp->rcv_rtt_est.seq == subflow->prev_rtt_seq)
    1033                 :             :                 return;
    1034                 :             : 
    1035                 :       37789 :         subflow->prev_rtt_seq = tp->rcv_rtt_est.seq;
    1036                 :             : 
    1037                 :             :         /* Pairs with READ_ONCE() in mptcp_rtt_us_est(). */
    1038                 :       37789 :         id = msk->rcv_rtt_est.next_sample;
    1039                 :       37789 :         WRITE_ONCE(msk->rcv_rtt_est.samples[id], rtt_us);
    1040         [ +  + ]:       37789 :         if (++msk->rcv_rtt_est.next_sample == MPTCP_RTT_SAMPLES)
    1041                 :        7045 :                 msk->rcv_rtt_est.next_sample = 0;
    1042                 :             : 
    1043                 :             :         /* EWMA among the incoming subflows */
    1044                 :       37789 :         msk->scaling_ratio = ((msk->scaling_ratio << 3) - msk->scaling_ratio +
    1045                 :       37789 :                              tp->scaling_ratio) >> 3;
    1046                 :             : }
    1047                 :             : 
    1048                 :     1241062 : void mptcp_data_ready(struct sock *sk, struct sock *ssk)
    1049                 :             : {
    1050         [ -  + ]:     1241062 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1051         [ -  + ]:     1241062 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1052                 :             : 
    1053                 :             :         /* The peer can send data while we are shutting down this
    1054                 :             :          * subflow at subflow destruction time, but we must avoid enqueuing
    1055                 :             :          * more data to the msk receive queue
    1056                 :             :          */
    1057         [ +  - ]:     1241062 :         if (unlikely(subflow->closing))
    1058                 :             :                 return;
    1059                 :             : 
    1060                 :     1241062 :         mptcp_data_lock(sk);
    1061                 :     1241062 :         mptcp_rcv_rtt_update(msk, subflow);
    1062         [ +  + ]:     1241062 :         if (!sock_owned_by_user(sk)) {
    1063                 :             :                 /* Wake-up the reader only for in-sequence data */
    1064   [ +  +  +  - ]:      956820 :                 if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk))
    1065                 :      763021 :                         sk->sk_data_ready(sk);
    1066                 :             :         } else {
    1067                 :      284242 :                 __mptcp_move_skbs_from_subflow(msk, ssk, false);
    1068                 :             :         }
    1069                 :     1241062 :         mptcp_data_unlock(sk);
    1070                 :             : }
    1071                 :             : 
    1072                 :        1278 : static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
    1073                 :             : {
    1074                 :        1278 :         mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
    1075                 :        1278 :         msk->allow_infinite_fallback = false;
    1076                 :        1278 :         mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
    1077                 :        1278 : }
    1078                 :             : 
    1079                 :         648 : static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
    1080                 :             : {
    1081                 :         648 :         struct sock *sk = (struct sock *)msk;
    1082                 :             : 
    1083         [ -  + ]:         648 :         if (sk->sk_state != TCP_ESTABLISHED)
    1084                 :             :                 return false;
    1085                 :             : 
    1086                 :         648 :         spin_lock_bh(&msk->fallback_lock);
    1087         [ -  + ]:         648 :         if (!msk->allow_subflows) {
           [ -  +  -  + ]
    1088                 :           0 :                 spin_unlock_bh(&msk->fallback_lock);
    1089                 :           0 :                 return false;
    1090                 :             :         }
    1091                 :         648 :         mptcp_subflow_joined(msk, ssk);
    1092                 :         648 :         spin_unlock_bh(&msk->fallback_lock);
    1093                 :             : 
    1094                 :         648 :         mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
    1095                 :         648 :         mptcp_sockopt_sync_locked(msk, ssk);
    1096                 :         648 :         mptcp_stop_tout_timer(sk);
    1097         [ +  - ]:         648 :         __mptcp_propagate_sndbuf(sk, ssk);
    1098                 :             :         return true;
    1099                 :             : }
    1100                 :             : 
    1101                 :          88 : static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
    1102                 :             : {
    1103                 :          88 :         struct mptcp_subflow_context *tmp, *subflow;
    1104         [ -  + ]:          88 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1105                 :             : 
    1106         [ +  + ]:         134 :         list_for_each_entry_safe(subflow, tmp, join_list, node) {
    1107                 :          46 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    1108                 :          46 :                 bool slow = lock_sock_fast(ssk);
    1109                 :             : 
    1110                 :          46 :                 list_move_tail(&subflow->node, &msk->conn_list);
    1111         [ -  + ]:          46 :                 if (!__mptcp_finish_join(msk, ssk))
    1112                 :           0 :                         mptcp_subflow_reset(ssk);
    1113                 :          46 :                 unlock_sock_fast(ssk, slow);
    1114                 :             :         }
    1115                 :          88 : }
    1116                 :             : 
    1117                 :      217606 : static bool mptcp_rtx_timer_pending(struct sock *sk)
    1118                 :             : {
    1119                 :      687702 :         return timer_pending(&sk->mptcp_retransmit_timer);
    1120                 :             : }
    1121                 :             : 
    1122                 :      465970 : static void mptcp_reset_rtx_timer(struct sock *sk)
    1123                 :             : {
    1124                 :      465970 :         unsigned long tout;
    1125                 :             : 
    1126                 :             :         /* prevent rescheduling on close */
    1127         [ +  + ]:      465970 :         if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
    1128                 :             :                 return;
    1129                 :             : 
    1130         [ -  + ]:      465903 :         tout = mptcp_sk(sk)->timer_ival;
    1131                 :      465903 :         sk_reset_timer(sk, &sk->mptcp_retransmit_timer, jiffies + tout);
    1132                 :             : }
    1133                 :             : 
    1134                 :       16288 : bool mptcp_schedule_work(struct sock *sk)
    1135                 :             : {
    1136         [ +  + ]:       16288 :         if (inet_sk_state_load(sk) == TCP_CLOSE)
    1137                 :             :                 return false;
    1138                 :             : 
    1139                 :             :         /* Get a reference on this socket, mptcp_worker() will release it.
    1140                 :             :          * As mptcp_worker() might complete before us, we can not avoid
    1141                 :             :          * a sock_hold()/sock_put() if schedule_work() returns false.
    1142                 :             :          */
    1143                 :       15478 :         sock_hold(sk);
    1144                 :             : 
    1145   [ -  +  +  + ]:       15478 :         if (schedule_work(&mptcp_sk(sk)->work))
    1146                 :             :                 return true;
    1147                 :             : 
    1148                 :        1700 :         sock_put(sk);
    1149                 :        1700 :         return false;
    1150                 :             : }
    1151                 :             : 
    1152                 :      950068 : static bool mptcp_skb_can_collapse_to(u64 write_seq,
    1153                 :             :                                       const struct sk_buff *skb,
    1154                 :             :                                       const struct mptcp_ext *mpext)
    1155                 :             : {
    1156         [ +  + ]:      950068 :         if (!tcp_skb_can_collapse_to(skb))
    1157                 :             :                 return false;
    1158                 :             : 
    1159                 :             :         /* can collapse only if MPTCP level sequence is in order and this
    1160                 :             :          * mapping has not been xmitted yet
    1161                 :             :          */
    1162   [ +  -  +  + ]:     1785232 :         return mpext && mpext->data_seq + mpext->data_len == write_seq &&
    1163         [ +  + ]:      851490 :                !mpext->frozen;
    1164                 :             : }
    1165                 :             : 
    1166                 :             : /* we can append data to the given data frag if:
    1167                 :             :  * - there is space available in the backing page_frag
    1168                 :             :  * - the data frag tail matches the current page_frag free offset
    1169                 :             :  * - the data frag end sequence number matches the current write seq
    1170                 :             :  */
    1171                 :      896896 : static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
    1172                 :             :                                        const struct page_frag *pfrag,
    1173                 :             :                                        const struct mptcp_data_frag *df)
    1174                 :             : {
    1175         [ +  - ]:      399944 :         return df && !df->eor &&
    1176         [ +  - ]:      399944 :                 pfrag->page == df->page &&
    1177         [ +  + ]:      399944 :                 pfrag->size - pfrag->offset > 0 &&
    1178   [ +  +  +  - ]:     1100424 :                 pfrag->offset == (df->offset + df->data_len) &&
    1179         [ -  + ]:      203528 :                 df->data_seq + df->data_len == msk->write_seq;
    1180                 :             : }
    1181                 :             : 
    1182                 :      216649 : static void dfrag_uncharge(struct sock *sk, int len)
    1183                 :             : {
    1184                 :      988517 :         sk_mem_uncharge(sk, len);
    1185                 :      988517 :         sk_wmem_queued_add(sk, -len);
    1186                 :      295158 : }
    1187                 :             : 
    1188                 :      693359 : static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
    1189                 :             : {
    1190                 :      693359 :         int len = dfrag->data_len + dfrag->overhead;
    1191                 :             : 
    1192                 :      693359 :         list_del(&dfrag->list);
    1193                 :      693359 :         dfrag_uncharge(sk, len);
    1194                 :      693359 :         put_page(dfrag->page);
    1195                 :      693359 : }
    1196                 :             : 
    1197                 :             : /* called under both the msk socket lock and the data lock */
    1198                 :      474332 : static void __mptcp_clean_una(struct sock *sk)
    1199                 :             : {
    1200         [ -  + ]:      474332 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1201                 :      474332 :         struct mptcp_data_frag *dtmp, *dfrag;
    1202                 :      474332 :         u64 snd_una;
    1203                 :             : 
    1204                 :      474332 :         snd_una = msk->snd_una;
    1205         [ +  + ]:     1167515 :         list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
    1206         [ +  + ]:     1098522 :                 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
    1207                 :             :                         break;
    1208                 :             : 
    1209         [ -  + ]:      693183 :                 if (unlikely(dfrag == msk->first_pending)) {
    1210                 :             :                         /* in recovery mode can see ack after the current snd head */
    1211         [ #  # ]:           0 :                         if (WARN_ON_ONCE(!msk->recovery))
           [ #  #  #  # ]
    1212                 :             :                                 break;
    1213                 :             : 
    1214                 :           0 :                         msk->first_pending = mptcp_send_next(sk);
    1215                 :             :                 }
    1216                 :             : 
    1217                 :      693183 :                 dfrag_clear(sk, dfrag);
    1218                 :             :         }
    1219                 :             : 
    1220                 :      474332 :         dfrag = mptcp_rtx_head(sk);
    1221   [ +  +  +  + ]:      474332 :         if (dfrag && after64(snd_una, dfrag->data_seq)) {
    1222                 :      295158 :                 u64 delta = snd_una - dfrag->data_seq;
    1223                 :             : 
    1224                 :             :                 /* prevent wrap around in recovery mode */
    1225         [ -  + ]:      295158 :                 if (unlikely(delta > dfrag->already_sent)) {
    1226         [ #  # ]:           0 :                         if (WARN_ON_ONCE(!msk->recovery))
           [ #  #  #  # ]
    1227                 :           0 :                                 goto out;
    1228   [ #  #  #  # ]:           0 :                         if (WARN_ON_ONCE(delta > dfrag->data_len))
    1229                 :           0 :                                 goto out;
    1230                 :           0 :                         dfrag->already_sent += delta - dfrag->already_sent;
    1231                 :             :                 }
    1232                 :             : 
    1233                 :      295158 :                 dfrag->data_seq += delta;
    1234                 :      295158 :                 dfrag->offset += delta;
    1235                 :      295158 :                 dfrag->data_len -= delta;
    1236                 :      295158 :                 dfrag->already_sent -= delta;
    1237                 :             : 
    1238                 :      295158 :                 dfrag_uncharge(sk, delta);
    1239                 :             :         }
    1240                 :             : 
    1241                 :             :         /* all retransmitted data acked, recovery completed */
    1242   [ +  +  +  + ]:      474332 :         if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
           [ -  +  +  +  
                   +  + ]
    1243                 :          51 :                 msk->recovery = false;
    1244                 :             : 
    1245                 :      422744 : out:
    1246   [ +  +  +  + ]:      474332 :         if (snd_una == msk->snd_nxt && snd_una == msk->write_seq) {
    1247         [ +  + ]:       66251 :                 if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
           [ +  +  +  + ]
    1248                 :       49776 :                         mptcp_stop_rtx_timer(sk);
    1249                 :             :         } else {
    1250                 :      408081 :                 mptcp_reset_rtx_timer(sk);
    1251                 :             :         }
    1252                 :             : 
    1253         [ +  + ]:      474332 :         if (mptcp_pending_data_fin_ack(sk))
    1254                 :        2568 :                 mptcp_schedule_work(sk);
    1255                 :      474332 : }
    1256                 :             : 
    1257                 :      137743 : static void __mptcp_clean_una_wakeup(struct sock *sk)
    1258                 :             : {
    1259   [ +  -  -  + ]:      173156 :         lockdep_assert_held_once(&sk->sk_lock.slock);
    1260                 :             : 
    1261                 :      173156 :         __mptcp_clean_una(sk);
    1262                 :      152891 :         mptcp_write_space(sk);
    1263                 :      172124 : }
    1264                 :             : 
    1265                 :           0 : static void mptcp_enter_memory_pressure(struct sock *sk)
    1266                 :             : {
    1267                 :           0 :         struct mptcp_subflow_context *subflow;
    1268         [ #  # ]:           0 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1269                 :           0 :         bool first = true;
    1270                 :             : 
    1271         [ #  # ]:           0 :         mptcp_for_each_subflow(msk, subflow) {
    1272         [ #  # ]:           0 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    1273                 :             : 
    1274   [ #  #  #  # ]:           0 :                 if (first && !ssk->sk_bypass_prot_mem) {
    1275                 :           0 :                         tcp_enter_memory_pressure(ssk);
    1276                 :           0 :                         first = false;
    1277                 :             :                 }
    1278                 :             : 
    1279                 :           0 :                 sk_stream_moderate_sndbuf(ssk);
    1280                 :             :         }
    1281                 :           0 :         __mptcp_sync_sndbuf(sk);
    1282                 :           0 : }
    1283                 :             : 
    1284                 :             : /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
    1285                 :             :  * data
    1286                 :             :  */
    1287                 :      693368 : static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
    1288                 :             : {
    1289         [ -  + ]:      693368 :         if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
    1290                 :             :                                         pfrag, sk->sk_allocation)))
    1291                 :             :                 return true;
    1292                 :             : 
    1293                 :           0 :         mptcp_enter_memory_pressure(sk);
    1294                 :           0 :         return false;
    1295                 :             : }
    1296                 :             : 
    1297                 :             : static struct mptcp_data_frag *
    1298                 :      693368 : mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
    1299                 :             :                       int orig_offset)
    1300                 :             : {
    1301                 :      693368 :         int offset = ALIGN(orig_offset, sizeof(long));
    1302                 :      693368 :         struct mptcp_data_frag *dfrag;
    1303                 :             : 
    1304                 :      693368 :         dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
    1305                 :      693368 :         dfrag->data_len = 0;
    1306                 :      693368 :         dfrag->data_seq = msk->write_seq;
    1307                 :      693368 :         dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
    1308                 :      693368 :         dfrag->offset = offset + sizeof(struct mptcp_data_frag);
    1309                 :      693368 :         dfrag->already_sent = 0;
    1310                 :      693368 :         dfrag->page = pfrag->page;
    1311                 :      693368 :         dfrag->eor = 0;
    1312                 :             : 
    1313                 :      693368 :         return dfrag;
    1314                 :             : }
    1315                 :             : 
    1316                 :             : struct mptcp_sendmsg_info {
    1317                 :             :         int mss_now;
    1318                 :             :         int size_goal;
    1319                 :             :         u16 limit;
    1320                 :             :         u16 sent;
    1321                 :             :         unsigned int flags;
    1322                 :             :         bool data_lock_held;
    1323                 :             : };
    1324                 :             : 
    1325                 :     1464834 : static size_t mptcp_check_allowed_size(const struct mptcp_sock *msk,
    1326                 :             :                                        struct sock *ssk, u64 data_seq,
    1327                 :             :                                        size_t avail_size)
    1328                 :             : {
    1329                 :     1464834 :         u64 window_end = mptcp_wnd_end(msk);
    1330                 :     1464834 :         u64 mptcp_snd_wnd;
    1331                 :             : 
    1332         [ +  + ]:     1464834 :         if (__mptcp_check_fallback(msk))
    1333                 :             :                 return avail_size;
    1334                 :             : 
    1335                 :     1398852 :         mptcp_snd_wnd = window_end - data_seq;
    1336                 :     1398852 :         avail_size = min(mptcp_snd_wnd, avail_size);
    1337                 :             : 
    1338   [ -  +  +  + ]:     1398852 :         if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
    1339         [ -  + ]:       12978 :                 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
    1340         [ +  - ]:       12978 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
    1341                 :             :         }
    1342                 :             : 
    1343                 :             :         return avail_size;
    1344                 :             : }
    1345                 :             : 
    1346                 :      683699 : static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
    1347                 :             : {
    1348                 :      683699 :         struct skb_ext *mpext = __skb_ext_alloc(gfp);
    1349                 :             : 
    1350         [ +  - ]:      683699 :         if (!mpext)
    1351                 :             :                 return false;
    1352                 :      683699 :         __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
    1353                 :      683699 :         return true;
    1354                 :             : }
    1355                 :             : 
    1356                 :      683699 : static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
    1357                 :             : {
    1358                 :      683699 :         struct sk_buff *skb;
    1359                 :             : 
    1360                 :      683699 :         skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
    1361         [ +  - ]:      683699 :         if (likely(skb)) {
    1362         [ +  - ]:      683699 :                 if (likely(__mptcp_add_ext(skb, gfp))) {
    1363                 :      683699 :                         skb_reserve(skb, MAX_TCP_HEADER);
    1364                 :      683699 :                         skb->ip_summed = CHECKSUM_PARTIAL;
    1365                 :      683699 :                         INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
    1366                 :      683699 :                         return skb;
    1367                 :             :                 }
    1368                 :           0 :                 __kfree_skb(skb);
    1369                 :             :         } else {
    1370                 :           0 :                 mptcp_enter_memory_pressure(sk);
    1371                 :             :         }
    1372                 :             :         return NULL;
    1373                 :             : }
    1374                 :             : 
    1375                 :      683699 : static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
    1376                 :             : {
    1377                 :      683699 :         struct sk_buff *skb;
    1378                 :             : 
    1379                 :      683699 :         skb = __mptcp_do_alloc_tx_skb(sk, gfp);
    1380         [ -  + ]:      683699 :         if (!skb)
    1381                 :             :                 return NULL;
    1382                 :             : 
    1383         [ +  - ]:      683699 :         if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
    1384                 :      683699 :                 tcp_skb_entail(ssk, skb);
    1385                 :      683699 :                 return skb;
    1386                 :             :         }
    1387                 :           0 :         tcp_skb_tsorted_anchor_cleanup(skb);
    1388                 :           0 :         kfree_skb(skb);
    1389                 :           0 :         return NULL;
    1390                 :             : }
    1391                 :             : 
    1392                 :       92794 : static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
    1393                 :             : {
    1394                 :      430981 :         gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
    1395                 :             : 
    1396                 :      683699 :         return __mptcp_alloc_tx_skb(sk, ssk, gfp);
    1397                 :             : }
    1398                 :             : 
    1399                 :             : /* note: this always recompute the csum on the whole skb, even
    1400                 :             :  * if we just appended a single frag. More status info needed
    1401                 :             :  */
    1402                 :       78595 : static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
    1403                 :             : {
    1404         [ +  - ]:       78595 :         struct mptcp_ext *mpext = mptcp_get_ext(skb);
    1405                 :       78595 :         __wsum csum = ~csum_unfold(mpext->csum);
    1406                 :       78595 :         int offset = skb->len - added;
    1407                 :             : 
    1408         [ #  # ]:       78595 :         mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
    1409                 :       78595 : }
    1410                 :             : 
    1411                 :           2 : static void mptcp_update_infinite_map(struct mptcp_sock *msk,
    1412                 :             :                                       struct sock *ssk,
    1413                 :             :                                       struct mptcp_ext *mpext)
    1414                 :             : {
    1415         [ +  - ]:           2 :         if (!mpext)
    1416                 :             :                 return;
    1417                 :             : 
    1418                 :           2 :         mpext->infinite_map = 1;
    1419                 :           2 :         mpext->data_len = 0;
    1420                 :             : 
    1421         [ -  + ]:           2 :         if (!mptcp_try_fallback(ssk, MPTCP_MIB_INFINITEMAPTX)) {
    1422         [ #  # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_FALLBACKFAILED);
    1423                 :           0 :                 mptcp_subflow_reset(ssk);
    1424                 :           0 :                 return;
    1425                 :             :         }
    1426                 :             : 
    1427                 :           2 :         mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
    1428                 :             : }
    1429                 :             : 
    1430                 :             : #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1))
    1431                 :             : 
    1432                 :     1464835 : static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
    1433                 :             :                               struct mptcp_data_frag *dfrag,
    1434                 :             :                               struct mptcp_sendmsg_info *info)
    1435                 :             : {
    1436                 :     1464835 :         u64 data_seq = dfrag->data_seq + info->sent;
    1437                 :     1464835 :         int offset = dfrag->offset + info->sent;
    1438         [ -  + ]:     1464835 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1439                 :     1464835 :         bool zero_window_probe = false;
    1440                 :     1464835 :         struct mptcp_ext *mpext = NULL;
    1441                 :     1464835 :         bool can_coalesce = false;
    1442                 :     1464835 :         bool reuse_skb = true;
    1443                 :     1464835 :         struct sk_buff *skb;
    1444                 :     1464835 :         size_t copy;
    1445                 :     1464835 :         int i;
    1446                 :             : 
    1447   [ -  +  -  - ]:     1464835 :         pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u\n",
    1448                 :             :                  msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
    1449                 :             : 
    1450   [ +  -  -  + ]:     1464835 :         if (WARN_ON_ONCE(info->sent > info->limit ||
    1451                 :             :                          info->limit > dfrag->data_len))
    1452                 :           0 :                 return 0;
    1453                 :             : 
    1454         [ +  + ]:     1464835 :         if (unlikely(!__tcp_can_send(ssk)))
    1455                 :             :                 return -EAGAIN;
    1456                 :             : 
    1457                 :             :         /* compute send limit */
    1458         [ -  + ]:     1464834 :         if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
    1459                 :           0 :                 ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
    1460                 :     1464834 :         info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
    1461                 :     1464834 :         copy = info->size_goal;
    1462                 :             : 
    1463         [ +  + ]:     1464834 :         skb = tcp_write_queue_tail(ssk);
    1464   [ +  -  +  + ]:     1251782 :         if (skb && copy > skb->len) {
    1465                 :             :                 /* Limit the write to the size available in the
    1466                 :             :                  * current skb, if any, so that we create at most a new skb.
    1467                 :             :                  * Explicitly tells TCP internals to avoid collapsing on later
    1468                 :             :                  * queue management operation, to avoid breaking the ext <->
    1469                 :             :                  * SSN association set here
    1470                 :             :                  */
    1471         [ +  - ]:      950068 :                 mpext = mptcp_get_ext(skb);
    1472         [ +  + ]:      950068 :                 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
    1473                 :      163179 :                         TCP_SKB_CB(skb)->eor = 1;
    1474         [ -  + ]:      163179 :                         tcp_mark_push(tcp_sk(ssk), skb);
    1475                 :      163179 :                         goto alloc_skb;
    1476                 :             :                 }
    1477                 :             : 
    1478         [ +  + ]:      786889 :                 i = skb_shinfo(skb)->nr_frags;
    1479         [ +  + ]:      786889 :                 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
    1480   [ +  +  +  + ]:      786889 :                 if (!can_coalesce && i >= READ_ONCE(net_hotdata.sysctl_max_skb_frags)) {
    1481         [ -  + ]:        5754 :                         tcp_mark_push(tcp_sk(ssk), skb);
    1482                 :        5754 :                         goto alloc_skb;
    1483                 :             :                 }
    1484                 :             : 
    1485                 :      781135 :                 copy -= skb->len;
    1486                 :             :         } else {
    1487         [ +  + ]:       92794 : alloc_skb:
    1488         [ +  + ]:      683699 :                 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
           [ -  +  +  + ]
    1489         [ -  + ]:      683699 :                 if (!skb)
    1490                 :             :                         return -ENOMEM;
    1491                 :             : 
    1492         [ +  - ]:      683699 :                 i = skb_shinfo(skb)->nr_frags;
    1493                 :      683699 :                 reuse_skb = false;
    1494         [ +  - ]:      683699 :                 mpext = mptcp_get_ext(skb);
    1495                 :             :         }
    1496                 :             : 
    1497                 :             :         /* Zero window and all data acked? Probe. */
    1498                 :     1464834 :         copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
    1499         [ +  + ]:     1464834 :         if (copy == 0) {
    1500                 :      357403 :                 u64 snd_una = READ_ONCE(msk->snd_una);
    1501                 :             : 
    1502                 :             :                 /* No need for zero probe if there are any data pending
    1503                 :             :                  * either at the msk or ssk level; skb is the current write
    1504                 :             :                  * queue tail and can be empty at this point.
    1505                 :             :                  */
    1506   [ +  +  +  +  :      368362 :                 if (snd_una != msk->snd_nxt || skb->len ||
                   +  + ]
           [ +  +  +  - ]
    1507         [ +  + ]:         478 :                     skb != tcp_send_head(ssk)) {
    1508                 :      354467 :                         tcp_remove_empty_skb(ssk);
    1509                 :      354467 :                         return 0;
    1510                 :             :                 }
    1511                 :             : 
    1512                 :        2936 :                 zero_window_probe = true;
    1513                 :        2936 :                 data_seq = snd_una - 1;
    1514                 :        2936 :                 copy = 1;
    1515                 :             :         }
    1516                 :             : 
    1517                 :     1110367 :         copy = min_t(size_t, copy, info->limit - info->sent);
    1518         [ -  + ]:     1110367 :         if (!sk_wmem_schedule(ssk, copy)) {
    1519                 :           0 :                 tcp_remove_empty_skb(ssk);
    1520                 :           0 :                 return -ENOMEM;
    1521                 :             :         }
    1522                 :             : 
    1523         [ +  + ]:     1110367 :         if (can_coalesce) {
    1524                 :       53962 :                 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
    1525                 :             :         } else {
    1526                 :     1056405 :                 get_page(dfrag->page);
    1527         [ -  + ]:     1282886 :                 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
    1528                 :             :         }
    1529                 :             : 
    1530                 :     1110367 :         skb->len += copy;
    1531                 :     1110367 :         skb->data_len += copy;
    1532                 :     1110367 :         skb->truesize += copy;
    1533         [ +  - ]:     1110367 :         sk_wmem_queued_add(ssk, copy);
    1534         [ +  - ]:     1110367 :         sk_mem_charge(ssk, copy);
    1535   [ -  +  -  + ]:     1110367 :         WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
    1536                 :     1110367 :         TCP_SKB_CB(skb)->end_seq += copy;
    1537         [ +  + ]:     1110367 :         tcp_skb_pcount_set(skb, 0);
    1538                 :             : 
    1539                 :             :         /* on skb reuse we just need to update the DSS len */
    1540         [ +  + ]:     1110367 :         if (reuse_skb) {
    1541                 :      511127 :                 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
    1542                 :      511127 :                 mpext->data_len += copy;
    1543                 :      511127 :                 goto out;
    1544                 :             :         }
    1545                 :             : 
    1546                 :      599240 :         memset(mpext, 0, sizeof(*mpext));
    1547                 :      599240 :         mpext->data_seq = data_seq;
    1548         [ -  + ]:      599240 :         mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
    1549                 :      599240 :         mpext->data_len = copy;
    1550                 :      599240 :         mpext->use_map = 1;
    1551                 :      599240 :         mpext->dsn64 = 1;
    1552                 :             : 
    1553   [ -  +  -  - ]:      599240 :         pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d\n",
    1554                 :             :                  mpext->data_seq, mpext->subflow_seq, mpext->data_len,
    1555                 :             :                  mpext->dsn64);
    1556                 :             : 
    1557         [ +  + ]:      599240 :         if (zero_window_probe) {
    1558         [ +  - ]:        2936 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_WINPROBE);
    1559         [ +  + ]:        2936 :                 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
    1560                 :        2936 :                 mpext->frozen = 1;
    1561         [ +  + ]:        2936 :                 if (READ_ONCE(msk->csum_enabled))
           [ -  +  +  + ]
    1562                 :         496 :                         mptcp_update_data_checksum(skb, copy);
    1563                 :        2936 :                 tcp_push_pending_frames(ssk);
    1564                 :        2936 :                 return 0;
    1565                 :             :         }
    1566                 :      596304 : out:
    1567         [ +  + ]:     1107431 :         if (READ_ONCE(msk->csum_enabled))
           [ -  +  +  + ]
    1568                 :       78099 :                 mptcp_update_data_checksum(skb, copy);
    1569         [ +  + ]:     1107431 :         if (mptcp_subflow_ctx(ssk)->send_infinite_map)
    1570                 :           2 :                 mptcp_update_infinite_map(msk, ssk, mpext);
    1571                 :     1107431 :         trace_mptcp_sendmsg_frag(mpext);
    1572         [ +  + ]:     1107431 :         mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
    1573                 :             : 
    1574                 :             :         /* if this is the last chunk of a dfrag with MSG_EOR set,
    1575                 :             :          * mark the skb to prevent coalescing with subsequent data.
    1576                 :             :          */
    1577   [ +  +  -  + ]:     1107431 :         if (dfrag->eor && info->sent + copy >= dfrag->data_len)
    1578                 :           6 :                 TCP_SKB_CB(skb)->eor = 1;
    1579                 :             : 
    1580                 :             :         return copy;
    1581                 :             : }
    1582                 :             : 
    1583                 :             : #define MPTCP_SEND_BURST_SIZE           ((1 << 16) - \
    1584                 :             :                                          sizeof(struct tcphdr) - \
    1585                 :             :                                          MAX_TCP_OPTION_SPACE - \
    1586                 :             :                                          sizeof(struct ipv6hdr) - \
    1587                 :             :                                          sizeof(struct frag_hdr))
    1588                 :             : 
    1589                 :             : struct subflow_send_info {
    1590                 :             :         struct sock *ssk;
    1591                 :             :         u64 linger_time;
    1592                 :             : };
    1593                 :             : 
    1594                 :        3445 : void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
    1595                 :             : {
    1596         [ -  + ]:        3445 :         if (!subflow->stale)
    1597                 :             :                 return;
    1598                 :             : 
    1599                 :           0 :         subflow->stale = 0;
    1600         [ #  # ]:           0 :         MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
    1601                 :             : }
    1602                 :             : 
    1603                 :     1809294 : bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
    1604                 :             : {
    1605         [ +  + ]:     1809294 :         if (unlikely(subflow->stale)) {
    1606         [ -  + ]:      124859 :                 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
    1607                 :             : 
    1608         [ -  + ]:      124859 :                 if (subflow->stale_rcv_tstamp == rcv_tstamp)
    1609                 :             :                         return false;
    1610                 :             : 
    1611                 :           0 :                 mptcp_subflow_set_active(subflow);
    1612                 :             :         }
    1613                 :     1684435 :         return __mptcp_subflow_active(subflow);
    1614                 :             : }
    1615                 :             : 
    1616                 :             : #define SSK_MODE_ACTIVE 0
    1617                 :             : #define SSK_MODE_BACKUP 1
    1618                 :             : #define SSK_MODE_MAX    2
    1619                 :             : 
    1620                 :             : /* implement the mptcp packet scheduler;
    1621                 :             :  * returns the subflow that will transmit the next DSS
    1622                 :             :  * additionally updates the rtx timeout
    1623                 :             :  */
    1624                 :      792640 : struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
    1625                 :             : {
    1626                 :      792640 :         struct subflow_send_info send_info[SSK_MODE_MAX];
    1627                 :      792640 :         struct mptcp_subflow_context *subflow;
    1628                 :      792640 :         struct sock *sk = (struct sock *)msk;
    1629                 :      792640 :         u32 pace, burst, wmem;
    1630                 :      792640 :         int i, nr_active = 0;
    1631                 :      792640 :         struct sock *ssk;
    1632                 :      792640 :         u64 linger_time;
    1633                 :      792640 :         long tout = 0;
    1634                 :             : 
    1635                 :             :         /* pick the subflow with the lower wmem/wspace ratio */
    1636         [ +  + ]:     2377920 :         for (i = 0; i < SSK_MODE_MAX; ++i) {
    1637                 :     1585280 :                 send_info[i].ssk = NULL;
    1638                 :     1585280 :                 send_info[i].linger_time = -1;
    1639                 :             :         }
    1640                 :             : 
    1641         [ +  + ]:     2100189 :         mptcp_for_each_subflow(msk, subflow) {
    1642   [ +  +  +  + ]:     1307549 :                 bool backup = subflow->backup || subflow->request_bkup;
    1643                 :             : 
    1644                 :     1307549 :                 trace_mptcp_subflow_get_send(subflow);
    1645                 :     1307549 :                 ssk =  mptcp_subflow_tcp_sock(subflow);
    1646         [ +  + ]:     1307549 :                 if (!mptcp_subflow_active(subflow))
    1647                 :      126208 :                         continue;
    1648                 :             : 
    1649         [ +  + ]:     1181341 :                 tout = max(tout, mptcp_timeout_from_subflow(subflow));
    1650                 :     1181341 :                 nr_active += !backup;
    1651                 :     1181341 :                 pace = subflow->avg_pacing_rate;
    1652         [ +  + ]:     1181341 :                 if (unlikely(!pace)) {
    1653                 :             :                         /* init pacing rate from socket */
    1654                 :        3548 :                         subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
    1655                 :        3548 :                         pace = subflow->avg_pacing_rate;
    1656         [ -  + ]:        3548 :                         if (!pace)
    1657                 :           0 :                                 continue;
    1658                 :             :                 }
    1659                 :             : 
    1660         [ +  + ]:     1181341 :                 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
    1661         [ +  + ]:     1181341 :                 if (linger_time < send_info[backup].linger_time) {
    1662                 :      937781 :                         send_info[backup].ssk = ssk;
    1663                 :      937781 :                         send_info[backup].linger_time = linger_time;
    1664                 :             :                 }
    1665                 :             :         }
    1666                 :      792640 :         __mptcp_set_timeout(sk, tout);
    1667                 :             : 
    1668                 :             :         /* pick the best backup if no other subflow is active */
    1669         [ +  + ]:      792640 :         if (!nr_active)
    1670                 :       46035 :                 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
    1671                 :             : 
    1672                 :             :         /* According to the blest algorithm, to avoid HoL blocking for the
    1673                 :             :          * faster flow, we need to:
    1674                 :             :          * - estimate the faster flow linger time
    1675                 :             :          * - use the above to estimate the amount of byte transferred
    1676                 :             :          *   by the faster flow
    1677                 :             :          * - check that the amount of queued data is greater than the above,
    1678                 :             :          *   otherwise do not use the picked, slower, subflow
    1679                 :             :          * We select the subflow with the shorter estimated time to flush
    1680                 :             :          * the queued mem, which basically ensure the above. We just need
    1681                 :             :          * to check that subflow has a non empty cwin.
    1682                 :             :          */
    1683                 :      792640 :         ssk = send_info[SSK_MODE_ACTIVE].ssk;
    1684   [ +  +  +  + ]:     1585244 :         if (!ssk || !sk_stream_memory_free(ssk))
    1685                 :      156315 :                 return NULL;
    1686                 :             : 
    1687                 :      636325 :         burst = min(MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
    1688                 :      636325 :         wmem = READ_ONCE(ssk->sk_wmem_queued);
    1689         [ +  + ]:      636325 :         if (!burst)
    1690                 :             :                 return ssk;
    1691                 :             : 
    1692                 :      397586 :         subflow = mptcp_subflow_ctx(ssk);
    1693                 :      397586 :         subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
    1694                 :      397586 :                                            READ_ONCE(ssk->sk_pacing_rate) * burst,
    1695                 :             :                                            burst + wmem);
    1696                 :      397586 :         msk->snd_burst = burst;
    1697                 :      397586 :         return ssk;
    1698                 :             : }
    1699                 :             : 
    1700                 :      710832 : static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
    1701                 :             : {
    1702         [ -  + ]:      710832 :         tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
    1703                 :      710832 :         release_sock(ssk);
    1704                 :      710832 : }
    1705                 :             : 
    1706                 :     1097587 : static void mptcp_update_post_push(struct mptcp_sock *msk,
    1707                 :             :                                    struct mptcp_data_frag *dfrag,
    1708                 :             :                                    u32 sent)
    1709                 :             : {
    1710                 :     1097587 :         u64 snd_nxt_new = dfrag->data_seq;
    1711                 :             : 
    1712                 :     1097587 :         dfrag->already_sent += sent;
    1713                 :             : 
    1714                 :     1097587 :         msk->snd_burst -= sent;
    1715                 :             : 
    1716                 :     1097587 :         snd_nxt_new += dfrag->already_sent;
    1717                 :             : 
    1718                 :             :         /* snd_nxt_new can be smaller than snd_nxt in case mptcp
    1719                 :             :          * is recovering after a failover. In that event, this re-sends
    1720                 :             :          * old segments.
    1721                 :             :          *
    1722                 :             :          * Thus compute snd_nxt_new candidate based on
    1723                 :             :          * the dfrag->data_seq that was sent and the data
    1724                 :             :          * that has been handed to the subflow for transmission
    1725                 :             :          * and skip update in case it was old dfrag.
    1726                 :             :          */
    1727         [ +  + ]:     1097587 :         if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
    1728                 :     1092239 :                 msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
    1729                 :     1092239 :                 WRITE_ONCE(msk->snd_nxt, snd_nxt_new);
    1730                 :             :         }
    1731                 :     1097587 : }
    1732                 :             : 
    1733                 :        8285 : void mptcp_check_and_set_pending(struct sock *sk)
    1734                 :             : {
    1735         [ +  + ]:        8285 :         if (mptcp_send_head(sk)) {
    1736                 :         218 :                 mptcp_data_lock(sk);
    1737         [ -  + ]:         218 :                 mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
    1738                 :         218 :                 mptcp_data_unlock(sk);
    1739                 :             :         }
    1740                 :        8285 : }
    1741                 :             : 
    1742                 :      949891 : static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
    1743                 :             :                                   struct mptcp_sendmsg_info *info)
    1744                 :             : {
    1745         [ -  + ]:      949891 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1746                 :      340561 :         struct mptcp_data_frag *dfrag;
    1747                 :      340561 :         int len, copied = 0, err = 0;
    1748                 :             : 
    1749         [ +  + ]:     1366062 :         while ((dfrag = mptcp_send_head(sk))) {
    1750                 :     1055655 :                 info->sent = dfrag->already_sent;
    1751                 :     1055655 :                 info->limit = dfrag->data_len;
    1752                 :     1055655 :                 len = dfrag->data_len - dfrag->already_sent;
    1753         [ +  + ]:     2153242 :                 while (len > 0) {
    1754                 :     1454989 :                         int ret = 0;
    1755                 :             : 
    1756                 :     1454989 :                         ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
    1757         [ +  + ]:     1454989 :                         if (ret <= 0) {
    1758         [ +  + ]:      357402 :                                 err = copied ? : ret;
    1759                 :      357402 :                                 goto out;
    1760                 :             :                         }
    1761                 :             : 
    1762                 :     1097587 :                         info->sent += ret;
    1763                 :     1097587 :                         copied += ret;
    1764                 :     1097587 :                         len -= ret;
    1765                 :             : 
    1766                 :     1097587 :                         mptcp_update_post_push(msk, dfrag, ret);
    1767                 :             :                 }
    1768                 :      698253 :                 msk->first_pending = mptcp_send_next(sk);
    1769                 :             : 
    1770   [ +  +  +  + ]:     1089661 :                 if (msk->snd_burst <= 0 ||
                 [ +  + ]
    1771         [ -  + ]:      457619 :                     !sk_stream_memory_free(ssk) ||
           [ +  -  -  + ]
    1772                 :      416171 :                     !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
    1773                 :      282082 :                         err = copied;
    1774                 :      282082 :                         goto out;
    1775                 :             :                 }
    1776                 :      416171 :                 mptcp_set_timeout(sk);
    1777                 :             :         }
    1778                 :             :         err = copied;
    1779                 :             : 
    1780                 :      949891 : out:
    1781         [ +  + ]:      949891 :         if (err > 0)
    1782                 :      651414 :                 msk->last_data_sent = tcp_jiffies32;
    1783                 :      949891 :         return err;
    1784                 :             : }
    1785                 :             : 
    1786                 :     1013469 : void __mptcp_push_pending(struct sock *sk, unsigned int flags)
    1787                 :             : {
    1788                 :     1013469 :         struct sock *prev_ssk = NULL, *ssk = NULL;
    1789         [ -  + ]:     1013469 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1790                 :     1013469 :         struct mptcp_sendmsg_info info = {
    1791                 :             :                                 .flags = flags,
    1792                 :             :         };
    1793                 :     1013469 :         bool copied = false;
    1794                 :     1013469 :         int push_count = 1;
    1795                 :             : 
    1796   [ +  +  +  + ]:     1726219 :         while (mptcp_send_head(sk) && (push_count > 0)) {
    1797                 :      765283 :                 struct mptcp_subflow_context *subflow;
    1798                 :      765283 :                 int ret = 0;
    1799                 :             : 
    1800         [ +  + ]:      765283 :                 if (mptcp_sched_get_send(msk))
    1801                 :             :                         break;
    1802                 :             : 
    1803                 :      712750 :                 push_count = 0;
    1804                 :             : 
    1805         [ +  + ]:     1834523 :                 mptcp_for_each_subflow(msk, subflow) {
    1806         [ +  + ]:     1121773 :                         if (READ_ONCE(subflow->scheduled)) {
           [ -  +  +  + ]
    1807                 :      754013 :                                 mptcp_subflow_set_scheduled(subflow, false);
    1808                 :             : 
    1809                 :      754013 :                                 prev_ssk = ssk;
    1810         [ +  + ]:      754013 :                                 ssk = mptcp_subflow_tcp_sock(subflow);
    1811         [ +  + ]:      754013 :                                 if (ssk != prev_ssk) {
    1812                 :             :                                         /* First check. If the ssk has changed since
    1813                 :             :                                          * the last round, release prev_ssk
    1814                 :             :                                          */
    1815         [ +  + ]:      710832 :                                         if (prev_ssk)
    1816                 :       44592 :                                                 mptcp_push_release(prev_ssk, &info);
    1817                 :             : 
    1818                 :             :                                         /* Need to lock the new subflow only if different
    1819                 :             :                                          * from the previous one, otherwise we are still
    1820                 :             :                                          * helding the relevant lock
    1821                 :             :                                          */
    1822                 :      710832 :                                         lock_sock(ssk);
    1823                 :             :                                 }
    1824                 :             : 
    1825                 :      754013 :                                 push_count++;
    1826                 :             : 
    1827                 :      754013 :                                 ret = __subflow_push_pending(sk, ssk, &info);
    1828         [ +  + ]:      754013 :                                 if (ret <= 0) {
    1829         [ -  + ]:      213463 :                                         if (ret != -EAGAIN ||
           [ +  +  +  - ]
    1830         [ -  + ]:           1 :                                             (1 << ssk->sk_state) &
    1831                 :             :                                              (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
    1832                 :      113831 :                                                 push_count--;
    1833                 :      213462 :                                         continue;
    1834                 :             :                                 }
    1835                 :             :                                 copied = true;
    1836                 :             :                         }
    1837                 :             :                 }
    1838                 :             :         }
    1839                 :             : 
    1840                 :             :         /* at this point we held the socket lock for the last subflow we used */
    1841         [ +  + ]:     1013469 :         if (ssk)
    1842                 :      666240 :                 mptcp_push_release(ssk, &info);
    1843                 :             : 
    1844                 :             :         /* Avoid scheduling the rtx timer if no data has been pushed; the timer
    1845                 :             :          * will be updated on positive acks by __mptcp_cleanup_una().
    1846                 :             :          */
    1847         [ +  + ]:     1013469 :         if (copied) {
    1848         [ +  + ]:      504934 :                 if (!mptcp_rtx_timer_pending(sk))
    1849                 :       55822 :                         mptcp_reset_rtx_timer(sk);
    1850                 :      504934 :                 mptcp_check_send_data_fin(sk);
    1851                 :             :         }
    1852                 :     1013469 : }
    1853                 :             : 
    1854                 :     1434881 : static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
    1855                 :             : {
    1856         [ -  + ]:     1434881 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1857                 :     1434881 :         struct mptcp_sendmsg_info info = {
    1858                 :             :                 .data_lock_held = true,
    1859                 :             :         };
    1860                 :     1434881 :         bool keep_pushing = true;
    1861                 :     1434881 :         struct sock *xmit_ssk;
    1862                 :     1434881 :         int copied = 0;
    1863                 :             : 
    1864                 :     1434881 :         info.flags = 0;
    1865   [ +  +  +  + ]:     1638041 :         while (mptcp_send_head(sk) && keep_pushing) {
    1866         [ +  + ]:      312001 :                 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1867                 :      312001 :                 int ret = 0;
    1868                 :             : 
    1869                 :             :                 /* check for a different subflow usage only after
    1870                 :             :                  * spooling the first chunk of data
    1871                 :             :                  */
    1872         [ +  + ]:      312001 :                 if (first) {
    1873                 :        8548 :                         mptcp_subflow_set_scheduled(subflow, false);
    1874                 :        8548 :                         ret = __subflow_push_pending(sk, ssk, &info);
    1875                 :        8548 :                         first = false;
    1876         [ +  + ]:        8548 :                         if (ret <= 0)
    1877                 :             :                                 break;
    1878                 :        5606 :                         copied += ret;
    1879                 :        5606 :                         continue;
    1880                 :             :                 }
    1881                 :             : 
    1882         [ +  + ]:      303453 :                 if (mptcp_sched_get_send(msk))
    1883                 :      105899 :                         goto out;
    1884                 :             : 
    1885         [ +  + ]:      197554 :                 if (READ_ONCE(subflow->scheduled)) {
           [ -  +  +  + ]
    1886                 :      187330 :                         mptcp_subflow_set_scheduled(subflow, false);
    1887                 :      187330 :                         ret = __subflow_push_pending(sk, ssk, &info);
    1888         [ +  + ]:      187330 :                         if (ret <= 0)
    1889                 :       82073 :                                 keep_pushing = false;
    1890                 :      187330 :                         copied += ret;
    1891                 :             :                 }
    1892                 :             : 
    1893         [ +  + ]:      517470 :                 mptcp_for_each_subflow(msk, subflow) {
    1894         [ +  + ]:      319916 :                         if (READ_ONCE(subflow->scheduled)) {
           [ -  +  +  + ]
    1895         [ +  - ]:       10224 :                                 xmit_ssk = mptcp_subflow_tcp_sock(subflow);
    1896         [ +  - ]:       10224 :                                 if (xmit_ssk != ssk) {
    1897                 :       10224 :                                         mptcp_subflow_delegate(subflow,
    1898                 :             :                                                                MPTCP_DELEGATE_SEND);
    1899                 :       10224 :                                         keep_pushing = false;
    1900                 :             :                                 }
    1901                 :             :                         }
    1902                 :             :                 }
    1903                 :             :         }
    1904                 :             : 
    1905                 :     1328982 : out:
    1906                 :             :         /* __mptcp_alloc_tx_skb could have released some wmem and we are
    1907                 :             :          * not going to flush it via release_sock()
    1908                 :             :          */
    1909         [ +  + ]:     1434881 :         if (copied) {
    1910         [ -  + ]:      104238 :                 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
    1911                 :             :                          info.size_goal);
    1912         [ +  + ]:      104238 :                 if (!mptcp_rtx_timer_pending(sk))
    1913                 :          15 :                         mptcp_reset_rtx_timer(sk);
    1914                 :             : 
    1915         [ +  + ]:      104238 :                 if (msk->snd_data_fin_enable &&
           [ -  +  +  + ]
    1916         [ +  + ]:       12975 :                     msk->snd_nxt + 1 == msk->write_seq)
    1917                 :         417 :                         mptcp_schedule_work(sk);
    1918                 :             :         }
    1919                 :     1434881 : }
    1920                 :             : 
    1921                 :             : static int mptcp_disconnect(struct sock *sk, int flags);
    1922                 :             : 
    1923                 :         112 : static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
    1924                 :             :                                   size_t len, int *copied_syn)
    1925                 :             : {
    1926                 :         112 :         unsigned int saved_flags = msg->msg_flags;
    1927         [ -  + ]:         112 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1928                 :         112 :         struct sock *ssk;
    1929                 :         112 :         int ret;
    1930                 :             : 
    1931                 :             :         /* on flags based fastopen the mptcp is supposed to create the
    1932                 :             :          * first subflow right now. Otherwise we are in the defer_connect
    1933                 :             :          * path, and the first subflow must be already present.
    1934                 :             :          * Since the defer_connect flag is cleared after the first succsful
    1935                 :             :          * fastopen attempt, no need to check for additional subflow status.
    1936                 :             :          */
    1937         [ +  + ]:         112 :         if (msg->msg_flags & MSG_FASTOPEN) {
    1938                 :          76 :                 ssk = __mptcp_nmpc_sk(msk);
    1939         [ +  + ]:          76 :                 if (IS_ERR(ssk))
    1940                 :           6 :                         return PTR_ERR(ssk);
    1941                 :             :         }
    1942         [ +  - ]:         106 :         if (!msk->first)
    1943                 :             :                 return -EINVAL;
    1944                 :             : 
    1945                 :         106 :         ssk = msk->first;
    1946                 :             : 
    1947                 :         106 :         lock_sock(ssk);
    1948                 :         106 :         msg->msg_flags |= MSG_DONTWAIT;
    1949                 :         106 :         msk->fastopening = 1;
    1950                 :         106 :         ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
    1951                 :         106 :         msk->fastopening = 0;
    1952                 :         106 :         msg->msg_flags = saved_flags;
    1953                 :         106 :         release_sock(ssk);
    1954                 :             : 
    1955                 :             :         /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
    1956   [ +  -  +  + ]:         106 :         if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
    1957                 :          46 :                 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
    1958                 :             :                                             msg->msg_namelen, msg->msg_flags, 1);
    1959                 :             : 
    1960                 :             :                 /* Keep the same behaviour of plain TCP: zero the copied bytes in
    1961                 :             :                  * case of any error, except timeout or signal
    1962                 :             :                  */
    1963   [ -  +  -  - ]:          46 :                 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
    1964                 :           0 :                         *copied_syn = 0;
    1965         [ +  - ]:          60 :         } else if (ret && ret != -EINPROGRESS) {
    1966                 :             :                 /* The disconnect() op called by tcp_sendmsg_fastopen()/
    1967                 :             :                  * __inet_stream_connect() can fail, due to looking check,
    1968                 :             :                  * see mptcp_disconnect().
    1969                 :             :                  * Attempt it again outside the problematic scope.
    1970                 :             :                  */
    1971         [ #  # ]:           0 :                 if (!mptcp_disconnect(sk, 0)) {
    1972                 :           0 :                         sk->sk_disconnects++;
    1973                 :           0 :                         sk->sk_socket->state = SS_UNCONNECTED;
    1974                 :             :                 }
    1975                 :             :         }
    1976                 :         106 :         inet_clear_bit(DEFER_CONNECT, sk);
    1977                 :             : 
    1978                 :         106 :         return ret;
    1979                 :             : }
    1980                 :             : 
    1981                 :      896896 : static int do_copy_data_nocache(struct sock *sk, int copy,
    1982                 :             :                                 struct iov_iter *from, char *to)
    1983                 :             : {
    1984         [ -  + ]:      896896 :         if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
    1985         [ #  # ]:           0 :                 if (!copy_from_iter_full_nocache(to, copy, from))
    1986                 :           0 :                         return -EFAULT;
    1987         [ -  + ]:      896896 :         } else if (!copy_from_iter_full(to, copy, from)) {
    1988                 :           0 :                 return -EFAULT;
    1989                 :             :         }
    1990                 :             :         return 0;
    1991                 :             : }
    1992                 :             : 
    1993                 :             : /* open-code sk_stream_memory_free() plus sent limit computation to
    1994                 :             :  * avoid indirect calls in fast-path.
    1995                 :             :  * Called under the msk socket lock, so we can avoid a bunch of ONCE
    1996                 :             :  * annotations.
    1997                 :             :  */
    1998                 :      904249 : static u32 mptcp_send_limit(const struct sock *sk)
    1999                 :             : {
    2000         [ -  + ]:      904249 :         const struct mptcp_sock *msk = mptcp_sk(sk);
    2001                 :      904249 :         u32 limit, not_sent;
    2002                 :             : 
    2003         [ +  + ]:      904249 :         if (sk->sk_wmem_queued >= READ_ONCE(sk->sk_sndbuf))
    2004                 :             :                 return 0;
    2005                 :             : 
    2006                 :      896902 :         limit = mptcp_notsent_lowat(sk);
    2007         [ +  + ]:      896902 :         if (limit == UINT_MAX)
    2008                 :             :                 return UINT_MAX;
    2009                 :             : 
    2010                 :          12 :         not_sent = msk->write_seq - msk->snd_nxt;
    2011         [ +  + ]:          12 :         if (not_sent >= limit)
    2012                 :             :                 return 0;
    2013                 :             : 
    2014                 :           6 :         return limit - not_sent;
    2015                 :             : }
    2016                 :             : 
    2017                 :     2277168 : static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
    2018                 :             : {
    2019                 :     2277168 :         struct mptcp_subflow_context *subflow;
    2020                 :             : 
    2021         [ -  + ]:     2277168 :         if (!rfs_is_needed())
    2022                 :             :                 return;
    2023                 :             : 
    2024         [ #  # ]:           0 :         mptcp_for_each_subflow(msk, subflow) {
    2025                 :           0 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    2026                 :             : 
    2027                 :           0 :                 sock_rps_record_flow(ssk);
    2028                 :             :         }
    2029                 :             : }
    2030                 :             : 
    2031                 :      697042 : static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
    2032                 :             : {
    2033         [ -  + ]:      697042 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2034                 :      697042 :         struct page_frag *pfrag;
    2035                 :      697042 :         size_t copied = 0;
    2036                 :      697042 :         int ret = 0;
    2037                 :      697042 :         long timeo;
    2038                 :             : 
    2039                 :             :         /* silently ignore everything else */
    2040                 :      697042 :         msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
    2041                 :             :                           MSG_FASTOPEN | MSG_EOR;
    2042                 :             : 
    2043                 :      697042 :         lock_sock(sk);
    2044                 :             : 
    2045                 :      697042 :         mptcp_rps_record_subflows(msk);
    2046                 :             : 
    2047   [ +  +  +  + ]:      697042 :         if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
    2048                 :             :                      msg->msg_flags & MSG_FASTOPEN)) {
    2049                 :         112 :                 int copied_syn = 0;
    2050                 :             : 
    2051                 :         112 :                 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
    2052                 :         112 :                 copied += copied_syn;
    2053   [ +  +  +  + ]:         112 :                 if (ret == -EINPROGRESS && copied_syn > 0)
    2054                 :          42 :                         goto out;
    2055         [ +  + ]:          70 :                 else if (ret)
    2056                 :          24 :                         goto do_error;
    2057                 :             :         }
    2058                 :             : 
    2059         [ +  + ]:      696976 :         timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
    2060                 :             : 
    2061         [ +  + ]:      696976 :         if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
           [ -  +  +  + ]
    2062                 :          45 :                 ret = sk_stream_wait_connect(sk, &timeo);
    2063         [ +  - ]:          45 :                 if (ret)
    2064                 :          45 :                         goto do_error;
    2065                 :             :         }
    2066                 :             : 
    2067                 :      696931 :         ret = -EPIPE;
    2068   [ +  -  -  + ]:      696931 :         if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
    2069                 :           0 :                 goto do_error;
    2070                 :             : 
    2071         [ +  - ]:      696931 :         pfrag = sk_page_frag(sk);
    2072                 :             : 
    2073         [ +  + ]:     1601174 :         while (msg_data_left(msg)) {
    2074                 :      904249 :                 int total_ts, frag_truesize = 0;
    2075                 :      904249 :                 struct mptcp_data_frag *dfrag;
    2076                 :      904249 :                 bool dfrag_collapsed;
    2077                 :      904249 :                 size_t psize, offset;
    2078                 :      904249 :                 u32 copy_limit;
    2079                 :             : 
    2080                 :             :                 /* ensure fitting the notsent_lowat() constraint */
    2081                 :      904249 :                 copy_limit = mptcp_send_limit(sk);
    2082         [ +  + ]:      904249 :                 if (!copy_limit)
    2083                 :        7353 :                         goto wait_for_memory;
    2084                 :             : 
    2085                 :             :                 /* reuse tail pfrag, if possible, or carve a new one from the
    2086                 :             :                  * page allocator
    2087                 :             :                  */
    2088                 :      896896 :                 dfrag = mptcp_pending_tail(sk);
    2089                 :      896896 :                 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
    2090         [ +  + ]:      896896 :                 if (!dfrag_collapsed) {
    2091         [ -  + ]:      693368 :                         if (!mptcp_page_frag_refill(sk, pfrag))
    2092                 :           0 :                                 goto wait_for_memory;
    2093                 :             : 
    2094                 :      693368 :                         dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
    2095                 :      693368 :                         frag_truesize = dfrag->overhead;
    2096                 :             :                 }
    2097                 :             : 
    2098                 :             :                 /* we do not bound vs wspace, to allow a single packet.
    2099                 :             :                  * memory accounting will prevent execessive memory usage
    2100                 :             :                  * anyway
    2101                 :             :                  */
    2102                 :      896896 :                 offset = dfrag->offset + dfrag->data_len;
    2103                 :      896896 :                 psize = pfrag->size - offset;
    2104                 :      896896 :                 psize = min_t(size_t, psize, msg_data_left(msg));
    2105                 :      896896 :                 psize = min_t(size_t, psize, copy_limit);
    2106                 :      896896 :                 total_ts = psize + frag_truesize;
    2107                 :             : 
    2108         [ -  + ]:      896896 :                 if (!sk_wmem_schedule(sk, total_ts))
    2109                 :           0 :                         goto wait_for_memory;
    2110                 :             : 
    2111                 :     1158465 :                 ret = do_copy_data_nocache(sk, psize, &msg->msg_iter,
    2112                 :      896896 :                                            page_address(dfrag->page) + offset);
    2113         [ -  + ]:      896896 :                 if (ret)
    2114                 :           0 :                         goto do_error;
    2115                 :             : 
    2116                 :             :                 /* data successfully copied into the write queue */
    2117         [ +  + ]:      896896 :                 sk_forward_alloc_add(sk, -total_ts);
    2118                 :      896896 :                 copied += psize;
    2119                 :      896896 :                 dfrag->data_len += psize;
    2120                 :      896896 :                 frag_truesize += psize;
    2121                 :      896896 :                 pfrag->offset += frag_truesize;
    2122                 :      896896 :                 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
    2123                 :             : 
    2124                 :             :                 /* charge data on mptcp pending queue to the msk socket
    2125                 :             :                  * Note: we charge such data both to sk and ssk
    2126                 :             :                  */
    2127         [ +  + ]:      896896 :                 sk_wmem_queued_add(sk, frag_truesize);
    2128         [ +  + ]:      896896 :                 if (!dfrag_collapsed) {
    2129                 :      693368 :                         get_page(dfrag->page);
    2130         [ +  + ]:      693368 :                         list_add_tail(&dfrag->list, &msk->rtx_queue);
    2131         [ +  + ]:      693368 :                         if (!msk->first_pending)
    2132                 :      496952 :                                 msk->first_pending = dfrag;
    2133                 :             :                 }
    2134   [ -  +  -  - ]:      896896 :                 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d\n", msk,
    2135                 :             :                          dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
    2136                 :             :                          !dfrag_collapsed);
    2137                 :             : 
    2138                 :      896896 :                 continue;
    2139                 :             : 
    2140                 :        7353 : wait_for_memory:
    2141                 :        7353 :                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
    2142                 :        7353 :                 __mptcp_push_pending(sk, msg->msg_flags);
    2143                 :        7353 :                 ret = sk_stream_wait_memory(sk, &timeo);
    2144         [ +  + ]:        7353 :                 if (ret)
    2145                 :           6 :                         goto do_error;
    2146                 :             :         }
    2147                 :             : 
    2148         [ +  + ]:      696925 :         if (copied) {
    2149                 :             :                 /* mark the last dfrag with EOR if MSG_EOR was set */
    2150         [ +  + ]:      696842 :                 if (msg->msg_flags & MSG_EOR) {
    2151                 :           6 :                         struct mptcp_data_frag *dfrag = mptcp_pending_tail(sk);
    2152                 :             : 
    2153         [ +  - ]:           6 :                         if (dfrag)
    2154                 :           6 :                                 dfrag->eor = 1;
    2155                 :             :                 }
    2156                 :      696842 :                 __mptcp_push_pending(sk, msg->msg_flags);
    2157                 :             :         }
    2158                 :             : 
    2159                 :          83 : out:
    2160                 :      697042 :         release_sock(sk);
    2161                 :      697042 :         return copied;
    2162                 :             : 
    2163                 :          75 : do_error:
    2164         [ +  + ]:          75 :         if (copied)
    2165                 :           6 :                 goto out;
    2166                 :             : 
    2167                 :          69 :         copied = sk_stream_error(sk, msg->msg_flags, ret);
    2168                 :          69 :         goto out;
    2169                 :             : }
    2170                 :             : 
    2171                 :             : static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied);
    2172                 :             : 
    2173                 :      867273 : static void mptcp_eat_recv_skb(struct sock *sk, struct sk_buff *skb)
    2174                 :             : {
    2175                 :             :         /* avoid the indirect call, we know the destructor is sock_rfree */
    2176                 :      867273 :         skb->destructor = NULL;
    2177                 :      867273 :         skb->sk = NULL;
    2178                 :      867273 :         atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
    2179                 :      867273 :         sk_mem_uncharge(sk, skb->truesize);
    2180                 :      867273 :         __skb_unlink(skb, &sk->sk_receive_queue);
    2181                 :      867273 :         skb_attempt_defer_free(skb);
    2182                 :      867273 : }
    2183                 :             : 
    2184                 :     1477419 : static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,
    2185                 :             :                                 size_t len, int flags, int copied_total,
    2186                 :             :                                 struct scm_timestamping_internal *tss,
    2187                 :             :                                 int *cmsg_flags, struct sk_buff **last)
    2188                 :             : {
    2189         [ -  + ]:     1477419 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2190                 :     1477419 :         struct sk_buff *skb, *tmp;
    2191                 :     1477419 :         int total_data_len = 0;
    2192                 :     1477419 :         int copied = 0;
    2193                 :             : 
    2194         [ +  + ]:     2160816 :         skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
    2195                 :     1425682 :                 u32 delta, offset = MPTCP_SKB_CB(skb)->offset;
    2196                 :     1425682 :                 u32 data_len = skb->len - offset;
    2197                 :     1425682 :                 u32 count;
    2198                 :     1425682 :                 int err;
    2199                 :             : 
    2200         [ +  + ]:     1425682 :                 if (flags & MSG_PEEK) {
    2201                 :             :                         /* skip already peeked skbs */
    2202         [ +  + ]:       54614 :                         if (total_data_len + data_len <= copied_total) {
    2203                 :          10 :                                 total_data_len += data_len;
    2204                 :          10 :                                 *last = skb;
    2205                 :          10 :                                 continue;
    2206                 :             :                         }
    2207                 :             : 
    2208                 :             :                         /* skip the already peeked data in the current skb */
    2209                 :       54604 :                         delta = copied_total - total_data_len;
    2210                 :       54604 :                         offset += delta;
    2211                 :       54604 :                         data_len -= delta;
    2212                 :             :                 }
    2213                 :             : 
    2214                 :     1425672 :                 count = min_t(size_t, len - copied, data_len);
    2215         [ +  - ]:     1425672 :                 if (!(flags & MSG_TRUNC)) {
    2216                 :     1425672 :                         err = skb_copy_datagram_msg(skb, offset, msg, count);
    2217         [ -  + ]:     1425672 :                         if (unlikely(err < 0)) {
    2218         [ #  # ]:           0 :                                 if (!copied)
    2219                 :             :                                         return err;
    2220                 :             :                                 break;
    2221                 :             :                         }
    2222                 :             :                 }
    2223                 :             : 
    2224         [ +  + ]:     1425672 :                 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
    2225                 :         713 :                         tcp_update_recv_tstamps(skb, tss);
    2226                 :         713 :                         *cmsg_flags |= MPTCP_CMSG_TS;
    2227                 :             :                 }
    2228                 :             : 
    2229                 :     1425672 :                 copied += count;
    2230                 :             : 
    2231         [ +  + ]:     1425672 :                 if (!(flags & MSG_PEEK)) {
    2232                 :     1371068 :                         msk->bytes_consumed += count;
    2233         [ +  + ]:     1371068 :                         if (count < data_len) {
    2234                 :      601702 :                                 MPTCP_SKB_CB(skb)->offset += count;
    2235                 :      601702 :                                 MPTCP_SKB_CB(skb)->map_seq += count;
    2236                 :      601702 :                                 break;
    2237                 :             :                         }
    2238                 :             : 
    2239                 :      769366 :                         mptcp_eat_recv_skb(sk, skb);
    2240                 :             :                 } else {
    2241                 :       54604 :                         *last = skb;
    2242                 :             :                 }
    2243                 :             : 
    2244         [ +  + ]:      823970 :                 if (copied >= len)
    2245                 :             :                         break;
    2246                 :             :         }
    2247                 :             : 
    2248                 :     1477419 :         mptcp_rcv_space_adjust(msk, copied);
    2249                 :     1477419 :         return copied;
    2250                 :             : }
    2251                 :             : 
    2252                 :        2840 : static void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
    2253                 :             : {
    2254         [ -  + ]:        2840 :         const struct tcp_sock *tp = tcp_sk(ssk);
    2255                 :             : 
    2256                 :        2840 :         msk->rcvspace_init = 1;
    2257                 :        2840 :         msk->rcvq_space.copied = 0;
    2258                 :             : 
    2259                 :             :         /* initial rcv_space offering made to peer */
    2260                 :        2840 :         msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
    2261                 :             :                                       TCP_INIT_CWND * tp->advmss);
    2262         [ +  + ]:        2840 :         if (msk->rcvq_space.space == 0)
    2263                 :           1 :                 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
    2264                 :        2840 : }
    2265                 :             : 
    2266                 :             : /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
    2267                 :             :  *
    2268                 :             :  * Only difference: Use lowest rtt estimate of the subflows in use, see
    2269                 :             :  * mptcp_rcv_rtt_update() and mptcp_rtt_us_est().
    2270                 :             :  */
    2271                 :     1908943 : static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
    2272                 :             : {
    2273                 :     1908943 :         struct mptcp_subflow_context *subflow;
    2274                 :     1908943 :         struct sock *sk = (struct sock *)msk;
    2275                 :     1908943 :         u32 time, rtt_us;
    2276                 :     1908943 :         u64 mstamp;
    2277                 :             : 
    2278                 :     1908943 :         msk_owned_by_me(msk);
    2279                 :             : 
    2280         [ +  + ]:     1908943 :         if (copied <= 0)
    2281                 :             :                 return;
    2282                 :             : 
    2283         [ +  + ]:     1596601 :         if (!msk->rcvspace_init)
    2284                 :        1174 :                 mptcp_rcv_space_init(msk, msk->first);
    2285                 :             : 
    2286                 :     1596601 :         msk->rcvq_space.copied += copied;
    2287                 :             : 
    2288                 :     1596601 :         mstamp = mptcp_stamp();
    2289                 :     1596601 :         time = tcp_stamp_us_delta(mstamp, READ_ONCE(msk->rcvq_space.time));
    2290                 :             : 
    2291                 :     1596601 :         rtt_us = mptcp_rtt_us_est(msk);
    2292   [ +  +  +  + ]:     1596601 :         if (rtt_us == U32_MAX || time < (rtt_us >> 3))
    2293                 :             :                 return;
    2294                 :             : 
    2295                 :      149013 :         copied = msk->rcvq_space.copied;
    2296                 :      149013 :         copied -= mptcp_inq_hint(sk);
    2297         [ +  + ]:      149013 :         if (copied <= msk->rcvq_space.space)
    2298                 :      143987 :                 goto new_measure;
    2299                 :             : 
    2300                 :        5026 :         trace_mptcp_rcvbuf_grow(sk, time);
    2301         [ +  + ]:        5026 :         if (mptcp_rcvbuf_grow(sk, copied)) {
    2302                 :             :                 /* Make subflows follow along.  If we do not do this, we
    2303                 :             :                  * get drops at subflow level if skbs can't be moved to
    2304                 :             :                  * the mptcp rx queue fast enough (announced rcv_win can
    2305                 :             :                  * exceed ssk->sk_rcvbuf).
    2306                 :             :                  */
    2307         [ +  + ]:        5269 :                 mptcp_for_each_subflow(msk, subflow) {
    2308                 :        2801 :                         struct sock *ssk;
    2309                 :        2801 :                         bool slow;
    2310                 :             : 
    2311                 :        2801 :                         ssk = mptcp_subflow_tcp_sock(subflow);
    2312                 :        2801 :                         slow = lock_sock_fast(ssk);
    2313                 :             :                         /* subflows can be added before tcp_init_transfer() */
    2314   [ -  +  +  - ]:        2801 :                         if (tcp_sk(ssk)->rcvq_space.space)
    2315                 :        2801 :                                 tcp_rcvbuf_grow(ssk, copied);
    2316                 :        2801 :                         unlock_sock_fast(ssk, slow);
    2317                 :             :                 }
    2318                 :             :         }
    2319                 :             : 
    2320                 :        5026 : new_measure:
    2321                 :      149013 :         msk->rcvq_space.copied = 0;
    2322                 :      149013 :         msk->rcvq_space.time = mstamp;
    2323                 :             : }
    2324                 :             : 
    2325                 :      211142 : static bool __mptcp_move_skbs(struct sock *sk, struct list_head *skbs, u32 *delta)
    2326                 :             : {
    2327                 :      211142 :         struct sk_buff *skb = list_first_entry(skbs, struct sk_buff, list);
    2328         [ -  + ]:      211142 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2329                 :             :         bool moved = false;
    2330                 :             : 
    2331                 :      300762 :         while (1) {
    2332                 :      255952 :                 prefetch(skb->next);
    2333                 :      255952 :                 list_del(&skb->list);
    2334                 :      255952 :                 *delta += skb->truesize;
    2335                 :             : 
    2336                 :      255952 :                 moved |= __mptcp_move_skb(sk, skb);
    2337         [ +  + ]:      255952 :                 if (list_empty(skbs))
    2338                 :             :                         break;
    2339                 :             : 
    2340                 :       44810 :                 skb = list_first_entry(skbs, struct sk_buff, list);
    2341                 :             :         }
    2342                 :             : 
    2343                 :      211142 :         __mptcp_ofo_queue(msk);
    2344         [ +  + ]:      211142 :         if (moved)
    2345                 :      206987 :                 mptcp_check_data_fin((struct sock *)msk);
    2346                 :      211142 :         return moved;
    2347                 :             : }
    2348                 :             : 
    2349                 :     3083133 : static bool mptcp_can_spool_backlog(struct sock *sk, struct list_head *skbs)
    2350                 :             : {
    2351         [ -  + ]:     3083133 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2352                 :             : 
    2353                 :             :         /* After CG initialization, subflows should never add skb before
    2354                 :             :          * gaining the CG themself.
    2355                 :             :          */
    2356   [ +  +  +  +  :     3083133 :         DEBUG_NET_WARN_ON_ONCE(msk->backlog_unaccounted && sk->sk_socket &&
                   -  + ]
    2357                 :             :                                mem_cgroup_from_sk(sk));
    2358                 :             : 
    2359         [ +  + ]:     3083133 :         if (list_empty(&msk->backlog_list))
    2360                 :             :                 return false;
    2361                 :             : 
    2362         [ +  - ]:      211142 :         INIT_LIST_HEAD(skbs);
    2363         [ +  - ]:      211142 :         list_splice_init(&msk->backlog_list, skbs);
    2364                 :             :         return true;
    2365                 :             : }
    2366                 :             : 
    2367                 :       23034 : static bool mptcp_move_skbs(struct sock *sk)
    2368                 :             : {
    2369         [ -  + ]:       23034 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2370                 :       23034 :         struct list_head skbs;
    2371                 :       23034 :         bool enqueued = false;
    2372                 :       23034 :         u32 moved = 0;
    2373                 :             : 
    2374                 :       23034 :         mptcp_data_lock(sk);
    2375         [ +  + ]:       70336 :         while (mptcp_can_spool_backlog(sk, &skbs)) {
    2376                 :       24268 :                 mptcp_data_unlock(sk);
    2377                 :       24268 :                 enqueued |= __mptcp_move_skbs(sk, &skbs, &moved);
    2378                 :             : 
    2379                 :       24268 :                 mptcp_data_lock(sk);
    2380                 :             :         }
    2381                 :       23034 :         WRITE_ONCE(msk->backlog_len, msk->backlog_len - moved);
    2382                 :       23034 :         mptcp_data_unlock(sk);
    2383                 :             : 
    2384   [ +  +  +  - ]:       23034 :         if (enqueued && mptcp_epollin_ready(sk))
    2385                 :       22358 :                 sk->sk_data_ready(sk);
    2386                 :             : 
    2387                 :       23034 :         return enqueued;
    2388                 :             : }
    2389                 :             : 
    2390                 :      149428 : static unsigned int mptcp_inq_hint(const struct sock *sk)
    2391                 :             : {
    2392         [ -  + ]:      149428 :         const struct mptcp_sock *msk = mptcp_sk(sk);
    2393                 :      149428 :         const struct sk_buff *skb;
    2394                 :             : 
    2395         [ +  + ]:      149428 :         skb = skb_peek(&sk->sk_receive_queue);
    2396         [ +  - ]:       99613 :         if (skb) {
    2397                 :       99613 :                 u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
    2398                 :             : 
    2399         [ +  - ]:       99613 :                 if (hint_val >= INT_MAX)
    2400                 :             :                         return INT_MAX;
    2401                 :             : 
    2402                 :       99613 :                 return (unsigned int)hint_val;
    2403                 :             :         }
    2404                 :             : 
    2405   [ +  +  +  + ]:       49815 :         if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
    2406                 :         227 :                 return 1;
    2407                 :             : 
    2408                 :             :         return 0;
    2409                 :             : }
    2410                 :             : 
    2411                 :     1285527 : static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
    2412                 :             :                          int flags)
    2413                 :             : {
    2414         [ -  + ]:     1285527 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2415                 :     1285527 :         struct scm_timestamping_internal tss;
    2416                 :     1285527 :         int copied = 0, cmsg_flags = 0;
    2417                 :     1285527 :         int target;
    2418                 :     1285527 :         long timeo;
    2419                 :             : 
    2420         [ +  + ]:     1285527 :         if (unlikely(flags & MSG_ERRQUEUE))
    2421                 :           4 :                 return inet_recv_error(sk, msg, len);
    2422                 :             : 
    2423                 :     1285523 :         lock_sock(sk);
    2424         [ -  + ]:     1285523 :         if (unlikely(sk->sk_state == TCP_LISTEN)) {
    2425                 :           0 :                 copied = -ENOTCONN;
    2426                 :           0 :                 goto out_err;
    2427                 :             :         }
    2428                 :             : 
    2429                 :     1285523 :         mptcp_rps_record_subflows(msk);
    2430                 :             : 
    2431         [ +  + ]:     1285523 :         timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
    2432                 :             : 
    2433                 :     1285523 :         len = min_t(size_t, len, INT_MAX);
    2434         [ +  + ]:     1285523 :         target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
    2435                 :             : 
    2436         [ +  + ]:     1285523 :         if (unlikely(msk->recvmsg_inq))
    2437                 :         415 :                 cmsg_flags = MPTCP_CMSG_INQ;
    2438                 :             : 
    2439         [ +  + ]:     1484823 :         while (copied < len) {
    2440                 :     1477419 :                 struct sk_buff *last = NULL;
    2441                 :     1477419 :                 int err, bytes_read;
    2442                 :             : 
    2443                 :     1477419 :                 bytes_read = __mptcp_recvmsg_mskq(sk, msg, len - copied, flags,
    2444                 :             :                                                   copied, &tss, &cmsg_flags,
    2445                 :             :                                                   &last);
    2446         [ -  + ]:     1477419 :                 if (unlikely(bytes_read < 0)) {
    2447         [ #  # ]:           0 :                         if (!copied)
    2448                 :           0 :                                 copied = bytes_read;
    2449                 :           0 :                         goto out_err;
    2450                 :             :                 }
    2451                 :             : 
    2452                 :     1477419 :                 copied += bytes_read;
    2453                 :             : 
    2454   [ +  +  +  + ]:     1477419 :                 if (!list_empty(&msk->backlog_list) && mptcp_move_skbs(sk))
    2455                 :       21853 :                         continue;
    2456                 :             : 
    2457                 :             :                 /* only the MPTCP socket status is relevant here. The exit
    2458                 :             :                  * conditions mirror closely tcp_recvmsg()
    2459                 :             :                  */
    2460         [ +  + ]:     1455566 :                 if (copied >= target)
    2461                 :             :                         break;
    2462                 :             : 
    2463         [ +  + ]:      179441 :                 if (copied) {
    2464         [ +  - ]:           6 :                         if (tcp_recv_should_stop(sk) ||
    2465         [ +  - ]:           6 :                             !timeo)
    2466                 :             :                                 break;
    2467                 :             :                 } else {
    2468         [ +  + ]:      179435 :                         if (sk->sk_err) {
    2469                 :           1 :                                 copied = sock_error(sk);
    2470                 :           1 :                                 break;
    2471                 :             :                         }
    2472                 :             : 
    2473         [ +  + ]:      179434 :                         if (sk->sk_shutdown & RCV_SHUTDOWN)
    2474                 :             :                                 break;
    2475                 :             : 
    2476         [ +  + ]:      177459 :                         if (sk->sk_state == TCP_CLOSE) {
    2477                 :             :                                 copied = -ENOTCONN;
    2478                 :             :                                 break;
    2479                 :             :                         }
    2480                 :             : 
    2481         [ +  - ]:      177441 :                         if (!timeo) {
    2482                 :             :                                 copied = -EAGAIN;
    2483                 :             :                                 break;
    2484                 :             :                         }
    2485                 :             : 
    2486         [ -  + ]:      177441 :                         if (signal_pending(current)) {
    2487         [ -  - ]:      252481 :                                 copied = sock_intr_errno(timeo);
    2488                 :             :                                 break;
    2489                 :             :                         }
    2490                 :             :                 }
    2491                 :             : 
    2492   [ -  +  -  - ]:      177447 :                 pr_debug("block timeout %ld\n", timeo);
    2493                 :      177447 :                 mptcp_cleanup_rbuf(msk, copied);
    2494                 :      177447 :                 err = sk_wait_data(sk, &timeo, last);
    2495         [ -  + ]:      177447 :                 if (err < 0) {
    2496                 :           0 :                         err = copied ? : err;
    2497                 :           0 :                         goto out_err;
    2498                 :             :                 }
    2499                 :             :         }
    2500                 :             : 
    2501                 :     1285523 :         mptcp_cleanup_rbuf(msk, copied);
    2502                 :             : 
    2503                 :     1285523 : out_err:
    2504   [ +  +  +  - ]:     1285523 :         if (cmsg_flags && copied >= 0) {
    2505         [ +  + ]:         679 :                 if (cmsg_flags & MPTCP_CMSG_TS)
    2506                 :         671 :                         tcp_recv_timestamp(msg, sk, &tss);
    2507                 :             : 
    2508         [ +  + ]:         679 :                 if (cmsg_flags & MPTCP_CMSG_INQ) {
    2509                 :         415 :                         unsigned int inq = mptcp_inq_hint(sk);
    2510                 :             : 
    2511                 :         415 :                         put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
    2512                 :             :                 }
    2513                 :             :         }
    2514                 :             : 
    2515   [ -  +  -  - ]:     1285523 :         pr_debug("msk=%p rx queue empty=%d copied=%d\n",
    2516                 :             :                  msk, skb_queue_empty(&sk->sk_receive_queue), copied);
    2517                 :             : 
    2518                 :     1285523 :         release_sock(sk);
    2519                 :     1285523 :         return copied;
    2520                 :             : }
    2521                 :             : 
    2522                 :       11145 : static void mptcp_retransmit_timer(struct timer_list *t)
    2523                 :             : {
    2524                 :       11145 :         struct sock *sk = timer_container_of(sk, t, mptcp_retransmit_timer);
    2525         [ -  + ]:       11145 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2526                 :             : 
    2527                 :       11145 :         bh_lock_sock(sk);
    2528         [ +  + ]:       11145 :         if (!sock_owned_by_user(sk)) {
    2529                 :             :                 /* we need a process context to retransmit */
    2530         [ +  + ]:        8375 :                 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
    2531                 :        5118 :                         mptcp_schedule_work(sk);
    2532                 :             :         } else {
    2533                 :             :                 /* delegate our work to tcp_release_cb() */
    2534   [ -  +  -  -  :        6018 :                 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
                   -  - ]
    2535                 :             :         }
    2536                 :       11145 :         bh_unlock_sock(sk);
    2537                 :       11145 :         sock_put(sk);
    2538                 :       11145 : }
    2539                 :             : 
    2540                 :         126 : static void mptcp_tout_timer(struct timer_list *t)
    2541                 :             : {
    2542                 :         126 :         struct inet_connection_sock *icsk =
    2543                 :         126 :                 timer_container_of(icsk, t, mptcp_tout_timer);
    2544                 :         126 :         struct sock *sk = &icsk->icsk_inet.sk;
    2545                 :             : 
    2546                 :         126 :         mptcp_schedule_work(sk);
    2547                 :         126 :         sock_put(sk);
    2548                 :         126 : }
    2549                 :             : 
    2550                 :             : /* Find an idle subflow.  Return NULL if there is unacked data at tcp
    2551                 :             :  * level.
    2552                 :             :  *
    2553                 :             :  * A backup subflow is returned only if that is the only kind available.
    2554                 :             :  */
    2555                 :        5188 : struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
    2556                 :             : {
    2557                 :        5188 :         struct sock *backup = NULL, *pick = NULL;
    2558                 :        5188 :         struct mptcp_subflow_context *subflow;
    2559                 :        5188 :         int min_stale_count = INT_MAX;
    2560                 :             : 
    2561         [ +  + ]:       13234 :         mptcp_for_each_subflow(msk, subflow) {
    2562                 :        8046 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    2563                 :             : 
    2564         [ +  + ]:        8046 :                 if (!__mptcp_subflow_active(subflow))
    2565                 :        1724 :                         continue;
    2566                 :             : 
    2567                 :             :                 /* still data outstanding at TCP level? skip this */
    2568         [ +  + ]:        6322 :                 if (!tcp_rtx_and_write_queues_empty(ssk)) {
    2569                 :        2768 :                         min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
    2570                 :        2768 :                         continue;
    2571                 :             :                 }
    2572                 :             : 
    2573   [ +  +  +  + ]:        3554 :                 if (subflow->backup || subflow->request_bkup) {
    2574         [ +  + ]:          83 :                         if (!backup)
    2575                 :          69 :                                 backup = ssk;
    2576                 :          83 :                         continue;
    2577                 :             :                 }
    2578                 :             : 
    2579         [ +  + ]:        3471 :                 if (!pick)
    2580                 :        2547 :                         pick = ssk;
    2581                 :             :         }
    2582                 :             : 
    2583         [ +  + ]:        5188 :         if (pick)
    2584                 :             :                 return pick;
    2585                 :             : 
    2586                 :             :         /* use backup only if there are no progresses anywhere */
    2587         [ +  + ]:        2641 :         return min_stale_count > 1 ? backup : NULL;
    2588                 :             : }
    2589                 :             : 
    2590                 :        2390 : bool __mptcp_retransmit_pending_data(struct sock *sk)
    2591                 :             : {
    2592                 :        2390 :         struct mptcp_data_frag *cur, *rtx_head;
    2593         [ -  + ]:        2390 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2594                 :             : 
    2595         [ +  + ]:        2390 :         if (__mptcp_check_fallback(msk))
    2596                 :             :                 return false;
    2597                 :             : 
    2598                 :             :         /* the closing socket has some data untransmitted and/or unacked:
    2599                 :             :          * some data in the mptcp rtx queue has not really xmitted yet.
    2600                 :             :          * keep it simple and re-inject the whole mptcp level rtx queue
    2601                 :             :          */
    2602                 :        2300 :         mptcp_data_lock(sk);
    2603                 :        2300 :         __mptcp_clean_una_wakeup(sk);
    2604                 :        2300 :         rtx_head = mptcp_rtx_head(sk);
    2605         [ +  + ]:        2300 :         if (!rtx_head) {
    2606                 :        2224 :                 mptcp_data_unlock(sk);
    2607                 :        2224 :                 return false;
    2608                 :             :         }
    2609                 :             : 
    2610                 :          76 :         msk->recovery_snd_nxt = msk->snd_nxt;
    2611                 :          76 :         msk->recovery = true;
    2612                 :          76 :         mptcp_data_unlock(sk);
    2613                 :             : 
    2614                 :          76 :         msk->first_pending = rtx_head;
    2615                 :          76 :         msk->snd_burst = 0;
    2616                 :             : 
    2617                 :             :         /* be sure to clear the "sent status" on all re-injected fragments */
    2618         [ +  + ]:        5001 :         list_for_each_entry(cur, &msk->rtx_queue, list) {
    2619         [ +  + ]:        4939 :                 if (!cur->already_sent)
    2620                 :             :                         break;
    2621                 :        4925 :                 cur->already_sent = 0;
    2622                 :             :         }
    2623                 :             : 
    2624                 :             :         return true;
    2625                 :             : }
    2626                 :             : 
    2627                 :             : /* flags for __mptcp_close_ssk() */
    2628                 :             : #define MPTCP_CF_PUSH           BIT(1)
    2629                 :             : 
    2630                 :             : /* be sure to send a reset only if the caller asked for it, also
    2631                 :             :  * clean completely the subflow status when the subflow reaches
    2632                 :             :  * TCP_CLOSE state
    2633                 :             :  */
    2634                 :        1727 : static void __mptcp_subflow_disconnect(struct sock *ssk,
    2635                 :             :                                        struct mptcp_subflow_context *subflow,
    2636                 :             :                                        bool fastclosing)
    2637                 :             : {
    2638   [ +  +  -  + ]:        1727 :         if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
           [ -  +  +  +  
                   +  + ]
    2639                 :             :             fastclosing) {
    2640                 :             :                 /* The MPTCP code never wait on the subflow sockets, TCP-level
    2641                 :             :                  * disconnect should never fail
    2642                 :             :                  */
    2643         [ -  + ]:        1698 :                 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
    2644                 :        1698 :                 mptcp_subflow_ctx_reset(subflow);
    2645                 :             :         } else {
    2646                 :          29 :                 tcp_shutdown(ssk, SEND_SHUTDOWN);
    2647                 :             :         }
    2648                 :        1727 : }
    2649                 :             : 
    2650                 :        2375 : static void mptcp_cleanup_ssk_backlog(struct sock *sk, struct sock *ssk)
    2651                 :             : {
    2652         [ -  + ]:        2375 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2653                 :        2375 :         struct sk_buff *skb;
    2654                 :             : 
    2655                 :        2375 :         mptcp_data_lock(sk);
    2656         [ +  + ]:        2381 :         list_for_each_entry(skb, &msk->backlog_list, list) {
    2657         [ +  - ]:           6 :                 if (skb->sk != ssk)
    2658                 :           6 :                         continue;
    2659                 :             : 
    2660                 :           0 :                 atomic_sub(skb->truesize, &skb->sk->sk_rmem_alloc);
    2661                 :           0 :                 skb->sk = NULL;
    2662                 :             :         }
    2663                 :        2375 :         mptcp_data_unlock(sk);
    2664                 :        2375 : }
    2665                 :             : 
    2666                 :             : /* subflow sockets can be either outgoing (connect) or incoming
    2667                 :             :  * (accept).
    2668                 :             :  *
    2669                 :             :  * Outgoing subflows use in-kernel sockets.
    2670                 :             :  * Incoming subflows do not have their own 'struct socket' allocated,
    2671                 :             :  * so we need to use tcp_close() after detaching them from the mptcp
    2672                 :             :  * parent socket.
    2673                 :             :  */
    2674                 :        8114 : static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
    2675                 :             :                               struct mptcp_subflow_context *subflow,
    2676                 :             :                               unsigned int flags)
    2677                 :             : {
    2678         [ -  + ]:        8114 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2679                 :        8114 :         bool dispose_it, need_push = false;
    2680                 :        8114 :         int fwd_remaining;
    2681                 :             : 
    2682                 :             :         /* Do not pass RX data to the msk, even if the subflow socket is not
    2683                 :             :          * going to be freed (i.e. even for the first subflow on graceful
    2684                 :             :          * subflow close.
    2685                 :             :          */
    2686                 :        8114 :         lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
    2687                 :        8114 :         subflow->closing = 1;
    2688                 :             : 
    2689         [ +  + ]:        8114 :         if (flags & MPTCP_CF_PUSH)
    2690                 :        2375 :                 mptcp_cleanup_ssk_backlog(sk, ssk);
    2691                 :             : 
    2692                 :             :         /* Borrow the fwd allocated page left-over; fwd memory for the subflow
    2693                 :             :          * could be negative at this point, but will be reach zero soon - when
    2694                 :             :          * the data allocated using such fragment will be freed.
    2695                 :             :          */
    2696         [ +  + ]:        8114 :         if (subflow->lent_mem_frag) {
    2697                 :        2927 :                 fwd_remaining = PAGE_SIZE - subflow->lent_mem_frag;
    2698                 :        2927 :                 sk_forward_alloc_add(sk, fwd_remaining);
    2699                 :        2927 :                 sk_forward_alloc_add(ssk, -fwd_remaining);
    2700                 :        2927 :                 subflow->lent_mem_frag = 0;
    2701                 :             :         }
    2702                 :             : 
    2703                 :             :         /* If the first subflow moved to a close state before accept, e.g. due
    2704                 :             :          * to an incoming reset or listener shutdown, the subflow socket is
    2705                 :             :          * already deleted by inet_child_forget() and the mptcp socket can't
    2706                 :             :          * survive too.
    2707                 :             :          */
    2708   [ +  +  +  +  :        8151 :         if (msk->in_accept_queue && msk->first == ssk &&
                   +  + ]
    2709         [ -  + ]:          38 :             (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
    2710                 :             :                 /* ensure later check in mptcp_worker() will dispose the msk */
    2711                 :          36 :                 sock_set_flag(sk, SOCK_DEAD);
    2712                 :          36 :                 mptcp_set_close_tout(sk, tcp_jiffies32 - (mptcp_close_timeout(sk) + 1));
    2713                 :          36 :                 mptcp_subflow_drop_ctx(ssk);
    2714                 :          36 :                 goto out_release;
    2715                 :             :         }
    2716                 :             : 
    2717   [ +  +  +  + ]:        8078 :         dispose_it = msk->free_first || ssk != msk->first;
    2718                 :             :         if (dispose_it)
    2719                 :        6351 :                 list_del(&subflow->node);
    2720                 :             : 
    2721   [ +  +  +  + ]:        8078 :         if (subflow->send_fastclose && ssk->sk_state != TCP_CLOSE)
    2722                 :         427 :                 tcp_set_state(ssk, TCP_CLOSE);
    2723                 :             : 
    2724   [ +  +  +  + ]:        8078 :         need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
    2725         [ +  + ]:        8078 :         if (!dispose_it) {
    2726                 :        1727 :                 __mptcp_subflow_disconnect(ssk, subflow, msk->fastclosing);
    2727                 :        1727 :                 release_sock(ssk);
    2728                 :             : 
    2729                 :        1727 :                 goto out;
    2730                 :             :         }
    2731                 :             : 
    2732                 :        6351 :         subflow->disposable = 1;
    2733                 :             : 
    2734                 :             :         /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
    2735                 :             :          * the ssk has been already destroyed, we just need to release the
    2736                 :             :          * reference owned by msk;
    2737                 :             :          */
    2738         [ -  + ]:        6351 :         if (!inet_csk(ssk)->icsk_ulp_ops) {
    2739         [ #  # ]:           0 :                 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
    2740         [ #  # ]:           0 :                 kfree_rcu(subflow, rcu);
    2741                 :             :         } else {
    2742                 :             :                 /* otherwise tcp will dispose of the ssk and subflow ctx */
    2743                 :        6351 :                 __tcp_close(ssk, 0);
    2744                 :             : 
    2745                 :             :                 /* close acquired an extra ref */
    2746                 :        6351 :                 __sock_put(ssk);
    2747                 :             :         }
    2748                 :             : 
    2749                 :        6387 : out_release:
    2750                 :        6387 :         __mptcp_subflow_error_report(sk, ssk);
    2751                 :        6387 :         release_sock(ssk);
    2752                 :             : 
    2753                 :        6387 :         sock_put(ssk);
    2754                 :             : 
    2755         [ +  + ]:        6387 :         if (ssk == msk->first)
    2756                 :        5069 :                 WRITE_ONCE(msk->first, NULL);
    2757                 :             : 
    2758                 :        1318 : out:
    2759                 :        8114 :         __mptcp_sync_sndbuf(sk);
    2760         [ +  + ]:        8114 :         if (need_push)
    2761                 :          61 :                 __mptcp_push_pending(sk, 0);
    2762                 :             : 
    2763                 :             :         /* Catch every 'all subflows closed' scenario, including peers silently
    2764                 :             :          * closing them, e.g. due to timeout.
    2765                 :             :          * For established sockets, allow an additional timeout before closing,
    2766                 :             :          * as the protocol can still create more subflows.
    2767                 :             :          */
    2768   [ +  +  +  +  :       11975 :         if (list_is_singular(&msk->conn_list) && msk->first &&
                   +  + ]
           [ +  +  +  + ]
    2769         [ +  - ]:          66 :             inet_sk_state_load(msk->first) == TCP_CLOSE) {
    2770   [ +  +  +  + ]:        1683 :                 if (sk->sk_state != TCP_ESTABLISHED ||
    2771         [ -  + ]:          27 :                     msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
    2772                 :        1656 :                         mptcp_set_state(sk, TCP_CLOSE);
    2773                 :        1656 :                         mptcp_close_wake_up(sk);
    2774                 :             :                 } else {
    2775                 :          27 :                         mptcp_start_tout_timer(sk);
    2776                 :             :                 }
    2777                 :             :         }
    2778                 :        8114 : }
    2779                 :             : 
    2780                 :        2425 : void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
    2781                 :             :                      struct mptcp_subflow_context *subflow)
    2782                 :             : {
    2783                 :             :         /* The first subflow can already be closed or disconnected */
    2784   [ +  +  +  + ]:        2425 :         if (subflow->close_event_done || READ_ONCE(subflow->local_id) < 0)
    2785                 :             :                 return;
    2786                 :             : 
    2787                 :        2375 :         subflow->close_event_done = true;
    2788                 :             : 
    2789         [ +  + ]:        2375 :         if (sk->sk_state == TCP_ESTABLISHED)
    2790         [ -  + ]:         337 :                 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
    2791                 :             : 
    2792                 :             :         /* subflow aborted before reaching the fully_established status
    2793                 :             :          * attempt the creation of the next subflow
    2794                 :             :          */
    2795         [ -  + ]:        2375 :         mptcp_pm_subflow_check_next(mptcp_sk(sk), subflow);
    2796                 :             : 
    2797                 :        2375 :         __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
    2798                 :             : }
    2799                 :             : 
    2800                 :           0 : static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
    2801                 :             : {
    2802                 :           0 :         return 0;
    2803                 :             : }
    2804                 :             : 
    2805                 :        1682 : static void __mptcp_close_subflow(struct sock *sk)
    2806                 :             : {
    2807                 :        1682 :         struct mptcp_subflow_context *subflow, *tmp;
    2808         [ -  + ]:        1682 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2809                 :             : 
    2810                 :        1682 :         might_sleep();
    2811                 :             : 
    2812         [ +  + ]:        4287 :         mptcp_for_each_subflow_safe(msk, subflow, tmp) {
    2813                 :        2605 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    2814                 :        2605 :                 int ssk_state = inet_sk_state_load(ssk);
    2815                 :             : 
    2816   [ +  +  +  + ]:        2605 :                 if (ssk_state != TCP_CLOSE &&
    2817         [ +  + ]:          83 :                     (ssk_state != TCP_CLOSE_WAIT ||
    2818         [ -  + ]:          42 :                      inet_sk_state_load(sk) != TCP_ESTABLISHED ||
           [ #  #  #  # ]
    2819                 :          42 :                      __mptcp_check_fallback(msk)))
    2820                 :         383 :                         continue;
    2821                 :             : 
    2822                 :             :                 /* 'subflow_data_ready' will re-sched once rx queue is empty */
    2823         [ -  + ]:        2222 :                 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
    2824                 :           0 :                         continue;
    2825                 :             : 
    2826                 :        2222 :                 mptcp_close_ssk(sk, ssk, subflow);
    2827                 :             :         }
    2828                 :             : 
    2829                 :        1682 : }
    2830                 :             : 
    2831                 :       12647 : static bool mptcp_close_tout_expired(const struct sock *sk)
    2832                 :             : {
    2833         [ +  + ]:       12647 :         if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
    2834         [ +  + ]:        5079 :             sk->sk_state == TCP_CLOSE)
    2835                 :             :                 return false;
    2836                 :             : 
    2837                 :        3791 :         return time_after32(tcp_jiffies32,
    2838                 :             :                   inet_csk(sk)->icsk_mtup.probe_timestamp + mptcp_close_timeout(sk));
    2839                 :             : }
    2840                 :             : 
    2841                 :       12647 : static void mptcp_check_fastclose(struct mptcp_sock *msk)
    2842                 :             : {
    2843                 :       12647 :         struct mptcp_subflow_context *subflow, *tmp;
    2844                 :       12647 :         struct sock *sk = (struct sock *)msk;
    2845                 :             : 
    2846         [ +  + ]:       12647 :         if (likely(!READ_ONCE(msk->rcv_fastclose)))
           [ -  +  +  + ]
    2847                 :             :                 return;
    2848                 :             : 
    2849                 :         228 :         mptcp_token_destroy(msk);
    2850                 :             : 
    2851         [ +  + ]:         496 :         mptcp_for_each_subflow_safe(msk, subflow, tmp) {
    2852                 :         268 :                 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
    2853                 :         268 :                 bool slow;
    2854                 :             : 
    2855                 :         268 :                 slow = lock_sock_fast(tcp_sk);
    2856         [ +  + ]:         268 :                 if (tcp_sk->sk_state != TCP_CLOSE) {
    2857                 :          26 :                         mptcp_send_active_reset_reason(tcp_sk);
    2858                 :          26 :                         tcp_set_state(tcp_sk, TCP_CLOSE);
    2859                 :             :                 }
    2860                 :         268 :                 unlock_sock_fast(tcp_sk, slow);
    2861                 :             :         }
    2862                 :             : 
    2863                 :             :         /* Mirror the tcp_reset() error propagation */
    2864   [ -  +  +  - ]:         228 :         switch (sk->sk_state) {
    2865                 :             :         case TCP_SYN_SENT:
    2866                 :           0 :                 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
    2867                 :           0 :                 break;
    2868                 :             :         case TCP_CLOSE_WAIT:
    2869                 :          26 :                 WRITE_ONCE(sk->sk_err, EPIPE);
    2870                 :          26 :                 break;
    2871                 :             :         case TCP_CLOSE:
    2872                 :             :                 return;
    2873                 :             :         default:
    2874                 :         202 :                 WRITE_ONCE(sk->sk_err, ECONNRESET);
    2875                 :             :         }
    2876                 :             : 
    2877                 :         228 :         mptcp_set_state(sk, TCP_CLOSE);
    2878                 :         228 :         WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
    2879                 :         228 :         smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
    2880                 :         228 :         set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
    2881                 :             : 
    2882                 :             :         /* the calling mptcp_worker will properly destroy the socket */
    2883         [ +  + ]:         228 :         if (sock_flag(sk, SOCK_DEAD))
    2884                 :             :                 return;
    2885                 :             : 
    2886                 :         133 :         sk->sk_state_change(sk);
    2887                 :         133 :         sk_error_report(sk);
    2888                 :             : }
    2889                 :             : 
    2890                 :             : /*
    2891                 :             :  * Retransmit the specified data fragment on all the selected subflows,
    2892                 :             :  * starting from the specified sequence
    2893                 :             :  */
    2894                 :        8696 : static int __mptcp_push_retrans(struct sock *sk, struct mptcp_data_frag *dfrag,
    2895                 :             :                                 u64 sent_seq)
    2896                 :             : {
    2897                 :        8696 :         struct mptcp_sendmsg_info info = { .data_lock_held = true, };
    2898         [ -  + ]:        8696 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2899                 :        8696 :         struct mptcp_subflow_context *subflow;
    2900                 :        8696 :         struct sock *ssk;
    2901                 :        8696 :         int ret, len = 0;
    2902                 :             : 
    2903         [ +  + ]:       25919 :         mptcp_for_each_subflow(msk, subflow) {
    2904         [ +  + ]:       17223 :                 if (READ_ONCE(subflow->scheduled)) {
           [ -  +  +  + ]
    2905                 :        9834 :                         u16 offset = sent_seq - dfrag->data_seq;
    2906                 :        9834 :                         u16 copied = 0;
    2907                 :             : 
    2908                 :        9834 :                         mptcp_subflow_set_scheduled(subflow, false);
    2909                 :             : 
    2910                 :        9834 :                         ssk = mptcp_subflow_tcp_sock(subflow);
    2911                 :             : 
    2912                 :        9834 :                         lock_sock(ssk);
    2913                 :             : 
    2914                 :             :                         /* limit retransmission to the bytes already sent on some subflows */
    2915                 :        9834 :                         info.sent = offset;
    2916         [ -  + ]:        9834 :                         info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
           [ -  +  +  + ]
    2917                 :             :                                                                     dfrag->already_sent;
    2918                 :             : 
    2919                 :             :                         /*
    2920                 :             :                          * make the whole retrans decision, xmit, disallow
    2921                 :             :                          * fallback atomic, note that we can't retrans even
    2922                 :             :                          * when an infinite fallback is in progress, i.e. new
    2923                 :             :                          * subflows are disallowed.
    2924                 :             :                          */
    2925                 :        9834 :                         spin_lock_bh(&msk->fallback_lock);
    2926         [ +  - ]:        9834 :                         if (__mptcp_check_fallback(msk) ||
    2927         [ -  + ]:        9834 :                             !msk->allow_subflows) {
           [ -  +  -  + ]
    2928                 :           0 :                                 spin_unlock_bh(&msk->fallback_lock);
    2929                 :           0 :                                 release_sock(ssk);
    2930                 :           0 :                                 return -1;
    2931                 :             :                         }
    2932                 :             : 
    2933         [ +  + ]:       19678 :                         while (info.sent < info.limit) {
    2934                 :        9846 :                                 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
    2935         [ +  + ]:        9846 :                                 if (ret <= 0)
    2936                 :             :                                         break;
    2937                 :             : 
    2938         [ +  - ]:        9844 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
    2939                 :        9844 :                                 copied += ret;
    2940                 :        9844 :                                 info.sent += ret;
    2941                 :             :                         }
    2942         [ +  - ]:        9834 :                         if (copied) {
    2943                 :        9834 :                                 len = max(copied, len);
    2944         [ -  + ]:        9834 :                                 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
    2945                 :             :                                          info.size_goal);
    2946                 :        9834 :                                 msk->allow_infinite_fallback = false;
    2947                 :             :                         }
    2948                 :        9834 :                         spin_unlock_bh(&msk->fallback_lock);
    2949                 :             : 
    2950                 :        9834 :                         release_sock(ssk);
    2951                 :             :                 }
    2952                 :             :         }
    2953                 :             :         return len;
    2954                 :             : }
    2955                 :             : 
    2956                 :       10939 : static void __mptcp_retrans(struct sock *sk)
    2957                 :             : {
    2958         [ -  + ]:       10939 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2959                 :       10939 :         struct mptcp_subflow_context *subflow;
    2960                 :       10939 :         struct mptcp_data_frag *dfrag;
    2961                 :       10939 :         u64 retrans_seq, sent_seq;
    2962                 :       10939 :         bool need_retrans;
    2963                 :       10939 :         int err, len;
    2964                 :             : 
    2965                 :       10939 :         mptcp_pm_chk_stale(msk);
    2966                 :             : 
    2967                 :             :         /* Get an updated and consistent rtx queue status. */
    2968                 :       10939 :         mptcp_data_lock(sk);
    2969                 :       10939 :         __mptcp_clean_una_wakeup(sk);
    2970                 :       10939 :         retrans_seq = msk->snd_una;
    2971                 :       10939 :         dfrag = mptcp_rtx_head(sk);
    2972                 :       10939 :         need_retrans = !!dfrag;
    2973                 :       10939 :         mptcp_data_unlock(sk);
    2974                 :             : 
    2975                 :        4874 :         for (;;) {
    2976                 :       15813 :                 bool already_acked;
    2977                 :             : 
    2978                 :       15813 :                 err = mptcp_sched_get_retrans(msk);
    2979         [ +  + ]:       15813 :                 if (err)
    2980                 :             :                         break;
    2981                 :             : 
    2982                 :             :                 /* `already_sent` can be 0 for `dfrag` belonging to the RTX
    2983                 :             :                  *  queue due to __mptcp_retransmit_pending_data().
    2984                 :             :                  */
    2985   [ +  +  +  - ]:       12060 :                 if (!dfrag || !dfrag->already_sent)
    2986                 :             :                         break;
    2987                 :             : 
    2988                 :             :                 /* Can fail only in case of fallback. */
    2989                 :        8696 :                 len = __mptcp_push_retrans(sk, dfrag, retrans_seq);
    2990         [ -  + ]:        8696 :                 if (len < 0)
    2991                 :           0 :                         goto clear_scheduled;
    2992                 :             : 
    2993                 :        8696 :                 retrans_seq += len;
    2994                 :        8696 :                 msk->bytes_retrans += len;
    2995                 :        8696 :                 dfrag->already_sent = max_t(u16, dfrag->already_sent,
    2996                 :             :                                             retrans_seq - dfrag->data_seq);
    2997                 :             : 
    2998                 :             :                 /* With csum enabled, retransmission can send new data. */
    2999                 :        8696 :                 sent_seq = dfrag->already_sent + dfrag->data_seq;
    3000         [ -  + ]:        8696 :                 if (after64(sent_seq, msk->snd_nxt))
    3001                 :           0 :                         WRITE_ONCE(msk->snd_nxt, sent_seq);
    3002                 :             : 
    3003                 :             :                 /* Attempt the next fragment only if the current one is
    3004                 :             :                  * completely retransmitted.
    3005                 :             :                  */
    3006         [ +  + ]:        8696 :                 if (before64(retrans_seq, dfrag->data_seq + dfrag->data_len))
    3007                 :             :                         break;
    3008                 :             : 
    3009         [ +  + ]:        8691 :                 dfrag = list_is_last(&dfrag->list, &msk->rtx_queue) ?
    3010         [ +  + ]:        8691 :                                 NULL : list_next_entry(dfrag, list);
    3011         [ +  - ]:        4874 :                 if (!dfrag)
    3012                 :             :                         break;
    3013                 :             : 
    3014                 :             :                 /* Incoming acks can move snd_una after the current dfrag
    3015                 :             :                  * across loop iterations, if so start again from RTX head.
    3016                 :             :                  */
    3017                 :        4874 :                 mptcp_data_lock(sk);
    3018                 :        4874 :                 already_acked = !before64(msk->snd_una, dfrag->data_seq +
    3019         [ +  + ]:        4874 :                                           dfrag->already_sent);
    3020         [ +  + ]:        4874 :                 if (already_acked) {
    3021                 :        1471 :                         __mptcp_clean_una_wakeup(sk);
    3022                 :        1471 :                         retrans_seq = msk->snd_una;
    3023                 :        1471 :                         dfrag = mptcp_rtx_head(sk);
    3024                 :        1471 :                         need_retrans = !!dfrag;
    3025         [ +  + ]:        3403 :                 } else if (after64(msk->snd_una, retrans_seq)) {
    3026                 :           1 :                         retrans_seq = msk->snd_una;
    3027                 :             :                 }
    3028                 :        4874 :                 mptcp_data_unlock(sk);
    3029                 :             :         }
    3030                 :             : 
    3031                 :             :         /* Attempt data-fin retransmission only when the RTX queue is empty. */
    3032         [ +  + ]:       10939 :         if (!need_retrans) {
    3033         [ +  + ]:        4133 :                 if (mptcp_data_fin_enabled(msk)) {
    3034                 :        1379 :                         struct inet_connection_sock *icsk = inet_csk(sk);
    3035                 :             : 
    3036                 :        1379 :                         WRITE_ONCE(icsk->icsk_retransmits,
    3037                 :             :                                    icsk->icsk_retransmits + 1);
    3038                 :        1379 :                         mptcp_set_datafin_timeout(sk);
    3039                 :        1379 :                         mptcp_send_ack(msk);
    3040                 :        1379 :                         goto reset_timer;
    3041                 :             :                 }
    3042                 :             : 
    3043         [ +  + ]:        2754 :                 if (!mptcp_send_head(sk))
    3044                 :        2681 :                         goto clear_scheduled;
    3045                 :             :         }
    3046                 :             : 
    3047                 :        3064 : reset_timer:
    3048                 :        8258 :         mptcp_check_and_set_pending(sk);
    3049                 :             : 
    3050         [ +  + ]:        8258 :         if (!mptcp_rtx_timer_pending(sk))
    3051                 :         165 :                 mptcp_reset_rtx_timer(sk);
    3052                 :             : 
    3053                 :        8093 : clear_scheduled:
    3054                 :             :         /* If no rtx data was available or in case of fallback, there
    3055                 :             :          * could be left-over scheduled subflows; clear them all
    3056                 :             :          * or later xmit could use bad ones
    3057                 :             :          */
    3058         [ +  + ]:       29570 :         mptcp_for_each_subflow(msk, subflow)
    3059         [ +  + ]:       18631 :                 if (READ_ONCE(subflow->scheduled))
           [ -  +  +  + ]
    3060                 :        3713 :                         mptcp_subflow_set_scheduled(subflow, false);
    3061                 :       10939 : }
    3062                 :             : 
    3063                 :             : /* schedule the timeout timer for the relevant event: either close timeout
    3064                 :             :  * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
    3065                 :             :  */
    3066                 :        1502 : void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
    3067                 :             : {
    3068                 :        1502 :         struct sock *sk = (struct sock *)msk;
    3069                 :        1502 :         unsigned long timeout, close_timeout;
    3070                 :             : 
    3071   [ +  +  +  - ]:        1502 :         if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
    3072                 :             :                 return;
    3073                 :             : 
    3074                 :        3004 :         close_timeout = (unsigned long)inet_csk(sk)->icsk_mtup.probe_timestamp -
    3075                 :        1502 :                         tcp_jiffies32 + jiffies + mptcp_close_timeout(sk);
    3076                 :             : 
    3077                 :             :         /* the close timeout takes precedence on the fail one, and here at least one of
    3078                 :             :          * them is active
    3079                 :             :          */
    3080         [ +  + ]:        1502 :         timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
    3081                 :             : 
    3082                 :        1502 :         sk_reset_timer(sk, &inet_csk(sk)->mptcp_tout_timer, timeout);
    3083                 :             : }
    3084                 :             : 
    3085                 :           0 : static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
    3086                 :             : {
    3087                 :           0 :         struct sock *ssk = msk->first;
    3088                 :           0 :         bool slow;
    3089                 :             : 
    3090         [ #  # ]:           0 :         if (!ssk)
    3091                 :             :                 return;
    3092                 :             : 
    3093   [ #  #  #  # ]:           0 :         pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
    3094                 :             : 
    3095                 :           0 :         slow = lock_sock_fast(ssk);
    3096                 :           0 :         mptcp_subflow_reset(ssk);
    3097                 :           0 :         WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
    3098                 :           0 :         unlock_sock_fast(ssk, slow);
    3099                 :             : }
    3100                 :             : 
    3101                 :        5736 : static void mptcp_backlog_purge(struct sock *sk)
    3102                 :             : {
    3103         [ -  + ]:        5736 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3104                 :        5736 :         struct sk_buff *tmp, *skb;
    3105                 :        5736 :         LIST_HEAD(backlog);
    3106                 :             : 
    3107                 :        5736 :         mptcp_data_lock(sk);
    3108         [ -  + ]:        5736 :         list_splice_init(&msk->backlog_list, &backlog);
    3109                 :        5736 :         msk->backlog_len = 0;
    3110                 :        5736 :         mptcp_data_unlock(sk);
    3111                 :             : 
    3112         [ -  + ]:        5736 :         list_for_each_entry_safe(skb, tmp, &backlog, list) {
    3113                 :           0 :                 mptcp_borrow_fwdmem(sk, skb);
    3114                 :           0 :                 kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE);
    3115                 :             :         }
    3116                 :        5736 :         sk_mem_reclaim(sk);
    3117                 :        5736 : }
    3118                 :             : 
    3119                 :         539 : static void mptcp_do_fastclose(struct sock *sk)
    3120                 :             : {
    3121                 :         539 :         struct mptcp_subflow_context *subflow, *tmp;
    3122         [ -  + ]:         539 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3123                 :             : 
    3124                 :         539 :         mptcp_set_state(sk, TCP_CLOSE);
    3125                 :         539 :         mptcp_backlog_purge(sk);
    3126                 :         539 :         msk->fastclosing = 1;
    3127                 :             : 
    3128                 :             :         /* Explicitly send the fastclose reset as need */
    3129         [ +  + ]:         539 :         if (__mptcp_check_fallback(msk))
    3130                 :             :                 return;
    3131                 :             : 
    3132         [ +  + ]:        1023 :         mptcp_for_each_subflow_safe(msk, subflow, tmp) {
    3133                 :         553 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    3134                 :             : 
    3135                 :         553 :                 lock_sock(ssk);
    3136                 :             : 
    3137                 :             :                 /* Some subflow socket states don't allow/need a reset.*/
    3138         [ +  + ]:         553 :                 if ((1 << ssk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
           [ -  +  +  + ]
    3139                 :          95 :                         goto unlock;
    3140                 :             : 
    3141                 :         458 :                 subflow->send_fastclose = 1;
    3142                 :             : 
    3143                 :             :                 /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0
    3144                 :             :                  * issue in __tcp_select_window(), see tcp_disconnect().
    3145                 :             :                  */
    3146                 :         458 :                 inet_csk(ssk)->icsk_ack.rcv_mss = TCP_MIN_MSS;
    3147                 :             : 
    3148                 :         458 :                 tcp_send_active_reset(ssk, ssk->sk_allocation,
    3149                 :             :                                       SK_RST_REASON_TCP_ABORT_ON_CLOSE);
    3150                 :         553 : unlock:
    3151                 :         553 :                 release_sock(ssk);
    3152                 :             :         }
    3153                 :             : }
    3154                 :             : 
    3155                 :       13742 : static void mptcp_worker(struct work_struct *work)
    3156                 :             : {
    3157                 :       13742 :         struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
    3158                 :       13742 :         struct sock *sk = (struct sock *)msk;
    3159                 :       13742 :         unsigned long fail_tout;
    3160                 :       13742 :         int state;
    3161                 :             : 
    3162                 :       13742 :         lock_sock(sk);
    3163                 :       13742 :         state = sk->sk_state;
    3164         [ +  + ]:       13742 :         if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
           [ -  +  +  + ]
    3165                 :        1095 :                 goto unlock;
    3166                 :             : 
    3167                 :       12647 :         mptcp_check_fastclose(msk);
    3168                 :             : 
    3169                 :       12647 :         mptcp_pm_worker(msk);
    3170                 :             : 
    3171                 :       12647 :         mptcp_check_send_data_fin(sk);
    3172                 :       12647 :         mptcp_check_data_fin_ack(sk);
    3173                 :       12647 :         mptcp_check_data_fin(sk);
    3174                 :             : 
    3175         [ +  + ]:       19516 :         if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
    3176                 :        1682 :                 __mptcp_close_subflow(sk);
    3177                 :             : 
    3178         [ +  + ]:       12647 :         if (mptcp_close_tout_expired(sk)) {
    3179                 :         130 :                 struct mptcp_subflow_context *subflow, *tmp;
    3180                 :             : 
    3181                 :         130 :                 mptcp_do_fastclose(sk);
    3182         [ +  + ]:         282 :                 mptcp_for_each_subflow_safe(msk, subflow, tmp)
    3183                 :         152 :                         __mptcp_close_ssk(sk, subflow->tcp_sock, subflow, 0);
    3184                 :         130 :                 mptcp_close_wake_up(sk);
    3185                 :             :         }
    3186                 :             : 
    3187   [ +  +  +  + ]:       12647 :         if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
    3188                 :        1406 :                 __mptcp_destroy_sock(sk);
    3189                 :        1406 :                 goto unlock;
    3190                 :             :         }
    3191                 :             : 
    3192         [ +  + ]:       17363 :         if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
    3193                 :        4929 :                 __mptcp_retrans(sk);
    3194                 :             : 
    3195   [ +  -  +  - ]:       11241 :         fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
    3196   [ +  -  -  - ]:       11241 :         if (fail_tout && time_after(jiffies, fail_tout))
    3197                 :           0 :                 mptcp_mp_fail_no_response(msk);
    3198                 :             : 
    3199                 :           0 : unlock:
    3200                 :       13742 :         release_sock(sk);
    3201                 :       13742 :         sock_put(sk);
    3202                 :       13742 : }
    3203                 :             : 
    3204                 :        5208 : static void __mptcp_init_sock(struct sock *sk)
    3205                 :             : {
    3206                 :        5208 :         struct inet_connection_sock *icsk = inet_csk(sk);
    3207         [ -  + ]:        5208 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3208         [ -  + ]:        5208 :         struct net *net = sock_net(sk);
    3209                 :             : 
    3210         [ -  + ]:        5208 :         INIT_LIST_HEAD(&msk->conn_list);
    3211                 :        5208 :         INIT_LIST_HEAD(&msk->join_list);
    3212                 :        5208 :         INIT_LIST_HEAD(&msk->rtx_queue);
    3213                 :        5208 :         INIT_LIST_HEAD(&msk->backlog_list);
    3214         [ -  + ]:        5208 :         INIT_WORK(&msk->work, mptcp_worker);
    3215                 :        5208 :         msk->out_of_order_queue = RB_ROOT;
    3216                 :        5208 :         msk->first_pending = NULL;
    3217                 :             : 
    3218                 :             :         /* msk does not go through tcp_init_sock(); seed RTO bounds. */
    3219                 :       10416 :         icsk->icsk_rto_min =
    3220         [ -  + ]:        5208 :                 usecs_to_jiffies(READ_ONCE(net->ipv4.sysctl_tcp_rto_min_us));
    3221                 :       10416 :         icsk->icsk_rto_max =
    3222         [ -  + ]:        5208 :                 msecs_to_jiffies(READ_ONCE(net->ipv4.sysctl_tcp_rto_max_ms));
    3223                 :        5208 :         msk->timer_ival = icsk->icsk_rto_min;
    3224                 :        5208 :         msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
    3225                 :        5208 :         msk->backlog_len = 0;
    3226                 :        5208 :         mptcp_init_rtt_est(msk);
    3227                 :             : 
    3228                 :        5208 :         WRITE_ONCE(msk->first, NULL);
    3229                 :        5208 :         inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
    3230                 :        5208 :         WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
    3231                 :        5208 :         msk->allow_infinite_fallback = true;
    3232                 :        5208 :         msk->allow_subflows = true;
    3233                 :        5208 :         msk->recovery = false;
    3234                 :        5208 :         msk->subflow_id = 1;
    3235                 :        5208 :         msk->last_data_sent = tcp_jiffies32;
    3236                 :        5208 :         msk->last_data_recv = tcp_jiffies32;
    3237                 :        5208 :         msk->last_ack_recv = tcp_jiffies32;
    3238                 :             : 
    3239                 :        5208 :         mptcp_pm_data_init(msk);
    3240                 :        5208 :         spin_lock_init(&msk->fallback_lock);
    3241                 :             : 
    3242                 :             :         /* re-use the csk retrans timer for MPTCP-level retrans */
    3243                 :        5208 :         timer_setup(&sk->mptcp_retransmit_timer, mptcp_retransmit_timer, 0);
    3244                 :        5208 :         timer_setup(&msk->sk.mptcp_tout_timer, mptcp_tout_timer, 0);
    3245                 :        5208 : }
    3246                 :             : 
    3247                 :        3638 : static void mptcp_ca_reset(struct sock *sk)
    3248                 :             : {
    3249                 :        3638 :         struct inet_connection_sock *icsk = inet_csk(sk);
    3250                 :             : 
    3251                 :        3638 :         tcp_assign_congestion_control(sk);
    3252         [ -  + ]:        3638 :         strscpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name,
    3253                 :             :                 sizeof(mptcp_sk(sk)->ca_name));
    3254                 :             : 
    3255                 :             :         /* no need to keep a reference to the ops, the name will suffice */
    3256                 :        3638 :         tcp_cleanup_congestion_control(sk);
    3257                 :        3638 :         icsk->icsk_ca_ops = NULL;
    3258                 :        3638 : }
    3259                 :             : 
    3260                 :        3542 : static int mptcp_init_sock(struct sock *sk)
    3261                 :             : {
    3262                 :        3542 :         struct net *net = sock_net(sk);
    3263                 :        3542 :         int ret;
    3264                 :             : 
    3265                 :        3542 :         __mptcp_init_sock(sk);
    3266                 :             : 
    3267         [ +  + ]:        3542 :         if (!mptcp_is_enabled(net))
    3268                 :             :                 return -ENOPROTOOPT;
    3269                 :             : 
    3270   [ +  +  +  - ]:        3532 :         if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
    3271                 :             :                 return -ENOMEM;
    3272                 :             : 
    3273                 :        3532 :         rcu_read_lock();
    3274         [ -  + ]:        3532 :         ret = mptcp_init_sched(mptcp_sk(sk),
    3275                 :             :                                mptcp_sched_find(mptcp_get_scheduler(net)));
    3276                 :        3532 :         rcu_read_unlock();
    3277         [ +  - ]:        3532 :         if (ret)
    3278                 :             :                 return ret;
    3279                 :             : 
    3280                 :        3532 :         set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
    3281                 :             : 
    3282                 :             :         /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
    3283                 :             :          * propagate the correct value
    3284                 :             :          */
    3285                 :        3532 :         mptcp_ca_reset(sk);
    3286                 :             : 
    3287                 :        3532 :         sk_sockets_allocated_inc(sk);
    3288                 :        3532 :         sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
    3289                 :        3532 :         sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
    3290                 :        3532 :         sk->sk_write_space = sk_stream_write_space;
    3291                 :             : 
    3292                 :        3532 :         return 0;
    3293                 :             : }
    3294                 :             : 
    3295                 :        5197 : static void __mptcp_clear_xmit(struct sock *sk)
    3296                 :             : {
    3297         [ -  + ]:        5197 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3298                 :        5197 :         struct mptcp_data_frag *dtmp, *dfrag;
    3299                 :             : 
    3300                 :        5197 :         msk->first_pending = NULL;
    3301         [ +  + ]:        5373 :         list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
    3302                 :         176 :                 dfrag_clear(sk, dfrag);
    3303                 :        5197 : }
    3304                 :             : 
    3305                 :        3675 : void mptcp_cancel_work(struct sock *sk)
    3306                 :             : {
    3307         [ -  + ]:        3675 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3308                 :             : 
    3309         [ +  + ]:        3675 :         if (cancel_work_sync(&msk->work))
    3310                 :          36 :                 __sock_put(sk);
    3311                 :        3675 : }
    3312                 :             : 
    3313                 :        4217 : void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
    3314                 :             : {
    3315                 :        4217 :         lock_sock(ssk);
    3316                 :             : 
    3317      [ -  +  + ]:        4217 :         switch (ssk->sk_state) {
    3318                 :           0 :         case TCP_LISTEN:
    3319         [ #  # ]:           0 :                 if (!(how & RCV_SHUTDOWN))
    3320                 :             :                         break;
    3321                 :          18 :                 fallthrough;
    3322                 :             :         case TCP_SYN_SENT:
    3323         [ -  + ]:          18 :                 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
    3324                 :             :                 break;
    3325                 :        4199 :         default:
    3326   [ -  +  +  + ]:        4199 :                 if (__mptcp_check_fallback(mptcp_sk(sk))) {
    3327   [ -  +  -  - ]:         178 :                         pr_debug("Fallback\n");
    3328                 :         178 :                         ssk->sk_shutdown |= how;
    3329                 :         178 :                         tcp_shutdown(ssk, how);
    3330                 :             : 
    3331                 :             :                         /* simulate the data_fin ack reception to let the state
    3332                 :             :                          * machine move forward
    3333                 :             :                          */
    3334   [ -  +  -  + ]:         178 :                         WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
    3335                 :         178 :                         mptcp_schedule_work(sk);
    3336                 :             :                 } else {
    3337   [ -  +  -  - ]:        4021 :                         pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
    3338                 :        4021 :                         tcp_send_ack(ssk);
    3339         [ +  + ]:        4021 :                         if (!mptcp_rtx_timer_pending(sk))
    3340                 :        1887 :                                 mptcp_reset_rtx_timer(sk);
    3341                 :             :                 }
    3342                 :             :                 break;
    3343                 :             :         }
    3344                 :             : 
    3345                 :        4217 :         release_sock(ssk);
    3346                 :        4217 : }
    3347                 :             : 
    3348                 :       25107 : void mptcp_set_state(struct sock *sk, int state)
    3349                 :             : {
    3350                 :       26084 :         int oldstate = sk->sk_state;
    3351                 :             : 
    3352      [ +  +  - ]:       25052 :         switch (state) {
    3353                 :        3348 :         case TCP_ESTABLISHED:
    3354         [ +  - ]:        3348 :                 if (oldstate != TCP_ESTABLISHED)
    3355         [ +  - ]:        3348 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
    3356                 :             :                 break;
    3357                 :             :         case TCP_CLOSE_WAIT:
    3358                 :             :                 /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
    3359                 :             :                  * MPTCP "accepted" sockets will be created later on. So no
    3360                 :             :                  * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
    3361                 :             :                  */
    3362                 :             :                 break;
    3363                 :       21704 :         default:
    3364         [ +  + ]:       21704 :                 if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
    3365         [ +  - ]:        3348 :                         MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
    3366                 :             :         }
    3367                 :             : 
    3368                 :       26084 :         inet_sk_state_store(sk, state);
    3369                 :        1032 : }
    3370                 :             : 
    3371                 :             : static const unsigned char new_state[16] = {
    3372                 :             :         /* current state:     new state:      action:   */
    3373                 :             :         [0 /* (Invalid) */] = TCP_CLOSE,
    3374                 :             :         [TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
    3375                 :             :         [TCP_SYN_SENT]      = TCP_CLOSE,
    3376                 :             :         [TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
    3377                 :             :         [TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
    3378                 :             :         [TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
    3379                 :             :         [TCP_TIME_WAIT]     = TCP_CLOSE,        /* should not happen ! */
    3380                 :             :         [TCP_CLOSE]         = TCP_CLOSE,
    3381                 :             :         [TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
    3382                 :             :         [TCP_LAST_ACK]      = TCP_LAST_ACK,
    3383                 :             :         [TCP_LISTEN]        = TCP_CLOSE,
    3384                 :             :         [TCP_CLOSING]       = TCP_CLOSING,
    3385                 :             :         [TCP_NEW_SYN_RECV]  = TCP_CLOSE,        /* should not happen ! */
    3386                 :             : };
    3387                 :             : 
    3388                 :        3527 : static int mptcp_close_state(struct sock *sk)
    3389                 :             : {
    3390                 :        3527 :         int next = (int)new_state[sk->sk_state];
    3391                 :        3527 :         int ns = next & TCP_STATE_MASK;
    3392                 :             : 
    3393                 :        3527 :         mptcp_set_state(sk, ns);
    3394                 :             : 
    3395                 :        3527 :         return next & TCP_ACTION_FIN;
    3396                 :             : }
    3397                 :             : 
    3398                 :      520639 : static void mptcp_check_send_data_fin(struct sock *sk)
    3399                 :             : {
    3400                 :      520639 :         struct mptcp_subflow_context *subflow;
    3401         [ -  + ]:      520639 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3402                 :             : 
    3403   [ -  +  -  - ]:      520639 :         pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
           [ -  +  -  -  
                   -  - ]
    3404                 :             :                  msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
    3405                 :             :                  msk->snd_nxt, msk->write_seq);
    3406                 :             : 
    3407                 :             :         /* we still need to enqueue subflows or not really shutting down,
    3408                 :             :          * skip this
    3409                 :             :          */
    3410   [ +  +  +  +  :      523691 :         if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
           -  + ][ -  +  
          +  +  +  +  -  
                      + ]
    3411                 :        3052 :             mptcp_send_head(sk))
    3412                 :      517587 :                 return;
    3413                 :             : 
    3414                 :        3052 :         WRITE_ONCE(msk->snd_nxt, msk->write_seq);
    3415                 :             : 
    3416         [ +  + ]:        7072 :         mptcp_for_each_subflow(msk, subflow) {
    3417                 :        4020 :                 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
    3418                 :             : 
    3419                 :        4020 :                 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
    3420                 :             :         }
    3421                 :             : }
    3422                 :             : 
    3423                 :        3058 : static void __mptcp_wr_shutdown(struct sock *sk)
    3424                 :             : {
    3425         [ -  + ]:        3058 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3426                 :             : 
    3427   [ -  +  -  - ]:        3058 :         pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
           [ -  +  -  -  
                   -  - ]
    3428                 :             :                  msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
    3429                 :             :                  !!mptcp_send_head(sk));
    3430                 :             : 
    3431                 :             :         /* will be ignored by fallback sockets */
    3432                 :        3058 :         WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
    3433                 :        3058 :         WRITE_ONCE(msk->snd_data_fin_enable, 1);
    3434                 :             : 
    3435                 :        3058 :         mptcp_check_send_data_fin(sk);
    3436                 :        3058 : }
    3437                 :             : 
    3438                 :        5081 : static void __mptcp_destroy_sock(struct sock *sk)
    3439                 :             : {
    3440         [ -  + ]:        5081 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3441                 :             : 
    3442   [ -  +  -  - ]:        5081 :         pr_debug("msk=%p\n", msk);
    3443                 :             : 
    3444                 :        5081 :         might_sleep();
    3445                 :             : 
    3446                 :        5081 :         mptcp_stop_rtx_timer(sk);
    3447                 :        5081 :         sk_stop_timer(sk, &inet_csk(sk)->mptcp_tout_timer);
    3448                 :        5081 :         msk->pm.status = 0;
    3449                 :        5081 :         mptcp_release_sched(msk);
    3450                 :             : 
    3451                 :        5081 :         sk->sk_prot->destroy(sk);
    3452                 :             : 
    3453                 :        5081 :         sk_stream_kill_queues(sk);
    3454                 :        5081 :         xfrm_sk_free_policy(sk);
    3455                 :             : 
    3456                 :        5081 :         sock_put(sk);
    3457                 :        5081 : }
    3458                 :             : 
    3459                 :          36 : void __mptcp_unaccepted_force_close(struct sock *sk)
    3460                 :             : {
    3461                 :          36 :         sock_set_flag(sk, SOCK_DEAD);
    3462                 :          36 :         mptcp_do_fastclose(sk);
    3463                 :          36 :         __mptcp_destroy_sock(sk);
    3464                 :          36 : }
    3465                 :             : 
    3466                 :           0 : static __poll_t mptcp_check_readable(struct sock *sk)
    3467                 :             : {
    3468         [ +  + ]:     1254315 :         return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
    3469                 :             : }
    3470                 :             : 
    3471                 :        3408 : static void mptcp_check_listen_stop(struct sock *sk)
    3472                 :             : {
    3473                 :        3408 :         struct sock *ssk;
    3474                 :             : 
    3475         [ +  + ]:        3408 :         if (inet_sk_state_load(sk) != TCP_LISTEN)
    3476                 :             :                 return;
    3477                 :             : 
    3478                 :        1791 :         sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
    3479         [ -  + ]:        1791 :         ssk = mptcp_sk(sk)->first;
    3480   [ +  -  -  + ]:        3582 :         if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
    3481                 :           0 :                 return;
    3482                 :             : 
    3483                 :        1791 :         lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
    3484                 :        1791 :         tcp_set_state(ssk, TCP_CLOSE);
    3485                 :        1791 :         mptcp_subflow_queue_clean(sk, ssk);
    3486                 :        1791 :         inet_csk_listen_stop(ssk);
    3487                 :        1791 :         mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
    3488                 :        1791 :         release_sock(ssk);
    3489                 :             : }
    3490                 :             : 
    3491                 :        5112 : bool __mptcp_close(struct sock *sk, long timeout)
    3492                 :             : {
    3493                 :        5112 :         struct mptcp_subflow_context *subflow;
    3494         [ -  + ]:        5112 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3495                 :        5112 :         bool do_cancel_work = false;
    3496                 :        5112 :         int subflows_alive = 0;
    3497                 :             : 
    3498                 :        5112 :         WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
    3499                 :             : 
    3500         [ +  + ]:        5112 :         if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
           [ -  +  +  + ]
    3501                 :        3302 :                 mptcp_check_listen_stop(sk);
    3502                 :        3302 :                 mptcp_set_state(sk, TCP_CLOSE);
    3503                 :        3302 :                 goto cleanup;
    3504                 :             :         }
    3505                 :             : 
    3506   [ +  +  +  -  :        3365 :         if (mptcp_data_avail(msk) || timeout < 0 ||
                   +  + ]
    3507         [ +  - ]:        1567 :             (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
    3508                 :             :                 /* If the msk has read data, or the caller explicitly ask it,
    3509                 :             :                  * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
    3510                 :             :                  */
    3511                 :         267 :                 mptcp_do_fastclose(sk);
    3512                 :         267 :                 timeout = 0;
    3513         [ +  + ]:        1543 :         } else if (mptcp_close_state(sk)) {
    3514                 :        1234 :                 __mptcp_wr_shutdown(sk);
    3515                 :             :         }
    3516                 :             : 
    3517                 :        1810 :         sk_stream_wait_close(sk, timeout);
    3518                 :             : 
    3519                 :        5112 : cleanup:
    3520                 :             :         /* orphan all the subflows */
    3521         [ +  + ]:       10775 :         mptcp_for_each_subflow(msk, subflow) {
    3522                 :        5663 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    3523                 :        5663 :                 bool slow = lock_sock_fast_nested(ssk);
    3524                 :             : 
    3525                 :        5663 :                 subflows_alive += ssk->sk_state != TCP_CLOSE;
    3526                 :             : 
    3527                 :             :                 /* since the close timeout takes precedence on the fail one,
    3528                 :             :                  * cancel the latter
    3529                 :             :                  */
    3530         [ +  + ]:        5663 :                 if (ssk == msk->first)
    3531                 :        5100 :                         subflow->fail_tout = 0;
    3532                 :             : 
    3533                 :             :                 /* detach from the parent socket, but allow data_ready to
    3534                 :             :                  * push incoming data into the mptcp stack, to properly ack it
    3535                 :             :                  */
    3536                 :        5663 :                 ssk->sk_socket = NULL;
    3537                 :        5663 :                 ssk->sk_wq = NULL;
    3538                 :        5663 :                 unlock_sock_fast(ssk, slow);
    3539                 :             :         }
    3540                 :        5112 :         sock_orphan(sk);
    3541                 :             : 
    3542                 :             :         /* all the subflows are closed, only timeout can change the msk
    3543                 :             :          * state, let's not keep resources busy for no reasons
    3544                 :             :          */
    3545         [ +  + ]:        5112 :         if (subflows_alive == 0)
    3546                 :        2870 :                 mptcp_set_state(sk, TCP_CLOSE);
    3547                 :             : 
    3548                 :        5112 :         sock_hold(sk);
    3549   [ -  +  -  - ]:        5112 :         pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
    3550                 :        5112 :         mptcp_pm_connection_closed(msk);
    3551                 :             : 
    3552         [ +  + ]:        5112 :         if (sk->sk_state == TCP_CLOSE) {
    3553                 :        3639 :                 __mptcp_destroy_sock(sk);
    3554                 :        3639 :                 do_cancel_work = true;
    3555                 :             :         } else {
    3556                 :        1473 :                 mptcp_start_tout_timer(sk);
    3557                 :             :         }
    3558                 :             : 
    3559                 :        5112 :         return do_cancel_work;
    3560                 :             : }
    3561                 :             : 
    3562                 :        5112 : static void mptcp_close(struct sock *sk, long timeout)
    3563                 :             : {
    3564                 :        5112 :         bool do_cancel_work;
    3565                 :             : 
    3566                 :        5112 :         lock_sock(sk);
    3567                 :             : 
    3568                 :        5112 :         do_cancel_work = __mptcp_close(sk, timeout);
    3569                 :        5112 :         release_sock(sk);
    3570         [ +  + ]:        5112 :         if (do_cancel_work)
    3571                 :        3639 :                 mptcp_cancel_work(sk);
    3572                 :             : 
    3573                 :        5112 :         sock_put(sk);
    3574                 :        5112 : }
    3575                 :             : 
    3576                 :        6992 : static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
    3577                 :             : {
    3578                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    3579         [ +  - ]:        6992 :         const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
    3580         [ +  - ]:        6992 :         struct ipv6_pinfo *msk6 = inet6_sk(msk);
    3581                 :             : 
    3582                 :        6992 :         msk->sk_v6_daddr = ssk->sk_v6_daddr;
    3583                 :        6992 :         msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
    3584                 :             : 
    3585         [ +  + ]:        6992 :         if (msk6 && ssk6) {
    3586                 :        3074 :                 msk6->saddr = ssk6->saddr;
    3587                 :        3074 :                 msk6->flow_label = ssk6->flow_label;
    3588                 :             :         }
    3589                 :             : #endif
    3590                 :             : 
    3591                 :        6992 :         inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
    3592                 :        6992 :         inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
    3593                 :        6992 :         inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
    3594                 :        6992 :         inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
    3595                 :        6992 :         inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
    3596                 :        6992 :         inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
    3597                 :        6992 : }
    3598                 :             : 
    3599                 :        5197 : static void mptcp_destroy_common(struct mptcp_sock *msk)
    3600                 :             : {
    3601                 :        5197 :         struct mptcp_subflow_context *subflow, *tmp;
    3602                 :        5197 :         struct sock *sk = (struct sock *)msk;
    3603                 :             : 
    3604                 :        5197 :         __mptcp_clear_xmit(sk);
    3605                 :        5197 :         mptcp_backlog_purge(sk);
    3606                 :             : 
    3607                 :             :         /* join list will be eventually flushed (with rst) at sock lock release time */
    3608         [ +  + ]:       10784 :         mptcp_for_each_subflow_safe(msk, subflow, tmp)
    3609                 :        5587 :                 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, 0);
    3610                 :             : 
    3611                 :        5197 :         __skb_queue_purge(&sk->sk_receive_queue);
    3612                 :        5197 :         skb_rbtree_purge(&msk->out_of_order_queue);
    3613                 :             : 
    3614                 :             :         /* move all the rx fwd alloc into the sk_mem_reclaim_final in
    3615                 :             :          * inet_sock_destruct() will dispose it
    3616                 :             :          */
    3617                 :        5197 :         mptcp_token_destroy(msk);
    3618                 :        5197 :         mptcp_pm_destroy(msk);
    3619                 :        5197 : }
    3620                 :             : 
    3621                 :         106 : static int mptcp_disconnect(struct sock *sk, int flags)
    3622                 :             : {
    3623         [ -  + ]:         106 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3624                 :             : 
    3625                 :             :         /* We are on the fastopen error path. We can't call straight into the
    3626                 :             :          * subflows cleanup code due to lock nesting (we are already under
    3627                 :             :          * msk->firstsocket lock).
    3628                 :             :          */
    3629         [ +  - ]:         106 :         if (msk->fastopening)
    3630                 :             :                 return -EBUSY;
    3631                 :             : 
    3632                 :         106 :         mptcp_check_listen_stop(sk);
    3633                 :         106 :         mptcp_set_state(sk, TCP_CLOSE);
    3634                 :             : 
    3635                 :         106 :         mptcp_stop_rtx_timer(sk);
    3636                 :         106 :         mptcp_stop_tout_timer(sk);
    3637                 :             : 
    3638                 :         106 :         mptcp_pm_connection_closed(msk);
    3639                 :             : 
    3640                 :             :         /* msk->subflow is still intact, the following will not free the first
    3641                 :             :          * subflow
    3642                 :             :          */
    3643                 :         106 :         mptcp_do_fastclose(sk);
    3644                 :         106 :         mptcp_destroy_common(msk);
    3645                 :             : 
    3646                 :             :         /* The first subflow is already in TCP_CLOSE status, the following
    3647                 :             :          * can't overlap with a fallback anymore
    3648                 :             :          */
    3649                 :         106 :         spin_lock_bh(&msk->fallback_lock);
    3650                 :         106 :         msk->allow_subflows = true;
    3651                 :         106 :         msk->allow_infinite_fallback = true;
    3652                 :         106 :         WRITE_ONCE(msk->flags, 0);
    3653                 :         106 :         spin_unlock_bh(&msk->fallback_lock);
    3654                 :             : 
    3655                 :         106 :         msk->cb_flags = 0;
    3656                 :         106 :         msk->recovery = false;
    3657                 :         106 :         WRITE_ONCE(msk->can_ack, false);
    3658                 :         106 :         WRITE_ONCE(msk->fully_established, false);
    3659                 :         106 :         WRITE_ONCE(msk->rcv_data_fin, false);
    3660                 :         106 :         WRITE_ONCE(msk->snd_data_fin_enable, false);
    3661                 :         106 :         WRITE_ONCE(msk->rcv_fastclose, false);
    3662                 :         106 :         WRITE_ONCE(msk->use_64bit_ack, false);
    3663                 :         106 :         WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
    3664                 :         106 :         mptcp_pm_data_reset(msk);
    3665                 :         106 :         mptcp_ca_reset(sk);
    3666                 :         106 :         msk->bytes_consumed = 0;
    3667                 :         106 :         msk->bytes_acked = 0;
    3668                 :         106 :         msk->bytes_received = 0;
    3669                 :         106 :         msk->bytes_sent = 0;
    3670                 :         106 :         msk->bytes_retrans = 0;
    3671                 :         106 :         msk->rcvspace_init = 0;
    3672                 :         106 :         msk->fastclosing = 0;
    3673                 :         106 :         mptcp_init_rtt_est(msk);
    3674                 :             : 
    3675                 :             :         /* for fallback's sake */
    3676                 :         106 :         WRITE_ONCE(msk->ack_seq, 0);
    3677                 :         106 :         atomic64_set(&msk->rcv_wnd_sent, 0);
    3678                 :             : 
    3679                 :         106 :         WRITE_ONCE(sk->sk_shutdown, 0);
    3680                 :         106 :         sk_error_report(sk);
    3681                 :         106 :         return 0;
    3682                 :             : }
    3683                 :             : 
    3684                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    3685                 :         786 : static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
    3686                 :             : {
    3687         [ -  + ]:         786 :         struct mptcp6_sock *msk6 = container_of(mptcp_sk(sk), struct mptcp6_sock, msk);
    3688                 :             : 
    3689                 :         786 :         return &msk6->np;
    3690                 :             : }
    3691                 :             : 
    3692                 :         786 : static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
    3693                 :             : {
    3694         [ +  - ]:         786 :         const struct ipv6_pinfo *np = inet6_sk(sk);
    3695                 :         786 :         struct ipv6_txoptions *opt;
    3696                 :         786 :         struct ipv6_pinfo *newnp;
    3697                 :             : 
    3698         [ +  - ]:         786 :         newnp = inet6_sk(newsk);
    3699                 :             : 
    3700                 :         786 :         rcu_read_lock();
    3701   [ +  -  -  +  :         786 :         opt = rcu_dereference(np->opt);
          -  -  -  -  -  
                      - ]
    3702         [ +  - ]:         786 :         if (opt) {
    3703                 :           0 :                 opt = ipv6_dup_options(newsk, opt);
    3704         [ #  # ]:           0 :                 if (!opt)
    3705         [ #  # ]:           0 :                         net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
    3706                 :             :         }
    3707                 :         786 :         RCU_INIT_POINTER(newnp->opt, opt);
    3708                 :         786 :         rcu_read_unlock();
    3709                 :         786 : }
    3710                 :             : #endif
    3711                 :             : 
    3712                 :         880 : static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
    3713                 :             : {
    3714                 :         880 :         struct ip_options_rcu *inet_opt, *newopt = NULL;
    3715                 :         880 :         const struct inet_sock *inet = inet_sk(sk);
    3716                 :         880 :         struct inet_sock *newinet;
    3717                 :             : 
    3718                 :         880 :         newinet = inet_sk(newsk);
    3719                 :             : 
    3720                 :         880 :         rcu_read_lock();
    3721   [ +  -  -  +  :         880 :         inet_opt = rcu_dereference(inet->inet_opt);
          -  -  -  -  -  
                      - ]
    3722         [ +  - ]:         880 :         if (inet_opt) {
    3723                 :           0 :                 newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
    3724                 :           0 :                                       inet_opt->opt.optlen, GFP_ATOMIC);
    3725         [ #  # ]:           0 :                 if (!newopt)
    3726         [ #  # ]:           0 :                         net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
    3727                 :             :         }
    3728                 :         880 :         RCU_INIT_POINTER(newinet->inet_opt, newopt);
    3729                 :         880 :         rcu_read_unlock();
    3730                 :         880 : }
    3731                 :             : 
    3732                 :        1666 : struct sock *mptcp_sk_clone_init(const struct sock *sk,
    3733                 :             :                                  const struct mptcp_options_received *mp_opt,
    3734                 :             :                                  struct sock *ssk,
    3735                 :             :                                  struct request_sock *req)
    3736                 :             : {
    3737                 :        1666 :         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
    3738                 :        1666 :         struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
    3739                 :        1666 :         struct mptcp_subflow_context *subflow;
    3740                 :        1666 :         struct mptcp_sock *msk;
    3741                 :             : 
    3742         [ +  - ]:        1666 :         if (!nsk)
    3743                 :             :                 return NULL;
    3744                 :             : 
    3745                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    3746         [ +  + ]:        1666 :         if (nsk->sk_family == AF_INET6)
    3747                 :         786 :                 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
    3748                 :             : #endif
    3749                 :             : 
    3750                 :        1666 :         __mptcp_init_sock(nsk);
    3751                 :             : 
    3752                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    3753         [ +  + ]:        1666 :         if (nsk->sk_family == AF_INET6)
    3754                 :         786 :                 mptcp_copy_ip6_options(nsk, sk);
    3755                 :             :         else
    3756                 :             : #endif
    3757                 :         880 :                 mptcp_copy_ip_options(nsk, sk);
    3758                 :             : 
    3759         [ -  + ]:        1666 :         msk = mptcp_sk(nsk);
    3760                 :        1666 :         WRITE_ONCE(msk->local_key, subflow_req->local_key);
    3761                 :        1666 :         WRITE_ONCE(msk->token, subflow_req->token);
    3762                 :        1666 :         msk->in_accept_queue = 1;
    3763                 :        1666 :         WRITE_ONCE(msk->fully_established, false);
    3764         [ +  + ]:        1666 :         if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
    3765                 :         118 :                 WRITE_ONCE(msk->csum_enabled, true);
    3766                 :             : 
    3767                 :        1666 :         WRITE_ONCE(msk->write_seq, subflow_req->idsn + 1);
    3768                 :        1666 :         WRITE_ONCE(msk->snd_nxt, msk->write_seq);
    3769                 :        1666 :         WRITE_ONCE(msk->snd_una, msk->write_seq);
    3770         [ -  + ]:        1666 :         WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
    3771         [ -  + ]:        1666 :         msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
    3772         [ -  + ]:        1666 :         mptcp_init_sched(msk, mptcp_sk(sk)->sched);
    3773                 :             : 
    3774                 :             :         /* passive msk is created after the first/MPC subflow */
    3775                 :        1666 :         msk->subflow_id = 2;
    3776                 :             : 
    3777                 :        1666 :         sock_reset_flag(nsk, SOCK_RCU_FREE);
    3778                 :        1666 :         security_inet_csk_clone(nsk, req);
    3779                 :             : 
    3780                 :             :         /* this can't race with mptcp_close(), as the msk is
    3781                 :             :          * not yet exposted to user-space
    3782                 :             :          */
    3783                 :        1666 :         mptcp_set_state(nsk, TCP_ESTABLISHED);
    3784                 :             : 
    3785                 :             :         /* The msk maintain a ref to each subflow in the connections list */
    3786                 :        1666 :         WRITE_ONCE(msk->first, ssk);
    3787                 :        1666 :         subflow = mptcp_subflow_ctx(ssk);
    3788                 :        1666 :         list_add(&subflow->node, &msk->conn_list);
    3789                 :        1666 :         sock_hold(ssk);
    3790                 :             : 
    3791                 :             :         /* new mpc subflow takes ownership of the newly
    3792                 :             :          * created mptcp socket
    3793                 :             :          */
    3794                 :        1666 :         mptcp_token_accept(subflow_req, msk);
    3795                 :             : 
    3796                 :             :         /* set msk addresses early to ensure mptcp_pm_get_local_id()
    3797                 :             :          * uses the correct data
    3798                 :             :          */
    3799                 :        1666 :         mptcp_copy_inaddrs(nsk, ssk);
    3800                 :             : 
    3801                 :        1666 :         mptcp_rcv_space_init(msk, ssk);
    3802                 :        1666 :         msk->rcvq_space.time = mptcp_stamp();
    3803                 :             : 
    3804         [ +  + ]:        1666 :         if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
    3805                 :        1610 :                 __mptcp_subflow_fully_established(msk, subflow, mp_opt);
    3806                 :        1666 :         bh_unlock_sock(nsk);
    3807                 :             : 
    3808                 :             :         /* note: the newly allocated socket refcount is 2 now */
    3809                 :        1666 :         return nsk;
    3810                 :             : }
    3811                 :             : 
    3812                 :        5091 : static void mptcp_destroy(struct sock *sk)
    3813                 :             : {
    3814         [ -  + ]:        5091 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3815                 :             : 
    3816                 :             :         /* allow the following to close even the initial subflow */
    3817                 :        5091 :         msk->free_first = 1;
    3818                 :        5091 :         mptcp_destroy_common(msk);
    3819                 :        5091 :         sk_sockets_allocated_dec(sk);
    3820                 :        5091 : }
    3821                 :             : 
    3822                 :      640236 : void __mptcp_data_acked(struct sock *sk)
    3823                 :             : {
    3824         [ +  + ]:      640236 :         if (!sock_owned_by_user(sk))
    3825                 :      298925 :                 __mptcp_clean_una(sk);
    3826                 :             :         else
    3827         [ -  + ]:      341311 :                 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
    3828                 :      640236 : }
    3829                 :             : 
    3830                 :     2042808 : void __mptcp_check_push(struct sock *sk, struct sock *ssk)
    3831                 :             : {
    3832         [ +  + ]:     2042808 :         if (!sock_owned_by_user(sk))
    3833                 :     1426089 :                 __mptcp_subflow_push_pending(sk, ssk, false);
    3834                 :             :         else
    3835         [ -  + ]:      616719 :                 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
    3836                 :     2042808 : }
    3837                 :             : 
    3838                 :             : #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
    3839                 :             :                                       BIT(MPTCP_RETRANSMIT) | \
    3840                 :             :                                       BIT(MPTCP_FLUSH_JOIN_LIST))
    3841                 :             : 
    3842                 :             : /* processes deferred events and flush wmem */
    3843                 :     2654946 : static void mptcp_release_cb(struct sock *sk)
    3844                 :             :         __must_hold(&sk->sk_lock.slock)
    3845                 :             : {
    3846         [ -  + ]:     2654946 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3847                 :     2654946 :         u32 moved = 0;
    3848                 :             : 
    3849                 :     1001359 :         for (;;) {
    3850                 :     3035831 :                 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
    3851                 :     3035831 :                 struct list_head join_list, skbs;
    3852                 :     3035831 :                 bool spool_bl;
    3853                 :             : 
    3854                 :     3035831 :                 spool_bl = mptcp_can_spool_backlog(sk, &skbs);
    3855         [ +  + ]:     3035831 :                 if (!flags && !spool_bl)
    3856                 :             :                         break;
    3857                 :             : 
    3858         [ +  + ]:      380885 :                 INIT_LIST_HEAD(&join_list);
    3859         [ +  + ]:      380885 :                 list_splice_init(&msk->join_list, &join_list);
    3860                 :             : 
    3861                 :             :                 /* the following actions acquire the subflow socket lock
    3862                 :             :                  *
    3863                 :             :                  * 1) can't be invoked in atomic scope
    3864                 :             :                  * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
    3865                 :             :                  *    datapath acquires the msk socket spinlock while helding
    3866                 :             :                  *    the subflow socket lock
    3867                 :             :                  */
    3868                 :      380885 :                 msk->cb_flags &= ~flags;
    3869                 :      380885 :                 spin_unlock_bh(&sk->sk_lock.slock);
    3870                 :             : 
    3871         [ +  + ]:      380885 :                 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
    3872                 :          46 :                         __mptcp_flush_join_list(sk, &join_list);
    3873         [ +  + ]:      380885 :                 if (flags & BIT(MPTCP_PUSH_PENDING))
    3874                 :      309198 :                         __mptcp_push_pending(sk, 0);
    3875         [ +  + ]:      380885 :                 if (flags & BIT(MPTCP_RETRANSMIT))
    3876                 :        6010 :                         __mptcp_retrans(sk);
    3877   [ +  +  +  + ]:      380885 :                 if (spool_bl && __mptcp_move_skbs(sk, &skbs, &moved)) {
    3878                 :             :                         /* notify ack seq update */
    3879                 :      183408 :                         mptcp_cleanup_rbuf(msk, 0);
    3880                 :      183408 :                         sk->sk_data_ready(sk);
    3881                 :             :                 }
    3882                 :             : 
    3883                 :      380885 :                 cond_resched();
    3884                 :      380885 :                 spin_lock_bh(&sk->sk_lock.slock);
    3885                 :             :         }
    3886         [ +  + ]:     2654946 :         if (moved)
    3887                 :      181591 :                 WRITE_ONCE(msk->backlog_len, msk->backlog_len - moved);
    3888                 :             : 
    3889   [ -  +  -  -  :     4779042 :         if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
             -  -  +  + ]
    3890                 :      160697 :                 __mptcp_clean_una_wakeup(sk);
    3891         [ +  + ]:     2654946 :         if (unlikely(msk->cb_flags)) {
    3892                 :             :                 /* be sure to sync the msk state before taking actions
    3893                 :             :                  * depending on sk_state (MPTCP_ERROR_REPORT)
    3894                 :             :                  * On sk release avoid actions depending on the first subflow
    3895                 :             :                  */
    3896   [ -  +  -  -  :        9305 :                 if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
          -  -  +  +  +  
                      - ]
    3897                 :        1193 :                         __mptcp_sync_state(sk, msk->pending_state);
    3898   [ -  +  -  -  :        9305 :                 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
             -  -  +  + ]
    3899                 :         622 :                         __mptcp_error_report(sk);
    3900   [ -  +  -  -  :        9305 :                 if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
             -  -  +  + ]
    3901                 :        2891 :                         __mptcp_sync_sndbuf(sk);
    3902                 :             :         }
    3903                 :     2654946 : }
    3904                 :             : 
    3905                 :             : /* MP_JOIN client subflow must wait for 4th ack before sending any data:
    3906                 :             :  * TCP can't schedule delack timer before the subflow is fully established.
    3907                 :             :  * MPTCP uses the delack timer to do 3rd ack retransmissions
    3908                 :             :  */
    3909                 :         645 : static void schedule_3rdack_retransmission(struct sock *ssk)
    3910                 :             : {
    3911                 :         645 :         struct inet_connection_sock *icsk = inet_csk(ssk);
    3912         [ -  + ]:         645 :         struct tcp_sock *tp = tcp_sk(ssk);
    3913                 :         645 :         unsigned long timeout;
    3914                 :             : 
    3915         [ +  + ]:         645 :         if (READ_ONCE(mptcp_subflow_ctx(ssk)->fully_established))
           [ -  +  +  + ]
    3916                 :             :                 return;
    3917                 :             : 
    3918                 :             :         /* reschedule with a timeout above RTT, as we must look only for drop */
    3919         [ +  - ]:         101 :         if (tp->srtt_us)
    3920         [ -  + ]:         101 :                 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
    3921                 :             :         else
    3922                 :             :                 timeout = TCP_TIMEOUT_INIT;
    3923                 :         101 :         timeout += jiffies;
    3924                 :             : 
    3925         [ -  + ]:         101 :         WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
    3926                 :         101 :         smp_store_release(&icsk->icsk_ack.pending,
    3927                 :             :                           icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
    3928                 :         101 :         sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
    3929                 :             : }
    3930                 :             : 
    3931                 :       31069 : void mptcp_subflow_process_delegated(struct sock *ssk, long status)
    3932                 :             : {
    3933         [ +  + ]:       31069 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    3934                 :       31069 :         struct sock *sk = subflow->conn;
    3935                 :             : 
    3936         [ +  + ]:       31069 :         if (status & BIT(MPTCP_DELEGATE_SEND)) {
    3937                 :        9549 :                 mptcp_data_lock(sk);
    3938         [ +  + ]:        9549 :                 if (!sock_owned_by_user(sk))
    3939                 :        8792 :                         __mptcp_subflow_push_pending(sk, ssk, true);
    3940                 :             :                 else
    3941         [ -  + ]:         757 :                         __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
    3942                 :        9549 :                 mptcp_data_unlock(sk);
    3943                 :             :         }
    3944         [ +  + ]:       31069 :         if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
    3945                 :       19782 :                 mptcp_data_lock(sk);
    3946         [ +  + ]:       19782 :                 if (!sock_owned_by_user(sk))
    3947                 :       15468 :                         __mptcp_sync_sndbuf(sk);
    3948                 :             :                 else
    3949         [ -  + ]:        4314 :                         __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
    3950                 :       19782 :                 mptcp_data_unlock(sk);
    3951                 :             :         }
    3952         [ +  + ]:       31069 :         if (status & BIT(MPTCP_DELEGATE_ACK))
    3953                 :         645 :                 schedule_3rdack_retransmission(ssk);
    3954                 :       31069 : }
    3955                 :             : 
    3956                 :           0 : static int mptcp_hash(struct sock *sk)
    3957                 :             : {
    3958                 :             :         /* should never be called,
    3959                 :             :          * we hash the TCP subflows not the MPTCP socket
    3960                 :             :          */
    3961                 :           0 :         WARN_ON_ONCE(1);
    3962                 :           0 :         return 0;
    3963                 :             : }
    3964                 :             : 
    3965                 :          10 : static void mptcp_unhash(struct sock *sk)
    3966                 :             : {
    3967                 :             :         /* called from sk_common_release(), but nothing to do here */
    3968                 :          10 : }
    3969                 :             : 
    3970                 :           0 : static int mptcp_get_port(struct sock *sk, unsigned short snum)
    3971                 :             : {
    3972         [ #  # ]:           0 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3973                 :             : 
    3974   [ #  #  #  # ]:           0 :         pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
    3975         [ #  # ]:           0 :         if (WARN_ON_ONCE(!msk->first))
    3976                 :           0 :                 return -EINVAL;
    3977                 :             : 
    3978                 :           0 :         return inet_csk_get_port(msk->first, snum);
    3979                 :             : }
    3980                 :             : 
    3981                 :        1508 : void mptcp_finish_connect(struct sock *ssk)
    3982                 :             : {
    3983                 :        1508 :         struct mptcp_subflow_context *subflow;
    3984                 :        1508 :         struct mptcp_sock *msk;
    3985                 :        1508 :         struct sock *sk;
    3986                 :             : 
    3987         [ -  + ]:        1508 :         subflow = mptcp_subflow_ctx(ssk);
    3988                 :        1508 :         sk = subflow->conn;
    3989         [ -  + ]:        1508 :         msk = mptcp_sk(sk);
    3990                 :             : 
    3991   [ -  +  -  - ]:        1508 :         pr_debug("msk=%p, token=%u\n", sk, subflow->token);
    3992                 :             : 
    3993                 :        1508 :         subflow->map_seq = subflow->iasn;
    3994                 :        1508 :         subflow->map_subflow_seq = 1;
    3995                 :             : 
    3996                 :             :         /* the socket is not connected yet, no msk/subflow ops can access/race
    3997                 :             :          * accessing the field below
    3998                 :             :          */
    3999                 :        1508 :         WRITE_ONCE(msk->local_key, subflow->local_key);
    4000                 :        1508 :         WRITE_ONCE(msk->rcvq_space.time, mptcp_stamp());
    4001                 :             : 
    4002                 :        1508 :         mptcp_pm_new_connection(msk, ssk, 0);
    4003                 :        1508 : }
    4004                 :             : 
    4005                 :        6478 : void mptcp_sock_graft(struct sock *sk, struct socket *parent)
    4006                 :             : {
    4007                 :        6478 :         write_lock_bh(&sk->sk_callback_lock);
    4008                 :        6478 :         rcu_assign_pointer(sk->sk_wq, &parent->wq);
    4009         [ +  - ]:        6478 :         sk_set_socket(sk, parent);
    4010                 :        6478 :         write_unlock_bh(&sk->sk_callback_lock);
    4011                 :        6478 : }
    4012                 :             : 
    4013                 :             : /* Can be called without holding the msk socket lock; use the callback lock
    4014                 :             :  * to avoid {READ_,WRITE_}ONCE annotations on sk_socket.
    4015                 :             :  */
    4016                 :         648 : static void mptcp_sock_check_graft(struct sock *sk, struct sock *ssk)
    4017                 :             : {
    4018                 :         648 :         struct socket *sock;
    4019                 :             : 
    4020                 :         648 :         write_lock_bh(&sk->sk_callback_lock);
    4021                 :         648 :         sock = sk->sk_socket;
    4022                 :         648 :         write_unlock_bh(&sk->sk_callback_lock);
    4023         [ +  + ]:         648 :         if (sock) {
    4024                 :         601 :                 mptcp_sock_graft(ssk, sock);
    4025                 :         601 :                 __mptcp_inherit_cgrp_data(sk, ssk);
    4026                 :         601 :                 __mptcp_inherit_memcg(sk, ssk, GFP_ATOMIC);
    4027                 :             :         }
    4028                 :         648 : }
    4029                 :             : 
    4030                 :        1278 : bool mptcp_finish_join(struct sock *ssk)
    4031                 :             : {
    4032         [ -  + ]:        1278 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    4033         [ -  + ]:        1278 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
    4034                 :        1278 :         struct sock *parent = (void *)msk;
    4035                 :        1278 :         bool ret = true;
    4036                 :             : 
    4037   [ -  +  -  - ]:        1278 :         pr_debug("msk=%p, subflow=%p\n", msk, subflow);
    4038                 :             : 
    4039                 :             :         /* mptcp socket already closing? */
    4040         [ -  + ]:        1278 :         if (!mptcp_is_fully_established(parent)) {
    4041         [ #  # ]:           0 :                 MPTCP_INC_STATS(sock_net(parent), MPTCP_MIB_MPJOINNOTESTABLISHED);
    4042                 :           0 :                 subflow->reset_reason = MPTCP_RST_EMPTCP;
    4043                 :           0 :                 return false;
    4044                 :             :         }
    4045                 :             : 
    4046                 :             :         /* Active subflow, already present inside the conn_list; is grafted
    4047                 :             :          * either by __mptcp_subflow_connect() or accept.
    4048                 :             :          */
    4049         [ +  + ]:        1278 :         if (!list_empty(&subflow->node)) {
    4050                 :         630 :                 spin_lock_bh(&msk->fallback_lock);
    4051         [ -  + ]:         630 :                 if (!msk->allow_subflows) {
           [ -  +  -  + ]
    4052                 :           0 :                         spin_unlock_bh(&msk->fallback_lock);
    4053                 :           0 :                         return false;
    4054                 :             :                 }
    4055                 :         630 :                 mptcp_subflow_joined(msk, ssk);
    4056                 :         630 :                 spin_unlock_bh(&msk->fallback_lock);
    4057                 :         630 :                 mptcp_propagate_sndbuf(parent, ssk);
    4058                 :         630 :                 return true;
    4059                 :             :         }
    4060                 :             : 
    4061         [ -  + ]:         648 :         if (!mptcp_pm_allow_new_subflow(msk)) {
    4062         [ #  # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_JOINREJECTED);
    4063                 :           0 :                 goto err_prohibited;
    4064                 :             :         }
    4065                 :             : 
    4066                 :             :         /* If we can't acquire msk socket lock here, let the release callback
    4067                 :             :          * handle it
    4068                 :             :          */
    4069                 :         648 :         mptcp_data_lock(parent);
    4070         [ +  + ]:         648 :         if (!sock_owned_by_user(parent)) {
    4071                 :         602 :                 ret = __mptcp_finish_join(msk, ssk);
    4072         [ +  - ]:         602 :                 if (ret) {
    4073                 :         602 :                         sock_hold(ssk);
    4074                 :         602 :                         list_add_tail(&subflow->node, &msk->conn_list);
    4075                 :         602 :                         mptcp_sock_check_graft(parent, ssk);
    4076                 :             :                 }
    4077                 :             :         } else {
    4078                 :          46 :                 sock_hold(ssk);
    4079         [ -  + ]:          46 :                 list_add_tail(&subflow->node, &msk->join_list);
    4080   [ -  +  -  -  :          46 :                 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
                   -  - ]
    4081                 :             : 
    4082                 :             :                 /* In case of later failures, __mptcp_flush_join_list() will
    4083                 :             :                  * properly orphan the ssk via mptcp_close_ssk().
    4084                 :             :                  */
    4085                 :          46 :                 mptcp_sock_check_graft(parent, ssk);
    4086                 :             :         }
    4087                 :         648 :         mptcp_data_unlock(parent);
    4088                 :             : 
    4089         [ -  + ]:         648 :         if (!ret) {
    4090                 :           0 :                 mptcp_pm_close_subflow(msk);
    4091                 :           0 : err_prohibited:
    4092                 :           0 :                 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
    4093                 :           0 :                 return false;
    4094                 :             :         }
    4095                 :             : 
    4096                 :             :         return true;
    4097                 :             : }
    4098                 :             : 
    4099                 :        1984 : static void mptcp_shutdown(struct sock *sk, int how)
    4100                 :             : {
    4101   [ -  +  -  - ]:        1984 :         pr_debug("sk=%p, how=%d\n", sk, how);
    4102                 :             : 
    4103   [ +  -  +  + ]:        1984 :         if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
    4104                 :        1824 :                 __mptcp_wr_shutdown(sk);
    4105                 :        1984 : }
    4106                 :             : 
    4107                 :          16 : static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
    4108                 :             : {
    4109                 :          16 :         const struct sock *sk = (void *)msk;
    4110                 :          16 :         u64 delta;
    4111                 :             : 
    4112         [ +  - ]:          16 :         if (sk->sk_state == TCP_LISTEN)
    4113                 :             :                 return -EINVAL;
    4114                 :             : 
    4115         [ +  - ]:          16 :         if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
           [ -  +  +  - ]
    4116                 :             :                 return 0;
    4117                 :             : 
    4118                 :          16 :         delta = msk->write_seq - v;
    4119   [ +  +  +  + ]:          16 :         if (__mptcp_check_fallback(msk) && msk->first) {
    4120         [ -  + ]:          16 :                 struct tcp_sock *tp = tcp_sk(msk->first);
    4121                 :             : 
    4122                 :             :                 /* the first subflow is disconnected after close - see
    4123                 :             :                  * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
    4124                 :             :                  * so ignore that status, too.
    4125                 :             :                  */
    4126         [ +  - ]:          16 :                 if (!((1 << msk->first->sk_state) &
           [ -  +  -  + ]
    4127                 :             :                       (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
    4128                 :          16 :                         delta += READ_ONCE(tp->write_seq) - tp->snd_una;
    4129                 :             :         }
    4130                 :          16 :         if (delta > INT_MAX)
    4131                 :             :                 delta = INT_MAX;
    4132                 :             : 
    4133                 :          16 :         return (int)delta;
    4134                 :             : }
    4135                 :             : 
    4136                 :          16 : static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
    4137                 :             : {
    4138         [ -  + ]:          16 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4139                 :          16 :         bool slow;
    4140                 :             : 
    4141   [ -  +  +  - ]:          16 :         switch (cmd) {
    4142                 :           0 :         case SIOCINQ:
    4143         [ #  # ]:           0 :                 if (sk->sk_state == TCP_LISTEN)
    4144                 :             :                         return -EINVAL;
    4145                 :             : 
    4146                 :           0 :                 lock_sock(sk);
    4147         [ #  # ]:           0 :                 if (mptcp_move_skbs(sk))
    4148                 :           0 :                         mptcp_cleanup_rbuf(msk, 0);
    4149                 :           0 :                 *karg = mptcp_inq_hint(sk);
    4150                 :           0 :                 release_sock(sk);
    4151                 :           0 :                 break;
    4152                 :           4 :         case SIOCOUTQ:
    4153                 :           8 :                 slow = lock_sock_fast(sk);
    4154                 :           8 :                 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
    4155                 :           8 :                 unlock_sock_fast(sk, slow);
    4156                 :           8 :                 break;
    4157                 :           4 :         case SIOCOUTQNSD:
    4158                 :           8 :                 slow = lock_sock_fast(sk);
    4159                 :           8 :                 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
    4160                 :           8 :                 unlock_sock_fast(sk, slow);
    4161                 :           8 :                 break;
    4162                 :             :         default:
    4163                 :             :                 return -ENOIOCTLCMD;
    4164                 :             :         }
    4165                 :             : 
    4166                 :             :         return 0;
    4167                 :             : }
    4168                 :             : 
    4169                 :        1756 : static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
    4170                 :             :                          int addr_len)
    4171                 :             : {
    4172                 :        1756 :         struct mptcp_subflow_context *subflow;
    4173         [ -  + ]:        1756 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4174                 :        1756 :         int err = -EINVAL;
    4175                 :        1756 :         struct sock *ssk;
    4176                 :             : 
    4177                 :        1756 :         ssk = __mptcp_nmpc_sk(msk);
    4178         [ -  + ]:        1756 :         if (IS_ERR(ssk))
    4179                 :           0 :                 return PTR_ERR(ssk);
    4180                 :             : 
    4181                 :        1756 :         mptcp_set_state(sk, TCP_SYN_SENT);
    4182         [ +  - ]:        1756 :         subflow = mptcp_subflow_ctx(ssk);
    4183                 :             : #ifdef CONFIG_TCP_MD5SIG
    4184                 :             :         /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
    4185                 :             :          * TCP option space.
    4186                 :             :          */
    4187                 :             :         if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
    4188                 :             :                 mptcp_early_fallback(msk, subflow, MPTCP_MIB_MD5SIGFALLBACK);
    4189                 :             : #endif
    4190         [ +  - ]:        1756 :         if (subflow->request_mptcp) {
    4191         [ +  + ]:        1756 :                 if (mptcp_active_should_disable(sk))
    4192                 :           6 :                         mptcp_early_fallback(msk, subflow,
    4193                 :             :                                              MPTCP_MIB_MPCAPABLEACTIVEDISABLED);
    4194         [ -  + ]:        1750 :                 else if (mptcp_token_new_connect(ssk) < 0)
    4195                 :           0 :                         mptcp_early_fallback(msk, subflow,
    4196                 :             :                                              MPTCP_MIB_TOKENFALLBACKINIT);
    4197                 :             :         }
    4198                 :             : 
    4199                 :        1756 :         WRITE_ONCE(msk->write_seq, subflow->idsn);
    4200                 :        1756 :         WRITE_ONCE(msk->snd_nxt, subflow->idsn);
    4201                 :        1756 :         WRITE_ONCE(msk->snd_una, subflow->idsn);
    4202         [ +  + ]:        1756 :         if (likely(!__mptcp_check_fallback(msk)))
    4203         [ +  - ]:        1750 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
    4204                 :             : 
    4205                 :             :         /* if reaching here via the fastopen/sendmsg path, the caller already
    4206                 :             :          * acquired the subflow socket lock, too.
    4207                 :             :          */
    4208         [ +  + ]:        1756 :         if (!msk->fastopening)
    4209                 :        1686 :                 lock_sock(ssk);
    4210                 :             : 
    4211                 :             :         /* the following mirrors closely a very small chunk of code from
    4212                 :             :          * __inet_stream_connect()
    4213                 :             :          */
    4214         [ -  + ]:        1756 :         if (ssk->sk_state != TCP_CLOSE)
    4215                 :           0 :                 goto out;
    4216                 :             : 
    4217   [ +  -  -  +  :        1756 :         if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
                   -  - ]
    4218                 :           0 :                 err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
    4219         [ -  - ]:           0 :                 if (err)
    4220                 :           0 :                         goto out;
    4221                 :             :         }
    4222                 :             : 
    4223                 :        1756 :         err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
    4224         [ -  + ]:        1756 :         if (err < 0)
    4225                 :           0 :                 goto out;
    4226                 :             : 
    4227         [ +  + ]:        1756 :         inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
    4228                 :             : 
    4229                 :        1756 : out:
    4230         [ +  + ]:        1756 :         if (!msk->fastopening)
    4231                 :        1686 :                 release_sock(ssk);
    4232                 :             : 
    4233                 :             :         /* on successful connect, the msk state will be moved to established by
    4234                 :             :          * subflow_finish_connect()
    4235                 :             :          */
    4236         [ -  + ]:        1756 :         if (unlikely(err)) {
    4237                 :             :                 /* avoid leaving a dangling token in an unconnected socket */
    4238                 :           0 :                 mptcp_token_destroy(msk);
    4239                 :           0 :                 mptcp_set_state(sk, TCP_CLOSE);
    4240                 :           0 :                 return err;
    4241                 :             :         }
    4242                 :             : 
    4243                 :        1756 :         mptcp_copy_inaddrs(sk, ssk);
    4244                 :        1756 :         return 0;
    4245                 :             : }
    4246                 :             : 
    4247                 :             : static struct proto mptcp_prot = {
    4248                 :             :         .name           = "MPTCP",
    4249                 :             :         .owner          = THIS_MODULE,
    4250                 :             :         .init           = mptcp_init_sock,
    4251                 :             :         .connect        = mptcp_connect,
    4252                 :             :         .disconnect     = mptcp_disconnect,
    4253                 :             :         .close          = mptcp_close,
    4254                 :             :         .setsockopt     = mptcp_setsockopt,
    4255                 :             :         .getsockopt     = mptcp_getsockopt,
    4256                 :             :         .shutdown       = mptcp_shutdown,
    4257                 :             :         .destroy        = mptcp_destroy,
    4258                 :             :         .sendmsg        = mptcp_sendmsg,
    4259                 :             :         .ioctl          = mptcp_ioctl,
    4260                 :             :         .recvmsg        = mptcp_recvmsg,
    4261                 :             :         .release_cb     = mptcp_release_cb,
    4262                 :             :         .hash           = mptcp_hash,
    4263                 :             :         .unhash         = mptcp_unhash,
    4264                 :             :         .get_port       = mptcp_get_port,
    4265                 :             :         .stream_memory_free     = mptcp_stream_memory_free,
    4266                 :             :         .sockets_allocated      = &mptcp_sockets_allocated,
    4267                 :             : 
    4268                 :             :         .memory_allocated       = &net_aligned_data.tcp_memory_allocated,
    4269                 :             :         .per_cpu_fw_alloc       = &tcp_memory_per_cpu_fw_alloc,
    4270                 :             : 
    4271                 :             :         .memory_pressure        = &tcp_memory_pressure,
    4272                 :             :         .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_wmem),
    4273                 :             :         .sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_rmem),
    4274                 :             :         .sysctl_mem     = sysctl_tcp_mem,
    4275                 :             :         .obj_size       = sizeof(struct mptcp_sock),
    4276                 :             :         .slab_flags     = SLAB_TYPESAFE_BY_RCU,
    4277                 :             :         .no_autobind    = true,
    4278                 :             : };
    4279                 :             : 
    4280                 :        1788 : static int mptcp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
    4281                 :             : {
    4282         [ -  + ]:        1788 :         struct mptcp_sock *msk = mptcp_sk(sock->sk);
    4283                 :        1788 :         struct sock *ssk, *sk = sock->sk;
    4284                 :        1788 :         int err = -EINVAL;
    4285                 :             : 
    4286                 :        1788 :         lock_sock(sk);
    4287                 :        1788 :         ssk = __mptcp_nmpc_sk(msk);
    4288         [ -  + ]:        1788 :         if (IS_ERR(ssk)) {
    4289                 :           0 :                 err = PTR_ERR(ssk);
    4290                 :           0 :                 goto unlock;
    4291                 :             :         }
    4292                 :             : 
    4293         [ +  + ]:        1788 :         if (sk->sk_family == AF_INET)
    4294                 :         938 :                 err = inet_bind_sk(ssk, uaddr, addr_len);
    4295                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    4296         [ +  - ]:         850 :         else if (sk->sk_family == AF_INET6)
    4297                 :         850 :                 err = inet6_bind_sk(ssk, uaddr, addr_len);
    4298                 :             : #endif
    4299         [ +  + ]:        1788 :         if (!err)
    4300                 :        1785 :                 mptcp_copy_inaddrs(sk, ssk);
    4301                 :             : 
    4302                 :           3 : unlock:
    4303                 :        1788 :         release_sock(sk);
    4304                 :        1788 :         return err;
    4305                 :             : }
    4306                 :             : 
    4307                 :        1785 : static int mptcp_listen(struct socket *sock, int backlog)
    4308                 :             : {
    4309         [ -  + ]:        1785 :         struct mptcp_sock *msk = mptcp_sk(sock->sk);
    4310                 :        1785 :         struct sock *sk = sock->sk;
    4311                 :        1785 :         struct sock *ssk;
    4312                 :        1785 :         int err;
    4313                 :             : 
    4314   [ -  +  -  - ]:        1785 :         pr_debug("msk=%p\n", msk);
    4315                 :             : 
    4316                 :        1785 :         lock_sock(sk);
    4317                 :             : 
    4318                 :        1785 :         err = -EINVAL;
    4319   [ +  -  -  + ]:        1785 :         if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
    4320                 :           0 :                 goto unlock;
    4321                 :             : 
    4322                 :        1785 :         ssk = __mptcp_nmpc_sk(msk);
    4323         [ -  + ]:        1785 :         if (IS_ERR(ssk)) {
    4324                 :           0 :                 err = PTR_ERR(ssk);
    4325                 :           0 :                 goto unlock;
    4326                 :             :         }
    4327                 :             : 
    4328                 :        1785 :         mptcp_set_state(sk, TCP_LISTEN);
    4329                 :        1785 :         sock_set_flag(sk, SOCK_RCU_FREE);
    4330                 :             : 
    4331                 :        1785 :         lock_sock(ssk);
    4332                 :        1785 :         err = __inet_listen_sk(ssk, backlog);
    4333                 :        1785 :         release_sock(ssk);
    4334                 :        1785 :         mptcp_set_state(sk, inet_sk_state_load(ssk));
    4335                 :             : 
    4336         [ -  + ]:        1785 :         if (!err) {
    4337                 :        1785 :                 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
    4338                 :        1785 :                 mptcp_copy_inaddrs(sk, ssk);
    4339                 :        1785 :                 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
    4340                 :             :         }
    4341                 :             : 
    4342                 :           0 : unlock:
    4343                 :        1785 :         release_sock(sk);
    4344                 :        1785 :         return err;
    4345                 :             : }
    4346                 :             : 
    4347                 :        1630 : static void mptcp_graft_subflows(struct sock *sk)
    4348                 :             : {
    4349                 :        1630 :         struct mptcp_subflow_context *subflow;
    4350         [ -  + ]:        1630 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4351                 :             : 
    4352         [ +  + ]:        1630 :         if (mem_cgroup_sockets_enabled) {
    4353                 :          42 :                 LIST_HEAD(join_list);
    4354                 :             : 
    4355                 :             :                 /* Subflows joining after __inet_accept() will get the
    4356                 :             :                  * mem CG properly initialized at mptcp_finish_join() time,
    4357                 :             :                  * but subflows pending in join_list need explicit
    4358                 :             :                  * initialization before flushing `backlog_unaccounted`
    4359                 :             :                  * or MPTCP can later unexpectedly observe unaccounted memory.
    4360                 :             :                  */
    4361                 :          42 :                 mptcp_data_lock(sk);
    4362         [ -  + ]:          42 :                 list_splice_init(&msk->join_list, &join_list);
    4363                 :          42 :                 mptcp_data_unlock(sk);
    4364                 :             : 
    4365                 :          42 :                 __mptcp_flush_join_list(sk, &join_list);
    4366                 :             :         }
    4367                 :             : 
    4368         [ +  + ]:        3301 :         mptcp_for_each_subflow(msk, subflow) {
    4369                 :        1671 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    4370                 :             : 
    4371                 :        1671 :                 lock_sock(ssk);
    4372                 :             : 
    4373                 :             :                 /* Set ssk->sk_socket of accept()ed flows to mptcp socket.
    4374                 :             :                  * This is needed so NOSPACE flag can be set from tcp stack.
    4375                 :             :                  */
    4376         [ +  - ]:        1671 :                 if (!ssk->sk_socket)
    4377                 :        1671 :                         mptcp_sock_graft(ssk, sk->sk_socket);
    4378                 :             : 
    4379         [ +  + ]:        1671 :                 if (!mem_cgroup_sk_enabled(sk))
    4380                 :        1629 :                         goto unlock;
    4381                 :             : 
    4382                 :          42 :                 __mptcp_inherit_cgrp_data(sk, ssk);
    4383                 :          42 :                 __mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);
    4384                 :             : 
    4385                 :        1671 : unlock:
    4386                 :        1671 :                 release_sock(ssk);
    4387                 :             :         }
    4388                 :             : 
    4389         [ +  + ]:        1630 :         if (mem_cgroup_sk_enabled(sk)) {
    4390                 :          42 :                 gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL;
    4391                 :          42 :                 int amt;
    4392                 :             : 
    4393                 :             :                 /* Account the backlog memory; prior accept() is aware of
    4394                 :             :                  * fwd and rmem only.
    4395                 :             :                  */
    4396                 :          42 :                 mptcp_data_lock(sk);
    4397                 :          84 :                 amt = sk_mem_pages(sk->sk_forward_alloc +
    4398                 :          63 :                                    msk->backlog_unaccounted +
    4399                 :          42 :                                    atomic_read(&sk->sk_rmem_alloc)) -
    4400                 :          63 :                       sk_mem_pages(sk->sk_forward_alloc +
    4401                 :          42 :                                    atomic_read(&sk->sk_rmem_alloc));
    4402                 :          42 :                 msk->backlog_unaccounted = 0;
    4403                 :          42 :                 mptcp_data_unlock(sk);
    4404                 :             : 
    4405         [ -  + ]:          42 :                 if (amt)
    4406                 :           0 :                         mem_cgroup_sk_charge(sk, amt, gfp);
    4407                 :             :         }
    4408                 :        1630 : }
    4409                 :             : 
    4410                 :        1814 : static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
    4411                 :             :                                struct proto_accept_arg *arg)
    4412                 :             : {
    4413         [ -  + ]:        1814 :         struct mptcp_sock *msk = mptcp_sk(sock->sk);
    4414                 :        1814 :         struct sock *ssk, *newsk;
    4415                 :             : 
    4416   [ -  +  -  - ]:        1814 :         pr_debug("msk=%p\n", msk);
    4417                 :             : 
    4418                 :             :         /* Buggy applications can call accept on socket states other then LISTEN
    4419                 :             :          * but no need to allocate the first subflow just to error out.
    4420                 :             :          */
    4421                 :        1814 :         ssk = READ_ONCE(msk->first);
    4422         [ +  - ]:        1814 :         if (!ssk)
    4423                 :             :                 return -EINVAL;
    4424                 :             : 
    4425   [ -  +  -  - ]:        1814 :         pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
    4426                 :        1814 :         newsk = inet_csk_accept(ssk, arg);
    4427         [ +  + ]:        1814 :         if (!newsk)
    4428                 :           6 :                 return arg->err;
    4429                 :             : 
    4430   [ -  +  -  - ]:        1808 :         pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
           [ -  +  -  -  
                   -  - ]
    4431         [ +  + ]:        1808 :         if (sk_is_mptcp(newsk)) {
           [ -  +  +  + ]
    4432                 :        1630 :                 struct mptcp_subflow_context *subflow;
    4433                 :        1630 :                 struct sock *new_mptcp_sock;
    4434                 :             : 
    4435         [ -  + ]:        1630 :                 subflow = mptcp_subflow_ctx(newsk);
    4436                 :        1630 :                 new_mptcp_sock = subflow->conn;
    4437                 :             : 
    4438                 :             :                 /* is_mptcp should be false if subflow->conn is missing, see
    4439                 :             :                  * subflow_syn_recv_sock()
    4440                 :             :                  */
    4441         [ -  + ]:        1630 :                 if (WARN_ON_ONCE(!new_mptcp_sock)) {
    4442         [ #  # ]:           0 :                         tcp_sk(newsk)->is_mptcp = 0;
    4443                 :           0 :                         goto tcpfallback;
    4444                 :             :                 }
    4445                 :             : 
    4446                 :        1630 :                 newsk = new_mptcp_sock;
    4447         [ +  - ]:        1630 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
    4448                 :             : 
    4449         [ -  + ]:        1630 :                 newsk->sk_kern_sock = arg->kern;
    4450                 :        1630 :                 lock_sock(newsk);
    4451                 :        1630 :                 __inet_accept(sock, newsock, newsk);
    4452                 :             : 
    4453                 :        1630 :                 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
    4454         [ -  + ]:        1630 :                 msk = mptcp_sk(newsk);
    4455                 :        1630 :                 msk->in_accept_queue = 0;
    4456                 :             : 
    4457                 :        1630 :                 mptcp_graft_subflows(newsk);
    4458                 :        1630 :                 mptcp_rps_record_subflows(msk);
    4459         [ +  + ]:        1630 :                 __mptcp_propagate_sndbuf(newsk, mptcp_subflow_tcp_sock(subflow));
    4460                 :             : 
    4461                 :             :                 /* Do late cleanup for the first subflow as necessary. Also
    4462                 :             :                  * deal with bad peers not doing a complete shutdown.
    4463                 :             :                  */
    4464         [ +  + ]:        1630 :                 if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
    4465         [ +  - ]:           6 :                         if (unlikely(list_is_singular(&msk->conn_list)))
    4466                 :           6 :                                 mptcp_set_state(newsk, TCP_CLOSE);
    4467                 :           6 :                         mptcp_close_ssk(newsk, msk->first,
    4468                 :           6 :                                         mptcp_subflow_ctx(msk->first));
    4469                 :             :                 }
    4470                 :             :         } else {
    4471                 :         178 : tcpfallback:
    4472         [ -  + ]:         178 :                 newsk->sk_kern_sock = arg->kern;
    4473                 :         178 :                 lock_sock(newsk);
    4474                 :         178 :                 __inet_accept(sock, newsock, newsk);
    4475                 :             :                 /* we are being invoked after accepting a non-mp-capable
    4476                 :             :                  * flow: sk is a tcp_sk, not an mptcp one.
    4477                 :             :                  *
    4478                 :             :                  * Hand the socket over to tcp so all further socket ops
    4479                 :             :                  * bypass mptcp.
    4480                 :             :                  */
    4481                 :         178 :                 WRITE_ONCE(newsock->sk->sk_socket->ops,
    4482                 :             :                            mptcp_fallback_tcp_ops(newsock->sk));
    4483                 :             :         }
    4484                 :        1808 :         release_sock(newsk);
    4485                 :             : 
    4486                 :        1808 :         return 0;
    4487                 :             : }
    4488                 :             : 
    4489                 :      677775 : static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
    4490                 :             : {
    4491                 :      677775 :         struct sock *sk = (struct sock *)msk;
    4492                 :             : 
    4493         [ +  + ]:      677775 :         if (__mptcp_stream_is_writeable(sk, 1))
    4494                 :             :                 return EPOLLOUT | EPOLLWRNORM;
    4495                 :             : 
    4496                 :      274237 :         set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
    4497                 :      274237 :         smp_mb__after_atomic(); /* NOSPACE is changed by mptcp_write_space() */
    4498         [ +  + ]:      274237 :         if (__mptcp_stream_is_writeable(sk, 1))
    4499                 :             :                 return EPOLLOUT | EPOLLWRNORM;
    4500                 :             : 
    4501                 :             :         return 0;
    4502                 :             : }
    4503                 :             : 
    4504                 :     1257083 : static __poll_t mptcp_poll(struct file *file, struct socket *sock,
    4505                 :             :                            struct poll_table_struct *wait)
    4506                 :             : {
    4507                 :     1257083 :         struct sock *sk = sock->sk;
    4508                 :     1257083 :         struct mptcp_sock *msk;
    4509                 :     1257083 :         __poll_t mask = 0;
    4510                 :     1257083 :         u8 shutdown;
    4511                 :     1257083 :         int state;
    4512                 :             : 
    4513         [ -  + ]:     1257083 :         msk = mptcp_sk(sk);
    4514                 :     1257083 :         sock_poll_wait(file, sock, wait);
    4515                 :             : 
    4516                 :     1257083 :         state = inet_sk_state_load(sk);
    4517   [ -  +  -  - ]:     1257083 :         pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
    4518         [ +  + ]:     1257083 :         if (state == TCP_LISTEN) {
    4519                 :        2768 :                 struct sock *ssk = READ_ONCE(msk->first);
    4520                 :             : 
    4521         [ -  + ]:        2768 :                 if (WARN_ON_ONCE(!ssk))
    4522                 :           0 :                         return 0;
    4523                 :             : 
    4524         [ +  + ]:        4160 :                 return inet_csk_listen_poll(ssk);
    4525                 :             :         }
    4526                 :             : 
    4527                 :     1254315 :         shutdown = READ_ONCE(sk->sk_shutdown);
    4528         [ +  + ]:     1254315 :         if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
    4529                 :        3381 :                 mask |= EPOLLHUP;
    4530         [ +  + ]:     1254315 :         if (shutdown & RCV_SHUTDOWN)
    4531                 :      208804 :                 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
    4532                 :             : 
    4533         [ +  - ]:     1254315 :         if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
    4534                 :     1254315 :                 mask |= mptcp_check_readable(sk);
    4535         [ +  + ]:     1254315 :                 if (shutdown & SEND_SHUTDOWN)
    4536                 :      576540 :                         mask |= EPOLLOUT | EPOLLWRNORM;
    4537                 :             :                 else
    4538                 :      677775 :                         mask |= mptcp_check_writeable(msk);
    4539   [ #  #  #  # ]:           0 :         } else if (state == TCP_SYN_SENT &&
                 [ #  # ]
    4540         [ #  # ]:           0 :                    inet_test_bit(DEFER_CONNECT, sk)) {
    4541                 :             :                 /* cf tcp_poll() note about TFO */
    4542                 :           0 :                 mask |= EPOLLOUT | EPOLLWRNORM;
    4543                 :             :         }
    4544                 :             : 
    4545                 :             :         /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
    4546                 :     1254315 :         smp_rmb();
    4547   [ +  +  -  + ]:     1254315 :         if (READ_ONCE(sk->sk_err) ||
    4548         [ -  + ]:     1254306 :             !skb_queue_empty_lockless(&sk->sk_error_queue))
    4549                 :           9 :                 mask |= EPOLLERR;
    4550                 :             : 
    4551                 :             :         return mask;
    4552                 :             : }
    4553                 :             : 
    4554                 :      826889 : static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)
    4555                 :             : {
    4556         [ -  + ]:      826889 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4557                 :      826889 :         struct sk_buff *skb;
    4558                 :      826889 :         u32 offset;
    4559                 :             : 
    4560         [ +  + ]:      826889 :         if (!list_empty(&msk->backlog_list))
    4561                 :         505 :                 mptcp_move_skbs(sk);
    4562                 :             : 
    4563   [ +  +  +  - ]:      826889 :         while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
    4564                 :      530541 :                 offset = MPTCP_SKB_CB(skb)->offset;
    4565         [ +  - ]:      530541 :                 if (offset < skb->len) {
    4566                 :      530541 :                         *off = offset;
    4567                 :      530541 :                         return skb;
    4568                 :             :                 }
    4569                 :           0 :                 mptcp_eat_recv_skb(sk, skb);
    4570                 :             :         }
    4571                 :             :         return NULL;
    4572                 :             : }
    4573                 :             : 
    4574                 :             : /*
    4575                 :             :  * Note:
    4576                 :             :  *      - It is assumed that the socket was locked by the caller.
    4577                 :             :  */
    4578                 :      431524 : static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
    4579                 :             :                              sk_read_actor_t recv_actor, bool noack)
    4580                 :             : {
    4581         [ -  + ]:      431524 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4582                 :      431524 :         struct sk_buff *skb;
    4583                 :      431524 :         int copied = 0;
    4584                 :      431524 :         u32 offset;
    4585                 :             : 
    4586                 :      431524 :         msk_owned_by_me(msk);
    4587                 :             : 
    4588         [ +  - ]:      431524 :         if (sk->sk_state == TCP_LISTEN)
    4589                 :             :                 return -ENOTCONN;
    4590         [ +  + ]:      528254 :         while ((skb = mptcp_recv_skb(sk, &offset)) != NULL) {
    4591                 :      314522 :                 u32 data_len = skb->len - offset;
    4592                 :      314522 :                 int count;
    4593                 :      314522 :                 u32 size;
    4594                 :             : 
    4595                 :      314522 :                 size = min_t(size_t, data_len, INT_MAX);
    4596                 :      314522 :                 count = recv_actor(desc, skb, offset, size);
    4597         [ +  + ]:      314522 :                 if (count <= 0) {
    4598         [ +  + ]:         779 :                         if (!copied)
    4599                 :         752 :                                 copied = count;
    4600                 :             :                         break;
    4601                 :             :                 }
    4602                 :             : 
    4603                 :      313743 :                 copied += count;
    4604                 :             : 
    4605                 :      313743 :                 msk->bytes_consumed += count;
    4606         [ +  + ]:      313743 :                 if (count < data_len) {
    4607                 :      215836 :                         MPTCP_SKB_CB(skb)->offset += count;
    4608                 :      215836 :                         MPTCP_SKB_CB(skb)->map_seq += count;
    4609                 :      215836 :                         break;
    4610                 :             :                 }
    4611                 :             : 
    4612                 :       97907 :                 mptcp_eat_recv_skb(sk, skb);
    4613         [ +  + ]:       97907 :                 if (!desc->count)
    4614                 :             :                         break;
    4615                 :             :         }
    4616                 :             : 
    4617         [ -  + ]:      431524 :         if (noack)
    4618                 :           0 :                 goto out;
    4619                 :             : 
    4620                 :      431524 :         mptcp_rcv_space_adjust(msk, copied);
    4621                 :             : 
    4622         [ +  + ]:      431524 :         if (copied > 0) {
    4623                 :      298635 :                 mptcp_recv_skb(sk, &offset);
    4624                 :      298635 :                 mptcp_cleanup_rbuf(msk, copied);
    4625                 :             :         }
    4626                 :      132889 : out:
    4627                 :             :         return copied;
    4628                 :             : }
    4629                 :             : 
    4630                 :           0 : static int mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
    4631                 :             :                            sk_read_actor_t recv_actor)
    4632                 :             : {
    4633                 :           0 :         return __mptcp_read_sock(sk, desc, recv_actor, false);
    4634                 :             : }
    4635                 :             : 
    4636                 :      431524 : static int __mptcp_splice_read(struct sock *sk, struct tcp_splice_state *tss)
    4637                 :             : {
    4638                 :             :         /* Store TCP splice context information in read_descriptor_t. */
    4639                 :      431524 :         read_descriptor_t rd_desc = {
    4640                 :             :                 .arg.data = tss,
    4641                 :      431524 :                 .count    = tss->len,
    4642                 :             :         };
    4643                 :             : 
    4644                 :      431524 :         return mptcp_read_sock(sk, &rd_desc, tcp_splice_data_recv);
    4645                 :             : }
    4646                 :             : 
    4647                 :             : /**
    4648                 :             :  *  mptcp_splice_read - splice data from MPTCP socket to a pipe
    4649                 :             :  * @sock:       socket to splice from
    4650                 :             :  * @ppos:       position (not valid)
    4651                 :             :  * @pipe:       pipe to splice to
    4652                 :             :  * @len:        number of bytes to splice
    4653                 :             :  * @flags:      splice modifier flags
    4654                 :             :  *
    4655                 :             :  * Description:
    4656                 :             :  *    Will read pages from given socket and fill them into a pipe.
    4657                 :             :  *
    4658                 :             :  * Return:
    4659                 :             :  *    Amount of bytes that have been spliced.
    4660                 :             :  *
    4661                 :             :  **/
    4662                 :      292973 : static ssize_t mptcp_splice_read(struct socket *sock, loff_t *ppos,
    4663                 :             :                                  struct pipe_inode_info *pipe, size_t len,
    4664                 :             :                                  unsigned int flags)
    4665                 :             : {
    4666                 :      292973 :         struct tcp_splice_state tss = {
    4667                 :             :                 .pipe   = pipe,
    4668                 :             :                 .len    = len,
    4669                 :             :                 .flags  = flags,
    4670                 :             :         };
    4671                 :      292973 :         struct sock *sk = sock->sk;
    4672                 :      292973 :         ssize_t spliced = 0;
    4673                 :      292973 :         int ret = 0;
    4674                 :      292973 :         long timeo;
    4675                 :             : 
    4676                 :             :         /*
    4677                 :             :          * We can't seek on a socket input
    4678                 :             :          */
    4679         [ +  - ]:      292973 :         if (unlikely(*ppos))
    4680                 :             :                 return -ESPIPE;
    4681                 :             : 
    4682                 :      292973 :         lock_sock(sk);
    4683                 :             : 
    4684         [ -  + ]:      292973 :         mptcp_rps_record_subflows(mptcp_sk(sk));
    4685                 :             : 
    4686         [ +  - ]:      292973 :         timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK);
    4687         [ +  - ]:      431524 :         while (tss.len) {
    4688                 :      431524 :                 ret = __mptcp_splice_read(sk, &tss);
    4689         [ +  + ]:      431524 :                 if (ret < 0) {
    4690                 :             :                         break;
    4691         [ +  + ]:      430772 :                 } else if (!ret) {
    4692         [ +  + ]:      132137 :                         if (spliced)
    4693                 :             :                                 break;
    4694         [ +  - ]:       56555 :                         if (sock_flag(sk, SOCK_DONE))
    4695                 :             :                                 break;
    4696         [ -  + ]:       56555 :                         if (sk->sk_err) {
    4697                 :           0 :                                 ret = sock_error(sk);
    4698                 :           0 :                                 break;
    4699                 :             :                         }
    4700         [ +  + ]:       56555 :                         if (sk->sk_shutdown & RCV_SHUTDOWN)
    4701                 :             :                                 break;
    4702         [ +  - ]:       56315 :                         if (sk->sk_state == TCP_CLOSE) {
    4703                 :             :                                 /*
    4704                 :             :                                  * This occurs when user tries to read
    4705                 :             :                                  * from never connected socket.
    4706                 :             :                                  */
    4707                 :             :                                 ret = -ENOTCONN;
    4708                 :             :                                 break;
    4709                 :             :                         }
    4710         [ +  - ]:       56315 :                         if (!timeo) {
    4711                 :             :                                 ret = -EAGAIN;
    4712                 :             :                                 break;
    4713                 :             :                         }
    4714                 :             :                         /* if __mptcp_splice_read() got nothing while we have
    4715                 :             :                          * an skb in receive queue, we do not want to loop.
    4716                 :             :                          * This might happen with URG data.
    4717                 :             :                          */
    4718         [ +  - ]:       56315 :                         if (!skb_queue_empty(&sk->sk_receive_queue))
    4719                 :             :                                 break;
    4720                 :       56315 :                         ret = sk_wait_data(sk, &timeo, NULL);
    4721         [ +  - ]:       56315 :                         if (ret < 0)
    4722                 :             :                                 break;
    4723         [ -  + ]:       56315 :                         if (signal_pending(current)) {
    4724         [ #  # ]:           0 :                                 ret = sock_intr_errno(timeo);
    4725                 :             :                                 break;
    4726                 :             :                         }
    4727                 :       56315 :                         continue;
    4728                 :             :                 }
    4729                 :      298635 :                 tss.len -= ret;
    4730                 :      298635 :                 spliced += ret;
    4731                 :             : 
    4732   [ +  +  -  + ]:      298635 :                 if (!tss.len || !timeo)
    4733                 :             :                         break;
    4734                 :       82391 :                 release_sock(sk);
    4735                 :       82391 :                 lock_sock(sk);
    4736                 :             : 
    4737         [ +  + ]:       82391 :                 if (tcp_recv_should_stop(sk))
    4738                 :             :                         break;
    4739                 :             :         }
    4740                 :             : 
    4741                 :      292973 :         release_sock(sk);
    4742                 :             : 
    4743         [ +  + ]:      292973 :         if (spliced)
    4744                 :             :                 return spliced;
    4745                 :             : 
    4746                 :         240 :         return ret;
    4747                 :             : }
    4748                 :             : 
    4749                 :             : static const struct proto_ops mptcp_stream_ops = {
    4750                 :             :         .family            = PF_INET,
    4751                 :             :         .owner             = THIS_MODULE,
    4752                 :             :         .release           = inet_release,
    4753                 :             :         .bind              = mptcp_bind,
    4754                 :             :         .connect           = inet_stream_connect,
    4755                 :             :         .socketpair        = sock_no_socketpair,
    4756                 :             :         .accept            = mptcp_stream_accept,
    4757                 :             :         .getname           = inet_getname,
    4758                 :             :         .poll              = mptcp_poll,
    4759                 :             :         .ioctl             = inet_ioctl,
    4760                 :             :         .gettstamp         = sock_gettstamp,
    4761                 :             :         .listen            = mptcp_listen,
    4762                 :             :         .shutdown          = inet_shutdown,
    4763                 :             :         .setsockopt        = sock_common_setsockopt,
    4764                 :             :         .getsockopt        = sock_common_getsockopt,
    4765                 :             :         .sendmsg           = inet_sendmsg,
    4766                 :             :         .recvmsg           = inet_recvmsg,
    4767                 :             :         .mmap              = sock_no_mmap,
    4768                 :             :         .set_rcvlowat      = mptcp_set_rcvlowat,
    4769                 :             :         .read_sock         = mptcp_read_sock,
    4770                 :             :         .splice_read       = mptcp_splice_read,
    4771                 :             : };
    4772                 :             : 
    4773                 :             : static struct inet_protosw mptcp_protosw = {
    4774                 :             :         .type           = SOCK_STREAM,
    4775                 :             :         .protocol       = IPPROTO_MPTCP,
    4776                 :             :         .prot           = &mptcp_prot,
    4777                 :             :         .ops            = &mptcp_stream_ops,
    4778                 :             :         .flags          = INET_PROTOSW_ICSK,
    4779                 :             : };
    4780                 :             : 
    4781                 :       66472 : static int mptcp_napi_poll(struct napi_struct *napi, int budget)
    4782                 :             : {
    4783                 :       66472 :         struct mptcp_delegated_action *delegated;
    4784                 :       66472 :         struct mptcp_subflow_context *subflow;
    4785                 :       66472 :         int work_done = 0;
    4786                 :             : 
    4787                 :       66472 :         delegated = container_of(napi, struct mptcp_delegated_action, napi);
    4788         [ +  + ]:      134679 :         while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
    4789                 :       68207 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    4790                 :             : 
    4791                 :       68207 :                 bh_lock_sock_nested(ssk);
    4792         [ +  + ]:       68207 :                 if (!sock_owned_by_user(ssk)) {
    4793                 :       25317 :                         mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
    4794                 :             :                 } else {
    4795                 :             :                         /* tcp_release_cb_override already processed
    4796                 :             :                          * the action or will do at next release_sock().
    4797                 :             :                          * In both case must dequeue the subflow here - on the same
    4798                 :             :                          * CPU that scheduled it.
    4799                 :             :                          */
    4800                 :       42890 :                         smp_wmb();
    4801                 :       42890 :                         clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
    4802                 :             :                 }
    4803                 :       68207 :                 bh_unlock_sock(ssk);
    4804                 :       68207 :                 sock_put(ssk);
    4805                 :             : 
    4806         [ +  - ]:       68207 :                 if (++work_done == budget)
    4807                 :             :                         return budget;
    4808                 :             :         }
    4809                 :             : 
    4810                 :             :         /* always provide a 0 'work_done' argument, so that napi_complete_done
    4811                 :             :          * will not try accessing the NULL napi->dev ptr
    4812                 :             :          */
    4813                 :       66472 :         napi_complete_done(napi, 0);
    4814                 :       66472 :         return work_done;
    4815                 :             : }
    4816                 :             : 
    4817                 :           6 : void __init mptcp_proto_init(void)
    4818                 :             : {
    4819                 :           6 :         struct mptcp_delegated_action *delegated;
    4820                 :           6 :         int cpu;
    4821                 :             : 
    4822                 :           6 :         mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
    4823                 :             : 
    4824         [ -  + ]:           6 :         if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
    4825                 :           0 :                 panic("Failed to allocate MPTCP pcpu counter\n");
    4826                 :             : 
    4827                 :           6 :         mptcp_napi_dev = alloc_netdev_dummy(0);
    4828         [ -  + ]:           6 :         if (!mptcp_napi_dev)
    4829                 :           0 :                 panic("Failed to allocate MPTCP dummy netdev\n");
    4830   [ +  -  +  - ]:          54 :         for_each_possible_cpu(cpu) {
    4831                 :          24 :                 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
    4832                 :          24 :                 INIT_LIST_HEAD(&delegated->head);
    4833                 :          24 :                 netif_napi_add_tx(mptcp_napi_dev, &delegated->napi,
    4834                 :             :                                   mptcp_napi_poll);
    4835                 :          24 :                 napi_enable(&delegated->napi);
    4836                 :             :         }
    4837                 :             : 
    4838                 :           6 :         mptcp_subflow_init();
    4839                 :           6 :         mptcp_pm_init();
    4840                 :           6 :         mptcp_sched_init();
    4841                 :           6 :         mptcp_token_init();
    4842                 :             : 
    4843         [ -  + ]:           6 :         if (proto_register(&mptcp_prot, 1) != 0)
    4844                 :           0 :                 panic("Failed to register MPTCP proto.\n");
    4845                 :             : 
    4846                 :           6 :         inet_register_protosw(&mptcp_protosw);
    4847                 :             : 
    4848                 :           6 :         BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
    4849                 :             : 
    4850                 :             :         /* struct mptcp_data_frag: 'overhead' corresponds to the alignment
    4851                 :             :          * (ALIGN(1, sizeof(long)) - 1, so 8-1) + the struct's size
    4852                 :             :          */
    4853                 :           6 :         BUILD_BUG_ON(ALIGN(1, sizeof(long)) - 1 + sizeof(struct mptcp_data_frag)
    4854                 :             :                      > U8_MAX);
    4855                 :           6 : }
    4856                 :             : 
    4857                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    4858                 :             : static const struct proto_ops mptcp_v6_stream_ops = {
    4859                 :             :         .family            = PF_INET6,
    4860                 :             :         .owner             = THIS_MODULE,
    4861                 :             :         .release           = inet6_release,
    4862                 :             :         .bind              = mptcp_bind,
    4863                 :             :         .connect           = inet_stream_connect,
    4864                 :             :         .socketpair        = sock_no_socketpair,
    4865                 :             :         .accept            = mptcp_stream_accept,
    4866                 :             :         .getname           = inet6_getname,
    4867                 :             :         .poll              = mptcp_poll,
    4868                 :             :         .ioctl             = inet6_ioctl,
    4869                 :             :         .gettstamp         = sock_gettstamp,
    4870                 :             :         .listen            = mptcp_listen,
    4871                 :             :         .shutdown          = inet_shutdown,
    4872                 :             :         .setsockopt        = sock_common_setsockopt,
    4873                 :             :         .getsockopt        = sock_common_getsockopt,
    4874                 :             :         .sendmsg           = inet6_sendmsg,
    4875                 :             :         .recvmsg           = inet6_recvmsg,
    4876                 :             :         .mmap              = sock_no_mmap,
    4877                 :             : #ifdef CONFIG_COMPAT
    4878                 :             :         .compat_ioctl      = inet6_compat_ioctl,
    4879                 :             : #endif
    4880                 :             :         .set_rcvlowat      = mptcp_set_rcvlowat,
    4881                 :             :         .read_sock         = mptcp_read_sock,
    4882                 :             :         .splice_read       = mptcp_splice_read,
    4883                 :             : };
    4884                 :             : 
    4885                 :             : static struct proto mptcp_v6_prot;
    4886                 :             : 
    4887                 :             : static struct inet_protosw mptcp_v6_protosw = {
    4888                 :             :         .type           = SOCK_STREAM,
    4889                 :             :         .protocol       = IPPROTO_MPTCP,
    4890                 :             :         .prot           = &mptcp_v6_prot,
    4891                 :             :         .ops            = &mptcp_v6_stream_ops,
    4892                 :             :         .flags          = INET_PROTOSW_ICSK,
    4893                 :             : };
    4894                 :             : 
    4895                 :           6 : int __init mptcp_proto_v6_init(void)
    4896                 :             : {
    4897                 :           6 :         int err;
    4898                 :             : 
    4899                 :           6 :         mptcp_subflow_v6_init();
    4900                 :             : 
    4901                 :           6 :         mptcp_v6_prot = mptcp_prot;
    4902                 :           6 :         strscpy(mptcp_v6_prot.name, "MPTCPv6", sizeof(mptcp_v6_prot.name));
    4903                 :           6 :         mptcp_v6_prot.slab = NULL;
    4904                 :           6 :         mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
    4905                 :           6 :         mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
    4906                 :             : 
    4907                 :           6 :         err = proto_register(&mptcp_v6_prot, 1);
    4908         [ +  - ]:           6 :         if (err)
    4909                 :             :                 return err;
    4910                 :             : 
    4911                 :           6 :         err = inet6_register_protosw(&mptcp_v6_protosw);
    4912         [ -  + ]:           6 :         if (err)
    4913                 :           0 :                 proto_unregister(&mptcp_v6_prot);
    4914                 :             : 
    4915                 :             :         return err;
    4916                 :             : }
    4917                 :             : #endif
        

Generated by: LCOV version 2.3.1-1