diff --git a/core/hakmem_tiny_free.inc b/core/hakmem_tiny_free.inc index fe2bfdb0..59293434 100644 --- a/core/hakmem_tiny_free.inc +++ b/core/hakmem_tiny_free.inc @@ -1,4 +1,5 @@ #include +#include #include "tiny_remote.h" #include "slab_handle.h" #include "tiny_refill.h" diff --git a/core/tiny_superslab_alloc.inc.h b/core/tiny_superslab_alloc.inc.h index b552e9a3..b866c059 100644 --- a/core/tiny_superslab_alloc.inc.h +++ b/core/tiny_superslab_alloc.inc.h @@ -165,8 +165,13 @@ static SuperSlab* superslab_refill(int class_idx) { SuperSlab* current_chunk = head->current_chunk; if (current_chunk) { // Check if current chunk has available slabs + // Bitmap semantics: 0=FREE, 1=OCCUPIED + // - 0x00000000 = all free (32 available) + // - 0xFFFFFFFF = all occupied (0 available) int chunk_cap = ss_slabs_capacity(current_chunk); - if (current_chunk->slab_bitmap != 0x00000000) { + uint32_t full_mask = (chunk_cap >= 32) ? 0xFFFFFFFF : ((1U << chunk_cap) - 1); + + if (current_chunk->slab_bitmap != full_mask) { // Current chunk has free slabs, use normal refill logic below // (Will be handled by existing code that checks tls->ss) if (tls->ss != current_chunk) { @@ -174,28 +179,48 @@ static SuperSlab* superslab_refill(int class_idx) { tls->ss = current_chunk; } } else { - // Current chunk exhausted (bitmap = 0x00000000), try to expand + // Current chunk exhausted (all slabs occupied), try to expand extern __thread int g_hakmem_lock_depth; g_hakmem_lock_depth++; - fprintf(stderr, "[HAKMEM] SuperSlab chunk exhausted for class %d (bitmap=0x00000000), expanding...\n", class_idx); + fprintf(stderr, "[HAKMEM] SuperSlab chunk exhausted for class %d (bitmap=0x%08x), expanding...\n", + class_idx, current_chunk->slab_bitmap); g_hakmem_lock_depth--; - // Try to expand by allocating a new chunk - if (expand_superslab_head(head) < 0) { + // Protect expansion with global lock (race condition fix) + static pthread_mutex_t expand_lock = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&expand_lock); + + // Re-check after acquiring lock (another thread may have expanded) + current_chunk = head->current_chunk; + uint32_t recheck_mask = (ss_slabs_capacity(current_chunk) >= 32) ? 0xFFFFFFFF : + ((1U << ss_slabs_capacity(current_chunk)) - 1); + + if (current_chunk->slab_bitmap == recheck_mask) { + // Still exhausted, expand now + if (expand_superslab_head(head) < 0) { + pthread_mutex_unlock(&expand_lock); + g_hakmem_lock_depth++; + fprintf(stderr, "[HAKMEM] CRITICAL: Failed to expand SuperSlabHead for class %d (system OOM)\n", class_idx); + g_hakmem_lock_depth--; + return NULL; // True system OOM + } + g_hakmem_lock_depth++; - fprintf(stderr, "[HAKMEM] CRITICAL: Failed to expand SuperSlabHead for class %d (system OOM)\n", class_idx); + fprintf(stderr, "[HAKMEM] Successfully expanded SuperSlabHead for class %d\n", class_idx); g_hakmem_lock_depth--; - return NULL; // True system OOM } - // Update current_chunk and tls->ss to point to new chunk + // Update current_chunk and tls->ss to point to (potentially new) chunk current_chunk = head->current_chunk; tls->ss = current_chunk; + pthread_mutex_unlock(&expand_lock); - // Verify new chunk has free slabs - if (!current_chunk || current_chunk->slab_bitmap == 0x00000000) { + // Verify chunk has free slabs + full_mask = (ss_slabs_capacity(current_chunk) >= 32) ? 0xFFFFFFFF : + ((1U << ss_slabs_capacity(current_chunk)) - 1); + if (!current_chunk || current_chunk->slab_bitmap == full_mask) { g_hakmem_lock_depth++; - fprintf(stderr, "[HAKMEM] CRITICAL: New chunk still has no free slabs for class %d\n", class_idx); + fprintf(stderr, "[HAKMEM] CRITICAL: Chunk still has no free slabs for class %d after expansion\n", class_idx); g_hakmem_lock_depth--; return NULL; }