Skip to contentSkip to conversation

mptcp: improve mptcp release callback

PATCH In review Paolo Abeni 3 participants 8 messages Feb 9, 2021 – Feb 10, 2021

Subsystems (2)
lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

cover mptcp: improve mptcp release callback

The current callback implementation is probe to some race and still use some suboptimal code from older times.

This series clean-up the code a bit, fixing the race and hopefully simplifying future wider usage of delegated actions

Paolo Abeni (3):
mptcp: factor out __mptcp_retrans helper()
mptcp: fix race in release_cb
mptcp: clean-up the rtx path.

 net/mptcp/protocol.c | 164 +++++++++++++++++++++----------------------
 net/mptcp/protocol.h |   1 +
 2 files changed, 81 insertions(+), 84 deletions(-)
Signature · 1 line
2.26.2
author
#1 lore raw

PATCH 1/3 mptcp: factor out __mptcp_retrans helper()

Will simplify the following patch, no functional change intended.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 93 ++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 43 deletions(-)
net/mptcp/protocol.c +50 -43
index b5d290c8d998f..7e5016903e11c 100644
@@ -2232,14 +2232,58 @@ static void mptcp_check_fastclose(struct mptcp_sock *msk)
 	mptcp_close_wake_up(sk);
 }
 
-static void mptcp_worker(struct work_struct *work)
+static void __mptcp_retrans(struct sock *sk)
 {
-	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
-	struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
+	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct mptcp_sendmsg_info info = {};
 	struct mptcp_data_frag *dfrag;
 	size_t copied = 0;
-	int state, ret;
+	struct sock *ssk;
+	int ret;
+
+	__mptcp_clean_una(sk);
+	dfrag = mptcp_rtx_head(sk);
+	if (!dfrag)
+		return;
+
+	ssk = mptcp_subflow_get_retrans(msk);
+	if (!ssk)
+		goto reset_timer;
+
+	lock_sock(ssk);
+
+	/* limit retransmission to the bytes already sent on some subflows */
+	info.sent = 0;
+	info.limit = dfrag->already_sent;
+	while (info.sent < dfrag->already_sent) {
+		if (!mptcp_alloc_tx_skb(sk, ssk))
+			break;
+
+		ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
+		if (ret <= 0)
+			break;
+
+		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
+		copied += ret;
+		info.sent += ret;
+	}
+	if (copied)
+		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
+			 info.size_goal);
+
+	mptcp_set_timeout(sk, ssk);
+	release_sock(ssk);
+
+reset_timer:
+	if (!mptcp_timer_pending(sk))
+		mptcp_reset_timer(sk);
+}
+
+static void mptcp_worker(struct work_struct *work)
+{
+	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
+	struct sock *sk = &msk->sk.icsk_inet.sk;
+	int state;
 
 	lock_sock(sk);
 	state = sk->sk_state;
@@ -2275,45 +2319,8 @@ static void mptcp_worker(struct work_struct *work)
 	if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
 		__mptcp_close_subflow(msk);
 
-	if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
-		goto unlock;
-
-	__mptcp_clean_una(sk);
-	dfrag = mptcp_rtx_head(sk);
-	if (!dfrag)
-		goto unlock;
-
-	ssk = mptcp_subflow_get_retrans(msk);
-	if (!ssk)
-		goto reset_unlock;
-
-	lock_sock(ssk);
-
-	/* limit retransmission to the bytes already sent on some subflows */
-	info.sent = 0;
-	info.limit = dfrag->already_sent;
-	while (info.sent < dfrag->already_sent) {
-		if (!mptcp_alloc_tx_skb(sk, ssk))
-			break;
-
-		ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
-		if (ret <= 0)
-			break;
-
-		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
-		copied += ret;
-		info.sent += ret;
-	}
-	if (copied)
-		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
-			 info.size_goal);
-
-	mptcp_set_timeout(sk, ssk);
-	release_sock(ssk);
-
-reset_unlock:
-	if (!mptcp_timer_pending(sk))
-		mptcp_reset_timer(sk);
+	if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
+		__mptcp_retrans(sk);
 
 unlock:
 	release_sock(sk);
Signature · 1 line
2.26.2
author
#2 lore raw

PATCH 2/3 mptcp: fix race in release_cb

In reply to Paolo Abeni

If we receive a MPTCP_PUSH_PENDING even from a subflow when mptcp_release_cb() is serving the previous one, the latter will be delayed up to the next release_sock(msk).

