Re: [BUG] Memory ordering between kmalloc() and kfree()? it's confusing!
Harry Yoo <[email protected]> Fri, 27 Feb 2026 02:11:49 +0900
| Newsgroups | dev.linux.lists.lkmm,org.kernel.vger.linux-arch,org.kernel.vger.linux-kernel,org.kvack.linux-mm |
|---|---|
| Message-ID | <aaB-1e1LoxkcYl2f@hyeyoo> |
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. 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. -- Cheers, Harry / Hyeonggon