Re: [BUG] Memory ordering between kmalloc() and kfree()? it's confusing!

From: Alan Stern

Date: Fri Feb 27 2026 - 12:05:29 EST


On Fri, Feb 27, 2026 at 09:36:37PM +0900, Harry Yoo wrote:
> > 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 :)

You got it. :-)

But since I don't know anything about the details of how the slab
allocator works, can you explain in more detail what the locks and
atomic operations are and how they prevent CPUs from interfering when an
object is transferred from one CPU to another within the slab allocator?

In particular, which part of the mechanism fails (or doesn't get used)
when the object is transferred by the user with no memory barriers? I'm
trying to learn exactly how these two cases differ, because at first
glance I can't imagine how you could accomplish the first without also
accomplishing the second. It seems that transferring an object from one
CPU to another within the slab allocator should be very much like
transferring it from the slab allocator back to the kmalloc caller.

Alan Stern