LCOV - code coverage report
Current view: top level - mptcp/protocol.c (source / functions) Coverage Total Hit
Test: export-net Lines: 92.4 % 2475 2288
Test Date: 2026-07-28 10:49:00 Functions: 95.4 % 153 146
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 67.9 % 1971 1339

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

Generated by: LCOV version 2.3.1-1