blob: faf077d50d427f06be455a47e2d0f8c39e666a8b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2The intent of this file is to give a brief summary of hugetlbpage support in
3the Linux kernel. This support is built on top of multiple page size support
Masanari Iidac0d73052014-11-07 00:31:15 +09004that is provided by most modern architectures. For example, x86 CPUs normally
5support 4K and 2M (1G if architecturally supported) page sizes, ia64
Linus Torvalds1da177e2005-04-16 15:20:36 -07006architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M,
7256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical
8translations. Typically this is a very scarce resource on processor.
9Operating systems try to make best use of limited number of TLB resources.
10This optimization is more critical now as bigger and bigger physical memories
11(several GBs) are more readily available.
12
13Users can use the huge page support in Linux kernel by either using the mmap
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080014system call or standard SYSV shared memory system calls (shmget, shmat).
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Muli Ben-Yehuda5c7ad512005-11-07 00:59:42 -080016First the Linux kernel needs to be built with the CONFIG_HUGETLBFS
17(present under "File systems") and CONFIG_HUGETLB_PAGE (selected
18automatically when CONFIG_HUGETLBFS is selected) configuration
19options.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080021The /proc/meminfo file provides information about the total number of
22persistent hugetlb pages in the kernel's huge page pool. It also displays
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -080023default huge page size and information about the number of free, reserved
24and surplus huge pages in the pool of huge pages of default size.
25The huge page size is needed for generating the proper alignment and
26size of the arguments to system calls that map huge page regions.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080028The output of "cat /proc/meminfo" will include lines like:
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30.....
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -080031HugePages_Total: uuu
32HugePages_Free: vvv
33HugePages_Rsvd: www
34HugePages_Surp: xxx
35Hugepagesize: yyy kB
36Hugetlb: zzz kB
Randy Dunlap5e122272006-04-18 22:21:51 -070037
38where:
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070039HugePages_Total is the size of the pool of huge pages.
40HugePages_Free is the number of huge pages in the pool that are not yet
41 allocated.
42HugePages_Rsvd 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.
47HugePages_Surp is short for "surplus," and is the number of huge pages in
48 the pool above the value in /proc/sys/vm/nr_hugepages. The
49 maximum number of surplus huge pages is controlled by
50 /proc/sys/vm/nr_overcommit_hugepages.
Roman Gushchinfcb2b0c2018-01-31 16:16:22 -080051Hugepagesize is the default hugepage size (in Kb).
52Hugetlb is the total amount of memory (in kB), consumed by huge
53 pages of all sizes.
54 If huge pages of different sizes are in use, this number
55 will exceed HugePages_Total * Hugepagesize. To get more
56 detailed information, please, refer to
57 /sys/kernel/mm/hugepages (described below).
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/proc/filesystems should also show a filesystem of type "hugetlbfs" configured
61in the kernel.
62
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080063/proc/sys/vm/nr_hugepages indicates the current number of "persistent" huge
64pages in the kernel's huge page pool. "Persistent" huge pages will be
65returned to the huge page pool when freed by a task. A user with root
66privileges can dynamically allocate more or free some persistent huge pages
67by increasing or decreasing the value of 'nr_hugepages'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080069Pages that are used as huge pages are reserved inside the kernel and cannot
70be used for other purposes. Huge pages cannot be swapped out under
71memory pressure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080073Once a number of huge pages have been pre-allocated to the kernel huge page
74pool, a user with appropriate privilege can use either the mmap system call
75or shared memory system calls to use the huge pages. See the discussion of
76Using Huge Pages, below.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080078The administrator can allocate persistent huge pages on the kernel boot
79command line by specifying the "hugepages=N" parameter, where 'N' = the
80number of huge pages requested. This is the most reliable method of
81allocating huge pages as memory has not yet become fragmented.
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070082
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080083Some platforms support multiple huge page sizes. To allocate huge pages
Lucas De Marchi25985ed2011-03-30 22:57:33 -030084of a specific size, one must precede the huge pages boot command parameters
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070085with a huge page size selection parameter "hugepagesz=<size>". <size> must
86be specified in bytes with optional scale suffix [kKmMgG]. The default huge
87page size may be selected with the "default_hugepagesz=<size>" boot parameter.
88
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080089When multiple huge page sizes are supported, /proc/sys/vm/nr_hugepages
90indicates the current number of pre-allocated huge pages of the default size.
91Thus, one can use the following command to dynamically allocate/deallocate
92default sized persistent huge pages:
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 echo 20 > /proc/sys/vm/nr_hugepages
95
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080096This command will try to adjust the number of default sized huge pages in the
97huge page pool to 20, allocating or freeing huge pages, as required.
98
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070099On a NUMA platform, the kernel will attempt to distribute the huge page pool
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800100over all the set of allowed nodes specified by the NUMA memory policy of the
101task that modifies nr_hugepages. The default for the allowed nodes--when the
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -0800102task has default memory policy--is all on-line nodes with memory. Allowed
103nodes with insufficient available, contiguous memory for a huge page will be
104silently skipped when allocating persistent huge pages. See the discussion
105below of the interaction of task memory policy, cpusets and per node attributes
106with the allocation and freeing of persistent huge pages.
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -0800107
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700108The success or failure of huge page allocation depends on the amount of
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800109physically contiguous memory that is present in system at the time of the
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700110allocation attempt. If the kernel is unable to allocate huge pages from
111some nodes in a NUMA system, it will attempt to make up the difference by
112allocating extra pages on other nodes with sufficient available contiguous
113memory, if any.
114
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800115System administrators may want to put this command in one of the local rc
116init files. This will enable the kernel to allocate huge pages early in
117the boot process when the possibility of getting physical contiguous pages
118is still very high. Administrators can verify the number of huge pages
119actually allocated by checking the sysctl or meminfo. To check the per node
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700120distribution of huge pages in a NUMA system, use:
121
122 cat /sys/devices/system/node/node*/meminfo | fgrep Huge
123
124/proc/sys/vm/nr_overcommit_hugepages specifies how large the pool of
125huge pages can grow, if more huge pages than /proc/sys/vm/nr_hugepages are
126requested by applications. Writing any non-zero value into this file
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800127indicates that the hugetlb subsystem is allowed to try to obtain that
128number of "surplus" huge pages from the kernel's normal page pool, when the
129persistent huge page pool is exhausted. As these surplus huge pages become
130unused, they are freed back to the kernel's normal page pool.
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -0800131
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800132When increasing the huge page pool size via nr_hugepages, any existing surplus
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700133pages will first be promoted to persistent huge pages. Then, additional
134huge pages will be allocated, if necessary and if possible, to fulfill
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800135the new persistent huge page pool size.
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700136
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800137The administrator may shrink the pool of persistent huge pages for
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700138the default huge page size by setting the nr_hugepages sysctl to a
139smaller value. The kernel will attempt to balance the freeing of huge pages
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800140across all nodes in the memory policy of the task modifying nr_hugepages.
141Any free huge pages on the selected nodes will be freed back to the kernel's
142normal page pool.
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700143
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800144Caveat: Shrinking the persistent huge page pool via nr_hugepages such that
145it becomes less than the number of huge pages in use will convert the balance
146of the in-use huge pages to surplus huge pages. This will occur even if
147the number of surplus pages it would exceed the overcommit value. As long as
148this condition holds--that is, until nr_hugepages+nr_overcommit_hugepages is
149increased sufficiently, or the surplus huge pages go out of use and are freed--
150no more surplus huge pages will be allowed to be allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700152With support for multiple huge page pools at run-time available, much of
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800153the huge page userspace interface in /proc/sys/vm has been duplicated in sysfs.
154The /proc interfaces discussed above have been retained for backwards
155compatibility. The root huge page control directory in sysfs is:
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700156
157 /sys/kernel/mm/hugepages
158
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700159For each huge page size supported by the running kernel, a subdirectory
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800160will exist, of the form:
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700161
162 hugepages-${size}kB
163
164Inside each of these directories, the same set of files will exist:
165
166 nr_hugepages
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800167 nr_hugepages_mempolicy
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700168 nr_overcommit_hugepages
169 free_hugepages
170 resv_hugepages
171 surplus_hugepages
172
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700173which function as described above for the default huge page-sized case.
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700174
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800175
176Interaction of Task Memory Policy with Huge Page Allocation/Freeing
Davidlohr Bueso15610c82013-09-11 14:21:48 -0700177===================================================================
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800178
179Whether huge pages are allocated and freed via the /proc interface or
180the /sysfs interface using the nr_hugepages_mempolicy attribute, the NUMA
181nodes from which huge pages are allocated or freed are controlled by the
182NUMA memory policy of the task that modifies the nr_hugepages_mempolicy
183sysctl or attribute. When the nr_hugepages attribute is used, mempolicy
184is ignored.
185
186The recommended method to allocate or free huge pages to/from the kernel
187huge page pool, using the nr_hugepages example above, is:
188
189 numactl --interleave <node-list> echo 20 \
190 >/proc/sys/vm/nr_hugepages_mempolicy
191
192or, more succinctly:
193
194 numactl -m <node-list> echo 20 >/proc/sys/vm/nr_hugepages_mempolicy
195
196This will allocate or free abs(20 - nr_hugepages) to or from the nodes
197specified in <node-list>, depending on whether number of persistent huge pages
198is initially less than or greater than 20, respectively. No huge pages will be
199allocated nor freed on any node not included in the specified <node-list>.
200
201When adjusting the persistent hugepage count via nr_hugepages_mempolicy, any
202memory policy mode--bind, preferred, local or interleave--may be used. The
203resulting effect on persistent huge page allocation is as follows:
204
2051) Regardless of mempolicy mode [see Documentation/vm/numa_memory_policy.txt],
206 persistent huge pages will be distributed across the node or nodes
207 specified in the mempolicy as if "interleave" had been specified.
208 However, if a node in the policy does not contain sufficient contiguous
209 memory for a huge page, the allocation will not "fallback" to the nearest
210 neighbor node with sufficient contiguous memory. To do this would cause
211 undesirable imbalance in the distribution of the huge page pool, or
212 possibly, allocation of persistent huge pages on nodes not allowed by
213 the task's memory policy.
214
2152) One or more nodes may be specified with the bind or interleave policy.
216 If more than one node is specified with the preferred policy, only the
217 lowest numeric id will be used. Local policy will select the node where
218 the task is running at the time the nodes_allowed mask is constructed.
219 For local policy to be deterministic, the task must be bound to a cpu or
220 cpus in a single node. Otherwise, the task could be migrated to some
221 other node at any time after launch and the resulting node will be
222 indeterminate. Thus, local policy is not very useful for this purpose.
223 Any of the other mempolicy modes may be used to specify a single node.
224
2253) The nodes allowed mask will be derived from any non-default task mempolicy,
226 whether this policy was set explicitly by the task itself or one of its
227 ancestors, such as numactl. This means that if the task is invoked from a
228 shell with non-default policy, that policy will be used. One can specify a
229 node list of "all" with numactl --interleave or --membind [-m] to achieve
230 interleaving over all nodes in the system or cpuset.
231
Kees Cook08559652016-04-26 16:41:21 -07002324) Any task mempolicy specified--e.g., using numactl--will be constrained by
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800233 the resource limits of any cpuset in which the task runs. Thus, there will
234 be no way for a task with non-default policy running in a cpuset with a
235 subset of the system nodes to allocate huge pages outside the cpuset
236 without first moving to a cpuset that contains all of the desired nodes.
237
2385) Boot-time huge page allocation attempts to distribute the requested number
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -0800239 of huge pages over all on-lines nodes with memory.
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800240
241Per Node Hugepages Attributes
Davidlohr Bueso15610c82013-09-11 14:21:48 -0700242=============================
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800243
244A subset of the contents of the root huge page control directory in sysfs,
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800245described above, will be replicated under each the system device of each
246NUMA node with memory in:
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800247
248 /sys/devices/system/node/node[0-9]*/hugepages/
249
250Under this directory, the subdirectory for each supported huge page size
251contains the following attribute files:
252
253 nr_hugepages
254 free_hugepages
255 surplus_hugepages
256
257The free_' and surplus_' attribute files are read-only. They return the number
258of free and surplus [overcommitted] huge pages, respectively, on the parent
259node.
260
261The nr_hugepages attribute returns the total number of huge pages on the
262specified node. When this attribute is written, the number of persistent huge
263pages on the parent node will be adjusted to the specified value, if sufficient
264resources exist, regardless of the task's mempolicy or cpuset constraints.
265
266Note that the number of overcommit and reserve pages remain global quantities,
267as we don't know until fault time, when the faulting task's mempolicy is
268applied, from which node the huge page allocation will be attempted.
269
270
271Using Huge Pages
Davidlohr Bueso15610c82013-09-11 14:21:48 -0700272================
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800273
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700274If the user applications are going to request huge pages using mmap system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275call, then it is required that system administrator mount a file system of
276type hugetlbfs:
277
Randy Dunlape73a75f2007-07-15 23:40:52 -0700278 mount -t hugetlbfs \
Mike Kravetz8c9b9702015-04-15 16:13:45 -0700279 -o uid=<value>,gid=<value>,mode=<value>,pagesize=<value>,size=<value>,\
280 min_size=<value>,nr_inodes=<value> none /mnt/huge
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700283/mnt/huge. Any files created on /mnt/huge uses huge pages. The uid and gid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284options sets the owner and group of the root of the file system. By default
285the uid and gid of the current process are taken. The mode option sets the
Kirill Smelkov011bc482014-10-22 19:54:46 +0400286mode of root of file system to value & 01777. This value is given in octal.
Geert Uytterhoeven30955e72016-05-10 20:59:56 +0200287By default the value 0755 is picked. If the platform supports multiple huge
Mike Kravetz8c9b9702015-04-15 16:13:45 -0700288page sizes, the pagesize option can be used to specify the huge page size and
289associated pool. pagesize is specified in bytes. If pagesize is not specified
Geert Uytterhoeven30955e72016-05-10 20:59:56 +0200290the platform's default huge page size and associated pool will be used. The
Mike Kravetz8c9b9702015-04-15 16:13:45 -0700291size option sets the maximum value of memory (huge pages) allowed for that
292filesystem (/mnt/huge). The size option can be specified in bytes, or as a
293percentage of the specified huge page pool (nr_hugepages). The size is
294rounded down to HPAGE_SIZE boundary. The min_size option sets the minimum
295value of memory (huge pages) allowed for the filesystem. min_size can be
296specified in the same way as size, either bytes or a percentage of the
297huge page pool. At mount time, the number of huge pages specified by
298min_size are reserved for use by the filesystem. If there are not enough
299free huge pages available, the mount will fail. As huge pages are allocated
300to the filesystem and freed, the reserve count is adjusted so that the sum
301of allocated and reserved huge pages is always at least min_size. The option
302nr_inodes sets the maximum number of inodes that /mnt/huge can use. If the
303size, min_size or nr_inodes option is not provided on command line then
304no limits are set. For pagesize, size, min_size and nr_inodes options, you
305can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For example, size=2K
306has the same meaning as size=2048.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -0800308While read system calls are supported on files that reside on hugetlb
309file systems, write system calls are not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Randy Dunlap21a26d42006-04-10 22:53:04 -0700311Regular chown, chgrp, and chmod commands (with right permissions) could be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312used to change the file attributes on hugetlbfs.
313
David Rientjes80d6b942015-04-15 16:14:26 -0700314Also, it is important to note that no such mount command is required if
Eric B Munson94bf5ce2009-09-21 17:03:48 -0700315applications are going to use only shmat/shmget system calls or mmap with
David Rientjes80d6b942015-04-15 16:14:26 -0700316MAP_HUGETLB. For an example of how to use mmap with MAP_HUGETLB see map_hugetlb
317below.
318
319Users who wish to use hugetlb memory via shared memory segment should be a
320member of a supplementary group and system admin needs to configure that gid
321into /proc/sys/vm/hugetlb_shm_group. It is possible for same or different
322applications to use any combination of mmaps and shm* calls, though the mount of
323filesystem will be required for using mmap calls without MAP_HUGETLB.
324
325Syscalls that operate on memory backed by hugetlb pages only have their lengths
326aligned to the native page size of the processor; they will normally fail with
327errno set to EINVAL or exclude hugetlb pages that extend beyond the length if
328not hugepage aligned. For example, munmap(2) will fail if memory is backed by
329a hugetlb page and the length is smaller than the hugepage size.
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Davidlohr Bueso15610c82013-09-11 14:21:48 -0700332Examples
333========
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Davidlohr Bueso15610c82013-09-11 14:21:48 -07003351) map_hugetlb: see tools/testing/selftests/vm/map_hugetlb.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Davidlohr Bueso15610c82013-09-11 14:21:48 -07003372) hugepage-shm: see tools/testing/selftests/vm/hugepage-shm.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Davidlohr Bueso15610c82013-09-11 14:21:48 -07003393) hugepage-mmap: see tools/testing/selftests/vm/hugepage-mmap.c
Zhouping Liud46f3d82012-08-21 16:15:57 -0700340
Mike Kravetze6590742015-09-08 15:02:58 -07003414) The libhugetlbfs (https://github.com/libhugetlbfs/libhugetlbfs) library
342 provides a wide range of userspace tools to help with huge page usability,
343 environment setup, and control.
344
345Kernel development regression testing
346=====================================
347
348The most complete set of hugetlb tests are in the libhugetlbfs repository.
349If you modify any hugetlb related code, use the libhugetlbfs test suite
350to check for regressions. In addition, if you add any new hugetlb
351functionality, please add appropriate tests to libhugetlbfs.