Skip to contentSkip to conversation

mptcp: plug mptcp_accept memory leaks

PATCH Superseded Florian Westphal 2 participants 6 messages May 14, 2019 – May 16, 2019

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

v2 cover mptcp: plug mptcp_accept memory leaks

v2:
- rename helper in patch 2
- add back crucial mptcp_init_sock() after clone in patch 3. This was lost during rebase and broke the tests but i forgot to refresh this patch when i sent v1.

v1 cover letter:

This series resolves the memory leaks that occur when running mptcp_connect.sh.

I don't mind if they get squashed.
I've decided to split this into 3 distinct changes to make it a bit clearer what is going on.

Second patch is not a bug fix, i can resend and drop it if the consensus is to continue using the sk->sk_socket back reference.

Third patch is a bit large due to the change in how the new mptcp socket is created.

See the commit messages for more details.
After these changes kmemleak doesn't report any leaks when the test script has completed.

author
#1 lore raw

PATCH v2 1/3 mptcp: release mptcp socket in mptcp_close

kmemleak reports several stray objects.
This fixes following splats:

unreferenced object 0xffff88810e27f240 (size 2016):

    comm "mptcp_connect", pid 2187, jiffies 4295218907 (age 1371.080s)
    hex dump (first 32 bytes):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
      02 00 01 41 00 00 00 00 00 00 00 00 00 00 00 00  ...A............
    backtrace:
      [<00000000298ae054>] sk_alloc+0x4c/0x600
      [<000000001cd7d411>] inet_create+0x1f4/0x6a0
      [<00000000ac4d1289>] __sock_create+0x1b4/0x3c0
      [<00000000cf2663ed>] __sys_socket+0xb6/0x170
      [<00000000b8efb3a9>] __x64_sys_socket+0x3d/0x50
      [<000000006e83b432>] do_syscall_64+0x72/0x270
      [<0000000007655965>] entry_SYSCALL_64_after_hwframe+0x49/0xbe
      [<0000000073c2278e>] 0xffffffffffffffff

This is because mptcp_close forgot to put the last reference.

We also need to set the state to CLOSED and orphan the socket, else the put will trigger warning about 'alive' inet socket being torn down.

Signed-off-by Florian Westphal <fw(a)strlen.de>

---
v2: no changes.

 net/mptcp/protocol.c | 5 +++++
 1 file changed, 5 insertions(+)
net/mptcp/protocol.c +5 -0
index 5f7ba68ebbbe..ae65b5b6a1dd 100644
@@ -578,6 +578,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct socket *ssk = NULL;
 	struct list_head list;
 
+	inet_sk_state_store(sk, TCP_CLOSE);
+
 	spin_lock_bh(&msk->conn_list_lock);
 	if (msk->subflow) {
 		ssk = msk->subflow;
@@ -601,6 +603,9 @@ static void mptcp_close(struct sock *sk, long timeout)
 		pr_debug("conn_list->subflow=%p", subflow);
 		sock_release(sock_sk(subflow)->sk_socket);
 	};
+
+	sock_orphan(sk);
+	sock_put(sk);
 }
 
 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH v2 2/3 mptcp: store tcp socket, not tcp sock in subflow ctx

In reply to Florian Westphal

Instead of using sk->sk_socket to get back the socket reference, store the 'socket' pointer that is coming from kernel_accept().

This doesn't fix a bug, but it appears cleaner to me.

v2: rename helper (Peter)

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 19 ++++++++++---------
 net/mptcp/protocol.h |  6 +++---
 net/mptcp/subflow.c  | 10 +++++++---
 3 files changed, 20 insertions(+), 15 deletions(-)
net/mptcp/protocol.c +10 -9
index ae65b5b6a1dd..e738bfad7430 100644
@@ -27,7 +27,7 @@ static inline bool before64(__u64 seq1, __u64 seq2)
 
 static bool mptcp_subflow_hold(struct subflow_context *subflow)
 {
-	struct sock *sk = sock_sk(subflow);
+	struct sock *sk = mptcp_subflow_tcp_socket(subflow)->sk;
 
 	return refcount_inc_not_zero(&sk->sk_refcnt);
 }
