Re: [PATCH v4 5/7] mm/khugepaged: Refactor the PTE state checks into a helper
From: David Hildenbrand (Arm)
Date: Wed Aug 12 2026 - 05:58:49 EST
> Huh, that looks odd.
>
> That should just be a VM_WARN_ON_FOLIO(true, ..) or sth like that.
>
> But in collapse_scan_pmd() that warning never existed? So this raises eyebrows.
>
> [...]
>
> I'll play with it to see if we can do better and will reply here later.
Okay, I think below is what we should do.
There is one behavioral change: we now longer trace the last folio, which is the right
thing to do IMHO.
And I think there is one fix we should pull out and evaluate first: A zeropage with a
uffd-wp marker is not checked properly, IIUC.
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 5a06e3942e889..8a223659fce2d 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -119,6 +119,18 @@ struct collapse_control {
DECLARE_BITMAP(mthp_present_ptes, MAX_PTRS_PER_PTE);
};
+struct collapse_anon_pte_check_ctx {
+ struct collapse_control *cc;
+ struct vm_area_struct *vma;
+ unsigned int order;
+ int none_or_zero_ptes;
+ int nonpresent_ptes;
+ int shared_ptes;
+ unsigned int max_ptes_none;
+ int max_ptes_swap;
+ unsigned int max_ptes_shared;
+};
+
/**
* struct khugepaged_scan - cursor for scanning
* @mm_head: the head of the mm list to scan
@@ -696,74 +708,104 @@ static void count_collapse_event(unsigned int order, enum vm_event_item vm_event
count_mthp_stat(order, mthp_event);
}
+static enum scan_result collapse_anon_pte_check(pte_t pteval,
+ unsigned long addr, struct collapse_anon_pte_check_ctx *ctx,
+ struct folio **foliop)
+{
+ *foliop = NULL;
+
+ /*
+ * Don't collapse if any of the small PTEs are armed with uffd
+ * write protection. Marking the new huge pmd as write protected
+ * could bring userfault messages that fall outside of the
+ * registered range.
+ */
+ if ((pte_present(pteval) && pte_uffd(pteval)) ||
+ (!pte_present(pteval) && pte_swp_uffd_any(pteval)))
+ return SCAN_PTE_UFFD;
+
+ if (pte_none_or_zero(pteval)) {
+ if (++ctx->none_or_zero_ptes > ctx->max_ptes_none) {
+ count_collapse_event(ctx->order, THP_SCAN_EXCEED_NONE_PTE,
+ MTHP_STAT_COLLAPSE_EXCEED_NONE);
+ return SCAN_EXCEED_NONE_PTE;
+ }
+ return SCAN_SUCCEED;
+ }
+ if (!pte_present(pteval)) {
+ if (ctx->max_ptes_swap < 0)
+ return SCAN_PTE_NON_PRESENT;
+ if (++ctx->nonpresent_ptes > ctx->max_ptes_swap) {
+ count_collapse_event(ctx->order, THP_SCAN_EXCEED_SWAP_PTE,
+ MTHP_STAT_COLLAPSE_EXCEED_SWAP);
+ return SCAN_EXCEED_SWAP_PTE;
+ }
+ return SCAN_SUCCEED;
+ }
+
+ *foliop = vm_normal_folio(ctx->vma, addr, pteval);
+ if (unlikely(!*foliop) || unlikely(folio_is_zone_device(*foliop)))
+ return SCAN_PAGE_NULL;
+
+ /*
+ * If the vma has the VM_DROPPABLE flag, the collapse will
+ * preserve the lazyfree property without needing to skip.
+ */
+ if (ctx->cc->is_khugepaged && !(ctx->vma->vm_flags & VM_DROPPABLE) &&
+ folio_test_lazyfree(*foliop) && !pte_dirty(pteval))
+ return SCAN_PAGE_LAZYFREE;
+
+ if (!folio_test_anon(*foliop))
+ return SCAN_PAGE_ANON;
+
+ if (folio_maybe_mapped_shared(*foliop)) {
+ /*
+ * 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 (++ctx->shared_ptes > ctx->max_ptes_shared) {
+ count_collapse_event(ctx->order, THP_SCAN_EXCEED_SHARED_PTE,
+ MTHP_STAT_COLLAPSE_EXCEED_SHARED);
+ return SCAN_EXCEED_SHARED_PTE;
+ }
+ }
+
+ return SCAN_SUCCEED;
+}
+
static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
unsigned long start_addr, pte_t *pte, struct collapse_control *cc,
unsigned int order, struct list_head *compound_pagelist)
{
- const unsigned int max_ptes_none = collapse_max_ptes_none(cc, vma, order);
- const unsigned int max_ptes_shared = collapse_max_ptes_shared(cc, order);
const unsigned long nr_pages = 1UL << order;
- struct page *page = NULL;
struct folio *folio = NULL;
unsigned long addr = start_addr;
- pte_t *_pte;
- int none_or_zero = 0, shared = 0, referenced = 0;
+ pte_t *_pte, pteval;
+ int referenced = 0;
enum scan_result result = SCAN_FAIL;
+ struct collapse_anon_pte_check_ctx ctx = {
+ .cc = cc,
+ .vma = vma,
+ .order = order,
+ .max_ptes_none = collapse_max_ptes_none(cc, vma, order),
+ .max_ptes_swap = -1, /* Don't tolerate any non-present ptes. */
+ .max_ptes_shared = collapse_max_ptes_shared(cc, order),
+ };
for (_pte = pte; _pte < pte + nr_pages;
_pte++, addr += PAGE_SIZE) {
- pte_t pteval = ptep_get(_pte);
- if (pte_none_or_zero(pteval)) {
- if (++none_or_zero > max_ptes_none) {
- result = SCAN_EXCEED_NONE_PTE;
- count_collapse_event(order, THP_SCAN_EXCEED_NONE_PTE,
- MTHP_STAT_COLLAPSE_EXCEED_NONE);
- goto out;
- }
- continue;
- }
- if (!pte_present(pteval)) {
- result = SCAN_PTE_NON_PRESENT;
- goto out;
- }
- if (pte_uffd(pteval)) {
- result = SCAN_PTE_UFFD;
- goto out;
- }
- page = vm_normal_page(vma, addr, pteval);
- if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
- result = SCAN_PAGE_NULL;
- goto out;
- }
-
- folio = page_folio(page);
- VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
+ pteval = ptep_get(_pte);
- /*
- * If the vma has the VM_DROPPABLE flag, the collapse will
- * preserve the lazyfree property without needing to skip.
- */
- if (cc->is_khugepaged && !(vma->vm_flags & VM_DROPPABLE) &&
- folio_test_lazyfree(folio) && !pte_dirty(pteval)) {
- result = SCAN_PAGE_LAZYFREE;
+ result = collapse_anon_pte_check(pteval, addr, &ctx, &folio);
+ if (result != SCAN_SUCCEED) {
+ VM_WARN_ON_ONCE(result == SCAN_PAGE_ANON);
goto out;
}
+ if (!folio)
+ continue;
- /* See collapse_scan_pmd(). */
- if (folio_maybe_mapped_shared(folio)) {
- /*
- * 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 (++shared > max_ptes_shared) {
- result = SCAN_EXCEED_SHARED_PTE;
- count_collapse_event(order, THP_SCAN_EXCEED_SHARED_PTE,
- MTHP_STAT_COLLAPSE_EXCEED_SHARED);
- goto out;
- }
- }
/*
* TODO: In some cases of partially-mapped folios, we'd actually
* want to collapse.
@@ -841,13 +883,13 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
result = SCAN_LACK_REFERENCED_PAGE;
} else {
result = SCAN_SUCCEED;
- trace_mm_collapse_huge_page_isolate(folio, none_or_zero,
+ trace_mm_collapse_huge_page_isolate(folio, ctx.none_or_zero_ptes,
referenced, result, order);
return result;
}
out:
release_pte_pages(pte, _pte, compound_pagelist);
- trace_mm_collapse_huge_page_isolate(folio, none_or_zero,
+ trace_mm_collapse_huge_page_isolate(folio, ctx.none_or_zero_ptes,
referenced, result, order);
return result;
}
@@ -1613,21 +1655,25 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr,
bool *lock_dropped, struct collapse_control *cc)
{
- const unsigned int max_ptes_shared = collapse_max_ptes_shared(cc, HPAGE_PMD_ORDER);
- const unsigned int max_ptes_swap = collapse_max_ptes_swap(cc, HPAGE_PMD_ORDER);
- unsigned int max_ptes_none = collapse_max_ptes_none(cc, vma, HPAGE_PMD_ORDER);
enum tva_type tva_flags = cc->is_khugepaged ? TVA_KHUGEPAGED : TVA_FORCED_COLLAPSE;
pmd_t *pmd;
pte_t *pte, *_pte, pteval;
int i;
- int none_or_zero = 0, shared = 0, referenced = 0;
enum scan_result result = SCAN_FAIL;
- struct page *page = NULL;
struct folio *folio = NULL;
+ int referenced = 0;
unsigned long addr;
unsigned long enabled_orders;
spinlock_t *ptl;
- int node = NUMA_NO_NODE, unmapped = 0;
+ int node = NUMA_NO_NODE;
+ struct collapse_anon_pte_check_ctx ctx = {
+ .cc = cc,
+ .vma = vma,
+ .order = HPAGE_PMD_ORDER,
+ .max_ptes_none = collapse_max_ptes_none(cc, vma, HPAGE_PMD_ORDER),
+ .max_ptes_swap = collapse_max_ptes_swap(cc, HPAGE_PMD_ORDER),
+ .max_ptes_shared = collapse_max_ptes_shared(cc, HPAGE_PMD_ORDER),
+ };
VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
@@ -1647,7 +1693,7 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
* is then checked again in mthp_collapse() for each attempted order.
*/
if (enabled_orders != BIT(HPAGE_PMD_ORDER))
- max_ptes_none = KHUGEPAGED_MAX_PTES_LIMIT;
+ ctx.max_ptes_none = KHUGEPAGED_MAX_PTES_LIMIT;
pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
if (!pte) {
@@ -1663,81 +1709,11 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
cc->progress++;
- if (pte_none_or_zero(pteval)) {
- if (++none_or_zero > max_ptes_none) {
- result = SCAN_EXCEED_NONE_PTE;
- count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_NONE_PTE,
- MTHP_STAT_COLLAPSE_EXCEED_NONE);
- goto out_unmap;
- }
- continue;
- }
- if (!pte_present(pteval)) {
- if (++unmapped > max_ptes_swap) {
- result = SCAN_EXCEED_SWAP_PTE;
- count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SWAP_PTE,
- MTHP_STAT_COLLAPSE_EXCEED_SWAP);
- goto out_unmap;
- }
- /*
- * Always be strict with uffd-wp
- * enabled swap entries. Please see
- * comment below for pte_uffd().
- */
- if (pte_swp_uffd_any(pteval)) {
- result = SCAN_PTE_UFFD;
- goto out_unmap;
- }
- continue;
- }
- if (pte_uffd(pteval)) {
- /*
- * Don't collapse the page if any of the small
- * PTEs are armed with uffd write protection.
- * Here we can also mark the new huge pmd as
- * write protected if any of the small ones is
- * marked but that could bring unknown
- * userfault messages that falls outside of
- * the registered range. So, just be simple.
- */
- result = SCAN_PTE_UFFD;
- goto out_unmap;
- }
-
- page = vm_normal_page(vma, addr, pteval);
- if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
- result = SCAN_PAGE_NULL;
- goto out_unmap;
- }
- folio = page_folio(page);
-
- /*
- * If the vma has the VM_DROPPABLE flag, the collapse will
- * preserve the lazyfree property without needing to skip.
- */
- if (cc->is_khugepaged && !(vma->vm_flags & VM_DROPPABLE) &&
- folio_test_lazyfree(folio) && !pte_dirty(pteval)) {
- result = SCAN_PAGE_LAZYFREE;
- goto out_unmap;
- }
-
- if (!folio_test_anon(folio)) {
- result = SCAN_PAGE_ANON;
+ result = collapse_anon_pte_check(pteval, addr, &ctx, &folio);
+ if (result != SCAN_SUCCEED)
goto out_unmap;
- }
-
- /*
- * We treat a single page as shared if any part of the THP
- * is shared.
- */
- if (folio_maybe_mapped_shared(folio)) {
- if (++shared > max_ptes_shared) {
- result = SCAN_EXCEED_SHARED_PTE;
- count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SHARED_PTE,
- MTHP_STAT_COLLAPSE_EXCEED_SHARED);
- goto out_unmap;
- }
- }
+ if (!folio)
+ continue;
/* Set bit for occupied pages */
__set_bit(i, cc->mthp_present_ptes);
@@ -1780,7 +1756,7 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
}
if (cc->is_khugepaged &&
(!referenced ||
- (unmapped && referenced < HPAGE_PMD_NR / 2))) {
+ (ctx.nonpresent_ptes && referenced < HPAGE_PMD_NR / 2))) {
result = SCAN_LACK_REFERENCED_PAGE;
} else {
result = SCAN_SUCCEED;
@@ -1791,13 +1767,13 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
/* collapse_huge_page() expects the lock to be dropped before calling */
mmap_read_unlock(mm);
result = mthp_collapse(mm, start_addr, referenced,
- unmapped, cc, enabled_orders);
+ ctx.nonpresent_ptes, cc, enabled_orders);
/* mmap_lock was released above, set lock_dropped */
*lock_dropped = true;
}
out:
- trace_mm_khugepaged_scan_pmd(mm, folio, referenced,
- none_or_zero, result, unmapped);
+ trace_mm_khugepaged_scan_pmd(mm, folio, referenced, ctx.none_or_zero_ptes,
+ result, ctx.nonpresent_ptes);
return result;
}
--
2.43.0
--
Cheers,
David