mirror of
https://github.com/torvalds/linux.git
synced 2024-12-02 17:11:33 +00:00
3822a7c409
F_SEAL_EXEC") which permits the setting of the memfd execute bit at memfd creation time, with the option of sealing the state of the X bit. - Peter Xu adds a patch series ("mm/hugetlb: Make huge_pte_offset() thread-safe for pmd unshare") which addresses a rare race condition related to PMD unsharing. - Several folioification patch serieses from Matthew Wilcox, Vishal Moola, Sidhartha Kumar and Lorenzo Stoakes - Johannes Weiner has a series ("mm: push down lock_page_memcg()") which does perform some memcg maintenance and cleanup work. - SeongJae Park has added DAMOS filtering to DAMON, with the series "mm/damon/core: implement damos filter". These filters provide users with finer-grained control over DAMOS's actions. SeongJae has also done some DAMON cleanup work. - Kairui Song adds a series ("Clean up and fixes for swap"). - Vernon Yang contributed the series "Clean up and refinement for maple tree". - Yu Zhao has contributed the "mm: multi-gen LRU: memcg LRU" series. It adds to MGLRU an LRU of memcgs, to improve the scalability of global reclaim. - David Hildenbrand has added some userfaultfd cleanup work in the series "mm: uffd-wp + change_protection() cleanups". - Christoph Hellwig has removed the generic_writepages() library function in the series "remove generic_writepages". - Baolin Wang has performed some maintenance on the compaction code in his series "Some small improvements for compaction". - Sidhartha Kumar is doing some maintenance work on struct page in his series "Get rid of tail page fields". - David Hildenbrand contributed some cleanup, bugfixing and generalization of pte management and of pte debugging in his series "mm: support __HAVE_ARCH_PTE_SWP_EXCLUSIVE on all architectures with swap PTEs". - Mel Gorman and Neil Brown have removed the __GFP_ATOMIC allocation flag in the series "Discard __GFP_ATOMIC". - Sergey Senozhatsky has improved zsmalloc's memory utilization with his series "zsmalloc: make zspage chain size configurable". - Joey Gouly has added prctl() support for prohibiting the creation of writeable+executable mappings. The previous BPF-based approach had shortcomings. See "mm: In-kernel support for memory-deny-write-execute (MDWE)". - Waiman Long did some kmemleak cleanup and bugfixing in the series "mm/kmemleak: Simplify kmemleak_cond_resched() & fix UAF". - T.J. Alumbaugh has contributed some MGLRU cleanup work in his series "mm: multi-gen LRU: improve". - Jiaqi Yan has provided some enhancements to our memory error statistics reporting, mainly by presenting the statistics on a per-node basis. See the series "Introduce per NUMA node memory error statistics". - Mel Gorman has a second and hopefully final shot at fixing a CPU-hog regression in compaction via his series "Fix excessive CPU usage during compaction". - Christoph Hellwig does some vmalloc maintenance work in the series "cleanup vfree and vunmap". - Christoph Hellwig has removed block_device_operations.rw_page() in ths series "remove ->rw_page". - We get some maple_tree improvements and cleanups in Liam Howlett's series "VMA tree type safety and remove __vma_adjust()". - Suren Baghdasaryan has done some work on the maintainability of our vm_flags handling in the series "introduce vm_flags modifier functions". - Some pagemap cleanup and generalization work in Mike Rapoport's series "mm, arch: add generic implementation of pfn_valid() for FLATMEM" and "fixups for generic implementation of pfn_valid()" - Baoquan He has done some work to make /proc/vmallocinfo and /proc/kcore better represent the real state of things in his series "mm/vmalloc.c: allow vread() to read out vm_map_ram areas". - Jason Gunthorpe rationalized the GUP system's interface to the rest of the kernel in the series "Simplify the external interface for GUP". - SeongJae Park wishes to migrate people from DAMON's debugfs interface over to its sysfs interface. To support this, we'll temporarily be printing warnings when people use the debugfs interface. See the series "mm/damon: deprecate DAMON debugfs interface". - Andrey Konovalov provided the accurately named "lib/stackdepot: fixes and clean-ups" series. - Huang Ying has provided a dramatic reduction in migration's TLB flush IPI rates with the series "migrate_pages(): batch TLB flushing". - Arnd Bergmann has some objtool fixups in "objtool warning fixes". -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCY/PoPQAKCRDdBJ7gKXxA jlvpAPsFECUBBl20qSue2zCYWnHC7Yk4q9ytTkPB/MMDrFEN9wD/SNKEm2UoK6/K DmxHkn0LAitGgJRS/W9w81yrgig9tAQ= =MlGs -----END PGP SIGNATURE----- Merge tag 'mm-stable-2023-02-20-13-37' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Pull MM updates from Andrew Morton: - Daniel Verkamp has contributed a memfd series ("mm/memfd: add F_SEAL_EXEC") which permits the setting of the memfd execute bit at memfd creation time, with the option of sealing the state of the X bit. - Peter Xu adds a patch series ("mm/hugetlb: Make huge_pte_offset() thread-safe for pmd unshare") which addresses a rare race condition related to PMD unsharing. - Several folioification patch serieses from Matthew Wilcox, Vishal Moola, Sidhartha Kumar and Lorenzo Stoakes - Johannes Weiner has a series ("mm: push down lock_page_memcg()") which does perform some memcg maintenance and cleanup work. - SeongJae Park has added DAMOS filtering to DAMON, with the series "mm/damon/core: implement damos filter". These filters provide users with finer-grained control over DAMOS's actions. SeongJae has also done some DAMON cleanup work. - Kairui Song adds a series ("Clean up and fixes for swap"). - Vernon Yang contributed the series "Clean up and refinement for maple tree". - Yu Zhao has contributed the "mm: multi-gen LRU: memcg LRU" series. It adds to MGLRU an LRU of memcgs, to improve the scalability of global reclaim. - David Hildenbrand has added some userfaultfd cleanup work in the series "mm: uffd-wp + change_protection() cleanups". - Christoph Hellwig has removed the generic_writepages() library function in the series "remove generic_writepages". - Baolin Wang has performed some maintenance on the compaction code in his series "Some small improvements for compaction". - Sidhartha Kumar is doing some maintenance work on struct page in his series "Get rid of tail page fields". - David Hildenbrand contributed some cleanup, bugfixing and generalization of pte management and of pte debugging in his series "mm: support __HAVE_ARCH_PTE_SWP_EXCLUSIVE on all architectures with swap PTEs". - Mel Gorman and Neil Brown have removed the __GFP_ATOMIC allocation flag in the series "Discard __GFP_ATOMIC". - Sergey Senozhatsky has improved zsmalloc's memory utilization with his series "zsmalloc: make zspage chain size configurable". - Joey Gouly has added prctl() support for prohibiting the creation of writeable+executable mappings. The previous BPF-based approach had shortcomings. See "mm: In-kernel support for memory-deny-write-execute (MDWE)". - Waiman Long did some kmemleak cleanup and bugfixing in the series "mm/kmemleak: Simplify kmemleak_cond_resched() & fix UAF". - T.J. Alumbaugh has contributed some MGLRU cleanup work in his series "mm: multi-gen LRU: improve". - Jiaqi Yan has provided some enhancements to our memory error statistics reporting, mainly by presenting the statistics on a per-node basis. See the series "Introduce per NUMA node memory error statistics". - Mel Gorman has a second and hopefully final shot at fixing a CPU-hog regression in compaction via his series "Fix excessive CPU usage during compaction". - Christoph Hellwig does some vmalloc maintenance work in the series "cleanup vfree and vunmap". - Christoph Hellwig has removed block_device_operations.rw_page() in ths series "remove ->rw_page". - We get some maple_tree improvements and cleanups in Liam Howlett's series "VMA tree type safety and remove __vma_adjust()". - Suren Baghdasaryan has done some work on the maintainability of our vm_flags handling in the series "introduce vm_flags modifier functions". - Some pagemap cleanup and generalization work in Mike Rapoport's series "mm, arch: add generic implementation of pfn_valid() for FLATMEM" and "fixups for generic implementation of pfn_valid()" - Baoquan He has done some work to make /proc/vmallocinfo and /proc/kcore better represent the real state of things in his series "mm/vmalloc.c: allow vread() to read out vm_map_ram areas". - Jason Gunthorpe rationalized the GUP system's interface to the rest of the kernel in the series "Simplify the external interface for GUP". - SeongJae Park wishes to migrate people from DAMON's debugfs interface over to its sysfs interface. To support this, we'll temporarily be printing warnings when people use the debugfs interface. See the series "mm/damon: deprecate DAMON debugfs interface". - Andrey Konovalov provided the accurately named "lib/stackdepot: fixes and clean-ups" series. - Huang Ying has provided a dramatic reduction in migration's TLB flush IPI rates with the series "migrate_pages(): batch TLB flushing". - Arnd Bergmann has some objtool fixups in "objtool warning fixes". * tag 'mm-stable-2023-02-20-13-37' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (505 commits) include/linux/migrate.h: remove unneeded externs mm/memory_hotplug: cleanup return value handing in do_migrate_range() mm/uffd: fix comment in handling pte markers mm: change to return bool for isolate_movable_page() mm: hugetlb: change to return bool for isolate_hugetlb() mm: change to return bool for isolate_lru_page() mm: change to return bool for folio_isolate_lru() objtool: add UACCESS exceptions for __tsan_volatile_read/write kmsan: disable ftrace in kmsan core code kasan: mark addr_has_metadata __always_inline mm: memcontrol: rename memcg_kmem_enabled() sh: initialize max_mapnr m68k/nommu: add missing definition of ARCH_PFN_OFFSET mm: percpu: fix incorrect size in pcpu_obj_full_size() maple_tree: reduce stack usage with gcc-9 and earlier mm: page_alloc: call panic() when memoryless node allocation fails mm: multi-gen LRU: avoid futile retries migrate_pages: move THP/hugetlb migration support check to simplify code migrate_pages: batch flushing TLB migrate_pages: share more code between _unmap and _move ...
170 lines
7.5 KiB
ReStructuredText
170 lines
7.5 KiB
ReStructuredText
============================
|
|
Transparent Hugepage Support
|
|
============================
|
|
|
|
This document describes design principles for Transparent Hugepage (THP)
|
|
support and its interaction with other parts of the memory management
|
|
system.
|
|
|
|
Design principles
|
|
=================
|
|
|
|
- "graceful fallback": mm components which don't have transparent hugepage
|
|
knowledge fall back to breaking huge pmd mapping into table of ptes and,
|
|
if necessary, split a transparent hugepage. Therefore these components
|
|
can continue working on the regular pages or regular pte mappings.
|
|
|
|
- if a hugepage allocation fails because of memory fragmentation,
|
|
regular pages should be gracefully allocated instead and mixed in
|
|
the same vma without any failure or significant delay and without
|
|
userland noticing
|
|
|
|
- if some task quits and more hugepages become available (either
|
|
immediately in the buddy or through the VM), guest physical memory
|
|
backed by regular pages should be relocated on hugepages
|
|
automatically (with khugepaged)
|
|
|
|
- it doesn't require memory reservation and in turn it uses hugepages
|
|
whenever possible (the only possible reservation here is kernelcore=
|
|
to avoid unmovable pages to fragment all the memory but such a tweak
|
|
is not specific to transparent hugepage support and it's a generic
|
|
feature that applies to all dynamic high order allocations in the
|
|
kernel)
|
|
|
|
get_user_pages and follow_page
|
|
==============================
|
|
|
|
get_user_pages and follow_page if run on a hugepage, will return the
|
|
head or tail pages as usual (exactly as they would do on
|
|
hugetlbfs). Most GUP users will only care about the actual physical
|
|
address of the page and its temporary pinning to release after the I/O
|
|
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
|
|
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
|
|
to check head page instead. Taking a reference on any head/tail page would
|
|
prevent the page from being split by anyone.
|
|
|
|
.. note::
|
|
these aren't new constraints to the GUP API, and they match the
|
|
same constraints that apply to hugetlbfs too, so any driver capable
|
|
of handling GUP on hugetlbfs will also work fine on transparent
|
|
hugepage backed mappings.
|
|
|
|
Graceful fallback
|
|
=================
|
|
|
|
Code walking pagetables but unaware about huge pmds can simply call
|
|
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
|
|
by just grepping for "pmd_offset" and adding split_huge_pmd where
|
|
missing after pmd_offset returns the pmd. Thanks to the graceful
|
|
fallback design, with a one liner change, you can avoid to write
|
|
hundreds if not thousands of lines of complex code to make your code
|
|
hugepage aware.
|
|
|
|
If you're not walking pagetables but you run into a physical hugepage
|
|
that you can't handle natively in your code, you can split it by
|
|
calling split_huge_page(page). This is what the Linux VM does before
|
|
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
|
|
change::
|
|
|
|
diff --git a/mm/mremap.c b/mm/mremap.c
|
|
--- a/mm/mremap.c
|
|
+++ b/mm/mremap.c
|
|
@@ -41,6 +41,7 @@ static pmd_t *get_old_pmd(struct mm_stru
|
|
return NULL;
|
|
|
|
pmd = pmd_offset(pud, addr);
|
|
+ split_huge_pmd(vma, pmd, addr);
|
|
if (pmd_none_or_clear_bad(pmd))
|
|
return NULL;
|
|
|
|
Locking in hugepage aware code
|
|
==============================
|
|
|
|
We want as much code as possible hugepage aware, as calling
|
|
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
|
|
pmd_trans_huge() on the pmd returned by pmd_offset. You must hold the
|
|
mmap_lock in read (or write) mode to be sure a huge pmd cannot be
|
|
created from under you by khugepaged (khugepaged collapse_huge_page
|
|
takes the mmap_lock in write mode in addition to the anon_vma lock). If
|
|
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
|
|
page table lock (pmd_lock()) and re-run pmd_trans_huge. Taking the
|
|
page table lock will prevent the huge pmd being converted into a
|
|
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
|
|
should just drop the page table lock and fallback to the old code as
|
|
before. Otherwise, you can proceed to process the huge pmd and the
|
|
hugepage natively. Once finished, you can drop the page table lock.
|
|
|
|
Refcounts and transparent huge pages
|
|
====================================
|
|
|
|
Refcounting on THP is mostly consistent with refcounting on other compound
|
|
pages:
|
|
|
|
- get_page()/put_page() and GUP operate on the folio->_refcount.
|
|
|
|
- ->_refcount in tail pages is always zero: get_page_unless_zero() never
|
|
succeeds on tail pages.
|
|
|
|
- map/unmap of a PMD entry for the whole THP increment/decrement
|
|
folio->_entire_mapcount and also increment/decrement
|
|
folio->_nr_pages_mapped by COMPOUND_MAPPED when _entire_mapcount
|
|
goes from -1 to 0 or 0 to -1.
|
|
|
|
- map/unmap of individual pages with PTE entry increment/decrement
|
|
page->_mapcount and also increment/decrement folio->_nr_pages_mapped
|
|
when page->_mapcount goes from -1 to 0 or 0 to -1 as this counts
|
|
the number of pages mapped by PTE.
|
|
|
|
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
|
|
structures. It can be done easily for refcounts taken by page table
|
|
entries, but we don't have enough information on how to distribute any
|
|
additional pins (i.e. from get_user_pages). split_huge_page() fails any
|
|
requests to split pinned huge pages: it expects page count to be equal to
|
|
the sum of mapcount of all sub-pages plus one (split_huge_page caller must
|
|
have a reference to the head page).
|
|
|
|
split_huge_page uses migration entries to stabilize page->_refcount and
|
|
page->_mapcount of anonymous pages. File pages just get unmapped.
|
|
|
|
We are safe against physical memory scanners too: the only legitimate way
|
|
a scanner can get a reference to a page is get_page_unless_zero().
|
|
|
|
All tail pages have zero ->_refcount until atomic_add(). This prevents the
|
|
scanner from getting a reference to the tail page up to that point. After the
|
|
atomic_add() we don't care about the ->_refcount value. We already know how
|
|
many references should be uncharged from the head page.
|
|
|
|
For head page get_page_unless_zero() will succeed and we don't mind. It's
|
|
clear where references should go after split: it will stay on the head page.
|
|
|
|
Note that split_huge_pmd() doesn't have any limitations on refcounting:
|
|
pmd can be split at any point and never fails.
|
|
|
|
Partial unmap and deferred_split_folio()
|
|
========================================
|
|
|
|
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 also might be
|
|
counterproductive since in many cases partial unmap happens during exit(2) if
|
|
a THP crosses a VMA boundary.
|
|
|
|
The function deferred_split_folio() is used to queue a folio for splitting.
|
|
The splitting itself will happen when we get memory pressure via shrinker
|
|
interface.
|