Skip to content
systemcoding.dev
Performance

Reserve 1 GB Huge Pages at Boot With hugepagesz and hugepages

Reserve 1 GiB huge pages at boot on Linux with the hugepagesz=1G and hugepages= kernel parameters, check the pdpe1gb flag, and verify the pool in /proc/meminfo.

6 min read
Summarize in AI
Flat illustration of a boot chain of stages, each handing over to the next, with the Linux, C and GNU logos.

A 1 GiB page needs 1 GiB of physically contiguous, 1 GiB-aligned memory. Once a machine has been running for a while, memory is fragmented and the kernel can rarely find such a run, so reserving 1 GiB pages after boot usually fails or returns fewer than requested. The reliable way is to reserve them at boot, before fragmentation starts, using two kernel command-line parameters: hugepagesz=1G and hugepages=N. This post covers how to check that the CPU supports them, what to put on the command line, how to make it persistent with GRUB, how to verify the pool, and how a program uses it. The commands apply to Linux 6.12 on x86-64, and the concept sits in the guide to memory bandwidth, NUMA and huge pages.

Check the CPU first

On x86-64, 1 GiB pages need the pdpe1gb CPU feature. It is listed in /proc/cpuinfo flags:

Text
grep -m1 -o pdpe1gb /proc/cpuinfo

If it prints pdpe1gb, the processor can map 1 GiB pages at the page directory pointer level. No output means the CPU or the hypervisor does not expose it, and the kernel will refuse the setting. Many virtual machines hide the flag, so confirm inside the guest, not on the host. On arm64 the supported huge page sizes depend on the base page size, and /sys/kernel/mm/hugepages/ lists the sizes the kernel actually offers.

The command-line parameters

The kernel's hugetlbpage documentation defines the boot parameters. Two matter here:

  • hugepagesz= selects a huge page size for the hugepages= that follows it. Valid x86-64 values are 2M and 1G.

  • hugepages= is the number of pages of that size to reserve at boot.

Order matters: hugepagesz= must come before the hugepages= it applies to. To reserve sixteen 1 GiB pages:

Text
default_hugepagesz=1G hugepagesz=1G hugepages=16

default_hugepagesz=1G makes 1 GiB the default size for interfaces that do not name one, such as a hugetlbfs mount without a pagesize= option and MAP_HUGETLB without a size flag. Leave it out if you want 2 MiB to stay the default and ask for 1 GiB explicitly. Sixteen pages is 16 GiB, so the machine must have that memory free at boot with some margin for the system.

Make it persistent with GRUB

On a distribution that uses GRUB, add the parameters to GRUB_CMDLINE_LINUX in /etc/default/grub, regenerate the configuration, and reboot:

Text
GRUB_CMDLINE_LINUX="default_hugepagesz=1G hugepagesz=1G hugepages=16"
Text
sudo update-grub
sudo reboot

On Fedora and related systems the regeneration command is sudo grub2-mkconfig -o /boot/grub2/grub.cfg, and grubby --update-kernel=ALL --args="..." edits entries directly. Systems using systemd-boot edit the options line of the loader entry instead. After the reboot, check /proc/cmdline to confirm the kernel received the parameters.

NUMA: per-node reservation

On a NUMA system the hugepages= count at boot is split evenly across nodes. Sixteen pages on two nodes gives eight on each. Pages per node are visible and, for 2 MiB pages and for 1 GiB pages that were reserved at boot, adjustable through sysfs:

Text
cat /sys/devices/system/node/node0/hugepages/hugepages-1048576kB/nr_hugepages
cat /sys/devices/system/node/node1/hugepages/hugepages-1048576kB/nr_hugepages

A node with too little free memory at boot reserves fewer pages than its share, and the kernel does not shift the shortfall to the other node. Check both nodes. The kernel documentation also allows per-node boot reservation with the syntax hugepages=0:8,1:8, which asks for eight pages on node 0 and eight on node 1 and makes the intent explicit.

Verify the pool

/proc/meminfo summarizes the default size pool, and /sys/kernel/mm/hugepages/ holds one directory per size.

Text
grep -i huge /proc/meminfo
Text
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
FileHugePages:         0 kB
HugePages_Total:      16
HugePages_Free:       16
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:    1048576 kB
Hugetlb:        16777216 kB

HugePages_Total is the pool size, HugePages_Free is the pages not yet in use and HugePages_Rsvd is pages promised to mappings that have not faulted them in yet. Hugepagesize shows the default, 1048576 kB, which is 1 GiB. Hugetlb is the total memory held in all huge page pools, here 16 times 1 GiB, which is 16777216 kB. If HugePages_Total is lower than requested, memory was too fragmented or too scarce at boot, or the CPU lacks pdpe1gb. The kernel log, dmesg | grep -i huge, says which.

Use the pool from a program

Reserved pages are not used automatically. A program must map them, either with mmap and MAP_HUGETLB, or through a hugetlbfs mount. With 1 GiB as the default size, a plain MAP_HUGETLB suffices. When 2 MiB is the default, the size goes in the flags using MAP_HUGE_1GB.

gig.cc
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

#ifndef MAP_HUGE_SHIFT
#define MAP_HUGE_SHIFT 26
#endif
#ifndef MAP_HUGE_1GB
#define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)
#endif

int main(void) {
    size_t len = 1UL << 30;                        /* exactly one 1 GiB page */
    void *p = mmap(NULL, len, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB,
                   -1, 0);
    if (p == MAP_FAILED) {
        perror("mmap");
        return 1;
    }
    memset(p, 0, 4096);        /* touch it so the page is actually faulted in */
    printf("mapped 1 GiB at a 1 GiB aligned address: %s\n",
           ((unsigned long)p % len) == 0 ? "yes" : "no");
    munmap(p, len);
    return 0;
}

MAP_HUGE_1GB encodes the base-2 logarithm of the size, 30, in the bits above MAP_HUGE_SHIFT, which is 26, as defined in <linux/mman.h>. The length must be a multiple of the huge page size, or mmap fails with EINVAL. With the pool reserved, the program prints:

Text
mapped 1 GiB at a 1 GiB aligned address: yes

Without a reserved page it prints mmap: Cannot allocate memory. While it runs, HugePages_Free drops by one. The longer treatment of the interface is in reserving explicit huge pages with hugetlbfs and MAP_HUGETLB.

Why bother with 1 GiB pages

Each page needs a TLB entry. A 1 GiB page covers 262,144 times the memory of a 4 KiB page per entry, so a working set of tens of gigabytes that thrashes a 4 KiB page TLB can fit in a handful of entries. The processor also has fewer page-table levels to walk on a miss. The benefit shows up in programs with large, randomly accessed working sets, such as in-memory databases and virtual machine monitors, and it can be confirmed by counting dTLB-load-misses, covered in measuring TLB misses with perf stat.

When not to do this

Reserved pages are removed from general use. The 16 GiB above cannot be used by the page cache or other programs even when idle. Reserve what you will use and no more. The reservation is static, so a workload that changes size wastes memory between runs. Systems without a fixed workload are better served by Transparent Huge Pages at 2 MiB, which the kernel manages dynamically.

A note on testing the reservation safely

Because a boot parameter takes effect only after a reboot, test the change on a machine that you can reach out of band, or add the parameter to a single boot entry first by editing the GRUB command line at the menu, and make it permanent only after /proc/meminfo shows the pool you expected. A typing error in a kernel parameter rarely prevents booting, but an oversized reservation can leave too little memory for the system itself, and the kernel will then simply reserve fewer pages and log that it did so.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.