Re: [PATCH slab/for-next v2 14/14] mm/slab: do not set SLAB_MAY_ACCOUNT for all caches

From: Hao Li

Date: Thu Dec 03 2026 - 10:48:01 EST


On Thu, Dec 03, 2026 at 02:46:49PM +0000, Harry Yoo wrote:
> On Thu, Dec 03, 2026 at 02:36:57PM +0000, Hao Li wrote:
> > On Thu, Dec 03, 2026 at 02:27:25PM +0000, Harry Yoo wrote:
> > > On Thu, Dec 03, 2026 at 02:17:21PM +0000, Hao Li wrote:
> > > > 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) <harry@xxxxxxxxxx>
> > > > > ---
> > > > > 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.
> > >
> > > Hmm you mean SLAB_ACCOUNT and SLAB_MAY_ACCOUNT should be cleared at
> > > run-time during merging decision with cgroup.memory=nokmem?
> >
> > Ah, not really. I mean, assuming kmem is enabled, before this patchset
> > all caches have SLAB_MAY_ACCOUNT. After applying it, some have the flag and
> > some don't, so `if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))`
> > in find_mergeable() might not hold anymore.
>
> Ah, I see.
> This is intentional and benign I think :-)
>
> We don't want to merge cache X and Y when only one of them has
> SLAB_MAY_ACCOUNT as that'll bloat metadata size.

Yeah, you're right. I just happened to think of commit 08c78a6d33af ("mm, slab:
rework KMALLOC_RECLAIM handling of SLUB_TINY"), which drops
SLAB_RECLAIM_ACCOUNT under slab_tiny so that caches can still be merged. Still,
memory savings from this patchset should far outweigh the slab merging we lose!

Looks good to me!
Reviewed-by: Hao Li <hao.li@xxxxxxxxx>

--
Thanks,
Hao