Skip to contentSkip to conversation

mptcp: improve accept() and disconnect()

RFC PATCH In review Paolo Abeni 4 participants 9 messages Nov 5, 2021 – Nov 9, 2021

Subsystems (2)
lore
Share & tools
lore

Paolo Abeni

author maintainer
#0 lore raw

cover mptcp: improve accept() and disconnect()

As outlined in the public mtg, mptcp_accept() is currently quite suboptimal, both from performance and code complexity

This series tries to clean it up, enforcing a wider lifetime for the initial subflow, so that we don't need to acquire additional references there.

To reach such goal we need to properly define the disconnect() behavior, which is currently quite incomplete.

Some additional self-tests is likely required,

Paolo Abeni (3):
mptcp: never allow the PM to close a listener subflow
mptcp: full disconnect implementation
mptcp: cleanup accept and poll

 net/mptcp/pm.c         |  10 ++--
 net/mptcp/pm_netlink.c |   3 ++
 net/mptcp/protocol.c   | 120 ++++++++++++++++++++++++++---------------
 net/mptcp/protocol.h   |  15 +++++-
 net/mptcp/subflow.c    |   1 -
 net/mptcp/token.c      |   1 +
 6 files changed, 102 insertions(+), 48 deletions(-)
Signature · 1 line
2.26.3
author maintainer
#1 lore raw

RFC PATCH 1/3 mptcp: never allow the PM to close a listener subflow

Currently, when deleting an endpoint the netlink PM treverses all the local MPTCP sockets, regardless of their status.

If an MPTCP listener socket is bound to the IP matching the delete endpoint, the listener TCP socket will be closed. That is unexpected, the PM should only affect data subflows.

Fix the issue explicitly skipping MPTCP socket in TCP_LISTEN status.

---
 net/mptcp/pm_netlink.c | 3 +++
 1 file changed, 3 insertions(+)
net/mptcp/pm_netlink.c +3 -0
index 7b96be1e9f14..f523051f5aef 100644
@@ -700,6 +700,9 @@ static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
 
 	msk_owned_by_me(msk);
 
+	if (sk->sk_state == TCP_LISTEN)
+		return;
+
 	if (!rm_list->nr)
 		return;
 
Signature · 1 line
2.26.3
author maintainer
#5 lore raw

RFC PATCH 2/3 mptcp: full disconnect implementation

In reply to Paolo Abeni

The current mptcp_disconnect() implementation lacks several steps, we additionally need to reset the msk socket state and flush the subflow list.

Factor out the needed helper to avoid code duplication.

Additionally ensure that the initial subflow is disposed only after mptcp_close(), just reset it at disconnect time.

---
 net/mptcp/pm.c       | 10 +++--
 net/mptcp/protocol.c | 95 ++++++++++++++++++++++++++++++++------------
 net/mptcp/protocol.h | 14 +++++++
 net/mptcp/token.c    |  1 +
 4 files changed, 92 insertions(+), 28 deletions(-)
net/mptcp/pm.c +7 -3
index 6ab386ff3294..6b2bfe89d445 100644
@@ -356,7 +356,7 @@ void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
 	}
 }
 
-void mptcp_pm_data_init(struct mptcp_sock *msk)
+void mptcp_pm_data_reset(struct mptcp_sock *msk)
 {
 	msk->pm.add_addr_signaled = 0;
 	msk->pm.add_addr_accepted = 0;
@@ -371,10 +371,14 @@ void mptcp_pm_data_init(struct mptcp_sock *msk)
 	WRITE_ONCE(msk->pm.remote_deny_join_id0, false);
 	msk->pm.status = 0;
 
+	mptcp_pm_nl_data_init(msk);
+}
+
+void mptcp_pm_data_init(struct mptcp_sock *msk)
+{
 	spin_lock_init(&msk->pm.lock);
 	INIT_LIST_HEAD(&msk->pm.anno_list);
-
-	mptcp_pm_nl_data_init(msk);
+	mptcp_pm_data_reset(msk);
 }
 
 void __init mptcp_pm_init(void)
net/mptcp/protocol.c +70 -25

178 lines not on this page · raw message

