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 <chirantan@chromium.org>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Tested-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Dylan Reid 2020-06-02 18:33:33 -07:00 committed by Commit Bot
parent ce64fc06d7
commit 82d7b9f094
5 changed files with 28 additions and 4 deletions

2
Cargo.lock generated
View file

@ -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",

View file

@ -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" }

View file

@ -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;

View file

@ -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

View file

@ -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<uring_mem::BorrowedIoVec<'s>> {
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::*;