From 271e17bd79f4bb3b101d146e246345b319aca62b Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 12 Jan 2022 10:06:46 +0000 Subject: [PATCH] vmm: Extract code for opening a file for memory This function is used to open an FD (wrapped in a File) that points to guest memory from memfd_create() or backed on the filesystem. Signed-off-by: Rob Bradford --- vmm/src/memory_manager.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index 4fb90c28c..54efa3bfa 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -1144,18 +1144,13 @@ impl MemoryManager { } } - #[allow(clippy::too_many_arguments)] - fn create_ram_region( + fn open_memory_file( backing_file: &Option, file_offset: u64, - start_addr: GuestAddress, size: usize, - prefault: bool, - shared: bool, hugepages: bool, hugepage_size: Option, - host_numa_node: Option, - ) -> Result, Error> { + ) -> Result<(File, u64), Error> { let (f, f_off) = match backing_file { Some(ref file) => { if file.is_dir() { @@ -1218,6 +1213,24 @@ impl MemoryManager { } }; + Ok((f, f_off)) + } + + #[allow(clippy::too_many_arguments)] + fn create_ram_region( + backing_file: &Option, + file_offset: u64, + start_addr: GuestAddress, + size: usize, + prefault: bool, + shared: bool, + hugepages: bool, + hugepage_size: Option, + host_numa_node: Option, + ) -> Result, Error> { + let (f, f_off) = + Self::open_memory_file(backing_file, file_offset, size, hugepages, hugepage_size)?; + let mut mmap_flags = libc::MAP_NORESERVE | if shared { libc::MAP_SHARED