Address the issue implementing a test/serve loop for such event.

Additionally rename the push helper to __mptcp_push_pending() to be more consistent with the existing code.

Fixes 6e628cd3a8f7 ("mptcp: use mptcp release_cb for delayed tasks")
Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +21 -12
index 7e5016903e11c..0393cef5a08d8 100644
@@ -1442,7 +1442,7 @@ static void mptcp_push_release(struct sock *sk, struct sock *ssk,
 	release_sock(ssk);
 }
 
-static void mptcp_push_pending(struct sock *sk, unsigned int flags)
+static void __mptcp_push_pending(struct sock *sk, unsigned int flags)
 {
 	struct sock *prev_ssk = NULL, *ssk = NULL;
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -1694,14 +1694,14 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 
 wait_for_memory:
 		mptcp_set_nospace(sk);
-		mptcp_push_pending(sk, msg->msg_flags);
+		__mptcp_push_pending(sk, msg->msg_flags);
 		ret = sk_stream_wait_memory(sk, &timeo);
 		if (ret)
 			goto out;
 	}
 
 	if (copied)
-		mptcp_push_pending(sk, msg->msg_flags);
+		__mptcp_push_pending(sk, msg->msg_flags);
 
 out:
 	release_sock(sk);
@@ -2942,13 +2942,14 @@ static void mptcp_release_cb(struct sock *sk)
 {
 	unsigned long flags, nflags;
 
-	/* push_pending may touch wmem_reserved, do it before the later
-	 * cleanup
-	 */
-	if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
-		__mptcp_clean_una(sk);
-	if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) {
-		/* mptcp_push_pending() acquires the subflow socket lock
+	for (;;) {
+		flags = 0;
+		if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
+			flags |= MPTCP_PUSH_PENDING;
+		if (!flags)
+			break;
+
+		/* the following actions acquire the subflow socket lock
 		 *
 		 * 1) can't be invoked in atomic scope
 		 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
@@ -2957,13 +2958,21 @@ static void mptcp_release_cb(struct sock *sk)
 		 */
 
 		spin_unlock_bh(&sk->sk_lock.slock);
-		mptcp_push_pending(sk, 0);
+		if (flags & MPTCP_PUSH_PENDING)
+			__mptcp_push_pending(sk, 0);
+
+		cond_resched();
 		spin_lock_bh(&sk->sk_lock.slock);
 	}
+
+	if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
+		__mptcp_clean_una(sk);
 	if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
 		__mptcp_error_report(sk);
 
-	/* clear any wmem reservation and errors */
+	/* push_pending may touch wmem_reserved, ensure we do the cleanup
+	 * later
+	 */
 	__mptcp_update_wmem(sk);
 	__mptcp_update_rmem(sk);
 
Signature · 1 line
2.26.2
author
#5 lore raw

PATCH 3/3 mptcp: clean-up the rtx path.

In reply to Paolo Abeni

After the previous patch we can easily avoid invoking the workqueue to perform the retransmission, if the msk socket lock is held at rtx timer expiration.

This also simplifies the relevant code.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 42 +++++++++++-------------------------------
 net/mptcp/protocol.h |  1 +
 2 files changed, 12 insertions(+), 31 deletions(-)
net/mptcp/protocol.c +11 -31
index 0393cef5a08d8..646f91e1c792e 100644
@@ -2038,28 +2038,21 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	return copied;
 }
 
-static void mptcp_retransmit_handler(struct sock *sk)
-{
-	struct mptcp_sock *msk = mptcp_sk(sk);
-
-	set_bit(MPTCP_WORK_RTX, &msk->flags);
-	mptcp_schedule_work(sk);
-}
-
 static void mptcp_retransmit_timer(struct timer_list *t)
 {
 	struct inet_connection_sock *icsk = from_timer(icsk, t,
 						       icsk_retransmit_timer);
 	struct sock *sk = &icsk->icsk_inet.sk;
+	struct mptcp_sock *msk = mptcp_sk(sk);
 
 	bh_lock_sock(sk);
 	if (!sock_owned_by_user(sk)) {
-		mptcp_retransmit_handler(sk);
+		/* we need a process context to retransmit */
+		if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
+			mptcp_schedule_work(sk);
 	} else {
 		/* delegate our work to tcp_release_cb() */
-		if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED,
-				      &sk->sk_tsq_flags))
-			sock_hold(sk);
+		set_bit(MPTCP_RETRANSMIT, &msk->flags);
 	}
 	bh_unlock_sock(sk);
 	sock_put(sk);
