[PATCH slab/for-next 13/14] mm/memcontrol: do not allow charging objects without SLAB_MAY_ACCOUNT

From: Harry Yoo (Meta)

Date: Wed Oct 21 2026 - 07:55:46 EST


Objects of a cache without SLAB_MAY_ACCOUNT cannot store the objcg
pointer once slabobj_ext space is no longer reserved for every cache.
Skip charging when __GFP_ACCOUNT hits such a cache, and warn once under
CONFIG_DEBUG_VM to catch callers that did not opt in.

Refusing the charge here is sufficient for correctness: an object that
is never charged does not access the objcg slot again, and the free
path already skips such caches via slab_needs_objcg().

Assisted-by: LLM # Changelog
Cc: Johannes Weiner <hannes@xxxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxxxx>
Cc: Shakeel Butt <shakeel.butt@xxxxxxxxx>
Cc: Muchun Song <muchun.song@xxxxxxxxx>
Cc: cgroups@xxxxxxxxxxxxxxx
Signed-off-by: Harry Yoo (Meta) <harry@xxxxxxxxxx>
---
mm/memcontrol.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1271d390b617..9f996206592f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3622,6 +3622,12 @@ bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,
if (unlikely(*p == NULL))
return true;

+ /* Do not allow __GFP_ACCOUNT without SLAB_MAY_ACCOUNT */
+ if (unlikely(!cache_needs_objcg(s))) {
+ VM_WARN_ON_ONCE(1);
+ return true;
+ }
+
flags &= gfp_allowed_mask;

if (lru) {

--
2.54.0