blob: 37c57ca326297a500a507e036bde0aeeaa47c0d2 [file] [log] [blame]
Mike Rapoport44f380f2018-03-21 21:22:41 +02001.. _transhuge:
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -08002
Mike Rapoport44f380f2018-03-21 21:22:41 +02003============================
4Transparent Hugepage Support
5============================
6
Ralph Campbell41f0a952019-04-26 11:04:29 -07007This document describes design principles for Transparent Hugepage (THP)
8support and its interaction with other parts of the memory management
9system.
Mike Rapoport07a83032018-05-14 11:13:38 +030010
11Design principles
12=================
13
14- "graceful fallback": mm components which don't have transparent hugepage
15 knowledge fall back to breaking huge pmd mapping into table of ptes and,
16 if necessary, split a transparent hugepage. Therefore these components
17 can continue working on the regular pages or regular pte mappings.
18
19- if a hugepage allocation fails because of memory fragmentation,
20 regular pages should be gracefully allocated instead and mixed in
21 the same vma without any failure or significant delay and without
22 userland noticing
23
24- if some task quits and more hugepages become available (either
25 immediately in the buddy or through the VM), guest physical memory
26 backed by regular pages should be relocated on hugepages
27 automatically (with khugepaged)
28
29- it doesn't require memory reservation and in turn it uses hugepages
30 whenever possible (the only possible reservation here is kernelcore=
31 to avoid unmovable pages to fragment all the memory but such a tweak
32 is not specific to transparent hugepage support and it's a generic
33 feature that applies to all dynamic high order allocations in the
34 kernel)
35
Mike Rapoport44f380f2018-03-21 21:22:41 +020036get_user_pages and follow_page
37==============================
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080038
39get_user_pages and follow_page if run on a hugepage, will return the
40head or tail pages as usual (exactly as they would do on
Ralph Campbell41f0a952019-04-26 11:04:29 -070041hugetlbfs). Most GUP users will only care about the actual physical
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080042address of the page and its temporary pinning to release after the I/O
43is complete, so they won't ever notice the fact the page is huge. But
44if any driver is going to mangle over the page structure of the tail
45page (like for checking page->mapping or other bits that are relevant
46for the head page and not the tail page), it should be updated to jump
Ralph Campbell41f0a952019-04-26 11:04:29 -070047to check head page instead. Taking a reference on any head/tail page would
48prevent the page from being split by anyone.
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080049
Mike Rapoport44f380f2018-03-21 21:22:41 +020050.. note::
51 these aren't new constraints to the GUP API, and they match the
Ralph Campbell41f0a952019-04-26 11:04:29 -070052 same constraints that apply to hugetlbfs too, so any driver capable
Mike Rapoport44f380f2018-03-21 21:22:41 +020053 of handling GUP on hugetlbfs will also work fine on transparent
54 hugepage backed mappings.
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080055
56In case you can't handle compound pages if they're returned by
Ralph Campbell41f0a952019-04-26 11:04:29 -070057follow_page, the FOLL_SPLIT bit can be specified as a parameter to
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080058follow_page, so that it will split the hugepages before returning
Yang Shia4966962019-04-19 04:17:04 +080059them.
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080060
Mike Rapoport44f380f2018-03-21 21:22:41 +020061Graceful fallback
62=================
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080063
Eric Engestrom89474d52016-05-20 16:58:07 -070064Code walking pagetables but unaware about huge pmds can simply call
Kirill A. Shutemova46e6372016-01-15 16:54:30 -080065split_huge_pmd(vma, pmd, addr) where the pmd is the one returned by
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080066pmd_offset. It's trivial to make the code transparent hugepage aware
Kirill A. Shutemova46e6372016-01-15 16:54:30 -080067by just grepping for "pmd_offset" and adding split_huge_pmd where
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080068missing after pmd_offset returns the pmd. Thanks to the graceful
69fallback design, with a one liner change, you can avoid to write
Ralph Campbell41f0a952019-04-26 11:04:29 -070070hundreds if not thousands of lines of complex code to make your code
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080071hugepage aware.
72
73If you're not walking pagetables but you run into a physical hugepage
Ralph Campbell41f0a952019-04-26 11:04:29 -070074that you can't handle natively in your code, you can split it by
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080075calling split_huge_page(page). This is what the Linux VM does before
Kirill A. Shutemova46e6372016-01-15 16:54:30 -080076it tries to swapout the hugepage for example. split_huge_page() can fail
77if the page is pinned and you must handle this correctly.
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080078
79Example to make mremap.c transparent hugepage aware with a one liner
Mike Rapoport44f380f2018-03-21 21:22:41 +020080change::
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080081
Mike Rapoport44f380f2018-03-21 21:22:41 +020082 diff --git a/mm/mremap.c b/mm/mremap.c
83 --- a/mm/mremap.c
84 +++ b/mm/mremap.c
85 @@ -41,6 +41,7 @@ static pmd_t *get_old_pmd(struct mm_stru
86 return NULL;
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080087
Mike Rapoport44f380f2018-03-21 21:22:41 +020088 pmd = pmd_offset(pud, addr);
89 + split_huge_pmd(vma, pmd, addr);
90 if (pmd_none_or_clear_bad(pmd))
91 return NULL;
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080092
Mike Rapoport44f380f2018-03-21 21:22:41 +020093Locking in hugepage aware code
94==============================
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080095
96We want as much code as possible hugepage aware, as calling
Kirill A. Shutemova46e6372016-01-15 16:54:30 -080097split_huge_page() or split_huge_pmd() has a cost.
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -080098
99To make pagetable walks huge pmd aware, all you need to do is to call
100pmd_trans_huge() on the pmd returned by pmd_offset. You must hold the
Ralph Campbell41f0a952019-04-26 11:04:29 -0700101mmap_sem in read (or write) mode to be sure a huge pmd cannot be
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -0800102created from under you by khugepaged (khugepaged collapse_huge_page
103takes the mmap_sem in write mode in addition to the anon_vma lock). If
104pmd_trans_huge returns false, you just fallback in the old code
105paths. If instead pmd_trans_huge returns true, you have to take the
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800106page table lock (pmd_lock()) and re-run pmd_trans_huge. Taking the
Ralph Campbell41f0a952019-04-26 11:04:29 -0700107page table lock will prevent the huge pmd being converted into a
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800108regular pmd from under you (split_huge_pmd can run in parallel to the
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -0800109pagetable walk). If the second pmd_trans_huge returns false, you
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800110should just drop the page table lock and fallback to the old code as
Ralph Campbell41f0a952019-04-26 11:04:29 -0700111before. Otherwise, you can proceed to process the huge pmd and the
112hugepage natively. Once finished, you can drop the page table lock.
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -0800113
Mike Rapoport44f380f2018-03-21 21:22:41 +0200114Refcounts and transparent huge pages
115====================================
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800116
117Refcounting on THP is mostly consistent with refcounting on other compound
118pages:
119
Ralph Campbell41f0a952019-04-26 11:04:29 -0700120 - get_page()/put_page() and GUP operate on head page's ->_refcount.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800121
Joonsoo Kim0139aa72016-05-19 17:10:49 -0700122 - ->_refcount in tail pages is always zero: get_page_unless_zero() never
Ralph Campbell41f0a952019-04-26 11:04:29 -0700123 succeeds on tail pages.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800124
125 - map/unmap of the pages with PTE entry increment/decrement ->_mapcount
126 on relevant sub-page of the compound page.
127
Ralph Campbell41f0a952019-04-26 11:04:29 -0700128 - map/unmap of the whole compound page is accounted for in compound_mapcount
Kirill A. Shutemov1b5946a2016-07-26 15:26:40 -0700129 (stored in first tail page). For file huge pages, we also increment
130 ->_mapcount of all sub-pages in order to have race-free detection of
131 last unmap of subpages.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800132
Kirill A. Shutemov1b5946a2016-07-26 15:26:40 -0700133PageDoubleMap() indicates that the page is *possibly* mapped with PTEs.
134
Ralph Campbell41f0a952019-04-26 11:04:29 -0700135For anonymous pages, PageDoubleMap() also indicates ->_mapcount in all
Kirill A. Shutemov1b5946a2016-07-26 15:26:40 -0700136subpages is offset up by one. This additional reference is required to
137get race-free detection of unmap of subpages when we have them mapped with
138both PMDs and PTEs.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800139
Ralph Campbell41f0a952019-04-26 11:04:29 -0700140This optimization is required to lower the overhead of per-subpage mapcount
141tracking. The alternative is to alter ->_mapcount in all subpages on each
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800142map/unmap of the whole compound page.
143
Ralph Campbell41f0a952019-04-26 11:04:29 -0700144For anonymous pages, we set PG_double_map when a PMD of the page is split
145for the first time, but still have a PMD mapping. The additional references
146go away with the last compound_mapcount.
Kirill A. Shutemov1b5946a2016-07-26 15:26:40 -0700147
Ralph Campbell41f0a952019-04-26 11:04:29 -0700148File pages get PG_double_map set on the first map of the page with PTE and
149goes away when the page gets evicted from the page cache.
Andrea Arcangeli1c9bf222011-01-13 15:46:30 -0800150
151split_huge_page internally has to distribute the refcounts in the head
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800152page to the tail pages before clearing all PG_head/tail bits from the page
153structures. It can be done easily for refcounts taken by page table
Ralph Campbell41f0a952019-04-26 11:04:29 -0700154entries, but we don't have enough information on how to distribute any
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800155additional pins (i.e. from get_user_pages). split_huge_page() fails any
Ralph Campbell41f0a952019-04-26 11:04:29 -0700156requests to split pinned huge pages: it expects page count to be equal to
157the sum of mapcount of all sub-pages plus one (split_huge_page caller must
158have a reference to the head page).
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800159
Joonsoo Kim0139aa72016-05-19 17:10:49 -0700160split_huge_page uses migration entries to stabilize page->_refcount and
Ralph Campbell41f0a952019-04-26 11:04:29 -0700161page->_mapcount of anonymous pages. File pages just get unmapped.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800162
Ralph Campbell41f0a952019-04-26 11:04:29 -0700163We are safe against physical memory scanners too: the only legitimate way
164a scanner can get a reference to a page is get_page_unless_zero().
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800165
Eric Engestrom89474d52016-05-20 16:58:07 -0700166All tail pages have zero ->_refcount until atomic_add(). This prevents the
167scanner from getting a reference to the tail page up to that point. After the
Ralph Campbell41f0a952019-04-26 11:04:29 -0700168atomic_add() we don't care about the ->_refcount value. We already know how
Eric Engestrom89474d52016-05-20 16:58:07 -0700169many references should be uncharged from the head page.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800170
171For head page get_page_unless_zero() will succeed and we don't mind. It's
Ralph Campbell41f0a952019-04-26 11:04:29 -0700172clear where references should go after split: it will stay on the head page.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800173
Ralph Campbell41f0a952019-04-26 11:04:29 -0700174Note that split_huge_pmd() doesn't have any limitations on refcounting:
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800175pmd can be split at any point and never fails.
176
Mike Rapoport44f380f2018-03-21 21:22:41 +0200177Partial unmap and deferred_split_huge_page()
178============================================
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800179
180Unmapping part of THP (with munmap() or other way) is not going to free
181memory immediately. Instead, we detect that a subpage of THP is not in use
182in page_remove_rmap() and queue the THP for splitting if memory pressure
183comes. Splitting will free up unused subpages.
184
185Splitting the page right away is not an option due to locking context in
Ralph Campbell41f0a952019-04-26 11:04:29 -0700186the place where we can detect partial unmap. It also might be
SeongJae Park929f9d22017-05-08 15:59:02 -0700187counterproductive since in many cases partial unmap happens during exit(2) if
188a THP crosses a VMA boundary.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800189
Ralph Campbell41f0a952019-04-26 11:04:29 -0700190The function deferred_split_huge_page() is used to queue a page for splitting.
Kirill A. Shutemova46e6372016-01-15 16:54:30 -0800191The splitting itself will happen when we get memory pressure via shrinker
192interface.