Back to home page

OSCL-LXR

 
 

    


0001 .. _hugetlbpage:
0002 
0003 =============
0004 HugeTLB Pages
0005 =============
0006 
0007 Overview
0008 ========
0009 
0010 The intent of this file is to give a brief summary of hugetlbpage support in
0011 the Linux kernel.  This support is built on top of multiple page size support
0012 that is provided by most modern architectures.  For example, x86 CPUs normally
0013 support 4K and 2M (1G if architecturally supported) page sizes, ia64
0014 architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M,
0015 256M and ppc64 supports 4K and 16M.  A TLB is a cache of virtual-to-physical
0016 translations.  Typically this is a very scarce resource on processor.
0017 Operating systems try to make best use of limited number of TLB resources.
0018 This optimization is more critical now as bigger and bigger physical memories
0019 (several GBs) are more readily available.
0020 
0021 Users can use the huge page support in Linux kernel by either using the mmap
0022 system call or standard SYSV shared memory system calls (shmget, shmat).
0023 
0024 First the Linux kernel needs to be built with the CONFIG_HUGETLBFS
0025 (present under "File systems") and CONFIG_HUGETLB_PAGE (selected
0026 automatically when CONFIG_HUGETLBFS is selected) configuration
0027 options.
0028 
0029 The ``/proc/meminfo`` file provides information about the total number of
0030 persistent hugetlb pages in the kernel's huge page pool.  It also displays
0031 default huge page size and information about the number of free, reserved
0032 and surplus huge pages in the pool of huge pages of default size.
0033 The huge page size is needed for generating the proper alignment and
0034 size of the arguments to system calls that map huge page regions.
0035 
0036 The output of ``cat /proc/meminfo`` will include lines like::
0037 
0038         HugePages_Total: uuu
0039         HugePages_Free:  vvv
0040         HugePages_Rsvd:  www
0041         HugePages_Surp:  xxx
0042         Hugepagesize:    yyy kB
0043         Hugetlb:         zzz kB
0044 
0045 where:
0046 
0047 HugePages_Total
0048         is the size of the pool of huge pages.
0049 HugePages_Free
0050         is the number of huge pages in the pool that are not yet
0051         allocated.
0052 HugePages_Rsvd
0053         is short for "reserved," and is the number of huge pages for
0054         which a commitment to allocate from the pool has been made,
0055         but no allocation has yet been made.  Reserved huge pages
0056         guarantee that an application will be able to allocate a
0057         huge page from the pool of huge pages at fault time.
0058 HugePages_Surp
0059         is short for "surplus," and is the number of huge pages in
0060         the pool above the value in ``/proc/sys/vm/nr_hugepages``. The
0061         maximum number of surplus huge pages is controlled by
0062         ``/proc/sys/vm/nr_overcommit_hugepages``.
0063         Note: When the feature of freeing unused vmemmap pages associated
0064         with each hugetlb page is enabled, the number of surplus huge pages
0065         may be temporarily larger than the maximum number of surplus huge
0066         pages when the system is under memory pressure.
0067 Hugepagesize
0068         is the default hugepage size (in Kb).
0069 Hugetlb
0070         is the total amount of memory (in kB), consumed by huge
0071         pages of all sizes.
0072         If huge pages of different sizes are in use, this number
0073         will exceed HugePages_Total \* Hugepagesize. To get more
0074         detailed information, please, refer to
0075         ``/sys/kernel/mm/hugepages`` (described below).
0076 
0077 
0078 ``/proc/filesystems`` should also show a filesystem of type "hugetlbfs"
0079 configured in the kernel.
0080 
0081 ``/proc/sys/vm/nr_hugepages`` indicates the current number of "persistent" huge
0082 pages in the kernel's huge page pool.  "Persistent" huge pages will be
0083 returned to the huge page pool when freed by a task.  A user with root
0084 privileges can dynamically allocate more or free some persistent huge pages
0085 by increasing or decreasing the value of ``nr_hugepages``.
0086 
0087 Note: When the feature of freeing unused vmemmap pages associated with each
0088 hugetlb page is enabled, we can fail to free the huge pages triggered by
0089 the user when ths system is under memory pressure.  Please try again later.
0090 
0091 Pages that are used as huge pages are reserved inside the kernel and cannot
0092 be used for other purposes.  Huge pages cannot be swapped out under
0093 memory pressure.
0094 
0095 Once a number of huge pages have been pre-allocated to the kernel huge page
0096 pool, a user with appropriate privilege can use either the mmap system call
0097 or shared memory system calls to use the huge pages.  See the discussion of
0098 :ref:`Using Huge Pages <using_huge_pages>`, below.
0099 
0100 The administrator can allocate persistent huge pages on the kernel boot
0101 command line by specifying the "hugepages=N" parameter, where 'N' = the
0102 number of huge pages requested.  This is the most reliable method of
0103 allocating huge pages as memory has not yet become fragmented.
0104 
0105 Some platforms support multiple huge page sizes.  To allocate huge pages
0106 of a specific size, one must precede the huge pages boot command parameters
0107 with a huge page size selection parameter "hugepagesz=<size>".  <size> must
0108 be specified in bytes with optional scale suffix [kKmMgG].  The default huge
0109 page size may be selected with the "default_hugepagesz=<size>" boot parameter.
0110 
0111 Hugetlb boot command line parameter semantics
0112 
0113 hugepagesz
0114         Specify a huge page size.  Used in conjunction with hugepages
0115         parameter to preallocate a number of huge pages of the specified
0116         size.  Hence, hugepagesz and hugepages are typically specified in
0117         pairs such as::
0118 
0119                 hugepagesz=2M hugepages=512
0120 
0121         hugepagesz can only be specified once on the command line for a
0122         specific huge page size.  Valid huge page sizes are architecture
0123         dependent.
0124 hugepages
0125         Specify the number of huge pages to preallocate.  This typically
0126         follows a valid hugepagesz or default_hugepagesz parameter.  However,
0127         if hugepages is the first or only hugetlb command line parameter it
0128         implicitly specifies the number of huge pages of default size to
0129         allocate.  If the number of huge pages of default size is implicitly
0130         specified, it can not be overwritten by a hugepagesz,hugepages
0131         parameter pair for the default size.  This parameter also has a
0132         node format.  The node format specifies the number of huge pages
0133         to allocate on specific nodes.
0134 
0135         For example, on an architecture with 2M default huge page size::
0136 
0137                 hugepages=256 hugepagesz=2M hugepages=512
0138 
0139         will result in 256 2M huge pages being allocated and a warning message
0140         indicating that the hugepages=512 parameter is ignored.  If a hugepages
0141         parameter is preceded by an invalid hugepagesz parameter, it will
0142         be ignored.
0143 
0144         Node format example::
0145 
0146                 hugepagesz=2M hugepages=0:1,1:2
0147 
0148         It will allocate 1 2M hugepage on node0 and 2 2M hugepages on node1.
0149         If the node number is invalid,  the parameter will be ignored.
0150 
0151 default_hugepagesz
0152         Specify the default huge page size.  This parameter can
0153         only be specified once on the command line.  default_hugepagesz can
0154         optionally be followed by the hugepages parameter to preallocate a
0155         specific number of huge pages of default size.  The number of default
0156         sized huge pages to preallocate can also be implicitly specified as
0157         mentioned in the hugepages section above.  Therefore, on an
0158         architecture with 2M default huge page size::
0159 
0160                 hugepages=256
0161                 default_hugepagesz=2M hugepages=256
0162                 hugepages=256 default_hugepagesz=2M
0163 
0164         will all result in 256 2M huge pages being allocated.  Valid default
0165         huge page size is architecture dependent.
0166 hugetlb_free_vmemmap
0167         When CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP is set, this enables HugeTLB
0168         Vmemmap Optimization (HVO).
0169 
0170 When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages``
0171 indicates the current number of pre-allocated huge pages of the default size.
0172 Thus, one can use the following command to dynamically allocate/deallocate
0173 default sized persistent huge pages::
0174 
0175         echo 20 > /proc/sys/vm/nr_hugepages
0176 
0177 This command will try to adjust the number of default sized huge pages in the
0178 huge page pool to 20, allocating or freeing huge pages, as required.
0179 
0180 On a NUMA platform, the kernel will attempt to distribute the huge page pool
0181 over all the set of allowed nodes specified by the NUMA memory policy of the
0182 task that modifies ``nr_hugepages``. The default for the allowed nodes--when the
0183 task has default memory policy--is all on-line nodes with memory.  Allowed
0184 nodes with insufficient available, contiguous memory for a huge page will be
0185 silently skipped when allocating persistent huge pages.  See the
0186 :ref:`discussion below <mem_policy_and_hp_alloc>`
0187 of the interaction of task memory policy, cpusets and per node attributes
0188 with the allocation and freeing of persistent huge pages.
0189 
0190 The success or failure of huge page allocation depends on the amount of
0191 physically contiguous memory that is present in system at the time of the
0192 allocation attempt.  If the kernel is unable to allocate huge pages from
0193 some nodes in a NUMA system, it will attempt to make up the difference by
0194 allocating extra pages on other nodes with sufficient available contiguous
0195 memory, if any.
0196 
0197 System administrators may want to put this command in one of the local rc
0198 init files.  This will enable the kernel to allocate huge pages early in
0199 the boot process when the possibility of getting physical contiguous pages
0200 is still very high.  Administrators can verify the number of huge pages
0201 actually allocated by checking the sysctl or meminfo.  To check the per node
0202 distribution of huge pages in a NUMA system, use::
0203 
0204         cat /sys/devices/system/node/node*/meminfo | fgrep Huge
0205 
0206 ``/proc/sys/vm/nr_overcommit_hugepages`` specifies how large the pool of
0207 huge pages can grow, if more huge pages than ``/proc/sys/vm/nr_hugepages`` are
0208 requested by applications.  Writing any non-zero value into this file
0209 indicates that the hugetlb subsystem is allowed to try to obtain that
0210 number of "surplus" huge pages from the kernel's normal page pool, when the
0211 persistent huge page pool is exhausted. As these surplus huge pages become
0212 unused, they are freed back to the kernel's normal page pool.
0213 
0214 When increasing the huge page pool size via ``nr_hugepages``, any existing
0215 surplus pages will first be promoted to persistent huge pages.  Then, additional
0216 huge pages will be allocated, if necessary and if possible, to fulfill
0217 the new persistent huge page pool size.
0218 
0219 The administrator may shrink the pool of persistent huge pages for
0220 the default huge page size by setting the ``nr_hugepages`` sysctl to a
0221 smaller value.  The kernel will attempt to balance the freeing of huge pages
0222 across all nodes in the memory policy of the task modifying ``nr_hugepages``.
0223 Any free huge pages on the selected nodes will be freed back to the kernel's
0224 normal page pool.
0225 
0226 Caveat: Shrinking the persistent huge page pool via ``nr_hugepages`` such that
0227 it becomes less than the number of huge pages in use will convert the balance
0228 of the in-use huge pages to surplus huge pages.  This will occur even if
0229 the number of surplus pages would exceed the overcommit value.  As long as
0230 this condition holds--that is, until ``nr_hugepages+nr_overcommit_hugepages`` is
0231 increased sufficiently, or the surplus huge pages go out of use and are freed--
0232 no more surplus huge pages will be allowed to be allocated.
0233 
0234 With support for multiple huge page pools at run-time available, much of
0235 the huge page userspace interface in ``/proc/sys/vm`` has been duplicated in
0236 sysfs.
0237 The ``/proc`` interfaces discussed above have been retained for backwards
0238 compatibility. The root huge page control directory in sysfs is::
0239 
0240         /sys/kernel/mm/hugepages
0241 
0242 For each huge page size supported by the running kernel, a subdirectory
0243 will exist, of the form::
0244 
0245         hugepages-${size}kB
0246 
0247 Inside each of these directories, the set of files contained in ``/proc``
0248 will exist.  In addition, two additional interfaces for demoting huge
0249 pages may exist::
0250 
0251         demote
0252         demote_size
0253         nr_hugepages
0254         nr_hugepages_mempolicy
0255         nr_overcommit_hugepages
0256         free_hugepages
0257         resv_hugepages
0258         surplus_hugepages
0259 
0260 The demote interfaces provide the ability to split a huge page into
0261 smaller huge pages.  For example, the x86 architecture supports both
0262 1GB and 2MB huge pages sizes.  A 1GB huge page can be split into 512
0263 2MB huge pages.  Demote interfaces are not available for the smallest
0264 huge page size.  The demote interfaces are:
0265 
0266 demote_size
0267         is the size of demoted pages.  When a page is demoted a corresponding
0268         number of huge pages of demote_size will be created.  By default,
0269         demote_size is set to the next smaller huge page size.  If there are
0270         multiple smaller huge page sizes, demote_size can be set to any of
0271         these smaller sizes.  Only huge page sizes less than the current huge
0272         pages size are allowed.
0273 
0274 demote
0275         is used to demote a number of huge pages.  A user with root privileges
0276         can write to this file.  It may not be possible to demote the
0277         requested number of huge pages.  To determine how many pages were
0278         actually demoted, compare the value of nr_hugepages before and after
0279         writing to the demote interface.  demote is a write only interface.
0280 
0281 The interfaces which are the same as in ``/proc`` (all except demote and
0282 demote_size) function as described above for the default huge page-sized case.
0283 
0284 .. _mem_policy_and_hp_alloc:
0285 
0286 Interaction of Task Memory Policy with Huge Page Allocation/Freeing
0287 ===================================================================
0288 
0289 Whether huge pages are allocated and freed via the ``/proc`` interface or
0290 the ``/sysfs`` interface using the ``nr_hugepages_mempolicy`` attribute, the
0291 NUMA nodes from which huge pages are allocated or freed are controlled by the
0292 NUMA memory policy of the task that modifies the ``nr_hugepages_mempolicy``
0293 sysctl or attribute.  When the ``nr_hugepages`` attribute is used, mempolicy
0294 is ignored.
0295 
0296 The recommended method to allocate or free huge pages to/from the kernel
0297 huge page pool, using the ``nr_hugepages`` example above, is::
0298 
0299     numactl --interleave <node-list> echo 20 \
0300                                 >/proc/sys/vm/nr_hugepages_mempolicy
0301 
0302 or, more succinctly::
0303 
0304     numactl -m <node-list> echo 20 >/proc/sys/vm/nr_hugepages_mempolicy
0305 
0306 This will allocate or free ``abs(20 - nr_hugepages)`` to or from the nodes
0307 specified in <node-list>, depending on whether number of persistent huge pages
0308 is initially less than or greater than 20, respectively.  No huge pages will be
0309 allocated nor freed on any node not included in the specified <node-list>.
0310 
0311 When adjusting the persistent hugepage count via ``nr_hugepages_mempolicy``, any
0312 memory policy mode--bind, preferred, local or interleave--may be used.  The
0313 resulting effect on persistent huge page allocation is as follows:
0314 
0315 #. Regardless of mempolicy mode [see
0316    :ref:`Documentation/admin-guide/mm/numa_memory_policy.rst <numa_memory_policy>`],
0317    persistent huge pages will be distributed across the node or nodes
0318    specified in the mempolicy as if "interleave" had been specified.
0319    However, if a node in the policy does not contain sufficient contiguous
0320    memory for a huge page, the allocation will not "fallback" to the nearest
0321    neighbor node with sufficient contiguous memory.  To do this would cause
0322    undesirable imbalance in the distribution of the huge page pool, or
0323    possibly, allocation of persistent huge pages on nodes not allowed by
0324    the task's memory policy.
0325 
0326 #. One or more nodes may be specified with the bind or interleave policy.
0327    If more than one node is specified with the preferred policy, only the
0328    lowest numeric id will be used.  Local policy will select the node where
0329    the task is running at the time the nodes_allowed mask is constructed.
0330    For local policy to be deterministic, the task must be bound to a cpu or
0331    cpus in a single node.  Otherwise, the task could be migrated to some
0332    other node at any time after launch and the resulting node will be
0333    indeterminate.  Thus, local policy is not very useful for this purpose.
0334    Any of the other mempolicy modes may be used to specify a single node.
0335 
0336 #. The nodes allowed mask will be derived from any non-default task mempolicy,
0337    whether this policy was set explicitly by the task itself or one of its
0338    ancestors, such as numactl.  This means that if the task is invoked from a
0339    shell with non-default policy, that policy will be used.  One can specify a
0340    node list of "all" with numactl --interleave or --membind [-m] to achieve
0341    interleaving over all nodes in the system or cpuset.
0342 
0343 #. Any task mempolicy specified--e.g., using numactl--will be constrained by
0344    the resource limits of any cpuset in which the task runs.  Thus, there will
0345    be no way for a task with non-default policy running in a cpuset with a
0346    subset of the system nodes to allocate huge pages outside the cpuset
0347    without first moving to a cpuset that contains all of the desired nodes.
0348 
0349 #. Boot-time huge page allocation attempts to distribute the requested number
0350    of huge pages over all on-lines nodes with memory.
0351 
0352 Per Node Hugepages Attributes
0353 =============================
0354 
0355 A subset of the contents of the root huge page control directory in sysfs,
0356 described above, will be replicated under each the system device of each
0357 NUMA node with memory in::
0358 
0359         /sys/devices/system/node/node[0-9]*/hugepages/
0360 
0361 Under this directory, the subdirectory for each supported huge page size
0362 contains the following attribute files::
0363 
0364         nr_hugepages
0365         free_hugepages
0366         surplus_hugepages
0367 
0368 The free\_' and surplus\_' attribute files are read-only.  They return the number
0369 of free and surplus [overcommitted] huge pages, respectively, on the parent
0370 node.
0371 
0372 The ``nr_hugepages`` attribute returns the total number of huge pages on the
0373 specified node.  When this attribute is written, the number of persistent huge
0374 pages on the parent node will be adjusted to the specified value, if sufficient
0375 resources exist, regardless of the task's mempolicy or cpuset constraints.
0376 
0377 Note that the number of overcommit and reserve pages remain global quantities,
0378 as we don't know until fault time, when the faulting task's mempolicy is
0379 applied, from which node the huge page allocation will be attempted.
0380 
0381 .. _using_huge_pages:
0382 
0383 Using Huge Pages
0384 ================
0385 
0386 If the user applications are going to request huge pages using mmap system
0387 call, then it is required that system administrator mount a file system of
0388 type hugetlbfs::
0389 
0390   mount -t hugetlbfs \
0391         -o uid=<value>,gid=<value>,mode=<value>,pagesize=<value>,size=<value>,\
0392         min_size=<value>,nr_inodes=<value> none /mnt/huge
0393 
0394 This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
0395 ``/mnt/huge``.  Any file created on ``/mnt/huge`` uses huge pages.
0396 
0397 The ``uid`` and ``gid`` options sets the owner and group of the root of the
0398 file system.  By default the ``uid`` and ``gid`` of the current process
0399 are taken.
0400 
0401 The ``mode`` option sets the mode of root of file system to value & 01777.
0402 This value is given in octal. By default the value 0755 is picked.
0403 
0404 If the platform supports multiple huge page sizes, the ``pagesize`` option can
0405 be used to specify the huge page size and associated pool. ``pagesize``
0406 is specified in bytes. If ``pagesize`` is not specified the platform's
0407 default huge page size and associated pool will be used.
0408 
0409 The ``size`` option sets the maximum value of memory (huge pages) allowed
0410 for that filesystem (``/mnt/huge``). The ``size`` option can be specified
0411 in bytes, or as a percentage of the specified huge page pool (``nr_hugepages``).
0412 The size is rounded down to HPAGE_SIZE boundary.
0413 
0414 The ``min_size`` option sets the minimum value of memory (huge pages) allowed
0415 for the filesystem. ``min_size`` can be specified in the same way as ``size``,
0416 either bytes or a percentage of the huge page pool.
0417 At mount time, the number of huge pages specified by ``min_size`` are reserved
0418 for use by the filesystem.
0419 If there are not enough free huge pages available, the mount will fail.
0420 As huge pages are allocated to the filesystem and freed, the reserve count
0421 is adjusted so that the sum of allocated and reserved huge pages is always
0422 at least ``min_size``.
0423 
0424 The option ``nr_inodes`` sets the maximum number of inodes that ``/mnt/huge``
0425 can use.
0426 
0427 If the ``size``, ``min_size`` or ``nr_inodes`` option is not provided on
0428 command line then no limits are set.
0429 
0430 For ``pagesize``, ``size``, ``min_size`` and ``nr_inodes`` options, you can
0431 use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo.
0432 For example, size=2K has the same meaning as size=2048.
0433 
0434 While read system calls are supported on files that reside on hugetlb
0435 file systems, write system calls are not.
0436 
0437 Regular chown, chgrp, and chmod commands (with right permissions) could be
0438 used to change the file attributes on hugetlbfs.
0439 
0440 Also, it is important to note that no such mount command is required if
0441 applications are going to use only shmat/shmget system calls or mmap with
0442 MAP_HUGETLB.  For an example of how to use mmap with MAP_HUGETLB see
0443 :ref:`map_hugetlb <map_hugetlb>` below.
0444 
0445 Users who wish to use hugetlb memory via shared memory segment should be
0446 members of a supplementary group and system admin needs to configure that gid
0447 into ``/proc/sys/vm/hugetlb_shm_group``.  It is possible for same or different
0448 applications to use any combination of mmaps and shm* calls, though the mount of
0449 filesystem will be required for using mmap calls without MAP_HUGETLB.
0450 
0451 Syscalls that operate on memory backed by hugetlb pages only have their lengths
0452 aligned to the native page size of the processor; they will normally fail with
0453 errno set to EINVAL or exclude hugetlb pages that extend beyond the length if
0454 not hugepage aligned.  For example, munmap(2) will fail if memory is backed by
0455 a hugetlb page and the length is smaller than the hugepage size.
0456 
0457 
0458 Examples
0459 ========
0460 
0461 .. _map_hugetlb:
0462 
0463 ``map_hugetlb``
0464         see tools/testing/selftests/vm/map_hugetlb.c
0465 
0466 ``hugepage-shm``
0467         see tools/testing/selftests/vm/hugepage-shm.c
0468 
0469 ``hugepage-mmap``
0470         see tools/testing/selftests/vm/hugepage-mmap.c
0471 
0472 The `libhugetlbfs`_  library provides a wide range of userspace tools
0473 to help with huge page usability, environment setup, and control.
0474 
0475 .. _libhugetlbfs: https://github.com/libhugetlbfs/libhugetlbfs