提交 a46e6376 编写于 作者: K Kirill A. Shutemov 提交者: Linus Torvalds

thp: update documentation

The patch updates Documentation/vm/transhuge.txt to reflect changes in
THP design.
Signed-off-by: NKirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: NJerome Marchand <jmarchan@redhat.com>
Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Rik van Riel <riel@redhat.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Cc: Steve Capper <steve.capper@linaro.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
上级 61f5d698
...@@ -35,10 +35,10 @@ miss is going to run faster. ...@@ -35,10 +35,10 @@ miss is going to run faster.
== Design == == Design ==
- "graceful fallback": mm components which don't have transparent - "graceful fallback": mm components which don't have transparent hugepage
hugepage knowledge fall back to breaking a transparent hugepage and knowledge fall back to breaking huge pmd mapping into table of ptes and,
working on the regular pages and their respective regular pmd/pte if necessary, split a transparent hugepage. Therefore these components
mappings can continue working on the regular pages or regular pte mappings.
- if a hugepage allocation fails because of memory fragmentation, - if a hugepage allocation fails because of memory fragmentation,
regular pages should be gracefully allocated instead and mixed in regular pages should be gracefully allocated instead and mixed in
...@@ -221,9 +221,18 @@ thp_collapse_alloc_failed is incremented if khugepaged found a range ...@@ -221,9 +221,18 @@ thp_collapse_alloc_failed is incremented if khugepaged found a range
of pages that should be collapsed into one huge page but failed of pages that should be collapsed into one huge page but failed
the allocation. the allocation.
thp_split is incremented every time a huge page is split into base thp_split_page is incremented every time a huge page is split into base
pages. This can happen for a variety of reasons but a common pages. This can happen for a variety of reasons but a common
reason is that a huge page is old and is being reclaimed. reason is that a huge page is old and is being reclaimed.
This action implies splitting all PMD the page mapped with.
thp_split_page_failed is is incremented if kernel fails to split huge
page. This can happen if the page was pinned by somebody.
thp_split_pmd is incremented every time a PMD split into table of PTEs.
This can happen, for instance, when application calls mprotect() or
munmap() on part of huge page. It doesn't split huge page, only
page table entry.
thp_zero_page_alloc is incremented every time a huge zero page is thp_zero_page_alloc is incremented every time a huge zero page is
successfully allocated. It includes allocations which where successfully allocated. It includes allocations which where
...@@ -274,10 +283,8 @@ is complete, so they won't ever notice the fact the page is huge. But ...@@ -274,10 +283,8 @@ is complete, so they won't ever notice the fact the page is huge. But
if any driver is going to mangle over the page structure of the tail if any driver is going to mangle over the page structure of the tail
page (like for checking page->mapping or other bits that are relevant page (like for checking page->mapping or other bits that are relevant
for the head page and not the tail page), it should be updated to jump for the head page and not the tail page), it should be updated to jump
to check head page instead (while serializing properly against to check head page instead. Taking reference on any head/tail page would
split_huge_page() to avoid the head and tail pages to disappear from prevent page from being split by anyone.
under it, see the futex code to see an example of that, hugetlbfs also
needed special handling in futex code for similar reasons).
NOTE: these aren't new constraints to the GUP API, and they match the NOTE: these aren't new constraints to the GUP API, and they match the
same constrains that applies to hugetlbfs too, so any driver capable same constrains that applies to hugetlbfs too, so any driver capable
...@@ -312,9 +319,9 @@ unaffected. libhugetlbfs will also work fine as usual. ...@@ -312,9 +319,9 @@ unaffected. libhugetlbfs will also work fine as usual.
== Graceful fallback == == Graceful fallback ==
Code walking pagetables but unware about huge pmds can simply call Code walking pagetables but unware about huge pmds can simply call
split_huge_page_pmd(vma, addr, pmd) where the pmd is the one returned by split_huge_pmd(vma, pmd, addr) where the pmd is the one returned by
pmd_offset. It's trivial to make the code transparent hugepage aware pmd_offset. It's trivial to make the code transparent hugepage aware
by just grepping for "pmd_offset" and adding split_huge_page_pmd where by just grepping for "pmd_offset" and adding split_huge_pmd where
missing after pmd_offset returns the pmd. Thanks to the graceful missing after pmd_offset returns the pmd. Thanks to the graceful
fallback design, with a one liner change, you can avoid to write fallback design, with a one liner change, you can avoid to write
hundred if not thousand of lines of complex code to make your code hundred if not thousand of lines of complex code to make your code
...@@ -323,7 +330,8 @@ hugepage aware. ...@@ -323,7 +330,8 @@ hugepage aware.
If you're not walking pagetables but you run into a physical hugepage If you're not walking pagetables but you run into a physical hugepage
but you can't handle it natively in your code, you can split it by but you can't handle it natively in your code, you can split it by
calling split_huge_page(page). This is what the Linux VM does before calling split_huge_page(page). This is what the Linux VM does before
it tries to swapout the hugepage for example. it tries to swapout the hugepage for example. split_huge_page() can fail
if the page is pinned and you must handle this correctly.
Example to make mremap.c transparent hugepage aware with a one liner Example to make mremap.c transparent hugepage aware with a one liner
change: change:
...@@ -335,14 +343,14 @@ diff --git a/mm/mremap.c b/mm/mremap.c ...@@ -335,14 +343,14 @@ diff --git a/mm/mremap.c b/mm/mremap.c
return NULL; return NULL;
pmd = pmd_offset(pud, addr); pmd = pmd_offset(pud, addr);
+ split_huge_page_pmd(vma, addr, pmd); + split_huge_pmd(vma, pmd, addr);
if (pmd_none_or_clear_bad(pmd)) if (pmd_none_or_clear_bad(pmd))
return NULL; return NULL;
== Locking in hugepage aware code == == Locking in hugepage aware code ==
We want as much code as possible hugepage aware, as calling We want as much code as possible hugepage aware, as calling
split_huge_page() or split_huge_page_pmd() has a cost. split_huge_page() or split_huge_pmd() has a cost.
To make pagetable walks huge pmd aware, all you need to do is to call To make pagetable walks huge pmd aware, all you need to do is to call
pmd_trans_huge() on the pmd returned by pmd_offset. You must hold the pmd_trans_huge() on the pmd returned by pmd_offset. You must hold the
...@@ -351,47 +359,80 @@ created from under you by khugepaged (khugepaged collapse_huge_page ...@@ -351,47 +359,80 @@ created from under you by khugepaged (khugepaged collapse_huge_page
takes the mmap_sem in write mode in addition to the anon_vma lock). If takes the mmap_sem in write mode in addition to the anon_vma lock). If
pmd_trans_huge returns false, you just fallback in the old code pmd_trans_huge returns false, you just fallback in the old code
paths. If instead pmd_trans_huge returns true, you have to take the paths. If instead pmd_trans_huge returns true, you have to take the
mm->page_table_lock and re-run pmd_trans_huge. Taking the page table lock (pmd_lock()) and re-run pmd_trans_huge. Taking the
page_table_lock will prevent the huge pmd to be converted into a page table lock will prevent the huge pmd to be converted into a
regular pmd from under you (split_huge_page can run in parallel to the regular pmd from under you (split_huge_pmd can run in parallel to the
pagetable walk). If the second pmd_trans_huge returns false, you pagetable walk). If the second pmd_trans_huge returns false, you
should just drop the page_table_lock and fallback to the old code as should just drop the page table lock and fallback to the old code as
before. Otherwise you should run pmd_trans_splitting on the pmd. In before. Otherwise you can proceed to process the huge pmd and the
case pmd_trans_splitting returns true, it means split_huge_page is hugepage natively. Once finished you can drop the page table lock.
already in the middle of splitting the page. So if pmd_trans_splitting
returns true it's enough to drop the page_table_lock and call == Refcounts and transparent huge pages ==
wait_split_huge_page and then fallback the old code paths. You are
guaranteed by the time wait_split_huge_page returns, the pmd isn't Refcounting on THP is mostly consistent with refcounting on other compound
huge anymore. If pmd_trans_splitting returns false, you can proceed to pages:
process the huge pmd and the hugepage natively. Once finished you can
drop the page_table_lock. - get_page()/put_page() and GUP operate in head page's ->_count.
== compound_lock, get_user_pages and put_page == - ->_count in tail pages is always zero: get_page_unless_zero() never
succeed on tail pages.
- map/unmap of the pages with PTE entry increment/decrement ->_mapcount
on relevant sub-page of the compound page.
- map/unmap of the whole compound page accounted in compound_mapcount
(stored in first tail page).
PageDoubleMap() indicates that ->_mapcount in all subpages is offset up by one.
This additional reference is required to get race-free detection of unmap of
subpages when we have them mapped with both PMDs and PTEs.
This is optimization required to lower overhead of per-subpage mapcount
tracking. The alternative is alter ->_mapcount in all subpages on each
map/unmap of the whole compound page.
We set PG_double_map when a PMD of the page got split for the first time,
but still have PMD mapping. The addtional references go away with last
compound_mapcount.
split_huge_page internally has to distribute the refcounts in the head split_huge_page internally has to distribute the refcounts in the head
page to the tail pages before clearing all PG_head/tail bits from the page to the tail pages before clearing all PG_head/tail bits from the page
page structures. It can do that easily for refcounts taken by huge pmd structures. It can be done easily for refcounts taken by page table
mappings. But the GUI API as created by hugetlbfs (that returns head entries. But we don't have enough information on how to distribute any
and tail pages if running get_user_pages on an address backed by any additional pins (i.e. from get_user_pages). split_huge_page() fails any
hugepage), requires the refcount to be accounted on the tail pages and requests to split pinned huge page: it expects page count to be equal to
not only in the head pages, if we want to be able to run sum of mapcount of all sub-pages plus one (split_huge_page caller must
split_huge_page while there are gup pins established on any tail have reference for head page).
page. Failure to be able to run split_huge_page if there's any gup pin
on any tail page, would mean having to split all hugepages upfront in split_huge_page uses migration entries to stabilize page->_count and
get_user_pages which is unacceptable as too many gup users are page->_mapcount.
performance critical and they must work natively on hugepages like
they work natively on hugetlbfs already (hugetlbfs is simpler because We safe against physical memory scanners too: the only legitimate way
hugetlbfs pages cannot be split so there wouldn't be requirement of scanner can get reference to a page is get_page_unless_zero().
accounting the pins on the tail pages for hugetlbfs). If we wouldn't
account the gup refcounts on the tail pages during gup, we won't know All tail pages has zero ->_count until atomic_add(). It prevent scanner
anymore which tail page is pinned by gup and which is not while we run from geting reference to tail page up to the point. After the atomic_add()
split_huge_page. But we still have to add the gup pin to the head page we don't care about ->_count value. We already known how many references
too, to know when we can free the compound page in case it's never with should uncharge from head page.
split during its lifetime. That requires changing not just
get_page, but put_page as well so that when put_page runs on a tail For head page get_page_unless_zero() will succeed and we don't mind. It's
page (and only on a tail page) it will find its respective head page, clear where reference should go after split: it will stay on head page.
and then it will decrease the head page refcount in addition to the
tail page refcount. To obtain a head page reliably and to decrease its Note that split_huge_pmd() doesn't have any limitation on refcounting:
refcount without race conditions, put_page has to serialize against pmd can be split at any point and never fails.
__split_huge_page_refcount using a special per-page lock called
compound_lock. == Partial unmap and deferred_split_huge_page() ==
Unmapping part of THP (with munmap() or other way) is not going to free
memory immediately. Instead, we detect that a subpage of THP is not in use
in page_remove_rmap() and queue the THP for splitting if memory pressure
comes. Splitting will free up unused subpages.
Splitting the page right away is not an option due to locking context in
the place where we can detect partial unmap. It's also might be
counterproductive since in many cases partial unmap unmap happens during
exit(2) if an THP crosses VMA boundary.
Function deferred_split_huge_page() is used to queue page for splitting.
The splitting itself will happen when we get memory pressure via shrinker
interface.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册