Skip to contentSkip to conversation

mptcp: fallback socket cleanup

PATCH Superseded Paolo Abeni 3 participants 12 messages Dec 9, 2019 – Dec 10, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

cover mptcp: fallback socket cleanup

This is a rebase on top of last rebased by Florian.
The old patch 2/5 has been dropped - obsoleted by Florian's patches - and the following patches has been reworked a bit, due to mptcp_subflow_tcp_socket() -> mptcp_subflow_tcp_sock() change.
Last patch now also deal with mptcp_copy_inaddrs() in order to remove the lock from mptcp_finish_connect() we must move that to connect() and calling it also from bind(), listen() allows simplifying mptcp_getname(), too.

From the original cover letter:
---

After all the above, performance for connection fallen back to plain TCP should be almost the same of TCP connection on unpacked kernel.

Addittionally 'nc' and 'netperf' now run successfully when forced to MPTCP. The latter still report a non fatal error at connection closing time as it performs:

getsockopt(4, SOL_TCP, TCP_MAXSEG, //...);
getsockopt(4, SOL_TCP, TCP_INFO, //...);

which is still unsupported after that the mp capable handshake is started.

For the records on the debug build I'm using netperf scores better result with MPTCP that with plain TCP ;) [agreed, completely irrelevant, still nice]

The series applies at selftests and is unsquashed - will require quite a bit of work there and more work to rebase/adjust the patches after kselftest. I'll start that after the pending patches are in.

_Any_ feedback _very_ welcome!

Paolo Abeni (4):
mptcp: clear 'is_tcp' socket flag when the MP_CAPABLE handshake fails
imptcp: cleanup fallback handling
mptcp: add subflow to conn_list early
mptcp: avoid acquiring the msk lock in mptcp_finisch_connect()

 net/mptcp/options.c  |   3 +
 net/mptcp/protocol.c | 320 +++++++++++++++++++++----------------------
 net/mptcp/subflow.c  |   2 +
 3 files changed, 163 insertions(+), 162 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH 1/4 mptcp: clear 'is_tcp' socket flag when the MP_CAPABLE handshake fails

Currently we don't have a single flag for 'mp capable handshake failed', but we can clear the is_tcp flag to record such info.

This will make also processing for fallback socket almost as fast as native ones, and will help cleaning 'fallback' fields usage in later patches.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/options.c | 3 +++
 net/mptcp/subflow.c | 2 ++
 2 files changed, 5 insertions(+)
net/mptcp/options.c +3 -0
index bce5b6ad2829..9ed86e2187d5 100644
@@ -306,6 +306,8 @@ void mptcp_rcv_synsent(struct sock *sk)
 		subflow->mp_capable = 1;
 		subflow->can_ack = 1;
 		subflow->remote_key = tp->rx_opt.mptcp.sndr_key;
+	} else {
+		tcp_sk(sk)->is_mptcp = 0;
 	}
 }
 
@@ -508,6 +510,7 @@ static bool check_fourth_ack(struct mptcp_subflow_context *subflow,
 	 */
 	if (!mp_opt->mp_capable) {
 		subflow->mp_capable = 0;
+		tcp_sk(mptcp_subflow_tcp_sock(subflow))->is_mptcp = 0;
 		return false;
 	}
 	subflow->remote_key = mp_opt->sndr_key;
net/mptcp/subflow.c +2 -0
index e276c9cbf0e8..3726fce5868a 100644
@@ -733,6 +733,8 @@ static void subflow_ulp_clone(const struct request_sock *req,
 		new_ctx->token = subflow_req->token;
 		new_ctx->ssn_offset = subflow_req->ssn_offset;
 		new_ctx->idsn = subflow_req->idsn;
+	} else {
+		tcp_sk(newsk)->is_mptcp = 0;
 	}
 }
 
Signature · 1 line
2.21.0
#2 lore raw

[MPTCP] Re: [PATCH v1 1/4] mptcp: clear 'is_tcp' socket flag when the MP_CAPABLE handshake fails

Quoted 2 lines · Paolo Abeni

Currently we don't have a single flag for 'mp capable handshake failed', but we can clear the is_tcp flag to record such info.

Hi Paolo -

Minor commit message issue, says 'is_tcp' instead of 'is_mptcp' in subject line and first sentence.

Code looks fine.

Mat

Quoted 47 lines

This will make also processing for fallback socket almost as fast as native ones, and will help cleaning 'fallback' fields usage in later patches.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
net/mptcp/options.c | 3 +++
net/mptcp/subflow.c | 2 ++

2 files changed, 5 insertions(+)
net/mptcp/options.c +3 -0
index bce5b6ad2829..9ed86e2187d5 100644
@@ -306,6 +306,8 @@ void mptcp_rcv_synsent(struct sock *sk)
 		subflow->mp_capable = 1;
 		subflow->can_ack = 1;
 		subflow->remote_key = tp->rx_opt.mptcp.sndr_key;
+	} else {
+		tcp_sk(sk)->is_mptcp = 0;
 	}
 }
 
@@ -508,6 +510,7 @@ static bool check_fourth_ack(struct mptcp_subflow_context *subflow,
 	 */
 	if (!mp_opt->mp_capable) {
 		subflow->mp_capable = 0;
+		tcp_sk(mptcp_subflow_tcp_sock(subflow))->is_mptcp = 0;
 		return false;
 	}
 	subflow->remote_key = mp_opt->sndr_key;
net/mptcp/subflow.c +2 -0
index e276c9cbf0e8..3726fce5868a 100644
@@ -733,6 +733,8 @@ static void subflow_ulp_clone(const struct request_sock *req,
 		new_ctx->token = subflow_req->token;
 		new_ctx->ssn_offset = subflow_req->ssn_offset;
 		new_ctx->idsn = subflow_req->idsn;
+	} else {
+		tcp_sk(newsk)->is_mptcp = 0;
 	}
 }
 
Signature · 1 line
2.21.0

--
Mat Martineau
Intel

author
#3 lore raw

PATCH 2/4 imptcp: cleanup fallback handling

In reply to Paolo Abeni

The 'fallback' field has many different meanings:
tcp fallback socket, first subflow, listening socket. Enforcing the correct usage is not easy - currently we have several problems:

