Skip to contentSkip to conversation

mptcp: Make MPTCP socket block/wakeup ignore sk_receive_queue

PATCH In review Mat Martineau 3 participants 5 messages Aug 3, 2019 – Aug 30, 2019

Subsystems (2)
lore
Share & tools
lore

Mat Martineau

author
#0 lore raw

PATCH v2 1/1 mptcp: Make MPTCP socket block/wakeup ignore sk_receive_queue

The MPTCP-level socket doesn't use sk_receive_queue, so it was possible for mptcp_recvmsg() to remain blocked when there was data ready for it to read. When the MPTCP socket is waiting for additional data and it releases the subflow socket lock, the subflow may have incoming packets ready to process and it sometimes called subflow_data_ready() before the MPTCP socket called sk_wait_data().

This change adds a new function for the MPTCP socket to use when waiting for a data ready signal. Atomic bitops with memory barriers are used to set, test, and clear a MPTCP socket flag that indicates waiting subflow data. This flag replaces the sk_receive_queue checks used by other socket types.

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

---

v2: Use existing sock_def_readable instead of creating a new function,
and add barriers around atomic ops.

 net/mptcp/protocol.c | 31 ++++++++++++++++++++++++++++++-
 net/mptcp/protocol.h |  4 ++++
 net/mptcp/subflow.c  |  5 +++++
 3 files changed, 39 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +30 -1
index 4d2844bff36f..18ba430e8009 100644
@@ -384,6 +384,31 @@ static enum mapping_status mptcp_get_mapping(struct sock *ssk)
 	return ret;
 }
 
