Skip to contentSkip to conversation

Squash-to: "mptcp: leverage the backlog for RX packet processing"

PATCH In review Paolo Abeni 4 participants 8 messages Nov 9, 2025 – Nov 12, 2025

Subsystems (2)
lore
Share & tools
lore

Paolo Abeni

author maintainer
#0 lore raw

PATCH v2 1/1 Squash-to: "mptcp: leverage the backlog for RX packet processing"

If a subflow receives data before gaining the memcg while the msk socket lock is held at accept time, or the PM locks the msk socket while still unaccepted and subflows push data to it at the same time, the mptcp_graph_subflows() can complete with a non empty backlog.

The msk will try to borrow such memory, but (some) of the skbs there where not memcg charged. When the msk finally will return such accounted memory, we should hit the same splat of #597.
[even if so far I was unable to replicate this scenario]

This patch tries to address such potential issue by:
- preventing the subflow from queuing data into the backlog after gaining the memcg. This ensure that at the end of the look all the skbs in the backlog (if any) are _not_ memory accounted.
- mem charge the backlog to msk
- 'restart' the subflow and spool any data waiting there.

---
 net/mptcp/protocol.c | 46 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +44 -2
index 5e9325c7ea9c..d6b08e1de358 100644
@@ -4082,10 +4082,12 @@ static void mptcp_graph_subflows(struct sock *sk)
 {
 	struct mptcp_subflow_context *subflow;
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sock *ssk;
+	int old_amt, amt;
+	bool slow;
 
 	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-		bool slow;
+		ssk = mptcp_subflow_tcp_sock(subflow);
 
 		slow = lock_sock_fast(ssk);
 
@@ -4095,8 +4097,48 @@ static void mptcp_graph_subflows(struct sock *sk)
 		if (!ssk->sk_socket)
 			mptcp_sock_graft(ssk, sk->sk_socket);
 
+		if (!mem_cgroup_from_sk(sk))
+			goto unlock;
+
 		__mptcp_inherit_cgrp_data(sk, ssk);
 		__mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);
+
+		/* Prevent subflows from queueing data into the backlog
+		 * as soon as cg is set; note that we can't race
+		 * with __mptcp_close_ssk setting this bit for a really
+		 * closing socket, because we hold the msk socket lock here.
+		 */
+		subflow->closing = 1;
+
+unlock:
+		unlock_sock_fast(ssk, slow);
+	}
+
+	if (!mem_cgroup_from_sk(sk))
+		return;
+
+	/* Charge the bl memory, note that __sk_charge accounted for
+	 * fwd memory and rmem only
+	 */
+	mptcp_data_lock(sk);
+	old_amt = sk_mem_pages(sk->sk_forward_alloc +
+			       atomic_read(&sk->sk_rmem_alloc));
+	amt = sk_mem_pages(msk->backlog_len + sk->sk_forward_alloc +
+		     atomic_read(&sk->sk_rmem_alloc));
+	amt -= old_amt;
+	if (amt)
+		mem_cgroup_sk_charge(sk, amt, GFP_ATOMIC | __GFP_NOFAIL);
+	mptcp_data_unlock(sk);
+
+	/* Finally let the subflow restart queuing data. */
+	mptcp_for_each_subflow(msk, subflow) {
+		ssk = mptcp_subflow_tcp_sock(subflow);
+
+		slow = lock_sock_fast(ssk);
+		subflow->closing = 0;
+
+		if (mptcp_subflow_data_available(ssk))
+			mptcp_data_ready(sk, ssk);
 		unlock_sock_fast(ssk, slow);
 	}
 }
Signature · 1 line
2.51.0
#1 lore raw

Hi Paolo,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- 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/19214327116

Initiator: Matthieu Baerts (NGI0)
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/d100023cce38
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1021300

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)

reviewer
#2 lore raw
In reply to Paolo Abeni

Hi Paolo,

Thanks for this fix.

Quoted 49 lines · Paolo Abeni

