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