@@ -40,7 +40,7 @@ static struct sock *mptcp_subflow_get_ref(const struct mptcp_sock *msk)
 	mptcp_for_each_subflow(msk, subflow) {
 		if (mptcp_subflow_hold(subflow)) {
 			rcu_read_unlock();
-			return sock_sk(subflow);
+			return mptcp_subflow_tcp_socket(subflow)->sk;
 		}
 	}
 
@@ -232,7 +232,7 @@ static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
 
 static u64 get_mapped_dsn(struct subflow_context *subflow)
 {
-	u32 map_offset = (tcp_sk(sock_sk(subflow))->copied_seq -
+	u32 map_offset = (tcp_sk(mptcp_subflow_tcp_socket(subflow)->sk)->copied_seq -
 			  subflow->ssn_offset -
 			  subflow->map_subflow_seq);
 
@@ -601,8 +601,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 
 	list_for_each_entry_safe(subflow, tmp, &list, node) {
 		pr_debug("conn_list->subflow=%p", subflow);
-		sock_release(sock_sk(subflow)->sk_socket);
-	};
+		sock_release(mptcp_subflow_tcp_socket(subflow));
+	}
 
 	sock_orphan(sk);
 	sock_put(sk);
@@ -656,6 +656,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
 		subflow->conn = new_mptcp_sock->sk;
+		subflow->tcp_sock = new_sock;
 	} else {
 		msk->subflow = new_sock;
 	}
@@ -934,8 +935,8 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 		if (!tmp)
 			break;
 
-		ret |= tcp_poll(file, sock_sk(tmp)->sk_socket, wait);
-		sock_put(sock_sk(tmp));
+		ret |= tcp_poll(file, mptcp_subflow_tcp_socket(tmp), wait);
+		sock_put(mptcp_subflow_tcp_socket(tmp)->sk);
 	}
 
 	return ret;
@@ -964,9 +965,9 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	lock_sock(sock->sk);
 	rcu_read_lock();
 	list_for_each_entry_rcu(subflow, &msk->conn_list, node) {
-		rcu_read_unlock();
 		pr_debug("conn_list->subflow=%p", subflow);
-		ret = kernel_sock_shutdown(sock_sk(subflow)->sk_socket, how);
+		rcu_read_unlock();
+		ret = kernel_sock_shutdown(mptcp_subflow_tcp_socket(subflow), how);
 		rcu_read_lock();
 	}
 	rcu_read_unlock();
net/mptcp/protocol.h +3 -3
index de1131c543d0..3dfe15f42058 100644
@@ -90,7 +90,7 @@ struct subflow_context {
 		map_valid : 1,
 		use_checksum : 1;
 
-	struct  sock *sk;       /* underlying tcp_sock */
+	struct  socket *tcp_sock;/* underlying tcp_sock */
 	struct  sock *conn;     /* parent mptcp_sock */
 	void	(*tcp_sk_data_ready)(struct sock *sk);
 };
@@ -101,9 +101,9 @@ static inline struct subflow_context *subflow_ctx(const struct sock *sk)
 	return (struct subflow_context *)icsk->icsk_ulp_data;
 }
 
-static inline struct sock *sock_sk(const struct subflow_context *subflow)
+static inline struct socket *mptcp_subflow_tcp_socket(const struct subflow_context *subflow)
 {
-	return subflow->sk;
+	return subflow->tcp_sock;
 }
 
 int subflow_init(void);
net/mptcp/subflow.c +7 -3
index f88bc48ca997..7487753d531d 100644
@@ -174,6 +174,7 @@ static void subflow_data_ready(struct sock *sk)
 }
 
 static struct subflow_context *subflow_create_ctx(struct sock *sk,
+						  struct socket *sock,
 						  gfp_t priority)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
@@ -187,7 +188,8 @@ static struct subflow_context *subflow_create_ctx(struct sock *sk,
 
 	pr_debug("subflow=%p", ctx);
 
-	ctx->sk = sk;
+	/* might be NULL */
+	ctx->tcp_sock = sock;
 
 	return ctx;
 }
@@ -199,7 +201,7 @@ static int subflow_ulp_init(struct sock *sk)
 	struct subflow_context *ctx;
 	int err = 0;
 