1) this will fail, but it should not:
fd = socket(AF_INET{,6}, SOCK_STREAM, MPTCP_PROTO); setsockopt(fd, SOL_IP{,V6}, //...) = -1 EOPNOTSUPP (Operation not supported)

2) this will report success, but it should not:
fd = socket(AF_INET{,6}, SOCK_STREAM, MPTCP_PROTO); connect(fd, //)
bind(fd, //) = 0

3) this will report success, but it should not:
fd = socket(AF_INET{,6}, SOCK_STREAM, MPTCP_PROTO); listen(fd, // )
s = accept(fd, )
bind(s, ...) = 0

This patch:

- explicitly handle the msk socket status in some missing places
- wrap msk->subflow access with helpers describing the expected semantic
- use the above to enforce correct syscall behavior
- create on demand the subflow socket for setsockopt(), if possible

RFC -> v1:
- drop __mptcp_first_socket() helper, always use __mptcp_nmpc_socket() instead

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 182 +++++++++++++++++++++++++++----------------
 1 file changed, 115 insertions(+), 67 deletions(-)
net/mptcp/protocol.c +115 -67
index f0944a5b562d..2a78123a20f4 100644
@@ -22,25 +22,33 @@
 #include <net/mptcp.h>
 #include "protocol.h"
 
-static struct socket *__mptcp_fallback_get_ref(const struct mptcp_sock *msk)
+/* if msk has a single subflow socket, and the mp_capable handshake is not
+ * completed yet or has failed - that is, the socket is Not MP Capable,
+ * returns it.
+ * Otherwise returns NULL
+ */
+static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
 {
-	sock_owned_by_me((const struct sock *)msk);
-
 	if (!msk->subflow)
 		return NULL;
 
-	sock_hold(msk->subflow->sk);
 	return msk->subflow;
 }
 
-static struct socket *mptcp_fallback_get_ref(const struct mptcp_sock *msk)
+/* if msk has a single subflow, and the mp_capable handshake is failed,
+ * get a reference and return it.
+ * Otherwise returns NULL
+ */
+static struct socket *__mptcp_tcp_fallback_get(const struct mptcp_sock *msk)
 {
-	struct socket *ssock;
+	struct socket *ssock = __mptcp_nmpc_socket(msk);
 
-	lock_sock((struct sock *)msk);
-	ssock = __mptcp_fallback_get_ref(msk);
-	release_sock((struct sock *)msk);
+	sock_owned_by_me((const struct sock *)msk);
 
+	if (!ssock || tcp_sk(ssock->sk)->is_mptcp)
+		return NULL;
+
+	sock_hold(ssock->sk);
 	return ssock;
 }
 
@@ -57,6 +65,49 @@ static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)
 	return NULL;
 }
 
