Back to home page

OSCL-LXR

 
 

    


0001 .. _transhuge:
0002 
0003 ============================
0004 Transparent Hugepage Support
0005 ============================
0006 
0007 This document describes design principles for Transparent Hugepage (THP)
0008 support and its interaction with other parts of the memory management
0009 system.
0010 
0011 Design principles
0012 =================
0013 
0014 - "graceful fallback": mm components which don't have transparent hugepage
0015   knowledge fall back to breaking huge pmd mapping into table of ptes and,
0016   if necessary, split a transparent hugepage. Therefore these components
0017   can continue working on the regular pages or regular pte mappings.
0018 
0019 - if a hugepage allocation fails because of memory fragmentation,
0020   regular pages should be gracefully allocated instead and mixed in
0021   the same vma without any failure or significant delay and without
0022   userland noticing
0023 
0024 - if some task quits and more hugepages become available (either
0025   immediately in the buddy or through the VM), guest physical memory
0026   backed by regular pages should be relocated on hugepages
0027   automatically (with khugepaged)
0028 
0029 - it doesn't require memory reservation and in turn it uses hugepages
0030   whenever possible (the only possible reservation here is kernelcore=
0031   to avoid unmovable pages to fragment all the memory but such a tweak
0032   is not specific to transparent hugepage support and it's a generic
0033   feature that applies to all dynamic high order allocations in the
0034   kernel)
0035 
0036 get_user_pages and follow_page
0037 ==============================
0038 
0039 get_user_pages and follow_page if run on a hugepage, will return the
0040 head or tail pages as usual (exactly as they would do on
0041 hugetlbfs). Most GUP users will only care about the actual physical
0042 address of the page and its temporary pinning to release after the I/O
0043 is complete, so they won't ever notice the fact the page is huge. But
0044 if any driver is going to mangle over the page structure of the tail
0045 page (like for checking page->mapping or other bits that are relevant
0046 for the head page and not the tail page), it should be updated to jump
0047 to check head page instead. Taking a reference on any head/tail page would
0048 prevent the page from being split by anyone.
0049 
0050 .. note::
0051    these aren't new constraints to the GUP API, and they match the
0052    same constraints that apply to hugetlbfs too, so any driver capable
0053    of handling GUP on hugetlbfs will also work fine on transparent
0054    hugepage backed mappings.
0055 
0056 Graceful fallback
0057 =================
0058 
0059 Code walking pagetables but unaware about huge pmds can simply call
0060 split_huge_pmd(vma, pmd, addr) where the pmd is the one returned by
0061 pmd_offset. It's trivial to make the code transparent hugepage aware
0062 by just grepping for "pmd_offset" and adding split_huge_pmd where
0063 missing after pmd_offset returns the pmd. Thanks to the graceful
0064 fallback design, with a one liner change, you can avoid to write
0065 hundreds if not thousands of lines of complex code to make your code
0066 hugepage aware.
0067 
0068 If you're not walking pagetables but you run into a physical hugepage
0069 that you can't handle natively in your code, you can split it by
0070 calling split_huge_page(page). This is what the Linux VM does before
0071 it tries to swapout the hugepage for example. split_huge_page() can fail
0072 if the page is pinned and you must handle this correctly.
0073 
0074 Example to make mremap.c transparent hugepage aware with a one liner
0075 change::
0076 
0077         diff --git a/mm/mremap.c b/mm/mremap.c
0078         --- a/mm/mremap.c
0079         +++ b/mm/mremap.c
0080         @@ -41,6 +41,7 @@ static pmd_t *get_old_pmd(struct mm_stru
0081                         return NULL;
0082 
0083                 pmd = pmd_offset(pud, addr);
0084         +       split_huge_pmd(vma, pmd, addr);
0085                 if (pmd_none_or_clear_bad(pmd))
0086                         return NULL;
0087 
0088 Locking in hugepage aware code
0089 ==============================
0090 
0091 We want as much code as possible hugepage aware, as calling
0092 split_huge_page() or split_huge_pmd() has a cost.
0093 
0094 To make pagetable walks huge pmd aware, all you need to do is to call
0095 pmd_trans_huge() on the pmd returned by pmd_offset. You must hold the
0096 mmap_lock in read (or write) mode to be sure a huge pmd cannot be
0097 created from under you by khugepaged (khugepaged collapse_huge_page
0098 takes the mmap_lock in write mode in addition to the anon_vma lock). If
0099 pmd_trans_huge returns false, you just fallback in the old code
0100 paths. If instead pmd_trans_huge returns true, you have to take the
0101 page table lock (pmd_lock()) and re-run pmd_trans_huge. Taking the
0102 page table lock will prevent the huge pmd being converted into a
0103 regular pmd from under you (split_huge_pmd can run in parallel to the
0104 pagetable walk). If the second pmd_trans_huge returns false, you
0105 should just drop the page table lock and fallback to the old code as
0106 before. Otherwise, you can proceed to process the huge pmd and the
0107 hugepage natively. Once finished, you can drop the page table lock.
0108 
0109 Refcounts and transparent huge pages
0110 ====================================
0111 
0112 Refcounting on THP is mostly consistent with refcounting on other compound
0113 pages:
0114 
0115   - get_page()/put_page() and GUP operate on head page's ->_refcount.
0116 
0117   - ->_refcount in tail pages is always zero: get_page_unless_zero() never
0118     succeeds on tail pages.
0119 
0120   - map/unmap of the pages with PTE entry increment/decrement ->_mapcount
0121     on relevant sub-page of the compound page.
0122 
0123   - map/unmap of the whole compound page is accounted for in compound_mapcount
0124     (stored in first tail page). For file huge pages, we also increment
0125     ->_mapcount of all sub-pages in order to have race-free detection of
0126     last unmap of subpages.
0127 
0128 PageDoubleMap() indicates that the page is *possibly* mapped with PTEs.
0129 
0130 For anonymous pages, PageDoubleMap() also indicates ->_mapcount in all
0131 subpages is offset up by one. This additional reference is required to
0132 get race-free detection of unmap of subpages when we have them mapped with
0133 both PMDs and PTEs.
0134 
0135 This optimization is required to lower the overhead of per-subpage mapcount
0136 tracking. The alternative is to alter ->_mapcount in all subpages on each
0137 map/unmap of the whole compound page.
0138 
0139 For anonymous pages, we set PG_double_map when a PMD of the page is split
0140 for the first time, but still have a PMD mapping. The additional references
0141 go away with the last compound_mapcount.
0142 
0143 File pages get PG_double_map set on the first map of the page with PTE and
0144 goes away when the page gets evicted from the page cache.
0145 
0146 split_huge_page internally has to distribute the refcounts in the head
0147 page to the tail pages before clearing all PG_head/tail bits from the page
0148 structures. It can be done easily for refcounts taken by page table
0149 entries, but we don't have enough information on how to distribute any
0150 additional pins (i.e. from get_user_pages). split_huge_page() fails any
0151 requests to split pinned huge pages: it expects page count to be equal to
0152 the sum of mapcount of all sub-pages plus one (split_huge_page caller must
0153 have a reference to the head page).
0154 
0155 split_huge_page uses migration entries to stabilize page->_refcount and
0156 page->_mapcount of anonymous pages. File pages just get unmapped.
0157 
0158 We are safe against physical memory scanners too: the only legitimate way
0159 a scanner can get a reference to a page is get_page_unless_zero().
0160 
0161 All tail pages have zero ->_refcount until atomic_add(). This prevents the
0162 scanner from getting a reference to the tail page up to that point. After the
0163 atomic_add() we don't care about the ->_refcount value. We already know how
0164 many references should be uncharged from the head page.
0165 
0166 For head page get_page_unless_zero() will succeed and we don't mind. It's
0167 clear where references should go after split: it will stay on the head page.
0168 
0169 Note that split_huge_pmd() doesn't have any limitations on refcounting:
0170 pmd can be split at any point and never fails.
0171 
0172 Partial unmap and deferred_split_huge_page()
0173 ============================================
0174 
0175 Unmapping part of THP (with munmap() or other way) is not going to free
0176 memory immediately. Instead, we detect that a subpage of THP is not in use
0177 in page_remove_rmap() and queue the THP for splitting if memory pressure
0178 comes. Splitting will free up unused subpages.
0179 
0180 Splitting the page right away is not an option due to locking context in
0181 the place where we can detect partial unmap. It also might be
0182 counterproductive since in many cases partial unmap happens during exit(2) if
0183 a THP crosses a VMA boundary.
0184 
0185 The function deferred_split_huge_page() is used to queue a page for splitting.
0186 The splitting itself will happen when we get memory pressure via shrinker
0187 interface.