If a subflow receives data before gaining the memcg while the msk socket lock is held at accept time, or the PM locks the msk socket while still unaccepted and subflows push data to it at the same time, the mptcp_graph_subflows() can complete with a non empty backlog.

The msk will try to borrow such memory, but (some) of the skbs there where not memcg charged. When the msk finally will return such accounted
memory, we should hit the same splat of #597.
[even if so far I was unable to replicate this scenario]

This patch tries to address such potential issue by:
- preventing the subflow from queuing data into the backlog after gaining the memcg. This ensure that at the end of the look all the skbs in the backlog (if any) are _not_ memory accounted.
- mem charge the backlog to msk
- 'restart' the subflow and spool any data waiting there.

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

 1 file changed, 44 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +3 -0
index 5e9325c7ea9c..d6b08e1de358 100644
@@ -4082,10 +4082,12 @@ static void mptcp_graph_subflows(struct sock
 *sk)
  {
  	struct mptcp_subflow_context *subflow;
  	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sock *ssk;
+	int old_amt, amt;
+	bool slow;


  mptcp_for_each_subflow(msk, subflow) {

net/mptcp/protocol.c +1 -2
-		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-		bool slow;
+		ssk = mptcp_subflow_tcp_sock(subflow);


  slow = lock_sock_fast(ssk);

net/mptcp/protocol.c +0 -0
@@ -4095,8 +4097,48 @@ static void mptcp_graph_subflows(struct sock
 *sk)
  		if (!ssk->sk_socket)
  			mptcp_sock_graft(ssk, sk->sk_socket);

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4101

Quoted 2 lines
net/mptcp/protocol.c +2 -0
+		if (!mem_cgroup_from_sk(sk))
+			goto unlock;

I think it's better to use "continue" here, just like in v1, so that other subflows also have a chance to call mptcp_sock_graft(), but we need to call unlock_sock_fast() before "continue".

Besides, wouldn't it be more appropriate to squash these lines into "mptcp: fix memcg accounting for passive sockets"?

Quoted 19 lines

+
  __mptcp_inherit_cgrp_data(sk, ssk); __mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);

net/mptcp/protocol.c +2 -0
+
+		/* Prevent subflows from queueing data into the

backlog

net/mptcp/protocol.c +2 -0
+		 * as soon as cg is set; note that we can't race
+		 * with __mptcp_close_ssk setting this bit for a

really

net/mptcp/protocol.c +1 -0
+		 * closing socket, because we hold the msk socket

lock here.

net/mptcp/protocol.c +8 -0
+		 */
+		subflow->closing = 1;
+
+unlock:
+		unlock_sock_fast(ssk, slow);
+	}
+
+	if (!mem_cgroup_from_sk(sk))

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4127

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		return;
+
+	/* Charge the bl memory, note that __sk_charge accounted for
+	 * fwd memory and rmem only
+	 */
+	mptcp_data_lock(sk);
+	old_amt = sk_mem_pages(sk->sk_forward_alloc +
+			       atomic_read(&sk->sk_rmem_alloc));
+	amt = sk_mem_pages(msk->backlog_len + sk->sk_forward_alloc +
+		     atomic_read(&sk->sk_rmem_alloc));

The code here is not aligned properly.

Quoted 4 lines
net/mptcp/protocol.c +3 -0
+	amt -= old_amt;
+	if (amt)
+		mem_cgroup_sk_charge(sk, amt, GFP_ATOMIC |

__GFP_NOFAIL);

I'm not sure if we need to call kmem_cache_charge() here, just like in __sk_charge().

WDYT?

Thanks,
-Geliang

Quoted 14 lines
net/mptcp/protocol.c +11 -0
+	mptcp_data_unlock(sk);
+
+	/* Finally let the subflow restart queuing data. */
+	mptcp_for_each_subflow(msk, subflow) {
+		ssk = mptcp_subflow_tcp_sock(subflow);
+
+		slow = lock_sock_fast(ssk);
+		subflow->closing = 0;
+
+		if (mptcp_subflow_data_available(ssk))
+			mptcp_data_ready(sk, ssk);

  unlock_sock_fast(ssk, slow);
  }
 }

