Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 1 | /* include/asm-generic/tlb.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * Generic TLB shootdown code |
| 4 | * |
| 5 | * Copyright 2001 Red Hat, Inc. |
| 6 | * Based on code from mm/memory.c Copyright Linus Torvalds and others. |
| 7 | * |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 8 | * Copyright 2011 Red Hat, Inc., Peter Zijlstra |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | #ifndef _ASM_GENERIC__TLB_H |
| 16 | #define _ASM_GENERIC__TLB_H |
| 17 | |
Nicholas Piggin | fd1102f | 2018-08-23 18:47:09 +1000 | [diff] [blame] | 18 | #include <linux/mmu_notifier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/swap.h> |
Ingo Molnar | 62152d0 | 2008-01-31 22:05:48 +0100 | [diff] [blame] | 20 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/tlbflush.h> |
Peter Zijlstra | e7fd28a | 2018-08-27 13:00:17 +0200 | [diff] [blame] | 22 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Will Deacon | faaadaf | 2018-08-24 13:28:28 +0100 | [diff] [blame] | 24 | #ifdef CONFIG_MMU |
| 25 | |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 26 | /* |
| 27 | * Generic MMU-gather implementation. |
| 28 | * |
| 29 | * The mmu_gather data structure is used by the mm code to implement the |
| 30 | * correct and efficient ordering of freeing pages and TLB invalidations. |
| 31 | * |
| 32 | * This correct ordering is: |
| 33 | * |
| 34 | * 1) unhook page |
| 35 | * 2) TLB invalidate page |
| 36 | * 3) free page |
| 37 | * |
| 38 | * That is, we must never free a page before we have ensured there are no live |
| 39 | * translations left to it. Otherwise it might be possible to observe (or |
| 40 | * worse, change) the page content after it has been reused. |
| 41 | * |
| 42 | * The mmu_gather API consists of: |
| 43 | * |
| 44 | * - tlb_gather_mmu() / tlb_finish_mmu(); start and finish a mmu_gather |
| 45 | * |
| 46 | * Finish in particular will issue a (final) TLB invalidate and free |
| 47 | * all (remaining) queued pages. |
| 48 | * |
| 49 | * - tlb_start_vma() / tlb_end_vma(); marks the start / end of a VMA |
| 50 | * |
| 51 | * Defaults to flushing at tlb_end_vma() to reset the range; helps when |
| 52 | * there's large holes between the VMAs. |
| 53 | * |
| 54 | * - tlb_remove_page() / __tlb_remove_page() |
| 55 | * - tlb_remove_page_size() / __tlb_remove_page_size() |
| 56 | * |
| 57 | * __tlb_remove_page_size() is the basic primitive that queues a page for |
| 58 | * freeing. __tlb_remove_page() assumes PAGE_SIZE. Both will return a |
| 59 | * boolean indicating if the queue is (now) full and a call to |
| 60 | * tlb_flush_mmu() is required. |
| 61 | * |
| 62 | * tlb_remove_page() and tlb_remove_page_size() imply the call to |
| 63 | * tlb_flush_mmu() when required and has no return value. |
| 64 | * |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 65 | * - tlb_change_page_size() |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 66 | * |
| 67 | * call before __tlb_remove_page*() to set the current page-size; implies a |
| 68 | * possible tlb_flush_mmu() call. |
| 69 | * |
| 70 | * - tlb_flush_mmu() / tlb_flush_mmu_tlbonly() / tlb_flush_mmu_free() |
| 71 | * |
| 72 | * tlb_flush_mmu_tlbonly() - does the TLB invalidate (and resets |
| 73 | * related state, like the range) |
| 74 | * |
| 75 | * tlb_flush_mmu_free() - frees the queued pages; make absolutely |
| 76 | * sure no additional tlb_remove_page() |
| 77 | * calls happen between _tlbonly() and this. |
| 78 | * |
| 79 | * tlb_flush_mmu() - the above two calls. |
| 80 | * |
| 81 | * - mmu_gather::fullmm |
| 82 | * |
| 83 | * A flag set by tlb_gather_mmu() to indicate we're going to free |
| 84 | * the entire mm; this allows a number of optimizations. |
| 85 | * |
| 86 | * - We can ignore tlb_{start,end}_vma(); because we don't |
| 87 | * care about ranges. Everything will be shot down. |
| 88 | * |
| 89 | * - (RISC) architectures that use ASIDs can cycle to a new ASID |
| 90 | * and delay the invalidation until ASID space runs out. |
| 91 | * |
| 92 | * - mmu_gather::need_flush_all |
| 93 | * |
| 94 | * A flag that can be set by the arch code if it wants to force |
| 95 | * flush the entire TLB irrespective of the range. For instance |
| 96 | * x86-PAE needs this when changing top-level entries. |
| 97 | * |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 98 | * And allows the architecture to provide and implement tlb_flush(): |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 99 | * |
| 100 | * tlb_flush() may, in addition to the above mentioned mmu_gather fields, make |
| 101 | * use of: |
| 102 | * |
| 103 | * - mmu_gather::start / mmu_gather::end |
| 104 | * |
| 105 | * which provides the range that needs to be flushed to cover the pages to |
| 106 | * be freed. |
| 107 | * |
| 108 | * - mmu_gather::freed_tables |
| 109 | * |
| 110 | * set when we freed page table pages |
| 111 | * |
| 112 | * - tlb_get_unmap_shift() / tlb_get_unmap_size() |
| 113 | * |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 114 | * returns the smallest TLB entry size unmapped in this range. |
| 115 | * |
| 116 | * If an architecture does not provide tlb_flush() a default implementation |
Peter Zijlstra | a30e32b | 2018-10-11 16:51:51 +0200 | [diff] [blame^] | 117 | * based on flush_tlb_range() will be used, unless MMU_GATHER_NO_RANGE is |
| 118 | * specified, in which case we'll default to flush_tlb_mm(). |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 119 | * |
| 120 | * Additionally there are a few opt-in features: |
| 121 | * |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 122 | * HAVE_MMU_GATHER_PAGE_SIZE |
| 123 | * |
| 124 | * This ensures we call tlb_flush() every time tlb_change_page_size() actually |
| 125 | * changes the size and provides mmu_gather::page_size to tlb_flush(). |
| 126 | * |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 127 | * HAVE_RCU_TABLE_FREE |
| 128 | * |
| 129 | * This provides tlb_remove_table(), to be used instead of tlb_remove_page() |
| 130 | * for page directores (__p*_free_tlb()). This provides separate freeing of |
| 131 | * the page-table pages themselves in a semi-RCU fashion (see comment below). |
| 132 | * Useful if your architecture doesn't use IPIs for remote TLB invalidates |
| 133 | * and therefore doesn't naturally serialize with software page-table walkers. |
| 134 | * |
| 135 | * When used, an architecture is expected to provide __tlb_remove_table() |
| 136 | * which does the actual freeing of these pages. |
| 137 | * |
| 138 | * HAVE_RCU_TABLE_INVALIDATE |
| 139 | * |
| 140 | * This makes HAVE_RCU_TABLE_FREE call tlb_flush_mmu_tlbonly() before freeing |
| 141 | * the page-table pages. Required if you use HAVE_RCU_TABLE_FREE and your |
| 142 | * architecture uses the Linux page-tables natively. |
| 143 | * |
Peter Zijlstra | a30e32b | 2018-10-11 16:51:51 +0200 | [diff] [blame^] | 144 | * MMU_GATHER_NO_RANGE |
| 145 | * |
| 146 | * Use this if your architecture lacks an efficient flush_tlb_range(). |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 147 | */ |
| 148 | #define HAVE_GENERIC_MMU_GATHER |
| 149 | |
Peter Zijlstra | 2672391 | 2011-05-24 17:12:00 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_HAVE_RCU_TABLE_FREE |
| 151 | /* |
| 152 | * Semi RCU freeing of the page directories. |
| 153 | * |
| 154 | * This is needed by some architectures to implement software pagetable walkers. |
| 155 | * |
| 156 | * gup_fast() and other software pagetable walkers do a lockless page-table |
| 157 | * walk and therefore needs some synchronization with the freeing of the page |
| 158 | * directories. The chosen means to accomplish that is by disabling IRQs over |
| 159 | * the walk. |
| 160 | * |
| 161 | * Architectures that use IPIs to flush TLBs will then automagically DTRT, |
| 162 | * since we unlink the page, flush TLBs, free the page. Since the disabling of |
| 163 | * IRQs delays the completion of the TLB flush we can never observe an already |
| 164 | * freed page. |
| 165 | * |
| 166 | * Architectures that do not have this (PPC) need to delay the freeing by some |
| 167 | * other means, this is that means. |
| 168 | * |
| 169 | * What we do is batch the freed directory pages (tables) and RCU free them. |
| 170 | * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling |
| 171 | * holds off grace periods. |
| 172 | * |
| 173 | * However, in order to batch these pages we need to allocate storage, this |
| 174 | * allocation is deep inside the MM code and can thus easily fail on memory |
| 175 | * pressure. To guarantee progress we fall back to single table freeing, see |
| 176 | * the implementation of tlb_remove_table_one(). |
| 177 | * |
| 178 | */ |
| 179 | struct mmu_table_batch { |
| 180 | struct rcu_head rcu; |
| 181 | unsigned int nr; |
| 182 | void *tables[0]; |
| 183 | }; |
| 184 | |
| 185 | #define MAX_TABLE_BATCH \ |
| 186 | ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *)) |
| 187 | |
| 188 | extern void tlb_table_flush(struct mmu_gather *tlb); |
| 189 | extern void tlb_remove_table(struct mmu_gather *tlb, void *table); |
| 190 | |
| 191 | #endif |
| 192 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 193 | /* |
| 194 | * If we can't allocate a page to make a big batch of page pointers |
| 195 | * to work on, then just handle a few from the on-stack structure. |
| 196 | */ |
| 197 | #define MMU_GATHER_BUNDLE 8 |
| 198 | |
Peter Zijlstra | e303297 | 2011-05-24 17:12:01 -0700 | [diff] [blame] | 199 | struct mmu_gather_batch { |
| 200 | struct mmu_gather_batch *next; |
| 201 | unsigned int nr; |
| 202 | unsigned int max; |
| 203 | struct page *pages[0]; |
| 204 | }; |
| 205 | |
| 206 | #define MAX_GATHER_BATCH \ |
| 207 | ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *)) |
| 208 | |
Michal Hocko | 53a59fc | 2013-01-04 15:35:12 -0800 | [diff] [blame] | 209 | /* |
| 210 | * Limit the maximum number of mmu_gather batches to reduce a risk of soft |
| 211 | * lockups for non-preemptible kernels on huge machines when a lot of memory |
| 212 | * is zapped during unmapping. |
| 213 | * 10K pages freed at once should be safe even without a preemption point. |
| 214 | */ |
| 215 | #define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH) |
| 216 | |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 217 | /* |
| 218 | * struct mmu_gather is an opaque type used by the mm code for passing around |
Hugh Dickins | 15a23ff | 2005-10-29 18:16:01 -0700 | [diff] [blame] | 219 | * any data needed by arch specific code for tlb_remove_page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | */ |
| 221 | struct mmu_gather { |
| 222 | struct mm_struct *mm; |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 223 | |
Peter Zijlstra | 2672391 | 2011-05-24 17:12:00 -0700 | [diff] [blame] | 224 | #ifdef CONFIG_HAVE_RCU_TABLE_FREE |
| 225 | struct mmu_table_batch *batch; |
| 226 | #endif |
Peter Zijlstra | dea2434 | 2018-09-04 10:43:14 +0200 | [diff] [blame] | 227 | |
Alex Shi | 597e1c3 | 2012-06-28 09:02:21 +0800 | [diff] [blame] | 228 | unsigned long start; |
| 229 | unsigned long end; |
Peter Zijlstra | 22a61c3 | 2018-08-23 20:27:25 +0100 | [diff] [blame] | 230 | /* |
| 231 | * we are in the middle of an operation to clear |
| 232 | * a full mm and can make some optimizations |
| 233 | */ |
| 234 | unsigned int fullmm : 1; |
| 235 | |
| 236 | /* |
| 237 | * we have performed an operation which |
| 238 | * requires a complete flush of the tlb |
| 239 | */ |
| 240 | unsigned int need_flush_all : 1; |
| 241 | |
| 242 | /* |
| 243 | * we have removed page directories |
| 244 | */ |
| 245 | unsigned int freed_tables : 1; |
Peter Zijlstra | e303297 | 2011-05-24 17:12:01 -0700 | [diff] [blame] | 246 | |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 247 | /* |
| 248 | * at which levels have we cleared entries? |
| 249 | */ |
| 250 | unsigned int cleared_ptes : 1; |
| 251 | unsigned int cleared_pmds : 1; |
| 252 | unsigned int cleared_puds : 1; |
| 253 | unsigned int cleared_p4ds : 1; |
| 254 | |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 255 | /* |
| 256 | * tracks VM_EXEC | VM_HUGETLB in tlb_start_vma |
| 257 | */ |
| 258 | unsigned int vma_exec : 1; |
| 259 | unsigned int vma_huge : 1; |
| 260 | |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 261 | unsigned int batch_count; |
| 262 | |
Peter Zijlstra | e303297 | 2011-05-24 17:12:01 -0700 | [diff] [blame] | 263 | struct mmu_gather_batch *active; |
| 264 | struct mmu_gather_batch local; |
| 265 | struct page *__pages[MMU_GATHER_BUNDLE]; |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 266 | |
| 267 | #ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE |
| 268 | unsigned int page_size; |
| 269 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
Minchan Kim | 56236a5 | 2017-08-10 15:24:05 -0700 | [diff] [blame] | 272 | void arch_tlb_gather_mmu(struct mmu_gather *tlb, |
| 273 | struct mm_struct *mm, unsigned long start, unsigned long end); |
Peter Zijlstra | 9547d01 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 274 | void tlb_flush_mmu(struct mmu_gather *tlb); |
Minchan Kim | 56236a5 | 2017-08-10 15:24:05 -0700 | [diff] [blame] | 275 | void arch_tlb_finish_mmu(struct mmu_gather *tlb, |
Minchan Kim | 99baac2 | 2017-08-10 15:24:12 -0700 | [diff] [blame] | 276 | unsigned long start, unsigned long end, bool force); |
Peter Zijlstra | 196d9d8 | 2018-09-03 15:07:36 +0100 | [diff] [blame] | 277 | void tlb_flush_mmu_free(struct mmu_gather *tlb); |
Aneesh Kumar K.V | e77b085 | 2016-07-26 15:24:12 -0700 | [diff] [blame] | 278 | extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, |
| 279 | int page_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 281 | static inline void __tlb_adjust_range(struct mmu_gather *tlb, |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 282 | unsigned long address, |
| 283 | unsigned int range_size) |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 284 | { |
| 285 | tlb->start = min(tlb->start, address); |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 286 | tlb->end = max(tlb->end, address + range_size); |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static inline void __tlb_reset_range(struct mmu_gather *tlb) |
| 290 | { |
Will Deacon | 721c21c | 2015-01-12 19:10:55 +0000 | [diff] [blame] | 291 | if (tlb->fullmm) { |
| 292 | tlb->start = tlb->end = ~0; |
| 293 | } else { |
| 294 | tlb->start = TASK_SIZE; |
| 295 | tlb->end = 0; |
| 296 | } |
Peter Zijlstra | 22a61c3 | 2018-08-23 20:27:25 +0100 | [diff] [blame] | 297 | tlb->freed_tables = 0; |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 298 | tlb->cleared_ptes = 0; |
| 299 | tlb->cleared_pmds = 0; |
| 300 | tlb->cleared_puds = 0; |
| 301 | tlb->cleared_p4ds = 0; |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 302 | /* |
| 303 | * Do not reset mmu_gather::vma_* fields here, we do not |
| 304 | * call into tlb_start_vma() again to set them if there is an |
| 305 | * intermediate flush. |
| 306 | */ |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Peter Zijlstra | a30e32b | 2018-10-11 16:51:51 +0200 | [diff] [blame^] | 309 | #ifdef CONFIG_MMU_GATHER_NO_RANGE |
| 310 | |
| 311 | #if defined(tlb_flush) || defined(tlb_start_vma) || defined(tlb_end_vma) |
| 312 | #error MMU_GATHER_NO_RANGE relies on default tlb_flush(), tlb_start_vma() and tlb_end_vma() |
| 313 | #endif |
| 314 | |
| 315 | /* |
| 316 | * When an architecture does not have efficient means of range flushing TLBs |
| 317 | * there is no point in doing intermediate flushes on tlb_end_vma() to keep the |
| 318 | * range small. We equally don't have to worry about page granularity or other |
| 319 | * things. |
| 320 | * |
| 321 | * All we need to do is issue a full flush for any !0 range. |
| 322 | */ |
| 323 | static inline void tlb_flush(struct mmu_gather *tlb) |
| 324 | { |
| 325 | if (tlb->end) |
| 326 | flush_tlb_mm(tlb->mm); |
| 327 | } |
| 328 | |
| 329 | static inline void |
| 330 | tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { } |
| 331 | |
| 332 | #define tlb_end_vma tlb_end_vma |
| 333 | static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) { } |
| 334 | |
| 335 | #else /* CONFIG_MMU_GATHER_NO_RANGE */ |
| 336 | |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 337 | #ifndef tlb_flush |
| 338 | |
| 339 | #if defined(tlb_start_vma) || defined(tlb_end_vma) |
| 340 | #error Default tlb_flush() relies on default tlb_start_vma() and tlb_end_vma() |
| 341 | #endif |
| 342 | |
Peter Zijlstra | a30e32b | 2018-10-11 16:51:51 +0200 | [diff] [blame^] | 343 | /* |
| 344 | * When an architecture does not provide its own tlb_flush() implementation |
| 345 | * but does have a reasonably efficient flush_vma_range() implementation |
| 346 | * use that. |
| 347 | */ |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 348 | static inline void tlb_flush(struct mmu_gather *tlb) |
| 349 | { |
| 350 | if (tlb->fullmm || tlb->need_flush_all) { |
| 351 | flush_tlb_mm(tlb->mm); |
| 352 | } else if (tlb->end) { |
| 353 | struct vm_area_struct vma = { |
| 354 | .vm_mm = tlb->mm, |
| 355 | .vm_flags = (tlb->vma_exec ? VM_EXEC : 0) | |
| 356 | (tlb->vma_huge ? VM_HUGETLB : 0), |
| 357 | }; |
| 358 | |
| 359 | flush_tlb_range(&vma, tlb->start, tlb->end); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | static inline void |
| 364 | tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) |
| 365 | { |
| 366 | /* |
| 367 | * flush_tlb_range() implementations that look at VM_HUGETLB (tile, |
| 368 | * mips-4k) flush only large pages. |
| 369 | * |
| 370 | * flush_tlb_range() implementations that flush I-TLB also flush D-TLB |
| 371 | * (tile, xtensa, arm), so it's ok to just add VM_EXEC to an existing |
| 372 | * range. |
| 373 | * |
| 374 | * We rely on tlb_end_vma() to issue a flush, such that when we reset |
| 375 | * these values the batch is empty. |
| 376 | */ |
| 377 | tlb->vma_huge = !!(vma->vm_flags & VM_HUGETLB); |
| 378 | tlb->vma_exec = !!(vma->vm_flags & VM_EXEC); |
| 379 | } |
| 380 | |
| 381 | #else |
| 382 | |
| 383 | static inline void |
| 384 | tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { } |
| 385 | |
| 386 | #endif |
| 387 | |
Peter Zijlstra | a30e32b | 2018-10-11 16:51:51 +0200 | [diff] [blame^] | 388 | #endif /* CONFIG_MMU_GATHER_NO_RANGE */ |
| 389 | |
Nicholas Piggin | fd1102f | 2018-08-23 18:47:09 +1000 | [diff] [blame] | 390 | static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) |
| 391 | { |
| 392 | if (!tlb->end) |
| 393 | return; |
| 394 | |
| 395 | tlb_flush(tlb); |
| 396 | mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end); |
| 397 | __tlb_reset_range(tlb); |
| 398 | } |
| 399 | |
Aneesh Kumar K.V | e77b085 | 2016-07-26 15:24:12 -0700 | [diff] [blame] | 400 | static inline void tlb_remove_page_size(struct mmu_gather *tlb, |
| 401 | struct page *page, int page_size) |
| 402 | { |
Aneesh Kumar K.V | 692a68c | 2016-12-12 16:42:43 -0800 | [diff] [blame] | 403 | if (__tlb_remove_page_size(tlb, page, page_size)) |
Aneesh Kumar K.V | e77b085 | 2016-07-26 15:24:12 -0700 | [diff] [blame] | 404 | tlb_flush_mmu(tlb); |
Aneesh Kumar K.V | e77b085 | 2016-07-26 15:24:12 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Aneesh Kumar K.V | 692a68c | 2016-12-12 16:42:43 -0800 | [diff] [blame] | 407 | static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page) |
Aneesh Kumar K.V | e77b085 | 2016-07-26 15:24:12 -0700 | [diff] [blame] | 408 | { |
| 409 | return __tlb_remove_page_size(tlb, page, PAGE_SIZE); |
| 410 | } |
| 411 | |
Aneesh Kumar K.V | e9d55e1 | 2016-07-26 15:24:09 -0700 | [diff] [blame] | 412 | /* tlb_remove_page |
| 413 | * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when |
| 414 | * required. |
| 415 | */ |
| 416 | static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page) |
| 417 | { |
Aneesh Kumar K.V | e77b085 | 2016-07-26 15:24:12 -0700 | [diff] [blame] | 418 | return tlb_remove_page_size(tlb, page, PAGE_SIZE); |
Aneesh Kumar K.V | e9d55e1 | 2016-07-26 15:24:09 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 421 | static inline void tlb_change_page_size(struct mmu_gather *tlb, |
Aneesh Kumar K.V | 07e3266 | 2016-12-12 16:42:40 -0800 | [diff] [blame] | 422 | unsigned int page_size) |
| 423 | { |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 424 | #ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE |
| 425 | if (tlb->page_size && tlb->page_size != page_size) { |
| 426 | if (!tlb->fullmm) |
| 427 | tlb_flush_mmu(tlb); |
| 428 | } |
| 429 | |
Aneesh Kumar K.V | 07e3266 | 2016-12-12 16:42:40 -0800 | [diff] [blame] | 430 | tlb->page_size = page_size; |
| 431 | #endif |
| 432 | } |
Aneesh Kumar K.V | 07e3266 | 2016-12-12 16:42:40 -0800 | [diff] [blame] | 433 | |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 434 | static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb) |
| 435 | { |
| 436 | if (tlb->cleared_ptes) |
| 437 | return PAGE_SHIFT; |
| 438 | if (tlb->cleared_pmds) |
| 439 | return PMD_SHIFT; |
| 440 | if (tlb->cleared_puds) |
| 441 | return PUD_SHIFT; |
| 442 | if (tlb->cleared_p4ds) |
| 443 | return P4D_SHIFT; |
| 444 | |
| 445 | return PAGE_SHIFT; |
| 446 | } |
| 447 | |
| 448 | static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb) |
| 449 | { |
| 450 | return 1UL << tlb_get_unmap_shift(tlb); |
| 451 | } |
| 452 | |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 453 | /* |
| 454 | * In the case of tlb vma handling, we can optimise these away in the |
| 455 | * case where we're doing a full MM flush. When we're doing a munmap, |
| 456 | * the vmas are adjusted to only cover the region to be torn down. |
| 457 | */ |
| 458 | #ifndef tlb_start_vma |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 459 | static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) |
| 460 | { |
| 461 | if (tlb->fullmm) |
| 462 | return; |
| 463 | |
| 464 | tlb_update_vma_flags(tlb, vma); |
| 465 | flush_cache_range(vma, vma->vm_start, vma->vm_end); |
| 466 | } |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 467 | #endif |
| 468 | |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 469 | #ifndef tlb_end_vma |
Peter Zijlstra | 5f307be | 2018-09-04 13:18:15 +0200 | [diff] [blame] | 470 | static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) |
| 471 | { |
| 472 | if (tlb->fullmm) |
| 473 | return; |
| 474 | |
| 475 | /* |
| 476 | * Do a TLB flush and reset the range at VMA boundaries; this avoids |
| 477 | * the ranges growing with the unused space between consecutive VMAs, |
| 478 | * but also the mmu_gather::vma_* flags from tlb_start_vma() rely on |
| 479 | * this. |
| 480 | */ |
| 481 | tlb_flush_mmu_tlbonly(tlb); |
| 482 | } |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 483 | #endif |
| 484 | |
| 485 | #ifndef __tlb_remove_tlb_entry |
| 486 | #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0) |
| 487 | #endif |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | /** |
| 490 | * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation. |
| 491 | * |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 492 | * Record the fact that pte's were really unmapped by updating the range, |
| 493 | * so we can later optimise away the tlb invalidate. This helps when |
| 494 | * userspace is unmapping already-unmapped pages, which happens quite a lot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | */ |
| 496 | #define tlb_remove_tlb_entry(tlb, ptep, address) \ |
| 497 | do { \ |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 498 | __tlb_adjust_range(tlb, address, PAGE_SIZE); \ |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 499 | tlb->cleared_ptes = 1; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | __tlb_remove_tlb_entry(tlb, ptep, address); \ |
| 501 | } while (0) |
| 502 | |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 503 | #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \ |
| 504 | do { \ |
| 505 | unsigned long _sz = huge_page_size(h); \ |
| 506 | __tlb_adjust_range(tlb, address, _sz); \ |
| 507 | if (_sz == PMD_SIZE) \ |
| 508 | tlb->cleared_pmds = 1; \ |
| 509 | else if (_sz == PUD_SIZE) \ |
| 510 | tlb->cleared_puds = 1; \ |
| 511 | __tlb_remove_tlb_entry(tlb, ptep, address); \ |
Aneesh Kumar K.V | b528e4b | 2016-12-12 16:42:37 -0800 | [diff] [blame] | 512 | } while (0) |
| 513 | |
Shaohua Li | f21760b | 2012-01-12 17:19:16 -0800 | [diff] [blame] | 514 | /** |
| 515 | * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation |
| 516 | * This is a nop so far, because only x86 needs it. |
| 517 | */ |
| 518 | #ifndef __tlb_remove_pmd_tlb_entry |
| 519 | #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0) |
| 520 | #endif |
| 521 | |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 522 | #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \ |
| 523 | do { \ |
| 524 | __tlb_adjust_range(tlb, address, HPAGE_PMD_SIZE); \ |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 525 | tlb->cleared_pmds = 1; \ |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 526 | __tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \ |
Shaohua Li | f21760b | 2012-01-12 17:19:16 -0800 | [diff] [blame] | 527 | } while (0) |
| 528 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 529 | /** |
| 530 | * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb |
| 531 | * invalidation. This is a nop so far, because only x86 needs it. |
| 532 | */ |
| 533 | #ifndef __tlb_remove_pud_tlb_entry |
| 534 | #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0) |
| 535 | #endif |
| 536 | |
| 537 | #define tlb_remove_pud_tlb_entry(tlb, pudp, address) \ |
| 538 | do { \ |
| 539 | __tlb_adjust_range(tlb, address, HPAGE_PUD_SIZE); \ |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 540 | tlb->cleared_puds = 1; \ |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 541 | __tlb_remove_pud_tlb_entry(tlb, pudp, address); \ |
| 542 | } while (0) |
| 543 | |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 544 | /* |
| 545 | * For things like page tables caches (ie caching addresses "inside" the |
| 546 | * page tables, like x86 does), for legacy reasons, flushing an |
| 547 | * individual page had better flush the page table caches behind it. This |
| 548 | * is definitely how x86 works, for example. And if you have an |
| 549 | * architected non-legacy page table cache (which I'm not aware of |
| 550 | * anybody actually doing), you're going to have some architecturally |
| 551 | * explicit flushing for that, likely *separate* from a regular TLB entry |
| 552 | * flush, and thus you'd need more than just some range expansion.. |
| 553 | * |
| 554 | * So if we ever find an architecture |
| 555 | * that would want something that odd, I think it is up to that |
| 556 | * architecture to do its own odd thing, not cause pain for others |
| 557 | * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com |
| 558 | * |
| 559 | * For now w.r.t page table cache, mark the range_size as PAGE_SIZE |
| 560 | */ |
| 561 | |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 562 | #ifndef pte_free_tlb |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 563 | #define pte_free_tlb(tlb, ptep, address) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | do { \ |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 565 | __tlb_adjust_range(tlb, address, PAGE_SIZE); \ |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 566 | tlb->freed_tables = 1; \ |
| 567 | tlb->cleared_pmds = 1; \ |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 568 | __pte_free_tlb(tlb, ptep, address); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } while (0) |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 570 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 572 | #ifndef pmd_free_tlb |
Kirill A. Shutemov | 048456d | 2017-03-09 17:24:06 +0300 | [diff] [blame] | 573 | #define pmd_free_tlb(tlb, pmdp, address) \ |
| 574 | do { \ |
Peter Zijlstra | 22a61c3 | 2018-08-23 20:27:25 +0100 | [diff] [blame] | 575 | __tlb_adjust_range(tlb, address, PAGE_SIZE); \ |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 576 | tlb->freed_tables = 1; \ |
| 577 | tlb->cleared_puds = 1; \ |
Kirill A. Shutemov | 048456d | 2017-03-09 17:24:06 +0300 | [diff] [blame] | 578 | __pmd_free_tlb(tlb, pmdp, address); \ |
| 579 | } while (0) |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 580 | #endif |
Kirill A. Shutemov | 048456d | 2017-03-09 17:24:06 +0300 | [diff] [blame] | 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | #ifndef __ARCH_HAS_4LEVEL_HACK |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 583 | #ifndef pud_free_tlb |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 584 | #define pud_free_tlb(tlb, pudp, address) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | do { \ |
Aneesh Kumar K.V | b5bc66b | 2016-12-12 16:42:34 -0800 | [diff] [blame] | 586 | __tlb_adjust_range(tlb, address, PAGE_SIZE); \ |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 587 | tlb->freed_tables = 1; \ |
| 588 | tlb->cleared_p4ds = 1; \ |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 589 | __pud_free_tlb(tlb, pudp, address); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } while (0) |
| 591 | #endif |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 592 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Kirill A. Shutemov | 048456d | 2017-03-09 17:24:06 +0300 | [diff] [blame] | 594 | #ifndef __ARCH_HAS_5LEVEL_HACK |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 595 | #ifndef p4d_free_tlb |
Kirill A. Shutemov | 048456d | 2017-03-09 17:24:06 +0300 | [diff] [blame] | 596 | #define p4d_free_tlb(tlb, pudp, address) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | do { \ |
Peter Zijlstra | 22a61c3 | 2018-08-23 20:27:25 +0100 | [diff] [blame] | 598 | __tlb_adjust_range(tlb, address, PAGE_SIZE); \ |
Will Deacon | a6d6024 | 2018-08-23 21:01:46 +0100 | [diff] [blame] | 599 | tlb->freed_tables = 1; \ |
Kirill A. Shutemov | 048456d | 2017-03-09 17:24:06 +0300 | [diff] [blame] | 600 | __p4d_free_tlb(tlb, pudp, address); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } while (0) |
Kirill A. Shutemov | 048456d | 2017-03-09 17:24:06 +0300 | [diff] [blame] | 602 | #endif |
Nicholas Piggin | a90744b | 2018-07-13 16:59:03 -0700 | [diff] [blame] | 603 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Will Deacon | faaadaf | 2018-08-24 13:28:28 +0100 | [diff] [blame] | 605 | #endif /* CONFIG_MMU */ |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | #define tlb_migrate_finish(mm) do {} while (0) |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | #endif /* _ASM_GENERIC__TLB_H */ |