Skip to contentSkip to conversation

mptcp: rx path refactor

PATCH Superseded Paolo Abeni 3 participants 9 messages Nov 29, 2024 – Dec 3, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Paolo Abeni

author maintainer
#0 lore raw

cover mptcp: rx path refactor

This is a batch of changes I had sitting in my local tree for a while. Why another refactor you may ask? Two main resons:

- currently the mptcp RX path introduces quite a bit of 'exceptional' accounting/locking processing WRT to plain TCP, adding up to the implementation complexity in a misurable way
- the performance gap WRT plain TCP for single subflow connections is quite measurable.

The present refactor addresses both the above items: most of the additional complexity is dropped, and single stream performances increase measurably - from 55Gbps to 71Gbps in my loopback test. As a reference, plain TCP is around 84Gps on the same host.

The above comes to a price: the patch are invasive, even in subtle ways:
the chance of destabilizing the implementation is real (ence the additional, intentional '-next' into the subj).

In any case keeping the patch hidden for longer was not going to do any good, so here we are.

Paolo Abeni (3):
mptcp: consolidate subflow cleanup
mptcp: move the whole rx path under msk socket lock protection
mptcp: cleanup mem accounting.

 net/mptcp/fastopen.c |   4 +-
 net/mptcp/protocol.c | 202 +++++++++----------------------------------
 net/mptcp/protocol.h |   6 +-
 net/mptcp/subflow.c  |  33 +++----
 4 files changed, 66 insertions(+), 179 deletions(-)
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH 1/3 mptcp: consolidate subflow cleanup

Consolidate all the cleanup actions requiring the worked in a single helper and ensure the dummy data fin creation for fallback socket is performed only when the tcp rx queue is empty.

There are no functional changes intended, but this will simplify the next patch, when the tcp rx queue spooling could be delayed at release_cb time.

---
 net/mptcp/subflow.c | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)
net/mptcp/subflow.c +18 -15
index fd021cf8286e..2926bdf88e42 100644
@@ -1271,7 +1271,12 @@ static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
 		subflow->map_valid = 0;
 }
 
-/* sched mptcp worker to remove the subflow if no more data is pending */
+static bool subflow_is_done(const struct sock *sk)
+{
+	return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
+}
+
+/* sched mptcp worker for subflow cleanup if no more data is pending */
 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
 {
 	struct sock *sk = (struct sock *)msk;
@@ -1281,8 +1286,18 @@ static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ss
 		    inet_sk_state_load(sk) != TCP_ESTABLISHED)))
 		return;
 
-	if (skb_queue_empty(&ssk->sk_receive_queue) &&
-	    !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
+	if (!skb_queue_empty(&ssk->sk_receive_queue))
+		return;
+
+	if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
+		mptcp_schedule_work(sk);
+
+	/* when the fallback subflow closes the rx side, trigger a 'dummy'
+	 * ingress data fin, so that the msk state will follow along
+	 */
+	if (__mptcp_check_fallback(msk) && subflow_is_done(ssk) &&
+	    msk->first == ssk &&
+	    mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true))
 		mptcp_schedule_work(sk);
 }
 
@@ -1842,11 +1857,6 @@ static void __subflow_state_change(struct sock *sk)
 	rcu_read_unlock();
 }
 
-static bool subflow_is_done(const struct sock *sk)
-{
-	return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
-}
-
 static void subflow_state_change(struct sock *sk)
 {
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
@@ -1873,13 +1883,6 @@ static void subflow_state_change(struct sock *sk)
 		subflow_error_report(sk);
 
 	subflow_sched_work_if_closed(mptcp_sk(parent), sk);
-
-	/* when the fallback subflow closes the rx side, trigger a 'dummy'
-	 * ingress data fin, so that the msk state will follow along
-	 */
-	if (__mptcp_check_fallback(msk) && subflow_is_done(sk) && msk->first == sk &&
-	    mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true))
-		mptcp_schedule_work(parent);
 }
 
 void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
Signature · 1 line
2.45.2
author maintainer
#2 lore raw

PATCH 2/3 mptcp: move the whole rx path under msk socket lock protection

In reply to Paolo Abeni

After commit c2e6048fa1cf ("mptcp: fix race in release_cb") it's pretty straight forward move the whole MPTCP rx path under the socket lock leveraging the release_cb.

We can drop a bunch of spin_lock pairs in the receive functions, use a single receive queue and invoke __mptcp_move_skbs only when subflows ask for it.

This will allow more cleanup in the next patch

---
 net/mptcp/fastopen.c |  2 ++
 net/mptcp/protocol.c | 76 +++++++++++++++++++-------------------------
 net/mptcp/protocol.h |  2 +-
 3 files changed, 36 insertions(+), 44 deletions(-)
