memcg: add charging of already allocated slab objects
Subsystems (6)
PATCH v2 1/1 memcg: add charging of already allocated slab objects
At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the accept() time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.
To measure the performance impact of this change, tcp_crr is used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.
The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6
Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
base (throughput) with-patchtcp_crr 14545 (+- 80) 14463 (+- 56)
It seems like the performance impact is within the noise.
---
v1: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Correctly handle large allocations which bypass slab
- Rearrange code to avoid compilation errors for !CONFIG_MEMCG builds
RFC: https://lore.kernel.org/all/[email protected]/
Changes since the RFC:
- Added check for already charged slab objects.
- Added performance results from neper's tcp_crr
include/linux/slab.h | 1 + mm/slub.c | 51 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 3 files changed, 55 insertions(+), 2 deletions(-)
include/linux/slab.h +1 -0
@@ -547,6 +547,7 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc;#define kmem_cache_alloc_lru (...) alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__)) +bool kmem_cache_charge (void *objp,gfp_t gfpflags);void kmem_cache_free (struct kmem_cache *s,void *objp); kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
mm/slub.c +51 -0
69 lines not on this page · raw message
net/ipv4/inet_connection_sock.c +3 -2
@@ -715,6 +715,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)release_sock (sk);if (newsk && mem_cgroup_sockets_enabled) {int amt =0 ; +gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL;/* atomically get the memory usage, set and charge the * newsk->sk_memcg.
@@ -731,8 +732,8 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg) }if (amt) -mem_cgroup_charge_skmem (newsk->sk_memcg, amt, -GFP_KERNEL | __GFP_NOFAIL); +mem_cgroup_charge_skmem (newsk->sk_memcg, amt, gfp); +kmem_cache_charge (newsk, gfp);release_sock (newsk); }
Signature · 1 line
2.43.5
Quoted 82 lines · Shakeel Butt
At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the
accept()time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.To measure the performance impact of this change,
tcp_crris used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
base (throughput) with-patch
tcp_crr14545 (+- 80) 14463 (+- 56)It seems like the performance impact is within the noise.
Link https://github.com/google/neper [1]Signed-off-by Shakeel Butt <[email protected]>---
v1: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Correctly handle large allocations which bypass slab
- Rearrange code to avoid compilation errors for !CONFIG_MEMCG buildsRFC: https://lore.kernel.org/all/[email protected]/
Changes since the RFC:
- Added check for already charged slab objects.
- Added performance results from neper'stcp_crrinclude/linux/slab.h | 1 + mm/slub.c | 51 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 3 files changed, 55 insertions(+), 2 deletions(-)include/linux/slab.h +1
-0@@ -547,6 +547,7 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc;#define kmem_cache_alloc_lru (...) alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__)) +bool kmem_cache_charge (void *objp,gfp_t gfpflags);void kmem_cache_free (struct kmem_cache *s,void *objp); kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,mm/slub.c +7
-0@@ -2185,6 +2185,43 @@ void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,__memcg_slab_free_hook (s, slab, p, objects, obj_exts); } + +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2204
Quoted 10 lines
mm/slub.c +10
-0+struct slabobj_ext *slab_exts; +struct kmem_cache *s; +struct folio *folio; +struct slab *slab; +unsigned long off; + + folio =virt_to_folio (p); +if (!folio_test_slab (folio)) { +return __memcg_kmem_charge_page (folio_page (folio,0 ), flags, +folio_order (folio)) ==0 ;
Will this charge the folio again if it was already charged? It seems like we avoid this for already charged slab objects below but not here.
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2212
Quoted 8 lines
mm/slub.c +8
-0+ } + + slab =folio_slab (folio); + s = slab->slab_cache; + +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ +if ((s->flags &KMALLOC_TYPE ) ==SLAB_KMALLOC ) +return true;
Would it be clearer to check if the slab cache is one of kmalloc_caches[KMALLOC_NORMAL]? This should be doable by comparing the address of the slab cache with the addresses of kmalloc_cache[KMALLOC_NORMAL] (perhaps in a helper). I need to refer to your reply to Roman to understand why this works.
Quoted 70 lines
mm/slub.c +26
-0+ +/* Ignore already charged objects. */ + slab_exts =slab_obj_exts (slab); +if (slab_exts) { + off =obj_to_index (s, slab, p); +if (unlikely (slab_exts[off].objcg)) +return true; + } + +return __memcg_slab_post_alloc_hook (s,NULL , flags,1 , &p); +} +#else /* CONFIG_MEMCG */ static inline bool memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,@@ -2198,6 +2235,11 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, int objects) { } + +static inline bool memcg_slab_post_charge (void *p,gfp_t flags) +{ +return true; +}#endif /* CONFIG_MEMCG */ /* @@ -4062,6 +4104,15 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, }EXPORT_SYMBOL (kmem_cache_alloc_lru_noprof); +bool kmem_cache_charge (void *objp,gfp_t gfpflags) +{ +if (!memcg_kmem_online ()) +return true; + +return memcg_slab_post_charge (objp, gfpflags); +} +EXPORT_SYMBOL (kmem_cache_charge); +/** * kmem_cache_alloc_node - Allocate an object on the specified node * @s: The cache to allocate from. net/ipv4/inet_connection_sock.c +3
-3@@ -715,6 +715,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)release_sock (sk);if (newsk && mem_cgroup_sockets_enabled) {int amt =0 ; +gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL;/* atomically get the memory usage, set and charge the * newsk->sk_memcg. @@ -731,8 +732,8 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg) }if (amt) -mem_cgroup_charge_skmem (newsk->sk_memcg, amt, -GFP_KERNEL | __GFP_NOFAIL); +mem_cgroup_charge_skmem (newsk->sk_memcg, amt, gfp); +kmem_cache_charge (newsk, gfp);release_sock (newsk); } --2.43.5
Quoted 1 line · Yosry Ahmed
On Tue, Aug 27, 2024 at 4:52 PM Shakeel Butt <[email protected]> wrote:
[...]
Quoted 13 lines
Quoted 13 lines
mm/slub.c +13
-0+ +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{ +struct slabobj_ext *slab_exts; +struct kmem_cache *s; +struct folio *folio; +struct slab *slab; +unsigned long off; +
Quoted 9 lines
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2204
Quoted 4 lines
mm/slub.c +4
-0+ folio =virt_to_folio (p); +if (!folio_test_slab (folio)) { +return __memcg_kmem_charge_page (folio_page (folio,0 ), flags, +folio_order (folio)) ==0 ;Will this charge the folio again if it was already charged? It seems like we avoid this for already charged slab objects below but not here.
Thanks for catchig this. It's an easy fix and will do in v3.
Quoted 6 lines
Quoted 6 lines
mm/slub.c +6
-0+ } + + slab =folio_slab (folio); + s = slab->slab_cache; + +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if ((s->flags & KMALLOC_TYPE) == SLAB_KMALLOC) + return true;Would it be clearer to check if the slab cache is one of kmalloc_caches[KMALLOC_NORMAL]? This should be doable by comparing the address of the slab cache with the addresses of kmalloc_cache[KMALLOC_NORMAL] (perhaps in a helper). I need to refer to your reply to Roman to understand why this works.
Do you mean looping over kmalloc_caches[KMALLOC_NORMAL] and comparing the given slab cache address? Nah man why do long loop of pointer comparisons when we can simply check the flag of the given kmem cache. Also this array will increase with the recent proposed random kmalloc caches.
Thanks,
Shakeel
Quoted 45 lines · Shakeel Butt
Quoted 1 line · Yosry Ahmed
On Tue, Aug 27, 2024 at 4:52 PM Shakeel Butt <[email protected]> wrote:
[...]
Quoted 13 lines
Quoted 13 lines
mm/slub.c +13
-0+ +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{ +struct slabobj_ext *slab_exts; +struct kmem_cache *s; +struct folio *folio; +struct slab *slab; +unsigned long off; +Quoted 9 lines
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2204
Quoted 4 lines
mm/slub.c +4
-0+ folio =virt_to_folio (p); +if (!folio_test_slab (folio)) { +return __memcg_kmem_charge_page (folio_page (folio,0 ), flags, +folio_order (folio)) ==0 ;Will this charge the folio again if it was already charged? It seems like we avoid this for already charged slab objects below but not here.
Thanks for catchig this. It's an easy fix and will do in v3.
Quoted 6 lines
Quoted 6 lines
mm/slub.c +6
-0+ } + + slab =folio_slab (folio); + s = slab->slab_cache; + +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if ((s->flags & KMALLOC_TYPE) == SLAB_KMALLOC) + return true;Would it be clearer to check if the slab cache is one of kmalloc_caches[KMALLOC_NORMAL]? This should be doable by comparing the address of the slab cache with the addresses of kmalloc_cache[KMALLOC_NORMAL] (perhaps in a helper). I need to refer to your reply to Roman to understand why this works.
Quoted 5 lines
Do you mean looping over kmalloc_caches[KMALLOC_NORMAL] and comparing the given slab cache address? Nah man why do long loop of pointer comparisons when we can simply check the flag of the given kmem cache. Also this array will increase with the recent proposed random kmalloc caches.
Oh I thought kmalloc_caches[KMALLOC_NORMAL] is an array of the actual struct kmem_cache objects, so I thought we can just check if:
s >= kmalloc_caches[KMALLOC_NORMAL][0] &&
s >= kmalloc_caches[KMALLOC_NORMAL][LAST_INDEX]
I just realized it's an array of pointers, so we would need to loop and compare them.
I still find the flags comparisons unclear and not very future-proof tbh. I think we can just store the type in struct kmem_cache? I think there are multiple holes there.
Quoted 57 lines · Yosry Ahmed
Quoted 45 lines · Shakeel Butt
Quoted 1 line · Yosry Ahmed
On Tue, Aug 27, 2024 at 4:52 PM Shakeel Butt <[email protected]> wrote:
[...]
Quoted 13 lines
Quoted 13 lines
mm/slub.c +13
-0+ +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{ +struct slabobj_ext *slab_exts; +struct kmem_cache *s; +struct folio *folio; +struct slab *slab; +unsigned long off; +Quoted 9 lines
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2204
Quoted 4 lines
mm/slub.c +4
-0+ folio =virt_to_folio (p); +if (!folio_test_slab (folio)) { +return __memcg_kmem_charge_page (folio_page (folio,0 ), flags, +folio_order (folio)) ==0 ;Will this charge the folio again if it was already charged? It seems like we avoid this for already charged slab objects below but not here.
Thanks for catchig this. It's an easy fix and will do in v3.
Quoted 6 lines
Quoted 6 lines
mm/slub.c +6
-0+ } + + slab =folio_slab (folio); + s = slab->slab_cache; + +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if ((s->flags & KMALLOC_TYPE) == SLAB_KMALLOC) + return true;Would it be clearer to check if the slab cache is one of kmalloc_caches[KMALLOC_NORMAL]? This should be doable by comparing the address of the slab cache with the addresses of kmalloc_cache[KMALLOC_NORMAL] (perhaps in a helper). I need to refer to your reply to Roman to understand why this works.
Quoted 5 lines
Do you mean looping over kmalloc_caches[KMALLOC_NORMAL] and comparing the given slab cache address? Nah man why do long loop of pointer comparisons when we can simply check the flag of the given kmem cache. Also this array will increase with the recent proposed random kmalloc caches.
Oh I thought kmalloc_caches[KMALLOC_NORMAL] is an array of the actual struct
kmem_cacheobjects, so I thought we can just check if:
s >= kmalloc_caches[KMALLOC_NORMAL][0] &&
s >= kmalloc_caches[KMALLOC_NORMAL][LAST_INDEX]
Quoted 6 lines
I just realized it's an array of pointers, so we would need to loop and compare them.
I still find the flags comparisons unclear and not very future-proof tbh. I think we can just store the type in struct
kmem_cache? I think there are multiple holes there.
Do you mean adding a new SLAB_KMALLOC_NORMAL? I will wait for SLAB maintainers for their opinion on that. BTW this kind of checks are in the kernel particularly for gfp flags.
Quoted 62 lines · Shakeel Butt
Quoted 60 lines · Yosry Ahmed
Quoted 45 lines · Shakeel Butt
Quoted 1 line · Yosry Ahmed
On Tue, Aug 27, 2024 at 4:52 PM Shakeel Butt <[email protected]> wrote:
[...]
Quoted 13 lines
Quoted 13 lines
mm/slub.c +13
-0+ +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{ +struct slabobj_ext *slab_exts; +struct kmem_cache *s; +struct folio *folio; +struct slab *slab; +unsigned long off; +Quoted 9 lines
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2204
Quoted 4 lines
mm/slub.c +4
-0+ folio =virt_to_folio (p); +if (!folio_test_slab (folio)) { +return __memcg_kmem_charge_page (folio_page (folio,0 ), flags, +folio_order (folio)) ==0 ;Will this charge the folio again if it was already charged? It seems like we avoid this for already charged slab objects below but not here.
Thanks for catchig this. It's an easy fix and will do in v3.
Quoted 6 lines
Quoted 6 lines
mm/slub.c +6
-0+ } + + slab =folio_slab (folio); + s = slab->slab_cache; + +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if ((s->flags & KMALLOC_TYPE) == SLAB_KMALLOC) + return true;Would it be clearer to check if the slab cache is one of kmalloc_caches[KMALLOC_NORMAL]? This should be doable by comparing the address of the slab cache with the addresses of kmalloc_cache[KMALLOC_NORMAL] (perhaps in a helper). I need to refer to your reply to Roman to understand why this works.
Quoted 5 lines
Do you mean looping over kmalloc_caches[KMALLOC_NORMAL] and comparing the given slab cache address? Nah man why do long loop of pointer comparisons when we can simply check the flag of the given kmem cache. Also this array will increase with the recent proposed random kmalloc caches.
Oh I thought kmalloc_caches[KMALLOC_NORMAL] is an array of the actual struct
kmem_cacheobjects, so I thought we can just check if:
s >= kmalloc_caches[KMALLOC_NORMAL][0] &&
s >= kmalloc_caches[KMALLOC_NORMAL][LAST_INDEX]I just realized it's an array of pointers, so we would need to loop and compare them.
Quoted 7 lines
Quoted 3 lines
I still find the flags comparisons unclear and not very future-proof tbh. I think we can just store the type in struct
kmem_cache? I think there are multiple holes there.Do you mean adding a new
SLAB_KMALLOC_NORMAL? I will wait for SLAB maintainers for their opinion on that. BTW this kind of checks are in the kernel particularly for gfp flags.
I meant maybe in new_kmalloc_cache() pass in 'type' to create_kmalloc_cache() and store it in struct kmem_cache (we'd want a KMALLOC_NONE a similar for non-kmalloc caches). Having a new flag like SLAB_KMALLOC_NORMAL would also work.
Or maybe using the flags to deduce the kmalloc cache type is fine, but in this case I think a well-documented helper that takes in a kmem_cache and restores a type based on the combination of flags would be better.
I just think in this case it's easy for the flags to change from under us here, and the code is not very clear.
Hopefully the slab maintainers will tell us what they think here, my concerns could very possibly be unfounded.
Quoted 90 lines · Shakeel Butt
At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the
accept()time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.To measure the performance impact of this change,
tcp_crris used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
base (throughput) with-patch
tcp_crr14545 (+- 80) 14463 (+- 56)It seems like the performance impact is within the noise.
Link https://github.com/google/neper [1]Signed-off-by Shakeel Butt <[email protected]>---
v1: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Correctly handle large allocations which bypass slab
- Rearrange code to avoid compilation errors for !CONFIG_MEMCG buildsRFC: https://lore.kernel.org/all/[email protected]/
Changes since the RFC:
- Added check for already charged slab objects.
- Added performance results from neper'stcp_crrinclude/linux/slab.h | 1 + mm/slub.c | 51 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 3 files changed, 55 insertions(+), 2 deletions(-)include/linux/slab.h +1
-0@@ -547,6 +547,7 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc;#define kmem_cache_alloc_lru (...) alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__)) +bool kmem_cache_charge (void *objp,gfp_t gfpflags);void kmem_cache_free (struct kmem_cache *s,void *objp); kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,mm/slub.c +15
-0@@ -2185,6 +2185,43 @@ void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,__memcg_slab_free_hook (s, slab, p, objects, obj_exts); } + +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{ +struct slabobj_ext *slab_exts; +struct kmem_cache *s; +struct folio *folio; +struct slab *slab; +unsigned long off; + + folio =virt_to_folio (p); +if (!folio_test_slab (folio)) {
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2212
Quoted 10 lines
mm/slub.c +10
-0+return __memcg_kmem_charge_page (folio_page (folio,0 ), flags, +folio_order (folio)) ==0 ; + } + + slab =folio_slab (folio); + s = slab->slab_cache; + +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ +if ((s->flags &KMALLOC_TYPE ) ==SLAB_KMALLOC ) +return true;
Taking a step back here, why do we need this? Which circular dependency are we avoiding here?
Quoted 2 lines · Yosry Ahmed
Quoted 1 line · Shakeel Butt
[...]
Quoted 7 lines
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2212
Quoted 4 lines
mm/slub.c +4
-0+ +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ +if ((s->flags &KMALLOC_TYPE ) ==SLAB_KMALLOC ) +return true;Taking a step back here, why do we need this? Which circular dependency are we avoiding here?
commit 494c1dfe855ec1f70f89552fce5eadf4a1717552
Author: Waiman Long <[email protected]> Date: Mon Jun 28 19:37:38 2021 -0700
mm: memcg/slab: create a new set of kmalloc-cg-<n> caches
There are currently two problems in the way the objcg pointer array (memcg_data) in the page structure is being allocated and freed.
On its allocation, it is possible that the allocated objcg pointer array comes from the same slab that requires memory accounting. If this happens, the slab will never become empty again as there is at least one object left (the obj_cgroup array) in the slab.
When it is freed, the objcg pointer array object may be the last one in its slab and hence causes kfree() to be called again. With the right workload, the slab cache may be set up in a way that allows the recursive kfree() calling loop to nest deep enough to cause a kernel stack overflow and panic the system.
...
Quoted 27 lines · Shakeel Butt
Quoted 2 lines · Yosry Ahmed
Quoted 1 line · Shakeel Butt
[...]
Quoted 7 lines
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2212
Quoted 4 lines
mm/slub.c +4
-0+ +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ +if ((s->flags &KMALLOC_TYPE ) ==SLAB_KMALLOC ) +return true;Taking a step back here, why do we need this? Which circular dependency are we avoiding here?
commit
494c1dfe855ec1f70f89552fce5eadf4a1717552Author: Waiman Long <[email protected]> Date: Mon Jun 28 19:37:38 2021 -0700mm: memcg/slab: create a new set of kmalloc-cg-<n> caches
There are currently two problems in the way the objcg pointer array (
memcg_data) in the page structure is being allocated and freed.On its allocation, it is possible that the allocated objcg pointer array comes from the same slab that requires memory accounting. If this happens, the slab will never become empty again as there is at least one object left (the
obj_cgrouparray) in the slab.
Quoted 6 lines
When it is freed, the objcg pointer array object may be the last one in its slab and hence causes
kfree()to be called again. With the right workload, the slab cache may be set up in a way that allows the recursivekfree()calling loop to nest deep enough to cause a kernel stack overflow and panic the system.
...
Thanks for the reference, this makes sense.
Wouldn't it be easier to special case the specific slab cache used for the objcg vector or use a dedicated cache for it instead of using kmalloc caches to begin with?
Anyway, I am fine with any approach you and/or the slab maintainers prefer, as long as we make things clear. If you keep the following approach as-is, please expand the comment or refer to the commit you just referenced.
Personally, I prefer either explicitly special casing the slab cache used for the objcgs vector, explicitly tagging KMALLOC_NORMAL allocations, or having a dedicated documented helper that finds the slab cache kmalloc type (if any) or checks if it is a KMALLOC_NORMAL cache.
Quoted 28 lines · Yosry Ahmed
Quoted 27 lines · Shakeel Butt
Quoted 2 lines · Yosry Ahmed
Quoted 1 line · Shakeel Butt
[...]
Quoted 7 lines
mm/slub.c void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, · line 2212
Quoted 4 lines
mm/slub.c +4
-0+ +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ +if ((s->flags &KMALLOC_TYPE ) ==SLAB_KMALLOC ) +return true;Taking a step back here, why do we need this? Which circular dependency are we avoiding here?
commit
494c1dfe855ec1f70f89552fce5eadf4a1717552Author: Waiman Long <[email protected]> Date: Mon Jun 28 19:37:38 2021 -0700mm: memcg/slab: create a new set of kmalloc-cg-<n> caches
There are currently two problems in the way the objcg pointer array (
memcg_data) in the page structure is being allocated and freed.On its allocation, it is possible that the allocated objcg pointer array comes from the same slab that requires memory accounting. If this happens, the slab will never become empty again as there is at least one object left (the
obj_cgrouparray) in the slab.
Quoted 8 lines
Quoted 6 lines
When it is freed, the objcg pointer array object may be the last one in its slab and hence causes
kfree()to be called again. With the right workload, the slab cache may be set up in a way that allows the recursivekfree()calling loop to nest deep enough to cause a kernel stack overflow and panic the system.
...Thanks for the reference, this makes sense.
Another reason is memory savings, if we have a small subset of objects in KMALLOC_NORMAL caches accounted, there might be e.g. one vector per a slab just to account on object while the rest is unaccounted. Separating between kmalloc and kmalloc-cg caches keeps the former with no vectors and the latter with fully used vectors.
Quoted 3 lines
Wouldn't it be easier to special case the specific slab cache used for the objcg vector or use a dedicated cache for it instead of using kmalloc caches to begin with?
The problem is the vector isn't a fixed size, it depends on how many objects a particular slab (not even a particular cache) has.
Quoted 10 lines
Anyway, I am fine with any approach you and/or the slab maintainers prefer, as long as we make things clear. If you keep the following approach as-is, please expand the comment or refer to the commit you just referenced.
Personally, I prefer either explicitly special casing the slab cache used for the objcgs vector, explicitly tagging
KMALLOC_NORMALallocations, or having a dedicated documented helper that finds the slab cache kmalloc type (if any) or checks if it is aKMALLOC_NORMALcache.
A helper to check is_kmalloc_normal() would be better than defining KMALLOC_TYPE and using it directly, yes. We don't need to handle any other types now until anyone needs those.
Quoted 49 lines · Shakeel Butt
At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the
accept()time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.To measure the performance impact of this change,
tcp_crris used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
base (throughput) with-patch
tcp_crr14545 (+- 80) 14463 (+- 56)It seems like the performance impact is within the noise.
Link https://github.com/google/neper [1]Signed-off-by Shakeel Butt <[email protected]>---
v1: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Correctly handle large allocations which bypass slab
- Rearrange code to avoid compilation errors for !CONFIG_MEMCG buildsRFC: https://lore.kernel.org/all/[email protected]/
Changes since the RFC:
- Added check for already charged slab objects.
- Added performance results from neper'stcp_crr
Quoted 4 lines
include/linux/slab.h | 1 + mm/slub.c | 51 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 3 files changed, 55 insertions(+), 2 deletions(-)
I can take the v3 in slab tree, if net people ack?
BTW, will this be also useful for Linus's idea of charging struct files only after they exist? But IIRC there was supposed to be also a part where we have a way to quickly determine if we're not over limit (while allowing some overcharge to make it quicker).
Because right now this just overcharges unconditionally, but that's understandable when the irq context creating the socket can't know the memcg upfront. In the open() situation this is different.
Quoted 112 lines
include/linux/slab.h +1
-0@@ -547,6 +547,7 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc;#define kmem_cache_alloc_lru (...) alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__)) +bool kmem_cache_charge (void *objp,gfp_t gfpflags);void kmem_cache_free (struct kmem_cache *s,void *objp); kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,mm/slub.c +51
-0@@ -2185,6 +2185,43 @@ void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,__memcg_slab_free_hook (s, slab, p, objects, obj_exts); } + +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{ +struct slabobj_ext *slab_exts; +struct kmem_cache *s; +struct folio *folio; +struct slab *slab; +unsigned long off; + + folio =virt_to_folio (p); +if (!folio_test_slab (folio)) { +return __memcg_kmem_charge_page (folio_page (folio,0 ), flags, +folio_order (folio)) ==0 ; + } + + slab =folio_slab (folio); + s = slab->slab_cache; + +/* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ +if ((s->flags &KMALLOC_TYPE ) ==SLAB_KMALLOC ) +return true; + +/* Ignore already charged objects. */ + slab_exts =slab_obj_exts (slab); +if (slab_exts) { + off =obj_to_index (s, slab, p); +if (unlikely (slab_exts[off].objcg)) +return true; + } + +return __memcg_slab_post_alloc_hook (s,NULL , flags,1 , &p); +} +#else /* CONFIG_MEMCG */ static inline bool memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,@@ -2198,6 +2235,11 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, int objects) { } + +static inline bool memcg_slab_post_charge (void *p,gfp_t flags) +{ +return true; +}#endif /* CONFIG_MEMCG */ /* @@ -4062,6 +4104,15 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, }EXPORT_SYMBOL (kmem_cache_alloc_lru_noprof); +bool kmem_cache_charge (void *objp,gfp_t gfpflags) +{ +if (!memcg_kmem_online ()) +return true; + +return memcg_slab_post_charge (objp, gfpflags); +} +EXPORT_SYMBOL (kmem_cache_charge); +/** * kmem_cache_alloc_node - Allocate an object on the specified node * @s: The cache to allocate from. net/ipv4/inet_connection_sock.c +3
-2@@ -715,6 +715,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)release_sock (sk);if (newsk && mem_cgroup_sockets_enabled) {int amt =0 ; +gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL;/* atomically get the memory usage, set and charge the * newsk->sk_memcg. @@ -731,8 +732,8 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg) }if (amt) -mem_cgroup_charge_skmem (newsk->sk_memcg, amt, -GFP_KERNEL | __GFP_NOFAIL); +mem_cgroup_charge_skmem (newsk->sk_memcg, amt, gfp); +kmem_cache_charge (newsk, gfp);release_sock (newsk); }
Quoted 1 line · Vlastimil Babka
On 8/29/24 02:49, Yosry Ahmed wrote:
[...]
Quoted 11 lines
Quoted 6 lines
Personally, I prefer either explicitly special casing the slab cache used for the objcgs vector, explicitly tagging
KMALLOC_NORMALallocations, or having a dedicated documented helper that finds the slab cache kmalloc type (if any) or checks if it is aKMALLOC_NORMALcache.A helper to check
is_kmalloc_normal()would be better than definingKMALLOC_TYPEand using it directly, yes. We don't need to handle any other types now until anyone needs those.
Sounds good, will update in v3.
Quoted 50 lines · Vlastimil Babka
Quoted 49 lines · Shakeel Butt
At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the
accept()time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.To measure the performance impact of this change,
tcp_crris used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
base (throughput) with-patch
tcp_crr14545 (+- 80) 14463 (+- 56)It seems like the performance impact is within the noise.
Link https://github.com/google/neper [1]Signed-off-by Shakeel Butt <[email protected]>---
v1: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Correctly handle large allocations which bypass slab
- Rearrange code to avoid compilation errors for !CONFIG_MEMCG buildsRFC: https://lore.kernel.org/all/[email protected]/
Changes since the RFC:
- Added check for already charged slab objects.
- Added performance results from neper'stcp_crr
Quoted 6 lines
Quoted 4 lines
include/linux/slab.h | 1 + mm/slub.c | 51 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 3 files changed, 55 insertions(+), 2 deletions(-)I can take the v3 in slab tree, if net people ack?
Thanks.
Quoted 6 lines
BTW, will this be also useful for Linus's idea of charging struct files only after they exist? But IIRC there was supposed to be also a part where we have a way to quickly determine if we're not over limit (while allowing some overcharge to make it quicker).
Do you have link to those discussions or pointers to the code? From what you have described, I think this should work. We have the relevant gfp flags to control the charging behavior (with some caveats).
Quoted 4 lines
Because right now this just overcharges unconditionally, but that's understandable when the irq context creating the socket can't know the memcg upfront. In the
open()situation this is different.
For networking we deliberately overcharges in the irq context (if needed) and the course correct in the task context. However networking stack is very robust due to mechanisms like backoff, retransmit to handle situations like packet drops, allocation failures, congestion etc. Other subsystem are not that robust against ENOMEM. Once I have more detail I can follow up on the struct files case.
thanks,
Shakeel
Roman Gushchin
reviewerQuoted 51 lines · Shakeel Butt
Quoted 50 lines · Vlastimil Babka
Quoted 49 lines · Shakeel Butt
At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the
accept()time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.To measure the performance impact of this change,
tcp_crris used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
base (throughput) with-patch
tcp_crr14545 (+- 80) 14463 (+- 56)It seems like the performance impact is within the noise.
Link https://github.com/google/neper [1]Signed-off-by Shakeel Butt <[email protected]>---
v1: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Correctly handle large allocations which bypass slab
- Rearrange code to avoid compilation errors for !CONFIG_MEMCG buildsRFC: https://lore.kernel.org/all/[email protected]/
Changes since the RFC:
- Added check for already charged slab objects.
- Added performance results from neper'stcp_crr
Quoted 14 lines
Quoted 6 lines
Quoted 4 lines
include/linux/slab.h | 1 + mm/slub.c | 51 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 3 files changed, 55 insertions(+), 2 deletions(-)I can take the v3 in slab tree, if net people ack?
Thanks.
Quoted 5 lines
BTW, will this be also useful for Linus's idea of charging struct files only after they exist? But IIRC there was supposed to be also a part where we have a way to quickly determine if we're not over limit (while allowing some overcharge to make it quicker).
It should work and speed up the case when we can drop the object before charging. I'd suggest to implement it in a separate change though.
Thanks!
Quoted 67 lines · Shakeel Butt
Quoted 50 lines · Vlastimil Babka
Quoted 49 lines · Shakeel Butt
At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the
accept()time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.To measure the performance impact of this change,
tcp_crris used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
base (throughput) with-patch
tcp_crr14545 (+- 80) 14463 (+- 56)It seems like the performance impact is within the noise.
Link https://github.com/google/neper [1]Signed-off-by Shakeel Butt <[email protected]>---
v1: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Correctly handle large allocations which bypass slab
- Rearrange code to avoid compilation errors for !CONFIG_MEMCG buildsRFC: https://lore.kernel.org/all/[email protected]/
Changes since the RFC:
- Added check for already charged slab objects.
- Added performance results from neper'stcp_crrQuoted 6 lines
Quoted 4 lines
include/linux/slab.h | 1 + mm/slub.c | 51 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 3 files changed, 55 insertions(+), 2 deletions(-)I can take the v3 in slab tree, if net people ack?
Thanks.
Quoted 6 lines
BTW, will this be also useful for Linus's idea of charging struct files only after they exist? But IIRC there was supposed to be also a part where we have a way to quickly determine if we're not over limit (while allowing some overcharge to make it quicker).
Quoted 3 lines
Do you have link to those discussions or pointers to the code? From what you have described, I think this should work. We have the relevant gfp flags to control the charging behavior (with some caveats).
I think this was the last part of the discussion, and in the cover letter of that there are links to the older threads for more context
https://lore.kernel.org/all/CAHk-%3DwhgFtbTxCAg2CWQtDj7n6CEyzvdV1wcCj2qpMfpw0%[email protected]/
Quoted 11 lines
Quoted 4 lines
Because right now this just overcharges unconditionally, but that's understandable when the irq context creating the socket can't know the memcg upfront. In the
open()situation this is different.For networking we deliberately overcharges in the irq context (if needed) and the course correct in the task context. However networking stack is very robust due to mechanisms like backoff, retransmit to handle situations like packet drops, allocation failures, congestion etc. Other subsystem are not that robust against ENOMEM. Once I have more detail I can follow up on the struct files case.
Ack. Agreed with Roman that it would be a separate followup change.
Quoted 4 lines
thanks,
Shakeel
[..]
Quoted 6 lines
Another reason is memory savings, if we have a small subset of objects in
KMALLOC_NORMALcaches accounted, there might be e.g. one vector per a slab just to account on object while the rest is unaccounted. Separating between kmalloc and kmalloc-cg caches keeps the former with no vectors and the latter with fully used vectors.
Makes sense.
Quoted 7 lines
Quoted 4 lines
Wouldn't it be easier to special case the specific slab cache used for the objcg vector or use a dedicated cache for it instead of using kmalloc caches to begin with?
The problem is the vector isn't a fixed size, it depends on how many objects a particular slab (not even a particular cache) has.
Oh right, I missed that part. Thanks for pointing it out.
Quoted 12 lines
Quoted 11 lines
Anyway, I am fine with any approach you and/or the slab maintainers prefer, as long as we make things clear. If you keep the following approach as-is, please expand the comment or refer to the commit you just referenced.
Personally, I prefer either explicitly special casing the slab cache used for the objcgs vector, explicitly tagging
KMALLOC_NORMALallocations, or having a dedicated documented helper that finds the slab cache kmalloc type (if any) or checks if it is aKMALLOC_NORMALcache.
Quoted 3 lines
A helper to check
is_kmalloc_normal()would be better than definingKMALLOC_TYPEand using it directly, yes. We don't need to handle any other types now until anyone needs those.
is_kmalloc_normal() sounds good to me.
Thanks, Vlastimil.
On Tue, Aug 27, 2024 at 04:52:28PM -0700, Shakeel Butt wrote:
41;2500;0c> At the moment, the slab objects are charged to the memcg at the
Quoted 30 lines
allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context.
Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the
accept()time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage.To measure the performance impact of this change,
tcp_crris used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong.The server and the client are run inside 3 level of cgroup hierarchy using the following commands:
Server:
$ tcp_crr -6Client:
$ tcp_crr -6 -c -H ${server_ip}If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change.
For the same machine experiment with v6.11-rc5 as base.
Quoted 7 lines
base (throughput) with-patch
tcp_crr14545 (+- 80) 14463 (+- 56)It seems like the performance impact is within the noise.
Link https://github.com/google/neper [1]Signed-off-by Shakeel Butt <[email protected]>
Thanks!