vmm, virtio: fs: Move freeing of mappped region into device

Move the release of the managed memory region from the DeviceManager to
the vhost-user-fs device. This ensures that the memory will be freed when
the device is unplugged which will lead to it being Drop()ed.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-04-14 15:47:11 +01:00
parent 0c6706a510
commit 70ecd6bab4
2 changed files with 10 additions and 8 deletions

View File

@ -28,6 +28,7 @@ use vhost_rs::vhost_user::{
use vhost_rs::VhostBackend; use vhost_rs::VhostBackend;
use vm_memory::{ use vm_memory::{
Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap, Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap,
MmapRegion,
}; };
use vm_migration::{Migratable, MigratableError, Pausable, Snapshottable, Transportable}; use vm_migration::{Migratable, MigratableError, Pausable, Snapshottable, Transportable};
use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::eventfd::EventFd;
@ -258,7 +259,9 @@ pub struct Fs {
config: VirtioFsConfig, config: VirtioFsConfig,
kill_evt: Option<EventFd>, kill_evt: Option<EventFd>,
pause_evt: Option<EventFd>, pause_evt: Option<EventFd>,
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, slave_req_support: bool,
queue_evts: Option<Vec<EventFd>>, queue_evts: Option<Vec<EventFd>>,
interrupt_cb: Option<Arc<dyn VirtioInterrupt>>, interrupt_cb: Option<Arc<dyn VirtioInterrupt>>,
@ -273,7 +276,7 @@ impl Fs {
tag: &str, tag: &str,
req_num_queues: usize, req_num_queues: usize,
queue_size: u16, queue_size: u16,
cache: Option<(VirtioSharedMemoryList, u64)>, cache: Option<(VirtioSharedMemoryList, u64, MmapRegion)>,
) -> Result<Fs> { ) -> Result<Fs> {
let mut slave_req_support = false; let mut slave_req_support = false;
@ -471,7 +474,7 @@ impl VirtioDevice for Fs {
// Initialize slave communication. // Initialize slave communication.
let slave_req_handler = if self.slave_req_support { 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 { let vu_master_req_handler = Arc::new(Mutex::new(SlaveReqHandler {
cache_offset: cache.0.addr, cache_offset: cache.0.addr,
cache_size: cache.0.len, cache_size: cache.0.len,
@ -542,8 +545,8 @@ impl VirtioDevice for Fs {
} }
fn get_shm_regions(&self) -> Option<VirtioSharedMemoryList> { fn get_shm_regions(&self) -> Option<VirtioSharedMemoryList> {
if let Some(cache) = self.cache.clone() { if let Some(cache) = self.cache.as_ref() {
Some(cache.0) Some(cache.0.clone())
} else { } else {
None None
} }

View File

@ -1331,7 +1331,7 @@ impl DeviceManager {
if let Some(fs_list_cfg) = &self.config.lock().unwrap().fs { if let Some(fs_list_cfg) = &self.config.lock().unwrap().fs {
for fs_cfg in fs_list_cfg.iter() { for fs_cfg in fs_list_cfg.iter() {
if let Some(fs_sock) = fs_cfg.sock.to_str() { 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; let fs_cache = fs_cfg.cache_size;
// The memory needs to be 2MiB aligned in order to support // The memory needs to be 2MiB aligned in order to support
// hugepages. // hugepages.
@ -1356,8 +1356,6 @@ impl DeviceManager {
.map_err(DeviceManagerError::NewMmapRegion)?; .map_err(DeviceManagerError::NewMmapRegion)?;
let addr: u64 = mmap_region.as_ptr() as u64; let addr: u64 = mmap_region.as_ptr() as u64;
self._mmap_regions.push(mmap_region);
self.memory_manager self.memory_manager
.lock() .lock()
.unwrap() .unwrap()
@ -1383,6 +1381,7 @@ impl DeviceManager {
region_list, region_list,
}, },
addr, addr,
mmap_region,
)) ))
} else { } else {
None None