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