Skip to contentSkip to conversation

mmu_gather: move tlb flush for VM_PFNMAP/VM_MIXEDMAP vmas into free_pgtables()

PATCH Applied Roman Gushchin 4 participants 4 messages May 22, 2025 – May 30, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 mmu_gather: move tlb flush for VM_PFNMAP/VM_MIXEDMAP vmas into free_pgtables()

Commit b67fbebd4cf9 ("mmu_gather: Force tlb-flush VM_PFNMAP vmas") added a forced tlbflush to tlb_vma_end(), which is required to avoid a race between munmap() and unmap_mapping_range(). However it added some overhead to other paths where tlb_vma_end() is used, but vmas are not removed, e.g. madvise(MADV_DONTNEED).

Fix this by moving the tlb flush out of tlb_end_vma() into new tlb_flush_vmas() called from free_pgtables(), somewhat similar to the stable version of the original commit:
commit 895428ee124a ("mm: Force TLB flush for PFNMAP mappings before unlink_file_vma()").

Note, that if tlb->fullmm is set, no flush is required, as the whole mm is about to be destroyed.

Cc Jann Horn <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Will Deacon <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Nick Piggin <[email protected]>
Cc Hugh Dickins <[email protected]>

---
v6:
- tlb->vma_pfn is initialized in __tlb_gather_mmu() and is never cleared (by Jann H)

v5:
- tlb_free_vma() -> tlb_free_vmas() to avoid extra checks

v4:
- naming/comments update (by Peter Z.)
- check vma->vma->vm_flags in tlb_free_vma() (by Peter Z.)

v3:
- added initialization of vma_pfn in __tlb_reset_range() (by Hugh D.)

v2:
- moved vma_pfn flag handling into tlb.h (by Peter Z.)
- added comments (by Peter Z.)
- fixed the vma_pfn flag setting (by Hugh D.)

---
 include/asm-generic/tlb.h | 46 ++++++++++++++++++++++++++++++---------
 mm/memory.c               |  2 ++
 mm/mmu_gather.c           |  1 +
 3 files changed, 39 insertions(+), 10 deletions(-)
include/asm-generic/tlb.h +36 -10
index 88a42973fa47..1fff717cae51 100644
@@ -58,6 +58,11 @@
  *    Defaults to flushing at tlb_end_vma() to reset the range; helps when
  *    there's large holes between the VMAs.
  *
+ *  - tlb_free_vmas()
+ *
+ *    tlb_free_vmas() marks the start of unlinking of one or more vmas
+ *    and freeing page-tables.
+ *
  *  - tlb_remove_table()
  *
  *    tlb_remove_table() is the basic primitive to free page-table directories
@@ -464,7 +469,12 @@ tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma)
 	 */
 	tlb->vma_huge = is_vm_hugetlb_page(vma);
 	tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
-	tlb->vma_pfn  = !!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP));
+
+	/*
+	 * Track if there's at least one VM_PFNMAP/VM_MIXEDMAP vma
+	 * in the tracked range, see tlb_free_vmas().
+	 */
+	tlb->vma_pfn |= !!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP));
 }
 
 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
@@ -547,23 +557,39 @@ static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *
 }
 
 static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
