11# Backing Guest Memory by Huge Pages
22
3- Firecracker supports backing the guest memory of a VM by 2MB hugetlbfs pages.
4- This can be enabled by setting the ` huge_pages ` field of ` PUT ` or ` PATCH `
5- requests to the ` /machine-config ` endpoint to ` 2M ` .
6-
7- Backing guest memory by huge pages can bring performance improvements for
8- specific workloads, due to less TLB contention and less overhead during
9- virtual->physical address resolution. It can also help reduce the number of
10- KVM_EXITS required to rebuild extended page tables post snapshot restore, as
11- well as improve boot times (by up to 50% as measured by Firecracker's
3+ Firecracker supports three modes for the ` huge_pages ` field of ` PUT ` or ` PATCH `
4+ requests to the ` /machine-config ` endpoint:
5+
6+ - ` None ` (default): Uses regular 4K pages with no huge page behavior.
7+ - ` Transparent ` : Uses ` madvise(MADV_HUGEPAGE) ` to request transparent huge pages
8+ for guest memory. Guest memory size must be a multiple of 2MB.
9+ - ` 2M ` : Backs guest memory by 2MB hugetlbfs pages.
10+
11+ ## Transparent Huge Pages (THP)
12+
13+ Setting ` huge_pages ` to ` Transparent ` enables transparent huge pages for guest
14+ memory via ` madvise(MADV_HUGEPAGE) ` . This allows the kernel to opportunistically
15+ back guest memory with huge pages without requiring a pre-allocated hugetlbfs
16+ pool.
17+
18+ Note that while traditional THP uses PMD-sized pages (2MB on x86_64), the actual
19+ THP size depends on the CPU architecture. Modern kernels also support
20+ "multi-size THP" (mTHP), which can allocate pages in various power-of-2 sizes
21+ between the base page size and PMD size (e.g. 16K, 32K, 64K). Firecracker
22+ requires guest memory size to be a multiple of 2MB regardless of the THP size
23+ used by the host kernel.
24+
25+ Limitations:
26+
27+ - When vhost-user-blk devices are in use, guest memory is memfd-backed (shared
28+ memory). THP for shared/shmem memory is controlled separately from anonymous
29+ memory via ` /sys/kernel/mm/transparent_hugepage/shmem_enabled ` and may not be
30+ enabled by default. Refer to the
31+ [ Linux Documentation on shmem THP] [ thp_shmem_docs ] for details on how to
32+ configure it.
33+ - THP does not integrate with UFFD; no transparent huge pages will be allocated
34+ during userfault-handling while resuming from a snapshot.
35+
36+ Please refer to the [ Linux Documentation] [ thp_docs ] for more information.
37+
38+ ## Hugetlbfs (2M)
39+
40+ Setting ` huge_pages ` to ` 2M ` backs guest memory by 2MB hugetlbfs pages. This can
41+ bring performance improvements for specific workloads, due to less TLB
42+ contention and less overhead during virtual->physical address resolution. It can
43+ also help reduce the number of KVM_EXITS required to rebuild extended page
44+ tables post snapshot restore, as well as improve boot times (by up to 50% as
45+ measured by Firecracker's
1246[ boot time performance tests] ( ../tests/integration_tests/performance/test_boottime.py ) )
1347
1448Using hugetlbfs requires the host running Firecracker to have a pre-allocated
@@ -19,7 +53,7 @@ not try to reserve sufficient hugetlbfs pages at the time of the `mmap` call,
1953trying to claim them from the pool on-demand. For details on how to manage this
2054pool, please refer to the [ Linux Documentation] [ hugetlbfs_docs ] .
2155
22- ## Huge Pages and Snapshotting
56+ ### Huge Pages and Snapshotting
2357
2458Restoring a Firecracker snapshot of a microVM backed by huge pages will also use
2559huge pages to back the restored guest. There is no option to flip between
@@ -43,15 +77,6 @@ the device is unable to reclaim the hugepage backing of the guest and drop RSS.
4377However, the balloon can still be inflated and used to restrict memory usage in
4478the guest.
4579
46- ## FAQ
47-
48- ### Why does Firecracker not offer a transparent huge pages (THP) setting?
49-
50- Firecracker's guest memory can be memfd based. Linux (as of 6.1) does not offer
51- a way to dynamically enable THP for such memory regions. Additionally, UFFD does
52- not integrate with THP (no transparent huge pages will be allocated during
53- userfaulting). Please refer to the [ Linux Documentation] [ thp_docs ] for more
54- information.
55-
5680[ hugetlbfs_docs ] : https://docs.kernel.org/admin-guide/mm/hugetlbpage.html
5781[ thp_docs ] : https://www.kernel.org/doc/html/next/admin-guide/mm/transhuge.html#hugepages-in-tmpfs-shmem
82+ [ thp_shmem_docs ] : https://www.kernel.org/doc/html/latest/admin-guide/mm/transhuge.html#shmem-internal-tmpfs
0 commit comments