hugetlbpage.txt 17 KB

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