Re: [PATCH v2 8/9] mm/migrate_device: do not migrate a uffd-protected zero page

From: Tal Zussman

Date: Mon Nov 16 2026 - 18:07:15 EST


On 11/16/26 3:45 PM, David Hildenbrand (Arm) wrote:
> On 11/16/26 21:02, Tal Zussman wrote:
>> Device migration replaces a PTE that maps the zero page with a new PTE
>> that is built only from the VMA flags, so its userfaultfd WP or RWP
>> protection is lost. The same applies to the huge zero PMD. Writes and
>> accesses to the page are then no longer reported to userfaultfd.
>>
>> To fix it, do not migrate a uffd-protected zero page. Skip it during
>> collection, and check again under the page table lock before inserting,
>> as another thread can protect the page after it was collected.
>>
>> The alternative would be to carry the uffd state over to the new entry,
>> but a uffd-protected zero page in a range that a driver migrates should
>> be rare.
>>
>> Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit")
>> Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
>> Cc: Thomas Hellström <thomas.hellstrom@xxxxxxxxxxxxxxx>
>> Cc: Lyude Paul <lyude@xxxxxxxxxx>
>> Cc: Danilo Krummrich <dakr@xxxxxxxxxx>
>> Cc: Felix Kuehling <Felix.Kuehling@xxxxxxx>
>> Cc: Jason Gunthorpe <jgg@xxxxxxxx>
>> Cc: Leon Romanovsky <leon@xxxxxxxxxx>
>> Cc: dri-devel@xxxxxxxxxxxxxxxxxxxxx
>> Cc: intel-xe@xxxxxxxxxxxxxxxxxxxxx
>> Cc: nouveau@xxxxxxxxxxxxxxxxxxxxx
>> Cc: amd-gfx@xxxxxxxxxxxxxxxxxxxxx
>> Reviewed-by: Alistair Popple <apopple@xxxxxxxxxx>
>> Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>
>> ---
>> mm/migrate_device.c | 23 +++++++++++++++++++++--
>> 1 file changed, 21 insertions(+), 2 deletions(-)
>>
>> diff --git a/mm/migrate_device.c b/mm/migrate_device.c
>> index 9e19fadfa65c..140b99f72508 100644
>> --- a/mm/migrate_device.c
>> +++ b/mm/migrate_device.c
>> @@ -159,7 +159,16 @@ static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,
>>
>> folio = pmd_folio(*pmdp);
>> if (is_huge_zero_folio(folio)) {
>> + /*
>> + * Nothing carries the uffd state of the zero page
>> + * over, so skip it if its uffd bit is set.
>> + */
>> + bool protected = userfaultfd_protected(walk->vma) &&
>> + pmd_uffd(*pmdp);
>
> Now that I read it 3 times in a row below, these needs a helper.
>
> userfaultfd_pte_protected() ? userfaultfd_pte?
>
> That might help you to get rid of the local variable (that should have been
> called something that includes uffd and should have been const)
>
>
> Maybe just add a simple helper for the pmd as well and use it below.
>

Will do. I'll do userfaultfd_pte_protected() and userfaultfd_huge_pmd_protected()
to match the existing wp and rwp helpers.

Re: the local variable, I'll rid of it and just call spin_unlock() in
both branches, which is a little ugly.

>
> --
> Cheers,
>
> David
>