Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
32 changes: 17 additions & 15 deletions src/hyperlight_host/src/mem/shared_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = core::result::Result<T, SharedMemoryError>;
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Loading