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

Hao Li hao.li at linux.dev
Thu Dec 3 14:21:00 UTC 2026


On Wed, Nov 25, 2026 at 09:37:14PM +0000, Harry Yoo (Meta) wrote:
> 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().
> 
> Cc: Johannes Weiner <hannes at cmpxchg.org>
> Cc: Michal Hocko <mhocko at kernel.org>
> Cc: Shakeel Butt <shakeel.butt at linux.dev>
> Cc: Muchun Song <muchun.song at linux.dev>
> Cc: cgroups at vger.kernel.org
> Signed-off-by: Harry Yoo (Meta) <harry at kernel.org>
> ---
>  mm/memcontrol.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index a07cc73acfc7..0eddb16173fd 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -3707,6 +3707,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;
> +	}
> +

Would it be better to put this check before `if (!objcg ||
obj_cgroup_is_root(objcg))`, so we can catch abnormal cases more aggressively?

>  	flags &= gfp_allowed_mask;
>  
>  	if (lru) {
> 
> -- 
> 2.54.0
> 

-- 
Thanks,
Hao



More information about the Linux-security-module-archive mailing list