net/mptcp/protocol.h +14 -0
index f6f4054e19ee..188b9e044672 100644
@@ -392,6 +392,9 @@ DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
 /* MPTCP subflow context */
 struct mptcp_subflow_context {
 	struct	list_head node;/* conn_list of subflows */
+
+	char	reset_start[0];
+
 	unsigned long avg_pacing_rate; /* protected by msk socket lock */
 	u64	local_key;
 	u64	remote_key;
@@ -438,6 +441,9 @@ struct mptcp_subflow_context {
 	u8	stale_count;
 
 	long	delegated_status;
+
+	char	reset_end[0];
+
 	struct	list_head delegated_node;   /* link into delegated_action, protected by local BH */
 
 	u32	setsockopt_seq;
@@ -469,6 +475,13 @@ mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
 	return subflow->tcp_sock;
 }
 
+static inline void
+mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow)
+{
+	memset(subflow->reset_start, 0, subflow->reset_end - subflow->reset_start);
+	subflow->request_mptcp = 1;
+}
+
 static inline u64
 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
 {
@@ -708,6 +721,7 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
 
 void __init mptcp_pm_init(void);
 void mptcp_pm_data_init(struct mptcp_sock *msk);
+void mptcp_pm_data_reset(struct mptcp_sock *msk);
 void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk);
 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk);
 void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side);
net/mptcp/token.c +1 -0
index e581b341c5be..f52ee7b26aed 100644
@@ -384,6 +384,7 @@ void mptcp_token_destroy(struct mptcp_sock *msk)
 		bucket->chain_len--;
 	}
 	spin_unlock_bh(&bucket->lock);
+	WRITE_ONCE(msk->token, 0);
 }
 
 void __init mptcp_token_init(void)
Signature · 1 line
2.26.3
author maintainer
#6 lore raw

RFC PATCH 3/3 mptcp: cleanup accept and poll

In reply to Paolo Abeni

After the previous patch, msk->subflow will never be deleted during the whole msk lifetime. We don't need anymore to acquire references to it in mptcp_stream_accept() and we can use the listener subflow accept queue to simplify mptcp_poll() for listener socket.

Overall this removes a lock pair and 4 more atomic operations per accept().

---
 net/mptcp/protocol.c | 25 +++++++------------------
 net/mptcp/protocol.h |  1 -
 net/mptcp/subflow.c  |  1 -
 3 files changed, 7 insertions(+), 20 deletions(-)
net/mptcp/protocol.c +7 -18
index 5fe19adb88a5..38f5b4bcba69 100644
@@ -3402,17 +3402,9 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 
 	pr_debug("msk=%p", msk);
 
-	lock_sock(sock->sk);
-	if (sock->sk->sk_state != TCP_LISTEN)
-		goto unlock_fail;
-
 	ssock = __mptcp_nmpc_socket(msk);
 	if (!ssock)
-		goto unlock_fail;
-
-	clear_bit(MPTCP_DATA_READY, &msk->flags);
-	sock_hold(ssock->sk);
-	release_sock(sock->sk);
+		return -EINVAL;
 
 	err = ssock->ops->accept(sock, newsock, flags, kern);
 	if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
@@ -3452,14 +3444,7 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 		release_sock(newsk);
 	}
 
-	if (inet_csk_listen_poll(ssock->sk))
-		set_bit(MPTCP_DATA_READY, &msk->flags);
-	sock_put(ssock->sk);
 	return err;
-
-unlock_fail:
-	release_sock(sock->sk);
-	return -EINVAL;
 }
 
 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
@@ -3505,8 +3490,12 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 
 	state = inet_sk_state_load(sk);
 	pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
-	if (state == TCP_LISTEN)
-		return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 0;
+	if (state == TCP_LISTEN) {
+		if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk))
+			return 0;
+
+		return inet_csk_listen_poll(msk->subflow->sk);
+	}
 
 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
 		mask |= mptcp_check_readable(msk);
net/mptcp/protocol.h +0 -1
index 188b9e044672..b2d016f8803a 100644
@@ -111,7 +111,6 @@
 #define MPTCP_RST_TRANSIENT	BIT(0)
 
 /* MPTCP socket flags */
-#define MPTCP_DATA_READY	0
 #define MPTCP_NOSPACE		1
 #define MPTCP_WORK_RTX		2
 #define MPTCP_WORK_EOF		3
net/mptcp/subflow.c +0 -1
index 72ccbd85941b..4e7395cfab9b 100644
@@ -1293,7 +1293,6 @@ static void subflow_data_ready(struct sock *sk)
 		if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
 			return;
 
-		set_bit(MPTCP_DATA_READY, &msk->flags);
 		parent->sk_data_ready(parent);
 		return;
 	}
Signature · 1 line
2.26.3
#7 lore raw

Re: mptcp: cleanup accept and poll: Build Failure

Hi Paolo,

Thank you for your modifications, that's great!

