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