From c25bd447a1080aa4bdea4deb2df5b0192fc38a8b Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 14 Oct 2021 16:31:03 +0100 Subject: [PATCH] vmm: Ensure that allocate_bars() is called before mmio_regions() The allocate_bars method has a side effect which collates the BARs used for the device and stores them internally. Ensure that any use of this internal state is after the state is created otherwise no MMIO regions will be seen and so none will be mapped. Fixes: #3237 Signed-off-by: Rob Bradford --- vmm/src/device_manager.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 41bc926a5..ec8c66804 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2886,7 +2886,7 @@ impl DeviceManager { None }; - let mut vfio_pci_device = VfioPciDevice::new( + let vfio_pci_device = VfioPciDevice::new( &self.address_manager.vm, vfio_device, vfio_container, @@ -2908,21 +2908,6 @@ impl DeviceManager { id }; - vfio_pci_device - .map_mmio_regions(&self.address_manager.vm, || { - self.memory_manager.lock().unwrap().allocate_memory_slot() - }) - .map_err(DeviceManagerError::VfioMapRegion)?; - - let mut node = device_node!(vfio_name); - - for region in vfio_pci_device.mmio_regions() { - node.resources.push(Resource::MmioAddressRange { - base: region.start.0, - size: region.length as u64, - }); - } - let vfio_pci_device = Arc::new(Mutex::new(vfio_pci_device)); self.add_pci_device( @@ -2931,6 +2916,23 @@ impl DeviceManager { pci_device_bdf, )?; + vfio_pci_device + .lock() + .unwrap() + .map_mmio_regions(&self.address_manager.vm, || { + self.memory_manager.lock().unwrap().allocate_memory_slot() + }) + .map_err(DeviceManagerError::VfioMapRegion)?; + + let mut node = device_node!(vfio_name); + + for region in vfio_pci_device.lock().unwrap().mmio_regions() { + node.resources.push(Resource::MmioAddressRange { + base: region.start.0, + size: region.length as u64, + }); + } + node.pci_bdf = Some(pci_device_bdf); node.pci_device_handle = Some(PciDeviceHandle::Vfio(vfio_pci_device));