Skip to content

Commit 1b823e8

Browse files
authored
Merge branch 'main' into fix/bar-rollback-defensive
2 parents 6fb6283 + 1b479e4 commit 1b823e8

14 files changed

Lines changed: 110 additions & 166 deletions

File tree

cloud-hypervisor/tests/common/utils.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -255,41 +255,6 @@ pub(crate) fn prepare_swtpm_daemon(tmp_dir: &TempDir) -> (std::process::Command,
255255
(swtpm_command, swtpm_socket_path)
256256
}
257257

258-
pub(crate) fn remote_command(api_socket: &str, command: &str, arg: Option<&str>) -> bool {
259-
let mut cmd = Command::new(clh_command("ch-remote"));
260-
cmd.args([&format!("--api-socket={api_socket}"), command]);
261-
262-
if let Some(arg) = arg {
263-
cmd.arg(arg);
264-
}
265-
let output = cmd.output().unwrap();
266-
if output.status.success() {
267-
true
268-
} else {
269-
eprintln!("Error running ch-remote command: {:?}", &cmd);
270-
let stderr = String::from_utf8_lossy(&output.stderr);
271-
eprintln!("stderr: {stderr}");
272-
false
273-
}
274-
}
275-
276-
pub(crate) fn remote_command_w_output(
277-
api_socket: &str,
278-
command: &str,
279-
arg: Option<&str>,
280-
) -> (bool, Vec<u8>) {
281-
let mut cmd = Command::new(clh_command("ch-remote"));
282-
cmd.args([&format!("--api-socket={api_socket}"), command]);
283-
284-
if let Some(arg) = arg {
285-
cmd.arg(arg);
286-
}
287-
288-
let output = cmd.output().expect("Failed to launch ch-remote");
289-
290-
(output.status.success(), output.stdout)
291-
}
292-
293258
pub(crate) fn resize_command(
294259
api_socket: &str,
295260
desired_vcpus: Option<u8>,

devices/src/ivshmem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl BusDevice for IvshmemDevice {
217217
impl PciDevice for IvshmemDevice {
218218
fn allocate_bars(
219219
&mut self,
220-
_allocator: &Arc<Mutex<SystemAllocator>>,
220+
_allocator: &mut SystemAllocator,
221221
mmio32_allocator: &mut AddressAllocator,
222222
mmio64_allocator: &mut AddressAllocator,
223223
resources: Option<Vec<Resource>>,

devices/src/pvmemcontrol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use std::collections::HashMap;
77
use std::ffi::CString;
8-
use std::sync::{Arc, Barrier, Mutex, RwLock};
8+
use std::sync::{Arc, Barrier, RwLock};
99
use std::{io, result};
1010

1111
use log::{debug, warn};
@@ -722,7 +722,7 @@ impl PciDevice for PvmemcontrolPciDevice {
722722

723723
fn allocate_bars(
724724
&mut self,
725-
_allocator: &Arc<Mutex<SystemAllocator>>,
725+
_allocator: &mut SystemAllocator,
726726
mmio32_allocator: &mut AddressAllocator,
727727
_mmio64_allocator: &mut AddressAllocator,
728728
resources: Option<Vec<Resource>>,

devices/src/pvpanic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use std::any::Any;
77
use std::result;
8-
use std::sync::{Arc, Barrier, Mutex};
8+
use std::sync::{Arc, Barrier};
99

1010
use anyhow::anyhow;
1111
use event_monitor::event;
@@ -174,7 +174,7 @@ impl PciDevice for PvPanicDevice {
174174

175175
fn allocate_bars(
176176
&mut self,
177-
_allocator: &Arc<Mutex<SystemAllocator>>,
177+
_allocator: &mut SystemAllocator,
178178
mmio32_allocator: &mut AddressAllocator,
179179
_mmio64_allocator: &mut AddressAllocator,
180180
resources: Option<Vec<Resource>>,

pci/src/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
66

77
use std::any::Any;
8-
use std::sync::{Arc, Barrier, Mutex};
8+
use std::sync::{Arc, Barrier};
99
use std::{io, result};
1010

1111
use serde::{Deserialize, Serialize};
@@ -49,7 +49,7 @@ pub trait PciDevice: Send {
4949
/// returns an address. Returns a Vec of (GuestAddress, GuestUsize) tuples.
5050
fn allocate_bars(
5151
&mut self,
52-
_allocator: &Arc<Mutex<SystemAllocator>>,
52+
_allocator: &mut SystemAllocator,
5353
_mmio32_allocator: &mut AddressAllocator,
5454
_mmio64_allocator: &mut AddressAllocator,
5555
_resources: Option<Vec<Resource>>,

pci/src/vfio.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl VfioCommon {
602602
#[allow(unused_variables)]
603603
pub(crate) fn allocate_bars(
604604
&mut self,
605-
allocator: &Arc<Mutex<SystemAllocator>>,
605+
allocator: &mut SystemAllocator,
606606
mmio32_allocator: &mut AddressAllocator,
607607
mmio64_allocator: &mut AddressAllocator,
608608
resources: Option<&[Resource]>,
@@ -741,8 +741,6 @@ impl VfioCommon {
741741
PciBarRegionType::IoRegion => {
742742
// The address needs to be 4 bytes aligned.
743743
allocator
744-
.lock()
745-
.unwrap()
746744
.allocate_io_addresses(restored_bar_addr, region_size, Some(0x4))
747745
.ok_or(PciDeviceError::IoAllocationFailed(region_size))?
748746
}
@@ -1852,7 +1850,7 @@ const PCI_ROM_EXP_BAR_INDEX: usize = 12;
18521850
impl PciDevice for VfioPciDevice {
18531851
fn allocate_bars(
18541852
&mut self,
1855-
allocator: &Arc<Mutex<SystemAllocator>>,
1853+
allocator: &mut SystemAllocator,
18561854
mmio32_allocator: &mut AddressAllocator,
18571855
mmio64_allocator: &mut AddressAllocator,
18581856
resources: Option<Vec<Resource>>,

pci/src/vfio_user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl Vfio for VfioUserClientWrapper {
391391
impl PciDevice for VfioUserPciDevice {
392392
fn allocate_bars(
393393
&mut self,
394-
allocator: &Arc<Mutex<SystemAllocator>>,
394+
allocator: &mut SystemAllocator,
395395
mmio32_allocator: &mut AddressAllocator,
396396
mmio64_allocator: &mut AddressAllocator,
397397
resources: Option<Vec<Resource>>,

performance-metrics/src/performance_tests.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
// Performance tests
77

8-
use std::path::PathBuf;
98
use std::time::Duration;
109
use std::{fs, thread};
1110

@@ -14,8 +13,6 @@ use thiserror::Error;
1413

1514
use crate::{ImageFormat, PerformanceTestControl, PerformanceTestOverrides, mean};
1615

17-
#[cfg(target_arch = "x86_64")]
18-
pub const FOCAL_IMAGE_NAME: &str = "focal-server-cloudimg-amd64-custom-20210609-0.raw";
1916
#[cfg(target_arch = "aarch64")]
2017
pub const FOCAL_IMAGE_NAME: &str = "focal-server-cloudimg-arm64-custom-20210929-0-update-tool.raw";
2118

@@ -127,45 +124,6 @@ fn performance_test_new_guest(disk_config: Box<dyn DiskConfig>) -> Guest {
127124
Guest::new_from_ip_range(disk_config, "172.19", 0)
128125
}
129126

130-
const DIRECT_KERNEL_BOOT_CMDLINE: &str =
131-
"root=/dev/vda1 console=hvc0 rw systemd.journald.forward_to_console=1";
132-
133-
// Creates the path for direct kernel boot and return the path.
134-
// For x86_64, this function returns the vmlinux kernel path.
135-
// For AArch64, this function returns the PE kernel path.
136-
fn direct_kernel_boot_path() -> PathBuf {
137-
let mut workload_path = dirs::home_dir().unwrap();
138-
workload_path.push("workloads");
139-
140-
let mut kernel_path = workload_path;
141-
#[cfg(target_arch = "x86_64")]
142-
kernel_path.push("vmlinux-x86_64");
143-
#[cfg(target_arch = "aarch64")]
144-
kernel_path.push("Image-arm64");
145-
146-
kernel_path
147-
}
148-
149-
fn remote_command(api_socket: &str, command: &str, arg: Option<&str>) -> bool {
150-
let mut cmd = std::process::Command::new(clh_command("ch-remote"));
151-
cmd.args([&format!("--api-socket={api_socket}"), command]);
152-
153-
if let Some(arg) = arg {
154-
cmd.arg(arg);
155-
}
156-
let output = cmd.output().unwrap();
157-
if output.status.success() {
158-
true
159-
} else {
160-
eprintln!(
161-
"Error running ch-remote command: {:?}\nstderr: {}",
162-
&cmd,
163-
String::from_utf8_lossy(&output.stderr)
164-
);
165-
false
166-
}
167-
}
168-
169127
pub fn performance_net_throughput(control: &PerformanceTestControl) -> f64 {
170128
let test_timeout = control.test_timeout;
171129
let (rx, bandwidth) = control.net_control.unwrap();

test_infra/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,42 @@ pub fn clh_command(cmd: &str) -> String {
17761776
String::from(full_path.to_str().unwrap())
17771777
}
17781778

1779+
pub fn remote_command(api_socket: &str, command: &str, arg: Option<&str>) -> bool {
1780+
let mut cmd = Command::new(clh_command("ch-remote"));
1781+
cmd.args([&format!("--api-socket={api_socket}"), command]);
1782+
1783+
if let Some(arg) = arg {
1784+
cmd.arg(arg);
1785+
}
1786+
1787+
let output = cmd.output().unwrap();
1788+
if output.status.success() {
1789+
true
1790+
} else {
1791+
eprintln!("Error running ch-remote command: {:?}", &cmd);
1792+
let stderr = String::from_utf8_lossy(&output.stderr);
1793+
eprintln!("stderr: {stderr}");
1794+
false
1795+
}
1796+
}
1797+
1798+
pub fn remote_command_w_output(
1799+
api_socket: &str,
1800+
command: &str,
1801+
arg: Option<&str>,
1802+
) -> (bool, Vec<u8>) {
1803+
let mut cmd = Command::new(clh_command("ch-remote"));
1804+
cmd.args([&format!("--api-socket={api_socket}"), command]);
1805+
1806+
if let Some(arg) = arg {
1807+
cmd.arg(arg);
1808+
}
1809+
1810+
let output = cmd.output().expect("Failed to launch ch-remote");
1811+
1812+
(output.status.success(), output.stdout)
1813+
}
1814+
17791815
pub fn parse_iperf3_output(output: &[u8], sender: bool, bandwidth: bool) -> Result<f64, Error> {
17801816
std::panic::catch_unwind(|| {
17811817
let s = String::from_utf8_lossy(output);

virtio-devices/src/block.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -974,24 +974,24 @@ impl Block {
974974
}
975975
}
976976

977-
fn update_writeback(&mut self) {
978-
// Use writeback from config if VIRTIO_BLK_F_CONFIG_WCE
979-
let writeback = if self.common.feature_acked(VIRTIO_BLK_F_CONFIG_WCE.into()) {
980-
self.config.writeback == 1
981-
} else {
982-
// Else check if VIRTIO_BLK_F_FLUSH negotiated
983-
self.common.feature_acked(VIRTIO_BLK_F_FLUSH.into())
984-
};
977+
/// The virtio v1.2 spec says "If VIRTIO_BLK_F_CONFIG_WCE was not
978+
/// negotiated but VIRTIO_BLK_F_FLUSH was, the driver SHOULD assume
979+
/// presence of a writeback cache." It also says "If
980+
/// VIRTIO_BLK_F_CONFIG_WCE is negotiated but VIRTIO_BLK_F_FLUSH is not,
981+
/// the device MUST initialize writeback to 0."
982+
fn is_writeback_enabled(&self, desired: bool) -> bool {
983+
let flush = self.common.feature_acked(VIRTIO_BLK_F_FLUSH.into());
984+
let wce = self.common.feature_acked(VIRTIO_BLK_F_CONFIG_WCE.into());
985+
if wce { flush && desired } else { flush }
986+
}
985987

988+
fn set_writeback_mode(&mut self, enabled: bool) {
989+
self.config.writeback = enabled as u8;
990+
self.writeback.store(enabled, Ordering::Release);
986991
info!(
987992
"Changing cache mode to {}",
988-
if writeback {
989-
"writeback"
990-
} else {
991-
"writethrough"
992-
}
993+
if enabled { "writeback" } else { "writethrough" }
993994
);
994-
self.writeback.store(writeback, Ordering::Release);
995995
}
996996

997997
pub fn resize(&mut self, new_size: u64) -> Result<()> {
@@ -1073,8 +1073,8 @@ impl VirtioDevice for Block {
10731073
return;
10741074
}
10751075

1076-
self.config.writeback = data[0];
1077-
self.update_writeback();
1076+
let writeback = self.is_writeback_enabled(data[0] == 1);
1077+
self.set_writeback_mode(writeback);
10781078
}
10791079

10801080
fn activate(&mut self, context: crate::device::ActivationContext) -> ActivateResult {
@@ -1097,7 +1097,8 @@ impl VirtioDevice for Block {
10971097
// Recompute the barrier size from the queues that are actually activated.
10981098
self.common.paused_sync = Some(Arc::new(Barrier::new(queues.len() + 1)));
10991099

1100-
self.update_writeback();
1100+
let writeback = self.is_writeback_enabled(self.config.writeback == 1);
1101+
self.set_writeback_mode(writeback);
11011102

11021103
let mut epoll_threads = Vec::new();
11031104
let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into());
@@ -1167,6 +1168,7 @@ impl VirtioDevice for Block {
11671168

11681169
fn reset(&mut self) -> Option<Arc<dyn VirtioInterrupt>> {
11691170
let result = self.common.reset();
1171+
self.set_writeback_mode(true);
11701172
event!("virtio-device", "reset", "id", &self.id);
11711173
result
11721174
}

0 commit comments

Comments
 (0)