Skip to content

Commit 9028cde

Browse files
shakeelbtehcaster
authored andcommitted
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-patch tcp_crr 14545 (+- 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]> Reviewed-by: Roman Gushchin <[email protected]> Reviewed-by: Yosry Ahmed <[email protected]> Acked-by: Paolo Abeni <[email protected]> # net Signed-off-by: Vlastimil Babka <[email protected]>
1 parent b8c8ba7 commit 9028cde

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

include/linux/slab.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,35 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
547547
gfp_t gfpflags) __assume_slab_alignment __malloc;
548548
#define kmem_cache_alloc_lru(...) alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__))
549549

550+
/**
551+
* kmem_cache_charge - memcg charge an already allocated slab memory
552+
* @objp: address of the slab object to memcg charge
553+
* @gfpflags: describe the allocation context
554+
*
555+
* kmem_cache_charge allows charging a slab object to the current memcg,
556+
* primarily in cases where charging at allocation time might not be possible
557+
* because the target memcg is not known (i.e. softirq context)
558+
*
559+
* The objp should be pointer returned by the slab allocator functions like
560+
* kmalloc (with __GFP_ACCOUNT in flags) or kmem_cache_alloc. The memcg charge
561+
* behavior can be controlled through gfpflags parameter, which affects how the
562+
* necessary internal metadata can be allocated. Including __GFP_NOFAIL denotes
563+
* that overcharging is requested instead of failure, but is not applied for the
564+
* internal metadata allocation.
565+
*
566+
* There are several cases where it will return true even if the charging was
567+
* not done:
568+
* More specifically:
569+
*
570+
* 1. For !CONFIG_MEMCG or cgroup_disable=memory systems.
571+
* 2. Already charged slab objects.
572+
* 3. For slab objects from KMALLOC_NORMAL caches - allocated by kmalloc()
573+
* without __GFP_ACCOUNT
574+
* 4. Allocating internal metadata has failed
575+
*
576+
* Return: true if charge was successful otherwise false.
577+
*/
578+
bool kmem_cache_charge(void *objp, gfp_t gfpflags);
550579
void kmem_cache_free(struct kmem_cache *s, void *objp);
551580

552581
kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,

mm/slab.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ static inline bool is_kmalloc_cache(struct kmem_cache *s)
443443
return (s->flags & SLAB_KMALLOC);
444444
}
445445

446+
static inline bool is_kmalloc_normal(struct kmem_cache *s)
447+
{
448+
if (!is_kmalloc_cache(s))
449+
return false;
450+
return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
451+
}
452+
446453
/* Legal flag mask for kmem_cache_create(), for various configurations */
447454
#define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
448455
SLAB_CACHE_DMA32 | SLAB_PANIC | \

mm/slub.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,45 @@ void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,
21852185

21862186
__memcg_slab_free_hook(s, slab, p, objects, obj_exts);
21872187
}
2188+
2189+
static __fastpath_inline
2190+
bool memcg_slab_post_charge(void *p, gfp_t flags)
2191+
{
2192+
struct slabobj_ext *slab_exts;
2193+
struct kmem_cache *s;
2194+
struct folio *folio;
2195+
struct slab *slab;
2196+
unsigned long off;
2197+
2198+
folio = virt_to_folio(p);
2199+
if (!folio_test_slab(folio)) {
2200+
return folio_memcg_kmem(folio) ||
2201+
(__memcg_kmem_charge_page(folio_page(folio, 0), flags,
2202+
folio_order(folio)) == 0);
2203+
}
2204+
2205+
slab = folio_slab(folio);
2206+
s = slab->slab_cache;
2207+
2208+
/*
2209+
* Ignore KMALLOC_NORMAL cache to avoid possible circular dependency
2210+
* of slab_obj_exts being allocated from the same slab and thus the slab
2211+
* becoming effectively unfreeable.
2212+
*/
2213+
if (is_kmalloc_normal(s))
2214+
return true;
2215+
2216+
/* Ignore already charged objects. */
2217+
slab_exts = slab_obj_exts(slab);
2218+
if (slab_exts) {
2219+
off = obj_to_index(s, slab, p);
2220+
if (unlikely(slab_exts[off].objcg))
2221+
return true;
2222+
}
2223+
2224+
return __memcg_slab_post_alloc_hook(s, NULL, flags, 1, &p);
2225+
}
2226+
21882227
#else /* CONFIG_MEMCG */
21892228
static inline bool memcg_slab_post_alloc_hook(struct kmem_cache *s,
21902229
struct list_lru *lru,
@@ -2198,6 +2237,11 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
21982237
void **p, int objects)
21992238
{
22002239
}
2240+
2241+
static inline bool memcg_slab_post_charge(void *p, gfp_t flags)
2242+
{
2243+
return true;
2244+
}
22012245
#endif /* CONFIG_MEMCG */
22022246

22032247
#ifdef CONFIG_SLUB_RCU_DEBUG
@@ -4105,6 +4149,15 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
41054149
}
41064150
EXPORT_SYMBOL(kmem_cache_alloc_lru_noprof);
41074151

4152+
bool kmem_cache_charge(void *objp, gfp_t gfpflags)
4153+
{
4154+
if (!memcg_kmem_online())
4155+
return true;
4156+
4157+
return memcg_slab_post_charge(objp, gfpflags);
4158+
}
4159+
EXPORT_SYMBOL(kmem_cache_charge);
4160+
41084161
/**
41094162
* kmem_cache_alloc_node - Allocate an object on the specified node
41104163
* @s: The cache to allocate from.

net/ipv4/inet_connection_sock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)
714714
out:
715715
release_sock(sk);
716716
if (newsk && mem_cgroup_sockets_enabled) {
717+
gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL;
717718
int amt = 0;
718719

719720
/* atomically get the memory usage, set and charge the
@@ -731,8 +732,8 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)
731732
}
732733

733734
if (amt)
734-
mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
735-
GFP_KERNEL | __GFP_NOFAIL);
735+
mem_cgroup_charge_skmem(newsk->sk_memcg, amt, gfp);
736+
kmem_cache_charge(newsk, gfp);
736737

737738
release_sock(newsk);
738739
}

0 commit comments

Comments
 (0)