PAGEMAP_SCAN with PM_SCAN_WP_MATCHING write-protects a huge PMD through make_uffd_wp_pmd(), which handles a present THP and a migration entry but does nothing for a device-private THP. The PMD never gets the uffd bit. pagemap_thp_category() reports a non-present PMD without the uffd bit as written. A user tracking writes with async uffd-wp therefore sees every page of a device-private THP as written on every scan, whether it was written to or not. This requires a driver that migrates THPs to device memory. A device-private PMD must be write-protected like any other huge PMD. To fix it, pass any valid PMD softleaf entry to set_uffd_wp_softleaf_pmd(), not only a migration entry. It downgrades a writable entry and sets the uffd bit. When the THP migrates back, set_pmd_migration_entry() carries the uffd bit over to the migration entry, so the write protection is kept. Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages") Tested-by: Gregory Price (Meta) Acked-by: David Hildenbrand (Arm) Signed-off-by: Tal Zussman --- fs/proc/task_mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 0127bf9c3f77..15cb65c3ab94 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -2510,7 +2510,7 @@ static void make_uffd_wp_pmd(struct vm_area_struct *vma, old = pmdp_invalidate_ad(vma, addr, pmdp); pmd = pmd_mkuffd(old); set_pmd_at(vma->vm_mm, addr, pmdp, pmd); - } else if (pmd_is_migration_entry(pmd)) { + } else if (pmd_is_valid_softleaf(pmd)) { pmd = set_uffd_wp_softleaf_pmd(pmd); set_pmd_at(vma->vm_mm, addr, pmdp, pmd); } -- 2.39.5