diff --git a/vm-virtio/src/pmem.rs b/vm-virtio/src/pmem.rs index 87a97e47a..4ab05a4f8 100644 --- a/vm-virtio/src/pmem.rs +++ b/vm-virtio/src/pmem.rs @@ -8,8 +8,8 @@ use super::Error as DeviceError; use super::{ - ActivateError, ActivateResult, DescriptorChain, DeviceEventT, Queue, VirtioDevice, - VirtioDeviceType, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1, + ActivateError, ActivateResult, DescriptorChain, DeviceEventT, Queue, UserspaceMapping, + VirtioDevice, VirtioDeviceType, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1, }; use crate::{VirtioInterrupt, VirtioInterruptType}; use epoll; @@ -322,6 +322,7 @@ pub struct Pmem { interrupt_cb: Option>, epoll_threads: Option>>>, paused: Arc, + mapping: UserspaceMapping, // Hold ownership of the memory that is allocated for the device // which will be automatically dropped when the device is dropped @@ -332,6 +333,7 @@ impl Pmem { pub fn new( disk: File, addr: GuestAddress, + mapping: UserspaceMapping, _region: MmapRegion, iommu: bool, ) -> io::Result { @@ -357,6 +359,7 @@ impl Pmem { interrupt_cb: None, epoll_threads: None, paused: Arc::new(AtomicBool::new(false)), + mapping, _region, }) } @@ -513,6 +516,10 @@ impl VirtioDevice for Pmem { self.queue_evts.take().unwrap(), )) } + + fn userspace_mappings(&self) -> Vec { + vec![self.mapping.clone()] + } } virtio_pausable!(Pmem); diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 96b93c8ed..0c5bd696b 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1569,22 +1569,31 @@ impl DeviceManager { }, ) .map_err(DeviceManagerError::NewMmapRegion)?; - let addr: u64 = mmap_region.as_ptr() as u64; + let host_addr: u64 = mmap_region.as_ptr() as u64; - self.memory_manager + let mem_slot = self + .memory_manager .lock() .unwrap() .create_userspace_mapping( pmem_guest_addr.raw_value(), size, - addr, + host_addr, pmem_cfg.mergeable, pmem_cfg.discard_writes, ) .map_err(DeviceManagerError::MemoryManager)?; + let mapping = vm_virtio::UserspaceMapping { + host_addr, + mem_slot, + addr: pmem_guest_addr, + len: size, + mergeable: pmem_cfg.mergeable, + }; + let virtio_pmem_device = Arc::new(Mutex::new( - vm_virtio::Pmem::new(file, pmem_guest_addr, mmap_region, pmem_cfg.iommu) + vm_virtio::Pmem::new(file, pmem_guest_addr, mapping, mmap_region, pmem_cfg.iommu) .map_err(DeviceManagerError::CreateVirtioPmem)?, ));