Re: [PATCH RFC v2 08/20] slab: add optimized sheaf refill from partial list

From: Hao Li

Date: Fri Jan 16 2026 - 02:57:21 EST


On Fri, Jan 16, 2026 at 08:32:00AM +0100, Vlastimil Babka wrote:
> On 1/16/26 07:27, Hao Li wrote:
> > On Thu, Jan 15, 2026 at 03:25:59PM +0100, Vlastimil Babka wrote:
> >> On 1/12/26 16:17, Vlastimil Babka wrote:
> >> > At this point we have sheaves enabled for all caches, but their refill
> >> > is done via __kmem_cache_alloc_bulk() which relies on cpu (partial)
> >> > slabs - now a redundant caching layer that we are about to remove.
> >> >
> >> > The refill will thus be done from slabs on the node partial list.
> >> > Introduce new functions that can do that in an optimized way as it's
> >> > easier than modifying the __kmem_cache_alloc_bulk() call chain.
> >> >
> >> > Extend struct partial_context so it can return a list of slabs from the
> >> > partial list with the sum of free objects in them within the requested
> >> > min and max.
> >> >
> >> > Introduce get_partial_node_bulk() that removes the slabs from freelist
> >> > and returns them in the list.
> >> >
> >> > Introduce get_freelist_nofreeze() which grabs the freelist without
> >> > freezing the slab.
> >> >
> >> > Introduce alloc_from_new_slab() which can allocate multiple objects from
> >> > a newly allocated slab where we don't need to synchronize with freeing.
> >> > In some aspects it's similar to alloc_single_from_new_slab() but assumes
> >> > the cache is a non-debug one so it can avoid some actions.
> >> >
> >> > Introduce __refill_objects() that uses the functions above to fill an
> >> > array of objects. It has to handle the possibility that the slabs will
> >> > contain more objects that were requested, due to concurrent freeing of
> >> > objects to those slabs. When no more slabs on partial lists are
> >> > available, it will allocate new slabs. It is intended to be only used
> >> > in context where spinning is allowed, so add a WARN_ON_ONCE check there.
> >> >
> >> > Finally, switch refill_sheaf() to use __refill_objects(). Sheaves are
> >> > only refilled from contexts that allow spinning, or even blocking.
> >> >
> >> > Signed-off-by: Vlastimil Babka <vbabka@xxxxxxx>
> >>
> >> ...
> >>
> >> > +static unsigned int alloc_from_new_slab(struct kmem_cache *s, struct slab *slab,
> >> > + void **p, unsigned int count, bool allow_spin)
> >> > +{
> >> > + unsigned int allocated = 0;
> >> > + struct kmem_cache_node *n;
> >> > + unsigned long flags;
> >> > + void *object;
> >> > +
> >> > + if (!allow_spin && (slab->objects - slab->inuse) > count) {
> >> > +
> >> > + n = get_node(s, slab_nid(slab));
> >> > +
> >> > + if (!spin_trylock_irqsave(&n->list_lock, flags)) {
> >> > + /* Unlucky, discard newly allocated slab */
> >> > + defer_deactivate_slab(slab, NULL);
> >>
> >> This actually does dec_slabs_node() only with slab->frozen which we don't set.
> >
> > Hi, I think I follow the intent, but I got a little tripped up here: patch 08
> > (current patch) seems to assume "slab->frozen = 1" is already gone. That's true
> > after the whole series, but the removal only happens in patch 09.
> >
> > Would it make sense to avoid relying on that assumption when looking at patch 08
> > in isolation?
>
> Hm I did think it's fine. alloc_from_new_slab() introduced here is only used
> from __refill_objects() and that one doesn't set slab->frozen = 1 on the new
> slab?

Yes, exactly!

>
> Then patch 09 switches ___slab_alloc() to alloc_from_new_slab() and at the
> same time also stops setting slab->frozen = 1 so it should be also fine.

Yes. This make sense to me.

>
> And then 12/20 slab: remove defer_deactivate_slab() removes the frozen = 1
> treatment as nobody uses it anymore.
>
> If there's some mistake in the above, please tell!

Everything makes sense to me. The analysis looks reasonable. Thanks!

Just a quick note - I noticed that the code in your repo for b4/sheaves-for-all
has been updated. I also saw that Harry posted the latest link and did an inline
review in his reply to [05/20].

Do you happen to plan a v3 version of this patchset? Thanks!

>
> Thanks.