How to set up KVM guests to use HugePages?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 6
  • Red Hat Enterprise Linux (RHEL) 5
  • KVM Virtualization

Issue

  • How to set up KVM guests to use HugePages?

Resolution

Mount the HugeTLB filesystem on the host

You may use any mountpoint desired, here we have created /hugepages

mkdir -p /hugepages
mount -t hugetlbfs hugetlbfs /hugepages

This is also possible via an entry in /etc/fstab, for example

hugetlbfs    /hugepages    hugetlbfs    defaults    0 0
Increase the memory lock limit on the host

Alter the following values in /etc/security/limits.conf depending on your required memory usage

# Lock max 8Gb
soft memlock 8388608
hard memlock 8388608
Reserve HugePages and give the KVM group access to them

Alter to following lines in /etc/sysctl.conf depending on your required memory usage

vm.nr_hugepages = 4096
vm.hugetlb_shm_group = 36
Add HugePage backing to the KVM guest definition

Add the following to the guest config of an existing KVM guest. This can be done with virsh edit <guestname> or virsh define <guest.xml>

<memoryBacking>
  <hugepages/>
</memoryBacking>
Restart the host

This is required to re-allocate contigous memory to HugePages

Start a guest
Confirm the guest has HugePage backing

Check the qemu-kvm process associated with that guest for the presence of -mem-path in the run command

ps -ef | grep qemu

root      4182     1  1 17:35 ?        00:00:42 /usr/libexec/qemu-kvm -S -M rhel5.4.0 -m 1024 -mem-prealloc
-mem-path /hugepages/libvirt/qemu -smp 1 -name vm1 -uuid 3f1f3a98-89f8-19ac-b5b5-bf496e2ed9be -no-kvm-pit-reinjection
-monitor pty -pidfile /var/run/libvirt/qemu//vm1.pid -boot c -drive file=/vmimages/vm1,if=ide,index=0,boot=on,cache=none
-drive file=,if=ide,media=cdrom,index=2 -net nic,macaddr=54:52:00:00:00:01,vlan=0 -net tap,fd=15,script=,vlan=0,ifname=vnet0
-serial pty -parallel none -usb -vnc 127.0.0.1:0 -k en-us
Confirm HugePage use on the system

Here we can see HugePages are being allocated at startup, as well as used/reserved for the guests

cat /proc/meminfo | grep Huge

HugePages_Total:    4096
HugePages_Free:      873
HugePages_Rsvd:      761
Hugepagesize:       2048 kB

Root Cause

The default method of allocating memory for KVM guests is to use regular 4k pages. This can result in

  • large page tables which occupy unnecessary and inefficient amounts of memory
  • increased memory fragmentation which can slow down some kernel-based actions which require contigous memory (eg: disk writes, network access)
  • increasing page faults which can slow down all applications
  • risking swapping components of virtual guests out to disk which would cause a large performance hit

Using HugePages, page table sizes are dramatically reduced, contigous areas of memory are mapped, and HugePages cannot be swapped by design.

Note: These steps are not necessary with KVM on RHEL6, which uses Transparent HugePages to dynamically map contigous 2Mb areas of memory but also allows that memory to be broken up into 4k pages to be merged with KSM or swapped when the system is under memory pressure.

The above steps can be applied to RHEL6 if HugePages are desired over Transparent HugePages.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.