maintainer
#3 lore raw

Hi Geliang,

Quoted 52 lines · Geliang Tang

Hi Paolo,

Thanks for this fix.

Quoted 47 lines · Paolo Abeni

If a subflow receives data before gaining the memcg while the msk socket lock is held at accept time, or the PM locks the msk socket while still unaccepted and subflows push data to it at the same time, the mptcp_graph_subflows() can complete with a non empty backlog.

The msk will try to borrow such memory, but (some) of the skbs there where not memcg charged. When the msk finally will return such accounted
memory, we should hit the same splat of #597.
[even if so far I was unable to replicate this scenario]

This patch tries to address such potential issue by:
- preventing the subflow from queuing data into the backlog after gaining the memcg. This ensure that at the end of the look all the skbs in the backlog (if any) are _not_ memory accounted.
- mem charge the backlog to msk
- 'restart' the subflow and spool any data waiting there.

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

 1 file changed, 44 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +3 -0
index 5e9325c7ea9c..d6b08e1de358 100644
@@ -4082,10 +4082,12 @@ static void mptcp_graph_subflows(struct sock
 *sk)
  {
  	struct mptcp_subflow_context *subflow;
  	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sock *ssk;
+	int old_amt, amt;
+	bool slow;


  mptcp_for_each_subflow(msk, subflow) {

net/mptcp/protocol.c +1 -2
-		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-		bool slow;
+		ssk = mptcp_subflow_tcp_sock(subflow);


  slow = lock_sock_fast(ssk);

net/mptcp/protocol.c +0 -0
@@ -4095,8 +4097,48 @@ static void mptcp_graph_subflows(struct sock
 *sk)
  		if (!ssk->sk_socket)
Quoted 8 lines

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4101

Quoted 4 lines
net/mptcp/protocol.c +0 -0
  			mptcp_sock_graft(ssk, sk->sk_socket);

net/mptcp/protocol.c +2 -0
+		if (!mem_cgroup_from_sk(sk))
+			goto unlock;

I think it's better to use "continue" here, just like in v1, so that other subflows also have a chance to call mptcp_sock_graft(), but we need to call unlock_sock_fast() before "continue".

Mmh, that's what this code is doing: unlock and continue, no?

Quoted 2 lines

Besides, wouldn't it be more appropriate to squash these lines into "mptcp: fix memcg accounting for passive sockets"?

From what I understood, that's not strickly needed: such check are done in the helpers below: here it is just to avoid doing the same check yet another time for 'subflow->closing = 1'.

Quoted 46 lines
Quoted 20 lines

+
  __mptcp_inherit_cgrp_data(sk, ssk); __mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);

net/mptcp/protocol.c +2 -0
+
+		/* Prevent subflows from queueing data into the

backlog

net/mptcp/protocol.c +2 -0
+		 * as soon as cg is set; note that we can't race
+		 * with __mptcp_close_ssk setting this bit for a

really

net/mptcp/protocol.c +1 -0
+		 * closing socket, because we hold the msk socket

lock here.

net/mptcp/protocol.c +8 -0
+		 */
+		subflow->closing = 1;
+
+unlock:
+		unlock_sock_fast(ssk, slow);
+	}
+
+	if (!mem_cgroup_from_sk(sk))

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4127

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		return;
+
+	/* Charge the bl memory, note that __sk_charge accounted for
+	 * fwd memory and rmem only
+	 */
+	mptcp_data_lock(sk);
+	old_amt = sk_mem_pages(sk->sk_forward_alloc +
+			       atomic_read(&sk->sk_rmem_alloc));
+	amt = sk_mem_pages(msk->backlog_len + sk->sk_forward_alloc +
+		     atomic_read(&sk->sk_rmem_alloc));

