Gigabyte hugepages

Last modified: Sun Mar 29 12:53:57 EDT 2020

In the Linux kernel source tree, see Documentation/admin-guide/mm/hugetlbpage.rst and transhuge.rst.

If the flags in /proc/cpuinfo include pdpe1gb, your CPU supports 1 GiB pages.  Compared with the 4 KiB and 2 MiB pages that the kernel normally doles out to applications, 1 GiB pages might offer a marginal performance boost.  I find that they somehow make QEMU less crashy.

Since the kernel has no interest in using 1 GiB pages automatically, they must be allocated at boot time.  This memory is subtracted from the common pool and unfortunately becomes unavailable for other uses.

To set aside 4 × 1 GiB hugepages, boot with kernel parameters hugepagesz=1GB hugepages=4.

QEMU still cannot use these pages without a further step of mounting a pseudo-filesystem though which it can mmap them:

if [ -e /sys/kernel/mm/hugepages/hugepages-1048576kB/free_hugepages ]; then
  NRP=`cat /sys/kernel/mm/hugepages/hugepages-1048576kB/free_hugepages`
  if [ ${NRP} -gt 0 ]; then
    echo "Mounting hugetlbfs with pagesize=1G,size=${NRP}G,min_size=${NRP}G."
    if [ ! -d /hugepages ]; then
      mkdir /hugepages
    mount -t hugetlbfs \
          -o uid=1000,gid=100,pagesize=1G,size=${NRP}G,min_size=${NRP}G \
          hugetlbfs /hugepages
    echo "No free 1 GiB hugepages; not mounting hugetlbfs."
  echo "No 1 GiB hugepages; not mounting hugetlbfs."

(Beware of the access granted to unprivileged users by uid=1000,gid=100 and adjust as appropriate.)

QEMU can then be told to use those hugepages for guest memory with the options -m 4G -mem-prealloc -mem-path /hugepages/qemu.