Re: [PATCH v3 09/21] slab: add optimized sheaf refill from partial list
From: Hao Li
Date: Tue Jan 20 2026 - 04:32:59 EST
On Tue, Jan 20, 2026 at 10:41:37AM +0900, Harry Yoo wrote:
> On Mon, Jan 19, 2026 at 11:54:18AM +0100, Vlastimil Babka wrote:
> > On 1/19/26 07:41, Harry Yoo wrote:
> > > On Fri, Jan 16, 2026 at 03:40:29PM +0100, Vlastimil Babka wrote:
> > >> /*
> > >> * Try to allocate a partial slab from a specific node.
> > >> */
> > >> +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);
> > >> + return 0;
> > >> + }
> > >> + }
> > >> +
> > >> + object = slab->freelist;
> > >> + while (object && allocated < count) {
> > >> + p[allocated] = object;
> > >> + object = get_freepointer(s, object);
> > >> + maybe_wipe_obj_freeptr(s, p[allocated]);
> > >> +
> > >> + slab->inuse++;
> > >> + allocated++;
> > >> + }
> > >> + slab->freelist = object;
> > >> +
> > >> + if (slab->freelist) {
> > >> +
> > >> + if (allow_spin) {
> > >> + n = get_node(s, slab_nid(slab));
> > >> + spin_lock_irqsave(&n->list_lock, flags);
> > >> + }
> > >> + add_partial(n, slab, DEACTIVATE_TO_HEAD);
> > >> + spin_unlock_irqrestore(&n->list_lock, flags);
> > >> + }
> > >> +
> > >> + inc_slabs_node(s, slab_nid(slab), slab->objects);
> > >
> > > Maybe add a comment explaining why inc_slabs_node() doesn't need to be
> > > called under n->list_lock?
I think this is a great observation.
> >
> > Hm, we might not even be holding it. The old code also did the inc with no
> > comment. If anything could use one, it would be in
> > alloc_single_from_new_slab()? But that's outside the scope here.
>
> Ok. Perhaps worth adding something like this later, but yeah it's outside
> the scope here.
>
> diff --git a/mm/slub.c b/mm/slub.c
> index 698c0d940f06..c5a1e47dfe16 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -1633,6 +1633,9 @@ static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
> {
> struct kmem_cache_node *n = get_node(s, node);
>
> + if (kmem_cache_debug(s))
> + /* slab validation may generate false errors without the lock */
> + lockdep_assert_held(&n->list_lock);
> atomic_long_inc(&n->nr_slabs);
> atomic_long_add(objects, &n->total_objects);
> }
Yes. This makes sense to me.
Just to double-check - I noticed that inc_slabs_node() is also called by
early_kmem_cache_node_alloc(). Could this potentially lead to false positive
warnings for boot-time caches when debug flags are enabled?
--
Thanks,
Hao