kmem_cache_charge(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– 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.
  1. For !CONFIG_MEMCG or cgroup_disable=memory systems.
  2. Already charged slab objects.
  3. For slab objects from KMALLOC_NORMAL caches β€” allocated by kmalloc without __GFP_ACCOUNT.
  4. 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