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 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<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,
queue_evts: Option<Vec<EventFd>>,
interrupt_cb: Option<Arc<dyn VirtioInterrupt>>,
@ -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<Fs> {
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<VirtioSharedMemoryList> {
if let Some(cache) = self.cache.clone() {
Some(cache.0)
if let Some(cache) = self.cache.as_ref() {
Some(cache.0.clone())
} else {
None
}

View File

@ -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