hugetlbpage.rst 22 KB

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