ARM: tlb: delay page freeing for SMP and ARMv7 CPUs

We need to delay freeing any mapped page on SMP and ARMv7 systems to
ensure that the data is not accessed by other CPUs, or is used for
speculative prefetch with ARMv7. This includes not only mapped pages
but also pages used for the page tables themselves.

This avoids races with the MMU/other CPUs accessing pages after they've
been freed but before we've invalidated the TLB.

Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>

+89 -13
+89 -13
arch/arm/include/asm/tlb.h
··· 18 #define __ASMARM_TLB_H 19 20 #include <asm/cacheflush.h> 21 - #include <asm/tlbflush.h> 22 23 #ifndef CONFIG_MMU 24 ··· 26 27 #else /* !CONFIG_MMU */ 28 29 #include <asm/pgalloc.h> 30 31 /* 32 * TLB handling. This allows us to remove pages from the page ··· 51 struct mmu_gather { 52 struct mm_struct *mm; 53 unsigned int fullmm; 54 unsigned long range_start; 55 unsigned long range_end; 56 }; 57 58 DECLARE_PER_CPU(struct mmu_gather, mmu_gathers); 59 60 static inline struct mmu_gather * 61 tlb_gather_mmu(struct mm_struct *mm, unsigned int full_mm_flush) ··· 110 111 tlb->mm = mm; 112 tlb->fullmm = full_mm_flush; 113 114 return tlb; 115 } ··· 119 static inline void 120 tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 121 { 122 - if (tlb->fullmm) 123 - flush_tlb_mm(tlb->mm); 124 125 /* keep the page table cache within bounds */ 126 check_pgt_cache(); ··· 133 static inline void 134 tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long addr) 135 { 136 - if (!tlb->fullmm) { 137 - if (addr < tlb->range_start) 138 - tlb->range_start = addr; 139 - if (addr + PAGE_SIZE > tlb->range_end) 140 - tlb->range_end = addr + PAGE_SIZE; 141 - } 142 } 143 144 /* ··· 146 { 147 if (!tlb->fullmm) { 148 flush_cache_range(vma, vma->vm_start, vma->vm_end); 149 tlb->range_start = TASK_SIZE; 150 tlb->range_end = 0; 151 } ··· 155 static inline void 156 tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) 157 { 158 - if (!tlb->fullmm && tlb->range_end > 0) 159 - flush_tlb_range(vma, tlb->range_start, tlb->range_end); 160 } 161 162 - #define tlb_remove_page(tlb,page) free_page_and_swap_cache(page) 163 - #define pte_free_tlb(tlb, ptep, addr) pte_free((tlb)->mm, ptep) 164 #define pmd_free_tlb(tlb, pmdp, addr) pmd_free((tlb)->mm, pmdp) 165 166 #define tlb_migrate_finish(mm) do { } while (0)
··· 18 #define __ASMARM_TLB_H 19 20 #include <asm/cacheflush.h> 21 22 #ifndef CONFIG_MMU 23 ··· 27 28 #else /* !CONFIG_MMU */ 29 30 + #include <linux/swap.h> 31 #include <asm/pgalloc.h> 32 + #include <asm/tlbflush.h> 33 + 34 + /* 35 + * We need to delay page freeing for SMP as other CPUs can access pages 36 + * which have been removed but not yet had their TLB entries invalidated. 37 + * Also, as ARMv7 speculative prefetch can drag new entries into the TLB, 38 + * we need to apply this same delaying tactic to ensure correct operation. 39 + */ 40 + #if defined(CONFIG_SMP) || defined(CONFIG_CPU_32v7) 41 + #define tlb_fast_mode(tlb) 0 42 + #define FREE_PTE_NR 500 43 + #else 44 + #define tlb_fast_mode(tlb) 1 45 + #define FREE_PTE_NR 0 46 + #endif 47 48 /* 49 * TLB handling. This allows us to remove pages from the page ··· 36 struct mmu_gather { 37 struct mm_struct *mm; 38 unsigned int fullmm; 39 + struct vm_area_struct *vma; 40 unsigned long range_start; 41 unsigned long range_end; 42 + unsigned int nr; 43 + struct page *pages[FREE_PTE_NR]; 44 }; 45 46 DECLARE_PER_CPU(struct mmu_gather, mmu_gathers); 47 + 48 + /* 49 + * This is unnecessarily complex. There's three ways the TLB shootdown 50 + * code is used: 51 + * 1. Unmapping a range of vmas. See zap_page_range(), unmap_region(). 52 + * tlb->fullmm = 0, and tlb_start_vma/tlb_end_vma will be called. 53 + * tlb->vma will be non-NULL. 54 + * 2. Unmapping all vmas. See exit_mmap(). 55 + * tlb->fullmm = 1, and tlb_start_vma/tlb_end_vma will be called. 56 + * tlb->vma will be non-NULL. Additionally, page tables will be freed. 57 + * 3. Unmapping argument pages. See shift_arg_pages(). 58 + * tlb->fullmm = 0, but tlb_start_vma/tlb_end_vma will not be called. 59 + * tlb->vma will be NULL. 60 + */ 61 + static inline void tlb_flush(struct mmu_gather *tlb) 62 + { 63 + if (tlb->fullmm || !tlb->vma) 64 + flush_tlb_mm(tlb->mm); 65 + else if (tlb->range_end > 0) { 66 + flush_tlb_range(tlb->vma, tlb->range_start, tlb->range_end); 67 + tlb->range_start = TASK_SIZE; 68 + tlb->range_end = 0; 69 + } 70 + } 71 + 72 + static inline void tlb_add_flush(struct mmu_gather *tlb, unsigned long addr) 73 + { 74 + if (!tlb->fullmm) { 75 + if (addr < tlb->range_start) 76 + tlb->range_start = addr; 77 + if (addr + PAGE_SIZE > tlb->range_end) 78 + tlb->range_end = addr + PAGE_SIZE; 79 + } 80 + } 81 + 82 + static inline void tlb_flush_mmu(struct mmu_gather *tlb) 83 + { 84 + tlb_flush(tlb); 85 + if (!tlb_fast_mode(tlb)) { 86 + free_pages_and_swap_cache(tlb->pages, tlb->nr); 87 + tlb->nr = 0; 88 + } 89 + } 90 91 static inline struct mmu_gather * 92 tlb_gather_mmu(struct mm_struct *mm, unsigned int full_mm_flush) ··· 49 50 tlb->mm = mm; 51 tlb->fullmm = full_mm_flush; 52 + tlb->vma = NULL; 53 + tlb->nr = 0; 54 55 return tlb; 56 } ··· 56 static inline void 57 tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 58 { 59 + tlb_flush_mmu(tlb); 60 61 /* keep the page table cache within bounds */ 62 check_pgt_cache(); ··· 71 static inline void 72 tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long addr) 73 { 74 + tlb_add_flush(tlb, addr); 75 } 76 77 /* ··· 89 { 90 if (!tlb->fullmm) { 91 flush_cache_range(vma, vma->vm_start, vma->vm_end); 92 + tlb->vma = vma; 93 tlb->range_start = TASK_SIZE; 94 tlb->range_end = 0; 95 } ··· 97 static inline void 98 tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) 99 { 100 + if (!tlb->fullmm) 101 + tlb_flush(tlb); 102 } 103 104 + static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page) 105 + { 106 + if (tlb_fast_mode(tlb)) { 107 + free_page_and_swap_cache(page); 108 + } else { 109 + tlb->pages[tlb->nr++] = page; 110 + if (tlb->nr >= FREE_PTE_NR) 111 + tlb_flush_mmu(tlb); 112 + } 113 + } 114 + 115 + static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, 116 + unsigned long addr) 117 + { 118 + pgtable_page_dtor(pte); 119 + tlb_add_flush(tlb, addr); 120 + tlb_remove_page(tlb, pte); 121 + } 122 + 123 + #define pte_free_tlb(tlb, ptep, addr) __pte_free_tlb(tlb, ptep, addr) 124 #define pmd_free_tlb(tlb, pmdp, addr) pmd_free((tlb)->mm, pmdp) 125 126 #define tlb_migrate_finish(mm) do { } while (0)