blob: ea8714fcc3ad4882dba62d625d22e61ac0a3079f [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
14system call or standard SYSv shared memory system calls (shmget, shmat).
15
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
21The kernel built with hugepage support should show the number of configured
Muli Ben-Yehuda5c7ad512005-11-07 00:59:42 -080022hugepages in the system by running the "cat /proc/meminfo" command.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/proc/meminfo also provides information about the total number of hugetlb
25pages configured in the kernel. It also displays information about the
26number of free hugetlb pages at any time. It also displays information about
27the configured hugepage size - this is needed for generating the proper
28alignment and size of the arguments to the above system calls.
29
Randy Dunlap21a26d42006-04-10 22:53:04 -070030The output of "cat /proc/meminfo" will have lines like:
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32.....
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -080033HugePages_Total: vvv
34HugePages_Free: www
35HugePages_Rsvd: xxx
36HugePages_Surp: yyy
Randy Dunlap5e122272006-04-18 22:21:51 -070037Hugepagesize: zzz kB
38
39where:
40HugePages_Total is the size of the pool of hugepages.
41HugePages_Free is the number of hugepages in the pool that are not yet
42allocated.
43HugePages_Rsvd is short for "reserved," and is the number of hugepages
44for which a commitment to allocate from the pool has been made, but no
45allocation has yet been made. It's vaguely analogous to overcommit.
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -080046HugePages_Surp is short for "surplus," and is the number of hugepages in
47the pool above the value in /proc/sys/vm/nr_hugepages. The maximum
48number of surplus hugepages 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
54/proc/sys/vm/nr_hugepages indicates the current number of configured hugetlb
55pages in the kernel. Super user can dynamically request more (or free some
Muli Ben-Yehuda5c7ad512005-11-07 00:59:42 -080056pre-configured) hugepages.
57The allocation (or deallocation) of hugetlb pages is possible only if there are
Linus Torvalds1da177e2005-04-16 15:20:36 -070058enough physically contiguous free pages in system (freeing of hugepages is
Randy Dunlap21a26d42006-04-10 22:53:04 -070059possible only if there are enough hugetlb pages free that can be transferred
Linus Torvalds1da177e2005-04-16 15:20:36 -070060back to regular memory pool).
61
Randy Dunlap21a26d42006-04-10 22:53:04 -070062Pages that are used as hugetlb pages are reserved inside the kernel and cannot
63be used for other purposes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65Once the kernel with Hugetlb page support is built and running, a user can
66use either the mmap system call or shared memory system calls to start using
67the huge pages. It is required that the system administrator preallocate
Muli Ben-Yehuda5c7ad512005-11-07 00:59:42 -080068enough memory for huge page purposes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70Use the following command to dynamically allocate/deallocate hugepages:
71
72 echo 20 > /proc/sys/vm/nr_hugepages
73
74This command will try to configure 20 hugepages in the system. The success
75or failure of allocation depends on the amount of physically contiguous
76memory that is preset in system at this time. System administrators may want
Randy Dunlap21a26d42006-04-10 22:53:04 -070077to put this command in one of the local rc init files. This will enable the
Linus Torvalds1da177e2005-04-16 15:20:36 -070078kernel to request huge pages early in the boot process (when the possibility
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -080079of getting physical contiguous pages is still very high). In either
Matt LaPlanted9195882008-07-25 19:45:33 -070080case, administrators will want to verify the number of hugepages actually
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -080081allocated by checking the sysctl or meminfo.
82
83/proc/sys/vm/nr_overcommit_hugepages indicates how large the pool of
84hugepages can grow, if more hugepages than /proc/sys/vm/nr_hugepages are
85requested by applications. echo'ing any non-zero value into this file
86indicates that the hugetlb subsystem is allowed to try to obtain
87hugepages from the buddy allocator, if the normal pool is exhausted. As
88these surplus hugepages go out of use, they are freed back to the buddy
89allocator.
90
Nishanth Aravamudan423bec42008-04-15 14:34:43 -070091Caveat: Shrinking the pool via nr_hugepages such that it becomes less
92than the number of hugepages in use will convert the balance to surplus
93huge pages even if it would exceed the overcommit value. As long as
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -080094this condition holds, however, no more surplus huge pages will be
95allowed on the system until one of the two sysctls are increased
96sufficiently, or the surplus huge pages go out of use and are freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Nishanth Aravamudana3437872008-07-23 21:27:44 -070098With support for multiple hugepage pools at run-time available, much of
99the hugepage userspace interface has been duplicated in sysfs. The above
100information applies to the default hugepage size (which will be
101controlled by the proc interfaces for backwards compatibility). The root
102hugepage control directory is
103
104 /sys/kernel/mm/hugepages
105
106For each hugepage size supported by the running kernel, a subdirectory
107will exist, of the form
108
109 hugepages-${size}kB
110
111Inside each of these directories, the same set of files will exist:
112
113 nr_hugepages
114 nr_overcommit_hugepages
115 free_hugepages
116 resv_hugepages
117 surplus_hugepages
118
119which function as described above for the default hugepage-sized case.
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121If the user applications are going to request hugepages using mmap system
122call, then it is required that system administrator mount a file system of
123type hugetlbfs:
124
Randy Dunlape73a75f2007-07-15 23:40:52 -0700125 mount -t hugetlbfs \
126 -o uid=<value>,gid=<value>,mode=<value>,size=<value>,nr_inodes=<value> \
127 none /mnt/huge
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
130/mnt/huge. Any files created on /mnt/huge uses hugepages. The uid and gid
131options sets the owner and group of the root of the file system. By default
132the uid and gid of the current process are taken. The mode option sets the
133mode of root of file system to value & 0777. This value is given in octal.
134By default the value 0755 is picked. The size option sets the maximum value of
135memory (huge pages) allowed for that filesystem (/mnt/huge). The size is
Randy Dunlap21a26d42006-04-10 22:53:04 -0700136rounded down to HPAGE_SIZE. The option nr_inodes sets the maximum number of
Randy Dunlape73a75f2007-07-15 23:40:52 -0700137inodes that /mnt/huge can use. If the size or nr_inodes option is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138provided on command line then no limits are set. For size and nr_inodes
Muli Ben-Yehuda5c7ad512005-11-07 00:59:42 -0800139options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For
Randy Dunlape73a75f2007-07-15 23:40:52 -0700140example, size=2K has the same meaning as size=2048.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Nishanth Aravamudand5dbac82007-12-17 16:20:25 -0800142While read system calls are supported on files that reside on hugetlb
143file systems, write system calls are not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Randy Dunlap21a26d42006-04-10 22:53:04 -0700145Regular chown, chgrp, and chmod commands (with right permissions) could be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146used to change the file attributes on hugetlbfs.
147
148Also, it is important to note that no such mount command is required if the
149applications are going to use only shmat/shmget system calls. Users who
150wish to use hugetlb page via shared memory segment should be a member of
151a supplementary group and system admin needs to configure that gid into
152/proc/sys/vm/hugetlb_shm_group. It is possible for same or different
Randy Dunlap21a26d42006-04-10 22:53:04 -0700153applications to use any combination of mmaps and shm* calls, though the
154mount of filesystem will be required for using mmap calls.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156*******************************************************************
157
158/*
159 * Example of using hugepage memory in a user application using Sys V shared
160 * memory system calls. In this example the app is requesting 256MB of
161 * memory that is backed by huge pages. The application uses the flag
162 * SHM_HUGETLB in the shmget system call to inform the kernel that it is
163 * requesting hugepages.
164 *
165 * For the ia64 architecture, the Linux kernel reserves Region number 4 for
166 * hugepages. That means the addresses starting with 0x800000... will need
167 * to be specified. Specifying a fixed address is not required on ppc64,
168 * i386 or x86_64.
169 *
170 * Note: The default shared memory limit is quite low on many kernels,
171 * you may need to increase it via:
172 *
173 * echo 268435456 > /proc/sys/kernel/shmmax
174 *
175 * This will increase the maximum size per shared memory segment to 256MB.
176 * The other limit that you will hit eventually is shmall which is the
177 * total amount of shared memory in pages. To set it to 16GB on a system
178 * with a 4kB pagesize do:
179 *
180 * echo 4194304 > /proc/sys/kernel/shmall
181 */
182#include <stdlib.h>
183#include <stdio.h>
184#include <sys/types.h>
185#include <sys/ipc.h>
186#include <sys/shm.h>
187#include <sys/mman.h>
188
189#ifndef SHM_HUGETLB
190#define SHM_HUGETLB 04000
191#endif
192
193#define LENGTH (256UL*1024*1024)
194
195#define dprintf(x) printf(x)
196
197/* Only ia64 requires this */
198#ifdef __ia64__
199#define ADDR (void *)(0x8000000000000000UL)
200#define SHMAT_FLAGS (SHM_RND)
201#else
202#define ADDR (void *)(0x0UL)
203#define SHMAT_FLAGS (0)
204#endif
205
206int main(void)
207{
208 int shmid;
209 unsigned long i;
210 char *shmaddr;
211
212 if ((shmid = shmget(2, LENGTH,
213 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W)) < 0) {
214 perror("shmget");
215 exit(1);
216 }
217 printf("shmid: 0x%x\n", shmid);
218
219 shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
220 if (shmaddr == (char *)-1) {
221 perror("Shared memory attach failure");
222 shmctl(shmid, IPC_RMID, NULL);
223 exit(2);
224 }
225 printf("shmaddr: %p\n", shmaddr);
226
227 dprintf("Starting the writes:\n");
228 for (i = 0; i < LENGTH; i++) {
229 shmaddr[i] = (char)(i);
230 if (!(i % (1024 * 1024)))
231 dprintf(".");
232 }
233 dprintf("\n");
234
235 dprintf("Starting the Check...");
236 for (i = 0; i < LENGTH; i++)
237 if (shmaddr[i] != (char)i)
238 printf("\nIndex %lu mismatched\n", i);
239 dprintf("Done.\n");
240
241 if (shmdt((const void *)shmaddr) != 0) {
242 perror("Detach failure");
243 shmctl(shmid, IPC_RMID, NULL);
244 exit(3);
245 }
246
247 shmctl(shmid, IPC_RMID, NULL);
248
249 return 0;
250}
251
252*******************************************************************
253
254/*
255 * Example of using hugepage memory in a user application using the mmap
256 * system call. Before running this application, make sure that the
257 * administrator has mounted the hugetlbfs filesystem (on some directory
258 * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
259 * example, the app is requesting memory of size 256MB that is backed by
260 * huge pages.
261 *
262 * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
263 * That means the addresses starting with 0x800000... will need to be
264 * specified. Specifying a fixed address is not required on ppc64, i386
265 * or x86_64.
266 */
267#include <stdlib.h>
268#include <stdio.h>
269#include <unistd.h>
270#include <sys/mman.h>
271#include <fcntl.h>
272
273#define FILE_NAME "/mnt/hugepagefile"
274#define LENGTH (256UL*1024*1024)
275#define PROTECTION (PROT_READ | PROT_WRITE)
276
277/* Only ia64 requires this */
278#ifdef __ia64__
279#define ADDR (void *)(0x8000000000000000UL)
280#define FLAGS (MAP_SHARED | MAP_FIXED)
281#else
282#define ADDR (void *)(0x0UL)
283#define FLAGS (MAP_SHARED)
284#endif
285
286void check_bytes(char *addr)
287{
288 printf("First hex is %x\n", *((unsigned int *)addr));
289}
290
291void write_bytes(char *addr)
292{
293 unsigned long i;
294
295 for (i = 0; i < LENGTH; i++)
296 *(addr + i) = (char)i;
297}
298
299void read_bytes(char *addr)
300{
301 unsigned long i;
302
303 check_bytes(addr);
304 for (i = 0; i < LENGTH; i++)
305 if (*(addr + i) != (char)i) {
306 printf("Mismatch at %lu\n", i);
307 break;
308 }
309}
310
311int main(void)
312{
313 void *addr;
314 int fd;
315
316 fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
317 if (fd < 0) {
318 perror("Open failed");
319 exit(1);
320 }
321
322 addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
323 if (addr == MAP_FAILED) {
324 perror("mmap");
325 unlink(FILE_NAME);
326 exit(1);
327 }
328
329 printf("Returned address is %p\n", addr);
330 check_bytes(addr);
331 write_bytes(addr);
332 read_bytes(addr);
333
334 munmap(addr, LENGTH);
335 close(fd);
336 unlink(FILE_NAME);
337
338 return 0;
339}