Skip to content

Commit deb4c93

Browse files
xzpeterakpm00
authored andcommitted
mm/khugepaged: don't recycle vma pgtable if uffd-wp registered
When we're trying to collapse a 2M huge shmem page, don't retract pgtable pmd page if it's registered with uffd-wp, because that pgtable could have pte markers installed. Recycling of that pgtable means we'll lose the pte markers. That could cause data loss for an uffd-wp enabled application on shmem. Instead of disabling khugepaged on these files, simply skip retracting these special VMAs, then the page cache can still be merged into a huge thp, and other mm/vma can still map the range of file with a huge thp when proper. Note that checking VM_UFFD_WP needs to be done with mmap_sem held for write, that avoids race like: khugepaged user thread ========== =========== check VM_UFFD_WP, not set UFFDIO_REGISTER with uffd-wp on shmem wr-protect some pages (install markers) take mmap_sem write lock erase pmd and free pmd page --> pte markers are dropped unnoticed! Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Peter Xu <[email protected]> Cc: Alistair Popple <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Axel Rasmussen <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jerome Glisse <[email protected]> Cc: "Kirill A . Shutemov" <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Nadav Amit <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent bc70fbf commit deb4c93

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

mm/khugepaged.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,10 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
14561456
if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE))
14571457
return;
14581458

1459+
/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
1460+
if (userfaultfd_wp(vma))
1461+
return;
1462+
14591463
hpage = find_lock_page(vma->vm_file->f_mapping,
14601464
linear_page_index(vma, haddr));
14611465
if (!hpage)
@@ -1591,7 +1595,15 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
15911595
* reverse order. Trylock is a way to avoid deadlock.
15921596
*/
15931597
if (mmap_write_trylock(mm)) {
1594-
if (!khugepaged_test_exit(mm))
1598+
/*
1599+
* When a vma is registered with uffd-wp, we can't
1600+
* recycle the pmd pgtable because there can be pte
1601+
* markers installed. Skip it only, so the rest mm/vma
1602+
* can still have the same file mapped hugely, however
1603+
* it'll always mapped in small page size for uffd-wp
1604+
* registered ranges.
1605+
*/
1606+
if (!khugepaged_test_exit(mm) && !userfaultfd_wp(vma))
15951607
collapse_and_free_pmd(mm, vma, addr, pmd);
15961608
mmap_write_unlock(mm);
15971609
} else {

0 commit comments

Comments
 (0)