-	ctx = subflow_create_ctx(sk, GFP_KERNEL);
+	ctx = subflow_create_ctx(sk, sk->sk_socket, GFP_KERNEL);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto out;
@@ -234,7 +236,9 @@ static void subflow_ulp_clone(const struct request_sock *req,
 	struct subflow_context *new_ctx;
 
 	old_ctx = inet_csk(newsk)->icsk_ulp_data;
-	new_ctx = subflow_create_ctx(newsk, priority);
+
+	/* newsk->sk_socket is NULL at this point */
+	new_ctx = subflow_create_ctx(newsk, NULL, priority);
 
 	if (!new_ctx)
 		return;
Signature · 1 line
2.21.0
#3 lore raw

Re: [MPTCP] [PATCH mptcp v2 2/3] mptcp: store tcp socket, not tcp sock in subflow ctx

Quoted 37 lines · Florian Westphal

Instead of using sk->sk_socket to get back the socket reference, store the 'socket' pointer that is coming from kernel_accept().

This doesn't fix a bug, but it appears cleaner to me.

v2: rename helper (Peter)

Signed-off-by Florian Westphal <fw(a)strlen.de>

---

net/mptcp/protocol.c | 19 ++++++++++---------
net/mptcp/protocol.h |  6 +++---
net/mptcp/subflow.c  | 10 +++++++---
3 files changed, 20 insertions(+), 15 deletions(-)
net/mptcp/protocol.c +2 -2
index ae65b5b6a1dd..e738bfad7430 100644
@@ -27,7 +27,7 @@ static inline bool before64(__u64 seq1, __u64 seq2)
 
 static bool mptcp_subflow_hold(struct subflow_context *subflow)
 {
-	struct sock *sk = sock_sk(subflow);
+	struct sock *sk = mptcp_subflow_tcp_socket(subflow)->sk;
 
 	return refcount_inc_not_zero(&sk->sk_refcnt);
 }
@@ -40,7 +40,7 @@ static struct sock *mptcp_subflow_get_ref(const struct mptcp_sock *msk)
 	mptcp_for_each_subflow(msk, subflow) {
 		if (mptcp_subflow_hold(subflow)) {
 			rcu_read_unlock();
-			return sock_sk(subflow);
+			return mptcp_subflow_tcp_socket(subflow)->sk;
 		}
 	}
 

net/mptcp/protocol.c static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq) · line 235

Quoted 5 lines
net/mptcp/protocol.c +1 -1
@@ -232,7 +232,7 @@ static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
 
 static u64 get_mapped_dsn(struct subflow_context *subflow)
 {
-	u32 map_offset = (tcp_sk(sock_sk(subflow))->copied_seq -
+	u32 map_offset = (tcp_sk(mptcp_subflow_tcp_socket(subflow)->sk)->copied_seq -

This line conflicts with one of Paolo's recently applied patches. Otherwise the change looks fine.

Mat

Quoted 120 lines
net/mptcp/protocol.c +7 -6
 			  subflow->ssn_offset -
 			  subflow->map_subflow_seq);
 
@@ -601,8 +601,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 
 	list_for_each_entry_safe(subflow, tmp, &list, node) {
 		pr_debug("conn_list->subflow=%p", subflow);
-		sock_release(sock_sk(subflow)->sk_socket);
-	};
+		sock_release(mptcp_subflow_tcp_socket(subflow));
+	}
 
 	sock_orphan(sk);
 	sock_put(sk);
@@ -656,6 +656,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
 		subflow->conn = new_mptcp_sock->sk;
+		subflow->tcp_sock = new_sock;
 	} else {
 		msk->subflow = new_sock;
 	}
@@ -934,8 +935,8 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 		if (!tmp)
 			break;
 
-		ret |= tcp_poll(file, sock_sk(tmp)->sk_socket, wait);
-		sock_put(sock_sk(tmp));
+		ret |= tcp_poll(file, mptcp_subflow_tcp_socket(tmp), wait);
+		sock_put(mptcp_subflow_tcp_socket(tmp)->sk);
 	}
 
 	return ret;
@@ -964,9 +965,9 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	lock_sock(sock->sk);
 	rcu_read_lock();
 	list_for_each_entry_rcu(subflow, &msk->conn_list, node) {
-		rcu_read_unlock();
 		pr_debug("conn_list->subflow=%p", subflow);
-		ret = kernel_sock_shutdown(sock_sk(subflow)->sk_socket, how);
+		rcu_read_unlock();
+		ret = kernel_sock_shutdown(mptcp_subflow_tcp_socket(subflow), how);
 		rcu_read_lock();
 	}
 	rcu_read_unlock();
