LCOV - code coverage report
Current view: top level - mptcp/protocol.c (source / functions) Coverage Total Hit
Test: export Lines: 92.6 % 2442 2261
Test Date: 2026-02-20 07:46:52 Functions: 95.4 % 152 145
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 67.9 % 1951 1325

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

Generated by: LCOV version 2.3.1-1