Skip to content

Commit 7a8f8d9

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 <roypat@amazon.co.uk>
1 parent 1349cf5 commit 7a8f8d9

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
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: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ 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,
17-
kvm_userspace_memory_region, kvm_userspace_memory_region2,
16+
KVM_MEM_GUEST_MEMFD, KVM_MEM_LOG_DIRTY_PAGES, KVM_MEMORY_ATTRIBUTE_PRIVATE,
17+
kvm_create_guest_memfd, kvm_memory_attributes, kvm_userspace_memory_region,
18+
kvm_userspace_memory_region2,
1819
};
1920
use kvm_ioctls::{Cap, VmFd};
2021
use vmm_sys_util::eventfd::EventFd;
@@ -68,6 +69,8 @@ pub enum VmError {
6869
GuestMemfd(kvm_ioctls::Error),
6970
/// guest_memfd is not supported on this host kernel.
7071
GuestMemfdNotSupported,
72+
/// Failed to set memory attributes to private: {0}
73+
SetMemoryAttributes(kvm_ioctls::Error),
7174
}
7275

7376
/// Contains Vm functions that are usable across CPU architectures
@@ -276,6 +279,28 @@ impl Vm {
276279
&self.common.guest_memory
277280
}
278281

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

0 commit comments

Comments
 (0)