Skip to content

Commit 302b051

Browse files
committed
tmp: set memory attributes to private on x86
The current version of the mmap-support patches require that on x86, memory attributes have to be set to private even if the guest_memfd VMA is short-circuited back into the memslot (on ARM, memory attributes are not even supported in this scenario). Signed-off-by: Patrick Roy <[email protected]>
1 parent 816a073 commit 302b051

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/vmm/src/builder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ pub fn build_microvm_for_boot(
253253
.register_memory_regions(guest_memory)
254254
.map_err(VmmError::Vm)?;
255255

256+
#[cfg(target_arch = "x86_64")]
257+
vmm.vm.set_memory_private().map_err(VmmError::Vm)?;
258+
256259
let entry_point = load_kernel(
257260
MaybeBounce::new(boot_config.kernel_file.try_clone().unwrap(), secret_free),
258261
vmm.vm.guest_memory(),

src/vmm/src/vstate/vm.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::path::Path;
1313
use std::sync::Arc;
1414

1515
use kvm_bindings::{
16-
KVM_MEM_GUEST_MEMFD, KVM_MEM_LOG_DIRTY_PAGES, kvm_create_guest_memfd,
16+
KVM_MEM_GUEST_MEMFD, KVM_MEM_LOG_DIRTY_PAGES, KVM_MEMORY_ATTRIBUTE_PRIVATE, kvm_create_guest_memfd, kvm_memory_attributes,
1717
kvm_userspace_memory_region, kvm_userspace_memory_region2,
1818
};
1919
use kvm_ioctls::{Cap, VmFd};
@@ -68,6 +68,8 @@ pub enum VmError {
6868
GuestMemfd(kvm_ioctls::Error),
6969
/// guest_memfd is not supported on this host kernel.
7070
GuestMemfdNotSupported,
71+
/// Failed to set memory attributes to private: {0}
72+
SetMemoryAttributes(kvm_ioctls::Error),
7173
}
7274

7375
/// Contains Vm functions that are usable across CPU architectures
@@ -274,6 +276,24 @@ impl Vm {
274276
&self.common.guest_memory
275277
}
276278

279+
/// Sets the memory attributes on all guest_memfd-backed regions to private
280+
pub fn set_memory_private(&self) -> Result<(), VmError> {
281+
for region in self.guest_memory().iter() {
282+
let attr = kvm_memory_attributes {
283+
address: region.start_addr().0,
284+
size: region.len(),
285+
attributes: KVM_MEMORY_ATTRIBUTE_PRIVATE as u64,
286+
..Default::default()
287+
};
288+
289+
self.fd()
290+
.set_memory_attributes(attr)
291+
.map_err(VmError::SetMemoryAttributes)?
292+
}
293+
294+
Ok(())
295+
}
296+
277297
/// Resets the KVM dirty bitmap for each of the guest's memory regions.
278298
pub fn reset_dirty_bitmap(&self) {
279299
self.guest_memory()

0 commit comments

Comments
 (0)