The code here is not aligned properly.

Quoted 4 lines
net/mptcp/protocol.c +3 -0
+	amt -= old_amt;
+	if (amt)
+		mem_cgroup_sk_charge(sk, amt, GFP_ATOMIC |

__GFP_NOFAIL);

I'm not sure if we need to call kmem_cache_charge() here, just like in __sk_charge().

WDYT?

Thanks,
-Geliang

Quoted 16 lines
Quoted 11 lines
net/mptcp/protocol.c +11 -0
+	mptcp_data_unlock(sk);
+
+	/* Finally let the subflow restart queuing data. */
+	mptcp_for_each_subflow(msk, subflow) {
+		ssk = mptcp_subflow_tcp_sock(subflow);
+
+		slow = lock_sock_fast(ssk);
+		subflow->closing = 0;
+
+		if (mptcp_subflow_data_available(ssk))
+			mptcp_data_ready(sk, ssk);
Quoted 3 lines

  unlock_sock_fast(ssk, slow);
  }
 }

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
maintainer
#4 lore raw
In reply to Paolo Abeni

Hi Paolo,

Quoted 42 lines · Paolo Abeni

If a subflow receives data before gaining the memcg while the msk socket lock is held at accept time, or the PM locks the msk socket while still unaccepted and subflows push data to it at the same time, the mptcp_graph_subflows() can complete with a non empty backlog.

The msk will try to borrow such memory, but (some) of the skbs there where not memcg charged. When the msk finally will return such accounted memory, we should hit the same splat of #597.
[even if so far I was unable to replicate this scenario]

This patch tries to address such potential issue by:
- preventing the subflow from queuing data into the backlog after gaining the memcg. This ensure that at the end of the look all the skbs in the backlog (if any) are _not_ memory accounted.
- mem charge the backlog to msk
- 'restart' the subflow and spool any data waiting there.

---
 net/mptcp/protocol.c | 46 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +4 -2
index 5e9325c7ea9c..d6b08e1de358 100644
@@ -4082,10 +4082,12 @@ static void mptcp_graph_subflows(struct sock *sk)
 {
 	struct mptcp_subflow_context *subflow;
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sock *ssk;
+	int old_amt, amt;
+	bool slow;
 
 	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-		bool slow;
+		ssk = mptcp_subflow_tcp_sock(subflow);
 
 		slow = lock_sock_fast(ssk);
 

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4100

Quoted 4 lines
net/mptcp/protocol.c +1 -0
@@ -4095,8 +4097,48 @@ static void mptcp_graph_subflows(struct sock *sk)
 		if (!ssk->sk_socket)
 			mptcp_sock_graft(ssk, sk->sk_socket);
 
+		if (!mem_cgroup_from_sk(sk))

Should we not call mem_cgroup_sk_enabled() instead? It does this:

  return mem_cgroup_sockets_enabled && mem_cgroup_from_sk(sk);

That's what is done in net/core/sock.c and net/ipv4/tcp_output.c. Not in __inet_accept(), because mem_cgroup_sockets_enabled() is checked before. Maybe we should do the same here?

(Note that it is not clear to me if mem_cgroup can be enabled later on, and if yes, what should be done with existing connections.)

Quoted 7 lines
net/mptcp/protocol.c +5 -0
+			goto unlock;
+
 		__mptcp_inherit_cgrp_data(sk, ssk);
 		__mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);
+
+		/* Prevent subflows from queueing data into the backlog
+		 * as soon as cg is set; note that we can't race

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4117

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		 * with __mptcp_close_ssk setting this bit for a really
+		 * closing socket, because we hold the msk socket lock here.
+		 */
+		subflow->closing = 1;
+
+unlock:
+		unlock_sock_fast(ssk, slow);
+	}
+
+	if (!mem_cgroup_from_sk(sk))

