1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* include/asm-generic/tlb.h
3 *
4 * Generic TLB shootdown code
5 *
6 * Copyright 2001 Red Hat, Inc.
7 * Based on code from mm/memory.c Copyright Linus Torvalds and others.
8 *
9 * Copyright 2011 Red Hat, Inc., Peter Zijlstra
10 */
11 #ifndef _ASM_GENERIC__TLB_H
12 #define _ASM_GENERIC__TLB_H
13
14 #include <linux/mmu_notifier.h>
15 #include <linux/swap.h>
16 #include <linux/hugetlb_inline.h>
17 #include <asm/tlbflush.h>
18 #include <asm/cacheflush.h>
19
20 /*
21 * Blindly accessing user memory from NMI context can be dangerous
22 * if we're in the middle of switching the current user task or switching
23 * the loaded mm.
24 */
25 #ifndef nmi_uaccess_okay
26 # define nmi_uaccess_okay() true
27 #endif
28
29 #ifdef CONFIG_MMU
30
31 /*
32 * Generic MMU-gather implementation.
33 *
34 * The mmu_gather data structure is used by the mm code to implement the
35 * correct and efficient ordering of freeing pages and TLB invalidations.
36 *
37 * This correct ordering is:
38 *
39 * 1) unhook page
40 * 2) TLB invalidate page
41 * 3) free page
42 *
43 * That is, we must never free a page before we have ensured there are no live
44 * translations left to it. Otherwise it might be possible to observe (or
45 * worse, change) the page content after it has been reused.
46 *
47 * The mmu_gather API consists of:
48 *
49 * - tlb_gather_mmu() / tlb_gather_mmu_fullmm() / tlb_finish_mmu()
50 *
51 * start and finish a mmu_gather
52 *
53 * Finish in particular will issue a (final) TLB invalidate and free
54 * all (remaining) queued pages.
55 *
56 * - tlb_start_vma() / tlb_end_vma(); marks the start / end of a VMA
57 *
58 * Defaults to flushing at tlb_end_vma() to reset the range; helps when
59 * there's large holes between the VMAs.
60 *
61 * - tlb_remove_table()
62 *
63 * tlb_remove_table() is the basic primitive to free page-table directories
64 * (__p*_free_tlb()). In it's most primitive form it is an alias for
65 * tlb_remove_page() below, for when page directories are pages and have no
66 * additional constraints.
67 *
68 * See also MMU_GATHER_TABLE_FREE and MMU_GATHER_RCU_TABLE_FREE.
69 *
70 * - tlb_remove_page() / __tlb_remove_page()
71 * - tlb_remove_page_size() / __tlb_remove_page_size()
72 * - __tlb_remove_folio_pages()
73 *
74 * __tlb_remove_page_size() is the basic primitive that queues a page for
75 * freeing. __tlb_remove_page() assumes PAGE_SIZE. Both will return a
76 * boolean indicating if the queue is (now) full and a call to
77 * tlb_flush_mmu() is required.
78 *
79 * tlb_remove_page() and tlb_remove_page_size() imply the call to
80 * tlb_flush_mmu() when required and has no return value.
81 *
82 * __tlb_remove_folio_pages() is similar to __tlb_remove_page(), however,
83 * instead of removing a single page, remove the given number of consecutive
84 * pages that are all part of the same (large) folio: just like calling
85 * __tlb_remove_page() on each page individually.
86 *
87 * - tlb_change_page_size()
88 *
89 * call before __tlb_remove_page*() to set the current page-size; implies a
90 * possible tlb_flush_mmu() call.
91 *
92 * - tlb_flush_mmu() / tlb_flush_mmu_tlbonly()
93 *
94 * tlb_flush_mmu_tlbonly() - does the TLB invalidate (and resets
95 * related state, like the range)
96 *
97 * tlb_flush_mmu() - in addition to the above TLB invalidate, also frees
98 * whatever pages are still batched.
99 *
100 * - mmu_gather::fullmm
101 *
102 * A flag set by tlb_gather_mmu_fullmm() to indicate we're going to free
103 * the entire mm; this allows a number of optimizations.
104 *
105 * - We can ignore tlb_{start,end}_vma(); because we don't
106 * care about ranges. Everything will be shot down.
107 *
108 * - (RISC) architectures that use ASIDs can cycle to a new ASID
109 * and delay the invalidation until ASID space runs out.
110 *
111 * - mmu_gather::need_flush_all
112 *
113 * A flag that can be set by the arch code if it wants to force
114 * flush the entire TLB irrespective of the range. For instance
115 * x86-PAE needs this when changing top-level entries.
116 *
117 * And allows the architecture to provide and implement tlb_flush():
118 *
119 * tlb_flush() may, in addition to the above mentioned mmu_gather fields, make
120 * use of:
121 *
122 * - mmu_gather::start / mmu_gather::end
123 *
124 * which provides the range that needs to be flushed to cover the pages to
125 * be freed.
126 *
127 * - mmu_gather::freed_tables
128 *
129 * set when we freed page table pages
130 *
131 * - tlb_get_unmap_shift() / tlb_get_unmap_size()
132 *
133 * returns the smallest TLB entry size unmapped in this range.
134 *
135 * If an architecture does not provide tlb_flush() a default implementation
136 * based on flush_tlb_range() will be used, unless MMU_GATHER_NO_RANGE is
137 * specified, in which case we'll default to flush_tlb_mm().
138 *
139 * Additionally there are a few opt-in features:
140 *
141 * MMU_GATHER_PAGE_SIZE
142 *
143 * This ensures we call tlb_flush() every time tlb_change_page_size() actually
144 * changes the size and provides mmu_gather::page_size to tlb_flush().
145 *
146 * This might be useful if your architecture has size specific TLB
147 * invalidation instructions.
148 *
149 * MMU_GATHER_TABLE_FREE
150 *
151 * This provides tlb_remove_table(), to be used instead of tlb_remove_page()
152 * for page directores (__p*_free_tlb()).
153 *
154 * Useful if your architecture has non-page page directories.
155 *
156 * When used, an architecture is expected to provide __tlb_remove_table() or
157 * use the generic __tlb_remove_table(), which does the actual freeing of these
158 * pages.
159 *
160 * MMU_GATHER_RCU_TABLE_FREE
161 *
162 * Like MMU_GATHER_TABLE_FREE, and adds semi-RCU semantics to the free (see
163 * comment below).
164 *
165 * Useful if your architecture doesn't use IPIs for remote TLB invalidates
166 * and therefore doesn't naturally serialize with software page-table walkers.
167 *
168 * MMU_GATHER_NO_FLUSH_CACHE
169 *
170 * Indicates the architecture has flush_cache_range() but it needs *NOT* be called
171 * before unmapping a VMA.
172 *
173 * NOTE: strictly speaking we shouldn't have this knob and instead rely on
174 * flush_cache_range() being a NOP, except Sparc64 seems to be
175 * different here.
176 *
177 * MMU_GATHER_MERGE_VMAS
178 *
179 * Indicates the architecture wants to merge ranges over VMAs; typical when
180 * multiple range invalidates are more expensive than a full invalidate.
181 *
182 * MMU_GATHER_NO_RANGE
183 *
184 * Use this if your architecture lacks an efficient flush_tlb_range(). This
185 * option implies MMU_GATHER_MERGE_VMAS above.
186 *
187 * MMU_GATHER_NO_GATHER
188 *
189 * If the option is set the mmu_gather will not track individual pages for
190 * delayed page free anymore. A platform that enables the option needs to
191 * provide its own implementation of the __tlb_remove_page_size() function to
192 * free pages.
193 *
194 * This is useful if your architecture already flushes TLB entries in the
195 * various ptep_get_and_clear() functions.
196 */
197
198 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
199
200 struct mmu_table_batch {
201 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
202 struct rcu_head rcu;
203 #endif
204 unsigned int nr;
205 void *tables[];
206 };
207
208 #define MAX_TABLE_BATCH \
209 ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
210
211 #ifndef __HAVE_ARCH_TLB_REMOVE_TABLE
__tlb_remove_table(void * table)212 static inline void __tlb_remove_table(void *table)
213 {
214 struct ptdesc *ptdesc = (struct ptdesc *)table;
215
216 pagetable_dtor_free(ptdesc);
217 }
218 #endif
219
220 extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
221
222 #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
223
224 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page);
225 /*
226 * Without MMU_GATHER_TABLE_FREE the architecture is assumed to have page based
227 * page directories and we can use the normal page batching to free them.
228 */
tlb_remove_table(struct mmu_gather * tlb,void * table)229 static inline void tlb_remove_table(struct mmu_gather *tlb, void *table)
230 {
231 struct page *page = (struct page *)table;
232
233 pagetable_dtor(page_ptdesc(page));
234 tlb_remove_page(tlb, page);
235 }
236 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
237
238 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
239 /*
240 * This allows an architecture that does not use the linux page-tables for
241 * hardware to skip the TLBI when freeing page tables.
242 */
243 #ifndef tlb_needs_table_invalidate
244 #define tlb_needs_table_invalidate() (true)
245 #endif
246
247 void tlb_remove_table_sync_one(void);
248
249 #else
250
251 #ifdef tlb_needs_table_invalidate
252 #error tlb_needs_table_invalidate() requires MMU_GATHER_RCU_TABLE_FREE
253 #endif
254
tlb_remove_table_sync_one(void)255 static inline void tlb_remove_table_sync_one(void) { }
256
257 #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
258
259
260 #ifndef CONFIG_MMU_GATHER_NO_GATHER
261 /*
262 * If we can't allocate a page to make a big batch of page pointers
263 * to work on, then just handle a few from the on-stack structure.
264 */
265 #define MMU_GATHER_BUNDLE 8
266
267 struct mmu_gather_batch {
268 struct mmu_gather_batch *next;
269 unsigned int nr;
270 unsigned int max;
271 struct encoded_page *encoded_pages[];
272 };
273
274 #define MAX_GATHER_BATCH \
275 ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
276
277 /*
278 * Limit the maximum number of mmu_gather batches to reduce a risk of soft
279 * lockups for non-preemptible kernels on huge machines when a lot of memory
280 * is zapped during unmapping.
281 * 10K pages freed at once should be safe even without a preemption point.
282 */
283 #define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH)
284
285 extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
286 bool delay_rmap, int page_size);
287 bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page,
288 unsigned int nr_pages, bool delay_rmap);
289
290 #ifdef CONFIG_SMP
291 /*
292 * This both sets 'delayed_rmap', and returns true. It would be an inline
293 * function, except we define it before the 'struct mmu_gather'.
294 */
295 #define tlb_delay_rmap(tlb) (((tlb)->delayed_rmap = 1), true)
296 extern void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma);
297 #endif
298
299 #endif
300
301 /*
302 * We have a no-op version of the rmap removal that doesn't
303 * delay anything. That is used on S390, which flushes remote
304 * TLBs synchronously, and on UP, which doesn't have any
305 * remote TLBs to flush and is not preemptible due to this
306 * all happening under the page table lock.
307 */
308 #ifndef tlb_delay_rmap
309 #define tlb_delay_rmap(tlb) (false)
tlb_flush_rmaps(struct mmu_gather * tlb,struct vm_area_struct * vma)310 static inline void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
311 #endif
312
313 /*
314 * struct mmu_gather is an opaque type used by the mm code for passing around
315 * any data needed by arch specific code for tlb_remove_page.
316 */
317 struct mmu_gather {
318 struct mm_struct *mm;
319
320 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
321 struct mmu_table_batch *batch;
322 #endif
323
324 unsigned long start;
325 unsigned long end;
326 /*
327 * we are in the middle of an operation to clear
328 * a full mm and can make some optimizations
329 */
330 unsigned int fullmm : 1;
331
332 /*
333 * we have performed an operation which
334 * requires a complete flush of the tlb
335 */
336 unsigned int need_flush_all : 1;
337
338 /*
339 * we have removed page directories
340 */
341 unsigned int freed_tables : 1;
342
343 /*
344 * Do we have pending delayed rmap removals?
345 */
346 unsigned int delayed_rmap : 1;
347
348 /*
349 * at which levels have we cleared entries?
350 */
351 unsigned int cleared_ptes : 1;
352 unsigned int cleared_pmds : 1;
353 unsigned int cleared_puds : 1;
354 unsigned int cleared_p4ds : 1;
355
356 /*
357 * tracks VM_EXEC | VM_HUGETLB in tlb_start_vma
358 */
359 unsigned int vma_exec : 1;
360 unsigned int vma_huge : 1;
361 unsigned int vma_pfn : 1;
362
363 unsigned int batch_count;
364
365 #ifndef CONFIG_MMU_GATHER_NO_GATHER
366 struct mmu_gather_batch *active;
367 struct mmu_gather_batch local;
368 struct page *__pages[MMU_GATHER_BUNDLE];
369
370 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
371 unsigned int page_size;
372 #endif
373 #endif
374 };
375
376 void tlb_flush_mmu(struct mmu_gather *tlb);
377
__tlb_adjust_range(struct mmu_gather * tlb,unsigned long address,unsigned int range_size)378 static inline void __tlb_adjust_range(struct mmu_gather *tlb,
379 unsigned long address,
380 unsigned int range_size)
381 {
382 tlb->start = min(tlb->start, address);
383 tlb->end = max(tlb->end, address + range_size);
384 }
385
__tlb_reset_range(struct mmu_gather * tlb)386 static inline void __tlb_reset_range(struct mmu_gather *tlb)
387 {
388 if (tlb->fullmm) {
389 tlb->start = tlb->end = ~0;
390 } else {
391 tlb->start = TASK_SIZE;
392 tlb->end = 0;
393 }
394 tlb->freed_tables = 0;
395 tlb->cleared_ptes = 0;
396 tlb->cleared_pmds = 0;
397 tlb->cleared_puds = 0;
398 tlb->cleared_p4ds = 0;
399 /*
400 * Do not reset mmu_gather::vma_* fields here, we do not
401 * call into tlb_start_vma() again to set them if there is an
402 * intermediate flush.
403 */
404 }
405
406 #ifdef CONFIG_MMU_GATHER_NO_RANGE
407
408 #if defined(tlb_flush)
409 #error MMU_GATHER_NO_RANGE relies on default tlb_flush()
410 #endif
411
412 /*
413 * When an architecture does not have efficient means of range flushing TLBs
414 * there is no point in doing intermediate flushes on tlb_end_vma() to keep the
415 * range small. We equally don't have to worry about page granularity or other
416 * things.
417 *
418 * All we need to do is issue a full flush for any !0 range.
419 */
tlb_flush(struct mmu_gather * tlb)420 static inline void tlb_flush(struct mmu_gather *tlb)
421 {
422 if (tlb->end)
423 flush_tlb_mm(tlb->mm);
424 }
425
426 #else /* CONFIG_MMU_GATHER_NO_RANGE */
427
428 #ifndef tlb_flush
429 /*
430 * When an architecture does not provide its own tlb_flush() implementation
431 * but does have a reasonably efficient flush_vma_range() implementation
432 * use that.
433 */
tlb_flush(struct mmu_gather * tlb)434 static inline void tlb_flush(struct mmu_gather *tlb)
435 {
436 if (tlb->fullmm || tlb->need_flush_all) {
437 flush_tlb_mm(tlb->mm);
438 } else if (tlb->end) {
439 struct vm_area_struct vma = {
440 .vm_mm = tlb->mm,
441 .vm_flags = (tlb->vma_exec ? VM_EXEC : 0) |
442 (tlb->vma_huge ? VM_HUGETLB : 0),
443 };
444
445 flush_tlb_range(&vma, tlb->start, tlb->end);
446 }
447 }
448 #endif
449
450 #endif /* CONFIG_MMU_GATHER_NO_RANGE */
451
452 static inline void
tlb_update_vma_flags(struct mmu_gather * tlb,struct vm_area_struct * vma)453 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma)
454 {
455 /*
456 * flush_tlb_range() implementations that look at VM_HUGETLB (tile,
457 * mips-4k) flush only large pages.
458 *
459 * flush_tlb_range() implementations that flush I-TLB also flush D-TLB
460 * (tile, xtensa, arm), so it's ok to just add VM_EXEC to an existing
461 * range.
462 *
463 * We rely on tlb_end_vma() to issue a flush, such that when we reset
464 * these values the batch is empty.
465 */
466 tlb->vma_huge = is_vm_hugetlb_page(vma);
467 tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
468 tlb->vma_pfn = !!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP));
469 }
470
tlb_flush_mmu_tlbonly(struct mmu_gather * tlb)471 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
472 {
473 /*
474 * Anything calling __tlb_adjust_range() also sets at least one of
475 * these bits.
476 */
477 if (!(tlb->freed_tables || tlb->cleared_ptes || tlb->cleared_pmds ||
478 tlb->cleared_puds || tlb->cleared_p4ds))
479 return;
480
481 tlb_flush(tlb);
482 __tlb_reset_range(tlb);
483 }
484
tlb_remove_page_size(struct mmu_gather * tlb,struct page * page,int page_size)485 static inline void tlb_remove_page_size(struct mmu_gather *tlb,
486 struct page *page, int page_size)
487 {
488 if (__tlb_remove_page_size(tlb, page, false, page_size))
489 tlb_flush_mmu(tlb);
490 }
491
__tlb_remove_page(struct mmu_gather * tlb,struct page * page,bool delay_rmap)492 static __always_inline bool __tlb_remove_page(struct mmu_gather *tlb,
493 struct page *page, bool delay_rmap)
494 {
495 return __tlb_remove_page_size(tlb, page, delay_rmap, PAGE_SIZE);
496 }
497
498 /* tlb_remove_page
499 * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
500 * required.
501 */
tlb_remove_page(struct mmu_gather * tlb,struct page * page)502 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
503 {
504 return tlb_remove_page_size(tlb, page, PAGE_SIZE);
505 }
506
tlb_remove_ptdesc(struct mmu_gather * tlb,void * pt)507 static inline void tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt)
508 {
509 tlb_remove_table(tlb, pt);
510 }
511
512 /* Like tlb_remove_ptdesc, but for page-like page directories. */
tlb_remove_page_ptdesc(struct mmu_gather * tlb,struct ptdesc * pt)513 static inline void tlb_remove_page_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt)
514 {
515 tlb_remove_page(tlb, ptdesc_page(pt));
516 }
517
tlb_change_page_size(struct mmu_gather * tlb,unsigned int page_size)518 static inline void tlb_change_page_size(struct mmu_gather *tlb,
519 unsigned int page_size)
520 {
521 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
522 if (tlb->page_size && tlb->page_size != page_size) {
523 if (!tlb->fullmm && !tlb->need_flush_all)
524 tlb_flush_mmu(tlb);
525 }
526
527 tlb->page_size = page_size;
528 #endif
529 }
530
tlb_get_unmap_shift(struct mmu_gather * tlb)531 static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb)
532 {
533 if (tlb->cleared_ptes)
534 return PAGE_SHIFT;
535 if (tlb->cleared_pmds)
536 return PMD_SHIFT;
537 if (tlb->cleared_puds)
538 return PUD_SHIFT;
539 if (tlb->cleared_p4ds)
540 return P4D_SHIFT;
541
542 return PAGE_SHIFT;
543 }
544
tlb_get_unmap_size(struct mmu_gather * tlb)545 static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb)
546 {
547 return 1UL << tlb_get_unmap_shift(tlb);
548 }
549
550 /*
551 * In the case of tlb vma handling, we can optimise these away in the
552 * case where we're doing a full MM flush. When we're doing a munmap,
553 * the vmas are adjusted to only cover the region to be torn down.
554 */
tlb_start_vma(struct mmu_gather * tlb,struct vm_area_struct * vma)555 static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
556 {
557 if (tlb->fullmm)
558 return;
559
560 tlb_update_vma_flags(tlb, vma);
561 #ifndef CONFIG_MMU_GATHER_NO_FLUSH_CACHE
562 flush_cache_range(vma, vma->vm_start, vma->vm_end);
563 #endif
564 }
565
tlb_end_vma(struct mmu_gather * tlb,struct vm_area_struct * vma)566 static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
567 {
568 if (tlb->fullmm)
569 return;
570
571 /*
572 * VM_PFNMAP is more fragile because the core mm will not track the
573 * page mapcount -- there might not be page-frames for these PFNs after
574 * all. Force flush TLBs for such ranges to avoid munmap() vs
575 * unmap_mapping_range() races.
576 */
577 if (tlb->vma_pfn || !IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS)) {
578 /*
579 * Do a TLB flush and reset the range at VMA boundaries; this avoids
580 * the ranges growing with the unused space between consecutive VMAs.
581 */
582 tlb_flush_mmu_tlbonly(tlb);
583 }
584 }
585
586 /*
587 * tlb_flush_{pte|pmd|pud|p4d}_range() adjust the tlb->start and tlb->end,
588 * and set corresponding cleared_*.
589 */
tlb_flush_pte_range(struct mmu_gather * tlb,unsigned long address,unsigned long size)590 static inline void tlb_flush_pte_range(struct mmu_gather *tlb,
591 unsigned long address, unsigned long size)
592 {
593 __tlb_adjust_range(tlb, address, size);
594 tlb->cleared_ptes = 1;
595 }
596
tlb_flush_pmd_range(struct mmu_gather * tlb,unsigned long address,unsigned long size)597 static inline void tlb_flush_pmd_range(struct mmu_gather *tlb,
598 unsigned long address, unsigned long size)
599 {
600 __tlb_adjust_range(tlb, address, size);
601 tlb->cleared_pmds = 1;
602 }
603
tlb_flush_pud_range(struct mmu_gather * tlb,unsigned long address,unsigned long size)604 static inline void tlb_flush_pud_range(struct mmu_gather *tlb,
605 unsigned long address, unsigned long size)
606 {
607 __tlb_adjust_range(tlb, address, size);
608 tlb->cleared_puds = 1;
609 }
610
tlb_flush_p4d_range(struct mmu_gather * tlb,unsigned long address,unsigned long size)611 static inline void tlb_flush_p4d_range(struct mmu_gather *tlb,
612 unsigned long address, unsigned long size)
613 {
614 __tlb_adjust_range(tlb, address, size);
615 tlb->cleared_p4ds = 1;
616 }
617
618 #ifndef __tlb_remove_tlb_entry
__tlb_remove_tlb_entry(struct mmu_gather * tlb,pte_t * ptep,unsigned long address)619 static inline void __tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long address)
620 {
621 }
622 #endif
623
624 /**
625 * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
626 *
627 * Record the fact that pte's were really unmapped by updating the range,
628 * so we can later optimise away the tlb invalidate. This helps when
629 * userspace is unmapping already-unmapped pages, which happens quite a lot.
630 */
631 #define tlb_remove_tlb_entry(tlb, ptep, address) \
632 do { \
633 tlb_flush_pte_range(tlb, address, PAGE_SIZE); \
634 __tlb_remove_tlb_entry(tlb, ptep, address); \
635 } while (0)
636
637 /**
638 * tlb_remove_tlb_entries - remember unmapping of multiple consecutive ptes for
639 * later tlb invalidation.
640 *
641 * Similar to tlb_remove_tlb_entry(), but remember unmapping of multiple
642 * consecutive ptes instead of only a single one.
643 */
tlb_remove_tlb_entries(struct mmu_gather * tlb,pte_t * ptep,unsigned int nr,unsigned long address)644 static inline void tlb_remove_tlb_entries(struct mmu_gather *tlb,
645 pte_t *ptep, unsigned int nr, unsigned long address)
646 {
647 tlb_flush_pte_range(tlb, address, PAGE_SIZE * nr);
648 for (;;) {
649 __tlb_remove_tlb_entry(tlb, ptep, address);
650 if (--nr == 0)
651 break;
652 ptep++;
653 address += PAGE_SIZE;
654 }
655 }
656
657 #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
658 do { \
659 unsigned long _sz = huge_page_size(h); \
660 if (_sz >= P4D_SIZE) \
661 tlb_flush_p4d_range(tlb, address, _sz); \
662 else if (_sz >= PUD_SIZE) \
663 tlb_flush_pud_range(tlb, address, _sz); \
664 else if (_sz >= PMD_SIZE) \
665 tlb_flush_pmd_range(tlb, address, _sz); \
666 else \
667 tlb_flush_pte_range(tlb, address, _sz); \
668 __tlb_remove_tlb_entry(tlb, ptep, address); \
669 } while (0)
670
671 /**
672 * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
673 * This is a nop so far, because only x86 needs it.
674 */
675 #ifndef __tlb_remove_pmd_tlb_entry
676 #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
677 #endif
678
679 #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
680 do { \
681 tlb_flush_pmd_range(tlb, address, HPAGE_PMD_SIZE); \
682 __tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
683 } while (0)
684
685 /**
686 * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
687 * invalidation. This is a nop so far, because only x86 needs it.
688 */
689 #ifndef __tlb_remove_pud_tlb_entry
690 #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
691 #endif
692
693 #define tlb_remove_pud_tlb_entry(tlb, pudp, address) \
694 do { \
695 tlb_flush_pud_range(tlb, address, HPAGE_PUD_SIZE); \
696 __tlb_remove_pud_tlb_entry(tlb, pudp, address); \
697 } while (0)
698
699 /*
700 * For things like page tables caches (ie caching addresses "inside" the
701 * page tables, like x86 does), for legacy reasons, flushing an
702 * individual page had better flush the page table caches behind it. This
703 * is definitely how x86 works, for example. And if you have an
704 * architected non-legacy page table cache (which I'm not aware of
705 * anybody actually doing), you're going to have some architecturally
706 * explicit flushing for that, likely *separate* from a regular TLB entry
707 * flush, and thus you'd need more than just some range expansion..
708 *
709 * So if we ever find an architecture
710 * that would want something that odd, I think it is up to that
711 * architecture to do its own odd thing, not cause pain for others
712 * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
713 *
714 * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
715 */
716
717 #ifndef pte_free_tlb
718 #define pte_free_tlb(tlb, ptep, address) \
719 do { \
720 tlb_flush_pmd_range(tlb, address, PAGE_SIZE); \
721 tlb->freed_tables = 1; \
722 __pte_free_tlb(tlb, ptep, address); \
723 } while (0)
724 #endif
725
726 #ifndef pmd_free_tlb
727 #define pmd_free_tlb(tlb, pmdp, address) \
728 do { \
729 tlb_flush_pud_range(tlb, address, PAGE_SIZE); \
730 tlb->freed_tables = 1; \
731 __pmd_free_tlb(tlb, pmdp, address); \
732 } while (0)
733 #endif
734
735 #ifndef pud_free_tlb
736 #define pud_free_tlb(tlb, pudp, address) \
737 do { \
738 tlb_flush_p4d_range(tlb, address, PAGE_SIZE); \
739 tlb->freed_tables = 1; \
740 __pud_free_tlb(tlb, pudp, address); \
741 } while (0)
742 #endif
743
744 #ifndef p4d_free_tlb
745 #define p4d_free_tlb(tlb, pudp, address) \
746 do { \
747 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
748 tlb->freed_tables = 1; \
749 __p4d_free_tlb(tlb, pudp, address); \
750 } while (0)
751 #endif
752
753 #ifndef pte_needs_flush
pte_needs_flush(pte_t oldpte,pte_t newpte)754 static inline bool pte_needs_flush(pte_t oldpte, pte_t newpte)
755 {
756 return true;
757 }
758 #endif
759
760 #ifndef huge_pmd_needs_flush
huge_pmd_needs_flush(pmd_t oldpmd,pmd_t newpmd)761 static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)
762 {
763 return true;
764 }
765 #endif
766
767 #endif /* CONFIG_MMU */
768
769 #endif /* _ASM_GENERIC__TLB_H */
770