diff --git a/vm-virtio/src/vhost_user/fs.rs b/vm-virtio/src/vhost_user/fs.rs index 4ed59c392..4bfbbecd7 100644 --- a/vm-virtio/src/vhost_user/fs.rs +++ b/vm-virtio/src/vhost_user/fs.rs @@ -28,6 +28,7 @@ use vhost_rs::vhost_user::{ use vhost_rs::VhostBackend; use vm_memory::{ Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap, + MmapRegion, }; use vm_migration::{Migratable, MigratableError, Pausable, Snapshottable, Transportable}; use vmm_sys_util::eventfd::EventFd; @@ -258,7 +259,9 @@ pub struct Fs { config: VirtioFsConfig, kill_evt: Option, pause_evt: Option, - cache: Option<(VirtioSharedMemoryList, u64)>, + // Hold ownership of the memory that is allocated for the device + // which will be automatically dropped when the device is dropped + cache: Option<(VirtioSharedMemoryList, u64, MmapRegion)>, slave_req_support: bool, queue_evts: Option>, interrupt_cb: Option>, @@ -273,7 +276,7 @@ impl Fs { tag: &str, req_num_queues: usize, queue_size: u16, - cache: Option<(VirtioSharedMemoryList, u64)>, + cache: Option<(VirtioSharedMemoryList, u64, MmapRegion)>, ) -> Result { let mut slave_req_support = false; @@ -471,7 +474,7 @@ impl VirtioDevice for Fs { // Initialize slave communication. let slave_req_handler = if self.slave_req_support { - if let Some(cache) = self.cache.clone() { + if let Some(cache) = self.cache.as_ref() { let vu_master_req_handler = Arc::new(Mutex::new(SlaveReqHandler { cache_offset: cache.0.addr, cache_size: cache.0.len, @@ -542,8 +545,8 @@ impl VirtioDevice for Fs { } fn get_shm_regions(&self) -> Option { - if let Some(cache) = self.cache.clone() { - Some(cache.0) + if let Some(cache) = self.cache.as_ref() { + Some(cache.0.clone()) } else { None } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 27330f9cc..594c374a1 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1331,7 +1331,7 @@ impl DeviceManager { if let Some(fs_list_cfg) = &self.config.lock().unwrap().fs { for fs_cfg in fs_list_cfg.iter() { if let Some(fs_sock) = fs_cfg.sock.to_str() { - let cache: Option<(VirtioSharedMemoryList, u64)> = if fs_cfg.dax { + let cache = if fs_cfg.dax { let fs_cache = fs_cfg.cache_size; // The memory needs to be 2MiB aligned in order to support // hugepages. @@ -1356,8 +1356,6 @@ impl DeviceManager { .map_err(DeviceManagerError::NewMmapRegion)?; let addr: u64 = mmap_region.as_ptr() as u64; - self._mmap_regions.push(mmap_region); - self.memory_manager .lock() .unwrap() @@ -1383,6 +1381,7 @@ impl DeviceManager { region_list, }, addr, + mmap_region, )) } else { None