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 <asm/pgalloc.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_page() / __tlb_remove_page()
60 * - tlb_remove_page_size() / __tlb_remove_page_size()
62 * __tlb_remove_page_size() is the basic primitive that queues a page for
63 * freeing. __tlb_remove_page() assumes PAGE_SIZE. Both will return a
64 * boolean indicating if the queue is (now) full and a call to
65 * tlb_flush_mmu() is required.
67 * tlb_remove_page() and tlb_remove_page_size() imply the call to
68 * tlb_flush_mmu() when required and has no return value.
70 * - tlb_change_page_size()
72 * call before __tlb_remove_page*() to set the current page-size; implies a
73 * possible tlb_flush_mmu() call.
75 * - tlb_flush_mmu() / tlb_flush_mmu_tlbonly()
77 * tlb_flush_mmu_tlbonly() - does the TLB invalidate (and resets
78 * related state, like the range)
80 * tlb_flush_mmu() - in addition to the above TLB invalidate, also frees
81 * whatever pages are still batched.
83 * - mmu_gather::fullmm
85 * A flag set by tlb_gather_mmu() to indicate we're going to free
86 * the entire mm; this allows a number of optimizations.
88 * - We can ignore tlb_{start,end}_vma(); because we don't
89 * care about ranges. Everything will be shot down.
91 * - (RISC) architectures that use ASIDs can cycle to a new ASID
92 * and delay the invalidation until ASID space runs out.
94 * - mmu_gather::need_flush_all
96 * A flag that can be set by the arch code if it wants to force
97 * flush the entire TLB irrespective of the range. For instance
98 * x86-PAE needs this when changing top-level entries.
100 * And allows the architecture to provide and implement tlb_flush():
102 * tlb_flush() may, in addition to the above mentioned mmu_gather fields, make
105 * - mmu_gather::start / mmu_gather::end
107 * which provides the range that needs to be flushed to cover the pages to
110 * - mmu_gather::freed_tables
112 * set when we freed page table pages
114 * - tlb_get_unmap_shift() / tlb_get_unmap_size()
116 * returns the smallest TLB entry size unmapped in this range.
118 * If an architecture does not provide tlb_flush() a default implementation
119 * based on flush_tlb_range() will be used, unless MMU_GATHER_NO_RANGE is
120 * specified, in which case we'll default to flush_tlb_mm().
122 * Additionally there are a few opt-in features:
124 * HAVE_MMU_GATHER_PAGE_SIZE
126 * This ensures we call tlb_flush() every time tlb_change_page_size() actually
127 * changes the size and provides mmu_gather::page_size to tlb_flush().
129 * HAVE_RCU_TABLE_FREE
131 * This provides tlb_remove_table(), to be used instead of tlb_remove_page()
132 * for page directores (__p*_free_tlb()). This provides separate freeing of
133 * the page-table pages themselves in a semi-RCU fashion (see comment below).
134 * Useful if your architecture doesn't use IPIs for remote TLB invalidates
135 * and therefore doesn't naturally serialize with software page-table walkers.
137 * When used, an architecture is expected to provide __tlb_remove_table()
138 * which does the actual freeing of these pages.
140 * HAVE_RCU_TABLE_NO_INVALIDATE
142 * This makes HAVE_RCU_TABLE_FREE avoid calling tlb_flush_mmu_tlbonly() before
143 * freeing the page-table pages. This can be avoided if you use
144 * HAVE_RCU_TABLE_FREE and your architecture does _NOT_ use the Linux
145 * page-tables natively.
147 * MMU_GATHER_NO_RANGE
149 * Use this if your architecture lacks an efficient flush_tlb_range().
152 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
154 * Semi RCU freeing of the page directories.
156 * This is needed by some architectures to implement software pagetable walkers.
158 * gup_fast() and other software pagetable walkers do a lockless page-table
159 * walk and therefore needs some synchronization with the freeing of the page
160 * directories. The chosen means to accomplish that is by disabling IRQs over
163 * Architectures that use IPIs to flush TLBs will then automagically DTRT,
164 * since we unlink the page, flush TLBs, free the page. Since the disabling of
165 * IRQs delays the completion of the TLB flush we can never observe an already
168 * Architectures that do not have this (PPC) need to delay the freeing by some
169 * other means, this is that means.
171 * What we do is batch the freed directory pages (tables) and RCU free them.
172 * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
173 * holds off grace periods.
175 * However, in order to batch these pages we need to allocate storage, this
176 * allocation is deep inside the MM code and can thus easily fail on memory
177 * pressure. To guarantee progress we fall back to single table freeing, see
178 * the implementation of tlb_remove_table_one().
181 struct mmu_table_batch {
187 #define MAX_TABLE_BATCH \
188 ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
190 extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
194 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
196 * If we can't allocate a page to make a big batch of page pointers
197 * to work on, then just handle a few from the on-stack structure.
199 #define MMU_GATHER_BUNDLE 8
201 struct mmu_gather_batch {
202 struct mmu_gather_batch *next;
205 struct page *pages[0];
208 #define MAX_GATHER_BATCH \
209 ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
212 * Limit the maximum number of mmu_gather batches to reduce a risk of soft
213 * lockups for non-preemptible kernels on huge machines when a lot of memory
214 * is zapped during unmapping.
215 * 10K pages freed at once should be safe even without a preemption point.
217 #define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH)
219 extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
224 * struct mmu_gather is an opaque type used by the mm code for passing around
225 * any data needed by arch specific code for tlb_remove_page.
228 struct mm_struct *mm;
230 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
231 struct mmu_table_batch *batch;
237 * we are in the middle of an operation to clear
238 * a full mm and can make some optimizations
240 unsigned int fullmm : 1;
243 * we have performed an operation which
244 * requires a complete flush of the tlb
246 unsigned int need_flush_all : 1;
249 * we have removed page directories
251 unsigned int freed_tables : 1;
254 * at which levels have we cleared entries?
256 unsigned int cleared_ptes : 1;
257 unsigned int cleared_pmds : 1;
258 unsigned int cleared_puds : 1;
259 unsigned int cleared_p4ds : 1;
262 * tracks VM_EXEC | VM_HUGETLB in tlb_start_vma
264 unsigned int vma_exec : 1;
265 unsigned int vma_huge : 1;
267 unsigned int batch_count;
269 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
270 struct mmu_gather_batch *active;
271 struct mmu_gather_batch local;
272 struct page *__pages[MMU_GATHER_BUNDLE];
274 #ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
275 unsigned int page_size;
280 void arch_tlb_gather_mmu(struct mmu_gather *tlb,
281 struct mm_struct *mm, unsigned long start, unsigned long end);
282 void tlb_flush_mmu(struct mmu_gather *tlb);
283 void arch_tlb_finish_mmu(struct mmu_gather *tlb,
284 unsigned long start, unsigned long end, bool force);
286 static inline void __tlb_adjust_range(struct mmu_gather *tlb,
287 unsigned long address,
288 unsigned int range_size)
290 tlb->start = min(tlb->start, address);
291 tlb->end = max(tlb->end, address + range_size);
294 static inline void __tlb_reset_range(struct mmu_gather *tlb)
297 tlb->start = tlb->end = ~0;
299 tlb->start = TASK_SIZE;
302 tlb->freed_tables = 0;
303 tlb->cleared_ptes = 0;
304 tlb->cleared_pmds = 0;
305 tlb->cleared_puds = 0;
306 tlb->cleared_p4ds = 0;
308 * Do not reset mmu_gather::vma_* fields here, we do not
309 * call into tlb_start_vma() again to set them if there is an
310 * intermediate flush.
314 #ifdef CONFIG_MMU_GATHER_NO_RANGE
316 #if defined(tlb_flush) || defined(tlb_start_vma) || defined(tlb_end_vma)
317 #error MMU_GATHER_NO_RANGE relies on default tlb_flush(), tlb_start_vma() and tlb_end_vma()
321 * When an architecture does not have efficient means of range flushing TLBs
322 * there is no point in doing intermediate flushes on tlb_end_vma() to keep the
323 * range small. We equally don't have to worry about page granularity or other
326 * All we need to do is issue a full flush for any !0 range.
328 static inline void tlb_flush(struct mmu_gather *tlb)
331 flush_tlb_mm(tlb->mm);
335 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
337 #define tlb_end_vma tlb_end_vma
338 static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
340 #else /* CONFIG_MMU_GATHER_NO_RANGE */
344 #if defined(tlb_start_vma) || defined(tlb_end_vma)
345 #error Default tlb_flush() relies on default tlb_start_vma() and tlb_end_vma()
349 * When an architecture does not provide its own tlb_flush() implementation
350 * but does have a reasonably efficient flush_vma_range() implementation
353 static inline void tlb_flush(struct mmu_gather *tlb)
355 if (tlb->fullmm || tlb->need_flush_all) {
356 flush_tlb_mm(tlb->mm);
357 } else if (tlb->end) {
358 struct vm_area_struct vma = {
360 .vm_flags = (tlb->vma_exec ? VM_EXEC : 0) |
361 (tlb->vma_huge ? VM_HUGETLB : 0),
364 flush_tlb_range(&vma, tlb->start, tlb->end);
369 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma)
372 * flush_tlb_range() implementations that look at VM_HUGETLB (tile,
373 * mips-4k) flush only large pages.
375 * flush_tlb_range() implementations that flush I-TLB also flush D-TLB
376 * (tile, xtensa, arm), so it's ok to just add VM_EXEC to an existing
379 * We rely on tlb_end_vma() to issue a flush, such that when we reset
380 * these values the batch is empty.
382 tlb->vma_huge = !!(vma->vm_flags & VM_HUGETLB);
383 tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
389 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
393 #endif /* CONFIG_MMU_GATHER_NO_RANGE */
395 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
401 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
402 __tlb_reset_range(tlb);
405 static inline void tlb_remove_page_size(struct mmu_gather *tlb,
406 struct page *page, int page_size)
408 if (__tlb_remove_page_size(tlb, page, page_size))
412 static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
414 return __tlb_remove_page_size(tlb, page, PAGE_SIZE);
418 * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
421 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
423 return tlb_remove_page_size(tlb, page, PAGE_SIZE);
426 static inline void tlb_change_page_size(struct mmu_gather *tlb,
427 unsigned int page_size)
429 #ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
430 if (tlb->page_size && tlb->page_size != page_size) {
435 tlb->page_size = page_size;
439 static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb)
441 if (tlb->cleared_ptes)
443 if (tlb->cleared_pmds)
445 if (tlb->cleared_puds)
447 if (tlb->cleared_p4ds)
453 static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb)
455 return 1UL << tlb_get_unmap_shift(tlb);
459 * In the case of tlb vma handling, we can optimise these away in the
460 * case where we're doing a full MM flush. When we're doing a munmap,
461 * the vmas are adjusted to only cover the region to be torn down.
463 #ifndef tlb_start_vma
464 static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
469 tlb_update_vma_flags(tlb, vma);
470 flush_cache_range(vma, vma->vm_start, vma->vm_end);
475 static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
481 * Do a TLB flush and reset the range at VMA boundaries; this avoids
482 * the ranges growing with the unused space between consecutive VMAs,
483 * but also the mmu_gather::vma_* flags from tlb_start_vma() rely on
486 tlb_flush_mmu_tlbonly(tlb);
490 #ifndef __tlb_remove_tlb_entry
491 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
495 * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
497 * Record the fact that pte's were really unmapped by updating the range,
498 * so we can later optimise away the tlb invalidate. This helps when
499 * userspace is unmapping already-unmapped pages, which happens quite a lot.
501 #define tlb_remove_tlb_entry(tlb, ptep, address) \
503 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
504 tlb->cleared_ptes = 1; \
505 __tlb_remove_tlb_entry(tlb, ptep, address); \
508 #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
510 unsigned long _sz = huge_page_size(h); \
511 __tlb_adjust_range(tlb, address, _sz); \
512 if (_sz == PMD_SIZE) \
513 tlb->cleared_pmds = 1; \
514 else if (_sz == PUD_SIZE) \
515 tlb->cleared_puds = 1; \
516 __tlb_remove_tlb_entry(tlb, ptep, address); \
520 * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
521 * This is a nop so far, because only x86 needs it.
523 #ifndef __tlb_remove_pmd_tlb_entry
524 #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
527 #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
529 __tlb_adjust_range(tlb, address, HPAGE_PMD_SIZE); \
530 tlb->cleared_pmds = 1; \
531 __tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
535 * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
536 * invalidation. This is a nop so far, because only x86 needs it.
538 #ifndef __tlb_remove_pud_tlb_entry
539 #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
542 #define tlb_remove_pud_tlb_entry(tlb, pudp, address) \
544 __tlb_adjust_range(tlb, address, HPAGE_PUD_SIZE); \
545 tlb->cleared_puds = 1; \
546 __tlb_remove_pud_tlb_entry(tlb, pudp, address); \
550 * For things like page tables caches (ie caching addresses "inside" the
551 * page tables, like x86 does), for legacy reasons, flushing an
552 * individual page had better flush the page table caches behind it. This
553 * is definitely how x86 works, for example. And if you have an
554 * architected non-legacy page table cache (which I'm not aware of
555 * anybody actually doing), you're going to have some architecturally
556 * explicit flushing for that, likely *separate* from a regular TLB entry
557 * flush, and thus you'd need more than just some range expansion..
559 * So if we ever find an architecture
560 * that would want something that odd, I think it is up to that
561 * architecture to do its own odd thing, not cause pain for others
562 * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
564 * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
568 #define pte_free_tlb(tlb, ptep, address) \
570 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
571 tlb->freed_tables = 1; \
572 tlb->cleared_pmds = 1; \
573 __pte_free_tlb(tlb, ptep, address); \
578 #define pmd_free_tlb(tlb, pmdp, address) \
580 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
581 tlb->freed_tables = 1; \
582 tlb->cleared_puds = 1; \
583 __pmd_free_tlb(tlb, pmdp, address); \
587 #ifndef __ARCH_HAS_4LEVEL_HACK
589 #define pud_free_tlb(tlb, pudp, address) \
591 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
592 tlb->freed_tables = 1; \
593 tlb->cleared_p4ds = 1; \
594 __pud_free_tlb(tlb, pudp, address); \
599 #ifndef __ARCH_HAS_5LEVEL_HACK
601 #define p4d_free_tlb(tlb, pudp, address) \
603 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
604 tlb->freed_tables = 1; \
605 __p4d_free_tlb(tlb, pudp, address); \
610 #endif /* CONFIG_MMU */
612 #endif /* _ASM_GENERIC__TLB_H */