xref: /linux/Documentation/admin-guide/mm/transhuge.rst (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1============================
2Transparent Hugepage Support
3============================
4
5Objective
6=========
7
8Performance critical computing applications dealing with large memory
9working sets are already running on top of libhugetlbfs and in turn
10hugetlbfs. Transparent HugePage Support (THP) is an alternative mean of
11using huge pages for the backing of virtual memory with huge pages
12that supports the automatic promotion and demotion of page sizes and
13without the shortcomings of hugetlbfs.
14
15Currently THP only works for anonymous memory mappings and tmpfs/shmem.
16But in the future it can expand to other filesystems.
17
18.. note::
19   in the examples below we presume that the basic page size is 4K and
20   the huge page size is 2M, although the actual numbers may vary
21   depending on the CPU architecture.
22
23The reason applications are running faster is because of two
24factors. The first factor is almost completely irrelevant and it's not
25of significant interest because it'll also have the downside of
26requiring larger clear-page copy-page in page faults which is a
27potentially negative effect. The first factor consists in taking a
28single page fault for each 2M virtual region touched by userland (so
29reducing the enter/exit kernel frequency by a 512 times factor). This
30only matters the first time the memory is accessed for the lifetime of
31a memory mapping. The second long lasting and much more important
32factor will affect all subsequent accesses to the memory for the whole
33runtime of the application. The second factor consist of two
34components:
35
361) the TLB miss will run faster (especially with virtualization using
37   nested pagetables but almost always also on bare metal without
38   virtualization)
39
402) a single TLB entry will be mapping a much larger amount of virtual
41   memory in turn reducing the number of TLB misses. With
42   virtualization and nested pagetables the TLB can be mapped of
43   larger size only if both KVM and the Linux guest are using
44   hugepages but a significant speedup already happens if only one of
45   the two is using hugepages just because of the fact the TLB miss is
46   going to run faster.
47
48Modern kernels support "multi-size THP" (mTHP), which introduces the
49ability to allocate memory in blocks that are bigger than a base page
50but smaller than traditional PMD-size (as described above), in
51increments of a power-of-2 number of pages. mTHP can back anonymous
52memory (for example 16K, 32K, 64K, etc). These THPs continue to be
53PTE-mapped, but in many cases can still provide similar benefits to
54those outlined above: Page faults are significantly reduced (by a
55factor of e.g. 4, 8, 16, etc), but latency spikes are much less
56prominent because the size of each page isn't as huge as the PMD-sized
57variant and there is less memory to clear in each page fault. Some
58architectures also employ TLB compression mechanisms to squeeze more
59entries in when a set of PTEs are virtually and physically contiguous
60and appropriately aligned. In this case, TLB misses will occur less
61often.
62
63THP can be enabled system wide or restricted to certain tasks or even
64memory ranges inside task's address space. Unless THP is completely
65disabled, there is ``khugepaged`` daemon that scans memory and
66collapses sequences of basic pages into huge pages of either PMD size
67or mTHP sizes, if the system is configured to do so.
68
69The THP behaviour is controlled via :ref:`sysfs <thp_sysfs>`
70interface and using madvise(2) and prctl(2) system calls.
71
72Transparent Hugepage Support maximizes the usefulness of free memory
73if compared to the reservation approach of hugetlbfs by allowing all
74unused memory to be used as cache or other movable (or even unmovable
75entities). It doesn't require reservation to prevent hugepage
76allocation failures to be noticeable from userland. It allows paging
77and all other advanced VM features to be available on the
78hugepages. It requires no modifications for applications to take
79advantage of it.
80
81Applications however can be further optimized to take advantage of
82this feature, like for example they've been optimized before to avoid
83a flood of mmap system calls for every malloc(4k). Optimizing userland
84is by far not mandatory and khugepaged already can take care of long
85lived page allocations even for hugepage unaware applications that
86deals with large amounts of memory.
87
88In certain cases when hugepages are enabled system wide, application
89may end up allocating more memory resources. An application may mmap a
90large region but only touch 1 byte of it, in that case a 2M page might
91be allocated instead of a 4k page for no good. This is why it's
92possible to disable hugepages system-wide and to only have them inside
93MADV_HUGEPAGE madvise regions.
94
95Embedded systems should enable hugepages only inside madvise regions
96to eliminate any risk of wasting any precious byte of memory and to
97only run faster.
98
99Applications that gets a lot of benefit from hugepages and that don't
100risk to lose memory by using hugepages, should use
101madvise(MADV_HUGEPAGE) on their critical mmapped regions.
102
103.. _thp_sysfs:
104
105sysfs
106=====
107
108Global THP controls
109-------------------
110
111Transparent Hugepage Support for anonymous memory can be disabled
112(mostly for debugging purposes) or only enabled inside MADV_HUGEPAGE
113regions (to avoid the risk of consuming more memory resources) or enabled
114system wide. This can be achieved per-supported-THP-size with one of::
115
116	echo always >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
117	echo madvise >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
118	echo never >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
119
120where <size> is the hugepage size being addressed, the available sizes
121for which vary by system.
122
123.. note:: Setting "never" in all sysfs THP controls does **not** disable
124          Transparent Huge Pages globally. This is because ``madvise(...,
125          MADV_COLLAPSE)`` ignores these settings and collapses ranges to
126          PMD-sized huge pages unconditionally.
127
128For example::
129
130	echo always >/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled
131
132Alternatively it is possible to specify that a given hugepage size
133will inherit the top-level "enabled" value::
134
135	echo inherit >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
136
137For example::
138
139	echo inherit >/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled
140
141The top-level setting (for use with "inherit") can be set by issuing
142one of the following commands::
143
144	echo always >/sys/kernel/mm/transparent_hugepage/enabled
145	echo madvise >/sys/kernel/mm/transparent_hugepage/enabled
146	echo never >/sys/kernel/mm/transparent_hugepage/enabled
147
148By default, PMD-sized hugepages have enabled="inherit" and all other
149hugepage sizes have enabled="never". If enabling multiple hugepage
150sizes, the kernel will select the most appropriate enabled size for a
151given allocation.
152
153It's also possible to limit defrag efforts in the VM to generate
154anonymous hugepages in case they're not immediately free to madvise
155regions or to never try to defrag memory and simply fallback to regular
156pages unless hugepages are immediately available. Clearly if we spend CPU
157time to defrag memory, we would expect to gain even more by the fact we
158use hugepages later instead of regular pages. This isn't always
159guaranteed, but it may be more likely in case the allocation is for a
160MADV_HUGEPAGE region.
161
162::
163
164	echo always >/sys/kernel/mm/transparent_hugepage/defrag
165	echo defer >/sys/kernel/mm/transparent_hugepage/defrag
166	echo defer+madvise >/sys/kernel/mm/transparent_hugepage/defrag
167	echo madvise >/sys/kernel/mm/transparent_hugepage/defrag
168	echo never >/sys/kernel/mm/transparent_hugepage/defrag
169
170always
171	means that an application requesting THP will stall on
172	allocation failure and directly reclaim pages and compact
173	memory in an effort to allocate a THP immediately. This may be
174	desirable for virtual machines that benefit heavily from THP
175	use and are willing to delay the VM start to utilise them.
176
177defer
178	means that an application will wake kswapd in the background
179	to reclaim pages and wake kcompactd to compact memory so that
180	THP is available in the near future. It's the responsibility
181	of khugepaged to then install the THP pages later.
182
183defer+madvise
184	will enter direct reclaim and compaction like ``always``, but
185	only for regions that have used madvise(MADV_HUGEPAGE); all
186	other regions will wake kswapd in the background to reclaim
187	pages and wake kcompactd to compact memory so that THP is
188	available in the near future.
189
190madvise
191	will enter direct reclaim like ``always`` but only for regions
192	that are have used madvise(MADV_HUGEPAGE). This is the default
193	behaviour.
194
195never
196	should be self-explanatory. Note that ``madvise(...,
197	MADV_COLLAPSE)`` can still cause transparent huge pages to be
198	obtained even if this mode is specified everywhere.
199
200By default kernel tries to use huge, PMD-mappable zero page on read
201page fault to anonymous mapping. It's possible to disable huge zero
202page by writing 0 or enable it back by writing 1::
203
204	echo 0 >/sys/kernel/mm/transparent_hugepage/use_zero_page
205	echo 1 >/sys/kernel/mm/transparent_hugepage/use_zero_page
206
207Some userspace (such as a test program, or an optimized memory
208allocation library) may want to know the size (in bytes) of a
209PMD-mappable transparent hugepage::
210
211	cat /sys/kernel/mm/transparent_hugepage/hpage_pmd_size
212
213All THPs at fault and collapse time will be added to _deferred_list,
214and will therefore be split under memory pressure if they are considered
215"underused". A THP is underused if the number of zero-filled pages in
216the THP is above max_ptes_none (see below). It is possible to disable
217this behaviour by writing 0 to shrink_underused, and enable it by writing
2181 to it::
219
220	echo 0 > /sys/kernel/mm/transparent_hugepage/shrink_underused
221	echo 1 > /sys/kernel/mm/transparent_hugepage/shrink_underused
222
223khugepaged will be automatically started when any THP size is enabled
224(either of the per-size anon control or the top-level control are set
225to "always" or "madvise"), and it'll be automatically shutdown when
226all THP sizes are disabled (when both the per-size anon control and the
227top-level control are "never").
228
229process THP controls
230--------------------
231
232A process can control its own THP behaviour using the ``PR_SET_THP_DISABLE``
233and ``PR_GET_THP_DISABLE`` pair of prctl(2) calls. The THP behaviour set using
234``PR_SET_THP_DISABLE`` is inherited across fork(2) and execve(2). These calls
235support the following arguments::
236
237	prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0):
238		This will disable THPs completely for the process, irrespective
239		of global THP controls or madvise(..., MADV_COLLAPSE) being used.
240
241	prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, 0, 0):
242		This will disable THPs for the process except when the usage of THPs is
243		advised. Consequently, THPs will only be used when:
244		- Global THP controls are set to "always" or "madvise" and
245		  madvise(..., MADV_HUGEPAGE) or madvise(..., MADV_COLLAPSE) is used.
246		- Global THP controls are set to "never" and madvise(..., MADV_COLLAPSE)
247		  is used. This is the same behavior as if THPs would not be disabled on
248		  a process level.
249		Note that MADV_COLLAPSE is currently always rejected if
250		madvise(..., MADV_NOHUGEPAGE) is set on an area.
251
252	prctl(PR_SET_THP_DISABLE, 0, 0, 0, 0):
253		This will re-enable THPs for the process, as if they were never disabled.
254		Whether THPs will actually be used depends on global THP controls and
255		madvise() calls.
256
257	prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0):
258		This returns a value whose bits indicate how THP-disable is configured:
259		Bits
260		 1 0  Value  Description
261		|0|0|   0    No THP-disable behaviour specified.
262		|0|1|   1    THP is entirely disabled for this process.
263		|1|1|   3    THP-except-advised mode is set for this process.
264
265Khugepaged controls
266-------------------
267
268.. note::
269   khugepaged currently only searches for opportunities to collapse file/shmem
270   to PMD-sized THP. Only anonymous memory will attempt to collapse to other THP
271   sizes.
272
273khugepaged runs usually at low frequency so while one may not want to
274invoke defrag algorithms synchronously during the page faults, it
275should be worth invoking defrag at least in khugepaged. However it's
276also possible to disable defrag in khugepaged by writing 0 or enable
277defrag in khugepaged by writing 1::
278
279	echo 0 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
280	echo 1 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
281
282You can also control how many pages khugepaged should scan at each
283pass::
284
285	/sys/kernel/mm/transparent_hugepage/khugepaged/pages_to_scan
286
287and how many milliseconds to wait in khugepaged between each pass (you
288can set this to 0 to run khugepaged at 100% utilization of one core)::
289
290	/sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs
291
292and how many milliseconds to wait in khugepaged if there's an hugepage
293allocation failure to throttle the next allocation attempt::
294
295	/sys/kernel/mm/transparent_hugepage/khugepaged/alloc_sleep_millisecs
296
297The khugepaged progress can be seen in the number of pages collapsed (note
298that this counter may not be an exact count of the number of pages
299collapsed, since "collapsed" could mean multiple things: (1) A PTE mapping
300being replaced by a PMD mapping, or (2) physical pages replaced by one
301hugepage of various sizes (PMD-sized or mTHP). Each may happen independently,
302or together, depending on the type of memory and the failures that occur.
303As such, this value should be interpreted roughly as a sign of progress,
304and counters in /proc/vmstat consulted for more accurate accounting.
305Per-order mTHP collapse statistics are also available under
306/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/stats/)::
307
308	/sys/kernel/mm/transparent_hugepage/khugepaged/pages_collapsed
309
310for each pass::
311
312	/sys/kernel/mm/transparent_hugepage/khugepaged/full_scans
313
314``max_ptes_none`` specifies how many empty (none/zero) pages are allowed
315when collapsing a group of small pages into one large page::
316
317	/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none
318
319For PMD-sized THP collapse, this directly limits the number of empty pages
320allowed in the 2MB region.
321
322For mTHP collapse, only 0 or (HPAGE_PMD_NR - 1) are supported. At
323HPAGE_PMD_NR - 1, we collapse to the highest possible order. Any intermediate
324value will emit a warning and mTHP collapse will default to max_ptes_none=0.
325
326A higher value allows more empty pages, potentially leading to more memory
327usage but better THP performance. A lower value is more conservative and
328may result in fewer THP collapses.
329
330``max_ptes_swap`` specifies how many pages can be brought in from
331swap when collapsing a group of pages into a transparent huge page::
332
333	/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_swap
334
335A higher value can cause excessive swap IO and waste
336memory. A lower value can prevent THPs from being
337collapsed, resulting fewer pages being collapsed into
338THPs, and lower memory access performance.
339
340``max_ptes_shared`` specifies how many pages can be shared across multiple
341processes. khugepaged might treat pages of THPs as shared if any page of
342that THP is shared. Exceeding the number would block the collapse::
343
344	/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_shared
345
346A higher value may increase memory footprint for some workloads.
347
348.. note::
349   For mTHP collapse, khugepaged does not support collapsing regions that
350   contain shared or swapped out pages, as this could lead to continuous
351   promotion to higher orders. The collapse will fail if any shared or
352   swapped PTEs are encountered during the scan.
353
354   Currently, madvise_collapse only supports collapsing to PMD-sized THPs
355   and does not attempt mTHP collapses.
356
357Boot parameters
358===============
359
360You can change the sysfs boot time default for the top-level "enabled"
361control by passing the parameter ``transparent_hugepage=always`` or
362``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
363kernel command line.
364
365Alternatively, each supported anonymous THP size can be controlled by
366passing ``thp_anon=<size>[KMG],<size>[KMG]:<state>;<size>[KMG]-<size>[KMG]:<state>``,
367where ``<size>`` is the THP size (must be a power of 2 of PAGE_SIZE and
368supported anonymous THP)  and ``<state>`` is one of ``always``, ``madvise``,
369``never`` or ``inherit``.
370
371For example, the following will set 16K, 32K, 64K THP to ``always``,
372set 128K, 512K to ``inherit``, set 256K to ``madvise`` and 1M, 2M
373to ``never``::
374
375	thp_anon=16K-64K:always;128K,512K:inherit;256K:madvise;1M-2M:never
376
377``thp_anon=`` may be specified multiple times to configure all THP sizes as
378required. If ``thp_anon=`` is specified at least once, any anon THP sizes
379not explicitly configured on the command line are implicitly set to
380``never``.
381
382``transparent_hugepage`` setting only affects the global toggle. If
383``thp_anon`` is not specified, PMD_ORDER THP will default to ``inherit``.
384However, if a valid ``thp_anon`` setting is provided by the user, the
385PMD_ORDER THP policy will be overridden. If the policy for PMD_ORDER
386is not defined within a valid ``thp_anon``, its policy will default to
387``never``.
388
389Similarly to ``transparent_hugepage``, you can control the hugepage
390allocation policy for the internal shmem mount by using the kernel parameter
391``transparent_hugepage_shmem=<policy>``, where ``<policy>`` is one of the
392seven valid policies for shmem (``always``, ``within_size``, ``advise``,
393``never``, ``deny``, and ``force``).
394
395Similarly to ``transparent_hugepage_shmem``, you can control the default
396hugepage allocation policy for the tmpfs mount by using the kernel parameter
397``transparent_hugepage_tmpfs=<policy>``, where ``<policy>`` is one of the
398four valid policies for tmpfs (``always``, ``within_size``, ``advise``,
399``never``). The tmpfs mount default policy is ``never``.
400
401Additionally, Kconfig options are available to set the default hugepage
402policies for shmem (``CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_*``) and tmpfs
403(``CONFIG_TRANSPARENT_HUGEPAGE_TMPFS_HUGE_*``) at build time. Refer to the
404Kconfig help for more details.
405
406In the same manner as ``thp_anon`` controls each supported anonymous THP
407size, ``thp_shmem`` controls each supported shmem THP size. ``thp_shmem``
408has the same format as ``thp_anon``, but also supports the policy
409``within_size``.
410
411``thp_shmem=`` may be specified multiple times to configure all THP sizes
412as required. If ``thp_shmem=`` is specified at least once, any shmem THP
413sizes not explicitly configured on the command line are implicitly set to
414``never``.
415
416``transparent_hugepage_shmem`` setting only affects the global toggle. If
417``thp_shmem`` is not specified, PMD_ORDER hugepage will default to
418``inherit``. However, if a valid ``thp_shmem`` setting is provided by the
419user, the PMD_ORDER hugepage policy will be overridden. If the policy for
420PMD_ORDER is not defined within a valid ``thp_shmem``, its policy will
421default to ``never``.
422
423Hugepages in tmpfs/shmem
424========================
425
426Traditionally, tmpfs only supported a single huge page size ("PMD"). Today,
427it also supports smaller sizes just like anonymous memory, often referred
428to as "multi-size THP" (mTHP). Huge pages of any size are commonly
429represented in the kernel as "large folios".
430
431While there is fine control over the huge page sizes to use for the internal
432shmem mount (see below), ordinary tmpfs mounts will make use of all available
433huge page sizes without any control over the exact sizes, behaving more like
434other file systems.
435
436tmpfs mounts
437------------
438
439The THP allocation policy for tmpfs mounts can be adjusted using the mount
440option: ``huge=``. It can have following values:
441
442always
443    Attempt to allocate huge pages every time we need a new page;
444    Always try PMD-sized huge pages first, and fall back to smaller-sized
445    huge pages if the PMD-sized huge page allocation fails;
446
447never
448    Do not allocate huge pages. Note that ``madvise(..., MADV_COLLAPSE)``
449    can still cause transparent huge pages to be obtained even if this mode
450    is specified everywhere;
451
452within_size
453    Only allocate huge page if it will be fully within i_size;
454    Always try PMD-sized huge pages first, and fall back to smaller-sized
455    huge pages if the PMD-sized huge page allocation fails;
456    Also respect madvise() hints;
457
458advise
459    Only allocate huge pages if requested with madvise();
460
461Remember, that the kernel may use huge pages of all available sizes, and
462that no fine control as for the internal tmpfs mount is available.
463
464The default policy in the past was ``never``, but it can now be adjusted
465using the kernel parameter ``transparent_hugepage_tmpfs=<policy>``.
466
467``mount -o remount,huge= /mountpoint`` works fine after mount: remounting
468``huge=never`` will not attempt to break up huge pages at all, just stop more
469from being allocated.
470
471In addition to policies listed above, the sysfs knob
472/sys/kernel/mm/transparent_hugepage/shmem_enabled will affect the
473allocation policy of tmpfs mounts, when set to the following values:
474
475deny
476    For use in emergencies, to force the huge option off from
477    all mounts;
478force
479    Force the huge option on for all - very useful for testing;
480
481shmem / internal tmpfs
482----------------------
483The mount internal tmpfs mount is used for SysV SHM, memfds, shared anonymous
484mmaps (of /dev/zero or MAP_ANONYMOUS), GPU drivers' DRM  objects, Ashmem.
485
486To control the THP allocation policy for this internal tmpfs mount, the
487sysfs knob /sys/kernel/mm/transparent_hugepage/shmem_enabled and the knobs
488per THP size in
489'/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/shmem_enabled'
490can be used.
491
492The global knob has the same semantics as the ``huge=`` mount options
493for tmpfs mounts, except that the different huge page sizes can be controlled
494individually, and will only use the setting of the global knob when the
495per-size knob is set to 'inherit'.
496
497The options 'force' and 'deny' are dropped for the individual sizes, which
498are rather testing artifacts from the old ages.
499
500always
501    Attempt to allocate <size> huge pages every time we need a new page;
502
503inherit
504    Inherit the top-level "shmem_enabled" value. By default, PMD-sized hugepages
505    have enabled="inherit" and all other hugepage sizes have enabled="never";
506
507never
508    Do not allocate <size> huge pages. Note that ``madvise(...,
509    MADV_COLLAPSE)`` can still cause transparent huge pages to be obtained
510    even if this mode is specified everywhere;
511
512within_size
513    Only allocate <size> huge page if it will be fully within i_size.
514    Also respect madvise() hints;
515
516advise
517    Only allocate <size> huge pages if requested with madvise();
518
519Need of application restart
520===========================
521
522The transparent_hugepage/enabled and
523transparent_hugepage/hugepages-<size>kB/enabled values and tmpfs mount
524option only affect future behavior. So to make them effective you need
525to restart any application that could have been using hugepages. This
526also applies to the regions registered in khugepaged.
527
528Monitoring usage
529================
530
531The number of PMD-sized anonymous transparent huge pages currently used by the
532system is available by reading the AnonHugePages field in ``/proc/meminfo``.
533To identify what applications are using PMD-sized anonymous transparent huge
534pages, it is necessary to read ``/proc/PID/smaps`` and count the AnonHugePages
535fields for each mapping. (Note that AnonHugePages only applies to traditional
536PMD-sized THP for historical reasons and should have been called
537AnonHugePmdMapped).
538
539The number of file transparent huge pages mapped to userspace is available
540by reading ShmemPmdMapped and ShmemHugePages fields in ``/proc/meminfo``.
541To identify what applications are mapping file transparent huge pages, it
542is necessary to read ``/proc/PID/smaps`` and count the FilePmdMapped fields
543for each mapping.
544
545Note that reading the smaps file is expensive and reading it
546frequently will incur overhead.
547
548There are a number of counters in ``/proc/vmstat`` that may be used to
549monitor how successfully the system is providing huge pages for use.
550
551thp_fault_alloc
552	is incremented every time a huge page is successfully
553	allocated and charged to handle a page fault.
554
555thp_collapse_alloc
556	is incremented by khugepaged when it has found
557	a range of pages to collapse into one huge page and has
558	successfully allocated a new huge page to store the data.
559
560thp_fault_fallback
561	is incremented if a page fault fails to allocate or charge
562	a huge page and instead falls back to using small pages.
563
564thp_fault_fallback_charge
565	is incremented if a page fault fails to charge a huge page and
566	instead falls back to using small pages even though the
567	allocation was successful.
568
569thp_collapse_alloc_failed
570	is incremented if khugepaged found a range
571	of pages that should be collapsed into one huge page but failed
572	the allocation.
573
574thp_file_alloc
575	is incremented every time a shmem huge page is successfully
576	allocated (Note that despite being named after "file", the counter
577	measures only shmem).
578
579thp_file_fallback
580	is incremented if a shmem huge page is attempted to be allocated
581	but fails and instead falls back to using small pages. (Note that
582	despite being named after "file", the counter measures only shmem).
583
584thp_file_fallback_charge
585	is incremented if a shmem huge page cannot be charged and instead
586	falls back to using small pages even though the allocation was
587	successful. (Note that despite being named after "file", the
588	counter measures only shmem).
589
590thp_file_mapped
591	is incremented every time a file or shmem huge page is mapped into
592	user address space.
593
594thp_split_page
595	is incremented every time a huge page is split into base
596	pages. This can happen for a variety of reasons but a common
597	reason is that a huge page is old and is being reclaimed.
598	This action implies splitting all PMD the page mapped with.
599
600thp_split_page_failed
601	is incremented if kernel fails to split huge
602	page. This can happen if the page was pinned by somebody.
603
604thp_deferred_split_page
605	is incremented when a huge page is put onto split
606	queue. This happens when a huge page is partially unmapped and
607	splitting it would free up some memory. Pages on split queue are
608	going to be split under memory pressure.
609
610thp_underused_split_page
611	is incremented when a huge page on the split queue was split
612	because it was underused. A THP is underused if the number of
613	zero pages in the THP is above a certain threshold
614	(/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none).
615
616thp_split_pmd
617	is incremented every time a PMD split into table of PTEs.
618	This can happen, for instance, when application calls mprotect() or
619	munmap() on part of huge page. It doesn't split huge page, only
620	page table entry.
621
622thp_zero_page_alloc
623	is incremented every time a huge zero page used for thp is
624	successfully allocated. Note, it doesn't count every map of
625	the huge zero page, only its allocation.
626
627thp_zero_page_alloc_failed
628	is incremented if kernel fails to allocate
629	huge zero page and falls back to using small pages.
630
631thp_swpout
632	is incremented every time a huge page is swapout in one
633	piece without splitting.
634
635thp_swpout_fallback
636	is incremented if a huge page has to be split before swapout.
637	Usually because failed to allocate some continuous swap space
638	for the huge page.
639
640In /sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/stats, There are
641also individual counters for each huge page size, which can be utilized to
642monitor the system's effectiveness in providing huge pages for usage. Each
643counter has its own corresponding file.
644
645anon_fault_alloc
646	is incremented every time a huge page is successfully
647	allocated and charged to handle a page fault.
648
649anon_fault_fallback
650	is incremented if a page fault fails to allocate or charge
651	a huge page and instead falls back to using huge pages with
652	lower orders or small pages.
653
654anon_fault_fallback_charge
655	is incremented if a page fault fails to charge a huge page and
656	instead falls back to using huge pages with lower orders or
657	small pages even though the allocation was successful.
658
659collapse_alloc
660	is incremented every time a huge page is successfully allocated for a
661	khugepaged collapse.
662
663collapse_alloc_failed
664	is incremented every time a huge page allocation fails during a
665	khugepaged collapse.
666
667zswpout
668	is incremented every time a huge page is swapped out to zswap in one
669	piece without splitting.
670
671swpin
672	is incremented every time a huge page is swapped in from a non-zswap
673	swap device in one piece.
674
675swpin_fallback
676	is incremented if swapin fails to allocate or charge a huge page
677	and instead falls back to using huge pages with lower orders or
678	small pages.
679
680swpin_fallback_charge
681	is incremented if swapin fails to charge a huge page and instead
682	falls back to using  huge pages with lower orders or small pages
683	even though the allocation was successful.
684
685swpout
686	is incremented every time a huge page is swapped out to a non-zswap
687	swap device in one piece without splitting.
688
689swpout_fallback
690	is incremented if a huge page has to be split before swapout.
691	Usually because failed to allocate some continuous swap space
692	for the huge page.
693
694shmem_alloc
695	is incremented every time a shmem huge page is successfully
696	allocated.
697
698shmem_fallback
699	is incremented if a shmem huge page is attempted to be allocated
700	but fails and instead falls back to using small pages.
701
702shmem_fallback_charge
703	is incremented if a shmem huge page cannot be charged and instead
704	falls back to using small pages even though the allocation was
705	successful.
706
707split
708	is incremented every time a huge page is successfully split into
709	smaller orders. This can happen for a variety of reasons but a
710	common reason is that a huge page is old and is being reclaimed.
711
712split_failed
713	is incremented if kernel fails to split huge
714	page. This can happen if the page was pinned by somebody.
715
716split_deferred
717        is incremented when a huge page is put onto split queue.
718        This happens when a huge page is partially unmapped and splitting
719        it would free up some memory. Pages on split queue are going to
720        be split under memory pressure, if splitting is possible.
721
722nr_anon
723       the number of anonymous THP we have in the whole system. These THPs
724       might be currently entirely mapped or have partially unmapped/unused
725       subpages.
726
727nr_anon_partially_mapped
728       the number of anonymous THP which are likely partially mapped, possibly
729       wasting memory, and have been queued for deferred memory reclamation.
730       Note that in corner some cases (e.g., failed migration), we might detect
731       an anonymous THP as "partially mapped" and count it here, even though it
732       is not actually partially mapped anymore.
733
734collapse_exceed_none_pte
735       The number of collapse attempts that failed due to exceeding the
736       max_ptes_none threshold.
737
738collapse_exceed_swap_pte
739       The number of collapse attempts that failed due to exceeding the
740       max_ptes_swap threshold. For non-PMD orders this occurs if a mTHP range
741       contains at least one swap PTE.
742
743collapse_exceed_shared_pte
744       The number of collapse attempts that failed due to exceeding the
745       max_ptes_shared threshold. For non-PMD orders this occurs if a mTHP range
746       contains at least one shared PTE.
747
748As the system ages, allocating huge pages may be expensive as the
749system uses memory compaction to copy data around memory to free a
750huge page for use. There are some counters in ``/proc/vmstat`` to help
751monitor this overhead.
752
753compact_stall
754	is incremented every time a process stalls to run
755	memory compaction so that a huge page is free for use.
756
757compact_success
758	is incremented if the system compacted memory and
759	freed a huge page for use.
760
761compact_fail
762	is incremented if the system tries to compact memory
763	but failed.
764
765It is possible to establish how long the stalls were using the function
766tracer to record how long was spent in the page allocator and
767using the mm_page_alloc tracepoint to identify which allocations were
768for huge pages.
769
770Optimizing the applications
771===========================
772
773To be guaranteed that the kernel will map a THP immediately in any
774memory region, the mmap region has to be hugepage naturally
775aligned. posix_memalign() can provide that guarantee.
776
777Hugetlbfs
778=========
779
780You can use hugetlbfs on a kernel that has transparent hugepage
781support enabled just fine as always. No difference can be noted in
782hugetlbfs other than there will be less overall fragmentation. All
783usual features belonging to hugetlbfs are preserved and
784unaffected. libhugetlbfs will also work fine as usual.
785