vmm: Move codebase to GuestMemoryAtomic from vm-memory

Relying on the latest vm-memory version, including the freshly
introduced structure GuestMemoryAtomic, this patch replaces every
occurrence of Arc<ArcSwap<GuestMemoryMmap> with
GuestMemoryAtomic<GuestMemoryMmap>.

The point is to rely on the common RCU-like implementation from
vm-memory so that we don't have to do it from Cloud-Hypervisor.

Fixes #735

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-02-11 17:22:40 +01:00 committed by Rob Bradford
parent ddf6caf955
commit 793d4e7b8d
23 changed files with 116 additions and 147 deletions

37
Cargo.lock generated
View File

@ -140,14 +140,6 @@ dependencies = [
"ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cast"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cc"
version = "1.0.50"
@ -772,14 +764,6 @@ name = "rustc-serialize"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ryu"
version = "1.0.2"
@ -790,19 +774,6 @@ name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.104"
@ -1159,9 +1130,9 @@ dependencies = [
[[package]]
name = "vm-memory"
version = "0.1.0"
source = "git+https://github.com/rust-vmm/vm-memory#f615b19469ea7faafd22930dde51595b2f3c592a"
source = "git+https://github.com/rust-vmm/vm-memory#2099f4162f978d6647ca92c26e94d76ea6139352"
dependencies = [
"cast 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1291,7 +1262,6 @@ dependencies = [
"checksum blake2b_simd 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a"
"checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
"checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb"
"checksum cast 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0"
"checksum cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
@ -1355,11 +1325,8 @@ dependencies = [
"checksum rust-argon2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017"
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
"checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
"checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
"checksum serde_json 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "9371ade75d4c2d6cb154141b9752cf3781ec9c05e0e5cf35060e1e70ee7b9c25"

View File

@ -3,7 +3,6 @@
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
//
use crate::vec_with_array_field;
use arc_swap::ArcSwap;
use byteorder::{ByteOrder, LittleEndian};
use kvm_ioctls::*;
use std::collections::HashMap;
@ -24,7 +23,10 @@ use vfio_bindings::bindings::vfio::*;
use vfio_bindings::bindings::IrqSet;
use vfio_ioctls::*;
use vm_device::{get_host_address_range, ExternalDmaMapping};
use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
use vm_memory::{
Address, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, GuestMemoryMmap,
GuestMemoryRegion,
};
use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::fam::FamStruct;
use vmm_sys_util::ioctl::*;
@ -522,11 +524,11 @@ impl VfioDeviceInfo {
/// associated with a specific VFIO container.
pub struct VfioDmaMapping {
container: Arc<VfioContainer>,
memory: Arc<ArcSwap<GuestMemoryMmap>>,
memory: GuestMemoryAtomic<GuestMemoryMmap>,
}
impl VfioDmaMapping {
pub fn new(container: Arc<VfioContainer>, memory: Arc<ArcSwap<GuestMemoryMmap>>) -> Self {
pub fn new(container: Arc<VfioContainer>, memory: GuestMemoryAtomic<GuestMemoryMmap>) -> Self {
VfioDmaMapping { container, memory }
}
}
@ -534,7 +536,7 @@ impl VfioDmaMapping {
impl ExternalDmaMapping for VfioDmaMapping {
fn map(&self, iova: u64, gpa: u64, size: u64) -> result::Result<(), io::Error> {
let user_addr = if let Some(addr) = get_host_address_range(
&self.memory.load(),
&self.memory.memory(),
GuestAddress(gpa),
size.try_into().unwrap(),
) {
@ -585,7 +587,7 @@ pub struct VfioDevice {
group: VfioGroup,
regions: Vec<VfioRegion>,
irqs: HashMap<u32, VfioIrq>,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
iommu_attached: bool,
}
@ -596,7 +598,7 @@ impl VfioDevice {
pub fn new(
sysfspath: &Path,
device_fd: Arc<DeviceFd>,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
iommu_attached: bool,
) -> Result<Self> {
let uuid_path: PathBuf = [sysfspath, Path::new("iommu_group")].iter().collect();
@ -842,7 +844,7 @@ impl VfioDevice {
/// then vfio kernel driver could access guest memory from gfn
pub fn setup_dma_map(&self) -> Result<()> {
if !self.iommu_attached {
self.mem.load().with_regions(|_index, region| {
self.mem.memory().with_regions(|_index, region| {
self.vfio_dma_map(
region.start_addr().raw_value(),
region.len() as u64,
@ -857,7 +859,7 @@ impl VfioDevice {
/// then vfio kernel driver couldn't access this guest memory
pub fn unset_dma_map(&self) -> Result<()> {
if !self.iommu_attached {
self.mem.load().with_regions(|_index, region| {
self.mem.memory().with_regions(|_index, region| {
self.vfio_dma_unmap(region.start_addr().raw_value(), region.len() as u64)
})?;
}

View File

@ -14,7 +14,6 @@ use super::{
VirtioDeviceType, VirtioInterruptType,
};
use crate::VirtioInterrupt;
use arc_swap::ArcSwap;
use epoll;
use libc::{c_void, EFD_NONBLOCK};
use std::alloc::{alloc_zeroed, dealloc, Layout};
@ -33,7 +32,10 @@ use std::sync::{Arc, Mutex};
use std::thread;
use virtio_bindings::bindings::virtio_blk::*;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError, GuestMemoryMmap};
use vm_memory::{
ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic,
GuestMemoryError, GuestMemoryMmap,
};
use vmm_sys_util::{eventfd::EventFd, seek_hole::SeekHole, write_zeroes::PunchHole};
const SECTOR_SHIFT: u8 = 9;
@ -590,7 +592,7 @@ impl Request {
struct BlockEpollHandler<T: DiskFile> {
queue: Queue,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
disk_image: Arc<Mutex<T>>,
disk_nsectors: u64,
interrupt_cb: Arc<dyn VirtioInterrupt>,
@ -605,7 +607,7 @@ impl<T: DiskFile> BlockEpollHandler<T> {
let mut used_desc_heads = Vec::new();
let mut used_count = 0;
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in queue.iter(&mem) {
let len;
match Request::parse(&avail_desc, &mem) {
@ -938,7 +940,7 @@ impl<T: 'static + DiskFile + Send> VirtioDevice for Block<T> {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
mut queues: Vec<Queue>,
mut queue_evts: Vec<EventFd>,

View File

@ -7,7 +7,6 @@ use super::{
VirtioInterruptType, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1,
};
use crate::VirtioInterrupt;
use arc_swap::ArcSwap;
use epoll;
use libc::EFD_NONBLOCK;
use std;
@ -22,7 +21,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{ByteValued, Bytes, GuestMemoryMmap};
use vm_memory::{ByteValued, Bytes, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
const QUEUE_SIZE: u16 = 256;
@ -58,7 +57,7 @@ unsafe impl ByteValued for VirtioConsoleConfig {}
struct ConsoleEpollHandler {
queues: Vec<Queue>,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
in_buffer: Arc<Mutex<VecDeque<u8>>>,
out: Arc<Mutex<Box<dyn io::Write + Send + Sync + 'static>>>,
@ -87,7 +86,7 @@ impl ConsoleEpollHandler {
return false;
}
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in recv_queue.iter(&mem) {
let len = cmp::min(avail_desc.len as u32, in_buffer.len() as u32);
let source_slice = in_buffer.drain(..len as usize).collect::<Vec<u8>>();
@ -124,7 +123,7 @@ impl ConsoleEpollHandler {
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
let mut used_count = 0;
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in trans_queue.iter(&mem) {
let len;
let mut out = self.out.lock().unwrap();
@ -456,7 +455,7 @@ impl VirtioDevice for Console {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queues: Vec<Queue>,
mut queue_evts: Vec<EventFd>,

View File

@ -7,9 +7,8 @@
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
use super::*;
use arc_swap::ArcSwap;
use std::sync::Arc;
use vm_memory::{GuestAddress, GuestMemoryMmap, GuestUsize};
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap, GuestUsize};
use vmm_sys_util::eventfd::EventFd;
pub enum VirtioInterruptType {
@ -81,7 +80,7 @@ pub trait VirtioDevice: Send {
/// Activates this device for real usage.
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_evt: Arc<dyn VirtioInterrupt>,
queues: Vec<Queue>,
queue_evts: Vec<EventFd>,

View File

@ -8,7 +8,6 @@ use super::{
VirtioDeviceType, VIRTIO_F_VERSION_1,
};
use crate::{DmaRemapping, VirtioInterrupt, VirtioInterruptType};
use arc_swap::ArcSwap;
use epoll;
use libc::EFD_NONBLOCK;
use std::cmp;
@ -23,7 +22,10 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use std::thread;
use vm_device::{ExternalDmaMapping, Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemoryError, GuestMemoryMmap};
use vm_memory::{
Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic,
GuestMemoryError, GuestMemoryMmap,
};
use vmm_sys_util::eventfd::EventFd;
/// Queues sizes
@ -588,7 +590,7 @@ impl Request {
struct IommuEpollHandler {
queues: Vec<Queue>,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queue_evts: Vec<EventFd>,
kill_evt: EventFd,
@ -602,7 +604,7 @@ impl IommuEpollHandler {
fn request_queue(&mut self) -> bool {
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
let mut used_count = 0;
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in self.queues[0].iter(&mem) {
let len = match Request::parse(
&avail_desc,
@ -941,7 +943,7 @@ impl VirtioDevice for Iommu {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queues: Vec<Queue>,
queue_evts: Vec<EventFd>,

View File

@ -15,7 +15,6 @@ use super::{
ActivateError, ActivateResult, Queue, VirtioDevice, VirtioDeviceType, VirtioInterruptType,
};
use crate::VirtioInterrupt;
use arc_swap::ArcSwap;
use epoll;
use libc::EAGAIN;
use libc::EFD_NONBLOCK;
@ -32,7 +31,7 @@ use std::thread;
use std::vec::Vec;
use virtio_bindings::bindings::virtio_net::*;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{ByteValued, GuestMemoryMmap};
use vm_memory::{ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
#[derive(Debug)]
@ -44,7 +43,7 @@ pub enum Error {
pub type Result<T> = result::Result<T, Error>;
struct NetEpollHandler {
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
tap: Tap,
rx: RxVirtio,
tx: TxVirtio,
@ -69,7 +68,7 @@ impl NetEpollHandler {
// if a buffer was used, and false if the frame must be deferred until a buffer
// is made available by the driver.
fn rx_single_frame(&mut self, mut queue: &mut Queue) -> bool {
let mem = self.mem.load();
let mem = self.mem.memory();
let next_desc = queue.iter(&mem).next();
if next_desc.is_none() {
@ -142,7 +141,7 @@ impl NetEpollHandler {
}
fn process_tx(&mut self, mut queue: &mut Queue) -> result::Result<(), DeviceError> {
let mem = self.mem.load();
let mem = self.mem.memory();
self.tx.process_desc_chain(&mem, &mut self.tap, &mut queue);
@ -432,7 +431,7 @@ impl VirtioDevice for Net {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
mut queues: Vec<Queue>,
mut queue_evts: Vec<EventFd>,

View File

@ -4,7 +4,6 @@
use super::Error as DeviceError;
use super::{DescriptorChain, DeviceEventT, Queue};
use arc_swap::ArcSwap;
use net_util::{MacAddr, Tap, TapError};
use std::cmp;
use std::fs;
@ -15,7 +14,10 @@ use std::os::unix::io::{AsRawFd, RawFd};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use virtio_bindings::bindings::virtio_net::*;
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemoryError, GuestMemoryMmap};
use vm_memory::{
ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryError,
GuestMemoryMmap,
};
use vmm_sys_util::eventfd::EventFd;
type Result<T> = std::result::Result<T, Error>;
@ -202,7 +204,7 @@ pub fn unregister_listener(
}
pub struct NetCtrlEpollHandler {
pub mem: Arc<ArcSwap<GuestMemoryMmap>>,
pub mem: GuestMemoryAtomic<GuestMemoryMmap>,
pub kill_evt: EventFd,
pub pause_evt: EventFd,
pub ctrl_q: CtrlVirtio,
@ -254,7 +256,7 @@ impl NetCtrlEpollHandler {
match ev_type {
CTRL_QUEUE_EVENT => {
let mem = self.mem.load();
let mem = self.mem.memory();
if let Err(e) = self.ctrl_q.queue_evt.read() {
error!("failed to get ctl queue event: {:?}", e);
}

View File

@ -12,7 +12,6 @@ use super::{
VirtioDeviceType, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1,
};
use crate::{VirtioInterrupt, VirtioInterruptType};
use arc_swap::ArcSwap;
use epoll;
use libc::EFD_NONBLOCK;
use std::cmp;
@ -27,7 +26,8 @@ use std::sync::Arc;
use std::thread;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{
Address, ByteValued, Bytes, GuestAddress, GuestMemoryError, GuestMemoryMmap, GuestUsize,
Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic,
GuestMemoryError, GuestMemoryMmap, GuestUsize,
};
use vmm_sys_util::eventfd::EventFd;
@ -158,7 +158,7 @@ impl Request {
struct PmemEpollHandler {
queue: Queue,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
disk: File,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queue_evt: EventFd,
@ -170,7 +170,7 @@ impl PmemEpollHandler {
fn process_queue(&mut self) -> bool {
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
let mut used_count = 0;
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in self.queue.iter(&mem) {
let len = match Request::parse(&avail_desc, &mem) {
Ok(ref req) if (req.type_ == RequestType::Flush) => {
@ -406,7 +406,7 @@ impl VirtioDevice for Pmem {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
mut queues: Vec<Queue>,
mut queue_evts: Vec<EventFd>,

View File

@ -8,7 +8,6 @@ use super::{
VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1,
};
use crate::{VirtioInterrupt, VirtioInterruptType};
use arc_swap::ArcSwap;
use epoll;
use libc::EFD_NONBLOCK;
use std;
@ -20,7 +19,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{Bytes, GuestMemoryMmap};
use vm_memory::{Bytes, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
const QUEUE_SIZE: u16 = 256;
@ -36,7 +35,7 @@ const PAUSE_EVENT: DeviceEventT = 2;
struct RngEpollHandler {
queues: Vec<Queue>,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
random_file: File,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queue_evt: EventFd,
@ -50,7 +49,7 @@ impl RngEpollHandler {
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
let mut used_count = 0;
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in queue.iter(&mem) {
let mut len = 0;
@ -257,7 +256,7 @@ impl VirtioDevice for Rng {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queues: Vec<Queue>,
mut queue_evts: Vec<EventFd>,

View File

@ -8,7 +8,6 @@ use crate::{
DEVICE_DRIVER_OK, DEVICE_FAILED, DEVICE_FEATURES_OK, DEVICE_INIT,
INTERRUPT_STATUS_CONFIG_CHANGED, INTERRUPT_STATUS_USED_RING,
};
use arc_swap::ArcSwap;
use byteorder::{ByteOrder, LittleEndian};
use devices::BusDevice;
use libc::EFD_NONBLOCK;
@ -17,7 +16,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use vm_device::interrupt::InterruptSourceGroup;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{GuestAddress, GuestMemoryMmap};
use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::{errno::Result, eventfd::EventFd};
const VENDOR_ID: u32 = 0;
@ -86,13 +85,13 @@ pub struct MmioDevice {
config_generation: u32,
queues: Vec<Queue>,
queue_evts: Vec<EventFd>,
mem: Option<Arc<ArcSwap<GuestMemoryMmap>>>,
mem: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
}
impl MmioDevice {
/// Constructs a new MMIO transport for the given virtio device.
pub fn new(
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
device: Arc<Mutex<dyn VirtioDevice>>,
) -> Result<MmioDevice> {
let device_clone = device.clone();
@ -136,7 +135,7 @@ impl MmioDevice {
fn are_queues_valid(&self) -> bool {
if let Some(mem) = self.mem.as_ref() {
self.queues.iter().all(|q| q.is_valid(mem.load().as_ref()))
self.queues.iter().all(|q| q.is_valid(&mem.memory()))
} else {
false
}

View File

@ -255,9 +255,8 @@ impl VirtioPciCommonConfig {
mod tests {
use super::*;
use crate::{ActivateResult, VirtioInterrupt};
use arc_swap::ArcSwap;
use std::sync::Arc;
use vm_memory::GuestMemoryMmap;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
struct DummyDevice(u32);
@ -273,7 +272,7 @@ mod tests {
}
fn activate(
&mut self,
_mem: Arc<ArcSwap<GuestMemoryMmap>>,
_mem: GuestMemoryAtomic<GuestMemoryMmap>,
_interrupt_evt: Arc<dyn VirtioInterrupt>,
_queues: Vec<Queue>,
_queue_evts: Vec<EventFd>,

View File

@ -20,7 +20,6 @@ use crate::{
VirtioIommuRemapping, DEVICE_ACKNOWLEDGE, DEVICE_DRIVER, DEVICE_DRIVER_OK, DEVICE_FAILED,
DEVICE_FEATURES_OK, DEVICE_INIT, VIRTIO_MSI_NO_VECTOR,
};
use arc_swap::ArcSwap;
use devices::BusDevice;
use libc::EFD_NONBLOCK;
use pci::{
@ -39,7 +38,10 @@ use vm_device::interrupt::{
InterruptIndex, InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig,
};
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{Address, ByteValued, GuestAddress, GuestMemoryMmap, GuestUsize, Le32};
use vm_memory::{
Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap,
GuestUsize, Le32,
};
use vmm_sys_util::{errno::Result, eventfd::EventFd};
#[allow(clippy::enum_variant_names)]
@ -283,7 +285,7 @@ pub struct VirtioPciDevice {
queue_evts: Vec<EventFd>,
// Guest memory
memory: Option<Arc<ArcSwap<GuestMemoryMmap>>>,
memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
// Setting PCI BAR
settings_bar: u8,
@ -303,7 +305,7 @@ pub struct VirtioPciDevice {
impl VirtioPciDevice {
/// Constructs a new PCI transport for the given virtio device.
pub fn new(
memory: Arc<ArcSwap<GuestMemoryMmap>>,
memory: GuestMemoryAtomic<GuestMemoryMmap>,
device: Arc<Mutex<dyn VirtioDevice>>,
msix_num: u16,
iommu_mapping_cb: Option<Arc<VirtioIommuRemapping>>,
@ -434,7 +436,7 @@ impl VirtioPciDevice {
fn are_queues_valid(&self) -> bool {
if let Some(mem) = self.memory.as_ref() {
self.queues.iter().all(|q| q.is_valid(mem.load().as_ref()))
self.queues.iter().all(|q| q.is_valid(&mem.memory()))
} else {
false
}

View File

@ -8,7 +8,6 @@ use super::Error as DeviceError;
use super::{Error, Result};
use crate::block::VirtioBlockConfig;
use crate::VirtioInterrupt;
use arc_swap::ArcSwap;
use libc;
use libc::EFD_NONBLOCK;
use std::cmp;
@ -26,7 +25,7 @@ use vhost_rs::vhost_user::{Master, VhostUserMaster, VhostUserMasterReqHandler};
use vhost_rs::VhostBackend;
use virtio_bindings::bindings::virtio_blk::*;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{ByteValued, GuestMemoryMmap};
use vm_memory::{ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
struct SlaveReqHandler {}
@ -219,7 +218,7 @@ impl VirtioDevice for Blk {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queues: Vec<Queue>,
queue_evts: Vec<EventFd>,
@ -257,7 +256,7 @@ impl VirtioDevice for Blk {
let mut vu_interrupt_list = setup_vhost_user(
&mut self.vhost_user_blk,
mem.load().as_ref(),
&mem.memory(),
queues,
queue_evts,
&interrupt_cb,

View File

@ -9,7 +9,6 @@ use crate::{
ActivateError, ActivateResult, Queue, VirtioDevice, VirtioDeviceType, VirtioInterrupt,
VirtioSharedMemoryList, VIRTIO_F_VERSION_1,
};
use arc_swap::ArcSwap;
use libc::{self, EFD_NONBLOCK};
use std::cmp;
use std::io;
@ -28,7 +27,7 @@ use vhost_rs::vhost_user::{
};
use vhost_rs::VhostBackend;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{ByteValued, GuestMemoryMmap};
use vm_memory::{ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
const NUM_QUEUE_OFFSET: usize = 1;
@ -329,7 +328,7 @@ impl VirtioDevice for Fs {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queues: Vec<Queue>,
queue_evts: Vec<EventFd>,
@ -376,7 +375,7 @@ impl VirtioDevice for Fs {
let vu_call_evt_queue_list = setup_vhost_user(
&mut self.vu,
mem.load().as_ref(),
&mem.memory(),
queues,
queue_evts,
&interrupt_cb,

View File

@ -11,7 +11,6 @@ use super::vu_common_ctrl::*;
use super::Error as DeviceError;
use super::{Error, Result};
use crate::VirtioInterrupt;
use arc_swap::ArcSwap;
use libc;
use libc::EFD_NONBLOCK;
use net_util::MacAddr;
@ -29,7 +28,7 @@ use vhost_rs::VhostBackend;
use virtio_bindings::bindings::virtio_net;
use virtio_bindings::bindings::virtio_ring;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{ByteValued, GuestMemoryMmap};
use vm_memory::{ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
const DEFAULT_QUEUE_NUMBER: usize = 2;
@ -222,7 +221,7 @@ impl VirtioDevice for Net {
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
mut queues: Vec<Queue>,
mut queue_evts: Vec<EventFd>,
@ -295,7 +294,7 @@ impl VirtioDevice for Net {
let mut vu_interrupt_list = setup_vhost_user(
&mut self.vhost_user_net,
mem.load().as_ref(),
&mem.memory(),
queues,
queue_evts,
&interrupt_cb,

View File

@ -34,7 +34,6 @@ use crate::{
/// - an event queue FD; and
/// - a backend FD.
///
use arc_swap::ArcSwap;
use byteorder::{ByteOrder, LittleEndian};
use epoll;
use libc::EFD_NONBLOCK;
@ -46,7 +45,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use std::thread;
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::GuestMemoryMmap;
use vm_memory::{GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
const QUEUE_SIZE: u16 = 256;
@ -86,7 +85,7 @@ pub const EVENTS_LEN: usize = 6;
/// - again, attempt to fetch any incoming packets queued by the backend into virtio RX buffers.
///
pub struct VsockEpollHandler<B: VsockBackend> {
pub mem: Arc<ArcSwap<GuestMemoryMmap>>,
pub mem: GuestMemoryAtomic<GuestMemoryMmap>,
pub queues: Vec<Queue>,
pub queue_evts: Vec<EventFd>,
pub kill_evt: EventFd,
@ -121,7 +120,7 @@ where
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
let mut used_count = 0;
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in self.queues[0].iter(&mem) {
let used_len = match VsockPacket::from_rx_virtq_head(&avail_desc) {
Ok(mut pkt) => {
@ -163,7 +162,7 @@ where
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
let mut used_count = 0;
let mem = self.mem.load();
let mem = self.mem.memory();
for avail_desc in self.queues[1].iter(&mem) {
let pkt = match VsockPacket::from_tx_virtq_head(&avail_desc) {
Ok(pkt) => pkt,
@ -481,7 +480,7 @@ where
fn activate(
&mut self,
mem: Arc<ArcSwap<GuestMemoryMmap>>,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
interrupt_cb: Arc<dyn VirtioInterrupt>,
queues: Vec<Queue>,
queue_evts: Vec<EventFd>,
@ -644,7 +643,7 @@ mod tests {
// Test a bad activation.
let bad_activate = ctx.device.activate(
Arc::new(ArcSwap::from(Arc::new(ctx.mem.clone()))),
GuestMemoryAtomic::new(ctx.mem.clone()),
Arc::new(NoopVirtioInterrupt {}),
Vec::new(),
Vec::new(),
@ -657,7 +656,7 @@ mod tests {
// Test a correct activation.
ctx.device
.activate(
Arc::new(ArcSwap::new(Arc::new(ctx.mem.clone()))),
GuestMemoryAtomic::new(ctx.mem.clone()),
Arc::new(NoopVirtioInterrupt {}),
vec![Queue::new(256), Queue::new(256), Queue::new(256)],
vec![

View File

@ -164,12 +164,11 @@ mod tests {
use crate::queue::tests::VirtQueue as GuestQ;
use crate::queue::Queue;
use crate::{VIRTQ_DESC_F_NEXT, VIRTQ_DESC_F_WRITE};
use arc_swap::ArcSwap;
use libc::EFD_NONBLOCK;
use std::os::unix::io::AsRawFd;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, RwLock};
use vm_memory::{GuestAddress, GuestMemoryMmap};
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
pub struct NoopVirtioInterrupt {}
@ -310,7 +309,7 @@ mod tests {
guest_txvq,
guest_evvq,
handler: VsockEpollHandler {
mem: Arc::new(ArcSwap::new(Arc::new(self.mem.clone()))),
mem: GuestMemoryAtomic::new(self.mem.clone()),
queues,
queue_evts,
kill_evt: EventFd::new(EFD_NONBLOCK).unwrap(),

View File

@ -45,4 +45,4 @@ features = ["elf", "bzimage"]
[dependencies.vm-memory]
git = "https://github.com/rust-vmm/vm-memory"
features = ["backend-mmap"]
features = ["backend-mmap", "backend-atomic"]

View File

@ -11,7 +11,6 @@
use crate::device_manager::DeviceManager;
#[cfg(feature = "acpi")]
use acpi_tables::{aml, aml::Aml, sdt::SDT};
use arc_swap::ArcSwap;
#[cfg(feature = "acpi")]
use arch::layout;
use devices::{ioapic, BusDevice};
@ -25,7 +24,7 @@ use std::sync::{Arc, Barrier, Mutex, Weak};
use std::thread;
use std::{fmt, io, result};
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
use vm_memory::{Address, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::signal::{register_signal_handler, SIGRTMIN};
@ -277,7 +276,7 @@ impl Vcpu {
pub fn configure(
&mut self,
kernel_start_addr: Option<GuestAddress>,
vm_memory: &Arc<ArcSwap<GuestMemoryMmap>>,
vm_memory: &GuestMemoryAtomic<GuestMemoryMmap>,
cpuid: CpuId,
) -> Result<()> {
let mut cpuid = cpuid;
@ -297,7 +296,7 @@ impl Vcpu {
)
.map_err(Error::REGSConfiguration)?;
arch::x86_64::regs::setup_fpu(&self.fd).map_err(Error::FPUConfiguration)?;
arch::x86_64::regs::setup_sregs(&vm_memory.load(), &self.fd)
arch::x86_64::regs::setup_sregs(&vm_memory.memory(), &self.fd)
.map_err(Error::SREGSConfiguration)?;
}
arch::x86_64::interrupts::set_lint(&self.fd).map_err(Error::LocalIntConfiguration)?;
@ -376,7 +375,7 @@ pub struct CpuManager {
io_bus: Weak<devices::Bus>,
mmio_bus: Arc<devices::Bus>,
ioapic: Option<Arc<Mutex<ioapic::Ioapic>>>,
vm_memory: Arc<ArcSwap<GuestMemoryMmap>>,
vm_memory: GuestMemoryAtomic<GuestMemoryMmap>,
cpuid: CpuId,
fd: Arc<VmFd>,
vcpus_kill_signalled: Arc<AtomicBool>,
@ -496,7 +495,7 @@ impl CpuManager {
boot_vcpus: u8,
max_vcpus: u8,
device_manager: &DeviceManager,
guest_memory: Arc<ArcSwap<GuestMemoryMmap>>,
guest_memory: GuestMemoryAtomic<GuestMemoryMmap>,
fd: Arc<VmFd>,
cpuid: CpuId,
reset_evt: EventFd,

View File

@ -49,6 +49,8 @@ use vm_device::interrupt::{
};
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::guest_memory::FileOffset;
#[cfg(feature = "cmos")]
use vm_memory::GuestAddressSpace;
use vm_memory::{Address, GuestAddress, GuestUsize, MmapRegion};
#[cfg(feature = "pci_support")]
use vm_virtio::transport::VirtioPciDevice;
@ -739,7 +741,7 @@ impl DeviceManager {
.lock()
.unwrap()
.guest_memory()
.load()
.memory()
.last_addr()
.0
+ 1;

View File

@ -5,7 +5,6 @@
#[cfg(feature = "acpi")]
use acpi_tables::{aml, aml::Aml};
use arc_swap::ArcSwap;
use arch::RegionType;
use devices::BusDevice;
use kvm_bindings::kvm_userspace_memory_region;
@ -19,8 +18,9 @@ use std::sync::{Arc, Mutex};
use vm_allocator::SystemAllocator;
use vm_memory::guest_memory::FileOffset;
use vm_memory::{
mmap::MmapRegionError, Address, Error as MmapError, GuestAddress, GuestMemory, GuestMemoryMmap,
GuestMemoryRegion, GuestRegionMmap, GuestUsize, MmapRegion,
mmap::MmapRegionError, Address, Error as MmapError, GuestAddress, GuestAddressSpace,
GuestMemory, GuestMemoryAtomic, GuestMemoryMmap, GuestMemoryRegion, GuestRegionMmap,
GuestUsize, MmapRegion,
};
const HOTPLUG_COUNT: usize = 8;
@ -35,12 +35,11 @@ struct HotPlugState {
}
pub struct MemoryManager {
guest_memory: Arc<ArcSwap<GuestMemoryMmap>>,
guest_memory: GuestMemoryAtomic<GuestMemoryMmap>,
next_kvm_memory_slot: u32,
start_of_device_area: GuestAddress,
end_of_device_area: GuestAddress,
fd: Arc<VmFd>,
mem_regions: Vec<Arc<GuestRegionMmap>>,
hotplug_slots: Vec<HotPlugState>,
selected_slot: usize,
backing_file: Option<PathBuf>,
@ -219,7 +218,7 @@ impl MemoryManager {
}
let guest_memory =
GuestMemoryMmap::from_arc_regions(mem_regions.clone()).map_err(Error::GuestMemory)?;
GuestMemoryMmap::from_arc_regions(mem_regions).map_err(Error::GuestMemory)?;
let end_of_device_area = GuestAddress((1 << get_host_cpu_phys_bits()) - 1);
let mem_end = guest_memory.last_addr();
@ -233,7 +232,7 @@ impl MemoryManager {
start_of_device_area = start_of_device_area.unchecked_add(size);
}
let guest_memory = Arc::new(ArcSwap::new(Arc::new(guest_memory)));
let guest_memory = GuestMemoryAtomic::new(guest_memory);
let mut hotplug_slots = Vec::with_capacity(HOTPLUG_COUNT);
hotplug_slots.resize_with(HOTPLUG_COUNT, HotPlugState::default);
@ -244,7 +243,6 @@ impl MemoryManager {
start_of_device_area,
end_of_device_area,
fd,
mem_regions,
hotplug_slots,
selected_slot: 0,
backing_file: backing_file.clone(),
@ -254,7 +252,7 @@ impl MemoryManager {
next_hotplug_slot: 0,
}));
guest_memory.load().with_regions(|_, region| {
guest_memory.memory().with_regions(|_, region| {
let _ = memory_manager.lock().unwrap().create_userspace_mapping(
region.start_addr().raw_value(),
region.len() as u64,
@ -331,7 +329,7 @@ impl MemoryManager {
// Start address needs to be non-contiguous with last memory added (leaving a gap of 256MiB)
// and also aligned to 128MiB boundary. It must also start at the 64bit start.
let mem_end = self.guest_memory.load().last_addr();
let mem_end = self.guest_memory.memory().last_addr();
let start_addr = if mem_end < arch::layout::MEM_32BIT_RESERVED_START {
arch::layout::RAM_64BIT_START
} else {
@ -371,15 +369,17 @@ impl MemoryManager {
self.next_hotplug_slot += 1;
// Update the GuestMemoryMmap with the new range
self.mem_regions.push(region);
let guest_memory = GuestMemoryMmap::from_arc_regions(self.mem_regions.clone())
let guest_memory = self
.guest_memory
.memory()
.insert_region(region)
.map_err(Error::GuestMemory)?;
self.guest_memory.store(Arc::new(guest_memory));
self.guest_memory.lock().unwrap().replace(guest_memory);
Ok(())
}
pub fn guest_memory(&self) -> Arc<ArcSwap<GuestMemoryMmap>> {
pub fn guest_memory(&self) -> GuestMemoryAtomic<GuestMemoryMmap> {
self.guest_memory.clone()
}

View File

@ -39,13 +39,15 @@ use signal_hook::{iterator::Signals, SIGINT, SIGTERM, SIGWINCH};
use std::ffi::CString;
use std::fs::File;
use std::io;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock};
use std::{result, str, thread};
use vm_allocator::{GsiApic, SystemAllocator};
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
use vm_memory::{
Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap, GuestMemoryRegion, GuestUsize,
Address, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryMmap,
GuestMemoryRegion, GuestUsize,
};
use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::terminal::Terminal;
@ -388,9 +390,9 @@ impl Vm {
let cmdline_cstring = CString::new(cmdline).map_err(Error::CmdLineCString)?;
let guest_memory = self.memory_manager.lock().as_ref().unwrap().guest_memory();
let mem = guest_memory.load_full();
let mem = guest_memory.memory();
let entry_addr = match linux_loader::loader::Elf::load(
mem.as_ref(),
mem.deref(),
None,
&mut self.kernel,
Some(arch::layout::HIGH_RAM_START),
@ -398,7 +400,7 @@ impl Vm {
Ok(entry_addr) => entry_addr,
Err(linux_loader::loader::Error::InvalidElfMagicNumber) => {
linux_loader::loader::BzImage::load(
mem.as_ref(),
mem.deref(),
None,
&mut self.kernel,
Some(arch::layout::HIGH_RAM_START),
@ -409,7 +411,7 @@ impl Vm {
};
linux_loader::loader::load_cmdline(
mem.as_ref(),
mem.deref(),
arch::layout::CMDLINE_START,
&cmdline_cstring,
)
@ -423,7 +425,7 @@ impl Vm {
#[cfg(feature = "acpi")]
{
rsdp_addr = Some(crate::acpi::create_acpi_tables(
&mem,
mem.deref(),
&self.devices,
&self.cpu_manager,
&self.memory_manager,