Same here?

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4127

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		return;
+
+	/* Charge the bl memory, note that __sk_charge accounted for
+	 * fwd memory and rmem only
+	 */
+	mptcp_data_lock(sk);
+	old_amt = sk_mem_pages(sk->sk_forward_alloc +
+			       atomic_read(&sk->sk_rmem_alloc));
+	amt = sk_mem_pages(msk->backlog_len + sk->sk_forward_alloc +
+		     atomic_read(&sk->sk_rmem_alloc));

(Same as Geliang for the alignment here, and eventually calling kmem_cache_charge() like in __inet_accept())

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4130

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+	amt -= old_amt;
+	if (amt)
+		mem_cgroup_sk_charge(sk, amt, GFP_ATOMIC | __GFP_NOFAIL);

Just to be sure: no need to check if there was an error? It is not done in __inet_accept() either, so I guess no?

Quoted 4 lines
net/mptcp/protocol.c +4 -0
+	mptcp_data_unlock(sk);
+
+	/* Finally let the subflow restart queuing data. */
+	mptcp_for_each_subflow(msk, subflow) {

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4144

Quoted 10 lines
net/mptcp/protocol.c +7 -0
+		ssk = mptcp_subflow_tcp_sock(subflow);
+
+		slow = lock_sock_fast(ssk);
+		subflow->closing = 0;
+
+		if (mptcp_subflow_data_available(ssk))
+			mptcp_data_ready(sk, ssk);
 		unlock_sock_fast(ssk, slow);
 	}
 }

Cheers,
Matt

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

Hi Paolo,

Quoted 52 lines · Matthieu Baerts

Hi Paolo,

Quoted 42 lines · Paolo Abeni

If a subflow receives data before gaining the memcg while the msk socket lock is held at accept time, or the PM locks the msk socket while still unaccepted and subflows push data to it at the same time, the mptcp_graph_subflows() can complete with a non empty backlog.

The msk will try to borrow such memory, but (some) of the skbs there where not memcg charged. When the msk finally will return such accounted memory, we should hit the same splat of #597.
[even if so far I was unable to replicate this scenario]

This patch tries to address such potential issue by:
- preventing the subflow from queuing data into the backlog after gaining the memcg. This ensure that at the end of the look all the skbs in the backlog (if any) are _not_ memory accounted.
- mem charge the backlog to msk
- 'restart' the subflow and spool any data waiting there.

---
 net/mptcp/protocol.c | 46 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +4 -2
index 5e9325c7ea9c..d6b08e1de358 100644
@@ -4082,10 +4082,12 @@ static void mptcp_graph_subflows(struct sock *sk)
 {
 	struct mptcp_subflow_context *subflow;
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sock *ssk;
+	int old_amt, amt;
+	bool slow;
 
 	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-		bool slow;
+		ssk = mptcp_subflow_tcp_sock(subflow);
 
 		slow = lock_sock_fast(ssk);
 

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4100

Quoted 4 lines
net/mptcp/protocol.c +1 -0
@@ -4095,8 +4097,48 @@ static void mptcp_graph_subflows(struct sock *sk)
 		if (!ssk->sk_socket)
 			mptcp_sock_graft(ssk, sk->sk_socket);
 
+		if (!mem_cgroup_from_sk(sk))

Should we not call mem_cgroup_sk_enabled() instead? It does this:

Quoted 8 lines
  return mem_cgroup_sockets_enabled && mem_cgroup_from_sk(sk);

That's what is done in net/core/sock.c and net/ipv4/tcp_output.c. Not in __inet_accept(), because mem_cgroup_sockets_enabled() is checked before. Maybe we should do the same here?

(Note that it is not clear to me if mem_cgroup can be enabled later on, and if yes, what should be done with existing connections.)

Also, do you not still need to call __mptcp_inherit_cgrp_data() even if !mem_cgroup_sockets_enabled() (or !mem_cgroup_from_sk())?

I guess the two are often linked, but can they not be used independently?

Cheers,
Matt

Quoted 56 lines
Quoted 8 lines
net/mptcp/protocol.c +5 -0
+			goto unlock;
+
 		__mptcp_inherit_cgrp_data(sk, ssk);
 		__mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);
