From 7536a95424b328a263347cd3a67b421de6e6cef4 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Tue, 28 Oct 2025 10:49:34 +0100 Subject: [PATCH] misc: cleanup `&Arc` -> `&dyn T` Consuming `&Arc` as argument is almost always an antipattern as it hides whether the callee is going to take over (shared) ownership (by .clone()) or not. Instead, it is better to consume `&dyn T` or `Arc` to be more explicit. This commit cleans up the code. The change is very mechanic and was very easy to implement across the code base. Signed-off-by: Philipp Schuster On-behalf-of: SAP philipp.schuster@sap.com --- arch/src/aarch64/mod.rs | 2 +- arch/src/riscv64/mod.rs | 2 +- arch/src/x86_64/mod.rs | 5 ++-- block/src/lib.rs | 3 +- hypervisor/src/cpu.rs | 5 +--- hypervisor/src/kvm/mod.rs | 2 +- hypervisor/src/mshv/mod.rs | 2 +- net_util/src/ctrl_queue.rs | 4 +-- net_util/src/queue_pair.rs | 8 +++--- pci/src/vfio.rs | 4 +-- pci/src/vfio_user.rs | 4 +-- virtio-devices/src/balloon.rs | 2 +- virtio-devices/src/block.rs | 4 +-- virtio-devices/src/console.rs | 6 ++-- virtio-devices/src/device.rs | 4 +-- virtio-devices/src/iommu.rs | 2 +- virtio-devices/src/mem.rs | 2 +- virtio-devices/src/net.rs | 8 ++++-- virtio-devices/src/pmem.rs | 6 ++-- virtio-devices/src/rng.rs | 4 +-- virtio-devices/src/transport/pci_device.rs | 6 ++-- virtio-devices/src/vdpa.rs | 10 +++---- virtio-devices/src/vhost_user/blk.rs | 2 +- virtio-devices/src/vhost_user/fs.rs | 2 +- virtio-devices/src/vhost_user/mod.rs | 4 +-- virtio-devices/src/vhost_user/net.rs | 2 +- .../src/vhost_user/vu_common_ctrl.rs | 4 +-- virtio-devices/src/vsock/device.rs | 6 ++-- virtio-devices/src/vsock/packet.rs | 5 ++-- virtio-devices/src/watchdog.rs | 2 +- vm-device/src/bus.rs | 5 ++-- vm-virtio/src/lib.rs | 13 ++++----- vmm/src/cpu.rs | 18 ++++++------ vmm/src/device_manager.rs | 28 +++++++++---------- vmm/src/interrupt.rs | 12 ++++---- vmm/src/lib.rs | 21 ++++++++------ vmm/src/vm.rs | 19 +++++++------ 37 files changed, 121 insertions(+), 117 deletions(-) diff --git a/arch/src/aarch64/mod.rs b/arch/src/aarch64/mod.rs index cfb7e1ff5..95c4cb3fd 100644 --- a/arch/src/aarch64/mod.rs +++ b/arch/src/aarch64/mod.rs @@ -180,7 +180,7 @@ pub fn initramfs_load_addr( } } -pub fn get_host_cpu_phys_bits(hypervisor: &Arc) -> u8 { +pub fn get_host_cpu_phys_bits(hypervisor: &dyn hypervisor::Hypervisor) -> u8 { let host_cpu_phys_bits = hypervisor.get_host_ipa_limit().try_into().unwrap(); if host_cpu_phys_bits == 0 { // Host kernel does not support `get_host_ipa_limit`, diff --git a/arch/src/riscv64/mod.rs b/arch/src/riscv64/mod.rs index 53aa479e2..b4478aaa6 100644 --- a/arch/src/riscv64/mod.rs +++ b/arch/src/riscv64/mod.rs @@ -156,7 +156,7 @@ pub fn initramfs_load_addr( } } -pub fn get_host_cpu_phys_bits(_hypervisor: &Arc) -> u8 { +pub fn get_host_cpu_phys_bits(_hypervisor: &dyn hypervisor::Hypervisor) -> u8 { 40 } diff --git a/arch/src/x86_64/mod.rs b/arch/src/x86_64/mod.rs index d720e69b6..5d17065c8 100644 --- a/arch/src/x86_64/mod.rs +++ b/arch/src/x86_64/mod.rs @@ -6,7 +6,6 @@ // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE-BSD-3-Clause file. -use std::sync::Arc; pub mod interrupts; pub mod layout; mod mpspec; @@ -547,7 +546,7 @@ impl CpuidFeatureEntry { } pub fn generate_common_cpuid( - hypervisor: &Arc, + hypervisor: &dyn hypervisor::Hypervisor, config: &CpuidConfig, ) -> super::Result> { // SAFETY: cpuid called with valid leaves @@ -1258,7 +1257,7 @@ pub fn initramfs_load_addr( Ok(aligned_addr) } -pub fn get_host_cpu_phys_bits(hypervisor: &Arc) -> u8 { +pub fn get_host_cpu_phys_bits(hypervisor: &dyn hypervisor::Hypervisor) -> u8 { // SAFETY: call cpuid with valid leaves unsafe { let leaf = x86_64::__cpuid(0x8000_0000); diff --git a/block/src/lib.rs b/block/src/lib.rs index 4cfed2283..f7388e505 100644 --- a/block/src/lib.rs +++ b/block/src/lib.rs @@ -39,7 +39,6 @@ use std::io::{self, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; use std::os::linux::fs::MetadataExt; use std::os::unix::io::AsRawFd; use std::path::Path; -use std::sync::Arc; use std::time::Instant; use std::{cmp, result}; @@ -260,7 +259,7 @@ pub struct Request { impl Request { pub fn parse( desc_chain: &mut DescriptorChain>>, - access_platform: Option<&Arc>, + access_platform: Option<&dyn AccessPlatform>, ) -> result::Result { let hdr_desc = desc_chain .next() diff --git a/hypervisor/src/cpu.rs b/hypervisor/src/cpu.rs index 0d52d3281..763eaa455 100644 --- a/hypervisor/src/cpu.rs +++ b/hypervisor/src/cpu.rs @@ -10,9 +10,6 @@ // // -#[cfg(target_arch = "aarch64")] -use std::sync::Arc; - use thiserror::Error; #[cfg(not(target_arch = "riscv64"))] use vm_memory::GuestAddress; @@ -473,7 +470,7 @@ pub trait Vcpu: Send + Sync { #[cfg(target_arch = "aarch64")] fn vcpu_set_processor_features( &self, - vm: &Arc, + vm: &dyn crate::Vm, kvi: &mut VcpuInit, id: u32, ) -> Result<()>; diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index dfdf8fa19..5ce110d89 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -2098,7 +2098,7 @@ impl cpu::Vcpu for KvmVcpu { #[cfg(target_arch = "aarch64")] fn vcpu_set_processor_features( &self, - vm: &Arc, + vm: &dyn crate::Vm, kvi: &mut crate::VcpuInit, id: u32, ) -> cpu::Result<()> { diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 950dd55f1..46a6103b5 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -1306,7 +1306,7 @@ impl cpu::Vcpu for MshvVcpu { #[cfg(target_arch = "aarch64")] fn vcpu_set_processor_features( &self, - _vm: &Arc, + _vm: &dyn crate::Vm, _kvi: &mut crate::VcpuInit, _id: u32, ) -> cpu::Result<()> { diff --git a/net_util/src/ctrl_queue.rs b/net_util/src/ctrl_queue.rs index 3fd242f66..6b9cfb23b 100644 --- a/net_util/src/ctrl_queue.rs +++ b/net_util/src/ctrl_queue.rs @@ -2,8 +2,6 @@ // // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause -use std::sync::Arc; - use thiserror::Error; use virtio_bindings::virtio_net::{ VIRTIO_NET_CTRL_GUEST_OFFLOADS, VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, VIRTIO_NET_CTRL_MQ, @@ -67,7 +65,7 @@ impl CtrlQueue { &mut self, mem: &GuestMemoryMmap, queue: &mut Queue, - access_platform: Option<&Arc>, + access_platform: Option<&dyn AccessPlatform>, ) -> Result<()> { while let Some(mut desc_chain) = queue.pop_descriptor_chain(mem) { let ctrl_desc = desc_chain.next().ok_or(Error::NoControlHeaderDescriptor)?; diff --git a/net_util/src/queue_pair.rs b/net_util/src/queue_pair.rs index c3c145646..0ad328ff9 100644 --- a/net_util/src/queue_pair.rs +++ b/net_util/src/queue_pair.rs @@ -45,7 +45,7 @@ impl TxVirtio { tap: &Tap, queue: &mut Queue, rate_limiter: &mut Option, - access_platform: Option<&Arc>, + access_platform: Option<&dyn AccessPlatform>, ) -> Result { let mut retry_write = false; let mut rate_limit_reached = false; @@ -173,7 +173,7 @@ impl RxVirtio { tap: &Tap, queue: &mut Queue, rate_limiter: &mut Option, - access_platform: Option<&Arc>, + access_platform: Option<&dyn AccessPlatform>, ) -> Result { let mut exhausted_descs = true; let mut rate_limit_reached = false; @@ -413,7 +413,7 @@ impl NetQueuePair { &self.tap, queue, &mut self.tx_rate_limiter, - self.access_platform.as_ref(), + self.access_platform.as_deref(), )?; // We got told to try again when writing to the tap. Wait for the TAP to be writable @@ -463,7 +463,7 @@ impl NetQueuePair { &self.tap, queue, &mut self.rx_rate_limiter, - self.access_platform.as_ref(), + self.access_platform.as_deref(), )?; let rate_limit_reached = self .rx_rate_limiter diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index abe80073b..13a528eba 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -1449,7 +1449,7 @@ impl VfioPciDevice { #[allow(clippy::too_many_arguments)] pub fn new( id: String, - vm: &Arc, + vm: Arc, device: VfioDevice, container: Arc, msi_interrupt_manager: Arc>, @@ -1478,7 +1478,7 @@ impl VfioPciDevice { let vfio_pci_device = VfioPciDevice { id, - vm: vm.clone(), + vm, device, container, common, diff --git a/pci/src/vfio_user.rs b/pci/src/vfio_user.rs index 79dfb659a..be0c775ef 100644 --- a/pci/src/vfio_user.rs +++ b/pci/src/vfio_user.rs @@ -69,7 +69,7 @@ impl VfioUserPciDevice { #[allow(clippy::too_many_arguments)] pub fn new( id: String, - vm: &Arc, + vm: Arc, client: Arc>, msi_interrupt_manager: Arc>, legacy_interrupt_group: Option>, @@ -103,7 +103,7 @@ impl VfioUserPciDevice { Ok(Self { id, - vm: vm.clone(), + vm, client, common, memory_slot_allocator, diff --git a/virtio-devices/src/balloon.rs b/virtio-devices/src/balloon.rs index b721410e3..d28d1d117 100644 --- a/virtio-devices/src/balloon.rs +++ b/virtio-devices/src/balloon.rs @@ -591,7 +591,7 @@ impl VirtioDevice for Balloon { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let (kill_evt, pause_evt) = self.common.dup_eventfds(); let mut virtqueues = Vec::new(); diff --git a/virtio-devices/src/block.rs b/virtio-devices/src/block.rs index 89228c382..ff8adf3b4 100644 --- a/virtio-devices/src/block.rs +++ b/virtio-devices/src/block.rs @@ -171,7 +171,7 @@ impl BlockEpollHandler { let mut batch_inflight_requests = Vec::new(); while let Some(mut desc_chain) = queue.pop_descriptor_chain(self.mem.memory()) { - let mut request = Request::parse(&mut desc_chain, self.access_platform.as_ref()) + let mut request = Request::parse(&mut desc_chain, self.access_platform.as_deref()) .map_err(Error::RequestParsing)?; // For virtio spec compliance @@ -905,7 +905,7 @@ impl VirtioDevice for Block { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; self.update_writeback(); diff --git a/virtio-devices/src/console.rs b/virtio-devices/src/console.rs index 37b10b91b..5756c9273 100644 --- a/virtio-devices/src/console.rs +++ b/virtio-devices/src/console.rs @@ -217,7 +217,7 @@ impl ConsoleEpollHandler { .write_slice( &source_slice[..], desc.addr() - .translate_gva(self.access_platform.as_ref(), desc.len() as usize), + .translate_gva(self.access_platform.as_deref(), desc.len() as usize), ) .map_err(Error::GuestMemoryWrite)?; @@ -253,7 +253,7 @@ impl ConsoleEpollHandler { .memory() .write_volatile_to( desc.addr() - .translate_gva(self.access_platform.as_ref(), desc.len() as usize), + .translate_gva(self.access_platform.as_deref(), desc.len() as usize), &mut buf, desc.len() as usize, ) @@ -707,7 +707,7 @@ impl VirtioDevice for Console { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; self.resizer .acked_features .store(self.common.acked_features, Ordering::Relaxed); diff --git a/virtio-devices/src/device.rs b/virtio-devices/src/device.rs index 1a6a79b99..a30ad6064 100644 --- a/virtio-devices/src/device.rs +++ b/virtio-devices/src/device.rs @@ -221,7 +221,7 @@ impl VirtioCommon { pub fn activate( &mut self, queues: &[(usize, Queue, EventFd)], - interrupt_cb: &Arc, + interrupt_cb: Arc, ) -> ActivateResult { if queues.len() < self.min_queues.into() { error!( @@ -246,7 +246,7 @@ impl VirtioCommon { // Save the interrupt EventFD as we need to return it on reset // but clone it to pass into the thread. - self.interrupt_cb = Some(interrupt_cb.clone()); + self.interrupt_cb = Some(interrupt_cb); Ok(()) } diff --git a/virtio-devices/src/iommu.rs b/virtio-devices/src/iommu.rs index ca6b1a1be..f2acbcec9 100644 --- a/virtio-devices/src/iommu.rs +++ b/virtio-devices/src/iommu.rs @@ -1079,7 +1079,7 @@ impl VirtioDevice for Iommu { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let (kill_evt, pause_evt) = self.common.dup_eventfds(); let (_, request_queue, request_queue_evt) = queues.remove(0); diff --git a/virtio-devices/src/mem.rs b/virtio-devices/src/mem.rs index 6205c822a..6620a5edf 100644 --- a/virtio-devices/src/mem.rs +++ b/virtio-devices/src/mem.rs @@ -912,7 +912,7 @@ impl VirtioDevice for Mem { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let (kill_evt, pause_evt) = self.common.dup_eventfds(); let (_, queue, queue_evt) = queues.remove(0); diff --git a/virtio-devices/src/net.rs b/virtio-devices/src/net.rs index 598bce71d..fd6a4a3b0 100644 --- a/virtio-devices/src/net.rs +++ b/virtio-devices/src/net.rs @@ -100,7 +100,11 @@ impl EpollHelperHandler for NetCtrlEpollHandler { )) })?; self.ctrl_q - .process(mem.deref(), &mut self.queue, self.access_platform.as_ref()) + .process( + mem.deref(), + &mut self.queue, + self.access_platform.as_deref(), + ) .map_err(|e| { EpollHelperError::HandleEvent(anyhow!( "Failed to process control queue: {e:?}" @@ -693,7 +697,7 @@ impl VirtioDevice for Net { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let num_queues = queues.len(); let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into()); diff --git a/virtio-devices/src/pmem.rs b/virtio-devices/src/pmem.rs index ff17fa690..2916b6efc 100644 --- a/virtio-devices/src/pmem.rs +++ b/virtio-devices/src/pmem.rs @@ -105,7 +105,7 @@ struct Request { impl Request { fn parse( desc_chain: &mut DescriptorChain>, - access_platform: Option<&Arc>, + access_platform: Option<&dyn AccessPlatform>, ) -> result::Result { let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?; // The descriptor contains the request type which MUST be readable. @@ -165,7 +165,7 @@ impl PmemEpollHandler { fn process_queue(&mut self) -> result::Result { let mut used_descs = false; while let Some(mut desc_chain) = self.queue.pop_descriptor_chain(self.mem.memory()) { - let len = match Request::parse(&mut desc_chain, self.access_platform.as_ref()) { + let len = match Request::parse(&mut desc_chain, self.access_platform.as_deref()) { Ok(ref req) if (req.type_ == RequestType::Flush) => { let status_code = match self.disk.sync_all() { Ok(()) => VIRTIO_PMEM_RESP_TYPE_OK, @@ -387,7 +387,7 @@ impl VirtioDevice for Pmem { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let (kill_evt, pause_evt) = self.common.dup_eventfds(); if let Some(disk) = self.disk.as_ref() { let disk = disk.try_clone().map_err(|e| { diff --git a/virtio-devices/src/rng.rs b/virtio-devices/src/rng.rs index 3ca58778b..f87c523a3 100644 --- a/virtio-devices/src/rng.rs +++ b/virtio-devices/src/rng.rs @@ -76,7 +76,7 @@ impl RngEpollHandler { .memory() .read_volatile_from( desc.addr() - .translate_gva(self.access_platform.as_ref(), desc.len() as usize), + .translate_gva(self.access_platform.as_deref(), desc.len() as usize), &mut self.random_file, desc.len() as usize, ) @@ -248,7 +248,7 @@ impl VirtioDevice for Rng { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let (kill_evt, pause_evt) = self.common.dup_eventfds(); if let Some(file) = self.random_file.as_ref() { diff --git a/virtio-devices/src/transport/pci_device.rs b/virtio-devices/src/transport/pci_device.rs index 68adae664..4f34ffea6 100644 --- a/virtio-devices/src/transport/pci_device.rs +++ b/virtio-devices/src/transport/pci_device.rs @@ -382,7 +382,7 @@ impl VirtioPciDevice { device: Arc>, msix_num: u16, access_platform: Option>, - interrupt_manager: &Arc>, + interrupt_manager: &dyn InterruptManager, pci_device_bdf: u32, activate_evt: EventFd, use_64bit_bar: bool, @@ -815,8 +815,8 @@ impl VirtioPciDevice { !self.device_activated.load(Ordering::SeqCst) && self.is_driver_ready() } - pub fn dma_handler(&self) -> Option<&Arc> { - self.dma_handler.as_ref() + pub fn dma_handler(&self) -> Option<&dyn ExternalDmaMapping> { + self.dma_handler.as_deref() } } diff --git a/virtio-devices/src/vdpa.rs b/virtio-devices/src/vdpa.rs index 46ef48781..d4739e499 100644 --- a/virtio-devices/src/vdpa.rs +++ b/virtio-devices/src/vdpa.rs @@ -216,7 +216,7 @@ impl Vdpa { fn activate_vdpa( &mut self, mem: &GuestMemoryMmap, - virtio_interrupt: &Arc, + virtio_interrupt: &dyn VirtioInterrupt, queues: Vec<(usize, Queue, EventFd)>, ) -> Result<()> { assert!(self.vhost.is_some()); @@ -245,15 +245,15 @@ impl Vdpa { queue_size, flags: 0u32, desc_table_addr: queue.desc_table().translate_gpa( - self.common.access_platform.as_ref(), + self.common.access_platform.as_deref(), queue_size as usize * std::mem::size_of::(), ), used_ring_addr: queue.used_ring().translate_gpa( - self.common.access_platform.as_ref(), + self.common.access_platform.as_deref(), 4 + queue_size as usize * 8, ), avail_ring_addr: queue.avail_ring().translate_gpa( - self.common.access_platform.as_ref(), + self.common.access_platform.as_deref(), 4 + queue_size as usize * 2, ), log_addr: None, @@ -421,7 +421,7 @@ impl VirtioDevice for Vdpa { virtio_interrupt: Arc, queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.activate_vdpa(&mem.memory(), &virtio_interrupt, queues) + self.activate_vdpa(&mem.memory(), virtio_interrupt.as_ref(), queues) .map_err(ActivateError::ActivateVdpa)?; // Store the virtio interrupt handler as we need to return it on reset diff --git a/virtio-devices/src/vhost_user/blk.rs b/virtio-devices/src/vhost_user/blk.rs index 20309080c..5dab8442a 100644 --- a/virtio-devices/src/vhost_user/blk.rs +++ b/virtio-devices/src/vhost_user/blk.rs @@ -284,7 +284,7 @@ impl VirtioDevice for Blk { interrupt_cb: Arc, queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; self.guest_memory = Some(mem.clone()); let backend_req_handler: Option> = None; diff --git a/virtio-devices/src/vhost_user/fs.rs b/virtio-devices/src/vhost_user/fs.rs index bf724316c..ab07aea36 100644 --- a/virtio-devices/src/vhost_user/fs.rs +++ b/virtio-devices/src/vhost_user/fs.rs @@ -265,7 +265,7 @@ impl VirtioDevice for Fs { interrupt_cb: Arc, queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; self.guest_memory = Some(mem.clone()); let backend_req_handler: Option> = None; diff --git a/virtio-devices/src/vhost_user/mod.rs b/virtio-devices/src/vhost_user/mod.rs index f9dcb363f..1db305421 100644 --- a/virtio-devices/src/vhost_user/mod.rs +++ b/virtio-devices/src/vhost_user/mod.rs @@ -228,7 +228,7 @@ impl VhostUserEpollHandler { .iter() .map(|(i, q, e)| (*i, vm_virtio::clone_queue(q), e.try_clone().unwrap())) .collect(), - &self.virtio_interrupt, + self.virtio_interrupt.as_ref(), self.acked_features, self.acked_protocol_features, &self.backend_req_handler, @@ -332,7 +332,7 @@ impl VhostUserCommon { .iter() .map(|(i, q, e)| (*i, vm_virtio::clone_queue(q), e.try_clone().unwrap())) .collect(), - &interrupt_cb, + interrupt_cb.as_ref(), acked_features, &backend_req_handler, inflight.as_mut(), diff --git a/virtio-devices/src/vhost_user/net.rs b/virtio-devices/src/vhost_user/net.rs index 4f9d366c1..2a5d05d7e 100644 --- a/virtio-devices/src/vhost_user/net.rs +++ b/virtio-devices/src/vhost_user/net.rs @@ -295,7 +295,7 @@ impl VirtioDevice for Net { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; self.guest_memory = Some(mem.clone()); let num_queues = queues.len(); diff --git a/virtio-devices/src/vhost_user/vu_common_ctrl.rs b/virtio-devices/src/vhost_user/vu_common_ctrl.rs index eba029217..f1255c7f7 100644 --- a/virtio-devices/src/vhost_user/vu_common_ctrl.rs +++ b/virtio-devices/src/vhost_user/vu_common_ctrl.rs @@ -156,7 +156,7 @@ impl VhostUserHandle { &mut self, mem: &GuestMemoryMmap, queues: Vec<(usize, Queue, EventFd)>, - virtio_interrupt: &Arc, + virtio_interrupt: &dyn VirtioInterrupt, acked_features: u64, backend_req_handler: &Option>, inflight: Option<&mut Inflight>, @@ -340,7 +340,7 @@ impl VhostUserHandle { &mut self, mem: &GuestMemoryMmap, queues: Vec<(usize, Queue, EventFd)>, - virtio_interrupt: &Arc, + virtio_interrupt: &dyn VirtioInterrupt, acked_features: u64, acked_protocol_features: u64, backend_req_handler: &Option>, diff --git a/virtio-devices/src/vsock/device.rs b/virtio-devices/src/vsock/device.rs index 385b5a242..657e80879 100644 --- a/virtio-devices/src/vsock/device.rs +++ b/virtio-devices/src/vsock/device.rs @@ -124,7 +124,7 @@ where while let Some(mut desc_chain) = self.queues[0].pop_descriptor_chain(self.mem.memory()) { let used_len = match VsockPacket::from_rx_virtq_head( &mut desc_chain, - self.access_platform.as_ref(), + self.access_platform.as_deref(), ) { Ok(mut pkt) => { if self.backend.write().unwrap().recv_pkt(&mut pkt).is_ok() { @@ -166,7 +166,7 @@ where while let Some(mut desc_chain) = self.queues[1].pop_descriptor_chain(self.mem.memory()) { let pkt = match VsockPacket::from_tx_virtq_head( &mut desc_chain, - self.access_platform.as_ref(), + self.access_platform.as_deref(), ) { Ok(pkt) => pkt, Err(e) => { @@ -430,7 +430,7 @@ where interrupt_cb: Arc, queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let (kill_evt, pause_evt) = self.common.dup_eventfds(); let mut virtqueues = Vec::new(); diff --git a/virtio-devices/src/vsock/packet.rs b/virtio-devices/src/vsock/packet.rs index a6749381d..4606c93b1 100644 --- a/virtio-devices/src/vsock/packet.rs +++ b/virtio-devices/src/vsock/packet.rs @@ -17,7 +17,6 @@ //! to temporary buffers, before passing it on to the vsock backend. use std::ops::Deref; -use std::sync::Arc; use byteorder::{ByteOrder, LittleEndian}; use virtio_queue::DescriptorChain; @@ -111,7 +110,7 @@ impl VsockPacket { /// pub fn from_tx_virtq_head( desc_chain: &mut DescriptorChain, - access_platform: Option<&Arc>, + access_platform: Option<&dyn AccessPlatform>, ) -> Result where M: Clone + Deref, @@ -203,7 +202,7 @@ impl VsockPacket { /// pub fn from_rx_virtq_head( desc_chain: &mut DescriptorChain, - access_platform: Option<&Arc>, + access_platform: Option<&dyn AccessPlatform>, ) -> Result where M: Clone + Deref, diff --git a/virtio-devices/src/watchdog.rs b/virtio-devices/src/watchdog.rs index cb4b5845c..25978f252 100644 --- a/virtio-devices/src/watchdog.rs +++ b/virtio-devices/src/watchdog.rs @@ -330,7 +330,7 @@ impl VirtioDevice for Watchdog { interrupt_cb: Arc, mut queues: Vec<(usize, Queue, EventFd)>, ) -> ActivateResult { - self.common.activate(&queues, &interrupt_cb)?; + self.common.activate(&queues, interrupt_cb.clone())?; let (kill_evt, pause_evt) = self.common.dup_eventfds(); let reset_evt = self.reset_evt.try_clone().map_err(|e| { diff --git a/vm-device/src/bus.rs b/vm-device/src/bus.rs index 70bbc049a..fdfb82c9f 100644 --- a/vm-device/src/bus.rs +++ b/vm-device/src/bus.rs @@ -192,12 +192,13 @@ impl Bus { } /// Removes all entries referencing the given device. - pub fn remove_by_device(&self, device: &Arc) -> Result<()> { + pub fn remove_by_device(&self, device: &dyn BusDeviceSync) -> Result<()> { let mut device_list = self.devices.write().unwrap(); let mut remove_key_list = Vec::new(); for (key, value) in device_list.iter() { - if Arc::ptr_eq(&value.upgrade().unwrap(), device) { + let value = value.upgrade().unwrap(); + if core::ptr::eq(Arc::as_ptr(&value), device) { remove_key_list.push(*key); } } diff --git a/vm-virtio/src/lib.rs b/vm-virtio/src/lib.rs index 87dcd0a8d..b7f537070 100644 --- a/vm-virtio/src/lib.rs +++ b/vm-virtio/src/lib.rs @@ -11,7 +11,6 @@ //! Implements virtio queues use std::fmt::{self, Debug}; -use std::sync::Arc; use virtio_queue::{Queue, QueueT}; use vm_memory::GuestAddress; @@ -102,28 +101,28 @@ pub trait AccessPlatform: Send + Sync + Debug { } pub trait Translatable { - fn translate_gva(&self, access_platform: Option<&Arc>, len: usize) -> Self; - fn translate_gpa(&self, access_platform: Option<&Arc>, len: usize) -> Self; + fn translate_gva(&self, access_platform: Option<&dyn AccessPlatform>, len: usize) -> Self; + fn translate_gpa(&self, access_platform: Option<&dyn AccessPlatform>, len: usize) -> Self; } impl Translatable for GuestAddress { - fn translate_gva(&self, access_platform: Option<&Arc>, len: usize) -> Self { + fn translate_gva(&self, access_platform: Option<&dyn AccessPlatform>, len: usize) -> Self { GuestAddress(self.0.translate_gva(access_platform, len)) } - fn translate_gpa(&self, access_platform: Option<&Arc>, len: usize) -> Self { + fn translate_gpa(&self, access_platform: Option<&dyn AccessPlatform>, len: usize) -> Self { GuestAddress(self.0.translate_gpa(access_platform, len)) } } impl Translatable for u64 { - fn translate_gva(&self, access_platform: Option<&Arc>, len: usize) -> Self { + fn translate_gva(&self, access_platform: Option<&dyn AccessPlatform>, len: usize) -> Self { if let Some(access_platform) = access_platform { access_platform.translate_gva(*self, len as u64).unwrap() } else { *self } } - fn translate_gpa(&self, access_platform: Option<&Arc>, len: usize) -> Self { + fn translate_gpa(&self, access_platform: Option<&dyn AccessPlatform>, len: usize) -> Self { if let Some(access_platform) = access_platform { access_platform.translate_gpa(*self, len as u64).unwrap() } else { diff --git a/vmm/src/cpu.rs b/vmm/src/cpu.rs index 4f8f0dcc9..01e12e807 100644 --- a/vmm/src/cpu.rs +++ b/vmm/src/cpu.rs @@ -413,7 +413,7 @@ impl Vcpu { pub fn new( id: u32, apic_id: u32, - vm: &Arc, + vm: &dyn hypervisor::Vm, vm_ops: Option>, #[cfg(target_arch = "x86_64")] cpu_vendor: CpuVendor, ) -> Result { @@ -441,7 +441,7 @@ impl Vcpu { /// * `cpuid` - (x86_64) CpuId, wrapper over the `kvm_cpuid2` structure. pub fn configure( &mut self, - #[cfg(target_arch = "aarch64")] vm: &Arc, + #[cfg(target_arch = "aarch64")] vm: &dyn hypervisor::Vm, boot_setup: Option<(EntryPoint, &GuestMemoryAtomic)>, #[cfg(target_arch = "x86_64")] cpuid: Vec, #[cfg(target_arch = "x86_64")] kvm_hyperv: bool, @@ -486,7 +486,7 @@ impl Vcpu { /// Initializes an aarch64 specific vcpu for booting Linux. #[cfg(target_arch = "aarch64")] - pub fn init(&self, vm: &Arc) -> Result<()> { + pub fn init(&self, vm: &dyn hypervisor::Vm) -> Result<()> { use std::arch::is_aarch64_feature_detected; #[allow(clippy::nonminimal_bool)] let sve_supported = @@ -748,7 +748,7 @@ impl CpuManager { exit_evt: EventFd, reset_evt: EventFd, #[cfg(feature = "guest_debug")] vm_debug_evt: EventFd, - hypervisor: &Arc, + hypervisor: Arc, seccomp_action: SeccompAction, vm_ops: Arc, #[cfg(feature = "tdx")] tdx_enabled: bool, @@ -854,7 +854,7 @@ impl CpuManager { proximity_domain_per_cpu, affinity, dynamic, - hypervisor: hypervisor.clone(), + hypervisor, #[cfg(feature = "sev_snp")] sev_snp_enabled, }))) @@ -863,7 +863,7 @@ impl CpuManager { #[cfg(target_arch = "x86_64")] pub fn populate_cpuid( &mut self, - hypervisor: &Arc, + hypervisor: &dyn hypervisor::Hypervisor, #[cfg(feature = "tdx")] tdx: bool, ) -> Result<()> { self.cpuid = { @@ -897,7 +897,7 @@ impl CpuManager { let mut vcpu = Vcpu::new( cpu_id, x2apic_id, - &self.vm, + self.vm.as_ref(), Some(self.vm_ops.clone()), #[cfg(target_arch = "x86_64")] self.hypervisor.get_cpu_vendor(), @@ -906,7 +906,7 @@ impl CpuManager { if let Some(snapshot) = snapshot { // AArch64 vCPUs should be initialized after created. #[cfg(target_arch = "aarch64")] - vcpu.init(&self.vm)?; + vcpu.init(self.vm.as_ref())?; let state: CpuState = snapshot.to_state().map_err(|e| { Error::VcpuCreate(anyhow!("Could not get vCPU state from snapshot {e:?}")) @@ -977,7 +977,7 @@ impl CpuManager { )?; #[cfg(target_arch = "aarch64")] - vcpu.configure(&self.vm, boot_setup)?; + vcpu.configure(self.vm.as_ref(), boot_setup)?; #[cfg(target_arch = "riscv64")] vcpu.configure(boot_setup)?; diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 4b04ee8eb..e52c5900b 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1433,11 +1433,11 @@ impl DeviceManager { )?; #[cfg(target_arch = "aarch64")] - self.add_legacy_devices(&legacy_interrupt_manager)?; + self.add_legacy_devices(legacy_interrupt_manager.as_ref())?; { self.ged_notification_device = self.add_acpi_devices( - &legacy_interrupt_manager, + legacy_interrupt_manager.as_ref(), self.reset_evt .try_clone() .map_err(DeviceManagerError::EventFd)?, @@ -1450,7 +1450,7 @@ impl DeviceManager { self.original_termios_opt = original_termios_opt; self.console = self.add_console_devices( - &legacy_interrupt_manager, + legacy_interrupt_manager.as_ref(), &mut virtio_devices, console_info, console_resize_pipe, @@ -1813,7 +1813,7 @@ impl DeviceManager { fn add_acpi_devices( &mut self, - interrupt_manager: &Arc>, + interrupt_manager: &dyn InterruptManager, reset_evt: EventFd, exit_evt: EventFd, ) -> DeviceManagerResult>>> { @@ -1999,7 +1999,7 @@ impl DeviceManager { #[cfg(target_arch = "aarch64")] fn add_legacy_devices( &mut self, - interrupt_manager: &Arc>, + interrupt_manager: &dyn InterruptManager, ) -> DeviceManagerResult<()> { // Add a RTC device let rtc_irq = self @@ -2133,7 +2133,7 @@ impl DeviceManager { #[cfg(target_arch = "x86_64")] fn add_serial_device( &mut self, - interrupt_manager: &Arc>, + interrupt_manager: &dyn InterruptManager, serial_writer: Option>, ) -> DeviceManagerResult>> { // Serial is tied to IRQ #4 @@ -2184,7 +2184,7 @@ impl DeviceManager { #[cfg(target_arch = "aarch64")] fn add_serial_device( &mut self, - interrupt_manager: &Arc>, + interrupt_manager: &dyn InterruptManager, serial_writer: Option>, ) -> DeviceManagerResult>> { let id = String::from(SERIAL_DEVICE_NAME); @@ -2248,7 +2248,7 @@ impl DeviceManager { #[cfg(target_arch = "riscv64")] fn add_serial_device( &mut self, - interrupt_manager: &Arc>, + interrupt_manager: &dyn InterruptManager, serial_writer: Option>, ) -> DeviceManagerResult>> { let id = String::from(SERIAL_DEVICE_NAME); @@ -2398,7 +2398,7 @@ impl DeviceManager { /// - virtio-console fn add_console_devices( &mut self, - interrupt_manager: &Arc>, + interrupt_manager: &dyn InterruptManager, virtio_devices: &mut Vec, console_info: Option, console_resize_pipe: Option>, @@ -3790,7 +3790,7 @@ impl DeviceManager { let vfio_pci_device = VfioPciDevice::new( vfio_name.clone(), - &self.address_manager.vm, + self.address_manager.vm.clone(), vfio_device, vfio_container, self.msi_interrupt_manager.clone(), @@ -3956,7 +3956,7 @@ impl DeviceManager { let mut vfio_user_pci_device = VfioUserPciDevice::new( vfio_user_name.clone(), - &self.address_manager.vm, + self.address_manager.vm.clone(), client.clone(), self.msi_interrupt_manager.clone(), legacy_interrupt_group, @@ -4134,7 +4134,7 @@ impl DeviceManager { virtio_device, msix_num, access_platform, - &self.msi_interrupt_manager, + self.msi_interrupt_manager.as_ref(), pci_device_bdf.into(), self.activate_evt .try_clone() @@ -4706,12 +4706,12 @@ impl DeviceManager { #[cfg(target_arch = "x86_64")] // Remove the device from the IO bus self.io_bus() - .remove_by_device(&bus_device) + .remove_by_device(bus_device.as_ref()) .map_err(DeviceManagerError::RemoveDeviceFromIoBus)?; // Remove the device from the MMIO bus self.mmio_bus() - .remove_by_device(&bus_device) + .remove_by_device(bus_device.as_ref()) .map_err(DeviceManagerError::RemoveDeviceFromMmioBus)?; // Remove the device from the list of BusDevice held by the diff --git a/vmm/src/interrupt.rs b/vmm/src/interrupt.rs index 8bafd800a..8c4c94b0e 100644 --- a/vmm/src/interrupt.rs +++ b/vmm/src/interrupt.rs @@ -40,7 +40,7 @@ impl InterruptRoute { }) } - pub fn enable(&self, vm: &Arc) -> Result<()> { + pub fn enable(&self, vm: &dyn hypervisor::Vm) -> Result<()> { if !self.registered.load(Ordering::Acquire) { vm.register_irqfd(&self.irq_fd, self.gsi) .map_err(|e| io::Error::other(format!("Failed registering irq_fd: {e}")))?; @@ -52,7 +52,7 @@ impl InterruptRoute { Ok(()) } - pub fn disable(&self, vm: &Arc) -> Result<()> { + pub fn disable(&self, vm: &dyn hypervisor::Vm) -> Result<()> { if self.registered.load(Ordering::Acquire) { vm.unregister_irqfd(&self.irq_fd, self.gsi) .map_err(|e| io::Error::other(format!("Failed unregistering irq_fd: {e}")))?; @@ -122,7 +122,7 @@ impl MsiInterruptGroup { impl InterruptSourceGroup for MsiInterruptGroup { fn enable(&self) -> Result<()> { for (_, route) in self.irq_routes.iter() { - route.enable(&self.vm)?; + route.enable(self.vm.as_ref())?; } Ok(()) @@ -130,7 +130,7 @@ impl InterruptSourceGroup for MsiInterruptGroup { fn disable(&self) -> Result<()> { for (_, route) in self.irq_routes.iter() { - route.disable(&self.vm)?; + route.disable(self.vm.as_ref())?; } Ok(()) @@ -172,7 +172,7 @@ impl InterruptSourceGroup for MsiInterruptGroup { // So it's required to call disable() (which deassign KVM_IRQFD) before // set_gsi_routes() to avoid kernel panic (see #3827) if masked { - route.disable(&self.vm)?; + route.disable(self.vm.as_ref())?; } let mut routes = self.gsi_msi_routes.lock().unwrap(); @@ -185,7 +185,7 @@ impl InterruptSourceGroup for MsiInterruptGroup { // panic on kernel which not have commit a80ced6ea514 // (KVM: SVM: fix panic on out-of-bounds guest IRQ). if !masked { - route.enable(&self.vm)?; + route.enable(self.vm.as_ref())?; } return Ok(()); diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index bac3b3e13..9137ba180 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -873,7 +873,7 @@ impl Vmm { } let vm = Vm::create_hypervisor_vm( - &self.hypervisor, + self.hypervisor.as_ref(), #[cfg(feature = "tdx")] false, #[cfg(feature = "sev_snp")] @@ -892,8 +892,10 @@ impl Vmm { vm.enable_x2apic_api().unwrap(); } - let phys_bits = - vm::physical_bits(&self.hypervisor, config.lock().unwrap().cpus.max_phys_bits); + let phys_bits = vm::physical_bits( + self.hypervisor.as_ref(), + config.lock().unwrap().cpus.max_phys_bits, + ); let memory_manager = MemoryManager::new( vm, @@ -1129,10 +1131,12 @@ impl Vmm { }; let amx = vm_config.lock().unwrap().cpus.features.amx; - let phys_bits = - vm::physical_bits(&hypervisor, vm_config.lock().unwrap().cpus.max_phys_bits); + let phys_bits = vm::physical_bits( + hypervisor.as_ref(), + vm_config.lock().unwrap().cpus.max_phys_bits, + ); arch::generate_common_cpuid( - &hypervisor, + hypervisor.as_ref(), &arch::CpuidConfig { phys_bits, kvm_hyperv: vm_config.lock().unwrap().cpus.kvm_hyperv, @@ -1268,9 +1272,10 @@ impl Vmm { let dest_cpuid = &{ let vm_config = &src_vm_config.lock().unwrap(); - let phys_bits = vm::physical_bits(&self.hypervisor, vm_config.cpus.max_phys_bits); + let phys_bits = + vm::physical_bits(self.hypervisor.as_ref(), vm_config.cpus.max_phys_bits); arch::generate_common_cpuid( - &self.hypervisor.clone(), + self.hypervisor.as_ref(), &arch::CpuidConfig { phys_bits, kvm_hyperv: vm_config.cpus.kvm_hyperv, diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 9ea83cb05..2271c65fd 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -493,7 +493,7 @@ impl VmOps for VmOpsHandler { } } -pub fn physical_bits(hypervisor: &Arc, max_phys_bits: u8) -> u8 { +pub fn physical_bits(hypervisor: &dyn hypervisor::Hypervisor, max_phys_bits: u8) -> u8 { let host_phys_bits = get_host_cpu_phys_bits(hypervisor); cmp::min(host_phys_bits, max_phys_bits) @@ -592,7 +592,7 @@ impl Vm { reset_evt.try_clone().map_err(Error::EventFdClone)?, #[cfg(feature = "guest_debug")] vm_debug_evt, - &hypervisor, + hypervisor.clone(), seccomp_action.clone(), vm_ops, #[cfg(feature = "tdx")] @@ -608,7 +608,7 @@ impl Vm { .lock() .unwrap() .populate_cpuid( - &hypervisor, + hypervisor.as_ref(), #[cfg(feature = "tdx")] tdx_enabled, ) @@ -1015,7 +1015,7 @@ impl Vm { }; let vm = Self::create_hypervisor_vm( - &hypervisor, + hypervisor.as_ref(), #[cfg(feature = "tdx")] tdx_enabled, #[cfg(feature = "sev_snp")] @@ -1029,7 +1029,10 @@ impl Vm { vm.enable_x2apic_api().unwrap(); } - let phys_bits = physical_bits(&hypervisor, vm_config.lock().unwrap().cpus.max_phys_bits); + let phys_bits = physical_bits( + hypervisor.as_ref(), + vm_config.lock().unwrap().cpus.max_phys_bits, + ); let memory_manager = if let Some(snapshot) = snapshot_from_id(snapshot.as_ref(), MEMORY_MANAGER_SNAPSHOT_ID) @@ -1078,7 +1081,7 @@ impl Vm { } pub fn create_hypervisor_vm( - hypervisor: &Arc, + hypervisor: &dyn hypervisor::Hypervisor, #[cfg(feature = "tdx")] tdx_enabled: bool, #[cfg(feature = "sev_snp")] sev_snp_enabled: bool, #[cfg(feature = "sev_snp")] mem_size: u64, @@ -2876,11 +2879,11 @@ impl Snapshottable for Vm { let common_cpuid = { let amx = self.config.lock().unwrap().cpus.features.amx; let phys_bits = physical_bits( - &self.hypervisor, + self.hypervisor.as_ref(), self.config.lock().unwrap().cpus.max_phys_bits, ); arch::generate_common_cpuid( - &self.hypervisor, + self.hypervisor.as_ref(), &arch::CpuidConfig { phys_bits, kvm_hyperv: self.config.lock().unwrap().cpus.kvm_hyperv,