Skip to content

Commit 24bf08c

Browse files
davidhildenbrandakpm00
authored andcommitted
mm/userfaultfd: fix uffd-wp handling for THP migration entries
Looks like what we fixed for hugetlb in commit 44f8639 ("mm/hugetlb: fix uffd-wp handling for migration entries in hugetlb_change_protection()") similarly applies to THP. Setting/clearing uffd-wp on THP migration entries is not implemented properly. Further, while removing migration PMDs considers the uffd-wp bit, inserting migration PMDs does not consider the uffd-wp bit. We have to set/clear independently of the migration entry type in change_huge_pmd() and properly copy the uffd-wp bit in set_pmd_migration_entry(). Verified using a simple reproducer that triggers migration of a THP, that the set_pmd_migration_entry() no longer loses the uffd-wp bit. Link: https://lkml.kernel.org/r/[email protected] Fixes: f45ec5f ("userfaultfd: wp: support swap and page migration") Signed-off-by: David Hildenbrand <[email protected]> Reviewed-by: Peter Xu <[email protected]> Cc: <[email protected]> Cc: Muhammad Usama Anjum <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 998ad18 commit 24bf08c

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

mm/huge_memory.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,10 +1838,10 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
18381838
if (is_swap_pmd(*pmd)) {
18391839
swp_entry_t entry = pmd_to_swp_entry(*pmd);
18401840
struct page *page = pfn_swap_entry_to_page(entry);
1841+
pmd_t newpmd;
18411842

18421843
VM_BUG_ON(!is_pmd_migration_entry(*pmd));
18431844
if (is_writable_migration_entry(entry)) {
1844-
pmd_t newpmd;
18451845
/*
18461846
* A protection check is difficult so
18471847
* just be safe and disable write
@@ -1855,8 +1855,16 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
18551855
newpmd = pmd_swp_mksoft_dirty(newpmd);
18561856
if (pmd_swp_uffd_wp(*pmd))
18571857
newpmd = pmd_swp_mkuffd_wp(newpmd);
1858-
set_pmd_at(mm, addr, pmd, newpmd);
1858+
} else {
1859+
newpmd = *pmd;
18591860
}
1861+
1862+
if (uffd_wp)
1863+
newpmd = pmd_swp_mkuffd_wp(newpmd);
1864+
else if (uffd_wp_resolve)
1865+
newpmd = pmd_swp_clear_uffd_wp(newpmd);
1866+
if (!pmd_same(*pmd, newpmd))
1867+
set_pmd_at(mm, addr, pmd, newpmd);
18601868
goto unlock;
18611869
}
18621870
#endif
@@ -3251,6 +3259,8 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
32513259
pmdswp = swp_entry_to_pmd(entry);
32523260
if (pmd_soft_dirty(pmdval))
32533261
pmdswp = pmd_swp_mksoft_dirty(pmdswp);
3262+
if (pmd_uffd_wp(pmdval))
3263+
pmdswp = pmd_swp_mkuffd_wp(pmdswp);
32543264
set_pmd_at(mm, address, pvmw->pmd, pmdswp);
32553265
page_remove_rmap(page, vma, true);
32563266
put_page(page);

0 commit comments

Comments
 (0)