From f97938062030e209677aaf27057ce4644c492f1d Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Tue, 19 Nov 2019 15:22:20 -0800 Subject: [PATCH] vmm: Mark guest persistent memory pages as mergeable In case the VM is started with the flag "--pmem mergeable=on", it means the user expects the guest persistent memory pages to be marked as mergeable. This commit relies on the madvise(MADV_MERGEABLE) system call to inform the host kernel about these pages. Signed-off-by: Sebastien Boeuf --- vmm/src/device_manager.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 223011689..7d0ab8c94 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1078,6 +1078,31 @@ impl DeviceManager { // Safe because the guest regions are guaranteed not to overlap. let _ = unsafe { vm_info.vm_fd.set_user_memory_region(mem_region) }; + // Mark the pages as mergeable if explicitly asked for. + if pmem_cfg.mergeable { + // Safe because the address and size are valid since the + // mmap succeeded.. + let ret = unsafe { + libc::madvise( + addr as *mut libc::c_void, + size as libc::size_t, + libc::MADV_MERGEABLE, + ) + }; + if ret != 0 { + let err = io::Error::last_os_error(); + // Safe to unwrap because the error is constructed with + // last_os_error(), which ensures the output will be Some(). + let errno = err.raw_os_error().unwrap(); + if errno == libc::EINVAL { + warn!("kernel not configured with CONFIG_KSM"); + } else { + warn!("madvise error: {}", err); + } + warn!("failed to mark pages as mergeable"); + } + } + // Increment the KVM slot number *mem_slots += 1;