From f7197e84150e41f1acc533e7d03ef0a36d51be86 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 19 Mar 2020 10:13:53 +0000 Subject: [PATCH] vmm: Add a "discard_writes=" to --pmem This opens the backing file read-only, makes the pages in the mmap() read-only and also makes the KVM mapping read-only. The file is also mapped with MAP_PRIVATE to make the changes local to this process only. This is functional alternative to having support for making a virtio-pmem device readonly. Unfortunately there is no concept of readonly virtio-pmem (or any type of NVDIMM/PMEM) in the Linux kernel so to be able to have a block device that is appears readonly in the guest requires significant specification and kernel changes. Signed-off-by: Rob Bradford --- src/main.rs | 2 +- vmm/src/api/openapi/cloud-hypervisor.yaml | 3 +++ vmm/src/config.rs | 6 ++++++ vmm/src/device_manager.rs | 15 ++++++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 502711b93..8cfa22811 100755 --- a/src/main.rs +++ b/src/main.rs @@ -173,7 +173,7 @@ fn create_app<'a, 'b>( .help( "Persistent memory parameters \ \"file=,size=,iommu=on|off,\ - mergeable=on|off\"", + mergeable=on|off,discard_writes=on|off,\"", ) .takes_value(true) .min_values(1) diff --git a/vmm/src/api/openapi/cloud-hypervisor.yaml b/vmm/src/api/openapi/cloud-hypervisor.yaml index af3b23bd9..fb3ec6dc6 100644 --- a/vmm/src/api/openapi/cloud-hypervisor.yaml +++ b/vmm/src/api/openapi/cloud-hypervisor.yaml @@ -410,6 +410,9 @@ components: mergeable: type: boolean default: false + discard_writes: + type: boolean + default: false ConsoleConfig: required: diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 207e441ad..e09c4033c 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -766,6 +766,8 @@ pub struct PmemConfig { pub iommu: bool, #[serde(default)] pub mergeable: bool, + #[serde(default)] + pub discard_writes: bool, } impl PmemConfig { @@ -777,6 +779,7 @@ impl PmemConfig { let mut size_str: &str = ""; let mut iommu_str: &str = ""; let mut mergeable_str: &str = ""; + let mut discard_writes_str: &str = ""; for param in params_list.iter() { if param.starts_with("file=") { @@ -787,6 +790,8 @@ impl PmemConfig { iommu_str = ¶m[6..]; } else if param.starts_with("mergeable=") { mergeable_str = ¶m[10..]; + } else if param.starts_with("discard_writes=") { + discard_writes_str = ¶m[15..]; } } @@ -799,6 +804,7 @@ impl PmemConfig { size: parse_size(size_str)?, iommu: parse_on_off(iommu_str)?, mergeable: parse_on_off(mergeable_str)?, + discard_writes: parse_on_off(discard_writes_str)?, }) } } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index bb6b26c53..5ab477df0 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -27,7 +27,7 @@ use arch::layout::{APIC_START, IOAPIC_SIZE, IOAPIC_START}; use devices::{ioapic, BusDevice, HotPlugNotificationFlags}; use kvm_ioctls::*; use libc::TIOCGWINSZ; -use libc::{MAP_NORESERVE, MAP_SHARED, O_RDONLY, O_TMPFILE, PROT_READ, PROT_WRITE}; +use libc::{MAP_NORESERVE, MAP_PRIVATE, MAP_SHARED, O_TMPFILE, PROT_READ, PROT_WRITE}; #[cfg(feature = "pci_support")] use pci::{ DeviceRelocation, PciBarRegionType, PciBus, PciConfigIo, PciConfigMmio, PciDevice, PciRoot, @@ -1392,7 +1392,7 @@ impl DeviceManager { let file = OpenOptions::new() .read(true) - .write(true) + .write(!pmem_cfg.discard_writes) .custom_flags(custom_flags) .open(&pmem_cfg.file) .map_err(DeviceManagerError::PmemFileOpen)?; @@ -1406,12 +1406,17 @@ impl DeviceManager { let mmap_region = MmapRegion::build( Some(FileOffset::new(cloned_file, 0)), size as usize, - if pmem_cfg.readonly { + if pmem_cfg.discard_writes { PROT_READ } else { PROT_READ | PROT_WRITE }, - MAP_NORESERVE | MAP_SHARED, + MAP_NORESERVE + | if pmem_cfg.discard_writes { + MAP_PRIVATE + } else { + MAP_SHARED + }, ) .map_err(DeviceManagerError::NewMmapRegion)?; let addr: u64 = mmap_region.as_ptr() as u64; @@ -1426,7 +1431,7 @@ impl DeviceManager { size, addr, pmem_cfg.mergeable, - false, + pmem_cfg.discard_writes, ) .map_err(DeviceManagerError::MemoryManager)?;