+{
+	if (tlb->fullmm || IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS))
+		return;
+
+	/*
+	 * Do a TLB flush and reset the range at VMA boundaries; this avoids
+	 * the ranges growing with the unused space between consecutive VMAs,
+	 * but also the mmu_gather::vma_* flags from tlb_start_vma() rely on
+	 * this.
+	 */
+	tlb_flush_mmu_tlbonly(tlb);
+}
+
+static inline void tlb_free_vmas(struct mmu_gather *tlb)
 {
 	if (tlb->fullmm)
 		return;
 
 	/*
 	 * VM_PFNMAP is more fragile because the core mm will not track the
-	 * page mapcount -- there might not be page-frames for these PFNs after
-	 * all. Force flush TLBs for such ranges to avoid munmap() vs
-	 * unmap_mapping_range() races.
+	 * page mapcount -- there might not be page-frames for these PFNs
+	 * after all.
+	 *
+	 * Specifically() there is a race between munmap() and
+	 * unmap_mapping_range(), where munmap() will unlink the VMA, such
+	 * that unmap_mapping_range() will no longer observe the VMA and
+	 * no-op, without observing the TLBI, returning prematurely.
+	 *
+	 * So if we're about to unlink such a VMA, and we have pending
+	 * TLBI for such a vma, flush things now.
 	 */
-	if (tlb->vma_pfn || !IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS)) {
-		/*
-		 * Do a TLB flush and reset the range at VMA boundaries; this avoids
-		 * the ranges growing with the unused space between consecutive VMAs.
-		 */
+	if (tlb->vma_pfn)
 		tlb_flush_mmu_tlbonly(tlb);
-	}
 }
 
 /*
mm/memory.c +2 -0
index 5cb48f262ab0..6b71a66cc4fe 100644
@@ -358,6 +358,8 @@ void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
 {
 	struct unlink_vma_file_batch vb;
 
+	tlb_free_vmas(tlb);
+
 	do {
 		unsigned long addr = vma->vm_start;
 		struct vm_area_struct *next;
mm/mmu_gather.c +1 -0
index db7ba4a725d6..b49cc6385f1f 100644
@@ -424,6 +424,7 @@ static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
 	tlb->page_size = 0;
 #endif
+	tlb->vma_pfn = 0;
 
 	__tlb_reset_range(tlb);
 	inc_tlb_flush_pending(tlb->mm);
Signature · 1 line
2.49.0.1143.g0be31eac6b-goog
Reviewed-by on v6 1/1
#1 lore raw
Quoted 15 lines · Roman Gushchin

Commit b67fbebd4cf9 ("mmu_gather: Force tlb-flush VM_PFNMAP vmas") added a forced tlbflush to tlb_vma_end(), which is required to avoid a race between munmap() and unmap_mapping_range(). However it added some overhead to other paths where tlb_vma_end() is used, but vmas are not removed, e.g. madvise(MADV_DONTNEED).

Fix this by moving the tlb flush out of tlb_end_vma() into new tlb_flush_vmas() called from free_pgtables(), somewhat similar to the stable version of the original commit:
commit 895428ee124a ("mm: Force TLB flush for PFNMAP mappings before unlink_file_vma()").

Note, that if tlb->fullmm is set, no flush is required, as the whole mm is about to be destroyed.

Quoted 10 lines
Cc Jann Horn <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Will Deacon <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Nick Piggin <[email protected]>
Cc Hugh Dickins <[email protected]>

Thanks, this looks better.

Acked-by on v6 1/1
#2 lore raw
In reply to Roman Gushchin
Quoted 26 lines · Roman Gushchin

Commit b67fbebd4cf9 ("mmu_gather: Force tlb-flush VM_PFNMAP vmas") added a forced tlbflush to tlb_vma_end(), which is required to avoid a race between munmap() and unmap_mapping_range(). However it added some overhead to other paths where tlb_vma_end() is used, but vmas are not removed, e.g. madvise(MADV_DONTNEED).

Fix this by moving the tlb flush out of tlb_end_vma() into new tlb_flush_vmas() called from free_pgtables(), somewhat similar to the stable version of the original commit:
commit 895428ee124a ("mm: Force TLB flush for PFNMAP mappings before unlink_file_vma()").

Note, that if tlb->fullmm is set, no flush is required, as the whole mm is about to be destroyed.

Cc Jann Horn <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Will Deacon <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Nick Piggin <[email protected]>
Cc Hugh Dickins <[email protected]>
Quoted 4 lines

---
v6:
- tlb->vma_pfn is initialized in __tlb_gather_mmu() and is never cleared (by Jann H)

maintainer Acked-by on v6 1/1
#3 lore raw
In reply to Roman Gushchin
Quoted 12 lines · Roman Gushchin

Commit b67fbebd4cf9 ("mmu_gather: Force tlb-flush VM_PFNMAP vmas") added a forced tlbflush to tlb_vma_end(), which is required to avoid a race between munmap() and unmap_mapping_range(). However it added some overhead to other paths where tlb_vma_end() is used, but vmas are not removed, e.g. madvise(MADV_DONTNEED).

Fix this by moving the tlb flush out of tlb_end_vma() into new tlb_flush_vmas() called from free_pgtables(), somewhat similar to the stable version of the original commit:
commit 895428ee124a ("mm: Force TLB flush for PFNMAP mappings before unlink_file_vma()").

Quoted 4 lines

Note, that if tlb->fullmm is set, no flush is required, as the whole mm is about to be destroyed.