1.. _hugetlbpage: 2 3============= 4HugeTLB Pages 5============= 6 7Overview 8======== 9 10The intent of this file is to give a brief summary of hugetlbpage support in 11the Linux kernel. This support is built on top of multiple page size support 12that is provided by most modern architectures. For example, x86 CPUs normally 13support 4K and 2M (1G if architecturally supported) page sizes, ia64 14architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M, 15256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical 16translations. Typically this is a very scarce resource on processor. 17Operating systems try to make best use of limited number of TLB resources. 18This optimization is more critical now as bigger and bigger physical memories 19(several GBs) are more readily available. 20 21Users can use the huge page support in Linux kernel by either using the mmap 22system call or standard SYSV shared memory system calls (shmget, shmat). 23 24First the Linux kernel needs to be built with the CONFIG_HUGETLBFS 25(present under "File systems") and CONFIG_HUGETLB_PAGE (selected 26automatically when CONFIG_HUGETLBFS is selected) configuration 27options. 28 29The ``/proc/meminfo`` file provides information about the total number of 30persistent hugetlb pages in the kernel's huge page pool. It also displays 31default huge page size and information about the number of free, reserved 32and surplus huge pages in the pool of huge pages of default size. 33The huge page size is needed for generating the proper alignment and 34size of the arguments to system calls that map huge page regions. 35 36The output of ``cat /proc/meminfo`` will include lines like:: 37 38 HugePages_Total: uuu 39 HugePages_Free: vvv 40 HugePages_Rsvd: www 41 HugePages_Surp: xxx 42 Hugepagesize: yyy kB 43 Hugetlb: zzz kB 44 45where: 46 47HugePages_Total 48 is the size of the pool of huge pages. 49HugePages_Free 50 is the number of huge pages in the pool that are not yet 51 allocated. 52HugePages_Rsvd 53 is short for "reserved," and is the number of huge pages for 54 which a commitment to allocate from the pool has been made, 55 but no allocation has yet been made. Reserved huge pages 56 guarantee that an application will be able to allocate a 57 huge page from the pool of huge pages at fault time. 58HugePages_Surp 59 is short for "surplus," and is the number of huge pages in 60 the pool above the value in ``/proc/sys/vm/nr_hugepages``. The 61 maximum number of surplus huge pages is controlled by 62 ``/proc/sys/vm/nr_overcommit_hugepages``. 63Hugepagesize 64 is the default hugepage size (in Kb). 65Hugetlb 66 is the total amount of memory (in kB), consumed by huge 67 pages of all sizes. 68 If huge pages of different sizes are in use, this number 69 will exceed HugePages_Total \* Hugepagesize. To get more 70 detailed information, please, refer to 71 ``/sys/kernel/mm/hugepages`` (described below). 72 73 74``/proc/filesystems`` should also show a filesystem of type "hugetlbfs" 75configured in the kernel. 76 77``/proc/sys/vm/nr_hugepages`` indicates the current number of "persistent" huge 78pages in the kernel's huge page pool. "Persistent" huge pages will be 79returned to the huge page pool when freed by a task. A user with root 80privileges can dynamically allocate more or free some persistent huge pages 81by increasing or decreasing the value of ``nr_hugepages``. 82 83Pages that are used as huge pages are reserved inside the kernel and cannot 84be used for other purposes. Huge pages cannot be swapped out under 85memory pressure. 86 87Once a number of huge pages have been pre-allocated to the kernel huge page 88pool, a user with appropriate privilege can use either the mmap system call 89or shared memory system calls to use the huge pages. See the discussion of 90:ref:`Using Huge Pages <using_huge_pages>`, below. 91 92The administrator can allocate persistent huge pages on the kernel boot 93command line by specifying the "hugepages=N" parameter, where 'N' = the 94number of huge pages requested. This is the most reliable method of 95allocating huge pages as memory has not yet become fragmented. 96 97Some platforms support multiple huge page sizes. To allocate huge pages 98of a specific size, one must precede the huge pages boot command parameters 99with a huge page size selection parameter "hugepagesz=<size>". <size> must 100be specified in bytes with optional scale suffix [kKmMgG]. The default huge 101page size may be selected with the "default_hugepagesz=<size>" boot parameter. 102 103Hugetlb boot command line parameter semantics 104 105hugepagesz 106 Specify a huge page size. Used in conjunction with hugepages 107 parameter to preallocate a number of huge pages of the specified 108 size. Hence, hugepagesz and hugepages are typically specified in 109 pairs such as:: 110 111 hugepagesz=2M hugepages=512 112 113 hugepagesz can only be specified once on the command line for a 114 specific huge page size. Valid huge page sizes are architecture 115 dependent. 116hugepages 117 Specify the number of huge pages to preallocate. This typically 118 follows a valid hugepagesz or default_hugepagesz parameter. However, 119 if hugepages is the first or only hugetlb command line parameter it 120 implicitly specifies the number of huge pages of default size to 121 allocate. If the number of huge pages of default size is implicitly 122 specified, it can not be overwritten by a hugepagesz,hugepages 123 parameter pair for the default size. 124 125 For example, on an architecture with 2M default huge page size:: 126 127 hugepages=256 hugepagesz=2M hugepages=512 128 129 will result in 256 2M huge pages being allocated and a warning message 130 indicating that the hugepages=512 parameter is ignored. If a hugepages 131 parameter is preceded by an invalid hugepagesz parameter, it will 132 be ignored. 133default_hugepagesz 134 Specify the default huge page size. This parameter can 135 only be specified once on the command line. default_hugepagesz can 136 optionally be followed by the hugepages parameter to preallocate a 137 specific number of huge pages of default size. The number of default 138 sized huge pages to preallocate can also be implicitly specified as 139 mentioned in the hugepages section above. Therefore, on an 140 architecture with 2M default huge page size:: 141 142 hugepages=256 143 default_hugepagesz=2M hugepages=256 144 hugepages=256 default_hugepagesz=2M 145 146 will all result in 256 2M huge pages being allocated. Valid default 147 huge page size is architecture dependent. 148 149When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages`` 150indicates the current number of pre-allocated huge pages of the default size. 151Thus, one can use the following command to dynamically allocate/deallocate 152default sized persistent huge pages:: 153 154 echo 20 > /proc/sys/vm/nr_hugepages 155 156This command will try to adjust the number of default sized huge pages in the 157huge page pool to 20, allocating or freeing huge pages, as required. 158 159On a NUMA platform, the kernel will attempt to distribute the huge page pool 160over all the set of allowed nodes specified by the NUMA memory policy of the 161task that modifies ``nr_hugepages``. The default for the allowed nodes--when the 162task has default memory policy--is all on-line nodes with memory. Allowed 163nodes with insufficient available, contiguous memory for a huge page will be 164silently skipped when allocating persistent huge pages. See the 165:ref:`discussion below <mem_policy_and_hp_alloc>` 166of the interaction of task memory policy, cpusets and per node attributes 167with the allocation and freeing of persistent huge pages. 168 169The success or failure of huge page allocation depends on the amount of 170physically contiguous memory that is present in system at the time of the 171allocation attempt. If the kernel is unable to allocate huge pages from 172some nodes in a NUMA system, it will attempt to make up the difference by 173allocating extra pages on other nodes with sufficient available contiguous 174memory, if any. 175 176System administrators may want to put this command in one of the local rc 177init files. This will enable the kernel to allocate huge pages early in 178the boot process when the possibility of getting physical contiguous pages 179is still very high. Administrators can verify the number of huge pages 180actually allocated by checking the sysctl or meminfo. To check the per node 181distribution of huge pages in a NUMA system, use:: 182 183 cat /sys/devices/system/node/node*/meminfo | fgrep Huge 184 185``/proc/sys/vm/nr_overcommit_hugepages`` specifies how large the pool of 186huge pages can grow, if more huge pages than ``/proc/sys/vm/nr_hugepages`` are 187requested by applications. Writing any non-zero value into this file 188indicates that the hugetlb subsystem is allowed to try to obtain that 189number of "surplus" huge pages from the kernel's normal page pool, when the 190persistent huge page pool is exhausted. As these surplus huge pages become 191unused, they are freed back to the kernel's normal page pool. 192 193When increasing the huge page pool size via ``nr_hugepages``, any existing 194surplus pages will first be promoted to persistent huge pages. Then, additional 195huge pages will be allocated, if necessary and if possible, to fulfill 196the new persistent huge page pool size. 197 198The administrator may shrink the pool of persistent huge pages for 199the default huge page size by setting the ``nr_hugepages`` sysctl to a 200smaller value. The kernel will attempt to balance the freeing of huge pages 201across all nodes in the memory policy of the task modifying ``nr_hugepages``. 202Any free huge pages on the selected nodes will be freed back to the kernel's 203normal page pool. 204 205Caveat: Shrinking the persistent huge page pool via ``nr_hugepages`` such that 206it becomes less than the number of huge pages in use will convert the balance 207of the in-use huge pages to surplus huge pages. This will occur even if 208the number of surplus pages would exceed the overcommit value. As long as 209this condition holds--that is, until ``nr_hugepages+nr_overcommit_hugepages`` is 210increased sufficiently, or the surplus huge pages go out of use and are freed-- 211no more surplus huge pages will be allowed to be allocated. 212 213With support for multiple huge page pools at run-time available, much of 214the huge page userspace interface in ``/proc/sys/vm`` has been duplicated in 215sysfs. 216The ``/proc`` interfaces discussed above have been retained for backwards 217compatibility. The root huge page control directory in sysfs is:: 218 219 /sys/kernel/mm/hugepages 220 221For each huge page size supported by the running kernel, a subdirectory 222will exist, of the form:: 223 224 hugepages-${size}kB 225 226Inside each of these directories, the same set of files will exist:: 227 228 nr_hugepages 229 nr_hugepages_mempolicy 230 nr_overcommit_hugepages 231 free_hugepages 232 resv_hugepages 233 surplus_hugepages 234 235which function as described above for the default huge page-sized case. 236 237.. _mem_policy_and_hp_alloc: 238 239Interaction of Task Memory Policy with Huge Page Allocation/Freeing 240=================================================================== 241 242Whether huge pages are allocated and freed via the ``/proc`` interface or 243the ``/sysfs`` interface using the ``nr_hugepages_mempolicy`` attribute, the 244NUMA nodes from which huge pages are allocated or freed are controlled by the 245NUMA memory policy of the task that modifies the ``nr_hugepages_mempolicy`` 246sysctl or attribute. When the ``nr_hugepages`` attribute is used, mempolicy 247is ignored. 248 249The recommended method to allocate or free huge pages to/from the kernel 250huge page pool, using the ``nr_hugepages`` example above, is:: 251 252 numactl --interleave <node-list> echo 20 \ 253 >/proc/sys/vm/nr_hugepages_mempolicy 254 255or, more succinctly:: 256 257 numactl -m <node-list> echo 20 >/proc/sys/vm/nr_hugepages_mempolicy 258 259This will allocate or free ``abs(20 - nr_hugepages)`` to or from the nodes 260specified in <node-list>, depending on whether number of persistent huge pages 261is initially less than or greater than 20, respectively. No huge pages will be 262allocated nor freed on any node not included in the specified <node-list>. 263 264When adjusting the persistent hugepage count via ``nr_hugepages_mempolicy``, any 265memory policy mode--bind, preferred, local or interleave--may be used. The 266resulting effect on persistent huge page allocation is as follows: 267 268#. Regardless of mempolicy mode [see 269 :ref:`Documentation/admin-guide/mm/numa_memory_policy.rst <numa_memory_policy>`], 270 persistent huge pages will be distributed across the node or nodes 271 specified in the mempolicy as if "interleave" had been specified. 272 However, if a node in the policy does not contain sufficient contiguous 273 memory for a huge page, the allocation will not "fallback" to the nearest 274 neighbor node with sufficient contiguous memory. To do this would cause 275 undesirable imbalance in the distribution of the huge page pool, or 276 possibly, allocation of persistent huge pages on nodes not allowed by 277 the task's memory policy. 278 279#. One or more nodes may be specified with the bind or interleave policy. 280 If more than one node is specified with the preferred policy, only the 281 lowest numeric id will be used. Local policy will select the node where 282 the task is running at the time the nodes_allowed mask is constructed. 283 For local policy to be deterministic, the task must be bound to a cpu or 284 cpus in a single node. Otherwise, the task could be migrated to some 285 other node at any time after launch and the resulting node will be 286 indeterminate. Thus, local policy is not very useful for this purpose. 287 Any of the other mempolicy modes may be used to specify a single node. 288 289#. The nodes allowed mask will be derived from any non-default task mempolicy, 290 whether this policy was set explicitly by the task itself or one of its 291 ancestors, such as numactl. This means that if the task is invoked from a 292 shell with non-default policy, that policy will be used. One can specify a 293 node list of "all" with numactl --interleave or --membind [-m] to achieve 294 interleaving over all nodes in the system or cpuset. 295 296#. Any task mempolicy specified--e.g., using numactl--will be constrained by 297 the resource limits of any cpuset in which the task runs. Thus, there will 298 be no way for a task with non-default policy running in a cpuset with a 299 subset of the system nodes to allocate huge pages outside the cpuset 300 without first moving to a cpuset that contains all of the desired nodes. 301 302#. Boot-time huge page allocation attempts to distribute the requested number 303 of huge pages over all on-lines nodes with memory. 304 305Per Node Hugepages Attributes 306============================= 307 308A subset of the contents of the root huge page control directory in sysfs, 309described above, will be replicated under each the system device of each 310NUMA node with memory in:: 311 312 /sys/devices/system/node/node[0-9]*/hugepages/ 313 314Under this directory, the subdirectory for each supported huge page size 315contains the following attribute files:: 316 317 nr_hugepages 318 free_hugepages 319 surplus_hugepages 320 321The free\_' and surplus\_' attribute files are read-only. They return the number 322of free and surplus [overcommitted] huge pages, respectively, on the parent 323node. 324 325The ``nr_hugepages`` attribute returns the total number of huge pages on the 326specified node. When this attribute is written, the number of persistent huge 327pages on the parent node will be adjusted to the specified value, if sufficient 328resources exist, regardless of the task's mempolicy or cpuset constraints. 329 330Note that the number of overcommit and reserve pages remain global quantities, 331as we don't know until fault time, when the faulting task's mempolicy is 332applied, from which node the huge page allocation will be attempted. 333 334.. _using_huge_pages: 335 336Using Huge Pages 337================ 338 339If the user applications are going to request huge pages using mmap system 340call, then it is required that system administrator mount a file system of 341type hugetlbfs:: 342 343 mount -t hugetlbfs \ 344 -o uid=<value>,gid=<value>,mode=<value>,pagesize=<value>,size=<value>,\ 345 min_size=<value>,nr_inodes=<value> none /mnt/huge 346 347This command mounts a (pseudo) filesystem of type hugetlbfs on the directory 348``/mnt/huge``. Any file created on ``/mnt/huge`` uses huge pages. 349 350The ``uid`` and ``gid`` options sets the owner and group of the root of the 351file system. By default the ``uid`` and ``gid`` of the current process 352are taken. 353 354The ``mode`` option sets the mode of root of file system to value & 01777. 355This value is given in octal. By default the value 0755 is picked. 356 357If the platform supports multiple huge page sizes, the ``pagesize`` option can 358be used to specify the huge page size and associated pool. ``pagesize`` 359is specified in bytes. If ``pagesize`` is not specified the platform's 360default huge page size and associated pool will be used. 361 362The ``size`` option sets the maximum value of memory (huge pages) allowed 363for that filesystem (``/mnt/huge``). The ``size`` option can be specified 364in bytes, or as a percentage of the specified huge page pool (``nr_hugepages``). 365The size is rounded down to HPAGE_SIZE boundary. 366 367The ``min_size`` option sets the minimum value of memory (huge pages) allowed 368for the filesystem. ``min_size`` can be specified in the same way as ``size``, 369either bytes or a percentage of the huge page pool. 370At mount time, the number of huge pages specified by ``min_size`` are reserved 371for use by the filesystem. 372If there are not enough free huge pages available, the mount will fail. 373As huge pages are allocated to the filesystem and freed, the reserve count 374is adjusted so that the sum of allocated and reserved huge pages is always 375at least ``min_size``. 376 377The option ``nr_inodes`` sets the maximum number of inodes that ``/mnt/huge`` 378can use. 379 380If the ``size``, ``min_size`` or ``nr_inodes`` option is not provided on 381command line then no limits are set. 382 383For ``pagesize``, ``size``, ``min_size`` and ``nr_inodes`` options, you can 384use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. 385For example, size=2K has the same meaning as size=2048. 386 387While read system calls are supported on files that reside on hugetlb 388file systems, write system calls are not. 389 390Regular chown, chgrp, and chmod commands (with right permissions) could be 391used to change the file attributes on hugetlbfs. 392 393Also, it is important to note that no such mount command is required if 394applications are going to use only shmat/shmget system calls or mmap with 395MAP_HUGETLB. For an example of how to use mmap with MAP_HUGETLB see 396:ref:`map_hugetlb <map_hugetlb>` below. 397 398Users who wish to use hugetlb memory via shared memory segment should be 399members of a supplementary group and system admin needs to configure that gid 400into ``/proc/sys/vm/hugetlb_shm_group``. It is possible for same or different 401applications to use any combination of mmaps and shm* calls, though the mount of 402filesystem will be required for using mmap calls without MAP_HUGETLB. 403 404Syscalls that operate on memory backed by hugetlb pages only have their lengths 405aligned to the native page size of the processor; they will normally fail with 406errno set to EINVAL or exclude hugetlb pages that extend beyond the length if 407not hugepage aligned. For example, munmap(2) will fail if memory is backed by 408a hugetlb page and the length is smaller than the hugepage size. 409 410 411Examples 412======== 413 414.. _map_hugetlb: 415 416``map_hugetlb`` 417 see tools/testing/selftests/vm/map_hugetlb.c 418 419``hugepage-shm`` 420 see tools/testing/selftests/vm/hugepage-shm.c 421 422``hugepage-mmap`` 423 see tools/testing/selftests/vm/hugepage-mmap.c 424 425The `libhugetlbfs`_ library provides a wide range of userspace tools 426to help with huge page usability, environment setup, and control. 427 428.. _libhugetlbfs: https://github.com/libhugetlbfs/libhugetlbfs 429