libpas background-prefault allocator
Component: libpas / bmalloc | 954ff13
Source/JavaScriptCore/runtime/JSONObject.cpp
Source/WTF/wtf/FastMalloc.cpp
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 │
Significance
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.
Audit directions
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:
- realloc and shrink paths (StringImpl
adopt,toStringImplMallocSpan, and JSONObject'sshrink/shrinkToFit) for size or range mismatches between registered requests and the actual allocation, partial frees, and interior pointers; - the request-queue overflow path when all slots are full;
- PGM large deallocation, which has distinct guard-page layouts;
- any StringImpl buffer allocated through the new prefault allocator but freed through a generic path.