From: Harry Yoo <harry.yoo@oracle.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: linux-mm@kvack.org, Dmitry Vyukov <dvyukov@google.com>,
lkmm@lists.linux.dev, linux-arch@vger.kernel.org,
linux-kernel@vger.kernel.org,
Joel Fernandes <joelagnelf@nvidia.com>,
Daniel Lustig <dlustig@nvidia.com>,
Akira Yokosawa <akiyks@gmail.com>,
"Paul E. McKenney" <paulmck@kernel.org>,
Luc Maranget <luc.maranget@inria.fr>,
Jade Alglave <j.alglave@ucl.ac.uk>,
David Howells <dhowells@redhat.com>,
Nicholas Piggin <npiggin@gmail.com>,
Boqun Feng <boqun@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Will Deacon <will@kernel.org>,
Andrea Parri <parri.andrea@gmail.com>,
Pedro Falcato <pfalcato@suse.de>,
Vlastimil Babka <vbabka@suse.cz>,
Christoph Lameter <cl@gentwo.org>,
David Rientjes <rientjes@google.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
Hao Li <hao.li@linux.dev>, Shakeel Butt <shakeel.butt@linux.dev>,
Venkat Rao Bagalkote <venkat88@linux.ibm.com>,
Mateusz Guzik <mjguzik@gmail.com>,
Suren Baghdasaryan <surenb@google.com>,
Marco Elver <elver@google.com>
Subject: Re: [BUG] Memory ordering between kmalloc() and kfree()? it's confusing!
Date: Fri, 27 Feb 2026 21:36:37 +0900 [thread overview]
Message-ID: <aaGP1Se8vkBhn9uj@hyeyoo> (raw)
In-Reply-To: <9240040d-598e-48bd-b802-bf91cce92d0c@rowland.harvard.edu>
On Thu, Feb 26, 2026 at 01:06:51PM -0500, Alan Stern wrote:
> On Fri, Feb 27, 2026 at 02:11:49AM +0900, Harry Yoo wrote:
> > On Thu, Feb 26, 2026 at 11:42:02AM -0500, Alan Stern wrote:
> > > On Fri, Feb 27, 2026 at 01:17:52AM +0900, Harry Yoo wrote:
> > > > On Thu, Feb 26, 2026 at 10:45:55AM -0500, Alan Stern wrote:
> > > > > On Thu, Feb 26, 2026 at 03:35:08PM +0900, Harry Yoo wrote:
> > > > > > Because the slab allocator itself doesn't guarantee that such
> > > > > > barriers are invoked within the allocator, it relies on users to
> > > > > > do this when needed.
> > > > >
> > > > > It doesn't? Then how does the slab allocator guarantee that two
> > > > > different CPUs won't try to perform allocations or deallocations from
> > > > > the same slab at the same time, messing everything up?
> > > >
> > > > Ah, alloc/free slowpaths do use cmpxchg128 or spinlock and
> > > > don't mess things up.
> > > >
> > > > But fastpath allocs/frees are served from percpu array that is protected
> > > > by a local_lock. local_lock has a compiler barrier in it, but that's
> > > > not enough.
> > >
> > > If those things rely on a percpu array, how can one CPU possibly
> > > manipulate a resource (slab or something else) that was changed by a
> > > different CPU?
> >
> > AFAICT that shouldn't happen within the slab allocator.
> >
> > > The whole point of percpu data structures is that each
> > > CPU gets its own copy.
> >
> > Exactly.
> >
> > But I'm not talking about what happens within the allocator,
> > but rather, about what slab expects to happen outside the allocator.
>
> I understand.
>
> > Something like this:
> >
> > CPU X CPU Y
> > ptr = kmalloc();
> > WRITE_ONCE(gp, ptr);
> > if (p = READ_ONCE(gp))
> > kfree(p);
> >
> > Yes, it's a crazy thing to do. CPU Y isn't guaranteed to see
> > up-to-date version of object content or metadata.
> >
> > Instead, the code should do:
> >
> > CPU X CPU Y
> > ptr = kmalloc();
> > gp = smp_store_release(&gp, ptr);
> >
> > if (p = smp_load_acquire(&gp))
> > kfree(p);
> >
> > One reason that I started this discussion was to argue that we should
> > have a well-defined a contract between the slab allocator and its users.
>
> Yes, you have made that quite clear. But you're missing _my_ point.
Okay. Looks like I misread your point...
> Which is: The same mechanism that the slab allocator uses to ensure that
> CPU X and CPU Y won't step on each other's toes if they both run
> kmalloc/kfree at the same time should also be able to guarantee that the
> metadata changes made by CPU X will be visible to CPU Y if Y manipulates
> a slab that X just finished with.
Within the slab allocator, I believe there are sufficient mechanisms
(either spinlock or cmpxchg) to prevent CPUs from interfereing
with each other.
My earlier statement "Because the slab allocator itself doesn't
guarantee such barriers are invoked within the allocator, ..." may have
caused some confusion. To be clarify, the slab allocator of course uses
proper locks and atomic operations to avoid CPUs interfereing with each
other, and yes, those mechanisms should guarantee that the metadata changes
made by CPU X will be visible to CPU Y e.g) when the object is transferred
from CPU X to Y within the slab allocator.
What I meant by the statement was that the slab allocator doesn't provide
enough barriers to ensure correctness when a user performs a drive-by
free on a different CPU w/o proper barriers.
Hopefully I'm not missing your point this time :)
Thanks!
> To put it another way, ensuring non-interference during simultaneous
> accesses isn't all that different from ensuring coherence during
> sequential accesses. Doing the first should easily allow doing the
> second.
>
> And if it doesn't then something questionable is going on.
--
Cheers,
Harry / Hyeonggon
next prev parent reply other threads:[~2026-02-27 12:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-26 6:35 Harry Yoo
2026-02-26 15:45 ` Alan Stern
2026-02-26 16:17 ` Harry Yoo
2026-02-26 16:42 ` Alan Stern
2026-02-26 17:11 ` Harry Yoo
2026-02-26 18:06 ` Alan Stern
2026-02-27 12:36 ` Harry Yoo [this message]
2026-02-27 17:00 ` Alan Stern
2026-02-26 17:59 ` Christoph Lameter (Ampere)
2026-02-27 8:06 ` Hao Li
2026-02-27 9:03 ` Harry Yoo
2026-02-27 9:14 ` Akira Yokosawa
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=aaGP1Se8vkBhn9uj@hyeyoo \
--to=harry.yoo@oracle.com \
--cc=akiyks@gmail.com \
--cc=boqun@kernel.org \
--cc=cl@gentwo.org \
--cc=dhowells@redhat.com \
--cc=dlustig@nvidia.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=hao.li@linux.dev \
--cc=j.alglave@ucl.ac.uk \
--cc=joelagnelf@nvidia.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lkmm@lists.linux.dev \
--cc=luc.maranget@inria.fr \
--cc=mjguzik@gmail.com \
--cc=npiggin@gmail.com \
--cc=parri.andrea@gmail.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=pfalcato@suse.de \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=stern@rowland.harvard.edu \
--cc=surenb@google.com \
--cc=vbabka@suse.cz \
--cc=venkat88@linux.ibm.com \
--cc=will@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