From e55297ab609b5a179002048a4159be70be33719c Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 16 Feb 2024 15:45:05 +0000 Subject: [PATCH] refactor(virtio): Simplify read_config `io::Write` has higher overhead and unnecessary error handling. Slices can be copied without it. Signed-off-by: Kornel --- src/vmm/src/devices/virtio/balloon/device.rs | 18 +++++------------- .../devices/virtio/block/vhost_user/device.rs | 15 ++++----------- src/vmm/src/devices/virtio/net/device.rs | 16 ++++------------ 3 files changed, 13 insertions(+), 36 deletions(-) diff --git a/src/vmm/src/devices/virtio/balloon/device.rs b/src/vmm/src/devices/virtio/balloon/device.rs index 34e19a93ea4..9efb6bcb906 100644 --- a/src/vmm/src/devices/virtio/balloon/device.rs +++ b/src/vmm/src/devices/virtio/balloon/device.rs @@ -5,7 +5,7 @@ use std::io::Write; use std::sync::atomic::AtomicU32; use std::sync::Arc; use std::time::Duration; -use std::{cmp, fmt}; +use std::fmt; use log::error; use serde::Serialize; @@ -594,19 +594,11 @@ impl VirtioDevice for Balloon { } fn read_config(&self, offset: u64, mut data: &mut [u8]) { - let config_space_bytes = self.config_space.as_slice(); - let config_len = config_space_bytes.len() as u64; - if offset >= config_len { + if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) { + let len = config_space_bytes.len().min(data.len()); + data[..len].copy_from_slice(&config_space_bytes[..len]); + } else { error!("Failed to read config space"); - return; - } - - if let Some(end) = offset.checked_add(data.len() as u64) { - // This write can't fail, offset and end are checked against config_len. - data.write_all( - &config_space_bytes[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))], - ) - .unwrap(); } } diff --git a/src/vmm/src/devices/virtio/block/vhost_user/device.rs b/src/vmm/src/devices/virtio/block/vhost_user/device.rs index a9cb8523f0f..b64cdb4c52a 100644 --- a/src/vmm/src/devices/virtio/block/vhost_user/device.rs +++ b/src/vmm/src/devices/virtio/block/vhost_user/device.rs @@ -4,7 +4,6 @@ // Portions Copyright 2019 Intel Corporation. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use std::cmp; use std::io::Write; use std::sync::atomic::AtomicU32; use std::sync::Arc; @@ -323,18 +322,12 @@ impl VirtioDevice for VhostUserBlock } fn read_config(&self, offset: u64, mut data: &mut [u8]) { - let config_len = self.config_space.len() as u64; - if offset >= config_len { + if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) { + let len = config_space_bytes.len().min(data.len()); + data[..len].copy_from_slice(&config_space_bytes[..len]); + } else { error!("Failed to read config space"); self.metrics.cfg_fails.inc(); - return; - } - if let Some(end) = offset.checked_add(data.len() as u64) { - // This write can't fail, offset and end are checked against config_len. - data.write_all( - &self.config_space[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))], - ) - .unwrap(); } } diff --git a/src/vmm/src/devices/virtio/net/device.rs b/src/vmm/src/devices/virtio/net/device.rs index a4adeee6e07..f6463feeffc 100755 --- a/src/vmm/src/devices/virtio/net/device.rs +++ b/src/vmm/src/devices/virtio/net/device.rs @@ -7,7 +7,6 @@ #[cfg(not(test))] use std::io::Read; -use std::io::Write; use std::net::Ipv4Addr; use std::sync::atomic::AtomicU32; use std::sync::{Arc, Mutex}; @@ -822,19 +821,12 @@ impl VirtioDevice for Net { } fn read_config(&self, offset: u64, mut data: &mut [u8]) { - let config_space_bytes = self.config_space.as_slice(); - let config_len = config_space_bytes.len() as u64; - if offset >= config_len { + if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) { + let len = config_space_bytes.len().min(data.len()); + data[..len].copy_from_slice(&config_space_bytes[..len]); + } else { error!("Failed to read config space"); self.metrics.cfg_fails.inc(); - return; - } - if let Some(end) = offset.checked_add(data.len() as u64) { - // This write can't fail, offset and end are checked against config_len. - data.write_all( - &config_space_bytes[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))], - ) - .unwrap(); } }