Re: [PATCH 07/32] mm/khugepaged: check the VMA before walking page tables when collapsing
From: Dev Jain
Date: Wed Dec 02 2026 - 00:19:09 EST
On 29/11/26 3:36 pm, David Hildenbrand (Arm) wrote:
> MADV_COLLAPSE and khugepaged can end up traversing page tables of a VMA
> through find_pmd_or_thp_or_none() before verifying that the VMA is still
> as expected.
>
> Calling find_pmd_or_thp_or_none() on a mapping with PUDs
> (hugetlb/dax/PFNMAP) is invalid, as it treats every present PUD entry as
> a pointer to a PMD table.
>
> Consequently, we can end up interpreting the memory mapped by a PUD leaf
> as a PMD table.
>
> Let's fix it by performing the thp_vma_allowable_order() check before
> the page table walk.
>
> Further, let's also make find_pmd_or_thp_or_none() harder to get wrong
> by rejecting any pgd/p4d/pud leaves.
>
> This has high urgency, as unprivileged users can trigger it, although
> getting relevant PUD mappings (e.g., hugetlb) should be rather uncommon
> in most setups.
>
> Fixes: 58ac9a8993a1 ("mm/khugepaged: attempt to map file/shmem-backed pte-mapped THPs by pmds")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
So for retract_page_tables(), when we use the struct address_space to traverse
the VMAs, we cannot get a hugetlb or DAX VMA.
But for the case described here, vma_lookup() can perhaps get a completely new
VMA.
Reviewed-by: Dev Jain <dev.jain@xxxxxxx>
> mm/khugepaged.c | 10 +++++-----
> mm/rmap.c | 6 +++---
> 2 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 93d4a6f159e81..b133c378dac72 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -1894,11 +1894,6 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
> !range_in_vma(vma, haddr, haddr + HPAGE_PMD_SIZE))
> return SCAN_VMA_CHECK;
>
> - /* Fast check before locking page if already PMD-mapped */
> - result = find_pmd_or_thp_or_none(mm, haddr, &pmd);
> - if (result == SCAN_PMD_MAPPED)
> - return result;
> -
> /*
> * If we are here, we've succeeded in replacing all the native pages
> * in the page cache with a single hugepage. If a mm were to fault-in
> @@ -1909,6 +1904,11 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
> if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
> return SCAN_VMA_CHECK;
>
> + /* Quick check if already PMD-mapped before locking the folio. */
> + result = find_pmd_or_thp_or_none(mm, haddr, &pmd);
> + if (result == SCAN_PMD_MAPPED)
> + return result;
> +
> /*
> * Keep pmd pgtable while the uffd bit is in use; see comment in
> * retract_page_tables().
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 81a97b94ca4b9..e7e2a0df1780b 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -883,15 +883,15 @@ pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address)
> pmd_t *pmd = NULL;
>
> pgd = pgd_offset(mm, address);
> - if (!pgd_present(*pgd))
> + if (!pgd_present(*pgd) || pgd_leaf(*pgd))
> goto out;
>
> p4d = p4d_offset(pgd, address);
> - if (!p4d_present(*p4d))
> + if (!p4d_present(*p4d) || p4d_leaf(*p4d))
> goto out;
>
> pud = pud_offset(p4d, address);
> - if (!pud_present(*pud))
> + if (!pud_present(*pud) || pud_leaf(*pud))
> goto out;
>
> pmd = pmd_offset(pud, address);
>