+static void mptcp_wait_data(struct sock *sk, long *timeo)
+{
+	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	int data_ready;
+
+	add_wait_queue(sk_sleep(sk), &wait);
+	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
+
+	release_sock(sk);
+
+	smp_mb__before_atomic();
+	data_ready = test_and_clear_bit(MPTCP_DATA_READY, &msk->flags);
+	smp_mb__after_atomic();
+
+	if (!data_ready)
+		*timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, *timeo);
+
+	sched_annotate_sleep();
+	lock_sock(sk);
+
+	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
+	remove_wait_queue(sk_sleep(sk), &wait);
+}
+
 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 			 int nonblock, int flags, int *addr_len)
 {
@@ -434,6 +459,10 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 		u64 old_ack;
 		u32 ssn;
 
+		smp_mb__before_atomic();
+		clear_bit(MPTCP_DATA_READY, &msk->flags);
+		smp_mb__after_atomic();
+
 		status = mptcp_get_mapping(ssk);
 
 		if (status == MAPPING_ADDED) {
@@ -567,7 +596,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 
 		pr_debug("block");
 		release_sock(ssk);
-		sk_wait_data(sk, &timeo, NULL);
+		mptcp_wait_data(sk, &timeo);
 		lock_sock(ssk);
 	}
 
net/mptcp/protocol.h +4 -0
index 4c098acf4170..7a515d8e99d0 100644
@@ -72,6 +72,9 @@
 #define MPTCP_ADDR_IPVERSION_4	4
 #define MPTCP_ADDR_IPVERSION_6	6
 
+/* MPTCP socket flags */
+#define MPTCP_DATA_READY	BIT(0)
+
 static inline u32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
 {
 	return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
@@ -98,6 +101,7 @@ struct mptcp_sock {
 	u64		write_seq;
 	u64		ack_seq;
 	u32		token;
+	unsigned long	flags;
 	struct list_head conn_list;
 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
 	struct pm_data	pm;
net/mptcp/subflow.c +5 -0
index d91d817b8779..81c5cfae933a 100644
@@ -191,6 +191,11 @@ static void subflow_data_ready(struct sock *sk)
 
 	if (parent) {
 		pr_debug("parent=%p", parent);
+
+		smp_mb__before_atomic();
+		set_bit(MPTCP_DATA_READY, &mptcp_sk(parent)->flags);
+		smp_mb__after_atomic();
+
 		parent->sk_data_ready(parent);
 	}
 }
Signature · 1 line
2.22.0
#1 lore raw
Quoted 7 lines · Mat Martineau

The MPTCP-level socket doesn't use sk_receive_queue, so it was possible for mptcp_recvmsg() to remain blocked when there was data ready for it to read. When the MPTCP socket is waiting for additional data and it releases the subflow socket lock, the subflow may have incoming packets ready to process and it sometimes called subflow_data_ready() before the MPTCP socket called sk_wait_data().

Quoted 7 lines

This change adds a new function for the MPTCP socket to use when waiting for a data ready signal. Atomic bitops with memory barriers are used to set, test, and clear a MPTCP socket flag that indicates waiting subflow data. This flag replaces the sk_receive_queue checks used by other socket types.

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

(sorry for lagging behind so much)

The patch LGTM, but I have some related questions: how do you reproduce the issue fixed here? can we add that to the self-tests? (I assume/sort of recall the patch fix the issue in your scenario).

Thanks,

Paolo

author
#2 lore raw
Quoted 14 lines · Paolo Abeni
Quoted 13 lines · Mat Martineau

The MPTCP-level socket doesn't use sk_receive_queue, so it was possible for mptcp_recvmsg() to remain blocked when there was data ready for it to read. When the MPTCP socket is waiting for additional data and it releases the subflow socket lock, the subflow may have incoming packets ready to process and it sometimes called subflow_data_ready() before the MPTCP socket called sk_wait_data().

This change adds a new function for the MPTCP socket to use when waiting for a data ready signal. Atomic bitops with memory barriers are used to set, test, and clear a MPTCP socket flag that indicates waiting subflow data. This flag replaces the sk_receive_queue checks used by other socket types.

Quoted 8 lines
Quoted 1 line
Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>

(sorry for lagging behind so much)

The patch LGTM, but I have some related questions: how do you reproduce the issue fixed here? can we add that to the self-tests? (I assume/sort of recall the patch fix the issue in your scenario).

I was seeing the issue between the multipath-tcp.org kernel and ours, with a couple of simple test programs. The multipath-tcp.org system had a program running that would do a recvmsg() and then immediately send anything it received, and the prototype system would send data and then try to read it back. The prototype system could get stuck waiting for a blocked recvmsg() to return even though the peer had sent the data.

It may have been timing-dependent, I'll see if I can make it happen in a self test.

--
Mat Martineau
Intel

#3 lore raw
Quoted 32 lines · Mat Martineau
Quoted 14 lines · Paolo Abeni
Quoted 13 lines · Mat Martineau

The MPTCP-level socket doesn't use sk_receive_queue, so it was possible for mptcp_recvmsg() to remain blocked when there was data ready for it to read. When the MPTCP socket is waiting for additional data and it releases the subflow socket lock, the subflow may have incoming packets ready to process and it sometimes called subflow_data_ready() before the MPTCP socket called sk_wait_data().

This change adds a new function for the MPTCP socket to use when waiting for a data ready signal. Atomic bitops with memory barriers are used to set, test, and clear a MPTCP socket flag that indicates waiting subflow data. This flag replaces the sk_receive_queue checks used by other socket types.

Quoted 8 lines
Quoted 1 line
Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>

(sorry for lagging behind so much)

The patch LGTM, but I have some related questions: how do you reproduce the issue fixed here? can we add that to the self-tests? (I assume/sort of recall the patch fix the issue in your scenario).

I was seeing the issue between the multipath-tcp.org kernel and ours, with a couple of simple test programs. The multipath-tcp.org system had a program running that would do a recvmsg() and then immediately send anything it received, and the prototype system would send data and then try to read it back. The prototype system could get stuck waiting for a blocked recvmsg() to return even though the peer had sent the data.

Quoted 2 lines

It may have been timing-dependent, I'll see if I can make it happen in a self test.

Ok, I think that a self-test here is not strictly required.

I think we can merge this patch as is.

Paolo

#4 lore raw

Hi Mat, Paolo,

Quoted 33 lines · Paolo Abeni
Quoted 32 lines · Mat Martineau
Quoted 14 lines · Paolo Abeni
Quoted 13 lines · Mat Martineau

The MPTCP-level socket doesn't use sk_receive_queue, so it was possible for mptcp_recvmsg() to remain blocked when there was data ready for it to read. When the MPTCP socket is waiting for additional data and it releases the subflow socket lock, the subflow may have incoming packets ready to process and it sometimes called subflow_data_ready() before the MPTCP socket called sk_wait_data().

This change adds a new function for the MPTCP socket to use when waiting for a data ready signal. Atomic bitops with memory barriers are used to set, test, and clear a MPTCP socket flag that indicates waiting subflow data. This flag replaces the sk_receive_queue checks used by other socket types.

Quoted 8 lines
Quoted 1 line
Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>

(sorry for lagging behind so much)

The patch LGTM, but I have some related questions: how do you reproduce the issue fixed here? can we add that to the self-tests? (I assume/sort of recall the patch fix the issue in your scenario).

I was seeing the issue between the multipath-tcp.org kernel and ours, with a couple of simple test programs. The multipath-tcp.org system had a program running that would do a recvmsg() and then immediately send anything it received, and the prototype system would send data and then try to read it back. The prototype system could get stuck waiting for a blocked recvmsg() to return even though the peer had sent the data.

Quoted 6 lines
Quoted 2 lines

It may have been timing-dependent, I'll see if I can make it happen in a self test.

Ok, I think that a self-test here is not strictly required.

I think we can merge this patch as is.

Thank you for the patch and the review!

Just added at the end of the series.

https://github.com/multipath-tcp/mptcp_net-next/commits/export

I had a minor conflicts:

net/mptcp/protocol.h +0 -0
diff --cc net/mptcp/protocol.h
index 68877e952780,7a515d8e99d0..000000000000
@@@ -117,10 -101,10 +120,14 @@@ struct mptcp_sock
        u64             write_seq;
        u64             ack_seq;
        u32             token;
++<<<<<<< HEAD
 +      u16             dport;
++=======
+       unsigned long   flags;
++>>>>>>> mptcp: Make MPTCP socket block/wakeup ignore sk_receive_queue
        struct list_head conn_list;
        struct socket   *subflow; /* outgoing

connect/listener/!mp_capable */
- struct pm_data pm;

 +      struct mptcp_pm_data    pm;
        u8              addr_signal;

};

Fixed with:

net/mptcp/protocol.h +0 -0
diff --cc net/mptcp/protocol.h
index 68877e952780,7a515d8e99d0..000000000000
@@@ -117,10 -101,10 +120,11 @@@ struct mptcp_sock
        u64             write_seq;
        u64             ack_seq;
        u32             token;
+       unsigned long   flags;
 +      u16             dport;
        struct list_head conn_list;
        struct socket   *subflow; /* outgoing

connect/listener/!mp_capable */

- struct pm_data pm;

 +      struct mptcp_pm_data    pm;
        u8              addr_signal;

};

Tests are still OK!

Cheers,
Matt

Signature · 5 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium