mptcp: fix memcg accounting for passive sockets
Subsystems (3)
Paolo Abeni
maintainercover mptcp: fix memcg accounting for passive sockets
It turns out that the splat reported earlier is indeed due to an old bug/missing feature that hit hard on top of "mptcp: borrow forward memory from subflow", as we can borrow accounted memory from a socket not doing any accounting;)
It should land upstream before the above mentioned commit, ence the 'net' target, but it could as well be net-next material.
I think there is still a racing window to produce the splat reported in #597: if a subflow receive data while the msk is locked, and before the memcg is initialized on such subflow, it will push the data into the msk becklog, and later borrow will again try to grab accounted memory from non accounting ssk.
I have a tentative fix for that one but it's quite ugly and I'll keep it separate from this series.
Paolo Abeni (3):
net: factor-out _sk_charge() helper
mptcp: factor-out cgroup data inherit helper
mptcp: fix memcg accounting for passive sockets
include/net/sock.h | 2 ++ net/core/sock.c | 18 ++++++++++++++++++ net/ipv4/af_inet.c | 17 +---------------- net/mptcp/protocol.c | 36 ++++++++++++++++++++++++++---------- net/mptcp/protocol.h | 3 +++ net/mptcp/subflow.c | 30 ++++++++++++++++++++++-------- 6 files changed, 72 insertions(+), 34 deletions(-)
Signature · 1 line
2.51.0
Paolo Abeni
maintainerPATCH 1/3 net: factor-out _sk_charge() helper
Move out of __inet_accept() the code dealing charging newly accepted socket to memcg. MPTCP will soon use it to on a per subflow basis, in different contexts.
No functional changes intended.
--- include/net/sock.h | 2 ++ net/core/sock.c | 18 ++++++++++++++++++ net/ipv4/af_inet.c | 17 +---------------- 3 files changed, 21 insertions(+), 16 deletions(-)
include/net/sock.h +2 -0
@@ -1631,6 +1631,8 @@ static inline void sk_mem_uncharge(struct sock *sk, int size)sk_mem_reclaim (sk); } +void __sk_charge (struct sock *sk,gfp_t gfp); +#if IS_ENABLED (CONFIG_PROVE_LOCKING ) &&IS_ENABLED (CONFIG_MODULES )static inline void sk_owner_set (struct sock *sk,struct module *owner) {
net/core/sock.c +18 -0
@@ -3448,6 +3448,24 @@ void __sk_mem_reclaim(struct sock *sk, int amount) }EXPORT_SYMBOL (__sk_mem_reclaim); +void __sk_charge (struct sock *sk,gfp_t gfp) +{ +int amt; + + gfp |= __GFP_NOFAIL; +if (mem_cgroup_from_sk (sk)) { +/* The socket has not been accepted yet, no need +* to look at newsk->sk_wmem_queued. +*/ + amt =sk_mem_pages (sk->sk_forward_alloc + +atomic_read (&sk->sk_rmem_alloc)); +if (amt) +mem_cgroup_sk_charge (sk, amt, gfp); + } + +kmem_cache_charge (sk, gfp); +} +int sk_set_peek_off (struct sock *sk,int val) {WRITE_ONCE (sk->sk_peek_off, val);
net/ipv4/af_inet.c +1 -16
@@ -756,23 +756,8 @@ EXPORT_SYMBOL(inet_stream_connect);void __inet_accept (struct socket *sock,struct socket *newsock,struct sock *newsk) {if (mem_cgroup_sockets_enabled) { -gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL; -mem_cgroup_sk_alloc (newsk); - -if (mem_cgroup_from_sk (newsk)) { -int amt; - -/* The socket has not been accepted yet, no need -* to look at newsk->sk_wmem_queued. -*/ - amt =sk_mem_pages (newsk->sk_forward_alloc + -atomic_read (&newsk->sk_rmem_alloc)); -if (amt) -mem_cgroup_sk_charge (newsk, amt, gfp); - } - -kmem_cache_charge (newsk, gfp); +__sk_charge (newsk,GFP_KERNEL ); }sock_rps_record_flow (newsk);
Signature · 1 line
2.51.0
Paolo Abeni
maintainerPATCH 2/3 mptcp: factor-out cgroup data inherit helper
In reply to Paolo AbeniMPTCP will soon need the same functionality for passive sockets, factor them out in a common helper. No functional change intended.
--- net/mptcp/protocol.h | 2 ++ net/mptcp/subflow.c | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-)
net/mptcp/protocol.h +2 -0
@@ -755,6 +755,8 @@ mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated)return ret; } +void __mptcp_inherit_cgrp_data (struct sock *sk,struct sock *ssk); +int mptcp_is_enabled (const struct net *net);unsigned int mptcp_get_add_addr_timeout (const struct net *net);int mptcp_is_checksum_enabled (const struct net *net);
net/mptcp/subflow.c +12 -8
@@ -1721,21 +1721,25 @@ int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local,return err; } -static void mptcp_attach_cgroup (struct sock *parent,struct sock *child) +void __mptcp_inherit_cgrp_data (struct sock *sk,struct sock *ssk) {#ifdef CONFIG_SOCK_CGROUP_DATA -struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data, - *child_skcd = &child->sk_cgrp_data; +struct sock_cgroup_data *sk_cd = &sk->sk_cgrp_data, + *ssk_cd = &ssk->sk_cgrp_data;/* only the additional subflows created by kworkers have to be modified */ -if (cgroup_id (sock_cgroup_ptr (parent_skcd)) != -cgroup_id (sock_cgroup_ptr (child_skcd))) { -cgroup_sk_free (child_skcd); - *child_skcd = *parent_skcd; -cgroup_sk_clone (child_skcd); +if (cgroup_id (sock_cgroup_ptr (sk_cd)) != +cgroup_id (sock_cgroup_ptr (ssk_cd))) { +cgroup_sk_free (ssk_cd); + *ssk_cd = *sk_cd; +cgroup_sk_clone (sk_cd); }#endif /* CONFIG_SOCK_CGROUP_DATA */ +} +static void mptcp_attach_cgroup (struct sock *parent,struct sock *child) +{ +__mptcp_inherit_cgrp_data (parent, child);if (mem_cgroup_sockets_enabled)mem_cgroup_sk_inherit (parent, child); }
Signature · 1 line
2.51.0
Paolo Abeni
maintainerPATCH 3/3 mptcp: fix memcg accounting for passive sockets
In reply to Paolo AbeniThe passive sockets never got proper memcg accounting: the msk socket is associated with the memcg at accept time, but the passive subflows never got it right.
At accept time, traverse the subflows list and associate each of them with the msk memcg, and try to do the same at join completion time, if the msk has been already accepted.
--- net/mptcp/protocol.c | 36 ++++++++++++++++++++++++++---------- net/mptcp/protocol.h | 1 + net/mptcp/subflow.c | 10 ++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-)
net/mptcp/protocol.c +26 -10
@@ -959,6 +959,8 @@ static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)mptcp_subflow_ctx (ssk)->subflow_id = msk->subflow_id++;mptcp_sockopt_sync_locked (msk, ssk);mptcp_stop_tout_timer (sk); +__mptcp_inherit_cgrp_data (sk, ssk); +__mptcp_inherit_memcg (sk, ssk,GFP_ATOMIC );__mptcp_propagate_sndbuf (sk, ssk);__mptcp_propagate_rcvspace (sk, ssk);return true;
@@ -4076,6 +4078,29 @@ static int mptcp_listen(struct socket *sock, int backlog)return err; } +static void mptcp_graph_subflows (struct sock *sk) +{ +struct mptcp_subflow_context *subflow; +struct mptcp_sock *msk =mptcp_sk (sk); + +mptcp_for_each_subflow (msk, subflow) { +struct sock *ssk =mptcp_subflow_tcp_sock (subflow); +bool slow; + + slow =lock_sock_fast (ssk); + +/* set ssk->sk_socket of accept()ed flows to mptcp socket. +* This is needed so NOSPACE flag can be set from tcp stack. +*/ +if (!ssk->sk_socket) +mptcp_sock_graft (ssk, sk->sk_socket); + +__mptcp_inherit_cgrp_data (sk, ssk); +__mptcp_inherit_memcg (sk, ssk,GFP_KERNEL ); +unlock_sock_fast (ssk, slow); + } +} +static int mptcp_stream_accept (struct socket *sock,struct socket *newsock,struct proto_accept_arg *arg) {
@@ -4123,16 +4148,7 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, msk =mptcp_sk (newsk); msk->in_accept_queue =0 ; -/* set ssk->sk_socket of accept()ed flows to mptcp socket. -* This is needed so NOSPACE flag can be set from tcp stack. -*/ -mptcp_for_each_subflow (msk, subflow) { -struct sock *ssk =mptcp_subflow_tcp_sock (subflow); - -if (!ssk->sk_socket) -mptcp_sock_graft (ssk, newsock); - } - +mptcp_graph_subflows (newsk);mptcp_rps_record_subflows (msk);/* Do late cleanup for the first subflow as necessary. Also
net/mptcp/protocol.h +1 -0
@@ -755,6 +755,7 @@ mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated)return ret; } +void __mptcp_inherit_memcg (struct sock *sk,struct sock *ssk,gfp_t gfp);void __mptcp_inherit_cgrp_data (struct sock *sk,struct sock *ssk);int mptcp_is_enabled (const struct net *net);
net/mptcp/subflow.c +10 -0
@@ -1721,6 +1721,16 @@ int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local,return err; } +void __mptcp_inherit_memcg (struct sock *sk,struct sock *ssk,gfp_t gfp) +{ +/* Only if the msk has been accepted already (and not orphaned).*/ +if (!mem_cgroup_sockets_enabled || !sk->sk_socket) +return ; + +mem_cgroup_sk_inherit (sk, ssk); +__sk_charge (ssk, gfp); +} +void __mptcp_inherit_cgrp_data (struct sock *sk,struct sock *ssk) {#ifdef CONFIG_SOCK_CGROUP_DATA
Signature · 1 line
2.51.0
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): Critical: KMemLeak ❌
- 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/19194272317
Initiator: Matthieu Baerts (NGI0)
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/5451f3b28abd
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1021093
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-normalFor 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)
Re: [PATCH mptcp-net 1/3] net: factor-out _sk_charge() helper
In reply to Paolo AbeniHi Paolo,
Quoted 7 lines · Paolo Abeni
Move out of
__inet_accept()the code dealing charging newly accepted socket to memcg. MPTCP will soon use it to on a per subflow basis, in different contexts.No functional changes intended.
Signed-off-by Paolo Abeni <[email protected]>
This patch looks good to me.
Thanks,
-Geliang
Quoted 84 lines
---
include/net/sock.h | 2 ++
net/core/sock.c | 18 ++++++++++++++++++
net/ipv4/af_inet.c | 17 +----------------3 files changed, 21 insertions(+), 16 deletions(-)include/net/sock.h +0
-0@@ -1631,6 +1631,8 @@ static inline void sk_mem_uncharge(struct sock *sk, int size) sk_mem_reclaim(sk); }include/net/sock.h +2
-0+void __sk_charge (struct sock *sk,gfp_t gfp); +#if
IS_ENABLED(CONFIG_PROVE_LOCKING)&&IS_ENABLED(CONFIG_MODULES)static inline voidsk_owner_set(struct sock *sk, struct module *owner)
{net/core/sock.c +0
-0@@ -3448,6 +3448,24 @@ void __sk_mem_reclaim(struct sock *sk, int amount) } EXPORT_SYMBOL(__sk_mem_reclaim);net/core/sock.c +18
-0+void __sk_charge (struct sock *sk,gfp_t gfp) +{ +int amt; + + gfp |= __GFP_NOFAIL; +if (mem_cgroup_from_sk (sk)) { +/* The socket has not been accepted yet, no need +* to look at newsk->sk_wmem_queued. +*/ + amt =sk_mem_pages (sk->sk_forward_alloc + + atomic_read(&sk->sk_rmem_alloc)); +if (amt) +mem_cgroup_sk_charge (sk, amt, gfp); + } + +kmem_cache_charge (sk, gfp); +} +int
sk_set_peek_off(struct sock *sk, int val)
{
WRITE_ONCE(sk->sk_peek_off, val);net/ipv4/af_inet.c +0
-7@@ -756,23 +756,8 @@ EXPORT_SYMBOL(inet_stream_connect); void __inet_accept(struct socket *sock, struct socket *newsock,struct sock *newsk) { if (mem_cgroup_sockets_enabled) { -gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL; - mem_cgroup_sk_alloc(newsk); - -if (mem_cgroup_from_sk (newsk)) { -int amt; - -/* The socket has not been accepted yet, no need
net/ipv4/af_inet.c +0
-4-* to look at newsk->sk_wmem_queued. -*/ - amt =sk_mem_pages (newsk->sk_forward_alloc + - atomic_read(&newsk-Quoted 1 line
sk_rmem_alloc));net/ipv4/af_inet.c +0
-2-if (amt) - mem_cgroup_sk_charge(newsk, amt,gfp);
net/ipv4/af_inet.c +1
-3- } - -kmem_cache_charge (newsk, gfp); +__sk_charge (newsk,GFP_KERNEL );}
sock_rps_record_flow(newsk);
Re: [PATCH mptcp-net 2/3] mptcp: factor-out cgroup data inherit helper
In reply to Paolo AbeniHi Paolo,
Quoted 4 lines · Paolo Abeni
MPTCP will soon need the same functionality for passive sockets, factor them out in a common helper. No functional change intended.
Signed-off-by Paolo Abeni <[email protected]>
This patch looks good to me.
Thanks,
-Geliang
Quoted 62 lines
---
net/mptcp/protocol.h | 2 ++
net/mptcp/subflow.c | 20 ++++++++++++--------2 files changed, 14 insertions(+), 8 deletions(-)net/mptcp/protocol.h +0
-0@@ -755,6 +755,8 @@ mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated) return ret; }net/mptcp/protocol.h +2
-0+void __mptcp_inherit_cgrp_data (struct sock *sk,struct sock *ssk); +int
mptcp_is_enabled(const struct net *net);
unsigned intmptcp_get_add_addr_timeout(const struct net *net); intmptcp_is_checksum_enabled(const struct net *net);net/mptcp/subflow.c +0
-0@@ -1721,21 +1721,25 @@ int __mptcp_subflow_connect(struct sock *sk,const struct mptcp_pm_local *local, return err; }
-static voidmptcp_attach_cgroup(struct sock *parent, struct sock *child)
+void__mptcp_inherit_cgrp_data(struct sock *sk, struct sock *ssk){
#ifdefCONFIG_SOCK_CGROUP_DATAnet/mptcp/subflow.c +0
-1- struct sock_cgroup_data *parent_skcd = &parent-Quoted 1 line
sk_cgrp_data,net/mptcp/subflow.c +2
-1- *child_skcd = &child->sk_cgrp_data; +struct sock_cgroup_data *sk_cd = &sk->sk_cgrp_data, + *ssk_cd = &ssk->sk_cgrp_data;
/* only the additional subflows created by kworkers have to be modified */net/mptcp/subflow.c +5
-5-if (cgroup_id (sock_cgroup_ptr (parent_skcd)) != - cgroup_id(sock_cgroup_ptr(child_skcd))) { -cgroup_sk_free (child_skcd); - *child_skcd = *parent_skcd; -cgroup_sk_clone (child_skcd); +if (cgroup_id (sock_cgroup_ptr (sk_cd)) != + cgroup_id(sock_cgroup_ptr(ssk_cd))) { +cgroup_sk_free (ssk_cd); + *ssk_cd = *sk_cd; +cgroup_sk_clone (sk_cd);}
#endif /*CONFIG_SOCK_CGROUP_DATA*/
+}
+static voidmptcp_attach_cgroup(struct sock *parent, struct sock *child)net/mptcp/subflow.c +2
-0+{ +__mptcp_inherit_cgrp_data (parent, child);if (
mem_cgroup_sockets_enabled)mem_cgroup_sk_inherit(parent, child); }
Geliang Tang
reviewerHi Paolo,
Thanks for this fix.
Quoted 25 lines · Paolo Abeni
The passive sockets never got proper memcg accounting: the msk socket is associated with the memcg at accept time, but the passive subflows never got it right.
At accept time, traverse the subflows list and associate each of them with the msk memcg, and try to do the same at join completion time, if
the msk has been already accepted.Fixes cf7da0d66cc1 ("mptcp: Create SUBFLOW socket for incomingconnections")
Signed-off-by Paolo Abeni <[email protected]>---
net/mptcp/protocol.c | 36 ++++++++++++++++++++++++++----------net/mptcp/protocol.h| 1 +
net/mptcp/subflow.c | 10 ++++++++++3 files changed, 37 insertions(+), 10 deletions(-)net/mptcp/protocol.c +0
-0
net/mptcp/protocol.c static bool __mptcp_finish_join(struct mptcp_sock · line 964
Quoted 6 lines
net/mptcp/protocol.c +2
-0@@ -959,6 +959,8 @@ static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; mptcp_sockopt_sync_locked(msk, ssk); mptcp_stop_tout_timer(sk); +__mptcp_inherit_cgrp_data (sk, ssk); +__mptcp_inherit_memcg (sk, ssk,GFP_ATOMIC );
In mptcp_graph_subflows(), these two lines already have the mem_cgroup_from_sk() check (in the squash-to patch). It's unclear whether mem_cgroup_from_sk() check needs to be added here too.
Thanks,
-Geliang
Quoted 98 lines
net/mptcp/protocol.c +0
-0__mptcp_propagate_sndbuf(sk, ssk); __mptcp_propagate_rcvspace(sk, ssk);return true;
net/mptcp/protocol.c +0
-0@@ -4076,6 +4078,29 @@ static int mptcp_listen(struct socket *sock, int backlog) return err; }net/mptcp/protocol.c +12
-0+static void mptcp_graph_subflows (struct sock *sk) +{ +struct mptcp_subflow_context *subflow; +struct mptcp_sock *msk =mptcp_sk (sk); + +mptcp_for_each_subflow (msk, subflow) { +struct sock *ssk =mptcp_subflow_tcp_sock (subflow); +bool slow; + + slow =lock_sock_fast (ssk); + +/* set ssk->sk_socket of accept()ed flows to mptcp socket.
net/mptcp/protocol.c +1
-0+* This is needed so NOSPACE flag can be set from tcp stack.
net/mptcp/protocol.c +10
-0+*/ +if (!ssk->sk_socket) +mptcp_sock_graft (ssk, sk->sk_socket); + +__mptcp_inherit_cgrp_data (sk, ssk); +__mptcp_inherit_memcg (sk, ssk,GFP_KERNEL ); +unlock_sock_fast (ssk, slow); + } +} +static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
structproto_accept_arg*arg) {net/mptcp/protocol.c +0
-0@@ -4123,16 +4148,7 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, msk = mptcp_sk(newsk); msk->in_accept_queue = 0;net/mptcp/protocol.c +0
-1-/* set ssk->sk_socket of accept()ed flows to mptcp socket.
net/mptcp/protocol.c +0
-1-* This is needed so NOSPACE flag can be set from tcp stack.
net/mptcp/protocol.c +0
-3-*/ -mptcp_for_each_subflow (msk, subflow) { - struct sock *ssk =
mptcp_subflow_tcp_sock(subflow);net/mptcp/protocol.c +1
-5- -if (!ssk->sk_socket) -mptcp_sock_graft (ssk, newsock); - } - +mptcp_graph_subflows (newsk);
mptcp_rps_record_subflows(msk);
/* Do late cleanup for the first subflow as necessary. Alsonet/mptcp/protocol.h +0
-0@@ -755,6 +755,7 @@ mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated) return ret; }
+void__mptcp_inherit_memcg(struct sock *sk, struct sock *ssk, gfp_t gfp);
void__mptcp_inherit_cgrp_data(struct sock *sk, struct sock *ssk);
intmptcp_is_enabled(const struct net *net);net/mptcp/subflow.c +0
-0@@ -1721,6 +1721,16 @@ int __mptcp_subflow_connect(struct sock *sk,const struct mptcp_pm_local *local, return err; }
+void__mptcp_inherit_memcg(struct sock *sk, struct sock *ssk, gfp_t gfp)net/mptcp/subflow.c +2
-0+{ +/* Only if the msk has been accepted already (and not orphaned).*/
net/mptcp/subflow.c +7
-0+if (!mem_cgroup_sockets_enabled || !sk->sk_socket) +return ; + +mem_cgroup_sk_inherit (sk, ssk); +__sk_charge (ssk, gfp); +} +void
__mptcp_inherit_cgrp_data(struct sock *sk, struct sock *ssk){
#ifdefCONFIG_SOCK_CGROUP_DATA
Matthieu Baerts
maintainerHi Geliang,
Quoted 25 lines · Geliang Tang
Hi Paolo,
Thanks for this fix.
Quoted 20 lines · Paolo Abeni
The passive sockets never got proper memcg accounting: the msk socket is associated with the memcg at accept time, but the passive subflows never got it right.
At accept time, traverse the subflows list and associate each of them with the msk memcg, and try to do the same at join completion time, if
the msk has been already accepted.Fixes cf7da0d66cc1 ("mptcp: Create SUBFLOW socket for incomingconnections")
Signed-off-by Paolo Abeni <[email protected]>---
net/mptcp/protocol.c | 36 ++++++++++++++++++++++++++----------net/mptcp/protocol.h| 1 +
net/mptcp/subflow.c | 10 ++++++++++3 files changed, 37 insertions(+), 10 deletions(-)
Quoted 15 lines
net/mptcp/protocol.c static bool __mptcp_finish_join(struct mptcp_sock · line 964
Quoted 11 lines
net/mptcp/protocol.c +2
-0@@ -959,6 +959,8 @@ static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; mptcp_sockopt_sync_locked(msk, ssk); mptcp_stop_tout_timer(sk); +__mptcp_inherit_cgrp_data (sk, ssk); +__mptcp_inherit_memcg (sk, ssk,GFP_ATOMIC );In
mptcp_graph_subflows(), these two lines already have themem_cgroup_from_sk()check (in the squash-to patch). It's unclear whethermem_cgroup_from_sk()check needs to be added here too.
From what I understood, no need to check mem_cgroup_from_sk() here:
- __mptcp_inherit_cgrp_data() should not depend on that (I think)
- __mptcp_inherit_memcg() is doing that via mem_cgroup_sockets_enabled()
I *think* the squash-to patch needs some small further modifications, but this series looks good to me.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Hi Paolo,
Quoted 7 lines · Paolo Abeni
It turns out that the splat reported earlier is indeed due to an old bug/missing feature that hit hard on top of "mptcp: borrow forward memory from subflow", as we can borrow accounted memory from a socket not doing any accounting;)
It should land upstream before the above mentioned commit, ence the 'net' target, but it could as well be net-next material.
Thank you for these fixes!
They look good to me for 'net':
Quoted 8 lines
I think there is still a racing window to produce the splat reported in #597: if a subflow receive data while the msk is locked, and before the memcg is initialized on such subflow, it will push the data into the msk becklog, and later borrow will again try to grab accounted memory from non accounting ssk.
I have a tentative fix for that one but it's quite ugly and I'll keep it separate from this series.
Good idea!
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Matthieu Baerts
maintainerRe: [PATCH mptcp-net 1/3] net: factor-out _sk_charge() helper
In reply to Paolo AbeniHi Paolo,
Quoted 70 lines · Paolo Abeni
Move out of
__inet_accept()the code dealing charging newly accepted socket to memcg. MPTCP will soon use it to on a per subflow basis, in different contexts.No functional changes intended.
Signed-off-by Paolo Abeni <[email protected]>--- include/net/sock.h | 2 ++ net/core/sock.c | 18 ++++++++++++++++++ net/ipv4/af_inet.c | 17 +---------------- 3 files changed, 21 insertions(+), 16 deletions(-)include/net/sock.h +2
-0@@ -1631,6 +1631,8 @@ static inline void sk_mem_uncharge(struct sock *sk, int size)sk_mem_reclaim (sk); } +void __sk_charge (struct sock *sk,gfp_t gfp); +#if IS_ENABLED (CONFIG_PROVE_LOCKING ) &&IS_ENABLED (CONFIG_MODULES )static inline void sk_owner_set (struct sock *sk,struct module *owner) {net/core/sock.c +18
-0@@ -3448,6 +3448,24 @@ void __sk_mem_reclaim(struct sock *sk, int amount) }EXPORT_SYMBOL (__sk_mem_reclaim); +void __sk_charge (struct sock *sk,gfp_t gfp) +{ +int amt; + + gfp |= __GFP_NOFAIL; +if (mem_cgroup_from_sk (sk)) { +/* The socket has not been accepted yet, no need +* to look at newsk->sk_wmem_queued. +*/ + amt =sk_mem_pages (sk->sk_forward_alloc + +atomic_read (&sk->sk_rmem_alloc)); +if (amt) +mem_cgroup_sk_charge (sk, amt, gfp); + } + +kmem_cache_charge (sk, gfp); +} +int sk_set_peek_off (struct sock *sk,int val) {WRITE_ONCE (sk->sk_peek_off, val);net/ipv4/af_inet.c +0
-6@@ -756,23 +756,8 @@ EXPORT_SYMBOL(inet_stream_connect);void __inet_accept (struct socket *sock,struct socket *newsock,struct sock *newsk) {if (mem_cgroup_sockets_enabled) { -gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL; -mem_cgroup_sk_alloc (newsk); - -if (mem_cgroup_from_sk (newsk)) { -int amt; -
net/ipv4/af_inet.c EXPORT_SYMBOL(inet_stream_connect); · line 775
Quoted 10 lines
net/ipv4/af_inet.c +0
-10-/* The socket has not been accepted yet, no need -* to look at newsk->sk_wmem_queued. -*/ - amt =sk_mem_pages (newsk->sk_forward_alloc + -atomic_read (&newsk->sk_rmem_alloc)); -if (amt) -mem_cgroup_sk_charge (newsk, amt, gfp); - } - -kmem_cache_charge (newsk, gfp);
Mmh, this code has been moved from inet_csk_accept() to __inet_accept() in net-next only, see commit 4a997d49d92a ("tcp: Save lock_sock() for memcg in inet_csk_accept()."):
https://lore.kernel.org/[email protected]
Should we only apply these patches in our export branch, for net-next? If yes, then I guess we should remove the Fixes tag in patch 3/3.
net/ipv4/af_inet.c EXPORT_SYMBOL(inet_stream_connect); · line 763
Quoted 4 lines
net/ipv4/af_inet.c +1
-0+__sk_charge (newsk,GFP_KERNEL ); }sock_rps_record_flow (newsk);
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Paolo Abeni
maintainerRe: [PATCH mptcp-net 1/3] net: factor-out _sk_charge() helper
Quoted 86 lines · Matthieu Baerts
Quoted 70 lines · Paolo Abeni
Move out of
__inet_accept()the code dealing charging newly accepted socket to memcg. MPTCP will soon use it to on a per subflow basis, in different contexts.No functional changes intended.
Signed-off-by Paolo Abeni <[email protected]>--- include/net/sock.h | 2 ++ net/core/sock.c | 18 ++++++++++++++++++ net/ipv4/af_inet.c | 17 +---------------- 3 files changed, 21 insertions(+), 16 deletions(-)include/net/sock.h +2
-0@@ -1631,6 +1631,8 @@ static inline void sk_mem_uncharge(struct sock *sk, int size)sk_mem_reclaim (sk); } +void __sk_charge (struct sock *sk,gfp_t gfp); +#if IS_ENABLED (CONFIG_PROVE_LOCKING ) &&IS_ENABLED (CONFIG_MODULES )static inline void sk_owner_set (struct sock *sk,struct module *owner) {net/core/sock.c +18
-0@@ -3448,6 +3448,24 @@ void __sk_mem_reclaim(struct sock *sk, int amount) }EXPORT_SYMBOL (__sk_mem_reclaim); +void __sk_charge (struct sock *sk,gfp_t gfp) +{ +int amt; + + gfp |= __GFP_NOFAIL; +if (mem_cgroup_from_sk (sk)) { +/* The socket has not been accepted yet, no need +* to look at newsk->sk_wmem_queued. +*/ + amt =sk_mem_pages (sk->sk_forward_alloc + +atomic_read (&sk->sk_rmem_alloc)); +if (amt) +mem_cgroup_sk_charge (sk, amt, gfp); + } + +kmem_cache_charge (sk, gfp); +} +int sk_set_peek_off (struct sock *sk,int val) {WRITE_ONCE (sk->sk_peek_off, val);net/ipv4/af_inet.c +0
-6@@ -756,23 +756,8 @@ EXPORT_SYMBOL(inet_stream_connect);void __inet_accept (struct socket *sock,struct socket *newsock,struct sock *newsk) {if (mem_cgroup_sockets_enabled) { -gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL; -mem_cgroup_sk_alloc (newsk); - -if (mem_cgroup_from_sk (newsk)) { -int amt; -net/ipv4/af_inet.c EXPORT_SYMBOL(inet_stream_connect); · line 775
Quoted 10 lines
net/ipv4/af_inet.c +0
-10-/* The socket has not been accepted yet, no need -* to look at newsk->sk_wmem_queued. -*/ - amt =sk_mem_pages (newsk->sk_forward_alloc + -atomic_read (&newsk->sk_rmem_alloc)); -if (amt) -mem_cgroup_sk_charge (newsk, amt, gfp); - } - -kmem_cache_charge (newsk, gfp);Mmh, this code has been moved from
inet_csk_accept()to__inet_accept()in net-next only, see commit4a997d49d92a("tcp: Savelock_sock()for memcg ininet_csk_accept()."):
Quoted 1 line
Right you are!
Quoted 2 lines
Should we only apply these patches in our export branch, for net-next? If yes, then I guess we should remove the Fixes tag in patch 3/3.
Technically is a fix. Even prior to the backlog introduction bad thing could happen, as passive msk does not have memory by the subflow only accounted.
TCP-level OoO could potentially use a lot of system memory that will not be memaccounted.
Unfortunately it's not easy to have a clean net patch.
I suppose with can have this as net-next fixes (including the fixes tag in 3/3) as the change is invasive and the thing is broken since the beginning.
WDYT?
/P
Paolo Abeni
maintainerQuoted 42 lines · Matthieu Baerts
Quoted 25 lines · Geliang Tang
Hi Paolo,
Thanks for this fix.
Quoted 20 lines · Paolo Abeni
The passive sockets never got proper memcg accounting: the msk socket is associated with the memcg at accept time, but the passive subflows never got it right.
At accept time, traverse the subflows list and associate each of them with the msk memcg, and try to do the same at join completion time, if
the msk has been already accepted.Fixes cf7da0d66cc1 ("mptcp: Create SUBFLOW socket for incomingconnections")
Signed-off-by Paolo Abeni <[email protected]>---
net/mptcp/protocol.c | 36 ++++++++++++++++++++++++++----------net/mptcp/protocol.h| 1 +
net/mptcp/subflow.c | 10 ++++++++++3 files changed, 37 insertions(+), 10 deletions(-)Quoted 15 lines
net/mptcp/protocol.c static bool __mptcp_finish_join(struct mptcp_sock · line 964
Quoted 11 lines
net/mptcp/protocol.c +2
-0@@ -959,6 +959,8 @@ static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; mptcp_sockopt_sync_locked(msk, ssk); mptcp_stop_tout_timer(sk); +__mptcp_inherit_cgrp_data (sk, ssk); +__mptcp_inherit_memcg (sk, ssk,GFP_ATOMIC );In
mptcp_graph_subflows(), these two lines already have themem_cgroup_from_sk()check (in the squash-to patch). It's unclear whethermem_cgroup_from_sk()check needs to be added here too.
Quoted 4 lines
From what I understood, no need to check
mem_cgroup_from_sk()here:-
__mptcp_inherit_cgrp_data()should not depend on that (I think)
-__mptcp_inherit_memcg()is doing that viamem_cgroup_sockets_enabled()
The mem_cgroup_sockets_enabled() check is optional, mainly used a performance optimization, as it will cut the than branch via static branch.
To keep the code small I chose to avoid the optimization here, but still kept it in mptcp_graph_subflows() because there is much more stuff to be avoided (a whole loop with per subflow lock pair).
/P
Matthieu Baerts
maintainerRe: [PATCH mptcp-net 1/3] net: factor-out _sk_charge() helper
In reply to Paolo AbeniHi Paolo,
Thank you for your reply!
Quoted 8 lines · Paolo Abeni
Quoted 6 lines · Matthieu Baerts
Quoted 5 lines · Paolo Abeni
Move out of
__inet_accept()the code dealing charging newly accepted socket to memcg. MPTCP will soon use it to on a per subflow basis, in different contexts.No functional changes intended.
(...)
Quoted 44 lines
Quoted 30 lines
Quoted 15 lines
net/ipv4/af_inet.c +0
-6@@ -756,23 +756,8 @@ EXPORT_SYMBOL(inet_stream_connect);void __inet_accept (struct socket *sock,struct socket *newsock,struct sock *newsk) {if (mem_cgroup_sockets_enabled) { -gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL; -mem_cgroup_sk_alloc (newsk); - -if (mem_cgroup_from_sk (newsk)) { -int amt; -net/ipv4/af_inet.c EXPORT_SYMBOL(inet_stream_connect); · line 775
Quoted 10 lines
net/ipv4/af_inet.c +0
-10-/* The socket has not been accepted yet, no need -* to look at newsk->sk_wmem_queued. -*/ - amt =sk_mem_pages (newsk->sk_forward_alloc + -atomic_read (&newsk->sk_rmem_alloc)); -if (amt) -mem_cgroup_sk_charge (newsk, amt, gfp); - } - -kmem_cache_charge (newsk, gfp);Mmh, this code has been moved from
inet_csk_accept()to__inet_accept()in net-next only, see commit4a997d49d92a("tcp: Savelock_sock()for memcg ininet_csk_accept()."):Quoted 1 line
Right you are!
Quoted 2 lines
Should we only apply these patches in our export branch, for net-next? If yes, then I guess we should remove the Fixes tag in patch 3/3.
Technically is a fix. Even prior to the backlog introduction bad thing could happen, as passive msk does not have memory by the subflow only accounted.
TCP-level OoO could potentially use a lot of system memory that will not be memaccounted.
Quoted 7 lines
Unfortunately it's not easy to have a clean net patch.
I suppose with can have this as net-next fixes (including the fixes tag in 3/3) as the change is invasive and the thing is broken since the beginning.
WDYT?
It sounds good to me. When these patches will be in Linus tree, we can ask to backport these 3 patches from this series, plus commit 4a997d49d92a ("tcp: Save lock_sock() for memcg in inet_csk_accept()."), no?
If yes, do you think I can add a "Cc: stable", and explaining in the comments that it is because it depends on a patch that is only in net-next, and the fix is not urgent?
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Paolo Abeni
maintainerRe: [PATCH mptcp-net 1/3] net: factor-out _sk_charge() helper
Quoted 64 lines · Matthieu Baerts
Quoted 8 lines · Paolo Abeni
Quoted 6 lines · Matthieu Baerts
Quoted 5 lines · Paolo Abeni
Move out of
__inet_accept()the code dealing charging newly accepted socket to memcg. MPTCP will soon use it to on a per subflow basis, in different contexts.No functional changes intended.
(...)
Quoted 44 lines
Quoted 30 lines
Quoted 15 lines
net/ipv4/af_inet.c +0
-6@@ -756,23 +756,8 @@ EXPORT_SYMBOL(inet_stream_connect);void __inet_accept (struct socket *sock,struct socket *newsock,struct sock *newsk) {if (mem_cgroup_sockets_enabled) { -gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL; -mem_cgroup_sk_alloc (newsk); - -if (mem_cgroup_from_sk (newsk)) { -int amt; -net/ipv4/af_inet.c EXPORT_SYMBOL(inet_stream_connect); · line 775
Quoted 10 lines
net/ipv4/af_inet.c +0
-10-/* The socket has not been accepted yet, no need -* to look at newsk->sk_wmem_queued. -*/ - amt =sk_mem_pages (newsk->sk_forward_alloc + -atomic_read (&newsk->sk_rmem_alloc)); -if (amt) -mem_cgroup_sk_charge (newsk, amt, gfp); - } - -kmem_cache_charge (newsk, gfp);Mmh, this code has been moved from
inet_csk_accept()to__inet_accept()in net-next only, see commit4a997d49d92a("tcp: Savelock_sock()for memcg ininet_csk_accept()."):Quoted 1 line
Right you are!
Quoted 2 lines
Should we only apply these patches in our export branch, for net-next? If yes, then I guess we should remove the Fixes tag in patch 3/3.
Technically is a fix. Even prior to the backlog introduction bad thing could happen, as passive msk does not have memory by the subflow only accounted.
TCP-level OoO could potentially use a lot of system memory that will not be memaccounted.
Quoted 7 lines
Unfortunately it's not easy to have a clean net patch.
I suppose with can have this as net-next fixes (including the fixes tag in 3/3) as the change is invasive and the thing is broken since the beginning.
WDYT?
Quoted 7 lines
It sounds good to me. When these patches will be in Linus tree, we can ask to backport these 3 patches from this series, plus commit
4a997d49d92a("tcp: Savelock_sock()for memcg ininet_csk_accept()."), no?If yes, do you think I can add a "Cc: stable", and explaining in the comments that it is because it depends on a patch that is only in net-next, and the fix is not urgent?
You know I don't have all the fascination for stable;) My first take would be to avoid such tag. If you are willing to do the extra mile I guess that is a good plan. Alternatively you could just send the patch to stable after merge, with pre-req included.
/P
Paolo Abeni
maintainerQuoted 8 lines · Matthieu Baerts
Hi Paolo,
Quoted 5 lines · Paolo Abeni
It turns out that the splat reported earlier is indeed due to an old bug/missing feature that hit hard on top of "mptcp: borrow forward memory from subflow", as we can borrow accounted memory from a socket not doing any accounting;)
Quoted 8 lines
Quoted 2 lines
It should land upstream before the above mentioned commit, ence the 'net' target, but it could as well be net-next material.
Thank you for these fixes!
They look good to me for 'net':
Reviewed-by Matthieu Baerts (NGI0) <[email protected]>
If you don't have applied these patches yet, please wait a bit: I'll send a new revision including a minor delta to make 'Squash-to: "mptcp:
leverage the backlog for RX packet processing"' smaller/less ugly.
Thanks!
Paolo
Matthieu Baerts
maintainerRe: [PATCH mptcp-net 1/3] net: factor-out _sk_charge() helper
In reply to Paolo AbeniQuoted 69 lines · Paolo Abeni
Quoted 68 lines · Matthieu Baerts
Quoted 8 lines · Paolo Abeni
Quoted 6 lines · Matthieu Baerts
Quoted 5 lines · Paolo Abeni
Move out of
__inet_accept()the code dealing charging newly accepted socket to memcg. MPTCP will soon use it to on a per subflow basis, in different contexts.No functional changes intended.
(...)
Quoted 44 lines
Quoted 30 lines
Quoted 15 lines
net/ipv4/af_inet.c +0
-6@@ -756,23 +756,8 @@ EXPORT_SYMBOL(inet_stream_connect);void __inet_accept (struct socket *sock,struct socket *newsock,struct sock *newsk) {if (mem_cgroup_sockets_enabled) { -gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL; -mem_cgroup_sk_alloc (newsk); - -if (mem_cgroup_from_sk (newsk)) { -int amt; -net/ipv4/af_inet.c EXPORT_SYMBOL(inet_stream_connect); · line 775
Quoted 10 lines
net/ipv4/af_inet.c +0
-10-/* The socket has not been accepted yet, no need -* to look at newsk->sk_wmem_queued. -*/ - amt =sk_mem_pages (newsk->sk_forward_alloc + -atomic_read (&newsk->sk_rmem_alloc)); -if (amt) -mem_cgroup_sk_charge (newsk, amt, gfp); - } - -kmem_cache_charge (newsk, gfp);Mmh, this code has been moved from
inet_csk_accept()to__inet_accept()in net-next only, see commit4a997d49d92a("tcp: Savelock_sock()for memcg ininet_csk_accept()."):Quoted 1 line
Right you are!
Quoted 2 lines
Should we only apply these patches in our export branch, for net-next? If yes, then I guess we should remove the Fixes tag in patch 3/3.
Technically is a fix. Even prior to the backlog introduction bad thing could happen, as passive msk does not have memory by the subflow only accounted.
TCP-level OoO could potentially use a lot of system memory that will not be memaccounted.
Quoted 7 lines
Unfortunately it's not easy to have a clean net patch.
I suppose with can have this as net-next fixes (including the fixes tag in 3/3) as the change is invasive and the thing is broken since the beginning.
WDYT?
It sounds good to me. When these patches will be in Linus tree, we can ask to backport these 3 patches from this series, plus commit
4a997d49d92a("tcp: Savelock_sock()for memcg ininet_csk_accept()."), no?
Quoted 5 lines
Quoted 3 lines
If yes, do you think I can add a "Cc: stable", and explaining in the comments that it is because it depends on a patch that is only in net-next, and the fix is not urgent?
You know I don't have all the fascination for stable;)
I forgot :)
Quoted 4 lines
My first take
would be to avoid such tag. If you are willing to do the extra mile I guess that is a good plan. Alternatively you could just send the patch to stable after merge, with pre-req included.
Adding the "Cc: stable" allow me to forget about backporting it, except if there are conflicts! But I can add a reminder :)
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Matthieu Baerts
maintainerQuoted 14 lines · Paolo Abeni
Quoted 13 lines · Matthieu Baerts
Hi Paolo,
Quoted 7 lines · Paolo Abeni
It turns out that the splat reported earlier is indeed due to an old bug/missing feature that hit hard on top of "mptcp: borrow forward memory from subflow", as we can borrow accounted memory from a socket not doing any accounting;)
It should land upstream before the above mentioned commit, ence the 'net' target, but it could as well be net-next material.
Thank you for these fixes!
Quoted 7 lines
Quoted 3 lines
They look good to me for 'net':
Reviewed-by Matthieu Baerts (NGI0) <[email protected]>If you don't have applied these patches yet, please wait a bit: I'll send a new revision including a minor delta to make 'Squash-to: "mptcp:
leverage the backlog for RX packet processing"' smaller/less ugly.
I was going to apply them to help you not to have to carry many patches, but I can sure wait! :)
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Matthieu Baerts
maintainerHi Paolo,
Quoted 8 lines · Paolo Abeni
It turns out that the splat reported earlier is indeed due to an old bug/missing feature that hit hard on top of "mptcp: borrow forward memory from subflow", as we can borrow accounted memory from a socket not doing any accounting;)
It should land upstream before the above mentioned commit, ence the 'net' target, but it could as well be net-next material.
Quoted 8 lines
I think there is still a racing window to produce the splat reported in #597: if a subflow receive data while the msk is locked, and before the memcg is initialized on such subflow, it will push the data into the msk becklog, and later borrow will again try to grab accounted memory from non accounting ssk.
I have a tentative fix for that one but it's quite ugly and I'll keep it separate from this series.
As discussed on IRC, it is fine to already apply this series.
Now in our tree, feat. for net-next, before "mptcp: cleanup fallback data fin reception".
New patches for t/upstream:
- d4e154d9235e: net: factor-out _sk_charge() helper
(Note: I'm going to change the RvB tags to Acked-by: non MPTCP change)
- c39f3876137b: mptcp: factor-out cgroup data inherit helper
- ce0882b539e6: mptcp: fix memcg accounting for passive sockets
- Results: 83290f9ea1dc..8ecdad1578d9 (export)
Tests are now in progress:
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.