From b68add2d0d44e3ee77f732af644bc59cc1995873 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 2 Nov 2022 16:02:57 +0000 Subject: [PATCH] vmm: Enable THP when using anonymous memory If the memory is not backed by a file then it is possible to enable Transparent Huge Pages on the memory and take advantage of the benefits of huge pages without requiring the specific allocation of an appropriate number of huge pages. TEST=Boot and see that in /proc/`pidof cloud-hypervisor`/smaps that the region is now THPeligible (and that also pages are being used.) Signed-off-by: Rob Bradford --- vmm/src/memory_manager.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index b3d936a31..24a9edcf5 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -1336,6 +1336,19 @@ impl MemoryManager { ) .map_err(Error::GuestMemory)?; + if region.file_offset().is_none() { + info!( + "Anonymous mapping at 0x{:x} (size = 0x{:x})", + region.as_ptr() as u64, + size + ); + let ret = unsafe { libc::madvise(region.as_ptr() as _, size, libc::MADV_HUGEPAGE) }; + if ret != 0 { + let e = io::Error::last_os_error(); + warn!("Failed to mark pages as THP eligible: {}", e); + } + } + // Apply NUMA policy if needed. if let Some(node) = host_numa_node { let addr = region.deref().as_ptr();