misc: cleanup &Arc<dyn T> -> &dyn T

Consuming `&Arc<T>` 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<dyn T>` 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 <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster
2025-10-28 10:49:34 +01:00
committed by Rob Bradford
parent e295719967
commit 7536a95424
37 changed files with 121 additions and 117 deletions

View File

@@ -413,7 +413,7 @@ impl Vcpu {
pub fn new(
id: u32,
apic_id: u32,
vm: &Arc<dyn hypervisor::Vm>,
vm: &dyn hypervisor::Vm,
vm_ops: Option<Arc<dyn VmOps>>,
#[cfg(target_arch = "x86_64")] cpu_vendor: CpuVendor,
) -> Result<Self> {
@@ -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<dyn hypervisor::Vm>,
#[cfg(target_arch = "aarch64")] vm: &dyn hypervisor::Vm,
boot_setup: Option<(EntryPoint, &GuestMemoryAtomic<GuestMemoryMmap>)>,
#[cfg(target_arch = "x86_64")] cpuid: Vec<CpuIdEntry>,
#[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<dyn hypervisor::Vm>) -> 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<dyn hypervisor::Hypervisor>,
hypervisor: Arc<dyn hypervisor::Hypervisor>,
seccomp_action: SeccompAction,
vm_ops: Arc<dyn VmOps>,
#[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<dyn hypervisor::Hypervisor>,
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)?;

View File

@@ -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<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
interrupt_manager: &dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>,
reset_evt: EventFd,
exit_evt: EventFd,
) -> DeviceManagerResult<Option<Arc<Mutex<devices::AcpiGedDevice>>>> {
@@ -1999,7 +1999,7 @@ impl DeviceManager {
#[cfg(target_arch = "aarch64")]
fn add_legacy_devices(
&mut self,
interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
interrupt_manager: &dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>,
) -> 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<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
interrupt_manager: &dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>,
serial_writer: Option<Box<dyn io::Write + Send>>,
) -> DeviceManagerResult<Arc<Mutex<Serial>>> {
// 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<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
interrupt_manager: &dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>,
serial_writer: Option<Box<dyn io::Write + Send>>,
) -> DeviceManagerResult<Arc<Mutex<Pl011>>> {
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<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
interrupt_manager: &dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>,
serial_writer: Option<Box<dyn io::Write + Send>>,
) -> DeviceManagerResult<Arc<Mutex<Serial>>> {
let id = String::from(SERIAL_DEVICE_NAME);
@@ -2398,7 +2398,7 @@ impl DeviceManager {
/// - virtio-console
fn add_console_devices(
&mut self,
interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
interrupt_manager: &dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>,
virtio_devices: &mut Vec<MetaVirtioDevice>,
console_info: Option<ConsoleInfo>,
console_resize_pipe: Option<Arc<File>>,
@@ -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

View File

@@ -40,7 +40,7 @@ impl InterruptRoute {
})
}
pub fn enable(&self, vm: &Arc<dyn hypervisor::Vm>) -> 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<dyn hypervisor::Vm>) -> 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(());

View File

@@ -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,

View File

@@ -493,7 +493,7 @@ impl VmOps for VmOpsHandler {
}
}
pub fn physical_bits(hypervisor: &Arc<dyn hypervisor::Hypervisor>, 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<dyn hypervisor::Hypervisor>,
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,