From: Harry Yoo <harry@kernel.org>
To: Hao Li <hao.li@linux.dev>
Cc: vbabka@kernel.org, akpm@linux-foundation.org, cl@gentwo.org,
rientjes@google.com, roman.gushchin@linux.dev,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm/slub: refill prefilled sheaves from the barn
Date: Fri, 18 Sep 2026 16:52:43 +0100 [thread overview]
Message-ID: <aq1c0ycER3b4Mfds@thinkstation> (raw)
In-Reply-To: <aq0ylDidEHa2kg4X@thinkstation>
On Fri, Sep 18, 2026 at 04:35:17PM +0100, Harry Yoo wrote:
> On Fri, Sep 18, 2026 at 07:41:56PM +0800, Hao Li wrote:
> > +/*
> > + * Exchange @sheaf, which holds fewer objects than requested, for a full one,
> > + * keeping the leftover objects in the barn's partial sheaf instead of
> > + * flushing them.
> > + *
> > + * Returns a full sheaf, or NULL if the barn cannot make one.
> > + * The returned sheaf might be @sheaf itself or a new one.
> > + */
> > +static struct slab_sheaf *barn_replace_partial_sheaf(struct kmem_cache *s,
> > + struct node_barn *barn,
> > + struct slab_sheaf *sheaf)
> > +{
> > + struct slab_sheaf *full = NULL, *partial;
> > + unsigned int to_move;
> > + unsigned long flags;
> > +
> > + if (!data_race(barn->nr_full) && !data_race(barn->sheaf_partial))
> > + return NULL;
> > +
> > + spin_lock_irqsave(&barn->lock, flags);
> > +
> > + partial = barn->sheaf_partial;
> > + if (partial && partial->size + sheaf->size >= s->sheaf_capacity) {
> > + /* Fill the larger one to capacity from the smaller */
> > + if (partial->size > sheaf->size)
> > + swap(partial, sheaf);
>
> Hmm but why switch sheaves when we don't have to?
> Sounds like we're losing cache affinity unnecessarily.
>
> I think we should try to refill from barn->sheaf_partial,
> or if that's not available, refill from a full sheaf, and then move
> the previously-full-sheaf to barn->sheaf_partial or barn->sheaf_empty.
>
> Then we'll never replace the sheaf with a new one.
>
> With that, the control flow could be simplified quite a bit.
> Something like this. (Warning: pseudocode, it won't compile)
>
> // refill a sheaf from barn.
> // return true when the sheaf becomes full
> // return false when the sheaf is not full
>
> bool refill_sheaf_from_barn(s, sheaf) {
> struct node_barn *barn = get_barn(s);
> struct slab_sheaf *partial;
> unsigned int to_move;
> unsigned long flags;
>
> spin_lock_irqsave(&barn->lock, flags);
>
> partial = barn->sheaf_partial;
> barn->sheaf_partial = NULL;
>
> if (!partial && barn->nr_full) {
> // grab one from full list
> partial = [...];
> }
>
> if (!partial)
> // cannot refill from the barn. the caller will try
> // refilling from n->partial list
> goto done;
>
> to_move = min(s->sheaf_capacity - sheaf->size, partial->size);
> partial->size -= to_move;
> // copy `to_move` objects from `partial` to `sheaf`
> memcpy(...);
> sheaf->size += to_move;
Hmm, but if it's from barn->sheaf_partial, it might end up refilling
the sheaf from n->partial. Needs bit more thoughts. Perhaps retry if
it's still not full?
> if (!partial->size)
> // move it to empty list
> else
> barn->sheaf_partial = partial;
>
> done:
> spin_unlock_irqrestore(&barn->lock, flags);
> return (sheaf->size == s->sheaf_capacity);
> }
>
> What do you think?
>
> --
> Cheers,
> Harry / Hyeonggon
--
Cheers,
Harry / Hyeonggon
prev parent reply other threads:[~2026-09-18 15:52 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 11:41 Hao Li
2026-09-18 15:35 ` Harry Yoo
2026-09-18 15:52 ` Harry Yoo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aq1c0ycER3b4Mfds@thinkstation \
--to=harry@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=cl@gentwo.org \
--cc=hao.li@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=vbabka@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®