xref: /OK3568_Linux_fs/kernel/Documentation/vm/transhuge.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. _transhuge:
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun============================
4*4882a593SmuzhiyunTransparent Hugepage Support
5*4882a593Smuzhiyun============================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThis document describes design principles for Transparent Hugepage (THP)
8*4882a593Smuzhiyunsupport and its interaction with other parts of the memory management
9*4882a593Smuzhiyunsystem.
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunDesign principles
12*4882a593Smuzhiyun=================
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun- "graceful fallback": mm components which don't have transparent hugepage
15*4882a593Smuzhiyun  knowledge fall back to breaking huge pmd mapping into table of ptes and,
16*4882a593Smuzhiyun  if necessary, split a transparent hugepage. Therefore these components
17*4882a593Smuzhiyun  can continue working on the regular pages or regular pte mappings.
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun- if a hugepage allocation fails because of memory fragmentation,
20*4882a593Smuzhiyun  regular pages should be gracefully allocated instead and mixed in
21*4882a593Smuzhiyun  the same vma without any failure or significant delay and without
22*4882a593Smuzhiyun  userland noticing
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun- if some task quits and more hugepages become available (either
25*4882a593Smuzhiyun  immediately in the buddy or through the VM), guest physical memory
26*4882a593Smuzhiyun  backed by regular pages should be relocated on hugepages
27*4882a593Smuzhiyun  automatically (with khugepaged)
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun- it doesn't require memory reservation and in turn it uses hugepages
30*4882a593Smuzhiyun  whenever possible (the only possible reservation here is kernelcore=
31*4882a593Smuzhiyun  to avoid unmovable pages to fragment all the memory but such a tweak
32*4882a593Smuzhiyun  is not specific to transparent hugepage support and it's a generic
33*4882a593Smuzhiyun  feature that applies to all dynamic high order allocations in the
34*4882a593Smuzhiyun  kernel)
35*4882a593Smuzhiyun
36*4882a593Smuzhiyunget_user_pages and follow_page
37*4882a593Smuzhiyun==============================
38*4882a593Smuzhiyun
39*4882a593Smuzhiyunget_user_pages and follow_page if run on a hugepage, will return the
40*4882a593Smuzhiyunhead or tail pages as usual (exactly as they would do on
41*4882a593Smuzhiyunhugetlbfs). Most GUP users will only care about the actual physical
42*4882a593Smuzhiyunaddress of the page and its temporary pinning to release after the I/O
43*4882a593Smuzhiyunis complete, so they won't ever notice the fact the page is huge. But
44*4882a593Smuzhiyunif any driver is going to mangle over the page structure of the tail
45*4882a593Smuzhiyunpage (like for checking page->mapping or other bits that are relevant
46*4882a593Smuzhiyunfor the head page and not the tail page), it should be updated to jump
47*4882a593Smuzhiyunto check head page instead. Taking a reference on any head/tail page would
48*4882a593Smuzhiyunprevent the page from being split by anyone.
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun.. note::
51*4882a593Smuzhiyun   these aren't new constraints to the GUP API, and they match the
52*4882a593Smuzhiyun   same constraints that apply to hugetlbfs too, so any driver capable
53*4882a593Smuzhiyun   of handling GUP on hugetlbfs will also work fine on transparent
54*4882a593Smuzhiyun   hugepage backed mappings.
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunIn case you can't handle compound pages if they're returned by
57*4882a593Smuzhiyunfollow_page, the FOLL_SPLIT bit can be specified as a parameter to
58*4882a593Smuzhiyunfollow_page, so that it will split the hugepages before returning
59*4882a593Smuzhiyunthem.
60*4882a593Smuzhiyun
61*4882a593SmuzhiyunGraceful fallback
62*4882a593Smuzhiyun=================
63*4882a593Smuzhiyun
64*4882a593SmuzhiyunCode walking pagetables but unaware about huge pmds can simply call
65*4882a593Smuzhiyunsplit_huge_pmd(vma, pmd, addr) where the pmd is the one returned by
66*4882a593Smuzhiyunpmd_offset. It's trivial to make the code transparent hugepage aware
67*4882a593Smuzhiyunby just grepping for "pmd_offset" and adding split_huge_pmd where
68*4882a593Smuzhiyunmissing after pmd_offset returns the pmd. Thanks to the graceful
69*4882a593Smuzhiyunfallback design, with a one liner change, you can avoid to write
70*4882a593Smuzhiyunhundreds if not thousands of lines of complex code to make your code
71*4882a593Smuzhiyunhugepage aware.
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunIf you're not walking pagetables but you run into a physical hugepage
74*4882a593Smuzhiyunthat you can't handle natively in your code, you can split it by
75*4882a593Smuzhiyuncalling split_huge_page(page). This is what the Linux VM does before
76*4882a593Smuzhiyunit tries to swapout the hugepage for example. split_huge_page() can fail
77*4882a593Smuzhiyunif the page is pinned and you must handle this correctly.
78*4882a593Smuzhiyun
79*4882a593SmuzhiyunExample to make mremap.c transparent hugepage aware with a one liner
80*4882a593Smuzhiyunchange::
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun	diff --git a/mm/mremap.c b/mm/mremap.c
83*4882a593Smuzhiyun	--- a/mm/mremap.c
84*4882a593Smuzhiyun	+++ b/mm/mremap.c
85*4882a593Smuzhiyun	@@ -41,6 +41,7 @@ static pmd_t *get_old_pmd(struct mm_stru
86*4882a593Smuzhiyun			return NULL;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun		pmd = pmd_offset(pud, addr);
89*4882a593Smuzhiyun	+	split_huge_pmd(vma, pmd, addr);
90*4882a593Smuzhiyun		if (pmd_none_or_clear_bad(pmd))
91*4882a593Smuzhiyun			return NULL;
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunLocking in hugepage aware code
94*4882a593Smuzhiyun==============================
95*4882a593Smuzhiyun
96*4882a593SmuzhiyunWe want as much code as possible hugepage aware, as calling
97*4882a593Smuzhiyunsplit_huge_page() or split_huge_pmd() has a cost.
98*4882a593Smuzhiyun
99*4882a593SmuzhiyunTo make pagetable walks huge pmd aware, all you need to do is to call
100*4882a593Smuzhiyunpmd_trans_huge() on the pmd returned by pmd_offset. You must hold the
101*4882a593Smuzhiyunmmap_lock in read (or write) mode to be sure a huge pmd cannot be
102*4882a593Smuzhiyuncreated from under you by khugepaged (khugepaged collapse_huge_page
103*4882a593Smuzhiyuntakes the mmap_lock in write mode in addition to the anon_vma lock). If
104*4882a593Smuzhiyunpmd_trans_huge returns false, you just fallback in the old code
105*4882a593Smuzhiyunpaths. If instead pmd_trans_huge returns true, you have to take the
106*4882a593Smuzhiyunpage table lock (pmd_lock()) and re-run pmd_trans_huge. Taking the
107*4882a593Smuzhiyunpage table lock will prevent the huge pmd being converted into a
108*4882a593Smuzhiyunregular pmd from under you (split_huge_pmd can run in parallel to the
109*4882a593Smuzhiyunpagetable walk). If the second pmd_trans_huge returns false, you
110*4882a593Smuzhiyunshould just drop the page table lock and fallback to the old code as
111*4882a593Smuzhiyunbefore. Otherwise, you can proceed to process the huge pmd and the
112*4882a593Smuzhiyunhugepage natively. Once finished, you can drop the page table lock.
113*4882a593Smuzhiyun
114*4882a593SmuzhiyunRefcounts and transparent huge pages
115*4882a593Smuzhiyun====================================
116*4882a593Smuzhiyun
117*4882a593SmuzhiyunRefcounting on THP is mostly consistent with refcounting on other compound
118*4882a593Smuzhiyunpages:
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun  - get_page()/put_page() and GUP operate on head page's ->_refcount.
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun  - ->_refcount in tail pages is always zero: get_page_unless_zero() never
123*4882a593Smuzhiyun    succeeds on tail pages.
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun  - map/unmap of the pages with PTE entry increment/decrement ->_mapcount
126*4882a593Smuzhiyun    on relevant sub-page of the compound page.
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun  - map/unmap of the whole compound page is accounted for in compound_mapcount
129*4882a593Smuzhiyun    (stored in first tail page). For file huge pages, we also increment
130*4882a593Smuzhiyun    ->_mapcount of all sub-pages in order to have race-free detection of
131*4882a593Smuzhiyun    last unmap of subpages.
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunPageDoubleMap() indicates that the page is *possibly* mapped with PTEs.
134*4882a593Smuzhiyun
135*4882a593SmuzhiyunFor anonymous pages, PageDoubleMap() also indicates ->_mapcount in all
136*4882a593Smuzhiyunsubpages is offset up by one. This additional reference is required to
137*4882a593Smuzhiyunget race-free detection of unmap of subpages when we have them mapped with
138*4882a593Smuzhiyunboth PMDs and PTEs.
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunThis optimization is required to lower the overhead of per-subpage mapcount
141*4882a593Smuzhiyuntracking. The alternative is to alter ->_mapcount in all subpages on each
142*4882a593Smuzhiyunmap/unmap of the whole compound page.
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunFor anonymous pages, we set PG_double_map when a PMD of the page is split
145*4882a593Smuzhiyunfor the first time, but still have a PMD mapping. The additional references
146*4882a593Smuzhiyungo away with the last compound_mapcount.
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunFile pages get PG_double_map set on the first map of the page with PTE and
149*4882a593Smuzhiyungoes away when the page gets evicted from the page cache.
150*4882a593Smuzhiyun
151*4882a593Smuzhiyunsplit_huge_page internally has to distribute the refcounts in the head
152*4882a593Smuzhiyunpage to the tail pages before clearing all PG_head/tail bits from the page
153*4882a593Smuzhiyunstructures. It can be done easily for refcounts taken by page table
154*4882a593Smuzhiyunentries, but we don't have enough information on how to distribute any
155*4882a593Smuzhiyunadditional pins (i.e. from get_user_pages). split_huge_page() fails any
156*4882a593Smuzhiyunrequests to split pinned huge pages: it expects page count to be equal to
157*4882a593Smuzhiyunthe sum of mapcount of all sub-pages plus one (split_huge_page caller must
158*4882a593Smuzhiyunhave a reference to the head page).
159*4882a593Smuzhiyun
160*4882a593Smuzhiyunsplit_huge_page uses migration entries to stabilize page->_refcount and
161*4882a593Smuzhiyunpage->_mapcount of anonymous pages. File pages just get unmapped.
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunWe are safe against physical memory scanners too: the only legitimate way
164*4882a593Smuzhiyuna scanner can get a reference to a page is get_page_unless_zero().
165*4882a593Smuzhiyun
166*4882a593SmuzhiyunAll tail pages have zero ->_refcount until atomic_add(). This prevents the
167*4882a593Smuzhiyunscanner from getting a reference to the tail page up to that point. After the
168*4882a593Smuzhiyunatomic_add() we don't care about the ->_refcount value. We already know how
169*4882a593Smuzhiyunmany references should be uncharged from the head page.
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunFor head page get_page_unless_zero() will succeed and we don't mind. It's
172*4882a593Smuzhiyunclear where references should go after split: it will stay on the head page.
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunNote that split_huge_pmd() doesn't have any limitations on refcounting:
175*4882a593Smuzhiyunpmd can be split at any point and never fails.
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunPartial unmap and deferred_split_huge_page()
178*4882a593Smuzhiyun============================================
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunUnmapping part of THP (with munmap() or other way) is not going to free
181*4882a593Smuzhiyunmemory immediately. Instead, we detect that a subpage of THP is not in use
182*4882a593Smuzhiyunin page_remove_rmap() and queue the THP for splitting if memory pressure
183*4882a593Smuzhiyuncomes. Splitting will free up unused subpages.
184*4882a593Smuzhiyun
185*4882a593SmuzhiyunSplitting the page right away is not an option due to locking context in
186*4882a593Smuzhiyunthe place where we can detect partial unmap. It also might be
187*4882a593Smuzhiyuncounterproductive since in many cases partial unmap happens during exit(2) if
188*4882a593Smuzhiyuna THP crosses a VMA boundary.
189*4882a593Smuzhiyun
190*4882a593SmuzhiyunThe function deferred_split_huge_page() is used to queue a page for splitting.
191*4882a593SmuzhiyunThe splitting itself will happen when we get memory pressure via shrinker
192*4882a593Smuzhiyuninterface.
193