+
+		/* Prevent subflows from queueing data into the backlog
+		 * as soon as cg is set; note that we can't race

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4117

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		 * with __mptcp_close_ssk setting this bit for a really
+		 * closing socket, because we hold the msk socket lock here.
+		 */
+		subflow->closing = 1;
+
+unlock:
+		unlock_sock_fast(ssk, slow);
+	}
+
+	if (!mem_cgroup_from_sk(sk))

Same here?

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4127

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		return;
+
+	/* Charge the bl memory, note that __sk_charge accounted for
+	 * fwd memory and rmem only
+	 */
+	mptcp_data_lock(sk);
+	old_amt = sk_mem_pages(sk->sk_forward_alloc +
+			       atomic_read(&sk->sk_rmem_alloc));
+	amt = sk_mem_pages(msk->backlog_len + sk->sk_forward_alloc +
+		     atomic_read(&sk->sk_rmem_alloc));

(Same as Geliang for the alignment here, and eventually calling kmem_cache_charge() like in __inet_accept())

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4130

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+	amt -= old_amt;
+	if (amt)
+		mem_cgroup_sk_charge(sk, amt, GFP_ATOMIC | __GFP_NOFAIL);

Just to be sure: no need to check if there was an error? It is not done in __inet_accept() either, so I guess no?

Quoted 14 lines
net/mptcp/protocol.c +11 -0
+	mptcp_data_unlock(sk);
+
+	/* Finally let the subflow restart queuing data. */
+	mptcp_for_each_subflow(msk, subflow) {
+		ssk = mptcp_subflow_tcp_sock(subflow);
+
+		slow = lock_sock_fast(ssk);
+		subflow->closing = 0;
+
+		if (mptcp_subflow_data_available(ssk))
+			mptcp_data_ready(sk, ssk);
 		unlock_sock_fast(ssk, slow);
 	}
 }
Signature · 1 line
Sponsored by the NGI0 Core fund.
author maintainer
#6 lore raw
In reply to Matthieu Baerts
Quoted 50 lines · Matthieu Baerts
Quoted 42 lines · Paolo Abeni

If a subflow receives data before gaining the memcg while the msk socket lock is held at accept time, or the PM locks the msk socket while still unaccepted and subflows push data to it at the same time, the mptcp_graph_subflows() can complete with a non empty backlog.

The msk will try to borrow such memory, but (some) of the skbs there where not memcg charged. When the msk finally will return such accounted memory, we should hit the same splat of #597.
[even if so far I was unable to replicate this scenario]

This patch tries to address such potential issue by:
- preventing the subflow from queuing data into the backlog after gaining the memcg. This ensure that at the end of the look all the skbs in the backlog (if any) are _not_ memory accounted.
- mem charge the backlog to msk
- 'restart' the subflow and spool any data waiting there.

---
 net/mptcp/protocol.c | 46 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +4 -2
index 5e9325c7ea9c..d6b08e1de358 100644
@@ -4082,10 +4082,12 @@ static void mptcp_graph_subflows(struct sock *sk)
 {
 	struct mptcp_subflow_context *subflow;
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sock *ssk;
+	int old_amt, amt;
+	bool slow;
 
 	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-		bool slow;
+		ssk = mptcp_subflow_tcp_sock(subflow);
 
 		slow = lock_sock_fast(ssk);
 

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4100

Quoted 4 lines
net/mptcp/protocol.c +1 -0
@@ -4095,8 +4097,48 @@ static void mptcp_graph_subflows(struct sock *sk)
 		if (!ssk->sk_socket)
 			mptcp_sock_graft(ssk, sk->sk_socket);
 
+		if (!mem_cgroup_from_sk(sk))

Should we not call mem_cgroup_sk_enabled() instead? It does this:

Quoted 8 lines
  return mem_cgroup_sockets_enabled && mem_cgroup_from_sk(sk);

That's what is done in net/core/sock.c and net/ipv4/tcp_output.c. Not in __inet_accept(), because mem_cgroup_sockets_enabled() is checked before. Maybe we should do the same here?

(Note that it is not clear to me if mem_cgroup can be enabled later on, and if yes, what should be done with existing connections.)

It's just an additional optimization, to leverage static branch, but it's not strictly needed. Can be added, thus.

Quoted 20 lines
Quoted 7 lines
net/mptcp/protocol.c +5 -0
+			goto unlock;
+
 		__mptcp_inherit_cgrp_data(sk, ssk);
 		__mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);
