From c22fd39170bfa399ceb977281557ce5816df780f Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 20 Apr 2020 17:40:11 +0200 Subject: [PATCH] vmm: Remove virtio device's userspace mapping on hot-unplug When a virtio device is dynamically removed from the VM through the hot-unplug mechanism, every mapping associated with it must be properly removed. Based on the previous patches letting a VirtioDevice expose the list of userspace mappings associated with it, this patch can now remove all the KVM userspace memory regions through the MemoryManager. Signed-off-by: Sebastien Boeuf --- vmm/src/device_manager.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 0c5bd696b..e0ea04cd6 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2209,7 +2209,22 @@ impl DeviceManager { // Shutdown and remove the underlying virtio-device if present if let Some(virtio_device) = virtio_device { + for mapping in virtio_device.lock().unwrap().userspace_mappings() { + self.memory_manager + .lock() + .unwrap() + .remove_userspace_mapping( + mapping.addr.raw_value(), + mapping.len, + mapping.host_addr, + mapping.mergeable, + mapping.mem_slot, + ) + .map_err(DeviceManagerError::MemoryManager)?; + } + virtio_device.lock().unwrap().shutdown(); + self.virtio_devices .retain(|(d, _, _)| !Arc::ptr_eq(d, &virtio_device)); }