Skip to content

Commit ffc498a

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 fbbb2c0 commit ffc498a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/vmm/src/builder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ pub fn build_microvm_for_boot(
266266
.map_err(VmmError::Vm)?;
267267
}
268268

269+
#[cfg(target_arch = "x86_64")]
270+
vmm.vm.set_memory_private().map_err(VmmError::Vm)?;
271+
269272
if let Some(swiotlb) = swiotlb {
270273
vmm.vm
271274
.register_swiotlb_region(swiotlb)

src/vmm/src/vstate/vm.rs

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

16-
use kvm_bindings::kvm_create_guest_memfd;
16+
use kvm_bindings::{KVM_MEMORY_ATTRIBUTE_PRIVATE, kvm_create_guest_memfd, kvm_memory_attributes};
1717
use kvm_ioctls::{Cap, VmFd};
1818
use userfaultfd::{FeatureFlags, Uffd, UffdBuilder};
1919
use vmm_sys_util::eventfd::EventFd;
@@ -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
@@ -310,6 +312,26 @@ impl Vm {
310312
self.common.swiotlb_regions.num_regions() > 0
311313
}
312314

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

0 commit comments

Comments
 (0)