khugepaged: mTHP support
Subsystems (6)
v13 cover khugepaged: mTHP support
The following series provides khugepaged with the capability to collapse anonymous memory regions to mTHPs.
To achieve this we generalize the khugepaged functions to no longer depend on PMD_ORDER. Then during the PMD scan, we use a bitmap to track individual pages that are occupied (!none/zero). After the PMD scan is done, we use the bitmap to find the optimal mTHP sizes for the PMD range. The restriction on max_ptes_none is removed during the scan, to make sure we account for the whole PMD range in the bitmap. When no mTHP size is enabled, the legacy behavior of khugepaged is maintained.
We currently only support max_ptes_none values of 0 or HPAGE_PMD_NR - 1 (ie 511). If any other value is specified, the kernel will emit a warning and no mTHP collapse will be attempted. If a mTHP collapse is attempted, but contains swapped out, or shared pages, we don't perform the collapse. It is now also possible to collapse to mTHPs without requiring the PMD THP size to be enabled. These limitiations are to prevent collapse "creep" behavior. This prevents constantly promoting mTHPs to the next available size, which would occur because a collapse introduces more non-zero pages that would satisfy the promotion condition on subsequent scans.
Patch 1: Refactor/rename hpage_collapse Patch 2: Refactoring to combine madvise_collapse and khugepaged Patch 3-8: Generalize khugepaged functions for arbitrary orders and introduce some helper functions Patch 9: skip collapsing mTHP to smaller orders Patch 10-11: Add per-order mTHP statistics and tracepoints Patch 12: Introduce collapse_allowable_orders Patch 13-15: Introduce bitmap and mTHP collapse support, fully enabled Patch 16: Documentation
---------
Testing
---------
- Built for x86_64, aarch64, ppc64le, and s390x
- selftests mm
- I created a test script that I used to push khugepaged to its limits while monitoring a number of stats and tracepoints. The code is available here[1] (Run in legacy mode for these changes and set mthp sizes to inherit)
The summary from my testings was that there was no significant regression noticed through this test. In some cases my changes had better collapse latencies, and was able to scan more pages in the same amount of time/work, but for the most part the results were consistent.
- redis testing. I tested these changes along with my defer changes (see followup [2] post for more details). We've decided to get the mTHP changes merged first before attempting the defer series.
- some basic testing on 64k page size.
- lots of general use.
V13 Changes:
- Lots of minor nits, cleanups, comments, and renames
- Bitmap function simplification and more helpers (Wei, Lorenzo)
- Max_ptes_none (0 or 511) restriction
- commit description expansion
- list all reachable enum values in mthp_collapse()
- Fix ppc64 compile error due to using HPAGE_PMD_ORDER (replace with ilog2(MAX_PTRS_PER_PTE))
V12: https://lore.kernel.org/lkml/[email protected]/
V11: https://lore.kernel.org/lkml/[email protected]/
V10: https://lore.kernel.org/lkml/[email protected]/
V9 : https://lore.kernel.org/lkml/[email protected]/
V8 : https://lore.kernel.org/lkml/[email protected]/
V7 : https://lore.kernel.org/lkml/[email protected]/
V6 : https://lore.kernel.org/lkml/[email protected]/
V5 : https://lore.kernel.org/lkml/[email protected]/
V4 : https://lore.kernel.org/lkml/[email protected]/
V3 : https://lore.kernel.org/lkml/[email protected]/
V2 : https://lore.kernel.org/lkml/[email protected]/
V1 : https://lore.kernel.org/lkml/[email protected]/
A big thanks to everyone that has reviewed, tested, and participated in the development process. Its been a great experience working with all of you on this endeavour.
[1] - https://gitlab.com/npache/khugepaged_mthp_test
[2] - https://lore.kernel.org/lkml/[email protected]/
Baolin Wang (1):
khugepaged: run khugepaged for all orders
Dev Jain (1):
khugepaged: generalize alloc_charge_folio()
Nico Pache (14):
khugepaged: rename hpage_collapse_* to collapse_* introduce collapse_single_pmd to unify khugepaged and madvise_collapse
khugepaged: generalize hugepage_vma_revalidate for mTHP support
khugepaged: introduce is_mthp_order helper
khugepaged: generalize __collapse_huge_page_* for mTHP support
khugepaged: introduce collapse_max_ptes_none helper function
khugepaged: generalize collapse_huge_page for mTHP collapse
khugepaged: skip collapsing mTHP to smaller orders
khugepaged: add per-order mTHP collapse failure statistics
khugepaged: improve tracepoints for mTHP orders
khugepaged: introduce collapse_allowable_orders helper function
khugepaged: Introduce mTHP collapse support
khugepaged: avoid unnecessary mTHP collapse attempts
Documentation: mm: update the admin guide for mTHP collapse
Documentation/admin-guide/mm/transhuge.rst | 80 ++- include/linux/huge_mm.h | 5 + include/trace/events/huge_memory.h | 34 +- mm/huge_memory.c | 11 + mm/khugepaged.c | 696 +++++++++++++++------ mm/mremap.c | 2 +- 6 files changed, 618 insertions(+), 210 deletions(-)
Signature · 1 line
2.51.1
PATCH v13 1/16 khugepaged: rename hpage_collapse_* to collapse_*
The hpage_collapse functions describe functions used by madvise_collapse and khugepaged. remove the unnecessary hpage prefix to shorten the function name.
--- mm/khugepaged.c | 73 ++++++++++++++++++++++++------------------------- mm/mremap.c | 2 +- 2 files changed, 37 insertions(+), 38 deletions(-)
mm/khugepaged.c +36 -37
@@ -394,14 +394,14 @@ void __init khugepaged_destroy(void)kmem_cache_destroy (mm_slot_cache); } -static inline int hpage_collapse_test_exit (struct mm_struct *mm) +static inline int collapse_test_exit (struct mm_struct *mm) {return atomic_read (&mm->mm_users) ==0 ; } -static inline int hpage_collapse_test_exit_or_disable (struct mm_struct *mm) +static inline int collapse_test_exit_or_disable (struct mm_struct *mm) { -return hpage_collapse_test_exit (mm) || +return collapse_test_exit (mm) ||mm_flags_test (MMF_DISABLE_THP_COMPLETELY , mm); }
@@ -435,7 +435,7 @@ void __khugepaged_enter(struct mm_struct *mm)int wakeup;/* __khugepaged_exit() must not run from under us */ -VM_BUG_ON_MM (hpage_collapse_test_exit (mm), mm); +VM_BUG_ON_MM (collapse_test_exit (mm), mm);if (unlikely (mm_flags_test_and_set (MMF_VM_HUGEPAGE , mm)))return ;
@@ -489,7 +489,7 @@ void __khugepaged_exit(struct mm_struct *mm) }else if (slot) {/* * This is required to serialize against -* hpage_collapse_test_exit() (which is guaranteed to run +* collapse_test_exit() (which is guaranteed to run * under mmap sem read mode). Stop here (after we return all * pagetables will be destroyed) until khugepaged has finished * working on the pagetables under the mmap_lock.
@@ -579,7 +579,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, folio =page_folio (page);VM_BUG_ON_FOLIO (!folio_test_anon (folio), folio); -/* See hpage_collapse_scan_pmd(). */ +/* See collapse_scan_pmd(). */ if (folio_maybe_mapped_shared (folio)) { ++shared; if (cc->is_khugepaged &&
@@ -830,7 +830,7 @@ struct collapse_control khugepaged_collapse_control = { .is_khugepaged = true, }; -static bool hpage_collapse_scan_abort (int nid,struct collapse_control *cc) +static bool collapse_scan_abort (int nid,struct collapse_control *cc) {int i;
@@ -865,7 +865,7 @@ static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void) }#ifdef CONFIG_NUMA -static int hpage_collapse_find_target_node (struct collapse_control *cc) +static int collapse_find_target_node (struct collapse_control *cc) {int nid, target_node =0 , max_value =0 ;
@@ -884,7 +884,7 @@ static int hpage_collapse_find_target_node(struct collapse_control *cc) return target_node; }#else -static int hpage_collapse_find_target_node (struct collapse_control *cc) +static int collapse_find_target_node (struct collapse_control *cc) {return 0 ; }
@@ -905,7 +905,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,enum tva_type type = cc->is_khugepaged ?TVA_KHUGEPAGED :TVA_FORCED_COLLAPSE ; -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) +if (unlikely (collapse_test_exit_or_disable (mm)))return SCAN_ANY_PROCESS ; *vmap = vma =find_vma (mm, address);
@@ -978,7 +978,7 @@ static int check_pmd_still_valid(struct mm_struct *mm,/* * Bring missing pages in from swap, to complete THP collapse. -* Only done if hpage_collapse_scan_pmd believes it is worthwhile. +* Only done if khugepaged_scan_pmd believes it is worthwhile. * * Called and returns without pte mapped or spinlocks held. * Returns result: if not SCAN_SUCCEED, mmap_lock has been released.
@@ -1065,7 +1065,7 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, {gfp_t gfp = (cc->is_khugepaged ?alloc_hugepage_khugepaged_gfpmask () :GFP_TRANSHUGE ); -int node =hpage_collapse_find_target_node (cc); +int node =collapse_find_target_node (cc);struct folio *folio; folio =__folio_alloc (gfp,HPAGE_PMD_ORDER , node, &cc->alloc_nmask);
@@ -1244,10 +1244,10 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,return result; } -static int hpage_collapse_scan_pmd (struct mm_struct *mm, -struct vm_area_struct *vma, -unsigned long start_addr,bool *mmap_locked, -struct collapse_control *cc) +static int collapse_scan_pmd (struct mm_struct *mm, +struct vm_area_struct *vma, +unsigned long start_addr,bool *mmap_locked, +struct collapse_control *cc) {pmd_t *pmd;pte_t *pte, *_pte;
@@ -1355,7 +1355,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,* hit record. */ node =folio_nid (folio); -if (hpage_collapse_scan_abort (node, cc)) { +if (collapse_scan_abort (node, cc)) { result =SCAN_SCAN_ABORT ; goto out_unmap; }
@@ -1421,7 +1421,7 @@ static void collect_mm_slot(struct mm_slot *slot)lockdep_assert_held (&khugepaged_mm_lock); -if (hpage_collapse_test_exit (mm)) { +if (collapse_test_exit (mm)) {/* free mm_slot */ hash_del (&slot->hash);list_del (&slot->mm_node);
@@ -1769,7 +1769,7 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)if (find_pmd_or_thp_or_none (mm, addr, &pmd) !=SCAN_SUCCEED )continue ; -if (hpage_collapse_test_exit (mm)) +if (collapse_test_exit (mm))continue ;if (!file_backed_vma_is_retractable (vma))
@@ -2285,9 +2285,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,return result; } -static int hpage_collapse_scan_file (struct mm_struct *mm,unsigned long addr, -struct file *file,pgoff_t start, -struct collapse_control *cc) +static int collapse_scan_file (struct mm_struct *mm,unsigned long addr, +struct file *file,pgoff_t start, +struct collapse_control *cc) {struct folio *folio =NULL ;struct address_space *mapping = file->f_mapping;
@@ -2342,7 +2342,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr, } node =folio_nid (folio); -if (hpage_collapse_scan_abort (node, cc)) { +if (collapse_scan_abort (node, cc)) { result =SCAN_SCAN_ABORT ;folio_put (folio);break ;
@@ -2392,7 +2392,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,return result; } -static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, +static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result, struct collapse_control *cc) __releases(&khugepaged_mm_lock) __acquires(&khugepaged_mm_lock)
@@ -2427,7 +2427,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, goto breakouterloop_mmap_lock; progress++; -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) +if (unlikely (collapse_test_exit_or_disable (mm))) goto breakouterloop;vma_iter_init (&vmi, mm, khugepaged_scan.address);
@@ -2435,7 +2435,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,unsigned long hstart, hend;cond_resched (); -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) { +if (unlikely (collapse_test_exit_or_disable (mm))) { progress++;break ; }
@@ -2456,7 +2456,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,bool mmap_locked = true;cond_resched (); -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) +if (unlikely (collapse_test_exit_or_disable (mm))) goto breakouterloop; VM_BUG_ON(khugepaged_scan.address < hstart ||
@@ -2469,12 +2469,12 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,mmap_read_unlock (mm); mmap_locked = false; - *result =hpage_collapse_scan_file (mm, + *result =collapse_scan_file (mm, khugepaged_scan.address, file, pgoff, cc);fput (file);if (*result ==SCAN_PTE_MAPPED_HUGEPAGE ) {mmap_read_lock (mm); -if (hpage_collapse_test_exit_or_disable (mm)) +if (collapse_test_exit_or_disable (mm)) goto breakouterloop; *result =collapse_pte_mapped_thp (mm, khugepaged_scan.address, false);
@@ -2483,7 +2483,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,mmap_read_unlock (mm); } }else { - *result =hpage_collapse_scan_pmd (mm, vma, + *result =collapse_scan_pmd (mm, vma, khugepaged_scan.address, &mmap_locked, cc); }
@@ -2516,7 +2516,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,* Release the current mm_slot if this mm is about to die, or * if we scanned all vmas of this mm. */ -if (hpage_collapse_test_exit (mm) || !vma) { +if (collapse_test_exit (mm) || !vma) {/* * Make sure that if mm_users is reaching zero while * khugepaged runs here, khugepaged_exit will find
@@ -2567,8 +2567,8 @@ static void khugepaged_do_scan(struct collapse_control *cc) pass_through_head++;if (khugepaged_has_work () && pass_through_head <2 ) - progress +=khugepaged_scan_mm_slot (pages - progress, - &result, cc); + progress +=collapse_scan_mm_slot (pages - progress, + &result, cc);else progress = pages;spin_unlock (&khugepaged_mm_lock);
@@ -2807,12 +2807,11 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,mmap_read_unlock (mm); mmap_locked = false; - result =hpage_collapse_scan_file (mm, addr, file, pgoff, - cc); + result =collapse_scan_file (mm, addr, file, pgoff, cc);fput (file); }else { - result =hpage_collapse_scan_pmd (mm, vma, addr, - &mmap_locked, cc); + result =collapse_scan_pmd (mm, vma, addr, + &mmap_locked, cc); }if (!mmap_locked) *lock_dropped = true;
mm/mremap.c +1 -1
@@ -244,7 +244,7 @@ static int move_ptes(struct pagetable_move_control *pmc, goto out; }/* -* Now new_pte is none, so hpage_collapse_scan_file() path can not find +* Now new_pte is none, so collapse_scan_file() path can not find * this by traversing file->f_mapping, so there is no concurrency with * retract_page_tables(). In addition, we already hold the exclusive * mmap_lock, so this new_pte page is stable, so there is no need to get
Signature · 1 line
2.51.1
Re: [PATCH v13 mm-new 01/16] khugepaged: rename hpage_collapse_* to collapse_*
FYI there's a merge conflict vs. mm-unstable atm (mm-new is unworkably broken, so prefer to base on mm-unstable if possible thanks).
In mm/khugepaged.c:
<<<<<<< HEAD
static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, struct collapse_control *cc)
=======
static int collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma,
unsigned long start_addr, bool *mmap_locked, struct collapse_control *cc)
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
khugepaged: rename hpage_collapse_* to collapse_*
<<<<<<< HEAD
static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
struct file *file, pgoff_t start, struct collapse_control *cc)
=======
static int collapse_scan_file(struct mm_struct *mm, unsigned long addr,
struct file *file, pgoff_t start,
struct collapse_control *cc)
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
khugepaged: rename hpage_collapse_* to collapse_*
<<<<<<< HEAD
static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result *result,
=======
static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result,
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
khugepaged: rename hpage_collapse_* to collapse_*
<<<<<<< HEAD
static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
struct file *file, pgoff_t start, struct collapse_control *cc)
=======
static int collapse_scan_file(struct mm_struct *mm, unsigned long addr,
struct file *file, pgoff_t start,
struct collapse_control *cc)
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
khugepaged: rename hpage_collapse_* to collapse_*
Maybe worth just double-tab indenting the args on subsequent lines to make dealing with this kind of thing a bit easier in general? Tab/space alignment to function name, while neat, can be a pain when return type changes.
Quoted 296 lines · Nico Pache
The
hpage_collapsefunctions describe functions used bymadvise_collapseand khugepaged. remove the unnecessary hpage prefix to shorten the function name.Reviewed-by Wei Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Liam R. Howlett <[email protected]>Reviewed-by Zi Yan <[email protected]>Reviewed-by Baolin Wang <[email protected]>Reviewed-by Lorenzo Stoakes <[email protected]>Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 73 ++++++++++++++++++++++++------------------------- mm/mremap.c | 2 +- 2 files changed, 37 insertions(+), 38 deletions(-)mm/khugepaged.c +36
-37@@ -394,14 +394,14 @@ void __init khugepaged_destroy(void)kmem_cache_destroy (mm_slot_cache); } -static inline int hpage_collapse_test_exit (struct mm_struct *mm) +static inline int collapse_test_exit (struct mm_struct *mm) {return atomic_read (&mm->mm_users) ==0 ; } -static inline int hpage_collapse_test_exit_or_disable (struct mm_struct *mm) +static inline int collapse_test_exit_or_disable (struct mm_struct *mm) { -return hpage_collapse_test_exit (mm) || +return collapse_test_exit (mm) ||mm_flags_test (MMF_DISABLE_THP_COMPLETELY , mm); }@@ -435,7 +435,7 @@ void __khugepaged_enter(struct mm_struct *mm)int wakeup;/* __khugepaged_exit() must not run from under us */ -VM_BUG_ON_MM (hpage_collapse_test_exit (mm), mm); +VM_BUG_ON_MM (collapse_test_exit (mm), mm);if (unlikely (mm_flags_test_and_set (MMF_VM_HUGEPAGE , mm)))return ;@@ -489,7 +489,7 @@ void __khugepaged_exit(struct mm_struct *mm) }else if (slot) {/* * This is required to serialize against -* hpage_collapse_test_exit() (which is guaranteed to run +* collapse_test_exit() (which is guaranteed to run * under mmap sem read mode). Stop here (after we return all * pagetables will be destroyed) until khugepaged has finished * working on the pagetables under the mmap_lock. @@ -579,7 +579,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, folio =page_folio (page);VM_BUG_ON_FOLIO (!folio_test_anon (folio), folio); -/* See hpage_collapse_scan_pmd(). */ +/* See collapse_scan_pmd(). */ if (folio_maybe_mapped_shared (folio)) { ++shared; if (cc->is_khugepaged &&@@ -830,7 +830,7 @@ struct collapse_control khugepaged_collapse_control = { .is_khugepaged = true, }; -static bool hpage_collapse_scan_abort (int nid,struct collapse_control *cc) +static bool collapse_scan_abort (int nid,struct collapse_control *cc) {int i;@@ -865,7 +865,7 @@ static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void) }#ifdef CONFIG_NUMA -static int hpage_collapse_find_target_node (struct collapse_control *cc) +static int collapse_find_target_node (struct collapse_control *cc) {int nid, target_node =0 , max_value =0 ;@@ -884,7 +884,7 @@ static int hpage_collapse_find_target_node(struct collapse_control *cc) return target_node; }#else -static int hpage_collapse_find_target_node (struct collapse_control *cc) +static int collapse_find_target_node (struct collapse_control *cc) {return 0 ; }@@ -905,7 +905,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,enum tva_type type = cc->is_khugepaged ?TVA_KHUGEPAGED :TVA_FORCED_COLLAPSE ; -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) +if (unlikely (collapse_test_exit_or_disable (mm)))return SCAN_ANY_PROCESS ; *vmap = vma =find_vma (mm, address);@@ -978,7 +978,7 @@ static int check_pmd_still_valid(struct mm_struct *mm,/* * Bring missing pages in from swap, to complete THP collapse. -* Only done if hpage_collapse_scan_pmd believes it is worthwhile. +* Only done if khugepaged_scan_pmd believes it is worthwhile. * * Called and returns without pte mapped or spinlocks held. * Returns result: if not SCAN_SUCCEED, mmap_lock has been released. @@ -1065,7 +1065,7 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, {gfp_t gfp = (cc->is_khugepaged ?alloc_hugepage_khugepaged_gfpmask () :GFP_TRANSHUGE ); -int node =hpage_collapse_find_target_node (cc); +int node =collapse_find_target_node (cc);struct folio *folio; folio =__folio_alloc (gfp,HPAGE_PMD_ORDER , node, &cc->alloc_nmask);@@ -1244,10 +1244,10 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,return result; } -static int hpage_collapse_scan_pmd (struct mm_struct *mm, -struct vm_area_struct *vma, -unsigned long start_addr,bool *mmap_locked, -struct collapse_control *cc) +static int collapse_scan_pmd (struct mm_struct *mm, +struct vm_area_struct *vma, +unsigned long start_addr,bool *mmap_locked, +struct collapse_control *cc) {pmd_t *pmd;pte_t *pte, *_pte;@@ -1355,7 +1355,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,* hit record. */ node =folio_nid (folio); -if (hpage_collapse_scan_abort (node, cc)) { +if (collapse_scan_abort (node, cc)) { result =SCAN_SCAN_ABORT ; goto out_unmap; }@@ -1421,7 +1421,7 @@ static void collect_mm_slot(struct mm_slot *slot)lockdep_assert_held (&khugepaged_mm_lock); -if (hpage_collapse_test_exit (mm)) { +if (collapse_test_exit (mm)) {/* free mm_slot */ hash_del (&slot->hash);list_del (&slot->mm_node);@@ -1769,7 +1769,7 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)if (find_pmd_or_thp_or_none (mm, addr, &pmd) !=SCAN_SUCCEED )continue ; -if (hpage_collapse_test_exit (mm)) +if (collapse_test_exit (mm))continue ;if (!file_backed_vma_is_retractable (vma))@@ -2285,9 +2285,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,return result; } -static int hpage_collapse_scan_file (struct mm_struct *mm,unsigned long addr, -struct file *file,pgoff_t start, -struct collapse_control *cc) +static int collapse_scan_file (struct mm_struct *mm,unsigned long addr, +struct file *file,pgoff_t start, +struct collapse_control *cc) {struct folio *folio =NULL ;struct address_space *mapping = file->f_mapping;@@ -2342,7 +2342,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr, } node =folio_nid (folio); -if (hpage_collapse_scan_abort (node, cc)) { +if (collapse_scan_abort (node, cc)) { result =SCAN_SCAN_ABORT ;folio_put (folio);break ;@@ -2392,7 +2392,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,return result; } -static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, +static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result, struct collapse_control *cc) __releases(&khugepaged_mm_lock) __acquires(&khugepaged_mm_lock)@@ -2427,7 +2427,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, goto breakouterloop_mmap_lock; progress++; -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) +if (unlikely (collapse_test_exit_or_disable (mm))) goto breakouterloop;vma_iter_init (&vmi, mm, khugepaged_scan.address);@@ -2435,7 +2435,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,unsigned long hstart, hend;cond_resched (); -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) { +if (unlikely (collapse_test_exit_or_disable (mm))) { progress++;break ; }@@ -2456,7 +2456,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,bool mmap_locked = true;cond_resched (); -if (unlikely (hpage_collapse_test_exit_or_disable (mm))) +if (unlikely (collapse_test_exit_or_disable (mm))) goto breakouterloop; VM_BUG_ON(khugepaged_scan.address < hstart ||@@ -2469,12 +2469,12 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,mmap_read_unlock (mm); mmap_locked = false; - *result =hpage_collapse_scan_file (mm, + *result =collapse_scan_file (mm, khugepaged_scan.address, file, pgoff, cc);fput (file);if (*result ==SCAN_PTE_MAPPED_HUGEPAGE ) {mmap_read_lock (mm); -if (hpage_collapse_test_exit_or_disable (mm)) +if (collapse_test_exit_or_disable (mm)) goto breakouterloop; *result =collapse_pte_mapped_thp (mm, khugepaged_scan.address, false);@@ -2483,7 +2483,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,mmap_read_unlock (mm); } }else { - *result =hpage_collapse_scan_pmd (mm, vma, + *result =collapse_scan_pmd (mm, vma, khugepaged_scan.address, &mmap_locked, cc); }@@ -2516,7 +2516,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,* Release the current mm_slot if this mm is about to die, or * if we scanned all vmas of this mm. */ -if (hpage_collapse_test_exit (mm) || !vma) { +if (collapse_test_exit (mm) || !vma) {/* * Make sure that if mm_users is reaching zero while * khugepaged runs here, khugepaged_exit will find @@ -2567,8 +2567,8 @@ static void khugepaged_do_scan(struct collapse_control *cc) pass_through_head++;if (khugepaged_has_work () && pass_through_head <2 ) - progress +=khugepaged_scan_mm_slot (pages - progress, - &result, cc); + progress +=collapse_scan_mm_slot (pages - progress, + &result, cc);else progress = pages;spin_unlock (&khugepaged_mm_lock);@@ -2807,12 +2807,11 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,mmap_read_unlock (mm); mmap_locked = false; - result =hpage_collapse_scan_file (mm, addr, file, pgoff, - cc); + result =collapse_scan_file (mm, addr, file, pgoff, cc);fput (file); }else { - result =hpage_collapse_scan_pmd (mm, vma, addr, - &mmap_locked, cc); + result =collapse_scan_pmd (mm, vma, addr, + &mmap_locked, cc); }if (!mmap_locked) *lock_dropped = true;mm/mremap.c +1
-2@@ -244,7 +244,7 @@ static int move_ptes(struct pagetable_move_control *pmc, goto out; }/* -* Now new_pte is none, so hpage_collapse_scan_file() path can not find +* Now new_pte is none, so collapse_scan_file() path can not find * this by traversing file->f_mapping, so there is no concurrency with * retract_page_tables(). In addition, we already hold the exclusive * mmap_lock, so this new_pte page is stable, so there is no need to get -- 2.51.1
PATCH v13 2/16 introduce collapse_single_pmd to unify khugepaged and madvise_collapse
In reply to Nico PacheThe khugepaged daemon and madvise_collapse have two different implementations that do almost the same thing.
Create collapse_single_pmd to increase code reuse and create an entry point to these two users.
Refactor madvise_collapse and collapse_scan_mm_slot to use the new collapse_single_pmd function. This introduces a minor behavioral change that is most likely an undiscovered bug. The current implementation of khugepaged tests collapse_test_exit_or_disable before calling collapse_pte_mapped_thp, but we weren't doing it in the madvise_collapse case. By unifying these two callers madvise_collapse now also performs this check. We also modify the return value to be SCAN_ANY_PROCESS which properly indicates that this process is no longer valid to operate on.
We also guard the khugepaged_pages_collapsed variable to ensure its only incremented for khugepaged.
--- mm/khugepaged.c | 97 ++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 45 deletions(-)
mm/khugepaged.c +52 -45
@@ -2392,6 +2392,53 @@ static int collapse_scan_file(struct mm_struct *mm, unsigned long addr,return result; } +/* +* Try to collapse a single PMD starting at a PMD aligned addr, and return +* the results. +*/ +static int collapse_single_pmd (unsigned long addr, +struct vm_area_struct *vma,bool *mmap_locked, +struct collapse_control *cc) +{ +struct mm_struct *mm = vma->vm_mm; +int result; +struct file *file; +pgoff_t pgoff; + +if (vma_is_anonymous (vma)) { + result =collapse_scan_pmd (mm, vma, addr, mmap_locked, cc); + goto end; + } + + file =get_file (vma->vm_file); + pgoff =linear_page_index (vma, addr); + +mmap_read_unlock (mm); + *mmap_locked = false; + result =collapse_scan_file (mm, addr, file, pgoff, cc); +fput (file); +if (result !=SCAN_PTE_MAPPED_HUGEPAGE ) + goto end; + +mmap_read_lock (mm); + *mmap_locked = true; +if (collapse_test_exit_or_disable (mm)) { +mmap_read_unlock (mm); + *mmap_locked = false; +return SCAN_ANY_PROCESS ; + } + result =collapse_pte_mapped_thp (mm, addr, !cc->is_khugepaged); +if (result ==SCAN_PMD_MAPPED ) + result =SCAN_SUCCEED ; +mmap_read_unlock (mm); + *mmap_locked = false; + +end: +if (cc->is_khugepaged && result ==SCAN_SUCCEED ) + ++khugepaged_pages_collapsed; +return result; +} + static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result, struct collapse_control *cc) __releases(&khugepaged_mm_lock)
@@ -2462,34 +2509,9 @@ static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result,VM_BUG_ON (khugepaged_scan.address < hstart || khugepaged_scan.address +HPAGE_PMD_SIZE > hend); -if (!vma_is_anonymous (vma)) { -struct file *file =get_file (vma->vm_file); -pgoff_t pgoff =linear_page_index (vma, - khugepaged_scan.address); - -mmap_read_unlock (mm); - mmap_locked = false; - *result =collapse_scan_file (mm, - khugepaged_scan.address, file, pgoff, cc); -fput (file); -if (*result ==SCAN_PTE_MAPPED_HUGEPAGE ) { -mmap_read_lock (mm); -if (collapse_test_exit_or_disable (mm)) - goto breakouterloop; - *result =collapse_pte_mapped_thp (mm, - khugepaged_scan.address, false); -if (*result ==SCAN_PMD_MAPPED ) - *result =SCAN_SUCCEED ; -mmap_read_unlock (mm); - } - }else { - *result =collapse_scan_pmd (mm, vma, - khugepaged_scan.address, &mmap_locked, cc); - } - -if (*result ==SCAN_SUCCEED ) - ++khugepaged_pages_collapsed; + *result =collapse_single_pmd (khugepaged_scan.address, + vma, &mmap_locked, cc);/* move to next address */ khugepaged_scan.address +=HPAGE_PMD_SIZE ; progress +=HPAGE_PMD_NR ;
@@ -2801,35 +2823,20 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start, hend =min (hend, vma->vm_end &HPAGE_PMD_MASK ); }mmap_assert_locked (mm); -if (!vma_is_anonymous (vma)) { -struct file *file =get_file (vma->vm_file); -pgoff_t pgoff =linear_page_index (vma, addr); -mmap_read_unlock (mm); - mmap_locked = false; - result =collapse_scan_file (mm, addr, file, pgoff, cc); -fput (file); - }else { - result =collapse_scan_pmd (mm, vma, addr, - &mmap_locked, cc); - } + result =collapse_single_pmd (addr, vma, &mmap_locked, cc); +if (!mmap_locked) *lock_dropped = true; -handle_result:switch (result) {case SCAN_SUCCEED :case SCAN_PMD_MAPPED : ++thps;break ; -case SCAN_PTE_MAPPED_HUGEPAGE : -BUG_ON (mmap_locked); -mmap_read_lock (mm); - result =collapse_pte_mapped_thp (mm, addr, true); -mmap_read_unlock (mm); - goto handle_result;/* Whitelisted set of results where continuing OK */ case SCAN_NO_PTE_TABLE : +case SCAN_PTE_MAPPED_HUGEPAGE :case SCAN_PTE_NON_PRESENT :case SCAN_PTE_UFFD_WP :case SCAN_LACK_REFERENCED_PAGE :
Signature · 1 line
2.51.1
Zi Yan
reviewerRe: [PATCH v13 mm-new 02/16] introduce collapse_single_pmd to unify khugepaged and madvise_collapse
Quoted 24 lines · Nico Pache
The khugepaged daemon and
madvise_collapsehave two different implementations that do almost the same thing.Create
collapse_single_pmdto increase code reuse and create an entry point to these two users.Refactor
madvise_collapseandcollapse_scan_mm_slotto use the newcollapse_single_pmdfunction. This introduces a minor behavioral change that is most likely an undiscovered bug. The current implementation of khugepaged testscollapse_test_exit_or_disablebefore callingcollapse_pte_mapped_thp, but we weren't doing it in themadvise_collapsecase. By unifying these two callersmadvise_collapsenow also performs this check. We also modify the return value to beSCAN_ANY_PROCESSwhich properly indicates that this process is no longer valid to operate on.We also guard the
khugepaged_pages_collapsedvariable to ensure its only incremented for khugepaged.Reviewed-by Wei Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Lorenzo Stoakes <[email protected]>Reviewed-by Baolin Wang <[email protected]>Signed-off-by Nico Pache <[email protected]>
Quoted 4 lines
--- mm/khugepaged.c | 97 ++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 45 deletions(-)
LGTM. Reviewed-by: Zi Yan <[email protected]>
Best Regards,
Yan, Zi
Re: [PATCH v13 mm-new 02/16] introduce collapse_single_pmd to unify khugepaged and madvise_collapse
In reply to Nico PacheSorry I realise it's due to delay in review but, unsurprisingly got a knock-on merge conflict here:
Looks like it's commit 823953d831d8 ("mm/khugepaged: use enum scan_result for result variables and return types").
<<<<<<< HEAD
static unsigned int collapse_scan_mm_slot(unsigned int pages, enum scan_result *result,
struct collapse_control *cc)
=======
/*
* Try to collapse a single PMD starting at a PMD aligned addr, and return
* the results.
*/
static int collapse_single_pmd(unsigned long addr,
struct vm_area_struct *vma, bool *mmap_locked,
struct collapse_control *cc)
{
struct mm_struct *mm = vma->vm_mm;
int result;
struct file *file;
pgoff_t pgoff;
if (vma_is_anonymous(vma)) {
result = collapse_scan_pmd(mm, vma, addr, mmap_locked, cc);
goto end;
}
file = get_file(vma->vm_file);
pgoff = linear_page_index(vma, addr);
mmap_read_unlock(mm);
*mmap_locked = false;
result = collapse_scan_file(mm, addr, file, pgoff, cc);
fput(file);
if (result != SCAN_PTE_MAPPED_HUGEPAGE)
goto end;
mmap_read_lock(mm);
*mmap_locked = true;
if (collapse_test_exit_or_disable(mm)) {
mmap_read_unlock(mm);
*mmap_locked = false;
return SCAN_ANY_PROCESS;
}
result = collapse_pte_mapped_thp(mm, addr, !cc->is_khugepaged);
if (result == SCAN_PMD_MAPPED)
result = SCAN_SUCCEED;
mmap_read_unlock(mm);
*mmap_locked = false;end:
if (cc->is_khugepaged && result == SCAN_SUCCEED) ++khugepaged_pages_collapsed; return result; }
static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result,
struct collapse_control *cc)
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
introduce
collapse_single_pmdto unify khugepaged andmadvise_collapse
Quoted 165 lines · Nico Pache
The khugepaged daemon and
madvise_collapsehave two different implementations that do almost the same thing.Create
collapse_single_pmdto increase code reuse and create an entry point to these two users.Refactor
madvise_collapseandcollapse_scan_mm_slotto use the newcollapse_single_pmdfunction. This introduces a minor behavioral change that is most likely an undiscovered bug. The current implementation of khugepaged testscollapse_test_exit_or_disablebefore callingcollapse_pte_mapped_thp, but we weren't doing it in themadvise_collapsecase. By unifying these two callersmadvise_collapsenow also performs this check. We also modify the return value to beSCAN_ANY_PROCESSwhich properly indicates that this process is no longer valid to operate on.We also guard the
khugepaged_pages_collapsedvariable to ensure its only incremented for khugepaged.Reviewed-by Wei Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Lorenzo Stoakes <[email protected]>Reviewed-by Baolin Wang <[email protected]>Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 97 ++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 45 deletions(-)mm/khugepaged.c +52
-46@@ -2392,6 +2392,53 @@ static int collapse_scan_file(struct mm_struct *mm, unsigned long addr,return result; } +/* +* Try to collapse a single PMD starting at a PMD aligned addr, and return +* the results. +*/ +static int collapse_single_pmd (unsigned long addr, +struct vm_area_struct *vma,bool *mmap_locked, +struct collapse_control *cc) +{ +struct mm_struct *mm = vma->vm_mm; +int result; +struct file *file; +pgoff_t pgoff; + +if (vma_is_anonymous (vma)) { + result =collapse_scan_pmd (mm, vma, addr, mmap_locked, cc); + goto end; + } + + file =get_file (vma->vm_file); + pgoff =linear_page_index (vma, addr); + +mmap_read_unlock (mm); + *mmap_locked = false; + result =collapse_scan_file (mm, addr, file, pgoff, cc); +fput (file); +if (result !=SCAN_PTE_MAPPED_HUGEPAGE ) + goto end; + +mmap_read_lock (mm); + *mmap_locked = true; +if (collapse_test_exit_or_disable (mm)) { +mmap_read_unlock (mm); + *mmap_locked = false; +return SCAN_ANY_PROCESS ; + } + result =collapse_pte_mapped_thp (mm, addr, !cc->is_khugepaged); +if (result ==SCAN_PMD_MAPPED ) + result =SCAN_SUCCEED ; +mmap_read_unlock (mm); + *mmap_locked = false; + +end: +if (cc->is_khugepaged && result ==SCAN_SUCCEED ) + ++khugepaged_pages_collapsed; +return result; +} + static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result, struct collapse_control *cc) __releases(&khugepaged_mm_lock)@@ -2462,34 +2509,9 @@ static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result,VM_BUG_ON (khugepaged_scan.address < hstart || khugepaged_scan.address +HPAGE_PMD_SIZE > hend); -if (!vma_is_anonymous (vma)) { -struct file *file =get_file (vma->vm_file); -pgoff_t pgoff =linear_page_index (vma, - khugepaged_scan.address); - -mmap_read_unlock (mm); - mmap_locked = false; - *result =collapse_scan_file (mm, - khugepaged_scan.address, file, pgoff, cc); -fput (file); -if (*result ==SCAN_PTE_MAPPED_HUGEPAGE ) { -mmap_read_lock (mm); -if (collapse_test_exit_or_disable (mm)) - goto breakouterloop; - *result =collapse_pte_mapped_thp (mm, - khugepaged_scan.address, false); -if (*result ==SCAN_PMD_MAPPED ) - *result =SCAN_SUCCEED ; -mmap_read_unlock (mm); - } - }else { - *result =collapse_scan_pmd (mm, vma, - khugepaged_scan.address, &mmap_locked, cc); - } - -if (*result ==SCAN_SUCCEED ) - ++khugepaged_pages_collapsed; + *result =collapse_single_pmd (khugepaged_scan.address, + vma, &mmap_locked, cc);/* move to next address */ khugepaged_scan.address +=HPAGE_PMD_SIZE ; progress +=HPAGE_PMD_NR ;@@ -2801,35 +2823,20 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start, hend =min (hend, vma->vm_end &HPAGE_PMD_MASK ); }mmap_assert_locked (mm); -if (!vma_is_anonymous (vma)) { -struct file *file =get_file (vma->vm_file); -pgoff_t pgoff =linear_page_index (vma, addr); -mmap_read_unlock (mm); - mmap_locked = false; - result =collapse_scan_file (mm, addr, file, pgoff, cc); -fput (file); - }else { - result =collapse_scan_pmd (mm, vma, addr, - &mmap_locked, cc); - } + result =collapse_single_pmd (addr, vma, &mmap_locked, cc); +if (!mmap_locked) *lock_dropped = true; -handle_result:switch (result) {case SCAN_SUCCEED :case SCAN_PMD_MAPPED : ++thps;break ; -case SCAN_PTE_MAPPED_HUGEPAGE : -BUG_ON (mmap_locked); -mmap_read_lock (mm); - result =collapse_pte_mapped_thp (mm, addr, true); -mmap_read_unlock (mm); - goto handle_result;/* Whitelisted set of results where continuing OK */ case SCAN_NO_PTE_TABLE : +case SCAN_PTE_MAPPED_HUGEPAGE :case SCAN_PTE_NON_PRESENT :case SCAN_PTE_UFFD_WP :case SCAN_LACK_REFERENCED_PAGE : --2.51.1
PATCH v13 3/16 khugepaged: generalize hugepage_vma_revalidate for mTHP support
In reply to Nico PacheFor khugepaged to support different mTHP orders, we must generalize this to check if the PMD is not shared by another VMA and that the order is enabled.
No functional change in this patch. Also correct a comment about the functionality of the revalidation.
--- mm/khugepaged.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
mm/khugepaged.c +11 -9
@@ -892,14 +892,13 @@ static int collapse_find_target_node(struct collapse_control *cc)/* * If mmap_lock temporarily dropped, revalidate vma -* before taking mmap_lock. +* after taking the mmap_lock again. * Returns enum scan_result value. */ static int hugepage_vma_revalidate (struct mm_struct *mm,unsigned long address, -bool expect_anon, -struct vm_area_struct **vmap, -struct collapse_control *cc) +bool expect_anon,struct vm_area_struct **vmap, +struct collapse_control *cc,unsigned int order) {struct vm_area_struct *vma; enum tva_type type = cc->is_khugepaged ? TVA_KHUGEPAGED :
@@ -912,15 +911,16 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,if (!vma)return SCAN_VMA_NULL ; +/* Always check the PMD order to ensure its not shared by another VMA */ if (!thp_vma_suitable_order (vma, address,PMD_ORDER ))return SCAN_ADDRESS_RANGE ; -if (!thp_vma_allowable_order (vma, vma->vm_flags, type,PMD_ORDER )) +if (!thp_vma_allowable_orders (vma, vma->vm_flags, type,BIT (order)))return SCAN_VMA_CHECK ;/* * Anon VMA expected, the address may be unmapped then * remapped to file after khugepaged reaquired the mmap_lock. * -* thp_vma_allowable_order may return true for qualified file +* thp_vma_allowable_orders may return true for qualified file * vmas. */ if (expect_anon && (!(*vmap)->anon_vma || !vma_is_anonymous (*vmap)))
@@ -1117,7 +1117,8 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, goto out_nolock;mmap_read_lock (mm); - result =hugepage_vma_revalidate (mm, address, true, &vma, cc); + result =hugepage_vma_revalidate (mm, address, true, &vma, cc, +HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) {mmap_read_unlock (mm); goto out_nolock;
@@ -1151,7 +1152,8 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,* mmap_lock. */ mmap_write_lock (mm); - result =hugepage_vma_revalidate (mm, address, true, &vma, cc); + result =hugepage_vma_revalidate (mm, address, true, &vma, cc, +HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) goto out_up_write;/* check if the pmd is still valid */
@@ -2814,7 +2816,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,mmap_read_lock (mm); mmap_locked = true; result =hugepage_vma_revalidate (mm, addr, false, &vma, - cc); + cc,HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) { last_fail = result; goto out_nolock;
Signature · 1 line
2.51.1
Zi Yan
reviewerRe: [PATCH v13 mm-new 03/16] khugepaged: generalize hugepage_vma_revalidate for mTHP support
Quoted 15 lines · Nico Pache
For khugepaged to support different mTHP orders, we must generalize this to check if the PMD is not shared by another VMA and that the order is enabled.
No functional change in this patch. Also correct a comment about the functionality of the revalidation.
Reviewed-by Wei Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Baolin Wang <[email protected]>Reviewed-by Lorenzo Stoakes <[email protected]>Co-developed-by Dev Jain <[email protected]>Signed-off-by Dev Jain <[email protected]>Signed-off-by Nico Pache <[email protected]>
Quoted 4 lines
--- mm/khugepaged.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
LGTM. Reviewed-by: Zi Yan <[email protected]>
Best Regards,
Yan, Zi
Re: [PATCH v13 mm-new 03/16] khugepaged: generalize hugepage_vma_revalidate for mTHP support
In reply to Nico PacheOK I'll stop reporting the merge conflicts from the that series but yeah here too I guess it kills a whole bunch :)
Quoted 92 lines · Nico Pache
For khugepaged to support different mTHP orders, we must generalize this to check if the PMD is not shared by another VMA and that the order is enabled.
No functional change in this patch. Also correct a comment about the functionality of the revalidation.
Reviewed-by Wei Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Baolin Wang <[email protected]>Reviewed-by Lorenzo Stoakes <[email protected]>Co-developed-by Dev Jain <[email protected]>Signed-off-by Dev Jain <[email protected]>Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)mm/khugepaged.c +11
-10@@ -892,14 +892,13 @@ static int collapse_find_target_node(struct collapse_control *cc)/* * If mmap_lock temporarily dropped, revalidate vma -* before taking mmap_lock. +* after taking the mmap_lock again. * Returns enum scan_result value. */ static int hugepage_vma_revalidate (struct mm_struct *mm,unsigned long address, -bool expect_anon, -struct vm_area_struct **vmap, -struct collapse_control *cc) +bool expect_anon,struct vm_area_struct **vmap, +struct collapse_control *cc,unsigned int order) {struct vm_area_struct *vma; enum tva_type type = cc->is_khugepaged ? TVA_KHUGEPAGED :@@ -912,15 +911,16 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,if (!vma)return SCAN_VMA_NULL ; +/* Always check the PMD order to ensure its not shared by another VMA */ if (!thp_vma_suitable_order (vma, address,PMD_ORDER ))return SCAN_ADDRESS_RANGE ; -if (!thp_vma_allowable_order (vma, vma->vm_flags, type,PMD_ORDER )) +if (!thp_vma_allowable_orders (vma, vma->vm_flags, type,BIT (order)))return SCAN_VMA_CHECK ;/* * Anon VMA expected, the address may be unmapped then * remapped to file after khugepaged reaquired the mmap_lock. * -* thp_vma_allowable_order may return true for qualified file +* thp_vma_allowable_orders may return true for qualified file * vmas. */ if (expect_anon && (!(*vmap)->anon_vma || !vma_is_anonymous (*vmap)))@@ -1117,7 +1117,8 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, goto out_nolock;mmap_read_lock (mm); - result =hugepage_vma_revalidate (mm, address, true, &vma, cc); + result =hugepage_vma_revalidate (mm, address, true, &vma, cc, +HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) {mmap_read_unlock (mm); goto out_nolock;@@ -1151,7 +1152,8 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,* mmap_lock. */ mmap_write_lock (mm); - result =hugepage_vma_revalidate (mm, address, true, &vma, cc); + result =hugepage_vma_revalidate (mm, address, true, &vma, cc, +HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) goto out_up_write;/* check if the pmd is still valid */ @@ -2814,7 +2816,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,mmap_read_lock (mm); mmap_locked = true; result =hugepage_vma_revalidate (mm, addr, false, &vma, - cc); + cc,HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) { last_fail = result; goto out_nolock; --2.51.1
PATCH v13 4/16 khugepaged: generalize alloc_charge_folio()
In reply to Nico PacheFrom: Dev Jain <[email protected]>
Pass order to alloc_charge_folio() and update mTHP statistics.
--- Documentation/admin-guide/mm/transhuge.rst | 8 ++++++++ include/linux/huge_mm.h | 2 ++ mm/huge_memory.c | 4 ++++ mm/khugepaged.c | 17 +++++++++++------ 4 files changed, 25 insertions(+), 6 deletions(-)
Documentation/admin-guide/mm/transhuge.rst +8 -0
@@ -639,6 +639,14 @@ anon_fault_fallback_charge instead falls back to using huge pages with lower orders or small pages even though the allocation was successful. +collapse_alloc + is incremented every time a huge page is successfully allocated for a + khugepaged collapse. + +collapse_alloc_failed + is incremented every time a huge page allocation fails during a + khugepaged collapse. + zswpout is incremented every time a huge page is swapped out to zswap in one piece without splitting.
include/linux/huge_mm.h +2 -0
@@ -128,6 +128,8 @@ enum mthp_stat_item {MTHP_STAT_ANON_FAULT_ALLOC ,MTHP_STAT_ANON_FAULT_FALLBACK ,MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE , +MTHP_STAT_COLLAPSE_ALLOC , +MTHP_STAT_COLLAPSE_ALLOC_FAILED ,MTHP_STAT_ZSWPOUT ,MTHP_STAT_SWPIN , MTHP_STAT_SWPIN_FALLBACK,
mm/huge_memory.c +4 -0
@@ -621,6 +621,8 @@ static struct kobj_attribute _name##_attr = __ATTR_RO(_name)DEFINE_MTHP_STAT_ATTR (anon_fault_alloc,MTHP_STAT_ANON_FAULT_ALLOC );DEFINE_MTHP_STAT_ATTR (anon_fault_fallback,MTHP_STAT_ANON_FAULT_FALLBACK );DEFINE_MTHP_STAT_ATTR (anon_fault_fallback_charge,MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE ); +DEFINE_MTHP_STAT_ATTR (collapse_alloc,MTHP_STAT_COLLAPSE_ALLOC ); +DEFINE_MTHP_STAT_ATTR (collapse_alloc_failed,MTHP_STAT_COLLAPSE_ALLOC_FAILED );DEFINE_MTHP_STAT_ATTR (zswpout,MTHP_STAT_ZSWPOUT );DEFINE_MTHP_STAT_ATTR (swpin,MTHP_STAT_SWPIN );DEFINE_MTHP_STAT_ATTR (swpin_fallback,MTHP_STAT_SWPIN_FALLBACK );
@@ -686,6 +688,8 @@ static struct attribute *any_stats_attrs[] = {#endif &split_attr.attr, &split_failed_attr.attr, + &collapse_alloc_attr.attr, + &collapse_alloc_failed_attr.attr, NULL, };
mm/khugepaged.c +11 -6
@@ -1061,21 +1061,26 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm, }static int alloc_charge_folio (struct folio **foliop,struct mm_struct *mm, -struct collapse_control *cc) +struct collapse_control *cc,unsigned int order) {gfp_t gfp = (cc->is_khugepaged ?alloc_hugepage_khugepaged_gfpmask () :GFP_TRANSHUGE );int node =collapse_find_target_node (cc);struct folio *folio; - folio =__folio_alloc (gfp,HPAGE_PMD_ORDER , node, &cc->alloc_nmask); + folio =__folio_alloc (gfp, order, node, &cc->alloc_nmask);if (!folio) { *foliop =NULL ; -count_vm_event (THP_COLLAPSE_ALLOC_FAILED ); +if (order ==HPAGE_PMD_ORDER ) +count_vm_event (THP_COLLAPSE_ALLOC_FAILED ); +count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC_FAILED );return SCAN_ALLOC_HUGE_PAGE_FAIL ; } -count_vm_event (THP_COLLAPSE_ALLOC ); +if (order ==HPAGE_PMD_ORDER ) +count_vm_event (THP_COLLAPSE_ALLOC ); +count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC ); +if (unlikely (mem_cgroup_charge (folio, mm, gfp))) {folio_put (folio); *foliop =NULL ;
@@ -1112,7 +1117,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,*/ mmap_read_unlock (mm); - result =alloc_charge_folio (&folio, mm, cc); + result =alloc_charge_folio (&folio, mm, cc,HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) goto out_nolock;
@@ -1872,7 +1877,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,VM_BUG_ON (!IS_ENABLED (CONFIG_READ_ONLY_THP_FOR_FS ) && !is_shmem);VM_BUG_ON (start & (HPAGE_PMD_NR -1 )); - result =alloc_charge_folio (&new_folio, mm, cc); + result =alloc_charge_folio (&new_folio, mm, cc,HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) goto out;
Signature · 1 line
2.51.1
Zi Yan
reviewerRe: [PATCH v13 mm-new 04/16] khugepaged: generalize alloc_charge_folio()
Quoted 12 lines · Nico Pache
From: Dev Jain <[email protected]>
Pass order to
alloc_charge_folio()and update mTHP statistics.Reviewed-by Wei Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Baolin Wang <[email protected]>Reviewed-by Lorenzo Stoakes <[email protected]>Co-developed-by Nico Pache <[email protected]>Signed-off-by Nico Pache <[email protected]>Signed-off-by Dev Jain <[email protected]>
Quoted 7 lines
--- Documentation/admin-guide/mm/transhuge.rst | 8 ++++++++ include/linux/huge_mm.h | 2 ++ mm/huge_memory.c | 4 ++++ mm/khugepaged.c | 17 +++++++++++------ 4 files changed, 25 insertions(+), 6 deletions(-)
LGTM. Reviewed-by: Zi Yan <[email protected]>
Best Regards,
Yan, Zi
PATCH v13 5/16 khugepaged: introduce is_mthp_order helper
In reply to Nico PacheIn order to add mTHP support, we will often be checking if a given order is a mTHP or PMD order. Lets create a simple helper function to keep the code clean and readable.
--- mm/khugepaged.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
mm/khugepaged.c +7 -2
@@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)return pte_present (pte) &&is_zero_pfn (pte_pfn (pte)); } +static bool is_mthp_order (unsigned int order) +{ +return order !=HPAGE_PMD_ORDER ; +} +int hugepage_madvise (struct vm_area_struct *vma,vm_flags_t *vm_flags,int advice) {
@@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, folio =__folio_alloc (gfp, order, node, &cc->alloc_nmask);if (!folio) { *foliop =NULL ; -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC_FAILED );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC_FAILED );return SCAN_ALLOC_HUGE_PAGE_FAIL ; } -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC );
Signature · 1 line
2.51.1
Zi Yan
reviewerRe: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Quoted 15 lines · Nico Pache
In order to add mTHP support, we will often be checking if a given order is a mTHP or PMD order. Lets create a simple helper function to keep the code clean and readable.
Suggested-by Lorenzo Stoakes <[email protected]>Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)mm/khugepaged.c +0
-0
mm/khugepaged.c static bool pte_none_or_zero(pte_t pte) · line 345
Quoted 8 lines
mm/khugepaged.c +5
-0@@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)return pte_present (pte) &&is_zero_pfn (pte_pfn (pte)); } +static bool is_mthp_order (unsigned int order) +{ +return order !=HPAGE_PMD_ORDER ; +} +
The code at the bottom of __folio_split() in mm/huge_memory.c can use this helper.
The code in deferred_split_folio() uses folio_test_pmd_mappable() instead of order == HPAGE_PMD_ORDER, but the code can be changed to use the helper.
Quoted 21 lines
mm/khugepaged.c +2
-2int hugepage_madvise (struct vm_area_struct *vma,vm_flags_t *vm_flags,int advice) {@@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, folio =__folio_alloc (gfp, order, node, &cc->alloc_nmask);if (!folio) { *foliop =NULL ; -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC_FAILED );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC_FAILED );return SCAN_ALLOC_HUGE_PAGE_FAIL ; } -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC );Signature · 1 line
2.51.1
Best Regards,
Yan, Zi
Re: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Quoted 30 lines · Zi Yan
Quoted 15 lines · Nico Pache
In order to add mTHP support, we will often be checking if a given order is a mTHP or PMD order. Lets create a simple helper function to keep the code clean and readable.
Suggested-by Lorenzo Stoakes <[email protected]>Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)mm/khugepaged.c +0
-0mm/khugepaged.c static bool pte_none_or_zero(pte_t pte) · line 345
Quoted 8 lines
mm/khugepaged.c +5
-0@@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)return pte_present (pte) &&is_zero_pfn (pte_pfn (pte)); } +static bool is_mthp_order (unsigned int order) +{ +return order !=HPAGE_PMD_ORDER ; +} +The code at the bottom of
__folio_split()inmm/huge_memory.ccan use this helper.
Quoted 3 lines
The code in
deferred_split_folio()usesfolio_test_pmd_mappable()instead of order ==HPAGE_PMD_ORDER, but the code can be changed to use the helper.
static inline bool folio_test_pmd_mappable(struct folio *folio) {
return folio_order(folio) >= HPAGE_PMD_ORDER;
}
Should we make the is_mthp_order() check all orders that are less than HPAGE_PMD_ORDER.
ie)
+static bool is_mthp_order(unsigned int order)
+{
+ return order < HPAGE_PMD_ORDER;
}
This way it's true to its mTHP nature being smaller than the PMD.
Cheers,
-- Nico
Quoted 27 lines
Quoted 21 lines
mm/khugepaged.c +2
-3int hugepage_madvise (struct vm_area_struct *vma,vm_flags_t *vm_flags,int advice) {@@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, folio =__folio_alloc (gfp, order, node, &cc->alloc_nmask);if (!folio) { *foliop =NULL ; -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC_FAILED );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC_FAILED );return SCAN_ALLOC_HUGE_PAGE_FAIL ; } -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC ); --Quoted 1 line
2.51.1
Best Regards,
Yan, Zi
Zi Yan
reviewerRe: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
Quoted 50 lines · Nico Pache
Quoted 30 lines · Zi Yan
Quoted 15 lines · Nico Pache
In order to add mTHP support, we will often be checking if a given order is a mTHP or PMD order. Lets create a simple helper function to keep the code clean and readable.
Suggested-by Lorenzo Stoakes <[email protected]>Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)mm/khugepaged.c +0
-0mm/khugepaged.c static bool pte_none_or_zero(pte_t pte) · line 345
Quoted 8 lines
mm/khugepaged.c +5
-0@@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)return pte_present (pte) &&is_zero_pfn (pte_pfn (pte)); } +static bool is_mthp_order (unsigned int order) +{ +return order !=HPAGE_PMD_ORDER ; +} +The code at the bottom of
__folio_split()inmm/huge_memory.ccan use this helper.Quoted 3 lines
The code in
deferred_split_folio()usesfolio_test_pmd_mappable()instead of order ==HPAGE_PMD_ORDER, but the code can be changed to use the helper.static inline bool
folio_test_pmd_mappable(struct folio *folio){
returnfolio_order(folio)>=HPAGE_PMD_ORDER;
}Should we make the
is_mthp_order()check all orders that are less thanHPAGE_PMD_ORDER.ie)
mm/khugepaged.c +3
-0+static bool is_mthp_order (unsigned int order) +{ +return order <HPAGE_PMD_ORDER ;}
Quoted 1 line
This way it's true to its mTHP nature being smaller than the PMD.
I am not sure. It really depends on in the future whether we call
Quoted 1 line
9 order (can be mapped by multiple PMDs) large folio mTHP or not.
My current feeling is that THP is a legacy name for PMD THP and any other large folios are mTHP. So your current definition of is_mthp_order() looks right to me. This is just my feeling and I am open to other opinions.
folio_test_pmd_mappable() is different, since as long as a folio's order is greater than HPAGE_PMD_ORDER, it indeed can be mapped by PMDs.
--
Best Regards,
Yan, Zi
Re: [PATCH v13 mm-new 05/16] khugepaged: introduce is_mthp_order helper
In reply to Nico PacheQuoted 6 lines · Nico Pache
In order to add mTHP support, we will often be checking if a given order is a mTHP or PMD order. Lets create a simple helper function to keep the code clean and readable.
Suggested-by Lorenzo Stoakes <[email protected]>Signed-off-by Nico Pache <[email protected]>
Notwithstanding what Zi said re: using elsewhere (please do :), LGTM so:
Quoted 39 lines
--- mm/khugepaged.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)mm/khugepaged.c +7
-3@@ -338,6 +338,11 @@ static bool pte_none_or_zero(pte_t pte)return pte_present (pte) &&is_zero_pfn (pte_pfn (pte)); } +static bool is_mthp_order (unsigned int order) +{ +return order !=HPAGE_PMD_ORDER ; +} +int hugepage_madvise (struct vm_area_struct *vma,vm_flags_t *vm_flags,int advice) {@@ -1071,13 +1076,13 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, folio =__folio_alloc (gfp, order, node, &cc->alloc_nmask);if (!folio) { *foliop =NULL ; -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC_FAILED );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC_FAILED );return SCAN_ALLOC_HUGE_PAGE_FAIL ; } -if (order ==HPAGE_PMD_ORDER ) +if (!is_mthp_order (order))count_vm_event (THP_COLLAPSE_ALLOC );count_mthp_stat (order,MTHP_STAT_COLLAPSE_ALLOC ); --2.51.1
PATCH v13 6/16 khugepaged: generalize __collapse_huge_page_* for mTHP support
In reply to Nico Pachegeneralize the order of the __collapse_huge_page_* functions to support future mTHP collapse.
mTHP collapse will not honor the khugepaged_max_ptes_shared or khugepaged_max_ptes_swap parameters, and will fail if it encounters a shared or swapped entry.
No functional changes in this patch.
--- mm/khugepaged.c | 78 ++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 30 deletions(-)
mm/khugepaged.c +48 -30
@@ -541,25 +541,25 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte, }static int __collapse_huge_page_isolate (struct vm_area_struct *vma, -unsigned long start_addr, -pte_t *pte, -struct collapse_control *cc, -struct list_head *compound_pagelist) +unsigned long start_addr,pte_t *pte,struct collapse_control *cc, +unsigned int order,struct list_head *compound_pagelist) {struct page *page =NULL ;struct folio *folio =NULL ;unsigned long addr = start_addr;pte_t *_pte;int none_or_zero =0 , shared =0 , result =SCAN_FAIL , referenced =0 ; +const unsigned long nr_pages =1UL << order; +int max_ptes_none = khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - order); -for (_pte = pte; _pte < pte +HPAGE_PMD_NR ; +for (_pte = pte; _pte < pte + nr_pages; _pte++, addr +=PAGE_SIZE ) {pte_t pteval =ptep_get (_pte);if (pte_none_or_zero (pteval)) { ++none_or_zero;if (!userfaultfd_armed (vma) && (!cc->is_khugepaged || - none_or_zero <= khugepaged_max_ptes_none)) { + none_or_zero <= max_ptes_none)) {continue ; }else { result =SCAN_EXCEED_NONE_PTE ;
@@ -587,8 +587,14 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,/* See collapse_scan_pmd(). */ if (folio_maybe_mapped_shared (folio)) { ++shared; -if (cc->is_khugepaged && - shared > khugepaged_max_ptes_shared) { +/* +* TODO: Support shared pages without leading to further +* mTHP collapses. Currently bringing in new pages via +* shared may cause a future higher order collapse on a +* rescan of the same range. +*/ +if (is_mthp_order (order) || (cc->is_khugepaged && + shared > khugepaged_max_ptes_shared)) { result =SCAN_EXCEED_SHARED_PTE ;count_vm_event (THP_SCAN_EXCEED_SHARED_PTE ); goto out;
@@ -681,18 +687,18 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, }static void __collapse_huge_page_copy_succeeded (pte_t *pte, -struct vm_area_struct *vma, -unsigned long address, -spinlock_t *ptl, -struct list_head *compound_pagelist) +struct vm_area_struct *vma,unsigned long address, +spinlock_t *ptl,unsigned int order, +struct list_head *compound_pagelist) { -unsigned long end = address +HPAGE_PMD_SIZE ; +unsigned long end = address + (PAGE_SIZE << order);struct folio *src, *tmp;pte_t pteval;pte_t *_pte;unsigned int nr_ptes; +const unsigned long nr_pages =1UL << order; -for (_pte = pte; _pte < pte +HPAGE_PMD_NR ; _pte += nr_ptes, +for (_pte = pte; _pte < pte + nr_pages; _pte += nr_ptes, address += nr_ptes *PAGE_SIZE ) { nr_ptes =1 ; pteval =ptep_get (_pte);
@@ -745,13 +751,11 @@ static void __collapse_huge_page_copy_succeeded(pte_t *pte, }static void __collapse_huge_page_copy_failed (pte_t *pte, -pmd_t *pmd, -pmd_t orig_pmd, -struct vm_area_struct *vma, -struct list_head *compound_pagelist) +pmd_t *pmd,pmd_t orig_pmd,struct vm_area_struct *vma, +unsigned int order,struct list_head *compound_pagelist) {spinlock_t *pmd_ptl; - +const unsigned long nr_pages =1UL << order;/* * Re-establish the PMD to point to the original page table * entry. Restoring PMD needs to be done prior to releasing
@@ -765,7 +769,7 @@ static void __collapse_huge_page_copy_failed(pte_t *pte,* Release both raw and compound pages isolated * in __collapse_huge_page_isolate. */ -release_pte_pages (pte, pte +HPAGE_PMD_NR , compound_pagelist); +release_pte_pages (pte, pte + nr_pages, compound_pagelist); }/*
@@ -785,16 +789,16 @@ static void __collapse_huge_page_copy_failed(pte_t *pte,*/ static int __collapse_huge_page_copy (pte_t *pte,struct folio *folio,pmd_t *pmd,pmd_t orig_pmd,struct vm_area_struct *vma, -unsigned long address,spinlock_t *ptl, +unsigned long address,spinlock_t *ptl,unsigned int order,struct list_head *compound_pagelist) {unsigned int i;int result =SCAN_SUCCEED ; - +const unsigned long nr_pages =1UL << order;/* * Copying pages' contents is subject to memory poison at any iteration. */ -for (i =0 ; i <HPAGE_PMD_NR ; i++) { +for (i =0 ; i < nr_pages; i++) {pte_t pteval =ptep_get (pte + i);struct page *page =folio_page (folio, i);unsigned long src_addr = address + i *PAGE_SIZE ;
@@ -813,10 +817,10 @@ static int __collapse_huge_page_copy(pte_t *pte, struct folio *folio,if (likely (result ==SCAN_SUCCEED ))__collapse_huge_page_copy_succeeded (pte, vma, address, ptl, - compound_pagelist); + order, compound_pagelist);else __collapse_huge_page_copy_failed (pte, pmd, orig_pmd, vma, - compound_pagelist); + order, compound_pagelist);return result; }
@@ -989,13 +993,12 @@ static int check_pmd_still_valid(struct mm_struct *mm,* Returns result: if not SCAN_SUCCEED, mmap_lock has been released. */ static int __collapse_huge_page_swapin (struct mm_struct *mm, -struct vm_area_struct *vma, -unsigned long start_addr,pmd_t *pmd, -int referenced) +struct vm_area_struct *vma,unsigned long start_addr, +pmd_t *pmd,int referenced,unsigned int order) {int swapped_in =0 ;vm_fault_t ret =0 ; -unsigned long addr, end = start_addr + (HPAGE_PMD_NR *PAGE_SIZE ); +unsigned long addr, end = start_addr + (PAGE_SIZE << order);int result;pte_t *pte =NULL ;spinlock_t *ptl;
@@ -1027,6 +1030,19 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm, pte_present(vmf.orig_pte))continue ; +/* +* TODO: Support swapin without leading to further mTHP +* collapses. Currently bringing in new pages via swapin may +* cause a future higher order collapse on a rescan of the same +* range. +*/ +if (is_mthp_order (order)) { +pte_unmap (pte); +mmap_read_unlock (mm); + result =SCAN_EXCEED_SWAP_PTE ; + goto out; + } + vmf.pte = pte; vmf.ptl = ptl; ret =do_swap_page (&vmf);
@@ -1147,7 +1163,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,* that case. Continuing to collapse causes inconsistency. */ result =__collapse_huge_page_swapin (mm, vma, address, pmd, - referenced); + referenced,HPAGE_PMD_ORDER );if (result !=SCAN_SUCCEED ) goto out_nolock; }
@@ -1195,6 +1211,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, pte =pte_offset_map_lock (mm, &_pmd, address, &pte_ptl);if (pte) { result =__collapse_huge_page_isolate (vma, address, pte, cc, +HPAGE_PMD_ORDER , &compound_pagelist);spin_unlock (pte_ptl); }else {
@@ -1225,6 +1242,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, result =__collapse_huge_page_copy (pte, folio, pmd, _pmd, vma, address, pte_ptl, +HPAGE_PMD_ORDER , &compound_pagelist);pte_unmap (pte);if (unlikely (result !=SCAN_SUCCEED ))
Signature · 1 line
2.51.1
PATCH v13 7/16 khugepaged: introduce collapse_max_ptes_none helper function
In reply to Nico PacheThe current mechanism for determining mTHP collapse scales the khugepaged_max_ptes_none value based on the target order. This introduces an undesirable feedback loop, or "creep", when max_ptes_none is set to a value greater than HPAGE_PMD_NR / 2.
With this configuration, a successful collapse to order N will populate enough pages to satisfy the collapse condition on order N+1 on the next scan. This leads to unnecessary work and memory churn.
To fix this issue introduce a helper function that will limit mTHP collapse support to two max_ptes_none values, 0 and HPAGE_PMD_NR - 1. This effectively supports two modes:
- max_ptes_none=0: never introduce new none-pages for mTHP collapse.
- max_ptes_none=511 (on 4k pagesz): Always collapse to the highest available mTHP order.
This removes the possiblilty of "creep", while not modifying any uAPI expectations. A warning will be emitted if any non-supported max_ptes_none value is configured with mTHP enabled.
The limits can be ignored by passing full_scan=true, this is useful for madvise_collapse (which ignores limits), or in the case of collapse_scan_pmd(), allows the full PMD to be scanned when mTHP collapse is available.
--- mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
mm/khugepaged.c +42 -1
@@ -463,6 +463,44 @@ void __khugepaged_enter(struct mm_struct *mm)wake_up_interruptible (&khugepaged_wait); } +/** +* collapse_max_ptes_none - Calculate maximum allowed empty PTEs for collapse +* @order: The folio order being collapsed to +* @full_scan: Whether this is a full scan (ignore limits) +* +* For madvise-triggered collapses (full_scan=true), all limits are bypassed +* and allow up to HPAGE_PMD_NR - 1 empty PTEs. +* +* For PMD-sized collapses (order == HPAGE_PMD_ORDER), use the configured +* khugepaged_max_ptes_none value. +* +* For mTHP collapses, we currently only support khugepaged_max_pte_none values +* of 0 or (HPAGE_PMD_NR - 1). Any other value will emit a warning and no mTHP +* collapse will be attempted +* +* Return: Maximum number of empty PTEs allowed for the collapse operation +*/ +static unsigned int collapse_max_ptes_none (unsigned int order,bool full_scan) +{ +/* ignore max_ptes_none limits */ +if (full_scan) +return HPAGE_PMD_NR -1 ; + +if (!is_mthp_order (order)) +return khugepaged_max_ptes_none; + +/* Zero/non-present collapse disabled. */ +if (!khugepaged_max_ptes_none) +return 0 ; + +if (khugepaged_max_ptes_none ==HPAGE_PMD_NR -1 ) +return (1 << order) -1 ; + +pr_warn_once ("mTHP collapse only supports max_ptes_none values of 0 or %d\n" , +HPAGE_PMD_NR -1 ); +return -EINVAL ; +} +void khugepaged_enter_vma (struct vm_area_struct *vma,vm_flags_t vm_flags) {
@@ -550,7 +588,10 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,pte_t *_pte;int none_or_zero =0 , shared =0 , result =SCAN_FAIL , referenced =0 ;const unsigned long nr_pages =1UL << order; -int max_ptes_none = khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - order); +int max_ptes_none =collapse_max_ptes_none (order, !cc->is_khugepaged); + +if (max_ptes_none == -EINVAL ) + goto out;for (_pte = pte; _pte < pte + nr_pages; _pte++, addr +=PAGE_SIZE ) {
Signature · 1 line
2.51.1
Re: [PATCH v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function
Quoted 66 lines · Nico Pache
The current mechanism for determining mTHP collapse scales the
khugepaged_max_ptes_nonevalue based on the target order. This introduces an undesirable feedback loop, or "creep", whenmax_ptes_noneis set to a value greater thanHPAGE_PMD_NR/ 2.With this configuration, a successful collapse to order N will populate enough pages to satisfy the collapse condition on order N+1 on the next scan. This leads to unnecessary work and memory churn.
To fix this issue introduce a helper function that will limit mTHP collapse support to two
max_ptes_nonevalues, 0 andHPAGE_PMD_NR- 1. This effectively supports two modes:- max_ptes_none=0: never introduce new none-pages for mTHP collapse.
- max_ptes_none=511 (on 4k pagesz): Always collapse to the highest available mTHP order.This removes the possiblilty of "creep", while not modifying any uAPI expectations. A warning will be emitted if any non-supported
max_ptes_nonevalue is configured with mTHP enabled.The limits can be ignored by passing full_scan=true, this is useful for
madvise_collapse(which ignores limits), or in the case ofcollapse_scan_pmd(), allows the full PMD to be scanned when mTHP collapse is available.Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)mm/khugepaged.c +27
-0@@ -463,6 +463,44 @@ void __khugepaged_enter(struct mm_struct *mm)wake_up_interruptible (&khugepaged_wait); } +/** +* collapse_max_ptes_none - Calculate maximum allowed empty PTEs for collapse +* @order: The folio order being collapsed to +* @full_scan: Whether this is a full scan (ignore limits) +* +* For madvise-triggered collapses (full_scan=true), all limits are bypassed +* and allow up to HPAGE_PMD_NR - 1 empty PTEs. +* +* For PMD-sized collapses (order == HPAGE_PMD_ORDER), use the configured +* khugepaged_max_ptes_none value. +* +* For mTHP collapses, we currently only support khugepaged_max_pte_none values +* of 0 or (HPAGE_PMD_NR - 1). Any other value will emit a warning and no mTHP +* collapse will be attempted +* +* Return: Maximum number of empty PTEs allowed for the collapse operation +*/ +static unsigned int collapse_max_ptes_none (unsigned int order,bool full_scan) +{ +/* ignore max_ptes_none limits */ +if (full_scan) +return HPAGE_PMD_NR -1 ; + +if (!is_mthp_order (order)) +return khugepaged_max_ptes_none; + +/* Zero/non-present collapse disabled. */
mm/khugepaged.c void __khugepaged_enter(struct mm_struct *mm) · line 502
Quoted 10 lines
mm/khugepaged.c +10
-0+if (!khugepaged_max_ptes_none) +return 0 ; + +if (khugepaged_max_ptes_none ==HPAGE_PMD_NR -1 ) +return (1 << order) -1 ; + +pr_warn_once ("mTHP collapse only supports max_ptes_none values of 0 or %d\n" , +HPAGE_PMD_NR -1 ); +return -EINVAL ; +}
Thanks. That aligns with what we talked about previously. So
kernel test robot
build botRe: [PATCH v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function
In reply to Nico Pachekernel test robot noticed the following build warnings:
Hi Nico,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on next-20251203]
[cannot apply to linus/master v6.18]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Nico-Pache/khugepaged-rename-hpage_collapse_-to-collapse_/20251202-015150 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20251201174627.23295-8-npache%40redhat.com patch subject: [PATCH v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20251203/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251203/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> mm/khugepaged.c:593:6: warning: variable '_pte' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
593 | if (max_ptes_none == -EINVAL)
| ^~~~~~~~~~~~~~~~~~~~~~~~
mm/khugepaged.c:724:25: note: uninitialized use occurs here
724 | release_pte_pages(pte, _pte, compound_pagelist);
| ^~~~
mm/khugepaged.c:593:2: note: remove the 'if' if its condition is always false
593 | if (max_ptes_none == -EINVAL)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
594 | goto out;
| ~~~~~~~~
mm/khugepaged.c:588:13: note: initialize the variable '_pte' to silence this warning
588 | pte_t *_pte;
| ^
| = NULL1 warning generated.
vim +593 mm/khugepaged.c
580
581 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
582 unsigned long start_addr, pte_t *pte, struct collapse_control *cc,
583 unsigned int order, struct list_head *compound_pagelist)
584 {
585 struct page *page = NULL;
586 struct folio *folio = NULL;
587 unsigned long addr = start_addr;
588 pte_t *_pte;
589 int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
590 const unsigned long nr_pages = 1UL << order;
591 int max_ptes_none = collapse_max_ptes_none(order, !cc->is_khugepaged);
592
> 593 if (max_ptes_none == -EINVAL)
594 goto out;
595
596 for (_pte = pte; _pte < pte + nr_pages;
597 _pte++, addr += PAGE_SIZE) {
598 pte_t pteval = ptep_get(_pte);
599 if (pte_none_or_zero(pteval)) {
600 ++none_or_zero;
601 if (!userfaultfd_armed(vma) &&
602 (!cc->is_khugepaged ||
603 none_or_zero <= max_ptes_none)) {
604 continue;
605 } else {
606 result = SCAN_EXCEED_NONE_PTE;
607 count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
608 goto out;
609 }
610 }
611 if (!pte_present(pteval)) {
612 result = SCAN_PTE_NON_PRESENT;
613 goto out;
614 }
615 if (pte_uffd_wp(pteval)) {
616 result = SCAN_PTE_UFFD_WP;
617 goto out;
618 }
619 page = vm_normal_page(vma, addr, pteval);
620 if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
621 result = SCAN_PAGE_NULL;
622 goto out;
623 }
624
625 folio = page_folio(page);
626 VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
627
628 /* See collapse_scan_pmd(). */
629 if (folio_maybe_mapped_shared(folio)) {
630 ++shared;
631 /*
632 * TODO: Support shared pages without leading to further
633 * mTHP collapses. Currently bringing in new pages via
634 * shared may cause a future higher order collapse on a
635 * rescan of the same range.
636 */
637 if (is_mthp_order(order) || (cc->is_khugepaged &&
638 shared > khugepaged_max_ptes_shared)) {
639 result = SCAN_EXCEED_SHARED_PTE;
640 count_vm_event(THP_SCAN_EXCEED_SHARED_PTE);
641 goto out;
642 }
643 }
644
645 if (folio_test_large(folio)) {
646 struct folio *f;
647
648 /*
649 * Check if we have dealt with the compound page
650 * already
651 */
652 list_for_each_entry(f, compound_pagelist, lru) {
653 if (folio == f)
654 goto next;
655 }
656 }
657
658 /*
659 * We can do it before folio_isolate_lru because the
660 * folio can't be freed from under us. NOTE: PG_lock
661 * is needed to serialize against split_huge_page
662 * when invoked from the VM.
663 */
664 if (!folio_trylock(folio)) {
665 result = SCAN_PAGE_LOCK;
666 goto out;
667 }
668
669 /*
670 * Check if the page has any GUP (or other external) pins.
671 *
672 * The page table that maps the page has been already unlinked
673 * from the page table tree and this process cannot get
674 * an additional pin on the page.
675 *
676 * New pins can come later if the page is shared across fork,
677 * but not from this process. The other process cannot write to
678 * the page, only trigger CoW.
679 */
680 if (folio_expected_ref_count(folio) != folio_ref_count(folio)) {
681 folio_unlock(folio);
682 result = SCAN_PAGE_COUNT;
683 goto out;
684 }
685
686 /*
687 * Isolate the page to avoid collapsing an hugepage
688 * currently in use by the VM.
689 */
690 if (!folio_isolate_lru(folio)) {
691 folio_unlock(folio);
692 result = SCAN_DEL_PAGE_LRU;
693 goto out;
694 }
695 node_stat_mod_folio(folio,
696 NR_ISOLATED_ANON + folio_is_file_lru(folio),
697 folio_nr_pages(folio));
698 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
699 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
700
701 if (folio_test_large(folio))
702 list_add_tail(&folio->lru, compound_pagelist);
703 next:
704 /*
705 * If collapse was initiated by khugepaged, check that there is
706 * enough young pte to justify collapsing the page
707 */
708 if (cc->is_khugepaged &&
709 (pte_young(pteval) || folio_test_young(folio) ||
710 folio_test_referenced(folio) ||
711 mmu_notifier_test_young(vma->vm_mm, addr)))
712 referenced++;
713 }
714
715 if (unlikely(cc->is_khugepaged && !referenced)) {
716 result = SCAN_LACK_REFERENCED_PAGE;
717 } else {
718 result = SCAN_SUCCEED;
719 trace_mm_collapse_huge_page_isolate(folio, none_or_zero,
720 referenced, result);
721 return result;
722 }
723 out:
724 release_pte_pages(pte, _pte, compound_pagelist);
725 trace_mm_collapse_huge_page_isolate(folio, none_or_zero,
726 referenced, result);
727 return result;
728 }
729
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function
In reply to Nico PacheHi Andrew,
The bot has reported a potential uninitialized use of a variable.
Can you please squash the following fixup to this commit.
Thank you,
Nico
----8<----
From 846f79d91a25ebad76cbab3690ae315cfe3cf278 Mon Sep 17 00:00:00 2001
From: Nico Pache <[email protected]>
Date: Wed, 3 Dec 2025 13:42:18 -0700
Subject: [PATCH] khugepaged: fixup unintialized _pte variable
There is a potential use of an uninitialized variable after khugepaged: introduce collapse_max_ptes_none helper function
Andrew can you please append this to Patch 7 of my series
as reported by the kernel test robot
Quoted 1 line
Quoted 1 line
mm/khugepaged.c:593:6: warning: variable '_pte' is used uninitialized
whenever 'if' condition is true [-Wsometimes-uninitialized]
593 | if (max_ptes_none == -EINVAL)
| ^~~~~~~~~~~~~~~~~~~~~~~~mm/khugepaged.c:724:25: note: uninitialized use occurs here
724 | release_pte_pages(pte, _pte, compound_pagelist);
| ^~~~mm/khugepaged.c:593:2: note: remove the 'if' if its condition is always false 593 | if (max_ptes_none == -EINVAL)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
594 | goto out;
| ~~~~~~~~mm/khugepaged.c:588:13: note: initialize the variable '_pte' to silence this warning 588 | pte_t *_pte;
| ^
| = NULL
1 warning generated.
---mm/khugepaged.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
mm/khugepaged.c +0 -0
@@ -585,7 +585,7 @@ static int __collapse_huge_page_isolate(struct
vm_area_struct *vma,
struct page *page = NULL;
struct folio *folio = NULL;
unsigned long addr = start_addr;
- pte_t *_pte;
+ pte_t *_pte = pte;
int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0; const unsigned long nr_pages = 1UL << order;
int max_ptes_none = collapse_max_ptes_none(order, !cc->is_khugepaged); @@ -593,8 +593,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
if (max_ptes_none == -EINVAL)
goto out;
- for (_pte = pte; _pte < pte + nr_pages;
- _pte++, addr += PAGE_SIZE) {
+ for (; _pte < pte + nr_pages; _pte++, addr += PAGE_SIZE) { pte_t pteval = ptep_get(_pte);
if (pte_none_or_zero(pteval)) {
++none_or_zero;
--
2.52.0
Quoted 92 lines · Nico Pache
The current mechanism for determining mTHP collapse scales the
khugepaged_max_ptes_nonevalue based on the target order. This introduces an undesirable feedback loop, or "creep", whenmax_ptes_noneis set to a value greater thanHPAGE_PMD_NR/ 2.With this configuration, a successful collapse to order N will populate enough pages to satisfy the collapse condition on order N+1 on the next scan. This leads to unnecessary work and memory churn.
To fix this issue introduce a helper function that will limit mTHP collapse support to two
max_ptes_nonevalues, 0 andHPAGE_PMD_NR- 1. This effectively supports two modes:- max_ptes_none=0: never introduce new none-pages for mTHP collapse.
- max_ptes_none=511 (on 4k pagesz): Always collapse to the highest available mTHP order.This removes the possiblilty of "creep", while not modifying any uAPI expectations. A warning will be emitted if any non-supported
max_ptes_nonevalue is configured with mTHP enabled.The limits can be ignored by passing full_scan=true, this is useful for
madvise_collapse(which ignores limits), or in the case ofcollapse_scan_pmd(), allows the full PMD to be scanned when mTHP collapse is available.Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)mm/khugepaged.c +42
-1@@ -463,6 +463,44 @@ void __khugepaged_enter(struct mm_struct *mm)wake_up_interruptible (&khugepaged_wait); } +/** +* collapse_max_ptes_none - Calculate maximum allowed empty PTEs for collapse +* @order: The folio order being collapsed to +* @full_scan: Whether this is a full scan (ignore limits) +* +* For madvise-triggered collapses (full_scan=true), all limits are bypassed +* and allow up to HPAGE_PMD_NR - 1 empty PTEs. +* +* For PMD-sized collapses (order == HPAGE_PMD_ORDER), use the configured +* khugepaged_max_ptes_none value. +* +* For mTHP collapses, we currently only support khugepaged_max_pte_none values +* of 0 or (HPAGE_PMD_NR - 1). Any other value will emit a warning and no mTHP +* collapse will be attempted +* +* Return: Maximum number of empty PTEs allowed for the collapse operation +*/ +static unsigned int collapse_max_ptes_none (unsigned int order,bool full_scan) +{ +/* ignore max_ptes_none limits */ +if (full_scan) +return HPAGE_PMD_NR -1 ; + +if (!is_mthp_order (order)) +return khugepaged_max_ptes_none; + +/* Zero/non-present collapse disabled. */ +if (!khugepaged_max_ptes_none) +return 0 ; + +if (khugepaged_max_ptes_none ==HPAGE_PMD_NR -1 ) +return (1 << order) -1 ; + +pr_warn_once ("mTHP collapse only supports max_ptes_none values of 0 or %d\n" , +HPAGE_PMD_NR -1 ); +return -EINVAL ; +} +void khugepaged_enter_vma (struct vm_area_struct *vma,vm_flags_t vm_flags) {@@ -550,7 +588,10 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,pte_t *_pte;int none_or_zero =0 , shared =0 , result =SCAN_FAIL , referenced =0 ;const unsigned long nr_pages =1UL << order; -int max_ptes_none = khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - order); +int max_ptes_none =collapse_max_ptes_none (order, !cc->is_khugepaged); + +if (max_ptes_none == -EINVAL ) + goto out;for (_pte = pte; _pte < pte + nr_pages; _pte++, addr +=PAGE_SIZE ) {
Baolin Wang
reviewerRe: [PATCH v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function
In reply to Nico PacheHi Nico,
Quoted 81 lines · Nico Pache
The current mechanism for determining mTHP collapse scales the
khugepaged_max_ptes_nonevalue based on the target order. This introduces an undesirable feedback loop, or "creep", whenmax_ptes_noneis set to a value greater thanHPAGE_PMD_NR/ 2.With this configuration, a successful collapse to order N will populate enough pages to satisfy the collapse condition on order N+1 on the next scan. This leads to unnecessary work and memory churn.
To fix this issue introduce a helper function that will limit mTHP collapse support to two
max_ptes_nonevalues, 0 andHPAGE_PMD_NR- 1. This effectively supports two modes:- max_ptes_none=0: never introduce new none-pages for mTHP collapse.
- max_ptes_none=511 (on 4k pagesz): Always collapse to the highest available mTHP order.This removes the possiblilty of "creep", while not modifying any uAPI expectations. A warning will be emitted if any non-supported
max_ptes_nonevalue is configured with mTHP enabled.The limits can be ignored by passing full_scan=true, this is useful for
madvise_collapse(which ignores limits), or in the case ofcollapse_scan_pmd(), allows the full PMD to be scanned when mTHP collapse is available.Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)mm/khugepaged.c +38
-0@@ -463,6 +463,44 @@ void __khugepaged_enter(struct mm_struct *mm)wake_up_interruptible (&khugepaged_wait); } +/** +* collapse_max_ptes_none - Calculate maximum allowed empty PTEs for collapse +* @order: The folio order being collapsed to +* @full_scan: Whether this is a full scan (ignore limits) +* +* For madvise-triggered collapses (full_scan=true), all limits are bypassed +* and allow up to HPAGE_PMD_NR - 1 empty PTEs. +* +* For PMD-sized collapses (order == HPAGE_PMD_ORDER), use the configured +* khugepaged_max_ptes_none value. +* +* For mTHP collapses, we currently only support khugepaged_max_pte_none values +* of 0 or (HPAGE_PMD_NR - 1). Any other value will emit a warning and no mTHP +* collapse will be attempted +* +* Return: Maximum number of empty PTEs allowed for the collapse operation +*/ +static unsigned int collapse_max_ptes_none (unsigned int order,bool full_scan) +{ +/* ignore max_ptes_none limits */ +if (full_scan) +return HPAGE_PMD_NR -1 ; + +if (!is_mthp_order (order)) +return khugepaged_max_ptes_none; + +/* Zero/non-present collapse disabled. */ +if (!khugepaged_max_ptes_none) +return 0 ; + +if (khugepaged_max_ptes_none ==HPAGE_PMD_NR -1 ) +return (1 << order) -1 ; + +pr_warn_once ("mTHP collapse only supports max_ptes_none values of 0 or %d\n" , +HPAGE_PMD_NR -1 ); +return -EINVAL ; +} +void khugepaged_enter_vma (struct vm_area_struct *vma,vm_flags_t vm_flags) {
mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 594
Quoted 8 lines
mm/khugepaged.c +4
-1@@ -550,7 +588,10 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,pte_t *_pte;int none_or_zero =0 , shared =0 , result =SCAN_FAIL , referenced =0 ;const unsigned long nr_pages =1UL << order; -int max_ptes_none = khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - order); +int max_ptes_none =collapse_max_ptes_none (order, !cc->is_khugepaged); + +if (max_ptes_none == -EINVAL ) + goto out;
After testing your patchset, I hit the following crash. The reason is that when 'max_ptes_none' is -EINVAL here, it shouldn't goto out to call release_pte_pages(), because the '_pte' hasn't been initialized at this point, and there's no need to release folios either.
After applying the fix below, the crash issue is resolved. I'm not sure whether Andrew will help fix this or if you will send a new version to address this issue.
mm/khugepaged.c +0 -0
@@ -646,7 +646,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, int max_ptes_none = collapse_max_ptes_none(order,
!cc->is_khugepaged);
if (max_ptes_none == -EINVAL)
- goto out;
+ return result;
for (_pte = pte; _pte < pte + nr_pages;
_pte++, addr += PAGE_SIZE) {"
[ 565.319345] Unable to handle kernel paging request at virtual address fffffffffffffffa ....... [ 565.319409] swapper pgtable: 4k pages, 48-bit VAs, pgdp=0000001f8549a000 [ 565.319416] [fffffffffffffffa] pgd=0000001f85f2a403, p4d=0000001f85f2a403, pud=0000001f85f2b403, pmd=0000000000000000 [ 565.319427] Internal error: Oops: 0000000096000006 [#1] SMP ....... [ 565.326733] pc : release_pte_pages+0x68/0x178 [ 565.326960] lr : __collapse_huge_page_isolate+0xc0/0x748 [ 565.327232] sp : ffff800083593910 ....... [ 565.331476] Call trace: [ 565.331664] release_pte_pages+0x68/0x178 (P) [ 565.331940] __collapse_huge_page_isolate+0xc0/0x748 [ 565.332249] collapse_huge_page+0x4cc/0xa70 [ 565.332510] mthp_collapse+0x254/0x2a8 [ 565.332754] collapse_scan_pmd+0x5a0/0x6d8 [ 565.333010] collapse_single_pmd+0x214/0x288 [ 565.333275] collapse_scan_mm_slot.constprop.0+0x2ac/0x460 [ 565.333617] khugepaged+0x204/0x2c8 [ 565.333992] kthread+0xf8/0x110 [ 565.334368] ret_from_fork+0x10/0x20
"
Quoted 3 lines
mm/khugepaged.c +0
-0for (_pte = pte; _pte < pte + nr_pages; _pte++, addr +=PAGE_SIZE ) {
Re: [PATCH v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function
Quoted 2 lines · Baolin Wang
Hi Nico,
Hi Baolin! Thanks for testing :)
Did you happen to test with the changes I asked Andrew to append to this commit?
Either way, I think your fixup makes more sense than mine.
Cheers,
-- Nico
Quoted 147 lines · Nico Pache
Quoted 81 lines
The current mechanism for determining mTHP collapse scales the
khugepaged_max_ptes_nonevalue based on the target order. This introduces an undesirable feedback loop, or "creep", whenmax_ptes_noneis set to a value greater thanHPAGE_PMD_NR/ 2.With this configuration, a successful collapse to order N will populate enough pages to satisfy the collapse condition on order N+1 on the next scan. This leads to unnecessary work and memory churn.
To fix this issue introduce a helper function that will limit mTHP collapse support to two
max_ptes_nonevalues, 0 andHPAGE_PMD_NR- 1. This effectively supports two modes:- max_ptes_none=0: never introduce new none-pages for mTHP collapse.
- max_ptes_none=511 (on 4k pagesz): Always collapse to the highest available mTHP order.This removes the possiblilty of "creep", while not modifying any uAPI expectations. A warning will be emitted if any non-supported
max_ptes_nonevalue is configured with mTHP enabled.The limits can be ignored by passing full_scan=true, this is useful for
madvise_collapse(which ignores limits), or in the case ofcollapse_scan_pmd(), allows the full PMD to be scanned when mTHP collapse is available.Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)mm/khugepaged.c +38
-0@@ -463,6 +463,44 @@ void __khugepaged_enter(struct mm_struct *mm)wake_up_interruptible (&khugepaged_wait); } +/** +* collapse_max_ptes_none - Calculate maximum allowed empty PTEs for collapse +* @order: The folio order being collapsed to +* @full_scan: Whether this is a full scan (ignore limits) +* +* For madvise-triggered collapses (full_scan=true), all limits are bypassed +* and allow up to HPAGE_PMD_NR - 1 empty PTEs. +* +* For PMD-sized collapses (order == HPAGE_PMD_ORDER), use the configured +* khugepaged_max_ptes_none value. +* +* For mTHP collapses, we currently only support khugepaged_max_pte_none values +* of 0 or (HPAGE_PMD_NR - 1). Any other value will emit a warning and no mTHP +* collapse will be attempted +* +* Return: Maximum number of empty PTEs allowed for the collapse operation +*/ +static unsigned int collapse_max_ptes_none (unsigned int order,bool full_scan) +{ +/* ignore max_ptes_none limits */ +if (full_scan) +return HPAGE_PMD_NR -1 ; + +if (!is_mthp_order (order)) +return khugepaged_max_ptes_none; + +/* Zero/non-present collapse disabled. */ +if (!khugepaged_max_ptes_none) +return 0 ; + +if (khugepaged_max_ptes_none ==HPAGE_PMD_NR -1 ) +return (1 << order) -1 ; + +pr_warn_once ("mTHP collapse only supports max_ptes_none values of 0 or %d\n" , +HPAGE_PMD_NR -1 ); +return -EINVAL ; +} +void khugepaged_enter_vma (struct vm_area_struct *vma,vm_flags_t vm_flags) {mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 594
Quoted 8 lines
mm/khugepaged.c +4
-1@@ -550,7 +588,10 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,pte_t *_pte;int none_or_zero =0 , shared =0 , result =SCAN_FAIL , referenced =0 ;const unsigned long nr_pages =1UL << order; -int max_ptes_none = khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - order); +int max_ptes_none =collapse_max_ptes_none (order, !cc->is_khugepaged); + +if (max_ptes_none == -EINVAL ) + goto out;After testing your patchset, I hit the following crash. The reason is that when '
max_ptes_none' is-EINVALhere, it shouldn't goto out to callrelease_pte_pages(), because the '_pte' hasn't been initialized at this point, and there's no need to release folios either.After applying the fix below, the crash issue is resolved. I'm not sure whether Andrew will help fix this or if you will send a new version to address this issue.
mm/khugepaged.c +1
-1@@ -646,7 +646,7 @@ static int __collapse_huge_page_isolate(structvm_area_struct *vma, int max_ptes_none = collapse_max_ptes_none(order, !cc->is_khugepaged);if (max_ptes_none == -EINVAL ) - goto out; +return result;for (_pte = pte; _pte < pte + nr_pages; _pte++, addr += PAGE_SIZE) {"
[ 565.319345] Unable to handle kernel paging request at virtual address fffffffffffffffa ....... [ 565.319409] swapper pgtable: 4k pages, 48-bit VAs, pgdp=0000001f8549a000 [ 565.319416] [fffffffffffffffa] pgd=0000001f85f2a403, p4d=0000001f85f2a403, pud=0000001f85f2b403, pmd=0000000000000000 [ 565.319427] Internal error: Oops: 0000000096000006 [#1] SMP ....... [ 565.326733] pc : release_pte_pages+0x68/0x178 [ 565.326960] lr : __collapse_huge_page_isolate+0xc0/0x748 [ 565.327232] sp : ffff800083593910 ....... [ 565.331476] Call trace: [ 565.331664] release_pte_pages+0x68/0x178 (P) [ 565.331940] __collapse_huge_page_isolate+0xc0/0x748 [ 565.332249] collapse_huge_page+0x4cc/0xa70 [ 565.332510] mthp_collapse+0x254/0x2a8 [ 565.332754] collapse_scan_pmd+0x5a0/0x6d8 [ 565.333010] collapse_single_pmd+0x214/0x288 [ 565.333275] collapse_scan_mm_slot.constprop.0+0x2ac/0x460 [ 565.333617] khugepaged+0x204/0x2c8 [ 565.333992] kthread+0xf8/0x110 [ 565.334368] ret_from_fork+0x10/0x20"
Quoted 3 lines
mm/khugepaged.c +0
-0for (_pte = pte; _pte < pte + nr_pages; _pte++, addr +=PAGE_SIZE ) {
Baolin Wang
reviewerRe: [PATCH v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function
Quoted 11 lines · Nico Pache
Quoted 2 lines · Baolin Wang
Hi Nico,
Hi Baolin! Thanks for testing :)
Did you happen to test with the changes I asked Andrew to append to this commit?
Either way, I think your fixup makes more sense than mine.
Ah, I did not notice your fixup commit earlier, which seems to address this problem. And as I said, we don't need to call release_pte_pages() in this case.
Quoted 146 lines
Quoted 146 lines · Nico Pache
Quoted 81 lines
The current mechanism for determining mTHP collapse scales the
khugepaged_max_ptes_nonevalue based on the target order. This introduces an undesirable feedback loop, or "creep", whenmax_ptes_noneis set to a value greater thanHPAGE_PMD_NR/ 2.With this configuration, a successful collapse to order N will populate enough pages to satisfy the collapse condition on order N+1 on the next scan. This leads to unnecessary work and memory churn.
To fix this issue introduce a helper function that will limit mTHP collapse support to two
max_ptes_nonevalues, 0 andHPAGE_PMD_NR- 1. This effectively supports two modes:- max_ptes_none=0: never introduce new none-pages for mTHP collapse.
- max_ptes_none=511 (on 4k pagesz): Always collapse to the highest available mTHP order.This removes the possiblilty of "creep", while not modifying any uAPI expectations. A warning will be emitted if any non-supported
max_ptes_nonevalue is configured with mTHP enabled.The limits can be ignored by passing full_scan=true, this is useful for
madvise_collapse(which ignores limits), or in the case ofcollapse_scan_pmd(), allows the full PMD to be scanned when mTHP collapse is available.Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)mm/khugepaged.c +38
-0@@ -463,6 +463,44 @@ void __khugepaged_enter(struct mm_struct *mm)wake_up_interruptible (&khugepaged_wait); } +/** +* collapse_max_ptes_none - Calculate maximum allowed empty PTEs for collapse +* @order: The folio order being collapsed to +* @full_scan: Whether this is a full scan (ignore limits) +* +* For madvise-triggered collapses (full_scan=true), all limits are bypassed +* and allow up to HPAGE_PMD_NR - 1 empty PTEs. +* +* For PMD-sized collapses (order == HPAGE_PMD_ORDER), use the configured +* khugepaged_max_ptes_none value. +* +* For mTHP collapses, we currently only support khugepaged_max_pte_none values +* of 0 or (HPAGE_PMD_NR - 1). Any other value will emit a warning and no mTHP +* collapse will be attempted +* +* Return: Maximum number of empty PTEs allowed for the collapse operation +*/ +static unsigned int collapse_max_ptes_none (unsigned int order,bool full_scan) +{ +/* ignore max_ptes_none limits */ +if (full_scan) +return HPAGE_PMD_NR -1 ; + +if (!is_mthp_order (order)) +return khugepaged_max_ptes_none; + +/* Zero/non-present collapse disabled. */ +if (!khugepaged_max_ptes_none) +return 0 ; + +if (khugepaged_max_ptes_none ==HPAGE_PMD_NR -1 ) +return (1 << order) -1 ; + +pr_warn_once ("mTHP collapse only supports max_ptes_none values of 0 or %d\n" , +HPAGE_PMD_NR -1 ); +return -EINVAL ; +} +void khugepaged_enter_vma (struct vm_area_struct *vma,vm_flags_t vm_flags) {mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 594
Quoted 8 lines
mm/khugepaged.c +4
-1@@ -550,7 +588,10 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,pte_t *_pte;int none_or_zero =0 , shared =0 , result =SCAN_FAIL , referenced =0 ;const unsigned long nr_pages =1UL << order; -int max_ptes_none = khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - order); +int max_ptes_none =collapse_max_ptes_none (order, !cc->is_khugepaged); + +if (max_ptes_none == -EINVAL ) + goto out;After testing your patchset, I hit the following crash. The reason is that when '
max_ptes_none' is-EINVALhere, it shouldn't goto out to callrelease_pte_pages(), because the '_pte' hasn't been initialized at this point, and there's no need to release folios either.After applying the fix below, the crash issue is resolved. I'm not sure whether Andrew will help fix this or if you will send a new version to address this issue.
mm/khugepaged.c +1
-1@@ -646,7 +646,7 @@ static int __collapse_huge_page_isolate(structvm_area_struct *vma, int max_ptes_none = collapse_max_ptes_none(order, !cc->is_khugepaged);if (max_ptes_none == -EINVAL ) - goto out; +return result;for (_pte = pte; _pte < pte + nr_pages; _pte++, addr += PAGE_SIZE) {"
[ 565.319345] Unable to handle kernel paging request at virtual address fffffffffffffffa ....... [ 565.319409] swapper pgtable: 4k pages, 48-bit VAs, pgdp=0000001f8549a000 [ 565.319416] [fffffffffffffffa] pgd=0000001f85f2a403, p4d=0000001f85f2a403, pud=0000001f85f2b403, pmd=0000000000000000 [ 565.319427] Internal error: Oops: 0000000096000006 [#1] SMP ....... [ 565.326733] pc : release_pte_pages+0x68/0x178 [ 565.326960] lr : __collapse_huge_page_isolate+0xc0/0x748 [ 565.327232] sp : ffff800083593910 ....... [ 565.331476] Call trace: [ 565.331664] release_pte_pages+0x68/0x178 (P) [ 565.331940] __collapse_huge_page_isolate+0xc0/0x748 [ 565.332249] collapse_huge_page+0x4cc/0xa70 [ 565.332510] mthp_collapse+0x254/0x2a8 [ 565.332754] collapse_scan_pmd+0x5a0/0x6d8 [ 565.333010] collapse_single_pmd+0x214/0x288 [ 565.333275] collapse_scan_mm_slot.constprop.0+0x2ac/0x460 [ 565.333617] khugepaged+0x204/0x2c8 [ 565.333992] kthread+0xf8/0x110 [ 565.334368] ret_from_fork+0x10/0x20"
Quoted 3 lines
mm/khugepaged.c +0
-0for (_pte = pte; _pte < pte + nr_pages; _pte++, addr +=PAGE_SIZE ) {
PATCH v13 8/16 khugepaged: generalize collapse_huge_page for mTHP collapse
In reply to Nico PachePass an order and offset to collapse_huge_page to support collapsing anon memory to arbitrary orders within a PMD. order indicates what mTHP size we are attempting to collapse to, and offset indicates were in the PMD to start the collapse attempt.
For non-PMD collapse we must leave the anon VMA write locked until after we collapse the mTHP-- in the PMD case all the pages are isolated, but in the mTHP case this is not true, and we must keep the lock to prevent changes to the VMA from occurring.
--- mm/khugepaged.c | 112 ++++++++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 41 deletions(-)
mm/khugepaged.c +71 -41
@@ -1155,45 +1155,54 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,return SCAN_SUCCEED ; } -static int collapse_huge_page (struct mm_struct *mm,unsigned long address, -int referenced,int unmapped, -struct collapse_control *cc) +static int collapse_huge_page (struct mm_struct *mm,unsigned long start_addr, +int referenced,int unmapped,struct collapse_control *cc, +bool *mmap_locked,unsigned int order) {LIST_HEAD (compound_pagelist);pmd_t *pmd, _pmd; -pte_t *pte; +pte_t *pte =NULL ;pgtable_t pgtable;struct folio *folio;spinlock_t *pmd_ptl, *pte_ptl;int result =SCAN_FAIL ;struct vm_area_struct *vma;struct mmu_notifier_range range; +bool anon_vma_locked = false; +const unsigned long nr_pages =1UL << order; +const unsigned long pmd_address = start_addr &HPAGE_PMD_MASK ; -VM_BUG_ON (address & ~HPAGE_PMD_MASK ); +VM_WARN_ON_ONCE (pmd_address & ~HPAGE_PMD_MASK );/* * Before allocating the hugepage, release the mmap_lock read lock. * The allocation can take potentially a long time if it involves * sync compaction, and we do not need to hold the mmap_lock during * that. We will recheck the vma after taking it again in write mode. +* If collapsing mTHPs we may have already released the read_lock. */ -mmap_read_unlock (mm); +if (*mmap_locked) { +mmap_read_unlock (mm); + *mmap_locked = false; + } - result =alloc_charge_folio (&folio, mm, cc,HPAGE_PMD_ORDER ); + result =alloc_charge_folio (&folio, mm, cc, order);if (result !=SCAN_SUCCEED ) goto out_nolock;mmap_read_lock (mm); - result =hugepage_vma_revalidate (mm, address, true, &vma, cc, -HPAGE_PMD_ORDER ); + *mmap_locked = true; + result =hugepage_vma_revalidate (mm, pmd_address, true, &vma, cc, order);if (result !=SCAN_SUCCEED ) {mmap_read_unlock (mm); + *mmap_locked = false; goto out_nolock; } - result =find_pmd_or_thp_or_none (mm, address, &pmd); + result =find_pmd_or_thp_or_none (mm, pmd_address, &pmd);if (result !=SCAN_SUCCEED ) {mmap_read_unlock (mm); + *mmap_locked = false; goto out_nolock; }
@@ -1203,13 +1212,16 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,* released when it fails. So we jump out_nolock directly in * that case. Continuing to collapse causes inconsistency. */ - result =__collapse_huge_page_swapin (mm, vma, address, pmd, - referenced,HPAGE_PMD_ORDER ); -if (result !=SCAN_SUCCEED ) + result =__collapse_huge_page_swapin (mm, vma, start_addr, pmd, + referenced, order); +if (result !=SCAN_SUCCEED ) { + *mmap_locked = false; goto out_nolock; + } }mmap_read_unlock (mm); + *mmap_locked = false;/* * Prevent all access to pagetables with the exception of * gup_fast later handled by the ptep_clear_flush and the VM
@@ -1219,20 +1231,20 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,* mmap_lock. */ mmap_write_lock (mm); - result =hugepage_vma_revalidate (mm, address, true, &vma, cc, -HPAGE_PMD_ORDER ); + result =hugepage_vma_revalidate (mm, pmd_address, true, &vma, cc, order);if (result !=SCAN_SUCCEED ) goto out_up_write;/* check if the pmd is still valid */ vma_start_write (vma); - result =check_pmd_still_valid (mm, address, pmd); + result =check_pmd_still_valid (mm, pmd_address, pmd);if (result !=SCAN_SUCCEED ) goto out_up_write;anon_vma_lock_write (vma->anon_vma); + anon_vma_locked = true; -mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, address, - address +HPAGE_PMD_SIZE ); +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, start_addr, + start_addr + (PAGE_SIZE << order));mmu_notifier_invalidate_range_start (&range); pmd_ptl =pmd_lock (mm, pmd);/* probably unnecessary */
@@ -1244,24 +1256,21 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,* Parallel GUP-fast is fine since GUP-fast will back off when * it detects PMD is changed. */ - _pmd =pmdp_collapse_flush (vma, address, pmd); + _pmd =pmdp_collapse_flush (vma, pmd_address, pmd);spin_unlock (pmd_ptl);mmu_notifier_invalidate_range_end (&range);tlb_remove_table_sync_one (); - pte =pte_offset_map_lock (mm, &_pmd, address, &pte_ptl); + pte =pte_offset_map_lock (mm, &_pmd, start_addr, &pte_ptl);if (pte) { - result =__collapse_huge_page_isolate (vma, address, pte, cc, -HPAGE_PMD_ORDER , - &compound_pagelist); + result =__collapse_huge_page_isolate (vma, start_addr, pte, cc, + order, &compound_pagelist);spin_unlock (pte_ptl); }else { result =SCAN_NO_PTE_TABLE ; }if (unlikely (result !=SCAN_SUCCEED )) { -if (pte) -pte_unmap (pte);spin_lock (pmd_ptl);BUG_ON (!pmd_none (*pmd));/*
@@ -1271,21 +1280,21 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,*/ pmd_populate (mm, pmd,pmd_pgtable (_pmd));spin_unlock (pmd_ptl); -anon_vma_unlock_write (vma->anon_vma); goto out_up_write; }/* -* All pages are isolated and locked so anon_vma rmap -* can't run anymore. +* For PMD collapse all pages are isolated and locked so anon_vma +* rmap can't run anymore. For mTHP collapse we must hold the lock */ -anon_vma_unlock_write (vma->anon_vma); +if (!is_mthp_order (order)) { +anon_vma_unlock_write (vma->anon_vma); + anon_vma_locked = false; + } result =__collapse_huge_page_copy (pte, folio, pmd, _pmd, - vma, address, pte_ptl, -HPAGE_PMD_ORDER , - &compound_pagelist); -pte_unmap (pte); + vma, start_addr, pte_ptl, + order, &compound_pagelist);if (unlikely (result !=SCAN_SUCCEED )) goto out_up_write;
@@ -1295,20 +1304,42 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,* write. */ __folio_mark_uptodate (folio); - pgtable =pmd_pgtable (_pmd); +if (is_mthp_order (order)) {/* mTHP collapse */ +pte_t mthp_pte =mk_pte (folio_page (folio,0 ), vma->vm_page_prot); -spin_lock (pmd_ptl); -BUG_ON (!pmd_none (*pmd)); -pgtable_trans_huge_deposit (mm, pmd, pgtable); -map_anon_folio_pmd_nopf (folio, pmd, vma, address); + mthp_pte =maybe_mkwrite (pte_mkdirty (mthp_pte), vma); +spin_lock (pmd_ptl); +WARN_ON_ONCE (!pmd_none (*pmd)); +folio_ref_add (folio, nr_pages -1 ); +folio_add_new_anon_rmap (folio, vma, start_addr,RMAP_EXCLUSIVE ); +folio_add_lru_vma (folio, vma); +set_ptes (vma->vm_mm, start_addr, pte, mthp_pte, nr_pages); +update_mmu_cache_range (NULL , vma, start_addr, pte, nr_pages); + +smp_wmb ();/* make PTEs visible before PMD. See pmd_install() */ +pmd_populate (mm, pmd,pmd_pgtable (_pmd)); + }else {/* PMD collapse */ + pgtable =pmd_pgtable (_pmd); + +spin_lock (pmd_ptl); +WARN_ON_ONCE (!pmd_none (*pmd)); +pgtable_trans_huge_deposit (mm, pmd, pgtable); +map_anon_folio_pmd_nopf (folio, pmd, vma, pmd_address); + }spin_unlock (pmd_ptl); folio =NULL ; result =SCAN_SUCCEED ; out_up_write: +if (anon_vma_locked) +anon_vma_unlock_write (vma->anon_vma); +if (pte) +pte_unmap (pte);mmap_write_unlock (mm); + *mmap_locked = false; out_nolock: +WARN_ON_ONCE (*mmap_locked);if (folio)folio_put (folio);trace_mm_collapse_huge_page (mm, result ==SCAN_SUCCEED , result);
@@ -1476,9 +1507,8 @@ static int collapse_scan_pmd(struct mm_struct *mm,pte_unmap_unlock (pte, ptl);if (result ==SCAN_SUCCEED ) { result =collapse_huge_page (mm, start_addr, referenced, - unmapped, cc); -/* collapse_huge_page will return with the mmap_lock released */ - *mmap_locked = false; + unmapped, cc, mmap_locked, +HPAGE_PMD_ORDER ); } out: trace_mm_khugepaged_scan_pmd(mm, folio, referenced,
Signature · 1 line
2.51.1
PATCH v13 9/16 khugepaged: skip collapsing mTHP to smaller orders
In reply to Nico Pachekhugepaged may try to collapse a mTHP to a smaller mTHP, resulting in some pages being unmapped. Skip these cases until we have a way to check if its ok to collapse to a smaller mTHP size (like in the case of a partially mapped folio).
This patch is inspired by Dev Jain's work on khugepaged mTHP support [1].
[1] https://lore.kernel.org/lkml/[email protected]/
--- mm/khugepaged.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/khugepaged.c +8 -0
@@ -641,6 +641,14 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, goto out; } } +/* +* TODO: In some cases of partially-mapped folios, we'd actually +* want to collapse. +*/ +if (is_mthp_order (order) &&folio_order (folio) >= order) { + result =SCAN_PTE_MAPPED_HUGEPAGE ; + goto out; + }if (folio_test_large (folio)) {struct folio *f;
Signature · 1 line
2.51.1
PATCH v13 10/16 khugepaged: add per-order mTHP collapse failure statistics
In reply to Nico PacheAdd three new mTHP statistics to track collapse failures for different orders when encountering swap PTEs, excessive none PTEs, and shared PTEs:
- collapse_exceed_swap_pte: Increment when mTHP collapse fails due to swap PTEs
- collapse_exceed_none_pte: Counts when mTHP collapse fails due to exceeding the none PTE threshold for the given order
- collapse_exceed_shared_pte: Counts when mTHP collapse fails due to shared PTEs
These statistics complement the existing THP_SCAN_EXCEED_* events by providing per-order granularity for mTHP collapse attempts. The stats are exposed via sysfs under /sys/kernel/mm/transparent_hugepage/hugepages-*/stats/ for each supported hugepage size.
As we currently dont support collapsing mTHPs that contain a swap or shared entry, those statistics keep track of how often we are encountering failed mTHP collapses due to these restrictions.
--- Documentation/admin-guide/mm/transhuge.rst | 24 ++++++++++++++++++++++ include/linux/huge_mm.h | 3 +++ mm/huge_memory.c | 7 +++++++ mm/khugepaged.c | 16 ++++++++++++--- 4 files changed, 47 insertions(+), 3 deletions(-)
Documentation/admin-guide/mm/transhuge.rst +24 -0
@@ -714,6 +714,30 @@ nr_anon_partially_mapped an anonymous THP as "partially mapped" and count it here, even though it is not actually partially mapped anymore. +collapse_exceed_none_pte + The number of collapse attempts that failed due to exceeding the + max_ptes_none threshold. For mTHP collapse, Currently only max_ptes_none + values of 0 and (HPAGE_PMD_NR - 1) are supported. Any other value will + emit a warning and no mTHP collapse will be attempted. khugepaged will + try to collapse to the largest enabled (m)THP size, if it fails, it will + try the next lower enabled mTHP size. This counter records the number of + times a collapse attempt was skipped for exceeding the max_ptes_none + threshold, and khugepaged will move on to the next available mTHP size. + +collapse_exceed_swap_pte + The number of anonymous mTHP pte ranges which were unable to collapse due + to containing at least one swap PTE. Currently khugepaged does not + support collapsing mTHP regions that contain a swap PTE. This counter can + be used to monitor the number of khugepaged mTHP collapses that failed + due to the presence of a swap PTE. + +collapse_exceed_shared_pte + The number of anonymous mTHP pte ranges which were unable to collapse due + to containing at least one shared PTE. Currently khugepaged does not + support collapsing mTHP pte ranges that contain a shared PTE. This + counter can be used to monitor the number of khugepaged mTHP collapses + that failed due to the presence of a shared PTE. + As the system ages, allocating huge pages may be expensive as the system uses memory compaction to copy data around memory to free a huge page for use. There are some counters in ``/proc/vmstat`` to help
include/linux/huge_mm.h +3 -0
@@ -144,6 +144,9 @@ enum mthp_stat_item {MTHP_STAT_SPLIT_DEFERRED ,MTHP_STAT_NR_ANON ,MTHP_STAT_NR_ANON_PARTIALLY_MAPPED , +MTHP_STAT_COLLAPSE_EXCEED_SWAP , +MTHP_STAT_COLLAPSE_EXCEED_NONE , +MTHP_STAT_COLLAPSE_EXCEED_SHARED , __MTHP_STAT_COUNT };
mm/huge_memory.c +7 -0
@@ -639,6 +639,10 @@ DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);DEFINE_MTHP_STAT_ATTR (split_deferred,MTHP_STAT_SPLIT_DEFERRED );DEFINE_MTHP_STAT_ATTR (nr_anon,MTHP_STAT_NR_ANON );DEFINE_MTHP_STAT_ATTR (nr_anon_partially_mapped,MTHP_STAT_NR_ANON_PARTIALLY_MAPPED ); +DEFINE_MTHP_STAT_ATTR (collapse_exceed_swap_pte,MTHP_STAT_COLLAPSE_EXCEED_SWAP ); +DEFINE_MTHP_STAT_ATTR (collapse_exceed_none_pte,MTHP_STAT_COLLAPSE_EXCEED_NONE ); +DEFINE_MTHP_STAT_ATTR (collapse_exceed_shared_pte,MTHP_STAT_COLLAPSE_EXCEED_SHARED ); +static struct attribute *anon_stats_attrs[] = { &anon_fault_alloc_attr.attr,
@@ -655,6 +659,9 @@ static struct attribute *anon_stats_attrs[] = { &split_deferred_attr.attr, &nr_anon_attr.attr, &nr_anon_partially_mapped_attr.attr, + &collapse_exceed_swap_pte_attr.attr, + &collapse_exceed_none_pte_attr.attr, + &collapse_exceed_shared_pte_attr.attr, NULL, };
mm/khugepaged.c +13 -3
@@ -604,7 +604,9 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,continue ; }else { result =SCAN_EXCEED_NONE_PTE ; -count_vm_event (THP_SCAN_EXCEED_NONE_PTE ); +if (!is_mthp_order (order)) +count_vm_event (THP_SCAN_EXCEED_NONE_PTE ); +count_mthp_stat (order,MTHP_STAT_COLLAPSE_EXCEED_NONE ); goto out; } }
@@ -634,10 +636,17 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,* shared may cause a future higher order collapse on a * rescan of the same range. */ -if (is_mthp_order (order) || (cc->is_khugepaged && - shared > khugepaged_max_ptes_shared)) { +if (is_mthp_order (order)) { + result =SCAN_EXCEED_SHARED_PTE ; +count_mthp_stat (order,MTHP_STAT_COLLAPSE_EXCEED_SHARED ); + goto out; + } + +if (cc->is_khugepaged && + shared > khugepaged_max_ptes_shared) { result =SCAN_EXCEED_SHARED_PTE ;count_vm_event (THP_SCAN_EXCEED_SHARED_PTE ); +count_mthp_stat (order,MTHP_STAT_COLLAPSE_EXCEED_SHARED ); goto out; } }
@@ -1086,6 +1095,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,* range. */ if (is_mthp_order (order)) { +count_mthp_stat (order,MTHP_STAT_COLLAPSE_EXCEED_SWAP );pte_unmap (pte);mmap_read_unlock (mm); result =SCAN_EXCEED_SWAP_PTE ;
Signature · 1 line
2.51.1
Randy Dunlap
reviewerRe: [PATCH v13 mm-new 10/16] khugepaged: add per-order mTHP collapse failure statistics
Quoted 36 lines · Nico Pache
Add three new mTHP statistics to track collapse failures for different orders when encountering swap PTEs, excessive none PTEs, and shared PTEs:
-
collapse_exceed_swap_pte: Increment when mTHP collapse fails due to swap PTEs-
collapse_exceed_none_pte: Counts when mTHP collapse fails due to exceeding the none PTE threshold for the given order-
collapse_exceed_shared_pte: Counts when mTHP collapse fails due to shared PTEsThese statistics complement the existing THP_SCAN_EXCEED_* events by providing per-order granularity for mTHP collapse attempts. The stats are exposed via sysfs under
/sys/kernel/mm/transparent_hugepage/hugepages-*/stats/for each supported hugepage size.As we currently dont support collapsing mTHPs that contain a swap or shared entry, those statistics keep track of how often we are encountering failed mTHP collapses due to these restrictions.
Reviewed-by Baolin Wang <[email protected]>Signed-off-by Nico Pache <[email protected]>--- Documentation/admin-guide/mm/transhuge.rst | 24 ++++++++++++++++++++++ include/linux/huge_mm.h | 3 +++ mm/huge_memory.c | 7 +++++++ mm/khugepaged.c | 16 ++++++++++++--- 4 files changed, 47 insertions(+), 3 deletions(-)Documentation/admin-guide/mm/transhuge.rst +0
-0
Documentation/admin-guide/mm/transhuge.rst nr_anon_partially_mapped · line 719
Quoted 6 lines
Documentation/admin-guide/mm/transhuge.rst +3
-0@@ -714,6 +714,30 @@ nr_anon_partially_mapped an anonymous THP as "partially mapped" and count it here, even though it is not actually partially mapped anymore. +collapse_exceed_none_pte + The number of collapse attempts that failed due to exceeding the + max_ptes_none threshold. For mTHP collapse, Currently only max_ptes_none
currently
Documentation/admin-guide/mm/transhuge.rst nr_anon_partially_mapped · line 722
Quoted 3 lines
Documentation/admin-guide/mm/transhuge.rst +3
-0+ values of 0 and (HPAGE_PMD_NR - 1) are supported. Any other value will + emit a warning and no mTHP collapse will be attempted. khugepaged will + try to collapse to the largest enabled (m)THP size, if it fails, it will
size. If
or
size; if
Documentation/admin-guide/mm/transhuge.rst nr_anon_partially_mapped · line 728
Quoted 6 lines
Documentation/admin-guide/mm/transhuge.rst +6
-0+ try the next lower enabled mTHP size. This counter records the number of + times a collapse attempt was skipped for exceeding the max_ptes_none + threshold, and khugepaged will move on to the next available mTHP size. + +collapse_exceed_swap_pte + The number of anonymous mTHP pte ranges which were unable to collapse due
PTE
Documentation/admin-guide/mm/transhuge.rst nr_anon_partially_mapped · line 735
Quoted 7 lines
Documentation/admin-guide/mm/transhuge.rst +7
-0+ to containing at least one swap PTE. Currently khugepaged does not + support collapsing mTHP regions that contain a swap PTE. This counter can + be used to monitor the number of khugepaged mTHP collapses that failed + due to the presence of a swap PTE. + +collapse_exceed_shared_pte + The number of anonymous mTHP pte ranges which were unable to collapse due
PTE
Documentation/admin-guide/mm/transhuge.rst nr_anon_partially_mapped · line 737
Quoted 2 lines
Documentation/admin-guide/mm/transhuge.rst +2
-0+ to containing at least one shared PTE. Currently khugepaged does not + support collapsing mTHP pte ranges that contain a shared PTE. This
PTE
Quoted 6 lines
Documentation/admin-guide/mm/transhuge.rst +3
-0+ counter can be used to monitor the number of khugepaged mTHP collapses + that failed due to the presence of a shared PTE. + As the system ages, allocating huge pages may be expensive as the system uses memory compaction to copy data around memory to free a huge page for use. There are some counters in ``/proc/vmstat`` to help
Signature · 1 line
~Randy
PATCH v13 11/16 khugepaged: improve tracepoints for mTHP orders
In reply to Nico PacheAdd the order to the mm_collapse_huge_page<_swapin,_isolate> tracepoints to give better insight into what order is being operated at for.
--- include/trace/events/huge_memory.h | 34 +++++++++++++++++++----------- mm/khugepaged.c | 9 ++++---- 2 files changed, 27 insertions(+), 16 deletions(-)
include/trace/events/huge_memory.h +22 -12
@@ -87,40 +87,44 @@ TRACE_EVENT(mm_khugepaged_scan_pmd,TRACE_EVENT (mm_collapse_huge_page, - TP_PROTO(struct mm_struct *mm, int isolated, int status), + TP_PROTO(struct mm_struct *mm, int isolated, int status, unsigned int order), -TP_ARGS (mm, isolated, status), +TP_ARGS (mm, isolated, status, order),TP_STRUCT__entry ( __field(struct mm_struct *, mm) __field(int, isolated) __field(int, status) + __field(unsigned int, order) ),TP_fast_assign ( __entry->mm = mm; __entry->isolated = isolated; __entry->status = status; + __entry->order = order; ), -TP_printk ("mm=%p, isolated=%d, status=%s" , +TP_printk ("mm=%p, isolated=%d, status=%s order=%u" , __entry->mm, __entry->isolated, -__print_symbolic (__entry->status,SCAN_STATUS )) +__print_symbolic (__entry->status,SCAN_STATUS ), + __entry->order) ); TRACE_EVENT(mm_collapse_huge_page_isolate, TP_PROTO(struct folio *folio, int none_or_zero, - int referenced, int status), + int referenced, int status, unsigned int order), - TP_ARGS(folio, none_or_zero, referenced, status), + TP_ARGS(folio, none_or_zero, referenced, status, order), TP_STRUCT__entry( __field(unsigned long, pfn) __field(int, none_or_zero) __field(int, referenced) __field(int, status) + __field(unsigned int, order) ), TP_fast_assign(
@@ -128,26 +132,30 @@ TRACE_EVENT(mm_collapse_huge_page_isolate, __entry->none_or_zero = none_or_zero; __entry->referenced = referenced; __entry->status = status; + __entry->order = order; ), -TP_printk ("scan_pfn=0x%lx, none_or_zero=%d, referenced=%d, status=%s" , +TP_printk ("scan_pfn=0x%lx, none_or_zero=%d, referenced=%d, status=%s order=%u" , __entry->pfn, __entry->none_or_zero, __entry->referenced, -__print_symbolic (__entry->status,SCAN_STATUS )) +__print_symbolic (__entry->status,SCAN_STATUS ), + __entry->order) ); TRACE_EVENT(mm_collapse_huge_page_swapin, - TP_PROTO(struct mm_struct *mm, int swapped_in, int referenced, int ret), + TP_PROTO(struct mm_struct *mm, int swapped_in, int referenced, int ret, + unsigned int order), - TP_ARGS(mm, swapped_in, referenced, ret), + TP_ARGS(mm, swapped_in, referenced, ret, order), TP_STRUCT__entry( __field(struct mm_struct *, mm) __field(int, swapped_in) __field(int, referenced) __field(int, ret) + __field(unsigned int, order) ), TP_fast_assign(
@@ -155,13 +163,15 @@ TRACE_EVENT(mm_collapse_huge_page_swapin, __entry->swapped_in = swapped_in; __entry->referenced = referenced; __entry->ret = ret; + __entry->order = order; ), -TP_printk ("mm=%p, swapped_in=%d, referenced=%d, ret=%d" , +TP_printk ("mm=%p, swapped_in=%d, referenced=%d, ret=%d, order=%u" , __entry->mm, __entry->swapped_in, __entry->referenced, - __entry->ret) + __entry->ret, + __entry->order) ); TRACE_EVENT(mm_khugepaged_scan_file,
mm/khugepaged.c +5 -4
@@ -734,13 +734,13 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, }else { result =SCAN_SUCCEED ;trace_mm_collapse_huge_page_isolate (folio, none_or_zero, - referenced, result); + referenced, result, order);return result; } out:release_pte_pages (pte, _pte, compound_pagelist);trace_mm_collapse_huge_page_isolate (folio, none_or_zero, - referenced, result); + referenced, result, order);return result; }
@@ -1136,7 +1136,8 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm, result =SCAN_SUCCEED ; out: -trace_mm_collapse_huge_page_swapin (mm, swapped_in, referenced, result); +trace_mm_collapse_huge_page_swapin (mm, swapped_in, referenced, result, + order);return result; }
@@ -1360,7 +1361,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long start_addr,WARN_ON_ONCE (*mmap_locked);if (folio)folio_put (folio); -trace_mm_collapse_huge_page (mm, result ==SCAN_SUCCEED , result); +trace_mm_collapse_huge_page (mm, result ==SCAN_SUCCEED , result, order);return result; }
Signature · 1 line
2.51.1
PATCH v13 12/16 khugepaged: introduce collapse_allowable_orders helper function
In reply to Nico PacheAdd collapse_allowable_orders() to generalize THP order eligibility. The function determines which THP orders are permitted based on collapse context (khugepaged vs madv_collapse).
This consolidates collapse configuration logic and provides a clean interface for future mTHP collapse support where the orders may be different.
--- mm/khugepaged.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
mm/khugepaged.c +13 -3
@@ -501,12 +501,22 @@ static unsigned int collapse_max_ptes_none(unsigned int order, bool full_scan)return -EINVAL ; } +/* Check what orders are allowed based on the vma and collapse type */ +static unsigned long collapse_allowable_orders (struct vm_area_struct *vma, +vm_flags_t vm_flags,bool is_khugepaged) +{ +enum tva_type tva_flags = is_khugepaged ?TVA_KHUGEPAGED :TVA_FORCED_COLLAPSE ; +unsigned long orders =BIT (HPAGE_PMD_ORDER ); + +return thp_vma_allowable_orders (vma, vm_flags, tva_flags, orders); +} +void khugepaged_enter_vma (struct vm_area_struct *vma,vm_flags_t vm_flags) {if (!mm_flags_test (MMF_VM_HUGEPAGE , vma->vm_mm) &&hugepage_pmd_enabled ()) { -if (thp_vma_allowable_order (vma, vm_flags,TVA_KHUGEPAGED ,PMD_ORDER )) +if (collapse_allowable_orders (vma, vm_flags,/*is_khugepaged=*/ true))__khugepaged_enter (vma->vm_mm); } }
@@ -2606,7 +2616,7 @@ static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result, progress++;break ; } -if (!thp_vma_allowable_order (vma, vma->vm_flags,TVA_KHUGEPAGED ,PMD_ORDER )) { +if (!collapse_allowable_orders (vma, vma->vm_flags,/*is_khugepaged=*/ true)) { skip: progress++;continue ;
@@ -2912,7 +2922,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,BUG_ON (vma->vm_start > start);BUG_ON (vma->vm_end < end); -if (!thp_vma_allowable_order (vma, vma->vm_flags,TVA_FORCED_COLLAPSE ,PMD_ORDER )) +if (!collapse_allowable_orders (vma, vma->vm_flags,/*is_khugepaged=*/ false))return -EINVAL ; cc =kmalloc (sizeof (*cc),GFP_KERNEL );
Signature · 1 line
2.51.1
Re: [PATCH v13 mm-new 12/16] khugepaged: introduce collapse_allowable_orders helper function
This one seems to also conflict with (current mm tree) commit 85c89728af35 ("mm/khugepaged: remove unnecessary goto 'skip' label").
Quoted 63 lines · Nico Pache
Add
collapse_allowable_orders()to generalize THP order eligibility. The function determines which THP orders are permitted based on collapse context (khugepaged vsmadv_collapse).This consolidates collapse configuration logic and provides a clean interface for future mTHP collapse support where the orders may be different.
Reviewed-by Baolin Wang <[email protected]>Signed-off-by Nico Pache <[email protected]>--- mm/khugepaged.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)mm/khugepaged.c +13
-4@@ -501,12 +501,22 @@ static unsigned int collapse_max_ptes_none(unsigned int order, bool full_scan)return -EINVAL ; } +/* Check what orders are allowed based on the vma and collapse type */ +static unsigned long collapse_allowable_orders (struct vm_area_struct *vma, +vm_flags_t vm_flags,bool is_khugepaged) +{ +enum tva_type tva_flags = is_khugepaged ?TVA_KHUGEPAGED :TVA_FORCED_COLLAPSE ; +unsigned long orders =BIT (HPAGE_PMD_ORDER ); + +return thp_vma_allowable_orders (vma, vm_flags, tva_flags, orders); +} +void khugepaged_enter_vma (struct vm_area_struct *vma,vm_flags_t vm_flags) {if (!mm_flags_test (MMF_VM_HUGEPAGE , vma->vm_mm) &&hugepage_pmd_enabled ()) { -if (thp_vma_allowable_order (vma, vm_flags,TVA_KHUGEPAGED ,PMD_ORDER )) +if (collapse_allowable_orders (vma, vm_flags,/*is_khugepaged=*/ true))__khugepaged_enter (vma->vm_mm); } }@@ -2606,7 +2616,7 @@ static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result, progress++;break ; } -if (!thp_vma_allowable_order (vma, vma->vm_flags,TVA_KHUGEPAGED ,PMD_ORDER )) { +if (!collapse_allowable_orders (vma, vma->vm_flags,/*is_khugepaged=*/ true)) { skip: progress++;continue ;@@ -2912,7 +2922,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,BUG_ON (vma->vm_start > start);BUG_ON (vma->vm_end < end); -if (!thp_vma_allowable_order (vma, vma->vm_flags,TVA_FORCED_COLLAPSE ,PMD_ORDER )) +if (!collapse_allowable_orders (vma, vma->vm_flags,/*is_khugepaged=*/ false))return -EINVAL ; cc =kmalloc (sizeof (*cc),GFP_KERNEL ); --2.51.1
PATCH v13 13/16 khugepaged: Introduce mTHP collapse support
In reply to Nico PacheEnable khugepaged to collapse to mTHP orders. This patch implements the main scanning logic using a bitmap to track occupied pages and a stack structure that allows us to find optimal collapse sizes.
Previous to this patch, PMD collapse had 3 main phases, a light weight scanning phase (mmap_read_lock) that determines a potential PMD collapse, a alloc phase (mmap unlocked), then finally heavier collapse phase (mmap_write_lock).
To enabled mTHP collapse we make the following changes:
During PMD scan phase, track occupied pages in a bitmap. When mTHP orders are enabled, we remove the restriction of max_ptes_none during the scan phase to avoid missing potential mTHP collapse candidates. Once we have scanned the full PMD range and updated the bitmap to track occupied pages, we use the bitmap to find the optimal mTHP size.
Implement collapse_scan_bitmap() to perform binary recursion on the bitmap and determine the best eligible order for the collapse. A stack structure is used instead of traditional recursion to manage the search. The algorithm recursively splits the bitmap into smaller chunks to find the highest order mTHPs that satisfy the collapse criteria. We start by attempting the PMD order, then moved on the consecutively lower orders (mTHP collapse). The stack maintains a pair of variables (offset, order), indicating the number of PTEs from the start of the PMD, and the order of the potential collapse candidate.
The algorithm for consuming the bitmap works as such:
1) push (0, HPAGE_PMD_ORDER) onto the stack
2) pop the stack
3) check if the number of set bits in that (offset,order) pair statisfy the max_ptes_none threshold for that order
4) if yes, attempt collapse
5) if no (or collapse fails), push two new stack items representing the left and right halves of the current bitmap range, at the next lower order
6) repeat at step (2) until stack is empty.
Below is a diagram representing the algorithm and stack items:
offset mid_offset
| |
| |
v v
____________________________________
| PTE Page Table |
--------------------------------------
<-------><------->
order-1 order-1We currently only support mTHP collapse for max_ptes_none values of 0 and HPAGE_PMD_NR - 1. resulting in the following behavior:
- max_ptes_none=0: Never introduce new empty pages during collapse
- max_ptes_none=HPAGE_PMD_NR-1: Always try collapse to the highest available mTHP order
Any other max_ptes_none value will emit a warning and skip mTHP collapse attempts. There should be no behavior change for PMD collapse.
Once we determine what mTHP sizes fits best in that PMD range a collapse is attempted. A minimum collapse order of 2 is used as this is the lowest order supported by anon memory as defined by THP_ORDERS_ALL_ANON.
mTHP collapses reject regions containing swapped out or shared pages. This is because adding new entries can lead to new none pages, and these may lead to constant promotion into a higher order (m)THP. A similar issue can occur with "max_ptes_none > HPAGE_PMD_NR/2" due to a collapse introducing at least 2x the number of pages, and on a future scan will satisfy the promotion condition once again. This issue is prevented via the collapse_max_ptes_none() function which imposes the max_ptes_none restrictions above.
Currently madv_collapse is not supported and will only attempt PMD collapse.
We can also remove the check for is_khugepaged inside the PMD scan as the collapse_max_ptes_none() function handles this logic now.
--- mm/khugepaged.c | 183 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 175 insertions(+), 8 deletions(-)
mm/khugepaged.c +175 -8
@@ -92,6 +92,32 @@ static DEFINE_READ_MOSTLY_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);static struct kmem_cache *mm_slot_cache__ro_after_init ; +#define KHUGEPAGED_MIN_MTHP_ORDER 2 +/* +* The maximum number of mTHP ranges that can be stored on the stack. +* This is calculated based on the number of PTE entries in a PTE page table +* and the minimum mTHP order. +* +* ilog2(MAX_PTRS_PER_PTE) is log2 of the maximum number of PTE entries. +* This gives you the PMD_ORDER, and is needed in place of HPAGE_PMD_ORDER due +* to restrictions of some architectures (ie ppc64le). +* +* At most there will be 1 << (PMD_ORDER - KHUGEPAGED_MIN_MTHP_ORDER) mTHP ranges +*/ +#define MTHP_STACK_SIZE (1UL << (ilog2(MAX_PTRS_PER_PTE) - KHUGEPAGED_MIN_MTHP_ORDER)) + +/* +* Defines a range of PTE entries in a PTE page table which are being +* considered for (m)THP collapse. +* +* @offset: the offset of the first PTE entry in a PMD range. +* @order: the order of the PTE entries being considered for collapse. +*/ +struct mthp_range { +u16 offset; +u8 order; +}; +struct collapse_control {bool is_khugepaged;
@@ -100,6 +126,11 @@ struct collapse_control {/* nodemask for allocation fallback */ nodemask_t alloc_nmask; + +/* bitmap used for mTHP collapse */ +DECLARE_BITMAP (mthp_bitmap,MAX_PTRS_PER_PTE ); +DECLARE_BITMAP (mthp_bitmap_mask,MAX_PTRS_PER_PTE ); +struct mthp_range mthp_bitmap_stack[MTHP_STACK_SIZE ]; };/**
@@ -1375,6 +1406,121 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long start_addr,return result; } +static void mthp_stack_push (struct collapse_control *cc,int *stack_size, +u16 offset,u8 order) +{ +const int size = *stack_size; +struct mthp_range *stack = &cc->mthp_bitmap_stack[size]; + +VM_WARN_ON_ONCE (size >=MTHP_STACK_SIZE ); + stack->order = order; + stack->offset = offset; + (*stack_size)++; +} + +static struct mthp_range mthp_stack_pop (struct collapse_control *cc,int *stack_size) +{ +const int size = *stack_size; + +VM_WARN_ON_ONCE (size <=0 ); + (*stack_size)--; +return cc->mthp_bitmap_stack[size -1 ]; +} + +static unsigned int mthp_nr_occupied_pte_entries (struct collapse_control *cc, +u16 offset,unsigned long nr_pte_entries) +{ +bitmap_zero (cc->mthp_bitmap_mask,HPAGE_PMD_NR ); +bitmap_set (cc->mthp_bitmap_mask, offset, nr_pte_entries); +return bitmap_weight_and (cc->mthp_bitmap, cc->mthp_bitmap_mask,HPAGE_PMD_NR ); +} + +/* +* mthp_collapse() consumes the bitmap that is generated during +* collapse_scan_pmd() to determine what regions and mTHP orders fit best. +* +* Each bit in cc->mthp_bitmap represents a single occupied (!none/zero) page. +* A stack structure cc->mthp_bitmap_stack is used to check different regions +* of the bitmap for collapse eligibility. The stack maintains a pair of +* variables (offset, order), indicating the number of PTEs from the start of +* the PMD, and the order of the potential collapse candidate respectively. We +* start at the PMD order and check if it is eligible for collapse; if not, we +* add two entries to the stack at a lower order to represent the left and right +* halves of the PTE page table we are examining. +* +* offset mid_offset +* | | +* | | +* v v +* -------------------------------------- +* | cc->mthp_bitmap | +* -------------------------------------- +* <-------><-------> +* order-1 order-1 +* +* For each of these, we determine how many PTE entries are occupied in the +* range of PTE entries we propose to collapse, then we compare this to a +* threshold number of PTE entries which would need to be occupied for a +* collapse to be permitted at that order (accounting for max_ptes_none). + +* If a collapse is permitted, we attempt to collapse the PTE range into a +* mTHP. +*/ +static int mthp_collapse (struct mm_struct *mm,unsigned long address, +int referenced,int unmapped,struct collapse_control *cc, +bool *mmap_locked,unsigned long enabled_orders) +{ +unsigned int max_ptes_none, nr_occupied_ptes; +struct mthp_range range; +unsigned long collapse_address; +int collapsed =0 , stack_size =0 ; +unsigned long nr_pte_entries; +u16 offset; +u8 order; + +mthp_stack_push (cc, &stack_size,0 ,HPAGE_PMD_ORDER ); + +while (stack_size >0 ) { + range =mthp_stack_pop (cc, &stack_size); + order = range.order; + offset = range.offset; + nr_pte_entries =1UL << order; + +if (!test_bit (order, &enabled_orders)) + goto next_order; + + max_ptes_none =collapse_max_ptes_none (order, !cc->is_khugepaged); + +if (max_ptes_none == -EINVAL ) +return collapsed; + + nr_occupied_ptes =mthp_nr_occupied_pte_entries (cc, offset, nr_pte_entries); + +if (nr_occupied_ptes >= nr_pte_entries - max_ptes_none) { +int ret; + + collapse_address = address + offset *PAGE_SIZE ; + ret =collapse_huge_page (mm, collapse_address, referenced, + unmapped, cc, mmap_locked, + order); +if (ret ==SCAN_SUCCEED ) { + collapsed += nr_pte_entries; +continue ; + } + } + +next_order: +if (order >KHUGEPAGED_MIN_MTHP_ORDER ) { +const u8 next_order = order -1 ; +const u16 mid_offset = offset + (nr_pte_entries /2 ); + +mthp_stack_push (cc, &stack_size, mid_offset, next_order); +mthp_stack_push (cc, &stack_size, offset, next_order); + } + } +return collapsed; +} + static int collapse_scan_pmd(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
@@ -1382,11 +1528,15 @@ static int collapse_scan_pmd(struct mm_struct *mm, {pmd_t *pmd;pte_t *pte, *_pte; +int i;int result =SCAN_FAIL , referenced =0 ; -int none_or_zero =0 , shared =0 ; +int none_or_zero =0 , shared =0 , nr_collapsed =0 ;struct page *page =NULL ; +unsigned int max_ptes_none;struct folio *folio =NULL ;unsigned long addr; +unsigned long enabled_orders; +bool full_scan = true;spinlock_t *ptl;int node =NUMA_NO_NODE , unmapped =0 ;
@@ -1396,22 +1546,34 @@ static int collapse_scan_pmd(struct mm_struct *mm,if (result !=SCAN_SUCCEED ) goto out; +bitmap_zero (cc->mthp_bitmap,HPAGE_PMD_NR );memset (cc->node_load,0 ,sizeof (cc->node_load));nodes_clear (cc->alloc_nmask); + + enabled_orders =collapse_allowable_orders (vma, vma->vm_flags, cc->is_khugepaged); + +/* +* If PMD is the only enabled order, enforce max_ptes_none, otherwise +* scan all pages to populate the bitmap for mTHP collapse. +*/ +if (cc->is_khugepaged && enabled_orders ==BIT (HPAGE_PMD_ORDER )) + full_scan = false; + max_ptes_none =collapse_max_ptes_none (HPAGE_PMD_ORDER , full_scan); + pte =pte_offset_map_lock (mm, pmd, start_addr, &ptl);if (!pte) { result =SCAN_NO_PTE_TABLE ; goto out; } -for (addr = start_addr, _pte = pte; _pte < pte +HPAGE_PMD_NR ; - _pte++, addr +=PAGE_SIZE ) { +for (i =0 ; i <HPAGE_PMD_NR ; i++) { + _pte = pte + i; + addr = start_addr + i *PAGE_SIZE ;pte_t pteval =ptep_get (_pte);if (pte_none_or_zero (pteval)) { ++none_or_zero;if (!userfaultfd_armed (vma) && - (!cc->is_khugepaged || - none_or_zero <= khugepaged_max_ptes_none)) { + none_or_zero <= max_ptes_none) {continue ; }else { result =SCAN_EXCEED_NONE_PTE ;
@@ -1479,6 +1641,8 @@ static int collapse_scan_pmd(struct mm_struct *mm, } } +/* Set bit for occupied pages */ +bitmap_set (cc->mthp_bitmap, i,1 );/* * Record which node the original page is from and save this * information to cc->node_load[].
@@ -1535,9 +1699,12 @@ static int collapse_scan_pmd(struct mm_struct *mm, out_unmap:pte_unmap_unlock (pte, ptl);if (result ==SCAN_SUCCEED ) { - result =collapse_huge_page (mm, start_addr, referenced, - unmapped, cc, mmap_locked, -HPAGE_PMD_ORDER ); + nr_collapsed =mthp_collapse (mm, start_addr, referenced, unmapped, + cc, mmap_locked, enabled_orders); +if (nr_collapsed >0 ) + result =SCAN_SUCCEED ; +else + result =SCAN_FAIL ; } out: trace_mm_khugepaged_scan_pmd(mm, folio, referenced,
Signature · 1 line
2.51.1
PATCH v13 14/16 khugepaged: avoid unnecessary mTHP collapse attempts
In reply to Nico PacheThere are cases where, if an attempted collapse fails, all subsequent orders are guaranteed to also fail. Avoid these collapse attempts by bailing out early.
--- mm/khugepaged.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
mm/khugepaged.c +34 -1
@@ -1503,9 +1503,42 @@ static int mthp_collapse(struct mm_struct *mm, unsigned long address, ret =collapse_huge_page (mm, collapse_address, referenced, unmapped, cc, mmap_locked, order); -if (ret ==SCAN_SUCCEED ) { + +switch (ret) { +/* Cases were we continue to next collapse candidate */ +case SCAN_SUCCEED : collapsed += nr_pte_entries; + fallthrough; +case SCAN_PTE_MAPPED_HUGEPAGE :continue ; +/* Cases were lower orders might still succeed */ +case SCAN_LACK_REFERENCED_PAGE : +case SCAN_EXCEED_NONE_PTE : +case SCAN_EXCEED_SWAP_PTE : +case SCAN_EXCEED_SHARED_PTE : +case SCAN_PAGE_LOCK : +case SCAN_PAGE_COUNT : +case SCAN_PAGE_LRU : +case SCAN_PAGE_NULL : +case SCAN_DEL_PAGE_LRU : +case SCAN_PTE_NON_PRESENT : +case SCAN_PTE_UFFD_WP : +case SCAN_ALLOC_HUGE_PAGE_FAIL : + goto next_order; +/* Cases were no further collapse is possible */ +case SCAN_CGROUP_CHARGE_FAIL : +case SCAN_COPY_MC : +case SCAN_ADDRESS_RANGE : +case SCAN_NO_PTE_TABLE : +case SCAN_ANY_PROCESS : +case SCAN_VMA_NULL : +case SCAN_VMA_CHECK : +case SCAN_SCAN_ABORT : +case SCAN_PAGE_ANON : +case SCAN_PMD_MAPPED : +case SCAN_FAIL : +default : +return collapsed; } }
Signature · 1 line
2.51.1
PATCH v13 15/16 khugepaged: run khugepaged for all orders
In reply to Nico PacheFrom: Baolin Wang <[email protected]>
If any order (m)THP is enabled we should allow running khugepaged to attempt scanning and collapsing mTHPs. In order for khugepaged to operate when only mTHP sizes are specified in sysfs, we must modify the predicate function that determines whether it ought to run to do so.
This function is currently called hugepage_pmd_enabled(), this patch renames it to hugepage_enabled() and updates the logic to check to determine whether any valid orders may exist which would justify khugepaged running.
We must also update collapse_allowable_orders() to check all orders if the vma is anonymous and the collapse is khugepaged.
After this patch khugepaged mTHP collapse is fully enabled.
--- mm/khugepaged.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
mm/khugepaged.c +18 -12
@@ -441,23 +441,23 @@ static inline int collapse_test_exit_or_disable(struct mm_struct *mm)mm_flags_test (MMF_DISABLE_THP_COMPLETELY , mm); } -static bool hugepage_pmd_enabled (void ) +static bool hugepage_enabled (void ) {/* * We cover the anon, shmem and the file-backed case here; file-backed * hugepages, when configured in, are determined by the global control. -* Anon pmd-sized hugepages are determined by the pmd-size control. +* Anon hugepages are determined by its per-size mTHP control. * Shmem pmd-sized hugepages are also determined by its pmd-size control, * except when the global shmem_huge is set to SHMEM_HUGE_DENY. */ if (IS_ENABLED (CONFIG_READ_ONLY_THP_FOR_FS ) &&hugepage_global_enabled ())return true; -if (test_bit (PMD_ORDER , &huge_anon_orders_always)) +if (READ_ONCE (huge_anon_orders_always))return true; -if (test_bit (PMD_ORDER , &huge_anon_orders_madvise)) +if (READ_ONCE (huge_anon_orders_madvise))return true; -if (test_bit (PMD_ORDER , &huge_anon_orders_inherit) && +if (READ_ONCE (huge_anon_orders_inherit) &&hugepage_global_enabled ())return true;if (IS_ENABLED (CONFIG_SHMEM ) &&shmem_hpage_pmd_enabled ())
@@ -536,8 +536,14 @@ static unsigned int collapse_max_ptes_none(unsigned int order, bool full_scan)static unsigned long collapse_allowable_orders (struct vm_area_struct *vma,vm_flags_t vm_flags,bool is_khugepaged) { +unsigned long orders;enum tva_type tva_flags = is_khugepaged ?TVA_KHUGEPAGED :TVA_FORCED_COLLAPSE ; -unsigned long orders =BIT (HPAGE_PMD_ORDER ); + +/* If khugepaged is scanning an anonymous vma, allow mTHP collapse */ +if (is_khugepaged &&vma_is_anonymous (vma)) + orders =THP_ORDERS_ALL_ANON ; +else + orders =BIT (HPAGE_PMD_ORDER );return thp_vma_allowable_orders (vma, vm_flags, tva_flags, orders); }
@@ -546,7 +552,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, vm_flags_t vm_flags) {if (!mm_flags_test (MMF_VM_HUGEPAGE , vma->vm_mm) && -hugepage_pmd_enabled ()) { +hugepage_enabled ()) {if (collapse_allowable_orders (vma, vm_flags,/*is_khugepaged=*/ true))__khugepaged_enter (vma->vm_mm); }
@@ -2890,7 +2896,7 @@ static unsigned int collapse_scan_mm_slot(unsigned int pages, int *result,static int khugepaged_has_work (void ) { -return !list_empty (&khugepaged_scan.mm_head) &&hugepage_pmd_enabled (); +return !list_empty (&khugepaged_scan.mm_head) &&hugepage_enabled (); }static int khugepaged_wait_event (void )
@@ -2963,7 +2969,7 @@ static void khugepaged_wait_work(void)return ; } -if (hugepage_pmd_enabled ()) +if (hugepage_enabled ())wait_event_freezable (khugepaged_wait,khugepaged_wait_event ()); }
@@ -2994,7 +3000,7 @@ static void set_recommended_min_free_kbytes(void)int nr_zones =0 ;unsigned long recommended_min; -if (!hugepage_pmd_enabled ()) { +if (!hugepage_enabled ()) {calculate_min_free_kbytes (); goto update_wmarks; }
@@ -3044,7 +3050,7 @@ int start_stop_khugepaged(void)int err =0 ;mutex_lock (&khugepaged_mutex); -if (hugepage_pmd_enabled ()) { +if (hugepage_enabled ()) {if (!khugepaged_thread) khugepaged_thread =kthread_run (khugepaged,NULL ,"khugepaged" );
@@ -3070,7 +3076,7 @@ int start_stop_khugepaged(void)void khugepaged_min_free_kbytes_update (void ) {mutex_lock (&khugepaged_mutex); -if (hugepage_pmd_enabled () && khugepaged_thread) +if (hugepage_enabled () && khugepaged_thread)set_recommended_min_free_kbytes ();mutex_unlock (&khugepaged_mutex); }
Signature · 1 line
2.51.1
PATCH v13 16/16 Documentation: mm: update the admin guide for mTHP collapse
In reply to Nico PacheNow that we can collapse to mTHPs lets update the admin guide to reflect these changes and provide proper guidance on how to utilize it.
--- Documentation/admin-guide/mm/transhuge.rst | 48 +++++++++++++--------- 1 file changed, 28 insertions(+), 20 deletions(-)
Documentation/admin-guide/mm/transhuge.rst +28 -20
@@ -63,7 +63,8 @@ often. THP can be enabled system wide or restricted to certain tasks or even memory ranges inside task's address space. Unless THP is completely disabled, there is ``khugepaged`` daemon that scans memory and -collapses sequences of basic pages into PMD-sized huge pages. +collapses sequences of basic pages into huge pages of either PMD size +or mTHP sizes, if the system is configured to do so The THP behaviour is controlled via :ref:`sysfs <thp_sysfs>` interface and using madvise(2) and prctl(2) system calls.
@@ -219,10 +220,10 @@ this behaviour by writing 0 to shrink_underused, and enable it by writing echo 0 > /sys/kernel/mm/transparent_hugepage/shrink_underused echo 1 > /sys/kernel/mm/transparent_hugepage/shrink_underused -khugepaged will be automatically started when PMD-sized THP is enabled +khugepaged will be automatically started when any THP size is enabled (either of the per-size anon control or the top-level control are set to "always" or "madvise"), and it'll be automatically shutdown when -PMD-sized THP is disabled (when both the per-size anon control and the +all THP sizes are disabled (when both the per-size anon control and the top-level control are "never") process THP controls
@@ -264,11 +265,6 @@ support the following arguments:: Khugepaged controls ------------------- -.. note:: - khugepaged currently only searches for opportunities to collapse to - PMD-sized THP and no attempt is made to collapse to other THP - sizes. - khugepaged runs usually at low frequency so while one may not want to invoke defrag algorithms synchronously during the page faults, it should be worth invoking defrag at least in khugepaged. However it's
@@ -296,11 +292,11 @@ allocation failure to throttle the next allocation attempt:: The khugepaged progress can be seen in the number of pages collapsed (note that this counter may not be an exact count of the number of pages collapsed, since "collapsed" could mean multiple things: (1) A PTE mapping -being replaced by a PMD mapping, or (2) All 4K physical pages replaced by -one 2M hugepage. Each may happen independently, or together, depending on -the type of memory and the failures that occur. As such, this value should -be interpreted roughly as a sign of progress, and counters in /proc/vmstat -consulted for more accurate accounting):: +being replaced by a PMD mapping, or (2) physical pages replaced by one +hugepage of various sizes (PMD-sized or mTHP). Each may happen independently, +or together, depending on the type of memory and the failures that occur. +As such, this value should be interpreted roughly as a sign of progress, +and counters in /proc/vmstat consulted for more accurate accounting):: /sys/kernel/mm/transparent_hugepage/khugepaged/pages_collapsed
@@ -308,16 +304,19 @@ for each pass:: /sys/kernel/mm/transparent_hugepage/khugepaged/full_scans -``max_ptes_none`` specifies how many extra small pages (that are -not already mapped) can be allocated when collapsing a group -of small pages into one large page:: +``max_ptes_none`` specifies how many empty (none/zero) pages are allowed +when collapsing a group of small pages into one large page:: /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none -A higher value leads to use additional memory for programs. -A lower value leads to gain less thp performance. Value of -max_ptes_none can waste cpu time very little, you can -ignore it. +For PMD-sized THP collapse, this directly limits the number of empty pages +allowed in the 2MB region. For mTHP collapse, only 0 or (HPAGE_PMD_NR - 1) +are supported. Any other value will emit a warning and no mTHP collapse +will be attempted. + +A higher value allows more empty pages, potentially leading to more memory +usage but better THP performance. A lower value is more conservative and +may result in fewer THP collapses. ``max_ptes_swap`` specifies how many pages can be brought in from swap when collapsing a group of pages into a transparent huge page::
@@ -337,6 +336,15 @@ that THP is shared. Exceeding the number would block the collapse:: A higher value may increase memory footprint for some workloads. +.. note:: + For mTHP collapse, khugepaged does not support collapsing regions that + contain shared or swapped out pages, as this could lead to continuous + promotion to higher orders. The collapse will fail if any shared or + swapped PTEs are encountered during the scan. + + Currently, madvise_collapse only supports collapsing to PMD-sized THPs + and does not attempt mTHP collapses. + Boot parameters ===============
Signature · 1 line
2.51.1
Randy Dunlap
reviewerRe: [PATCH v13 mm-new 16/16] Documentation: mm: update the admin guide for mTHP collapse
Quoted 14 lines · Nico Pache
Now that we can collapse to mTHPs lets update the admin guide to reflect these changes and provide proper guidance on how to utilize it.
Reviewed-by Bagas Sanjaya <[email protected]>Signed-off-by Nico Pache <[email protected]>--- Documentation/admin-guide/mm/transhuge.rst | 48 +++++++++++++--------- 1 file changed, 28 insertions(+), 20 deletions(-)Documentation/admin-guide/mm/transhuge.rst +0
-0
Documentation/admin-guide/mm/transhuge.rst often. · line 67
Quoted 6 lines
Documentation/admin-guide/mm/transhuge.rst +2
-1@@ -63,7 +63,8 @@ often. THP can be enabled system wide or restricted to certain tasks or even memory ranges inside task's address space. Unless THP is completely disabled, there is ``khugepaged`` daemon that scans memory and -collapses sequences of basic pages into PMD-sized huge pages. +collapses sequences of basic pages into huge pages of either PMD size +or mTHP sizes, if the system is configured to do so
so.
Quoted 4 lines
Documentation/admin-guide/mm/transhuge.rst +0
-0The THP behaviour is controlled via :ref:`sysfs <thp_sysfs>` interface and using madvise(2) and prctl(2) system calls.
Documentation/admin-guide/mm/transhuge.rst this behaviour by writing 0 to shrink_underused, and enable it by writing · line 227
Quoted 10 lines
Documentation/admin-guide/mm/transhuge.rst +2
-2@@ -219,10 +220,10 @@ this behaviour by writing 0 to shrink_underused, and enable it by writing echo 0 > /sys/kernel/mm/transparent_hugepage/shrink_underused echo 1 > /sys/kernel/mm/transparent_hugepage/shrink_underused -khugepaged will be automatically started when PMD-sized THP is enabled +khugepaged will be automatically started when any THP size is enabled (either of the per-size anon control or the top-level control are set to "always" or "madvise"), and it'll be automatically shutdown when -PMD-sized THP is disabled (when both the per-size anon control and the +all THP sizes are disabled (when both the per-size anon control and the top-level control are "never")
"never").
Quoted 2 lines
Documentation/admin-guide/mm/transhuge.rst +0
-0process THP controls
[snip]
Signature · 1 line
~Randy
David Hildenbrand (Red Hat)
maintainerQuoted 21 lines · Nico Pache
The following series provides khugepaged with the capability to collapse anonymous memory regions to mTHPs.
To achieve this we generalize the khugepaged functions to no longer depend on
PMD_ORDER. Then during the PMD scan, we use a bitmap to track individual pages that are occupied (!none/zero). After the PMD scan is done, we use the bitmap to find the optimal mTHP sizes for the PMD range. The restriction onmax_ptes_noneis removed during the scan, to make sure we account for the whole PMD range in the bitmap. When no mTHP size is enabled, the legacy behavior of khugepaged is maintained.We currently only support
max_ptes_nonevalues of 0 orHPAGE_PMD_NR- 1 (ie 511). If any other value is specified, the kernel will emit a warning and no mTHP collapse will be attempted. If a mTHP collapse is attempted, but contains swapped out, or shared pages, we don't perform the collapse. It is now also possible to collapse to mTHPs without requiring the PMD THP size to be enabled. These limitiations are to prevent collapse "creep" behavior. This prevents constantly promoting mTHPs to the next available size, which would occur because a collapse introduces more non-zero pages that would satisfy the promotion condition on subsequent scans.
Quoted 2 lines
Patch 1: Refactor/rename hpage_collapse Patch 2: Refactoring to combine madvise_collapse and khugepaged
These two patches could get queued already separately, right?
Signature · 3 lines
Cheers David
One nit on this series - you sent it on the 1st day of the merge window :)
We unfortunately have a suboptimal approach to the merge window in mm in my view. Unfortunately it seems unlikely to change any time soon, so I would simply politely ask that in future esp. with big series whether you could send a respin within a cycle? :)
Sorry for the longer delay here btw, I've been on leave after that! Unfortunate timing.
Note that I am scaling down my review somewhat at the moment as I am simply disinclined to deal with the merge-by-default insanity mm engages in any longer, but since I promised to review this I will do so.
Cheers, Lorenzo
Quoted 112 lines · Nico Pache
The following series provides khugepaged with the capability to collapse anonymous memory regions to mTHPs.
To achieve this we generalize the khugepaged functions to no longer depend on
PMD_ORDER. Then during the PMD scan, we use a bitmap to track individual pages that are occupied (!none/zero). After the PMD scan is done, we use the bitmap to find the optimal mTHP sizes for the PMD range. The restriction onmax_ptes_noneis removed during the scan, to make sure we account for the whole PMD range in the bitmap. When no mTHP size is enabled, the legacy behavior of khugepaged is maintained.We currently only support
max_ptes_nonevalues of 0 orHPAGE_PMD_NR- 1 (ie 511). If any other value is specified, the kernel will emit a warning and no mTHP collapse will be attempted. If a mTHP collapse is attempted, but contains swapped out, or shared pages, we don't perform the collapse. It is now also possible to collapse to mTHPs without requiring the PMD THP size to be enabled. These limitiations are to prevent collapse "creep" behavior. This prevents constantly promoting mTHPs to the next available size, which would occur because a collapse introduces more non-zero pages that would satisfy the promotion condition on subsequent scans.Patch 1: Refactor/rename hpage_collapse Patch 2: Refactoring to combine madvise_collapse and khugepaged Patch 3-8: Generalize khugepaged functions for arbitrary orders and introduce some helper functions Patch 9: skip collapsing mTHP to smaller orders Patch 10-11: Add per-order mTHP statistics and tracepoints Patch 12: Introduce collapse_allowable_orders Patch 13-15: Introduce bitmap and mTHP collapse support, fully enabled Patch 16: Documentation---------
Testing
---------
- Built forx86_64, aarch64, ppc64le, and s390x
- selftests mm
- I created a test script that I used to push khugepaged to its limits while monitoring a number of stats and tracepoints. The code is available here[1] (Run in legacy mode for these changes and set mthp sizes to inherit)
The summary from my testings was that there was no significant regression noticed through this test. In some cases my changes had better collapse latencies, and was able to scan more pages in the same amount of time/work, but for the most part the results were consistent.
- redis testing. I tested these changes along with my defer changes (see followup [2] post for more details). We've decided to get the mTHP changes merged first before attempting the defer series.
- some basic testing on 64k page size.
- lots of general use.V13 Changes:
- Lots of minor nits, cleanups, comments, and renames
- Bitmap function simplification and more helpers (Wei, Lorenzo)
-Max_ptes_none(0 or 511) restriction
- commit description expansion
- list all reachable enum values inmthp_collapse()
- Fix ppc64 compile error due to usingHPAGE_PMD_ORDER(replace withilog2(MAX_PTRS_PER_PTE))V12: https://lore.kernel.org/lkml/[email protected]/
V11: https://lore.kernel.org/lkml/[email protected]/
V10: https://lore.kernel.org/lkml/[email protected]/
V9 : https://lore.kernel.org/lkml/[email protected]/
V8 : https://lore.kernel.org/lkml/[email protected]/
V7 : https://lore.kernel.org/lkml/[email protected]/
V6 : https://lore.kernel.org/lkml/[email protected]/
V5 : https://lore.kernel.org/lkml/[email protected]/
V4 : https://lore.kernel.org/lkml/[email protected]/
V3 : https://lore.kernel.org/lkml/[email protected]/
V2 : https://lore.kernel.org/lkml/[email protected]/
V1 : https://lore.kernel.org/lkml/[email protected]/A big thanks to everyone that has reviewed, tested, and participated in the development process. Its been a great experience working with all of you on this endeavour.
[1] - https://gitlab.com/npache/khugepaged_mthp_test
[2] - https://lore.kernel.org/lkml/[email protected]/Baolin Wang (1):
khugepaged: run khugepaged for all ordersDev Jain (1):
khugepaged: generalizealloc_charge_folio()Nico Pache (14):
khugepaged: rename hpage_collapse_* to collapse_* introducecollapse_single_pmdto unify khugepaged andmadvise_collapse
khugepaged: generalizehugepage_vma_revalidatefor mTHP support
khugepaged: introduceis_mthp_orderhelper
khugepaged: generalize __collapse_huge_page_* for mTHP support
khugepaged: introducecollapse_max_ptes_nonehelper function
khugepaged: generalizecollapse_huge_pagefor mTHP collapse
khugepaged: skip collapsing mTHP to smaller orders
khugepaged: add per-order mTHP collapse failure statistics
khugepaged: improve tracepoints for mTHP orders
khugepaged: introducecollapse_allowable_ordershelper function
khugepaged: Introduce mTHP collapse support
khugepaged: avoid unnecessary mTHP collapse attempts
Documentation: mm: update the admin guide for mTHP collapseDocumentation/admin-guide/mm/transhuge.rst | 80 ++- include/linux/huge_mm.h | 5 + include/trace/events/huge_memory.h | 34 +- mm/huge_memory.c | 11 + mm/khugepaged.c | 696 +++++++++++++++------ mm/mremap.c | 2 +- 6 files changed, 618 insertions(+), 210 deletions(-)--
2.51.1
(Sorry for multiple mails replying to same, lei/lore are broken again so my setup isn't working properly).
I tried to fixup the conflicts here to run tests locally but there's too many and I messed it up.
Could you please resend this series rebased on mm-unstable please?
Thanks, Lorenzo
Quoted 112 lines · Nico Pache
The following series provides khugepaged with the capability to collapse anonymous memory regions to mTHPs.
To achieve this we generalize the khugepaged functions to no longer depend on
PMD_ORDER. Then during the PMD scan, we use a bitmap to track individual pages that are occupied (!none/zero). After the PMD scan is done, we use the bitmap to find the optimal mTHP sizes for the PMD range. The restriction onmax_ptes_noneis removed during the scan, to make sure we account for the whole PMD range in the bitmap. When no mTHP size is enabled, the legacy behavior of khugepaged is maintained.We currently only support
max_ptes_nonevalues of 0 orHPAGE_PMD_NR- 1 (ie 511). If any other value is specified, the kernel will emit a warning and no mTHP collapse will be attempted. If a mTHP collapse is attempted, but contains swapped out, or shared pages, we don't perform the collapse. It is now also possible to collapse to mTHPs without requiring the PMD THP size to be enabled. These limitiations are to prevent collapse "creep" behavior. This prevents constantly promoting mTHPs to the next available size, which would occur because a collapse introduces more non-zero pages that would satisfy the promotion condition on subsequent scans.Patch 1: Refactor/rename hpage_collapse Patch 2: Refactoring to combine madvise_collapse and khugepaged Patch 3-8: Generalize khugepaged functions for arbitrary orders and introduce some helper functions Patch 9: skip collapsing mTHP to smaller orders Patch 10-11: Add per-order mTHP statistics and tracepoints Patch 12: Introduce collapse_allowable_orders Patch 13-15: Introduce bitmap and mTHP collapse support, fully enabled Patch 16: Documentation---------
Testing
---------
- Built forx86_64, aarch64, ppc64le, and s390x
- selftests mm
- I created a test script that I used to push khugepaged to its limits while monitoring a number of stats and tracepoints. The code is available here[1] (Run in legacy mode for these changes and set mthp sizes to inherit)
The summary from my testings was that there was no significant regression noticed through this test. In some cases my changes had better collapse latencies, and was able to scan more pages in the same amount of time/work, but for the most part the results were consistent.
- redis testing. I tested these changes along with my defer changes (see followup [2] post for more details). We've decided to get the mTHP changes merged first before attempting the defer series.
- some basic testing on 64k page size.
- lots of general use.V13 Changes:
- Lots of minor nits, cleanups, comments, and renames
- Bitmap function simplification and more helpers (Wei, Lorenzo)
-Max_ptes_none(0 or 511) restriction
- commit description expansion
- list all reachable enum values inmthp_collapse()
- Fix ppc64 compile error due to usingHPAGE_PMD_ORDER(replace withilog2(MAX_PTRS_PER_PTE))V12: https://lore.kernel.org/lkml/[email protected]/
V11: https://lore.kernel.org/lkml/[email protected]/
V10: https://lore.kernel.org/lkml/[email protected]/
V9 : https://lore.kernel.org/lkml/[email protected]/
V8 : https://lore.kernel.org/lkml/[email protected]/
V7 : https://lore.kernel.org/lkml/[email protected]/
V6 : https://lore.kernel.org/lkml/[email protected]/
V5 : https://lore.kernel.org/lkml/[email protected]/
V4 : https://lore.kernel.org/lkml/[email protected]/
V3 : https://lore.kernel.org/lkml/[email protected]/
V2 : https://lore.kernel.org/lkml/[email protected]/
V1 : https://lore.kernel.org/lkml/[email protected]/A big thanks to everyone that has reviewed, tested, and participated in the development process. Its been a great experience working with all of you on this endeavour.
[1] - https://gitlab.com/npache/khugepaged_mthp_test
[2] - https://lore.kernel.org/lkml/[email protected]/Baolin Wang (1):
khugepaged: run khugepaged for all ordersDev Jain (1):
khugepaged: generalizealloc_charge_folio()Nico Pache (14):
khugepaged: rename hpage_collapse_* to collapse_* introducecollapse_single_pmdto unify khugepaged andmadvise_collapse
khugepaged: generalizehugepage_vma_revalidatefor mTHP support
khugepaged: introduceis_mthp_orderhelper
khugepaged: generalize __collapse_huge_page_* for mTHP support
khugepaged: introducecollapse_max_ptes_nonehelper function
khugepaged: generalizecollapse_huge_pagefor mTHP collapse
khugepaged: skip collapsing mTHP to smaller orders
khugepaged: add per-order mTHP collapse failure statistics
khugepaged: improve tracepoints for mTHP orders
khugepaged: introducecollapse_allowable_ordershelper function
khugepaged: Introduce mTHP collapse support
khugepaged: avoid unnecessary mTHP collapse attempts
Documentation: mm: update the admin guide for mTHP collapseDocumentation/admin-guide/mm/transhuge.rst | 80 ++- include/linux/huge_mm.h | 5 + include/trace/events/huge_memory.h | 34 +- mm/huge_memory.c | 11 + mm/khugepaged.c | 696 +++++++++++++++------ mm/mremap.c | 2 +- 6 files changed, 618 insertions(+), 210 deletions(-)--
2.51.1
David Hildenbrand (Red Hat)
maintainerQuoted 7 lines · Lorenzo Stoakes
(Sorry for multiple mails replying to same, lei/lore are broken again so my setup isn't working properly).
I tried to fixup the conflicts here to run tests locally but there's too many and I messed it up.
Could you please resend this series rebased on mm-unstable please?
And maybe send out the two cleanups that can be applied earlier separately?
Signature · 3 lines
Cheers David
Quoted 11 lines · David Hildenbrand (Red Hat)
Quoted 7 lines · Lorenzo Stoakes
(Sorry for multiple mails replying to same, lei/lore are broken again so my setup isn't working properly).
I tried to fixup the conflicts here to run tests locally but there's too many and I messed it up.
Could you please resend this series rebased on mm-unstable please?
And maybe send out the two cleanups that can be applied earlier separately?
Yeah sounds sensible.
Quoted 5 lines
--
CheersDavid
Cheers, Lorenzo
Quoted 5 lines · David Hildenbrand (Red Hat)
Quoted 3 lines · Lorenzo Stoakes
(Sorry for multiple mails replying to same, lei/lore are broken again so my setup isn't working properly).
Quoted 7 lines
Quoted 4 lines
I tried to fixup the conflicts here to run tests locally but there's too many and I messed it up.
Could you please resend this series rebased on mm-unstable please?
And maybe send out the two cleanups that can be applied earlier separately?
Yeah I can do that. I'll just add a dependent line when I post the v14.
Quoted 6 lines
--
CheersDavid
Quoted 8 lines · Lorenzo Stoakes
(Sorry for multiple mails replying to same, lei/lore are broken again so my setup isn't working properly).
I tried to fixup the conflicts here to run tests locally but there's too many and I messed it up.
Could you please resend this series rebased on mm-unstable please?
Yes ofc! fixed all the conflicts yesterday, just finalizing my testing. Should be out tomorrow or Thursday.
Quoted 117 lines
Thanks, Lorenzo
Quoted 112 lines · Nico Pache
The following series provides khugepaged with the capability to collapse anonymous memory regions to mTHPs.
To achieve this we generalize the khugepaged functions to no longer depend on
PMD_ORDER. Then during the PMD scan, we use a bitmap to track individual pages that are occupied (!none/zero). After the PMD scan is done, we use the bitmap to find the optimal mTHP sizes for the PMD range. The restriction onmax_ptes_noneis removed during the scan, to make sure we account for the whole PMD range in the bitmap. When no mTHP size is enabled, the legacy behavior of khugepaged is maintained.We currently only support
max_ptes_nonevalues of 0 orHPAGE_PMD_NR- 1 (ie 511). If any other value is specified, the kernel will emit a warning and no mTHP collapse will be attempted. If a mTHP collapse is attempted, but contains swapped out, or shared pages, we don't perform the collapse. It is now also possible to collapse to mTHPs without requiring the PMD THP size to be enabled. These limitiations are to prevent collapse "creep" behavior. This prevents constantly promoting mTHPs to the next available size, which would occur because a collapse introduces more non-zero pages that would satisfy the promotion condition on subsequent scans.Patch 1: Refactor/rename hpage_collapse Patch 2: Refactoring to combine madvise_collapse and khugepaged Patch 3-8: Generalize khugepaged functions for arbitrary orders and introduce some helper functions Patch 9: skip collapsing mTHP to smaller orders Patch 10-11: Add per-order mTHP statistics and tracepoints Patch 12: Introduce collapse_allowable_orders Patch 13-15: Introduce bitmap and mTHP collapse support, fully enabled Patch 16: Documentation---------
Testing
---------
- Built forx86_64, aarch64, ppc64le, and s390x
- selftests mm
- I created a test script that I used to push khugepaged to its limits while monitoring a number of stats and tracepoints. The code is available here[1] (Run in legacy mode for these changes and set mthp sizes to inherit)
The summary from my testings was that there was no significant regression noticed through this test. In some cases my changes had better collapse latencies, and was able to scan more pages in the same amount of time/work, but for the most part the results were consistent.
- redis testing. I tested these changes along with my defer changes (see followup [2] post for more details). We've decided to get the mTHP changes merged first before attempting the defer series.
- some basic testing on 64k page size.
- lots of general use.V13 Changes:
- Lots of minor nits, cleanups, comments, and renames
- Bitmap function simplification and more helpers (Wei, Lorenzo)
-Max_ptes_none(0 or 511) restriction
- commit description expansion
- list all reachable enum values inmthp_collapse()
- Fix ppc64 compile error due to usingHPAGE_PMD_ORDER(replace withilog2(MAX_PTRS_PER_PTE))V12: https://lore.kernel.org/lkml/[email protected]/
V11: https://lore.kernel.org/lkml/[email protected]/
V10: https://lore.kernel.org/lkml/[email protected]/
V9 : https://lore.kernel.org/lkml/[email protected]/
V8 : https://lore.kernel.org/lkml/[email protected]/
V7 : https://lore.kernel.org/lkml/[email protected]/
V6 : https://lore.kernel.org/lkml/[email protected]/
V5 : https://lore.kernel.org/lkml/[email protected]/
V4 : https://lore.kernel.org/lkml/[email protected]/
V3 : https://lore.kernel.org/lkml/[email protected]/
V2 : https://lore.kernel.org/lkml/[email protected]/
V1 : https://lore.kernel.org/lkml/[email protected]/A big thanks to everyone that has reviewed, tested, and participated in the development process. Its been a great experience working with all of you on this endeavour.
[1] - https://gitlab.com/npache/khugepaged_mthp_test
[2] - https://lore.kernel.org/lkml/[email protected]/Baolin Wang (1):
khugepaged: run khugepaged for all ordersDev Jain (1):
khugepaged: generalizealloc_charge_folio()Nico Pache (14):
khugepaged: rename hpage_collapse_* to collapse_* introducecollapse_single_pmdto unify khugepaged andmadvise_collapse
khugepaged: generalizehugepage_vma_revalidatefor mTHP support
khugepaged: introduceis_mthp_orderhelper
khugepaged: generalize __collapse_huge_page_* for mTHP support
khugepaged: introducecollapse_max_ptes_nonehelper function
khugepaged: generalizecollapse_huge_pagefor mTHP collapse
khugepaged: skip collapsing mTHP to smaller orders
khugepaged: add per-order mTHP collapse failure statistics
khugepaged: improve tracepoints for mTHP orders
khugepaged: introducecollapse_allowable_ordershelper function
khugepaged: Introduce mTHP collapse support
khugepaged: avoid unnecessary mTHP collapse attempts
Documentation: mm: update the admin guide for mTHP collapseDocumentation/admin-guide/mm/transhuge.rst | 80 ++- include/linux/huge_mm.h | 5 + include/trace/events/huge_memory.h | 34 +- mm/huge_memory.c | 11 + mm/khugepaged.c | 696 +++++++++++++++------ mm/mremap.c | 2 +- 6 files changed, 618 insertions(+), 210 deletions(-)--
2.51.1