|
62 | 62 |
|
63 | 63 | #include <asm/tlbflush.h> |
64 | 64 |
|
| 65 | +#include <trace/events/tlb.h> |
| 66 | + |
65 | 67 | #include "internal.h" |
66 | 68 |
|
67 | 69 | static struct kmem_cache *anon_vma_cachep; |
@@ -583,6 +585,89 @@ vma_address(struct page *page, struct vm_area_struct *vma) |
583 | 585 | return address; |
584 | 586 | } |
585 | 587 |
|
| 588 | +#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH |
| 589 | +static void percpu_flush_tlb_batch_pages(void *data) |
| 590 | +{ |
| 591 | + /* |
| 592 | + * All TLB entries are flushed on the assumption that it is |
| 593 | + * cheaper to flush all TLBs and let them be refilled than |
| 594 | + * flushing individual PFNs. Note that we do not track mm's |
| 595 | + * to flush as that might simply be multiple full TLB flushes |
| 596 | + * for no gain. |
| 597 | + */ |
| 598 | + count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED); |
| 599 | + flush_tlb_local(); |
| 600 | +} |
| 601 | + |
| 602 | +/* |
| 603 | + * Flush TLB entries for recently unmapped pages from remote CPUs. It is |
| 604 | + * important if a PTE was dirty when it was unmapped that it's flushed |
| 605 | + * before any IO is initiated on the page to prevent lost writes. Similarly, |
| 606 | + * it must be flushed before freeing to prevent data leakage. |
| 607 | + */ |
| 608 | +void try_to_unmap_flush(void) |
| 609 | +{ |
| 610 | + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; |
| 611 | + int cpu; |
| 612 | + |
| 613 | + if (!tlb_ubc->flush_required) |
| 614 | + return; |
| 615 | + |
| 616 | + cpu = get_cpu(); |
| 617 | + |
| 618 | + trace_tlb_flush(TLB_REMOTE_SHOOTDOWN, -1UL); |
| 619 | + |
| 620 | + if (cpumask_test_cpu(cpu, &tlb_ubc->cpumask)) |
| 621 | + percpu_flush_tlb_batch_pages(&tlb_ubc->cpumask); |
| 622 | + |
| 623 | + if (cpumask_any_but(&tlb_ubc->cpumask, cpu) < nr_cpu_ids) { |
| 624 | + smp_call_function_many(&tlb_ubc->cpumask, |
| 625 | + percpu_flush_tlb_batch_pages, (void *)tlb_ubc, true); |
| 626 | + } |
| 627 | + cpumask_clear(&tlb_ubc->cpumask); |
| 628 | + tlb_ubc->flush_required = false; |
| 629 | + put_cpu(); |
| 630 | +} |
| 631 | + |
| 632 | +static void set_tlb_ubc_flush_pending(struct mm_struct *mm, |
| 633 | + struct page *page) |
| 634 | +{ |
| 635 | + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; |
| 636 | + |
| 637 | + cpumask_or(&tlb_ubc->cpumask, &tlb_ubc->cpumask, mm_cpumask(mm)); |
| 638 | + tlb_ubc->flush_required = true; |
| 639 | +} |
| 640 | + |
| 641 | +/* |
| 642 | + * Returns true if the TLB flush should be deferred to the end of a batch of |
| 643 | + * unmap operations to reduce IPIs. |
| 644 | + */ |
| 645 | +static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags) |
| 646 | +{ |
| 647 | + bool should_defer = false; |
| 648 | + |
| 649 | + if (!(flags & TTU_BATCH_FLUSH)) |
| 650 | + return false; |
| 651 | + |
| 652 | + /* If remote CPUs need to be flushed then defer batch the flush */ |
| 653 | + if (cpumask_any_but(mm_cpumask(mm), get_cpu()) < nr_cpu_ids) |
| 654 | + should_defer = true; |
| 655 | + put_cpu(); |
| 656 | + |
| 657 | + return should_defer; |
| 658 | +} |
| 659 | +#else |
| 660 | +static void set_tlb_ubc_flush_pending(struct mm_struct *mm, |
| 661 | + struct page *page) |
| 662 | +{ |
| 663 | +} |
| 664 | + |
| 665 | +static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags) |
| 666 | +{ |
| 667 | + return false; |
| 668 | +} |
| 669 | +#endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ |
| 670 | + |
586 | 671 | /* |
587 | 672 | * At what user virtual address is page expected in vma? |
588 | 673 | * Caller should check the page is actually part of the vma. |
@@ -1220,7 +1305,24 @@ static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma, |
1220 | 1305 |
|
1221 | 1306 | /* Nuke the page table entry. */ |
1222 | 1307 | flush_cache_page(vma, address, page_to_pfn(page)); |
1223 | | - pteval = ptep_clear_flush(vma, address, pte); |
| 1308 | + if (should_defer_flush(mm, flags)) { |
| 1309 | + /* |
| 1310 | + * We clear the PTE but do not flush so potentially a remote |
| 1311 | + * CPU could still be writing to the page. If the entry was |
| 1312 | + * previously clean then the architecture must guarantee that |
| 1313 | + * a clear->dirty transition on a cached TLB entry is written |
| 1314 | + * through and traps if the PTE is unmapped. |
| 1315 | + */ |
| 1316 | + pteval = ptep_get_and_clear(mm, address, pte); |
| 1317 | + |
| 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); |
| 1323 | + } else { |
| 1324 | + pteval = ptep_clear_flush(vma, address, pte); |
| 1325 | + } |
1224 | 1326 |
|
1225 | 1327 | /* Move the dirty bit to the physical page now the pte is gone. */ |
1226 | 1328 | if (pte_dirty(pteval)) |
|
0 commit comments