Re: [RFC PATCH 1/2] mm/slub: make the case handling in __slab_free() easier to follow

From: Hao Li

Date: Wed Sep 16 2026 - 10:16:51 EST


On Mon, Sep 14, 2026 at 02:39:16PM +0100, Harry Yoo wrote:
> On Mon, Aug 24, 2026 at 08:25:09PM +0800, Hao Li wrote:
> > There are 7 possible transitions in __slab_free():
> >
> > a. partial->partial
> > b. partial->empty, offlist
> > c. partial->empty, onlist, exceeding min_partial
> > d. partial->empty, onlist, not exceeding min_partial
> > e. full->empty, exceeding min_partial
> > f. full->empty, not exceeding min_partial
> > g. full->partial
> >
> > (There is no offlist variant of e, f and g as a full slab is on no
> > list.)
> >
> > Clarify which case each branch handles, and replace the goto with a
> > return at the end of the skipped block so that every branch explicitly
> > states its coverage.
> >
> > Case 'a' is the only path that needs neither list_lock nor list
> > handling. Give it an early continue: handling it upfront is much clearer
> > than forcing every other case into a nested block.
> >
> > Also, read SL_partial once after the loop right where it is used, rather
> > than re-reading it on every iteration.
> >
> > No functional change.
> >
> > Signed-off-by: Hao Li <hao.li@xxxxxxxxx>
> > ---
>
> Looks good to me (with Vlastimil's suggestions adjusted),
> Reviewed-by: Harry Yoo (Meta) <harry@xxxxxxxxxx>

Thanks!

>
> > mm/slub.c | 95 ++++++++++++++++++++++++++++---------------------------
> > 1 file changed, 49 insertions(+), 46 deletions(-)
> >
> > diff --git a/mm/slub.c b/mm/slub.c
> > index b0cd0572e2f2..e20375307770 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -5748,76 +5748,79 @@ static void __slab_free(struct kmem_cache *s, struct slab *slab,
>
> [...]
>
> > + /*
> > + * The slab might need to be taken off (due to becoming empty)
> > + * or added to (due to not being full anymore) the partial
> > + * list.
> > + *
> > + * Speculatively acquire list_lock before calling cmpxchg(), as
> > + * performing cmpxchg() prior to lock acquisition races with
> > + * concurrent paths, such as the shrinker.
>
> nit: it's bit weird to mention shrinkers specifically?

Yeah, better to remove it to be more generic.

--
Thanks,
Hao