Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 1 | .. _hugetlbpage: |
| 2 | |
| 3 | ============= |
| 4 | HugeTLB Pages |
| 5 | ============= |
| 6 | |
| 7 | Overview |
| 8 | ======== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
| 10 | The intent of this file is to give a brief summary of hugetlbpage support in |
| 11 | the Linux kernel. This support is built on top of multiple page size support |
Masanari Iida | c0d7305 | 2014-11-07 00:31:15 +0900 | [diff] [blame] | 12 | that is provided by most modern architectures. For example, x86 CPUs normally |
| 13 | support 4K and 2M (1G if architecturally supported) page sizes, ia64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M, |
| 15 | 256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical |
| 16 | translations. Typically this is a very scarce resource on processor. |
| 17 | Operating systems try to make best use of limited number of TLB resources. |
| 18 | This optimization is more critical now as bigger and bigger physical memories |
| 19 | (several GBs) are more readily available. |
| 20 | |
| 21 | Users can use the huge page support in Linux kernel by either using the mmap |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 22 | system call or standard SYSV shared memory system calls (shmget, shmat). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 24 | First the Linux kernel needs to be built with the CONFIG_HUGETLBFS |
| 25 | (present under "File systems") and CONFIG_HUGETLB_PAGE (selected |
| 26 | automatically when CONFIG_HUGETLBFS is selected) configuration |
| 27 | options. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 29 | The ``/proc/meminfo`` file provides information about the total number of |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 30 | persistent hugetlb pages in the kernel's huge page pool. It also displays |
Roman Gushchin | fcb2b0c | 2018-01-31 16:16:22 -0800 | [diff] [blame] | 31 | default huge page size and information about the number of free, reserved |
| 32 | and surplus huge pages in the pool of huge pages of default size. |
| 33 | The huge page size is needed for generating the proper alignment and |
| 34 | size of the arguments to system calls that map huge page regions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 36 | The output of ``cat /proc/meminfo`` will include lines like:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 38 | HugePages_Total: uuu |
| 39 | HugePages_Free: vvv |
| 40 | HugePages_Rsvd: www |
| 41 | HugePages_Surp: xxx |
| 42 | Hugepagesize: yyy kB |
| 43 | Hugetlb: zzz kB |
Randy Dunlap | 5e12227 | 2006-04-18 22:21:51 -0700 | [diff] [blame] | 44 | |
| 45 | where: |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 46 | |
| 47 | HugePages_Total |
| 48 | is the size of the pool of huge pages. |
| 49 | HugePages_Free |
| 50 | is the number of huge pages in the pool that are not yet |
| 51 | allocated. |
| 52 | HugePages_Rsvd |
| 53 | is short for "reserved," and is the number of huge pages for |
| 54 | which a commitment to allocate from the pool has been made, |
| 55 | but no allocation has yet been made. Reserved huge pages |
| 56 | guarantee that an application will be able to allocate a |
| 57 | huge page from the pool of huge pages at fault time. |
| 58 | HugePages_Surp |
| 59 | is short for "surplus," and is the number of huge pages in |
| 60 | the pool above the value in ``/proc/sys/vm/nr_hugepages``. The |
| 61 | maximum number of surplus huge pages is controlled by |
| 62 | ``/proc/sys/vm/nr_overcommit_hugepages``. |
Muchun Song | ad2fa37 | 2021-06-30 18:47:21 -0700 | [diff] [blame] | 63 | Note: When the feature of freeing unused vmemmap pages associated |
| 64 | with each hugetlb page is enabled, the number of surplus huge pages |
| 65 | may be temporarily larger than the maximum number of surplus huge |
| 66 | pages when the system is under memory pressure. |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 67 | Hugepagesize |
| 68 | is the default hugepage size (in Kb). |
| 69 | Hugetlb |
| 70 | is the total amount of memory (in kB), consumed by huge |
| 71 | pages of all sizes. |
| 72 | If huge pages of different sizes are in use, this number |
| 73 | will exceed HugePages_Total \* Hugepagesize. To get more |
| 74 | detailed information, please, refer to |
| 75 | ``/sys/kernel/mm/hugepages`` (described below). |
Roman Gushchin | fcb2b0c | 2018-01-31 16:16:22 -0800 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 78 | ``/proc/filesystems`` should also show a filesystem of type "hugetlbfs" |
| 79 | configured in the kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 81 | ``/proc/sys/vm/nr_hugepages`` indicates the current number of "persistent" huge |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 82 | pages in the kernel's huge page pool. "Persistent" huge pages will be |
| 83 | returned to the huge page pool when freed by a task. A user with root |
| 84 | privileges can dynamically allocate more or free some persistent huge pages |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 85 | by increasing or decreasing the value of ``nr_hugepages``. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Muchun Song | ad2fa37 | 2021-06-30 18:47:21 -0700 | [diff] [blame] | 87 | Note: When the feature of freeing unused vmemmap pages associated with each |
| 88 | hugetlb page is enabled, we can fail to free the huge pages triggered by |
| 89 | the user when ths system is under memory pressure. Please try again later. |
| 90 | |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 91 | Pages that are used as huge pages are reserved inside the kernel and cannot |
| 92 | be used for other purposes. Huge pages cannot be swapped out under |
| 93 | memory pressure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 95 | Once a number of huge pages have been pre-allocated to the kernel huge page |
| 96 | pool, a user with appropriate privilege can use either the mmap system call |
| 97 | or shared memory system calls to use the huge pages. See the discussion of |
Mike Rapoport | fde7917 | 2018-04-18 11:07:44 +0300 | [diff] [blame] | 98 | :ref:`Using Huge Pages <using_huge_pages>`, below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 100 | The administrator can allocate persistent huge pages on the kernel boot |
| 101 | command line by specifying the "hugepages=N" parameter, where 'N' = the |
| 102 | number of huge pages requested. This is the most reliable method of |
| 103 | allocating huge pages as memory has not yet become fragmented. |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 104 | |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 105 | Some platforms support multiple huge page sizes. To allocate huge pages |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 106 | of a specific size, one must precede the huge pages boot command parameters |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 107 | with a huge page size selection parameter "hugepagesz=<size>". <size> must |
| 108 | be specified in bytes with optional scale suffix [kKmMgG]. The default huge |
| 109 | page size may be selected with the "default_hugepagesz=<size>" boot parameter. |
| 110 | |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 111 | Hugetlb boot command line parameter semantics |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 112 | |
| 113 | hugepagesz |
| 114 | Specify a huge page size. Used in conjunction with hugepages |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 115 | parameter to preallocate a number of huge pages of the specified |
| 116 | size. Hence, hugepagesz and hugepages are typically specified in |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 117 | pairs such as:: |
| 118 | |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 119 | hugepagesz=2M hugepages=512 |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 120 | |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 121 | hugepagesz can only be specified once on the command line for a |
| 122 | specific huge page size. Valid huge page sizes are architecture |
| 123 | dependent. |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 124 | hugepages |
| 125 | Specify the number of huge pages to preallocate. This typically |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 126 | follows a valid hugepagesz or default_hugepagesz parameter. However, |
| 127 | if hugepages is the first or only hugetlb command line parameter it |
| 128 | implicitly specifies the number of huge pages of default size to |
| 129 | allocate. If the number of huge pages of default size is implicitly |
| 130 | specified, it can not be overwritten by a hugepagesz,hugepages |
Zhenguo Yao | b538908 | 2021-11-05 13:43:28 -0700 | [diff] [blame] | 131 | parameter pair for the default size. This parameter also has a |
| 132 | node format. The node format specifies the number of huge pages |
| 133 | to allocate on specific nodes. |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 134 | |
| 135 | For example, on an architecture with 2M default huge page size:: |
| 136 | |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 137 | hugepages=256 hugepagesz=2M hugepages=512 |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 138 | |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 139 | will result in 256 2M huge pages being allocated and a warning message |
| 140 | indicating that the hugepages=512 parameter is ignored. If a hugepages |
| 141 | parameter is preceded by an invalid hugepagesz parameter, it will |
| 142 | be ignored. |
Zhenguo Yao | b538908 | 2021-11-05 13:43:28 -0700 | [diff] [blame] | 143 | |
| 144 | Node format example:: |
| 145 | |
| 146 | hugepagesz=2M hugepages=0:1,1:2 |
| 147 | |
| 148 | It will allocate 1 2M hugepage on node0 and 2 2M hugepages on node1. |
| 149 | If the node number is invalid, the parameter will be ignored. |
| 150 | |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 151 | default_hugepagesz |
Baoquan He | 540809b | 2020-10-13 16:56:17 -0700 | [diff] [blame] | 152 | Specify the default huge page size. This parameter can |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 153 | only be specified once on the command line. default_hugepagesz can |
| 154 | optionally be followed by the hugepages parameter to preallocate a |
| 155 | specific number of huge pages of default size. The number of default |
| 156 | sized huge pages to preallocate can also be implicitly specified as |
| 157 | mentioned in the hugepages section above. Therefore, on an |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 158 | architecture with 2M default huge page size:: |
| 159 | |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 160 | hugepages=256 |
| 161 | default_hugepagesz=2M hugepages=256 |
| 162 | hugepages=256 default_hugepagesz=2M |
Mauro Carvalho Chehab | 72a3e3e | 2020-06-23 09:09:06 +0200 | [diff] [blame] | 163 | |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 164 | will all result in 256 2M huge pages being allocated. Valid default |
| 165 | huge page size is architecture dependent. |
Muchun Song | e9fdff8 | 2021-06-30 18:47:25 -0700 | [diff] [blame] | 166 | hugetlb_free_vmemmap |
| 167 | When CONFIG_HUGETLB_PAGE_FREE_VMEMMAP is set, this enables freeing |
| 168 | unused vmemmap pages associated with each HugeTLB page. |
Mike Kravetz | 282f421 | 2020-06-03 16:00:46 -0700 | [diff] [blame] | 169 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 170 | When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages`` |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 171 | indicates the current number of pre-allocated huge pages of the default size. |
| 172 | Thus, one can use the following command to dynamically allocate/deallocate |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 173 | default sized persistent huge pages:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | echo 20 > /proc/sys/vm/nr_hugepages |
| 176 | |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 177 | This command will try to adjust the number of default sized huge pages in the |
| 178 | huge page pool to 20, allocating or freeing huge pages, as required. |
| 179 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 180 | On a NUMA platform, the kernel will attempt to distribute the huge page pool |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 181 | over all the set of allowed nodes specified by the NUMA memory policy of the |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 182 | task that modifies ``nr_hugepages``. The default for the allowed nodes--when the |
Lee Schermerhorn | 9b5e5d0 | 2009-12-14 17:58:32 -0800 | [diff] [blame] | 183 | task has default memory policy--is all on-line nodes with memory. Allowed |
| 184 | nodes with insufficient available, contiguous memory for a huge page will be |
Mike Rapoport | fde7917 | 2018-04-18 11:07:44 +0300 | [diff] [blame] | 185 | silently skipped when allocating persistent huge pages. See the |
| 186 | :ref:`discussion below <mem_policy_and_hp_alloc>` |
| 187 | of the interaction of task memory policy, cpusets and per node attributes |
Lee Schermerhorn | 9b5e5d0 | 2009-12-14 17:58:32 -0800 | [diff] [blame] | 188 | with the allocation and freeing of persistent huge pages. |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 189 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 190 | The success or failure of huge page allocation depends on the amount of |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 191 | physically contiguous memory that is present in system at the time of the |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 192 | allocation attempt. If the kernel is unable to allocate huge pages from |
| 193 | some nodes in a NUMA system, it will attempt to make up the difference by |
| 194 | allocating extra pages on other nodes with sufficient available contiguous |
| 195 | memory, if any. |
| 196 | |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 197 | System administrators may want to put this command in one of the local rc |
| 198 | init files. This will enable the kernel to allocate huge pages early in |
| 199 | the boot process when the possibility of getting physical contiguous pages |
| 200 | is still very high. Administrators can verify the number of huge pages |
| 201 | actually allocated by checking the sysctl or meminfo. To check the per node |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 202 | distribution of huge pages in a NUMA system, use:: |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 203 | |
| 204 | cat /sys/devices/system/node/node*/meminfo | fgrep Huge |
| 205 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 206 | ``/proc/sys/vm/nr_overcommit_hugepages`` specifies how large the pool of |
| 207 | huge pages can grow, if more huge pages than ``/proc/sys/vm/nr_hugepages`` are |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 208 | requested by applications. Writing any non-zero value into this file |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 209 | indicates that the hugetlb subsystem is allowed to try to obtain that |
| 210 | number of "surplus" huge pages from the kernel's normal page pool, when the |
| 211 | persistent huge page pool is exhausted. As these surplus huge pages become |
| 212 | unused, they are freed back to the kernel's normal page pool. |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 213 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 214 | When increasing the huge page pool size via ``nr_hugepages``, any existing |
| 215 | surplus pages will first be promoted to persistent huge pages. Then, additional |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 216 | huge pages will be allocated, if necessary and if possible, to fulfill |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 217 | the new persistent huge page pool size. |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 218 | |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 219 | The administrator may shrink the pool of persistent huge pages for |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 220 | the default huge page size by setting the ``nr_hugepages`` sysctl to a |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 221 | smaller value. The kernel will attempt to balance the freeing of huge pages |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 222 | across all nodes in the memory policy of the task modifying ``nr_hugepages``. |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 223 | Any free huge pages on the selected nodes will be freed back to the kernel's |
| 224 | normal page pool. |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 225 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 226 | Caveat: Shrinking the persistent huge page pool via ``nr_hugepages`` such that |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 227 | it becomes less than the number of huge pages in use will convert the balance |
| 228 | of the in-use huge pages to surplus huge pages. This will occur even if |
Mike Rapoport | fde7917 | 2018-04-18 11:07:44 +0300 | [diff] [blame] | 229 | the number of surplus pages would exceed the overcommit value. As long as |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 230 | this condition holds--that is, until ``nr_hugepages+nr_overcommit_hugepages`` is |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 231 | increased sufficiently, or the surplus huge pages go out of use and are freed-- |
| 232 | no more surplus huge pages will be allowed to be allocated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 234 | With support for multiple huge page pools at run-time available, much of |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 235 | the huge page userspace interface in ``/proc/sys/vm`` has been duplicated in |
| 236 | sysfs. |
| 237 | The ``/proc`` interfaces discussed above have been retained for backwards |
| 238 | compatibility. The root huge page control directory in sysfs is:: |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 239 | |
| 240 | /sys/kernel/mm/hugepages |
| 241 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 242 | For each huge page size supported by the running kernel, a subdirectory |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 243 | will exist, of the form:: |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 244 | |
| 245 | hugepages-${size}kB |
| 246 | |
Mike Kravetz | 79dfc69 | 2021-11-05 13:41:20 -0700 | [diff] [blame] | 247 | Inside each of these directories, the set of files contained in ``/proc`` |
| 248 | will exist. In addition, two additional interfaces for demoting huge |
| 249 | pages may exist:: |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 250 | |
Mike Kravetz | 79dfc69 | 2021-11-05 13:41:20 -0700 | [diff] [blame] | 251 | demote |
| 252 | demote_size |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 253 | nr_hugepages |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 254 | nr_hugepages_mempolicy |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 255 | nr_overcommit_hugepages |
| 256 | free_hugepages |
| 257 | resv_hugepages |
| 258 | surplus_hugepages |
| 259 | |
Mike Kravetz | 79dfc69 | 2021-11-05 13:41:20 -0700 | [diff] [blame] | 260 | The demote interfaces provide the ability to split a huge page into |
| 261 | smaller huge pages. For example, the x86 architecture supports both |
| 262 | 1GB and 2MB huge pages sizes. A 1GB huge page can be split into 512 |
| 263 | 2MB huge pages. Demote interfaces are not available for the smallest |
| 264 | huge page size. The demote interfaces are: |
| 265 | |
| 266 | demote_size |
| 267 | is the size of demoted pages. When a page is demoted a corresponding |
| 268 | number of huge pages of demote_size will be created. By default, |
| 269 | demote_size is set to the next smaller huge page size. If there are |
| 270 | multiple smaller huge page sizes, demote_size can be set to any of |
| 271 | these smaller sizes. Only huge page sizes less than the current huge |
| 272 | pages size are allowed. |
| 273 | |
| 274 | demote |
| 275 | is used to demote a number of huge pages. A user with root privileges |
| 276 | can write to this file. It may not be possible to demote the |
| 277 | requested number of huge pages. To determine how many pages were |
| 278 | actually demoted, compare the value of nr_hugepages before and after |
| 279 | writing to the demote interface. demote is a write only interface. |
| 280 | |
| 281 | The interfaces which are the same as in ``/proc`` (all except demote and |
| 282 | demote_size) function as described above for the default huge page-sized case. |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 283 | |
Mike Rapoport | fde7917 | 2018-04-18 11:07:44 +0300 | [diff] [blame] | 284 | .. _mem_policy_and_hp_alloc: |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 285 | |
| 286 | Interaction of Task Memory Policy with Huge Page Allocation/Freeing |
Davidlohr Bueso | 15610c8 | 2013-09-11 14:21:48 -0700 | [diff] [blame] | 287 | =================================================================== |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 288 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 289 | Whether huge pages are allocated and freed via the ``/proc`` interface or |
| 290 | the ``/sysfs`` interface using the ``nr_hugepages_mempolicy`` attribute, the |
| 291 | NUMA nodes from which huge pages are allocated or freed are controlled by the |
| 292 | NUMA memory policy of the task that modifies the ``nr_hugepages_mempolicy`` |
| 293 | sysctl or attribute. When the ``nr_hugepages`` attribute is used, mempolicy |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 294 | is ignored. |
| 295 | |
| 296 | The recommended method to allocate or free huge pages to/from the kernel |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 297 | huge page pool, using the ``nr_hugepages`` example above, is:: |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 298 | |
| 299 | numactl --interleave <node-list> echo 20 \ |
| 300 | >/proc/sys/vm/nr_hugepages_mempolicy |
| 301 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 302 | or, more succinctly:: |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 303 | |
| 304 | numactl -m <node-list> echo 20 >/proc/sys/vm/nr_hugepages_mempolicy |
| 305 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 306 | This will allocate or free ``abs(20 - nr_hugepages)`` to or from the nodes |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 307 | specified in <node-list>, depending on whether number of persistent huge pages |
| 308 | is initially less than or greater than 20, respectively. No huge pages will be |
| 309 | allocated nor freed on any node not included in the specified <node-list>. |
| 310 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 311 | When adjusting the persistent hugepage count via ``nr_hugepages_mempolicy``, any |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 312 | memory policy mode--bind, preferred, local or interleave--may be used. The |
| 313 | resulting effect on persistent huge page allocation is as follows: |
| 314 | |
Mike Rapoport | e27a20f | 2018-04-18 11:07:50 +0300 | [diff] [blame] | 315 | #. Regardless of mempolicy mode [see |
Mike Rapoport | 3ecf53e | 2018-05-08 10:02:10 +0300 | [diff] [blame] | 316 | :ref:`Documentation/admin-guide/mm/numa_memory_policy.rst <numa_memory_policy>`], |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 317 | persistent huge pages will be distributed across the node or nodes |
| 318 | specified in the mempolicy as if "interleave" had been specified. |
| 319 | However, if a node in the policy does not contain sufficient contiguous |
| 320 | memory for a huge page, the allocation will not "fallback" to the nearest |
| 321 | neighbor node with sufficient contiguous memory. To do this would cause |
| 322 | undesirable imbalance in the distribution of the huge page pool, or |
| 323 | possibly, allocation of persistent huge pages on nodes not allowed by |
| 324 | the task's memory policy. |
| 325 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 326 | #. One or more nodes may be specified with the bind or interleave policy. |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 327 | If more than one node is specified with the preferred policy, only the |
| 328 | lowest numeric id will be used. Local policy will select the node where |
| 329 | the task is running at the time the nodes_allowed mask is constructed. |
| 330 | For local policy to be deterministic, the task must be bound to a cpu or |
| 331 | cpus in a single node. Otherwise, the task could be migrated to some |
| 332 | other node at any time after launch and the resulting node will be |
| 333 | indeterminate. Thus, local policy is not very useful for this purpose. |
| 334 | Any of the other mempolicy modes may be used to specify a single node. |
| 335 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 336 | #. The nodes allowed mask will be derived from any non-default task mempolicy, |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 337 | whether this policy was set explicitly by the task itself or one of its |
| 338 | ancestors, such as numactl. This means that if the task is invoked from a |
| 339 | shell with non-default policy, that policy will be used. One can specify a |
| 340 | node list of "all" with numactl --interleave or --membind [-m] to achieve |
| 341 | interleaving over all nodes in the system or cpuset. |
| 342 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 343 | #. Any task mempolicy specified--e.g., using numactl--will be constrained by |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 344 | the resource limits of any cpuset in which the task runs. Thus, there will |
| 345 | be no way for a task with non-default policy running in a cpuset with a |
| 346 | subset of the system nodes to allocate huge pages outside the cpuset |
| 347 | without first moving to a cpuset that contains all of the desired nodes. |
| 348 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 349 | #. Boot-time huge page allocation attempts to distribute the requested number |
Lee Schermerhorn | 9b5e5d0 | 2009-12-14 17:58:32 -0800 | [diff] [blame] | 350 | of huge pages over all on-lines nodes with memory. |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 351 | |
| 352 | Per Node Hugepages Attributes |
Davidlohr Bueso | 15610c8 | 2013-09-11 14:21:48 -0700 | [diff] [blame] | 353 | ============================= |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 354 | |
| 355 | A subset of the contents of the root huge page control directory in sysfs, |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 356 | described above, will be replicated under each the system device of each |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 357 | NUMA node with memory in:: |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 358 | |
| 359 | /sys/devices/system/node/node[0-9]*/hugepages/ |
| 360 | |
| 361 | Under this directory, the subdirectory for each supported huge page size |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 362 | contains the following attribute files:: |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 363 | |
| 364 | nr_hugepages |
| 365 | free_hugepages |
| 366 | surplus_hugepages |
| 367 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 368 | The free\_' and surplus\_' attribute files are read-only. They return the number |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 369 | of free and surplus [overcommitted] huge pages, respectively, on the parent |
| 370 | node. |
| 371 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 372 | The ``nr_hugepages`` attribute returns the total number of huge pages on the |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 373 | specified node. When this attribute is written, the number of persistent huge |
| 374 | pages on the parent node will be adjusted to the specified value, if sufficient |
| 375 | resources exist, regardless of the task's mempolicy or cpuset constraints. |
| 376 | |
| 377 | Note that the number of overcommit and reserve pages remain global quantities, |
| 378 | as we don't know until fault time, when the faulting task's mempolicy is |
| 379 | applied, from which node the huge page allocation will be attempted. |
| 380 | |
Mike Rapoport | fde7917 | 2018-04-18 11:07:44 +0300 | [diff] [blame] | 381 | .. _using_huge_pages: |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 382 | |
| 383 | Using Huge Pages |
Davidlohr Bueso | 15610c8 | 2013-09-11 14:21:48 -0700 | [diff] [blame] | 384 | ================ |
Lee Schermerhorn | 267b4c2 | 2009-12-14 17:58:30 -0800 | [diff] [blame] | 385 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 386 | If the user applications are going to request huge pages using mmap system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | call, then it is required that system administrator mount a file system of |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 388 | type hugetlbfs:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Randy Dunlap | e73a75f | 2007-07-15 23:40:52 -0700 | [diff] [blame] | 390 | mount -t hugetlbfs \ |
Mike Kravetz | 8c9b970 | 2015-04-15 16:13:45 -0700 | [diff] [blame] | 391 | -o uid=<value>,gid=<value>,mode=<value>,pagesize=<value>,size=<value>,\ |
| 392 | min_size=<value>,nr_inodes=<value> none /mnt/huge |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | This command mounts a (pseudo) filesystem of type hugetlbfs on the directory |
Mike Rapoport | fde7917 | 2018-04-18 11:07:44 +0300 | [diff] [blame] | 395 | ``/mnt/huge``. Any file created on ``/mnt/huge`` uses huge pages. |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 396 | |
| 397 | The ``uid`` and ``gid`` options sets the owner and group of the root of the |
| 398 | file system. By default the ``uid`` and ``gid`` of the current process |
| 399 | are taken. |
| 400 | |
| 401 | The ``mode`` option sets the mode of root of file system to value & 01777. |
| 402 | This value is given in octal. By default the value 0755 is picked. |
| 403 | |
| 404 | If the platform supports multiple huge page sizes, the ``pagesize`` option can |
| 405 | be used to specify the huge page size and associated pool. ``pagesize`` |
| 406 | is specified in bytes. If ``pagesize`` is not specified the platform's |
| 407 | default huge page size and associated pool will be used. |
| 408 | |
| 409 | The ``size`` option sets the maximum value of memory (huge pages) allowed |
| 410 | for that filesystem (``/mnt/huge``). The ``size`` option can be specified |
| 411 | in bytes, or as a percentage of the specified huge page pool (``nr_hugepages``). |
| 412 | The size is rounded down to HPAGE_SIZE boundary. |
| 413 | |
| 414 | The ``min_size`` option sets the minimum value of memory (huge pages) allowed |
| 415 | for the filesystem. ``min_size`` can be specified in the same way as ``size``, |
| 416 | either bytes or a percentage of the huge page pool. |
| 417 | At mount time, the number of huge pages specified by ``min_size`` are reserved |
| 418 | for use by the filesystem. |
| 419 | If there are not enough free huge pages available, the mount will fail. |
| 420 | As huge pages are allocated to the filesystem and freed, the reserve count |
| 421 | is adjusted so that the sum of allocated and reserved huge pages is always |
| 422 | at least ``min_size``. |
| 423 | |
| 424 | The option ``nr_inodes`` sets the maximum number of inodes that ``/mnt/huge`` |
| 425 | can use. |
| 426 | |
| 427 | If the ``size``, ``min_size`` or ``nr_inodes`` option is not provided on |
| 428 | command line then no limits are set. |
| 429 | |
| 430 | For ``pagesize``, ``size``, ``min_size`` and ``nr_inodes`` options, you can |
| 431 | use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. |
| 432 | For example, size=2K has the same meaning as size=2048. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 434 | While read system calls are supported on files that reside on hugetlb |
| 435 | file systems, write system calls are not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 437 | Regular chown, chgrp, and chmod commands (with right permissions) could be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | used to change the file attributes on hugetlbfs. |
| 439 | |
David Rientjes | 80d6b94 | 2015-04-15 16:14:26 -0700 | [diff] [blame] | 440 | Also, it is important to note that no such mount command is required if |
Eric B Munson | 94bf5ce | 2009-09-21 17:03:48 -0700 | [diff] [blame] | 441 | applications are going to use only shmat/shmget system calls or mmap with |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 442 | MAP_HUGETLB. For an example of how to use mmap with MAP_HUGETLB see |
| 443 | :ref:`map_hugetlb <map_hugetlb>` below. |
David Rientjes | 80d6b94 | 2015-04-15 16:14:26 -0700 | [diff] [blame] | 444 | |
Mike Rapoport | fde7917 | 2018-04-18 11:07:44 +0300 | [diff] [blame] | 445 | Users who wish to use hugetlb memory via shared memory segment should be |
| 446 | members of a supplementary group and system admin needs to configure that gid |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 447 | into ``/proc/sys/vm/hugetlb_shm_group``. It is possible for same or different |
David Rientjes | 80d6b94 | 2015-04-15 16:14:26 -0700 | [diff] [blame] | 448 | applications to use any combination of mmaps and shm* calls, though the mount of |
| 449 | filesystem will be required for using mmap calls without MAP_HUGETLB. |
| 450 | |
| 451 | Syscalls that operate on memory backed by hugetlb pages only have their lengths |
| 452 | aligned to the native page size of the processor; they will normally fail with |
| 453 | errno set to EINVAL or exclude hugetlb pages that extend beyond the length if |
| 454 | not hugepage aligned. For example, munmap(2) will fail if memory is backed by |
| 455 | a hugetlb page and the length is smaller than the hugepage size. |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Davidlohr Bueso | 15610c8 | 2013-09-11 14:21:48 -0700 | [diff] [blame] | 458 | Examples |
| 459 | ======== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 461 | .. _map_hugetlb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 463 | ``map_hugetlb`` |
| 464 | see tools/testing/selftests/vm/map_hugetlb.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 466 | ``hugepage-shm`` |
| 467 | see tools/testing/selftests/vm/hugepage-shm.c |
Zhouping Liu | d46f3d8 | 2012-08-21 16:15:57 -0700 | [diff] [blame] | 468 | |
Mike Rapoport | 148723f | 2018-03-21 21:22:23 +0200 | [diff] [blame] | 469 | ``hugepage-mmap`` |
| 470 | see tools/testing/selftests/vm/hugepage-mmap.c |
| 471 | |
| 472 | The `libhugetlbfs`_ library provides a wide range of userspace tools |
| 473 | to help with huge page usability, environment setup, and control. |
| 474 | |
| 475 | .. _libhugetlbfs: https://github.com/libhugetlbfs/libhugetlbfs |