481 lines · plain
1=============2HugeTLB Pages3=============4 5Overview6========7 8The intent of this file is to give a brief summary of hugetlbpage support in9the Linux kernel. This support is built on top of multiple page size support10that is provided by most modern architectures. For example, x86 CPUs normally11support 4K and 2M (1G if architecturally supported) page sizes, ia6412architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M,13256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical14translations. Typically this is a very scarce resource on processor.15Operating systems try to make best use of limited number of TLB resources.16This optimization is more critical now as bigger and bigger physical memories17(several GBs) are more readily available.18 19Users can use the huge page support in Linux kernel by either using the mmap20system call or standard SYSV shared memory system calls (shmget, shmat).21 22First the Linux kernel needs to be built with the CONFIG_HUGETLBFS23(present under "File systems") and CONFIG_HUGETLB_PAGE (selected24automatically when CONFIG_HUGETLBFS is selected) configuration25options.26 27The ``/proc/meminfo`` file provides information about the total number of28persistent hugetlb pages in the kernel's huge page pool. It also displays29default huge page size and information about the number of free, reserved30and surplus huge pages in the pool of huge pages of default size.31The huge page size is needed for generating the proper alignment and32size of the arguments to system calls that map huge page regions.33 34The output of ``cat /proc/meminfo`` will include lines like::35 36 HugePages_Total: uuu37 HugePages_Free: vvv38 HugePages_Rsvd: www39 HugePages_Surp: xxx40 Hugepagesize: yyy kB41 Hugetlb: zzz kB42 43where:44 45HugePages_Total46 is the size of the pool of huge pages.47HugePages_Free48 is the number of huge pages in the pool that are not yet49 allocated.50HugePages_Rsvd51 is short for "reserved," and is the number of huge pages for52 which a commitment to allocate from the pool has been made,53 but no allocation has yet been made. Reserved huge pages54 guarantee that an application will be able to allocate a55 huge page from the pool of huge pages at fault time.56HugePages_Surp57 is short for "surplus," and is the number of huge pages in58 the pool above the value in ``/proc/sys/vm/nr_hugepages``. The59 maximum number of surplus huge pages is controlled by60 ``/proc/sys/vm/nr_overcommit_hugepages``.61 Note: When the feature of freeing unused vmemmap pages associated62 with each hugetlb page is enabled, the number of surplus huge pages63 may be temporarily larger than the maximum number of surplus huge64 pages when the system is under memory pressure.65Hugepagesize66 is the default hugepage size (in kB).67Hugetlb68 is the total amount of memory (in kB), consumed by huge69 pages of all sizes.70 If huge pages of different sizes are in use, this number71 will exceed HugePages_Total \* Hugepagesize. To get more72 detailed information, please, refer to73 ``/sys/kernel/mm/hugepages`` (described below).74 75 76``/proc/filesystems`` should also show a filesystem of type "hugetlbfs"77configured in the kernel.78 79``/proc/sys/vm/nr_hugepages`` indicates the current number of "persistent" huge80pages in the kernel's huge page pool. "Persistent" huge pages will be81returned to the huge page pool when freed by a task. A user with root82privileges can dynamically allocate more or free some persistent huge pages83by increasing or decreasing the value of ``nr_hugepages``.84 85Note: When the feature of freeing unused vmemmap pages associated with each86hugetlb page is enabled, we can fail to free the huge pages triggered by87the user when the system is under memory pressure. Please try again later.88 89Pages that are used as huge pages are reserved inside the kernel and cannot90be used for other purposes. Huge pages cannot be swapped out under91memory pressure.92 93Once a number of huge pages have been pre-allocated to the kernel huge page94pool, a user with appropriate privilege can use either the mmap system call95or shared memory system calls to use the huge pages. See the discussion of96:ref:`Using Huge Pages <using_huge_pages>`, below.97 98The administrator can allocate persistent huge pages on the kernel boot99command line by specifying the "hugepages=N" parameter, where 'N' = the100number of huge pages requested. This is the most reliable method of101allocating huge pages as memory has not yet become fragmented.102 103Some platforms support multiple huge page sizes. To allocate huge pages104of a specific size, one must precede the huge pages boot command parameters105with a huge page size selection parameter "hugepagesz=<size>". <size> must106be specified in bytes with optional scale suffix [kKmMgG]. The default huge107page size may be selected with the "default_hugepagesz=<size>" boot parameter.108 109Hugetlb boot command line parameter semantics110 111hugepagesz112 Specify a huge page size. Used in conjunction with hugepages113 parameter to preallocate a number of huge pages of the specified114 size. Hence, hugepagesz and hugepages are typically specified in115 pairs such as::116 117 hugepagesz=2M hugepages=512118 119 hugepagesz can only be specified once on the command line for a120 specific huge page size. Valid huge page sizes are architecture121 dependent.122hugepages123 Specify the number of huge pages to preallocate. This typically124 follows a valid hugepagesz or default_hugepagesz parameter. However,125 if hugepages is the first or only hugetlb command line parameter it126 implicitly specifies the number of huge pages of default size to127 allocate. If the number of huge pages of default size is implicitly128 specified, it can not be overwritten by a hugepagesz,hugepages129 parameter pair for the default size. This parameter also has a130 node format. The node format specifies the number of huge pages131 to allocate on specific nodes.132 133 For example, on an architecture with 2M default huge page size::134 135 hugepages=256 hugepagesz=2M hugepages=512136 137 will result in 256 2M huge pages being allocated and a warning message138 indicating that the hugepages=512 parameter is ignored. If a hugepages139 parameter is preceded by an invalid hugepagesz parameter, it will140 be ignored.141 142 Node format example::143 144 hugepagesz=2M hugepages=0:1,1:2145 146 It will allocate 1 2M hugepage on node0 and 2 2M hugepages on node1.147 If the node number is invalid, the parameter will be ignored.148 149default_hugepagesz150 Specify the default huge page size. This parameter can151 only be specified once on the command line. default_hugepagesz can152 optionally be followed by the hugepages parameter to preallocate a153 specific number of huge pages of default size. The number of default154 sized huge pages to preallocate can also be implicitly specified as155 mentioned in the hugepages section above. Therefore, on an156 architecture with 2M default huge page size::157 158 hugepages=256159 default_hugepagesz=2M hugepages=256160 hugepages=256 default_hugepagesz=2M161 162 will all result in 256 2M huge pages being allocated. Valid default163 huge page size is architecture dependent.164hugetlb_free_vmemmap165 When CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP is set, this enables HugeTLB166 Vmemmap Optimization (HVO).167 168When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages``169indicates the current number of pre-allocated huge pages of the default size.170Thus, one can use the following command to dynamically allocate/deallocate171default sized persistent huge pages::172 173 echo 20 > /proc/sys/vm/nr_hugepages174 175This command will try to adjust the number of default sized huge pages in the176huge page pool to 20, allocating or freeing huge pages, as required.177 178On a NUMA platform, the kernel will attempt to distribute the huge page pool179over all the set of allowed nodes specified by the NUMA memory policy of the180task that modifies ``nr_hugepages``. The default for the allowed nodes--when the181task has default memory policy--is all on-line nodes with memory. Allowed182nodes with insufficient available, contiguous memory for a huge page will be183silently skipped when allocating persistent huge pages. See the184:ref:`discussion below <mem_policy_and_hp_alloc>`185of the interaction of task memory policy, cpusets and per node attributes186with the allocation and freeing of persistent huge pages.187 188The success or failure of huge page allocation depends on the amount of189physically contiguous memory that is present in system at the time of the190allocation attempt. If the kernel is unable to allocate huge pages from191some nodes in a NUMA system, it will attempt to make up the difference by192allocating extra pages on other nodes with sufficient available contiguous193memory, if any.194 195System administrators may want to put this command in one of the local rc196init files. This will enable the kernel to allocate huge pages early in197the boot process when the possibility of getting physical contiguous pages198is still very high. Administrators can verify the number of huge pages199actually allocated by checking the sysctl or meminfo. To check the per node200distribution of huge pages in a NUMA system, use::201 202 cat /sys/devices/system/node/node*/meminfo | fgrep Huge203 204``/proc/sys/vm/nr_overcommit_hugepages`` specifies how large the pool of205huge pages can grow, if more huge pages than ``/proc/sys/vm/nr_hugepages`` are206requested by applications. Writing any non-zero value into this file207indicates that the hugetlb subsystem is allowed to try to obtain that208number of "surplus" huge pages from the kernel's normal page pool, when the209persistent huge page pool is exhausted. As these surplus huge pages become210unused, they are freed back to the kernel's normal page pool.211 212When increasing the huge page pool size via ``nr_hugepages``, any existing213surplus pages will first be promoted to persistent huge pages. Then, additional214huge pages will be allocated, if necessary and if possible, to fulfill215the new persistent huge page pool size.216 217The administrator may shrink the pool of persistent huge pages for218the default huge page size by setting the ``nr_hugepages`` sysctl to a219smaller value. The kernel will attempt to balance the freeing of huge pages220across all nodes in the memory policy of the task modifying ``nr_hugepages``.221Any free huge pages on the selected nodes will be freed back to the kernel's222normal page pool.223 224Caveat: Shrinking the persistent huge page pool via ``nr_hugepages`` such that225it becomes less than the number of huge pages in use will convert the balance226of the in-use huge pages to surplus huge pages. This will occur even if227the number of surplus pages would exceed the overcommit value. As long as228this condition holds--that is, until ``nr_hugepages+nr_overcommit_hugepages`` is229increased sufficiently, or the surplus huge pages go out of use and are freed--230no more surplus huge pages will be allowed to be allocated.231 232With support for multiple huge page pools at run-time available, much of233the huge page userspace interface in ``/proc/sys/vm`` has been duplicated in234sysfs.235The ``/proc`` interfaces discussed above have been retained for backwards236compatibility. The root huge page control directory in sysfs is::237 238 /sys/kernel/mm/hugepages239 240For each huge page size supported by the running kernel, a subdirectory241will exist, of the form::242 243 hugepages-${size}kB244 245Inside each of these directories, the set of files contained in ``/proc``246will exist. In addition, two additional interfaces for demoting huge247pages may exist::248 249 demote250 demote_size251 nr_hugepages252 nr_hugepages_mempolicy253 nr_overcommit_hugepages254 free_hugepages255 resv_hugepages256 surplus_hugepages257 258The demote interfaces provide the ability to split a huge page into259smaller huge pages. For example, the x86 architecture supports both2601GB and 2MB huge pages sizes. A 1GB huge page can be split into 5122612MB huge pages. Demote interfaces are not available for the smallest262huge page size. The demote interfaces are:263 264demote_size265 is the size of demoted pages. When a page is demoted a corresponding266 number of huge pages of demote_size will be created. By default,267 demote_size is set to the next smaller huge page size. If there are268 multiple smaller huge page sizes, demote_size can be set to any of269 these smaller sizes. Only huge page sizes less than the current huge270 pages size are allowed.271 272demote273 is used to demote a number of huge pages. A user with root privileges274 can write to this file. It may not be possible to demote the275 requested number of huge pages. To determine how many pages were276 actually demoted, compare the value of nr_hugepages before and after277 writing to the demote interface. demote is a write only interface.278 279The interfaces which are the same as in ``/proc`` (all except demote and280demote_size) function as described above for the default huge page-sized case.281 282.. _mem_policy_and_hp_alloc:283 284Interaction of Task Memory Policy with Huge Page Allocation/Freeing285===================================================================286 287Whether huge pages are allocated and freed via the ``/proc`` interface or288the ``/sysfs`` interface using the ``nr_hugepages_mempolicy`` attribute, the289NUMA nodes from which huge pages are allocated or freed are controlled by the290NUMA memory policy of the task that modifies the ``nr_hugepages_mempolicy``291sysctl or attribute. When the ``nr_hugepages`` attribute is used, mempolicy292is ignored.293 294The recommended method to allocate or free huge pages to/from the kernel295huge page pool, using the ``nr_hugepages`` example above, is::296 297 numactl --interleave <node-list> echo 20 \298 >/proc/sys/vm/nr_hugepages_mempolicy299 300or, more succinctly::301 302 numactl -m <node-list> echo 20 >/proc/sys/vm/nr_hugepages_mempolicy303 304This will allocate or free ``abs(20 - nr_hugepages)`` to or from the nodes305specified in <node-list>, depending on whether number of persistent huge pages306is initially less than or greater than 20, respectively. No huge pages will be307allocated nor freed on any node not included in the specified <node-list>.308 309When adjusting the persistent hugepage count via ``nr_hugepages_mempolicy``, any310memory policy mode--bind, preferred, local or interleave--may be used. The311resulting effect on persistent huge page allocation is as follows:312 313#. Regardless of mempolicy mode [see314 Documentation/admin-guide/mm/numa_memory_policy.rst],315 persistent huge pages will be distributed across the node or nodes316 specified in the mempolicy as if "interleave" had been specified.317 However, if a node in the policy does not contain sufficient contiguous318 memory for a huge page, the allocation will not "fallback" to the nearest319 neighbor node with sufficient contiguous memory. To do this would cause320 undesirable imbalance in the distribution of the huge page pool, or321 possibly, allocation of persistent huge pages on nodes not allowed by322 the task's memory policy.323 324#. One or more nodes may be specified with the bind or interleave policy.325 If more than one node is specified with the preferred policy, only the326 lowest numeric id will be used. Local policy will select the node where327 the task is running at the time the nodes_allowed mask is constructed.328 For local policy to be deterministic, the task must be bound to a cpu or329 cpus in a single node. Otherwise, the task could be migrated to some330 other node at any time after launch and the resulting node will be331 indeterminate. Thus, local policy is not very useful for this purpose.332 Any of the other mempolicy modes may be used to specify a single node.333 334#. The nodes allowed mask will be derived from any non-default task mempolicy,335 whether this policy was set explicitly by the task itself or one of its336 ancestors, such as numactl. This means that if the task is invoked from a337 shell with non-default policy, that policy will be used. One can specify a338 node list of "all" with numactl --interleave or --membind [-m] to achieve339 interleaving over all nodes in the system or cpuset.340 341#. Any task mempolicy specified--e.g., using numactl--will be constrained by342 the resource limits of any cpuset in which the task runs. Thus, there will343 be no way for a task with non-default policy running in a cpuset with a344 subset of the system nodes to allocate huge pages outside the cpuset345 without first moving to a cpuset that contains all of the desired nodes.346 347#. Boot-time huge page allocation attempts to distribute the requested number348 of huge pages over all on-lines nodes with memory.349 350Per Node Hugepages Attributes351=============================352 353A subset of the contents of the root huge page control directory in sysfs,354described above, will be replicated under each the system device of each355NUMA node with memory in::356 357 /sys/devices/system/node/node[0-9]*/hugepages/358 359Under this directory, the subdirectory for each supported huge page size360contains the following attribute files::361 362 nr_hugepages363 free_hugepages364 surplus_hugepages365 366The free\_' and surplus\_' attribute files are read-only. They return the number367of free and surplus [overcommitted] huge pages, respectively, on the parent368node.369 370The ``nr_hugepages`` attribute returns the total number of huge pages on the371specified node. When this attribute is written, the number of persistent huge372pages on the parent node will be adjusted to the specified value, if sufficient373resources exist, regardless of the task's mempolicy or cpuset constraints.374 375Note that the number of overcommit and reserve pages remain global quantities,376as we don't know until fault time, when the faulting task's mempolicy is377applied, from which node the huge page allocation will be attempted.378 379The hugetlb may be migrated between the per-node hugepages pool in the following380scenarios: memory offline, memory failure, longterm pinning, syscalls(mbind,381migrate_pages and move_pages), alloc_contig_range() and alloc_contig_pages().382Now only memory offline, memory failure and syscalls allow fallbacking to allocate383a new hugetlb on a different node if the current node is unable to allocate during384hugetlb migration, that means these 3 cases can break the per-node hugepages pool.385 386.. _using_huge_pages:387 388Using Huge Pages389================390 391If the user applications are going to request huge pages using mmap system392call, then it is required that system administrator mount a file system of393type hugetlbfs::394 395 mount -t hugetlbfs \396 -o uid=<value>,gid=<value>,mode=<value>,pagesize=<value>,size=<value>,\397 min_size=<value>,nr_inodes=<value> none /mnt/huge398 399This command mounts a (pseudo) filesystem of type hugetlbfs on the directory400``/mnt/huge``. Any file created on ``/mnt/huge`` uses huge pages.401 402The ``uid`` and ``gid`` options sets the owner and group of the root of the403file system. By default the ``uid`` and ``gid`` of the current process404are taken.405 406The ``mode`` option sets the mode of root of file system to value & 01777.407This value is given in octal. By default the value 0755 is picked.408 409If the platform supports multiple huge page sizes, the ``pagesize`` option can410be used to specify the huge page size and associated pool. ``pagesize``411is specified in bytes. If ``pagesize`` is not specified the platform's412default huge page size and associated pool will be used.413 414The ``size`` option sets the maximum value of memory (huge pages) allowed415for that filesystem (``/mnt/huge``). The ``size`` option can be specified416in bytes, or as a percentage of the specified huge page pool (``nr_hugepages``).417The size is rounded down to HPAGE_SIZE boundary.418 419The ``min_size`` option sets the minimum value of memory (huge pages) allowed420for the filesystem. ``min_size`` can be specified in the same way as ``size``,421either bytes or a percentage of the huge page pool.422At mount time, the number of huge pages specified by ``min_size`` are reserved423for use by the filesystem.424If there are not enough free huge pages available, the mount will fail.425As huge pages are allocated to the filesystem and freed, the reserve count426is adjusted so that the sum of allocated and reserved huge pages is always427at least ``min_size``.428 429The option ``nr_inodes`` sets the maximum number of inodes that ``/mnt/huge``430can use.431 432If the ``size``, ``min_size`` or ``nr_inodes`` option is not provided on433command line then no limits are set.434 435For ``pagesize``, ``size``, ``min_size`` and ``nr_inodes`` options, you can436use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo.437For example, size=2K has the same meaning as size=2048.438 439While read system calls are supported on files that reside on hugetlb440file systems, write system calls are not.441 442Regular chown, chgrp, and chmod commands (with right permissions) could be443used to change the file attributes on hugetlbfs.444 445Also, it is important to note that no such mount command is required if446applications are going to use only shmat/shmget system calls or mmap with447MAP_HUGETLB. For an example of how to use mmap with MAP_HUGETLB see448:ref:`map_hugetlb <map_hugetlb>` below.449 450Users who wish to use hugetlb memory via shared memory segment should be451members of a supplementary group and system admin needs to configure that gid452into ``/proc/sys/vm/hugetlb_shm_group``. It is possible for same or different453applications to use any combination of mmaps and shm* calls, though the mount of454filesystem will be required for using mmap calls without MAP_HUGETLB.455 456Syscalls that operate on memory backed by hugetlb pages only have their lengths457aligned to the native page size of the processor; they will normally fail with458errno set to EINVAL or exclude hugetlb pages that extend beyond the length if459not hugepage aligned. For example, munmap(2) will fail if memory is backed by460a hugetlb page and the length is smaller than the hugepage size.461 462 463Examples464========465 466.. _map_hugetlb:467 468``map_hugetlb``469 see tools/testing/selftests/mm/map_hugetlb.c470 471``hugepage-shm``472 see tools/testing/selftests/mm/hugepage-shm.c473 474``hugepage-mmap``475 see tools/testing/selftests/mm/hugepage-mmap.c476 477The `libhugetlbfs`_ library provides a wide range of userspace tools478to help with huge page usability, environment setup, and control.479 480.. _libhugetlbfs: https://github.com/libhugetlbfs/libhugetlbfs481