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

Move the release of the managed memory region from the DeviceManager to
the virtio-pmem 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 6565e478e6
commit 0c6706a510
2 changed files with 14 additions and 6 deletions

View File

@ -26,7 +26,7 @@ use std::sync::Arc;
use std::thread; use std::thread;
use vm_memory::{ use vm_memory::{
Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic,
GuestMemoryError, GuestMemoryMmap, GuestUsize, GuestMemoryError, 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;
@ -322,13 +322,22 @@ pub struct Pmem {
interrupt_cb: Option<Arc<dyn VirtioInterrupt>>, interrupt_cb: Option<Arc<dyn VirtioInterrupt>>,
epoll_threads: Option<Vec<thread::JoinHandle<result::Result<(), DeviceError>>>>, epoll_threads: Option<Vec<thread::JoinHandle<result::Result<(), DeviceError>>>>,
paused: Arc<AtomicBool>, paused: Arc<AtomicBool>,
// Hold ownership of the memory that is allocated for the device
// which will be automatically dropped when the device is dropped
_region: MmapRegion,
} }
impl Pmem { impl Pmem {
pub fn new(disk: File, addr: GuestAddress, size: GuestUsize, iommu: bool) -> io::Result<Pmem> { pub fn new(
disk: File,
addr: GuestAddress,
_region: MmapRegion,
iommu: bool,
) -> io::Result<Pmem> {
let config = VirtioPmemConfig { let config = VirtioPmemConfig {
start: addr.raw_value().to_le(), start: addr.raw_value().to_le(),
size: size.to_le(), size: (_region.size() as u64).to_le(),
}; };
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1; let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
@ -348,6 +357,7 @@ impl Pmem {
interrupt_cb: None, interrupt_cb: None,
epoll_threads: None, epoll_threads: None,
paused: Arc::new(AtomicBool::new(false)), paused: Arc::new(AtomicBool::new(false)),
_region,
}) })
} }
} }

View File

@ -1467,8 +1467,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()
@ -1482,7 +1480,7 @@ impl DeviceManager {
.map_err(DeviceManagerError::MemoryManager)?; .map_err(DeviceManagerError::MemoryManager)?;
let virtio_pmem_device = Arc::new(Mutex::new( let virtio_pmem_device = Arc::new(Mutex::new(
vm_virtio::Pmem::new(file, pmem_guest_addr, size as GuestUsize, pmem_cfg.iommu) vm_virtio::Pmem::new(file, pmem_guest_addr, mmap_region, pmem_cfg.iommu)
.map_err(DeviceManagerError::CreateVirtioPmem)?, .map_err(DeviceManagerError::CreateVirtioPmem)?,
)); ));