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