net/mptcp/protocol.h +3 -3
index de1131c543d0..3dfe15f42058 100644
@@ -90,7 +90,7 @@ struct subflow_context {
 		map_valid : 1,
 		use_checksum : 1;
 
-	struct  sock *sk;       /* underlying tcp_sock */
+	struct  socket *tcp_sock;/* underlying tcp_sock */
 	struct  sock *conn;     /* parent mptcp_sock */
 	void	(*tcp_sk_data_ready)(struct sock *sk);
 };
@@ -101,9 +101,9 @@ static inline struct subflow_context *subflow_ctx(const struct sock *sk)
 	return (struct subflow_context *)icsk->icsk_ulp_data;
 }
 
-static inline struct sock *sock_sk(const struct subflow_context *subflow)
+static inline struct socket *mptcp_subflow_tcp_socket(const struct subflow_context *subflow)
 {
-	return subflow->sk;
+	return subflow->tcp_sock;
 }
 
 int subflow_init(void);
net/mptcp/subflow.c +7 -3
index f88bc48ca997..7487753d531d 100644
@@ -174,6 +174,7 @@ static void subflow_data_ready(struct sock *sk)
 }
 
 static struct subflow_context *subflow_create_ctx(struct sock *sk,
+						  struct socket *sock,
 						  gfp_t priority)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
@@ -187,7 +188,8 @@ static struct subflow_context *subflow_create_ctx(struct sock *sk,
 
 	pr_debug("subflow=%p", ctx);
 
-	ctx->sk = sk;
+	/* might be NULL */
+	ctx->tcp_sock = sock;
 
 	return ctx;
 }
@@ -199,7 +201,7 @@ static int subflow_ulp_init(struct sock *sk)
 	struct subflow_context *ctx;
 	int err = 0;
 
