diff options
| author | Kirill A. Shutemov <kirill.shutemov@linux.intel.com> | 2016-01-15 19:54:30 -0500 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2016-01-15 20:56:32 -0500 |
| commit | a46e63764eb6d0252ab4e96f96ad447594673274 (patch) | |
| tree | 239c42f3f5470fcc147b68d2a6f1b53e4a08c883 /Documentation/vm | |
| parent | 61f5d698cc97600e813ca5cf8e449b1ea1c11492 (diff) | |
thp: update documentation
The patch updates Documentation/vm/transhuge.txt to reflect changes in
THP design.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Jerome 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: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'Documentation/vm')
| -rw-r--r-- | Documentation/vm/transhuge.txt | 151 |
1 files changed, 96 insertions, 55 deletions
diff --git a/Documentation/vm/transhuge.txt b/Documentation/vm/transhuge.txt index 8a282687ee06..21cf34f3ddb2 100644 --- a/Documentation/vm/transhuge.txt +++ b/Documentation/vm/transhuge.txt | |||
| @@ -35,10 +35,10 @@ miss is going to run faster. | |||
| 35 | 35 | ||
| 36 | == Design == | 36 | == Design == |
| 37 | 37 | ||
| 38 | - "graceful fallback": mm components which don't have transparent | 38 | - "graceful fallback": mm components which don't have transparent hugepage |
| 39 | hugepage knowledge fall back to breaking a transparent hugepage and | 39 | knowledge fall back to breaking huge pmd mapping into table of ptes and, |
| 40 | working on the regular pages and their respective regular pmd/pte | 40 | if necessary, split a transparent hugepage. Therefore these components |
| 41 | mappings | 41 | can continue working on the regular pages or regular pte mappings. |
| 42 | 42 | ||
| 43 | - if a hugepage allocation fails because of memory fragmentation, | 43 | - if a hugepage allocation fails because of memory fragmentation, |
| 44 | regular pages should be gracefully allocated instead and mixed in | 44 | 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 | of pages that should be collapsed into one huge page but failed | 221 | of pages that should be collapsed into one huge page but failed |
| 222 | the allocation. | 222 | the allocation. |
| 223 | 223 | ||
| 224 | thp_split is incremented every time a huge page is split into base | 224 | thp_split_page is incremented every time a huge page is split into base |
| 225 | pages. This can happen for a variety of reasons but a common | 225 | pages. This can happen for a variety of reasons but a common |
| 226 | reason is that a huge page is old and is being reclaimed. | 226 | reason is that a huge page is old and is being reclaimed. |
| 227 | This action implies splitting all PMD the page mapped with. | ||
| 228 | |||
| 229 | thp_split_page_failed is is incremented if kernel fails to split huge | ||
| 230 | page. This can happen if the page was pinned by somebody. | ||
| 231 | |||
| 232 | thp_split_pmd is incremented every time a PMD split into table of PTEs. | ||
| 233 | This can happen, for instance, when application calls mprotect() or | ||
| 234 | munmap() on part of huge page. It doesn't split huge page, only | ||
| 235 | page table entry. | ||
| 227 | 236 | ||
| 228 | thp_zero_page_alloc is incremented every time a huge zero page is | 237 | thp_zero_page_alloc is incremented every time a huge zero page is |
| 229 | successfully allocated. It includes allocations which where | 238 | 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 | if any driver is going to mangle over the page structure of the tail | 283 | if any driver is going to mangle over the page structure of the tail |
| 275 | page (like for checking page->mapping or other bits that are relevant | 284 | page (like for checking page->mapping or other bits that are relevant |
| 276 | for the head page and not the tail page), it should be updated to jump | 285 | for the head page and not the tail page), it should be updated to jump |
| 277 | to check head page instead (while serializing properly against | 286 | to check head page instead. Taking reference on any head/tail page would |
| 278 | split_huge_page() to avoid the head and tail pages to disappear from | 287 | prevent page from being split by anyone. |
| 279 | under it, see the futex code to see an example of that, hugetlbfs also | ||
| 280 | needed special handling in futex code for similar reasons). | ||
| 281 | 288 | ||
| 282 | NOTE: these aren't new constraints to the GUP API, and they match the | 289 | NOTE: these aren't new constraints to the GUP API, and they match the |
| 283 | same constrains that applies to hugetlbfs too, so any driver capable | 290 | same constrains that applies to hugetlbfs too, so any driver capable |
| @@ -312,9 +319,9 @@ unaffected. libhugetlbfs will also work fine as usual. | |||
| 312 | == Graceful fallback == | 319 | == Graceful fallback == |
| 313 | 320 | ||
| 314 | Code walking pagetables but unware about huge pmds can simply call | 321 | Code walking pagetables but unware about huge pmds can simply call |
| 315 | split_huge_page_pmd(vma, addr, pmd) where the pmd is the one returned by | 322 | split_huge_pmd(vma, pmd, addr) where the pmd is the one returned by |
| 316 | pmd_offset. It's trivial to make the code transparent hugepage aware | 323 | pmd_offset. It's trivial to make the code transparent hugepage aware |
| 317 | by just grepping for "pmd_offset" and adding split_huge_page_pmd where | 324 | by just grepping for "pmd_offset" and adding split_huge_pmd where |
| 318 | missing after pmd_offset returns the pmd. Thanks to the graceful | 325 | missing after pmd_offset returns the pmd. Thanks to the graceful |
| 319 | fallback design, with a one liner change, you can avoid to write | 326 | fallback design, with a one liner change, you can avoid to write |
| 320 | hundred if not thousand of lines of complex code to make your code | 327 | hundred if not thousand of lines of complex code to make your code |
| @@ -323,7 +330,8 @@ hugepage aware. | |||
| 323 | If you're not walking pagetables but you run into a physical hugepage | 330 | If you're not walking pagetables but you run into a physical hugepage |
| 324 | but you can't handle it natively in your code, you can split it by | 331 | but you can't handle it natively in your code, you can split it by |
| 325 | calling split_huge_page(page). This is what the Linux VM does before | 332 | calling split_huge_page(page). This is what the Linux VM does before |
| 326 | it tries to swapout the hugepage for example. | 333 | it tries to swapout the hugepage for example. split_huge_page() can fail |
| 334 | if the page is pinned and you must handle this correctly. | ||
| 327 | 335 | ||
| 328 | Example to make mremap.c transparent hugepage aware with a one liner | 336 | Example to make mremap.c transparent hugepage aware with a one liner |
| 329 | change: | 337 | change: |
| @@ -335,14 +343,14 @@ diff --git a/mm/mremap.c b/mm/mremap.c | |||
| 335 | return NULL; | 343 | return NULL; |
| 336 | 344 | ||
| 337 | pmd = pmd_offset(pud, addr); | 345 | pmd = pmd_offset(pud, addr); |
| 338 | + split_huge_page_pmd(vma, addr, pmd); | 346 | + split_huge_pmd(vma, pmd, addr); |
| 339 | if (pmd_none_or_clear_bad(pmd)) | 347 | if (pmd_none_or_clear_bad(pmd)) |
| 340 | return NULL; | 348 | return NULL; |
| 341 | 349 | ||
| 342 | == Locking in hugepage aware code == | 350 | == Locking in hugepage aware code == |
| 343 | 351 | ||
| 344 | We want as much code as possible hugepage aware, as calling | 352 | We want as much code as possible hugepage aware, as calling |
| 345 | split_huge_page() or split_huge_page_pmd() has a cost. | 353 | split_huge_page() or split_huge_pmd() has a cost. |
| 346 | 354 | ||
| 347 | To make pagetable walks huge pmd aware, all you need to do is to call | 355 | To make pagetable walks huge pmd aware, all you need to do is to call |
| 348 | pmd_trans_huge() on the pmd returned by pmd_offset. You must hold the | 356 | 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 | takes the mmap_sem in write mode in addition to the anon_vma lock). If | 359 | takes the mmap_sem in write mode in addition to the anon_vma lock). If |
| 352 | pmd_trans_huge returns false, you just fallback in the old code | 360 | pmd_trans_huge returns false, you just fallback in the old code |
| 353 | paths. If instead pmd_trans_huge returns true, you have to take the | 361 | paths. If instead pmd_trans_huge returns true, you have to take the |
| 354 | mm->page_table_lock and re-run pmd_trans_huge. Taking the | 362 | page table lock (pmd_lock()) and re-run pmd_trans_huge. Taking the |
| 355 | page_table_lock will prevent the huge pmd to be converted into a | 363 | page table lock will prevent the huge pmd to be converted into a |
| 356 | regular pmd from under you (split_huge_page can run in parallel to the | 364 | regular pmd from under you (split_huge_pmd can run in parallel to the |
| 357 | pagetable walk). If the second pmd_trans_huge returns false, you | 365 | pagetable walk). If the second pmd_trans_huge returns false, you |
| 358 | should just drop the page_table_lock and fallback to the old code as | 366 | should just drop the page table lock and fallback to the old code as |
| 359 | before. Otherwise you should run pmd_trans_splitting on the pmd. In | 367 | before. Otherwise you can proceed to process the huge pmd and the |
| 360 | case pmd_trans_splitting returns true, it means split_huge_page is | 368 | hugepage natively. Once finished you can drop the page table lock. |
| 361 | already in the middle of splitting the page. So if pmd_trans_splitting | 369 | |
| 362 | returns true it's enough to drop the page_table_lock and call | 370 | == Refcounts and transparent huge pages == |
| 363 | wait_split_huge_page and then fallback the old code paths. You are | 371 | |
| 364 | guaranteed by the time wait_split_huge_page returns, the pmd isn't | 372 | Refcounting on THP is mostly consistent with refcounting on other compound |
| 365 | huge anymore. If pmd_trans_splitting returns false, you can proceed to | 373 | pages: |
| 366 | process the huge pmd and the hugepage natively. Once finished you can | 374 | |
| 367 | drop the page_table_lock. | 375 | - get_page()/put_page() and GUP operate in head page's ->_count. |
| 368 | 376 | ||
| 369 | == compound_lock, get_user_pages and put_page == | 377 | - ->_count in tail pages is always zero: get_page_unless_zero() never |
| 378 | succeed on tail pages. | ||
| 379 | |||
| 380 | - map/unmap of the pages with PTE entry increment/decrement ->_mapcount | ||
| 381 | on relevant sub-page of the compound page. | ||
| 382 | |||
| 383 | - map/unmap of the whole compound page accounted in compound_mapcount | ||
| 384 | (stored in first tail page). | ||
| 385 | |||
| 386 | PageDoubleMap() indicates that ->_mapcount in all subpages is offset up by one. | ||
| 387 | This additional reference is required to get race-free detection of unmap of | ||
| 388 | subpages when we have them mapped with both PMDs and PTEs. | ||
| 389 | |||
| 390 | This is optimization required to lower overhead of per-subpage mapcount | ||
| 391 | tracking. The alternative is alter ->_mapcount in all subpages on each | ||
| 392 | map/unmap of the whole compound page. | ||
| 393 | |||
| 394 | We set PG_double_map when a PMD of the page got split for the first time, | ||
| 395 | but still have PMD mapping. The addtional references go away with last | ||
| 396 | compound_mapcount. | ||
| 370 | 397 | ||
| 371 | split_huge_page internally has to distribute the refcounts in the head | 398 | split_huge_page internally has to distribute the refcounts in the head |
| 372 | page to the tail pages before clearing all PG_head/tail bits from the | 399 | page to the tail pages before clearing all PG_head/tail bits from the page |
| 373 | page structures. It can do that easily for refcounts taken by huge pmd | 400 | structures. It can be done easily for refcounts taken by page table |
| 374 | mappings. But the GUI API as created by hugetlbfs (that returns head | 401 | entries. But we don't have enough information on how to distribute any |
| 375 | and tail pages if running get_user_pages on an address backed by any | 402 | additional pins (i.e. from get_user_pages). split_huge_page() fails any |
| 376 | hugepage), requires the refcount to be accounted on the tail pages and | 403 | requests to split pinned huge page: it expects page count to be equal to |
| 377 | not only in the head pages, if we want to be able to run | 404 | sum of mapcount of all sub-pages plus one (split_huge_page caller must |
| 378 | split_huge_page while there are gup pins established on any tail | 405 | have reference for head page). |
| 379 | page. Failure to be able to run split_huge_page if there's any gup pin | 406 | |
| 380 | on any tail page, would mean having to split all hugepages upfront in | 407 | split_huge_page uses migration entries to stabilize page->_count and |
| 381 | get_user_pages which is unacceptable as too many gup users are | 408 | page->_mapcount. |
| 382 | performance critical and they must work natively on hugepages like | 409 | |
| 383 | they work natively on hugetlbfs already (hugetlbfs is simpler because | 410 | We safe against physical memory scanners too: the only legitimate way |
| 384 | hugetlbfs pages cannot be split so there wouldn't be requirement of | 411 | scanner can get reference to a page is get_page_unless_zero(). |
| 385 | accounting the pins on the tail pages for hugetlbfs). If we wouldn't | 412 | |
| 386 | account the gup refcounts on the tail pages during gup, we won't know | 413 | All tail pages has zero ->_count until atomic_add(). It prevent scanner |
| 387 | anymore which tail page is pinned by gup and which is not while we run | 414 | from geting reference to tail page up to the point. After the atomic_add() |
| 388 | split_huge_page. But we still have to add the gup pin to the head page | 415 | we don't care about ->_count value. We already known how many references |
| 389 | too, to know when we can free the compound page in case it's never | 416 | with should uncharge from head page. |
| 390 | split during its lifetime. That requires changing not just | 417 | |
| 391 | get_page, but put_page as well so that when put_page runs on a tail | 418 | For head page get_page_unless_zero() will succeed and we don't mind. It's |
| 392 | page (and only on a tail page) it will find its respective head page, | 419 | clear where reference should go after split: it will stay on head page. |
| 393 | and then it will decrease the head page refcount in addition to the | 420 | |
| 394 | tail page refcount. To obtain a head page reliably and to decrease its | 421 | Note that split_huge_pmd() doesn't have any limitation on refcounting: |
| 395 | refcount without race conditions, put_page has to serialize against | 422 | pmd can be split at any point and never fails. |
| 396 | __split_huge_page_refcount using a special per-page lock called | 423 | |
| 397 | compound_lock. | 424 | == Partial unmap and deferred_split_huge_page() == |
| 425 | |||
| 426 | Unmapping part of THP (with munmap() or other way) is not going to free | ||
| 427 | memory immediately. Instead, we detect that a subpage of THP is not in use | ||
| 428 | in page_remove_rmap() and queue the THP for splitting if memory pressure | ||
| 429 | comes. Splitting will free up unused subpages. | ||
| 430 | |||
| 431 | Splitting the page right away is not an option due to locking context in | ||
| 432 | the place where we can detect partial unmap. It's also might be | ||
| 433 | counterproductive since in many cases partial unmap unmap happens during | ||
| 434 | exit(2) if an THP crosses VMA boundary. | ||
| 435 | |||
| 436 | Function deferred_split_huge_page() is used to queue page for splitting. | ||
| 437 | The splitting itself will happen when we get memory pressure via shrinker | ||
| 438 | interface. | ||
