← All reports

libpas background-prefault allocator

Component: libpas / bmalloc | 954ff13

Source/JavaScriptCore/runtime/JSONObject.cpp

- Vector<CharType, dynamicBufferInlineCapacity, CrashOnOverflow, 16, WTF::StringImplMalloc> m_dynamicBuffer;
+ Vector<CharType, dynamicBufferInlineCapacity, CrashOnOverflow, 16, StringImplMallocWithBackgroundPrefault> m_dynamicBuffer;

Source/WTF/wtf/FastMalloc.cpp

+PRESERVE_MOST void* fastMallocWithBackgroundPrefault(size_t size, bmalloc::CompactAllocationMode mode)
+{
+ ...
+ void* result = bmalloc::api::mallocWithBackgroundPrefault(size, mode);
+ ...
+}

libpas is the allocator behind bmalloc and WebKit's FastMalloc. Large allocations are backed by fresh virtual memory, and the kernel maps physical pages only on first touch, so a buffer that fills slowly pays one page fault per page on the mutator thread. A prefault thread already existed to warm up JSC MarkedBlocks.

This commit generalizes that thread with bmalloc_try_allocate_auxiliary_with_background_prefault. Large allocations are registered in fixed-size fault-in request slots, and the prefault thread populates their pages in the background via madvise. The large-free paths (pas_try_deallocate_known_large, pas_try_deallocate_pgm_large) now scan the slots and either unregister a queued request or wait for an in-flight one; the tests also cover cancellation on reallocate. The first adopter is JSON.stringify, whose dynamic output buffer moves to a new StringImplMallocWithBackgroundPrefault allocator.

Main thread                      Prefault thread
  ├─ try_allocate_..._prefault(sz)    │
  │   └─► large alloc + request slot ─┤ (slot filled, thread woken)
  ├─ write buffer (slow fill)         ├─ madvise populate chunks (no write)
  ├─ free / realloc(ptr)              │
  │   └─► scan slots for ptr range    │
  │        ├─ queued    → clear slot  │
  │        └─ in-flight → wait ◄──────┤ finish chunk, signal
  └─► release pages to heap           │

The change moves first-touch page-fault cost for large, progressively filled buffers off the main thread, which speeds up large JSON.stringify results. The prefault thread becomes a general-purpose service, adding a new free-vs-populate synchronization contract inside the allocator. A second thread now operates on heap memory the allocator may be freeing, decommitting, or reallocating at the same time. A populate request must never outlive the allocation it targets, and must never clobber contents the mutator has already written.

The cancel/wait protocol is the first place to look. Probe for a window between slot lookup and madvise in which the range is freed and reused, including ABA reuse of the same address by a new allocation or a different request. Because the free path now blocks on the prefault thread, check for lock-order inversions against the heap lock, the scavenger, and any large-allocation bookkeeping locks held during free.

The tests FaultInTakesBackDecommittedPages and FaultInMarksAgedPagesInUse show that populating interacts with page commit state. Verify that populating cannot recommit pages the scavenger or a concurrent free has just returned, and cannot desync committed/decommitted accounting. Confirm per platform (Darwin vs Linux) that pas_page_malloc_fault_in_without_writing never clobbers bytes the main thread already wrote.

Further out, check: