diff --git a/CHANGELOG.md b/CHANGELOG.md index 22dbeb125..7e1a5ddc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). * Fix restore on WHP not rewinding the guest TSC on hosts with `TSC_ADJUST` support. * HVF sandbox initialization accepts seeds that match Apple hypercall identifiers. * Host-written snapshot memory has instruction-cache coherence before guest execution. +* Fix slow scratch zeroing on restore under KVM in default builds. ### Removed * **Breaking:** `SandboxBuilder::init_data`, `GuestEnvironment`, `GuestBlob`, diff --git a/src/hyperlight_host/src/mem/shared_mem.rs b/src/hyperlight_host/src/mem/shared_mem.rs index 2da3c44df..02b8d6cf8 100644 --- a/src/hyperlight_host/src/mem/shared_mem.rs +++ b/src/hyperlight_host/src/mem/shared_mem.rs @@ -30,6 +30,8 @@ use windows::core::PCSTR; use super::memory_region::{ HostGuestMemoryRegion, MemoryRegion, MemoryRegionFlags, MemoryRegionKind, MemoryRegionType, }; +#[cfg(kvm)] +use crate::hypervisor::virtual_machine::{HypervisorType, get_available_hypervisor}; use crate::log_then_return; type Result = core::result::Result; @@ -1510,8 +1512,8 @@ impl HostSharedMemory { /// Reset this memory region to all-zeros, choosing the fastest /// strategy for the current platform and hypervisor configuration. /// - /// On Linux/KVM (without mshv3), uses `MADV_DONTNEED` for lazy - /// zeroing. On Linux/mshv3, falls through to `fill(0)`. + /// When the running hypervisor is KVM, uses `MADV_DONTNEED` for + /// lazy zeroing. Otherwise, uses `fill(0)`. /// /// On Windows, zeroing via `fill(0)` is prohibitively expensive /// for large regions (e.g. 448 MiB scratch). Instead, the @@ -1534,24 +1536,24 @@ impl HostSharedMemory { #[cfg(not(target_os = "windows"))] { self.with_exclusivity(|e| { - #[allow(unused_mut)] - let mut do_copy = true; // TODO: Find a similar lazy zeroing approach that works on MSHV. // (See Note [Keeping mappings in sync between userspace and the guest]) - #[cfg(all(feature = "kvm", not(any(feature = "mshv3"))))] - unsafe { - let ret = libc::madvise( - e.region.ptr() as *mut libc::c_void, - e.region.size(), - libc::MADV_DONTNEED, - ); + #[cfg(kvm)] + if matches!(get_available_hypervisor(), Some(HypervisorType::Kvm)) { + // SAFETY: `with_exclusivity` holds the write lock on this private anonymous + // mapping. KVM MMU notifiers make the guest also read zeros after discard. + let ret = unsafe { + libc::madvise( + e.region.ptr() as *mut libc::c_void, + e.region.size(), + libc::MADV_DONTNEED, + ) + }; if ret == 0 { - do_copy = false; + return; } } - if do_copy { - e.as_mut_slice().fill(0); - } + e.as_mut_slice().fill(0); })?; Ok(None) }