From 82d7b9f094cd80d7cb3931a3a108f37a11f0fc17 Mon Sep 17 00:00:00 2001 From: Dylan Reid Date: Tue, 2 Jun 2020 18:33:33 -0700 Subject: [PATCH] vm_memory: Implement cros_async::BackingMemory for GuestMemory Implementing `BackingMemory` signals that `GuestMemory` regions can be used in uring transactions where the lifetime in which the kernel can modify the memory is not well defined. Change-Id: I3541fff4c5dac226062a94483672f570e7adeb18 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2275725 Reviewed-by: Chirantan Ekbote Commit-Queue: Dylan Reid Tested-by: Dylan Reid --- Cargo.lock | 2 +- cros_async/Cargo.toml | 2 +- cros_async/src/lib.rs | 4 ++-- vm_memory/Cargo.toml | 1 + vm_memory/src/guest_memory.rs | 23 +++++++++++++++++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 06e2a8e46b..6ee3a19c02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,7 +106,6 @@ dependencies = [ "pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)", "sys_util 0.1.0", "syscall_defines 0.1.0", - "vm_memory 0.1.0", ] [[package]] @@ -809,6 +808,7 @@ dependencies = [ name = "vm_memory" version = "0.1.0" dependencies = [ + "cros_async 0.1.0", "data_model 0.1.0", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "sys_util 0.1.0", diff --git a/cros_async/Cargo.toml b/cros_async/Cargo.toml index c8760ee7d3..b7196f315d 100644 --- a/cros_async/Cargo.toml +++ b/cros_async/Cargo.toml @@ -11,7 +11,6 @@ paste = "*" pin-utils = "0.1.0-alpha.4" sys_util = { path = "../sys_util" } syscall_defines = { path = "../syscall_defines" } -vm_memory = { path = "../vm_memory" } [dependencies.futures] version = "*" @@ -20,3 +19,4 @@ features = ["alloc"] [dev-dependencies] tempfile = { path = "../tempfile" } +vm_memory = { path = "../vm_memory" } diff --git a/cros_async/src/lib.rs b/cros_async/src/lib.rs index 1a4edcd5a7..399fd5f032 100644 --- a/cros_async/src/lib.rs +++ b/cros_async/src/lib.rs @@ -68,7 +68,7 @@ mod poll_source; mod select; mod uring_executor; mod uring_futures; -mod uring_mem; +pub mod uring_mem; mod waker; pub use executor::Executor; @@ -77,7 +77,7 @@ pub use poll_or_ring::Error as AsyncError; pub use poll_or_ring::{PollOrRing, U64Source}; pub use select::SelectResult; pub use uring_futures::UringSource; -pub use uring_mem::MemRegion; +pub use uring_mem::{BackingMemory, MemRegion}; use std::fmt::{self, Display}; use std::future::Future; diff --git a/vm_memory/Cargo.toml b/vm_memory/Cargo.toml index 077eab7c47..bc7336b985 100644 --- a/vm_memory/Cargo.toml +++ b/vm_memory/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" include = ["src/**/*", "Cargo.toml"] [dependencies] +cros_async = { path = "../cros_async" } # provided by ebuild data_model = { path = "../data_model" } # provided by ebuild libc = "*" sys_util = { path = "../sys_util" } # provided by ebuild diff --git a/vm_memory/src/guest_memory.rs b/vm_memory/src/guest_memory.rs index 63275c8c03..7a96769353 100644 --- a/vm_memory/src/guest_memory.rs +++ b/vm_memory/src/guest_memory.rs @@ -13,6 +13,10 @@ use std::result; use std::sync::Arc; use crate::guest_address::GuestAddress; +use cros_async::{ + uring_mem::{self, BorrowedIoVec}, + BackingMemory, +}; use data_model::volatile_memory::*; use data_model::DataInit; use sys_util::{pagesize, Error as SysError}; @@ -665,6 +669,25 @@ impl GuestMemory { } } +// It is safe to implement BackingMemory because GuestMemory can be mutated any time already. +unsafe impl BackingMemory for GuestMemory { + fn get_iovec<'s>( + &'s self, + mem_range: cros_async::MemRegion, + ) -> uring_mem::Result> { + let vs = self + .get_slice_at_addr(GuestAddress(mem_range.offset as u64), mem_range.len) + .map_err(|_| uring_mem::Error::InvalidOffset(mem_range.offset, mem_range.len))?; + // Safe because 'vs' is valid in the backing memory as checked above. + unsafe { + Ok(BorrowedIoVec::from_raw_parts( + vs.as_mut_ptr(), + vs.size() as usize, + )) + } + } +} + #[cfg(test)] mod tests { use super::*;