-	ctx = subflow_create_ctx(sk, GFP_KERNEL);
+	ctx = subflow_create_ctx(sk, sk->sk_socket, GFP_KERNEL);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto out;
@@ -234,7 +236,9 @@ static void subflow_ulp_clone(const struct request_sock *req,
 	struct subflow_context *new_ctx;
 
 	old_ctx = inet_csk(newsk)->icsk_ulp_data;
-	new_ctx = subflow_create_ctx(newsk, priority);
+
+	/* newsk->sk_socket is NULL at this point */
+	new_ctx = subflow_create_ctx(newsk, NULL, priority);
 
 	if (!new_ctx)
 		return;
Signature · 6 lines
2.21.0

_______________________________________________
mptcp mailing list
mptcp(a)lists.01.org
https://lists.01.org/mailman/listinfo/mptcp

--
Mat Martineau
Intel

author
#4 lore raw

PATCH v2 3/3 mptcp: accept: don't leak mptcp socket structure

In reply to Florian Westphal

accept() is supposed to prepare and return a 'struct sock'.

The caller holds a new inode/socket, and will associate the returned sock with it.

mptcp_accept however will allocate it via 'struct socket', then returns socket->sk.
This then leaks the outer socket struct inode returned by sock_create():

unreferenced object 0xffff88810512e8c0 (size 936):
comm "mptcp_connect", [..]
backtrace:

    [<00000000872561ba>] alloc_inode+0x35/0xe0
    [<00000000646e04ed>] new_inode_pseudo+0x12/0x80
    [<00000000e2e77036>] sock_alloc+0x26/0x100
    [<00000000870a8688>] __sock_create+0x8f/0x3c0
    [<000000000558b3fa>] mptcp_accept+0x140/0x530
    [<0000000044718d60>] inet_accept+0xac/0x470
    [<00000000da8f3979>] mptcp_stream_accept+0x62/0xa0
    [<00000000c9010499>] __sys_accept4+0x228/0x3c0

To fix this, make several (unfortunately, intrusive) changes:

1. Instead of allocating a new mptcp socket, clone the mptcp listen socket socket->sk.

This gives us a mptcp sock without the inode container. We return this sock struct to the caller, the caller will then complete creation of the mptcp socket.

2. For the 'compat' (old tcp, not mp capable) case, return the tcp socket directly and release the socket coming from kernel_accept().

We can use mptcp_getname() to override the socket->ops to tcp. This will make mptcp_accept work like tcp accept, and mptcp won't be involved anymore for the particular socket.

v2: add mptcp_init_sock() that got lost during last rebase.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 65 +++++++++++++++++++++++++++++++-------------
 1 file changed, 46 insertions(+), 19 deletions(-)
net/mptcp/protocol.c +46 -19
index e738bfad7430..a897f40bf966 100644
@@ -613,9 +613,9 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct socket *listener = msk->subflow;
-	struct socket *new_sock;
-	struct socket *new_mptcp_sock;
 	struct subflow_context *subflow;
+	struct socket *new_sock;
+	struct sock *newsk;
 
 	pr_debug("msk=%p, listener=%p", msk, subflow_ctx(listener->sk));
 	*err = kernel_accept(listener, &new_sock, flags);
@@ -625,28 +625,34 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 	subflow = subflow_ctx(new_sock->sk);
 	pr_debug("new subflow=%p", subflow);
 
-	*err = sock_create(PF_INET, SOCK_STREAM, IPPROTO_MPTCP, &new_mptcp_sock);
-	if (*err < 0) {
-		kernel_sock_shutdown(new_sock, SHUT_RDWR);
-		sock_release(new_sock);
-		return NULL;
-	}
-
-	msk = mptcp_sk(new_mptcp_sock->sk);
 	pr_debug("new msk=%p", msk);
 
 	if (subflow->mp_capable) {
+		struct sock *new_mptcp_sock;
 		u64 ack_seq;
 
+		local_bh_disable();
+		new_mptcp_sock = sk_clone_lock(sk, GFP_ATOMIC);
+		if (!new_sock) {
+			*err = -ENOBUFS;
+			local_bh_enable();
+			kernel_sock_shutdown(new_sock, SHUT_RDWR);
+			sock_release(new_sock);
+			return NULL;
+		}
+
+		mptcp_init_sock(new_mptcp_sock);
+
+		msk = mptcp_sk(new_mptcp_sock);
 		msk->remote_key = subflow->remote_key;
 		msk->local_key = subflow->local_key;
 		msk->token = subflow->token;
 		pr_debug("token=%u", msk->token);
-		token_update_accept(new_sock->sk, new_mptcp_sock->sk);
-		spin_lock_bh(&msk->conn_list_lock);
+		token_update_accept(new_sock->sk, new_mptcp_sock);
+		spin_lock(&msk->conn_list_lock);
 		list_add_rcu(&subflow->node, &msk->conn_list);
 		msk->subflow = NULL;
-		spin_unlock_bh(&msk->conn_list_lock);
+		spin_unlock(&msk->conn_list_lock);
 
 		crypto_key_sha1(msk->remote_key, NULL, &ack_seq);
 		msk->write_seq = subflow->idsn + 1;
@@ -655,14 +661,20 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_seq = ack_seq;
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
-		subflow->conn = new_mptcp_sock->sk;
 		subflow->tcp_sock = new_sock;
+		newsk = new_mptcp_sock;
+		subflow->conn = new_mptcp_sock;
+		bh_unlock_sock(new_mptcp_sock);
+		local_bh_enable();
+		inet_sk_state_store(newsk, TCP_ESTABLISHED);
 	} else {
-		msk->subflow = new_sock;
+		newsk = new_sock->sk;
+		tcp_sk(newsk)->is_mptcp = 0;
+		new_sock->sk = NULL;
+		sock_release(new_sock);
 	}
-	inet_sk_state_store(new_mptcp_sock->sk, TCP_ESTABLISHED);
 
-	return new_mptcp_sock->sk;
+	return newsk;
 }
 
 static void mptcp_destroy(struct sock *sk)
