From df7c728399783b909d3bfe51ae23cd0e7c594332 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Sun, 30 Oct 2022 10:27:56 +0000 Subject: [PATCH] vmm: memory_manager: Only file back memory when required If we do not need an anonymous file backing the memory then do not create one. As a side effect this addresses an issue with CoW (mmap with MAP_PRIVATE but no MAP_ANONYMOUS) when the memory is pinned for VFIO. Fixes: #4805 Signed-off-by: Rob Bradford --- vmm/src/memory_manager.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index 123e68a5b..d84af91f8 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -1303,8 +1303,10 @@ impl MemoryManager { Some(FileOffset::new(f, file_offset)) } else if let Some(backing_file) = backing_file { Some(Self::open_backing_file(backing_file, file_offset, size)?) - } else { + } else if shared || hugepages { Some(Self::create_anonymous_file(size, hugepages, hugepage_size)?) + } else { + None }; let mut mmap_flags = libc::MAP_NORESERVE @@ -1317,6 +1319,10 @@ impl MemoryManager { mmap_flags |= libc::MAP_POPULATE; } + if fo.is_none() { + mmap_flags |= libc::MAP_ANONYMOUS; + } + let region = GuestRegionMmap::new( MmapRegion::build(fo, size, libc::PROT_READ | libc::PROT_WRITE, mmap_flags) .map_err(Error::GuestMemoryRegion)?,