From 1e11e6789ac8788b5700450584b4f367843b87bf Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 22 Mar 2021 10:10:12 +0100 Subject: [PATCH] vmm: device_manager: Factorize passthrough_device creation There's no need to have the code creating the passthrough_device being duplicated since we can factorize it in a function used in both cases (both cold plugged and hot plugged devices VFIO devices). Signed-off-by: Sebastien Boeuf --- vmm/src/device_manager.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 9b45cb6b3..ce8c2130d 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2758,6 +2758,17 @@ impl DeviceManager { pci: &mut PciBus, device_cfg: &mut DeviceConfig, ) -> DeviceManagerResult<(u32, String)> { + // If the passthrough device has not been created yet, it is created + // here and stored in the DeviceManager structure for future needs. + if self.passthrough_device.is_none() { + self.passthrough_device = Some( + self.address_manager + .vm + .create_passthrough_device() + .map_err(|e| DeviceManagerError::CreatePassthroughDevice(e.into()))?, + ); + } + #[cfg(feature = "kvm")] return self.add_vfio_device(pci, device_cfg); @@ -2959,16 +2970,6 @@ impl DeviceManager { let mut devices = self.config.lock().unwrap().devices.clone(); if let Some(device_list_cfg) = &mut devices { - if self.passthrough_device.is_none() { - // Create the passthrough device. - self.passthrough_device = Some( - self.address_manager - .vm - .create_passthrough_device() - .map_err(|e| DeviceManagerError::CreatePassthroughDevice(e.into()))?, - ); - } - for device_cfg in device_list_cfg.iter_mut() { let (device_id, _) = self.add_passthrough_device(pci, device_cfg)?; if device_cfg.iommu && self.iommu_device.is_some() { @@ -3229,17 +3230,6 @@ impl DeviceManager { return Err(DeviceManagerError::NoPciBus); }; - if self.passthrough_device.is_none() { - // If the passthrough device has not been created yet, it is created - // here and stored in the DeviceManager structure for future needs. - self.passthrough_device = Some( - self.address_manager - .vm - .create_passthrough_device() - .map_err(|e| DeviceManagerError::CreatePassthroughDevice(e.into()))?, - ); - } - let (device_id, device_name) = self.add_passthrough_device(&mut pci.lock().unwrap(), device_cfg)?;