vm_memory: Remove AsRef impl for SharedMemory

With multiple regions potentially backing a single `GuestMemory`
instance, `AsRef` doesn't make sense any more. Switch the one user to
`shm_region` which returns the region for a given address.

The `AsRef` implementation was accidentally left in the change to allow
multiple regions in guest memory.

Change-Id: I1246de004315a44f1f9d58995d837f3fbecb5d6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2808745
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
This commit is contained in:
Dylan Reid 2021-04-06 19:02:10 -07:00 committed by Commit Bot
parent 4b47aa7105
commit 8a95e60799
2 changed files with 19 additions and 8 deletions

View file

@ -3,7 +3,6 @@
// found in the LICENSE file.
use std::collections::VecDeque;
use std::convert::AsRef;
use std::convert::TryInto;
use std::fmt::{self, Display};
use std::sync::atomic::{AtomicBool, Ordering};
@ -131,6 +130,8 @@ type GuestMemoryResult<T> = std::result::Result<T, GuestMemoryError>;
enum AudioError {
// Failed to create a new stream.
CreateStream(BoxError),
// Failure to get regions from guest memory.
GuestRegion(GuestMemoryError),
// Invalid buffer offset received from the audio server.
InvalidBufferOffset,
// Guest did not provide a buffer when needed.
@ -151,6 +152,7 @@ impl Display for AudioError {
match self {
CreateStream(e) => write!(f, "Failed to create audio stream: {}.", e),
GuestRegion(e) => write!(f, "Failed to get guest memory region: {}.", e),
InvalidBufferOffset => write!(f, "Offset > max usize"),
NoBufferAvailable => write!(f, "No buffer was available from the Guest"),
ReadingGuestError(e) => write!(f, "Failed to read guest memory: {}.", e),
@ -589,7 +591,12 @@ impl Ac97BusMaster {
sample_rate,
buffer_frames,
&Self::stream_effects(func),
self.mem.as_ref().inner(),
self.mem
.offset_region(starting_offsets[0])
.map_err(|e| {
AudioError::GuestRegion(GuestMemoryError::ReadingGuestBufferAddress(e))
})?
.inner(),
starting_offsets,
)
.map_err(AudioError::CreateStream)?;

View file

@ -26,6 +26,7 @@ use data_model::DataInit;
pub enum Error {
DescriptorChainOverflow,
InvalidGuestAddress(GuestAddress),
InvalidOffset(u64),
MemoryAccess(GuestAddress, MmapError),
MemoryMappingFailed(MmapError),
MemoryRegionOverlap,
@ -52,6 +53,7 @@ impl Display for Error {
"the combined length of all the buffers in a DescriptorChain is too large"
),
InvalidGuestAddress(addr) => write!(f, "invalid guest address {}", addr),
InvalidOffset(addr) => write!(f, "invalid offset {}", addr),
MemoryAccess(addr, e) => {
write!(f, "invalid guest memory access at addr={}: {}", addr, e)
}
@ -121,12 +123,6 @@ impl AsRawDescriptors for GuestMemory {
}
}
impl AsRef<SharedMemory> for GuestMemory {
fn as_ref(&self) -> &SharedMemory {
&self.regions[0].shm
}
}
impl GuestMemory {
/// Creates backing shm for GuestMemory regions
fn create_shm(ranges: &[(GuestAddress, u64)]) -> Result<SharedMemory> {
@ -631,6 +627,14 @@ impl GuestMemory {
.map(|region| region.shm.as_ref())
}
/// Returns the region that contains the memory at `offset` from the base of guest memory.
pub fn offset_region(&self, offset: u64) -> Result<&SharedMemory> {
self.shm_region(
self.checked_offset(self.regions[0].guest_base, offset)
.ok_or(Error::InvalidOffset(offset))?,
)
}
/// Loops over all guest memory regions of `self`, and performs the callback function `F` in
/// the target region that contains `guest_addr`. The callback function `F` takes in:
///