Skip to content

Commit bfe125f

Browse files
rgushchinakpm00
authored andcommitted
mmu_gather: move tlb flush for VM_PFNMAP/VM_MIXEDMAP vmas into free_pgtables()
Commit b67fbeb ("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 895428e ("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. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Roman Gushchin <[email protected]> Reviewed-by: Jann Horn <[email protected]> Acked-by: Hugh Dickins <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Cc: Will Deacon <[email protected]> Cc: "Aneesh Kumar K.V" <[email protected]> Cc: Nick Piggin <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 28615e6 commit bfe125f

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

include/asm-generic/tlb.h

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
* Defaults to flushing at tlb_end_vma() to reset the range; helps when
5959
* there's large holes between the VMAs.
6060
*
61+
* - tlb_free_vmas()
62+
*
63+
* tlb_free_vmas() marks the start of unlinking of one or more vmas
64+
* and freeing page-tables.
65+
*
6166
* - tlb_remove_table()
6267
*
6368
* 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)
464469
*/
465470
tlb->vma_huge = is_vm_hugetlb_page(vma);
466471
tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
467-
tlb->vma_pfn = !!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP));
472+
473+
/*
474+
* Track if there's at least one VM_PFNMAP/VM_MIXEDMAP vma
475+
* in the tracked range, see tlb_free_vmas().
476+
*/
477+
tlb->vma_pfn |= !!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP));
468478
}
469479

470480
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 *
547557
}
548558

549559
static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
560+
{
561+
if (tlb->fullmm || IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS))
562+
return;
563+
564+
/*
565+
* Do a TLB flush and reset the range at VMA boundaries; this avoids
566+
* the ranges growing with the unused space between consecutive VMAs,
567+
* but also the mmu_gather::vma_* flags from tlb_start_vma() rely on
568+
* this.
569+
*/
570+
tlb_flush_mmu_tlbonly(tlb);
571+
}
572+
573+
static inline void tlb_free_vmas(struct mmu_gather *tlb)
550574
{
551575
if (tlb->fullmm)
552576
return;
553577

554578
/*
555579
* VM_PFNMAP is more fragile because the core mm will not track the
556-
* page mapcount -- there might not be page-frames for these PFNs after
557-
* all. Force flush TLBs for such ranges to avoid munmap() vs
558-
* unmap_mapping_range() races.
580+
* page mapcount -- there might not be page-frames for these PFNs
581+
* after all.
582+
*
583+
* Specifically() there is a race between munmap() and
584+
* unmap_mapping_range(), where munmap() will unlink the VMA, such
585+
* that unmap_mapping_range() will no longer observe the VMA and
586+
* no-op, without observing the TLBI, returning prematurely.
587+
*
588+
* So if we're about to unlink such a VMA, and we have pending
589+
* TLBI for such a vma, flush things now.
559590
*/
560-
if (tlb->vma_pfn || !IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS)) {
561-
/*
562-
* Do a TLB flush and reset the range at VMA boundaries; this avoids
563-
* the ranges growing with the unused space between consecutive VMAs.
564-
*/
591+
if (tlb->vma_pfn)
565592
tlb_flush_mmu_tlbonly(tlb);
566-
}
567593
}
568594

569595
/*

mm/memory.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
358358
{
359359
struct unlink_vma_file_batch vb;
360360

361+
tlb_free_vmas(tlb);
362+
361363
do {
362364
unsigned long addr = vma->vm_start;
363365
struct vm_area_struct *next;

mm/mmu_gather.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
424424
#ifdef CONFIG_MMU_GATHER_PAGE_SIZE
425425
tlb->page_size = 0;
426426
#endif
427+
tlb->vma_pfn = 0;
427428

428429
__tlb_reset_range(tlb);
429430
inc_tlb_flush_pending(tlb->mm);

0 commit comments

Comments
 (0)