blob: 6850d8bab6d3636cd9ec230f6494ee8405887803 [file] [log] [blame]
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02001/* include/asm-generic/tlb.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Generic TLB shootdown code
4 *
5 * Copyright 2001 Red Hat, Inc.
6 * Based on code from mm/memory.c Copyright Linus Torvalds and others.
7 *
Peter Zijlstra90eec102015-11-16 11:08:45 +01008 * Copyright 2011 Red Hat, Inc., Peter Zijlstra
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15#ifndef _ASM_GENERIC__TLB_H
16#define _ASM_GENERIC__TLB_H
17
Nicholas Pigginfd1102f2018-08-23 18:47:09 +100018#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/swap.h>
Ingo Molnar62152d02008-01-31 22:05:48 +010020#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/tlbflush.h>
Peter Zijlstrae7fd28a2018-08-27 13:00:17 +020022#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Will Deaconfaaadaf2018-08-24 13:28:28 +010024#ifdef CONFIG_MMU
25
Peter Zijlstradea24342018-09-04 10:43:14 +020026/*
27 * Generic MMU-gather implementation.
28 *
29 * The mmu_gather data structure is used by the mm code to implement the
30 * correct and efficient ordering of freeing pages and TLB invalidations.
31 *
32 * This correct ordering is:
33 *
34 * 1) unhook page
35 * 2) TLB invalidate page
36 * 3) free page
37 *
38 * That is, we must never free a page before we have ensured there are no live
39 * translations left to it. Otherwise it might be possible to observe (or
40 * worse, change) the page content after it has been reused.
41 *
42 * The mmu_gather API consists of:
43 *
44 * - tlb_gather_mmu() / tlb_finish_mmu(); start and finish a mmu_gather
45 *
46 * Finish in particular will issue a (final) TLB invalidate and free
47 * all (remaining) queued pages.
48 *
49 * - tlb_start_vma() / tlb_end_vma(); marks the start / end of a VMA
50 *
51 * Defaults to flushing at tlb_end_vma() to reset the range; helps when
52 * there's large holes between the VMAs.
53 *
54 * - tlb_remove_page() / __tlb_remove_page()
55 * - tlb_remove_page_size() / __tlb_remove_page_size()
56 *
57 * __tlb_remove_page_size() is the basic primitive that queues a page for
58 * freeing. __tlb_remove_page() assumes PAGE_SIZE. Both will return a
59 * boolean indicating if the queue is (now) full and a call to
60 * tlb_flush_mmu() is required.
61 *
62 * tlb_remove_page() and tlb_remove_page_size() imply the call to
63 * tlb_flush_mmu() when required and has no return value.
64 *
Peter Zijlstraed6a7932018-08-31 14:46:08 +020065 * - tlb_change_page_size()
Peter Zijlstradea24342018-09-04 10:43:14 +020066 *
67 * call before __tlb_remove_page*() to set the current page-size; implies a
68 * possible tlb_flush_mmu() call.
69 *
70 * - tlb_flush_mmu() / tlb_flush_mmu_tlbonly() / tlb_flush_mmu_free()
71 *
72 * tlb_flush_mmu_tlbonly() - does the TLB invalidate (and resets
73 * related state, like the range)
74 *
75 * tlb_flush_mmu_free() - frees the queued pages; make absolutely
76 * sure no additional tlb_remove_page()
77 * calls happen between _tlbonly() and this.
78 *
79 * tlb_flush_mmu() - the above two calls.
80 *
81 * - mmu_gather::fullmm
82 *
83 * A flag set by tlb_gather_mmu() to indicate we're going to free
84 * the entire mm; this allows a number of optimizations.
85 *
86 * - We can ignore tlb_{start,end}_vma(); because we don't
87 * care about ranges. Everything will be shot down.
88 *
89 * - (RISC) architectures that use ASIDs can cycle to a new ASID
90 * and delay the invalidation until ASID space runs out.
91 *
92 * - mmu_gather::need_flush_all
93 *
94 * A flag that can be set by the arch code if it wants to force
95 * flush the entire TLB irrespective of the range. For instance
96 * x86-PAE needs this when changing top-level entries.
97 *
Peter Zijlstra5f307be2018-09-04 13:18:15 +020098 * And allows the architecture to provide and implement tlb_flush():
Peter Zijlstradea24342018-09-04 10:43:14 +020099 *
100 * tlb_flush() may, in addition to the above mentioned mmu_gather fields, make
101 * use of:
102 *
103 * - mmu_gather::start / mmu_gather::end
104 *
105 * which provides the range that needs to be flushed to cover the pages to
106 * be freed.
107 *
108 * - mmu_gather::freed_tables
109 *
110 * set when we freed page table pages
111 *
112 * - tlb_get_unmap_shift() / tlb_get_unmap_size()
113 *
Peter Zijlstra5f307be2018-09-04 13:18:15 +0200114 * returns the smallest TLB entry size unmapped in this range.
115 *
116 * If an architecture does not provide tlb_flush() a default implementation
Peter Zijlstraa30e32b2018-10-11 16:51:51 +0200117 * based on flush_tlb_range() will be used, unless MMU_GATHER_NO_RANGE is
118 * specified, in which case we'll default to flush_tlb_mm().
Peter Zijlstradea24342018-09-04 10:43:14 +0200119 *
120 * Additionally there are a few opt-in features:
121 *
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200122 * HAVE_MMU_GATHER_PAGE_SIZE
123 *
124 * This ensures we call tlb_flush() every time tlb_change_page_size() actually
125 * changes the size and provides mmu_gather::page_size to tlb_flush().
126 *
Peter Zijlstradea24342018-09-04 10:43:14 +0200127 * HAVE_RCU_TABLE_FREE
128 *
129 * This provides tlb_remove_table(), to be used instead of tlb_remove_page()
130 * for page directores (__p*_free_tlb()). This provides separate freeing of
131 * the page-table pages themselves in a semi-RCU fashion (see comment below).
132 * Useful if your architecture doesn't use IPIs for remote TLB invalidates
133 * and therefore doesn't naturally serialize with software page-table walkers.
134 *
135 * When used, an architecture is expected to provide __tlb_remove_table()
136 * which does the actual freeing of these pages.
137 *
138 * HAVE_RCU_TABLE_INVALIDATE
139 *
140 * This makes HAVE_RCU_TABLE_FREE call tlb_flush_mmu_tlbonly() before freeing
141 * the page-table pages. Required if you use HAVE_RCU_TABLE_FREE and your
142 * architecture uses the Linux page-tables natively.
143 *
Peter Zijlstraa30e32b2018-10-11 16:51:51 +0200144 * MMU_GATHER_NO_RANGE
145 *
146 * Use this if your architecture lacks an efficient flush_tlb_range().
Peter Zijlstradea24342018-09-04 10:43:14 +0200147 */
148#define HAVE_GENERIC_MMU_GATHER
149
Peter Zijlstra26723912011-05-24 17:12:00 -0700150#ifdef CONFIG_HAVE_RCU_TABLE_FREE
151/*
152 * Semi RCU freeing of the page directories.
153 *
154 * This is needed by some architectures to implement software pagetable walkers.
155 *
156 * gup_fast() and other software pagetable walkers do a lockless page-table
157 * walk and therefore needs some synchronization with the freeing of the page
158 * directories. The chosen means to accomplish that is by disabling IRQs over
159 * the walk.
160 *
161 * Architectures that use IPIs to flush TLBs will then automagically DTRT,
162 * since we unlink the page, flush TLBs, free the page. Since the disabling of
163 * IRQs delays the completion of the TLB flush we can never observe an already
164 * freed page.
165 *
166 * Architectures that do not have this (PPC) need to delay the freeing by some
167 * other means, this is that means.
168 *
169 * What we do is batch the freed directory pages (tables) and RCU free them.
170 * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
171 * holds off grace periods.
172 *
173 * However, in order to batch these pages we need to allocate storage, this
174 * allocation is deep inside the MM code and can thus easily fail on memory
175 * pressure. To guarantee progress we fall back to single table freeing, see
176 * the implementation of tlb_remove_table_one().
177 *
178 */
179struct mmu_table_batch {
180 struct rcu_head rcu;
181 unsigned int nr;
182 void *tables[0];
183};
184
185#define MAX_TABLE_BATCH \
186 ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
187
188extern void tlb_table_flush(struct mmu_gather *tlb);
189extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
190
191#endif
192
Peter Zijlstrad16dfc52011-05-24 17:11:45 -0700193/*
194 * If we can't allocate a page to make a big batch of page pointers
195 * to work on, then just handle a few from the on-stack structure.
196 */
197#define MMU_GATHER_BUNDLE 8
198
Peter Zijlstrae3032972011-05-24 17:12:01 -0700199struct mmu_gather_batch {
200 struct mmu_gather_batch *next;
201 unsigned int nr;
202 unsigned int max;
203 struct page *pages[0];
204};
205
206#define MAX_GATHER_BATCH \
207 ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
208
Michal Hocko53a59fc2013-01-04 15:35:12 -0800209/*
210 * Limit the maximum number of mmu_gather batches to reduce a risk of soft
211 * lockups for non-preemptible kernels on huge machines when a lot of memory
212 * is zapped during unmapping.
213 * 10K pages freed at once should be safe even without a preemption point.
214 */
215#define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH)
216
Peter Zijlstradea24342018-09-04 10:43:14 +0200217/*
218 * struct mmu_gather is an opaque type used by the mm code for passing around
Hugh Dickins15a23ff2005-10-29 18:16:01 -0700219 * any data needed by arch specific code for tlb_remove_page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221struct mmu_gather {
222 struct mm_struct *mm;
Peter Zijlstradea24342018-09-04 10:43:14 +0200223
Peter Zijlstra26723912011-05-24 17:12:00 -0700224#ifdef CONFIG_HAVE_RCU_TABLE_FREE
225 struct mmu_table_batch *batch;
226#endif
Peter Zijlstradea24342018-09-04 10:43:14 +0200227
Alex Shi597e1c32012-06-28 09:02:21 +0800228 unsigned long start;
229 unsigned long end;
Peter Zijlstra22a61c32018-08-23 20:27:25 +0100230 /*
231 * we are in the middle of an operation to clear
232 * a full mm and can make some optimizations
233 */
234 unsigned int fullmm : 1;
235
236 /*
237 * we have performed an operation which
238 * requires a complete flush of the tlb
239 */
240 unsigned int need_flush_all : 1;
241
242 /*
243 * we have removed page directories
244 */
245 unsigned int freed_tables : 1;
Peter Zijlstrae3032972011-05-24 17:12:01 -0700246
Will Deacona6d60242018-08-23 21:01:46 +0100247 /*
248 * at which levels have we cleared entries?
249 */
250 unsigned int cleared_ptes : 1;
251 unsigned int cleared_pmds : 1;
252 unsigned int cleared_puds : 1;
253 unsigned int cleared_p4ds : 1;
254
Peter Zijlstra5f307be2018-09-04 13:18:15 +0200255 /*
256 * tracks VM_EXEC | VM_HUGETLB in tlb_start_vma
257 */
258 unsigned int vma_exec : 1;
259 unsigned int vma_huge : 1;
260
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200261 unsigned int batch_count;
262
Peter Zijlstrae3032972011-05-24 17:12:01 -0700263 struct mmu_gather_batch *active;
264 struct mmu_gather_batch local;
265 struct page *__pages[MMU_GATHER_BUNDLE];
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200266
267#ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
268 unsigned int page_size;
269#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270};
271
Minchan Kim56236a52017-08-10 15:24:05 -0700272void arch_tlb_gather_mmu(struct mmu_gather *tlb,
273 struct mm_struct *mm, unsigned long start, unsigned long end);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700274void tlb_flush_mmu(struct mmu_gather *tlb);
Minchan Kim56236a52017-08-10 15:24:05 -0700275void arch_tlb_finish_mmu(struct mmu_gather *tlb,
Minchan Kim99baac22017-08-10 15:24:12 -0700276 unsigned long start, unsigned long end, bool force);
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100277void tlb_flush_mmu_free(struct mmu_gather *tlb);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700278extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
279 int page_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Will Deaconfb7332a2014-10-29 10:03:09 +0000281static inline void __tlb_adjust_range(struct mmu_gather *tlb,
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800282 unsigned long address,
283 unsigned int range_size)
Will Deaconfb7332a2014-10-29 10:03:09 +0000284{
285 tlb->start = min(tlb->start, address);
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800286 tlb->end = max(tlb->end, address + range_size);
Will Deaconfb7332a2014-10-29 10:03:09 +0000287}
288
289static inline void __tlb_reset_range(struct mmu_gather *tlb)
290{
Will Deacon721c21c2015-01-12 19:10:55 +0000291 if (tlb->fullmm) {
292 tlb->start = tlb->end = ~0;
293 } else {
294 tlb->start = TASK_SIZE;
295 tlb->end = 0;
296 }
Peter Zijlstra22a61c32018-08-23 20:27:25 +0100297 tlb->freed_tables = 0;
Will Deacona6d60242018-08-23 21:01:46 +0100298 tlb->cleared_ptes = 0;
299 tlb->cleared_pmds = 0;
300 tlb->cleared_puds = 0;
301 tlb->cleared_p4ds = 0;
Peter Zijlstra5f307be2018-09-04 13:18:15 +0200302 /*
303 * Do not reset mmu_gather::vma_* fields here, we do not
304 * call into tlb_start_vma() again to set them if there is an
305 * intermediate flush.
306 */
Will Deaconfb7332a2014-10-29 10:03:09 +0000307}
308
Peter Zijlstraa30e32b2018-10-11 16:51:51 +0200309#ifdef CONFIG_MMU_GATHER_NO_RANGE
310
311#if defined(tlb_flush) || defined(tlb_start_vma) || defined(tlb_end_vma)
312#error MMU_GATHER_NO_RANGE relies on default tlb_flush(), tlb_start_vma() and tlb_end_vma()
313#endif
314
315/*
316 * When an architecture does not have efficient means of range flushing TLBs
317 * there is no point in doing intermediate flushes on tlb_end_vma() to keep the
318 * range small. We equally don't have to worry about page granularity or other
319 * things.
320 *
321 * All we need to do is issue a full flush for any !0 range.
322 */
323static inline void tlb_flush(struct mmu_gather *tlb)
324{
325 if (tlb->end)
326 flush_tlb_mm(tlb->mm);
327}
328
329static inline void
330tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
331
332#define tlb_end_vma tlb_end_vma
333static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
334
335#else /* CONFIG_MMU_GATHER_NO_RANGE */
336
Peter Zijlstra5f307be2018-09-04 13:18:15 +0200337#ifndef tlb_flush
338
339#if defined(tlb_start_vma) || defined(tlb_end_vma)
340#error Default tlb_flush() relies on default tlb_start_vma() and tlb_end_vma()
341#endif
342
Peter Zijlstraa30e32b2018-10-11 16:51:51 +0200343/*
344 * When an architecture does not provide its own tlb_flush() implementation
345 * but does have a reasonably efficient flush_vma_range() implementation
346 * use that.
347 */
Peter Zijlstra5f307be2018-09-04 13:18:15 +0200348static inline void tlb_flush(struct mmu_gather *tlb)
349{
350 if (tlb->fullmm || tlb->need_flush_all) {
351 flush_tlb_mm(tlb->mm);
352 } else if (tlb->end) {
353 struct vm_area_struct vma = {
354 .vm_mm = tlb->mm,
355 .vm_flags = (tlb->vma_exec ? VM_EXEC : 0) |
356 (tlb->vma_huge ? VM_HUGETLB : 0),
357 };
358
359 flush_tlb_range(&vma, tlb->start, tlb->end);
360 }
361}
362
363static inline void
364tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma)
365{
366 /*
367 * flush_tlb_range() implementations that look at VM_HUGETLB (tile,
368 * mips-4k) flush only large pages.
369 *
370 * flush_tlb_range() implementations that flush I-TLB also flush D-TLB
371 * (tile, xtensa, arm), so it's ok to just add VM_EXEC to an existing
372 * range.
373 *
374 * We rely on tlb_end_vma() to issue a flush, such that when we reset
375 * these values the batch is empty.
376 */
377 tlb->vma_huge = !!(vma->vm_flags & VM_HUGETLB);
378 tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
379}
380
381#else
382
383static inline void
384tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
385
386#endif
387
Peter Zijlstraa30e32b2018-10-11 16:51:51 +0200388#endif /* CONFIG_MMU_GATHER_NO_RANGE */
389
Nicholas Pigginfd1102f2018-08-23 18:47:09 +1000390static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
391{
392 if (!tlb->end)
393 return;
394
395 tlb_flush(tlb);
396 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
397 __tlb_reset_range(tlb);
398}
399
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700400static inline void tlb_remove_page_size(struct mmu_gather *tlb,
401 struct page *page, int page_size)
402{
Aneesh Kumar K.V692a68c2016-12-12 16:42:43 -0800403 if (__tlb_remove_page_size(tlb, page, page_size))
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700404 tlb_flush_mmu(tlb);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700405}
406
Aneesh Kumar K.V692a68c2016-12-12 16:42:43 -0800407static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700408{
409 return __tlb_remove_page_size(tlb, page, PAGE_SIZE);
410}
411
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700412/* tlb_remove_page
413 * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
414 * required.
415 */
416static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
417{
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700418 return tlb_remove_page_size(tlb, page, PAGE_SIZE);
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700419}
420
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200421static inline void tlb_change_page_size(struct mmu_gather *tlb,
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -0800422 unsigned int page_size)
423{
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200424#ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
425 if (tlb->page_size && tlb->page_size != page_size) {
426 if (!tlb->fullmm)
427 tlb_flush_mmu(tlb);
428 }
429
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -0800430 tlb->page_size = page_size;
431#endif
432}
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -0800433
Will Deacona6d60242018-08-23 21:01:46 +0100434static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb)
435{
436 if (tlb->cleared_ptes)
437 return PAGE_SHIFT;
438 if (tlb->cleared_pmds)
439 return PMD_SHIFT;
440 if (tlb->cleared_puds)
441 return PUD_SHIFT;
442 if (tlb->cleared_p4ds)
443 return P4D_SHIFT;
444
445 return PAGE_SHIFT;
446}
447
448static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb)
449{
450 return 1UL << tlb_get_unmap_shift(tlb);
451}
452
Will Deaconfb7332a2014-10-29 10:03:09 +0000453/*
454 * In the case of tlb vma handling, we can optimise these away in the
455 * case where we're doing a full MM flush. When we're doing a munmap,
456 * the vmas are adjusted to only cover the region to be torn down.
457 */
458#ifndef tlb_start_vma
Peter Zijlstra5f307be2018-09-04 13:18:15 +0200459static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
460{
461 if (tlb->fullmm)
462 return;
463
464 tlb_update_vma_flags(tlb, vma);
465 flush_cache_range(vma, vma->vm_start, vma->vm_end);
466}
Will Deaconfb7332a2014-10-29 10:03:09 +0000467#endif
468
Will Deaconfb7332a2014-10-29 10:03:09 +0000469#ifndef tlb_end_vma
Peter Zijlstra5f307be2018-09-04 13:18:15 +0200470static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
471{
472 if (tlb->fullmm)
473 return;
474
475 /*
476 * Do a TLB flush and reset the range at VMA boundaries; this avoids
477 * the ranges growing with the unused space between consecutive VMAs,
478 * but also the mmu_gather::vma_* flags from tlb_start_vma() rely on
479 * this.
480 */
481 tlb_flush_mmu_tlbonly(tlb);
482}
Will Deaconfb7332a2014-10-29 10:03:09 +0000483#endif
484
485#ifndef __tlb_remove_tlb_entry
486#define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
487#endif
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/**
490 * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
491 *
Will Deaconfb7332a2014-10-29 10:03:09 +0000492 * Record the fact that pte's were really unmapped by updating the range,
493 * so we can later optimise away the tlb invalidate. This helps when
494 * userspace is unmapping already-unmapped pages, which happens quite a lot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
496#define tlb_remove_tlb_entry(tlb, ptep, address) \
497 do { \
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800498 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
Will Deacona6d60242018-08-23 21:01:46 +0100499 tlb->cleared_ptes = 1; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 __tlb_remove_tlb_entry(tlb, ptep, address); \
501 } while (0)
502
Will Deacona6d60242018-08-23 21:01:46 +0100503#define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
504 do { \
505 unsigned long _sz = huge_page_size(h); \
506 __tlb_adjust_range(tlb, address, _sz); \
507 if (_sz == PMD_SIZE) \
508 tlb->cleared_pmds = 1; \
509 else if (_sz == PUD_SIZE) \
510 tlb->cleared_puds = 1; \
511 __tlb_remove_tlb_entry(tlb, ptep, address); \
Aneesh Kumar K.Vb528e4b2016-12-12 16:42:37 -0800512 } while (0)
513
Shaohua Lif21760b2012-01-12 17:19:16 -0800514/**
515 * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
516 * This is a nop so far, because only x86 needs it.
517 */
518#ifndef __tlb_remove_pmd_tlb_entry
519#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
520#endif
521
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800522#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
523 do { \
524 __tlb_adjust_range(tlb, address, HPAGE_PMD_SIZE); \
Will Deacona6d60242018-08-23 21:01:46 +0100525 tlb->cleared_pmds = 1; \
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800526 __tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
Shaohua Lif21760b2012-01-12 17:19:16 -0800527 } while (0)
528
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800529/**
530 * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
531 * invalidation. This is a nop so far, because only x86 needs it.
532 */
533#ifndef __tlb_remove_pud_tlb_entry
534#define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
535#endif
536
537#define tlb_remove_pud_tlb_entry(tlb, pudp, address) \
538 do { \
539 __tlb_adjust_range(tlb, address, HPAGE_PUD_SIZE); \
Will Deacona6d60242018-08-23 21:01:46 +0100540 tlb->cleared_puds = 1; \
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800541 __tlb_remove_pud_tlb_entry(tlb, pudp, address); \
542 } while (0)
543
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800544/*
545 * For things like page tables caches (ie caching addresses "inside" the
546 * page tables, like x86 does), for legacy reasons, flushing an
547 * individual page had better flush the page table caches behind it. This
548 * is definitely how x86 works, for example. And if you have an
549 * architected non-legacy page table cache (which I'm not aware of
550 * anybody actually doing), you're going to have some architecturally
551 * explicit flushing for that, likely *separate* from a regular TLB entry
552 * flush, and thus you'd need more than just some range expansion..
553 *
554 * So if we ever find an architecture
555 * that would want something that odd, I think it is up to that
556 * architecture to do its own odd thing, not cause pain for others
557 * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
558 *
559 * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
560 */
561
Nicholas Piggina90744b2018-07-13 16:59:03 -0700562#ifndef pte_free_tlb
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000563#define pte_free_tlb(tlb, ptep, address) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 do { \
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800565 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
Will Deacona6d60242018-08-23 21:01:46 +0100566 tlb->freed_tables = 1; \
567 tlb->cleared_pmds = 1; \
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000568 __pte_free_tlb(tlb, ptep, address); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 } while (0)
Nicholas Piggina90744b2018-07-13 16:59:03 -0700570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Nicholas Piggina90744b2018-07-13 16:59:03 -0700572#ifndef pmd_free_tlb
Kirill A. Shutemov048456d2017-03-09 17:24:06 +0300573#define pmd_free_tlb(tlb, pmdp, address) \
574 do { \
Peter Zijlstra22a61c32018-08-23 20:27:25 +0100575 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
Will Deacona6d60242018-08-23 21:01:46 +0100576 tlb->freed_tables = 1; \
577 tlb->cleared_puds = 1; \
Kirill A. Shutemov048456d2017-03-09 17:24:06 +0300578 __pmd_free_tlb(tlb, pmdp, address); \
579 } while (0)
Nicholas Piggina90744b2018-07-13 16:59:03 -0700580#endif
Kirill A. Shutemov048456d2017-03-09 17:24:06 +0300581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#ifndef __ARCH_HAS_4LEVEL_HACK
Nicholas Piggina90744b2018-07-13 16:59:03 -0700583#ifndef pud_free_tlb
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000584#define pud_free_tlb(tlb, pudp, address) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 do { \
Aneesh Kumar K.Vb5bc66b2016-12-12 16:42:34 -0800586 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
Will Deacona6d60242018-08-23 21:01:46 +0100587 tlb->freed_tables = 1; \
588 tlb->cleared_p4ds = 1; \
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000589 __pud_free_tlb(tlb, pudp, address); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 } while (0)
591#endif
Nicholas Piggina90744b2018-07-13 16:59:03 -0700592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Kirill A. Shutemov048456d2017-03-09 17:24:06 +0300594#ifndef __ARCH_HAS_5LEVEL_HACK
Nicholas Piggina90744b2018-07-13 16:59:03 -0700595#ifndef p4d_free_tlb
Kirill A. Shutemov048456d2017-03-09 17:24:06 +0300596#define p4d_free_tlb(tlb, pudp, address) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 do { \
Peter Zijlstra22a61c32018-08-23 20:27:25 +0100598 __tlb_adjust_range(tlb, address, PAGE_SIZE); \
Will Deacona6d60242018-08-23 21:01:46 +0100599 tlb->freed_tables = 1; \
Kirill A. Shutemov048456d2017-03-09 17:24:06 +0300600 __p4d_free_tlb(tlb, pudp, address); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } while (0)
Kirill A. Shutemov048456d2017-03-09 17:24:06 +0300602#endif
Nicholas Piggina90744b2018-07-13 16:59:03 -0700603#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Will Deaconfaaadaf2018-08-24 13:28:28 +0100605#endif /* CONFIG_MMU */
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#define tlb_migrate_finish(mm) do {} while (0)
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609#endif /* _ASM_GENERIC__TLB_H */