Skip to content

Commit 4ab07cb

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 5cd73e1 commit 4ab07cb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/vmm/src/builder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ pub fn build_microvm_for_boot(
264264
.register_memory_regions(guest_memory, secret_free)
265265
.map_err(VmmError::Vm)?;
266266

267+
#[cfg(target_arch = "x86_64")]
268+
vmm.vm.set_memory_private().map_err(VmmError::Vm)?;
269+
267270
if let Some(swiotlb) = swiotlb {
268271
vmm.vm
269272
.register_swiotlb_region(swiotlb)

src/vmm/src/vstate/vm.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use std::os::fd::FromRawFd;
1212
use std::path::Path;
1313
use std::sync::Arc;
1414

15-
use kvm_bindings::{kvm_create_guest_memfd, kvm_userspace_memory_region};
15+
use kvm_bindings::{
16+
KVM_MEMORY_ATTRIBUTE_PRIVATE, kvm_create_guest_memfd, kvm_memory_attributes,
17+
kvm_userspace_memory_region,
18+
};
1619
use kvm_ioctls::{Cap, VmFd};
1720
use userfaultfd::{FeatureFlags, Uffd, UffdBuilder};
1821
use vmm_sys_util::eventfd::EventFd;
@@ -67,6 +70,8 @@ pub enum VmError {
6770
GuestMemfd(kvm_ioctls::Error),
6871
/// guest_memfd is not supported on this host kernel.
6972
GuestMemfdNotSupported,
73+
/// Failed to set memory attributes to private: {0}
74+
SetMemoryAttributes(kvm_ioctls::Error),
7075
}
7176

7277
/// Contains Vm functions that are usable across CPU architectures
@@ -312,6 +317,26 @@ impl Vm {
312317
self.common.swiotlb_regions.num_regions() > 0
313318
}
314319

320+
/// Sets the memory attributes on all guest_memfd-backed regions to private
321+
pub fn set_memory_private(&self) -> Result<(), VmError> {
322+
for region in self.guest_memory().iter() {
323+
if region.inner().guest_memfd != 0 {
324+
let attr = kvm_memory_attributes {
325+
address: region.start_addr().0,
326+
size: region.len(),
327+
attributes: KVM_MEMORY_ATTRIBUTE_PRIVATE as u64,
328+
..Default::default()
329+
};
330+
331+
self.fd()
332+
.set_memory_attributes(attr)
333+
.map_err(VmError::SetMemoryAttributes)?
334+
}
335+
}
336+
337+
Ok(())
338+
}
339+
315340
/// Returns an iterator over all regions, normal and swiotlb.
316341
fn all_regions(&self) -> impl Iterator<Item = &KvmRegion> {
317342
self.guest_memory()

0 commit comments

Comments
 (0)