@@ -2935,17 +2928,16 @@ void __mptcp_check_push(struct sock *sk, struct sock *ssk)
 	}
 }
 
-#define MPTCP_DEFERRED_ALL (TCPF_WRITE_TIMER_DEFERRED)
-
 /* processes deferred events and flush wmem */
 static void mptcp_release_cb(struct sock *sk)
 {
-	unsigned long flags, nflags;
-
 	for (;;) {
-		flags = 0;
+		unsigned long flags = 0;
+
 		if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
 			flags |= MPTCP_PUSH_PENDING;
+		if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags))
+			flags |= MPTCP_RETRANSMIT;
 		if (!flags)
 			break;
 
@@ -2960,6 +2952,8 @@ static void mptcp_release_cb(struct sock *sk)
 		spin_unlock_bh(&sk->sk_lock.slock);
 		if (flags & MPTCP_PUSH_PENDING)
 			__mptcp_push_pending(sk, 0);
+		if (flags & MPTCP_RETRANSMIT)
+			__mptcp_retrans(sk);
 
 		cond_resched();
 		spin_lock_bh(&sk->sk_lock.slock);
@@ -2975,20 +2969,6 @@ static void mptcp_release_cb(struct sock *sk)
 	 */
 	__mptcp_update_wmem(sk);
 	__mptcp_update_rmem(sk);
-
-	do {
-		flags = sk->sk_tsq_flags;
-		if (!(flags & MPTCP_DEFERRED_ALL))
-			return;
-		nflags = flags & ~MPTCP_DEFERRED_ALL;
-	} while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
-
-	sock_release_ownership(sk);
-
-	if (flags & TCPF_WRITE_TIMER_DEFERRED) {
-		mptcp_retransmit_handler(sk);
-		__sock_put(sk);
-	}
 }
 
 void mptcp_subflow_process_delegated(struct sock *ssk)
net/mptcp/protocol.h +1 -0
index 91827d9497669..9fc34f3479b75 100644
@@ -103,6 +103,7 @@
 #define MPTCP_PUSH_PENDING	6
 #define MPTCP_CLEAN_UNA		7
 #define MPTCP_ERROR_REPORT	8
