net-memcg: Gather memcg code under CONFIG_MEMCG.
Subsystems (8)
Kuniyuki Iwashima
maintainerv5 cover net-memcg: Gather memcg code under CONFIG_MEMCG.
This series converts most sk->sk_memcg access to helper functions under CONFIG_MEMCG and finally defines sk_memcg under CONFIG_MEMCG.
This is v5 of the series linked below but without core changes that decoupled memcg and global socket memory accounting.
I will defer the changes to a follow-up series that will use BPF to store a flag in sk->sk_memcg.
Overview of the series:
patch 1 is a trivial fix for MPTCP
patch 2 ~ 9 move sk->sk_memcg accesses to a single place patch 10 moves sk_memcg under CONFIG_MEMCG
Changes:
v5:
* Patch 1
* Don't use set_active_memcg() and restore
mem_cgroup_sk_inherit()
v4: https://lore.kernel.org/netdev/[email protected]/
* Drop patch 11 & 12
* Patch 1
* Use set_active_memcg()
v3: https://lore.kernel.org/netdev/[email protected]/
* Patch 12
* Fix build failrue for kTLS (include <net/proto_memory.h>)
v2: https://lore.kernel.org/netdev/[email protected]/
* Remove per-memcg knob
* Patch 11
* Set flag on sk_memcg based on memory.max
* Patch 12
* Add sk_should_enter_memory_pressure() and cover tcp_enter_memory_pressure() calls
* Update examples in changelog
v1: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (10):
mptcp: Fix up subflow's memcg when CONFIG_SOCK_CGROUP_DATA=n.
mptcp: Use tcp_under_memory_pressure() in mptcp_epollin_ready().
tcp: Simplify error path in inet_csk_accept().
net: Call trace_sock_exceed_buf_limit() for memcg failure with
SK_MEM_RECV.
net: Clean up __sk_mem_raise_allocated().
net-memcg: Introduce mem_cgroup_from_sk().
net-memcg: Introduce mem_cgroup_sk_enabled().
net-memcg: Pass struct sock to mem_cgroup_sk_(un)?charge().
net-memcg: Pass struct sock to mem_cgroup_sk_under_memory_pressure().
net: Define sk_memcg under CONFIG_MEMCG.
include/linux/memcontrol.h | 45 ++++++++++++++++++-------------- include/net/proto_memory.h | 4 +-- include/net/sock.h | 46 +++++++++++++++++++++++++++++++++ include/net/tcp.h | 4 +-- mm/memcontrol.c | 40 +++++++++++++++++++++------- net/core/sock.c | 38 ++++++++++++++------------- net/ipv4/inet_connection_sock.c | 19 +++++++------- net/ipv4/tcp_output.c | 5 ++-- net/mptcp/protocol.h | 4 +-- net/mptcp/subflow.c | 11 +++----- 10 files changed, 142 insertions(+), 74 deletions(-)
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Kuniyuki Iwashima
maintainerPATCH v5 1/10 mptcp: Fix up subflow's memcg when CONFIG_SOCK_CGROUP_DATA=n.
When sk_alloc() allocates a socket, mem_cgroup_sk_alloc() sets sk->sk_memcg based on the current task.
MPTCP subflow socket creation is triggered from userspace or an in-kernel worker.
In the latter case, sk->sk_memcg is not what we want. So, we fix it up from the parent socket's sk->sk_memcg in mptcp_attach_cgroup().
Although the code is placed under #ifdef CONFIG_MEMCG, it is buried under #ifdef CONFIG_SOCK_CGROUP_DATA.
The two configs are orthogonal. If CONFIG_MEMCG is enabled without CONFIG_SOCK_CGROUP_DATA, the subflow's memory usage is not charged correctly.
Let's move the code out of the wrong ifdef guard.
Note that sk->sk_memcg is freed in sk_prot_free() and the parent sk holds the refcnt of memcg->css here, so we don't need to use css_tryget().
--- include/linux/memcontrol.h | 6 ++++++ mm/memcontrol.c | 13 +++++++++++++ net/mptcp/subflow.c | 11 +++-------- 3 files changed, 22 insertions(+), 8 deletions(-)
include/linux/memcontrol.h +6 -0
@@ -1604,6 +1604,7 @@ extern struct static_key_false memcg_sockets_enabled_key;#define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)void mem_cgroup_sk_alloc (struct sock *sk);void mem_cgroup_sk_free (struct sock *sk); +void mem_cgroup_sk_inherit (const struct sock *sk,struct sock *newsk);#if BITS_PER_LONG <64 static inline void mem_cgroup_set_socket_pressure (struct mem_cgroup *memcg)
@@ -1661,6 +1662,11 @@ void reparent_shrinker_deferred(struct mem_cgroup *memcg);#define mem_cgroup_sockets_enabled 0static inline void mem_cgroup_sk_alloc (struct sock *sk) { };static inline void mem_cgroup_sk_free (struct sock *sk) { }; + +static inline void mem_cgroup_sk_inherit (const struct sock *sk,struct sock *newsk) +{ +} +static inline bool mem_cgroup_under_socket_pressure (struct mem_cgroup *memcg) {return false;
mm/memcontrol.c +13 -0
@@ -5024,6 +5024,19 @@ void mem_cgroup_sk_free(struct sock *sk)css_put (&sk->sk_memcg->css); } +void mem_cgroup_sk_inherit (const struct sock *sk,struct sock *newsk) +{ +if (sk->sk_memcg == newsk->sk_memcg) +return ; + +mem_cgroup_sk_free (newsk); + +if (sk->sk_memcg) +css_get (&sk->sk_memcg->css); + + newsk->sk_memcg = sk->sk_memcg; +} +/** * mem_cgroup_charge_skmem - charge socket memory * @memcg: memcg to charge
net/mptcp/subflow.c +3 -8
@@ -1717,19 +1717,14 @@ static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)/* 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))) { -#ifdef CONFIG_MEMCG -struct mem_cgroup *memcg = parent->sk_memcg; - -mem_cgroup_sk_free (child); -if (memcg &&css_tryget (&memcg->css)) - child->sk_memcg = memcg; -#endif /* CONFIG_MEMCG */ -cgroup_sk_free (child_skcd); *child_skcd = *parent_skcd;cgroup_sk_clone (child_skcd); }#endif /* CONFIG_SOCK_CGROUP_DATA */ + +if (mem_cgroup_sockets_enabled) +mem_cgroup_sk_inherit (parent, child); }static void mptcp_subflow_ops_override (struct sock *ssk)
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Re: [PATCH v5 net-next 01/10] mptcp: Fix up subflow's memcg when CONFIG_SOCK_CGROUP_DATA=n.
Quoted 18 lines · Kuniyuki Iwashima
When
sk_alloc()allocates a socket,mem_cgroup_sk_alloc()setssk->sk_memcgbased on the current task.MPTCP subflow socket creation is triggered from userspace or an in-kernel worker.
In the latter case,
sk->sk_memcgis not what we want. So, we fix it up from the parent socket'ssk->sk_memcginmptcp_attach_cgroup().Although the code is placed under #ifdef
CONFIG_MEMCG, it is buried under #ifdefCONFIG_SOCK_CGROUP_DATA.The two configs are orthogonal. If
CONFIG_MEMCGis enabled withoutCONFIG_SOCK_CGROUP_DATA, the subflow's memory usage is not charged correctly.Let's move the code out of the wrong ifdef guard.
Quoted 8 lines
Note that
sk->sk_memcgis freed insk_prot_free()and the parent sk holds the refcnt ofmemcg->csshere, so we don't need to usecss_tryget().Fixes 3764b0c5651e3 ("mptcp: attach subflow socket to parent cgroup")Signed-off-by Kuniyuki Iwashima <[email protected]>Reviewed-by Eric Dumazet <[email protected]>
Kuniyuki Iwashima
maintainerPATCH v5 2/10 mptcp: Use tcp_under_memory_pressure() in mptcp_epollin_ready().
In reply to Kuniyuki IwashimaSome conditions used in mptcp_epollin_ready() are the same as tcp_under_memory_pressure().
We will modify tcp_under_memory_pressure() in the later patch.
Let's use tcp_under_memory_pressure() instead.
--- net/mptcp/protocol.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
net/mptcp/protocol.h +1 -3
@@ -788,9 +788,7 @@ static inline bool mptcp_epollin_ready(const struct sock *sk)* as it can always coalesce them */ return (data_avail >= sk->sk_rcvlowat) || - (mem_cgroup_sockets_enabled && sk->sk_memcg && -mem_cgroup_under_socket_pressure (sk->sk_memcg)) || -READ_ONCE (tcp_memory_pressure); +tcp_under_memory_pressure (sk); }int mptcp_set_rcvlowat (struct sock *sk,int val);
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Re: [PATCH v5 net-next 02/10] mptcp: Use tcp_under_memory_pressure() in mptcp_epollin_ready().
Quoted 10 lines · Kuniyuki Iwashima
Some conditions used in
mptcp_epollin_ready()are the same astcp_under_memory_pressure().We will modify
tcp_under_memory_pressure()in the later patch.Let's use
tcp_under_memory_pressure()instead.Signed-off-by Kuniyuki Iwashima <[email protected]>Reviewed-by Eric Dumazet <[email protected]>Reviewed-by Matthieu Baerts (NGI0) <[email protected]>
Kuniyuki Iwashima
maintainerPATCH v5 3/10 tcp: Simplify error path in inet_csk_accept().
In reply to Kuniyuki IwashimaWhen an error occurs in inet_csk_accept(), what we should do is only call release_sock() and set the errno to arg->err.
But the path jumps to another label, which introduces unnecessary initialisation and tests for newsk.
Let's simplify the error path and remove the redundant NULL checks for newsk.
--- net/ipv4/inet_connection_sock.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
net/ipv4/inet_connection_sock.c +7 -8
@@ -706,9 +706,9 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)spin_unlock_bh (&queue->fastopenq.lock); } -out:release_sock (sk); -if (newsk && mem_cgroup_sockets_enabled) { + +if (mem_cgroup_sockets_enabled) {gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL;int amt =0 ;
@@ -732,18 +732,17 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)release_sock (newsk); } +if (req)reqsk_put (req); -if (newsk) -inet_init_csk_locks (newsk); - +inet_init_csk_locks (newsk);return newsk; + out_err: - newsk =NULL ; - req =NULL ; +release_sock (sk); arg->err = error; - goto out; +return NULL ; }EXPORT_SYMBOL (inet_csk_accept);
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Re: [PATCH v5 net-next 03/10] tcp: Simplify error path in inet_csk_accept().
Quoted 11 lines · Kuniyuki Iwashima
When an error occurs in
inet_csk_accept(), what we should do is only callrelease_sock()and set the errno toarg->err.But the path jumps to another label, which introduces unnecessary initialisation and tests for newsk.
Let's simplify the error path and remove the redundant NULL checks for newsk.
Signed-off-by Kuniyuki Iwashima <[email protected]>Reviewed-by Eric Dumazet <[email protected]>
Kuniyuki Iwashima
maintainerPATCH v5 4/10 net: Call trace_sock_exceed_buf_limit() for memcg failure with SK_MEM_RECV.
In reply to Kuniyuki IwashimaInitially, trace_sock_exceed_buf_limit() was invoked when __sk_mem_raise_allocated() failed due to the memcg limit or the global limit.
However, commit d6f19938eb031 ("net: expose sk wmem in sock_exceed_buf_limit tracepoint") somehow suppressed the event only when memcg failed to charge for SK_MEM_RECV, although the memcg failure for SK_MEM_SEND still triggers the event.
Let's restore the event for SK_MEM_RECV.
--- net/core/sock.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/core/sock.c +1 -2
@@ -3354,8 +3354,7 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind) } } -if (kind ==SK_MEM_SEND || (kind ==SK_MEM_RECV && charged)) -trace_sock_exceed_buf_limit (sk, prot, allocated, kind); +trace_sock_exceed_buf_limit (sk, prot, allocated, kind);sk_memory_allocated_sub (sk, amt);
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Re: [PATCH v5 net-next 04/10] net: Call trace_sock_exceed_buf_limit() for memcg failure with SK_MEM_RECV.
Quoted 9 lines · Kuniyuki Iwashima
Initially,
trace_sock_exceed_buf_limit()was invoked when__sk_mem_raise_allocated()failed due to the memcg limit or the global limit.However, commit
d6f19938eb031("net: expose sk wmem insock_exceed_buf_limittracepoint") somehow suppressed the event only when memcg failed to charge forSK_MEM_RECV, although the memcg failure forSK_MEM_SENDstill triggers the event.
Quoted 4 lines
Let's restore the event for
SK_MEM_RECV.Signed-off-by Kuniyuki Iwashima <[email protected]>Reviewed-by Eric Dumazet <[email protected]>
Kuniyuki Iwashima
maintainerPATCH v5 5/10 net: Clean up __sk_mem_raise_allocated().
In reply to Kuniyuki IwashimaIn __sk_mem_raise_allocated(), charged is initialised as true due to the weird condition removed in the previous patch.
It makes the variable unreliable by itself, so we have to check another variable, memcg, in advance.
Also, we will factorise the common check below for memcg later.
if (mem_cgroup_sockets_enabled && sk->sk_memcg)
As a prep, let's initialise charged as false and memcg as NULL.
--- net/core/sock.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
net/core/sock.c +5 -4
@@ -3263,15 +3263,16 @@ EXPORT_SYMBOL(sk_wait_data);*/ int __sk_mem_raise_allocated (struct sock *sk,int size,int amt,int kind) { -struct mem_cgroup *memcg = mem_cgroup_sockets_enabled ? sk->sk_memcg :NULL ;struct proto *prot = sk->sk_prot; -bool charged = true; +struct mem_cgroup *memcg =NULL ; +bool charged = false;long allocated;sk_memory_allocated_add (sk, amt); allocated =sk_memory_allocated (sk); -if (memcg) { +if (mem_cgroup_sockets_enabled && sk->sk_memcg) { + memcg = sk->sk_memcg; charged =mem_cgroup_charge_skmem (memcg, amt,gfp_memcg_charge ());if (!charged) goto suppress_allocation;
@@ -3358,7 +3359,7 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)sk_memory_allocated_sub (sk, amt); -if (memcg && charged) +if (charged)mem_cgroup_uncharge_skmem (memcg, amt);return 0 ;
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Re: [PATCH v5 net-next 05/10] net: Clean up __sk_mem_raise_allocated().
Quoted 6 lines · Kuniyuki Iwashima
In
__sk_mem_raise_allocated(), charged is initialised as true due to the weird condition removed in the previous patch.It makes the variable unreliable by itself, so we have to check another variable, memcg, in advance.
Quoted 8 lines
Also, we will factorise the common check below for memcg later.
if (mem_cgroup_sockets_enabled && sk->sk_memcg)As a prep, let's initialise charged as false and memcg as NULL.
Signed-off-by Kuniyuki Iwashima <[email protected]>Reviewed-by Eric Dumazet <[email protected]>
Kuniyuki Iwashima
maintainerPATCH v5 6/10 net-memcg: Introduce mem_cgroup_from_sk().
In reply to Kuniyuki IwashimaWe will store a flag in the lowest bit of sk->sk_memcg.
Then, directly dereferencing sk->sk_memcg will be illegal, and we do not want to allow touching the raw sk->sk_memcg in many places.
Let's introduce mem_cgroup_from_sk().
Other places accessing the raw sk->sk_memcg will be converted later.
Note that we cannot define the helper as an inline function in memcontrol.h as we cannot access any fields of struct sock there due to circular dependency, so it is placed in sock.h.
--- include/net/sock.h | 12 ++++++++++++ mm/memcontrol.c | 13 +++++++++---- net/ipv4/inet_connection_sock.c | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-)
include/net/sock.h +12 -0
@@ -2594,6 +2594,18 @@ static inline gfp_t gfp_memcg_charge(void)return in_softirq () ?GFP_ATOMIC :GFP_KERNEL ; } +#ifdef CONFIG_MEMCG +static inline struct mem_cgroup *mem_cgroup_from_sk (const struct sock *sk) +{ +return sk->sk_memcg; +} +#else +static inline struct mem_cgroup *mem_cgroup_from_sk (const struct sock *sk) +{ +return NULL ; +} +#endif +static inline long sock_rcvtimeo (const struct sock *sk,bool noblock) {return noblock ?0 :READ_ONCE (sk->sk_rcvtimeo);
mm/memcontrol.c +9 -4
@@ -5020,19 +5020,24 @@ void mem_cgroup_sk_alloc(struct sock *sk)void mem_cgroup_sk_free (struct sock *sk) { -if (sk->sk_memcg) -css_put (&sk->sk_memcg->css); +struct mem_cgroup *memcg =mem_cgroup_from_sk (sk); + +if (memcg) +css_put (&memcg->css); }void mem_cgroup_sk_inherit (const struct sock *sk,struct sock *newsk) { +struct mem_cgroup *memcg; +if (sk->sk_memcg == newsk->sk_memcg)return ;mem_cgroup_sk_free (newsk); -if (sk->sk_memcg) -css_get (&sk->sk_memcg->css); + memcg =mem_cgroup_from_sk (sk); +if (memcg) +css_get (&memcg->css); newsk->sk_memcg = sk->sk_memcg; }
net/ipv4/inet_connection_sock.c +1 -1
@@ -718,7 +718,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)lock_sock (newsk);mem_cgroup_sk_alloc (newsk); -if (newsk->sk_memcg) { +if (mem_cgroup_from_sk (newsk)) {/* The socket has not been accepted yet, no need * to look at newsk->sk_wmem_queued. */
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Kuniyuki Iwashima
maintainerPATCH v5 7/10 net-memcg: Introduce mem_cgroup_sk_enabled().
In reply to Kuniyuki IwashimaThe socket memcg feature is enabled by a static key and only works for non-root cgroup.
We check both conditions in many places.
Let's factorise it as a helper function.
--- include/net/proto_memory.h | 2 +- include/net/sock.h | 10 ++++++++++ include/net/tcp.h | 2 +- net/core/sock.c | 6 +++--- net/ipv4/tcp_output.c | 2 +- 5 files changed, 16 insertions(+), 6 deletions(-)
include/net/proto_memory.h +1 -1
@@ -31,7 +31,7 @@ static inline bool sk_under_memory_pressure(const struct sock *sk)if (!sk->sk_prot->memory_pressure)return false; -if (mem_cgroup_sockets_enabled && sk->sk_memcg && +if (mem_cgroup_sk_enabled (sk) &&mem_cgroup_under_socket_pressure (sk->sk_memcg))return true;
include/net/sock.h +10 -0
@@ -2599,11 +2599,21 @@ static inline struct mem_cgroup *mem_cgroup_from_sk(const struct sock *sk) {return sk->sk_memcg; } + +static inline bool mem_cgroup_sk_enabled (const struct sock *sk) +{ +return mem_cgroup_sockets_enabled &&mem_cgroup_from_sk (sk); +}#else static inline struct mem_cgroup *mem_cgroup_from_sk (const struct sock *sk) {return NULL ; } + +static inline bool mem_cgroup_sk_enabled (const struct sock *sk) +{ +return false; +}#endif static inline long sock_rcvtimeo (const struct sock *sk,bool noblock)
include/net/tcp.h +1 -1
@@ -275,7 +275,7 @@ extern unsigned long tcp_memory_pressure;/* optimized version of sk_under_memory_pressure() for TCP sockets */ static inline bool tcp_under_memory_pressure (const struct sock *sk) { -if (mem_cgroup_sockets_enabled && sk->sk_memcg && +if (mem_cgroup_sk_enabled (sk) &&mem_cgroup_under_socket_pressure (sk->sk_memcg))return true;
net/core/sock.c +3 -3
@@ -1032,7 +1032,7 @@ static int sock_reserve_memory(struct sock *sk, int bytes)bool charged;int pages; -if (!mem_cgroup_sockets_enabled || !sk->sk_memcg || !sk_has_account (sk)) +if (!mem_cgroup_sk_enabled (sk) || !sk_has_account (sk))return -EOPNOTSUPP ;if (!bytes)
@@ -3271,7 +3271,7 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)sk_memory_allocated_add (sk, amt); allocated =sk_memory_allocated (sk); -if (mem_cgroup_sockets_enabled && sk->sk_memcg) { +if (mem_cgroup_sk_enabled (sk)) { memcg = sk->sk_memcg; charged =mem_cgroup_charge_skmem (memcg, amt,gfp_memcg_charge ());if (!charged)
@@ -3398,7 +3398,7 @@ void __sk_mem_reduce_allocated(struct sock *sk, int amount) {sk_memory_allocated_sub (sk, amount); -if (mem_cgroup_sockets_enabled && sk->sk_memcg) +if (mem_cgroup_sk_enabled (sk))mem_cgroup_uncharge_skmem (sk->sk_memcg, amount); if (sk_under_global_memory_pressure(sk) &&
net/ipv4/tcp_output.c +1 -1
@@ -3578,7 +3578,7 @@ void sk_forced_mem_schedule(struct sock *sk, int size)sk_forward_alloc_add (sk, amt <<PAGE_SHIFT );sk_memory_allocated_add (sk, amt); -if (mem_cgroup_sockets_enabled && sk->sk_memcg) +if (mem_cgroup_sk_enabled (sk))mem_cgroup_charge_skmem (sk->sk_memcg, amt,gfp_memcg_charge () | __GFP_NOFAIL); }
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Kuniyuki Iwashima
maintainerPATCH v5 8/10 net-memcg: Pass struct sock to mem_cgroup_sk_(un)?charge().
In reply to Kuniyuki IwashimaWe will store a flag in the lowest bit of sk->sk_memcg.
Then, we cannot pass the raw pointer to mem_cgroup_charge_skmem() and mem_cgroup_uncharge_skmem().
Let's pass struct sock to the functions.
While at it, they are renamed to match other functions starting with mem_cgroup_sk_.
--- include/linux/memcontrol.h | 29 ++++++++++++++++++++++++----- mm/memcontrol.c | 18 +++++++++++------- net/core/sock.c | 24 +++++++++++------------- net/ipv4/inet_connection_sock.c | 2 +- net/ipv4/tcp_output.c | 3 +-- 5 files changed, 48 insertions(+), 28 deletions(-)
include/linux/memcontrol.h +24 -5
@@ -1596,15 +1596,16 @@ static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)#endif /* CONFIG_CGROUP_WRITEBACK */ struct sock ; -bool mem_cgroup_charge_skmem (struct mem_cgroup *memcg,unsigned int nr_pages, -gfp_t gfp_mask); -void mem_cgroup_uncharge_skmem (struct mem_cgroup *memcg,unsigned int nr_pages);#ifdef CONFIG_MEMCG extern struct static_key_false memcg_sockets_enabled_key;#define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key) +void mem_cgroup_sk_alloc (struct sock *sk);void mem_cgroup_sk_free (struct sock *sk);void mem_cgroup_sk_inherit (const struct sock *sk,struct sock *newsk); +bool mem_cgroup_sk_charge (const struct sock *sk,unsigned int nr_pages, +gfp_t gfp_mask); +void mem_cgroup_sk_uncharge (const struct sock *sk,unsigned int nr_pages);#if BITS_PER_LONG <64 static inline void mem_cgroup_set_socket_pressure (struct mem_cgroup *memcg)
@@ -1660,13 +1661,31 @@ void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);void reparent_shrinker_deferred (struct mem_cgroup *memcg);#else #define mem_cgroup_sockets_enabled 0 -static inline void mem_cgroup_sk_alloc (struct sock *sk) { }; -static inline void mem_cgroup_sk_free (struct sock *sk) { }; + +static inline void mem_cgroup_sk_alloc (struct sock *sk) +{ +} + +static inline void mem_cgroup_sk_free (struct sock *sk) +{ +}static inline void mem_cgroup_sk_inherit (const struct sock *sk,struct sock *newsk) { } +static inline bool mem_cgroup_sk_charge (const struct sock *sk, +unsigned int nr_pages, +gfp_t gfp_mask) +{ +return false; +} + +static inline void mem_cgroup_sk_uncharge (const struct sock *sk, +unsigned int nr_pages) +{ +} +static inline bool mem_cgroup_under_socket_pressure (struct mem_cgroup *memcg) {return false;
mm/memcontrol.c +11 -7
@@ -5043,17 +5043,19 @@ void mem_cgroup_sk_inherit(const struct sock *sk, struct sock *newsk) }/** -* mem_cgroup_charge_skmem - charge socket memory -* @memcg: memcg to charge +* mem_cgroup_sk_charge - charge socket memory +* @sk: socket in memcg to charge * @nr_pages: number of pages to charge * @gfp_mask: reclaim mode * * Charges @nr_pages to @memcg. Returns %true if the charge fit within * @memcg's configured limit, %false if it doesn't. */ -bool mem_cgroup_charge_skmem (struct mem_cgroup *memcg,unsigned int nr_pages, -gfp_t gfp_mask) +bool mem_cgroup_sk_charge (const struct sock *sk,unsigned int nr_pages, +gfp_t gfp_mask) { +struct mem_cgroup *memcg =mem_cgroup_from_sk (sk); +if (!cgroup_subsys_on_dfl (memory_cgrp_subsys))return memcg1_charge_skmem (memcg, nr_pages, gfp_mask);
@@ -5066,12 +5068,14 @@ bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, }/** -* mem_cgroup_uncharge_skmem - uncharge socket memory -* @memcg: memcg to uncharge +* mem_cgroup_sk_uncharge - uncharge socket memory +* @sk: socket in memcg to uncharge * @nr_pages: number of pages to uncharge */ -void mem_cgroup_uncharge_skmem (struct mem_cgroup *memcg,unsigned int nr_pages) +void mem_cgroup_sk_uncharge (const struct sock *sk,unsigned int nr_pages) { +struct mem_cgroup *memcg =mem_cgroup_from_sk (sk); +if (!cgroup_subsys_on_dfl (memory_cgrp_subsys)) {memcg1_uncharge_skmem (memcg, nr_pages);return ;
net/core/sock.c +11 -13
@@ -1041,8 +1041,8 @@ static int sock_reserve_memory(struct sock *sk, int bytes) pages =sk_mem_pages (bytes);/* pre-charge to memcg */ - charged =mem_cgroup_charge_skmem (sk->sk_memcg, pages, -GFP_KERNEL | __GFP_RETRY_MAYFAIL); + charged =mem_cgroup_sk_charge (sk, pages, +GFP_KERNEL | __GFP_RETRY_MAYFAIL);if (!charged)return -ENOMEM ;
@@ -1054,7 +1054,7 @@ static int sock_reserve_memory(struct sock *sk, int bytes)*/ if (allocated >sk_prot_mem_limits (sk,1 )) {sk_memory_allocated_sub (sk, pages); -mem_cgroup_uncharge_skmem (sk->sk_memcg, pages); +mem_cgroup_sk_uncharge (sk, pages);return -ENOMEM ; }sk_forward_alloc_add (sk, pages <<PAGE_SHIFT );
@@ -3263,17 +3263,16 @@ EXPORT_SYMBOL(sk_wait_data);*/ int __sk_mem_raise_allocated (struct sock *sk,int size,int amt,int kind) { +bool memcg_enabled = false, charged = false;struct proto *prot = sk->sk_prot; -struct mem_cgroup *memcg =NULL ; -bool charged = false;long allocated;sk_memory_allocated_add (sk, amt); allocated =sk_memory_allocated (sk);if (mem_cgroup_sk_enabled (sk)) { - memcg = sk->sk_memcg; - charged =mem_cgroup_charge_skmem (memcg, amt,gfp_memcg_charge ()); + memcg_enabled = true; + charged =mem_cgroup_sk_charge (sk, amt,gfp_memcg_charge ());if (!charged) goto suppress_allocation; }
@@ -3347,10 +3346,9 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)*/ if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) {/* Force charge with __GFP_NOFAIL */ -if (memcg && !charged) { -mem_cgroup_charge_skmem (memcg, amt, -gfp_memcg_charge () | __GFP_NOFAIL); - } +if (memcg_enabled && !charged) +mem_cgroup_sk_charge (sk, amt, +gfp_memcg_charge () | __GFP_NOFAIL);return 1 ; } }
@@ -3360,7 +3358,7 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)sk_memory_allocated_sub (sk, amt);if (charged) -mem_cgroup_uncharge_skmem (memcg, amt); +mem_cgroup_sk_uncharge (sk, amt);return 0 ; }
@@ -3399,7 +3397,7 @@ void __sk_mem_reduce_allocated(struct sock *sk, int amount)sk_memory_allocated_sub (sk, amount);if (mem_cgroup_sk_enabled (sk)) -mem_cgroup_uncharge_skmem (sk->sk_memcg, amount); +mem_cgroup_sk_uncharge (sk, amount);if (sk_under_global_memory_pressure (sk) && (sk_memory_allocated (sk) <sk_prot_mem_limits (sk,0 )))
net/ipv4/inet_connection_sock.c +1 -1
@@ -727,7 +727,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg) }if (amt) -mem_cgroup_charge_skmem (newsk->sk_memcg, amt, gfp); +mem_cgroup_sk_charge (newsk, amt, gfp);kmem_cache_charge (newsk, gfp);release_sock (newsk);
net/ipv4/tcp_output.c +1 -2
@@ -3579,8 +3579,7 @@ void sk_forced_mem_schedule(struct sock *sk, int size)sk_memory_allocated_add (sk, amt);if (mem_cgroup_sk_enabled (sk)) -mem_cgroup_charge_skmem (sk->sk_memcg, amt, -gfp_memcg_charge () | __GFP_NOFAIL); +mem_cgroup_sk_charge (sk, amt,gfp_memcg_charge () | __GFP_NOFAIL); }/* Send a FIN. The caller locks the socket for us.
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Kuniyuki Iwashima
maintainerPATCH v5 9/10 net-memcg: Pass struct sock to mem_cgroup_sk_under_memory_pressure().
In reply to Kuniyuki IwashimaWe will store a flag in the lowest bit of sk->sk_memcg.
Then, we cannot pass the raw pointer to mem_cgroup_under_socket_pressure().
Let's pass struct sock to it and rename the function to match other functions starting with mem_cgroup_sk_.
Note that the helper is moved to sock.h to use mem_cgroup_from_sk().
--- include/linux/memcontrol.h | 18 ------------------ include/net/proto_memory.h | 2 +- include/net/sock.h | 22 ++++++++++++++++++++++ include/net/tcp.h | 2 +- 4 files changed, 24 insertions(+), 20 deletions(-)
include/linux/memcontrol.h +0 -18
@@ -1642,19 +1642,6 @@ static inline u64 mem_cgroup_get_socket_pressure(struct mem_cgroup *memcg) }#endif -static inline bool mem_cgroup_under_socket_pressure (struct mem_cgroup *memcg) -{ -#ifdef CONFIG_MEMCG_V1 -if (!cgroup_subsys_on_dfl (memory_cgrp_subsys)) -return !!memcg->tcpmem_pressure; -#endif /* CONFIG_MEMCG_V1 */ -do { -if (time_before64 (get_jiffies_64 (),mem_cgroup_get_socket_pressure (memcg))) -return true; - }while ((memcg =parent_mem_cgroup (memcg))); -return false; -} -int alloc_shrinker_info (struct mem_cgroup *memcg);void free_shrinker_info (struct mem_cgroup *memcg);void set_shrinker_bit (struct mem_cgroup *memcg,int nid,int shrinker_id);
@@ -1686,11 +1673,6 @@ static inline void mem_cgroup_sk_uncharge(const struct sock *sk, { } -static inline bool mem_cgroup_under_socket_pressure (struct mem_cgroup *memcg) -{ -return false; -} -static inline void set_shrinker_bit (struct mem_cgroup *memcg,int nid,int shrinker_id) {
include/net/proto_memory.h +1 -1
@@ -32,7 +32,7 @@ static inline bool sk_under_memory_pressure(const struct sock *sk)return false;if (mem_cgroup_sk_enabled (sk) && -mem_cgroup_under_socket_pressure (sk->sk_memcg)) +mem_cgroup_sk_under_memory_pressure (sk))return true;return !!READ_ONCE (*sk->sk_prot->memory_pressure);
include/net/sock.h +22 -0
@@ -2604,6 +2604,23 @@ static inline bool mem_cgroup_sk_enabled(const struct sock *sk) {return mem_cgroup_sockets_enabled &&mem_cgroup_from_sk (sk); } + +static inline bool mem_cgroup_sk_under_memory_pressure (const struct sock *sk) +{ +struct mem_cgroup *memcg =mem_cgroup_from_sk (sk); + +#ifdef CONFIG_MEMCG_V1 +if (!cgroup_subsys_on_dfl (memory_cgrp_subsys)) +return !!memcg->tcpmem_pressure; +#endif /* CONFIG_MEMCG_V1 */ + +do { +if (time_before64 (get_jiffies_64 (),mem_cgroup_get_socket_pressure (memcg))) +return true; + }while ((memcg =parent_mem_cgroup (memcg))); + +return false; +}#else static inline struct mem_cgroup *mem_cgroup_from_sk (const struct sock *sk) {
@@ -2614,6 +2631,11 @@ static inline bool mem_cgroup_sk_enabled(const struct sock *sk) {return false; } + +static inline bool mem_cgroup_sk_under_memory_pressure (const struct sock *sk) +{ +return false; +}#endif static inline long sock_rcvtimeo (const struct sock *sk,bool noblock)
include/net/tcp.h +1 -1
@@ -276,7 +276,7 @@ extern unsigned long tcp_memory_pressure;static inline bool tcp_under_memory_pressure (const struct sock *sk) {if (mem_cgroup_sk_enabled (sk) && -mem_cgroup_under_socket_pressure (sk->sk_memcg)) +mem_cgroup_sk_under_memory_pressure (sk))return true;return READ_ONCE (tcp_memory_pressure);
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Jakub Kicinski
maintainerRe: [PATCH v5 net-next 09/10] net-memcg: Pass struct sock to mem_cgroup_sk_under_memory_pressure().
Quoted 4 lines · On Fri, 15 Aug 2025 20:16:17 +0000 Kuniyuki Iwashima wrote:
We will store a flag in the lowest bit of
sk->sk_memcg.Then, we cannot pass the raw pointer to
mem_cgroup_under_socket_pressure().
Quoted 8 lines
Let's pass struct sock to it and rename the function to match other functions starting with
mem_cgroup_sk_.Note that the helper is moved to
sock.hto usemem_cgroup_from_sk().Signed-off-by Kuniyuki Iwashima <[email protected]>Reviewed-by Eric Dumazet <[email protected]>Acked-by Roman Gushchin <[email protected]>
Hi Shakeel, looks like you acked every single patch here but this one. Is this intentional? :)
Re: [PATCH v5 net-next 09/10] net-memcg: Pass struct sock to mem_cgroup_sk_under_memory_pressure().
Quoted 8 lines · Jakub Kicinski
Quoted 7 lines · On Fri, 15 Aug 2025 20:16:17 +0000 Kuniyuki Iwashima wrote:
We will store a flag in the lowest bit of
sk->sk_memcg.Then, we cannot pass the raw pointer to
mem_cgroup_under_socket_pressure().Let's pass struct sock to it and rename the function to match other functions starting with
mem_cgroup_sk_.
Quoted 8 lines
Quoted 5 lines
Note that the helper is moved to
sock.hto usemem_cgroup_from_sk().Signed-off-by Kuniyuki Iwashima <[email protected]>Reviewed-by Eric Dumazet <[email protected]>Acked-by Roman Gushchin <[email protected]>Hi Shakeel, looks like you acked every single patch here but this one. Is this intentional? :)
Nopes, sorry I missed it somehow. Please add:
Kuniyuki Iwashima
maintainerPATCH v5 10/10 net: Define sk_memcg under CONFIG_MEMCG.
In reply to Kuniyuki IwashimaExcept for sk_clone_lock(), all accesses to sk->sk_memcg is done under CONFIG_MEMCG.
As a bonus, let's define sk->sk_memcg under CONFIG_MEMCG.
--- include/net/sock.h | 2 ++ net/core/sock.c | 4 ++++ 2 files changed, 6 insertions(+)
include/net/sock.h +2 -0
@@ -443,7 +443,9 @@ struct sock {__cacheline_group_begin (sock_read_rxtx);int sk_err;struct socket *sk_socket; +#ifdef CONFIG_MEMCG struct mem_cgroup *sk_memcg; +#endif #ifdef CONFIG_XFRM struct xfrm_policy __rcu *sk_policy[2 ];#endif
net/core/sock.c +4 -0
@@ -2512,8 +2512,10 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)sock_reset_flag (newsk,SOCK_DONE ); +#ifdef CONFIG_MEMCG /* sk->sk_memcg will be populated at accept() time */ newsk->sk_memcg =NULL ; +#endif cgroup_sk_clone (&newsk->sk_cgrp_data);
@@ -4452,7 +4454,9 @@ static int __init sock_struct_check(void) CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rxtx, sk_err); CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rxtx, sk_socket); +#ifdef CONFIG_MEMCG CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rxtx, sk_memcg); +#endif CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rxtx, sk_lock); CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rxtx, sk_reserved_mem);
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
Hi Kuniyuki,
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: 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/16998832310
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/269f6acfebcd
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=992046
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)
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Fri, 15 Aug 2025 20:16:08 +0000 you wrote:
This series converts most
sk->sk_memcgaccess to helper functions underCONFIG_MEMCGand finally definessk_memcgunderCONFIG_MEMCG.This is v5 of the series linked below but without core changes that decoupled memcg and global socket memory accounting.
I will defer the changes to a follow-up series that will use BPF to store a flag in
sk->sk_memcg.[...]
Here is the summary with links:
- [v5,net-next,01/10] mptcp: Fix up subflow's memcg when CONFIG_SOCK_CGROUP_DATA=n.
https://git.kernel.org/netdev/net-next/c/68889dfd547b - [v5,net-next,02/10] mptcp: Use tcp_under_memory_pressure() in mptcp_epollin_ready(). https://git.kernel.org/netdev/net-next/c/1068b48ed108 - [v5,net-next,03/10] tcp: Simplify error path in inet_csk_accept(). https://git.kernel.org/netdev/net-next/c/e2afa83296bb - [v5,net-next,04/10] net: Call trace_sock_exceed_buf_limit() for memcg failure with SK_MEM_RECV. https://git.kernel.org/netdev/net-next/c/9d85c565a7b7 - [v5,net-next,05/10] net: Clean up __sk_mem_raise_allocated(). https://git.kernel.org/netdev/net-next/c/bd4aa2337374 - [v5,net-next,06/10] net-memcg: Introduce mem_cgroup_from_sk(). https://git.kernel.org/netdev/net-next/c/f7161b234f2e - [v5,net-next,07/10] net-memcg: Introduce mem_cgroup_sk_enabled(). https://git.kernel.org/netdev/net-next/c/43049b0db038 - [v5,net-next,08/10] net-memcg: Pass struct sock to mem_cgroup_sk_(un)?charge(). https://git.kernel.org/netdev/net-next/c/bb178c6bc085 - [v5,net-next,09/10] net-memcg: Pass struct sock to mem_cgroup_sk_under_memory_pressure(). https://git.kernel.org/netdev/net-next/c/b2ffd10cddde - [v5,net-next,10/10] net: Define sk_memcg under CONFIG_MEMCG. https://git.kernel.org/netdev/net-next/c/bf64002c94fc
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html