The existing code is rather horrible and already resulted in bugs in the past: we special-case UFFD inserting anonymous folios by grabbing the PMD lock early, but ignore any other markers (guard, poison through uffd) that could have been inserted into the page table in the meantime, effectively dropping them. Let's just unify and simplify the locking: try to grab the PMD table lock and if that fails, flush the TLB, drop the PTE lock, take PT locks in the right order and check if the page table is still empty. We still perform the heavy lifting (zapping the folio ptes) under PTE table lock only. Include for now, as x86's flush_tlb_range() macro depends on hugetlb helpers. This dependency needs a separate cleanup. Both issues this fixes are rather corner cases. Let's not CC stable for now. Fixes: f807123d578d ("mm: allow guard regions in file-backed and read-only mappings") Fixes: 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()") Signed-off-by: David Hildenbrand (Arm) --- mm/khugepaged.c | 64 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index dd17c3dae8006..4313abf9cfcb9 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -1832,6 +1833,17 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a return SCAN_SUCCEED; } +static bool pte_table_empty(pte_t *pte) +{ + int i; + + for (i = 0; i < HPAGE_PMD_NR; i++, pte++) { + if (!pte_none(ptep_get(pte))) + return false; + } + return true; +} + static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, bool install_pmd) { @@ -1935,33 +1947,14 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign } pte_unmap_unlock(start_pte, ptl); - ptl = NULL; mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, haddr, haddr + HPAGE_PMD_SIZE); mmu_notifier_invalidate_range_start(&range); notified = true; - /* - * pmd_lock covers a wider range than ptl, and (if split from mm's - * page_table_lock) ptl nests inside pml. The less time we hold pml, - * the better; but userfaultfd's mfill_atomic_pte() on a private VMA - * inserts a valid as-if-COWed PTE without even looking up page cache. - * So page lock of folio does not protect from it, so we must not drop - * ptl before pgt_pmd is removed, so uffd private needs pml taken now. - */ - if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED)) - pml = pmd_lock(mm, pmd); - - start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl); + start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl); if (!start_pte) goto abort; - if (!pml) - spin_lock(ptl); - else if (ptl != pml) - spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); - - if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) - goto abort; /* step 2: clear page table and adjust rmap */ for (i = 0, addr = haddr, pte = start_pte; i < HPAGE_PMD_NR; @@ -1992,10 +1985,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign nr_batch_ptes = folio_pte_batch(folio, pte, ptent, max_nr_batch_ptes); - /* - * Must clear entry, or a racing truncate may re-remove it. - * TLB flush can be left until pmdp_collapse_flush() does it. - */ + /* Must clear entry, or a racing truncate may re-remove it. */ ptent = get_and_clear_ptes(mm, addr, pte, nr_batch_ptes); if (pte_dirty(ptent)) folio_mark_dirty(folio); @@ -2003,25 +1993,33 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign nr_mapped_ptes += nr_batch_ptes; } - if (!pml) - spin_unlock(ptl); - /* step 3: set proper refcount and mm_counters. */ if (nr_mapped_ptes) { folio_ref_sub(folio, nr_mapped_ptes); add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes); } - /* step 4: remove empty page table */ - if (!pml) { + /* step 4: acquire the PMD lock and remove the empty page table */ + pml = pmd_lockptr(mm, pmd); + if (ptl != pml && !spin_trylock(pml)) { + /* We must flush before dropping the PTL. */ + if (nr_mapped_ptes) + flush_tlb_range(vma, haddr, end); + pte_unmap_unlock(start_pte, ptl); + ptl = NULL; + pml = pmd_lock(mm, pmd); + start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl); + if (!start_pte) + goto unlock; if (ptl != pml) spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); - if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) { - flush_tlb_mm(mm); + + if (!pte_table_empty(start_pte)) goto unlock; - } } + + /* This also flushes the zapped folio ptes properly. */ pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd); pmdp_get_lockless_sync(); pte_unmap_unlock(start_pte, ptl); @@ -2042,7 +2040,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign goto drop_folio; abort: if (nr_mapped_ptes) { - flush_tlb_mm(mm); + flush_tlb_range(vma, haddr, end); folio_ref_sub(folio, nr_mapped_ptes); add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes); } -- 2.43.0