Using Kernel Samepage Merging with KVM

For the original and writeup of the article, do look at Using KSM (Kernel Samepage Merging) with KVM. There is a correponding pdf article Increasing Virtual Machine Density with KSM (pdf) by QUMRANET

In short, from the article

Kernel SamePage Merging is a recent linux kernel feature which combines identical memory pages from multiple processes into one copy on write memory region. Because kvm guest virtual machines run as processes
under linux, this feature provides the memory overcommit feature to kvm so important to hypervisors for more efficient use of memory……

Pointer 1. Verifying Kernel KSM Support

# grep KSM /boot/config -'uname -r'

You should see something like this if KSM is enabled


You should also see a directory for KSM in

Pix taken from Linux-KVM
Pointer 2: By default, KSM is limited to 2000 kernel pages.

To verify, type the following command

# cat /sys/kernel/mm/ksm/max_kernel_pages
You should see

Pointer 3: Verifying KVM Support for Samepage Merging

From the article…..
In order for your KVM guests to take advantage of KSM, your version of qemu-kvm must explicitly request from the kernel that identical pages be merged using the new madvise interface. The patch for this feature was added to the kvm development tree just recently following the kvm-88 release. If you’re compiling kvm yourself you can verify whether your version of kvm will support KSM by inspecting exec.c source file for the following lines of code

If you don’t see these lines in your exec.c file then your kvm process will still run fine but but it won’t take advantage of KSM.

        madvise(new_block->host, size, MADV_MERGEABLE);

Pointer 4 – Run multiple simiar guests

…….With multiple virtual machines running, you can verify that KSM is working by inspecting the following file to see how many pages are being shared between your kvm guests.

If the value is greateer than zero, KSM is used

# cat /sys/kernel/mm/KSM/pages_sharing

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s