diff --git a/src/seccompiler/src/seccompiler_bin.rs b/src/seccompiler/src/seccompiler_bin.rs index 692ec5c8494..c6c97690bf6 100644 --- a/src/seccompiler/src/seccompiler_bin.rs +++ b/src/seccompiler/src/seccompiler_bin.rs @@ -112,16 +112,14 @@ fn build_arg_parser() -> ArgParser<'static> { } fn get_argument_values(arguments: &ArgumentsBag) -> Result { - let arch_string = arguments.single_value("target-arch"); - if arch_string.is_none() { + let Some(arch_string) = arguments.single_value("target-arch") else { return Err(SeccompError::MissingTargetArch); - } - let target_arch: TargetArch = arch_string.unwrap().as_str().try_into()?; + }; + let target_arch: TargetArch = arch_string.as_str().try_into()?; - let input_file = arguments.single_value("input-file"); - if input_file.is_none() { + let Some(input_file) = arguments.single_value("input-file") else { return Err(SeccompError::MissingInputFile); - } + }; let is_basic = arguments.flag_present("basic"); if is_basic { @@ -133,7 +131,7 @@ fn get_argument_values(arguments: &ArgumentsBag) -> Result Arc { - if METRICS.read().unwrap().metrics.get(&drive_id).is_none() { + Arc::clone( METRICS .write() .unwrap() .metrics - .insert(drive_id.clone(), Arc::new(BlockDeviceMetrics::new())); - } - METRICS - .read() - .unwrap() - .metrics - .get(&drive_id) - .unwrap() - .clone() + .entry(drive_id) + .or_insert_with(|| Arc::new(BlockDeviceMetrics::default())), + ) } } diff --git a/src/vmm/src/devices/virtio/net/metrics.rs b/src/vmm/src/devices/virtio/net/metrics.rs index 135fe1c8639..684a248cdc0 100644 --- a/src/vmm/src/devices/virtio/net/metrics.rs +++ b/src/vmm/src/devices/virtio/net/metrics.rs @@ -102,20 +102,14 @@ impl NetMetricsPerDevice { /// lock is always initialized so it is safe the unwrap /// the lock without a check. pub fn alloc(iface_id: String) -> Arc { - if METRICS.read().unwrap().metrics.get(&iface_id).is_none() { + Arc::clone( METRICS .write() .unwrap() .metrics - .insert(iface_id.clone(), Arc::new(NetDeviceMetrics::new())); - } - METRICS - .read() - .unwrap() - .metrics - .get(&iface_id) - .unwrap() - .clone() + .entry(iface_id) + .or_insert_with(|| Arc::new(NetDeviceMetrics::default())), + ) } } diff --git a/src/vmm/src/devices/virtio/vhost_user_metrics.rs b/src/vmm/src/devices/virtio/vhost_user_metrics.rs index ed64be31d59..87c86e847fb 100644 --- a/src/vmm/src/devices/virtio/vhost_user_metrics.rs +++ b/src/vmm/src/devices/virtio/vhost_user_metrics.rs @@ -95,19 +95,14 @@ impl VhostUserMetricsPerDevice { /// lock is always initialized so it is safe the unwrap /// the lock without a check. pub fn alloc(drive_id: String) -> Arc { - if METRICS.read().unwrap().metrics.get(&drive_id).is_none() { - METRICS.write().unwrap().metrics.insert( - drive_id.clone(), - Arc::new(VhostUserDeviceMetrics::default()), - ); - } - METRICS - .read() - .unwrap() - .metrics - .get(&drive_id) - .unwrap() - .clone() + Arc::clone( + METRICS + .write() + .unwrap() + .metrics + .entry(drive_id) + .or_insert_with(|| Arc::new(VhostUserDeviceMetrics::default())), + ) } } diff --git a/src/vmm/src/devices/virtio/vsock/unix/muxer_killq.rs b/src/vmm/src/devices/virtio/vsock/unix/muxer_killq.rs index c7b5b66c58f..360dfdf49e9 100644 --- a/src/vmm/src/devices/virtio/vsock/unix/muxer_killq.rs +++ b/src/vmm/src/devices/virtio/vsock/unix/muxer_killq.rs @@ -106,7 +106,7 @@ impl MuxerKillQ { pub fn pop(&mut self) -> Option { if let Some(item) = self.q.front() { if Instant::now() > item.kill_time { - return Some(self.q.pop_front().unwrap().key); + return self.q.pop_front().map(|entry| entry.key); } } None diff --git a/src/vmm/src/dumbo/tcp/connection.rs b/src/vmm/src/dumbo/tcp/connection.rs index 1ca8042f8d2..5fbcd089067 100644 --- a/src/vmm/src/dumbo/tcp/connection.rs +++ b/src/vmm/src/dumbo/tcp/connection.rs @@ -714,13 +714,9 @@ impl Connection { // We check this here because if a valid payload has been received, then we must have // set enqueue_ack = true earlier. - if payload_len > 0 { + if let Some(payload_len) = NonZeroUsize::new(payload_len.into()) { buf[..payload_len.into()].copy_from_slice(s.payload()); - // The unwrap is safe because payload_len > 0. - return Ok(( - Some(NonZeroUsize::new(payload_len.into()).unwrap()), - recv_status_flags, - )); + return Ok((Some(payload_len), recv_status_flags)); } } diff --git a/src/vmm/src/vmm_config/net.rs b/src/vmm/src/vmm_config/net.rs index c4e835e1ad9..5782face4b5 100644 --- a/src/vmm/src/vmm_config/net.rs +++ b/src/vmm/src/vmm_config/net.rs @@ -109,20 +109,20 @@ impl NetBuilder { &mut self, netif_config: NetworkInterfaceConfig, ) -> Result>, NetworkInterfaceError> { - let mac_conflict = |net: &Arc>| { - let net = net.lock().expect("Poisoned lock"); - // Check if another net dev has same MAC. - netif_config.guest_mac.is_some() - && netif_config.guest_mac.as_ref() == net.guest_mac() - && &netif_config.iface_id != net.id() - }; - // Validate there is no Mac conflict. - // No need to validate host_dev_name conflict. In such a case, - // an error will be thrown during device creation anyway. - if self.net_devices.iter().any(mac_conflict) { - return Err(NetworkInterfaceError::GuestMacAddressInUse( - netif_config.guest_mac.unwrap().to_string(), - )); + if let Some(ref mac_address) = netif_config.guest_mac { + let mac_conflict = |net: &Arc>| { + let net = net.lock().expect("Poisoned lock"); + // Check if another net dev has same MAC. + Some(mac_address) == net.guest_mac() && &netif_config.iface_id != net.id() + }; + // Validate there is no Mac conflict. + // No need to validate host_dev_name conflict. In such a case, + // an error will be thrown during device creation anyway. + if self.net_devices.iter().any(mac_conflict) { + return Err(NetworkInterfaceError::GuestMacAddressInUse( + mac_address.to_string(), + )); + } } // If this is an update, just remove the old one. diff --git a/src/vmm/src/vstate/vm.rs b/src/vmm/src/vstate/vm.rs index 1e0e451e058..55b0ec146e0 100644 --- a/src/vmm/src/vstate/vm.rs +++ b/src/vmm/src/vstate/vm.rs @@ -235,10 +235,10 @@ impl Vm { } guest_mem .iter() - .enumerate() - .try_for_each(|(index, region)| { + .zip(0u32..) + .try_for_each(|(region, slot)| { let memory_region = kvm_userspace_memory_region { - slot: u32::try_from(index).unwrap(), + slot, guest_phys_addr: region.start_addr().raw_value(), memory_size: region.len(), // It's safe to unwrap because the guest address is valid.