memcg: add charging of already allocated slab objects
Subsystems (6)
PATCH v3 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.
---
v2:
https://lore.kernel.org/all/[email protected]/
Change since v2:
- Add handling of already charged large kmalloc objects.
- Move the normal kmalloc cache check into a function.
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/slab.h | 7 +++++ mm/slub.c | 49 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 4 files changed, 60 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/slab.h +7 -0
@@ -443,6 +443,13 @@ static inline bool is_kmalloc_cache(struct kmem_cache *s)return (s->flags &SLAB_KMALLOC ); } +static inline bool is_kmalloc_normal (struct kmem_cache *s) +{ +if (!is_kmalloc_cache (s)) +return false; +return !(s->flags & (SLAB_CACHE_DMA |SLAB_ACCOUNT |SLAB_RECLAIM_ACCOUNT )); +} +/* Legal flag mask for kmem_cache_create(), for various configurations */ #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_PANIC | \
mm/slub.c +49 -0
67 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 31 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.
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, pushed to slab/for-next for test coverage, hopefully net people will ack.
Also one thing:
We should add some kernel doc for this, no? Explaining when people are supposed to use this, that objects from KMALLOC_NORMAL will be ignored, and what the return value means (including where it's faked to be true).
Quoted 35 lines
mm/slub.c +9
-0+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 40 lines · Vlastimil Babka
Quoted 31 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.
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]>
Quoted 9 lines
Thanks, pushed to slab/for-next for test coverage, hopefully net people will ack.
Also one thing:
We should add some kernel doc for this, no? Explaining when people are supposed to use this, that objects from
KMALLOC_NORMALwill be ignored, and what the return value means (including where it's faked to be true).
Yes this makes sense. I will add this info similar to the kmalloc() have. Should I send a v4 with this details?
Quoted 10 lines · Shakeel Butt
Quoted 7 lines
Also one thing:
We should add some kernel doc for this, no? Explaining when people are supposed to use this, that objects from
KMALLOC_NORMALwill be ignored, and what the return value means (including where it's faked to be true).Yes this makes sense. I will add this info similar to the
kmalloc()have. Should I send a v4 with this details?
Yeah please, will replace v3 with that.
Thanks