+
+		/* Prevent subflows from queueing data into the backlog
+		 * as soon as cg is set; note that we can't race

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4117

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		 * with __mptcp_close_ssk setting this bit for a really
+		 * closing socket, because we hold the msk socket lock here.
+		 */
+		subflow->closing = 1;
+
+unlock:
+		unlock_sock_fast(ssk, slow);
+	}
+
+	if (!mem_cgroup_from_sk(sk))

Same here?

Quoted 13 lines

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4127

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		return;
+
+	/* Charge the bl memory, note that __sk_charge accounted for
+	 * fwd memory and rmem only
+	 */
+	mptcp_data_lock(sk);
+	old_amt = sk_mem_pages(sk->sk_forward_alloc +
+			       atomic_read(&sk->sk_rmem_alloc));
+	amt = sk_mem_pages(msk->backlog_len + sk->sk_forward_alloc +
+		     atomic_read(&sk->sk_rmem_alloc));

(Same as Geliang for the alignment here, and eventually calling kmem_cache_charge() like in __inet_accept())

This and the next are the more obscure point. I chose to not call kmem_cache_charge() because I'm (was) a bit doubtful about such call being legit in __inet_accept(): active (plain TCP) sockets are not accounted, just passive ones. Re-thinking about it I guess it's better to be consistent with TCP than trying to be smarted (history has proved it does not work so well :-P)

TL;DR: I'll add the missing kmem_cache_charge();

Quoted 6 lines

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4130

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+	amt -= old_amt;
+	if (amt)
+		mem_cgroup_sk_charge(sk, amt, GFP_ATOMIC | __GFP_NOFAIL);

Just to be sure: no need to check if there was an error? It is not done in __inet_accept() either, so I guess no?

The __GFP_NOFAIL flag ensures that the call can not fail. Adding it with GFP_ATOMIC is at least "original" (this is the only call site with this flags combo). In the next version I'll move the call outside the spinlock (we are still under the msk socket lock) to replace GFP_ATOMIC with GFP_KERNEL.

Many thanks for all the review effort!

/P

maintainer
#7 lore raw

Hi Paolo,

Thank you for your reply!

Quoted 57 lines · Paolo Abeni
Quoted 56 lines · Matthieu Baerts
Quoted 42 lines · Paolo Abeni

If a subflow receives data before gaining the memcg while the msk socket lock is held at accept time, or the PM locks the msk socket while still unaccepted and subflows push data to it at the same time, the mptcp_graph_subflows() can complete with a non empty backlog.

The msk will try to borrow such memory, but (some) of the skbs there where not memcg charged. When the msk finally will return such accounted memory, we should hit the same splat of #597.
[even if so far I was unable to replicate this scenario]

This patch tries to address such potential issue by:
- preventing the subflow from queuing data into the backlog after gaining the memcg. This ensure that at the end of the look all the skbs in the backlog (if any) are _not_ memory accounted.
- mem charge the backlog to msk
- 'restart' the subflow and spool any data waiting there.

---
 net/mptcp/protocol.c | 46 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +4 -2
