Add a pooling allocator mode based on copy-on-write mappings of memfds.
As first suggested by Jan on the Zulip here [1], a cheap and effective way to obtain copy-on-write semantics of a "backing image" for a Wasm memory is to mmap a file with `MAP_PRIVATE`. The `memfd` mechanism provided by the Linux kernel allows us to create anonymous, in-memory-only files that we can use for this mapping, so we can construct the image contents on-the-fly then effectively create a CoW overlay. Furthermore, and importantly, `madvise(MADV_DONTNEED, ...)` will discard the CoW overlay, returning the mapping to its original state. By itself this is almost enough for a very fast instantiation-termination loop of the same image over and over, without changing the address space mapping at all (which is expensive). The only missing bit is how to implement heap *growth*. But here memfds can help us again: if we create another anonymous file and map it where the extended parts of the heap would go, we can take advantage of the fact that a `mmap()` mapping can be *larger than the file itself*, with accesses beyond the end generating a `SIGBUS`, and the fact that we can cheaply resize the file with `ftruncate`, even after a mapping exists. So we can map the "heap extension" file once with the maximum memory-slot size and grow the memfd itself as `memory.grow` operations occur. The above CoW technique and heap-growth technique together allow us a fastpath of `madvise()` and `ftruncate()` only when we re-instantiate the same module over and over, as long as we can reuse the same slot. This fastpath avoids all whole-process address-space locks in the Linux kernel, which should mean it is highly scalable. It also avoids the cost of copying data on read, as the `uffd` heap backend does when servicing pagefaults; the kernel's own optimized CoW logic (same as used by all file mmaps) is used instead. [1] https://bytecodealliance.zulipchat.com/#narrow/stream/206238-general/topic/Copy.20on.20write.20based.20instance.20reuse/near/266657772
This commit is contained in:
@@ -95,6 +95,19 @@ impl MemoryPlan {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine whether a data segment (memory initializer) is
|
||||
/// possibly out-of-bounds. Returns `true` if the initializer has a
|
||||
/// dynamic location and this question cannot be resolved
|
||||
/// pre-instantiation; hence, this method's result should not be
|
||||
/// used to signal an error, only to exit optimized/simple fastpaths.
|
||||
pub fn initializer_possibly_out_of_bounds(&self, init: &MemoryInitializer) -> bool {
|
||||
match init.end() {
|
||||
// Not statically known, so possibly out of bounds (we can't guarantee in-bounds).
|
||||
None => true,
|
||||
Some(end) => end > self.memory.minimum * (WASM_PAGE_SIZE as u64),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A WebAssembly linear memory initializer.
|
||||
@@ -113,6 +126,16 @@ pub struct MemoryInitializer {
|
||||
pub data: Range<u32>,
|
||||
}
|
||||
|
||||
impl MemoryInitializer {
|
||||
/// If this initializer has a definite, static, non-overflowed end address, return it.
|
||||
pub fn end(&self) -> Option<u64> {
|
||||
if self.base.is_some() {
|
||||
return None;
|
||||
}
|
||||
self.offset.checked_add(self.data.len() as u64)
|
||||
}
|
||||
}
|
||||
|
||||
/// The type of WebAssembly linear memory initialization to use for a module.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub enum MemoryInitialization {
|
||||
|
||||
Reference in New Issue
Block a user