Skip to content

Commit cf6b0f0

Browse files
committed
refactor: Change GuestMemoryExtension::with_file into memfd_backed
The function was only ever called with a `File` object created from a `memfd`. Thus instead of the call-site have to do two function calls (one for the memfd and then another for creating the guest memory mmap, which because of this had to do a `fstat` call to determine how big the memfd is), have the `GuestMemoryMmap` factory directly create the `memfd`. This is mainly preparatory to prevent callers to pass mismatched configurations to `memfd_create` and the subsequent `from_file` call (e.g. different huge page configs). Signed-off-by: Patrick Roy <[email protected]>
1 parent 8945939 commit cf6b0f0

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/vmm/src/builder.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ use crate::vmm_config::boot_source::BootConfig;
5757
use crate::vmm_config::drive::BlockDeviceType;
5858
use crate::vmm_config::instance_info::InstanceInfo;
5959
use crate::vmm_config::machine_config::{MachineConfigUpdate, VmConfig, VmConfigError};
60-
use crate::vstate::memory::{
61-
create_memfd, GuestAddress, GuestMemory, GuestMemoryExtension, GuestMemoryMmap,
62-
};
60+
use crate::vstate::memory::{GuestAddress, GuestMemory, GuestMemoryExtension, GuestMemoryMmap};
6361
use crate::vstate::vcpu::{Vcpu, VcpuConfig, VcpuError};
6462
use crate::vstate::vm::Vm;
6563
use crate::{device_manager, EventManager, Vmm, VmmError};
@@ -239,10 +237,9 @@ pub fn build_microvm_for_boot(
239237
.ok_or(MissingKernelConfig)?;
240238

241239
let track_dirty_pages = vm_resources.track_dirty_pages();
242-
let memfd = create_memfd(vm_resources.vm_config.mem_size_mib)
243-
.map_err(StartMicrovmError::GuestMemory)?;
244-
let guest_memory = GuestMemoryMmap::with_file(memfd.as_file(), track_dirty_pages)
245-
.map_err(StartMicrovmError::GuestMemory)?;
240+
let guest_memory =
241+
GuestMemoryMmap::memfd_backed(vm_resources.vm_config.mem_size_mib, track_dirty_pages)
242+
.map_err(StartMicrovmError::GuestMemory)?;
246243
let entry_addr = load_kernel(boot_config, &guest_memory)?;
247244
let initrd = load_initrd_from_config(boot_config, &guest_memory)?;
248245
// Clone the command-line so that a failed boot doesn't pollute the original.

src/vmm/src/vstate/memory.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub trait GuestMemoryExtension
5656
where
5757
Self: Sized,
5858
{
59-
/// Creates a GuestMemoryMmap with `size` in MiB and guard pages backed by file.
60-
fn with_file(file: &File, track_dirty_pages: bool) -> Result<Self, MemoryError>;
59+
/// Creates a GuestMemoryMmap with `size` in MiB backed by a memfd.
60+
fn memfd_backed(mem_size_mib: usize, track_dirty_pages: bool) -> Result<Self, MemoryError>;
6161

6262
/// Creates a GuestMemoryMmap with `size` in MiB and guard pages.
6363
fn with_size(size: usize, track_dirty_pages: bool) -> Result<Self, MemoryError>;
@@ -121,16 +121,15 @@ pub struct GuestMemoryState {
121121
}
122122

123123
impl GuestMemoryExtension for GuestMemoryMmap {
124-
/// Creates a GuestMemoryMmap with `size` in MiB and guard pages backed by file.
125-
fn with_file(file: &File, track_dirty_pages: bool) -> Result<Self, MemoryError> {
126-
let metadata = file.metadata().map_err(MemoryError::FileError)?;
127-
let mem_size = u64_to_usize(metadata.len());
124+
/// Creates a GuestMemoryMmap with `size` in MiB backed by a memfd.
125+
fn memfd_backed(mem_size_mib: usize, track_dirty_pages: bool) -> Result<Self, MemoryError> {
126+
let memfd_file = create_memfd(mem_size_mib)?.into_file();
128127

129128
let mut offset: u64 = 0;
130-
let regions = crate::arch::arch_memory_regions(mem_size)
129+
let regions = crate::arch::arch_memory_regions(mem_size_mib << 20)
131130
.iter()
132131
.map(|(guest_address, region_size)| {
133-
let file_clone = file.try_clone().map_err(MemoryError::FileError)?;
132+
let file_clone = memfd_file.try_clone().map_err(MemoryError::FileError)?;
134133
let file_offset = FileOffset::new(file_clone, offset);
135134
offset += *region_size as u64;
136135
Ok((file_offset, *guest_address, *region_size))
@@ -337,7 +336,7 @@ impl GuestMemoryExtension for GuestMemoryMmap {
337336
}
338337

339338
/// Creates a memfd file with the `size` in MiB.
340-
pub fn create_memfd(size: usize) -> Result<memfd::Memfd, MemoryError> {
339+
fn create_memfd(size: usize) -> Result<memfd::Memfd, MemoryError> {
341340
let mem_size = size << 20;
342341
// Create a memfd.
343342
let opts = memfd::MemfdOptions::default().allow_sealing(true);

0 commit comments

Comments
 (0)