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

From: Alan Stern

Date: Thu Feb 26 2026 - 12:06:56 EST


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? The whole point of percpu data structures is that each
CPU gets its own copy.

Alan Stern