Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 2 | The intent of this file is to give a brief summary of hugetlbpage support in |
| 3 | the Linux kernel. This support is built on top of multiple page size support |
| 4 | that is provided by most modern architectures. For example, i386 |
| 5 | architecture supports 4K and 4M (2M in PAE mode) page sizes, ia64 |
| 6 | architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M, |
| 7 | 256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical |
| 8 | translations. Typically this is a very scarce resource on processor. |
| 9 | Operating systems try to make best use of limited number of TLB resources. |
| 10 | This optimization is more critical now as bigger and bigger physical memories |
| 11 | (several GBs) are more readily available. |
| 12 | |
| 13 | Users can use the huge page support in Linux kernel by either using the mmap |
| 14 | system call or standard SYSv shared memory system calls (shmget, shmat). |
| 15 | |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 16 | First the Linux kernel needs to be built with the CONFIG_HUGETLBFS |
| 17 | (present under "File systems") and CONFIG_HUGETLB_PAGE (selected |
| 18 | automatically when CONFIG_HUGETLBFS is selected) configuration |
| 19 | options. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | The kernel built with hugepage support should show the number of configured |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 22 | hugepages in the system by running the "cat /proc/meminfo" command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /proc/meminfo also provides information about the total number of hugetlb |
| 25 | pages configured in the kernel. It also displays information about the |
| 26 | number of free hugetlb pages at any time. It also displays information about |
| 27 | the configured hugepage size - this is needed for generating the proper |
| 28 | alignment and size of the arguments to the above system calls. |
| 29 | |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 30 | The output of "cat /proc/meminfo" will have lines like: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | ..... |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 33 | HugePages_Total: vvv |
| 34 | HugePages_Free: www |
| 35 | HugePages_Rsvd: xxx |
| 36 | HugePages_Surp: yyy |
Randy Dunlap | 5e12227 | 2006-04-18 22:21:51 -0700 | [diff] [blame] | 37 | Hugepagesize: zzz kB |
| 38 | |
| 39 | where: |
| 40 | HugePages_Total is the size of the pool of hugepages. |
| 41 | HugePages_Free is the number of hugepages in the pool that are not yet |
| 42 | allocated. |
| 43 | HugePages_Rsvd is short for "reserved," and is the number of hugepages |
| 44 | for which a commitment to allocate from the pool has been made, but no |
| 45 | allocation has yet been made. It's vaguely analogous to overcommit. |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 46 | HugePages_Surp is short for "surplus," and is the number of hugepages in |
| 47 | the pool above the value in /proc/sys/vm/nr_hugepages. The maximum |
| 48 | number of surplus hugepages is controlled by |
| 49 | /proc/sys/vm/nr_overcommit_hugepages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /proc/filesystems should also show a filesystem of type "hugetlbfs" configured |
| 52 | in the kernel. |
| 53 | |
| 54 | /proc/sys/vm/nr_hugepages indicates the current number of configured hugetlb |
| 55 | pages in the kernel. Super user can dynamically request more (or free some |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 56 | pre-configured) hugepages. |
| 57 | The allocation (or deallocation) of hugetlb pages is possible only if there are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | enough physically contiguous free pages in system (freeing of hugepages is |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 59 | possible only if there are enough hugetlb pages free that can be transferred |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | back to regular memory pool). |
| 61 | |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 62 | Pages that are used as hugetlb pages are reserved inside the kernel and cannot |
| 63 | be used for other purposes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | Once the kernel with Hugetlb page support is built and running, a user can |
| 66 | use either the mmap system call or shared memory system calls to start using |
| 67 | the huge pages. It is required that the system administrator preallocate |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 68 | enough memory for huge page purposes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | Use the following command to dynamically allocate/deallocate hugepages: |
| 71 | |
| 72 | echo 20 > /proc/sys/vm/nr_hugepages |
| 73 | |
| 74 | This command will try to configure 20 hugepages in the system. The success |
| 75 | or failure of allocation depends on the amount of physically contiguous |
| 76 | memory that is preset in system at this time. System administrators may want |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 77 | to put this command in one of the local rc init files. This will enable the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | kernel to request huge pages early in the boot process (when the possibility |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 79 | of getting physical contiguous pages is still very high). In either |
| 80 | case, adminstrators will want to verify the number of hugepages actually |
| 81 | allocated by checking the sysctl or meminfo. |
| 82 | |
| 83 | /proc/sys/vm/nr_overcommit_hugepages indicates how large the pool of |
| 84 | hugepages can grow, if more hugepages than /proc/sys/vm/nr_hugepages are |
| 85 | requested by applications. echo'ing any non-zero value into this file |
| 86 | indicates that the hugetlb subsystem is allowed to try to obtain |
| 87 | hugepages from the buddy allocator, if the normal pool is exhausted. As |
| 88 | these surplus hugepages go out of use, they are freed back to the buddy |
| 89 | allocator. |
| 90 | |
| 91 | Caveat: Shrinking the pool via nr_hugepages while a surplus is in effect |
| 92 | will allow the number of surplus huge pages to exceed the overcommit |
| 93 | value, as the pool hugepages (which must have been in use for a surplus |
| 94 | hugepages to be allocated) will become surplus hugepages. As long as |
| 95 | this condition holds, however, no more surplus huge pages will be |
| 96 | allowed on the system until one of the two sysctls are increased |
| 97 | sufficiently, or the surplus huge pages go out of use and are freed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | If the user applications are going to request hugepages using mmap system |
| 100 | call, then it is required that system administrator mount a file system of |
| 101 | type hugetlbfs: |
| 102 | |
Randy Dunlap | e73a75f | 2007-07-15 23:40:52 -0700 | [diff] [blame] | 103 | mount -t hugetlbfs \ |
| 104 | -o uid=<value>,gid=<value>,mode=<value>,size=<value>,nr_inodes=<value> \ |
| 105 | none /mnt/huge |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | This command mounts a (pseudo) filesystem of type hugetlbfs on the directory |
| 108 | /mnt/huge. Any files created on /mnt/huge uses hugepages. The uid and gid |
| 109 | options sets the owner and group of the root of the file system. By default |
| 110 | the uid and gid of the current process are taken. The mode option sets the |
| 111 | mode of root of file system to value & 0777. This value is given in octal. |
| 112 | By default the value 0755 is picked. The size option sets the maximum value of |
| 113 | memory (huge pages) allowed for that filesystem (/mnt/huge). The size is |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 114 | rounded down to HPAGE_SIZE. The option nr_inodes sets the maximum number of |
Randy Dunlap | e73a75f | 2007-07-15 23:40:52 -0700 | [diff] [blame] | 115 | inodes that /mnt/huge can use. If the size or nr_inodes option is not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | provided on command line then no limits are set. For size and nr_inodes |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 117 | options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For |
Randy Dunlap | e73a75f | 2007-07-15 23:40:52 -0700 | [diff] [blame] | 118 | example, size=2K has the same meaning as size=2048. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 120 | While read system calls are supported on files that reside on hugetlb |
| 121 | file systems, write system calls are not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 123 | Regular chown, chgrp, and chmod commands (with right permissions) could be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | used to change the file attributes on hugetlbfs. |
| 125 | |
| 126 | Also, it is important to note that no such mount command is required if the |
| 127 | applications are going to use only shmat/shmget system calls. Users who |
| 128 | wish to use hugetlb page via shared memory segment should be a member of |
| 129 | a supplementary group and system admin needs to configure that gid into |
| 130 | /proc/sys/vm/hugetlb_shm_group. It is possible for same or different |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 131 | applications to use any combination of mmaps and shm* calls, though the |
| 132 | mount of filesystem will be required for using mmap calls. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | ******************************************************************* |
| 135 | |
| 136 | /* |
| 137 | * Example of using hugepage memory in a user application using Sys V shared |
| 138 | * memory system calls. In this example the app is requesting 256MB of |
| 139 | * memory that is backed by huge pages. The application uses the flag |
| 140 | * SHM_HUGETLB in the shmget system call to inform the kernel that it is |
| 141 | * requesting hugepages. |
| 142 | * |
| 143 | * For the ia64 architecture, the Linux kernel reserves Region number 4 for |
| 144 | * hugepages. That means the addresses starting with 0x800000... will need |
| 145 | * to be specified. Specifying a fixed address is not required on ppc64, |
| 146 | * i386 or x86_64. |
| 147 | * |
| 148 | * Note: The default shared memory limit is quite low on many kernels, |
| 149 | * you may need to increase it via: |
| 150 | * |
| 151 | * echo 268435456 > /proc/sys/kernel/shmmax |
| 152 | * |
| 153 | * This will increase the maximum size per shared memory segment to 256MB. |
| 154 | * The other limit that you will hit eventually is shmall which is the |
| 155 | * total amount of shared memory in pages. To set it to 16GB on a system |
| 156 | * with a 4kB pagesize do: |
| 157 | * |
| 158 | * echo 4194304 > /proc/sys/kernel/shmall |
| 159 | */ |
| 160 | #include <stdlib.h> |
| 161 | #include <stdio.h> |
| 162 | #include <sys/types.h> |
| 163 | #include <sys/ipc.h> |
| 164 | #include <sys/shm.h> |
| 165 | #include <sys/mman.h> |
| 166 | |
| 167 | #ifndef SHM_HUGETLB |
| 168 | #define SHM_HUGETLB 04000 |
| 169 | #endif |
| 170 | |
| 171 | #define LENGTH (256UL*1024*1024) |
| 172 | |
| 173 | #define dprintf(x) printf(x) |
| 174 | |
| 175 | /* Only ia64 requires this */ |
| 176 | #ifdef __ia64__ |
| 177 | #define ADDR (void *)(0x8000000000000000UL) |
| 178 | #define SHMAT_FLAGS (SHM_RND) |
| 179 | #else |
| 180 | #define ADDR (void *)(0x0UL) |
| 181 | #define SHMAT_FLAGS (0) |
| 182 | #endif |
| 183 | |
| 184 | int main(void) |
| 185 | { |
| 186 | int shmid; |
| 187 | unsigned long i; |
| 188 | char *shmaddr; |
| 189 | |
| 190 | if ((shmid = shmget(2, LENGTH, |
| 191 | SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W)) < 0) { |
| 192 | perror("shmget"); |
| 193 | exit(1); |
| 194 | } |
| 195 | printf("shmid: 0x%x\n", shmid); |
| 196 | |
| 197 | shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS); |
| 198 | if (shmaddr == (char *)-1) { |
| 199 | perror("Shared memory attach failure"); |
| 200 | shmctl(shmid, IPC_RMID, NULL); |
| 201 | exit(2); |
| 202 | } |
| 203 | printf("shmaddr: %p\n", shmaddr); |
| 204 | |
| 205 | dprintf("Starting the writes:\n"); |
| 206 | for (i = 0; i < LENGTH; i++) { |
| 207 | shmaddr[i] = (char)(i); |
| 208 | if (!(i % (1024 * 1024))) |
| 209 | dprintf("."); |
| 210 | } |
| 211 | dprintf("\n"); |
| 212 | |
| 213 | dprintf("Starting the Check..."); |
| 214 | for (i = 0; i < LENGTH; i++) |
| 215 | if (shmaddr[i] != (char)i) |
| 216 | printf("\nIndex %lu mismatched\n", i); |
| 217 | dprintf("Done.\n"); |
| 218 | |
| 219 | if (shmdt((const void *)shmaddr) != 0) { |
| 220 | perror("Detach failure"); |
| 221 | shmctl(shmid, IPC_RMID, NULL); |
| 222 | exit(3); |
| 223 | } |
| 224 | |
| 225 | shmctl(shmid, IPC_RMID, NULL); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | ******************************************************************* |
| 231 | |
| 232 | /* |
| 233 | * Example of using hugepage memory in a user application using the mmap |
| 234 | * system call. Before running this application, make sure that the |
| 235 | * administrator has mounted the hugetlbfs filesystem (on some directory |
| 236 | * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this |
| 237 | * example, the app is requesting memory of size 256MB that is backed by |
| 238 | * huge pages. |
| 239 | * |
| 240 | * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages. |
| 241 | * That means the addresses starting with 0x800000... will need to be |
| 242 | * specified. Specifying a fixed address is not required on ppc64, i386 |
| 243 | * or x86_64. |
| 244 | */ |
| 245 | #include <stdlib.h> |
| 246 | #include <stdio.h> |
| 247 | #include <unistd.h> |
| 248 | #include <sys/mman.h> |
| 249 | #include <fcntl.h> |
| 250 | |
| 251 | #define FILE_NAME "/mnt/hugepagefile" |
| 252 | #define LENGTH (256UL*1024*1024) |
| 253 | #define PROTECTION (PROT_READ | PROT_WRITE) |
| 254 | |
| 255 | /* Only ia64 requires this */ |
| 256 | #ifdef __ia64__ |
| 257 | #define ADDR (void *)(0x8000000000000000UL) |
| 258 | #define FLAGS (MAP_SHARED | MAP_FIXED) |
| 259 | #else |
| 260 | #define ADDR (void *)(0x0UL) |
| 261 | #define FLAGS (MAP_SHARED) |
| 262 | #endif |
| 263 | |
| 264 | void check_bytes(char *addr) |
| 265 | { |
| 266 | printf("First hex is %x\n", *((unsigned int *)addr)); |
| 267 | } |
| 268 | |
| 269 | void write_bytes(char *addr) |
| 270 | { |
| 271 | unsigned long i; |
| 272 | |
| 273 | for (i = 0; i < LENGTH; i++) |
| 274 | *(addr + i) = (char)i; |
| 275 | } |
| 276 | |
| 277 | void read_bytes(char *addr) |
| 278 | { |
| 279 | unsigned long i; |
| 280 | |
| 281 | check_bytes(addr); |
| 282 | for (i = 0; i < LENGTH; i++) |
| 283 | if (*(addr + i) != (char)i) { |
| 284 | printf("Mismatch at %lu\n", i); |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | int main(void) |
| 290 | { |
| 291 | void *addr; |
| 292 | int fd; |
| 293 | |
| 294 | fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755); |
| 295 | if (fd < 0) { |
| 296 | perror("Open failed"); |
| 297 | exit(1); |
| 298 | } |
| 299 | |
| 300 | addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0); |
| 301 | if (addr == MAP_FAILED) { |
| 302 | perror("mmap"); |
| 303 | unlink(FILE_NAME); |
| 304 | exit(1); |
| 305 | } |
| 306 | |
| 307 | printf("Returned address is %p\n", addr); |
| 308 | check_bytes(addr); |
| 309 | write_bytes(addr); |
| 310 | read_bytes(addr); |
| 311 | |
| 312 | munmap(addr, LENGTH); |
| 313 | close(fd); |
| 314 | unlink(FILE_NAME); |
| 315 | |
| 316 | return 0; |
| 317 | } |