+static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
+{
+	return ((struct sock *)msk)->sk_state == TCP_CLOSE;
+}
+
+static struct socket *mptcp_socket_create_get(struct mptcp_sock *msk, int state)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *sk = (struct sock *)msk;
+	struct socket *ssock;
+	int err;
+
+	lock_sock(sk);
+	ssock = __mptcp_nmpc_socket(msk);
+	if (ssock)
+		goto set_state;
+
+	if (!__mptcp_can_create_subflow(msk)) {
+		ssock = ERR_PTR(-EINVAL);
+		goto release;
+	}
+
+	err = mptcp_subflow_create_socket(sk, &ssock);
+	if (err) {
+		ssock = ERR_PTR(err);
+		goto release;
+	}
+
+	msk->subflow = ssock;
+	subflow = mptcp_subflow_ctx(msk->subflow->sk);
+	subflow->request_mptcp = 1; /* @@ if MPTCP enabled */
+	subflow->request_version = 1; /* only v1 supported */
+
+set_state:
+	sock_hold(ssock->sk);
+	if (state != TCP_MAX_STATES)
+		inet_sk_state_store(sk, state);
+
+release:
+	release_sock(sk);
+	return ssock;
+}
+
 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
 {
 	if (!msk->cached_ext)
@@ -221,7 +272,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		return -EOPNOTSUPP;
 
 	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_tcp_fallback_get(msk);
 	if (ssock) {
 		release_sock(sk);
 		pr_debug("fallback passthrough");
@@ -324,7 +375,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	long timeo;
 
 	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_tcp_fallback_get(msk);
 	if (ssock) {
 		release_sock(sk);
 		pr_debug("fallback-read subflow=%p",
@@ -579,7 +630,11 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 	struct socket *listener;
 	struct sock *newsk;
 
-	listener = msk->subflow;
+	listener = __mptcp_nmpc_socket(msk);
+	if (WARN_ON_ONCE(!listener)) {
+		*err = -EINVAL;
+		return NULL;
+	}
 
 	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
 	newsk = inet_csk_accept(listener->sk, flags, err, kern);
@@ -665,8 +720,8 @@ static int mptcp_setsockopt(struct sock *sk, int level, int optname,
 	optval = (char __kernel __force *)uoptval;
 
 	pr_debug("msk=%p", msk);
-	ssock = mptcp_fallback_get_ref(msk);
-	if (ssock) {
+	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
+	if (!IS_ERR(ssock)) {
 		pr_debug("subflow=%p", ssock->sk);
 		ret = kernel_setsockopt(ssock, level, optname, optval, optlen);
 		sock_put(ssock->sk);
@@ -693,8 +748,8 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 	option = (int __kernel __force *)uoption;
 
 	pr_debug("msk=%p", msk);
-	ssock = mptcp_fallback_get_ref(msk);
-	if (ssock) {
+	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
+	if (!IS_ERR(ssock)) {
 		pr_debug("subflow=%p", ssock->sk);
 		ret = kernel_getsockopt(ssock, level, optname, optval, option);
 		sock_put(ssock->sk);
@@ -710,11 +765,14 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 static int mptcp_get_port(struct sock *sk, unsigned short snum)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct socket *ssock;
 
-	pr_debug("msk=%p, subflow=%p", msk,
-		 mptcp_subflow_ctx(msk->subflow->sk));
+	ssock = __mptcp_nmpc_socket(msk);
+	pr_debug("msk=%p, subflow=%p", msk, ssock);
+	if (WARN_ON_ONCE(!ssock))
+		return -EINVAL;
 
-	return inet_csk_get_port(msk->subflow->sk, snum);
+	return inet_csk_get_port(ssock->sk, snum);
 }
 
 void mptcp_finish_connect(struct sock *ssk)
@@ -811,43 +869,13 @@ static struct proto mptcp_prot = {
 	.no_autobind	= true,
 };
 
-static struct socket *mptcp_socket_create_get(struct mptcp_sock *msk)
-{
-	struct mptcp_subflow_context *subflow;
-	struct sock *sk = (struct sock *)msk;
-	struct socket *ssock;
-	int err;
-
-	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
-	if (ssock)
-		goto release;
-
-	err = mptcp_subflow_create_socket(sk, &ssock);
-	if (err) {
-		ssock = ERR_PTR(err);
-		goto release;
-	}
-
-	msk->subflow = ssock;
-	subflow = mptcp_subflow_ctx(msk->subflow->sk);
-	subflow->request_mptcp = 1; /* @@ if MPTCP enabled */
-	subflow->request_version = 1; /* only v1 supported */
-
-	sock_hold(ssock->sk);
-
-release:
-	release_sock(sk);
-	return ssock;
-}
-
 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
 	struct socket *ssock;
 	int err;
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 
@@ -863,7 +891,7 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 	struct socket *ssock;
 	int err;
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_SYN_SENT);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 
@@ -876,6 +904,10 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 #endif
 
 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
+
+	/* should we keep msk socket lock instead? and release it this
+	 * latter state update */
+	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
 	sock_put(ssock->sk);
 	return err;
 }
@@ -887,16 +919,16 @@ static int mptcp_getname(struct socket *sock, struct sockaddr *uaddr,
 	struct socket *ssock;
 	int ret;
 
-	if (msk->subflow) {
-		lock_sock(sock->sk);
-		ssock = __mptcp_fallback_get_ref(msk);
-		release_sock(sock->sk);
-		if (ssock) {
-			pr_debug("subflow=%p", ssock->sk);
-			ret = ssock->ops->getname(ssock, uaddr, peer);
-			sock_put(ssock->sk);
-			return ret;
-		}
+	lock_sock(sock->sk);
+	ssock = __mptcp_nmpc_socket(msk);
+	if (ssock)
+		sock_hold(ssock->sk);
+	release_sock(sock->sk);
+	if (ssock) {
+		pr_debug("subflow=%p", ssock->sk);
+		ret = ssock->ops->getname(ssock, uaddr, peer);
+		sock_put(ssock->sk);
+		return ret;
 	}
 
 	switch (af) {
@@ -962,11 +994,15 @@ static int mptcp_listen(struct socket *sock, int backlog)
 
 	pr_debug("msk=%p", msk);
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_LISTEN);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 
 	err = ssock->ops->listen(ssock, backlog);
+
+	/* should we keep msk socket lock instead? and release it this
+	 * latter state update */
+	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
 	sock_put(ssock->sk);
 	return err;
 }
@@ -985,13 +1021,20 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
 	struct socket *ssock;
-	int err;
+	int err = -EINVAL;
 
 	pr_debug("msk=%p", msk);
 
-	ssock = mptcp_fallback_get_ref(msk);
+	lock_sock(sock->sk);
+	if (sock->sk->sk_state != TCP_LISTEN)
+		goto unlock_fail;
+
+	ssock = __mptcp_nmpc_socket(msk);
 	if (!ssock)
-		return -EINVAL;
+		goto unlock_fail;
+
+	sock_hold(ssock->sk);
+	release_sock(sock->sk);
 
 	err = ssock->ops->accept(sock, newsock, flags, kern);
 	if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {
@@ -1011,6 +1054,10 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 
 	sock_put(ssock->sk);
 	return err;
+
+unlock_fail:
+	release_sock(sock->sk);
+	return -EINVAL;
 }
 
 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
@@ -1023,8 +1070,9 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 
 	msk = mptcp_sk(sk);
 	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_nmpc_socket(msk);
 	if (ssock) {
+		sock_hold(ssock->sk);
 		release_sock(sk);
 		mask = ssock->ops->poll(file, ssock, wait);
 		sock_put(ssock->sk);
@@ -1061,7 +1109,7 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	if (how == SHUT_WR || how == SHUT_RDWR)
 		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_tcp_fallback_get(msk);
 	if (ssock) {
 		release_sock(sock->sk);
 		pr_debug("subflow=%p", ssock->sk);
Signature · 1 line
2.21.0
#4 lore raw

[MPTCP] Re: [PATCH v1 2/4] imptcp: cleanup fallback handling

Quoted 166 lines · Paolo Abeni

The 'fallback' field has many different meanings:
tcp fallback socket, first subflow, listening socket. Enforcing the correct usage is not easy - currently we have several problems:

1) this will fail, but it should not:
fd = socket(AF_INET{,6}, SOCK_STREAM, MPTCP_PROTO); setsockopt(fd, SOL_IP{,V6}, //...) = -1 EOPNOTSUPP (Operation not supported)

2) this will report success, but it should not:
fd = socket(AF_INET{,6}, SOCK_STREAM, MPTCP_PROTO); connect(fd, //)
bind(fd, //) = 0

3) this will report success, but it should not:
fd = socket(AF_INET{,6}, SOCK_STREAM, MPTCP_PROTO); listen(fd, // )
s = accept(fd, )
bind(s, ...) = 0

This patch:

- explicitly handle the msk socket status in some missing places
- wrap msk->subflow access with helpers describing the expected semantic
- use the above to enforce correct syscall behavior
- create on demand the subflow socket for setsockopt(), if possible

RFC -> v1:
- drop __mptcp_first_socket() helper, always use __mptcp_nmpc_socket() instead

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
net/mptcp/protocol.c | 182 +++++++++++++++++++++++++++----------------

1 file changed, 115 insertions(+), 67 deletions(-)
net/mptcp/protocol.c +67 -12
index f0944a5b562d..2a78123a20f4 100644
@@ -22,25 +22,33 @@
 #include <net/mptcp.h>
 #include "protocol.h"
 
-static struct socket *__mptcp_fallback_get_ref(const struct mptcp_sock *msk)
+/* if msk has a single subflow socket, and the mp_capable handshake is not
+ * completed yet or has failed - that is, the socket is Not MP Capable,
+ * returns it.
+ * Otherwise returns NULL
+ */
+static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
 {
-	sock_owned_by_me((const struct sock *)msk);
-
 	if (!msk->subflow)
 		return NULL;
 
-	sock_hold(msk->subflow->sk);
 	return msk->subflow;
 }
 
-static struct socket *mptcp_fallback_get_ref(const struct mptcp_sock *msk)
+/* if msk has a single subflow, and the mp_capable handshake is failed,
+ * get a reference and return it.
+ * Otherwise returns NULL
+ */
+static struct socket *__mptcp_tcp_fallback_get(const struct mptcp_sock *msk)
 {
-	struct socket *ssock;
+	struct socket *ssock = __mptcp_nmpc_socket(msk);
 
-	lock_sock((struct sock *)msk);
-	ssock = __mptcp_fallback_get_ref(msk);
-	release_sock((struct sock *)msk);
+	sock_owned_by_me((const struct sock *)msk);
 
+	if (!ssock || tcp_sk(ssock->sk)->is_mptcp)
+		return NULL;
+
+	sock_hold(ssock->sk);
 	return ssock;
 }
 
@@ -57,6 +65,49 @@ static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)
 	return NULL;
 }
 
+static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
+{
+	return ((struct sock *)msk)->sk_state == TCP_CLOSE;
+}
+
+static struct socket *mptcp_socket_create_get(struct mptcp_sock *msk, int state)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *sk = (struct sock *)msk;
+	struct socket *ssock;
+	int err;
+
+	lock_sock(sk);
+	ssock = __mptcp_nmpc_socket(msk);
+	if (ssock)
+		goto set_state;
+
+	if (!__mptcp_can_create_subflow(msk)) {
+		ssock = ERR_PTR(-EINVAL);
+		goto release;
+	}
+
+	err = mptcp_subflow_create_socket(sk, &ssock);
+	if (err) {
+		ssock = ERR_PTR(err);
+		goto release;
+	}
+
+	msk->subflow = ssock;
+	subflow = mptcp_subflow_ctx(msk->subflow->sk);
+	subflow->request_mptcp = 1; /* @@ if MPTCP enabled */
+	subflow->request_version = 1; /* only v1 supported */
+
+set_state:
+	sock_hold(ssock->sk);
+	if (state != TCP_MAX_STATES)
+		inet_sk_state_store(sk, state);
+
+release:
+	release_sock(sk);
+	return ssock;
+}
+
 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
 {
 	if (!msk->cached_ext)
@@ -221,7 +272,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		return -EOPNOTSUPP;
 
 	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_tcp_fallback_get(msk);
 	if (ssock) {
 		release_sock(sk);
 		pr_debug("fallback passthrough");
@@ -324,7 +375,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	long timeo;
 
 	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_tcp_fallback_get(msk);
 	if (ssock) {
 		release_sock(sk);
 		pr_debug("fallback-read subflow=%p",
@@ -579,7 +630,11 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 	struct socket *listener;
 	struct sock *newsk;
 
-	listener = msk->subflow;
+	listener = __mptcp_nmpc_socket(msk);
+	if (WARN_ON_ONCE(!listener)) {
+		*err = -EINVAL;
+		return NULL;
+	}
 
 	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
 	newsk = inet_csk_accept(listener->sk, flags, err, kern);

net/mptcp/protocol.c static int mptcp_setsockopt(struct sock *sk, int level, int optname, · line 723

Quoted 6 lines
net/mptcp/protocol.c +1 -2
@@ -665,8 +720,8 @@ static int mptcp_setsockopt(struct sock *sk, int level, int optname,
 	optval = (char __kernel __force *)uoptval;
 
 	pr_debug("msk=%p", msk);
-	ssock = mptcp_fallback_get_ref(msk);
-	if (ssock) {
+	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);

I think it would be helpful to have something like

#define MPTCP_SAME_STATE TCP_MAX_STATES

and use that name when calling mptcp_socket_create_get() and leaving the sk_state unchanged.

Quoted 88 lines
net/mptcp/protocol.c +11 -37
+	if (!IS_ERR(ssock)) {
 		pr_debug("subflow=%p", ssock->sk);
 		ret = kernel_setsockopt(ssock, level, optname, optval, optlen);
 		sock_put(ssock->sk);
@@ -693,8 +748,8 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 	option = (int __kernel __force *)uoption;
 
 	pr_debug("msk=%p", msk);
-	ssock = mptcp_fallback_get_ref(msk);
-	if (ssock) {
+	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
+	if (!IS_ERR(ssock)) {
 		pr_debug("subflow=%p", ssock->sk);
 		ret = kernel_getsockopt(ssock, level, optname, optval, option);
 		sock_put(ssock->sk);
@@ -710,11 +765,14 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 static int mptcp_get_port(struct sock *sk, unsigned short snum)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct socket *ssock;
 
-	pr_debug("msk=%p, subflow=%p", msk,
-		 mptcp_subflow_ctx(msk->subflow->sk));
+	ssock = __mptcp_nmpc_socket(msk);
+	pr_debug("msk=%p, subflow=%p", msk, ssock);
+	if (WARN_ON_ONCE(!ssock))
+		return -EINVAL;
 
-	return inet_csk_get_port(msk->subflow->sk, snum);
+	return inet_csk_get_port(ssock->sk, snum);
 }
 
 void mptcp_finish_connect(struct sock *ssk)
@@ -811,43 +869,13 @@ static struct proto mptcp_prot = {
 	.no_autobind	= true,
 };
 
-static struct socket *mptcp_socket_create_get(struct mptcp_sock *msk)
-{
-	struct mptcp_subflow_context *subflow;
-	struct sock *sk = (struct sock *)msk;
-	struct socket *ssock;
-	int err;
-
-	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
-	if (ssock)
-		goto release;
-
-	err = mptcp_subflow_create_socket(sk, &ssock);
-	if (err) {
-		ssock = ERR_PTR(err);
-		goto release;
-	}
-
-	msk->subflow = ssock;
-	subflow = mptcp_subflow_ctx(msk->subflow->sk);
-	subflow->request_mptcp = 1; /* @@ if MPTCP enabled */
-	subflow->request_version = 1; /* only v1 supported */
-
-	sock_hold(ssock->sk);
-
-release:
-	release_sock(sk);
-	return ssock;
-}
-
 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
 	struct socket *ssock;
 	int err;
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 
@@ -863,7 +891,7 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 	struct socket *ssock;
 	int err;
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_SYN_SENT);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 

net/mptcp/protocol.c static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, · line 909

Quoted 6 lines
net/mptcp/protocol.c +3 -0
@@ -876,6 +904,10 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 #endif
 
 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
+
+	/* should we keep msk socket lock instead? and release it this
+	 * latter state update */

I do think it would be better to keep the lock for this state manipulation. This locking gets changed in patch 4, so if this is all going to get squashed anyway then I guess it doesn't matter much.

Quoted 34 lines
net/mptcp/protocol.c +11 -10
+	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
 	sock_put(ssock->sk);
 	return err;
 }
@@ -887,16 +919,16 @@ static int mptcp_getname(struct socket *sock, struct sockaddr *uaddr,
 	struct socket *ssock;
 	int ret;
 
-	if (msk->subflow) {
-		lock_sock(sock->sk);
-		ssock = __mptcp_fallback_get_ref(msk);
-		release_sock(sock->sk);
-		if (ssock) {
-			pr_debug("subflow=%p", ssock->sk);
-			ret = ssock->ops->getname(ssock, uaddr, peer);
-			sock_put(ssock->sk);
-			return ret;
-		}
+	lock_sock(sock->sk);
+	ssock = __mptcp_nmpc_socket(msk);
+	if (ssock)
+		sock_hold(ssock->sk);
+	release_sock(sock->sk);
+	if (ssock) {
+		pr_debug("subflow=%p", ssock->sk);
+		ret = ssock->ops->getname(ssock, uaddr, peer);
+		sock_put(ssock->sk);
+		return ret;
 	}
 
 	switch (af) {
@@ -962,11 +994,15 @@ static int mptcp_listen(struct socket *sock, int backlog)
 
 	pr_debug("msk=%p", msk);

net/mptcp/protocol.c static int mptcp_listen(struct socket *sock, int backlog) · line 1004

Quoted 10 lines
net/mptcp/protocol.c +4 -1
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_LISTEN);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 
 	err = ssock->ops->listen(ssock, backlog);
+
+	/* should we keep msk socket lock instead? and release it this
+	 * latter state update */

Same locking comment as mptcp_stream_connect above, and same caveat concerning patch 4 and squashing.

Other than these two functions, locking looks ok.

Mat

Quoted 61 lines
net/mptcp/protocol.c +18 -5
+	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
 	sock_put(ssock->sk);
 	return err;
 }
@@ -985,13 +1021,20 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
 	struct socket *ssock;
-	int err;
+	int err = -EINVAL;
 
 	pr_debug("msk=%p", msk);
 
-	ssock = mptcp_fallback_get_ref(msk);
+	lock_sock(sock->sk);
+	if (sock->sk->sk_state != TCP_LISTEN)
+		goto unlock_fail;
+
+	ssock = __mptcp_nmpc_socket(msk);
 	if (!ssock)
-		return -EINVAL;
+		goto unlock_fail;
+
+	sock_hold(ssock->sk);
+	release_sock(sock->sk);
 
 	err = ssock->ops->accept(sock, newsock, flags, kern);
 	if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {
@@ -1011,6 +1054,10 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 
 	sock_put(ssock->sk);
 	return err;
+
+unlock_fail:
+	release_sock(sock->sk);
+	return -EINVAL;
 }
 
 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
@@ -1023,8 +1070,9 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 
 	msk = mptcp_sk(sk);
 	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_nmpc_socket(msk);
 	if (ssock) {
+		sock_hold(ssock->sk);
 		release_sock(sk);
 		mask = ssock->ops->poll(file, ssock, wait);
 		sock_put(ssock->sk);
@@ -1061,7 +1109,7 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	if (how == SHUT_WR || how == SHUT_RDWR)
 		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_tcp_fallback_get(msk);
 	if (ssock) {
 		release_sock(sock->sk);
 		pr_debug("subflow=%p", ssock->sk);
Signature · 1 line
2.21.0

--
Mat Martineau
Intel

#5 lore raw

[MPTCP] Re: [PATCH v1 2/4] imptcp: cleanup fallback handling

Quoted 1 line · Mat Martineau

On Tue, 10 Dec 2019, Paolo Abeni wrote:

...

Quoted 52 lines
Quoted 5 lines
net/mptcp/protocol.c +0 -0
@@ -962,11 +994,15 @@ static int mptcp_listen(struct socket *sock, int 
 backlog)
 
 	pr_debug("msk=%p", msk);

net/mptcp/protocol.c static int mptcp_listen(struct socket *sock, int · line 1005

Quoted 10 lines
net/mptcp/protocol.c +4 -1
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_LISTEN);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 
 	err = ssock->ops->listen(ssock, backlog);
+
+	/* should we keep msk socket lock instead? and release it this
+	 * latter state update */

Same locking comment as mptcp_stream_connect above, and same caveat concerning patch 4 and squashing.

Other than these two functions, locking looks ok.

Mat

Quoted 27 lines
net/mptcp/protocol.c +1 -0
+	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
 	sock_put(ssock->sk);
 	return err;

}

net/mptcp/protocol.c +10 -3
@@ -985,13 +1021,20 @@ static int mptcp_stream_accept(struct socket *sock, 
 struct socket *newsock,
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
 	struct socket *ssock;
-	int err;
+	int err = -EINVAL;
 
 	pr_debug("msk=%p", msk);
 
-	ssock = mptcp_fallback_get_ref(msk);
+	lock_sock(sock->sk);
+	if (sock->sk->sk_state != TCP_LISTEN)
+		goto unlock_fail;
+
+	ssock = __mptcp_nmpc_socket(msk);
 	if (!ssock)
-		return -EINVAL;
+		goto unlock_fail;
+
+	sock_hold(ssock->sk);
+	release_sock(sock->sk);
 
Quoted 2 lines
Quoted 2 lines
net/mptcp/protocol.c +0 -0
 	err = ssock->ops->accept(sock, newsock, flags, kern);
	if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {

I did just say "locking looks ok", but just below this is some iteration over msk->conn_list without the msk lock held. The locking there is unaffected by your patch, but this might be a good chance to take a look at the locking around that list iteration!

Mat

Quoted 29 lines
Quoted 29 lines
net/mptcp/protocol.c +4 -0
@@ -1011,6 +1054,10 @@ static int mptcp_stream_accept(struct socket *sock, 
 struct socket *newsock,
 
 	sock_put(ssock->sk);
 	return err;
+
+unlock_fail:
+	release_sock(sock->sk);
+	return -EINVAL;
 }
 

static __poll_t mptcp_poll(struct file *file, struct socket *sock,

net/mptcp/protocol.c +2 -1
@@ -1023,8 +1070,9 @@ static __poll_t mptcp_poll(struct file *file, struct 
 socket *sock,
 
 	msk = mptcp_sk(sk);
 	lock_sock(sk);
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_nmpc_socket(msk);
 	if (ssock) {
+		sock_hold(ssock->sk);
 		release_sock(sk);
 		mask = ssock->ops->poll(file, ssock, wait);
		sock_put(ssock->sk);
net/mptcp/protocol.c +0 -0
@@ -1061,7 +1109,7 @@ static int mptcp_shutdown(struct socket *sock, int 
 how)
 	if (how == SHUT_WR || how == SHUT_RDWR)
 		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
Quoted 15 lines
Quoted 7 lines
net/mptcp/protocol.c +1 -1
-	ssock = __mptcp_fallback_get_ref(msk);
+	ssock = __mptcp_tcp_fallback_get(msk);
 	if (ssock) {
 		release_sock(sock->sk);
		pr_debug("subflow=%p", ssock->sk);
Signature · 1 line
2.21.0

--
Mat Martineau
Intel
_______________________________________________
mptcp mailing list -- mptcp(a)lists.01.org
To unsubscribe send an email to mptcp-leave(a)lists.01.org

--
Mat Martineau
Intel

author
#6 lore raw

[MPTCP] Re: [PATCH v1 2/4] imptcp: cleanup fallback handling

Quoted 56 lines · Mat Martineau
Quoted 1 line · Mat Martineau

On Tue, 10 Dec 2019, Paolo Abeni wrote:

...

Quoted 51 lines
Quoted 4 lines
net/mptcp/protocol.c +0 -0
@@ -962,11 +994,15 @@ static int mptcp_listen(struct socket *sock, int 
 backlog)
 
 	pr_debug("msk=%p", msk);

net/mptcp/protocol.c static int mptcp_listen(struct socket *sock, int · line 1005

Quoted 10 lines
net/mptcp/protocol.c +4 -1
 
-	ssock = mptcp_socket_create_get(msk);
+	ssock = mptcp_socket_create_get(msk, TCP_LISTEN);
 	if (IS_ERR(ssock))
 		return PTR_ERR(ssock);
 
 	err = ssock->ops->listen(ssock, backlog);
+
+	/* should we keep msk socket lock instead? and release it this
+	 * latter state update */

Same locking comment as mptcp_stream_connect above, and same caveat concerning patch 4 and squashing.

Other than these two functions, locking looks ok.

Mat

Quoted 27 lines
net/mptcp/protocol.c +1 -0
+	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
 	sock_put(ssock->sk);
 	return err;

}

net/mptcp/protocol.c +10 -3
@@ -985,13 +1021,20 @@ static int mptcp_stream_accept(struct socket *sock, 
 struct socket *newsock,
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
 	struct socket *ssock;
-	int err;
+	int err = -EINVAL;
 
 	pr_debug("msk=%p", msk);
 
-	ssock = mptcp_fallback_get_ref(msk);
+	lock_sock(sock->sk);
+	if (sock->sk->sk_state != TCP_LISTEN)
+		goto unlock_fail;
+
+	ssock = __mptcp_nmpc_socket(msk);
 	if (!ssock)
-		return -EINVAL;
+		goto unlock_fail;
+
+	sock_hold(ssock->sk);
+	release_sock(sock->sk);
 
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
net/mptcp/protocol.c +0 -0
 	err = ssock->ops->accept(sock, newsock, flags, kern);
	if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {

I did just say "locking looks ok", but just below this is some iteration over msk->conn_list without the msk lock held. The locking there is unaffected by your patch, but this might be a good chance to take a look at the locking around that list iteration!

Thank you for the feedback! I'll cope in the next iteration.

The lockless access to msk->conn_list requires a bit more changes, I'm unsure I can deal with that in a timely manner. I'll try.

Idea would be dropping the intermendiate inet_accept() call and open- code part of it, so we don't have to acquire and release the locks multiple times.

Would it fit you if the above lands in a later patch?

Thanks!

Paolo

#7 lore raw

[MPTCP] Re: [PATCH v1 2/4] imptcp: cleanup fallback handling

Quoted 6 lines · Paolo Abeni
Quoted 4 lines · Mat Martineau

I did just say "locking looks ok", but just below this is some iteration over msk->conn_list without the msk lock held. The locking there is unaffected by your patch, but this might be a good chance to take a look at the locking around that list iteration!

Quoted 8 lines

Thank you for the feedback! I'll cope in the next iteration.

The lockless access to msk->conn_list requires a bit more changes, I'm unsure I can deal with that in a timely manner. I'll try.

Idea would be dropping the intermendiate inet_accept() call and open- code part of it, so we don't have to acquire and release the locks multiple times.

What about delaying

inet_sk_state_store(newsk, TCP_ESTABLISHED);

until after this unlocked list iteration?

This would/should prevent any concurrent joins and thus any list modifications.

author
#8 lore raw

[MPTCP] Re: [PATCH v1 2/4] imptcp: cleanup fallback handling

Quoted 16 lines · Florian Westphal
Quoted 6 lines · Paolo Abeni
Quoted 4 lines · Mat Martineau

I did just say "locking looks ok", but just below this is some iteration over msk->conn_list without the msk lock held. The locking there is unaffected by your patch, but this might be a good chance to take a look at the locking around that list iteration!

Quoted 8 lines

Thank you for the feedback! I'll cope in the next iteration.

The lockless access to msk->conn_list requires a bit more changes, I'm unsure I can deal with that in a timely manner. I'll try.

Idea would be dropping the intermendiate inet_accept() call and open- code part of it, so we don't have to acquire and release the locks multiple times.

Quoted 8 lines

What about delaying

inet_sk_state_store(newsk, TCP_ESTABLISHED);

until after this unlocked list iteration?

This would/should prevent any concurrent joins and thus any list modifications.

Agreed, code will be much simpler.

Thanks,

Paolo

author
#9 lore raw

PATCH 3/4 mptcp: add subflow to conn_list early

In reply to Paolo Abeni

After the previous commit we can move the subflow into conn_list at creation time, using the msk 'can_ack' field to track the fallaback status of such subflow.

Note that we still need to keep the subflow field around, as we can't easily access the non MP capable socket otherwise.

RFC -> v1:
- keep 'subflow' field around, after mptcp_subflow_tcp_sock() conversion we need some way to track such socket around

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
net/mptcp/protocol.c +3 -10
index 2a78123a20f4..a11352e5e322 100644
@@ -29,7 +29,7 @@
  */
 static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
 {
-	if (!msk->subflow)
+	if (!msk->subflow || READ_ONCE(msk->can_ack))
 		return NULL;
 
 	return msk->subflow;
@@ -94,7 +94,8 @@ static struct socket *mptcp_socket_create_get(struct mptcp_sock *msk, int state)
 	}
 
 	msk->subflow = ssock;
-	subflow = mptcp_subflow_ctx(msk->subflow->sk);
+	subflow = mptcp_subflow_ctx(ssock->sk);
+	list_add(&subflow->node, &msk->conn_list);
 	subflow->request_mptcp = 1; /* @@ if MPTCP enabled */
 	subflow->request_version = 1; /* only v1 supported */
 
@@ -582,11 +583,6 @@ static void mptcp_close(struct sock *sk, long timeout)
 
 	lock_sock(sk);
 
-	if (msk->subflow) {
-		sock_release(msk->subflow);
-		msk->subflow = NULL;
-	}
-
 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
 
@@ -669,7 +665,6 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		msk->token = subflow->token;
 
 		mptcp_token_update_accept(newsk, new_mptcp_sock);
-		msk->subflow = NULL;
 
 		msk->write_seq = subflow->idsn + 1;
 		if (subflow->can_ack) {
@@ -825,8 +820,6 @@ void mptcp_finish_connect(struct sock *ssk)
 		subflow->map_seq = ack_seq;
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
-		list_add(&subflow->node, &msk->conn_list);
-		msk->subflow = NULL;
 		bh_unlock_sock(sk);
 		local_bh_enable();
 	}
Signature · 1 line
2.21.0
author
#10 lore raw

PATCH 4/4 mptcp: avoid acquiring the msk lock in mptcp_finisch_connect()

In reply to Paolo Abeni

Move the mptcp_copy_inaddrs() into mptcp_finish_connect(), so that the mks lock is no longer required. Just document the unlocked access with WRITE_ONCE().

Since we now always acquire msk -> ssk lock in order, we can also avoid the msk lock/subflow lookup()/get()/ msk unlock()/ subflow op/ subflow put dance in most ops - simply keep the msk lock held the whole time.

Additionally we can simplify mptcp_getname(), as now the address information are always available in the msk.

RFC -> v1:
- move mptcp_copy_inaddrs() into finish_connect()

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 211 +++++++++++++++++--------------------------
 1 file changed, 83 insertions(+), 128 deletions(-)
net/mptcp/protocol.c +83 -128
index a11352e5e322..70090366adae 100644
@@ -36,10 +36,10 @@ static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
 }
 
 /* if msk has a single subflow, and the mp_capable handshake is failed,
- * get a reference and return it.
+ * return it.
  * Otherwise returns NULL
  */
-static struct socket *__mptcp_tcp_fallback_get(const struct mptcp_sock *msk)
+static struct socket *__mptcp_tcp_fallback(const struct mptcp_sock *msk)
 {
 	struct socket *ssock = __mptcp_nmpc_socket(msk);
 
@@ -48,7 +48,6 @@ static struct socket *__mptcp_tcp_fallback_get(const struct mptcp_sock *msk)
 	if (!ssock || tcp_sk(ssock->sk)->is_mptcp)
 		return NULL;
 
-	sock_hold(ssock->sk);
 	return ssock;
 }
 
@@ -70,28 +69,23 @@ static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
 	return ((struct sock *)msk)->sk_state == TCP_CLOSE;
 }
 
-static struct socket *mptcp_socket_create_get(struct mptcp_sock *msk, int state)
+static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
 {
 	struct mptcp_subflow_context *subflow;
 	struct sock *sk = (struct sock *)msk;
 	struct socket *ssock;
 	int err;
 
-	lock_sock(sk);
 	ssock = __mptcp_nmpc_socket(msk);
 	if (ssock)
 		goto set_state;
 
-	if (!__mptcp_can_create_subflow(msk)) {
-		ssock = ERR_PTR(-EINVAL);
-		goto release;
-	}
+	if (!__mptcp_can_create_subflow(msk))
+		return ERR_PTR(-EINVAL);
 
 	err = mptcp_subflow_create_socket(sk, &ssock);
-	if (err) {
-		ssock = ERR_PTR(err);
-		goto release;
-	}
+	if (err)
+		return ERR_PTR(err);
 
 	msk->subflow = ssock;
 	subflow = mptcp_subflow_ctx(ssock->sk);
@@ -100,12 +94,8 @@ static struct socket *mptcp_socket_create_get(struct mptcp_sock *msk, int state)
 	subflow->request_version = 1; /* only v1 supported */
 
 set_state:
-	sock_hold(ssock->sk);
 	if (state != TCP_MAX_STATES)
 		inet_sk_state_store(sk, state);
-
-release:
-	release_sock(sk);
 	return ssock;
 }
 
@@ -273,12 +263,11 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		return -EOPNOTSUPP;
 
 	lock_sock(sk);
-	ssock = __mptcp_tcp_fallback_get(msk);
+	ssock = __mptcp_tcp_fallback(msk);
 	if (ssock) {
-		release_sock(sk);
 		pr_debug("fallback passthrough");
 		ret = sock_sendmsg(ssock, msg);
-		sock_put(ssock->sk);
+		release_sock(sk);
 		return ret;
 	}
 
@@ -376,13 +365,12 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	long timeo;
 
 	lock_sock(sk);
-	ssock = __mptcp_tcp_fallback_get(msk);
+	ssock = __mptcp_tcp_fallback(msk);
 	if (ssock) {
-		release_sock(sk);
 		pr_debug("fallback-read subflow=%p",
 			 mptcp_subflow_ctx(ssock->sk));
 		copied = sock_recvmsg(ssock, msg, flags);
-		sock_put(ssock->sk);
+		release_sock(sk);
 		return copied;
 	}
 
@@ -708,25 +696,26 @@ static int mptcp_setsockopt(struct sock *sk, int level, int optname,
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	char __kernel *optval;
+	int ret = -EOPNOTSUPP;
 	struct socket *ssock;
-	int ret;
 
 	/* will be treated as __user in tcp_setsockopt */
 	optval = (char __kernel __force *)uoptval;
 
 	pr_debug("msk=%p", msk);
-	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
+
+	/* @@ the meaning of setsockopt() when the socket is connected and
+	 * there are multiple subflows is not defined.
+	 */
+	lock_sock(sk);
+	ssock = __mptcp_socket_create(msk, TCP_MAX_STATES);
 	if (!IS_ERR(ssock)) {
 		pr_debug("subflow=%p", ssock->sk);
 		ret = kernel_setsockopt(ssock, level, optname, optval, optlen);
-		sock_put(ssock->sk);
-		return ret;
 	}
+	release_sock(sk);
 
-	/* @@ the meaning of setsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
-	 */
-	return -EOPNOTSUPP;
+	return ret;
 }
 
 static int mptcp_getsockopt(struct sock *sk, int level, int optname,
@@ -734,27 +723,28 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	char __kernel *optval;
+	int ret = -EOPNOTSUPP;
 	int __kernel *option;
 	struct socket *ssock;
-	int ret;
 
 	/* will be treated as __user in tcp_getsockopt */
 	optval = (char __kernel __force *)uoptval;
 	option = (int __kernel __force *)uoption;
 
 	pr_debug("msk=%p", msk);
-	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
+	lock_sock(sk);
+
+	/* @@ the meaning of getsockopt() when the socket is connected and
+	 * there are multiple subflows is not defined.
+	 */
+	ssock = __mptcp_socket_create(msk, TCP_MAX_STATES);
 	if (!IS_ERR(ssock)) {
 		pr_debug("subflow=%p", ssock->sk);
 		ret = kernel_getsockopt(ssock, level, optname, optval, option);
-		sock_put(ssock->sk);
-		return ret;
 	}
+	release_sock(sk);
 
-	/* @@ the meaning of getsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
-	 */
-	return -EOPNOTSUPP;
+	return ret;
 }
 
 static int mptcp_get_port(struct sock *sk, unsigned short snum)
@@ -773,57 +763,34 @@ static int mptcp_get_port(struct sock *sk, unsigned short snum)
 void mptcp_finish_connect(struct sock *ssk)
 {
 	struct mptcp_subflow_context *subflow;
+	struct mptcp_sock *msk;
 	struct sock *sk;
+	u64 ack_seq;
 
 	subflow = mptcp_subflow_ctx(ssk);
-	sk = subflow->conn;
-
-	if (subflow->mp_capable) {
-		struct mptcp_sock *msk = mptcp_sk(sk);
-		u64 ack_seq;
+	if (!subflow->mp_capable)
+		return;
 
-		/* sk (new subflow socket) is already locked, but we need
-		 * to lock the parent (mptcp) socket now to add the tcp socket
-		 * to the subflow list.
-		 *
-		 * From lockdep point of view, this creates an ABBA type
-		 * deadlock: Normally (sendmsg, recvmsg, ..), we lock the mptcp
-		 * socket, then acquire a subflow lock.
-		 * Here we do the reverse: "subflow lock, then mptcp lock".
-		 *
-		 * Its alright to do this here, because this subflow is not yet
-		 * on the mptcp sockets subflow list.
-		 *
-		 * IOW, if another CPU has this mptcp socket locked, it cannot
-		 * acquire this particular subflow, because subflow->sk isn't
-		 * on msk->conn_list.
-		 *
-		 * This function can be called either from backlog processing
-		 * (BH will be enabled) or from softirq, so we need to use BH
-		 * locking scheme.
-		 */
-		local_bh_disable();
-		bh_lock_sock_nested(sk);
+	sk = subflow->conn;
+	msk = mptcp_sk(sk);
 
-		msk->remote_key = subflow->remote_key;
-		msk->local_key = subflow->local_key;
-		msk->token = subflow->token;
-		mptcp_copy_inaddrs(sk, ssk);
+	pr_debug("msk=%p, token=%u", sk, subflow->token);
 
-		pr_debug("msk=%p, token=%u", msk, msk->token);
+	mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
+	ack_seq++;
+	subflow->map_seq = ack_seq;
+	subflow->map_subflow_seq = 1;
+	subflow->rel_write_seq = 1;
 
-		mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
-		msk->write_seq = subflow->idsn + 1;
-		ack_seq++;
-		msk->ack_seq = ack_seq;
-		msk->can_ack = 1;
-		subflow->map_seq = ack_seq;
-		subflow->map_subflow_seq = 1;
-		subflow->rel_write_seq = 1;
-		bh_unlock_sock(sk);
-		local_bh_enable();
-	}
-	inet_sk_state_store(sk, TCP_ESTABLISHED);
+	/* the socket is not connected yet, no msk/subflow ops can access/race
+	 * accessing the field below
+	 */
+	WRITE_ONCE(msk->remote_key, subflow->remote_key);
+	WRITE_ONCE(msk->local_key, subflow->local_key);
+	WRITE_ONCE(msk->token, subflow->token);
+	WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
+	WRITE_ONCE(msk->ack_seq, ack_seq);
+	WRITE_ONCE(msk->can_ack, 1);
 }
 
 static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
@@ -868,12 +835,19 @@ static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 	struct socket *ssock;
 	int err;
 
-	ssock = mptcp_socket_create_get(msk, TCP_MAX_STATES);
-	if (IS_ERR(ssock))
-		return PTR_ERR(ssock);
+	lock_sock(sock->sk);
+	ssock = __mptcp_socket_create(msk, TCP_MAX_STATES);
+	if (IS_ERR(ssock)) {
+		err = PTR_ERR(ssock);
+		goto unlock;
+	}
 
 	err = ssock->ops->bind(ssock, uaddr, addr_len);
-	sock_put(ssock->sk);
+	if (!err)
+		mptcp_copy_inaddrs(sock->sk, ssock->sk);
+
+unlock:
+	release_sock(sock->sk);
 	return err;
 }
 
@@ -884,9 +858,12 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 	struct socket *ssock;
 	int err;
 
-	ssock = mptcp_socket_create_get(msk, TCP_SYN_SENT);
-	if (IS_ERR(ssock))
-		return PTR_ERR(ssock);
+	lock_sock(sock->sk);
+	ssock = __mptcp_socket_create(msk, TCP_SYN_SENT);
+	if (IS_ERR(ssock)) {
+		err = PTR_ERR(ssock);
+		goto unlock;
+	}
 
 #ifdef CONFIG_TCP_MD5SIG
 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
@@ -897,33 +874,19 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 #endif
 
 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
-
-	/* should we keep msk socket lock instead? and release it this
-	 * latter state update */
 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
-	sock_put(ssock->sk);
+	mptcp_copy_inaddrs(sock->sk, ssock->sk);
+
+unlock:
+	release_sock(sock->sk);
 	return err;
 }
 
 static int mptcp_getname(struct socket *sock, struct sockaddr *uaddr,
 			 int peer, int af)
 {
-	struct mptcp_sock *msk = mptcp_sk(sock->sk);
-	struct socket *ssock;
 	int ret;
 
-	lock_sock(sock->sk);
-	ssock = __mptcp_nmpc_socket(msk);
-	if (ssock)
-		sock_hold(ssock->sk);
-	release_sock(sock->sk);
-	if (ssock) {
-		pr_debug("subflow=%p", ssock->sk);
-		ret = ssock->ops->getname(ssock, uaddr, peer);
-		sock_put(ssock->sk);
-		return ret;
-	}
-
 	switch (af) {
 	case AF_INET:
 		ret = inet_getname(sock, uaddr, peer);
@@ -987,16 +950,20 @@ static int mptcp_listen(struct socket *sock, int backlog)
 
 	pr_debug("msk=%p", msk);
 
-	ssock = mptcp_socket_create_get(msk, TCP_LISTEN);
-	if (IS_ERR(ssock))
-		return PTR_ERR(ssock);
+	lock_sock(sock->sk);
+	ssock = __mptcp_socket_create(msk, TCP_LISTEN);
+	if (IS_ERR(ssock)) {
+		err = PTR_ERR(ssock);
+		goto unlock;
+	}
 
 	err = ssock->ops->listen(ssock, backlog);
-
-	/* should we keep msk socket lock instead? and release it this
-	 * latter state update */
 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
-	sock_put(ssock->sk);
+	if (!err)
+		mptcp_copy_inaddrs(sock->sk, ssock->sk);
+
+unlock:
+	release_sock(sock->sk);
 	return err;
 }
 
@@ -1065,10 +1032,8 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 	lock_sock(sk);
 	ssock = __mptcp_nmpc_socket(msk);
 	if (ssock) {
-		sock_hold(ssock->sk);
-		release_sock(sk);
 		mask = ssock->ops->poll(file, ssock, wait);
-		sock_put(ssock->sk);
+		release_sock(sk);
 		return mask;
 	}
 
@@ -1092,7 +1057,6 @@ static int mptcp_shutdown(struct socket *sock, int how)
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
 	struct mptcp_subflow_context *subflow;
-	struct socket *ssock;
 	int ret = 0;
 
 	pr_debug("sk=%p, how=%d", msk, how);
@@ -1102,15 +1066,6 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	if (how == SHUT_WR || how == SHUT_RDWR)
 		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
-	ssock = __mptcp_tcp_fallback_get(msk);
-	if (ssock) {
-		release_sock(sock->sk);
-		pr_debug("subflow=%p", ssock->sk);
-		ret = kernel_sock_shutdown(ssock, how);
-		sock_put(ssock->sk);
-		return ret;
-	}
-
 	how++;
 
 	if ((how & ~SHUTDOWN_MASK) || !how) {
Signature · 1 line
2.21.0
#11 lore raw

[MPTCP] Re: [PATCH v1 4/4] mptcp: avoid acquiring the msk lock in mptcp_finisch_connect()

Quoted 11 lines · Paolo Abeni

Move the mptcp_copy_inaddrs() into mptcp_finish_connect(), so that the mks lock is no longer required. Just document the unlocked access with WRITE_ONCE().

Since we now always acquire msk -> ssk lock in order, we can also avoid the msk lock/subflow lookup()/get()/ msk unlock()/ subflow op/ subflow put dance in most ops - simply keep the msk lock held the whole time.

Additionally we can simplify mptcp_getname(), as now the address information are always available in the msk.

Quoted 8 lines

RFC -> v1:
- move mptcp_copy_inaddrs() into finish_connect()

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
net/mptcp/protocol.c | 211 +++++++++++++++++--------------------------

1 file changed, 83 insertions(+), 128 deletions(-)

Code changes look good to me - it does clean things up nicely to keep the msk lock in those operations.

Thanks,

--
Mat Martineau
Intel