index 5e9325c7ea9c..d6b08e1de358 100644
@@ -4082,10 +4082,12 @@ static void mptcp_graph_subflows(struct sock *sk)
 {
 	struct mptcp_subflow_context *subflow;
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sock *ssk;
+	int old_amt, amt;
+	bool slow;
 
 	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-		bool slow;
+		ssk = mptcp_subflow_tcp_sock(subflow);
 
 		slow = lock_sock_fast(ssk);
 

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4100

Quoted 4 lines
net/mptcp/protocol.c +1 -0
@@ -4095,8 +4097,48 @@ static void mptcp_graph_subflows(struct sock *sk)
 		if (!ssk->sk_socket)
 			mptcp_sock_graft(ssk, sk->sk_socket);
 
+		if (!mem_cgroup_from_sk(sk))

Should we not call mem_cgroup_sk_enabled() instead? It does this:

  return mem_cgroup_sockets_enabled && mem_cgroup_from_sk(sk);

That's what is done in net/core/sock.c and net/ipv4/tcp_output.c. Not in __inet_accept(), because mem_cgroup_sockets_enabled() is checked before. Maybe we should do the same here?

Quoted 5 lines
Quoted 2 lines

(Note that it is not clear to me if mem_cgroup can be enabled later on, and if yes, what should be done with existing connections.)

It's just an additional optimization, to leverage static branch, but it's not strictly needed. Can be added, thus.

I see, thank you!

While at it, should we call mem_cgroup_*() only once by using local variables?

Quoted 34 lines
Quoted 20 lines
Quoted 7 lines
net/mptcp/protocol.c +5 -0
+			goto unlock;
+
 		__mptcp_inherit_cgrp_data(sk, ssk);
 		__mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);
+
+		/* Prevent subflows from queueing data into the backlog
+		 * as soon as cg is set; note that we can't race

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4117

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		 * with __mptcp_close_ssk setting this bit for a really
+		 * closing socket, because we hold the msk socket lock here.
+		 */
+		subflow->closing = 1;
+
+unlock:
+		unlock_sock_fast(ssk, slow);
+	}
+
+	if (!mem_cgroup_from_sk(sk))

Same here?

Quoted 13 lines

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4127

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+		return;
+
+	/* Charge the bl memory, note that __sk_charge accounted for
+	 * fwd memory and rmem only
+	 */
+	mptcp_data_lock(sk);
+	old_amt = sk_mem_pages(sk->sk_forward_alloc +
+			       atomic_read(&sk->sk_rmem_alloc));
+	amt = sk_mem_pages(msk->backlog_len + sk->sk_forward_alloc +
+		     atomic_read(&sk->sk_rmem_alloc));

(Same as Geliang for the alignment here, and eventually calling kmem_cache_charge() like in __inet_accept())

Quoted 8 lines

This and the next are the more obscure point. I chose to not call kmem_cache_charge() because I'm (was) a bit doubtful about such call being legit in __inet_accept(): active (plain TCP) sockets are not accounted, just passive ones. Re-thinking about it I guess it's better to be consistent with TCP than trying to be smarted (history has proved it does not work so well :-P)

TL;DR: I'll add the missing kmem_cache_charge();

:)

Indeed, safer. If it is changed in TCP, hopefully the same will be done in MPTCP side.

Quoted 8 lines
Quoted 6 lines

net/mptcp/protocol.c static void mptcp_graph_subflows(struct sock *sk) · line 4130

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+	amt -= old_amt;
+	if (amt)
+		mem_cgroup_sk_charge(sk, amt, GFP_ATOMIC | __GFP_NOFAIL);

Just to be sure: no need to check if there was an error? It is not done in __inet_accept() either, so I guess no?

The __GFP_NOFAIL flag ensures that the call can not fail.

Of course, I missed that!

Quoted 5 lines

Adding it with
GFP_ATOMIC is at least "original" (this is the only call site with this flags combo). In the next version I'll move the call outside the spinlock (we are still under the msk socket lock) to replace GFP_ATOMIC with GFP_KERNEL.

Good idea! Also better to align with what is done with TCP.

Quoted 1 line

Many thanks for all the review effort!

That's the least I can do with all the new fixes and optimisations :)

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.