+#define MPTCP_RETRANSMIT	9
 
 static inline bool before64(__u64 seq1, __u64 seq2)
 {
Signature · 1 line
2.26.2
#3 lore raw

[MPTCP] Re: [PATCH mptcp-next 2/3] mptcp: fix race in release_cb

In reply to Paolo Abeni

Hi Paolo,

Thank you for the patch!

Quoted 9 lines · Paolo Abeni

If we receive a MPTCP_PUSH_PENDING even from a subflow when mptcp_release_cb() is serving the previous one, the latter will be delayed up to the next release_sock(msk).

Address the issue implementing a test/serve loop for such event.

Additionally rename the push helper to __mptcp_push_pending() to be more consistent with the existing code.

(...)

Quoted 7 lines
net/mptcp/protocol.c +0 -3
@@ -2942,13 +2942,14 @@ static void mptcp_release_cb(struct sock *sk)
  {
  	unsigned long flags, nflags;
 
-	/* push_pending may touch wmem_reserved, do it before the later
-	 * cleanup
-	 */

net/mptcp/protocol.c static void mptcp_release_cb(struct sock *sk) · line 2950

Quoted 10 lines
net/mptcp/protocol.c +6 -4
-	if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
-		__mptcp_clean_una(sk);
-	if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) {
-		/* mptcp_push_pending() acquires the subflow socket lock
+	for (;;) {
+		flags = 0;
+		if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
+			flags |= MPTCP_PUSH_PENDING;
+		if (!flags)
+			break;

This looks strange: "flags" is reset to 0 then set if the previous condition is valid and we check what was done at the previous instruction. Maybe clearer with "else" instead of "if (!flags)"?

Quoted 15 lines
net/mptcp/protocol.c +6 -1
+
+		/* the following actions acquire the subflow socket lock
  		 *
  		 * 1) can't be invoked in atomic scope
  		 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
@@ -2957,13 +2958,21 @@ static void mptcp_release_cb(struct sock *sk)
  		 */
 
  		spin_unlock_bh(&sk->sk_lock.slock);
-		mptcp_push_pending(sk, 0);
+		if (flags & MPTCP_PUSH_PENDING)
+			__mptcp_push_pending(sk, 0);
+
+		cond_resched();
  		spin_lock_bh(&sk->sk_lock.slock);

net/mptcp/protocol.c static void mptcp_release_cb(struct sock *sk) · line 2974

Quoted 10 lines
net/mptcp/protocol.c +5 -1
  	}
+
+	if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
+		__mptcp_clean_una(sk);
  	if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
  		__mptcp_error_report(sk);
 
-	/* clear any wmem reservation and errors */
+	/* push_pending may touch wmem_reserved, ensure we do the cleanup
+	 * later

But we still need to update "rmem", right? :)
(oh yes but even before, we were only talking about "wmem", fine!)

net/mptcp/protocol.c static void mptcp_release_cb(struct sock *sk) · line 2978

Quoted 5 lines
net/mptcp/protocol.c +1 -0
+	 */
  	__mptcp_update_wmem(sk);
  	__mptcp_update_rmem(sk);
 

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#4 lore raw

[MPTCP] Re: [PATCH mptcp-next 2/3] mptcp: fix race in release_cb

Quoted 30 lines · Matthieu Baerts

Hi Paolo,

Thank you for the patch!

Quoted 9 lines · Paolo Abeni

If we receive a MPTCP_PUSH_PENDING even from a subflow when mptcp_release_cb() is serving the previous one, the latter will be delayed up to the next release_sock(msk).

Address the issue implementing a test/serve loop for such event.

Additionally rename the push helper to __mptcp_push_pending() to be more consistent with the existing code.

(...)

Quoted 13 lines
net/mptcp/protocol.c +3 -7
@@ -2942,13 +2942,14 @@ static void mptcp_release_cb(struct sock *sk)
   {
       unsigned long flags, nflags;
-    /* push_pending may touch wmem_reserved, do it before the later
-     * cleanup
-     */
-    if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
-        __mptcp_clean_una(sk);
-    if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) {
-        /* mptcp_push_pending() acquires the subflow socket lock
+    for (;;) {
+        flags = 0;
+        if (test_and_clear_bit(MPTCP_PUSH_PENDING, 
Quoted 8 lines

net/mptcp/protocol.c static void mptcp_release_cb(struct sock *sk) · line 2950

Quoted 4 lines
net/mptcp/protocol.c +3 -0
 &mptcp_sk(sk)->flags))
+            flags |= MPTCP_PUSH_PENDING;
+        if (!flags)
+            break;

This looks strange: "flags" is reset to 0 then set if the previous condition is valid and we check what was done at the previous instruction. Maybe clearer with "else" instead of "if (!flags)"?

My bad, I understand why after having looked at the next patch! I hit the send button too fast :)

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
Reviewed-by on the v1 series
#6 lore raw
In reply to Paolo Abeni
Quoted 7 lines · Paolo Abeni

The current callback implementation is probe to some race and still use some suboptimal code from older times.

This series clean-up the code a bit, fixing the race and hopefully simplifying future wider usage of delegated actions

Quoted 11 lines

Paolo Abeni (3):
mptcp: factor out __mptcp_retrans helper()
mptcp: fix race in release_cb
mptcp: clean-up the rtx path.

net/mptcp/protocol.c | 164 +++++++++++++++++++++----------------------
net/mptcp/protocol.h | 1 +

2 files changed, 81 insertions(+), 84 deletions(-)
Signature · 1 line
2.26.2

Thanks Paolo, series looks good:

Reviewed-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>

--
Mat Martineau
Intel

#7 lore raw
In reply to Paolo Abeni

Hi Paolo, Mat,

Quoted 11 lines · Paolo Abeni

The current callback implementation is probe to some race and still use some suboptimal code from older times.

This series clean-up the code a bit, fixing the race and hopefully simplifying future wider usage of delegated actions

Paolo Abeni (3):
mptcp: factor out __mptcp_retrans helper()
mptcp: fix race in release_cb
mptcp: clean-up the rtx path.

Thank you for the patches and the reviews!

It is now in our tree without a dot at the end of the last patch' subject and Mat's RvB tags:

- 1a97b665b9ee: mptcp: factor out __mptcp_retrans helper()
- f1c293d570e1: mptcp: fix race in release_cb
- 711d7b99ea0a: mptcp: clean-up the rtx path
- Results: b72113120e91..9c096197d99f

Tests + export are all good!

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net