blob: 457634c1e03e391e84be31a64da6cd9a995aaeba [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
4that is provided by most modern architectures. For example, i386
5architecture supports 4K and 4M (2M in PAE mode) page sizes, ia64
6architecture 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
23information about the number of free, reserved and surplus huge pages and the
24default huge page size. The huge page size is needed for generating the
25proper alignment and size of the arguments to system calls that map huge page
26regions.
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.....
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -080031HugePages_Total: vvv
32HugePages_Free: www
33HugePages_Rsvd: xxx
34HugePages_Surp: yyy
Randy Dunlap5e122272006-04-18 22:21:51 -070035Hugepagesize: zzz kB
36
37where:
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070038HugePages_Total is the size of the pool of huge pages.
39HugePages_Free is the number of huge pages in the pool that are not yet
40 allocated.
41HugePages_Rsvd is short for "reserved," and is the number of huge pages for
42 which a commitment to allocate from the pool has been made,
43 but no allocation has yet been made. Reserved huge pages
44 guarantee that an application will be able to allocate a
45 huge page from the pool of huge pages at fault time.
46HugePages_Surp is short for "surplus," and is the number of huge pages in
47 the pool above the value in /proc/sys/vm/nr_hugepages. The
48 maximum number of surplus huge pages is controlled by
49 /proc/sys/vm/nr_overcommit_hugepages.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/proc/filesystems should also show a filesystem of type "hugetlbfs" configured
52in the kernel.
53
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080054/proc/sys/vm/nr_hugepages indicates the current number of "persistent" huge
55pages in the kernel's huge page pool. "Persistent" huge pages will be
56returned to the huge page pool when freed by a task. A user with root
57privileges can dynamically allocate more or free some persistent huge pages
58by increasing or decreasing the value of 'nr_hugepages'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080060Pages that are used as huge pages are reserved inside the kernel and cannot
61be used for other purposes. Huge pages cannot be swapped out under
62memory pressure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080064Once a number of huge pages have been pre-allocated to the kernel huge page
65pool, a user with appropriate privilege can use either the mmap system call
66or shared memory system calls to use the huge pages. See the discussion of
67Using Huge Pages, below.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080069The administrator can allocate persistent huge pages on the kernel boot
70command line by specifying the "hugepages=N" parameter, where 'N' = the
71number of huge pages requested. This is the most reliable method of
72allocating huge pages as memory has not yet become fragmented.
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070073
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080074Some platforms support multiple huge page sizes. To allocate huge pages
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070075of a specific size, one must preceed the huge pages boot command parameters
76with a huge page size selection parameter "hugepagesz=<size>". <size> must
77be specified in bytes with optional scale suffix [kKmMgG]. The default huge
78page size may be selected with the "default_hugepagesz=<size>" boot parameter.
79
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080080When multiple huge page sizes are supported, /proc/sys/vm/nr_hugepages
81indicates the current number of pre-allocated huge pages of the default size.
82Thus, one can use the following command to dynamically allocate/deallocate
83default sized persistent huge pages:
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 echo 20 > /proc/sys/vm/nr_hugepages
86
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080087This command will try to adjust the number of default sized huge pages in the
88huge page pool to 20, allocating or freeing huge pages, as required.
89
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070090On a NUMA platform, the kernel will attempt to distribute the huge page pool
Lee Schermerhorn267b4c22009-12-14 17:58:30 -080091over all the set of allowed nodes specified by the NUMA memory policy of the
92task that modifies nr_hugepages. The default for the allowed nodes--when the
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -080093task has default memory policy--is all on-line nodes with memory. Allowed
94nodes with insufficient available, contiguous memory for a huge page will be
95silently skipped when allocating persistent huge pages. See the discussion
96below of the interaction of task memory policy, cpusets and per node attributes
97with the allocation and freeing of persistent huge pages.
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -080098
Lee Schermerhorn41a25e72009-09-21 17:01:24 -070099The success or failure of huge page allocation depends on the amount of
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800100physically contiguous memory that is present in system at the time of the
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700101allocation attempt. If the kernel is unable to allocate huge pages from
102some nodes in a NUMA system, it will attempt to make up the difference by
103allocating extra pages on other nodes with sufficient available contiguous
104memory, if any.
105
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800106System administrators may want to put this command in one of the local rc
107init files. This will enable the kernel to allocate huge pages early in
108the boot process when the possibility of getting physical contiguous pages
109is still very high. Administrators can verify the number of huge pages
110actually allocated by checking the sysctl or meminfo. To check the per node
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700111distribution of huge pages in a NUMA system, use:
112
113 cat /sys/devices/system/node/node*/meminfo | fgrep Huge
114
115/proc/sys/vm/nr_overcommit_hugepages specifies how large the pool of
116huge pages can grow, if more huge pages than /proc/sys/vm/nr_hugepages are
117requested by applications. Writing any non-zero value into this file
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800118indicates that the hugetlb subsystem is allowed to try to obtain that
119number of "surplus" huge pages from the kernel's normal page pool, when the
120persistent huge page pool is exhausted. As these surplus huge pages become
121unused, they are freed back to the kernel's normal page pool.
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -0800122
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800123When increasing the huge page pool size via nr_hugepages, any existing surplus
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700124pages will first be promoted to persistent huge pages. Then, additional
125huge pages will be allocated, if necessary and if possible, to fulfill
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800126the new persistent huge page pool size.
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700127
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800128The administrator may shrink the pool of persistent huge pages for
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700129the default huge page size by setting the nr_hugepages sysctl to a
130smaller value. The kernel will attempt to balance the freeing of huge pages
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800131across all nodes in the memory policy of the task modifying nr_hugepages.
132Any free huge pages on the selected nodes will be freed back to the kernel's
133normal page pool.
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700134
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800135Caveat: Shrinking the persistent huge page pool via nr_hugepages such that
136it becomes less than the number of huge pages in use will convert the balance
137of the in-use huge pages to surplus huge pages. This will occur even if
138the number of surplus pages it would exceed the overcommit value. As long as
139this condition holds--that is, until nr_hugepages+nr_overcommit_hugepages is
140increased sufficiently, or the surplus huge pages go out of use and are freed--
141no more surplus huge pages will be allowed to be allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700143With support for multiple huge page pools at run-time available, much of
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800144the huge page userspace interface in /proc/sys/vm has been duplicated in sysfs.
145The /proc interfaces discussed above have been retained for backwards
146compatibility. The root huge page control directory in sysfs is:
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700147
148 /sys/kernel/mm/hugepages
149
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700150For each huge page size supported by the running kernel, a subdirectory
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800151will exist, of the form:
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700152
153 hugepages-${size}kB
154
155Inside each of these directories, the same set of files will exist:
156
157 nr_hugepages
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800158 nr_hugepages_mempolicy
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700159 nr_overcommit_hugepages
160 free_hugepages
161 resv_hugepages
162 surplus_hugepages
163
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700164which function as described above for the default huge page-sized case.
Nishanth Aravamudana3437872008-07-23 21:27:44 -0700165
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800166
167Interaction of Task Memory Policy with Huge Page Allocation/Freeing
168
169Whether huge pages are allocated and freed via the /proc interface or
170the /sysfs interface using the nr_hugepages_mempolicy attribute, the NUMA
171nodes from which huge pages are allocated or freed are controlled by the
172NUMA memory policy of the task that modifies the nr_hugepages_mempolicy
173sysctl or attribute. When the nr_hugepages attribute is used, mempolicy
174is ignored.
175
176The recommended method to allocate or free huge pages to/from the kernel
177huge page pool, using the nr_hugepages example above, is:
178
179 numactl --interleave <node-list> echo 20 \
180 >/proc/sys/vm/nr_hugepages_mempolicy
181
182or, more succinctly:
183
184 numactl -m <node-list> echo 20 >/proc/sys/vm/nr_hugepages_mempolicy
185
186This will allocate or free abs(20 - nr_hugepages) to or from the nodes
187specified in <node-list>, depending on whether number of persistent huge pages
188is initially less than or greater than 20, respectively. No huge pages will be
189allocated nor freed on any node not included in the specified <node-list>.
190
191When adjusting the persistent hugepage count via nr_hugepages_mempolicy, any
192memory policy mode--bind, preferred, local or interleave--may be used. The
193resulting effect on persistent huge page allocation is as follows:
194
1951) Regardless of mempolicy mode [see Documentation/vm/numa_memory_policy.txt],
196 persistent huge pages will be distributed across the node or nodes
197 specified in the mempolicy as if "interleave" had been specified.
198 However, if a node in the policy does not contain sufficient contiguous
199 memory for a huge page, the allocation will not "fallback" to the nearest
200 neighbor node with sufficient contiguous memory. To do this would cause
201 undesirable imbalance in the distribution of the huge page pool, or
202 possibly, allocation of persistent huge pages on nodes not allowed by
203 the task's memory policy.
204
2052) One or more nodes may be specified with the bind or interleave policy.
206 If more than one node is specified with the preferred policy, only the
207 lowest numeric id will be used. Local policy will select the node where
208 the task is running at the time the nodes_allowed mask is constructed.
209 For local policy to be deterministic, the task must be bound to a cpu or
210 cpus in a single node. Otherwise, the task could be migrated to some
211 other node at any time after launch and the resulting node will be
212 indeterminate. Thus, local policy is not very useful for this purpose.
213 Any of the other mempolicy modes may be used to specify a single node.
214
2153) The nodes allowed mask will be derived from any non-default task mempolicy,
216 whether this policy was set explicitly by the task itself or one of its
217 ancestors, such as numactl. This means that if the task is invoked from a
218 shell with non-default policy, that policy will be used. One can specify a
219 node list of "all" with numactl --interleave or --membind [-m] to achieve
220 interleaving over all nodes in the system or cpuset.
221
2224) Any task mempolicy specifed--e.g., using numactl--will be constrained by
223 the resource limits of any cpuset in which the task runs. Thus, there will
224 be no way for a task with non-default policy running in a cpuset with a
225 subset of the system nodes to allocate huge pages outside the cpuset
226 without first moving to a cpuset that contains all of the desired nodes.
227
2285) Boot-time huge page allocation attempts to distribute the requested number
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -0800229 of huge pages over all on-lines nodes with memory.
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800230
231Per Node Hugepages Attributes
232
233A subset of the contents of the root huge page control directory in sysfs,
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800234described above, will be replicated under each the system device of each
235NUMA node with memory in:
Lee Schermerhorn267b4c22009-12-14 17:58:30 -0800236
237 /sys/devices/system/node/node[0-9]*/hugepages/
238
239Under this directory, the subdirectory for each supported huge page size
240contains the following attribute files:
241
242 nr_hugepages
243 free_hugepages
244 surplus_hugepages
245
246The free_' and surplus_' attribute files are read-only. They return the number
247of free and surplus [overcommitted] huge pages, respectively, on the parent
248node.
249
250The nr_hugepages attribute returns the total number of huge pages on the
251specified node. When this attribute is written, the number of persistent huge
252pages on the parent node will be adjusted to the specified value, if sufficient
253resources exist, regardless of the task's mempolicy or cpuset constraints.
254
255Note that the number of overcommit and reserve pages remain global quantities,
256as we don't know until fault time, when the faulting task's mempolicy is
257applied, from which node the huge page allocation will be attempted.
258
259
260Using Huge Pages
261
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700262If the user applications are going to request huge pages using mmap system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263call, then it is required that system administrator mount a file system of
264type hugetlbfs:
265
Randy Dunlape73a75f2007-07-15 23:40:52 -0700266 mount -t hugetlbfs \
267 -o uid=<value>,gid=<value>,mode=<value>,size=<value>,nr_inodes=<value> \
268 none /mnt/huge
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
Lee Schermerhorn41a25e72009-09-21 17:01:24 -0700271/mnt/huge. Any files created on /mnt/huge uses huge pages. The uid and gid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272options sets the owner and group of the root of the file system. By default
273the uid and gid of the current process are taken. The mode option sets the
274mode of root of file system to value & 0777. This value is given in octal.
275By default the value 0755 is picked. The size option sets the maximum value of
276memory (huge pages) allowed for that filesystem (/mnt/huge). The size is
Randy Dunlap21a26d42006-04-10 22:53:04 -0700277rounded down to HPAGE_SIZE. The option nr_inodes sets the maximum number of
Randy Dunlape73a75f2007-07-15 23:40:52 -0700278inodes that /mnt/huge can use. If the size or nr_inodes option is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279provided on command line then no limits are set. For size and nr_inodes
Muli Ben-Yehuda5c7ad512005-11-07 00:59:42 -0800280options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For
Randy Dunlape73a75f2007-07-15 23:40:52 -0700281example, size=2K has the same meaning as size=2048.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -0800283While read system calls are supported on files that reside on hugetlb
284file systems, write system calls are not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Randy Dunlap21a26d42006-04-10 22:53:04 -0700286Regular chown, chgrp, and chmod commands (with right permissions) could be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287used to change the file attributes on hugetlbfs.
288
289Also, it is important to note that no such mount command is required if the
Eric B Munson94bf5ce2009-09-21 17:03:48 -0700290applications are going to use only shmat/shmget system calls or mmap with
291MAP_HUGETLB. Users who wish to use hugetlb page via shared memory segment
292should be a member of a supplementary group and system admin needs to
293configure that gid into /proc/sys/vm/hugetlb_shm_group. It is possible for
294same or different applications to use any combination of mmaps and shm*
295calls, though the mount of filesystem will be required for using mmap calls
296without MAP_HUGETLB. For an example of how to use mmap with MAP_HUGETLB see
297map_hugetlb.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299*******************************************************************
300
301/*
Randy Dunlap70bace82010-03-10 15:21:58 -0800302 * hugepage-shm: see Documentation/vm/hugepage-shm.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305*******************************************************************
306
307/*
Randy Dunlap70bace82010-03-10 15:21:58 -0800308 * hugepage-mmap: see Documentation/vm/hugepage-mmap.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */