Re: [PATCH 08/12] mm/migrate_device: do not migrate a uffd-protected zero page
From: Tal Zussman
Date: Mon Nov 16 2026 - 14:16:56 EST
On 11/15/26 7:11 PM, Alistair Popple wrote:
> On 2026-11-14 at 09:26 +1100, Tal Zussman <tz2294@xxxxxxxxxxxx> wrote...
>> On 11/13/26 1:04 AM, Gregory Price (Meta) wrote:
>> > On 2026-11-11 12:55 -0500, Tal Zussman wrote:
>> >> migrate_vma_setup() installs a migration entry for each page it
>> >> collects, which carries the uffd bit over to the new PTE. However, a PTE
>> >> that maps the zero page is left in place without a migration entry.
>> >> migrate_vma_insert_page() later overwrites it with a new PTE that is
>> >> built only from the VMA flags, so the uffd-wp bit or the RWP PAGE_NONE
>> >> protection of the old PTE is lost. The same applies to the huge zero
>> >> PMD.
>> >>
>> >> Once a driver has migrated such a zero page, writes and accesses to it
>> >> no longer fault into userfaultfd, so the manager misses them. With async
>> >> tracking, PAGEMAP_SCAN unconditionally reports the page as written or
>> >> accessed.
>> >>
>> >> This requires a driver that migrates memory to a device.
>> >>
>> >> To fix it, do not mark a zero page for migration if its entry has the
>> >> uffd bit set in a VMA registered for WP or RWP. The driver sees an
>> >> address that was not migrated, which drivers already have to deal with.
>> >>
>> >> The uffd bit can also be set after the zero page was collected, as
>> >> write-protecting only takes the mmap lock for read. So check again under
>> >> the page table lock in migrate_vma_insert_page() and
>> >> migrate_vma_insert_huge_pmd_page().
>> >>
>> >
>> > Similar to last patch, this changelog could be improved. My best
>> > attempt at summarizing:
>> >
>> > """
>> > Device migration replaces a zero-page mapping with a fresh entry,
>> > losing its userfaultfd WP or RWP protection.
>> >
>> > Don't migrate the uffd-protected zero pages.
>> >
>> > Skip protected zero pages during collection. Check again under the
>> > page-table lock before insertion, because another thread can apply
>> > protection after collection finishes.
>> >
>> > The alternative ...
>> > """
>> >
>>
>> Thanks, I'll trim it along these lines.
>>
>> > I have concerns with this:
>> >
>> >> The alternative would be to carry the uffd state over in
>> >> migrate_vma_insert_page() and migrate_vma_insert_huge_pmd_page(). Not
>> >> migrating the zero page is simpler, and a zero page that is protected by
>> >> userfaultfd in a range that a driver migrates should be rare.
>> >
>> > This basically force-fails a migration of a uffd protected zero page.
>> >
>> > Wouldn't we expect this to cause drivers to fail to make progress if
>> > they require that page to be resident on the device? Not sure we want
>> > to force drivers to try to handle this special case (if they even can).
>> >
>> > We'll need some input from the migrate_device/GPU folks.
>
> Thanks, I was halfway through responding last week but Microslop had other
> ideas.
>
> Failing the migration is fine - drivers can never *require* a page to be
> resident on the device, as migration can always fail for a host of reasons.
> In these cases drivers should fallback to remote mapping pages (eg. using
> hmm_range_fault()).
>
> Now if userfaultfd+device memory was a commonly used feature then it would
> be worth fixing this as always failing would have a large performance impact.
> However I've never tested uffd+device memory and have never been confident the
> implementation was correct anyway (both features were merged around the same
> time). So I think the simple fix is fine:
>
> Reviewed-by: Alistair Popple <apopple@xxxxxxxxxx>
>
Thanks! In that case I'll leave this in with a trimmed commit message and
add some more GPU people to Cc.
>> Yeah, makes sense. I can implement the alternative approach, but testing
>> for this is a little tricky without the right devices... I might drop this
>> patch and send it separately with the GPU folks on Cc.
>
> It probably is worth copying other GPU folk as well though in case they have
> different use-cases. Matthew Brost (Intel) has done a lot of work in this area
> in DRM, but I notice he is already on CC.
>
> - Alistair
>
>> > --
>> > ~Gregory
>> >
>>
>