blob: 1b9bcd1fe98b3867d60a50241bbee7ab84099608 [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
16First the Linux kernel needs to be built with CONFIG_HUGETLB_PAGE (present
17under Processor types and feature) and CONFIG_HUGETLBFS (present under file
18system option on config menu) config options.
19
20The kernel built with hugepage support should show the number of configured
21hugepages in the system by running the "cat /proc/meminfo" command.
22
23/proc/meminfo also provides information about the total number of hugetlb
24pages configured in the kernel. It also displays information about the
25number of free hugetlb pages at any time. It also displays information about
26the configured hugepage size - this is needed for generating the proper
27alignment and size of the arguments to the above system calls.
28
29The output of "cat /proc/meminfo" will have output like:
30
31.....
32HugePages_Total: xxx
33HugePages_Free: yyy
34Hugepagesize: zzz KB
35
36/proc/filesystems should also show a filesystem of type "hugetlbfs" configured
37in the kernel.
38
39/proc/sys/vm/nr_hugepages indicates the current number of configured hugetlb
40pages in the kernel. Super user can dynamically request more (or free some
41pre-configured) hugepages.
42The allocation( or deallocation) of hugetlb pages is posible only if there are
43enough physically contiguous free pages in system (freeing of hugepages is
44possible only if there are enough hugetlb pages free that can be transfered
45back to regular memory pool).
46
47Pages that are used as hugetlb pages are reserved inside the kernel and can
48not be used for other purposes.
49
50Once the kernel with Hugetlb page support is built and running, a user can
51use either the mmap system call or shared memory system calls to start using
52the huge pages. It is required that the system administrator preallocate
53enough memory for huge page purposes.
54
55Use the following command to dynamically allocate/deallocate hugepages:
56
57 echo 20 > /proc/sys/vm/nr_hugepages
58
59This command will try to configure 20 hugepages in the system. The success
60or failure of allocation depends on the amount of physically contiguous
61memory that is preset in system at this time. System administrators may want
62to put this command in one of the local rc init file. This will enable the
63kernel to request huge pages early in the boot process (when the possibility
64of getting physical contiguous pages is still very high).
65
66If the user applications are going to request hugepages using mmap system
67call, then it is required that system administrator mount a file system of
68type hugetlbfs:
69
70 mount none /mnt/huge -t hugetlbfs <uid=value> <gid=value> <mode=value>
71 <size=value> <nr_inodes=value>
72
73This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
74/mnt/huge. Any files created on /mnt/huge uses hugepages. The uid and gid
75options sets the owner and group of the root of the file system. By default
76the uid and gid of the current process are taken. The mode option sets the
77mode of root of file system to value & 0777. This value is given in octal.
78By default the value 0755 is picked. The size option sets the maximum value of
79memory (huge pages) allowed for that filesystem (/mnt/huge). The size is
80rounded down to HPAGE_SIZE. The option nr_inode sets the maximum number of
81inodes that /mnt/huge can use. If the size or nr_inode options are not
82provided on command line then no limits are set. For size and nr_inodes
83options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For
84example, size=2K has the same meaning as size=2048. An example is given at
85the end of this document.
86
87read and write system calls are not supported on files that reside on hugetlb
88file systems.
89
90A regular chown, chgrp and chmod commands (with right permissions) could be
91used to change the file attributes on hugetlbfs.
92
93Also, it is important to note that no such mount command is required if the
94applications are going to use only shmat/shmget system calls. Users who
95wish to use hugetlb page via shared memory segment should be a member of
96a supplementary group and system admin needs to configure that gid into
97/proc/sys/vm/hugetlb_shm_group. It is possible for same or different
98applications to use any combination of mmaps and shm* calls. Though the
99mount of filesystem will be required for using mmaps.
100
101*******************************************************************
102
103/*
104 * Example of using hugepage memory in a user application using Sys V shared
105 * memory system calls. In this example the app is requesting 256MB of
106 * memory that is backed by huge pages. The application uses the flag
107 * SHM_HUGETLB in the shmget system call to inform the kernel that it is
108 * requesting hugepages.
109 *
110 * For the ia64 architecture, the Linux kernel reserves Region number 4 for
111 * hugepages. That means the addresses starting with 0x800000... will need
112 * to be specified. Specifying a fixed address is not required on ppc64,
113 * i386 or x86_64.
114 *
115 * Note: The default shared memory limit is quite low on many kernels,
116 * you may need to increase it via:
117 *
118 * echo 268435456 > /proc/sys/kernel/shmmax
119 *
120 * This will increase the maximum size per shared memory segment to 256MB.
121 * The other limit that you will hit eventually is shmall which is the
122 * total amount of shared memory in pages. To set it to 16GB on a system
123 * with a 4kB pagesize do:
124 *
125 * echo 4194304 > /proc/sys/kernel/shmall
126 */
127#include <stdlib.h>
128#include <stdio.h>
129#include <sys/types.h>
130#include <sys/ipc.h>
131#include <sys/shm.h>
132#include <sys/mman.h>
133
134#ifndef SHM_HUGETLB
135#define SHM_HUGETLB 04000
136#endif
137
138#define LENGTH (256UL*1024*1024)
139
140#define dprintf(x) printf(x)
141
142/* Only ia64 requires this */
143#ifdef __ia64__
144#define ADDR (void *)(0x8000000000000000UL)
145#define SHMAT_FLAGS (SHM_RND)
146#else
147#define ADDR (void *)(0x0UL)
148#define SHMAT_FLAGS (0)
149#endif
150
151int main(void)
152{
153 int shmid;
154 unsigned long i;
155 char *shmaddr;
156
157 if ((shmid = shmget(2, LENGTH,
158 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W)) < 0) {
159 perror("shmget");
160 exit(1);
161 }
162 printf("shmid: 0x%x\n", shmid);
163
164 shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
165 if (shmaddr == (char *)-1) {
166 perror("Shared memory attach failure");
167 shmctl(shmid, IPC_RMID, NULL);
168 exit(2);
169 }
170 printf("shmaddr: %p\n", shmaddr);
171
172 dprintf("Starting the writes:\n");
173 for (i = 0; i < LENGTH; i++) {
174 shmaddr[i] = (char)(i);
175 if (!(i % (1024 * 1024)))
176 dprintf(".");
177 }
178 dprintf("\n");
179
180 dprintf("Starting the Check...");
181 for (i = 0; i < LENGTH; i++)
182 if (shmaddr[i] != (char)i)
183 printf("\nIndex %lu mismatched\n", i);
184 dprintf("Done.\n");
185
186 if (shmdt((const void *)shmaddr) != 0) {
187 perror("Detach failure");
188 shmctl(shmid, IPC_RMID, NULL);
189 exit(3);
190 }
191
192 shmctl(shmid, IPC_RMID, NULL);
193
194 return 0;
195}
196
197*******************************************************************
198
199/*
200 * Example of using hugepage memory in a user application using the mmap
201 * system call. Before running this application, make sure that the
202 * administrator has mounted the hugetlbfs filesystem (on some directory
203 * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
204 * example, the app is requesting memory of size 256MB that is backed by
205 * huge pages.
206 *
207 * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
208 * That means the addresses starting with 0x800000... will need to be
209 * specified. Specifying a fixed address is not required on ppc64, i386
210 * or x86_64.
211 */
212#include <stdlib.h>
213#include <stdio.h>
214#include <unistd.h>
215#include <sys/mman.h>
216#include <fcntl.h>
217
218#define FILE_NAME "/mnt/hugepagefile"
219#define LENGTH (256UL*1024*1024)
220#define PROTECTION (PROT_READ | PROT_WRITE)
221
222/* Only ia64 requires this */
223#ifdef __ia64__
224#define ADDR (void *)(0x8000000000000000UL)
225#define FLAGS (MAP_SHARED | MAP_FIXED)
226#else
227#define ADDR (void *)(0x0UL)
228#define FLAGS (MAP_SHARED)
229#endif
230
231void check_bytes(char *addr)
232{
233 printf("First hex is %x\n", *((unsigned int *)addr));
234}
235
236void write_bytes(char *addr)
237{
238 unsigned long i;
239
240 for (i = 0; i < LENGTH; i++)
241 *(addr + i) = (char)i;
242}
243
244void read_bytes(char *addr)
245{
246 unsigned long i;
247
248 check_bytes(addr);
249 for (i = 0; i < LENGTH; i++)
250 if (*(addr + i) != (char)i) {
251 printf("Mismatch at %lu\n", i);
252 break;
253 }
254}
255
256int main(void)
257{
258 void *addr;
259 int fd;
260
261 fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
262 if (fd < 0) {
263 perror("Open failed");
264 exit(1);
265 }
266
267 addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
268 if (addr == MAP_FAILED) {
269 perror("mmap");
270 unlink(FILE_NAME);
271 exit(1);
272 }
273
274 printf("Returned address is %p\n", addr);
275 check_bytes(addr);
276 write_bytes(addr);
277 read_bytes(addr);
278
279 munmap(addr, LENGTH);
280 close(fd);
281 unlink(FILE_NAME);
282
283 return 0;
284}