From d06351f31c2b7e25d7a1b93b6801ea90f20fdf0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gr=C3=A9goire?= Date: Thu, 20 Aug 2026 21:59:28 -0400 Subject: [PATCH] mm/zone: defer free slab destruction --- src/mm/zone.zig | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/mm/zone.zig b/src/mm/zone.zig index 4f3e8ce..df7f14c 100644 --- a/src/mm/zone.zig +++ b/src/mm/zone.zig @@ -500,10 +500,12 @@ pub const Zone = struct { chunk_size: usize, /// Zone lock. lock: ke.Mutex, - /// List of full slabs in this zone. + /// List of full slabs (fully allocated) in this zone. full_slabs: rtl.List, - /// List of partial slabs in this zone. + /// List of partial slabs (partially allocated) in this zone. partial_slabs: rtl.List, + /// List of free slabs (fully unallocated) in this zone. + free_slabs: rtl.List, /// Linkage into the global zone list. next: ?*Zone, depot_lock: ke.QSpinLock, @@ -559,6 +561,7 @@ pub const Zone = struct { self.full_slabs.init(); self.partial_slabs.init(); + self.free_slabs.init(); self.name = name; self.obj_size = size; @@ -930,7 +933,7 @@ pub const Zone = struct { if (slab.refcount == 0) { // No more outstanding allocations, it is safe to reclaim the slab. slab.link.remove(); - self.slab_destroy(slab); + self.free_slabs.insert_tail(&slab.link); return; } } @@ -1066,7 +1069,20 @@ pub const Zone = struct { self.maglist_destroy(depot.full_mags.head, magazine_size.load()); } + // Destroy all free slabs. Swap the list under the lock. + const head = &self.free_slabs.head; + var entry = self.free_slabs.first(); + self.free_slabs.init(); + self.lock.release(); + + while (true) { + const slab: *Slab = @fieldParentPtr("link", entry); + const next = entry.next; + self.slab_destroy(slab); + if (next == head) break; + entry = next; + } } /// Called when memory is low and we need to make memory ASAP.