On Wed, Nov 25, 2026 at 09:37:15PM +0000, Harry Yoo (Meta) wrote:
> Now that all caches whose objects may be allocated with __GFP_ACCOUNT > specify SLAB_MAY_ACCOUNT (or SLAB_ACCOUNT, which implies it), stop > assuming any cache may be used with __GFP_ACCOUNT. This avoids wasting > the objcg slot in slabobj_ext for caches that are never charged to a > memcg. > > Signed-off-by: Harry Yoo (Meta) <har…@kernel.org> > --- > include/linux/slab.h | 16 +++++++++++++--- > mm/slab_common.c | 8 +++----- > 2 files changed, 16 insertions(+), 8 deletions(-) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index ad645967a3ee..8525e70eaa76 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -201,14 +201,24 @@ enum _slab_flag_bits { > /** > * define SLAB_ACCOUNT - Account allocations to memcg. > * > - * All object allocations from this cache will be memcg accounted, regardless of > - * __GFP_ACCOUNT being or not being passed to individual allocations. > + * All object allocations from this cache will be memcg accounted. > + * SLAB_ACCOUNT implies SLAB_MAY_ACCOUNT. > */ > #ifdef CONFIG_MEMCG > # define SLAB_ACCOUNT __SLAB_FLAG_BIT(_SLAB_ACCOUNT) > -# define SLAB_MAY_ACCOUNT __SLAB_FLAG_BIT(_SLAB_MAY_ACCOUNT) > #else > # define SLAB_ACCOUNT __SLAB_FLAG_UNUSED > +#endif > + > +/** > + * define SLAB_MAY_ACCOUNT > + * > + * Objects will be memcg accounted when __GFP_ACCOUNT flag is specified. > + * Requesting __GFP_ACCOUNT allocations without SLAB_MAY_ACCOUNT is not allowed. > + */ > +#ifdef CONFIG_MEMCG > +# define SLAB_MAY_ACCOUNT __SLAB_FLAG_BIT(_SLAB_MAY_ACCOUNT) > +#else > # define SLAB_MAY_ACCOUNT __SLAB_FLAG_UNUSED > #endif > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 193b5a03bc47..464695504248 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -367,12 +367,10 @@ 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()) > + if (flags & SLAB_ACCOUNT) > flags |= SLAB_MAY_ACCOUNT; > + if (mem_cgroup_kmem_disabled()) > + flags &= ~SLAB_MAY_ACCOUNT;
Btw, say maple_node/sk_buff/... could be merged with cacheX before. After this patch, cacheX loses SLAB_MAY_ACCOUNT while we still set SLAB_MAY_ACCOUNT for maple_node explicitly, so they can't be merged anymore. Not sure if it matters, though.
> > /* Fail closed on bad usersize of useroffset values. */ > if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) || > > -- > 2.54.0 >
-- Thanks, Hao