Skip to content

Commit 4e1d978

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 8b718e2 commit 4e1d978

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
@@ -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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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::{KVM_MEMORY_ATTRIBUTE_PRIVATE, kvm_create_guest_memfd, kvm_userspace_memory_region, kvm_memory_attributes};
1616
use kvm_ioctls::{Cap, VmFd};
1717
use userfaultfd::{FeatureFlags, Uffd, UffdBuilder};
1818
use vmm_sys_util::eventfd::EventFd;
@@ -67,6 +67,8 @@ pub enum VmError {
6767
GuestMemfd(kvm_ioctls::Error),
6868
/// guest_memfd is not supported on this host kernel.
6969
GuestMemfdNotSupported,
70+
/// Failed to set memory attributes to private: {0}
71+
SetMemoryAttributes(kvm_ioctls::Error),
7072
}
7173

7274
/// Contains Vm functions that are usable across CPU architectures
@@ -312,6 +314,26 @@ impl Vm {
312314
self.common.swiotlb_regions.num_regions() > 0
313315
}
314316

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

0 commit comments

Comments
 (0)