From 3e819ac797729998cf6ec83a61eabfb642aaeac8 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 28 Oct 2019 14:29:18 -0700 Subject: [PATCH] pci: Use a weak reference to the AddressManager Storing a strong reference to the AddressManager behind the DeviceRelocation trait results in a cyclic reference count. Use a weak reference to break that dependency. Signed-off-by: Rob Bradford Signed-off-by: Sebastien Boeuf --- pci/src/bus.rs | 11 +++++------ vmm/src/device_manager.rs | 7 ++++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pci/src/bus.rs b/pci/src/bus.rs index 1519474d5..4e7db0d95 100644 --- a/pci/src/bus.rs +++ b/pci/src/bus.rs @@ -10,8 +10,7 @@ use byteorder::{ByteOrder, LittleEndian}; use devices::BusDevice; use std; use std::ops::DerefMut; -use std::sync::Arc; -use std::sync::Mutex; +use std::sync::{Arc, Mutex, Weak}; use vm_memory::{Address, GuestAddress, GuestUsize}; const VENDOR_ID_INTEL: u16 = 0x8086; @@ -76,11 +75,11 @@ pub struct PciBus { /// Devices attached to this bus. /// Device 0 is host bridge. devices: Vec>>, - device_reloc: Arc, + device_reloc: Weak, } impl PciBus { - pub fn new(pci_root: PciRoot, device_reloc: Arc) -> Self { + pub fn new(pci_root: PciRoot, device_reloc: Weak) -> Self { let mut devices: Vec>> = Vec::new(); devices.push(Arc::new(Mutex::new(pci_root))); @@ -198,7 +197,7 @@ impl PciConfigIo { // Reprogram the BAR if needed if let Some(params) = bar_reprog_params { - pci_bus.device_reloc.move_bar( + pci_bus.device_reloc.upgrade().unwrap().move_bar( params.old_base, params.new_base, params.len, @@ -314,7 +313,7 @@ impl PciConfigMmio { // Reprogram the BAR if needed if let Some(params) = bar_reprog_params { - pci_bus.device_reloc.move_bar( + pci_bus.device_reloc.upgrade().unwrap().move_bar( params.old_base, params.new_base, params.len, diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 2926c4d1b..b710578f2 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -34,6 +34,8 @@ use std::os::unix::fs::OpenOptionsExt; use std::os::unix::io::AsRawFd; use std::ptr::null_mut; use std::result; +#[cfg(feature = "pci_support")] +use std::sync::Weak; use std::sync::{Arc, Mutex, RwLock}; #[cfg(feature = "pci_support")] use vfio::{VfioDevice, VfioDmaMapping, VfioPciDevice, VfioPciError}; @@ -536,7 +538,10 @@ impl DeviceManager { #[cfg(feature = "pci_support")] { let pci_root = PciRoot::new(None); - let mut pci_bus = PciBus::new(pci_root, address_manager.clone()); + let mut pci_bus = PciBus::new( + pci_root, + Arc::downgrade(&address_manager) as Weak, + ); let (mut iommu_device, iommu_mapping) = if vm_info.vm_cfg.iommu { let (device, mapping) =