Skip to contentSkip to conversation

Make subflow establishment lockless

PATCH In review Christoph Paasch 2 participants 30 messages Mar 26, 2018 – May 2, 2018

Subsystems (4)
lore
Share & tools
lore

Christoph Paasch

author
#0 lore raw

cover Make subflow establishment lockless

The most recent TCP stack supports lockless listeners. For MPTCP, we however are still taking the meta-lock when creating new subflows (cfr., tcp_v4_rcv and mptcp_lookup_join).

This patchset makes subflow establishment lockless by using lockless procedures in the stack, RCU-lists, atomic operations,...

Christoph Paasch (12):
mptcp: Cleanup
mptcp: Improved debugging
mptcp: Use correct tcp_queue when calling mptcp_fragment
mptcp: Use mptcp_can_new_subflow where possible
mptcp: Render mptcp_sub_inherit_sockopts lockless
mptcp: Make mptcp_set_new_pathindex lockless
mptcp: Remove cnt_subflows
mptcp: Remove cnt_established
mptcp: Rename tw_lock to mpcb_list_lock
mptcp: Make subflow-list an RCU-list
mptcp: Don't take meta-lock when receiving third ACK
mptcp: Make mptcp_do_join_short and mptcp_lookup_join lockless

 include/net/mptcp.h          | 120 ++++++++++-----------------
 net/ipv4/af_inet.c           |   9 ++-
 net/ipv4/ip_sockglue.c       |   7 +-
 net/ipv4/tcp.c               |  31 ++++---
 net/ipv4/tcp_ipv4.c          |  41 +---------
 net/ipv4/tcp_minisocks.c     |   9 ++-
 net/ipv6/tcp_ipv6.c          |  41 +---------
 net/mptcp/mctcp_desync.c     |  22 ++---
 net/mptcp/mptcp_balia.c      |  21 ++---
 net/mptcp/mptcp_binder.c     |  23 +++++-
 net/mptcp/mptcp_coupled.c    |  52 +++++-------
 net/mptcp/mptcp_ctrl.c       | 187 ++++++++++++++++++++++++++++---------------
 net/mptcp/mptcp_fullmesh.c   |  30 ++++---
 net/mptcp/mptcp_input.c      | 150 ++++++++++++----------------------
 net/mptcp/mptcp_ipv4.c       |  34 +++-----
 net/mptcp/mptcp_ipv6.c       |  15 ++--
 net/mptcp/mptcp_ndiffports.c |  17 +++-
 net/mptcp/mptcp_olia.c       |  28 ++++---
 net/mptcp/mptcp_output.c     |  50 ++++++++----
 net/mptcp/mptcp_redundant.c  | 151 +++++++++++++++++++++++++++-------
 net/mptcp/mptcp_rr.c         |  22 +++--
 net/mptcp/mptcp_sched.c      |  32 ++++----
 net/mptcp/mptcp_wvegas.c     |   5 +-
 23 files changed, 586 insertions(+), 511 deletions(-)
Signature · 1 line
2.16.2
author
#1 lore raw

PATCH 1/12 mptcp: Cleanup

Just some basic cleanup, fixing some comments, renaming sk to meta_sk, and moving mptcp_finish_handshake out of mptcp_ipv4.c to mptcp_ctrl.c

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 include/net/mptcp.h        |  8 ++++----
 net/mptcp/mptcp_ctrl.c     | 25 +++++++++++++++++++++----
 net/mptcp/mptcp_fullmesh.c |  2 +-
 net/mptcp/mptcp_input.c    |  7 ++-----
 net/mptcp/mptcp_ipv4.c     | 26 ++++----------------------
 net/mptcp/mptcp_ipv6.c     |  8 ++++----
 6 files changed, 36 insertions(+), 40 deletions(-)
include/net/mptcp.h +4 -4
index b9852081073f..a1ecb599f003 100644
@@ -663,10 +663,10 @@ extern int sysctl_mptcp_syn_retries;
 
 extern struct workqueue_struct *mptcp_wq;
 
-#define mptcp_debug(fmt, args...)					\
-	do {								\
-		if (unlikely(sysctl_mptcp_debug))			\
-			pr_err(fmt, ##args);	\
+#define mptcp_debug(fmt, args...)						\
+	do {									\
+		if (unlikely(sysctl_mptcp_debug))				\
+			pr_err(fmt, ##args);					\
 	} while (0)
 
 /* Iterates over all subflows */
net/mptcp/mptcp_ctrl.c +21 -4
index 62aa8c5e177b..29fe6485d69a 100644
@@ -1315,8 +1315,8 @@ void mptcp_fallback_meta_sk(struct sock *meta_sk)
 int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
 		   gfp_t flags)
 {
-	struct mptcp_cb *mpcb	= tcp_sk(meta_sk)->mpcb;
-	struct tcp_sock *tp	= tcp_sk(sk);
+	struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
+	struct tcp_sock *tp = tcp_sk(sk);
 
 	tp->mptcp = kmem_cache_zalloc(mptcp_sock_cache, flags);
 	if (!tp->mptcp)
@@ -2122,9 +2122,9 @@ struct sock *mptcp_check_req_child(struct sock *meta_sk,
 				   struct sk_buff *skb,
 				   const struct mptcp_options_received *mopt)
 {
-	struct tcp_sock *child_tp = tcp_sk(child);
+	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
 	struct mptcp_request_sock *mtreq = mptcp_rsk(req);
-	struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
+	struct tcp_sock *child_tp = tcp_sk(child);
 	u8 hash_mac_check[20];
 
 	child_tp->inside_tk_table = 0;
@@ -2490,6 +2490,23 @@ int mptcp_conn_request(struct sock *sk, struct sk_buff *skb)
 	return 0;
 }
 
+int mptcp_finish_handshake(struct sock *child, struct sk_buff *skb)
+	__releases(&child->sk_lock.slock)
+{
+	int ret;
+
+	/* We don't call tcp_child_process here, because we hold
+	 * already the meta-sk-lock and are sure that it is not owned
+	 * by the user.
+	 */
+	tcp_sk(child)->segs_in += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
+	ret = tcp_rcv_state_process(child, skb);
+	bh_unlock_sock(child);
+	sock_put(child);
+
+	return ret;
+}
+
 static void __mptcp_get_info(const struct sock *meta_sk,
 			     struct mptcp_meta_info *info)
 {
net/mptcp/mptcp_fullmesh.c +1 -1
index 9690478fae70..d29102a1dffb 100644
@@ -79,7 +79,7 @@ struct fullmesh_priv {
 	u8 rem4_bits;
 	u8 rem6_bits;
 
-	/* Are we established the additional subflows for primary pair? */
+	/* Have we established the additional subflows for primary pair? */
 	u8 first_pair:1;
 };
 
net/mptcp/mptcp_input.c +2 -5
index 12d7337b9b18..f3a4dbe45bc6 100644
@@ -89,10 +89,7 @@ static inline int mptcp_tso_acked_reinject(const struct sock *meta_sk,
 	return packets_acked;
 }
 
-/**
- * Cleans the meta-socket retransmission queue and the reinject-queue.
- * @sk must be the metasocket.
- */
+/* Cleans the meta-socket retransmission queue and the reinject-queue. */
 static void mptcp_clean_rtx_queue(struct sock *meta_sk, u32 prior_snd_una)
 {
 	struct sk_buff *skb, *tmp, *next;
@@ -350,7 +347,7 @@ static int mptcp_verif_dss_csum(struct sock *sk)
 
 	/* Now, checksum must be 0 */
 	if (unlikely(csum_fold(csum_tcp))) {
-		pr_err("%s csum is wrong: %#x data_seq %u dss_csum_added %d overflowed %d iterations %d\n",
+		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
 		       __func__, csum_fold(csum_tcp), TCP_SKB_CB(last)->seq,
 		       dss_csum_added, overflowed, iter);
 
net/mptcp/mptcp_ipv4.c +4 -22
index c1a2d89393f4..99ff09225ebf 100644
@@ -96,11 +96,11 @@ static u32 mptcp_v4_cookie_init_seq(struct request_sock *req, const struct sock
 }
 #endif
 
-static int mptcp_v4_join_init_req(struct request_sock *req, const struct sock *sk,
+static int mptcp_v4_join_init_req(struct request_sock *req, const struct sock *meta_sk,
 				  struct sk_buff *skb, bool want_cookie)
 {
 	struct mptcp_request_sock *mtreq = mptcp_rsk(req);
-	const struct mptcp_cb *mpcb = tcp_sk(sk)->mpcb;
+	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
 	union inet_addr addr;
 	int loc_id;
 	bool low_prio = false;
@@ -112,14 +112,14 @@ static int mptcp_v4_join_init_req(struct request_sock *req, const struct sock *s
 	 */
 	mtreq->hash_entry.pprev = NULL;
 
-	tcp_request_sock_ipv4_ops.init_req(req, sk, skb, want_cookie);
+	tcp_request_sock_ipv4_ops.init_req(req, meta_sk, skb, want_cookie);
 
 	mtreq->mptcp_loc_nonce = mptcp_v4_get_nonce(ip_hdr(skb)->saddr,
 						    ip_hdr(skb)->daddr,
 						    tcp_hdr(skb)->source,
 						    tcp_hdr(skb)->dest);
 	addr.ip = inet_rsk(req)->ir_loc_addr;
-	loc_id = mpcb->pm_ops->get_local_id(AF_INET, &addr, sock_net(sk), &low_prio);
+	loc_id = mpcb->pm_ops->get_local_id(AF_INET, &addr, sock_net(meta_sk), &low_prio);
 	if (loc_id == -1)
 		return -1;
 	mtreq->loc_id = loc_id;
@@ -149,24 +149,6 @@ static int mptcp_v4_join_request(struct sock *meta_sk, struct sk_buff *skb)
 				meta_sk, skb);
 }
 
-int mptcp_finish_handshake(struct sock *child, struct sk_buff *skb)
-	__releases(&child->sk_lock.slock)
-{
-	int ret;
-
-	/* We don't call tcp_child_process here, because we hold
-	 * already the meta-sk-lock and are sure that it is not owned
-	 * by the user.
-	 */
-	tcp_sk(child)->segs_in += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
-	ret = tcp_rcv_state_process(child, skb);
-	bh_unlock_sock(child);
-	sock_put(child);
-
-	return ret;
-}
-
-
 /* Similar to: tcp_v4_do_rcv
  * We only process join requests here. (either the SYN or the final ACK)
  */
net/mptcp/mptcp_ipv6.c +4 -4
index 7ef56440046d..04b69220baef 100644
@@ -125,11 +125,11 @@ static u32 mptcp_v6_cookie_init_seq(struct request_sock *req, const struct sock
 }
 #endif
 
-static int mptcp_v6_join_init_req(struct request_sock *req, const struct sock *sk,
+static int mptcp_v6_join_init_req(struct request_sock *req, const struct sock *meta_sk,
 				  struct sk_buff *skb, bool want_cookie)
 {
 	struct mptcp_request_sock *mtreq = mptcp_rsk(req);
-	const struct mptcp_cb *mpcb = tcp_sk(sk)->mpcb;
+	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
 	union inet_addr addr;
 	int loc_id;
 	bool low_prio = false;
@@ -141,14 +141,14 @@ static int mptcp_v6_join_init_req(struct request_sock *req, const struct sock *s
 	 */
 	mtreq->hash_entry.pprev = NULL;
 
-	tcp_request_sock_ipv6_ops.init_req(req, sk, skb, want_cookie);
+	tcp_request_sock_ipv6_ops.init_req(req, meta_sk, skb, want_cookie);
 
 	mtreq->mptcp_loc_nonce = mptcp_v6_get_nonce(ipv6_hdr(skb)->saddr.s6_addr32,
 						    ipv6_hdr(skb)->daddr.s6_addr32,
 						    tcp_hdr(skb)->source,
 						    tcp_hdr(skb)->dest);
 	addr.in6 = inet_rsk(req)->ir_v6_loc_addr;
-	loc_id = mpcb->pm_ops->get_local_id(AF_INET6, &addr, sock_net(sk), &low_prio);
+	loc_id = mpcb->pm_ops->get_local_id(AF_INET6, &addr, sock_net(meta_sk), &low_prio);
 	if (loc_id == -1)
 		return -1;
 	mtreq->loc_id = loc_id;
Signature · 1 line
2.16.2
author
#2 lore raw

PATCH 2/12 mptcp: Improved debugging

In reply to Christoph Paasch

Allow to print the value of ret when mptcp_add_sock fails.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 net/mptcp/mptcp_ipv4.c | 3 ++-
 net/mptcp/mptcp_ipv6.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
net/mptcp/mptcp_ipv4.c +2 -1
index 99ff09225ebf..36cdbf09a5b9 100644
@@ -280,7 +280,8 @@ int mptcp_init4_subsockets(struct sock *meta_sk, const struct mptcp_loc4 *loc,
 	lockdep_set_class_and_name(&(sk)->sk_lock.slock, &meta_slock_key, meta_slock_key_name);
 	lockdep_init_map(&(sk)->sk_lock.dep_map, meta_key_name, &meta_key, 0);
 
-	if (mptcp_add_sock(meta_sk, sk, loc->loc4_id, rem->rem4_id, GFP_KERNEL)) {
+	ret = mptcp_add_sock(meta_sk, sk, loc->loc4_id, rem->rem4_id, GFP_KERNEL);
+	if (ret) {
 		net_err_ratelimited("%s mptcp_add_sock failed ret: %d\n",
 				    __func__, ret);
 		goto error;
net/mptcp/mptcp_ipv6.c +2 -1
index 04b69220baef..bde4a9d7ab32 100644
@@ -306,7 +306,8 @@ int mptcp_init6_subsockets(struct sock *meta_sk, const struct mptcp_loc6 *loc,
 	lockdep_set_class_and_name(&(sk)->sk_lock.slock, &meta_slock_key, meta_slock_key_name);
 	lockdep_init_map(&(sk)->sk_lock.dep_map, meta_key_name, &meta_key, 0);
 
-	if (mptcp_add_sock(meta_sk, sk, loc->loc6_id, rem->rem6_id, GFP_KERNEL)) {
+	ret = mptcp_add_sock(meta_sk, sk, loc->loc6_id, rem->rem6_id, GFP_KERNEL);
+	if (ret) {
 		net_err_ratelimited("%s mptcp_add_sock failed ret: %d\n",
 				    __func__, ret);
 		goto error;
Signature · 1 line
2.16.2
author
#3 lore raw

PATCH 3/12 mptcp: Use correct tcp_queue when calling mptcp_fragment

In reply to Christoph Paasch

The skb may come from the write-queue, the rtx-queue or the reinject queue.

When it comes from the rtx-queue, we need to give mptcp_fragment the right tcp_queue.

To handle the case when it comes from the reinject-queue, I will post a follow-up patch as this seems to be a bug in the stable release as well.

Fixes 8a1051d977cf ("Merge tag 'v4.15' into mptcp_trunk")
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 net/mptcp/mptcp_output.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/mptcp/mptcp_output.c +9 -3
index 43a21f66ac34..fc950a98ce31 100644
@@ -607,7 +607,10 @@ static int mptcp_fragment(struct sock *meta_sk, enum tcp_queue tcp_queue,
 	if (ret)
 		return ret;
 
-	buff = skb->next;
+	if (tcp_queue == TCP_FRAG_IN_WRITE_QUEUE)
+		buff = skb->next;
+	else
+		buff = skb_rb_next(skb);
 
 	flags = TCP_SKB_CB(skb)->mptcp_flags;
 	TCP_SKB_CB(skb)->mptcp_flags = flags & ~(MPTCPHDR_FIN);
@@ -740,6 +743,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
 
 	while ((skb = mpcb->sched_ops->next_segment(meta_sk, &reinject, &subsk,
 						    &sublimit))) {
+		enum tcp_queue tcp_queue = TCP_FRAG_IN_WRITE_QUEUE;
 		unsigned int limit;
 
 		WARN(TCP_SKB_CB(skb)->sacked, "sacked: %u reinject: %u",
@@ -755,6 +759,8 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
 				__kfree_skb(skb);
 				continue;
 			}
+		} else if (reinject == -1) {
+			tcp_queue = TCP_FRAG_IN_RTX_QUEUE;
 		}
 
 		/* If the segment was cloned (e.g. a meta retransmission),
@@ -817,7 +823,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
 			limit = min(limit, sublimit);
 
 		if (skb->len > limit &&
-		    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_WRITE_QUEUE,
+		    unlikely(mptcp_fragment(meta_sk, tcp_queue,
 					    skb, limit, gfp, reinject)))
 			break;
 
@@ -1545,7 +1551,7 @@ int mptcp_retransmit_skb(struct sock *meta_sk, struct sk_buff *skb)
 					    TCP_NAGLE_OFF);
 
 	if (skb->len > limit &&
-	    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_WRITE_QUEUE, skb,
+	    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_RTX_QUEUE, skb,
 				    limit, GFP_ATOMIC, 0)))
 		goto failed;
 
Signature · 1 line
2.16.2
#4 lore raw

Re: [MPTCP] [PATCH 03/12] mptcp: Use correct tcp_queue when calling mptcp_fragment

Hi Christoph,

Quoted 2 lines · Christoph Paasch

The skb may come from the write-queue, the rtx-queue or the reinject queue.

Good catch! :)

Should we not clearly document that in mptcp_fragment() code? I mean the skb may come from the write-queue, the rtx-queue or the reinject-queue but a reinjection is not a retransmission on the same sk, it should then go in the write-queue. This is quite tricky and specific to MPTCP, we need to have that in mind when looking at this code.

I guess it is not easy to debug when this kind of fragments are in the wrong queue. :)

Best regards,
Matt

Quoted 64 lines

When it comes from the rtx-queue, we need to give mptcp_fragment the right tcp_queue.

To handle the case when it comes from the reinject-queue, I will post a follow-up patch as this seems to be a bug in the stable release as well.

Fixes 8a1051d977cf ("Merge tag 'v4.15' into mptcp_trunk")
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  net/mptcp/mptcp_output.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
net/mptcp/mptcp_output.c +9 -3
index 43a21f66ac34..fc950a98ce31 100644
@@ -607,7 +607,10 @@ static int mptcp_fragment(struct sock *meta_sk, enum tcp_queue tcp_queue,
  	if (ret)
  		return ret;
 
-	buff = skb->next;
+	if (tcp_queue == TCP_FRAG_IN_WRITE_QUEUE)
+		buff = skb->next;
+	else
+		buff = skb_rb_next(skb);
 
  	flags = TCP_SKB_CB(skb)->mptcp_flags;
  	TCP_SKB_CB(skb)->mptcp_flags = flags & ~(MPTCPHDR_FIN);
@@ -740,6 +743,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
 
  	while ((skb = mpcb->sched_ops->next_segment(meta_sk, &reinject, &subsk,
  						    &sublimit))) {
+		enum tcp_queue tcp_queue = TCP_FRAG_IN_WRITE_QUEUE;
  		unsigned int limit;
 
  		WARN(TCP_SKB_CB(skb)->sacked, "sacked: %u reinject: %u",
@@ -755,6 +759,8 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  				__kfree_skb(skb);
  				continue;
  			}
+		} else if (reinject == -1) {
+			tcp_queue = TCP_FRAG_IN_RTX_QUEUE;
  		}
 
  		/* If the segment was cloned (e.g. a meta retransmission),
@@ -817,7 +823,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  			limit = min(limit, sublimit);
 
  		if (skb->len > limit &&
-		    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_WRITE_QUEUE,
+		    unlikely(mptcp_fragment(meta_sk, tcp_queue,
  					    skb, limit, gfp, reinject)))
  			break;
 
@@ -1545,7 +1551,7 @@ int mptcp_retransmit_skb(struct sock *meta_sk, struct sk_buff *skb)
  					    TCP_NAGLE_OFF);
 
  	if (skb->len > limit &&
-	    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_WRITE_QUEUE, skb,
+	    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_RTX_QUEUE, skb,
  				    limit, GFP_ATOMIC, 0)))
  		goto failed;
 
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#5 lore raw

Re: [MPTCP] [PATCH 03/12] mptcp: Use correct tcp_queue when calling mptcp_fragment

Hello,

Quoted 6 lines · Matthieu Baerts

Hi Christoph,

Quoted 2 lines · Christoph Paasch

The skb may come from the write-queue, the rtx-queue or the reinject queue.

Quoted 7 lines

Good catch! :)

Should we not clearly document that in mptcp_fragment() code? I mean the skb may come from the write-queue, the rtx-queue or the reinject-queue but a reinjection is not a retransmission on the same sk, it should then go in the write-queue. This is quite tricky and specific to MPTCP, we need to have that in mind when looking at this code.

sounds good! I will add a comment at the top of the function.

Christoph

Quoted 84 lines

I guess it is not easy to debug when this kind of fragments are in the wrong queue. :)

Best regards,
Matt

Quoted 55 lines

When it comes from the rtx-queue, we need to give mptcp_fragment the right tcp_queue.

To handle the case when it comes from the reinject-queue, I will post a follow-up patch as this seems to be a bug in the stable release as well.

Fixes 8a1051d977cf ("Merge tag 'v4.15' into mptcp_trunk")
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  net/mptcp/mptcp_output.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
net/mptcp/mptcp_output.c +9 -3
index 43a21f66ac34..fc950a98ce31 100644
@@ -607,7 +607,10 @@ static int mptcp_fragment(struct sock *meta_sk, enum tcp_queue tcp_queue,
  	if (ret)
  		return ret;
-	buff = skb->next;
+	if (tcp_queue == TCP_FRAG_IN_WRITE_QUEUE)
+		buff = skb->next;
+	else
+		buff = skb_rb_next(skb);
  	flags = TCP_SKB_CB(skb)->mptcp_flags;
  	TCP_SKB_CB(skb)->mptcp_flags = flags & ~(MPTCPHDR_FIN);
@@ -740,6 +743,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  	while ((skb = mpcb->sched_ops->next_segment(meta_sk, &reinject, &subsk,
  						    &sublimit))) {
+		enum tcp_queue tcp_queue = TCP_FRAG_IN_WRITE_QUEUE;
  		unsigned int limit;
  		WARN(TCP_SKB_CB(skb)->sacked, "sacked: %u reinject: %u",
@@ -755,6 +759,8 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  				__kfree_skb(skb);
  				continue;
  			}
+		} else if (reinject == -1) {
+			tcp_queue = TCP_FRAG_IN_RTX_QUEUE;
  		}
  		/* If the segment was cloned (e.g. a meta retransmission),
@@ -817,7 +823,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  			limit = min(limit, sublimit);
  		if (skb->len > limit &&
-		    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_WRITE_QUEUE,
+		    unlikely(mptcp_fragment(meta_sk, tcp_queue,
  					    skb, limit, gfp, reinject)))
  			break;
@@ -1545,7 +1551,7 @@ int mptcp_retransmit_skb(struct sock *meta_sk, struct sk_buff *skb)
  					    TCP_NAGLE_OFF);
  	if (skb->len > limit &&
-	    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_WRITE_QUEUE, skb,
+	    unlikely(mptcp_fragment(meta_sk, TCP_FRAG_IN_RTX_QUEUE, skb,
  				    limit, GFP_ATOMIC, 0)))
  		goto failed;
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
author
#6 lore raw

PATCH 4/12 mptcp: Use mptcp_can_new_subflow where possible

In reply to Christoph Paasch

Let's reuse the function instead of implementing the same checks again.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 net/mptcp/mptcp_input.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/mptcp/mptcp_input.c +2 -4
index f3a4dbe45bc6..ba9e3fe62824 100644
@@ -1171,7 +1171,7 @@ int mptcp_lookup_join(struct sk_buff *skb, struct inet_timewait_sock *tw)
 	}
 
 	mpcb = tcp_sk(meta_sk)->mpcb;
-	if (mpcb->infinite_mapping_rcv || mpcb->send_infinite_mapping) {
+	if (!mptcp_can_new_subflow(meta_sk)) {
 		/* We are in fallback-mode on the reception-side -
 		 * no new subflows!
 		 */
@@ -1255,9 +1255,7 @@ int mptcp_do_join_short(struct sk_buff *skb,
 	 * And the send-reset will try to take yet another one (ip_send_reply).
 	 * Thus, we propagate the reset up to tcp_rcv_state_process.
 	 */
-	if (tcp_sk(meta_sk)->mpcb->infinite_mapping_rcv ||
-	    tcp_sk(meta_sk)->mpcb->send_infinite_mapping ||
-	    meta_sk->sk_state == TCP_CLOSE || !tcp_sk(meta_sk)->inside_tk_table) {
+	if (!mptcp_can_new_subflow(meta_sk)) {
 		MPTCP_INC_STATS(sock_net(meta_sk), MPTCP_MIB_JOINFALLBACK);
 		bh_unlock_sock(meta_sk);
 		sock_put(meta_sk); /* Taken by mptcp_hash_find */
Signature · 1 line
2.16.2
author
#7 lore raw

PATCH 5/12 mptcp: Render mptcp_sub_inherit_sockopts lockless

In reply to Christoph Paasch

We are accessing the meta-socket's TOS field twice. It could change in-between as we do this in a lockless manner.

Thus, let's make sure we read it only once.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 net/mptcp/mptcp_ctrl.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/mptcp/mptcp_ctrl.c +6 -2
index 29fe6485d69a..fb37053dd127 100644
@@ -945,11 +945,15 @@ static void mptcp_mpcb_inherit_sockopts(struct sock *meta_sk, struct sock *maste
 	inet_sk(master_sk)->recverr = 0;
 }
 
+/* Called without holding lock on meta_sk */
 static void mptcp_sub_inherit_sockopts(const struct sock *meta_sk, struct sock *sub_sk)
 {
+	__u8 meta_tos;
+
 	/* IP_TOS also goes to the subflow. */
-	if (inet_sk(sub_sk)->tos != inet_sk(meta_sk)->tos) {
-		inet_sk(sub_sk)->tos = inet_sk(meta_sk)->tos;
+	meta_tos = READ_ONCE(inet_sk(meta_sk)->tos);
+	if (inet_sk(sub_sk)->tos != meta_tos) {
+		inet_sk(sub_sk)->tos = meta_tos;
 		sub_sk->sk_priority = meta_sk->sk_priority;
 		sk_dst_reset(sub_sk);
 	}
Signature · 1 line
2.16.2
#8 lore raw

Re: [MPTCP] [PATCH 05/12] mptcp: Render mptcp_sub_inherit_sockopts lockless

Hi Christoph,

Quoted 2 lines · Christoph Paasch

We are accessing the meta-socket's TOS field twice. It could change in-between as we do this in a lockless manner.

I hope this will not add more complexities in the code :)

Should we not clearly identify/mark all functions that are called with or without holding locks? Functions could be renamed maybe? I know, quite a big work :-)

Best regards,
Matt

Quoted 30 lines

Thus, let's make sure we read it only once.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  net/mptcp/mptcp_ctrl.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
net/mptcp/mptcp_ctrl.c +6 -2
index 29fe6485d69a..fb37053dd127 100644
@@ -945,11 +945,15 @@ static void mptcp_mpcb_inherit_sockopts(struct sock *meta_sk, struct sock *maste
  	inet_sk(master_sk)->recverr = 0;
  }
 
+/* Called without holding lock on meta_sk */
  static void mptcp_sub_inherit_sockopts(const struct sock *meta_sk, struct sock *sub_sk)
  {
+	__u8 meta_tos;
+
  	/* IP_TOS also goes to the subflow. */
-	if (inet_sk(sub_sk)->tos != inet_sk(meta_sk)->tos) {
-		inet_sk(sub_sk)->tos = inet_sk(meta_sk)->tos;
+	meta_tos = READ_ONCE(inet_sk(meta_sk)->tos);
+	if (inet_sk(sub_sk)->tos != meta_tos) {
+		inet_sk(sub_sk)->tos = meta_tos;
  		sub_sk->sk_priority = meta_sk->sk_priority;
  		sk_dst_reset(sub_sk);
  	}
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#9 lore raw

Re: [MPTCP] [PATCH 05/12] mptcp: Render mptcp_sub_inherit_sockopts lockless

Quoted 11 lines · Matthieu Baerts

Hi Christoph,

Quoted 2 lines · Christoph Paasch

We are accessing the meta-socket's TOS field twice. It could change in-between as we do this in a lockless manner.

I hope this will not add more complexities in the code :)

Should we not clearly identify/mark all functions that are called with or without holding locks? Functions could be renamed maybe? I know, quite a big work :-)

Yes, and also that would mean quite some function-name-length bloat :)

For now, I just simply added a comment to each of these functions. I think that should be fine, as since 4.9 all TCP connection establishment is supposed to be lockless.

Christoph

Quoted 55 lines

Best regards,
Matt

Quoted 29 lines

Thus, let's make sure we read it only once.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  net/mptcp/mptcp_ctrl.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
net/mptcp/mptcp_ctrl.c +6 -2
index 29fe6485d69a..fb37053dd127 100644
@@ -945,11 +945,15 @@ static void mptcp_mpcb_inherit_sockopts(struct sock *meta_sk, struct sock *maste
  	inet_sk(master_sk)->recverr = 0;
  }
+/* Called without holding lock on meta_sk */
  static void mptcp_sub_inherit_sockopts(const struct sock *meta_sk, struct sock *sub_sk)
  {
+	__u8 meta_tos;
+
  	/* IP_TOS also goes to the subflow. */
-	if (inet_sk(sub_sk)->tos != inet_sk(meta_sk)->tos) {
-		inet_sk(sub_sk)->tos = inet_sk(meta_sk)->tos;
+	meta_tos = READ_ONCE(inet_sk(meta_sk)->tos);
+	if (inet_sk(sub_sk)->tos != meta_tos) {
+		inet_sk(sub_sk)->tos = meta_tos;
  		sub_sk->sk_priority = meta_sk->sk_priority;
  		sk_dst_reset(sub_sk);
  	}
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
#10 lore raw

Re: [MPTCP] [PATCH 05/12] mptcp: Render mptcp_sub_inherit_sockopts lockless

Hi Christoph,

Quoted 13 lines · Christoph Paasch
Quoted 11 lines · Matthieu Baerts

Hi Christoph,

Quoted 2 lines · Christoph Paasch

We are accessing the meta-socket's TOS field twice. It could change in-between as we do this in a lockless manner.

I hope this will not add more complexities in the code :)

Should we not clearly identify/mark all functions that are called with or without holding locks? Functions could be renamed maybe? I know, quite a big work :-)

Quoted 5 lines

Yes, and also that would mean quite some function-name-length bloat :)

For now, I just simply added a comment to each of these functions. I think that should be fine, as since 4.9 all TCP connection establishment is supposed to be lockless.

It makes sense, thank you!

Best regards,
Matt

Quoted 58 lines

Christoph

Quoted 55 lines

Best regards,
Matt

Quoted 29 lines

Thus, let's make sure we read it only once.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
   net/mptcp/mptcp_ctrl.c | 8 ++++++--
   1 file changed, 6 insertions(+), 2 deletions(-)
net/mptcp/mptcp_ctrl.c +6 -2
index 29fe6485d69a..fb37053dd127 100644
@@ -945,11 +945,15 @@ static void mptcp_mpcb_inherit_sockopts(struct sock *meta_sk, struct sock *maste
   	inet_sk(master_sk)->recverr = 0;
   }
+/* Called without holding lock on meta_sk */
   static void mptcp_sub_inherit_sockopts(const struct sock *meta_sk, struct sock *sub_sk)
   {
+	__u8 meta_tos;
+
   	/* IP_TOS also goes to the subflow. */
-	if (inet_sk(sub_sk)->tos != inet_sk(meta_sk)->tos) {
-		inet_sk(sub_sk)->tos = inet_sk(meta_sk)->tos;
+	meta_tos = READ_ONCE(inet_sk(meta_sk)->tos);
+	if (inet_sk(sub_sk)->tos != meta_tos) {
+		inet_sk(sub_sk)->tos = meta_tos;
   		sub_sk->sk_priority = meta_sk->sk_priority;
   		sk_dst_reset(sub_sk);
   	}
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#11 lore raw

PATCH 6/12 mptcp: Make mptcp_set_new_pathindex lockless

In reply to Christoph Paasch

There is quite a complex logic just for setting the next path-index. It also prevents us from making the operation lockless.

Let's simplify this and make it lockless through a simple test_and_set_bit.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 include/net/mptcp.h    | 34 +---------------------------------
 net/mptcp/mptcp_ctrl.c | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 33 deletions(-)
include/net/mptcp.h +1 -33
index a1ecb599f003..647183460200 100644
@@ -329,9 +329,7 @@ struct mptcp_cb {
 	u8 mptcp_pm[MPTCP_PM_SIZE] __aligned(8);
 	struct mptcp_pm_ops *pm_ops;
 
-	u32 path_index_bits;
-	/* Next pi to pick up in case a new path becomes available */
-	u8 next_path_index;
+	unsigned long path_index_bits;
 
 	__u8	mptcp_ver;
 
@@ -1275,36 +1273,6 @@ static inline bool mptcp_fallback_infinite(struct sock *sk, int flag)
 	return false;
 }
 
-/* Find the first index whose bit in the bit-field == 0 */
-static inline u8 mptcp_set_new_pathindex(struct mptcp_cb *mpcb)
-{
-	u8 base = mpcb->next_path_index;
-	int i;
-
-	/* Start at 1, because 0 is reserved for the meta-sk */
-	mptcp_for_each_bit_unset(mpcb->path_index_bits >> base, i) {
-		if (i + base < 1)
-			continue;
-		if (i + base >= sizeof(mpcb->path_index_bits) * 8)
-			break;
-		i += base;
-		mpcb->path_index_bits |= (1 << i);
-		mpcb->next_path_index = i + 1;
-		return i;
-	}
-	mptcp_for_each_bit_unset(mpcb->path_index_bits, i) {
-		if (i >= sizeof(mpcb->path_index_bits) * 8)
-			break;
-		if (i < 1)
-			continue;
-		mpcb->path_index_bits |= (1 << i);
-		mpcb->next_path_index = i + 1;
-		return i;
-	}
-
-	return 0;
-}
-
 static inline bool mptcp_v6_is_v4_mapped(const struct sock *sk)
 {
 	return sk->sk_family == AF_INET6 &&
net/mptcp/mptcp_ctrl.c +16 -0
index fb37053dd127..321a8578e9c3 100644
@@ -1316,6 +1316,22 @@ void mptcp_fallback_meta_sk(struct sock *meta_sk)
 	kmem_cache_free(mptcp_cb_cache, tcp_sk(meta_sk)->mpcb);
 }
 
+/*  Called without holding lock on mpcb */
+static u8 mptcp_set_new_pathindex(struct mptcp_cb *mpcb)
+{
+	int i;
+
+	/* Start at 1, because 0 is reserved for the meta-sk */
+	for (i = 1; i < sizeof(mpcb->path_index_bits) * 8; i++) {
+		if (!test_and_set_bit(i, &mpcb->path_index_bits))
+			break;
+	}
+
+	if (i == sizeof(mpcb->path_index_bits) * 8)
+		return 0;
+	return i;
+}
+
 int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
 		   gfp_t flags)
 {
Signature · 1 line
2.16.2
author
#12 lore raw

PATCH 7/12 mptcp: Remove cnt_subflows

In reply to Christoph Paasch

Our goal is to make subflow establishment lockless. cnt_subflows would need to either become an atomic variable, or we can remove it.

Well, let's remove it! In the general case it is not needed at all. Some exotic schedulers need cnt_subflows, but we don't care if we add complexity in these schedulers.

One place where it was used is in the /proc/net/mptcp_net/mptcp output. We don't guarantee backwards compatibility, so we can just change the output. I will submit a follow-up patch for net-tools.

Another place is mptcp_verif_dss_csum(). There the access was actually wrong. Simply because cnt_subflows > 1, it doesn't mean we have a working subflow. So, we fix that. I don't think this issue is big enough to backport it.

Finally mptcp_sched.c was also using cnt_subflows but merely for speeding up things to avoid going through the whole scheduler code when there is only one subflow. So, we can remove it and trade performance for a better design.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 include/net/mptcp.h          |  1 -
 net/mptcp/mptcp_binder.c     | 17 +++++++++++++++--
 net/mptcp/mptcp_ctrl.c       | 16 ++++++----------
 net/mptcp/mptcp_input.c      | 11 ++++++++++-
 net/mptcp/mptcp_ndiffports.c | 17 +++++++++++++++--
 net/mptcp/mptcp_output.c     |  2 +-
 net/mptcp/mptcp_sched.c      | 11 -----------
 7 files changed, 47 insertions(+), 28 deletions(-)
include/net/mptcp.h +0 -1
index 647183460200..523abc8ce01d 100644
@@ -280,7 +280,6 @@ struct mptcp_cb {
 		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
 
 	/* socket count in this connection */
-	u8 cnt_subflows;
 	u8 cnt_established;
 
 #define MPTCP_SCHED_DATA_SIZE 8
net/mptcp/mptcp_binder.c +15 -2
index 58a342dc1620..d6ef9c5b6ce5 100644
@@ -287,6 +287,18 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
 	return -1;
 }
 
+static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}
+
 /**
  * Create all new subflows, by doing calls to mptcp_initX_subsockets
  *
@@ -301,7 +313,7 @@ static void create_subflow_worker(struct work_struct *work)
 						     subflow_work);
 	struct mptcp_cb *mpcb = pm_priv->mpcb;
 	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
 
 next_subflow:
 	if (iter) {
@@ -322,8 +334,9 @@ static void create_subflow_worker(struct work_struct *work)
 	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
 		goto exit;
 
+	subflow_cnt = mptcp_binder_subflow_count(mpcb);
 	if (mptcp_binder_ndiffports > iter &&
-	    mptcp_binder_ndiffports > mpcb->cnt_subflows) {
+	    mptcp_binder_ndiffports > subflow_cnt) {
 		struct mptcp_loc4 loc;
 		struct mptcp_rem4 rem;
 
net/mptcp/mptcp_ctrl.c +6 -10
index 321a8578e9c3..b1109ee9770f 100644
@@ -1379,7 +1379,6 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
 	mpcb->connection_list = tp;
 	tp->mptcp->attached = 1;
 
-	mpcb->cnt_subflows++;
 	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
 		   &meta_sk->sk_rmem_alloc);
 
@@ -1399,23 +1398,21 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
 	sk->sk_destruct = mptcp_sock_destruct;
 
 	if (sk->sk_family == AF_INET)
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d\n",
 			    __func__ , mpcb->mptcp_loc_token,
 			    tp->mptcp->path_index,
 			    &((struct inet_sock *)tp)->inet_saddr,
 			    ntohs(((struct inet_sock *)tp)->inet_sport),
 			    &((struct inet_sock *)tp)->inet_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
 #if IS_ENABLED(CONFIG_IPV6)
 	else
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d\n",
 			    __func__ , mpcb->mptcp_loc_token,
 			    tp->mptcp->path_index, &inet6_sk(sk)->saddr,
 			    ntohs(((struct inet_sock *)tp)->inet_sport),
 			    &sk->sk_v6_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
 #endif
 
 	return 0;
@@ -1452,7 +1449,6 @@ void mptcp_del_sock(struct sock *sk)
 			}
 		}
 	}
-	mpcb->cnt_subflows--;
 	if (tp->mptcp->establish_increased)
 		mpcb->cnt_established--;
 
@@ -2816,8 +2812,8 @@ static int mptcp_pm_seq_show(struct seq_file *seq, void *v)
 					   ntohs(isk->inet_dport));
 #endif
 			}
-			seq_printf(seq, " %02X %02X %08X:%08X %lu",
-				   meta_sk->sk_state, mpcb->cnt_subflows,
+			seq_printf(seq, " %02X %08X:%08X %lu",
+				   meta_sk->sk_state,
 				   meta_tp->write_seq - meta_tp->snd_una,
 				   max_t(int, meta_tp->rcv_nxt -
 					 meta_tp->copied_seq, 0),
net/mptcp/mptcp_input.c +10 -1
index ba9e3fe62824..126c961ec55a 100644
@@ -347,6 +347,8 @@ static int mptcp_verif_dss_csum(struct sock *sk)
 
 	/* Now, checksum must be 0 */
 	if (unlikely(csum_fold(csum_tcp))) {
+		struct sock *sk_it = NULL;
+
 		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
 		       __func__, csum_fold(csum_tcp), TCP_SKB_CB(last)->seq,
 		       dss_csum_added, overflowed, iter);
@@ -359,7 +361,14 @@ static int mptcp_verif_dss_csum(struct sock *sk)
 		 */
 		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
 
-		if (tp->mpcb->cnt_subflows > 1) {
+		/* Search for another subflow that is fully established */
+		mptcp_for_each_sk(tp->mpcb, sk_it) {
+			if (sk_it != sk &&
+			    tcp_sk(sk_it)->mptcp->fully_established)
+				break;
+		}
+
+		if (sk_it) {
 			mptcp_send_reset(sk);
 			ans = -1;
 		} else {
net/mptcp/mptcp_ndiffports.c +15 -2
index 10147d5e3cca..fe73ade7ba24 100644
@@ -18,6 +18,18 @@ static int num_subflows __read_mostly = 2;
 module_param(num_subflows, int, 0644);
 MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connection");
 
+static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}
+
 /**
  * Create all new subflows, by doing calls to mptcp_initX_subsockets
  *
@@ -32,7 +44,7 @@ static void create_subflow_worker(struct work_struct *work)
 						     subflow_work);
 	struct mptcp_cb *mpcb = pm_priv->mpcb;
 	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
 
 next_subflow:
 	if (iter) {
@@ -53,7 +65,8 @@ static void create_subflow_worker(struct work_struct *work)
 	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
 		goto exit;
 
-	if (num_subflows > iter && num_subflows > mpcb->cnt_subflows) {
+	subflow_cnt = mptcp_ndiffports_subflow_count(mpcb);
+	if (num_subflows > iter && num_subflows > subflow_cnt) {
 		if (meta_sk->sk_family == AF_INET ||
 		    mptcp_v6_is_v4_mapped(meta_sk)) {
 			struct mptcp_loc4 loc;
net/mptcp/mptcp_output.c +1 -1
index fc950a98ce31..cc1a4f7d39e9 100644
@@ -1353,7 +1353,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
 	struct mptcp_cb *mpcb = meta_tp->mpcb;
 	struct sock *sk;
 
-	if (!mpcb->cnt_subflows)
+	if (!mpcb->connection_list)
 		return;
 
 	WARN_ON(meta_tp->send_mp_fclose);
net/mptcp/mptcp_sched.c +0 -11
index 2b10a6683c5e..c831515fd7f0 100644
@@ -219,14 +219,6 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
 	struct sock *sk;
 	bool force;
 
-	/* if there is only one subflow, bypass the scheduling function */
-	if (mpcb->cnt_subflows == 1) {
-		sk = (struct sock *)mpcb->connection_list;
-		if (!mptcp_is_available(sk, skb, zero_wnd_test))
-			sk = NULL;
-		return sk;
-	}
-
 	/* Answer data_fin on same subflow!!! */
 	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
 	    skb && mptcp_is_data_fin(skb)) {
@@ -268,9 +260,6 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
 	struct sk_buff *skb_head;
 	struct defsched_priv *dsp = defsched_get_priv(tp);
 
-	if (tp->mpcb->cnt_subflows == 1)
-		return NULL;
-
 	meta_sk = mptcp_meta_sk(sk);
 	skb_head = tcp_rtx_queue_head(meta_sk);
 
Signature · 1 line
2.16.2
#13 lore raw

Re: [MPTCP] [PATCH 07/12] mptcp: Remove cnt_subflows

Hi Christoph,

Quoted 2 lines · Christoph Paasch

Our goal is to make subflow establishment lockless. cnt_subflows would need to either become an atomic variable, or we can remove it.

Just out of curiosity, is it a big cost to have it atomic? That looks like a good idea to remove, it avoid duplicating data we can get differently.

Quoted 3 lines

Well, let's remove it! In the general case it is not needed at all. Some exotic schedulers need cnt_subflows, but we don't care if we add complexity in these schedulers.

On the other hand, that's not a big complexity to get this number, that's why we don't care adding this tiny bit more complexity :-)

Quoted 3 lines

One place where it was used is in the /proc/net/mptcp_net/mptcp output. We don't guarantee backwards compatibility, so we can just change the output. I will submit a follow-up patch for net-tools.

I agree that we don't guarantee backwards compatibility but this info is quite useful when debugging. Especially when someone discovering MPTCP wants to check if there is more than one subflow.

By chance, could we not count the number of subflows here without locking on each subflow? (Just with mptcp_for_each_sk(mpcb, sk_unused) i++;)

From my point of view, data from /proc/net/mptcp_net/mptcp doesn't need
to be accurate: if a new subflow has just been added/removed, that's fine if we missed that, we just don't want crashes and locking too much :)

Quoted 4 lines

Another place is mptcp_verif_dss_csum(). There the access was actually wrong. Simply because cnt_subflows > 1, it doesn't mean we have a working subflow. So, we fix that. I don't think this issue is big enough to backport it.

Can we not use 'cnt_established > 1' in the backports?

Quoted 4 lines

Finally mptcp_sched.c was also using cnt_subflows but merely for speeding up things to avoid going through the whole scheduler code when there is only one subflow. So, we can remove it and trade performance for a better design.

For this one, can we not check if there is more than one subflow to keep the fastpath? Anyway we will still have to iterate on each subflow just after I guess.

But in general, it is good to simplify the design!

Quoted 115 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h          |  1 -
  net/mptcp/mptcp_binder.c     | 17 +++++++++++++++--
  net/mptcp/mptcp_ctrl.c       | 16 ++++++----------
  net/mptcp/mptcp_input.c      | 11 ++++++++++-
  net/mptcp/mptcp_ndiffports.c | 17 +++++++++++++++--
  net/mptcp/mptcp_output.c     |  2 +-
  net/mptcp/mptcp_sched.c      | 11 -----------
  7 files changed, 47 insertions(+), 28 deletions(-)
include/net/mptcp.h +0 -1
index 647183460200..523abc8ce01d 100644
@@ -280,7 +280,6 @@ struct mptcp_cb {
  		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
 
  	/* socket count in this connection */
-	u8 cnt_subflows;
  	u8 cnt_established;
 
  #define MPTCP_SCHED_DATA_SIZE 8
net/mptcp/mptcp_binder.c +15 -2
index 58a342dc1620..d6ef9c5b6ce5 100644
@@ -287,6 +287,18 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
  	return -1;
  }
 
+static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}
+
  /**
   * Create all new subflows, by doing calls to mptcp_initX_subsockets
   *
@@ -301,7 +313,7 @@ static void create_subflow_worker(struct work_struct *work)
  						     subflow_work);
  	struct mptcp_cb *mpcb = pm_priv->mpcb;
  	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
 
  next_subflow:
  	if (iter) {
@@ -322,8 +334,9 @@ static void create_subflow_worker(struct work_struct *work)
  	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
  		goto exit;
 
+	subflow_cnt = mptcp_binder_subflow_count(mpcb);
  	if (mptcp_binder_ndiffports > iter &&
-	    mptcp_binder_ndiffports > mpcb->cnt_subflows) {
+	    mptcp_binder_ndiffports > subflow_cnt) {
  		struct mptcp_loc4 loc;
  		struct mptcp_rem4 rem;
 
net/mptcp/mptcp_ctrl.c +4 -8
index 321a8578e9c3..b1109ee9770f 100644
@@ -1379,7 +1379,6 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  	mpcb->connection_list = tp;
  	tp->mptcp->attached = 1;
 
-	mpcb->cnt_subflows++;
  	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
  		   &meta_sk->sk_rmem_alloc);
 
@@ -1399,23 +1398,21 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  	sk->sk_destruct = mptcp_sock_destruct;
 
  	if (sk->sk_family == AF_INET)
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d\n",
  			    __func__ , mpcb->mptcp_loc_token,
  			    tp->mptcp->path_index,
  			    &((struct inet_sock *)tp)->inet_saddr,
  			    ntohs(((struct inet_sock *)tp)->inet_sport),
  			    &((struct inet_sock *)tp)->inet_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
  #if IS_ENABLED(CONFIG_IPV6)
  	else
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d\n",
  			    __func__ , mpcb->mptcp_loc_token,
  			    tp->mptcp->path_index, &inet6_sk(sk)->saddr,
  			    ntohs(((struct inet_sock *)tp)->inet_sport),
  			    &sk->sk_v6_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
  #endif
 
  	return 0;
@@ -1452,7 +1449,6 @@ void mptcp_del_sock(struct sock *sk)
  			}
  		}
  	}
-	mpcb->cnt_subflows--;
  	if (tp->mptcp->establish_increased)
  		mpcb->cnt_established--;
 

net/mptcp/mptcp_ctrl.c static int mptcp_pm_seq_show(struct seq_file *seq, void *v) · line 2816

Quoted 7 lines
net/mptcp/mptcp_ctrl.c +2 -2
@@ -2816,8 +2812,8 @@ static int mptcp_pm_seq_show(struct seq_file *seq, void *v)
  					   ntohs(isk->inet_dport));
  #endif
  			}
-			seq_printf(seq, " %02X %02X %08X:%08X %lu",
-				   meta_sk->sk_state, mpcb->cnt_subflows,
+			seq_printf(seq, " %02X %08X:%08X %lu",
+				   meta_sk->sk_state,

You missed the update of the "header" at the line 2796 ;-) (to remove the 'ns' from there)

Quoted 41 lines
net/mptcp/mptcp_ctrl.c +0 -0
  				   meta_tp->write_seq - meta_tp->snd_una,
  				   max_t(int, meta_tp->rcv_nxt -
  					 meta_tp->copied_seq, 0),
net/mptcp/mptcp_input.c +10 -1
index ba9e3fe62824..126c961ec55a 100644
@@ -347,6 +347,8 @@ static int mptcp_verif_dss_csum(struct sock *sk)
 
  	/* Now, checksum must be 0 */
  	if (unlikely(csum_fold(csum_tcp))) {
+		struct sock *sk_it = NULL;
+
  		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
  		       __func__, csum_fold(csum_tcp), TCP_SKB_CB(last)->seq,
  		       dss_csum_added, overflowed, iter);
@@ -359,7 +361,14 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  		 */
  		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
 
-		if (tp->mpcb->cnt_subflows > 1) {
+		/* Search for another subflow that is fully established */
+		mptcp_for_each_sk(tp->mpcb, sk_it) {
+			if (sk_it != sk &&
+			    tcp_sk(sk_it)->mptcp->fully_established)
+				break;
+		}
+
+		if (sk_it) {
  			mptcp_send_reset(sk);
  			ans = -1;
  		} else {
net/mptcp/mptcp_ndiffports.c +1 -0
index 10147d5e3cca..fe73ade7ba24 100644
@@ -18,6 +18,18 @@ static int num_subflows __read_mostly = 2;
  module_param(num_subflows, int, 0644);
  MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connection");
 
+static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)

net/mptcp/mptcp_ndiffports.c static int num_subflows __read_mostly = 2; · line 31

Quoted 10 lines
net/mptcp/mptcp_ndiffports.c +10 -0
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}

Can we not add this function with a generic name in mptcp.h? (and use it here, in binder, maybe for /proc/net/mptcp_net/mptcp, etc.)

Cheers,
Matt

Quoted 66 lines
net/mptcp/mptcp_ndiffports.c +4 -2
+
  /**
   * Create all new subflows, by doing calls to mptcp_initX_subsockets
   *
@@ -32,7 +44,7 @@ static void create_subflow_worker(struct work_struct *work)
  						     subflow_work);
  	struct mptcp_cb *mpcb = pm_priv->mpcb;
  	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
 
  next_subflow:
  	if (iter) {
@@ -53,7 +65,8 @@ static void create_subflow_worker(struct work_struct *work)
  	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
  		goto exit;
 
-	if (num_subflows > iter && num_subflows > mpcb->cnt_subflows) {
+	subflow_cnt = mptcp_ndiffports_subflow_count(mpcb);
+	if (num_subflows > iter && num_subflows > subflow_cnt) {
  		if (meta_sk->sk_family == AF_INET ||
  		    mptcp_v6_is_v4_mapped(meta_sk)) {
  			struct mptcp_loc4 loc;
net/mptcp/mptcp_output.c +1 -1
index fc950a98ce31..cc1a4f7d39e9 100644
@@ -1353,7 +1353,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sock *sk;
 
-	if (!mpcb->cnt_subflows)
+	if (!mpcb->connection_list)
  		return;
 
  	WARN_ON(meta_tp->send_mp_fclose);
net/mptcp/mptcp_sched.c +0 -11
index 2b10a6683c5e..c831515fd7f0 100644
@@ -219,14 +219,6 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
  	struct sock *sk;
  	bool force;
 
-	/* if there is only one subflow, bypass the scheduling function */
-	if (mpcb->cnt_subflows == 1) {
-		sk = (struct sock *)mpcb->connection_list;
-		if (!mptcp_is_available(sk, skb, zero_wnd_test))
-			sk = NULL;
-		return sk;
-	}
-
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
@@ -268,9 +260,6 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  	struct sk_buff *skb_head;
  	struct defsched_priv *dsp = defsched_get_priv(tp);
 
-	if (tp->mpcb->cnt_subflows == 1)
-		return NULL;
-
  	meta_sk = mptcp_meta_sk(sk);
  	skb_head = tcp_rtx_queue_head(meta_sk);
 
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#14 lore raw

Re: [MPTCP] [PATCH 07/12] mptcp: Remove cnt_subflows

Hello,

Quoted 7 lines · Matthieu Baerts

Hi Christoph,

Quoted 2 lines · Christoph Paasch

Our goal is to make subflow establishment lockless. cnt_subflows would need to either become an atomic variable, or we can remove it.

Just out of curiosity, is it a big cost to have it atomic?

looking again at this patch and your comments, it's probably better to simply make it atomic.
I was a bit over-enthousiastic for removing code :-)

Sounds good?

Christoph

Quoted 293 lines

That looks like a good idea to remove, it avoid duplicating data we can get differently.

Quoted 3 lines

Well, let's remove it! In the general case it is not needed at all. Some exotic schedulers need cnt_subflows, but we don't care if we add complexity in these schedulers.

On the other hand, that's not a big complexity to get this number, that's why we don't care adding this tiny bit more complexity :-)

Quoted 3 lines

One place where it was used is in the /proc/net/mptcp_net/mptcp output. We don't guarantee backwards compatibility, so we can just change the output. I will submit a follow-up patch for net-tools.

I agree that we don't guarantee backwards compatibility but this info is quite useful when debugging. Especially when someone discovering MPTCP wants to check if there is more than one subflow.

By chance, could we not count the number of subflows here without locking on each subflow? (Just with mptcp_for_each_sk(mpcb, sk_unused) i++;)

From my point of view, data from /proc/net/mptcp_net/mptcp doesn't need to be accurate: if a new subflow has just been added/removed, that's fine if we missed that, we just don't want crashes and locking too much :)

Quoted 4 lines

Another place is mptcp_verif_dss_csum(). There the access was actually wrong. Simply because cnt_subflows > 1, it doesn't mean we have a working subflow. So, we fix that. I don't think this issue is big enough to backport it.

Can we not use 'cnt_established > 1' in the backports?

Quoted 4 lines

Finally mptcp_sched.c was also using cnt_subflows but merely for speeding up things to avoid going through the whole scheduler code when there is only one subflow. So, we can remove it and trade performance for a better design.

For this one, can we not check if there is more than one subflow to keep the fastpath? Anyway we will still have to iterate on each subflow just after I guess.

But in general, it is good to simplify the design!

Quoted 104 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h          |  1 -
  net/mptcp/mptcp_binder.c     | 17 +++++++++++++++--
  net/mptcp/mptcp_ctrl.c       | 16 ++++++----------
  net/mptcp/mptcp_input.c      | 11 ++++++++++-
  net/mptcp/mptcp_ndiffports.c | 17 +++++++++++++++--
  net/mptcp/mptcp_output.c     |  2 +-
  net/mptcp/mptcp_sched.c      | 11 -----------
  7 files changed, 47 insertions(+), 28 deletions(-)
include/net/mptcp.h +0 -1
index 647183460200..523abc8ce01d 100644
@@ -280,7 +280,6 @@ struct mptcp_cb {
  		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
  	/* socket count in this connection */
-	u8 cnt_subflows;
  	u8 cnt_established;
  #define MPTCP_SCHED_DATA_SIZE 8
net/mptcp/mptcp_binder.c +15 -2
index 58a342dc1620..d6ef9c5b6ce5 100644
@@ -287,6 +287,18 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
  	return -1;
  }
+static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}
+
  /**
   * Create all new subflows, by doing calls to mptcp_initX_subsockets
   *
@@ -301,7 +313,7 @@ static void create_subflow_worker(struct work_struct *work)
  						     subflow_work);
  	struct mptcp_cb *mpcb = pm_priv->mpcb;
  	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
  next_subflow:
  	if (iter) {
@@ -322,8 +334,9 @@ static void create_subflow_worker(struct work_struct *work)
  	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
  		goto exit;
+	subflow_cnt = mptcp_binder_subflow_count(mpcb);
  	if (mptcp_binder_ndiffports > iter &&
-	    mptcp_binder_ndiffports > mpcb->cnt_subflows) {
+	    mptcp_binder_ndiffports > subflow_cnt) {
  		struct mptcp_loc4 loc;
  		struct mptcp_rem4 rem;
net/mptcp/mptcp_ctrl.c +4 -8
index 321a8578e9c3..b1109ee9770f 100644
@@ -1379,7 +1379,6 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  	mpcb->connection_list = tp;
  	tp->mptcp->attached = 1;
-	mpcb->cnt_subflows++;
  	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
  		   &meta_sk->sk_rmem_alloc);
@@ -1399,23 +1398,21 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  	sk->sk_destruct = mptcp_sock_destruct;
  	if (sk->sk_family == AF_INET)
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d\n",
  			    __func__ , mpcb->mptcp_loc_token,
  			    tp->mptcp->path_index,
  			    &((struct inet_sock *)tp)->inet_saddr,
  			    ntohs(((struct inet_sock *)tp)->inet_sport),
  			    &((struct inet_sock *)tp)->inet_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
  #if IS_ENABLED(CONFIG_IPV6)
  	else
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d\n",
  			    __func__ , mpcb->mptcp_loc_token,
  			    tp->mptcp->path_index, &inet6_sk(sk)->saddr,
  			    ntohs(((struct inet_sock *)tp)->inet_sport),
  			    &sk->sk_v6_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
  #endif
  	return 0;
@@ -1452,7 +1449,6 @@ void mptcp_del_sock(struct sock *sk)
  			}
  		}
  	}
-	mpcb->cnt_subflows--;
  	if (tp->mptcp->establish_increased)
  		mpcb->cnt_established--;

net/mptcp/mptcp_ctrl.c static int mptcp_pm_seq_show(struct seq_file *seq, void *v) · line 2816

Quoted 7 lines
net/mptcp/mptcp_ctrl.c +2 -2
@@ -2816,8 +2812,8 @@ static int mptcp_pm_seq_show(struct seq_file *seq, void *v)
  					   ntohs(isk->inet_dport));
  #endif
  			}
-			seq_printf(seq, " %02X %02X %08X:%08X %lu",
-				   meta_sk->sk_state, mpcb->cnt_subflows,
+			seq_printf(seq, " %02X %08X:%08X %lu",
+				   meta_sk->sk_state,

You missed the update of the "header" at the line 2796 ;-) (to remove the 'ns' from there)

Quoted 38 lines
net/mptcp/mptcp_ctrl.c +0 -0
  				   meta_tp->write_seq - meta_tp->snd_una,
  				   max_t(int, meta_tp->rcv_nxt -
  					 meta_tp->copied_seq, 0),
net/mptcp/mptcp_input.c +10 -1
index ba9e3fe62824..126c961ec55a 100644
@@ -347,6 +347,8 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  	/* Now, checksum must be 0 */
  	if (unlikely(csum_fold(csum_tcp))) {
+		struct sock *sk_it = NULL;
+
  		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
  		       __func__, csum_fold(csum_tcp), TCP_SKB_CB(last)->seq,
  		       dss_csum_added, overflowed, iter);
@@ -359,7 +361,14 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  		 */
  		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
-		if (tp->mpcb->cnt_subflows > 1) {
+		/* Search for another subflow that is fully established */
+		mptcp_for_each_sk(tp->mpcb, sk_it) {
+			if (sk_it != sk &&
+			    tcp_sk(sk_it)->mptcp->fully_established)
+				break;
+		}
+
+		if (sk_it) {
  			mptcp_send_reset(sk);
  			ans = -1;
  		} else {
net/mptcp/mptcp_ndiffports.c +1 -0
index 10147d5e3cca..fe73ade7ba24 100644
@@ -18,6 +18,18 @@ static int num_subflows __read_mostly = 2;
  module_param(num_subflows, int, 0644);
  MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connection");
+static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)

net/mptcp/mptcp_ndiffports.c static int num_subflows __read_mostly = 2; · line 30

Quoted 10 lines
net/mptcp/mptcp_ndiffports.c +10 -0
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}

Can we not add this function with a generic name in mptcp.h? (and use it here, in binder, maybe for /proc/net/mptcp_net/mptcp, etc.)

Cheers,
Matt

Quoted 59 lines
net/mptcp/mptcp_ndiffports.c +4 -2
+
  /**
   * Create all new subflows, by doing calls to mptcp_initX_subsockets
   *
@@ -32,7 +44,7 @@ static void create_subflow_worker(struct work_struct *work)
  						     subflow_work);
  	struct mptcp_cb *mpcb = pm_priv->mpcb;
  	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
  next_subflow:
  	if (iter) {
@@ -53,7 +65,8 @@ static void create_subflow_worker(struct work_struct *work)
  	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
  		goto exit;
-	if (num_subflows > iter && num_subflows > mpcb->cnt_subflows) {
+	subflow_cnt = mptcp_ndiffports_subflow_count(mpcb);
+	if (num_subflows > iter && num_subflows > subflow_cnt) {
  		if (meta_sk->sk_family == AF_INET ||
  		    mptcp_v6_is_v4_mapped(meta_sk)) {
  			struct mptcp_loc4 loc;
net/mptcp/mptcp_output.c +1 -1
index fc950a98ce31..cc1a4f7d39e9 100644
@@ -1353,7 +1353,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sock *sk;
-	if (!mpcb->cnt_subflows)
+	if (!mpcb->connection_list)
  		return;
  	WARN_ON(meta_tp->send_mp_fclose);
net/mptcp/mptcp_sched.c +0 -11
index 2b10a6683c5e..c831515fd7f0 100644
@@ -219,14 +219,6 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
  	struct sock *sk;
  	bool force;
-	/* if there is only one subflow, bypass the scheduling function */
-	if (mpcb->cnt_subflows == 1) {
-		sk = (struct sock *)mpcb->connection_list;
-		if (!mptcp_is_available(sk, skb, zero_wnd_test))
-			sk = NULL;
-		return sk;
-	}
-
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
@@ -268,9 +260,6 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  	struct sk_buff *skb_head;
  	struct defsched_priv *dsp = defsched_get_priv(tp);
-	if (tp->mpcb->cnt_subflows == 1)
-		return NULL;
-
  	meta_sk = mptcp_meta_sk(sk);
  	skb_head = tcp_rtx_queue_head(meta_sk);
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
#15 lore raw

Re: [MPTCP] [PATCH 07/12] mptcp: Remove cnt_subflows

Quoted 9 lines · Christoph Paasch

Hello,

Quoted 6 lines · Matthieu Baerts

Hi Christoph,

Quoted 2 lines · Christoph Paasch

Our goal is to make subflow establishment lockless. cnt_subflows would need to either become an atomic variable, or we can remove it.

Quoted 7 lines
Quoted 1 line

Just out of curiosity, is it a big cost to have it atomic?

looking again at this patch and your comments, it's probably better to simply make it atomic.
I was a bit over-enthousiastic for removing code :-)

Sounds good?

I think it still makes sense to avoid duplicated info.

Here we can easily get the number subflows with a for-loop in a list. Except that in a later patch, it will be in a hlist I think. I don't know what's the cost of doing this check to count the number of sublows. If the cost is very low, why not removing cnt_subflows and call a function when we need the info?

Matt

Quoted 298 lines

Christoph

Quoted 293 lines

That looks like a good idea to remove, it avoid duplicating data we can get differently.

Quoted 3 lines

Well, let's remove it! In the general case it is not needed at all. Some exotic schedulers need cnt_subflows, but we don't care if we add complexity in these schedulers.

On the other hand, that's not a big complexity to get this number, that's why we don't care adding this tiny bit more complexity :-)

Quoted 3 lines

One place where it was used is in the /proc/net/mptcp_net/mptcp output. We don't guarantee backwards compatibility, so we can just change the output. I will submit a follow-up patch for net-tools.

I agree that we don't guarantee backwards compatibility but this info is quite useful when debugging. Especially when someone discovering MPTCP wants to check if there is more than one subflow.

By chance, could we not count the number of subflows here without locking on each subflow? (Just with mptcp_for_each_sk(mpcb, sk_unused) i++;)

From my point of view, data from /proc/net/mptcp_net/mptcp doesn't need to
be accurate: if a new subflow has just been added/removed, that's fine if we missed that, we just don't want crashes and locking too much :)

Quoted 4 lines

Another place is mptcp_verif_dss_csum(). There the access was actually wrong. Simply because cnt_subflows > 1, it doesn't mean we have a working subflow. So, we fix that. I don't think this issue is big enough to backport it.

Can we not use 'cnt_established > 1' in the backports?

Quoted 4 lines

Finally mptcp_sched.c was also using cnt_subflows but merely for speeding up things to avoid going through the whole scheduler code when there is only one subflow. So, we can remove it and trade performance for a better design.

For this one, can we not check if there is more than one subflow to keep the fastpath? Anyway we will still have to iterate on each subflow just after I guess.

But in general, it is good to simplify the design!

Quoted 104 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
   include/net/mptcp.h          |  1 -
   net/mptcp/mptcp_binder.c     | 17 +++++++++++++++--
   net/mptcp/mptcp_ctrl.c       | 16 ++++++----------
   net/mptcp/mptcp_input.c      | 11 ++++++++++-
   net/mptcp/mptcp_ndiffports.c | 17 +++++++++++++++--
   net/mptcp/mptcp_output.c     |  2 +-
   net/mptcp/mptcp_sched.c      | 11 -----------
   7 files changed, 47 insertions(+), 28 deletions(-)
include/net/mptcp.h +0 -1
index 647183460200..523abc8ce01d 100644
@@ -280,7 +280,6 @@ struct mptcp_cb {
   		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
   	/* socket count in this connection */
-	u8 cnt_subflows;
   	u8 cnt_established;
   #define MPTCP_SCHED_DATA_SIZE 8
net/mptcp/mptcp_binder.c +15 -2
index 58a342dc1620..d6ef9c5b6ce5 100644
@@ -287,6 +287,18 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
   	return -1;
   }
+static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}
+
   /**
    * Create all new subflows, by doing calls to mptcp_initX_subsockets
    *
@@ -301,7 +313,7 @@ static void create_subflow_worker(struct work_struct *work)
   						     subflow_work);
   	struct mptcp_cb *mpcb = pm_priv->mpcb;
   	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
   next_subflow:
   	if (iter) {
@@ -322,8 +334,9 @@ static void create_subflow_worker(struct work_struct *work)
   	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
   		goto exit;
+	subflow_cnt = mptcp_binder_subflow_count(mpcb);
   	if (mptcp_binder_ndiffports > iter &&
-	    mptcp_binder_ndiffports > mpcb->cnt_subflows) {
+	    mptcp_binder_ndiffports > subflow_cnt) {
   		struct mptcp_loc4 loc;
   		struct mptcp_rem4 rem;
net/mptcp/mptcp_ctrl.c +4 -8
index 321a8578e9c3..b1109ee9770f 100644
@@ -1379,7 +1379,6 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
   	mpcb->connection_list = tp;
   	tp->mptcp->attached = 1;
-	mpcb->cnt_subflows++;
   	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
   		   &meta_sk->sk_rmem_alloc);
@@ -1399,23 +1398,21 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
   	sk->sk_destruct = mptcp_sock_destruct;
   	if (sk->sk_family == AF_INET)
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d\n",
   			    __func__ , mpcb->mptcp_loc_token,
   			    tp->mptcp->path_index,
   			    &((struct inet_sock *)tp)->inet_saddr,
   			    ntohs(((struct inet_sock *)tp)->inet_sport),
   			    &((struct inet_sock *)tp)->inet_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
   #if IS_ENABLED(CONFIG_IPV6)
   	else
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d\n",
   			    __func__ , mpcb->mptcp_loc_token,
   			    tp->mptcp->path_index, &inet6_sk(sk)->saddr,
   			    ntohs(((struct inet_sock *)tp)->inet_sport),
   			    &sk->sk_v6_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
   #endif
   	return 0;
@@ -1452,7 +1449,6 @@ void mptcp_del_sock(struct sock *sk)
   			}
   		}
   	}
-	mpcb->cnt_subflows--;
   	if (tp->mptcp->establish_increased)
   		mpcb->cnt_established--;

net/mptcp/mptcp_ctrl.c static int mptcp_pm_seq_show(struct seq_file *seq, void *v) · line 2816

Quoted 7 lines
net/mptcp/mptcp_ctrl.c +2 -2
@@ -2816,8 +2812,8 @@ static int mptcp_pm_seq_show(struct seq_file *seq, void *v)
   					   ntohs(isk->inet_dport));
   #endif
   			}
-			seq_printf(seq, " %02X %02X %08X:%08X %lu",
-				   meta_sk->sk_state, mpcb->cnt_subflows,
+			seq_printf(seq, " %02X %08X:%08X %lu",
+				   meta_sk->sk_state,

You missed the update of the "header" at the line 2796 ;-) (to remove the 'ns' from there)

Quoted 38 lines
net/mptcp/mptcp_ctrl.c +0 -0
   				   meta_tp->write_seq - meta_tp->snd_una,
   				   max_t(int, meta_tp->rcv_nxt -
   					 meta_tp->copied_seq, 0),
net/mptcp/mptcp_input.c +10 -1
index ba9e3fe62824..126c961ec55a 100644
@@ -347,6 +347,8 @@ static int mptcp_verif_dss_csum(struct sock *sk)
   	/* Now, checksum must be 0 */
   	if (unlikely(csum_fold(csum_tcp))) {
+		struct sock *sk_it = NULL;
+
   		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
   		       __func__, csum_fold(csum_tcp), TCP_SKB_CB(last)->seq,
   		       dss_csum_added, overflowed, iter);
@@ -359,7 +361,14 @@ static int mptcp_verif_dss_csum(struct sock *sk)
   		 */
   		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
-		if (tp->mpcb->cnt_subflows > 1) {
+		/* Search for another subflow that is fully established */
+		mptcp_for_each_sk(tp->mpcb, sk_it) {
+			if (sk_it != sk &&
+			    tcp_sk(sk_it)->mptcp->fully_established)
+				break;
+		}
+
+		if (sk_it) {
   			mptcp_send_reset(sk);
   			ans = -1;
   		} else {
net/mptcp/mptcp_ndiffports.c +1 -0
index 10147d5e3cca..fe73ade7ba24 100644
@@ -18,6 +18,18 @@ static int num_subflows __read_mostly = 2;
   module_param(num_subflows, int, 0644);
   MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connection");
+static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)

net/mptcp/mptcp_ndiffports.c static int num_subflows __read_mostly = 2; · line 30

Quoted 10 lines
net/mptcp/mptcp_ndiffports.c +10 -0
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}

Can we not add this function with a generic name in mptcp.h? (and use it here, in binder, maybe for /proc/net/mptcp_net/mptcp, etc.)

Cheers,
Matt

Quoted 59 lines
net/mptcp/mptcp_ndiffports.c +4 -2
+
   /**
    * Create all new subflows, by doing calls to mptcp_initX_subsockets
    *
@@ -32,7 +44,7 @@ static void create_subflow_worker(struct work_struct *work)
   						     subflow_work);
   	struct mptcp_cb *mpcb = pm_priv->mpcb;
   	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
   next_subflow:
   	if (iter) {
@@ -53,7 +65,8 @@ static void create_subflow_worker(struct work_struct *work)
   	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
   		goto exit;
-	if (num_subflows > iter && num_subflows > mpcb->cnt_subflows) {
+	subflow_cnt = mptcp_ndiffports_subflow_count(mpcb);
+	if (num_subflows > iter && num_subflows > subflow_cnt) {
   		if (meta_sk->sk_family == AF_INET ||
   		    mptcp_v6_is_v4_mapped(meta_sk)) {
   			struct mptcp_loc4 loc;
net/mptcp/mptcp_output.c +1 -1
index fc950a98ce31..cc1a4f7d39e9 100644
@@ -1353,7 +1353,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
   	struct mptcp_cb *mpcb = meta_tp->mpcb;
   	struct sock *sk;
-	if (!mpcb->cnt_subflows)
+	if (!mpcb->connection_list)
   		return;
   	WARN_ON(meta_tp->send_mp_fclose);
net/mptcp/mptcp_sched.c +0 -11
index 2b10a6683c5e..c831515fd7f0 100644
@@ -219,14 +219,6 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
   	struct sock *sk;
   	bool force;
-	/* if there is only one subflow, bypass the scheduling function */
-	if (mpcb->cnt_subflows == 1) {
-		sk = (struct sock *)mpcb->connection_list;
-		if (!mptcp_is_available(sk, skb, zero_wnd_test))
-			sk = NULL;
-		return sk;
-	}
-
   	/* Answer data_fin on same subflow!!! */
   	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
   	    skb && mptcp_is_data_fin(skb)) {
@@ -268,9 +260,6 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
   	struct sk_buff *skb_head;
   	struct defsched_priv *dsp = defsched_get_priv(tp);
-	if (tp->mpcb->cnt_subflows == 1)
-		return NULL;
-
   	meta_sk = mptcp_meta_sk(sk);
   	skb_head = tcp_rtx_queue_head(meta_sk);
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#16 lore raw

Re: [MPTCP] [PATCH 07/12] mptcp: Remove cnt_subflows

Quoted 20 lines · Matthieu Baerts
Quoted 9 lines · Christoph Paasch

Hello,

Quoted 6 lines · Matthieu Baerts

Hi Christoph,

Quoted 2 lines · Christoph Paasch

Our goal is to make subflow establishment lockless. cnt_subflows would need to either become an atomic variable, or we can remove it.

Quoted 7 lines
Quoted 1 line

Just out of curiosity, is it a big cost to have it atomic?

looking again at this patch and your comments, it's probably better to simply make it atomic.
I was a bit over-enthousiastic for removing code :-)

Sounds good?

Quoted 7 lines

I think it still makes sense to avoid duplicated info.

Here we can easily get the number subflows with a for-loop in a list. Except that in a later patch, it will be in a hlist I think. I don't know what's the cost of doing this check to count the number of sublows. If the cost is very low, why not removing cnt_subflows and call a function when we need the info?

Hmmm... Well, honestly I'm not sure about what is the best design here :)

Let's do the following. I export a function to count the number of subflows and then when it's needed we call that one. Also, without any changes to /proc/

Christoph

Quoted 324 lines

Matt

Quoted 298 lines

Christoph

Quoted 293 lines

That looks like a good idea to remove, it avoid duplicating data we can get differently.

Quoted 3 lines

Well, let's remove it! In the general case it is not needed at all. Some exotic schedulers need cnt_subflows, but we don't care if we add complexity in these schedulers.

On the other hand, that's not a big complexity to get this number, that's why we don't care adding this tiny bit more complexity :-)

Quoted 3 lines

One place where it was used is in the /proc/net/mptcp_net/mptcp output. We don't guarantee backwards compatibility, so we can just change the output. I will submit a follow-up patch for net-tools.

I agree that we don't guarantee backwards compatibility but this info is quite useful when debugging. Especially when someone discovering MPTCP wants to check if there is more than one subflow.

By chance, could we not count the number of subflows here without locking on each subflow? (Just with mptcp_for_each_sk(mpcb, sk_unused) i++;)

From my point of view, data from /proc/net/mptcp_net/mptcp doesn't need to
be accurate: if a new subflow has just been added/removed, that's fine if we missed that, we just don't want crashes and locking too much :)

Quoted 4 lines

Another place is mptcp_verif_dss_csum(). There the access was actually wrong. Simply because cnt_subflows > 1, it doesn't mean we have a working subflow. So, we fix that. I don't think this issue is big enough to backport it.

Can we not use 'cnt_established > 1' in the backports?

Quoted 4 lines

Finally mptcp_sched.c was also using cnt_subflows but merely for speeding up things to avoid going through the whole scheduler code when there is only one subflow. So, we can remove it and trade performance for a better design.

For this one, can we not check if there is more than one subflow to keep the fastpath? Anyway we will still have to iterate on each subflow just after I guess.

But in general, it is good to simplify the design!

Quoted 104 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
   include/net/mptcp.h          |  1 -
   net/mptcp/mptcp_binder.c     | 17 +++++++++++++++--
   net/mptcp/mptcp_ctrl.c       | 16 ++++++----------
   net/mptcp/mptcp_input.c      | 11 ++++++++++-
   net/mptcp/mptcp_ndiffports.c | 17 +++++++++++++++--
   net/mptcp/mptcp_output.c     |  2 +-
   net/mptcp/mptcp_sched.c      | 11 -----------
   7 files changed, 47 insertions(+), 28 deletions(-)
include/net/mptcp.h +0 -1
index 647183460200..523abc8ce01d 100644
@@ -280,7 +280,6 @@ struct mptcp_cb {
   		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
   	/* socket count in this connection */
-	u8 cnt_subflows;
   	u8 cnt_established;
   #define MPTCP_SCHED_DATA_SIZE 8
net/mptcp/mptcp_binder.c +15 -2
index 58a342dc1620..d6ef9c5b6ce5 100644
@@ -287,6 +287,18 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
   	return -1;
   }
+static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}
+
   /**
    * Create all new subflows, by doing calls to mptcp_initX_subsockets
    *
@@ -301,7 +313,7 @@ static void create_subflow_worker(struct work_struct *work)
   						     subflow_work);
   	struct mptcp_cb *mpcb = pm_priv->mpcb;
   	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
   next_subflow:
   	if (iter) {
@@ -322,8 +334,9 @@ static void create_subflow_worker(struct work_struct *work)
   	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
   		goto exit;
+	subflow_cnt = mptcp_binder_subflow_count(mpcb);
   	if (mptcp_binder_ndiffports > iter &&
-	    mptcp_binder_ndiffports > mpcb->cnt_subflows) {
+	    mptcp_binder_ndiffports > subflow_cnt) {
   		struct mptcp_loc4 loc;
   		struct mptcp_rem4 rem;
net/mptcp/mptcp_ctrl.c +4 -8
index 321a8578e9c3..b1109ee9770f 100644
@@ -1379,7 +1379,6 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
   	mpcb->connection_list = tp;
   	tp->mptcp->attached = 1;
-	mpcb->cnt_subflows++;
   	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
   		   &meta_sk->sk_rmem_alloc);
@@ -1399,23 +1398,21 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
   	sk->sk_destruct = mptcp_sock_destruct;
   	if (sk->sk_family == AF_INET)
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d\n",
   			    __func__ , mpcb->mptcp_loc_token,
   			    tp->mptcp->path_index,
   			    &((struct inet_sock *)tp)->inet_saddr,
   			    ntohs(((struct inet_sock *)tp)->inet_sport),
   			    &((struct inet_sock *)tp)->inet_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
   #if IS_ENABLED(CONFIG_IPV6)
   	else
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d\n",
   			    __func__ , mpcb->mptcp_loc_token,
   			    tp->mptcp->path_index, &inet6_sk(sk)->saddr,
   			    ntohs(((struct inet_sock *)tp)->inet_sport),
   			    &sk->sk_v6_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
   #endif
   	return 0;
@@ -1452,7 +1449,6 @@ void mptcp_del_sock(struct sock *sk)
   			}
   		}
   	}
-	mpcb->cnt_subflows--;
   	if (tp->mptcp->establish_increased)
   		mpcb->cnt_established--;

net/mptcp/mptcp_ctrl.c static int mptcp_pm_seq_show(struct seq_file *seq, void *v) · line 2816

Quoted 7 lines
net/mptcp/mptcp_ctrl.c +2 -2
@@ -2816,8 +2812,8 @@ static int mptcp_pm_seq_show(struct seq_file *seq, void *v)
   					   ntohs(isk->inet_dport));
   #endif
   			}
-			seq_printf(seq, " %02X %02X %08X:%08X %lu",
-				   meta_sk->sk_state, mpcb->cnt_subflows,
+			seq_printf(seq, " %02X %08X:%08X %lu",
+				   meta_sk->sk_state,

You missed the update of the "header" at the line 2796 ;-) (to remove the 'ns' from there)

Quoted 38 lines
net/mptcp/mptcp_ctrl.c +0 -0
   				   meta_tp->write_seq - meta_tp->snd_una,
   				   max_t(int, meta_tp->rcv_nxt -
   					 meta_tp->copied_seq, 0),
net/mptcp/mptcp_input.c +10 -1
index ba9e3fe62824..126c961ec55a 100644
@@ -347,6 +347,8 @@ static int mptcp_verif_dss_csum(struct sock *sk)
   	/* Now, checksum must be 0 */
   	if (unlikely(csum_fold(csum_tcp))) {
+		struct sock *sk_it = NULL;
+
   		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
   		       __func__, csum_fold(csum_tcp), TCP_SKB_CB(last)->seq,
   		       dss_csum_added, overflowed, iter);
@@ -359,7 +361,14 @@ static int mptcp_verif_dss_csum(struct sock *sk)
   		 */
   		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
-		if (tp->mpcb->cnt_subflows > 1) {
+		/* Search for another subflow that is fully established */
+		mptcp_for_each_sk(tp->mpcb, sk_it) {
+			if (sk_it != sk &&
+			    tcp_sk(sk_it)->mptcp->fully_established)
+				break;
+		}
+
+		if (sk_it) {
   			mptcp_send_reset(sk);
   			ans = -1;
   		} else {
net/mptcp/mptcp_ndiffports.c +1 -0
index 10147d5e3cca..fe73ade7ba24 100644
@@ -18,6 +18,18 @@ static int num_subflows __read_mostly = 2;
   module_param(num_subflows, int, 0644);
   MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connection");
+static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)

net/mptcp/mptcp_ndiffports.c static int num_subflows __read_mostly = 2; · line 30

Quoted 10 lines
net/mptcp/mptcp_ndiffports.c +10 -0
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}

Can we not add this function with a generic name in mptcp.h? (and use it here, in binder, maybe for /proc/net/mptcp_net/mptcp, etc.)

Cheers,
Matt

Quoted 59 lines
net/mptcp/mptcp_ndiffports.c +4 -2
+
   /**
    * Create all new subflows, by doing calls to mptcp_initX_subsockets
    *
@@ -32,7 +44,7 @@ static void create_subflow_worker(struct work_struct *work)
   						     subflow_work);
   	struct mptcp_cb *mpcb = pm_priv->mpcb;
   	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
   next_subflow:
   	if (iter) {
@@ -53,7 +65,8 @@ static void create_subflow_worker(struct work_struct *work)
   	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
   		goto exit;
-	if (num_subflows > iter && num_subflows > mpcb->cnt_subflows) {
+	subflow_cnt = mptcp_ndiffports_subflow_count(mpcb);
+	if (num_subflows > iter && num_subflows > subflow_cnt) {
   		if (meta_sk->sk_family == AF_INET ||
   		    mptcp_v6_is_v4_mapped(meta_sk)) {
   			struct mptcp_loc4 loc;
net/mptcp/mptcp_output.c +1 -1
index fc950a98ce31..cc1a4f7d39e9 100644
@@ -1353,7 +1353,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
   	struct mptcp_cb *mpcb = meta_tp->mpcb;
   	struct sock *sk;
-	if (!mpcb->cnt_subflows)
+	if (!mpcb->connection_list)
   		return;
   	WARN_ON(meta_tp->send_mp_fclose);
net/mptcp/mptcp_sched.c +0 -11
index 2b10a6683c5e..c831515fd7f0 100644
@@ -219,14 +219,6 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
   	struct sock *sk;
   	bool force;
-	/* if there is only one subflow, bypass the scheduling function */
-	if (mpcb->cnt_subflows == 1) {
-		sk = (struct sock *)mpcb->connection_list;
-		if (!mptcp_is_available(sk, skb, zero_wnd_test))
-			sk = NULL;
-		return sk;
-	}
-
   	/* Answer data_fin on same subflow!!! */
   	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
   	    skb && mptcp_is_data_fin(skb)) {
@@ -268,9 +260,6 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
   	struct sk_buff *skb_head;
   	struct defsched_priv *dsp = defsched_get_priv(tp);
-	if (tp->mpcb->cnt_subflows == 1)
-		return NULL;
-
   	meta_sk = mptcp_meta_sk(sk);
   	skb_head = tcp_rtx_queue_head(meta_sk);
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
#17 lore raw

Re: [MPTCP] [PATCH 07/12] mptcp: Remove cnt_subflows

Hi Christoph,

Quoted 29 lines · Christoph Paasch
Quoted 20 lines · Matthieu Baerts
Quoted 9 lines · Christoph Paasch

Hello,

Quoted 6 lines · Matthieu Baerts

Hi Christoph,

Quoted 2 lines · Christoph Paasch

Our goal is to make subflow establishment lockless. cnt_subflows would need to either become an atomic variable, or we can remove it.

Quoted 7 lines
Quoted 1 line

Just out of curiosity, is it a big cost to have it atomic?

looking again at this patch and your comments, it's probably better to simply make it atomic.
I was a bit over-enthousiastic for removing code :-)

Sounds good?

Quoted 7 lines

I think it still makes sense to avoid duplicated info.

Here we can easily get the number subflows with a for-loop in a list. Except that in a later patch, it will be in a hlist I think. I don't know what's the cost of doing this check to count the number of sublows. If the cost is very low, why not removing cnt_subflows and call a function when we need the info?

Quoted 5 lines

Hmmm... Well, honestly I'm not sure about what is the best design here :)

Let's do the following. I export a function to count the number of subflows and then when it's needed we call that one. Also, without any changes to /proc/

Great, fine for me! :-)

Matt

Quoted 327 lines

Christoph

Quoted 324 lines

Matt

Quoted 298 lines

Christoph

Quoted 293 lines

That looks like a good idea to remove, it avoid duplicating data we can get differently.

Quoted 3 lines

Well, let's remove it! In the general case it is not needed at all. Some exotic schedulers need cnt_subflows, but we don't care if we add complexity in these schedulers.

On the other hand, that's not a big complexity to get this number, that's why we don't care adding this tiny bit more complexity :-)

Quoted 3 lines

One place where it was used is in the /proc/net/mptcp_net/mptcp output. We don't guarantee backwards compatibility, so we can just change the output. I will submit a follow-up patch for net-tools.

I agree that we don't guarantee backwards compatibility but this info is quite useful when debugging. Especially when someone discovering MPTCP wants to check if there is more than one subflow.

By chance, could we not count the number of subflows here without locking on each subflow? (Just with mptcp_for_each_sk(mpcb, sk_unused) i++;)

From my point of view, data from /proc/net/mptcp_net/mptcp doesn't need to
be accurate: if a new subflow has just been added/removed, that's fine if we missed that, we just don't want crashes and locking too much :)

Quoted 4 lines

Another place is mptcp_verif_dss_csum(). There the access was actually wrong. Simply because cnt_subflows > 1, it doesn't mean we have a working subflow. So, we fix that. I don't think this issue is big enough to backport it.

Can we not use 'cnt_established > 1' in the backports?

Quoted 4 lines

Finally mptcp_sched.c was also using cnt_subflows but merely for speeding up things to avoid going through the whole scheduler code when there is only one subflow. So, we can remove it and trade performance for a better design.

For this one, can we not check if there is more than one subflow to keep the fastpath? Anyway we will still have to iterate on each subflow just after I guess.

But in general, it is good to simplify the design!

Quoted 104 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
   include/net/mptcp.h          |  1 -
   net/mptcp/mptcp_binder.c     | 17 +++++++++++++++--
   net/mptcp/mptcp_ctrl.c       | 16 ++++++----------
   net/mptcp/mptcp_input.c      | 11 ++++++++++-
   net/mptcp/mptcp_ndiffports.c | 17 +++++++++++++++--
   net/mptcp/mptcp_output.c     |  2 +-
   net/mptcp/mptcp_sched.c      | 11 -----------
   7 files changed, 47 insertions(+), 28 deletions(-)
include/net/mptcp.h +0 -1
index 647183460200..523abc8ce01d 100644
@@ -280,7 +280,6 @@ struct mptcp_cb {
   		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
   	/* socket count in this connection */
-	u8 cnt_subflows;
   	u8 cnt_established;
   #define MPTCP_SCHED_DATA_SIZE 8
net/mptcp/mptcp_binder.c +15 -2
index 58a342dc1620..d6ef9c5b6ce5 100644
@@ -287,6 +287,18 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
   	return -1;
   }
+static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}
+
   /**
    * Create all new subflows, by doing calls to mptcp_initX_subsockets
    *
@@ -301,7 +313,7 @@ static void create_subflow_worker(struct work_struct *work)
   						     subflow_work);
   	struct mptcp_cb *mpcb = pm_priv->mpcb;
   	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
   next_subflow:
   	if (iter) {
@@ -322,8 +334,9 @@ static void create_subflow_worker(struct work_struct *work)
   	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
   		goto exit;
+	subflow_cnt = mptcp_binder_subflow_count(mpcb);
   	if (mptcp_binder_ndiffports > iter &&
-	    mptcp_binder_ndiffports > mpcb->cnt_subflows) {
+	    mptcp_binder_ndiffports > subflow_cnt) {
   		struct mptcp_loc4 loc;
   		struct mptcp_rem4 rem;
net/mptcp/mptcp_ctrl.c +4 -8
index 321a8578e9c3..b1109ee9770f 100644
@@ -1379,7 +1379,6 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
   	mpcb->connection_list = tp;
   	tp->mptcp->attached = 1;
-	mpcb->cnt_subflows++;
   	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
   		   &meta_sk->sk_rmem_alloc);
@@ -1399,23 +1398,21 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
   	sk->sk_destruct = mptcp_sock_destruct;
   	if (sk->sk_family == AF_INET)
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI4:%d dst_addr:%pI4:%d\n",
   			    __func__ , mpcb->mptcp_loc_token,
   			    tp->mptcp->path_index,
   			    &((struct inet_sock *)tp)->inet_saddr,
   			    ntohs(((struct inet_sock *)tp)->inet_sport),
   			    &((struct inet_sock *)tp)->inet_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
   #if IS_ENABLED(CONFIG_IPV6)
   	else
-		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d, cnt_subflows now %d\n",
+		mptcp_debug("%s: token %#x pi %d, src_addr:%pI6:%d dst_addr:%pI6:%d\n",
   			    __func__ , mpcb->mptcp_loc_token,
   			    tp->mptcp->path_index, &inet6_sk(sk)->saddr,
   			    ntohs(((struct inet_sock *)tp)->inet_sport),
   			    &sk->sk_v6_daddr,
-			    ntohs(((struct inet_sock *)tp)->inet_dport),
-			    mpcb->cnt_subflows);
+			    ntohs(((struct inet_sock *)tp)->inet_dport));
   #endif
   	return 0;
@@ -1452,7 +1449,6 @@ void mptcp_del_sock(struct sock *sk)
   			}
   		}
   	}
-	mpcb->cnt_subflows--;
   	if (tp->mptcp->establish_increased)
   		mpcb->cnt_established--;

net/mptcp/mptcp_ctrl.c static int mptcp_pm_seq_show(struct seq_file *seq, void *v) · line 2816

Quoted 7 lines
net/mptcp/mptcp_ctrl.c +2 -2
@@ -2816,8 +2812,8 @@ static int mptcp_pm_seq_show(struct seq_file *seq, void *v)
   					   ntohs(isk->inet_dport));
   #endif
   			}
-			seq_printf(seq, " %02X %02X %08X:%08X %lu",
-				   meta_sk->sk_state, mpcb->cnt_subflows,
+			seq_printf(seq, " %02X %08X:%08X %lu",
+				   meta_sk->sk_state,

You missed the update of the "header" at the line 2796 ;-) (to remove the 'ns' from there)

Quoted 38 lines
net/mptcp/mptcp_ctrl.c +0 -0
   				   meta_tp->write_seq - meta_tp->snd_una,
   				   max_t(int, meta_tp->rcv_nxt -
   					 meta_tp->copied_seq, 0),
net/mptcp/mptcp_input.c +10 -1
index ba9e3fe62824..126c961ec55a 100644
@@ -347,6 +347,8 @@ static int mptcp_verif_dss_csum(struct sock *sk)
   	/* Now, checksum must be 0 */
   	if (unlikely(csum_fold(csum_tcp))) {
+		struct sock *sk_it = NULL;
+
   		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
   		       __func__, csum_fold(csum_tcp), TCP_SKB_CB(last)->seq,
   		       dss_csum_added, overflowed, iter);
@@ -359,7 +361,14 @@ static int mptcp_verif_dss_csum(struct sock *sk)
   		 */
   		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
-		if (tp->mpcb->cnt_subflows > 1) {
+		/* Search for another subflow that is fully established */
+		mptcp_for_each_sk(tp->mpcb, sk_it) {
+			if (sk_it != sk &&
+			    tcp_sk(sk_it)->mptcp->fully_established)
+				break;
+		}
+
+		if (sk_it) {
   			mptcp_send_reset(sk);
   			ans = -1;
   		} else {
net/mptcp/mptcp_ndiffports.c +1 -0
index 10147d5e3cca..fe73ade7ba24 100644
@@ -18,6 +18,18 @@ static int num_subflows __read_mostly = 2;
   module_param(num_subflows, int, 0644);
   MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connection");
+static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)

net/mptcp/mptcp_ndiffports.c static int num_subflows __read_mostly = 2; · line 30

Quoted 10 lines
net/mptcp/mptcp_ndiffports.c +10 -0
+{
+	struct sock *sk;
+	int i = 0;
+
+	mptcp_for_each_sk(mpcb, sk) {
+		i++;
+	}
+
+	return i;
+}

Can we not add this function with a generic name in mptcp.h? (and use it here, in binder, maybe for /proc/net/mptcp_net/mptcp, etc.)

Cheers,
Matt

Quoted 59 lines
net/mptcp/mptcp_ndiffports.c +4 -2
+
   /**
    * Create all new subflows, by doing calls to mptcp_initX_subsockets
    *
@@ -32,7 +44,7 @@ static void create_subflow_worker(struct work_struct *work)
   						     subflow_work);
   	struct mptcp_cb *mpcb = pm_priv->mpcb;
   	struct sock *meta_sk = mpcb->meta_sk;
-	int iter = 0;
+	int iter = 0, subflow_cnt;
   next_subflow:
   	if (iter) {
@@ -53,7 +65,8 @@ static void create_subflow_worker(struct work_struct *work)
   	    !tcp_sk(mpcb->master_sk)->mptcp->fully_established)
   		goto exit;
-	if (num_subflows > iter && num_subflows > mpcb->cnt_subflows) {
+	subflow_cnt = mptcp_ndiffports_subflow_count(mpcb);
+	if (num_subflows > iter && num_subflows > subflow_cnt) {
   		if (meta_sk->sk_family == AF_INET ||
   		    mptcp_v6_is_v4_mapped(meta_sk)) {
   			struct mptcp_loc4 loc;
net/mptcp/mptcp_output.c +1 -1
index fc950a98ce31..cc1a4f7d39e9 100644
@@ -1353,7 +1353,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
   	struct mptcp_cb *mpcb = meta_tp->mpcb;
   	struct sock *sk;
-	if (!mpcb->cnt_subflows)
+	if (!mpcb->connection_list)
   		return;
   	WARN_ON(meta_tp->send_mp_fclose);
net/mptcp/mptcp_sched.c +0 -11
index 2b10a6683c5e..c831515fd7f0 100644
@@ -219,14 +219,6 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
   	struct sock *sk;
   	bool force;
-	/* if there is only one subflow, bypass the scheduling function */
-	if (mpcb->cnt_subflows == 1) {
-		sk = (struct sock *)mpcb->connection_list;
-		if (!mptcp_is_available(sk, skb, zero_wnd_test))
-			sk = NULL;
-		return sk;
-	}
-
   	/* Answer data_fin on same subflow!!! */
   	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
   	    skb && mptcp_is_data_fin(skb)) {
@@ -268,9 +260,6 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
   	struct sk_buff *skb_head;
   	struct defsched_priv *dsp = defsched_get_priv(tp);
-	if (tp->mpcb->cnt_subflows == 1)
-		return NULL;
-
   	meta_sk = mptcp_meta_sk(sk);
   	skb_head = tcp_rtx_queue_head(meta_sk);
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#18 lore raw

PATCH 8/12 mptcp: Remove cnt_established

In reply to Christoph Paasch

We want to remove the number of up-calls into the meta-socket. cnt_established is one of those where we are modifying a field in the mpcb.

We can remove it fairly safely. It's only use is in some MPTCP-specific congestion controls to short-circuit some parts of it.

One particular use was in mptcp_should_expand_sndbuf(). There, we use it to see whether to expand the sndbuf when there are only backup subflows available.

One problem with this old code is that the decision whether or not to send on a backup subflow is steered by the modular scheduler. We cannot assume in core-MPTCP code that backup subflows are never being sent on.

By no more taking backup subflows into account, we greatly simplify mptcp_should_expand_sndbuf at the cost of potentially increasing the window even if there is no subflow ready to send because it's a backup subflow.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 include/net/mptcp.h       |  4 ----
 net/mptcp/mctcp_desync.c  |  4 ++--
 net/mptcp/mptcp_balia.c   | 13 ++-----------
 net/mptcp/mptcp_coupled.c | 41 +++++++++++++++--------------------------
 net/mptcp/mptcp_ctrl.c    |  8 --------
 net/mptcp/mptcp_input.c   | 20 ++------------------
 net/mptcp/mptcp_olia.c    |  8 +++++---
 7 files changed, 26 insertions(+), 72 deletions(-)
include/net/mptcp.h +0 -4
index 523abc8ce01d..758f2038e7cf 100644
@@ -166,7 +166,6 @@ struct mptcp_tcp_sock {
 	u16	map_data_len;
 	u16	slave_sk:1,
 		fully_established:1,
-		establish_increased:1,
 		second_packet:1,
 		attached:1,
 		send_mp_fail:1,
@@ -279,9 +278,6 @@ struct mptcp_cb {
 		snd_hiseq_index:1, /* Index in snd_high_order of snd_nxt */
 		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
 
-	/* socket count in this connection */
-	u8 cnt_established;
-
 #define MPTCP_SCHED_DATA_SIZE 8
 	u8 mptcp_sched[MPTCP_SCHED_DATA_SIZE] __aligned(8);
 	struct mptcp_sched_ops *sched_ops;
net/mptcp/mctcp_desync.c +2 -2
index 9c5d6c196da5..cfc26fcc3161 100644
@@ -59,7 +59,7 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
 		tcp_reno_cong_avoid(sk, ack, acked);
 		return;
 	} else if (!tcp_is_cwnd_limited(sk)) {
@@ -121,7 +121,7 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
 		return max(tp->snd_cwnd >> 1U, 2U);
 	} else {
 		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
net/mptcp/mptcp_balia.c +2 -11
index 8b1fe11b08d5..ac5c5e550539 100644
@@ -86,10 +86,6 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
 	if (!mpcb)
 		return;
 
-	/* Only one subflow left - fall back to normal reno-behavior */
-	if (mpcb->cnt_established <= 1)
-		goto exit;
-
 	/* Find max_rate first */
 	mptcp_for_each_sk(mpcb, sub_sk) {
 		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
@@ -186,7 +182,6 @@ static void mptcp_balia_set_state(struct sock *sk, u8 ca_state)
 static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
 	int snd_cwnd;
 
 	if (!mptcp(tp)) {
@@ -209,10 +204,7 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
 		mptcp_set_forced(sk, 0);
 	}
 
-	if (mpcb->cnt_established > 1)
-		snd_cwnd = (int) mptcp_get_ai(sk);
-	else
-		snd_cwnd = tp->snd_cwnd;
+	snd_cwnd = (int) mptcp_get_ai(sk);
 
 	if (tp->snd_cwnd_cnt >= snd_cwnd) {
 		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
@@ -229,9 +221,8 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
 static u32 mptcp_balia_ssthresh(struct sock *sk)
 {
 	const struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
 
-	if (unlikely(!mptcp(tp) || mpcb->cnt_established <= 1))
+	if (unlikely(!mptcp(tp)))
 		return tcp_reno_ssthresh(sk);
 	else
 		return max((u32)(tp->snd_cwnd - mptcp_get_md(sk)), 1U);
net/mptcp/mptcp_coupled.c +15 -26

70 lines not on this page · raw message

net/mptcp/mptcp_ctrl.c +0 -8
index b1109ee9770f..da072fe21ec6 100644
@@ -737,11 +737,6 @@ static void mptcp_set_state(struct sock *sk)
 		tcp_sk(meta_sk)->lsndtime = tcp_jiffies32;
 	}
 
-	if (sk->sk_state == TCP_ESTABLISHED) {
-		tcp_sk(sk)->mptcp->establish_increased = 1;
-		tcp_sk(sk)->mpcb->cnt_established++;
-	}
-
 	if (sk->sk_state == TCP_CLOSE) {
 		if (!sock_flag(sk, SOCK_DEAD))
 			mptcp_sub_close(sk, 0);
@@ -1449,9 +1444,6 @@ void mptcp_del_sock(struct sock *sk)
 			}
 		}
 	}
-	if (tp->mptcp->establish_increased)
-		mpcb->cnt_established--;
-
 	tp->mptcp->next = NULL;
 	tp->mptcp->attached = 0;
 	mpcb->path_index_bits &= ~(1 << tp->mptcp->path_index);
net/mptcp/mptcp_input.c +2 -18
index 126c961ec55a..211605347f70 100644
@@ -2357,11 +2357,9 @@ int mptcp_rcv_synsent_state_process(struct sock *sk, struct sock **skptr,
 /* Similar to tcp_should_expand_sndbuf */
 bool mptcp_should_expand_sndbuf(const struct sock *sk)
 {
-	const struct sock *sk_it;
 	const struct sock *meta_sk = mptcp_meta_sk(sk);
 	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	int cnt_backups = 0;
-	int backup_available = 0;
+	const struct sock *sk_it;
 
 	/* We circumvent this check in tcp_check_space, because we want to
 	 * always call sk_write_space. So, we reproduce the check here.
@@ -2393,24 +2391,10 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
 		if (!mptcp_sk_can_send(sk_it))
 			continue;
 
-		/* Backup-flows have to be counted - if there is no other
-		 * subflow we take the backup-flow into account.
-		 */
-		if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio)
-			cnt_backups++;
-
-		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd) {
-			if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio) {
-				backup_available = 1;
-				continue;
-			}
+		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd)
 			return true;
-		}
 	}
 
-	/* Backup-flow is available for sending - update send-buffer */
-	if (meta_tp->mpcb->cnt_established == cnt_backups && backup_available)
-		return true;
 	return false;
 }
 
net/mptcp/mptcp_olia.c +5 -3
index c17c306849a3..9f61e1eff65a 100644
@@ -105,7 +105,7 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 	struct tcp_sock *tp;
 	struct sock *sk;
 	u64 tmp_int, tmp_rtt, best_int = 0, best_rtt = 1;
-	u32 max_cwnd, tmp_cwnd;
+	u32 max_cwnd, tmp_cwnd, established_cnt = 0;
 	u8 M = 0, B_not_M = 0;
 
 	/* TODO - integrate this in the following loop - we just want to iterate once */
@@ -120,6 +120,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 		if (!mptcp_olia_sk_can_send(sk))
 			continue;
 
+		established_cnt++;
+
 		tmp_rtt = (u64)tp->srtt_us * tp->srtt_us;
 		/* TODO - check here and rename variables */
 		tmp_int = max(ca->mptcp_loss3 - ca->mptcp_loss2,
@@ -173,10 +175,10 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 			if (tmp_cwnd < max_cwnd &&
 			    (u64)tmp_int * best_rtt == (u64)best_int * tmp_rtt) {
 				ca->epsilon_num = 1;
-				ca->epsilon_den = mpcb->cnt_established * B_not_M;
+				ca->epsilon_den = established_cnt * B_not_M;
 			} else if (tmp_cwnd == max_cwnd) {
 				ca->epsilon_num = -1;
-				ca->epsilon_den = mpcb->cnt_established  * M;
+				ca->epsilon_den = established_cnt * M;
 			} else {
 				ca->epsilon_num = 0;
 				ca->epsilon_den = 1;
Signature · 1 line
2.16.2
#19 lore raw

Re: [MPTCP] [PATCH 08/12] mptcp: Remove cnt_established

Hi Christoph,

Quoted 6 lines · Christoph Paasch

We want to remove the number of up-calls into the meta-socket. cnt_established is one of those where we are modifying a field in the mpcb.

We can remove it fairly safely. It's only use is in some MPTCP-specific congestion controls to short-circuit some parts of it.

Not a big optimisation I guess, no?
Anyway if MPTCP is not used, MPTCP-specific CC will still fallback to the TCP ones if I remember well.

Quoted 7 lines

One particular use was in mptcp_should_expand_sndbuf(). There, we use it to see whether to expand the sndbuf when there are only backup subflows available.

One problem with this old code is that the decision whether or not to send on a backup subflow is steered by the modular scheduler. We cannot assume in core-MPTCP code that backup subflows are never being sent on.

Should we not ask the scheduler if this subflow can be used?

Quoted 4 lines

By no more taking backup subflows into account, we greatly simplify mptcp_should_expand_sndbuf at the cost of potentially increasing the window even if there is no subflow ready to send because it's a backup subflow.

I am sorry, I didn't check when the callback should_expand_sndbuf is used yet. Is it only to pro-actively expend the sndbuf? Or is it asked always when it seems we need to increase the window? (If yes we should maybe ask the scheduler, no?)

But we can maybe see that as an optimisation and replace the code by a comment, no?

Best regards,
Matt

Quoted 284 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h       |  4 ----
  net/mptcp/mctcp_desync.c  |  4 ++--
  net/mptcp/mptcp_balia.c   | 13 ++-----------
  net/mptcp/mptcp_coupled.c | 41 +++++++++++++++--------------------------
  net/mptcp/mptcp_ctrl.c    |  8 --------
  net/mptcp/mptcp_input.c   | 20 ++------------------
  net/mptcp/mptcp_olia.c    |  8 +++++---
  7 files changed, 26 insertions(+), 72 deletions(-)
include/net/mptcp.h +0 -4
index 523abc8ce01d..758f2038e7cf 100644
@@ -166,7 +166,6 @@ struct mptcp_tcp_sock {
  	u16	map_data_len;
  	u16	slave_sk:1,
  		fully_established:1,
-		establish_increased:1,
  		second_packet:1,
  		attached:1,
  		send_mp_fail:1,
@@ -279,9 +278,6 @@ struct mptcp_cb {
  		snd_hiseq_index:1, /* Index in snd_high_order of snd_nxt */
  		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
 
-	/* socket count in this connection */
-	u8 cnt_established;
-
  #define MPTCP_SCHED_DATA_SIZE 8
  	u8 mptcp_sched[MPTCP_SCHED_DATA_SIZE] __aligned(8);
  	struct mptcp_sched_ops *sched_ops;
net/mptcp/mctcp_desync.c +2 -2
index 9c5d6c196da5..cfc26fcc3161 100644
@@ -59,7 +59,7 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
 
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
  		tcp_reno_cong_avoid(sk, ack, acked);
  		return;
  	} else if (!tcp_is_cwnd_limited(sk)) {
@@ -121,7 +121,7 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
 
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
  		return max(tp->snd_cwnd >> 1U, 2U);
  	} else {
  		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
net/mptcp/mptcp_balia.c +2 -11
index 8b1fe11b08d5..ac5c5e550539 100644
@@ -86,10 +86,6 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  	if (!mpcb)
  		return;
 
-	/* Only one subflow left - fall back to normal reno-behavior */
-	if (mpcb->cnt_established <= 1)
-		goto exit;
-
  	/* Find max_rate first */
  	mptcp_for_each_sk(mpcb, sub_sk) {
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
@@ -186,7 +182,6 @@ static void mptcp_balia_set_state(struct sock *sk, u8 ca_state)
  static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
  	int snd_cwnd;
 
  	if (!mptcp(tp)) {
@@ -209,10 +204,7 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  		mptcp_set_forced(sk, 0);
  	}
 
-	if (mpcb->cnt_established > 1)
-		snd_cwnd = (int) mptcp_get_ai(sk);
-	else
-		snd_cwnd = tp->snd_cwnd;
+	snd_cwnd = (int) mptcp_get_ai(sk);
 
  	if (tp->snd_cwnd_cnt >= snd_cwnd) {
  		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
@@ -229,9 +221,8 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  static u32 mptcp_balia_ssthresh(struct sock *sk)
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
 
-	if (unlikely(!mptcp(tp) || mpcb->cnt_established <= 1))
+	if (unlikely(!mptcp(tp)))
  		return tcp_reno_ssthresh(sk);
  	else
  		return max((u32)(tp->snd_cwnd - mptcp_get_md(sk)), 1U);
net/mptcp/mptcp_coupled.c +15 -26
index f9d856334ca9..a6569f5e20ab 100644
@@ -87,12 +87,6 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	if (!mpcb)
  		return;
 
-	/* Only one subflow left - fall back to normal reno-behavior
-	 * (set alpha to 1)
-	 */
-	if (mpcb->cnt_established <= 1)
-		goto exit;
-
  	/* Do regular alpha-calculation for multiple subflows */
 
  	/* Find the max numerator of the alpha-calculation */
@@ -137,8 +131,7 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	}
  	sum_denominator *= sum_denominator;
  	if (unlikely(!sum_denominator)) {
-		pr_err("%s: sum_denominator == 0, cnt_established:%d\n",
-		       __func__, mpcb->cnt_established);
+		pr_err("%s: sum_denominator == 0\n", __func__);
  		mptcp_for_each_sk(mpcb, sub_sk) {
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			pr_err("%s: pi:%d, state:%d\n, rtt:%u, cwnd: %u",
@@ -183,8 +176,8 @@ static void mptcp_ccc_set_state(struct sock *sk, u8 ca_state)
  static void mptcp_ccc_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
  	int snd_cwnd;
+	u64 alpha;
 
  	if (!mptcp(tp)) {
  		tcp_reno_cong_avoid(sk, ack, acked);
@@ -206,27 +199,23 @@ static void mptcp_ccc_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  		mptcp_set_forced(mptcp_meta_sk(sk), 0);
  	}
 
-	if (mpcb->cnt_established > 1) {
-		u64 alpha = mptcp_get_alpha(mptcp_meta_sk(sk));
+	alpha = mptcp_get_alpha(mptcp_meta_sk(sk));
 
-		/* This may happen, if at the initialization, the mpcb
-		 * was not yet attached to the sock, and thus
-		 * initializing alpha failed.
-		 */
-		if (unlikely(!alpha))
-			alpha = 1;
+	/* This may happen, if at the initialization, the mpcb
+	 * was not yet attached to the sock, and thus
+	 * initializing alpha failed.
+	 */
+	if (unlikely(!alpha))
+		alpha = 1;
 
-		snd_cwnd = (int) div_u64 ((u64) mptcp_ccc_scale(1, alpha_scale),
-						alpha);
+	snd_cwnd = (int) div_u64 ((u64) mptcp_ccc_scale(1, alpha_scale),
+					alpha);
 
-		/* snd_cwnd_cnt >= max (scale * tot_cwnd / alpha, cwnd)
-		 * Thus, we select here the max value.
-		 */
-		if (snd_cwnd < tp->snd_cwnd)
-			snd_cwnd = tp->snd_cwnd;
-	} else {
+	/* snd_cwnd_cnt >= max (scale * tot_cwnd / alpha, cwnd)
+	 * Thus, we select here the max value.
+	 */
+	if (snd_cwnd < tp->snd_cwnd)
  		snd_cwnd = tp->snd_cwnd;
-	}
 
  	if (tp->snd_cwnd_cnt >= snd_cwnd) {
  		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
net/mptcp/mptcp_ctrl.c +0 -8
index b1109ee9770f..da072fe21ec6 100644
@@ -737,11 +737,6 @@ static void mptcp_set_state(struct sock *sk)
  		tcp_sk(meta_sk)->lsndtime = tcp_jiffies32;
  	}
 
-	if (sk->sk_state == TCP_ESTABLISHED) {
-		tcp_sk(sk)->mptcp->establish_increased = 1;
-		tcp_sk(sk)->mpcb->cnt_established++;
-	}
-
  	if (sk->sk_state == TCP_CLOSE) {
  		if (!sock_flag(sk, SOCK_DEAD))
  			mptcp_sub_close(sk, 0);
@@ -1449,9 +1444,6 @@ void mptcp_del_sock(struct sock *sk)
  			}
  		}
  	}
-	if (tp->mptcp->establish_increased)
-		mpcb->cnt_established--;
-
  	tp->mptcp->next = NULL;
  	tp->mptcp->attached = 0;
  	mpcb->path_index_bits &= ~(1 << tp->mptcp->path_index);
net/mptcp/mptcp_input.c +2 -18
index 126c961ec55a..211605347f70 100644
@@ -2357,11 +2357,9 @@ int mptcp_rcv_synsent_state_process(struct sock *sk, struct sock **skptr,
  /* Similar to tcp_should_expand_sndbuf */
  bool mptcp_should_expand_sndbuf(const struct sock *sk)
  {
-	const struct sock *sk_it;
  	const struct sock *meta_sk = mptcp_meta_sk(sk);
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	int cnt_backups = 0;
-	int backup_available = 0;
+	const struct sock *sk_it;
 
  	/* We circumvent this check in tcp_check_space, because we want to
  	 * always call sk_write_space. So, we reproduce the check here.
@@ -2393,24 +2391,10 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  		if (!mptcp_sk_can_send(sk_it))
  			continue;
 
-		/* Backup-flows have to be counted - if there is no other
-		 * subflow we take the backup-flow into account.
-		 */
-		if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio)
-			cnt_backups++;
-
-		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd) {
-			if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio) {
-				backup_available = 1;
-				continue;
-			}
+		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd)
  			return true;
-		}
  	}
 
-	/* Backup-flow is available for sending - update send-buffer */
-	if (meta_tp->mpcb->cnt_established == cnt_backups && backup_available)
-		return true;
  	return false;
  }
 
net/mptcp/mptcp_olia.c +5 -3
index c17c306849a3..9f61e1eff65a 100644
@@ -105,7 +105,7 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	struct tcp_sock *tp;
  	struct sock *sk;
  	u64 tmp_int, tmp_rtt, best_int = 0, best_rtt = 1;
-	u32 max_cwnd, tmp_cwnd;
+	u32 max_cwnd, tmp_cwnd, established_cnt = 0;
  	u8 M = 0, B_not_M = 0;
 
  	/* TODO - integrate this in the following loop - we just want to iterate once */
@@ -120,6 +120,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  		if (!mptcp_olia_sk_can_send(sk))
  			continue;
 
+		established_cnt++;
+
  		tmp_rtt = (u64)tp->srtt_us * tp->srtt_us;
  		/* TODO - check here and rename variables */
  		tmp_int = max(ca->mptcp_loss3 - ca->mptcp_loss2,
@@ -173,10 +175,10 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  			if (tmp_cwnd < max_cwnd &&
  			    (u64)tmp_int * best_rtt == (u64)best_int * tmp_rtt) {
  				ca->epsilon_num = 1;
-				ca->epsilon_den = mpcb->cnt_established * B_not_M;
+				ca->epsilon_den = established_cnt * B_not_M;
  			} else if (tmp_cwnd == max_cwnd) {
  				ca->epsilon_num = -1;
-				ca->epsilon_den = mpcb->cnt_established  * M;
+				ca->epsilon_den = established_cnt * M;
  			} else {
  				ca->epsilon_num = 0;
  				ca->epsilon_den = 1;
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#20 lore raw

Re: [MPTCP] [PATCH 08/12] mptcp: Remove cnt_established

Quoted 7 lines · Matthieu Baerts

Hi Christoph,

Quoted 4 lines · Christoph Paasch

We want to remove the number of up-calls into the meta-socket. cnt_established is one of those where we are modifying a field in the mpcb.

Quoted 6 lines
Quoted 2 lines

We can remove it fairly safely. It's only use is in some MPTCP-specific congestion controls to short-circuit some parts of it.

Not a big optimisation I guess, no?
Anyway if MPTCP is not used, MPTCP-specific CC will still fallback to the TCP ones if I remember well.

Yes, that's what they do.

Quoted 10 lines
Quoted 8 lines

One particular use was in mptcp_should_expand_sndbuf(). There, we use it to see whether to expand the sndbuf when there are only backup subflows available.

One problem with this old code is that the decision whether or not to send on a backup subflow is steered by the modular scheduler. We cannot assume in core-MPTCP code that backup subflows are never being sent on.

Should we not ask the scheduler if this subflow can be used?

We could ask, but IMO it's kind of an over-optimization at that point.

Quoted 10 lines
Quoted 5 lines

By no more taking backup subflows into account, we greatly simplify mptcp_should_expand_sndbuf at the cost of potentially increasing the window even if there is no subflow ready to send because it's a backup subflow.

I am sorry, I didn't check when the callback should_expand_sndbuf is used yet. Is it only to pro-actively expend the sndbuf? Or is it asked always when it seems we need to increase the window? (If yes we should maybe ask the scheduler, no?)

Yes, it is called every time when we think sndbuf expansion is necessary (coming from tcp_check_space -> tcp_new_space -> ...).

Christoph

Quoted 289 lines

But we can maybe see that as an optimisation and replace the code by a comment, no?

Best regards,
Matt

Quoted 261 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h       |  4 ----
  net/mptcp/mctcp_desync.c  |  4 ++--
  net/mptcp/mptcp_balia.c   | 13 ++-----------
  net/mptcp/mptcp_coupled.c | 41 +++++++++++++++--------------------------
  net/mptcp/mptcp_ctrl.c    |  8 --------
  net/mptcp/mptcp_input.c   | 20 ++------------------
  net/mptcp/mptcp_olia.c    |  8 +++++---
  7 files changed, 26 insertions(+), 72 deletions(-)
include/net/mptcp.h +0 -4
index 523abc8ce01d..758f2038e7cf 100644
@@ -166,7 +166,6 @@ struct mptcp_tcp_sock {
  	u16	map_data_len;
  	u16	slave_sk:1,
  		fully_established:1,
-		establish_increased:1,
  		second_packet:1,
  		attached:1,
  		send_mp_fail:1,
@@ -279,9 +278,6 @@ struct mptcp_cb {
  		snd_hiseq_index:1, /* Index in snd_high_order of snd_nxt */
  		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
-	/* socket count in this connection */
-	u8 cnt_established;
-
  #define MPTCP_SCHED_DATA_SIZE 8
  	u8 mptcp_sched[MPTCP_SCHED_DATA_SIZE] __aligned(8);
  	struct mptcp_sched_ops *sched_ops;
net/mptcp/mctcp_desync.c +2 -2
index 9c5d6c196da5..cfc26fcc3161 100644
@@ -59,7 +59,7 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
  		tcp_reno_cong_avoid(sk, ack, acked);
  		return;
  	} else if (!tcp_is_cwnd_limited(sk)) {
@@ -121,7 +121,7 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
  		return max(tp->snd_cwnd >> 1U, 2U);
  	} else {
  		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
net/mptcp/mptcp_balia.c +2 -11
index 8b1fe11b08d5..ac5c5e550539 100644
@@ -86,10 +86,6 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  	if (!mpcb)
  		return;
-	/* Only one subflow left - fall back to normal reno-behavior */
-	if (mpcb->cnt_established <= 1)
-		goto exit;
-
  	/* Find max_rate first */
  	mptcp_for_each_sk(mpcb, sub_sk) {
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
@@ -186,7 +182,6 @@ static void mptcp_balia_set_state(struct sock *sk, u8 ca_state)
  static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
  	int snd_cwnd;
  	if (!mptcp(tp)) {
@@ -209,10 +204,7 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  		mptcp_set_forced(sk, 0);
  	}
-	if (mpcb->cnt_established > 1)
-		snd_cwnd = (int) mptcp_get_ai(sk);
-	else
-		snd_cwnd = tp->snd_cwnd;
+	snd_cwnd = (int) mptcp_get_ai(sk);
  	if (tp->snd_cwnd_cnt >= snd_cwnd) {
  		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
@@ -229,9 +221,8 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  static u32 mptcp_balia_ssthresh(struct sock *sk)
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
-	if (unlikely(!mptcp(tp) || mpcb->cnt_established <= 1))
+	if (unlikely(!mptcp(tp)))
  		return tcp_reno_ssthresh(sk);
  	else
  		return max((u32)(tp->snd_cwnd - mptcp_get_md(sk)), 1U);
net/mptcp/mptcp_coupled.c +15 -26
index f9d856334ca9..a6569f5e20ab 100644
@@ -87,12 +87,6 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	if (!mpcb)
  		return;
-	/* Only one subflow left - fall back to normal reno-behavior
-	 * (set alpha to 1)
-	 */
-	if (mpcb->cnt_established <= 1)
-		goto exit;
-
  	/* Do regular alpha-calculation for multiple subflows */
  	/* Find the max numerator of the alpha-calculation */
@@ -137,8 +131,7 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	}
  	sum_denominator *= sum_denominator;
  	if (unlikely(!sum_denominator)) {
-		pr_err("%s: sum_denominator == 0, cnt_established:%d\n",
-		       __func__, mpcb->cnt_established);
+		pr_err("%s: sum_denominator == 0\n", __func__);
  		mptcp_for_each_sk(mpcb, sub_sk) {
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			pr_err("%s: pi:%d, state:%d\n, rtt:%u, cwnd: %u",
@@ -183,8 +176,8 @@ static void mptcp_ccc_set_state(struct sock *sk, u8 ca_state)
  static void mptcp_ccc_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
  	int snd_cwnd;
+	u64 alpha;
  	if (!mptcp(tp)) {
  		tcp_reno_cong_avoid(sk, ack, acked);
@@ -206,27 +199,23 @@ static void mptcp_ccc_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  		mptcp_set_forced(mptcp_meta_sk(sk), 0);
  	}
-	if (mpcb->cnt_established > 1) {
-		u64 alpha = mptcp_get_alpha(mptcp_meta_sk(sk));
+	alpha = mptcp_get_alpha(mptcp_meta_sk(sk));
-		/* This may happen, if at the initialization, the mpcb
-		 * was not yet attached to the sock, and thus
-		 * initializing alpha failed.
-		 */
-		if (unlikely(!alpha))
-			alpha = 1;
+	/* This may happen, if at the initialization, the mpcb
+	 * was not yet attached to the sock, and thus
+	 * initializing alpha failed.
+	 */
+	if (unlikely(!alpha))
+		alpha = 1;
-		snd_cwnd = (int) div_u64 ((u64) mptcp_ccc_scale(1, alpha_scale),
-						alpha);
+	snd_cwnd = (int) div_u64 ((u64) mptcp_ccc_scale(1, alpha_scale),
+					alpha);
-		/* snd_cwnd_cnt >= max (scale * tot_cwnd / alpha, cwnd)
-		 * Thus, we select here the max value.
-		 */
-		if (snd_cwnd < tp->snd_cwnd)
-			snd_cwnd = tp->snd_cwnd;
-	} else {
+	/* snd_cwnd_cnt >= max (scale * tot_cwnd / alpha, cwnd)
+	 * Thus, we select here the max value.
+	 */
+	if (snd_cwnd < tp->snd_cwnd)
  		snd_cwnd = tp->snd_cwnd;
-	}
  	if (tp->snd_cwnd_cnt >= snd_cwnd) {
  		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
net/mptcp/mptcp_ctrl.c +0 -8
index b1109ee9770f..da072fe21ec6 100644
@@ -737,11 +737,6 @@ static void mptcp_set_state(struct sock *sk)
  		tcp_sk(meta_sk)->lsndtime = tcp_jiffies32;
  	}
-	if (sk->sk_state == TCP_ESTABLISHED) {
-		tcp_sk(sk)->mptcp->establish_increased = 1;
-		tcp_sk(sk)->mpcb->cnt_established++;
-	}
-
  	if (sk->sk_state == TCP_CLOSE) {
  		if (!sock_flag(sk, SOCK_DEAD))
  			mptcp_sub_close(sk, 0);
@@ -1449,9 +1444,6 @@ void mptcp_del_sock(struct sock *sk)
  			}
  		}
  	}
-	if (tp->mptcp->establish_increased)
-		mpcb->cnt_established--;
-
  	tp->mptcp->next = NULL;
  	tp->mptcp->attached = 0;
  	mpcb->path_index_bits &= ~(1 << tp->mptcp->path_index);
net/mptcp/mptcp_input.c +2 -18
index 126c961ec55a..211605347f70 100644
@@ -2357,11 +2357,9 @@ int mptcp_rcv_synsent_state_process(struct sock *sk, struct sock **skptr,
  /* Similar to tcp_should_expand_sndbuf */
  bool mptcp_should_expand_sndbuf(const struct sock *sk)
  {
-	const struct sock *sk_it;
  	const struct sock *meta_sk = mptcp_meta_sk(sk);
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	int cnt_backups = 0;
-	int backup_available = 0;
+	const struct sock *sk_it;
  	/* We circumvent this check in tcp_check_space, because we want to
  	 * always call sk_write_space. So, we reproduce the check here.
@@ -2393,24 +2391,10 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  		if (!mptcp_sk_can_send(sk_it))
  			continue;
-		/* Backup-flows have to be counted - if there is no other
-		 * subflow we take the backup-flow into account.
-		 */
-		if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio)
-			cnt_backups++;
-
-		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd) {
-			if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio) {
-				backup_available = 1;
-				continue;
-			}
+		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd)
  			return true;
-		}
  	}
-	/* Backup-flow is available for sending - update send-buffer */
-	if (meta_tp->mpcb->cnt_established == cnt_backups && backup_available)
-		return true;
  	return false;
  }
net/mptcp/mptcp_olia.c +5 -3
index c17c306849a3..9f61e1eff65a 100644
@@ -105,7 +105,7 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	struct tcp_sock *tp;
  	struct sock *sk;
  	u64 tmp_int, tmp_rtt, best_int = 0, best_rtt = 1;
-	u32 max_cwnd, tmp_cwnd;
+	u32 max_cwnd, tmp_cwnd, established_cnt = 0;
  	u8 M = 0, B_not_M = 0;
  	/* TODO - integrate this in the following loop - we just want to iterate once */
@@ -120,6 +120,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  		if (!mptcp_olia_sk_can_send(sk))
  			continue;
+		established_cnt++;
+
  		tmp_rtt = (u64)tp->srtt_us * tp->srtt_us;
  		/* TODO - check here and rename variables */
  		tmp_int = max(ca->mptcp_loss3 - ca->mptcp_loss2,
@@ -173,10 +175,10 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  			if (tmp_cwnd < max_cwnd &&
  			    (u64)tmp_int * best_rtt == (u64)best_int * tmp_rtt) {
  				ca->epsilon_num = 1;
-				ca->epsilon_den = mpcb->cnt_established * B_not_M;
+				ca->epsilon_den = established_cnt * B_not_M;
  			} else if (tmp_cwnd == max_cwnd) {
  				ca->epsilon_num = -1;
-				ca->epsilon_den = mpcb->cnt_established  * M;
+				ca->epsilon_den = established_cnt * M;
  			} else {
  				ca->epsilon_num = 0;
  				ca->epsilon_den = 1;
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
#21 lore raw

Re: [MPTCP] [PATCH 08/12] mptcp: Remove cnt_established

Hi Christoph,

Quoted 8 lines · Christoph Paasch
Quoted 7 lines · Matthieu Baerts

Hi Christoph,

Quoted 4 lines · Christoph Paasch

We want to remove the number of up-calls into the meta-socket. cnt_established is one of those where we are modifying a field in the mpcb.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

We can remove it fairly safely. It's only use is in some MPTCP-specific congestion controls to short-circuit some parts of it.

Not a big optimisation I guess, no?
Anyway if MPTCP is not used, MPTCP-specific CC will still fallback to the TCP ones if I remember well.

Yes, that's what they do.

Good point!

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

One particular use was in mptcp_should_expand_sndbuf(). There, we use it to see whether to expand the sndbuf when there are only backup subflows available.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

One problem with this old code is that the decision whether or not to send on a backup subflow is steered by the modular scheduler. We cannot assume in core-MPTCP code that backup subflows are never being sent on.

Should we not ask the scheduler if this subflow can be used?

We could ask, but IMO it's kind of an over-optimization at that point.

Fine for me! Can still be done later if needed.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

By no more taking backup subflows into account, we greatly simplify mptcp_should_expand_sndbuf at the cost of potentially increasing the window even if there is no subflow ready to send because it's a backup subflow.

Quoted 7 lines
Quoted 4 lines

I am sorry, I didn't check when the callback should_expand_sndbuf is used yet. Is it only to pro-actively expend the sndbuf? Or is it asked always when it seems we need to increase the window? (If yes we should maybe ask the scheduler, no?)

Yes, it is called every time when we think sndbuf expansion is necessary (coming from tcp_check_space -> tcp_new_space -> ...).

Thank you for your comment!

OK then I guess, regarding your comment about backup subflows that could be used, the sending window would not increase "quickly" enough: it would only depend on how the non-backup subflows are behaving. Not really a big issue as well.

Then if we look at the default scheduler where backup subflows are not used, we would potentially increase the sending window too quickly, not really a problem and it simplifies the code. Is it correct?

Cheers,
Matt

Quoted 291 lines

Christoph

Quoted 289 lines

But we can maybe see that as an optimisation and replace the code by a comment, no?

Best regards,
Matt

Quoted 261 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h       |  4 ----
  net/mptcp/mctcp_desync.c  |  4 ++--
  net/mptcp/mptcp_balia.c   | 13 ++-----------
  net/mptcp/mptcp_coupled.c | 41 +++++++++++++++--------------------------
  net/mptcp/mptcp_ctrl.c    |  8 --------
  net/mptcp/mptcp_input.c   | 20 ++------------------
  net/mptcp/mptcp_olia.c    |  8 +++++---
  7 files changed, 26 insertions(+), 72 deletions(-)
include/net/mptcp.h +0 -4
index 523abc8ce01d..758f2038e7cf 100644
@@ -166,7 +166,6 @@ struct mptcp_tcp_sock {
  	u16	map_data_len;
  	u16	slave_sk:1,
  		fully_established:1,
-		establish_increased:1,
  		second_packet:1,
  		attached:1,
  		send_mp_fail:1,
@@ -279,9 +278,6 @@ struct mptcp_cb {
  		snd_hiseq_index:1, /* Index in snd_high_order of snd_nxt */
  		rcv_hiseq_index:1; /* Index in rcv_high_order of rcv_nxt */
-	/* socket count in this connection */
-	u8 cnt_established;
-
  #define MPTCP_SCHED_DATA_SIZE 8
  	u8 mptcp_sched[MPTCP_SCHED_DATA_SIZE] __aligned(8);
  	struct mptcp_sched_ops *sched_ops;
net/mptcp/mctcp_desync.c +2 -2
index 9c5d6c196da5..cfc26fcc3161 100644
@@ -59,7 +59,7 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
  		tcp_reno_cong_avoid(sk, ack, acked);
  		return;
  	} else if (!tcp_is_cwnd_limited(sk)) {
@@ -121,7 +121,7 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	if (!mptcp(tp) || (1 == tp->mpcb->cnt_established)) {
+	if (!mptcp(tp)) {
  		return max(tp->snd_cwnd >> 1U, 2U);
  	} else {
  		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
net/mptcp/mptcp_balia.c +2 -11
index 8b1fe11b08d5..ac5c5e550539 100644
@@ -86,10 +86,6 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  	if (!mpcb)
  		return;
-	/* Only one subflow left - fall back to normal reno-behavior */
-	if (mpcb->cnt_established <= 1)
-		goto exit;
-
  	/* Find max_rate first */
  	mptcp_for_each_sk(mpcb, sub_sk) {
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
@@ -186,7 +182,6 @@ static void mptcp_balia_set_state(struct sock *sk, u8 ca_state)
  static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
  	int snd_cwnd;
  	if (!mptcp(tp)) {
@@ -209,10 +204,7 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  		mptcp_set_forced(sk, 0);
  	}
-	if (mpcb->cnt_established > 1)
-		snd_cwnd = (int) mptcp_get_ai(sk);
-	else
-		snd_cwnd = tp->snd_cwnd;
+	snd_cwnd = (int) mptcp_get_ai(sk);
  	if (tp->snd_cwnd_cnt >= snd_cwnd) {
  		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
@@ -229,9 +221,8 @@ static void mptcp_balia_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  static u32 mptcp_balia_ssthresh(struct sock *sk)
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
-	if (unlikely(!mptcp(tp) || mpcb->cnt_established <= 1))
+	if (unlikely(!mptcp(tp)))
  		return tcp_reno_ssthresh(sk);
  	else
  		return max((u32)(tp->snd_cwnd - mptcp_get_md(sk)), 1U);
net/mptcp/mptcp_coupled.c +15 -26
index f9d856334ca9..a6569f5e20ab 100644
@@ -87,12 +87,6 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	if (!mpcb)
  		return;
-	/* Only one subflow left - fall back to normal reno-behavior
-	 * (set alpha to 1)
-	 */
-	if (mpcb->cnt_established <= 1)
-		goto exit;
-
  	/* Do regular alpha-calculation for multiple subflows */
  	/* Find the max numerator of the alpha-calculation */
@@ -137,8 +131,7 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	}
  	sum_denominator *= sum_denominator;
  	if (unlikely(!sum_denominator)) {
-		pr_err("%s: sum_denominator == 0, cnt_established:%d\n",
-		       __func__, mpcb->cnt_established);
+		pr_err("%s: sum_denominator == 0\n", __func__);
  		mptcp_for_each_sk(mpcb, sub_sk) {
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			pr_err("%s: pi:%d, state:%d\n, rtt:%u, cwnd: %u",
@@ -183,8 +176,8 @@ static void mptcp_ccc_set_state(struct sock *sk, u8 ca_state)
  static void mptcp_ccc_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
-	const struct mptcp_cb *mpcb = tp->mpcb;
  	int snd_cwnd;
+	u64 alpha;
  	if (!mptcp(tp)) {
  		tcp_reno_cong_avoid(sk, ack, acked);
@@ -206,27 +199,23 @@ static void mptcp_ccc_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  		mptcp_set_forced(mptcp_meta_sk(sk), 0);
  	}
-	if (mpcb->cnt_established > 1) {
-		u64 alpha = mptcp_get_alpha(mptcp_meta_sk(sk));
+	alpha = mptcp_get_alpha(mptcp_meta_sk(sk));
-		/* This may happen, if at the initialization, the mpcb
-		 * was not yet attached to the sock, and thus
-		 * initializing alpha failed.
-		 */
-		if (unlikely(!alpha))
-			alpha = 1;
+	/* This may happen, if at the initialization, the mpcb
+	 * was not yet attached to the sock, and thus
+	 * initializing alpha failed.
+	 */
+	if (unlikely(!alpha))
+		alpha = 1;
-		snd_cwnd = (int) div_u64 ((u64) mptcp_ccc_scale(1, alpha_scale),
-						alpha);
+	snd_cwnd = (int) div_u64 ((u64) mptcp_ccc_scale(1, alpha_scale),
+					alpha);
-		/* snd_cwnd_cnt >= max (scale * tot_cwnd / alpha, cwnd)
-		 * Thus, we select here the max value.
-		 */
-		if (snd_cwnd < tp->snd_cwnd)
-			snd_cwnd = tp->snd_cwnd;
-	} else {
+	/* snd_cwnd_cnt >= max (scale * tot_cwnd / alpha, cwnd)
+	 * Thus, we select here the max value.
+	 */
+	if (snd_cwnd < tp->snd_cwnd)
  		snd_cwnd = tp->snd_cwnd;
-	}
  	if (tp->snd_cwnd_cnt >= snd_cwnd) {
  		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
net/mptcp/mptcp_ctrl.c +0 -8
index b1109ee9770f..da072fe21ec6 100644
@@ -737,11 +737,6 @@ static void mptcp_set_state(struct sock *sk)
  		tcp_sk(meta_sk)->lsndtime = tcp_jiffies32;
  	}
-	if (sk->sk_state == TCP_ESTABLISHED) {
-		tcp_sk(sk)->mptcp->establish_increased = 1;
-		tcp_sk(sk)->mpcb->cnt_established++;
-	}
-
  	if (sk->sk_state == TCP_CLOSE) {
  		if (!sock_flag(sk, SOCK_DEAD))
  			mptcp_sub_close(sk, 0);
@@ -1449,9 +1444,6 @@ void mptcp_del_sock(struct sock *sk)
  			}
  		}
  	}
-	if (tp->mptcp->establish_increased)
-		mpcb->cnt_established--;
-
  	tp->mptcp->next = NULL;
  	tp->mptcp->attached = 0;
  	mpcb->path_index_bits &= ~(1 << tp->mptcp->path_index);
net/mptcp/mptcp_input.c +2 -18
index 126c961ec55a..211605347f70 100644
@@ -2357,11 +2357,9 @@ int mptcp_rcv_synsent_state_process(struct sock *sk, struct sock **skptr,
  /* Similar to tcp_should_expand_sndbuf */
  bool mptcp_should_expand_sndbuf(const struct sock *sk)
  {
-	const struct sock *sk_it;
  	const struct sock *meta_sk = mptcp_meta_sk(sk);
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	int cnt_backups = 0;
-	int backup_available = 0;
+	const struct sock *sk_it;
  	/* We circumvent this check in tcp_check_space, because we want to
  	 * always call sk_write_space. So, we reproduce the check here.
@@ -2393,24 +2391,10 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  		if (!mptcp_sk_can_send(sk_it))
  			continue;
-		/* Backup-flows have to be counted - if there is no other
-		 * subflow we take the backup-flow into account.
-		 */
-		if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio)
-			cnt_backups++;
-
-		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd) {
-			if (tp_it->mptcp->rcv_low_prio || tp_it->mptcp->low_prio) {
-				backup_available = 1;
-				continue;
-			}
+		if (tcp_packets_in_flight(tp_it) < tp_it->snd_cwnd)
  			return true;
-		}
  	}
-	/* Backup-flow is available for sending - update send-buffer */
-	if (meta_tp->mpcb->cnt_established == cnt_backups && backup_available)
-		return true;
  	return false;
  }
net/mptcp/mptcp_olia.c +5 -3
index c17c306849a3..9f61e1eff65a 100644
@@ -105,7 +105,7 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	struct tcp_sock *tp;
  	struct sock *sk;
  	u64 tmp_int, tmp_rtt, best_int = 0, best_rtt = 1;
-	u32 max_cwnd, tmp_cwnd;
+	u32 max_cwnd, tmp_cwnd, established_cnt = 0;
  	u8 M = 0, B_not_M = 0;
  	/* TODO - integrate this in the following loop - we just want to iterate once */
@@ -120,6 +120,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  		if (!mptcp_olia_sk_can_send(sk))
  			continue;
+		established_cnt++;
+
  		tmp_rtt = (u64)tp->srtt_us * tp->srtt_us;
  		/* TODO - check here and rename variables */
  		tmp_int = max(ca->mptcp_loss3 - ca->mptcp_loss2,
@@ -173,10 +175,10 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  			if (tmp_cwnd < max_cwnd &&
  			    (u64)tmp_int * best_rtt == (u64)best_int * tmp_rtt) {
  				ca->epsilon_num = 1;
-				ca->epsilon_den = mpcb->cnt_established * B_not_M;
+				ca->epsilon_den = established_cnt * B_not_M;
  			} else if (tmp_cwnd == max_cwnd) {
  				ca->epsilon_num = -1;
-				ca->epsilon_den = mpcb->cnt_established  * M;
+				ca->epsilon_den = established_cnt * M;
  			} else {
  				ca->epsilon_num = 0;
  				ca->epsilon_den = 1;
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#22 lore raw

PATCH 9/12 mptcp: Rename tw_lock to mpcb_list_lock

In reply to Christoph Paasch

The lock is going to cover more than just the tw-list. Namely, the subflow-list stored in the mpcb.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 include/net/mptcp.h    |  4 +++-
 net/mptcp/mptcp_ctrl.c | 14 +++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)
include/net/mptcp.h +3 -1
index 758f2038e7cf..d455bdd101ec 100644
@@ -261,6 +261,9 @@ struct mptcp_cb {
 	/* list of sockets that need a call to release_cb */
 	struct hlist_head callback_list;
 
+	/* Lock used for protecting the different rcu-lists of mptcp_cb */
+	spinlock_t mpcb_list_lock;
+
 	/* High-order bits of 64-bit sequence numbers */
 	u32 snd_high_order[2];
 	u32 rcv_high_order[2];
@@ -292,7 +295,6 @@ struct mptcp_cb {
 	u64	infinite_rcv_seq;
 
 	/***** Start of fields, used for connection closure */
-	spinlock_t	 tw_lock;
 	unsigned char	 mptw_state;
 	u8		 dfin_path_index;
 
net/mptcp/mptcp_ctrl.c +7 -7
index da072fe21ec6..9237a73ec174 100644
@@ -665,14 +665,14 @@ void mptcp_sock_destruct(struct sock *sk)
 		 * update to the rcv_nxt of the time-wait-sock and remove
 		 * its reference to the mpcb.
 		 */
-		spin_lock_bh(&mpcb->tw_lock);
+		spin_lock_bh(&mpcb->mpcb_list_lock);
 		list_for_each_entry_rcu(mptw, &mpcb->tw_list, list) {
 			list_del_rcu(&mptw->list);
 			mptw->in_list = 0;
 			mptcp_mpcb_put(mpcb);
 			rcu_assign_pointer(mptw->mpcb, NULL);
 		}
-		spin_unlock_bh(&mpcb->tw_lock);
+		spin_unlock_bh(&mpcb->mpcb_list_lock);
 
 		mptcp_debug("%s destroying meta-sk token %#x\n", __func__,
 			    tcp_sk(sk)->mpcb->mptcp_loc_token);
@@ -1279,9 +1279,9 @@ static int mptcp_alloc_mpcb(struct sock *meta_sk, __u64 remote_key,
 
 	/* Init time-wait stuff */
 	INIT_LIST_HEAD(&mpcb->tw_list);
-	spin_lock_init(&mpcb->tw_lock);
 
 	INIT_HLIST_HEAD(&mpcb->callback_list);
+	spin_lock_init(&mpcb->mpcb_list_lock);
 
 	mptcp_mpcb_inherit_sockopts(meta_sk, master_sk);
 
@@ -2252,10 +2252,10 @@ int mptcp_init_tw_sock(struct sock *sk, struct tcp_timewait_sock *tw)
 		mptw->rcv_nxt++;
 	rcu_assign_pointer(mptw->mpcb, mpcb);
 
-	spin_lock_bh(&mpcb->tw_lock);
+	spin_lock_bh(&mpcb->mpcb_list_lock);
 	list_add_rcu(&mptw->list, &tp->mpcb->tw_list);
 	mptw->in_list = 1;
-	spin_unlock_bh(&mpcb->tw_lock);
+	spin_unlock_bh(&mpcb->mpcb_list_lock);
 
 	return 0;
 }
@@ -2271,12 +2271,12 @@ void mptcp_twsk_destructor(struct tcp_timewait_sock *tw)
 	 * from the list and drop the ref properly.
 	 */
 	if (mpcb && refcount_inc_not_zero(&mpcb->mpcb_refcnt)) {
-		spin_lock_bh(&mpcb->tw_lock);
+		spin_lock_bh(&mpcb->mpcb_list_lock);
 		if (tw->mptcp_tw->in_list) {
 			list_del_rcu(&tw->mptcp_tw->list);
 			tw->mptcp_tw->in_list = 0;
 		}
-		spin_unlock_bh(&mpcb->tw_lock);
+		spin_unlock_bh(&mpcb->mpcb_list_lock);
 
 		/* Twice, because we increased it above */
 		mptcp_mpcb_put(mpcb);
Signature · 1 line
2.16.2
author
#23 lore raw

PATCH 10/12 mptcp: Make subflow-list an RCU-list

In reply to Christoph Paasch

mptcp_add_sock() will now be called without holding the meta-level lock. However, mptcp_add_sock() wants to add the subflow to the meta-level list, thus we need to protect this by a lock. We use the mpcb_list_lock for that.

Now that we are locking during add/del, and want to allow lockless traversal of the list, this implies that we need to make it an RCU-list.

So, this patch transitions to the RCU hlist. The list-traversal macros (hlist_for_each_entry_rcu) require me to now pass the mptcp_tcp_sock to mptcp_for_each_*. So, I had to change all the places in the code where we call one of the list-traversal macros to adapt to this.

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 include/net/mptcp.h          |  69 ++++++++++----------
 net/ipv4/af_inet.c           |   9 +--
 net/ipv4/ip_sockglue.c       |   7 +-
 net/ipv4/tcp.c               |  31 +++++----
 net/mptcp/mctcp_desync.c     |  18 ++++--
 net/mptcp/mptcp_balia.c      |   8 ++-
 net/mptcp/mptcp_binder.c     |  10 ++-
 net/mptcp/mptcp_coupled.c    |  11 ++--
 net/mptcp/mptcp_ctrl.c       |  89 +++++++++++++++----------
 net/mptcp/mptcp_fullmesh.c   |  28 +++++---
 net/mptcp/mptcp_input.c      |  44 ++++++++-----
 net/mptcp/mptcp_ndiffports.c |   4 +-
 net/mptcp/mptcp_olia.c       |  20 ++++--
 net/mptcp/mptcp_output.c     |  38 +++++++----
 net/mptcp/mptcp_redundant.c  | 151 ++++++++++++++++++++++++++++++++++---------
 net/mptcp/mptcp_rr.c         |  22 +++++--
 net/mptcp/mptcp_sched.c      |  21 ++++--
 net/mptcp/mptcp_wvegas.c     |   5 +-
 18 files changed, 394 insertions(+), 191 deletions(-)
include/net/mptcp.h +36 -33
index d455bdd101ec..59e3da6c4031 100644
@@ -156,7 +156,7 @@ struct mptcp_options_received {
 };
 
 struct mptcp_tcp_sock {
-	struct tcp_sock	*next;		/* Next subflow socket */
+	struct hlist_node node;
 	struct hlist_node cb_list;
 	struct mptcp_options_received rx_opt;
 
@@ -257,7 +257,7 @@ struct mptcp_sched_ops {
 
 struct mptcp_cb {
 	/* list of sockets in this multipath connection */
-	struct tcp_sock *connection_list;
+	struct hlist_head conn_list;
 	/* list of sockets that need a call to release_cb */
 	struct hlist_head callback_list;
 
@@ -312,7 +312,7 @@ struct mptcp_cb {
 	/***** Start of fields, used for subflow establishment */
 	struct sock *meta_sk;
 
-	/* Master socket, also part of the connection_list, this
+	/* Master socket, also part of the conn_list, this
 	 * socket is the one that the application sees.
 	 */
 	struct sock *master_sk;
@@ -664,21 +664,17 @@ extern struct workqueue_struct *mptcp_wq;
 			pr_err(fmt, ##args);					\
 	} while (0)
 
-/* Iterates over all subflows */
-#define mptcp_for_each_tp(mpcb, tp)					\
-	for ((tp) = (mpcb)->connection_list; (tp); (tp) = (tp)->mptcp->next)
+static inline struct sock *mptcp_to_sock(const struct mptcp_tcp_sock *mptcp)
+{
+	return (struct sock *)mptcp->tp;
+}
 
-#define mptcp_for_each_sk(mpcb, sk)					\
-	for ((sk) = (struct sock *)(mpcb)->connection_list;		\
-	     sk;							\
-	     sk = (struct sock *)tcp_sk(sk)->mptcp->next)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	hlist_for_each_entry_rcu(__mptcp, &((__mpcb)->conn_list), node)
 
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)			\
-	for (__sk = (struct sock *)(__mpcb)->connection_list,		\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL; \
-	     __sk;							\
-	     __sk = __temp,						\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL)
+/* Must be called with the appropriate lock held */
+#define mptcp_for_each_sub_safe(__mpcb, __mptcp, __tmp)				\
+	hlist_for_each_entry_safe(__mptcp, __tmp, &((__mpcb)->conn_list), node)
 
 /* Iterates over all bit set to 1 in a bitset */
 #define mptcp_for_each_bit_set(b, i)					\
@@ -932,12 +928,14 @@ struct request_sock *rev_mptcp_rsk(const struct mptcp_request_sock *req)
 
 static inline bool mptcp_can_sendpage(struct sock *sk)
 {
-	struct sock *sk_it;
+	struct mptcp_tcp_sock *mptcp;
 
 	if (tcp_sk(sk)->mpcb->dss_csum)
 		return false;
 
-	mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+	mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
 		if (!(sk_it->sk_route_caps & NETIF_F_SG) ||
 		    !sk_check_csum_caps(sk_it))
 			return false;
@@ -972,9 +970,12 @@ static inline void mptcp_send_reset(struct sock *sk)
 static inline void mptcp_sub_force_close_all(struct mptcp_cb *mpcb,
 					     struct sock *except)
 {
-	struct sock *sk_it, *tmp;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
+
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
 
-	mptcp_for_each_sk_safe(mpcb, sk_it, tmp) {
 		if (sk_it != except)
 			mptcp_send_reset(sk_it);
 	}
@@ -1161,12 +1162,14 @@ static inline int mptcp_sk_can_send_ack(const struct sock *sk)
 /* Only support GSO if all subflows supports it */
 static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
 {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 
 	if (tcp_sk(meta_sk)->mpcb->dss_csum)
 		return false;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
 		if (!mptcp_sk_can_send(sk))
 			continue;
 		if (!sk_can_gso(sk))
@@ -1177,12 +1180,14 @@ static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
 
 static inline bool mptcp_can_sg(const struct sock *meta_sk)
 {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 
 	if (tcp_sk(meta_sk)->mpcb->dss_csum)
 		return false;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
 		if (!mptcp_sk_can_send(sk))
 			continue;
 		if (!(sk->sk_route_caps & NETIF_F_SG))
@@ -1193,9 +1198,9 @@ static inline bool mptcp_can_sg(const struct sock *meta_sk)
 
 static inline void mptcp_set_rto(struct sock *sk)
 {
-	struct tcp_sock *tp = tcp_sk(sk);
-	struct sock *sk_it;
 	struct inet_connection_sock *micsk = inet_csk(mptcp_meta_sk(sk));
+	struct tcp_sock *tp = tcp_sk(sk);
+	struct mptcp_tcp_sock *mptcp;
 	__u32 max_rto = 0;
 
 	/* We are in recovery-phase on the MPTCP-level. Do not update the
@@ -1204,7 +1209,9 @@ static inline void mptcp_set_rto(struct sock *sk)
 	if (micsk->icsk_retransmits)
 		return;
 
-	mptcp_for_each_sk(tp->mpcb, sk_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
 		if ((mptcp_sk_can_send(sk_it) || sk->sk_state == TCP_SYN_RECV) &&
 		    inet_csk(sk_it)->icsk_rto > max_rto)
 			max_rto = inet_csk(sk_it)->icsk_rto;
@@ -1303,12 +1310,8 @@ bool mptcp_prune_ofo_queue(struct sock *sk);
 	do {				\
 	} while (0)
 
-/* Without MPTCP, we just do one iteration
- * over the only socket available. This assumes that
- * the sk/tp arg is the socket in that case.
- */
-#define mptcp_for_each_sk(mpcb, sk)
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	if (0)
 
 #define MPTCP_INC_STATS(net, field)	\
 	do {				\
net/ipv4/af_inet.c +5 -4
index 426fa704e218..87a98edc18d4 100644
@@ -709,13 +709,14 @@ int inet_accept(struct socket *sock, struct socket *newsock, int flags,
 	sock_rps_record_flow(sk2);
 
 	if (sk2->sk_protocol == IPPROTO_TCP && mptcp(tcp_sk(sk2))) {
-		struct sock *sk_it = sk2;
+		struct mptcp_tcp_sock *mptcp;
 
-		mptcp_for_each_sk(tcp_sk(sk2)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tcp_sk(sk2)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
 
 		if (tcp_sk(sk2)->mpcb->master_sk) {
-			sk_it = tcp_sk(sk2)->mpcb->master_sk;
+			struct sock *sk_it = tcp_sk(sk2)->mpcb->master_sk;
 
 			write_lock_bh(&sk_it->sk_callback_lock);
 			sk_it->sk_wq = newsock->wq;
net/ipv4/ip_sockglue.c +5 -2
index 1fe79c7bcc9e..2b7a8c416c91 100644
@@ -756,8 +756,11 @@ static int do_ip_setsockopt(struct sock *sk, int level,
 			sk_dst_reset(sk);
 			/* Update TOS on mptcp subflow */
 			if (is_meta_sk(sk)) {
-				struct sock *sk_it;
-				mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+				struct mptcp_tcp_sock *mptcp;
+
+				mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+					struct sock *sk_it = mptcp_to_sock(mptcp);
+
 					if (inet_sk(sk_it)->tos != inet_sk(sk)->tos) {
 						inet_sk(sk_it)->tos = inet_sk(sk)->tos;
 						sk_it->sk_priority = sk->sk_priority;
net/ipv4/tcp.c +19 -12
index 93e15eeba870..fd53722bf59e 100644
@@ -804,9 +804,11 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
 
 #ifdef CONFIG_MPTCP
 	if (mptcp(tcp_sk(sk))) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
 	}
 #endif
 	/*
@@ -984,7 +986,7 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
 	}
 
 	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
+		struct mptcp_tcp_sock *mptcp;
 
 		/* We must check this with socket-lock hold because we iterate
 		 * over the subflows.
@@ -999,8 +1001,9 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
 			return ret;
 		}
 
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
 	}
 
 	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
@@ -1293,9 +1296,11 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
 	}
 
 	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
 	}
 
 	if (unlikely(tp->repair)) {
@@ -1883,9 +1888,11 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
 
 #ifdef CONFIG_MPTCP
 	if (mptcp(tp)) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
 	}
 #endif
 
net/mptcp/mctcp_desync.c +11 -7
index cfc26fcc3161..f6bf9251d59b 100644
@@ -67,7 +67,6 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
 	} else {
 		const struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
 		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
 
 		/* current aggregated cwnd */
 		u32 agg_cwnd = 0;
@@ -90,11 +89,14 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
 				tp->snd_ssthresh = tp->snd_cwnd = INI_MIN_CWND;
 			}
 			return;
-		}
-		  /* In dangerous area, increase slowly and linearly. */
-		  else {
+		} else {
+			/* In dangerous area, increase slowly and linearly. */
+			const struct mptcp_tcp_sock *mptcp;
+
 			/* get total cwnd and the subflow that has min cwnd */
-			mptcp_for_each_sk(tp->mpcb, sub_sk) {
+			mptcp_for_each_sub(tp->mpcb, mptcp) {
+				const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
 				if (mctcp_cc_sk_can_send(sub_sk)) {
 					const struct tcp_sock *sub_tp =
 								tcp_sk(sub_sk);
@@ -126,12 +128,14 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
 	} else {
 		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
 		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
+		const struct mptcp_tcp_sock *mptcp;
 		u32 max_cwnd = 0;
 		u8 max_cwnd_subfid = 0;
 
 		/* Find the subflow that has the max cwnd. */
-		mptcp_for_each_sk(tp->mpcb, sub_sk) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
 			if (mctcp_cc_sk_can_send(sub_sk)) {
 				const struct tcp_sock *sub_tp = tcp_sk(sub_sk);
 				if (max_cwnd < sub_tp->snd_cwnd) {
net/mptcp/mptcp_balia.c +5 -3
index ac5c5e550539..e8a22fa66a50 100644
@@ -78,7 +78,7 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
 {
 	const struct tcp_sock *tp = tcp_sk(sk);
 	const struct mptcp_cb *mpcb = tp->mpcb;
-	const struct sock *sub_sk;
+	struct mptcp_tcp_sock *mptcp;
 	u64 max_rate = 0, rate = 0, sum_rate = 0;
 	u64 alpha, ai = tp->snd_cwnd, md = (tp->snd_cwnd >> 1);
 	int num_scale_down = 0;
@@ -87,7 +87,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
 		return;
 
 	/* Find max_rate first */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
 		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
 		u64 tmp;
 
@@ -119,7 +120,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
 
 	if (num_scale_down) {
 		sum_rate = 0;
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
 			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
 			u64 tmp;
 
net/mptcp/mptcp_binder.c +7 -3
index d6ef9c5b6ce5..eaf8ba674d17 100644
@@ -46,6 +46,8 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
 	unsigned char *opt_ptr, *opt_end_ptr, opt[MAX_IPOPTLEN];
 
 	for (i = 0; i < MPTCP_GW_MAX_LISTS; ++i) {
+		struct mptcp_tcp_sock *mptcp;
+
 		if (mptcp_gws->len[i] == 0)
 			goto error;
 
@@ -53,7 +55,9 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
 		list_taken = 0;
 
 		/* Loop through all sub-sockets in this connection */
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk) {
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
 			mptcp_debug("mptcp_get_avail_list_ipv4: Next sock\n");
 
 			/* Reset length and options buffer, then retrieve
@@ -289,10 +293,10 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
 
 static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
 {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 	int i = 0;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
 		i++;
 	}
 
net/mptcp/mptcp_coupled.c +7 -4
index a6569f5e20ab..e1040a23e59f 100644
@@ -80,7 +80,7 @@ static inline void mptcp_set_forced(const struct sock *meta_sk, bool force)
 static void mptcp_ccc_recalc_alpha(const struct sock *sk)
 {
 	const struct mptcp_cb *mpcb = tcp_sk(sk)->mpcb;
-	const struct sock *sub_sk;
+	const struct mptcp_tcp_sock *mptcp;
 	int best_cwnd = 0, best_rtt = 0, can_send = 0;
 	u64 max_numerator = 0, sum_denominator = 0, alpha = 1;
 
@@ -90,7 +90,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
 	/* Do regular alpha-calculation for multiple subflows */
 
 	/* Find the max numerator of the alpha-calculation */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
 		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
 		u64 tmp;
 
@@ -118,7 +119,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
 		goto exit;
 
 	/* Calculate the denominator */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
 		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
 
 		if (!mptcp_ccc_sk_can_send(sub_sk))
@@ -132,7 +134,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
 	sum_denominator *= sum_denominator;
 	if (unlikely(!sum_denominator)) {
 		pr_err("%s: sum_denominator == 0\n", __func__);
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
 			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
 			pr_err("%s: pi:%d, state:%d\n, rtt:%u, cwnd: %u",
 			       __func__, sub_tp->mptcp->path_index,
net/mptcp/mptcp_ctrl.c +55 -34

236 lines not on this page · raw message

net/mptcp/mptcp_fullmesh.c +20 -8
index d29102a1dffb..53c1ba7319f5 100644
@@ -903,8 +903,9 @@ static void mptcp_address_worker(struct work_struct *work)
 			}
 
 			if (event->code == MPTCP_EVENT_DEL) {
-				struct sock *sk, *tmpsk;
+				struct mptcp_tcp_sock *mptcp;
 				struct mptcp_loc_addr *mptcp_local;
+				struct hlist_node *tmp;
 				bool found = false;
 
 				mptcp_local = rcu_dereference_bh(fm_ns->local);
@@ -914,7 +915,9 @@ static void mptcp_address_worker(struct work_struct *work)
 					update_addr_bitfields(meta_sk, mptcp_local);
 
 				/* Look for the socket and remove him */
-				mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+				mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
+
 					if ((event->family == AF_INET6 &&
 					     (sk->sk_family == AF_INET ||
 					      mptcp_v6_is_v4_mapped(sk))) ||
@@ -964,9 +967,10 @@ static void mptcp_address_worker(struct work_struct *work)
 			}
 
 			if (event->code == MPTCP_EVENT_MOD) {
-				struct sock *sk;
+				struct mptcp_tcp_sock *mptcp;
 
-				mptcp_for_each_sk(mpcb, sk) {
+				mptcp_for_each_sub(mpcb, mptcp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
 					struct tcp_sock *tp = tcp_sk(sk);
 					if (event->family == AF_INET &&
 					    (sk->sk_family == AF_INET ||
@@ -1455,8 +1459,9 @@ static void full_mesh_release_sock(struct sock *meta_sk)
 	struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
 	struct fullmesh_priv *fmp = fullmesh_get_priv(mpcb);
 	const struct mptcp_fm_ns *fm_ns = fm_get_ns(sock_net(meta_sk));
-	struct sock *sk, *tmpsk;
 	bool meta_v4 = meta_sk->sk_family == AF_INET;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
 	int i;
 
 	rcu_read_lock_bh();
@@ -1470,7 +1475,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
 		struct in_addr ifa = mptcp_local->locaddr4[i].addr;
 		bool found = false;
 
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
 			struct tcp_sock *tp = tcp_sk(sk);
 
 			if (sk->sk_family == AF_INET6 &&
@@ -1491,6 +1497,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
 		}
 
 		if (!found) {
+			struct sock *sk;
+
 			fmp->add_addr++;
 			mpcb->addr_signal = 1;
 
@@ -1511,7 +1519,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
 		struct in6_addr ifa = mptcp_local->locaddr6[i].addr;
 		bool found = false;
 
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
 			struct tcp_sock *tp = tcp_sk(sk);
 
 			if (sk->sk_family == AF_INET ||
@@ -1532,6 +1541,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
 		}
 
 		if (!found) {
+			struct sock *sk;
+
 			fmp->add_addr++;
 			mpcb->addr_signal = 1;
 
@@ -1546,7 +1557,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
 #endif
 
 	/* Now, detect address-removals */
-	mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
 		bool shall_remove = true;
 
 		if (sk->sk_family == AF_INET || mptcp_v6_is_v4_mapped(sk)) {
net/mptcp/mptcp_input.c +29 -15

112 lines not on this page · raw message

net/mptcp/mptcp_ndiffports.c +2 -2
index fe73ade7ba24..4219e9489655 100644
@@ -20,10 +20,10 @@ MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connecti
 
 static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)
 {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 	int i = 0;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
 		i++;
 	}
 
net/mptcp/mptcp_olia.c +13 -7
index 9f61e1eff65a..c44eb9208581 100644
@@ -61,10 +61,11 @@ static u32 mptcp_get_crt_cwnd(struct sock *sk)
 /* return the dominator of the first term of  the increasing term */
 static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
 {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 	u64 rate = 1; /* We have to avoid a zero-rate because it is used as a divisor */
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
 		struct tcp_sock *tp = tcp_sk(sk);
 		u64 scaled_num;
 		u32 tmp_cwnd;
@@ -83,10 +84,11 @@ static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
 /* find the maximum cwnd, used to find set M */
 static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
 {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 	u32 best_cwnd = 0;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
 		u32 tmp_cwnd;
 
 		if (!mptcp_olia_sk_can_send(sk))
@@ -101,6 +103,7 @@ static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
 
 static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 {
+	struct mptcp_tcp_sock *mptcp;
 	struct mptcp_olia *ca;
 	struct tcp_sock *tp;
 	struct sock *sk;
@@ -113,7 +116,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 	max_cwnd = mptcp_get_max_cwnd(mpcb);
 
 	/* find the best path */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
 		tp = tcp_sk(sk);
 		ca = inet_csk_ca(sk);
 
@@ -135,7 +139,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 
 	/* TODO - integrate this here in mptcp_get_max_cwnd and in the previous loop */
 	/* find the size of M and B_not_M */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
 		tp = tcp_sk(sk);
 		ca = inet_csk_ca(sk);
 
@@ -156,7 +161,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 	}
 
 	/* check if the path is in M or B_not_M and set the value of epsilon accordingly */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
 		tp = tcp_sk(sk);
 		ca = inet_csk_ca(sk);
 
net/mptcp/mptcp_output.c +25 -13
index cc1a4f7d39e9..29a7ab309751 100644
@@ -647,7 +647,6 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
 {
 	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
 	struct sk_buff *skb;
-	struct sock *sk_it;
 	int ans = 0;
 
 	if (meta_sk->sk_state == TCP_CLOSE)
@@ -704,17 +703,22 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
 
 		return 0;
 	} else {
+		struct mptcp_tcp_sock *mptcp;
+
 window_probe:
 		if (between(meta_tp->snd_up, meta_tp->snd_una + 1,
 			    meta_tp->snd_una + 0xFFFF)) {
-			mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+			mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+				struct sock *sk_it = mptcp_to_sock(mptcp);
+
 				if (mptcp_sk_can_send_ack(sk_it))
 					tcp_xmit_probe_skb(sk_it, 1, mib);
 			}
 		}
 
 		/* At least one of the tcp_xmit_probe_skb's has to succeed */
-		mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
 			int ret;
 
 			if (!mptcp_sk_can_send_ack(sk_it))
@@ -732,6 +736,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
 		     int push_one, gfp_t gfp)
 {
 	struct tcp_sock *meta_tp = tcp_sk(meta_sk), *subtp;
+	struct mptcp_tcp_sock *mptcp;
 	struct sock *subsk = NULL;
 	struct mptcp_cb *mpcb = meta_tp->mpcb;
 	struct sk_buff *skb;
@@ -856,7 +861,8 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
 			break;
 	}
 
-	mptcp_for_each_sk(mpcb, subsk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		subsk = mptcp_to_sock(mptcp);
 		subtp = tcp_sk(subsk);
 
 		if (!(path_mask & mptcp_pi_to_flag(subtp->mptcp->path_index)))
@@ -1353,7 +1359,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
 	struct mptcp_cb *mpcb = meta_tp->mpcb;
 	struct sock *sk;
 
-	if (!mpcb->connection_list)
+	if (hlist_empty(&mpcb->conn_list))
 		return;
 
 	WARN_ON(meta_tp->send_mp_fclose);
@@ -1728,10 +1734,11 @@ void mptcp_select_initial_window(const struct sock *sk, int __space, __u32 mss,
 static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
 				  unsigned int (*mss_cb)(struct sock *sk))
 {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 	u64 rate = 0;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
 		struct tcp_sock *tp = tcp_sk(sk);
 		int this_mss;
 		u64 this_rate;
@@ -1783,11 +1790,12 @@ static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
 static unsigned int __mptcp_current_mss(const struct sock *meta_sk,
 					unsigned int (*mss_cb)(struct sock *sk))
 {
+	struct mptcp_tcp_sock *mptcp;
 	unsigned int mss = 0;
 	u64 rate = 0;
-	struct sock *sk;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
 		int this_mss;
 		u64 this_rate;
 
@@ -1852,14 +1860,16 @@ int mptcp_select_size(const struct sock *meta_sk, bool sg, bool first_skb, bool
 
 int mptcp_check_snd_buf(const struct tcp_sock *tp)
 {
-	const struct sock *sk;
+	const struct mptcp_tcp_sock *mptcp;
 	u32 rtt_max = tp->srtt_us;
 	u64 bw_est;
 
 	if (!tp->srtt_us)
 		return tp->reordering + 1;
 
-	mptcp_for_each_sk(tp->mpcb, sk) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		const struct sock *sk = mptcp_to_sock(mptcp);
+
 		if (!mptcp_sk_can_send(sk))
 			continue;
 
@@ -1877,11 +1887,13 @@ int mptcp_check_snd_buf(const struct tcp_sock *tp)
 unsigned int mptcp_xmit_size_goal(const struct sock *meta_sk, u32 mss_now,
 				  int large_allowed)
 {
-	struct sock *sk;
 	u32 xmit_size_goal = 0;
 
 	if (large_allowed && mptcp_sk_can_gso(meta_sk)) {
-		mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
 			int this_size_goal;
 
 			if (!mptcp_sk_can_send(sk))
net/mptcp/mptcp_redundant.c +122 -29

235 lines not on this page · raw message

net/mptcp/mptcp_rr.c +15 -7
index 8910ba9e6052..d97706034646 100644
@@ -102,12 +102,14 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
 					     bool zero_wnd_test)
 {
 	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk, *bestsk = NULL, *backupsk = NULL;
+	struct sock *sk = NULL, *bestsk = NULL, *backupsk = NULL;
+	struct mptcp_tcp_sock *mptcp;
 
 	/* Answer data_fin on same subflow!!! */
 	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
 	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
 			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
 			    mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
 				return sk;
@@ -115,8 +117,11 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
 	}
 
 	/* First, find the best subflow */
-	mptcp_for_each_sk(mpcb, sk) {
-		struct tcp_sock *tp = tcp_sk(sk);
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct tcp_sock *tp;
+
+		sk = mptcp_to_sock(mptcp);
+		tp = tcp_sk(sk);
 
 		if (!mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
 			continue;
@@ -177,7 +182,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
 					     unsigned int *limit)
 {
 	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk_it, *choose_sk = NULL;
+	struct sock *choose_sk = NULL;
+	struct mptcp_tcp_sock *mptcp;
 	struct sk_buff *skb = __mptcp_rr_next_segment(meta_sk, reinject);
 	unsigned char split = num_segments;
 	unsigned char iter = 0, full_subs = 0;
@@ -199,7 +205,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
 retry:
 
 	/* First, we look for a subflow who is currently being used */
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
 		struct tcp_sock *tp_it = tcp_sk(sk_it);
 		struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
 
@@ -233,7 +240,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
 		/* So, we restart this round by setting quota to 0 and retry
 		 * to find a subflow.
 		 */
-		mptcp_for_each_sk(mpcb, sk_it) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
 			struct tcp_sock *tp_it = tcp_sk(sk_it);
 			struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
 
net/mptcp/mptcp_sched.c +15 -6
index c831515fd7f0..09246e286da8 100644
@@ -139,9 +139,10 @@ static struct sock
 	u32 min_srtt = 0xffffffff;
 	bool found_unused = false;
 	bool found_unused_una = false;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
 		struct tcp_sock *tp = tcp_sk(sk);
 		bool unused = false;
 
@@ -222,7 +223,11 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
 	/* Answer data_fin on same subflow!!! */
 	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
 	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
 			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
 			    mptcp_is_available(sk, skb, zero_wnd_test))
 				return sk;
@@ -256,7 +261,7 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
 {
 	struct sock *meta_sk;
 	const struct tcp_sock *tp = tcp_sk(sk);
-	struct tcp_sock *tp_it;
+	struct mptcp_tcp_sock *mptcp;
 	struct sk_buff *skb_head;
 	struct defsched_priv *dsp = defsched_get_priv(tp);
 
@@ -279,7 +284,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
 		goto retrans;
 
 	/* Half the cwnd of the slow flow */
-	mptcp_for_each_tp(tp->mpcb, tp_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct tcp_sock *tp_it = mptcp->tp;
+
 		if (tp_it != tp &&
 		    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
 			if (tp->srtt_us < tp_it->srtt_us && inet_csk((struct sock *)tp_it)->icsk_ca_state == TCP_CA_Open) {
@@ -302,7 +309,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
 	/* Segment not yet injected into this path? Take it!!! */
 	if (!(TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp->mptcp->path_index))) {
 		bool do_retrans = false;
-		mptcp_for_each_tp(tp->mpcb, tp_it) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			struct tcp_sock *tp_it = mptcp->tp;
+
 			if (tp_it != tp &&
 			    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
 				if (tp_it->snd_cwnd <= 4) {
net/mptcp/mptcp_wvegas.c +3 -2
index 4373b8d1ee8d..787ddaab98a2 100644
@@ -143,14 +143,15 @@ static inline u32 mptcp_wvegas_ssthresh(const struct tcp_sock *tp)
 static u64 mptcp_wvegas_weight(const struct mptcp_cb *mpcb, const struct sock *sk)
 {
 	u64 total_rate = 0;
-	struct sock *sub_sk;
 	const struct wvegas *wvegas = inet_csk_ca(sk);
+	struct mptcp_tcp_sock *mptcp;
 
 	if (!mpcb)
 		return wvegas->weight;
 
 
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sub_sk = mptcp_to_sock(mptcp);
 		struct wvegas *sub_wvegas = inet_csk_ca(sub_sk);
 
 		/* sampled_rtt is initialized by 0 */
Signature · 1 line
2.16.2
#24 lore raw

Re: [MPTCP] [PATCH 10/12] mptcp: Make subflow-list an RCU-list

Hi Christoph,

Quoted 5 lines · Christoph Paasch

mptcp_add_sock() will now be called without holding the meta-level lock. However, mptcp_add_sock() wants to add the subflow to the meta-level list, thus we need to protect this by a lock. We use the mpcb_list_lock for that.

Quoted 7 lines

Now that we are locking during add/del, and want to allow lockless traversal of the list, this implies that we need to make it an RCU-list.

So, this patch transitions to the RCU hlist. The list-traversal macros (hlist_for_each_entry_rcu) require me to now pass the mptcp_tcp_sock to mptcp_for_each_*. So, I had to change all the places in the code where we call one of the list-traversal macros to adapt to this.

Thank you for this big refactoring!
I quickly reviewed these changes, I hope no 'struct mptcp_tcp_sock' have been overridden but it looks fine, we always add a new variable 'mptcp' :-)

I only have comments in mptcp_redundant.c

Quoted 1,043 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h          |  69 ++++++++++----------
  net/ipv4/af_inet.c           |   9 +--
  net/ipv4/ip_sockglue.c       |   7 +-
  net/ipv4/tcp.c               |  31 +++++----
  net/mptcp/mctcp_desync.c     |  18 ++++--
  net/mptcp/mptcp_balia.c      |   8 ++-
  net/mptcp/mptcp_binder.c     |  10 ++-
  net/mptcp/mptcp_coupled.c    |  11 ++--
  net/mptcp/mptcp_ctrl.c       |  89 +++++++++++++++----------
  net/mptcp/mptcp_fullmesh.c   |  28 +++++---
  net/mptcp/mptcp_input.c      |  44 ++++++++-----
  net/mptcp/mptcp_ndiffports.c |   4 +-
  net/mptcp/mptcp_olia.c       |  20 ++++--
  net/mptcp/mptcp_output.c     |  38 +++++++----
  net/mptcp/mptcp_redundant.c  | 151 ++++++++++++++++++++++++++++++++++---------
  net/mptcp/mptcp_rr.c         |  22 +++++--
  net/mptcp/mptcp_sched.c      |  21 ++++--
  net/mptcp/mptcp_wvegas.c     |   5 +-
  18 files changed, 394 insertions(+), 191 deletions(-)
include/net/mptcp.h +36 -33
index d455bdd101ec..59e3da6c4031 100644
@@ -156,7 +156,7 @@ struct mptcp_options_received {
  };
 
  struct mptcp_tcp_sock {
-	struct tcp_sock	*next;		/* Next subflow socket */
+	struct hlist_node node;
  	struct hlist_node cb_list;
  	struct mptcp_options_received rx_opt;
 
@@ -257,7 +257,7 @@ struct mptcp_sched_ops {
 
  struct mptcp_cb {
  	/* list of sockets in this multipath connection */
-	struct tcp_sock *connection_list;
+	struct hlist_head conn_list;
  	/* list of sockets that need a call to release_cb */
  	struct hlist_head callback_list;
 
@@ -312,7 +312,7 @@ struct mptcp_cb {
  	/***** Start of fields, used for subflow establishment */
  	struct sock *meta_sk;
 
-	/* Master socket, also part of the connection_list, this
+	/* Master socket, also part of the conn_list, this
  	 * socket is the one that the application sees.
  	 */
  	struct sock *master_sk;
@@ -664,21 +664,17 @@ extern struct workqueue_struct *mptcp_wq;
  			pr_err(fmt, ##args);					\
  	} while (0)
 
-/* Iterates over all subflows */
-#define mptcp_for_each_tp(mpcb, tp)					\
-	for ((tp) = (mpcb)->connection_list; (tp); (tp) = (tp)->mptcp->next)
+static inline struct sock *mptcp_to_sock(const struct mptcp_tcp_sock *mptcp)
+{
+	return (struct sock *)mptcp->tp;
+}
 
-#define mptcp_for_each_sk(mpcb, sk)					\
-	for ((sk) = (struct sock *)(mpcb)->connection_list;		\
-	     sk;							\
-	     sk = (struct sock *)tcp_sk(sk)->mptcp->next)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	hlist_for_each_entry_rcu(__mptcp, &((__mpcb)->conn_list), node)
 
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)			\
-	for (__sk = (struct sock *)(__mpcb)->connection_list,		\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL; \
-	     __sk;							\
-	     __sk = __temp,						\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL)
+/* Must be called with the appropriate lock held */
+#define mptcp_for_each_sub_safe(__mpcb, __mptcp, __tmp)				\
+	hlist_for_each_entry_safe(__mptcp, __tmp, &((__mpcb)->conn_list), node)
 
  /* Iterates over all bit set to 1 in a bitset */
  #define mptcp_for_each_bit_set(b, i)					\
@@ -932,12 +928,14 @@ struct request_sock *rev_mptcp_rsk(const struct mptcp_request_sock *req)
 
  static inline bool mptcp_can_sendpage(struct sock *sk)
  {
-	struct sock *sk_it;
+	struct mptcp_tcp_sock *mptcp;
 
  	if (tcp_sk(sk)->mpcb->dss_csum)
  		return false;
 
-	mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+	mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if (!(sk_it->sk_route_caps & NETIF_F_SG) ||
  		    !sk_check_csum_caps(sk_it))
  			return false;
@@ -972,9 +970,12 @@ static inline void mptcp_send_reset(struct sock *sk)
  static inline void mptcp_sub_force_close_all(struct mptcp_cb *mpcb,
  					     struct sock *except)
  {
-	struct sock *sk_it, *tmp;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
+
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
 
-	mptcp_for_each_sk_safe(mpcb, sk_it, tmp) {
  		if (sk_it != except)
  			mptcp_send_reset(sk_it);
  	}
@@ -1161,12 +1162,14 @@ static inline int mptcp_sk_can_send_ack(const struct sock *sk)
  /* Only support GSO if all subflows supports it */
  static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 
  	if (tcp_sk(meta_sk)->mpcb->dss_csum)
  		return false;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
  		if (!sk_can_gso(sk))
@@ -1177,12 +1180,14 @@ static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
 
  static inline bool mptcp_can_sg(const struct sock *meta_sk)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 
  	if (tcp_sk(meta_sk)->mpcb->dss_csum)
  		return false;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
  		if (!(sk->sk_route_caps & NETIF_F_SG))
@@ -1193,9 +1198,9 @@ static inline bool mptcp_can_sg(const struct sock *meta_sk)
 
  static inline void mptcp_set_rto(struct sock *sk)
  {
-	struct tcp_sock *tp = tcp_sk(sk);
-	struct sock *sk_it;
  	struct inet_connection_sock *micsk = inet_csk(mptcp_meta_sk(sk));
+	struct tcp_sock *tp = tcp_sk(sk);
+	struct mptcp_tcp_sock *mptcp;
  	__u32 max_rto = 0;
 
  	/* We are in recovery-phase on the MPTCP-level. Do not update the
@@ -1204,7 +1209,9 @@ static inline void mptcp_set_rto(struct sock *sk)
  	if (micsk->icsk_retransmits)
  		return;
 
-	mptcp_for_each_sk(tp->mpcb, sk_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if ((mptcp_sk_can_send(sk_it) || sk->sk_state == TCP_SYN_RECV) &&
  		    inet_csk(sk_it)->icsk_rto > max_rto)
  			max_rto = inet_csk(sk_it)->icsk_rto;
@@ -1303,12 +1310,8 @@ bool mptcp_prune_ofo_queue(struct sock *sk);
  	do {				\
  	} while (0)
 
-/* Without MPTCP, we just do one iteration
- * over the only socket available. This assumes that
- * the sk/tp arg is the socket in that case.
- */
-#define mptcp_for_each_sk(mpcb, sk)
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	if (0)
 
  #define MPTCP_INC_STATS(net, field)	\
  	do {				\
net/ipv4/af_inet.c +5 -4
index 426fa704e218..87a98edc18d4 100644
@@ -709,13 +709,14 @@ int inet_accept(struct socket *sock, struct socket *newsock, int flags,
  	sock_rps_record_flow(sk2);
 
  	if (sk2->sk_protocol == IPPROTO_TCP && mptcp(tcp_sk(sk2))) {
-		struct sock *sk_it = sk2;
+		struct mptcp_tcp_sock *mptcp;
 
-		mptcp_for_each_sk(tcp_sk(sk2)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tcp_sk(sk2)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
 
  		if (tcp_sk(sk2)->mpcb->master_sk) {
-			sk_it = tcp_sk(sk2)->mpcb->master_sk;
+			struct sock *sk_it = tcp_sk(sk2)->mpcb->master_sk;
 
  			write_lock_bh(&sk_it->sk_callback_lock);
  			sk_it->sk_wq = newsock->wq;
net/ipv4/ip_sockglue.c +5 -2
index 1fe79c7bcc9e..2b7a8c416c91 100644
@@ -756,8 +756,11 @@ static int do_ip_setsockopt(struct sock *sk, int level,
  			sk_dst_reset(sk);
  			/* Update TOS on mptcp subflow */
  			if (is_meta_sk(sk)) {
-				struct sock *sk_it;
-				mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+				struct mptcp_tcp_sock *mptcp;
+
+				mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+					struct sock *sk_it = mptcp_to_sock(mptcp);
+
  					if (inet_sk(sk_it)->tos != inet_sk(sk)->tos) {
  						inet_sk(sk_it)->tos = inet_sk(sk)->tos;
  						sk_it->sk_priority = sk->sk_priority;
net/ipv4/tcp.c +19 -12
index 93e15eeba870..fd53722bf59e 100644
@@ -804,9 +804,11 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
 
  #ifdef CONFIG_MPTCP
  	if (mptcp(tcp_sk(sk))) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  #endif
  	/*
@@ -984,7 +986,7 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  	}
 
  	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
+		struct mptcp_tcp_sock *mptcp;
 
  		/* We must check this with socket-lock hold because we iterate
  		 * over the subflows.
@@ -999,8 +1001,9 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  			return ret;
  		}
 
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
 
  	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
@@ -1293,9 +1296,11 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
  	}
 
  	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
 
  	if (unlikely(tp->repair)) {
@@ -1883,9 +1888,11 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
 
  #ifdef CONFIG_MPTCP
  	if (mptcp(tp)) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  #endif
 
net/mptcp/mctcp_desync.c +11 -7
index cfc26fcc3161..f6bf9251d59b 100644
@@ -67,7 +67,6 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  	} else {
  		const struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
  		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
 
  		/* current aggregated cwnd */
  		u32 agg_cwnd = 0;
@@ -90,11 +89,14 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  				tp->snd_ssthresh = tp->snd_cwnd = INI_MIN_CWND;
  			}
  			return;
-		}
-		  /* In dangerous area, increase slowly and linearly. */
-		  else {
+		} else {
+			/* In dangerous area, increase slowly and linearly. */
+			const struct mptcp_tcp_sock *mptcp;
+
  			/* get total cwnd and the subflow that has min cwnd */
-			mptcp_for_each_sk(tp->mpcb, sub_sk) {
+			mptcp_for_each_sub(tp->mpcb, mptcp) {
+				const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
  				if (mctcp_cc_sk_can_send(sub_sk)) {
  					const struct tcp_sock *sub_tp =
  								tcp_sk(sub_sk);
@@ -126,12 +128,14 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
  	} else {
  		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
  		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
+		const struct mptcp_tcp_sock *mptcp;
  		u32 max_cwnd = 0;
  		u8 max_cwnd_subfid = 0;
 
  		/* Find the subflow that has the max cwnd. */
-		mptcp_for_each_sk(tp->mpcb, sub_sk) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
  			if (mctcp_cc_sk_can_send(sub_sk)) {
  				const struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  				if (max_cwnd < sub_tp->snd_cwnd) {
net/mptcp/mptcp_balia.c +5 -3
index ac5c5e550539..e8a22fa66a50 100644
@@ -78,7 +78,7 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
  	const struct mptcp_cb *mpcb = tp->mpcb;
-	const struct sock *sub_sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 max_rate = 0, rate = 0, sum_rate = 0;
  	u64 alpha, ai = tp->snd_cwnd, md = (tp->snd_cwnd >> 1);
  	int num_scale_down = 0;
@@ -87,7 +87,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  		return;
 
  	/* Find max_rate first */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		u64 tmp;
 
@@ -119,7 +120,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
 
  	if (num_scale_down) {
  		sum_rate = 0;
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			u64 tmp;
 
net/mptcp/mptcp_binder.c +7 -3
index d6ef9c5b6ce5..eaf8ba674d17 100644
@@ -46,6 +46,8 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
  	unsigned char *opt_ptr, *opt_end_ptr, opt[MAX_IPOPTLEN];
 
  	for (i = 0; i < MPTCP_GW_MAX_LISTS; ++i) {
+		struct mptcp_tcp_sock *mptcp;
+
  		if (mptcp_gws->len[i] == 0)
  			goto error;
 
@@ -53,7 +55,9 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
  		list_taken = 0;
 
  		/* Loop through all sub-sockets in this connection */
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk) {
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
  			mptcp_debug("mptcp_get_avail_list_ipv4: Next sock\n");
 
  			/* Reset length and options buffer, then retrieve
@@ -289,10 +293,10 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
 
  static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int i = 0;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		i++;
  	}
 
net/mptcp/mptcp_coupled.c +7 -4
index a6569f5e20ab..e1040a23e59f 100644
@@ -80,7 +80,7 @@ static inline void mptcp_set_forced(const struct sock *meta_sk, bool force)
  static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(sk)->mpcb;
-	const struct sock *sub_sk;
+	const struct mptcp_tcp_sock *mptcp;
  	int best_cwnd = 0, best_rtt = 0, can_send = 0;
  	u64 max_numerator = 0, sum_denominator = 0, alpha = 1;
 
@@ -90,7 +90,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	/* Do regular alpha-calculation for multiple subflows */
 
  	/* Find the max numerator of the alpha-calculation */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		u64 tmp;
 
@@ -118,7 +119,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  		goto exit;
 
  	/* Calculate the denominator */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
 
  		if (!mptcp_ccc_sk_can_send(sub_sk))
@@ -132,7 +134,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	sum_denominator *= sum_denominator;
  	if (unlikely(!sum_denominator)) {
  		pr_err("%s: sum_denominator == 0\n", __func__);
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			pr_err("%s: pi:%d, state:%d\n, rtt:%u, cwnd: %u",
  			       __func__, sub_tp->mptcp->path_index,
net/mptcp/mptcp_ctrl.c +55 -34
index 9237a73ec174..fe824649bcc3 100644
@@ -553,10 +553,12 @@ void mptcp_hash_remove_bh(struct tcp_sock *meta_tp)
  struct sock *mptcp_select_ack_sock(const struct sock *meta_sk)
  {
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk, *rttsk = NULL, *lastsk = NULL;
+	struct sock *rttsk = NULL, *lastsk = NULL;
  	u32 min_time = 0, last_active = 0;
+	struct mptcp_tcp_sock *mptcp;
 
-	mptcp_for_each_sk(meta_tp->mpcb, sk) {
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		u32 elapsed;
 
@@ -689,7 +691,8 @@ void mptcp_sock_destruct(struct sock *sk)
  void mptcp_destroy_sock(struct sock *sk)
  {
  	if (is_meta_sk(sk)) {
-		struct sock *sk_it, *tmpsk;
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
 
  		__skb_queue_purge(&tcp_sk(sk)->mpcb->reinject_queue);
 
@@ -699,7 +702,9 @@ void mptcp_destroy_sock(struct sock *sk)
  		 * not have been closed properly (as we are waiting for the
  		 * DATA_ACK of the DATA_FIN).
  		 */
-		mptcp_for_each_sk_safe(tcp_sk(sk)->mpcb, sk_it, tmpsk) {
+		mptcp_for_each_sub_safe(tcp_sk(sk)->mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
+
  			/* Already did call tcp_close - waiting for graceful
  			 * closure, or if we are retransmitting fast-close on
  			 * the subflow. The reset (or timeout) will kill the
@@ -1281,6 +1286,7 @@ static int mptcp_alloc_mpcb(struct sock *meta_sk, __u64 remote_key,
  	INIT_LIST_HEAD(&mpcb->tw_list);
 
  	INIT_HLIST_HEAD(&mpcb->callback_list);
+	INIT_HLIST_HEAD(&mpcb->conn_list);
  	spin_lock_init(&mpcb->mpcb_list_lock);
 
  	mptcp_mpcb_inherit_sockopts(meta_sk, master_sk);
@@ -1370,8 +1376,12 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  	sock_hold(meta_sk);
  	refcount_inc(&mpcb->mpcb_refcnt);
 
-	tp->mptcp->next = mpcb->connection_list;
-	mpcb->connection_list = tp;
+	local_bh_disable();
+	spin_lock(&mpcb->mpcb_list_lock);
+	hlist_add_head_rcu(&tp->mptcp->node, &mpcb->conn_list);
+	spin_unlock(&mpcb->mpcb_list_lock);
+	local_bh_enable();
+
  	tp->mptcp->attached = 1;
 
  	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
@@ -1415,14 +1425,13 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
 
  void mptcp_del_sock(struct sock *sk)
  {
-	struct tcp_sock *tp = tcp_sk(sk), *tp_prev;
+	struct tcp_sock *tp = tcp_sk(sk);
  	struct mptcp_cb *mpcb;
 
  	if (!tp->mptcp || !tp->mptcp->attached)
  		return;
 
  	mpcb = tp->mpcb;
-	tp_prev = mpcb->connection_list;
 
  	if (mpcb->sched_ops->release)
  		mpcb->sched_ops->release(sk);
@@ -1434,17 +1443,10 @@ void mptcp_del_sock(struct sock *sk)
  		    __func__, mpcb->mptcp_loc_token, tp->mptcp->path_index,
  		    sk->sk_state, is_meta_sk(sk));
 
-	if (tp_prev == tp) {
-		mpcb->connection_list = tp->mptcp->next;
-	} else {
-		for (; tp_prev && tp_prev->mptcp->next; tp_prev = tp_prev->mptcp->next) {
-			if (tp_prev->mptcp->next == tp) {
-				tp_prev->mptcp->next = tp->mptcp->next;
-				break;
-			}
-		}
-	}
-	tp->mptcp->next = NULL;
+	spin_lock(&mpcb->mpcb_list_lock);
+	hlist_del_init_rcu(&tp->mptcp->node);
+	spin_unlock(&mpcb->mpcb_list_lock);
+
  	tp->mptcp->attached = 0;
  	mpcb->path_index_bits &= ~(1 << tp->mptcp->path_index);
 
@@ -1491,8 +1493,8 @@ void mptcp_update_metasocket(const struct sock *meta_sk)
  void mptcp_cleanup_rbuf(struct sock *meta_sk, int copied)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk;
  	bool recheck_rcv_window = false;
+	struct mptcp_tcp_sock *mptcp;
  	__u32 rcv_window_now = 0;
 
  	if (copied > 0 && !(meta_sk->sk_shutdown & RCV_SHUTDOWN)) {
@@ -1503,7 +1505,8 @@ void mptcp_cleanup_rbuf(struct sock *meta_sk, int copied)
  			recheck_rcv_window = true;
  	}
 
-	mptcp_for_each_sk(meta_tp->mpcb, sk) {
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		const struct inet_connection_sock *icsk = inet_csk(sk);
 
@@ -1690,10 +1693,13 @@ EXPORT_SYMBOL(mptcp_sub_force_close);
   */
  void mptcp_update_sndbuf(const struct tcp_sock *tp)
  {
-	struct sock *meta_sk = tp->meta_sk, *sk;
+	struct sock *meta_sk = tp->meta_sk;
  	int new_sndbuf = 0, old_sndbuf = meta_sk->sk_sndbuf;
+	struct mptcp_tcp_sock *mptcp;
+
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
 
-	mptcp_for_each_sk(tp->mpcb, sk) {
  		if (!mptcp_sk_can_send(sk))
  			continue;
 
@@ -1722,8 +1728,8 @@ void mptcp_update_sndbuf(const struct tcp_sock *tp)
  void mptcp_close(struct sock *meta_sk, long timeout)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk_it, *tmpsk;
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb;
  	int data_was_unread = 0;
  	int state;
@@ -1756,7 +1762,12 @@ void mptcp_close(struct sock *meta_sk, long timeout)
 
  	/* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
  	if (meta_sk->sk_state == TCP_CLOSE) {
-		mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
+
+		mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk_it)->send_mp_fclose)
  				continue;
  			mptcp_sub_close(sk_it, 0);
@@ -1777,10 +1788,14 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	} else if (tcp_close_state(meta_sk)) {
  		mptcp_send_fin(meta_sk);
  	} else if (meta_tp->snd_una == meta_tp->write_seq) {
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
+
  		/* The DATA_FIN has been sent and acknowledged
  		 * (e.g., by sk_shutdown). Close all the other subflows
  		 */
-		mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
+		mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			unsigned long delay = 0;
  			/* If we are the passive closer, don't trigger
  			 * subflow-fin until the subflow has been finned
@@ -1804,7 +1819,9 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	/* socket will be freed after mptcp_close - we have to prevent
  	 * access from the subflows.
  	 */
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		/* Similar to sock_orphan, but we don't set it DEAD, because
  		 * the callbacks are still set and must be called.
  		 */
@@ -1889,8 +1906,9 @@ void mptcp_close(struct sock *meta_sk, long timeout)
 
  void mptcp_disconnect(struct sock *sk)
  {
-	struct sock *subsk, *tmpsk;
+	struct mptcp_tcp_sock *mptcp;
  	struct tcp_sock *tp = tcp_sk(sk);
+	struct hlist_node *tmp;
 
  	__skb_queue_purge(&tp->mpcb->reinject_queue);
 
@@ -1898,7 +1916,9 @@ void mptcp_disconnect(struct sock *sk)
  		mptcp_hash_remove_bh(tp);
 
  	local_bh_disable();
-	mptcp_for_each_sk_safe(tp->mpcb, subsk, tmpsk) {
+	mptcp_for_each_sub_safe(tp->mpcb, mptcp, tmp) {
+		struct sock *subsk = mptcp_to_sock(mptcp);
+
  		/* The socket will get removed from the subsocket-list
  		 * and made non-mptcp by setting mpc to 0.
  		 *
@@ -2582,7 +2602,6 @@ static void mptcp_get_sub_info(struct sock *sk, struct mptcp_sub_info *info)
  int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  {
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk;
 
  	struct mptcp_meta_info meta_info;
  	struct mptcp_info m_info;
@@ -2628,16 +2647,17 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
 
  	if (m_info.subflows) {
  		unsigned int len, sub_len = 0;
+		struct mptcp_tcp_sock *mptcp;
  		char __user *ptr;
 
  		ptr = (char __user *)m_info.subflows;
  		len = m_info.sub_len;
 
-		mptcp_for_each_sk(meta_tp->mpcb, sk) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
  			struct tcp_info t_info;
  			unsigned int tmp_len;
 
-			tcp_get_info(sk, &t_info);
+			tcp_get_info(mptcp_to_sock(mptcp), &t_info);
 
  			tmp_len = min_t(unsigned int, len, info_len);
  			len -= tmp_len;
@@ -2657,6 +2677,7 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
 
  	if (m_info.subflow_info) {
  		unsigned int len, sub_info_len, total_sub_info_len = 0;
+		struct mptcp_tcp_sock *mptcp;
  		char __user *ptr;
 
  		ptr = (char __user *)m_info.subflow_info;
@@ -2666,11 +2687,11 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  				     sizeof(struct mptcp_sub_info));
  		m_info.sub_info_len = sub_info_len;
 
-		mptcp_for_each_sk(meta_tp->mpcb, sk) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
  			struct mptcp_sub_info m_sub_info;
  			unsigned int tmp_len;
 
-			mptcp_get_sub_info(sk, &m_sub_info);
+			mptcp_get_sub_info(mptcp_to_sock(mptcp), &m_sub_info);
 
  			tmp_len = min_t(unsigned int, len, sub_info_len);
  			len -= tmp_len;
net/mptcp/mptcp_fullmesh.c +20 -8
index d29102a1dffb..53c1ba7319f5 100644
@@ -903,8 +903,9 @@ static void mptcp_address_worker(struct work_struct *work)
  			}
 
  			if (event->code == MPTCP_EVENT_DEL) {
-				struct sock *sk, *tmpsk;
+				struct mptcp_tcp_sock *mptcp;
  				struct mptcp_loc_addr *mptcp_local;
+				struct hlist_node *tmp;
  				bool found = false;
 
  				mptcp_local = rcu_dereference_bh(fm_ns->local);
@@ -914,7 +915,9 @@ static void mptcp_address_worker(struct work_struct *work)
  					update_addr_bitfields(meta_sk, mptcp_local);
 
  				/* Look for the socket and remove him */
-				mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+				mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
+
  					if ((event->family == AF_INET6 &&
  					     (sk->sk_family == AF_INET ||
  					      mptcp_v6_is_v4_mapped(sk))) ||
@@ -964,9 +967,10 @@ static void mptcp_address_worker(struct work_struct *work)
  			}
 
  			if (event->code == MPTCP_EVENT_MOD) {
-				struct sock *sk;
+				struct mptcp_tcp_sock *mptcp;
 
-				mptcp_for_each_sk(mpcb, sk) {
+				mptcp_for_each_sub(mpcb, mptcp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
  					struct tcp_sock *tp = tcp_sk(sk);
  					if (event->family == AF_INET &&
  					    (sk->sk_family == AF_INET ||
@@ -1455,8 +1459,9 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  	struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
  	struct fullmesh_priv *fmp = fullmesh_get_priv(mpcb);
  	const struct mptcp_fm_ns *fm_ns = fm_get_ns(sock_net(meta_sk));
-	struct sock *sk, *tmpsk;
  	bool meta_v4 = meta_sk->sk_family == AF_INET;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
  	int i;
 
  	rcu_read_lock_bh();
@@ -1470,7 +1475,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		struct in_addr ifa = mptcp_local->locaddr4[i].addr;
  		bool found = false;
 
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp = tcp_sk(sk);
 
  			if (sk->sk_family == AF_INET6 &&
@@ -1491,6 +1497,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		}
 
  		if (!found) {
+			struct sock *sk;
+
  			fmp->add_addr++;
  			mpcb->addr_signal = 1;
 
@@ -1511,7 +1519,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		struct in6_addr ifa = mptcp_local->locaddr6[i].addr;
  		bool found = false;
 
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp = tcp_sk(sk);
 
  			if (sk->sk_family == AF_INET ||
@@ -1532,6 +1541,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		}
 
  		if (!found) {
+			struct sock *sk;
+
  			fmp->add_addr++;
  			mpcb->addr_signal = 1;
 
@@ -1546,7 +1557,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  #endif
 
  	/* Now, detect address-removals */
-	mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		bool shall_remove = true;
 
  		if (sk->sk_family == AF_INET || mptcp_v6_is_v4_mapped(sk)) {
net/mptcp/mptcp_input.c +29 -15
index 211605347f70..64b0265bfd06 100644
@@ -126,12 +126,14 @@ static void mptcp_clean_rtx_queue(struct sock *meta_sk, u32 prior_snd_una)
  		tcp_rtx_queue_unlink(skb, meta_sk);
 
  		if (mptcp_is_data_fin(skb)) {
-			struct sock *sk_it, *sk_tmp;
+			struct mptcp_tcp_sock *mptcp;
+			struct hlist_node *tmp;
 
  			/* DATA_FIN has been acknowledged - now we can close
  			 * the subflows
  			 */
-			mptcp_for_each_sk_safe(mpcb, sk_it, sk_tmp) {
+			mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+				struct sock *sk_it = mptcp_to_sock(mptcp);
  				unsigned long delay = 0;
 
  				/* If we are the passive closer, don't trigger
@@ -347,6 +349,7 @@ static int mptcp_verif_dss_csum(struct sock *sk)
 
  	/* Now, checksum must be 0 */
  	if (unlikely(csum_fold(csum_tcp))) {
+		struct mptcp_tcp_sock *mptcp;
  		struct sock *sk_it = NULL;
 
  		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
@@ -362,7 +365,9 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
 
  		/* Search for another subflow that is fully established */
-		mptcp_for_each_sk(tp->mpcb, sk_it) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sk_it = mptcp_to_sock(mptcp);
+
  			if (sk_it != sk &&
  			    tcp_sk(sk_it)->mptcp->fully_established)
  				break;
@@ -1308,12 +1313,15 @@ int mptcp_do_join_short(struct sk_buff *skb,
   */
  void mptcp_fin(struct sock *meta_sk)
  {
-	struct sock *sk = NULL, *sk_it;
+	struct sock *sk = NULL;
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
+	struct mptcp_tcp_sock *mptcp;
  	unsigned char state;
 
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if (tcp_sk(sk_it)->mptcp->path_index == mpcb->dfin_path_index) {
  			sk = sk_it;
  			break;
@@ -1585,9 +1593,12 @@ void mptcp_clean_rtx_infinite(const struct sk_buff *skb, struct sock *sk)
 
  static void mptcp_send_reset_rem_id(const struct mptcp_cb *mpcb, u8 rem_id)
  {
-	struct sock *sk_it, *tmpsk;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
+
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
 
-	mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
  		if (tcp_sk(sk_it)->mptcp->rem_id == rem_id) {
  			mptcp_reinject_data(sk_it, 0);
  			mptcp_send_reset(sk_it);
@@ -1892,13 +1903,15 @@ void tcp_parse_mptcp_options(const struct sk_buff *skb,
  bool mptcp_check_rtt(const struct tcp_sock *tp, int time)
  {
  	struct mptcp_cb *mpcb = tp->mpcb;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u32 rtt_max = 0;
 
  	/* In MPTCP, we take the max delay across all flows,
  	 * in order to take into account meta-reordering buffers.
  	 */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_recv(sk))
  			continue;
 
@@ -2173,9 +2186,9 @@ bool mptcp_handle_options(struct sock *sk, const struct tcphdr *th,
  		if (mopt->saw_low_prio == 1) {
  			tp->mptcp->rcv_low_prio = mopt->low_prio;
  		} else {
-			struct sock *sk_it;
-			mptcp_for_each_sk(tp->mpcb, sk_it) {
-				struct mptcp_tcp_sock *mptcp = tcp_sk(sk_it)->mptcp;
+			struct mptcp_tcp_sock *mptcp;
+
+			mptcp_for_each_sub(tp->mpcb, mptcp) {
  				if (mptcp->rem_id == mopt->prio_addr_id)
  					mptcp->rcv_low_prio = mopt->low_prio;
  			}
@@ -2359,7 +2372,7 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  {
  	const struct sock *meta_sk = mptcp_meta_sk(sk);
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	const struct sock *sk_it;
+	const struct mptcp_tcp_sock *mptcp;
 
  	/* We circumvent this check in tcp_check_space, because we want to
  	 * always call sk_write_space. So, we reproduce the check here.
@@ -2385,8 +2398,9 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  	/* For MPTCP we look for a subsocket that could send data.
  	 * If we found one, then we update the send-buffer.
  	 */
-	mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
-		struct tcp_sock *tp_it = tcp_sk(sk_it);
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		const struct sock *sk_it = mptcp_to_sock(mptcp);
+		const struct tcp_sock *tp_it = tcp_sk(sk_it);
 
  		if (!mptcp_sk_can_send(sk_it))
  			continue;
net/mptcp/mptcp_ndiffports.c +2 -2
index fe73ade7ba24..4219e9489655 100644
@@ -20,10 +20,10 @@ MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connecti
 
  static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int i = 0;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		i++;
  	}
 
net/mptcp/mptcp_olia.c +13 -7
index 9f61e1eff65a..c44eb9208581 100644
@@ -61,10 +61,11 @@ static u32 mptcp_get_crt_cwnd(struct sock *sk)
  /* return the dominator of the first term of  the increasing term */
  static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 rate = 1; /* We have to avoid a zero-rate because it is used as a divisor */
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		u64 scaled_num;
  		u32 tmp_cwnd;
@@ -83,10 +84,11 @@ static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
  /* find the maximum cwnd, used to find set M */
  static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u32 best_cwnd = 0;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		u32 tmp_cwnd;
 
  		if (!mptcp_olia_sk_can_send(sk))
@@ -101,6 +103,7 @@ static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
 
  static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  {
+	struct mptcp_tcp_sock *mptcp;
  	struct mptcp_olia *ca;
  	struct tcp_sock *tp;
  	struct sock *sk;
@@ -113,7 +116,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	max_cwnd = mptcp_get_max_cwnd(mpcb);
 
  	/* find the best path */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
 
@@ -135,7 +139,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
 
  	/* TODO - integrate this here in mptcp_get_max_cwnd and in the previous loop */
  	/* find the size of M and B_not_M */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
 
@@ -156,7 +161,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	}
 
  	/* check if the path is in M or B_not_M and set the value of epsilon accordingly */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
 
net/mptcp/mptcp_output.c +0 -1
index cc1a4f7d39e9..29a7ab309751 100644
@@ -647,7 +647,6 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct sk_buff *skb;
-	struct sock *sk_it;
  	int ans = 0;
 
  	if (meta_sk->sk_state == TCP_CLOSE)

net/mptcp/mptcp_output.c int mptcp_write_wakeup(struct sock *meta_sk, int mib) · line 707

Quoted 5 lines
net/mptcp/mptcp_output.c +2 -0
@@ -704,17 +703,22 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
 
  		return 0;
  	} else {
+		struct mptcp_tcp_sock *mptcp;
+

detail: that's strange to see the declaration of the variable just before the flag :)
But that's correct.

Quoted 174 lines
net/mptcp/mptcp_output.c +23 -12
  window_probe:
  		if (between(meta_tp->snd_up, meta_tp->snd_una + 1,
  			    meta_tp->snd_una + 0xFFFF)) {
-			mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+			mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+				struct sock *sk_it = mptcp_to_sock(mptcp);
+
  				if (mptcp_sk_can_send_ack(sk_it))
  					tcp_xmit_probe_skb(sk_it, 1, mib);
  			}
  		}
 
  		/* At least one of the tcp_xmit_probe_skb's has to succeed */
-		mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			int ret;
 
  			if (!mptcp_sk_can_send_ack(sk_it))
@@ -732,6 +736,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  		     int push_one, gfp_t gfp)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk), *subtp;
+	struct mptcp_tcp_sock *mptcp;
  	struct sock *subsk = NULL;
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sk_buff *skb;
@@ -856,7 +861,8 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  			break;
  	}
 
-	mptcp_for_each_sk(mpcb, subsk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		subsk = mptcp_to_sock(mptcp);
  		subtp = tcp_sk(subsk);
 
  		if (!(path_mask & mptcp_pi_to_flag(subtp->mptcp->path_index)))
@@ -1353,7 +1359,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sock *sk;
 
-	if (!mpcb->connection_list)
+	if (hlist_empty(&mpcb->conn_list))
  		return;
 
  	WARN_ON(meta_tp->send_mp_fclose);
@@ -1728,10 +1734,11 @@ void mptcp_select_initial_window(const struct sock *sk, int __space, __u32 mss,
  static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
  				  unsigned int (*mss_cb)(struct sock *sk))
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 rate = 0;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		int this_mss;
  		u64 this_rate;
@@ -1783,11 +1790,12 @@ static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
  static unsigned int __mptcp_current_mss(const struct sock *meta_sk,
  					unsigned int (*mss_cb)(struct sock *sk))
  {
+	struct mptcp_tcp_sock *mptcp;
  	unsigned int mss = 0;
  	u64 rate = 0;
-	struct sock *sk;
 
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		int this_mss;
  		u64 this_rate;
 
@@ -1852,14 +1860,16 @@ int mptcp_select_size(const struct sock *meta_sk, bool sg, bool first_skb, bool
 
  int mptcp_check_snd_buf(const struct tcp_sock *tp)
  {
-	const struct sock *sk;
+	const struct mptcp_tcp_sock *mptcp;
  	u32 rtt_max = tp->srtt_us;
  	u64 bw_est;
 
  	if (!tp->srtt_us)
  		return tp->reordering + 1;
 
-	mptcp_for_each_sk(tp->mpcb, sk) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		const struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
 
@@ -1877,11 +1887,13 @@ int mptcp_check_snd_buf(const struct tcp_sock *tp)
  unsigned int mptcp_xmit_size_goal(const struct sock *meta_sk, u32 mss_now,
  				  int large_allowed)
  {
-	struct sock *sk;
  	u32 xmit_size_goal = 0;
 
  	if (large_allowed && mptcp_sk_can_gso(meta_sk)) {
-		mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			int this_size_goal;
 
  			if (!mptcp_sk_can_send(sk))
net/mptcp/mptcp_redundant.c +18 -9
index 001a3f8a974d..6c68a8a071c1 100644
@@ -52,10 +52,12 @@ static bool redsched_get_active_valid_sks(struct sock *meta_sk)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int active_valid_sks = 0;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (subflow_is_active((struct tcp_sock *)sk) &&
  		    !mptcp_is_def_unavailable(sk))
  			active_valid_sks++;
@@ -88,6 +90,10 @@ static bool redsched_use_subflow(struct sock *meta_sk,
  	return false;
  }
 
+#define mptcp_entry_next_rcu(__mptcp)						\
+	hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(			\
+		&(__mptcp)->node)), struct mptcp_tcp_sock, node)
+
  static struct sock *redundant_get_subflow(struct sock *meta_sk,
  					  struct sk_buff *skb,
  					  bool zero_wnd_test)
@@ -95,14 +101,16 @@ static struct sock *redundant_get_subflow(struct sock *meta_sk,
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct redsched_cb_data *cb_data = redsched_get_cb_data(meta_tp);
-	struct tcp_sock *first_tp = cb_data->next_subflow;
-	struct sock *sk;
-	struct tcp_sock *tp;
+	struct tcp_sock *first_tp = cb_data->next_subflow, *tp;
+	struct mptcp_tcp_sock *mptcp;
+	int found = 0;
 
  	/* Answer data_fin on same subflow */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk)->mptcp->path_index ==
  				mpcb->dfin_path_index &&
  			    mptcp_is_available(sk, skb, zero_wnd_test))
@@ -110,26 +118,58 @@ static struct sock *redundant_get_subflow(struct sock *meta_sk,
  		}
  	}
 
-	if (!first_tp)
-		first_tp = mpcb->connection_list;
+	if (!first_tp && !hlist_empty(&mpcb->conn_list)) {
+		first_tp = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(&mpcb->conn_list)),
+					    struct mptcp_tcp_sock, node)->tp;
+	}
  	tp = first_tp;
 
-	/* still NULL (no subflow in connection_list?) */

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 135

Quoted 10 lines
net/mptcp/mptcp_redundant.c +5 -1
+	/* still NULL (no subflow in conn_list?) */
  	if (!first_tp)
  		return NULL;
 
  	/* Search for any subflow to send it */
-	do {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		/* We go up to the subflow 'tp', then we pick the next one. */
+		if (!found)
+			continue;

I guess I missed something in the patch but found will always be false here, we will never do the rest, no?

Quoted 7 lines
net/mptcp/mptcp_redundant.c +5 -0
+		if (tp == mptcp->tp) {
+			found = 1;
+			continue; > +		}
+		tp = mptcp->tp;
+
  		if (mptcp_is_available((struct sock *)tp, skb,
  				       zero_wnd_test)) {

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 152

Quoted 10 lines
net/mptcp/mptcp_redundant.c +7 -1
-			cb_data->next_subflow = tp->mptcp->next;
+			struct mptcp_tcp_sock *mptcp;
+
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
  			return (struct sock *)tp;
  		}

Should we not break here?

Quoted 14 lines
net/mptcp/mptcp_redundant.c +11 -2
+	}
+
+	mptcp_for_each_sub(mpcb, mptcp) {
+		tp = mptcp->tp;
+
+		if (tp == first_tp)
+			break;
+
+		if (mptcp_is_available((struct sock *)tp, skb,
+				       zero_wnd_test)) {
+			struct mptcp_tcp_sock *mptcp;
 
-		tp = tp->mptcp->next;
-		if (!tp)

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 172

Quoted 10 lines
net/mptcp/mptcp_redundant.c +8 -2
-			tp = mpcb->connection_list;
-	} while (tp != first_tp);
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
+			return (struct sock *)tp;
+		}
+	}

I am not sure I understand why you need two mptcp_for_each_sub here. May you add a comment? :)

Quoted 35 lines
net/mptcp/mptcp_redundant.c +9 -6
  	/* No space */
  	return NULL;
@@ -190,10 +230,11 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct redsched_cb_data *cb_data = redsched_get_cb_data(meta_tp);
-	struct tcp_sock *first_tp = cb_data->next_subflow;
-	struct tcp_sock *tp;
-	struct sk_buff *skb;
+	struct tcp_sock *first_tp = cb_data->next_subflow, *tp;
+	struct mptcp_tcp_sock *mptcp;
  	int active_valid_sks = -1;
+	struct sk_buff *skb;
+	int found = 0;
 
  	/* As we set it, we have to reset it as well. */
  	*limit = 0;
@@ -215,10 +256,12 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
 
  	/* Then try indistinctly redundant and normal skbs */
 
-	if (!first_tp)
-		first_tp = mpcb->connection_list;
+	if (!first_tp && !hlist_empty(&mpcb->conn_list)) {
+		first_tp = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(&mpcb->conn_list)),
+					    struct mptcp_tcp_sock, node)->tp;
+	}
 
-	/* still NULL (no subflow in connection_list?) */
+	/* still NULL (no subflow in conn_list?) */
  	if (!first_tp)
  		return NULL;
 

net/mptcp/mptcp_redundant.c static struct sk_buff *redundant_next_segment(struct sock *meta_sk, · line 276

Quoted 9 lines
net/mptcp/mptcp_redundant.c +3 -1
@@ -226,9 +269,17 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
 
  	*reinject = 0;
  	active_valid_sks = redsched_get_active_valid_sks(meta_sk);
-	do {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		struct redsched_sock_data *sk_data;
 
+		if (!found)
+			continue;

Same here, I guess I missed a goto somewhere but found will always be 0 here.

Quoted 56 lines
net/mptcp/mptcp_redundant.c +35 -5
+		if (tp == mptcp->tp) {
+			found = 1;
+			continue;
+		}
+
  		/* Correct the skb pointers of the current subflow */
  		sk_data = redsched_get_sock_data(tp);
  		redsched_correct_skb_pointers(meta_sk, sk_data);
@@ -237,20 +288,55 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  						    sk_data->skb, meta_sk);
  		if (skb && redsched_use_subflow(meta_sk, active_valid_sks, tp,
  						skb)) {
+			struct mptcp_tcp_sock *mptcp;
+
  			sk_data->skb = skb;
  			sk_data->skb_end_seq = TCP_SKB_CB(skb)->end_seq;
-			cb_data->next_subflow = tp->mptcp->next;
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
  			*subsk = (struct sock *)tp;
 
  			if (TCP_SKB_CB(skb)->path_mask)
  				*reinject = -1;
  			return skb;
  		}
+	}
 
-		tp = tp->mptcp->next;
-		if (!tp)
-			tp = mpcb->connection_list;
-	} while (tp != first_tp);
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct redsched_sock_data *sk_data;
+
+		tp = mptcp->tp;
+
+		if (tp == first_tp)
+			break;
+
+		/* Correct the skb pointers of the current subflow */
+		sk_data = redsched_get_sock_data(tp);
+		redsched_correct_skb_pointers(meta_sk, sk_data);
+
+		skb = redundant_next_skb_from_queue(&meta_sk->sk_write_queue,
+						    sk_data->skb, meta_sk);
+		if (skb && redsched_use_subflow(meta_sk, active_valid_sks, tp,
+						skb)) {
+			struct mptcp_tcp_sock *mptcp;
+
+			sk_data->skb = skb;
+			sk_data->skb_end_seq = TCP_SKB_CB(skb)->end_seq;
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)

net/mptcp/mptcp_redundant.c static struct sk_buff *redundant_next_segment(struct sock *meta_sk, · line 339

Quoted 10 lines
net/mptcp/mptcp_redundant.c +10 -0
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
+			*subsk = (struct sock *)tp;
+
+			if (TCP_SKB_CB(skb)->path_mask)
+				*reinject = -1;
+			return skb;
+		}
+	}

Could you also add a comment explaining the two for_each's? :)

Quoted 8 lines
net/mptcp/mptcp_redundant.c +0 -1
  	/* Nothing to send */
  	return NULL;
@@ -264,8 +350,15 @@ static void redundant_release(struct sock *sk)
  	/* Check if the next subflow would be the released one. If yes correct
  	 * the pointer
  	 */
-	if (cb_data->next_subflow == tp)

net/mptcp/mptcp_redundant.c static void redundant_release(struct sock *sk) · line 361

Quoted 10 lines
net/mptcp/mptcp_redundant.c +9 -1
-		cb_data->next_subflow = tp->mptcp->next;
+	if (cb_data->next_subflow == tp) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp = mptcp_entry_next_rcu(tp->mptcp);
+		if (mptcp)
+			cb_data->next_subflow = mptcp->tp;
+		else
+			cb_data->next_subflow = NULL;
+	}

Should we not create a function for this block?

Best regards,
Matt

Quoted 152 lines
net/mptcp/mptcp_redundant.c +0 -0
  }
 
  static struct mptcp_sched_ops mptcp_sched_redundant = {
net/mptcp/mptcp_rr.c +15 -7
index 8910ba9e6052..d97706034646 100644
@@ -102,12 +102,14 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
  					     bool zero_wnd_test)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk, *bestsk = NULL, *backupsk = NULL;
+	struct sock *sk = NULL, *bestsk = NULL, *backupsk = NULL;
+	struct mptcp_tcp_sock *mptcp;
 
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
  			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
  			    mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
  				return sk;
@@ -115,8 +117,11 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
  	}
 
  	/* First, find the best subflow */
-	mptcp_for_each_sk(mpcb, sk) {
-		struct tcp_sock *tp = tcp_sk(sk);
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct tcp_sock *tp;
+
+		sk = mptcp_to_sock(mptcp);
+		tp = tcp_sk(sk);
 
  		if (!mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
  			continue;
@@ -177,7 +182,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  					     unsigned int *limit)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk_it, *choose_sk = NULL;
+	struct sock *choose_sk = NULL;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb = __mptcp_rr_next_segment(meta_sk, reinject);
  	unsigned char split = num_segments;
  	unsigned char iter = 0, full_subs = 0;
@@ -199,7 +205,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  retry:
 
  	/* First, we look for a subflow who is currently being used */
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp_it = tcp_sk(sk_it);
  		struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
 
@@ -233,7 +240,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  		/* So, we restart this round by setting quota to 0 and retry
  		 * to find a subflow.
  		 */
-		mptcp_for_each_sk(mpcb, sk_it) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp_it = tcp_sk(sk_it);
  			struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
 
net/mptcp/mptcp_sched.c +15 -6
index c831515fd7f0..09246e286da8 100644
@@ -139,9 +139,10 @@ static struct sock
  	u32 min_srtt = 0xffffffff;
  	bool found_unused = false;
  	bool found_unused_una = false;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
 
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		bool unused = false;
 
@@ -222,7 +223,11 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
  			    mptcp_is_available(sk, skb, zero_wnd_test))
  				return sk;
@@ -256,7 +261,7 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  {
  	struct sock *meta_sk;
  	const struct tcp_sock *tp = tcp_sk(sk);
-	struct tcp_sock *tp_it;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb_head;
  	struct defsched_priv *dsp = defsched_get_priv(tp);
 
@@ -279,7 +284,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  		goto retrans;
 
  	/* Half the cwnd of the slow flow */
-	mptcp_for_each_tp(tp->mpcb, tp_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct tcp_sock *tp_it = mptcp->tp;
+
  		if (tp_it != tp &&
  		    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
  			if (tp->srtt_us < tp_it->srtt_us && inet_csk((struct sock *)tp_it)->icsk_ca_state == TCP_CA_Open) {
@@ -302,7 +309,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  	/* Segment not yet injected into this path? Take it!!! */
  	if (!(TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp->mptcp->path_index))) {
  		bool do_retrans = false;
-		mptcp_for_each_tp(tp->mpcb, tp_it) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			struct tcp_sock *tp_it = mptcp->tp;
+
  			if (tp_it != tp &&
  			    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
  				if (tp_it->snd_cwnd <= 4) {
net/mptcp/mptcp_wvegas.c +3 -2
index 4373b8d1ee8d..787ddaab98a2 100644
@@ -143,14 +143,15 @@ static inline u32 mptcp_wvegas_ssthresh(const struct tcp_sock *tp)
  static u64 mptcp_wvegas_weight(const struct mptcp_cb *mpcb, const struct sock *sk)
  {
  	u64 total_rate = 0;
-	struct sock *sub_sk;
  	const struct wvegas *wvegas = inet_csk_ca(sk);
+	struct mptcp_tcp_sock *mptcp;
 
  	if (!mpcb)
  		return wvegas->weight;
 
 
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct wvegas *sub_wvegas = inet_csk_ca(sub_sk);
 
  		/* sampled_rtt is initialized by 0 */
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#25 lore raw

Re: [MPTCP] [PATCH 10/12] mptcp: Make subflow-list an RCU-list

Quoted 933 lines · Matthieu Baerts

Hi Christoph,

Quoted 5 lines · Christoph Paasch

mptcp_add_sock() will now be called without holding the meta-level lock. However, mptcp_add_sock() wants to add the subflow to the meta-level list, thus we need to protect this by a lock. We use the mpcb_list_lock for that.

Quoted 7 lines

Now that we are locking during add/del, and want to allow lockless traversal of the list, this implies that we need to make it an RCU-list.

So, this patch transitions to the RCU hlist. The list-traversal macros (hlist_for_each_entry_rcu) require me to now pass the mptcp_tcp_sock to mptcp_for_each_*. So, I had to change all the places in the code where we call one of the list-traversal macros to adapt to this.

Thank you for this big refactoring!
I quickly reviewed these changes, I hope no 'struct mptcp_tcp_sock' have been overridden but it looks fine, we always add a new variable 'mptcp' :-)

I only have comments in mptcp_redundant.c

Quoted 911 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h          |  69 ++++++++++----------
  net/ipv4/af_inet.c           |   9 +--
  net/ipv4/ip_sockglue.c       |   7 +-
  net/ipv4/tcp.c               |  31 +++++----
  net/mptcp/mctcp_desync.c     |  18 ++++--
  net/mptcp/mptcp_balia.c      |   8 ++-
  net/mptcp/mptcp_binder.c     |  10 ++-
  net/mptcp/mptcp_coupled.c    |  11 ++--
  net/mptcp/mptcp_ctrl.c       |  89 +++++++++++++++----------
  net/mptcp/mptcp_fullmesh.c   |  28 +++++---
  net/mptcp/mptcp_input.c      |  44 ++++++++-----
  net/mptcp/mptcp_ndiffports.c |   4 +-
  net/mptcp/mptcp_olia.c       |  20 ++++--
  net/mptcp/mptcp_output.c     |  38 +++++++----
  net/mptcp/mptcp_redundant.c  | 151 ++++++++++++++++++++++++++++++++++---------
  net/mptcp/mptcp_rr.c         |  22 +++++--
  net/mptcp/mptcp_sched.c      |  21 ++++--
  net/mptcp/mptcp_wvegas.c     |   5 +-
  18 files changed, 394 insertions(+), 191 deletions(-)
include/net/mptcp.h +36 -33
index d455bdd101ec..59e3da6c4031 100644
@@ -156,7 +156,7 @@ struct mptcp_options_received {
  };
  struct mptcp_tcp_sock {
-	struct tcp_sock	*next;		/* Next subflow socket */
+	struct hlist_node node;
  	struct hlist_node cb_list;
  	struct mptcp_options_received rx_opt;
@@ -257,7 +257,7 @@ struct mptcp_sched_ops {
  struct mptcp_cb {
  	/* list of sockets in this multipath connection */
-	struct tcp_sock *connection_list;
+	struct hlist_head conn_list;
  	/* list of sockets that need a call to release_cb */
  	struct hlist_head callback_list;
@@ -312,7 +312,7 @@ struct mptcp_cb {
  	/***** Start of fields, used for subflow establishment */
  	struct sock *meta_sk;
-	/* Master socket, also part of the connection_list, this
+	/* Master socket, also part of the conn_list, this
  	 * socket is the one that the application sees.
  	 */
  	struct sock *master_sk;
@@ -664,21 +664,17 @@ extern struct workqueue_struct *mptcp_wq;
  			pr_err(fmt, ##args);					\
  	} while (0)
-/* Iterates over all subflows */
-#define mptcp_for_each_tp(mpcb, tp)					\
-	for ((tp) = (mpcb)->connection_list; (tp); (tp) = (tp)->mptcp->next)
+static inline struct sock *mptcp_to_sock(const struct mptcp_tcp_sock *mptcp)
+{
+	return (struct sock *)mptcp->tp;
+}
-#define mptcp_for_each_sk(mpcb, sk)					\
-	for ((sk) = (struct sock *)(mpcb)->connection_list;		\
-	     sk;							\
-	     sk = (struct sock *)tcp_sk(sk)->mptcp->next)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	hlist_for_each_entry_rcu(__mptcp, &((__mpcb)->conn_list), node)
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)			\
-	for (__sk = (struct sock *)(__mpcb)->connection_list,		\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL; \
-	     __sk;							\
-	     __sk = __temp,						\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL)
+/* Must be called with the appropriate lock held */
+#define mptcp_for_each_sub_safe(__mpcb, __mptcp, __tmp)				\
+	hlist_for_each_entry_safe(__mptcp, __tmp, &((__mpcb)->conn_list), node)
  /* Iterates over all bit set to 1 in a bitset */
  #define mptcp_for_each_bit_set(b, i)					\
@@ -932,12 +928,14 @@ struct request_sock *rev_mptcp_rsk(const struct mptcp_request_sock *req)
  static inline bool mptcp_can_sendpage(struct sock *sk)
  {
-	struct sock *sk_it;
+	struct mptcp_tcp_sock *mptcp;
  	if (tcp_sk(sk)->mpcb->dss_csum)
  		return false;
-	mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+	mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if (!(sk_it->sk_route_caps & NETIF_F_SG) ||
  		    !sk_check_csum_caps(sk_it))
  			return false;
@@ -972,9 +970,12 @@ static inline void mptcp_send_reset(struct sock *sk)
  static inline void mptcp_sub_force_close_all(struct mptcp_cb *mpcb,
  					     struct sock *except)
  {
-	struct sock *sk_it, *tmp;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
+
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
-	mptcp_for_each_sk_safe(mpcb, sk_it, tmp) {
  		if (sk_it != except)
  			mptcp_send_reset(sk_it);
  	}
@@ -1161,12 +1162,14 @@ static inline int mptcp_sk_can_send_ack(const struct sock *sk)
  /* Only support GSO if all subflows supports it */
  static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	if (tcp_sk(meta_sk)->mpcb->dss_csum)
  		return false;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
  		if (!sk_can_gso(sk))
@@ -1177,12 +1180,14 @@ static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
  static inline bool mptcp_can_sg(const struct sock *meta_sk)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	if (tcp_sk(meta_sk)->mpcb->dss_csum)
  		return false;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
  		if (!(sk->sk_route_caps & NETIF_F_SG))
@@ -1193,9 +1198,9 @@ static inline bool mptcp_can_sg(const struct sock *meta_sk)
  static inline void mptcp_set_rto(struct sock *sk)
  {
-	struct tcp_sock *tp = tcp_sk(sk);
-	struct sock *sk_it;
  	struct inet_connection_sock *micsk = inet_csk(mptcp_meta_sk(sk));
+	struct tcp_sock *tp = tcp_sk(sk);
+	struct mptcp_tcp_sock *mptcp;
  	__u32 max_rto = 0;
  	/* We are in recovery-phase on the MPTCP-level. Do not update the
@@ -1204,7 +1209,9 @@ static inline void mptcp_set_rto(struct sock *sk)
  	if (micsk->icsk_retransmits)
  		return;
-	mptcp_for_each_sk(tp->mpcb, sk_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if ((mptcp_sk_can_send(sk_it) || sk->sk_state == TCP_SYN_RECV) &&
  		    inet_csk(sk_it)->icsk_rto > max_rto)
  			max_rto = inet_csk(sk_it)->icsk_rto;
@@ -1303,12 +1310,8 @@ bool mptcp_prune_ofo_queue(struct sock *sk);
  	do {				\
  	} while (0)
-/* Without MPTCP, we just do one iteration
- * over the only socket available. This assumes that
- * the sk/tp arg is the socket in that case.
- */
-#define mptcp_for_each_sk(mpcb, sk)
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	if (0)
  #define MPTCP_INC_STATS(net, field)	\
  	do {				\
net/ipv4/af_inet.c +5 -4
index 426fa704e218..87a98edc18d4 100644
@@ -709,13 +709,14 @@ int inet_accept(struct socket *sock, struct socket *newsock, int flags,
  	sock_rps_record_flow(sk2);
  	if (sk2->sk_protocol == IPPROTO_TCP && mptcp(tcp_sk(sk2))) {
-		struct sock *sk_it = sk2;
+		struct mptcp_tcp_sock *mptcp;
-		mptcp_for_each_sk(tcp_sk(sk2)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tcp_sk(sk2)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  		if (tcp_sk(sk2)->mpcb->master_sk) {
-			sk_it = tcp_sk(sk2)->mpcb->master_sk;
+			struct sock *sk_it = tcp_sk(sk2)->mpcb->master_sk;
  			write_lock_bh(&sk_it->sk_callback_lock);
  			sk_it->sk_wq = newsock->wq;
net/ipv4/ip_sockglue.c +5 -2
index 1fe79c7bcc9e..2b7a8c416c91 100644
@@ -756,8 +756,11 @@ static int do_ip_setsockopt(struct sock *sk, int level,
  			sk_dst_reset(sk);
  			/* Update TOS on mptcp subflow */
  			if (is_meta_sk(sk)) {
-				struct sock *sk_it;
-				mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+				struct mptcp_tcp_sock *mptcp;
+
+				mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+					struct sock *sk_it = mptcp_to_sock(mptcp);
+
  					if (inet_sk(sk_it)->tos != inet_sk(sk)->tos) {
  						inet_sk(sk_it)->tos = inet_sk(sk)->tos;
  						sk_it->sk_priority = sk->sk_priority;
net/ipv4/tcp.c +19 -12
index 93e15eeba870..fd53722bf59e 100644
@@ -804,9 +804,11 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
  #ifdef CONFIG_MPTCP
  	if (mptcp(tcp_sk(sk))) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  #endif
  	/*
@@ -984,7 +986,7 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  	}
  	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
+		struct mptcp_tcp_sock *mptcp;
  		/* We must check this with socket-lock hold because we iterate
  		 * over the subflows.
@@ -999,8 +1001,9 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  			return ret;
  		}
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
@@ -1293,9 +1296,11 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
  	}
  	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  	if (unlikely(tp->repair)) {
@@ -1883,9 +1888,11 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  #ifdef CONFIG_MPTCP
  	if (mptcp(tp)) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  #endif
net/mptcp/mctcp_desync.c +11 -7
index cfc26fcc3161..f6bf9251d59b 100644
@@ -67,7 +67,6 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  	} else {
  		const struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
  		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
  		/* current aggregated cwnd */
  		u32 agg_cwnd = 0;
@@ -90,11 +89,14 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  				tp->snd_ssthresh = tp->snd_cwnd = INI_MIN_CWND;
  			}
  			return;
-		}
-		  /* In dangerous area, increase slowly and linearly. */
-		  else {
+		} else {
+			/* In dangerous area, increase slowly and linearly. */
+			const struct mptcp_tcp_sock *mptcp;
+
  			/* get total cwnd and the subflow that has min cwnd */
-			mptcp_for_each_sk(tp->mpcb, sub_sk) {
+			mptcp_for_each_sub(tp->mpcb, mptcp) {
+				const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
  				if (mctcp_cc_sk_can_send(sub_sk)) {
  					const struct tcp_sock *sub_tp =
  								tcp_sk(sub_sk);
@@ -126,12 +128,14 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
  	} else {
  		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
  		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
+		const struct mptcp_tcp_sock *mptcp;
  		u32 max_cwnd = 0;
  		u8 max_cwnd_subfid = 0;
  		/* Find the subflow that has the max cwnd. */
-		mptcp_for_each_sk(tp->mpcb, sub_sk) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
  			if (mctcp_cc_sk_can_send(sub_sk)) {
  				const struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  				if (max_cwnd < sub_tp->snd_cwnd) {
net/mptcp/mptcp_balia.c +5 -3
index ac5c5e550539..e8a22fa66a50 100644
@@ -78,7 +78,7 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
  	const struct mptcp_cb *mpcb = tp->mpcb;
-	const struct sock *sub_sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 max_rate = 0, rate = 0, sum_rate = 0;
  	u64 alpha, ai = tp->snd_cwnd, md = (tp->snd_cwnd >> 1);
  	int num_scale_down = 0;
@@ -87,7 +87,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  		return;
  	/* Find max_rate first */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		u64 tmp;
@@ -119,7 +120,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  	if (num_scale_down) {
  		sum_rate = 0;
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			u64 tmp;
net/mptcp/mptcp_binder.c +7 -3
index d6ef9c5b6ce5..eaf8ba674d17 100644
@@ -46,6 +46,8 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
  	unsigned char *opt_ptr, *opt_end_ptr, opt[MAX_IPOPTLEN];
  	for (i = 0; i < MPTCP_GW_MAX_LISTS; ++i) {
+		struct mptcp_tcp_sock *mptcp;
+
  		if (mptcp_gws->len[i] == 0)
  			goto error;
@@ -53,7 +55,9 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
  		list_taken = 0;
  		/* Loop through all sub-sockets in this connection */
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk) {
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
  			mptcp_debug("mptcp_get_avail_list_ipv4: Next sock\n");
  			/* Reset length and options buffer, then retrieve
@@ -289,10 +293,10 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
  static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int i = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		i++;
  	}
net/mptcp/mptcp_coupled.c +7 -4
index a6569f5e20ab..e1040a23e59f 100644
@@ -80,7 +80,7 @@ static inline void mptcp_set_forced(const struct sock *meta_sk, bool force)
  static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(sk)->mpcb;
-	const struct sock *sub_sk;
+	const struct mptcp_tcp_sock *mptcp;
  	int best_cwnd = 0, best_rtt = 0, can_send = 0;
  	u64 max_numerator = 0, sum_denominator = 0, alpha = 1;
@@ -90,7 +90,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	/* Do regular alpha-calculation for multiple subflows */
  	/* Find the max numerator of the alpha-calculation */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		u64 tmp;
@@ -118,7 +119,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  		goto exit;
  	/* Calculate the denominator */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		if (!mptcp_ccc_sk_can_send(sub_sk))
@@ -132,7 +134,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	sum_denominator *= sum_denominator;
  	if (unlikely(!sum_denominator)) {
  		pr_err("%s: sum_denominator == 0\n", __func__);
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			pr_err("%s: pi:%d, state:%d\n, rtt:%u, cwnd: %u",
  			       __func__, sub_tp->mptcp->path_index,
net/mptcp/mptcp_ctrl.c +55 -34
index 9237a73ec174..fe824649bcc3 100644
@@ -553,10 +553,12 @@ void mptcp_hash_remove_bh(struct tcp_sock *meta_tp)
  struct sock *mptcp_select_ack_sock(const struct sock *meta_sk)
  {
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk, *rttsk = NULL, *lastsk = NULL;
+	struct sock *rttsk = NULL, *lastsk = NULL;
  	u32 min_time = 0, last_active = 0;
+	struct mptcp_tcp_sock *mptcp;
-	mptcp_for_each_sk(meta_tp->mpcb, sk) {
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		u32 elapsed;
@@ -689,7 +691,8 @@ void mptcp_sock_destruct(struct sock *sk)
  void mptcp_destroy_sock(struct sock *sk)
  {
  	if (is_meta_sk(sk)) {
-		struct sock *sk_it, *tmpsk;
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
  		__skb_queue_purge(&tcp_sk(sk)->mpcb->reinject_queue);
@@ -699,7 +702,9 @@ void mptcp_destroy_sock(struct sock *sk)
  		 * not have been closed properly (as we are waiting for the
  		 * DATA_ACK of the DATA_FIN).
  		 */
-		mptcp_for_each_sk_safe(tcp_sk(sk)->mpcb, sk_it, tmpsk) {
+		mptcp_for_each_sub_safe(tcp_sk(sk)->mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
+
  			/* Already did call tcp_close - waiting for graceful
  			 * closure, or if we are retransmitting fast-close on
  			 * the subflow. The reset (or timeout) will kill the
@@ -1281,6 +1286,7 @@ static int mptcp_alloc_mpcb(struct sock *meta_sk, __u64 remote_key,
  	INIT_LIST_HEAD(&mpcb->tw_list);
  	INIT_HLIST_HEAD(&mpcb->callback_list);
+	INIT_HLIST_HEAD(&mpcb->conn_list);
  	spin_lock_init(&mpcb->mpcb_list_lock);
  	mptcp_mpcb_inherit_sockopts(meta_sk, master_sk);
@@ -1370,8 +1376,12 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  	sock_hold(meta_sk);
  	refcount_inc(&mpcb->mpcb_refcnt);
-	tp->mptcp->next = mpcb->connection_list;
-	mpcb->connection_list = tp;
+	local_bh_disable();
+	spin_lock(&mpcb->mpcb_list_lock);
+	hlist_add_head_rcu(&tp->mptcp->node, &mpcb->conn_list);
+	spin_unlock(&mpcb->mpcb_list_lock);
+	local_bh_enable();
+
  	tp->mptcp->attached = 1;
  	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
@@ -1415,14 +1425,13 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  void mptcp_del_sock(struct sock *sk)
  {
-	struct tcp_sock *tp = tcp_sk(sk), *tp_prev;
+	struct tcp_sock *tp = tcp_sk(sk);
  	struct mptcp_cb *mpcb;
  	if (!tp->mptcp || !tp->mptcp->attached)
  		return;
  	mpcb = tp->mpcb;
-	tp_prev = mpcb->connection_list;
  	if (mpcb->sched_ops->release)
  		mpcb->sched_ops->release(sk);
@@ -1434,17 +1443,10 @@ void mptcp_del_sock(struct sock *sk)
  		    __func__, mpcb->mptcp_loc_token, tp->mptcp->path_index,
  		    sk->sk_state, is_meta_sk(sk));
-	if (tp_prev == tp) {
-		mpcb->connection_list = tp->mptcp->next;
-	} else {
-		for (; tp_prev && tp_prev->mptcp->next; tp_prev = tp_prev->mptcp->next) {
-			if (tp_prev->mptcp->next == tp) {
-				tp_prev->mptcp->next = tp->mptcp->next;
-				break;
-			}
-		}
-	}
-	tp->mptcp->next = NULL;
+	spin_lock(&mpcb->mpcb_list_lock);
+	hlist_del_init_rcu(&tp->mptcp->node);
+	spin_unlock(&mpcb->mpcb_list_lock);
+
  	tp->mptcp->attached = 0;
  	mpcb->path_index_bits &= ~(1 << tp->mptcp->path_index);
@@ -1491,8 +1493,8 @@ void mptcp_update_metasocket(const struct sock *meta_sk)
  void mptcp_cleanup_rbuf(struct sock *meta_sk, int copied)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk;
  	bool recheck_rcv_window = false;
+	struct mptcp_tcp_sock *mptcp;
  	__u32 rcv_window_now = 0;
  	if (copied > 0 && !(meta_sk->sk_shutdown & RCV_SHUTDOWN)) {
@@ -1503,7 +1505,8 @@ void mptcp_cleanup_rbuf(struct sock *meta_sk, int copied)
  			recheck_rcv_window = true;
  	}
-	mptcp_for_each_sk(meta_tp->mpcb, sk) {
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		const struct inet_connection_sock *icsk = inet_csk(sk);
@@ -1690,10 +1693,13 @@ EXPORT_SYMBOL(mptcp_sub_force_close);
   */
  void mptcp_update_sndbuf(const struct tcp_sock *tp)
  {
-	struct sock *meta_sk = tp->meta_sk, *sk;
+	struct sock *meta_sk = tp->meta_sk;
  	int new_sndbuf = 0, old_sndbuf = meta_sk->sk_sndbuf;
+	struct mptcp_tcp_sock *mptcp;
+
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
-	mptcp_for_each_sk(tp->mpcb, sk) {
  		if (!mptcp_sk_can_send(sk))
  			continue;
@@ -1722,8 +1728,8 @@ void mptcp_update_sndbuf(const struct tcp_sock *tp)
  void mptcp_close(struct sock *meta_sk, long timeout)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk_it, *tmpsk;
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb;
  	int data_was_unread = 0;
  	int state;
@@ -1756,7 +1762,12 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	/* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
  	if (meta_sk->sk_state == TCP_CLOSE) {
-		mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
+
+		mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk_it)->send_mp_fclose)
  				continue;
  			mptcp_sub_close(sk_it, 0);
@@ -1777,10 +1788,14 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	} else if (tcp_close_state(meta_sk)) {
  		mptcp_send_fin(meta_sk);
  	} else if (meta_tp->snd_una == meta_tp->write_seq) {
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
+
  		/* The DATA_FIN has been sent and acknowledged
  		 * (e.g., by sk_shutdown). Close all the other subflows
  		 */
-		mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
+		mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			unsigned long delay = 0;
  			/* If we are the passive closer, don't trigger
  			 * subflow-fin until the subflow has been finned
@@ -1804,7 +1819,9 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	/* socket will be freed after mptcp_close - we have to prevent
  	 * access from the subflows.
  	 */
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		/* Similar to sock_orphan, but we don't set it DEAD, because
  		 * the callbacks are still set and must be called.
  		 */
@@ -1889,8 +1906,9 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  void mptcp_disconnect(struct sock *sk)
  {
-	struct sock *subsk, *tmpsk;
+	struct mptcp_tcp_sock *mptcp;
  	struct tcp_sock *tp = tcp_sk(sk);
+	struct hlist_node *tmp;
  	__skb_queue_purge(&tp->mpcb->reinject_queue);
@@ -1898,7 +1916,9 @@ void mptcp_disconnect(struct sock *sk)
  		mptcp_hash_remove_bh(tp);
  	local_bh_disable();
-	mptcp_for_each_sk_safe(tp->mpcb, subsk, tmpsk) {
+	mptcp_for_each_sub_safe(tp->mpcb, mptcp, tmp) {
+		struct sock *subsk = mptcp_to_sock(mptcp);
+
  		/* The socket will get removed from the subsocket-list
  		 * and made non-mptcp by setting mpc to 0.
  		 *
@@ -2582,7 +2602,6 @@ static void mptcp_get_sub_info(struct sock *sk, struct mptcp_sub_info *info)
  int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  {
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk;
  	struct mptcp_meta_info meta_info;
  	struct mptcp_info m_info;
@@ -2628,16 +2647,17 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  	if (m_info.subflows) {
  		unsigned int len, sub_len = 0;
+		struct mptcp_tcp_sock *mptcp;
  		char __user *ptr;
  		ptr = (char __user *)m_info.subflows;
  		len = m_info.sub_len;
-		mptcp_for_each_sk(meta_tp->mpcb, sk) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
  			struct tcp_info t_info;
  			unsigned int tmp_len;
-			tcp_get_info(sk, &t_info);
+			tcp_get_info(mptcp_to_sock(mptcp), &t_info);
  			tmp_len = min_t(unsigned int, len, info_len);
  			len -= tmp_len;
@@ -2657,6 +2677,7 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  	if (m_info.subflow_info) {
  		unsigned int len, sub_info_len, total_sub_info_len = 0;
+		struct mptcp_tcp_sock *mptcp;
  		char __user *ptr;
  		ptr = (char __user *)m_info.subflow_info;
@@ -2666,11 +2687,11 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  				     sizeof(struct mptcp_sub_info));
  		m_info.sub_info_len = sub_info_len;
-		mptcp_for_each_sk(meta_tp->mpcb, sk) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
  			struct mptcp_sub_info m_sub_info;
  			unsigned int tmp_len;
-			mptcp_get_sub_info(sk, &m_sub_info);
+			mptcp_get_sub_info(mptcp_to_sock(mptcp), &m_sub_info);
  			tmp_len = min_t(unsigned int, len, sub_info_len);
  			len -= tmp_len;
net/mptcp/mptcp_fullmesh.c +20 -8
index d29102a1dffb..53c1ba7319f5 100644
@@ -903,8 +903,9 @@ static void mptcp_address_worker(struct work_struct *work)
  			}
  			if (event->code == MPTCP_EVENT_DEL) {
-				struct sock *sk, *tmpsk;
+				struct mptcp_tcp_sock *mptcp;
  				struct mptcp_loc_addr *mptcp_local;
+				struct hlist_node *tmp;
  				bool found = false;
  				mptcp_local = rcu_dereference_bh(fm_ns->local);
@@ -914,7 +915,9 @@ static void mptcp_address_worker(struct work_struct *work)
  					update_addr_bitfields(meta_sk, mptcp_local);
  				/* Look for the socket and remove him */
-				mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+				mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
+
  					if ((event->family == AF_INET6 &&
  					     (sk->sk_family == AF_INET ||
  					      mptcp_v6_is_v4_mapped(sk))) ||
@@ -964,9 +967,10 @@ static void mptcp_address_worker(struct work_struct *work)
  			}
  			if (event->code == MPTCP_EVENT_MOD) {
-				struct sock *sk;
+				struct mptcp_tcp_sock *mptcp;
-				mptcp_for_each_sk(mpcb, sk) {
+				mptcp_for_each_sub(mpcb, mptcp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
  					struct tcp_sock *tp = tcp_sk(sk);
  					if (event->family == AF_INET &&
  					    (sk->sk_family == AF_INET ||
@@ -1455,8 +1459,9 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  	struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
  	struct fullmesh_priv *fmp = fullmesh_get_priv(mpcb);
  	const struct mptcp_fm_ns *fm_ns = fm_get_ns(sock_net(meta_sk));
-	struct sock *sk, *tmpsk;
  	bool meta_v4 = meta_sk->sk_family == AF_INET;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
  	int i;
  	rcu_read_lock_bh();
@@ -1470,7 +1475,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		struct in_addr ifa = mptcp_local->locaddr4[i].addr;
  		bool found = false;
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp = tcp_sk(sk);
  			if (sk->sk_family == AF_INET6 &&
@@ -1491,6 +1497,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		}
  		if (!found) {
+			struct sock *sk;
+
  			fmp->add_addr++;
  			mpcb->addr_signal = 1;
@@ -1511,7 +1519,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		struct in6_addr ifa = mptcp_local->locaddr6[i].addr;
  		bool found = false;
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp = tcp_sk(sk);
  			if (sk->sk_family == AF_INET ||
@@ -1532,6 +1541,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		}
  		if (!found) {
+			struct sock *sk;
+
  			fmp->add_addr++;
  			mpcb->addr_signal = 1;
@@ -1546,7 +1557,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  #endif
  	/* Now, detect address-removals */
-	mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		bool shall_remove = true;
  		if (sk->sk_family == AF_INET || mptcp_v6_is_v4_mapped(sk)) {
net/mptcp/mptcp_input.c +29 -15
index 211605347f70..64b0265bfd06 100644
@@ -126,12 +126,14 @@ static void mptcp_clean_rtx_queue(struct sock *meta_sk, u32 prior_snd_una)
  		tcp_rtx_queue_unlink(skb, meta_sk);
  		if (mptcp_is_data_fin(skb)) {
-			struct sock *sk_it, *sk_tmp;
+			struct mptcp_tcp_sock *mptcp;
+			struct hlist_node *tmp;
  			/* DATA_FIN has been acknowledged - now we can close
  			 * the subflows
  			 */
-			mptcp_for_each_sk_safe(mpcb, sk_it, sk_tmp) {
+			mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+				struct sock *sk_it = mptcp_to_sock(mptcp);
  				unsigned long delay = 0;
  				/* If we are the passive closer, don't trigger
@@ -347,6 +349,7 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  	/* Now, checksum must be 0 */
  	if (unlikely(csum_fold(csum_tcp))) {
+		struct mptcp_tcp_sock *mptcp;
  		struct sock *sk_it = NULL;
  		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
@@ -362,7 +365,9 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
  		/* Search for another subflow that is fully established */
-		mptcp_for_each_sk(tp->mpcb, sk_it) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sk_it = mptcp_to_sock(mptcp);
+
  			if (sk_it != sk &&
  			    tcp_sk(sk_it)->mptcp->fully_established)
  				break;
@@ -1308,12 +1313,15 @@ int mptcp_do_join_short(struct sk_buff *skb,
   */
  void mptcp_fin(struct sock *meta_sk)
  {
-	struct sock *sk = NULL, *sk_it;
+	struct sock *sk = NULL;
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
+	struct mptcp_tcp_sock *mptcp;
  	unsigned char state;
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if (tcp_sk(sk_it)->mptcp->path_index == mpcb->dfin_path_index) {
  			sk = sk_it;
  			break;
@@ -1585,9 +1593,12 @@ void mptcp_clean_rtx_infinite(const struct sk_buff *skb, struct sock *sk)
  static void mptcp_send_reset_rem_id(const struct mptcp_cb *mpcb, u8 rem_id)
  {
-	struct sock *sk_it, *tmpsk;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
+
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
-	mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
  		if (tcp_sk(sk_it)->mptcp->rem_id == rem_id) {
  			mptcp_reinject_data(sk_it, 0);
  			mptcp_send_reset(sk_it);
@@ -1892,13 +1903,15 @@ void tcp_parse_mptcp_options(const struct sk_buff *skb,
  bool mptcp_check_rtt(const struct tcp_sock *tp, int time)
  {
  	struct mptcp_cb *mpcb = tp->mpcb;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u32 rtt_max = 0;
  	/* In MPTCP, we take the max delay across all flows,
  	 * in order to take into account meta-reordering buffers.
  	 */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_recv(sk))
  			continue;
@@ -2173,9 +2186,9 @@ bool mptcp_handle_options(struct sock *sk, const struct tcphdr *th,
  		if (mopt->saw_low_prio == 1) {
  			tp->mptcp->rcv_low_prio = mopt->low_prio;
  		} else {
-			struct sock *sk_it;
-			mptcp_for_each_sk(tp->mpcb, sk_it) {
-				struct mptcp_tcp_sock *mptcp = tcp_sk(sk_it)->mptcp;
+			struct mptcp_tcp_sock *mptcp;
+
+			mptcp_for_each_sub(tp->mpcb, mptcp) {
  				if (mptcp->rem_id == mopt->prio_addr_id)
  					mptcp->rcv_low_prio = mopt->low_prio;
  			}
@@ -2359,7 +2372,7 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  {
  	const struct sock *meta_sk = mptcp_meta_sk(sk);
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	const struct sock *sk_it;
+	const struct mptcp_tcp_sock *mptcp;
  	/* We circumvent this check in tcp_check_space, because we want to
  	 * always call sk_write_space. So, we reproduce the check here.
@@ -2385,8 +2398,9 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  	/* For MPTCP we look for a subsocket that could send data.
  	 * If we found one, then we update the send-buffer.
  	 */
-	mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
-		struct tcp_sock *tp_it = tcp_sk(sk_it);
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		const struct sock *sk_it = mptcp_to_sock(mptcp);
+		const struct tcp_sock *tp_it = tcp_sk(sk_it);
  		if (!mptcp_sk_can_send(sk_it))
  			continue;
net/mptcp/mptcp_ndiffports.c +2 -2
index fe73ade7ba24..4219e9489655 100644
@@ -20,10 +20,10 @@ MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connecti
  static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int i = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		i++;
  	}
net/mptcp/mptcp_olia.c +13 -7
index 9f61e1eff65a..c44eb9208581 100644
@@ -61,10 +61,11 @@ static u32 mptcp_get_crt_cwnd(struct sock *sk)
  /* return the dominator of the first term of  the increasing term */
  static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 rate = 1; /* We have to avoid a zero-rate because it is used as a divisor */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		u64 scaled_num;
  		u32 tmp_cwnd;
@@ -83,10 +84,11 @@ static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
  /* find the maximum cwnd, used to find set M */
  static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u32 best_cwnd = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		u32 tmp_cwnd;
  		if (!mptcp_olia_sk_can_send(sk))
@@ -101,6 +103,7 @@ static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
  static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  {
+	struct mptcp_tcp_sock *mptcp;
  	struct mptcp_olia *ca;
  	struct tcp_sock *tp;
  	struct sock *sk;
@@ -113,7 +116,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	max_cwnd = mptcp_get_max_cwnd(mpcb);
  	/* find the best path */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
@@ -135,7 +139,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	/* TODO - integrate this here in mptcp_get_max_cwnd and in the previous loop */
  	/* find the size of M and B_not_M */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
@@ -156,7 +161,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	}
  	/* check if the path is in M or B_not_M and set the value of epsilon accordingly */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
net/mptcp/mptcp_output.c +0 -1
index cc1a4f7d39e9..29a7ab309751 100644
@@ -647,7 +647,6 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct sk_buff *skb;
-	struct sock *sk_it;
  	int ans = 0;
  	if (meta_sk->sk_state == TCP_CLOSE)
@@ -704,17 +703,22 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
Quoted 8 lines

net/mptcp/mptcp_output.c int mptcp_write_wakeup(struct sock *meta_sk, int mib) · line 707

Quoted 4 lines
net/mptcp/mptcp_output.c +2 -0
  		return 0;
  	} else {
+		struct mptcp_tcp_sock *mptcp;
+

detail: that's strange to see the declaration of the variable just before the flag :)
But that's correct.

Yeah, should be fine :)

Quoted 159 lines
Quoted 159 lines
net/mptcp/mptcp_output.c +23 -12
  window_probe:
  		if (between(meta_tp->snd_up, meta_tp->snd_una + 1,
  			    meta_tp->snd_una + 0xFFFF)) {
-			mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+			mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+				struct sock *sk_it = mptcp_to_sock(mptcp);
+
  				if (mptcp_sk_can_send_ack(sk_it))
  					tcp_xmit_probe_skb(sk_it, 1, mib);
  			}
  		}
  		/* At least one of the tcp_xmit_probe_skb's has to succeed */
-		mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			int ret;
  			if (!mptcp_sk_can_send_ack(sk_it))
@@ -732,6 +736,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  		     int push_one, gfp_t gfp)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk), *subtp;
+	struct mptcp_tcp_sock *mptcp;
  	struct sock *subsk = NULL;
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sk_buff *skb;
@@ -856,7 +861,8 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  			break;
  	}
-	mptcp_for_each_sk(mpcb, subsk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		subsk = mptcp_to_sock(mptcp);
  		subtp = tcp_sk(subsk);
  		if (!(path_mask & mptcp_pi_to_flag(subtp->mptcp->path_index)))
@@ -1353,7 +1359,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sock *sk;
-	if (!mpcb->connection_list)
+	if (hlist_empty(&mpcb->conn_list))
  		return;
  	WARN_ON(meta_tp->send_mp_fclose);
@@ -1728,10 +1734,11 @@ void mptcp_select_initial_window(const struct sock *sk, int __space, __u32 mss,
  static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
  				  unsigned int (*mss_cb)(struct sock *sk))
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 rate = 0;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		int this_mss;
  		u64 this_rate;
@@ -1783,11 +1790,12 @@ static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
  static unsigned int __mptcp_current_mss(const struct sock *meta_sk,
  					unsigned int (*mss_cb)(struct sock *sk))
  {
+	struct mptcp_tcp_sock *mptcp;
  	unsigned int mss = 0;
  	u64 rate = 0;
-	struct sock *sk;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		int this_mss;
  		u64 this_rate;
@@ -1852,14 +1860,16 @@ int mptcp_select_size(const struct sock *meta_sk, bool sg, bool first_skb, bool
  int mptcp_check_snd_buf(const struct tcp_sock *tp)
  {
-	const struct sock *sk;
+	const struct mptcp_tcp_sock *mptcp;
  	u32 rtt_max = tp->srtt_us;
  	u64 bw_est;
  	if (!tp->srtt_us)
  		return tp->reordering + 1;
-	mptcp_for_each_sk(tp->mpcb, sk) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		const struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
@@ -1877,11 +1887,13 @@ int mptcp_check_snd_buf(const struct tcp_sock *tp)
  unsigned int mptcp_xmit_size_goal(const struct sock *meta_sk, u32 mss_now,
  				  int large_allowed)
  {
-	struct sock *sk;
  	u32 xmit_size_goal = 0;
  	if (large_allowed && mptcp_sk_can_gso(meta_sk)) {
-		mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			int this_size_goal;
  			if (!mptcp_sk_can_send(sk))
net/mptcp/mptcp_redundant.c +19 -9
index 001a3f8a974d..6c68a8a071c1 100644
@@ -52,10 +52,12 @@ static bool redsched_get_active_valid_sks(struct sock *meta_sk)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int active_valid_sks = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (subflow_is_active((struct tcp_sock *)sk) &&
  		    !mptcp_is_def_unavailable(sk))
  			active_valid_sks++;
@@ -88,6 +90,10 @@ static bool redsched_use_subflow(struct sock *meta_sk,
  	return false;
  }
+#define mptcp_entry_next_rcu(__mptcp)						\
+	hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(			\
+		&(__mptcp)->node)), struct mptcp_tcp_sock, node)
+
  static struct sock *redundant_get_subflow(struct sock *meta_sk,
  					  struct sk_buff *skb,
  					  bool zero_wnd_test)
@@ -95,14 +101,16 @@ static struct sock *redundant_get_subflow(struct sock *meta_sk,
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct redsched_cb_data *cb_data = redsched_get_cb_data(meta_tp);
-	struct tcp_sock *first_tp = cb_data->next_subflow;
-	struct sock *sk;
-	struct tcp_sock *tp;
+	struct tcp_sock *first_tp = cb_data->next_subflow, *tp;
+	struct mptcp_tcp_sock *mptcp;
+	int found = 0;
  	/* Answer data_fin on same subflow */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk)->mptcp->path_index ==
  				mpcb->dfin_path_index &&
  			    mptcp_is_available(sk, skb, zero_wnd_test))
@@ -110,26 +118,58 @@ static struct sock *redundant_get_subflow(struct sock *meta_sk,
  		}
  	}
-	if (!first_tp)
-		first_tp = mpcb->connection_list;
+	if (!first_tp && !hlist_empty(&mpcb->conn_list)) {
+		first_tp = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(&mpcb->conn_list)),
+					    struct mptcp_tcp_sock, node)->tp;
+	}
  	tp = first_tp;
-	/* still NULL (no subflow in connection_list?) */
+	/* still NULL (no subflow in conn_list?) */
  	if (!first_tp)
  		return NULL;
  	/* Search for any subflow to send it */
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 135

Quoted 5 lines
net/mptcp/mptcp_redundant.c +4 -1
-	do {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		/* We go up to the subflow 'tp', then we pick the next one. */
+		if (!found)
+			continue;

I guess I missed something in the patch but found will always be false here, we will never do the rest, no?

You are absolutely right! The "if (!found)" needs to come after the "if (tp == mptcp->tp)". Thanks!

Quoted 12 lines
Quoted 12 lines
net/mptcp/mptcp_redundant.c +8 -1
+		if (tp == mptcp->tp) {
+			found = 1;
+			continue; > +		}
+		tp = mptcp->tp;
+
  		if (mptcp_is_available((struct sock *)tp, skb,
  				       zero_wnd_test)) {
-			cb_data->next_subflow = tp->mptcp->next;
+			struct mptcp_tcp_sock *mptcp;
+
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 152

Quoted 6 lines
net/mptcp/mptcp_redundant.c +4 -0
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
  			return (struct sock *)tp;
  		}

Should we not break here?

No, we want to continue iterating over the remaining subflows.

Quoted 19 lines
Quoted 19 lines
net/mptcp/mptcp_redundant.c +14 -4
+	}
+
+	mptcp_for_each_sub(mpcb, mptcp) {
+		tp = mptcp->tp;
+
+		if (tp == first_tp)
+			break;
+
+		if (mptcp_is_available((struct sock *)tp, skb,
+				       zero_wnd_test)) {
+			struct mptcp_tcp_sock *mptcp;
-		tp = tp->mptcp->next;
-		if (!tp)
-			tp = mpcb->connection_list;
-	} while (tp != first_tp);
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 172

Quoted 5 lines
net/mptcp/mptcp_redundant.c +5 -0
+			else
+				cb_data->next_subflow = NULL;
+			return (struct sock *)tp;
+		}
+	}

I am not sure I understand why you need two mptcp_for_each_sub here. May you add a comment? :)

Yes, I will add the below comment above the very first mptcp_for_each_sub:

/* Search for a subflow to send it.
*
* We want to pick a subflow that is after 'tp' in the list of subflows.
* Thus, the first mptcp_for_each_sub()-loop tries to walk the list up
* to the subflow 'tp' and then checks whether any one of the remaining
* ones is eligible to send.
* The second mptcp_for_each-sub()-loop is then iterating from the
* beginning of the list up to 'tp'.
*/

Quoted 32 lines
Quoted 32 lines
net/mptcp/mptcp_redundant.c +9 -6
  	/* No space */
  	return NULL;
@@ -190,10 +230,11 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct redsched_cb_data *cb_data = redsched_get_cb_data(meta_tp);
-	struct tcp_sock *first_tp = cb_data->next_subflow;
-	struct tcp_sock *tp;
-	struct sk_buff *skb;
+	struct tcp_sock *first_tp = cb_data->next_subflow, *tp;
+	struct mptcp_tcp_sock *mptcp;
  	int active_valid_sks = -1;
+	struct sk_buff *skb;
+	int found = 0;
  	/* As we set it, we have to reset it as well. */
  	*limit = 0;
@@ -215,10 +256,12 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  	/* Then try indistinctly redundant and normal skbs */
-	if (!first_tp)
-		first_tp = mpcb->connection_list;
+	if (!first_tp && !hlist_empty(&mpcb->conn_list)) {
+		first_tp = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(&mpcb->conn_list)),
+					    struct mptcp_tcp_sock, node)->tp;
+	}
-	/* still NULL (no subflow in connection_list?) */
+	/* still NULL (no subflow in conn_list?) */
  	if (!first_tp)
  		return NULL;
@@ -226,9 +269,17 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  	*reinject = 0;
  	active_valid_sks = redsched_get_active_valid_sks(meta_sk);
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sk_buff *redundant_next_segment(struct sock *meta_sk, · line 276

Quoted 5 lines
net/mptcp/mptcp_redundant.c +3 -1
-	do {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		struct redsched_sock_data *sk_data;
+		if (!found)
+			continue;

Same here, I guess I missed a goto somewhere but found will always be 0 here.

Yes, same fix needed here as above.

Quoted 59 lines
Quoted 59 lines
net/mptcp/mptcp_redundant.c +39 -5
+		if (tp == mptcp->tp) {
+			found = 1;
+			continue;
+		}
+
  		/* Correct the skb pointers of the current subflow */
  		sk_data = redsched_get_sock_data(tp);
  		redsched_correct_skb_pointers(meta_sk, sk_data);
@@ -237,20 +288,55 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  						    sk_data->skb, meta_sk);
  		if (skb && redsched_use_subflow(meta_sk, active_valid_sks, tp,
  						skb)) {
+			struct mptcp_tcp_sock *mptcp;
+
  			sk_data->skb = skb;
  			sk_data->skb_end_seq = TCP_SKB_CB(skb)->end_seq;
-			cb_data->next_subflow = tp->mptcp->next;
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
  			*subsk = (struct sock *)tp;
  			if (TCP_SKB_CB(skb)->path_mask)
  				*reinject = -1;
  			return skb;
  		}
+	}
-		tp = tp->mptcp->next;
-		if (!tp)
-			tp = mpcb->connection_list;
-	} while (tp != first_tp);
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct redsched_sock_data *sk_data;
+
+		tp = mptcp->tp;
+
+		if (tp == first_tp)
+			break;
+
+		/* Correct the skb pointers of the current subflow */
+		sk_data = redsched_get_sock_data(tp);
+		redsched_correct_skb_pointers(meta_sk, sk_data);
+
+		skb = redundant_next_skb_from_queue(&meta_sk->sk_write_queue,
+						    sk_data->skb, meta_sk);
+		if (skb && redsched_use_subflow(meta_sk, active_valid_sks, tp,
+						skb)) {
+			struct mptcp_tcp_sock *mptcp;
+
+			sk_data->skb = skb;
+			sk_data->skb_end_seq = TCP_SKB_CB(skb)->end_seq;
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
+			*subsk = (struct sock *)tp;
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sk_buff *redundant_next_segment(struct sock *meta_sk, · line 339

Quoted 6 lines
net/mptcp/mptcp_redundant.c +6 -0
+
+			if (TCP_SKB_CB(skb)->path_mask)
+				*reinject = -1;
+			return skb;
+		}
+	}

Could you also add a comment explaining the two for_each's? :)

Sure, done!

Quoted 12 lines
Quoted 12 lines
net/mptcp/mptcp_redundant.c +3 -2
  	/* Nothing to send */
  	return NULL;
@@ -264,8 +350,15 @@ static void redundant_release(struct sock *sk)
  	/* Check if the next subflow would be the released one. If yes correct
  	 * the pointer
  	 */
-	if (cb_data->next_subflow == tp)
-		cb_data->next_subflow = tp->mptcp->next;
+	if (cb_data->next_subflow == tp) {
+		struct mptcp_tcp_sock *mptcp;
+
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 153

Quoted 6 lines
net/mptcp/mptcp_redundant.c +6 -0
+		mptcp = mptcp_entry_next_rcu(tp->mptcp);
+		if (mptcp)
+			cb_data->next_subflow = mptcp->tp;
+		else
+			cb_data->next_subflow = NULL;
+	}

Should we not create a function for this block?

Yes, we should :) Done as well.

Thanks!

Christoph

Quoted 163 lines

Best regards,
Matt

Quoted 137 lines
net/mptcp/mptcp_redundant.c +0 -0
  }
  static struct mptcp_sched_ops mptcp_sched_redundant = {
net/mptcp/mptcp_rr.c +15 -7
index 8910ba9e6052..d97706034646 100644
@@ -102,12 +102,14 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
  					     bool zero_wnd_test)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk, *bestsk = NULL, *backupsk = NULL;
+	struct sock *sk = NULL, *bestsk = NULL, *backupsk = NULL;
+	struct mptcp_tcp_sock *mptcp;
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
  			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
  			    mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
  				return sk;
@@ -115,8 +117,11 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
  	}
  	/* First, find the best subflow */
-	mptcp_for_each_sk(mpcb, sk) {
-		struct tcp_sock *tp = tcp_sk(sk);
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct tcp_sock *tp;
+
+		sk = mptcp_to_sock(mptcp);
+		tp = tcp_sk(sk);
  		if (!mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
  			continue;
@@ -177,7 +182,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  					     unsigned int *limit)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk_it, *choose_sk = NULL;
+	struct sock *choose_sk = NULL;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb = __mptcp_rr_next_segment(meta_sk, reinject);
  	unsigned char split = num_segments;
  	unsigned char iter = 0, full_subs = 0;
@@ -199,7 +205,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  retry:
  	/* First, we look for a subflow who is currently being used */
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp_it = tcp_sk(sk_it);
  		struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
@@ -233,7 +240,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  		/* So, we restart this round by setting quota to 0 and retry
  		 * to find a subflow.
  		 */
-		mptcp_for_each_sk(mpcb, sk_it) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp_it = tcp_sk(sk_it);
  			struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
net/mptcp/mptcp_sched.c +15 -6
index c831515fd7f0..09246e286da8 100644
@@ -139,9 +139,10 @@ static struct sock
  	u32 min_srtt = 0xffffffff;
  	bool found_unused = false;
  	bool found_unused_una = false;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		bool unused = false;
@@ -222,7 +223,11 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
  			    mptcp_is_available(sk, skb, zero_wnd_test))
  				return sk;
@@ -256,7 +261,7 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  {
  	struct sock *meta_sk;
  	const struct tcp_sock *tp = tcp_sk(sk);
-	struct tcp_sock *tp_it;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb_head;
  	struct defsched_priv *dsp = defsched_get_priv(tp);
@@ -279,7 +284,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  		goto retrans;
  	/* Half the cwnd of the slow flow */
-	mptcp_for_each_tp(tp->mpcb, tp_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct tcp_sock *tp_it = mptcp->tp;
+
  		if (tp_it != tp &&
  		    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
  			if (tp->srtt_us < tp_it->srtt_us && inet_csk((struct sock *)tp_it)->icsk_ca_state == TCP_CA_Open) {
@@ -302,7 +309,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  	/* Segment not yet injected into this path? Take it!!! */
  	if (!(TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp->mptcp->path_index))) {
  		bool do_retrans = false;
-		mptcp_for_each_tp(tp->mpcb, tp_it) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			struct tcp_sock *tp_it = mptcp->tp;
+
  			if (tp_it != tp &&
  			    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
  				if (tp_it->snd_cwnd <= 4) {
net/mptcp/mptcp_wvegas.c +3 -2
index 4373b8d1ee8d..787ddaab98a2 100644
@@ -143,14 +143,15 @@ static inline u32 mptcp_wvegas_ssthresh(const struct tcp_sock *tp)
  static u64 mptcp_wvegas_weight(const struct mptcp_cb *mpcb, const struct sock *sk)
  {
  	u64 total_rate = 0;
-	struct sock *sub_sk;
  	const struct wvegas *wvegas = inet_csk_ca(sk);
+	struct mptcp_tcp_sock *mptcp;
  	if (!mpcb)
  		return wvegas->weight;
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct wvegas *sub_wvegas = inet_csk_ca(sub_sk);
  		/* sampled_rtt is initialized by 0 */
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
#26 lore raw

Re: [MPTCP] [PATCH 10/12] mptcp: Make subflow-list an RCU-list

Hi Christoph,

Quoted 1,169 lines · Christoph Paasch
Quoted 933 lines · Matthieu Baerts

Hi Christoph,

Quoted 5 lines · Christoph Paasch

mptcp_add_sock() will now be called without holding the meta-level lock. However, mptcp_add_sock() wants to add the subflow to the meta-level list, thus we need to protect this by a lock. We use the mpcb_list_lock for that.

Quoted 7 lines

Now that we are locking during add/del, and want to allow lockless traversal of the list, this implies that we need to make it an RCU-list.

So, this patch transitions to the RCU hlist. The list-traversal macros (hlist_for_each_entry_rcu) require me to now pass the mptcp_tcp_sock to mptcp_for_each_*. So, I had to change all the places in the code where we call one of the list-traversal macros to adapt to this.

Thank you for this big refactoring!
I quickly reviewed these changes, I hope no 'struct mptcp_tcp_sock' have been overridden but it looks fine, we always add a new variable 'mptcp' :-)

I only have comments in mptcp_redundant.c

Quoted 911 lines
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
  include/net/mptcp.h          |  69 ++++++++++----------
  net/ipv4/af_inet.c           |   9 +--
  net/ipv4/ip_sockglue.c       |   7 +-
  net/ipv4/tcp.c               |  31 +++++----
  net/mptcp/mctcp_desync.c     |  18 ++++--
  net/mptcp/mptcp_balia.c      |   8 ++-
  net/mptcp/mptcp_binder.c     |  10 ++-
  net/mptcp/mptcp_coupled.c    |  11 ++--
  net/mptcp/mptcp_ctrl.c       |  89 +++++++++++++++----------
  net/mptcp/mptcp_fullmesh.c   |  28 +++++---
  net/mptcp/mptcp_input.c      |  44 ++++++++-----
  net/mptcp/mptcp_ndiffports.c |   4 +-
  net/mptcp/mptcp_olia.c       |  20 ++++--
  net/mptcp/mptcp_output.c     |  38 +++++++----
  net/mptcp/mptcp_redundant.c  | 151 ++++++++++++++++++++++++++++++++++---------
  net/mptcp/mptcp_rr.c         |  22 +++++--
  net/mptcp/mptcp_sched.c      |  21 ++++--
  net/mptcp/mptcp_wvegas.c     |   5 +-
  18 files changed, 394 insertions(+), 191 deletions(-)
include/net/mptcp.h +36 -33
index d455bdd101ec..59e3da6c4031 100644
@@ -156,7 +156,7 @@ struct mptcp_options_received {
  };
  struct mptcp_tcp_sock {
-	struct tcp_sock	*next;		/* Next subflow socket */
+	struct hlist_node node;
  	struct hlist_node cb_list;
  	struct mptcp_options_received rx_opt;
@@ -257,7 +257,7 @@ struct mptcp_sched_ops {
  struct mptcp_cb {
  	/* list of sockets in this multipath connection */
-	struct tcp_sock *connection_list;
+	struct hlist_head conn_list;
  	/* list of sockets that need a call to release_cb */
  	struct hlist_head callback_list;
@@ -312,7 +312,7 @@ struct mptcp_cb {
  	/***** Start of fields, used for subflow establishment */
  	struct sock *meta_sk;
-	/* Master socket, also part of the connection_list, this
+	/* Master socket, also part of the conn_list, this
  	 * socket is the one that the application sees.
  	 */
  	struct sock *master_sk;
@@ -664,21 +664,17 @@ extern struct workqueue_struct *mptcp_wq;
  			pr_err(fmt, ##args);					\
  	} while (0)
-/* Iterates over all subflows */
-#define mptcp_for_each_tp(mpcb, tp)					\
-	for ((tp) = (mpcb)->connection_list; (tp); (tp) = (tp)->mptcp->next)
+static inline struct sock *mptcp_to_sock(const struct mptcp_tcp_sock *mptcp)
+{
+	return (struct sock *)mptcp->tp;
+}
-#define mptcp_for_each_sk(mpcb, sk)					\
-	for ((sk) = (struct sock *)(mpcb)->connection_list;		\
-	     sk;							\
-	     sk = (struct sock *)tcp_sk(sk)->mptcp->next)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	hlist_for_each_entry_rcu(__mptcp, &((__mpcb)->conn_list), node)
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)			\
-	for (__sk = (struct sock *)(__mpcb)->connection_list,		\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL; \
-	     __sk;							\
-	     __sk = __temp,						\
-	     __temp = __sk ? (struct sock *)tcp_sk(__sk)->mptcp->next : NULL)
+/* Must be called with the appropriate lock held */
+#define mptcp_for_each_sub_safe(__mpcb, __mptcp, __tmp)				\
+	hlist_for_each_entry_safe(__mptcp, __tmp, &((__mpcb)->conn_list), node)
  /* Iterates over all bit set to 1 in a bitset */
  #define mptcp_for_each_bit_set(b, i)					\
@@ -932,12 +928,14 @@ struct request_sock *rev_mptcp_rsk(const struct mptcp_request_sock *req)
  static inline bool mptcp_can_sendpage(struct sock *sk)
  {
-	struct sock *sk_it;
+	struct mptcp_tcp_sock *mptcp;
  	if (tcp_sk(sk)->mpcb->dss_csum)
  		return false;
-	mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+	mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if (!(sk_it->sk_route_caps & NETIF_F_SG) ||
  		    !sk_check_csum_caps(sk_it))
  			return false;
@@ -972,9 +970,12 @@ static inline void mptcp_send_reset(struct sock *sk)
  static inline void mptcp_sub_force_close_all(struct mptcp_cb *mpcb,
  					     struct sock *except)
  {
-	struct sock *sk_it, *tmp;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
+
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
-	mptcp_for_each_sk_safe(mpcb, sk_it, tmp) {
  		if (sk_it != except)
  			mptcp_send_reset(sk_it);
  	}
@@ -1161,12 +1162,14 @@ static inline int mptcp_sk_can_send_ack(const struct sock *sk)
  /* Only support GSO if all subflows supports it */
  static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	if (tcp_sk(meta_sk)->mpcb->dss_csum)
  		return false;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
  		if (!sk_can_gso(sk))
@@ -1177,12 +1180,14 @@ static inline bool mptcp_sk_can_gso(const struct sock *meta_sk)
  static inline bool mptcp_can_sg(const struct sock *meta_sk)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	if (tcp_sk(meta_sk)->mpcb->dss_csum)
  		return false;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
  		if (!(sk->sk_route_caps & NETIF_F_SG))
@@ -1193,9 +1198,9 @@ static inline bool mptcp_can_sg(const struct sock *meta_sk)
  static inline void mptcp_set_rto(struct sock *sk)
  {
-	struct tcp_sock *tp = tcp_sk(sk);
-	struct sock *sk_it;
  	struct inet_connection_sock *micsk = inet_csk(mptcp_meta_sk(sk));
+	struct tcp_sock *tp = tcp_sk(sk);
+	struct mptcp_tcp_sock *mptcp;
  	__u32 max_rto = 0;
  	/* We are in recovery-phase on the MPTCP-level. Do not update the
@@ -1204,7 +1209,9 @@ static inline void mptcp_set_rto(struct sock *sk)
  	if (micsk->icsk_retransmits)
  		return;
-	mptcp_for_each_sk(tp->mpcb, sk_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if ((mptcp_sk_can_send(sk_it) || sk->sk_state == TCP_SYN_RECV) &&
  		    inet_csk(sk_it)->icsk_rto > max_rto)
  			max_rto = inet_csk(sk_it)->icsk_rto;
@@ -1303,12 +1310,8 @@ bool mptcp_prune_ofo_queue(struct sock *sk);
  	do {				\
  	} while (0)
-/* Without MPTCP, we just do one iteration
- * over the only socket available. This assumes that
- * the sk/tp arg is the socket in that case.
- */
-#define mptcp_for_each_sk(mpcb, sk)
-#define mptcp_for_each_sk_safe(__mpcb, __sk, __temp)
+#define mptcp_for_each_sub(__mpcb, __mptcp)					\
+	if (0)
  #define MPTCP_INC_STATS(net, field)	\
  	do {				\
net/ipv4/af_inet.c +5 -4
index 426fa704e218..87a98edc18d4 100644
@@ -709,13 +709,14 @@ int inet_accept(struct socket *sock, struct socket *newsock, int flags,
  	sock_rps_record_flow(sk2);
  	if (sk2->sk_protocol == IPPROTO_TCP && mptcp(tcp_sk(sk2))) {
-		struct sock *sk_it = sk2;
+		struct mptcp_tcp_sock *mptcp;
-		mptcp_for_each_sk(tcp_sk(sk2)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tcp_sk(sk2)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  		if (tcp_sk(sk2)->mpcb->master_sk) {
-			sk_it = tcp_sk(sk2)->mpcb->master_sk;
+			struct sock *sk_it = tcp_sk(sk2)->mpcb->master_sk;
  			write_lock_bh(&sk_it->sk_callback_lock);
  			sk_it->sk_wq = newsock->wq;
net/ipv4/ip_sockglue.c +5 -2
index 1fe79c7bcc9e..2b7a8c416c91 100644
@@ -756,8 +756,11 @@ static int do_ip_setsockopt(struct sock *sk, int level,
  			sk_dst_reset(sk);
  			/* Update TOS on mptcp subflow */
  			if (is_meta_sk(sk)) {
-				struct sock *sk_it;
-				mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it) {
+				struct mptcp_tcp_sock *mptcp;
+
+				mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+					struct sock *sk_it = mptcp_to_sock(mptcp);
+
  					if (inet_sk(sk_it)->tos != inet_sk(sk)->tos) {
  						inet_sk(sk_it)->tos = inet_sk(sk)->tos;
  						sk_it->sk_priority = sk->sk_priority;
net/ipv4/tcp.c +19 -12
index 93e15eeba870..fd53722bf59e 100644
@@ -804,9 +804,11 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
  #ifdef CONFIG_MPTCP
  	if (mptcp(tcp_sk(sk))) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  #endif
  	/*
@@ -984,7 +986,7 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  	}
  	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
+		struct mptcp_tcp_sock *mptcp;
  		/* We must check this with socket-lock hold because we iterate
  		 * over the subflows.
@@ -999,8 +1001,9 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  			return ret;
  		}
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
@@ -1293,9 +1296,11 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
  	}
  	if (mptcp(tp)) {
-		struct sock *sk_it = sk;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  	if (unlikely(tp->repair)) {
@@ -1883,9 +1888,11 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  #ifdef CONFIG_MPTCP
  	if (mptcp(tp)) {
-		struct sock *sk_it;
-		mptcp_for_each_sk(tp->mpcb, sk_it)
-			sock_rps_record_flow(sk_it);
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sock_rps_record_flow(mptcp_to_sock(mptcp));
+		}
  	}
  #endif
net/mptcp/mctcp_desync.c +11 -7
index cfc26fcc3161..f6bf9251d59b 100644
@@ -67,7 +67,6 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  	} else {
  		const struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
  		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
  		/* current aggregated cwnd */
  		u32 agg_cwnd = 0;
@@ -90,11 +89,14 @@ static void mctcp_desync_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  				tp->snd_ssthresh = tp->snd_cwnd = INI_MIN_CWND;
  			}
  			return;
-		}
-		  /* In dangerous area, increase slowly and linearly. */
-		  else {
+		} else {
+			/* In dangerous area, increase slowly and linearly. */
+			const struct mptcp_tcp_sock *mptcp;
+
  			/* get total cwnd and the subflow that has min cwnd */
-			mptcp_for_each_sk(tp->mpcb, sub_sk) {
+			mptcp_for_each_sub(tp->mpcb, mptcp) {
+				const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
  				if (mctcp_cc_sk_can_send(sub_sk)) {
  					const struct tcp_sock *sub_tp =
  								tcp_sk(sub_sk);
@@ -126,12 +128,14 @@ static u32 mctcp_desync_ssthresh(struct sock *sk)
  	} else {
  		struct mctcp_desync *ca = inet_csk_ca(mptcp_meta_sk(sk));
  		const u8 subfid = tp->mptcp->path_index;
-		const struct sock *sub_sk;
+		const struct mptcp_tcp_sock *mptcp;
  		u32 max_cwnd = 0;
  		u8 max_cwnd_subfid = 0;
  		/* Find the subflow that has the max cwnd. */
-		mptcp_for_each_sk(tp->mpcb, sub_sk) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
+
  			if (mctcp_cc_sk_can_send(sub_sk)) {
  				const struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  				if (max_cwnd < sub_tp->snd_cwnd) {
net/mptcp/mptcp_balia.c +5 -3
index ac5c5e550539..e8a22fa66a50 100644
@@ -78,7 +78,7 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
  	const struct mptcp_cb *mpcb = tp->mpcb;
-	const struct sock *sub_sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 max_rate = 0, rate = 0, sum_rate = 0;
  	u64 alpha, ai = tp->snd_cwnd, md = (tp->snd_cwnd >> 1);
  	int num_scale_down = 0;
@@ -87,7 +87,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  		return;
  	/* Find max_rate first */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		u64 tmp;
@@ -119,7 +120,8 @@ static void mptcp_balia_recalc_ai(const struct sock *sk)
  	if (num_scale_down) {
  		sum_rate = 0;
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			u64 tmp;
net/mptcp/mptcp_binder.c +7 -3
index d6ef9c5b6ce5..eaf8ba674d17 100644
@@ -46,6 +46,8 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
  	unsigned char *opt_ptr, *opt_end_ptr, opt[MAX_IPOPTLEN];
  	for (i = 0; i < MPTCP_GW_MAX_LISTS; ++i) {
+		struct mptcp_tcp_sock *mptcp;
+
  		if (mptcp_gws->len[i] == 0)
  			goto error;
@@ -53,7 +55,9 @@ static int mptcp_get_avail_list_ipv4(struct sock *sk)
  		list_taken = 0;
  		/* Loop through all sub-sockets in this connection */
-		mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk) {
+		mptcp_for_each_sub(tcp_sk(sk)->mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
  			mptcp_debug("mptcp_get_avail_list_ipv4: Next sock\n");
  			/* Reset length and options buffer, then retrieve
@@ -289,10 +293,10 @@ static int mptcp_parse_gateway_ipv4(char *gateways)
  static int mptcp_binder_subflow_count(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int i = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		i++;
  	}
net/mptcp/mptcp_coupled.c +7 -4
index a6569f5e20ab..e1040a23e59f 100644
@@ -80,7 +80,7 @@ static inline void mptcp_set_forced(const struct sock *meta_sk, bool force)
  static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(sk)->mpcb;
-	const struct sock *sub_sk;
+	const struct mptcp_tcp_sock *mptcp;
  	int best_cwnd = 0, best_rtt = 0, can_send = 0;
  	u64 max_numerator = 0, sum_denominator = 0, alpha = 1;
@@ -90,7 +90,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	/* Do regular alpha-calculation for multiple subflows */
  	/* Find the max numerator of the alpha-calculation */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		u64 tmp;
@@ -118,7 +119,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  		goto exit;
  	/* Calculate the denominator */
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		const struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  		if (!mptcp_ccc_sk_can_send(sub_sk))
@@ -132,7 +134,8 @@ static void mptcp_ccc_recalc_alpha(const struct sock *sk)
  	sum_denominator *= sum_denominator;
  	if (unlikely(!sum_denominator)) {
  		pr_err("%s: sum_denominator == 0\n", __func__);
-		mptcp_for_each_sk(mpcb, sub_sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			const struct sock *sub_sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *sub_tp = tcp_sk(sub_sk);
  			pr_err("%s: pi:%d, state:%d\n, rtt:%u, cwnd: %u",
  			       __func__, sub_tp->mptcp->path_index,
net/mptcp/mptcp_ctrl.c +55 -34
index 9237a73ec174..fe824649bcc3 100644
@@ -553,10 +553,12 @@ void mptcp_hash_remove_bh(struct tcp_sock *meta_tp)
  struct sock *mptcp_select_ack_sock(const struct sock *meta_sk)
  {
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk, *rttsk = NULL, *lastsk = NULL;
+	struct sock *rttsk = NULL, *lastsk = NULL;
  	u32 min_time = 0, last_active = 0;
+	struct mptcp_tcp_sock *mptcp;
-	mptcp_for_each_sk(meta_tp->mpcb, sk) {
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		u32 elapsed;
@@ -689,7 +691,8 @@ void mptcp_sock_destruct(struct sock *sk)
  void mptcp_destroy_sock(struct sock *sk)
  {
  	if (is_meta_sk(sk)) {
-		struct sock *sk_it, *tmpsk;
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
  		__skb_queue_purge(&tcp_sk(sk)->mpcb->reinject_queue);
@@ -699,7 +702,9 @@ void mptcp_destroy_sock(struct sock *sk)
  		 * not have been closed properly (as we are waiting for the
  		 * DATA_ACK of the DATA_FIN).
  		 */
-		mptcp_for_each_sk_safe(tcp_sk(sk)->mpcb, sk_it, tmpsk) {
+		mptcp_for_each_sub_safe(tcp_sk(sk)->mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
+
  			/* Already did call tcp_close - waiting for graceful
  			 * closure, or if we are retransmitting fast-close on
  			 * the subflow. The reset (or timeout) will kill the
@@ -1281,6 +1286,7 @@ static int mptcp_alloc_mpcb(struct sock *meta_sk, __u64 remote_key,
  	INIT_LIST_HEAD(&mpcb->tw_list);
  	INIT_HLIST_HEAD(&mpcb->callback_list);
+	INIT_HLIST_HEAD(&mpcb->conn_list);
  	spin_lock_init(&mpcb->mpcb_list_lock);
  	mptcp_mpcb_inherit_sockopts(meta_sk, master_sk);
@@ -1370,8 +1376,12 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  	sock_hold(meta_sk);
  	refcount_inc(&mpcb->mpcb_refcnt);
-	tp->mptcp->next = mpcb->connection_list;
-	mpcb->connection_list = tp;
+	local_bh_disable();
+	spin_lock(&mpcb->mpcb_list_lock);
+	hlist_add_head_rcu(&tp->mptcp->node, &mpcb->conn_list);
+	spin_unlock(&mpcb->mpcb_list_lock);
+	local_bh_enable();
+
  	tp->mptcp->attached = 1;
  	atomic_add(atomic_read(&((struct sock *)tp)->sk_rmem_alloc),
@@ -1415,14 +1425,13 @@ int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
  void mptcp_del_sock(struct sock *sk)
  {
-	struct tcp_sock *tp = tcp_sk(sk), *tp_prev;
+	struct tcp_sock *tp = tcp_sk(sk);
  	struct mptcp_cb *mpcb;
  	if (!tp->mptcp || !tp->mptcp->attached)
  		return;
  	mpcb = tp->mpcb;
-	tp_prev = mpcb->connection_list;
  	if (mpcb->sched_ops->release)
  		mpcb->sched_ops->release(sk);
@@ -1434,17 +1443,10 @@ void mptcp_del_sock(struct sock *sk)
  		    __func__, mpcb->mptcp_loc_token, tp->mptcp->path_index,
  		    sk->sk_state, is_meta_sk(sk));
-	if (tp_prev == tp) {
-		mpcb->connection_list = tp->mptcp->next;
-	} else {
-		for (; tp_prev && tp_prev->mptcp->next; tp_prev = tp_prev->mptcp->next) {
-			if (tp_prev->mptcp->next == tp) {
-				tp_prev->mptcp->next = tp->mptcp->next;
-				break;
-			}
-		}
-	}
-	tp->mptcp->next = NULL;
+	spin_lock(&mpcb->mpcb_list_lock);
+	hlist_del_init_rcu(&tp->mptcp->node);
+	spin_unlock(&mpcb->mpcb_list_lock);
+
  	tp->mptcp->attached = 0;
  	mpcb->path_index_bits &= ~(1 << tp->mptcp->path_index);
@@ -1491,8 +1493,8 @@ void mptcp_update_metasocket(const struct sock *meta_sk)
  void mptcp_cleanup_rbuf(struct sock *meta_sk, int copied)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk;
  	bool recheck_rcv_window = false;
+	struct mptcp_tcp_sock *mptcp;
  	__u32 rcv_window_now = 0;
  	if (copied > 0 && !(meta_sk->sk_shutdown & RCV_SHUTDOWN)) {
@@ -1503,7 +1505,8 @@ void mptcp_cleanup_rbuf(struct sock *meta_sk, int copied)
  			recheck_rcv_window = true;
  	}
-	mptcp_for_each_sk(meta_tp->mpcb, sk) {
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		const struct inet_connection_sock *icsk = inet_csk(sk);
@@ -1690,10 +1693,13 @@ EXPORT_SYMBOL(mptcp_sub_force_close);
   */
  void mptcp_update_sndbuf(const struct tcp_sock *tp)
  {
-	struct sock *meta_sk = tp->meta_sk, *sk;
+	struct sock *meta_sk = tp->meta_sk;
  	int new_sndbuf = 0, old_sndbuf = meta_sk->sk_sndbuf;
+	struct mptcp_tcp_sock *mptcp;
+
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
-	mptcp_for_each_sk(tp->mpcb, sk) {
  		if (!mptcp_sk_can_send(sk))
  			continue;
@@ -1722,8 +1728,8 @@ void mptcp_update_sndbuf(const struct tcp_sock *tp)
  void mptcp_close(struct sock *meta_sk, long timeout)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk_it, *tmpsk;
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb;
  	int data_was_unread = 0;
  	int state;
@@ -1756,7 +1762,12 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	/* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
  	if (meta_sk->sk_state == TCP_CLOSE) {
-		mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
+
+		mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk_it)->send_mp_fclose)
  				continue;
  			mptcp_sub_close(sk_it, 0);
@@ -1777,10 +1788,14 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	} else if (tcp_close_state(meta_sk)) {
  		mptcp_send_fin(meta_sk);
  	} else if (meta_tp->snd_una == meta_tp->write_seq) {
+		struct mptcp_tcp_sock *mptcp;
+		struct hlist_node *tmp;
+
  		/* The DATA_FIN has been sent and acknowledged
  		 * (e.g., by sk_shutdown). Close all the other subflows
  		 */
-		mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
+		mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			unsigned long delay = 0;
  			/* If we are the passive closer, don't trigger
  			 * subflow-fin until the subflow has been finned
@@ -1804,7 +1819,9 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  	/* socket will be freed after mptcp_close - we have to prevent
  	 * access from the subflows.
  	 */
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		/* Similar to sock_orphan, but we don't set it DEAD, because
  		 * the callbacks are still set and must be called.
  		 */
@@ -1889,8 +1906,9 @@ void mptcp_close(struct sock *meta_sk, long timeout)
  void mptcp_disconnect(struct sock *sk)
  {
-	struct sock *subsk, *tmpsk;
+	struct mptcp_tcp_sock *mptcp;
  	struct tcp_sock *tp = tcp_sk(sk);
+	struct hlist_node *tmp;
  	__skb_queue_purge(&tp->mpcb->reinject_queue);
@@ -1898,7 +1916,9 @@ void mptcp_disconnect(struct sock *sk)
  		mptcp_hash_remove_bh(tp);
  	local_bh_disable();
-	mptcp_for_each_sk_safe(tp->mpcb, subsk, tmpsk) {
+	mptcp_for_each_sub_safe(tp->mpcb, mptcp, tmp) {
+		struct sock *subsk = mptcp_to_sock(mptcp);
+
  		/* The socket will get removed from the subsocket-list
  		 * and made non-mptcp by setting mpc to 0.
  		 *
@@ -2582,7 +2602,6 @@ static void mptcp_get_sub_info(struct sock *sk, struct mptcp_sub_info *info)
  int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  {
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	struct sock *sk;
  	struct mptcp_meta_info meta_info;
  	struct mptcp_info m_info;
@@ -2628,16 +2647,17 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  	if (m_info.subflows) {
  		unsigned int len, sub_len = 0;
+		struct mptcp_tcp_sock *mptcp;
  		char __user *ptr;
  		ptr = (char __user *)m_info.subflows;
  		len = m_info.sub_len;
-		mptcp_for_each_sk(meta_tp->mpcb, sk) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
  			struct tcp_info t_info;
  			unsigned int tmp_len;
-			tcp_get_info(sk, &t_info);
+			tcp_get_info(mptcp_to_sock(mptcp), &t_info);
  			tmp_len = min_t(unsigned int, len, info_len);
  			len -= tmp_len;
@@ -2657,6 +2677,7 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  	if (m_info.subflow_info) {
  		unsigned int len, sub_info_len, total_sub_info_len = 0;
+		struct mptcp_tcp_sock *mptcp;
  		char __user *ptr;
  		ptr = (char __user *)m_info.subflow_info;
@@ -2666,11 +2687,11 @@ int mptcp_get_info(const struct sock *meta_sk, char __user *optval, int optlen)
  				     sizeof(struct mptcp_sub_info));
  		m_info.sub_info_len = sub_info_len;
-		mptcp_for_each_sk(meta_tp->mpcb, sk) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
  			struct mptcp_sub_info m_sub_info;
  			unsigned int tmp_len;
-			mptcp_get_sub_info(sk, &m_sub_info);
+			mptcp_get_sub_info(mptcp_to_sock(mptcp), &m_sub_info);
  			tmp_len = min_t(unsigned int, len, sub_info_len);
  			len -= tmp_len;
net/mptcp/mptcp_fullmesh.c +20 -8
index d29102a1dffb..53c1ba7319f5 100644
@@ -903,8 +903,9 @@ static void mptcp_address_worker(struct work_struct *work)
  			}
  			if (event->code == MPTCP_EVENT_DEL) {
-				struct sock *sk, *tmpsk;
+				struct mptcp_tcp_sock *mptcp;
  				struct mptcp_loc_addr *mptcp_local;
+				struct hlist_node *tmp;
  				bool found = false;
  				mptcp_local = rcu_dereference_bh(fm_ns->local);
@@ -914,7 +915,9 @@ static void mptcp_address_worker(struct work_struct *work)
  					update_addr_bitfields(meta_sk, mptcp_local);
  				/* Look for the socket and remove him */
-				mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+				mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
+
  					if ((event->family == AF_INET6 &&
  					     (sk->sk_family == AF_INET ||
  					      mptcp_v6_is_v4_mapped(sk))) ||
@@ -964,9 +967,10 @@ static void mptcp_address_worker(struct work_struct *work)
  			}
  			if (event->code == MPTCP_EVENT_MOD) {
-				struct sock *sk;
+				struct mptcp_tcp_sock *mptcp;
-				mptcp_for_each_sk(mpcb, sk) {
+				mptcp_for_each_sub(mpcb, mptcp) {
+					struct sock *sk = mptcp_to_sock(mptcp);
  					struct tcp_sock *tp = tcp_sk(sk);
  					if (event->family == AF_INET &&
  					    (sk->sk_family == AF_INET ||
@@ -1455,8 +1459,9 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  	struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
  	struct fullmesh_priv *fmp = fullmesh_get_priv(mpcb);
  	const struct mptcp_fm_ns *fm_ns = fm_get_ns(sock_net(meta_sk));
-	struct sock *sk, *tmpsk;
  	bool meta_v4 = meta_sk->sk_family == AF_INET;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
  	int i;
  	rcu_read_lock_bh();
@@ -1470,7 +1475,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		struct in_addr ifa = mptcp_local->locaddr4[i].addr;
  		bool found = false;
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp = tcp_sk(sk);
  			if (sk->sk_family == AF_INET6 &&
@@ -1491,6 +1497,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		}
  		if (!found) {
+			struct sock *sk;
+
  			fmp->add_addr++;
  			mpcb->addr_signal = 1;
@@ -1511,7 +1519,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		struct in6_addr ifa = mptcp_local->locaddr6[i].addr;
  		bool found = false;
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp = tcp_sk(sk);
  			if (sk->sk_family == AF_INET ||
@@ -1532,6 +1541,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  		}
  		if (!found) {
+			struct sock *sk;
+
  			fmp->add_addr++;
  			mpcb->addr_signal = 1;
@@ -1546,7 +1557,8 @@ static void full_mesh_release_sock(struct sock *meta_sk)
  #endif
  	/* Now, detect address-removals */
-	mptcp_for_each_sk_safe(mpcb, sk, tmpsk) {
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		bool shall_remove = true;
  		if (sk->sk_family == AF_INET || mptcp_v6_is_v4_mapped(sk)) {
net/mptcp/mptcp_input.c +29 -15
index 211605347f70..64b0265bfd06 100644
@@ -126,12 +126,14 @@ static void mptcp_clean_rtx_queue(struct sock *meta_sk, u32 prior_snd_una)
  		tcp_rtx_queue_unlink(skb, meta_sk);
  		if (mptcp_is_data_fin(skb)) {
-			struct sock *sk_it, *sk_tmp;
+			struct mptcp_tcp_sock *mptcp;
+			struct hlist_node *tmp;
  			/* DATA_FIN has been acknowledged - now we can close
  			 * the subflows
  			 */
-			mptcp_for_each_sk_safe(mpcb, sk_it, sk_tmp) {
+			mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+				struct sock *sk_it = mptcp_to_sock(mptcp);
  				unsigned long delay = 0;
  				/* If we are the passive closer, don't trigger
@@ -347,6 +349,7 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  	/* Now, checksum must be 0 */
  	if (unlikely(csum_fold(csum_tcp))) {
+		struct mptcp_tcp_sock *mptcp;
  		struct sock *sk_it = NULL;
  		pr_err("%s csum is wrong: %#x tcp-seq %u dss_csum_added %d overflowed %d iterations %d\n",
@@ -362,7 +365,9 @@ static int mptcp_verif_dss_csum(struct sock *sk)
  		tp->mpcb->csum_cutoff_seq = tp->mptcp->map_data_seq;
  		/* Search for another subflow that is fully established */
-		mptcp_for_each_sk(tp->mpcb, sk_it) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			sk_it = mptcp_to_sock(mptcp);
+
  			if (sk_it != sk &&
  			    tcp_sk(sk_it)->mptcp->fully_established)
  				break;
@@ -1308,12 +1313,15 @@ int mptcp_do_join_short(struct sk_buff *skb,
   */
  void mptcp_fin(struct sock *meta_sk)
  {
-	struct sock *sk = NULL, *sk_it;
+	struct sock *sk = NULL;
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
+	struct mptcp_tcp_sock *mptcp;
  	unsigned char state;
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
+
  		if (tcp_sk(sk_it)->mptcp->path_index == mpcb->dfin_path_index) {
  			sk = sk_it;
  			break;
@@ -1585,9 +1593,12 @@ void mptcp_clean_rtx_infinite(const struct sk_buff *skb, struct sock *sk)
  static void mptcp_send_reset_rem_id(const struct mptcp_cb *mpcb, u8 rem_id)
  {
-	struct sock *sk_it, *tmpsk;
+	struct mptcp_tcp_sock *mptcp;
+	struct hlist_node *tmp;
+
+	mptcp_for_each_sub_safe(mpcb, mptcp, tmp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
-	mptcp_for_each_sk_safe(mpcb, sk_it, tmpsk) {
  		if (tcp_sk(sk_it)->mptcp->rem_id == rem_id) {
  			mptcp_reinject_data(sk_it, 0);
  			mptcp_send_reset(sk_it);
@@ -1892,13 +1903,15 @@ void tcp_parse_mptcp_options(const struct sk_buff *skb,
  bool mptcp_check_rtt(const struct tcp_sock *tp, int time)
  {
  	struct mptcp_cb *mpcb = tp->mpcb;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u32 rtt_max = 0;
  	/* In MPTCP, we take the max delay across all flows,
  	 * in order to take into account meta-reordering buffers.
  	 */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_recv(sk))
  			continue;
@@ -2173,9 +2186,9 @@ bool mptcp_handle_options(struct sock *sk, const struct tcphdr *th,
  		if (mopt->saw_low_prio == 1) {
  			tp->mptcp->rcv_low_prio = mopt->low_prio;
  		} else {
-			struct sock *sk_it;
-			mptcp_for_each_sk(tp->mpcb, sk_it) {
-				struct mptcp_tcp_sock *mptcp = tcp_sk(sk_it)->mptcp;
+			struct mptcp_tcp_sock *mptcp;
+
+			mptcp_for_each_sub(tp->mpcb, mptcp) {
  				if (mptcp->rem_id == mopt->prio_addr_id)
  					mptcp->rcv_low_prio = mopt->low_prio;
  			}
@@ -2359,7 +2372,7 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  {
  	const struct sock *meta_sk = mptcp_meta_sk(sk);
  	const struct tcp_sock *meta_tp = tcp_sk(meta_sk);
-	const struct sock *sk_it;
+	const struct mptcp_tcp_sock *mptcp;
  	/* We circumvent this check in tcp_check_space, because we want to
  	 * always call sk_write_space. So, we reproduce the check here.
@@ -2385,8 +2398,9 @@ bool mptcp_should_expand_sndbuf(const struct sock *sk)
  	/* For MPTCP we look for a subsocket that could send data.
  	 * If we found one, then we update the send-buffer.
  	 */
-	mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
-		struct tcp_sock *tp_it = tcp_sk(sk_it);
+	mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+		const struct sock *sk_it = mptcp_to_sock(mptcp);
+		const struct tcp_sock *tp_it = tcp_sk(sk_it);
  		if (!mptcp_sk_can_send(sk_it))
  			continue;
net/mptcp/mptcp_ndiffports.c +2 -2
index fe73ade7ba24..4219e9489655 100644
@@ -20,10 +20,10 @@ MODULE_PARM_DESC(num_subflows, "choose the number of subflows per MPTCP connecti
  static int mptcp_ndiffports_subflow_count(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int i = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		i++;
  	}
net/mptcp/mptcp_olia.c +13 -7
index 9f61e1eff65a..c44eb9208581 100644
@@ -61,10 +61,11 @@ static u32 mptcp_get_crt_cwnd(struct sock *sk)
  /* return the dominator of the first term of  the increasing term */
  static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 rate = 1; /* We have to avoid a zero-rate because it is used as a divisor */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		u64 scaled_num;
  		u32 tmp_cwnd;
@@ -83,10 +84,11 @@ static u64 mptcp_get_rate(const struct mptcp_cb *mpcb , u32 path_rtt)
  /* find the maximum cwnd, used to find set M */
  static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u32 best_cwnd = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		u32 tmp_cwnd;
  		if (!mptcp_olia_sk_can_send(sk))
@@ -101,6 +103,7 @@ static u32 mptcp_get_max_cwnd(const struct mptcp_cb *mpcb)
  static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  {
+	struct mptcp_tcp_sock *mptcp;
  	struct mptcp_olia *ca;
  	struct tcp_sock *tp;
  	struct sock *sk;
@@ -113,7 +116,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	max_cwnd = mptcp_get_max_cwnd(mpcb);
  	/* find the best path */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
@@ -135,7 +139,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	/* TODO - integrate this here in mptcp_get_max_cwnd and in the previous loop */
  	/* find the size of M and B_not_M */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
@@ -156,7 +161,8 @@ static void mptcp_get_epsilon(const struct mptcp_cb *mpcb)
  	}
  	/* check if the path is in M or B_not_M and set the value of epsilon accordingly */
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		sk = mptcp_to_sock(mptcp);
  		tp = tcp_sk(sk);
  		ca = inet_csk_ca(sk);
net/mptcp/mptcp_output.c +0 -1
index cc1a4f7d39e9..29a7ab309751 100644
@@ -647,7 +647,6 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct sk_buff *skb;
-	struct sock *sk_it;
  	int ans = 0;
  	if (meta_sk->sk_state == TCP_CLOSE)
@@ -704,17 +703,22 @@ int mptcp_write_wakeup(struct sock *meta_sk, int mib)
Quoted 8 lines

net/mptcp/mptcp_output.c int mptcp_write_wakeup(struct sock *meta_sk, int mib) · line 707

Quoted 4 lines
net/mptcp/mptcp_output.c +2 -0
  		return 0;
  	} else {
+		struct mptcp_tcp_sock *mptcp;
+

detail: that's strange to see the declaration of the variable just before the flag :)
But that's correct.

Yeah, should be fine :)

Quoted 159 lines
Quoted 159 lines
net/mptcp/mptcp_output.c +23 -12
  window_probe:
  		if (between(meta_tp->snd_up, meta_tp->snd_una + 1,
  			    meta_tp->snd_una + 0xFFFF)) {
-			mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+			mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+				struct sock *sk_it = mptcp_to_sock(mptcp);
+
  				if (mptcp_sk_can_send_ack(sk_it))
  					tcp_xmit_probe_skb(sk_it, 1, mib);
  			}
  		}
  		/* At least one of the tcp_xmit_probe_skb's has to succeed */
-		mptcp_for_each_sk(meta_tp->mpcb, sk_it) {
+		mptcp_for_each_sub(meta_tp->mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			int ret;
  			if (!mptcp_sk_can_send_ack(sk_it))
@@ -732,6 +736,7 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  		     int push_one, gfp_t gfp)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk), *subtp;
+	struct mptcp_tcp_sock *mptcp;
  	struct sock *subsk = NULL;
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sk_buff *skb;
@@ -856,7 +861,8 @@ bool mptcp_write_xmit(struct sock *meta_sk, unsigned int mss_now, int nonagle,
  			break;
  	}
-	mptcp_for_each_sk(mpcb, subsk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		subsk = mptcp_to_sock(mptcp);
  		subtp = tcp_sk(subsk);
  		if (!(path_mask & mptcp_pi_to_flag(subtp->mptcp->path_index)))
@@ -1353,7 +1359,7 @@ void mptcp_send_active_reset(struct sock *meta_sk, gfp_t priority)
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct sock *sk;
-	if (!mpcb->connection_list)
+	if (hlist_empty(&mpcb->conn_list))
  		return;
  	WARN_ON(meta_tp->send_mp_fclose);
@@ -1728,10 +1734,11 @@ void mptcp_select_initial_window(const struct sock *sk, int __space, __u32 mss,
  static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
  				  unsigned int (*mss_cb)(struct sock *sk))
  {
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	u64 rate = 0;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		int this_mss;
  		u64 this_rate;
@@ -1783,11 +1790,12 @@ static inline u64 mptcp_calc_rate(const struct sock *meta_sk, unsigned int mss,
  static unsigned int __mptcp_current_mss(const struct sock *meta_sk,
  					unsigned int (*mss_cb)(struct sock *sk))
  {
+	struct mptcp_tcp_sock *mptcp;
  	unsigned int mss = 0;
  	u64 rate = 0;
-	struct sock *sk;
-	mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+	mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		int this_mss;
  		u64 this_rate;
@@ -1852,14 +1860,16 @@ int mptcp_select_size(const struct sock *meta_sk, bool sg, bool first_skb, bool
  int mptcp_check_snd_buf(const struct tcp_sock *tp)
  {
-	const struct sock *sk;
+	const struct mptcp_tcp_sock *mptcp;
  	u32 rtt_max = tp->srtt_us;
  	u64 bw_est;
  	if (!tp->srtt_us)
  		return tp->reordering + 1;
-	mptcp_for_each_sk(tp->mpcb, sk) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		const struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (!mptcp_sk_can_send(sk))
  			continue;
@@ -1877,11 +1887,13 @@ int mptcp_check_snd_buf(const struct tcp_sock *tp)
  unsigned int mptcp_xmit_size_goal(const struct sock *meta_sk, u32 mss_now,
  				  int large_allowed)
  {
-	struct sock *sk;
  	u32 xmit_size_goal = 0;
  	if (large_allowed && mptcp_sk_can_gso(meta_sk)) {
-		mptcp_for_each_sk(tcp_sk(meta_sk)->mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(tcp_sk(meta_sk)->mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
  			int this_size_goal;
  			if (!mptcp_sk_can_send(sk))
net/mptcp/mptcp_redundant.c +19 -9
index 001a3f8a974d..6c68a8a071c1 100644
@@ -52,10 +52,12 @@ static bool redsched_get_active_valid_sks(struct sock *meta_sk)
  {
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
  	int active_valid_sks = 0;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
+
  		if (subflow_is_active((struct tcp_sock *)sk) &&
  		    !mptcp_is_def_unavailable(sk))
  			active_valid_sks++;
@@ -88,6 +90,10 @@ static bool redsched_use_subflow(struct sock *meta_sk,
  	return false;
  }
+#define mptcp_entry_next_rcu(__mptcp)						\
+	hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(			\
+		&(__mptcp)->node)), struct mptcp_tcp_sock, node)
+
  static struct sock *redundant_get_subflow(struct sock *meta_sk,
  					  struct sk_buff *skb,
  					  bool zero_wnd_test)
@@ -95,14 +101,16 @@ static struct sock *redundant_get_subflow(struct sock *meta_sk,
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct redsched_cb_data *cb_data = redsched_get_cb_data(meta_tp);
-	struct tcp_sock *first_tp = cb_data->next_subflow;
-	struct sock *sk;
-	struct tcp_sock *tp;
+	struct tcp_sock *first_tp = cb_data->next_subflow, *tp;
+	struct mptcp_tcp_sock *mptcp;
+	int found = 0;
  	/* Answer data_fin on same subflow */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk)->mptcp->path_index ==
  				mpcb->dfin_path_index &&
  			    mptcp_is_available(sk, skb, zero_wnd_test))
@@ -110,26 +118,58 @@ static struct sock *redundant_get_subflow(struct sock *meta_sk,
  		}
  	}
-	if (!first_tp)
-		first_tp = mpcb->connection_list;
+	if (!first_tp && !hlist_empty(&mpcb->conn_list)) {
+		first_tp = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(&mpcb->conn_list)),
+					    struct mptcp_tcp_sock, node)->tp;
+	}
  	tp = first_tp;
-	/* still NULL (no subflow in connection_list?) */
+	/* still NULL (no subflow in conn_list?) */
  	if (!first_tp)
  		return NULL;
  	/* Search for any subflow to send it */
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 135

Quoted 5 lines
net/mptcp/mptcp_redundant.c +4 -1
-	do {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		/* We go up to the subflow 'tp', then we pick the next one. */
+		if (!found)
+			continue;

I guess I missed something in the patch but found will always be false here, we will never do the rest, no?

You are absolutely right! The "if (!found)" needs to come after the "if (tp == mptcp->tp)". Thanks!

Quoted 12 lines
Quoted 12 lines
net/mptcp/mptcp_redundant.c +8 -1
+		if (tp == mptcp->tp) {
+			found = 1;
+			continue; > +		}
+		tp = mptcp->tp;
+
  		if (mptcp_is_available((struct sock *)tp, skb,
  				       zero_wnd_test)) {
-			cb_data->next_subflow = tp->mptcp->next;
+			struct mptcp_tcp_sock *mptcp;
+
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 152

Quoted 6 lines
net/mptcp/mptcp_redundant.c +4 -0
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
  			return (struct sock *)tp;
  		}

Should we not break here?

No, we want to continue iterating over the remaining subflows.

Quoted 19 lines
Quoted 19 lines
net/mptcp/mptcp_redundant.c +14 -4
+	}
+
+	mptcp_for_each_sub(mpcb, mptcp) {
+		tp = mptcp->tp;
+
+		if (tp == first_tp)
+			break;
+
+		if (mptcp_is_available((struct sock *)tp, skb,
+				       zero_wnd_test)) {
+			struct mptcp_tcp_sock *mptcp;
-		tp = tp->mptcp->next;
-		if (!tp)
-			tp = mpcb->connection_list;
-	} while (tp != first_tp);
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 172

Quoted 5 lines
net/mptcp/mptcp_redundant.c +5 -0
+			else
+				cb_data->next_subflow = NULL;
+			return (struct sock *)tp;
+		}
+	}

I am not sure I understand why you need two mptcp_for_each_sub here. May you add a comment? :)

Yes, I will add the below comment above the very first mptcp_for_each_sub:

Quoted 9 lines

/* Search for a subflow to send it.
*
* We want to pick a subflow that is after 'tp' in the list of subflows.
* Thus, the first mptcp_for_each_sub()-loop tries to walk the list up
* to the subflow 'tp' and then checks whether any one of the remaining
* ones is eligible to send.
* The second mptcp_for_each-sub()-loop is then iterating from the
* beginning of the list up to 'tp'.
*/

Yes, thank you, clearer with this comment :)

Quoted 134 lines
Quoted 32 lines
Quoted 32 lines
net/mptcp/mptcp_redundant.c +9 -6
  	/* No space */
  	return NULL;
@@ -190,10 +230,11 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  	struct tcp_sock *meta_tp = tcp_sk(meta_sk);
  	struct mptcp_cb *mpcb = meta_tp->mpcb;
  	struct redsched_cb_data *cb_data = redsched_get_cb_data(meta_tp);
-	struct tcp_sock *first_tp = cb_data->next_subflow;
-	struct tcp_sock *tp;
-	struct sk_buff *skb;
+	struct tcp_sock *first_tp = cb_data->next_subflow, *tp;
+	struct mptcp_tcp_sock *mptcp;
  	int active_valid_sks = -1;
+	struct sk_buff *skb;
+	int found = 0;
  	/* As we set it, we have to reset it as well. */
  	*limit = 0;
@@ -215,10 +256,12 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  	/* Then try indistinctly redundant and normal skbs */
-	if (!first_tp)
-		first_tp = mpcb->connection_list;
+	if (!first_tp && !hlist_empty(&mpcb->conn_list)) {
+		first_tp = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(&mpcb->conn_list)),
+					    struct mptcp_tcp_sock, node)->tp;
+	}
-	/* still NULL (no subflow in connection_list?) */
+	/* still NULL (no subflow in conn_list?) */
  	if (!first_tp)
  		return NULL;
@@ -226,9 +269,17 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  	*reinject = 0;
  	active_valid_sks = redsched_get_active_valid_sks(meta_sk);
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sk_buff *redundant_next_segment(struct sock *meta_sk, · line 276

Quoted 5 lines
net/mptcp/mptcp_redundant.c +3 -1
-	do {
+	mptcp_for_each_sub(mpcb, mptcp) {
  		struct redsched_sock_data *sk_data;
+		if (!found)
+			continue;

Same here, I guess I missed a goto somewhere but found will always be 0 here.

Yes, same fix needed here as above.

Quoted 59 lines
Quoted 59 lines
net/mptcp/mptcp_redundant.c +39 -5
+		if (tp == mptcp->tp) {
+			found = 1;
+			continue;
+		}
+
  		/* Correct the skb pointers of the current subflow */
  		sk_data = redsched_get_sock_data(tp);
  		redsched_correct_skb_pointers(meta_sk, sk_data);
@@ -237,20 +288,55 @@ static struct sk_buff *redundant_next_segment(struct sock *meta_sk,
  						    sk_data->skb, meta_sk);
  		if (skb && redsched_use_subflow(meta_sk, active_valid_sks, tp,
  						skb)) {
+			struct mptcp_tcp_sock *mptcp;
+
  			sk_data->skb = skb;
  			sk_data->skb_end_seq = TCP_SKB_CB(skb)->end_seq;
-			cb_data->next_subflow = tp->mptcp->next;
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
  			*subsk = (struct sock *)tp;
  			if (TCP_SKB_CB(skb)->path_mask)
  				*reinject = -1;
  			return skb;
  		}
+	}
-		tp = tp->mptcp->next;
-		if (!tp)
-			tp = mpcb->connection_list;
-	} while (tp != first_tp);
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct redsched_sock_data *sk_data;
+
+		tp = mptcp->tp;
+
+		if (tp == first_tp)
+			break;
+
+		/* Correct the skb pointers of the current subflow */
+		sk_data = redsched_get_sock_data(tp);
+		redsched_correct_skb_pointers(meta_sk, sk_data);
+
+		skb = redundant_next_skb_from_queue(&meta_sk->sk_write_queue,
+						    sk_data->skb, meta_sk);
+		if (skb && redsched_use_subflow(meta_sk, active_valid_sks, tp,
+						skb)) {
+			struct mptcp_tcp_sock *mptcp;
+
+			sk_data->skb = skb;
+			sk_data->skb_end_seq = TCP_SKB_CB(skb)->end_seq;
+			mptcp = mptcp_entry_next_rcu(tp->mptcp);
+			if (mptcp)
+				cb_data->next_subflow = mptcp->tp;
+			else
+				cb_data->next_subflow = NULL;
+			*subsk = (struct sock *)tp;
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sk_buff *redundant_next_segment(struct sock *meta_sk, · line 339

Quoted 6 lines
net/mptcp/mptcp_redundant.c +6 -0
+
+			if (TCP_SKB_CB(skb)->path_mask)
+				*reinject = -1;
+			return skb;
+		}
+	}

Could you also add a comment explaining the two for_each's? :)

Sure, done!

Quoted 12 lines
Quoted 12 lines
net/mptcp/mptcp_redundant.c +3 -2
  	/* Nothing to send */
  	return NULL;
@@ -264,8 +350,15 @@ static void redundant_release(struct sock *sk)
  	/* Check if the next subflow would be the released one. If yes correct
  	 * the pointer
  	 */
-	if (cb_data->next_subflow == tp)
-		cb_data->next_subflow = tp->mptcp->next;
+	if (cb_data->next_subflow == tp) {
+		struct mptcp_tcp_sock *mptcp;
+
Quoted 8 lines

net/mptcp/mptcp_redundant.c static struct sock *redundant_get_subflow(struct sock *meta_sk, · line 153

Quoted 6 lines
net/mptcp/mptcp_redundant.c +6 -0
+		mptcp = mptcp_entry_next_rcu(tp->mptcp);
+		if (mptcp)
+			cb_data->next_subflow = mptcp->tp;
+		else
+			cb_data->next_subflow = NULL;
+	}

Should we not create a function for this block?

Quoted 1 line

Yes, we should :) Done as well.

Thanks!

Matt

Quoted 168 lines

Thanks!

Christoph

Quoted 163 lines

Best regards,
Matt

Quoted 137 lines
net/mptcp/mptcp_redundant.c +0 -0
  }
  static struct mptcp_sched_ops mptcp_sched_redundant = {
net/mptcp/mptcp_rr.c +15 -7
index 8910ba9e6052..d97706034646 100644
@@ -102,12 +102,14 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
  					     bool zero_wnd_test)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk, *bestsk = NULL, *backupsk = NULL;
+	struct sock *sk = NULL, *bestsk = NULL, *backupsk = NULL;
+	struct mptcp_tcp_sock *mptcp;
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
  			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
  			    mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
  				return sk;
@@ -115,8 +117,11 @@ static struct sock *rr_get_available_subflow(struct sock *meta_sk,
  	}
  	/* First, find the best subflow */
-	mptcp_for_each_sk(mpcb, sk) {
-		struct tcp_sock *tp = tcp_sk(sk);
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct tcp_sock *tp;
+
+		sk = mptcp_to_sock(mptcp);
+		tp = tcp_sk(sk);
  		if (!mptcp_rr_is_available(sk, skb, zero_wnd_test, true))
  			continue;
@@ -177,7 +182,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  					     unsigned int *limit)
  {
  	const struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
-	struct sock *sk_it, *choose_sk = NULL;
+	struct sock *choose_sk = NULL;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb = __mptcp_rr_next_segment(meta_sk, reinject);
  	unsigned char split = num_segments;
  	unsigned char iter = 0, full_subs = 0;
@@ -199,7 +205,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  retry:
  	/* First, we look for a subflow who is currently being used */
-	mptcp_for_each_sk(mpcb, sk_it) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk_it = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp_it = tcp_sk(sk_it);
  		struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
@@ -233,7 +240,8 @@ static struct sk_buff *mptcp_rr_next_segment(struct sock *meta_sk,
  		/* So, we restart this round by setting quota to 0 and retry
  		 * to find a subflow.
  		 */
-		mptcp_for_each_sk(mpcb, sk_it) {
+		mptcp_for_each_sub(mpcb, mptcp) {
+			struct sock *sk_it = mptcp_to_sock(mptcp);
  			struct tcp_sock *tp_it = tcp_sk(sk_it);
  			struct rrsched_priv *rsp = rrsched_get_priv(tp_it);
net/mptcp/mptcp_sched.c +15 -6
index c831515fd7f0..09246e286da8 100644
@@ -139,9 +139,10 @@ static struct sock
  	u32 min_srtt = 0xffffffff;
  	bool found_unused = false;
  	bool found_unused_una = false;
-	struct sock *sk;
+	struct mptcp_tcp_sock *mptcp;
-	mptcp_for_each_sk(mpcb, sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sk = mptcp_to_sock(mptcp);
  		struct tcp_sock *tp = tcp_sk(sk);
  		bool unused = false;
@@ -222,7 +223,11 @@ struct sock *get_available_subflow(struct sock *meta_sk, struct sk_buff *skb,
  	/* Answer data_fin on same subflow!!! */
  	if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
  	    skb && mptcp_is_data_fin(skb)) {
-		mptcp_for_each_sk(mpcb, sk) {
+		struct mptcp_tcp_sock *mptcp;
+
+		mptcp_for_each_sub(mpcb, mptcp) {
+			sk = mptcp_to_sock(mptcp);
+
  			if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
  			    mptcp_is_available(sk, skb, zero_wnd_test))
  				return sk;
@@ -256,7 +261,7 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  {
  	struct sock *meta_sk;
  	const struct tcp_sock *tp = tcp_sk(sk);
-	struct tcp_sock *tp_it;
+	struct mptcp_tcp_sock *mptcp;
  	struct sk_buff *skb_head;
  	struct defsched_priv *dsp = defsched_get_priv(tp);
@@ -279,7 +284,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  		goto retrans;
  	/* Half the cwnd of the slow flow */
-	mptcp_for_each_tp(tp->mpcb, tp_it) {
+	mptcp_for_each_sub(tp->mpcb, mptcp) {
+		struct tcp_sock *tp_it = mptcp->tp;
+
  		if (tp_it != tp &&
  		    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
  			if (tp->srtt_us < tp_it->srtt_us && inet_csk((struct sock *)tp_it)->icsk_ca_state == TCP_CA_Open) {
@@ -302,7 +309,9 @@ static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
  	/* Segment not yet injected into this path? Take it!!! */
  	if (!(TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp->mptcp->path_index))) {
  		bool do_retrans = false;
-		mptcp_for_each_tp(tp->mpcb, tp_it) {
+		mptcp_for_each_sub(tp->mpcb, mptcp) {
+			struct tcp_sock *tp_it = mptcp->tp;
+
  			if (tp_it != tp &&
  			    TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
  				if (tp_it->snd_cwnd <= 4) {
net/mptcp/mptcp_wvegas.c +3 -2
index 4373b8d1ee8d..787ddaab98a2 100644
@@ -143,14 +143,15 @@ static inline u32 mptcp_wvegas_ssthresh(const struct tcp_sock *tp)
  static u64 mptcp_wvegas_weight(const struct mptcp_cb *mpcb, const struct sock *sk)
  {
  	u64 total_rate = 0;
-	struct sock *sub_sk;
  	const struct wvegas *wvegas = inet_csk_ca(sk);
+	struct mptcp_tcp_sock *mptcp;
  	if (!mpcb)
  		return wvegas->weight;
-	mptcp_for_each_sk(mpcb, sub_sk) {
+	mptcp_for_each_sub(mpcb, mptcp) {
+		struct sock *sub_sk = mptcp_to_sock(mptcp);
  		struct wvegas *sub_wvegas = inet_csk_ca(sub_sk);
  		/* sampled_rtt is initialized by 0 */
 
Signature · 20 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and delete
this e-mail from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.
author
#27 lore raw

PATCH 11/12 mptcp: Don't take meta-lock when receiving third ACK

In reply to Christoph Paasch

Now everything is ready to do this in a lockless way.

Simply, in tcp_v4(6)_rcv, stop taking the meta-level lock. When creating the initial subflow, we currently are also holding the meta-level lock. We need to drop this one as soon as we handover control to the subflow (aka., when exiting mptcp_check_req_master()).

Data-reception however still requires the meta-level lock. Thus, we need to take it again in tcp_child_process().

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 net/ipv4/tcp_ipv4.c      | 41 ++++-------------------------------------
 net/ipv4/tcp_minisocks.c |  9 +++++++--
 net/ipv6/tcp_ipv6.c      | 41 ++++-------------------------------------
 net/mptcp/mptcp_ctrl.c   | 13 +++++++++++++
 net/mptcp/mptcp_ipv4.c   |  5 ++++-
 net/mptcp/mptcp_ipv6.c   |  4 ++++
 6 files changed, 36 insertions(+), 77 deletions(-)
net/ipv4/tcp_ipv4.c +4 -37
index 4735bbed5978..9f22a9644acd 100644
@@ -1730,45 +1730,16 @@ int tcp_v4_rcv(struct sk_buff *skb)
 			inet_csk_reqsk_queue_drop_and_put(sk, req);
 			goto lookup;
 		}
+		if (unlikely(is_meta_sk(sk) && !mptcp_can_new_subflow(sk))) {
+			inet_csk_reqsk_queue_drop_and_put(sk, req);
+			goto lookup;
+		}
 		/* We own a reference on the listener, increase it again
 		 * as we might lose it too soon.
 		 */
 		sock_hold(sk);
 		refcounted = true;
 
-		if (is_meta_sk(sk)) {
-			bh_lock_sock(sk);
-
-			if (!mptcp_can_new_subflow(sk)) {
-				inet_csk_reqsk_queue_drop_and_put(sk, req);
-				bh_unlock_sock(sk);
-
-				goto discard_and_relse;
-			}
-
-			if (sock_owned_by_user(sk)) {
-				th = (const struct tcphdr *)skb->data;
-				iph = ip_hdr(skb);
-				tcp_v4_fill_cb(skb, iph, th);
-
-				skb->sk = sk;
-				if (unlikely(sk_add_backlog(sk, skb,
-							    sk->sk_rcvbuf + sk->sk_sndbuf))) {
-					reqsk_put(req);
-
-					bh_unlock_sock(sk);
-					__NET_INC_STATS(net, LINUX_MIB_TCPBACKLOGDROP);
-					goto discard_and_relse;
-				}
-
-				reqsk_put(req);
-				bh_unlock_sock(sk);
-				sock_put(sk);
-
-				return 0;
-			}
-		}
-
 		nsk = NULL;
 		if (!tcp_filter(sk, skb)) {
 			th = (const struct tcphdr *)skb->data;
@@ -1778,15 +1749,11 @@ int tcp_v4_rcv(struct sk_buff *skb)
 		}
 		if (!nsk) {
 			reqsk_put(req);
-			if (is_meta_sk(sk))
-				bh_unlock_sock(sk);
 			goto discard_and_relse;
 		}
 		if (nsk == sk) {
 			reqsk_put(req);
 			tcp_v4_restore_cb(skb);
-			if (is_meta_sk(sk))
-				bh_unlock_sock(sk);
 		} else if (tcp_child_process(sk, nsk, skb)) {
 			tcp_v4_send_reset(nsk, skb);
 			goto discard_and_relse;
net/ipv4/tcp_minisocks.c +7 -2
index 99658e9973f5..a4aeb206381e 100644
@@ -897,6 +897,11 @@ int tcp_child_process(struct sock *parent, struct sock *child,
 	sk_mark_napi_id(child, skb);
 
 	tcp_segs_in(tcp_sk(child), skb);
+	/* The following will be removed when we allow lockless data-reception
+	 * on the subflows.
+	 */
+	if (mptcp(tcp_sk(child)))
+		bh_lock_sock(meta_sk);
 	if (!sock_owned_by_user(meta_sk)) {
 		ret = tcp_rcv_state_process(child, skb);
 		/* Wakeup parent, send SIGIO */
@@ -912,9 +917,9 @@ int tcp_child_process(struct sock *parent, struct sock *child,
 		__sk_add_backlog(meta_sk, skb);
 	}
 
+	bh_unlock_sock(child);
 	if (mptcp(tcp_sk(child)))
-		bh_unlock_sock(child);
-	bh_unlock_sock(meta_sk);
+		bh_unlock_sock(meta_sk);
 	sock_put(child);
 	return ret;
 }
net/ipv6/tcp_ipv6.c +4 -37

60 lines not on this page · raw message

net/mptcp/mptcp_ctrl.c +13 -0
index fe824649bcc3..e2ad2cafcef0 100644
@@ -1333,6 +1333,7 @@ static u8 mptcp_set_new_pathindex(struct mptcp_cb *mpcb)
 	return i;
 }
 
+/* May be called without holding the meta-level lock */
 int mptcp_add_sock(struct sock *meta_sk, struct sock *sk, u8 loc_id, u8 rem_id,
 		   gfp_t flags)
 {
@@ -2112,6 +2113,11 @@ int mptcp_check_req_fastopen(struct sock *child, struct request_sock *req)
 	/* Handled by the master_sk */
 	tcp_sk(meta_sk)->fastopen_rsk = NULL;
 
+	/* Subflow establishment is now lockless, drop the lock here it will
+	 * be taken again in tcp_child_process().
+	 */
+	bh_unlock_sock(meta_sk);
+
 	return 0;
 }
 
@@ -2141,9 +2147,15 @@ int mptcp_check_req_master(struct sock *sk, struct sock *child,
 		inet_csk_reqsk_queue_add(sk, req, meta_sk);
 	}
 
+	/* Subflow establishment is now lockless, drop the lock here it will
+	 * be taken again in tcp_child_process().
+	 */
+	bh_unlock_sock(meta_sk);
+
 	return 0;
 }
 
+/* May be called without holding the meta-level lock */
 struct sock *mptcp_check_req_child(struct sock *meta_sk,
 				   struct sock *child,
 				   struct request_sock *req,
@@ -2386,6 +2398,7 @@ void mptcp_tsq_sub_deferred(struct sock *meta_sk)
 	}
 }
 
+/* May be called without holding the meta-level lock */
 void mptcp_join_reqsk_init(const struct mptcp_cb *mpcb,
 			   const struct request_sock *req,
 			   struct sk_buff *skb)
net/mptcp/mptcp_ipv4.c +4 -1
index 36cdbf09a5b9..986a77e1bb51 100644
@@ -96,6 +96,7 @@ static u32 mptcp_v4_cookie_init_seq(struct request_sock *req, const struct sock
 }
 #endif
 
+/* May be called without holding the meta-level lock */
 static int mptcp_v4_join_init_req(struct request_sock *req, const struct sock *meta_sk,
 				  struct sk_buff *skb, bool want_cookie)
 {
@@ -141,7 +142,9 @@ struct request_sock_ops mptcp_request_sock_ops __read_mostly = {
 	.syn_ack_timeout =	tcp_syn_ack_timeout,
 };
 
-/* Similar to: tcp_v4_conn_request */
+/* Similar to: tcp_v4_conn_request
+ * May be called without holding the meta-level lock
+ */
 static int mptcp_v4_join_request(struct sock *meta_sk, struct sk_buff *skb)
 {
 	return tcp_conn_request(&mptcp_request_sock_ops,
net/mptcp/mptcp_ipv6.c +4 -0
index bde4a9d7ab32..87f1a088ff79 100644
@@ -125,6 +125,7 @@ static u32 mptcp_v6_cookie_init_seq(struct request_sock *req, const struct sock
 }
 #endif
 
+/* May be called without holding the meta-level lock */
 static int mptcp_v6_join_init_req(struct request_sock *req, const struct sock *meta_sk,
 				  struct sk_buff *skb, bool want_cookie)
 {
@@ -170,6 +171,9 @@ struct request_sock_ops mptcp6_request_sock_ops __read_mostly = {
 	.syn_ack_timeout =	tcp_syn_ack_timeout,
 };
 
+/* Similar to: tcp_v6_conn_request
+ * May be called without holding the meta-level lock
+ */
 static int mptcp_v6_join_request(struct sock *meta_sk, struct sk_buff *skb)
 {
 	return tcp_conn_request(&mptcp6_request_sock_ops,
Signature · 1 line
2.16.2
author
#28 lore raw

PATCH 12/12 mptcp: Make mptcp_do_join_short and mptcp_lookup_join lockless

In reply to Christoph Paasch

Now we can also make the subflow-SYN+MP_JOIN reception lockless.

In mptcp_lookup_join and mptcp_do_join_short, we were checking for mptcp_can_new_subflow(), because it is not possible to call tcp_v4_send_reset() while holding two locks (the listener lock and the meta-lock when coming from mptcp_do_join_short) when in mptcp_v4_do_rcv. Now that we remove the locking, we can remove the check for mptcp_can_new_subflow().

The remaining changes simply skip taking the meta-level lock which also implies that we don't have to check for sock_owned_by_user().

Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 net/mptcp/mptcp_input.c | 70 +++++++------------------------------------------
 1 file changed, 9 insertions(+), 61 deletions(-)
net/mptcp/mptcp_input.c +9 -61
index 64b0265bfd06..34cc613ee584 100644
@@ -1151,7 +1151,6 @@ struct mp_join *mptcp_find_join(const struct sk_buff *skb)
 
 int mptcp_lookup_join(struct sk_buff *skb, struct inet_timewait_sock *tw)
 {
-	const struct mptcp_cb *mpcb;
 	struct sock *meta_sk;
 	u32 token;
 	bool meta_v4;
@@ -1184,16 +1183,6 @@ int mptcp_lookup_join(struct sk_buff *skb, struct inet_timewait_sock *tw)
 		return -1;
 	}
 
-	mpcb = tcp_sk(meta_sk)->mpcb;
-	if (!mptcp_can_new_subflow(meta_sk)) {
-		/* We are in fallback-mode on the reception-side -
-		 * no new subflows!
-		 */
-		sock_put(meta_sk); /* Taken by mptcp_hash_find */
-		MPTCP_INC_STATS(sock_net(meta_sk), MPTCP_MIB_JOINFALLBACK);
-		return -1;
-	}
-
 	/* Coming from time-wait-sock processing in tcp_v4_rcv.
 	 * We have to deschedule it before continuing, because otherwise
 	 * mptcp_v4_do_rcv will hit again on it inside tcp_v4_hnd_req.
@@ -1204,26 +1193,13 @@ int mptcp_lookup_join(struct sk_buff *skb, struct inet_timewait_sock *tw)
 	/* OK, this is a new syn/join, let's create a new open request and
 	 * send syn+ack
 	 */
-	bh_lock_sock_nested(meta_sk);
-	if (sock_owned_by_user(meta_sk)) {
-		skb->sk = meta_sk;
-		if (unlikely(sk_add_backlog(meta_sk, skb,
-					    meta_sk->sk_rcvbuf + meta_sk->sk_sndbuf))) {
-			bh_unlock_sock(meta_sk);
-			__NET_INC_STATS(sock_net(meta_sk),
-					 LINUX_MIB_TCPBACKLOGDROP);
-			sock_put(meta_sk); /* Taken by mptcp_hash_find */
-			kfree_skb(skb);
-			return 1;
-		}
-	} else if (skb->protocol == htons(ETH_P_IP)) {
+	if (skb->protocol == htons(ETH_P_IP)) {
 		tcp_v4_do_rcv(meta_sk, skb);
 #if IS_ENABLED(CONFIG_IPV6)
 	} else {
 		tcp_v6_do_rcv(meta_sk, skb);
 #endif /* CONFIG_IPV6 */
 	}
-	bh_unlock_sock(meta_sk);
 	sock_put(meta_sk); /* Taken by mptcp_hash_find */
 	return 1;
 }
@@ -1260,48 +1236,20 @@ int mptcp_do_join_short(struct sk_buff *skb,
 	/* OK, this is a new syn/join, let's create a new open request and
 	 * send syn+ack
 	 */
-	bh_lock_sock(meta_sk);
 
-	/* This check is also done in mptcp_vX_do_rcv. But, there we cannot
-	 * call tcp_vX_send_reset, because we hold already two socket-locks.
-	 * (the listener and the meta from above)
-	 *
-	 * And the send-reset will try to take yet another one (ip_send_reply).
-	 * Thus, we propagate the reset up to tcp_rcv_state_process.
+	/* mptcp_v4_do_rcv tries to free the skb - we prevent this, as
+	 * the skb will finally be freed by tcp_v4_do_rcv (where we are
+	 * coming from)
 	 */
-	if (!mptcp_can_new_subflow(meta_sk)) {
-		MPTCP_INC_STATS(sock_net(meta_sk), MPTCP_MIB_JOINFALLBACK);
-		bh_unlock_sock(meta_sk);
-		sock_put(meta_sk); /* Taken by mptcp_hash_find */
-		return -1;
-	}
-
-	if (sock_owned_by_user(meta_sk)) {
-		skb->sk = meta_sk;
-		if (unlikely(sk_add_backlog(meta_sk, skb,
-					    meta_sk->sk_rcvbuf + meta_sk->sk_sndbuf)))
-			__NET_INC_STATS(net, LINUX_MIB_TCPBACKLOGDROP);
-		else
-			/* Must make sure that upper layers won't free the
-			 * skb if it is added to the backlog-queue.
-			 */
-			skb_get(skb);
-	} else {
-		/* mptcp_v4_do_rcv tries to free the skb - we prevent this, as
-		 * the skb will finally be freed by tcp_v4_do_rcv (where we are
-		 * coming from)
-		 */
-		skb_get(skb);
-		if (skb->protocol == htons(ETH_P_IP)) {
-			tcp_v4_do_rcv(meta_sk, skb);
+	skb_get(skb);
+	if (skb->protocol == htons(ETH_P_IP)) {
+		tcp_v4_do_rcv(meta_sk, skb);
 #if IS_ENABLED(CONFIG_IPV6)
-		} else { /* IPv6 */
-			tcp_v6_do_rcv(meta_sk, skb);
+	} else { /* IPv6 */
+		tcp_v6_do_rcv(meta_sk, skb);
 #endif /* CONFIG_IPV6 */
-		}
 	}
 
-	bh_unlock_sock(meta_sk);
 	sock_put(meta_sk); /* Taken by mptcp_hash_find */
 	return 0;
 }
Signature · 1 line
2.16.2
#29 lore raw
In reply to Christoph Paasch

Hi Christoph,

Quoted 6 lines · Christoph Paasch

The most recent TCP stack supports lockless listeners. For MPTCP, we however are still taking the meta-lock when creating new subflows (cfr., tcp_v4_rcv and mptcp_lookup_join).

This patchset makes subflow establishment lockless by using lockless procedures in the stack, RCU-lists, atomic operations,...

Thank you for this nice patch set! I took me a bit of time to even start the review, sorry for that!

Unfortunately, today, I have been interrupted in my review again. But here are already a few comments for the first 7th patches. I will continue to look at them ASAP!

Cheers,
Matt

Quoted 39 lines

Christoph Paasch (12):
mptcp: Cleanup
mptcp: Improved debugging
mptcp: Use correct tcp_queue when calling mptcp_fragment
mptcp: Use mptcp_can_new_subflow where possible
mptcp: Render mptcp_sub_inherit_sockopts lockless
mptcp: Make mptcp_set_new_pathindex lockless
mptcp: Remove cnt_subflows
mptcp: Remove cnt_established
mptcp: Rename tw_lock to mpcb_list_lock
mptcp: Make subflow-list an RCU-list
mptcp: Don't take meta-lock when receiving third ACK
mptcp: Make mptcp_do_join_short and mptcp_lookup_join lockless

  include/net/mptcp.h          | 120 ++++++++++-----------------
  net/ipv4/af_inet.c           |   9 ++-
  net/ipv4/ip_sockglue.c       |   7 +-
  net/ipv4/tcp.c               |  31 ++++---
  net/ipv4/tcp_ipv4.c          |  41 +---------
  net/ipv4/tcp_minisocks.c     |   9 ++-
  net/ipv6/tcp_ipv6.c          |  41 +---------
  net/mptcp/mctcp_desync.c     |  22 ++---
  net/mptcp/mptcp_balia.c      |  21 ++---
  net/mptcp/mptcp_binder.c     |  23 +++++-
  net/mptcp/mptcp_coupled.c    |  52 +++++-------
  net/mptcp/mptcp_ctrl.c       | 187 ++++++++++++++++++++++++++++---------------
  net/mptcp/mptcp_fullmesh.c   |  30 ++++---
  net/mptcp/mptcp_input.c      | 150 ++++++++++++----------------------
  net/mptcp/mptcp_ipv4.c       |  34 +++-----
  net/mptcp/mptcp_ipv6.c       |  15 ++--
  net/mptcp/mptcp_ndiffports.c |  17 +++-
  net/mptcp/mptcp_olia.c       |  28 ++++---
  net/mptcp/mptcp_output.c     |  50 ++++++++----
  net/mptcp/mptcp_redundant.c  | 151 +++++++++++++++++++++++++++-------
  net/mptcp/mptcp_rr.c         |  22 +++--
  net/mptcp/mptcp_sched.c      |  32 ++++----
  net/mptcp/mptcp_wvegas.c     |   5 +-
  23 files changed, 586 insertions(+), 511 deletions(-)
Signature · 21 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium

-- 


DISCLAIMER.
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify the 
system manager. This message contains confidential information and is 
intended only for the individual named. If you are not the named addressee 
you should not disseminate, distribute or copy this e-mail. Please notify 
the sender immediately by e-mail if you have received this e-mail by 
mistake and delete this e-mail from your system. If you are not the 
intended recipient you are notified that disclosing, copying, distributing 
or taking any action in reliance on the contents of this information is 
strictly prohibited.