But sadly, our CI spotted some issues with it when trying to build it.

You can find more details there:

https://patchwork.kernel.org/project/mptcp/patch/bcf5c3220ac27570e942835d97b4b10b8e448eb8.1636107361.git.pabeni@redhat.com/
https://github.com/multipath-tcp/mptcp_net-next/actions/runs/1425370804

Status: failure
Initiator: MPTCPimporter
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/f9fa79e12d45

Feel free to reply to this email if you cannot access logs, if you need some support to fix the error, if this doesn't seem to be caused by your modifications or if the error is a false positive one.

Cheers,
MPTCP GH Action bot

#8 lore raw
In reply to Paolo Abeni
Quoted 12 lines · Paolo Abeni

As outlined in the public mtg, mptcp_accept() is currently quite suboptimal, both from performance and code complexity

This series tries to clean it up, enforcing a wider lifetime for the initial subflow, so that we don't need to acquire additional references there.

To reach such goal we need to properly define the disconnect() behavior, which is currently quite incomplete.

Some additional self-tests is likely required,

Hi Paolo -

I don't have any changes to suggest right now except for the "msk->last_snd = 0" (vs NULL) that the CI found.

And this finally expunges MPTCP_DATA_READY too!

I agree that some kind of test that tries to close/destroy the msk while a listen() (or other calls) are pending would be very useful.

--
Mat Martineau
Intel

#2 lore raw

Re: [RFC PATCH 1/3] mptcp: never allow the PM to close a listener subflow

In reply to Paolo Abeni

Hi Paolo,

Quoted 9 lines · Paolo Abeni

Currently, when deleting an endpoint the netlink PM treverses all the local MPTCP sockets, regardless of their status.

If an MPTCP listener socket is bound to the IP matching the delete endpoint, the listener TCP socket will be closed. That is unexpected, the PM should only affect data subflows.

Fix the issue explicitly skipping MPTCP socket in TCP_LISTEN status.

Good catch!

Should we add a "Fixes" tag and have this backported in stable versions?

Cheers,
Matt

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

Re: [RFC PATCH 1/3] mptcp: never allow the PM to close a listener subflow

Quoted 10 lines · Matthieu Baerts

Hi Paolo,

Quoted 7 lines · Paolo Abeni

Currently, when deleting an endpoint the netlink PM treverses all the local MPTCP sockets, regardless of their status.

If an MPTCP listener socket is bound to the IP matching the delete endpoint, the listener TCP socket will be closed. That is unexpected, the PM should only affect data subflows.

Quoted 6 lines
Quoted 2 lines

Fix the issue explicitly skipping MPTCP socket in TCP_LISTEN status.

Good catch!

Should we add a "Fixes" tag and have this backported in stable versions?

We need this patch to be in the same series of the following two - or at least to enter the tree before the others. I did not had the Fixes tag to allow this patch to target the -net-next tree as the following ones.

BTW I'm working to add self-tests here (mptcp_connect based [!!!]). It looks like tha a good disconnect() implementation should use FASTCLOSE to shutdown the MPTCP-level connection, so I'm going to add the tx part of it, too.

Cheers,

Paolo

#4 lore raw

Re: [RFC PATCH 1/3] mptcp: never allow the PM to close a listener subflow

Quoted 14 lines · Paolo Abeni
Quoted 13 lines · Matthieu Baerts

Hi Paolo,

Quoted 9 lines · Paolo Abeni

Currently, when deleting an endpoint the netlink PM treverses all the local MPTCP sockets, regardless of their status.

If an MPTCP listener socket is bound to the IP matching the delete endpoint, the listener TCP socket will be closed. That is unexpected, the PM should only affect data subflows.

Fix the issue explicitly skipping MPTCP socket in TCP_LISTEN status.

Quoted 8 lines
Quoted 3 lines

Good catch!

Should we add a "Fixes" tag and have this backported in stable versions?

We need this patch to be in the same series of the following two - or at least to enter the tree before the others. I did not had the Fixes tag to allow this patch to target the -net-next tree as the following ones.

Can we not add the Fixes tag and send it to net-next? Or we already send this one to -net while net-next is closed?

Quoted 1 line

BTW I'm working to add self-tests here (mptcp_connect based [!!!]). It

I thought only Florian was allowed to modify mptcp_connect! :-O

Quoted 3 lines

looks like tha a good disconnect() implementation should use FASTCLOSE to shutdown the MPTCP-level connection, so I'm going to add the tx part of it, too.

Good idea, thank you!

Cheers,
Matt

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