kmem_cache_charge(9)
π kmem_cache_charge(9) β Kernel API Manual π
πΈ NAME πΈ
kmem_cache_charge β memcg charge an already allocated slab memory. Yes bestie, it's charging slab memory to memcg after the allocation has already happened. β¨
β¨ SYNOPSIS β¨
bool kmem_cache_charge (void *objp , gfp_t gfpflags );
π ARGUMENTS π
objpβ address of the slab object to memcg charge. This is the pointer to the thing you're charging.gfpflagsβ describe the allocation context. This is how you tell the charge what kind of allocation vibes it's operating under.
π¦ DESCRIPTION π¦
Okay so kmem_cache_charge allows charging a slab object to the current memcg. It's primarily for cases where charging at allocation time might not be possible because the target memcg is not known β i.e. softirq context. That's the main-character scenario this function is showing up for. π
The objp should be a pointer returned by the slab allocator functions like kmalloc (with __GFP_ACCOUNT in flags) or kmem_cache_alloc. Like, that's the pointer you hand over β no random pointer shenanigans. The memcg charge behavior can be controlled through the gfpflags parameter, which affects how the necessary internal metadata can be allocated. Including __GFP_NOFAIL denotes that overcharging is requested instead of failure, but it is not applied for the internal metadata allocation. That little caveat is important, bestie.
There are several cases where it will return true even if the charging was not done. More specifically:
β οΈ Plot twist: true does not always mean the charge actually happened.
- For
!CONFIG_MEMCGorcgroup_disable=memorysystems. - Already charged slab objects.
- For slab objects from
KMALLOC_NORMALcaches β allocated bykmallocwithout__GFP_ACCOUNT. - Allocating internal metadata has failed.
π RETURN π
true if charge was successful, otherwise false. The bool is literally giving yes/no energy. π«Ά
π SEE ALSO π
Kernel file ./include/linux/slab.h, plus the whole friend group: SLAB_HWCACHE_ALIGN(9), SLAB_TYPESAFE_BY_RCU(9), SLAB_ACCOUNT(9), SLAB_RECLAIM_ACCOUNT(9), struct kmem_cache_args(9), kmem_cache_create_usercopy(9), kmem_cache_create(9), krealloc_node_align(9), kmem_cache_alloc(9), kmalloc(9), kmalloc_nolock(9), __alloc_objs(9), __alloc_flex(9), kmalloc_obj(9), kmalloc_objs(9), kmalloc_flex(9), kmalloc_array(9), krealloc_array(9), kcalloc(9), kzalloc(9), kvmalloc_node(9), kvrealloc_node_align(9), kmalloc_size_roundup(9).
That's the page, bestie π«Άβ¨ Source/version vibes: ./include/linux, dated October 2026, and this iconic manual page is kmem_cache_charge(9) π
ππ¦
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms