Re: [PATCH v2 09/13] mm/migrate_device: fix use-after-free of the anon rmap

From: David Hildenbrand (Arm)

Date: Wed Nov 18 2026 - 10:34:22 EST


On 11/13/26 22:45, Lorenzo Stoakes (ARM) wrote:
> A munmap() of anonymous memory can result in a use-after-free of the
> anonymous rmap if a concurrent device memory migration occurs and the
> migration runs without the mmap_lock held.
>
> This can occur for a long-term pin of device-coherent memory, or a driver
> evicting its device memory by PFN.
>
> Consider:
>
> eviction munmap()
> ======== ========
> migrate_device_range()
> migrate_device_unmap()
> try_to_migrate()
> zap the migration entry
> unlink_anon_vmas()
> last VMA: free the anon_vma
> migrate_device_finalize()
> remove_migration_ptes()
> /* locks and walks the freed anon_vma */
>
> If munmap() runs before try_to_migrate() instead, the folio is no longer
> mapped and gets no migration entry. The walk still runs, and follows the
> stale folio->mapping to the freed anon_vma.
>
> Resolve this by pinning the anon rmap of each folio which possesses one in
> migrate_device_range(), migrate_device_pfns() and
> migrate_device_coherent_folio() before its PTEs are replaced, and
> unpinning it once the walk is done.
>
> A folio which is no longer mapped cannot be pinned and has no migration
> entries to remove, so skip it rather than migrating it.
>
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Fixes: e778406b40db ("mm/migrate_device.c: add migrate_device_range()")
> Cc: stable@xxxxxxxxxxxxxxx
> Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
> Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> ---

Great, I have to wash my eyes now. :)

Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

--
Cheers,

David