Re: [PATCH v2 7/9] mm/mremap: resolve uffd state when the context is dropped, not during the move
From: Tal Zussman
Date: Mon Nov 16 2026 - 18:00:55 EST
On 11/16/26 3:41 PM, David Hildenbrand (Arm) wrote:
> On 11/16/26 21:02, Tal Zussman wrote:
>> Without UFFD_FEATURE_EVENT_REMAP, the new VMA of an mremap() is not
>> registered with userfaultfd, so the uffd state of its entries is cleared
>> while they are moved. If the move fails halfway, the entries are moved
>> back into the old VMA, which is still registered, without their uffd
>> state. Writes and accesses to them are then no longer reported.
>>
>> To fix it, leave the entries alone during the move and clear the uffd
>> state of the new VMA in mremap_userfaultfd_prep(), right before dropping
>> the context, like unregistering does. This walks the page tables of the
>> new VMA a second time.
>>
>> This is done with userfaultfd_clear_ptes(), which also makes the PTEs
>> writable again where that is safe. That removes a spurious write fault
>> per protected page on private anonymous memory, but adds spurious write
>> faults on shmem and hugetlb, as change_protection() makes writable pages
>> that were never protected read-only. UFFDIO_UNREGISTER and
>> UFFDIO_WRITEPROTECT already behave this way. This will be fixed in
>> change_protection() separately.
>>
>> The entries no longer need per-entry handling during the move, so drop
>> uffd_supports_page_table_move() and clear_uffd_wp_pmd(). Whole page
>> tables of these VMAs can be moved again.
>>
>> Fixes: 0cef0bb836e3 ("mm: clear uffd-wp PTE/PMD state on mremap()")
>> Fixes: 763f43865fc4 ("mm: preserve RWP marker across PTE rewrites")
>> Reviewed-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
>> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
>> Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
>> Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
>> Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>
>> ---
>
> VMAs should be write-locked, so nobody should stumble over it and walk page tables.
>
> Rmap walkers can stumble over entries, because only the page table locks protect
> us here. With your change, that's now actually consistent while doing the move.
>
> I assume there is now a short period in time where the PTE bits don't match the
> VMA flags for an RMAP point of view, essentially in-between clearing uffd state
> from the VMA and clearing uffd-wp bits.
>
Yep, there's a brief window where the VMA is registered but PTEs are clear,
but that's fine. That's the same state as a registered but not-yet-protected
VMA.
> Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>
> --
> Cheers,
>
> David
>