Skip to content

Commit 9cfe242

Browse files
committed
Update to vm-memory 0.13.1
The new version of vm-memory requires to bump the log crate to version 0.4.20 and vm-virtio to 0.10.0. This has various consequences to the rest of the code. In particular: The vmm crate had to be reworked to add a reference to the guest memory in various places as it is not part of the Queue struct anymore. For this reason, it is preferable to wrap the array of Arc<GuestRegionMmap> with an Arc reference to optimize the various clone() operation that will be added. This has the advantage of reducing the duplication of memory and makes sure the various parts of the code to see the same array of elements (which would not be the case if we clone the array of Arc<> iteself). While at it, the reference to the guest memory was added to some key virtio functions. devices::virtio::{self, block, net} had all to be updated to also accept the Arc reference to the guest memory. Signed-off-by: Alvise Rigo <a.rigo@virtualopensystems.com> Reviewed-by: Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
1 parent d7c8c88 commit 9cfe242

File tree

14 files changed

+224
-178
lines changed

14 files changed

+224
-178
lines changed

Cargo.lock

Lines changed: 41 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/arch/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ edition = "2018"
88

99
[dependencies]
1010
vm-fdt = "0.2.0"
11-
vm-memory = "0.7.0"
11+
vm-memory = "0.13.1"

src/devices/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ event-manager = { version = "0.3.0", features = ["remote_endpoint"] }
1010
kvm-ioctls = "0.13.0"
1111
libc = "0.2.76"
1212
linux-loader = "0.4.0"
13-
log = "0.4.6"
14-
vm-memory = "0.7.0"
13+
log = "*"
14+
vm-memory = "0.13.1"
1515
vm-superio = "0.5.0"
1616
vmm-sys-util = "0.11.2"
1717
vm-device = "0.1.0"
@@ -23,5 +23,5 @@ virtio-queue = { git = "https://github.com/rust-vmm/vm-virtio.git"}
2323
utils = { path = "../utils" }
2424

2525
[dev-dependencies]
26-
vm-memory = { version = "0.7.0", features = ["backend-mmap"] }
26+
vm-memory = { version = "0.13.1", features = ["backend-mmap"] }
2727
kvm-bindings = "0.6.0"

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::sync::{Arc, Mutex};
99

1010
use virtio_blk::stdio_executor::StdIoBackend;
1111
use virtio_device::{VirtioConfig, VirtioDeviceActions, VirtioDeviceType, VirtioMmioDevice};
12-
use virtio_queue::Queue;
12+
use virtio_queue::{Queue, QueueT};
1313
use vm_device::bus::MmioAddress;
1414
use vm_device::device_manager::MmioManager;
1515
use vm_device::{DeviceMmio, MutDeviceMmio};
16-
use vm_memory::GuestAddressSpace;
16+
use vm_memory::{GuestAddressSpace, GuestMemoryMmap};
1717

1818
use crate::virtio::block::{BLOCK_DEVICE_ID, VIRTIO_BLK_F_RO};
1919
use crate::virtio::{CommonConfig, Env, SingleFdSignalQueue, QUEUE_MAX_SIZE};
@@ -25,31 +25,34 @@ use super::{build_config_space, BlockArgs, Error, Result};
2525
// This Block device can only use the MMIO transport for now, but we plan to reuse large parts of
2626
// the functionality when we implement virtio PCI as well, for example by having a base generic
2727
// type, and then separate concrete instantiations for `MmioConfig` and `PciConfig`.
28-
pub struct Block<M: GuestAddressSpace> {
28+
pub struct Block<M: GuestAddressSpace + Clone + Send + 'static> {
2929
cfg: CommonConfig<M>,
3030
file_path: PathBuf,
3131
read_only: bool,
3232
// We'll prob need to remember this for state save/restore unless we pass the info from
3333
// the outside.
3434
_root_device: bool,
35+
mem: Arc<GuestMemoryMmap>,
3536
}
3637

