Skip to content

Commit d950c94

Browse files
Mel Gormantorvalds
authored andcommitted
mm: defer flush of writable TLB entries
If a PTE is unmapped and it's dirty then it was writable recently. Due to deferred TLB flushing, it's best to assume a writable TLB cache entry exists. With that assumption, the TLB must be flushed before any IO can start or the page is freed to avoid lost writes or data corruption. This patch defers flushing of potentially writable TLBs as long as possible. Signed-off-by: Mel Gorman <mgorman@suse.de> Reviewed-by: Rik van Riel <riel@redhat.com> Cc: Dave Hansen <dave.hansen@intel.com> Acked-by: Ingo Molnar <mingo@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 72b252a commit d950c94

4 files changed

Lines changed: 38 additions & 8 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,13 @@ struct tlbflush_unmap_batch {
13541354

13551355
/* True if any bit in cpumask is set */
13561356
bool flush_required;
1357+
1358+
/*
1359+
* If true then the PTE was dirty when unmapped. The entry must be
1360+
* flushed before IO is initiated or a stale TLB entry potentially
1361+
* allows an update without redirtying the page.
1362+
*/
1363+
bool writable;
13571364
};
13581365

13591366
struct task_struct {

‎mm/internal.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,14 @@ struct tlbflush_unmap_batch;
431431

432432
#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
433433
void try_to_unmap_flush(void);
434+
void try_to_unmap_flush_dirty(void);
434435
#else
435436
static inline void try_to_unmap_flush(void)
436437
{
437438
}
439+
static inline void try_to_unmap_flush_dirty(void)
440+
{
441+
}
438442

439443
#endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
440444
#endif /* __MM_INTERNAL_H */

‎mm/rmap.c‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,16 +626,34 @@ void try_to_unmap_flush(void)
626626
}
627627
cpumask_clear(&tlb_ubc->cpumask);
628628
tlb_ubc->flush_required = false;
629+
tlb_ubc->writable = false;
629630
put_cpu();
630631
}
631632

633+
/* Flush iff there are potentially writable TLB entries that can race with IO */
634+
void try_to_unmap_flush_dirty(void)
635+
{
636+
struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
637+
638+
if (tlb_ubc->writable)
639+
try_to_unmap_flush();
640+
}
641+
632642
static void set_tlb_ubc_flush_pending(struct mm_struct *mm,
633-
struct page *page)
643+
struct page *page, bool writable)
634644
{
635645
struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
636646

637647
cpumask_or(&tlb_ubc->cpumask, &tlb_ubc->cpumask, mm_cpumask(mm));
638648
tlb_ubc->flush_required = true;
649+
650+
/*
651+
* If the PTE was dirty then it's best to assume it's writable. The
652+
* caller must use try_to_unmap_flush_dirty() or try_to_unmap_flush()
653+
* before the page is queued for IO.
654+
*/
655+
if (writable)
656+
tlb_ubc->writable = true;
639657
}
640658

641659
/*
@@ -658,7 +676,7 @@ static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags)
658676
}
659677
#else
660678
static void set_tlb_ubc_flush_pending(struct mm_struct *mm,
661-
struct page *page)
679+
struct page *page, bool writable)
662680
{
663681
}
664682

@@ -1315,11 +1333,7 @@ static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
13151333
*/
13161334
pteval = ptep_get_and_clear(mm, address, pte);
13171335

1318-
/* Potentially writable TLBs must be flushed before IO */
1319-
if (pte_dirty(pteval))
1320-
flush_tlb_page(vma, address);
1321-
else
1322-
set_tlb_ubc_flush_pending(mm, page);
1336+
set_tlb_ubc_flush_pending(mm, page, pte_dirty(pteval));
13231337
} else {
13241338
pteval = ptep_clear_flush(vma, address, pte);
13251339
}

‎mm/vmscan.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,12 @@ static unsigned long shrink_page_list(struct list_head *page_list,
10981098
if (!sc->may_writepage)
10991099
goto keep_locked;
11001100

1101-
/* Page is dirty, try to write it out here */
1101+
/*
1102+
* Page is dirty. Flush the TLB if a writable entry
1103+
* potentially exists to avoid CPU writes after IO
1104+
* starts and then write it out here.
1105+
*/
1106+
try_to_unmap_flush_dirty();
11021107
switch (pageout(page, mapping, sc)) {
11031108
case PAGE_KEEP:
11041109
goto keep_locked;

0 commit comments

Comments
 (0)