From 256f0c39e9151766e97dad6175139a30a8599126 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Wed, 17 Sep 2025 22:54:07 +0000 Subject: [PATCH] vmm: Drop 'vfio container' when no active vfio devices This provides clearer life-cycle management of resources around vfio, and aligns better with the kernel behavior as reported below with vfio legacy mode (with vfio container/group). Fixes: #7328 Signed-off-by: Bo Chen Signed-off-by: Yi Wang --- vmm/src/device_manager.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index a580f5282..992133f7a 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -4965,6 +4965,14 @@ impl DeviceManager { pub(crate) fn acpi_platform_addresses(&self) -> &AcpiPlatformAddresses { &self.acpi_platform_addresses } + + fn cleanup_vfio_container(&mut self) { + // Drop the 'vfio container' instance when "Self" is the only reference + if let Some(1) = self.vfio_container.as_ref().map(Arc::strong_count) { + debug!("Drop 'vfio container' given no active 'vfio devices'."); + self.vfio_container = None; + } + } } #[cfg(feature = "ivshmem")] @@ -5442,6 +5450,7 @@ impl BusDevice for DeviceManager { if let Err(e) = self.eject_device(self.selected_segment as u16, slot_id as u8) { error!("Failed ejecting device {slot_id}: {e:?}"); } + self.cleanup_vfio_container(); slot_bitmap &= !(1 << slot_id); } }