From dde5f6ef38ae1b0a2c8685cc27732ae732e11de3 Mon Sep 17 00:00:00 2001 From: Damian Barabonkov Date: Tue, 10 Feb 2026 12:23:36 -0800 Subject: [PATCH] vmm: Fix MMIO region removal during VFIO device hot-unplug When a VFIO device with multiple MMIO regions is hot-unplugged, each region must be individually matched and removed from the DeviceManager's mmio_regions list. Compare per-region rather than building an aggregate across all regions, which would never match any individual entry. Also remove the now-unused HashSet import. Signed-off-by: Damian Barabonkov --- vmm/src/device_manager.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 0cd91c5c9..e7f8d4020 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -4607,13 +4607,18 @@ impl DeviceManager { } let (pci_device, bus_device, virtio_device, remove_dma_handler) = match pci_device_handle { - // No need to remove any virtio-mem mapping here as the container outlives all devices + // VirtioMemMappingSource::Container cleanup is handled by + // cleanup_vfio_container when the last VFIO device is removed. PciDeviceHandle::Vfio(vfio_pci_device) => { - for mmio_region in vfio_pci_device.lock().unwrap().mmio_regions() { - self.mmio_regions - .lock() - .unwrap() - .retain(|x| x.start != mmio_region.start); + // Remove this device's MMIO regions from the DeviceManager's + // mmio_regions list. We match on UserMemoryRegion slot numbers + // rather than MmioRegion start addresses because move_bar() + // updates the device's region addresses but not the + // DeviceManager's cloned copies. + let device_regions = vfio_pci_device.lock().unwrap().mmio_regions().clone(); + let mut mmio_regions = self.mmio_regions.lock().unwrap(); + for device_region in &device_regions { + mmio_regions.retain(|x| !x.has_matching_slots(device_region)); } (