hugetlbpage.txt 14 KB

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