transhuge.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. .. _transhuge:
  2. ============================
  3. Transparent Hugepage Support
  4. ============================
  5. This document describes design principles for Transparent Hugepage (THP)
  6. support and its interaction with other parts of the memory management
  7. system.
  8. Design principles
  9. =================
  10. - "graceful fallback": mm components which don't have transparent hugepage
  11. knowledge fall back to breaking huge pmd mapping into table of ptes and,
  12. if necessary, split a transparent hugepage. Therefore these components
  13. can continue working on the regular pages or regular pte mappings.
  14. - if a hugepage allocation fails because of memory fragmentation,
  15. regular pages should be gracefully allocated instead and mixed in
  16. the same vma without any failure or significant delay and without
  17. userland noticing
  18. - if some task quits and more hugepages become available (either
  19. immediately in the buddy or through the VM), guest physical memory
  20. backed by regular pages should be relocated on hugepages
  21. automatically (with khugepaged)
  22. - it doesn't require memory reservation and in turn it uses hugepages
  23. whenever possible (the only possible reservation here is kernelcore=
  24. to avoid unmovable pages to fragment all the memory but such a tweak
  25. is not specific to transparent hugepage support and it's a generic
  26. feature that applies to all dynamic high order allocations in the
  27. kernel)
  28. get_user_pages and follow_page
  29. ==============================
  30. get_user_pages and follow_page if run on a hugepage, will return the
  31. head or tail pages as usual (exactly as they would do on
  32. hugetlbfs). Most GUP users will only care about the actual physical
  33. address of the page and its temporary pinning to release after the I/O
  34. is complete, so they won't ever notice the fact the page is huge. But
  35. if any driver is going to mangle over the page structure of the tail
  36. page (like for checking page->mapping or other bits that are relevant
  37. for the head page and not the tail page), it should be updated to jump
  38. to check head page instead. Taking a reference on any head/tail page would
  39. prevent the page from being split by anyone.
  40. .. note::
  41. these aren't new constraints to the GUP API, and they match the
  42. same constraints that apply to hugetlbfs too, so any driver capable
  43. of handling GUP on hugetlbfs will also work fine on transparent
  44. hugepage backed mappings.
  45. Graceful fallback
  46. =================
  47. Code walking pagetables but unaware about huge pmds can simply call
  48. split_huge_pmd(vma, pmd, addr) where the pmd is the one returned by
  49. pmd_offset. It's trivial to make the code transparent hugepage aware
  50. by just grepping for "pmd_offset" and adding split_huge_pmd where
  51. missing after pmd_offset returns the pmd. Thanks to the graceful
  52. fallback design, with a one liner change, you can avoid to write
  53. hundreds if not thousands of lines of complex code to make your code
  54. hugepage aware.
  55. If you're not walking pagetables but you run into a physical hugepage
  56. that you can't handle natively in your code, you can split it by
  57. calling split_huge_page(page). This is what the Linux VM does before
  58. it tries to swapout the hugepage for example. split_huge_page() can fail
  59. if the page is pinned and you must handle this correctly.
  60. Example to make mremap.c transparent hugepage aware with a one liner
  61. change::
  62. diff --git a/mm/mremap.c b/mm/mremap.c
  63. --- a/mm/mremap.c
  64. +++ b/mm/mremap.c
  65. @@ -41,6 +41,7 @@ static pmd_t *get_old_pmd(struct mm_stru
  66. return NULL;
  67. pmd = pmd_offset(pud, addr);
  68. + split_huge_pmd(vma, pmd, addr);
  69. if (pmd_none_or_clear_bad(pmd))
  70. return NULL;
  71. Locking in hugepage aware code
  72. ==============================
  73. We want as much code as possible hugepage aware, as calling
  74. split_huge_page() or split_huge_pmd() has a cost.
  75. To make pagetable walks huge pmd aware, all you need to do is to call
  76. pmd_trans_huge() on the pmd returned by pmd_offset. You must hold the
  77. mmap_lock in read (or write) mode to be sure a huge pmd cannot be
  78. created from under you by khugepaged (khugepaged collapse_huge_page
  79. takes the mmap_lock in write mode in addition to the anon_vma lock). If
  80. pmd_trans_huge returns false, you just fallback in the old code
  81. paths. If instead pmd_trans_huge returns true, you have to take the
  82. page table lock (pmd_lock()) and re-run pmd_trans_huge. Taking the
  83. page table lock will prevent the huge pmd being converted into a
  84. regular pmd from under you (split_huge_pmd can run in parallel to the
  85. pagetable walk). If the second pmd_trans_huge returns false, you
  86. should just drop the page table lock and fallback to the old code as
  87. before. Otherwise, you can proceed to process the huge pmd and the
  88. hugepage natively. Once finished, you can drop the page table lock.
  89. Refcounts and transparent huge pages
  90. ====================================
  91. Refcounting on THP is mostly consistent with refcounting on other compound
  92. pages:
  93. - get_page()/put_page() and GUP operate on head page's ->_refcount.
  94. - ->_refcount in tail pages is always zero: get_page_unless_zero() never
  95. succeeds on tail pages.
  96. - map/unmap of the pages with PTE entry increment/decrement ->_mapcount
  97. on relevant sub-page of the compound page.
  98. - map/unmap of the whole compound page is accounted for in compound_mapcount
  99. (stored in first tail page). For file huge pages, we also increment
  100. ->_mapcount of all sub-pages in order to have race-free detection of
  101. last unmap of subpages.
  102. PageDoubleMap() indicates that the page is *possibly* mapped with PTEs.
  103. For anonymous pages, PageDoubleMap() also indicates ->_mapcount in all
  104. subpages is offset up by one. This additional reference is required to
  105. get race-free detection of unmap of subpages when we have them mapped with
  106. both PMDs and PTEs.
  107. This optimization is required to lower the overhead of per-subpage mapcount
  108. tracking. The alternative is to alter ->_mapcount in all subpages on each
  109. map/unmap of the whole compound page.
  110. For anonymous pages, we set PG_double_map when a PMD of the page is split
  111. for the first time, but still have a PMD mapping. The additional references
  112. go away with the last compound_mapcount.
  113. File pages get PG_double_map set on the first map of the page with PTE and
  114. goes away when the page gets evicted from the page cache.
  115. split_huge_page internally has to distribute the refcounts in the head
  116. page to the tail pages before clearing all PG_head/tail bits from the page
  117. structures. It can be done easily for refcounts taken by page table
  118. entries, but we don't have enough information on how to distribute any
  119. additional pins (i.e. from get_user_pages). split_huge_page() fails any
  120. requests to split pinned huge pages: it expects page count to be equal to
  121. the sum of mapcount of all sub-pages plus one (split_huge_page caller must
  122. have a reference to the head page).
  123. split_huge_page uses migration entries to stabilize page->_refcount and
  124. page->_mapcount of anonymous pages. File pages just get unmapped.
  125. We are safe against physical memory scanners too: the only legitimate way
  126. a scanner can get a reference to a page is get_page_unless_zero().
  127. All tail pages have zero ->_refcount until atomic_add(). This prevents the
  128. scanner from getting a reference to the tail page up to that point. After the
  129. atomic_add() we don't care about the ->_refcount value. We already know how
  130. many references should be uncharged from the head page.
  131. For head page get_page_unless_zero() will succeed and we don't mind. It's
  132. clear where references should go after split: it will stay on the head page.
  133. Note that split_huge_pmd() doesn't have any limitations on refcounting:
  134. pmd can be split at any point and never fails.
  135. Partial unmap and deferred_split_huge_page()
  136. ============================================
  137. Unmapping part of THP (with munmap() or other way) is not going to free
  138. memory immediately. Instead, we detect that a subpage of THP is not in use
  139. in page_remove_rmap() and queue the THP for splitting if memory pressure
  140. comes. Splitting will free up unused subpages.
  141. Splitting the page right away is not an option due to locking context in
  142. the place where we can detect partial unmap. It also might be
  143. counterproductive since in many cases partial unmap happens during exit(2) if
  144. a THP crosses a VMA boundary.
  145. The function deferred_split_huge_page() is used to queue a page for splitting.
  146. The splitting itself will happen when we get memory pressure via shrinker
  147. interface.