1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* include/asm-generic/tlb.h
4 * Generic TLB shootdown code
6 * Copyright 2001 Red Hat, Inc.
7 * Based on code from mm/memory.c Copyright Linus Torvalds and others.
9 * Copyright 2011 Red Hat, Inc., Peter Zijlstra
11 #ifndef _ASM_GENERIC__TLB_H
12 #define _ASM_GENERIC__TLB_H
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>
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
25 #ifndef nmi_uaccess_okay
26 # define nmi_uaccess_okay() true
32 * Generic MMU-gather implementation.
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.
37 * This correct ordering is:
40 * 2) TLB invalidate page
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.
47 * The mmu_gather API consists of:
49 * - tlb_gather_mmu() / tlb_finish_mmu(); start and finish a mmu_gather
51 * Finish in particular will issue a (final) TLB invalidate and free
52 * all (remaining) queued pages.
54 * - tlb_start_vma() / tlb_end_vma(); marks the start / end of a VMA
56 * Defaults to flushing at tlb_end_vma() to reset the range; helps when
57 * there's large holes between the VMAs.
59 * - tlb_remove_table()
61 * tlb_remove_table() is the basic primitive to free page-table directories
62 * (__p*_free_tlb()). In it's most primitive form it is an alias for
63 * tlb_remove_page() below, for when page directories are pages and have no
64 * additional constraints.
66 * See also MMU_GATHER_TABLE_FREE and MMU_GATHER_RCU_TABLE_FREE.
68 * - tlb_remove_page() / __tlb_remove_page()
69 * - tlb_remove_page_size() / __tlb_remove_page_size()
71 * __tlb_remove_page_size() is the basic primitive that queues a page for
72 * freeing. __tlb_remove_page() assumes PAGE_SIZE. Both will return a
73 * boolean indicating if the queue is (now) full and a call to
74 * tlb_flush_mmu() is required.
76 * tlb_remove_page() and tlb_remove_page_size() imply the call to
77 * tlb_flush_mmu() when required and has no return value.
79 * - tlb_change_page_size()
81 * call before __tlb_remove_page*() to set the current page-size; implies a
82 * possible tlb_flush_mmu() call.
84 * - tlb_flush_mmu() / tlb_flush_mmu_tlbonly()
86 * tlb_flush_mmu_tlbonly() - does the TLB invalidate (and resets
87 * related state, like the range)
89 * tlb_flush_mmu() - in addition to the above TLB invalidate, also frees
90 * whatever pages are still batched.
92 * - mmu_gather::fullmm
94 * A flag set by tlb_gather_mmu() to indicate we're going to free
95 * the entire mm; this allows a number of optimizations.
97 * - We can ignore tlb_{start,end}_vma(); because we don't
98 * care about ranges. Everything will be shot down.
100 * - (RISC) architectures that use ASIDs can cycle to a new ASID
101 * and delay the invalidation until ASID space runs out.
103 * - mmu_gather::need_flush_all
105 * A flag that can be set by the arch code if it wants to force
106 * flush the entire TLB irrespective of the range. For instance
107 * x86-PAE needs this when changing top-level entries.
109 * And allows the architecture to provide and implement tlb_flush():
111 * tlb_flush() may, in addition to the above mentioned mmu_gather fields, make
114 * - mmu_gather::start / mmu_gather::end
116 * which provides the range that needs to be flushed to cover the pages to
119 * - mmu_gather::freed_tables
121 * set when we freed page table pages
123 * - tlb_get_unmap_shift() / tlb_get_unmap_size()
125 * returns the smallest TLB entry size unmapped in this range.
127 * If an architecture does not provide tlb_flush() a default implementation
128 * based on flush_tlb_range() will be used, unless MMU_GATHER_NO_RANGE is
129 * specified, in which case we'll default to flush_tlb_mm().
131 * Additionally there are a few opt-in features:
133 * MMU_GATHER_PAGE_SIZE
135 * This ensures we call tlb_flush() every time tlb_change_page_size() actually
136 * changes the size and provides mmu_gather::page_size to tlb_flush().
138 * This might be useful if your architecture has size specific TLB
139 * invalidation instructions.
141 * MMU_GATHER_TABLE_FREE
143 * This provides tlb_remove_table(), to be used instead of tlb_remove_page()
144 * for page directores (__p*_free_tlb()).
146 * Useful if your architecture has non-page page directories.
148 * When used, an architecture is expected to provide __tlb_remove_table()
149 * which does the actual freeing of these pages.
151 * MMU_GATHER_RCU_TABLE_FREE
153 * Like MMU_GATHER_TABLE_FREE, and adds semi-RCU semantics to the free (see
156 * Useful if your architecture doesn't use IPIs for remote TLB invalidates
157 * and therefore doesn't naturally serialize with software page-table walkers.
159 * MMU_GATHER_NO_RANGE
161 * Use this if your architecture lacks an efficient flush_tlb_range().
163 * MMU_GATHER_NO_GATHER
165 * If the option is set the mmu_gather will not track individual pages for
166 * delayed page free anymore. A platform that enables the option needs to
167 * provide its own implementation of the __tlb_remove_page_size() function to
170 * This is useful if your architecture already flushes TLB entries in the
171 * various ptep_get_and_clear() functions.
174 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
176 struct mmu_table_batch {
177 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
184 #define MAX_TABLE_BATCH \
185 ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
187 extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
189 #else /* !CONFIG_MMU_GATHER_HAVE_TABLE_FREE */
192 * Without MMU_GATHER_TABLE_FREE the architecture is assumed to have page based
193 * page directories and we can use the normal page batching to free them.
195 #define tlb_remove_table(tlb, page) tlb_remove_page((tlb), (page))
197 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
199 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
201 * This allows an architecture that does not use the linux page-tables for
202 * hardware to skip the TLBI when freeing page tables.
204 #ifndef tlb_needs_table_invalidate
205 #define tlb_needs_table_invalidate() (true)
208 void tlb_remove_table_sync_one(void);
212 #ifdef tlb_needs_table_invalidate
213 #error tlb_needs_table_invalidate() requires MMU_GATHER_RCU_TABLE_FREE
216 static inline void tlb_remove_table_sync_one(void) { }
218 #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
221 #ifndef CONFIG_MMU_GATHER_NO_GATHER
223 * If we can't allocate a page to make a big batch of page pointers
224 * to work on, then just handle a few from the on-stack structure.
226 #define MMU_GATHER_BUNDLE 8
228 struct mmu_gather_batch {
229 struct mmu_gather_batch *next;
232 struct page *pages[0];
235 #define MAX_GATHER_BATCH \
236 ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
239 * Limit the maximum number of mmu_gather batches to reduce a risk of soft
240 * lockups for non-preemptible kernels on huge machines when a lot of memory
241 * is zapped during unmapping.
242 * 10K pages freed at once should be safe even without a preemption point.
244 #define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH)
246 extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
251 * struct mmu_gather is an opaque type used by the mm code for passing around
252 * any data needed by arch specific code for tlb_remove_page.
255 struct mm_struct *mm;
257 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
258 struct mmu_table_batch *batch;
264 * we are in the middle of an operation to clear
265 * a full mm and can make some optimizations
267 unsigned int fullmm : 1;
270 * we have performed an operation which
271 * requires a complete flush of the tlb
273 unsigned int need_flush_all : 1;
276 * we have removed page directories
278 unsigned int freed_tables : 1;
281 * at which levels have we cleared entries?
283 unsigned int cleared_ptes : 1;
284 unsigned int cleared_pmds : 1;
285 unsigned int cleared_puds : 1;
286 unsigned int cleared_p4ds : 1;
289 * tracks VM_EXEC | VM_HUGETLB in tlb_start_vma
291 unsigned int vma_exec : 1;
292 unsigned int vma_huge : 1;
294 unsigned int batch_count;
296 #ifndef CONFIG_MMU_GATHER_NO_GATHER
297 struct mmu_gather_batch *active;
298 struct mmu_gather_batch local;
299 struct page *__pages[MMU_GATHER_BUNDLE];
301 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
302 unsigned int page_size;
307 void tlb_flush_mmu(struct mmu_gather *tlb);
309 static inline void __tlb_adjust_range(struct mmu_gather *tlb,
310 unsigned long address,
311 unsigned int range_size)
313 tlb->start = min(tlb->start, address);
314 tlb->end = max(tlb->end, address + range_size);
317 static inline void __tlb_reset_range(struct mmu_gather *tlb)
320 tlb->start = tlb->end = ~0;
322 tlb->start = TASK_SIZE;
325 tlb->freed_tables = 0;
326 tlb->cleared_ptes = 0;
327 tlb->cleared_pmds = 0;
328 tlb->cleared_puds = 0;
329 tlb->cleared_p4ds = 0;
331 * Do not reset mmu_gather::vma_* fields here, we do not
332 * call into tlb_start_vma() again to set them if there is an
333 * intermediate flush.
337 #ifdef CONFIG_MMU_GATHER_NO_RANGE
339 #if defined(tlb_flush) || defined(tlb_start_vma) || defined(tlb_end_vma)
340 #error MMU_GATHER_NO_RANGE relies on default tlb_flush(), tlb_start_vma() and tlb_end_vma()
344 * When an architecture does not have efficient means of range flushing TLBs
345 * there is no point in doing intermediate flushes on tlb_end_vma() to keep the
346 * range small. We equally don't have to worry about page granularity or other
349 * All we need to do is issue a full flush for any !0 range.
351 static inline void tlb_flush(struct mmu_gather *tlb)
354 flush_tlb_mm(tlb->mm);
358 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
360 #define tlb_end_vma tlb_end_vma
361 static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
363 #else /* CONFIG_MMU_GATHER_NO_RANGE */
367 #if defined(tlb_start_vma) || defined(tlb_end_vma)
368 #error Default tlb_flush() relies on default tlb_start_vma() and tlb_end_vma()
372 * When an architecture does not provide its own tlb_flush() implementation
373 * but does have a reasonably efficient flush_vma_range() implementation
376 static inline void tlb_flush(struct mmu_gather *tlb)
378 if (tlb->fullmm || tlb->need_flush_all) {
379 flush_tlb_mm(tlb->mm);
380 } else if (tlb->end) {
381 struct vm_area_struct vma = {
383 .vm_flags = (tlb->vma_exec ? VM_EXEC : 0) |
384 (tlb->vma_huge ? VM_HUGETLB : 0),
387 flush_tlb_range(&vma, tlb->start, tlb->end);
392 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma)
395 * flush_tlb_range() implementations that look at VM_HUGETLB (tile,
396 * mips-4k) flush only large pages.
398 * flush_tlb_range() implementations that flush I-TLB also flush D-TLB
399 * (tile, xtensa, arm), so it's ok to just add VM_EXEC to an existing
402 * We rely on tlb_end_vma() to issue a flush, such that when we reset
403 * these values the batch is empty.
405 tlb->vma_huge = is_vm_hugetlb_page(vma);
406 tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
412 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
416 #endif /* CONFIG_MMU_GATHER_NO_RANGE */
418 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
421 * Anything calling __tlb_adjust_range() also sets at least one of
424 if (!(tlb->freed_tables || tlb->cleared_ptes || tlb->cleared_pmds ||
425 tlb->cleared_puds || tlb->cleared_p4ds))
429 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
430 __tlb_reset_range(tlb);
433 static inline void tlb_remove_page_size(struct mmu_gather *tlb,
434 struct page *page, int page_size)
436 if (__tlb_remove_page_size(tlb, page, page_size))
440 static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
442 return __tlb_remove_page_size(tlb, page, PAGE_SIZE);
446 * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
449 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
451 return tlb_remove_page_size(tlb, page, PAGE_SIZE);
454 static inline void tlb_change_page_size(struct mmu_gather *tlb,
455 unsigned int page_size)
457 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
458 if (tlb->page_size && tlb->page_size != page_size) {
459 if (!tlb->fullmm && !tlb->need_flush_all)
463 tlb->page_size = page_size;
467 static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb)
469 if (tlb->cleared_ptes)
471 if (tlb->cleared_pmds)
473 if (tlb->cleared_puds)
475 if (tlb->cleared_p4ds)
481 static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb)
483 return 1UL << tlb_get_unmap_shift(tlb);
487 * In the case of tlb vma handling, we can optimise these away in the
488 * case where we're doing a full MM flush. When we're doing a munmap,
489 * the vmas are adjusted to only cover the region to be torn down.
491 #ifndef tlb_start_vma
492 static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
497 tlb_update_vma_flags(tlb, vma);
498 flush_cache_range(vma, vma->vm_start, vma->vm_end);
503 static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
509 * Do a TLB flush and reset the range at VMA boundaries; this avoids
510 * the ranges growing with the unused space between consecutive VMAs,
511 * but also the mmu_gather::vma_* flags from tlb_start_vma() rely on
514 tlb_flush_mmu_tlbonly(tlb);
519 * tlb_flush_{pte|pmd|pud|p4d}_range() adjust the tlb->start and tlb->end,
520 * and set corresponding cleared_*.
522 static inline void tlb_flush_pte_range(struct mmu_gather *tlb,
523 unsigned long address, unsigned long size)
525 __tlb_adjust_range(tlb, address, size);
526 tlb->cleared_ptes = 1;
529 static inline void tlb_flush_pmd_range(struct mmu_gather *tlb,
530 unsigned long address, unsigned long size)
532 __tlb_adjust_range(tlb, address, size);
533 tlb->cleared_pmds = 1;
536 static inline void tlb_flush_pud_range(struct mmu_gather *tlb,
537 unsigned long address, unsigned long size)
539 __tlb_adjust_range(tlb, address, size);
540 tlb->cleared_puds = 1;
543 static inline void tlb_flush_p4d_range(struct mmu_gather *tlb,
544 unsigned long address, unsigned long size)
546 __tlb_adjust_range(tlb, address, size);
547 tlb->cleared_p4ds = 1;
550 #ifndef __tlb_remove_tlb_entry
551 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
555 * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
557 * Record the fact that pte's were really unmapped by updating the range,
558 * so we can later optimise away the tlb invalidate. This helps when
559 * userspace is unmapping already-unmapped pages, which happens quite a lot.
561 #define tlb_remove_tlb_entry(tlb, ptep, address) \
563 tlb_flush_pte_range(tlb, address, PAGE_SIZE); \
564 __tlb_remove_tlb_entry(tlb, ptep, address); \
567 #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
569 unsigned long _sz = huge_page_size(h); \
570 if (_sz >= P4D_SIZE) \
571 tlb_flush_p4d_range(tlb, address, _sz); \
572 else if (_sz >= PUD_SIZE) \
573 tlb_flush_pud_range(tlb, address, _sz); \
574 else if (_sz >= PMD_SIZE) \
575 tlb_flush_pmd_range(tlb, address, _sz); \
577 tlb_flush_pte_range(tlb, address, _sz); \
578 __tlb_remove_tlb_entry(tlb, ptep, address); \
582 * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
583 * This is a nop so far, because only x86 needs it.
585 #ifndef __tlb_remove_pmd_tlb_entry
586 #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
589 #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
591 tlb_flush_pmd_range(tlb, address, HPAGE_PMD_SIZE); \
592 __tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
596 * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
597 * invalidation. This is a nop so far, because only x86 needs it.
599 #ifndef __tlb_remove_pud_tlb_entry
600 #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
603 #define tlb_remove_pud_tlb_entry(tlb, pudp, address) \
605 tlb_flush_pud_range(tlb, address, HPAGE_PUD_SIZE); \
606 __tlb_remove_pud_tlb_entry(tlb, pudp, address); \
610 * For things like page tables caches (ie caching addresses "inside" the
611 * page tables, like x86 does), for legacy reasons, flushing an
612 * individual page had better flush the page table caches behind it. This
613 * is definitely how x86 works, for example. And if you have an
614 * architected non-legacy page table cache (which I'm not aware of
615 * anybody actually doing), you're going to have some architecturally
616 * explicit flushing for that, likely *separate* from a regular TLB entry
617 * flush, and thus you'd need more than just some range expansion..
619 * So if we ever find an architecture
620 * that would want something that odd, I think it is up to that
621 * architecture to do its own odd thing, not cause pain for others
622 * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
624 * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
628 #define pte_free_tlb(tlb, ptep, address) \
630 tlb_flush_pmd_range(tlb, address, PAGE_SIZE); \
631 tlb->freed_tables = 1; \
632 __pte_free_tlb(tlb, ptep, address); \
637 #define pmd_free_tlb(tlb, pmdp, address) \
639 tlb_flush_pud_range(tlb, address, PAGE_SIZE); \
640 tlb->freed_tables = 1; \
641 __pmd_free_tlb(tlb, pmdp, address); \
646 #define pud_free_tlb(tlb, pudp, address) \
648 tlb_flush_p4d_range(tlb, address, PAGE_SIZE); \
649 tlb->freed_tables = 1; \
650 __pud_free_tlb(tlb, pudp, address); \
655 #define p4d_free_tlb(tlb, pudp, address) \
657 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
658 tlb->freed_tables = 1; \
659 __p4d_free_tlb(tlb, pudp, address); \
663 #endif /* CONFIG_MMU */
665 #endif /* _ASM_GENERIC__TLB_H */