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 <bchen@crusoe.ai>
Signed-off-by: Yi Wang <foxywang@tencent.com>
This commit is contained in:
Bo Chen
2025-09-17 22:54:07 +00:00
parent 205e62aaa8
commit 256f0c39e9

View File

@@ -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);
}
}