Re: [PATCH 07/32] mm/khugepaged: check the VMA before walking page tables when collapsing

From: David Hildenbrand (Arm)

Date: Wed Dec 02 2026 - 10:24:24 EST


On 12/2/26 06:18, Dev Jain wrote:
>
>
> 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.

Yes, quite scary ... :)

--
Cheers,

David