hypervisor: Upstream traits changes for Vm

Add to the `trait Vm` 2 new methods that allow more control for
hypervisors when dealing with memory (un)mapping. This is intended for
dealing with expanding and releasing memory from `virtio-balloon`.

For windows based hypervisors, balloon expansion triggers OS interaction
with reclaiming memory pages, which is automatically managed in KVM.
Similarly, a balloon deflation will cause OS interaction for releasing
memory back to the OS.

Update the error return for `remove_mapping` when `madvise` call fails.

BUG=b:213150327
TEST=compiled.

Change-Id: I6ac04d27ac06ea97825f4d22e36543b5dde5b032
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3640424
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Vaibhav Nagarnaik <vnagarnaik@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Vaibhav Nagarnaik 2022-05-11 00:06:01 +00:00 committed by Chromeos LUCI
parent 1b27aef288
commit bb30f23ce0
3 changed files with 33 additions and 1 deletions

View file

@ -696,7 +696,7 @@ impl MemoryMapping {
)
};
if ret < 0 {
Err(Error::InvalidRange(mem_offset, count, self.size()))
Err(Error::SystemCallFailed(super::Error::last()))
} else {
Ok(())
}

View file

@ -648,6 +648,19 @@ impl Vm for KvmVm {
Err(_) => Err(Error::new(EIO)),
}
}
fn handle_deflate(&mut self, guest_address: GuestAddress, size: u64) -> Result<()> {
match self.guest_mem.remove_range(guest_address, size) {
Ok(_) => Ok(()),
Err(vm_memory::Error::MemoryAccess(_, MmapError::SystemCallFailed(e))) => Err(e),
Err(_) => Err(Error::new(EIO)),
}
}
fn handle_inflate(&mut self, _guest_address: GuestAddress, _size: u64) -> Result<()> {
// No-op, when the guest attempts to access the pages again, Linux/KVM will provide them.
Ok(())
}
}
impl AsRawDescriptor for KvmVm {

View file

@ -159,6 +159,25 @@ pub trait Vm: Send {
/// Remove `size`-byte mapping starting at `offset`.
fn remove_mapping(&mut self, slot: u32, offset: usize, size: usize) -> Result<()>;
/// Frees the given segment of guest memory to be reclaimed by the host OS.
/// This is intended for use with virtio-balloon, where a guest driver determines
/// unused ranges and requests they be freed. Use without the guest's knowledge is sure
/// to break something. As per virtio-balloon spec, the given address and size
/// are intended to be page-aligned.
///
/// # Arguments
/// * `guest_address` - Address in the guest's "physical" memory to begin the unmapping
/// * `size` - The size of the region to unmap, in bytes
fn handle_deflate(&mut self, guest_address: GuestAddress, size: u64) -> Result<()>;
/// Reallocates memory and maps it to provide to the guest. This is intended to be used
/// exclusively in tandem with `handle_deflate`, and will return an `Err` Result otherwise.
///
/// # Arguments
/// * `guest_address` - Address in the guest's "physical" memory to begin the mapping
/// * `size` - The size of the region to map, in bytes
fn handle_inflate(&mut self, guest_address: GuestAddress, size: u64) -> Result<()>;
}
/// A unique fingerprint for a particular `VcpuRunHandle`, used in `Vcpu` impls to ensure the