net/mptcp/fastopen.c +2 -0
index a29ff901df75..fb945c0d50bf 100644
@@ -49,6 +49,7 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	MPTCP_SKB_CB(skb)->has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
 
 	mptcp_data_lock(sk);
+	DEBUG_NET_WARN_ON_ONCE(sock_owned_by_user_nocheck(sk));
 
 	mptcp_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
@@ -65,6 +66,7 @@ void __mptcp_fastopen_gen_msk_ackseq(struct mptcp_sock *msk, struct mptcp_subflo
 	struct sock *sk = (struct sock *)msk;
 	struct sk_buff *skb;
 
+	DEBUG_NET_WARN_ON_ONCE(sock_owned_by_user_nocheck(sk));
 	skb = skb_peek_tail(&sk->sk_receive_queue);
 	if (skb) {
 		WARN_ON_ONCE(MPTCP_SKB_CB(skb)->end_seq);
net/mptcp/protocol.c +33 -43

211 lines not on this page · raw message

net/mptcp/protocol.h +1 -1
index b4c72a73594f..ad940cc1f26f 100644
@@ -124,6 +124,7 @@
 #define MPTCP_FLUSH_JOIN_LIST	5
 #define MPTCP_SYNC_STATE	6
 #define MPTCP_SYNC_SNDBUF	7
+#define MPTCP_DEQUEUE		8
 
 struct mptcp_skb_cb {
 	u64 map_seq;
@@ -322,7 +323,6 @@ struct mptcp_sock {
 	struct work_struct work;
 	struct sk_buff  *ooo_last_skb;
 	struct rb_root  out_of_order_queue;
-	struct sk_buff_head receive_queue;
 	struct list_head conn_list;
 	struct list_head rtx_queue;
 	struct mptcp_data_frag *first_pending;
Signature · 1 line
2.45.2
maintainer
#3 lore raw

Re: [PATCH mptcp-next-next 2/3] mptcp: move the whole rx path under msk socket lock protection

Hi Paolo,

Quoted 9 lines · Paolo Abeni

After commit c2e6048fa1cf ("mptcp: fix race in release_cb") it's pretty straight forward move the whole MPTCP rx path under the socket lock leveraging the release_cb.

We can drop a bunch of spin_lock pairs in the receive functions, use a single receive queue and invoke __mptcp_move_skbs only when subflows ask for it.

This will allow more cleanup in the next patch

(...)

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index f768aa4473fb..159add48f6d9 100644

(...)

net/mptcp/protocol.c static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2286

Quoted 8 lines
net/mptcp/protocol.c +1 -3
@@ -2290,9 +2283,8 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 		}
 	}
 
-	pr_debug("msk=%p rx queue empty=%d:%d copied=%d\n",
-		 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
-		 skb_queue_empty(&msk->receive_queue), copied);
+	pr_debug("msk=%p rx queue empty=%d copied=%d",

A small detail: the '\n' at the end is missing: no need to delay the output if it is not supposed to be used with a pr_cont().

(Something we can fix when applying the patches.)

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author maintainer
#4 lore raw

Re: [PATCH mptcp-next-next 2/3] mptcp: move the whole rx path under msk socket lock protection

Quoted 26 lines · Matthieu Baerts

Hi Paolo,

Quoted 9 lines · Paolo Abeni

After commit c2e6048fa1cf ("mptcp: fix race in release_cb") it's pretty straight forward move the whole MPTCP rx path under the socket lock leveraging the release_cb.

We can drop a bunch of spin_lock pairs in the receive functions, use a single receive queue and invoke __mptcp_move_skbs only when subflows ask for it.

This will allow more cleanup in the next patch

(...)

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index f768aa4473fb..159add48f6d9 100644

(...)

Quoted 4 lines
net/mptcp/protocol.c +0 -0
@@ -2290,9 +2283,8 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 		}
 	}
 
Quoted 7 lines

net/mptcp/protocol.c static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2286

Quoted 4 lines
net/mptcp/protocol.c +1 -3
-	pr_debug("msk=%p rx queue empty=%d:%d copied=%d\n",
-		 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
-		 skb_queue_empty(&msk->receive_queue), copied);
+	pr_debug("msk=%p rx queue empty=%d copied=%d",

A small detail: the '\n' at the end is missing: no need to delay the output if it is not supposed to be used with a pr_cont().

Thanks! I'll fix in the next revision. I think there is at least a fixable race in the current code (the one causing the ST failure - different root cause from
https://github.com/multipath-tcp/mptcp_net-next/issues/483, this is specific to the refactor).

I hope to be able to share the next version much later today.

Cheers,

Paolo

Quoted 5 lines

(Something we can fix when applying the patches.)

Cheers,
Matt

author maintainer
#5 lore raw

PATCH 3/3 mptcp: cleanup mem accounting.

In reply to Paolo Abeni

After the previous patch, updating sk_forward_memory is cheap and we can drop a lot of complexity from the MPTCP memory acconting, removing the custom fwd mem allocations for rmem.

---
 net/mptcp/fastopen.c |   2 +-
 net/mptcp/protocol.c | 128 ++++---------------------------------------
 net/mptcp/protocol.h |   4 +-
 3 files changed, 13 insertions(+), 121 deletions(-)
net/mptcp/fastopen.c +1 -1
index fb945c0d50bf..b0f1dddfb143 100644
@@ -51,7 +51,7 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	mptcp_data_lock(sk);
 	DEBUG_NET_WARN_ON_ONCE(sock_owned_by_user_nocheck(sk));
 
-	mptcp_set_owner_r(skb, sk);
+	skb_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
 	mptcp_sk(sk)->bytes_received += skb->len;
 
net/mptcp/protocol.c +11 -117

254 lines not on this page · raw message

net/mptcp/protocol.h +1 -3
index ad940cc1f26f..a0d46b69746d 100644
@@ -278,7 +278,6 @@ struct mptcp_sock {
 	u64		rcv_data_fin_seq;
 	u64		bytes_retrans;
 	u64		bytes_consumed;
-	int		rmem_fwd_alloc;
 	int		snd_burst;
 	int		old_wspace;
 	u64		recovery_snd_nxt;	/* in recovery mode accept up to this seq;
@@ -293,7 +292,6 @@ struct mptcp_sock {
 	u32		last_ack_recv;
 	unsigned long	timer_ival;
 	u32		token;
-	int		rmem_released;
 	unsigned long	flags;
 	unsigned long	cb_flags;
 	bool		recovery;		/* closing subflow write queue reinjected */
@@ -384,7 +382,7 @@ static inline void msk_owned_by_me(const struct mptcp_sock *msk)
  */
 static inline int __mptcp_rmem(const struct sock *sk)
 {
-	return atomic_read(&sk->sk_rmem_alloc) - READ_ONCE(mptcp_sk(sk)->rmem_released);
+	return atomic_read(&sk->sk_rmem_alloc);
 }
 
 static inline int mptcp_win_from_space(const struct sock *sk, int space)
Signature · 1 line
2.45.2
#6 lore raw
In reply to Paolo Abeni

Hi Paolo,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Unstable: 1 failed test(s): selftest_mptcp_connect 🔴
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/12088981326

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/818e1f7b5c07
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=913372

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

author maintainer
#7 lore raw
Quoted 6 lines · MPTCP CI

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Unstable: 1 failed test(s): selftest_mptcp_connect 🔴

This is somewhat 'funny'; I can't reproduce it locally, and other tests do the connection successfully. I guess there is some race due to the slow env?!? Is virtme still running without KVM ?

Thanks,

Paolo

maintainer
#8 lore raw
In reply to Paolo Abeni

Hi Paolo,

Quoted 9 lines · Paolo Abeni

This is a batch of changes I had sitting in my local tree for a while. Why another refactor you may ask? Two main resons:

- currently the mptcp RX path introduces quite a bit of 'exceptional' accounting/locking processing WRT to plain TCP, adding up to the implementation complexity in a misurable way
- the performance gap WRT plain TCP for single subflow connections is quite measurable.

Quoted 4 lines

The present refactor addresses both the above items: most of the additional complexity is dropped, and single stream performances increase measurably - from 55Gbps to 71Gbps in my loopback test. As a reference, plain TCP is around 84Gps on the same host.

Nice clean-up, with an excellent improvement!

Quoted 3 lines

The above comes to a price: the patch are invasive, even in subtle ways:
the chance of destabilizing the implementation is real (ence the additional, intentional '-next' into the subj).

Because we are only at the beginning of a new cycle, do we need to have this in '-next'? If we can have syzkaller validating this for a couple of weeks, would it not be OK to send that to netdev, to have it in linux-next for a few weeks?

Quoted 7 lines

In any case keeping the patch hidden for longer was not going to do any good, so here we are.

Paolo Abeni (3):
mptcp: consolidate subflow cleanup
mptcp: move the whole rx path under msk socket lock protection
mptcp: cleanup mem accounting.

The series look good to me: the modifications seem to make sense, but I don't know well this part that we usually avoid modifying :)

@Mat: do you mind having a look please?

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.