misc: clippy: add needless_pass_by_value (partially)

This helps to uncover expensive and needless clones in the code base.
For example, I prevented extensive clones in the snapshot path where
(nested) BTreeMap's have been cloned over and over again. Further,
the lint helps devs to much better reason about the ownership of
parameters.

All of these changes have been done manually with the necessary
caution. A few structs that are cheap to clone are now `copy` so that
this lint won't trigger for them.

I didn't enable the lint so far as it is a massive rabbit hole and
needs much more fixes. Nevertheless, it is very useful.

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-11-25 14:29:36 +01:00
committed by Rob Bradford
parent 0a07c96d17
commit 6a86c157af
29 changed files with 162 additions and 155 deletions

View File

@@ -901,7 +901,11 @@ impl CpuManager {
Ok(())
}
fn create_vcpu(&mut self, cpu_id: u32, snapshot: Option<Snapshot>) -> Result<Arc<Mutex<Vcpu>>> {
fn create_vcpu(
&mut self,
cpu_id: u32,
snapshot: Option<&Snapshot>,
) -> Result<Arc<Mutex<Vcpu>>> {
info!("Creating vCPU: cpu_id = {cpu_id}");
#[cfg(target_arch = "x86_64")]
@@ -1006,7 +1010,7 @@ impl CpuManager {
fn create_vcpus(
&mut self,
desired_vcpus: u32,
snapshot: Option<Snapshot>,
snapshot: Option<&Snapshot>,
) -> Result<Vec<Arc<Mutex<Vcpu>>>> {
let mut vcpus: Vec<Arc<Mutex<Vcpu>>> = vec![];
info!(
@@ -1027,7 +1031,7 @@ impl CpuManager {
cpu_id,
// TODO: The special format of the CPU id can be removed once
// ready to break live upgrade.
snapshot_from_id(snapshot.as_ref(), cpu_id.to_string().as_str()),
snapshot_from_id(snapshot, cpu_id.to_string().as_str()),
)?);
}
@@ -1386,7 +1390,7 @@ impl CpuManager {
pub fn create_boot_vcpus(
&mut self,
snapshot: Option<Snapshot>,
snapshot: Option<&Snapshot>,
) -> Result<Vec<Arc<Mutex<Vcpu>>>> {
trace_scoped!("create_boot_vcpus");

View File

@@ -1155,7 +1155,7 @@ impl DeviceManager {
force_iommu: bool,
boot_id_list: BTreeSet<String>,
#[cfg(not(target_arch = "riscv64"))] timestamp: Instant,
snapshot: Option<Snapshot>,
snapshot: Option<&Snapshot>,
dynamic: bool,
) -> DeviceManagerResult<Arc<Mutex<Self>>> {
trace_scoped!("DeviceManager::new");
@@ -1367,7 +1367,7 @@ impl DeviceManager {
timestamp,
pending_activations: Arc::new(Mutex::new(Vec::default())),
acpi_platform_addresses: AcpiPlatformAddresses::default(),
snapshot,
snapshot: snapshot.cloned(),
rate_limit_groups,
mmio_regions: Arc::new(Mutex::new(Vec::new())),
#[cfg(feature = "fw_cfg")]
@@ -1795,7 +1795,7 @@ impl DeviceManager {
ioapic::Ioapic::new(
id.clone(),
APIC_START,
Arc::clone(&self.msi_interrupt_manager),
self.msi_interrupt_manager.as_ref(),
state_from_id(self.snapshot.as_ref(), id.as_str())
.map_err(DeviceManagerError::RestoreGetState)?,
)
@@ -2489,7 +2489,7 @@ impl DeviceManager {
tpm_path: PathBuf,
) -> DeviceManagerResult<Arc<Mutex<devices::tpm::Tpm>>> {
// Create TPM Device
let tpm = devices::tpm::Tpm::new(tpm_path.to_str().unwrap().to_string()).map_err(|e| {
let tpm = devices::tpm::Tpm::new(tpm_path.to_str().unwrap()).map_err(|e| {
DeviceManagerError::CreateTpmDevice(anyhow!("Failed to create TPM Device : {e:?}"))
})?;
let tpm = Arc::new(Mutex::new(tpm));

View File

@@ -965,7 +965,7 @@ impl Vmm {
self.console_info.clone(),
self.console_resize_pipe.clone(),
Arc::clone(&self.original_termios_opt),
Some(snapshot),
Some(&snapshot),
)
.map_err(|e| {
MigratableError::MigrateReceive(anyhow!("Error creating VM from snapshot: {e:?}"))
@@ -1334,7 +1334,7 @@ impl Vmm {
self.console_info.clone(),
self.console_resize_pipe.clone(),
Arc::clone(&self.original_termios_opt),
Some(snapshot),
Some(&snapshot),
Some(source_url),
Some(prefault),
)?;

View File

@@ -1169,7 +1169,7 @@ impl MemoryManager {
start_of_platform_device_area,
PLATFORM_DEVICE_AREA_SIZE,
#[cfg(target_arch = "x86_64")]
vec![GsiApic::new(
&[GsiApic::new(
X86_64_IRQ_BASE,
ioapic::NUM_IOAPIC_PINS as u32 - X86_64_IRQ_BASE,
)],

View File

@@ -543,7 +543,7 @@ impl Vm {
console_info: Option<ConsoleInfo>,
console_resize_pipe: Option<Arc<File>>,
original_termios: Arc<Mutex<Option<termios>>>,
snapshot: Option<Snapshot>,
snapshot: Option<&Snapshot>,
) -> Result<Self> {
trace_scoped!("Vm::new_from_memory_manager");
@@ -658,7 +658,7 @@ impl Vm {
boot_id_list,
#[cfg(not(target_arch = "riscv64"))]
timestamp,
snapshot_from_id(snapshot.as_ref(), DEVICE_MANAGER_SNAPSHOT_ID),
snapshot_from_id(snapshot, DEVICE_MANAGER_SNAPSHOT_ID),
dynamic,
)
.map_err(Error::DeviceManager)?;
@@ -723,7 +723,7 @@ impl Vm {
cpu_manager
.lock()
.unwrap()
.create_boot_vcpus(snapshot_from_id(snapshot.as_ref(), CPU_MANAGER_SNAPSHOT_ID))
.create_boot_vcpus(snapshot_from_id(snapshot, CPU_MANAGER_SNAPSHOT_ID))
.map_err(Error::CpuManager)?;
// For KVM, we need to create interrupt controller after we create boot vcpus.
@@ -992,7 +992,7 @@ impl Vm {
console_info: Option<ConsoleInfo>,
console_resize_pipe: Option<Arc<File>>,
original_termios: Arc<Mutex<Option<termios>>>,
snapshot: Option<Snapshot>,
snapshot: Option<&Snapshot>,
source_url: Option<&str>,
prefault: Option<bool>,
) -> Result<Self> {
@@ -1035,31 +1035,30 @@ impl Vm {
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)
{
MemoryManager::new_from_snapshot(
&snapshot,
vm.clone(),
&vm_config.lock().unwrap().memory.clone(),
source_url,
prefault.unwrap(),
phys_bits,
)
.map_err(Error::MemoryManager)?
} else {
MemoryManager::new(
vm.clone(),
&vm_config.lock().unwrap().memory.clone(),
None,
phys_bits,
#[cfg(feature = "tdx")]
tdx_enabled,
None,
None,
)
.map_err(Error::MemoryManager)?
};
let memory_manager =
if let Some(snapshot) = snapshot_from_id(snapshot, MEMORY_MANAGER_SNAPSHOT_ID) {
MemoryManager::new_from_snapshot(
snapshot,
vm.clone(),
&vm_config.lock().unwrap().memory.clone(),
source_url,
prefault.unwrap(),
phys_bits,
)
.map_err(Error::MemoryManager)?
} else {
MemoryManager::new(
vm.clone(),
&vm_config.lock().unwrap().memory.clone(),
None,
phys_bits,
#[cfg(feature = "tdx")]
tdx_enabled,
None,
None,
)
.map_err(Error::MemoryManager)?
};
Vm::new_from_memory_manager(
vm_config,