LCOV - code coverage report
Current view: top level - mptcp/protocol.c (source / functions) Coverage Total Hit
Test: export-net Lines: 92.3 % 2460 2270
Test Date: 2026-05-14 09:16:33 Functions: 95.4 % 151 144
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 68.2 % 1959 1337

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

Generated by: LCOV version 2.3.1-1