xref: /linux-6.15/include/asm-generic/tlb.h (revision 92ec7fd1)
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
212 static inline void __tlb_remove_table(void *table)
213 {
214 	struct ptdesc *ptdesc = (struct ptdesc *)table;
215 
216 	pagetable_dtor(ptdesc);
217 	pagetable_free(ptdesc);
218 }
219 #endif
220 
221 extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
222 
223 #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
224 
225 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page);
226 /*
227  * Without MMU_GATHER_TABLE_FREE the architecture is assumed to have page based
228  * page directories and we can use the normal page batching to free them.
229  */
230 static inline void tlb_remove_table(struct mmu_gather *tlb, void *table)
231 {
232 	struct page *page = (struct page *)table;
233 
234 	pagetable_dtor(page_ptdesc(page));
235 	tlb_remove_page(tlb, page);
236 }
237 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
238 
239 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
240 /*
241  * This allows an architecture that does not use the linux page-tables for
242  * hardware to skip the TLBI when freeing page tables.
243  */
244 #ifndef tlb_needs_table_invalidate
245 #define tlb_needs_table_invalidate() (true)
246 #endif
247 
248 void tlb_remove_table_sync_one(void);
249 
250 #else
251 
252 #ifdef tlb_needs_table_invalidate
253 #error tlb_needs_table_invalidate() requires MMU_GATHER_RCU_TABLE_FREE
254 #endif
255 
256 static inline void tlb_remove_table_sync_one(void) { }
257 
258 #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
259 
260 
261 #ifndef CONFIG_MMU_GATHER_NO_GATHER
262 /*
263  * If we can't allocate a page to make a big batch of page pointers
264  * to work on, then just handle a few from the on-stack structure.
265  */
266 #define MMU_GATHER_BUNDLE	8
267 
268 struct mmu_gather_batch {
269 	struct mmu_gather_batch	*next;
270 	unsigned int		nr;
271 	unsigned int		max;
272 	struct encoded_page	*encoded_pages[];
273 };
274 
275 #define MAX_GATHER_BATCH	\
276 	((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
277 
278 /*
279  * Limit the maximum number of mmu_gather batches to reduce a risk of soft
280  * lockups for non-preemptible kernels on huge machines when a lot of memory
281  * is zapped during unmapping.
282  * 10K pages freed at once should be safe even without a preemption point.
283  */
284 #define MAX_GATHER_BATCH_COUNT	(10000UL/MAX_GATHER_BATCH)
285 
286 extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
287 		bool delay_rmap, int page_size);
288 bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page,
289 		unsigned int nr_pages, bool delay_rmap);
290 
291 #ifdef CONFIG_SMP
292 /*
293  * This both sets 'delayed_rmap', and returns true. It would be an inline
294  * function, except we define it before the 'struct mmu_gather'.
295  */
296 #define tlb_delay_rmap(tlb) (((tlb)->delayed_rmap = 1), true)
297 extern void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma);
298 #endif
299 
300 #endif
301 
302 /*
303  * We have a no-op version of the rmap removal that doesn't
304  * delay anything. That is used on S390, which flushes remote
305  * TLBs synchronously, and on UP, which doesn't have any
306  * remote TLBs to flush and is not preemptible due to this
307  * all happening under the page table lock.
308  */
309 #ifndef tlb_delay_rmap
310 #define tlb_delay_rmap(tlb) (false)
311 static inline void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
312 #endif
313 
314 /*
315  * struct mmu_gather is an opaque type used by the mm code for passing around
316  * any data needed by arch specific code for tlb_remove_page.
317  */
318 struct mmu_gather {
319 	struct mm_struct	*mm;
320 
321 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
322 	struct mmu_table_batch	*batch;
323 #endif
324 
325 	unsigned long		start;
326 	unsigned long		end;
327 	/*
328 	 * we are in the middle of an operation to clear
329 	 * a full mm and can make some optimizations
330 	 */
331 	unsigned int		fullmm : 1;
332 
333 	/*
334 	 * we have performed an operation which
335 	 * requires a complete flush of the tlb
336 	 */
337 	unsigned int		need_flush_all : 1;
338 
339 	/*
340 	 * we have removed page directories
341 	 */
342 	unsigned int		freed_tables : 1;
343 
344 	/*
345 	 * Do we have pending delayed rmap removals?
346 	 */
347 	unsigned int		delayed_rmap : 1;
348 
349 	/*
350 	 * at which levels have we cleared entries?
351 	 */
352 	unsigned int		cleared_ptes : 1;
353 	unsigned int		cleared_pmds : 1;
354 	unsigned int		cleared_puds : 1;
355 	unsigned int		cleared_p4ds : 1;
356 
357 	/*
358 	 * tracks VM_EXEC | VM_HUGETLB in tlb_start_vma
359 	 */
360 	unsigned int		vma_exec : 1;
361 	unsigned int		vma_huge : 1;
362 	unsigned int		vma_pfn  : 1;
363 
364 	unsigned int		batch_count;
365 
366 #ifndef CONFIG_MMU_GATHER_NO_GATHER
367 	struct mmu_gather_batch *active;
368 	struct mmu_gather_batch	local;
369 	struct page		*__pages[MMU_GATHER_BUNDLE];
370 
371 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
372 	unsigned int page_size;
373 #endif
374 #endif
375 };
376 
377 void tlb_flush_mmu(struct mmu_gather *tlb);
378 
379 static inline void __tlb_adjust_range(struct mmu_gather *tlb,
380 				      unsigned long address,
381 				      unsigned int range_size)
382 {
383 	tlb->start = min(tlb->start, address);
384 	tlb->end = max(tlb->end, address + range_size);
385 }
386 
387 static inline void __tlb_reset_range(struct mmu_gather *tlb)
388 {
389 	if (tlb->fullmm) {
390 		tlb->start = tlb->end = ~0;
391 	} else {
392 		tlb->start = TASK_SIZE;
393 		tlb->end = 0;
394 	}
395 	tlb->freed_tables = 0;
396 	tlb->cleared_ptes = 0;
397 	tlb->cleared_pmds = 0;
398 	tlb->cleared_puds = 0;
399 	tlb->cleared_p4ds = 0;
400 	/*
401 	 * Do not reset mmu_gather::vma_* fields here, we do not
402 	 * call into tlb_start_vma() again to set them if there is an
403 	 * intermediate flush.
404 	 */
405 }
406 
407 #ifdef CONFIG_MMU_GATHER_NO_RANGE
408 
409 #if defined(tlb_flush)
410 #error MMU_GATHER_NO_RANGE relies on default tlb_flush()
411 #endif
412 
413 /*
414  * When an architecture does not have efficient means of range flushing TLBs
415  * there is no point in doing intermediate flushes on tlb_end_vma() to keep the
416  * range small. We equally don't have to worry about page granularity or other
417  * things.
418  *
419  * All we need to do is issue a full flush for any !0 range.
420  */
421 static inline void tlb_flush(struct mmu_gather *tlb)
422 {
423 	if (tlb->end)
424 		flush_tlb_mm(tlb->mm);
425 }
426 
427 #else /* CONFIG_MMU_GATHER_NO_RANGE */
428 
429 #ifndef tlb_flush
430 /*
431  * When an architecture does not provide its own tlb_flush() implementation
432  * but does have a reasonably efficient flush_vma_range() implementation
433  * use that.
434  */
435 static inline void tlb_flush(struct mmu_gather *tlb)
436 {
437 	if (tlb->fullmm || tlb->need_flush_all) {
438 		flush_tlb_mm(tlb->mm);
439 	} else if (tlb->end) {
440 		struct vm_area_struct vma = {
441 			.vm_mm = tlb->mm,
442 			.vm_flags = (tlb->vma_exec ? VM_EXEC    : 0) |
443 				    (tlb->vma_huge ? VM_HUGETLB : 0),
444 		};
445 
446 		flush_tlb_range(&vma, tlb->start, tlb->end);
447 	}
448 }
449 #endif
450 
451 #endif /* CONFIG_MMU_GATHER_NO_RANGE */
452 
453 static inline void
454 tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma)
455 {
456 	/*
457 	 * flush_tlb_range() implementations that look at VM_HUGETLB (tile,
458 	 * mips-4k) flush only large pages.
459 	 *
460 	 * flush_tlb_range() implementations that flush I-TLB also flush D-TLB
461 	 * (tile, xtensa, arm), so it's ok to just add VM_EXEC to an existing
462 	 * range.
463 	 *
464 	 * We rely on tlb_end_vma() to issue a flush, such that when we reset
465 	 * these values the batch is empty.
466 	 */
467 	tlb->vma_huge = is_vm_hugetlb_page(vma);
468 	tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
469 	tlb->vma_pfn  = !!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP));
470 }
471 
472 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
473 {
474 	/*
475 	 * Anything calling __tlb_adjust_range() also sets at least one of
476 	 * these bits.
477 	 */
478 	if (!(tlb->freed_tables || tlb->cleared_ptes || tlb->cleared_pmds ||
479 	      tlb->cleared_puds || tlb->cleared_p4ds))
480 		return;
481 
482 	tlb_flush(tlb);
483 	__tlb_reset_range(tlb);
484 }
485 
486 static inline void tlb_remove_page_size(struct mmu_gather *tlb,
487 					struct page *page, int page_size)
488 {
489 	if (__tlb_remove_page_size(tlb, page, false, page_size))
490 		tlb_flush_mmu(tlb);
491 }
492 
493 static __always_inline bool __tlb_remove_page(struct mmu_gather *tlb,
494 		struct page *page, bool delay_rmap)
495 {
496 	return __tlb_remove_page_size(tlb, page, delay_rmap, PAGE_SIZE);
497 }
498 
499 /* tlb_remove_page
500  *	Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
501  *	required.
502  */
503 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
504 {
505 	return tlb_remove_page_size(tlb, page, PAGE_SIZE);
506 }
507 
508 static inline void tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt)
509 {
510 	tlb_remove_table(tlb, pt);
511 }
512 
513 /* Like tlb_remove_ptdesc, but for page-like page directories. */
514 static inline void tlb_remove_page_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt)
515 {
516 	tlb_remove_page(tlb, ptdesc_page(pt));
517 }
518 
519 static inline void tlb_change_page_size(struct mmu_gather *tlb,
520 						     unsigned int page_size)
521 {
522 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
523 	if (tlb->page_size && tlb->page_size != page_size) {
524 		if (!tlb->fullmm && !tlb->need_flush_all)
525 			tlb_flush_mmu(tlb);
526 	}
527 
528 	tlb->page_size = page_size;
529 #endif
530 }
531 
532 static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb)
533 {
534 	if (tlb->cleared_ptes)
535 		return PAGE_SHIFT;
536 	if (tlb->cleared_pmds)
537 		return PMD_SHIFT;
538 	if (tlb->cleared_puds)
539 		return PUD_SHIFT;
540 	if (tlb->cleared_p4ds)
541 		return P4D_SHIFT;
542 
543 	return PAGE_SHIFT;
544 }
545 
546 static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb)
547 {
548 	return 1UL << tlb_get_unmap_shift(tlb);
549 }
550 
551 /*
552  * In the case of tlb vma handling, we can optimise these away in the
553  * case where we're doing a full MM flush.  When we're doing a munmap,
554  * the vmas are adjusted to only cover the region to be torn down.
555  */
556 static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
557 {
558 	if (tlb->fullmm)
559 		return;
560 
561 	tlb_update_vma_flags(tlb, vma);
562 #ifndef CONFIG_MMU_GATHER_NO_FLUSH_CACHE
563 	flush_cache_range(vma, vma->vm_start, vma->vm_end);
564 #endif
565 }
566 
567 static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
568 {
569 	if (tlb->fullmm)
570 		return;
571 
572 	/*
573 	 * VM_PFNMAP is more fragile because the core mm will not track the
574 	 * page mapcount -- there might not be page-frames for these PFNs after
575 	 * all. Force flush TLBs for such ranges to avoid munmap() vs
576 	 * unmap_mapping_range() races.
577 	 */
578 	if (tlb->vma_pfn || !IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS)) {
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 		 */
583 		tlb_flush_mmu_tlbonly(tlb);
584 	}
585 }
586 
587 /*
588  * tlb_flush_{pte|pmd|pud|p4d}_range() adjust the tlb->start and tlb->end,
589  * and set corresponding cleared_*.
590  */
591 static inline void tlb_flush_pte_range(struct mmu_gather *tlb,
592 				     unsigned long address, unsigned long size)
593 {
594 	__tlb_adjust_range(tlb, address, size);
595 	tlb->cleared_ptes = 1;
596 }
597 
598 static inline void tlb_flush_pmd_range(struct mmu_gather *tlb,
599 				     unsigned long address, unsigned long size)
600 {
601 	__tlb_adjust_range(tlb, address, size);
602 	tlb->cleared_pmds = 1;
603 }
604 
605 static inline void tlb_flush_pud_range(struct mmu_gather *tlb,
606 				     unsigned long address, unsigned long size)
607 {
608 	__tlb_adjust_range(tlb, address, size);
609 	tlb->cleared_puds = 1;
610 }
611 
612 static inline void tlb_flush_p4d_range(struct mmu_gather *tlb,
613 				     unsigned long address, unsigned long size)
614 {
615 	__tlb_adjust_range(tlb, address, size);
616 	tlb->cleared_p4ds = 1;
617 }
618 
619 #ifndef __tlb_remove_tlb_entry
620 static inline void __tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long address)
621 {
622 }
623 #endif
624 
625 /**
626  * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
627  *
628  * Record the fact that pte's were really unmapped by updating the range,
629  * so we can later optimise away the tlb invalidate.   This helps when
630  * userspace is unmapping already-unmapped pages, which happens quite a lot.
631  */
632 #define tlb_remove_tlb_entry(tlb, ptep, address)		\
633 	do {							\
634 		tlb_flush_pte_range(tlb, address, PAGE_SIZE);	\
635 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
636 	} while (0)
637 
638 /**
639  * tlb_remove_tlb_entries - remember unmapping of multiple consecutive ptes for
640  *			    later tlb invalidation.
641  *
642  * Similar to tlb_remove_tlb_entry(), but remember unmapping of multiple
643  * consecutive ptes instead of only a single one.
644  */
645 static inline void tlb_remove_tlb_entries(struct mmu_gather *tlb,
646 		pte_t *ptep, unsigned int nr, unsigned long address)
647 {
648 	tlb_flush_pte_range(tlb, address, PAGE_SIZE * nr);
649 	for (;;) {
650 		__tlb_remove_tlb_entry(tlb, ptep, address);
651 		if (--nr == 0)
652 			break;
653 		ptep++;
654 		address += PAGE_SIZE;
655 	}
656 }
657 
658 #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address)	\
659 	do {							\
660 		unsigned long _sz = huge_page_size(h);		\
661 		if (_sz >= P4D_SIZE)				\
662 			tlb_flush_p4d_range(tlb, address, _sz);	\
663 		else if (_sz >= PUD_SIZE)			\
664 			tlb_flush_pud_range(tlb, address, _sz);	\
665 		else if (_sz >= PMD_SIZE)			\
666 			tlb_flush_pmd_range(tlb, address, _sz);	\
667 		else						\
668 			tlb_flush_pte_range(tlb, address, _sz);	\
669 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
670 	} while (0)
671 
672 /**
673  * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
674  * This is a nop so far, because only x86 needs it.
675  */
676 #ifndef __tlb_remove_pmd_tlb_entry
677 #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
678 #endif
679 
680 #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address)			\
681 	do {								\
682 		tlb_flush_pmd_range(tlb, address, HPAGE_PMD_SIZE);	\
683 		__tlb_remove_pmd_tlb_entry(tlb, pmdp, address);		\
684 	} while (0)
685 
686 /**
687  * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
688  * invalidation. This is a nop so far, because only x86 needs it.
689  */
690 #ifndef __tlb_remove_pud_tlb_entry
691 #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
692 #endif
693 
694 #define tlb_remove_pud_tlb_entry(tlb, pudp, address)			\
695 	do {								\
696 		tlb_flush_pud_range(tlb, address, HPAGE_PUD_SIZE);	\
697 		__tlb_remove_pud_tlb_entry(tlb, pudp, address);		\
698 	} while (0)
699 
700 /*
701  * For things like page tables caches (ie caching addresses "inside" the
702  * page tables, like x86 does), for legacy reasons, flushing an
703  * individual page had better flush the page table caches behind it. This
704  * is definitely how x86 works, for example. And if you have an
705  * architected non-legacy page table cache (which I'm not aware of
706  * anybody actually doing), you're going to have some architecturally
707  * explicit flushing for that, likely *separate* from a regular TLB entry
708  * flush, and thus you'd need more than just some range expansion..
709  *
710  * So if we ever find an architecture
711  * that would want something that odd, I think it is up to that
712  * architecture to do its own odd thing, not cause pain for others
713  * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
714  *
715  * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
716  */
717 
718 #ifndef pte_free_tlb
719 #define pte_free_tlb(tlb, ptep, address)			\
720 	do {							\
721 		tlb_flush_pmd_range(tlb, address, PAGE_SIZE);	\
722 		tlb->freed_tables = 1;				\
723 		__pte_free_tlb(tlb, ptep, address);		\
724 	} while (0)
725 #endif
726 
727 #ifndef pmd_free_tlb
728 #define pmd_free_tlb(tlb, pmdp, address)			\
729 	do {							\
730 		tlb_flush_pud_range(tlb, address, PAGE_SIZE);	\
731 		tlb->freed_tables = 1;				\
732 		__pmd_free_tlb(tlb, pmdp, address);		\
733 	} while (0)
734 #endif
735 
736 #ifndef pud_free_tlb
737 #define pud_free_tlb(tlb, pudp, address)			\
738 	do {							\
739 		tlb_flush_p4d_range(tlb, address, PAGE_SIZE);	\
740 		tlb->freed_tables = 1;				\
741 		__pud_free_tlb(tlb, pudp, address);		\
742 	} while (0)
743 #endif
744 
745 #ifndef p4d_free_tlb
746 #define p4d_free_tlb(tlb, pudp, address)			\
747 	do {							\
748 		__tlb_adjust_range(tlb, address, PAGE_SIZE);	\
749 		tlb->freed_tables = 1;				\
750 		__p4d_free_tlb(tlb, pudp, address);		\
751 	} while (0)
752 #endif
753 
754 #ifndef pte_needs_flush
755 static inline bool pte_needs_flush(pte_t oldpte, pte_t newpte)
756 {
757 	return true;
758 }
759 #endif
760 
761 #ifndef huge_pmd_needs_flush
762 static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)
763 {
764 	return true;
765 }
766 #endif
767 
768 #endif /* CONFIG_MMU */
769 
770 #endif /* _ASM_GENERIC__TLB_H */
771