@@ -852,6 +864,18 @@ static int mptcp_getname(struct socket *sock, struct sockaddr *uaddr,
 
 	pr_debug("msk=%p", msk);
 
+	if (sock->sk->sk_prot == &tcp_prot) {
+		/* we are being invoked from __sys_accept4, after
+		 * mptcp_accept() has just accepted a non-mp-capable
+		 * flow: sk is a tcp_sk, not an mptcp one.
+		 *
+		 * Hand the socket over to tcp so all further socket ops
+		 * bypass mptcp.
+		 */
+		sock->ops = &inet_stream_ops;
+		return inet_getname(sock, uaddr, peer);
+	}
+
 	if (msk->subflow) {
 		pr_debug("subflow=%p", msk->subflow->sk);
 		return inet_getname(msk->subflow, uaddr, peer);
@@ -965,9 +989,12 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	lock_sock(sock->sk);
 	rcu_read_lock();
 	list_for_each_entry_rcu(subflow, &msk->conn_list, node) {
-		pr_debug("conn_list->subflow=%p", subflow);
+		struct socket *tcp_socket;
+
+		tcp_socket = mptcp_subflow_tcp_socket(subflow);
 		rcu_read_unlock();
-		ret = kernel_sock_shutdown(mptcp_subflow_tcp_socket(subflow), how);
+		pr_debug("conn_list->subflow=%p", subflow);
+		ret = kernel_sock_shutdown(tcp_socket, how);
 		rcu_read_lock();
 	}
 	rcu_read_unlock();
Signature · 1 line
2.21.0
#5 lore raw

Re: [MPTCP] [PATCH mptcp v2 3/3] mptcp: accept: don't leak mptcp socket structure

Quoted 72 lines · Florian Westphal

accept() is supposed to prepare and return a 'struct sock'.

The caller holds a new inode/socket, and will associate the returned sock with it.

mptcp_accept however will allocate it via 'struct socket', then returns socket->sk.
This then leaks the outer socket struct inode returned by sock_create():

unreferenced object 0xffff88810512e8c0 (size 936):
comm "mptcp_connect", [..]
backtrace:

   [<00000000872561ba>] alloc_inode+0x35/0xe0
   [<00000000646e04ed>] new_inode_pseudo+0x12/0x80
   [<00000000e2e77036>] sock_alloc+0x26/0x100
   [<00000000870a8688>] __sock_create+0x8f/0x3c0
   [<000000000558b3fa>] mptcp_accept+0x140/0x530
   [<0000000044718d60>] inet_accept+0xac/0x470
   [<00000000da8f3979>] mptcp_stream_accept+0x62/0xa0
   [<00000000c9010499>] __sys_accept4+0x228/0x3c0

To fix this, make several (unfortunately, intrusive) changes:

1. Instead of allocating a new mptcp socket, clone the mptcp listen socket socket->sk.

This gives us a mptcp sock without the inode container. We return this sock struct to the caller, the caller will then complete creation of the mptcp socket.

2. For the 'compat' (old tcp, not mp capable) case, return the tcp socket directly and release the socket coming from kernel_accept().

We can use mptcp_getname() to override the socket->ops to tcp. This will make mptcp_accept work like tcp accept, and mptcp won't be involved anymore for the particular socket.

v2: add mptcp_init_sock() that got lost during last rebase.

Signed-off-by Florian Westphal <fw(a)strlen.de>

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

1 file changed, 46 insertions(+), 19 deletions(-)
net/mptcp/protocol.c +2 -9
index e738bfad7430..a897f40bf966 100644
@@ -613,9 +613,9 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct socket *listener = msk->subflow;
-	struct socket *new_sock;
-	struct socket *new_mptcp_sock;
 	struct subflow_context *subflow;
+	struct socket *new_sock;
+	struct sock *newsk;
 
 	pr_debug("msk=%p, listener=%p", msk, subflow_ctx(listener->sk));
 	*err = kernel_accept(listener, &new_sock, flags);
@@ -625,28 +625,34 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 	subflow = subflow_ctx(new_sock->sk);
 	pr_debug("new subflow=%p", subflow);
 
-	*err = sock_create(PF_INET, SOCK_STREAM, IPPROTO_MPTCP, &new_mptcp_sock);
-	if (*err < 0) {
-		kernel_sock_shutdown(new_sock, SHUT_RDWR);
-		sock_release(new_sock);
-		return NULL;
-	}
-

net/mptcp/protocol.c static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, · line 636

Quoted 10 lines
net/mptcp/protocol.c +4 -1
-	msk = mptcp_sk(new_mptcp_sock->sk);
 	pr_debug("new msk=%p", msk);
 
 	if (subflow->mp_capable) {
+		struct sock *new_mptcp_sock;
 		u64 ack_seq;
 
+		local_bh_disable();
+		new_mptcp_sock = sk_clone_lock(sk, GFP_ATOMIC);
+		if (!new_sock) {

new_sock is non-null (it's dereferenced in the first line of this hunk), it looks like this is supposed to check new_mptcp_sock.

Mat

Quoted 92 lines
net/mptcp/protocol.c +40 -9
+			*err = -ENOBUFS;
+			local_bh_enable();
+			kernel_sock_shutdown(new_sock, SHUT_RDWR);
+			sock_release(new_sock);
+			return NULL;
+		}
+
+		mptcp_init_sock(new_mptcp_sock);
+
+		msk = mptcp_sk(new_mptcp_sock);
 		msk->remote_key = subflow->remote_key;
 		msk->local_key = subflow->local_key;
 		msk->token = subflow->token;
 		pr_debug("token=%u", msk->token);
-		token_update_accept(new_sock->sk, new_mptcp_sock->sk);
-		spin_lock_bh(&msk->conn_list_lock);
+		token_update_accept(new_sock->sk, new_mptcp_sock);
+		spin_lock(&msk->conn_list_lock);
 		list_add_rcu(&subflow->node, &msk->conn_list);
 		msk->subflow = NULL;
-		spin_unlock_bh(&msk->conn_list_lock);
+		spin_unlock(&msk->conn_list_lock);
 
 		crypto_key_sha1(msk->remote_key, NULL, &ack_seq);
 		msk->write_seq = subflow->idsn + 1;
@@ -655,14 +661,20 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_seq = ack_seq;
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
-		subflow->conn = new_mptcp_sock->sk;
 		subflow->tcp_sock = new_sock;
+		newsk = new_mptcp_sock;
+		subflow->conn = new_mptcp_sock;
+		bh_unlock_sock(new_mptcp_sock);
+		local_bh_enable();
+		inet_sk_state_store(newsk, TCP_ESTABLISHED);
 	} else {
-		msk->subflow = new_sock;
+		newsk = new_sock->sk;
+		tcp_sk(newsk)->is_mptcp = 0;
+		new_sock->sk = NULL;
+		sock_release(new_sock);
 	}
-	inet_sk_state_store(new_mptcp_sock->sk, TCP_ESTABLISHED);
 
-	return new_mptcp_sock->sk;
+	return newsk;
 }
 
 static void mptcp_destroy(struct sock *sk)
@@ -852,6 +864,18 @@ static int mptcp_getname(struct socket *sock, struct sockaddr *uaddr,
 
 	pr_debug("msk=%p", msk);
 
+	if (sock->sk->sk_prot == &tcp_prot) {
+		/* we are being invoked from __sys_accept4, after
+		 * mptcp_accept() has just accepted a non-mp-capable
+		 * flow: sk is a tcp_sk, not an mptcp one.
+		 *
+		 * Hand the socket over to tcp so all further socket ops
+		 * bypass mptcp.
+		 */
+		sock->ops = &inet_stream_ops;
+		return inet_getname(sock, uaddr, peer);
+	}
+
 	if (msk->subflow) {
 		pr_debug("subflow=%p", msk->subflow->sk);
 		return inet_getname(msk->subflow, uaddr, peer);
@@ -965,9 +989,12 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	lock_sock(sock->sk);
 	rcu_read_lock();
 	list_for_each_entry_rcu(subflow, &msk->conn_list, node) {
-		pr_debug("conn_list->subflow=%p", subflow);
+		struct socket *tcp_socket;
+
+		tcp_socket = mptcp_subflow_tcp_socket(subflow);
 		rcu_read_unlock();
-		ret = kernel_sock_shutdown(mptcp_subflow_tcp_socket(subflow), how);
+		pr_debug("conn_list->subflow=%p", subflow);
+		ret = kernel_sock_shutdown(tcp_socket, how);
 		rcu_read_lock();
 	}
 	rcu_read_unlock();
Signature · 6 lines
2.21.0

_______________________________________________
mptcp mailing list
mptcp(a)lists.01.org
https://lists.01.org/mailman/listinfo/mptcp

--
Mat Martineau
Intel