Skip to content

Commit bd18bfe

Browse files
kornelskiroypat
authored andcommitted
refactor(virtio): Simplify read_config
From: Kornel <[email protected]> `io::Write` has higher overhead and unnecessary error handling. Slices can be copied without it. Signed-off-by: Kornel <[email protected]> Signed-off-by: Patrick Roy <[email protected]> Co-authored-by: Patrick Roy <[email protected]>
1 parent ecf5ebf commit bd18bfe

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

src/vmm/src/devices/virtio/balloon/device.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::io::Write;
4+
use std::fmt;
55
use std::sync::atomic::AtomicU32;
66
use std::sync::Arc;
77
use std::time::Duration;
8-
use std::{cmp, fmt};
98

109
use log::error;
1110
use serde::Serialize;
@@ -593,20 +592,12 @@ impl VirtioDevice for Balloon {
593592
self.irq_trigger.irq_status.clone()
594593
}
595594

596-
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
597-
let config_space_bytes = self.config_space.as_slice();
598-
let config_len = config_space_bytes.len() as u64;
599-
if offset >= config_len {
595+
fn read_config(&self, offset: u64, data: &mut [u8]) {
596+
if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) {
597+
let len = config_space_bytes.len().min(data.len());
598+
data[..len].copy_from_slice(&config_space_bytes[..len]);
599+
} else {
600600
error!("Failed to read config space");
601-
return;
602-
}
603-
604-
if let Some(end) = offset.checked_add(data.len() as u64) {
605-
// This write can't fail, offset and end are checked against config_len.
606-
data.write_all(
607-
&config_space_bytes[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))],
608-
)
609-
.unwrap();
610601
}
611602
}
612603

src/vmm/src/devices/virtio/block/vhost_user/device.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// Portions Copyright 2019 Intel Corporation. All Rights Reserved.
55
// SPDX-License-Identifier: Apache-2.0
66

7-
use std::cmp;
8-
use std::io::Write;
97
use std::sync::atomic::AtomicU32;
108
use std::sync::Arc;
119

@@ -322,19 +320,13 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
322320
self.irq_trigger.irq_status.clone()
323321
}
324322

325-
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
326-
let config_len = self.config_space.len() as u64;
327-
if offset >= config_len {
323+
fn read_config(&self, offset: u64, data: &mut [u8]) {
324+
if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) {
325+
let len = config_space_bytes.len().min(data.len());
326+
data[..len].copy_from_slice(&config_space_bytes[..len]);
327+
} else {
328328
error!("Failed to read config space");
329329
self.metrics.cfg_fails.inc();
330-
return;
331-
}
332-
if let Some(end) = offset.checked_add(data.len() as u64) {
333-
// This write can't fail, offset and end are checked against config_len.
334-
data.write_all(
335-
&self.config_space[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))],
336-
)
337-
.unwrap();
338330
}
339331
}
340332

src/vmm/src/devices/virtio/net/device.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
#[cfg(not(test))]
99
use std::io::Read;
10-
use std::io::Write;
10+
use std::mem;
1111
use std::net::Ipv4Addr;
1212
use std::sync::atomic::AtomicU32;
1313
use std::sync::{Arc, Mutex};
14-
use std::{cmp, mem};
1514

1615
use libc::EAGAIN;
1716
use log::{error, warn};
@@ -832,20 +831,13 @@ impl VirtioDevice for Net {
832831
self.irq_trigger.irq_status.clone()
833832
}
834833

835-
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
836-
let config_space_bytes = self.config_space.as_slice();
837-
let config_len = config_space_bytes.len() as u64;
838-
if offset >= config_len {
834+
fn read_config(&self, offset: u64, data: &mut [u8]) {
835+
if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) {
836+
let len = config_space_bytes.len().min(data.len());
837+
data[..len].copy_from_slice(&config_space_bytes[..len]);
838+
} else {
839839
error!("Failed to read config space");
840840
self.metrics.cfg_fails.inc();
841-
return;
842-
}
843-
if let Some(end) = offset.checked_add(data.len() as u64) {
844-
// This write can't fail, offset and end are checked against config_len.
845-
data.write_all(
846-
&config_space_bytes[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))],
847-
)
848-
.unwrap();
849841
}
850842
}
851843

0 commit comments

Comments
 (0)