37-
impl<M> Block<M>
38-
where
39-
M: GuestAddressSpace + Clone + Send + 'static,
40-
{
38+
impl<M: GuestAddressSpace + Clone + Send + Sync + 'static> Block<M> {
4139
// Helper method that only creates a `Block` object.
42-
fn create_block<B>(env: &mut Env<M, B>, args: &BlockArgs) -> Result<Self> {
40+
fn create_block<B>(
41+
mem: Arc<GuestMemoryMmap>,
42+
env: &mut Env<M, B>,
43+
args: &BlockArgs,
44+
) -> Result<Self> {
4345
let device_features = args.device_features();
4446

4547
// A block device has a single queue.
46-
let queues = vec![Queue::new(env.mem.clone(), QUEUE_MAX_SIZE)];
48+
let queues = vec![Queue::new(QUEUE_MAX_SIZE).unwrap()];
4749
let config_space = build_config_space(&args.file_path)?;
4850
let virtio_cfg = VirtioConfig::new(device_features, queues, config_space);
4951

5052
let common_cfg = CommonConfig::new(virtio_cfg, env).map_err(Error::Virtio)?;
5153

5254
Ok(Block {
55+
mem,
5356
cfg: common_cfg,
5457
file_path: args.file_path.clone(),
5558
read_only: args.read_only,
@@ -59,14 +62,18 @@ where
5962

6063
// Create `Block` object, register it on the MMIO bus, and add any extra required info to
6164
// the kernel cmdline from the environment.
62-
pub fn new<B>(env: &mut Env<M, B>, args: &BlockArgs) -> Result<Arc<Mutex<Self>>>
65+
pub fn new<B>(
66+
mem: Arc<GuestMemoryMmap>,
67+
env: &mut Env<M, B>,
68+
args: &BlockArgs,
69+
) -> Result<Arc<Mutex<Self>>>
6370
where
6471
// We're using this (more convoluted) bound so we can pass both references and smart
6572
// pointers such as mutex guards here.
6673
B: DerefMut,
6774
B::Target: MmioManager<D = Arc<dyn DeviceMmio + Send + Sync>>,
6875
{
69-
let block = Arc::new(Mutex::new(Self::create_block(env, args)?));
76+
let block = Arc::new(Mutex::new(Self::create_block(mem, env, args)?));
7077

7178
// Register the device on the MMIO bus.
7279
env.register_mmio_device(block.clone())
@@ -79,14 +86,14 @@ where
7986
}
8087
}
8188

82-
impl<M: GuestAddressSpace + Clone + Send + 'static> Borrow<VirtioConfig<M>> for Block<M> {
83-
fn borrow(&self) -> &VirtioConfig<M> {
89+
impl<M: GuestAddressSpace + Clone + Send + 'static> Borrow<VirtioConfig<Queue>> for Block<M> {
90+
fn borrow(&self) -> &VirtioConfig<Queue> {
8491
&self.cfg.virtio
8592
}
8693
}
8794

88-
impl<M: GuestAddressSpace + Clone + Send + 'static> BorrowMut<VirtioConfig<M>> for Block<M> {
89-
fn borrow_mut(&mut self) -> &mut VirtioConfig<M> {
95+
impl<M: GuestAddressSpace + Clone + Send + 'static> BorrowMut<VirtioConfig<Queue>> for Block<M> {
96+
fn borrow_mut(&mut self) -> &mut VirtioConfig<Queue> {
9097
&mut self.cfg.virtio
9198
}
9299
}
@@ -131,6 +138,7 @@ impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioDeviceActions for Bloc
131138
};
132139

133140
let handler = Arc::new(Mutex::new(QueueHandler {
141+
mem: self.mem.clone(),
134142
inner,
135143
ioeventfd: ioevents.remove(0),
136144
}));
@@ -144,7 +152,7 @@ impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioDeviceActions for Bloc
144152
}
145153
}
146154

147-
impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioMmioDevice<M> for Block<M> {}
155+
impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioMmioDevice for Block<M> {}
148156

149157
impl<M: GuestAddressSpace + Clone + Send + 'static> MutDeviceMmio for Block<M> {
150158
fn mmio_read(&mut self, _base: MmioAddress, offset: u64, data: &mut [u8]) {
@@ -177,7 +185,7 @@ mod tests {
177185
advertise_flush: true,
178186
};
179187

180-
let block_mutex = Block::new(&mut env, &args).unwrap();
188+
let block_mutex = Block::new(env.mem.clone(), &mut env, &args).unwrap();
181189
let block = block_mutex.lock().unwrap();
182190

183191
assert_eq!(block.device_type(), BLOCK_DEVICE_ID);

0 commit comments

Comments
 (0)