Re: [PATCH v2 12/13] mm/slab: add cache_ and slab_needs_objcg() helpers

"Vlastimil Babka (SUSE)" <[email protected]> Thu, 23 Jul 2026 11:30:49 +0200
Newsgroups org.kernel.vger.cgroups,org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <[email protected]>
On 7/23/26 11:25, Hao Li wrote:
> On Mon, Jul 20, 2026 at 04:16:26PM +0200, Vlastimil Babka (SUSE) wrote:
>> @@ -359,6 +359,13 @@ struct kmem_cache *__kmem_cache_create_args(const char *name,
>>  		goto out_unlock;
>>  	}
>>  
>> +	/*
>> +	 * For now we assume any cache can be used with __GFP_ACCOUNT and thus
>> +	 * may need to store objcg pointers for objects
>> +	 */
>> +	if (!mem_cgroup_kmem_disabled())
>> +		flags |= SLAB_MAY_ACCOUNT;
> 
> if memcg itself is disabled via `cgroup_disable`, mem_cgroup_kmem_disabled()
> still return false, so maybe we can use:

Aha, but then shouldn't rather mem_cgroup_kmem_disabled() take that into
account on its own?

> if (!mem_cgroup_disabled() && !mem_cgroup_kmem_disabled())
> 	flags |= SLAB_MAY_ACCOUNT;
> 
>> +
>>  	/* Fail closed on bad usersize of useroffset values. */
>>  	if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) ||
>>  	    WARN_ON(!args->usersize && args->useroffset) ||
>> @@ -984,11 +991,19 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type)
>>  #endif
>>  
>>  	/*
>> -	 * If CONFIG_MEMCG is enabled, disable cache merging for
>> -	 * KMALLOC_NORMAL caches.
>> +	 * If memcg_kmem is enabled and this is a KMALLOC_NORMAL cache and  not
>> +	 * aliased with any other type, make sure it's never merged with any other
>> +	 * cache.
>> +	 *
>> +	 * In other cases the kmalloc cache may end up being used for a
>> +	 * __GFP_ACCOUNT allocation so mark it as such
>>  	 */
>> -	if (IS_ENABLED(CONFIG_MEMCG) && (type == KMALLOC_NORMAL))
>> -		flags |= SLAB_NO_MERGE;
>> +	if (!mem_cgroup_kmem_disabled()) {
> 
> the same as above
> 
> Others look good to me.
> 
> Reviewed-by: Hao Li <[email protected]>

Thanks!

> 
>> +		if (type == KMALLOC_NORMAL && KMALLOC_RECLAIM != KMALLOC_NORMAL)
>> +			flags |= SLAB_NO_MERGE;
>> +		else
>> +			flags |= SLAB_MAY_ACCOUNT;
>> +	}
>>  
>>  	if (minalign > ARCH_KMALLOC_MINALIGN) {
>>  		aligned_size = ALIGN(aligned_size, minalign);
>> diff --git a/mm/slub.c b/mm/slub.c
>> index d78d3e50c877..30435e2509ea 100644
>> --- a/mm/slub.c
>> +++ b/mm/slub.c
>> @@ -2557,7 +2557,7 @@ bool memcg_slab_post_charge(void *p, gfp_t flags)
>>  	 * of slab_obj_exts being allocated from the same slab and thus the slab
>>  	 * becoming effectively unfreeable.
>>  	 */
>> -	if (is_kmalloc_normal(s))
>> +	if (!cache_needs_objcg(s))
>>  		return true;
>>  
>>  	/* Ignore already charged objects. */
>> @@ -3435,6 +3435,10 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags,
>>  
>>  	slab->objects = oo_objects(oo);
>>  
>> +#ifdef CONFIG_64BIT
>> +	if (cache_needs_objcg(s))
>> +		slab->obj_exts_needs_objcg = 1;
>> +#endif
>>  	slab->slab_cache = s;
>>  
>>  	kasan_poison_slab(slab);
>> 
>> -- 
>> 2.55.0
>> 
>