1 /* include/asm-generic/tlb.h
2  *
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  *
8  * Copyright 2011 Red Hat, Inc., Peter Zijlstra
9  *
10  * 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 
18 #include <linux/mmu_notifier.h>
19 #include <linux/swap.h>
20 #include <asm/pgalloc.h>
21 #include <asm/tlbflush.h>
22 
23 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
24 /*
25  * Semi RCU freeing of the page directories.
26  *
27  * This is needed by some architectures to implement software pagetable walkers.
28  *
29  * gup_fast() and other software pagetable walkers do a lockless page-table
30  * walk and therefore needs some synchronization with the freeing of the page
31  * directories. The chosen means to accomplish that is by disabling IRQs over
32  * the walk.
33  *
34  * Architectures that use IPIs to flush TLBs will then automagically DTRT,
35  * since we unlink the page, flush TLBs, free the page. Since the disabling of
36  * IRQs delays the completion of the TLB flush we can never observe an already
37  * freed page.
38  *
39  * Architectures that do not have this (PPC) need to delay the freeing by some
40  * other means, this is that means.
41  *
42  * What we do is batch the freed directory pages (tables) and RCU free them.
43  * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
44  * holds off grace periods.
45  *
46  * However, in order to batch these pages we need to allocate storage, this
47  * allocation is deep inside the MM code and can thus easily fail on memory
48  * pressure. To guarantee progress we fall back to single table freeing, see
49  * the implementation of tlb_remove_table_one().
50  *
51  */
52 struct mmu_table_batch {
53 	struct rcu_head		rcu;
54 	unsigned int		nr;
55 	void			*tables[0];
56 };
57 
58 #define MAX_TABLE_BATCH		\
59 	((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
60 
61 extern void tlb_table_flush(struct mmu_gather *tlb);
62 extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
63 
64 void tlb_remove_table_sync_one(void);
65 
66 #else
67 
tlb_remove_table_sync_one(void)68 static inline void tlb_remove_table_sync_one(void) { }
69 
70 #endif
71 
72 /*
73  * If we can't allocate a page to make a big batch of page pointers
74  * to work on, then just handle a few from the on-stack structure.
75  */
76 #define MMU_GATHER_BUNDLE	8
77 
78 struct mmu_gather_batch {
79 	struct mmu_gather_batch	*next;
80 	unsigned int		nr;
81 	unsigned int		max;
82 	struct page		*pages[0];
83 };
84 
85 #define MAX_GATHER_BATCH	\
86 	((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
87 
88 /*
89  * Limit the maximum number of mmu_gather batches to reduce a risk of soft
90  * lockups for non-preemptible kernels on huge machines when a lot of memory
91  * is zapped during unmapping.
92  * 10K pages freed at once should be safe even without a preemption point.
93  */
94 #define MAX_GATHER_BATCH_COUNT	(10000UL/MAX_GATHER_BATCH)
95 
96 /* struct mmu_gather is an opaque type used by the mm code for passing around
97  * any data needed by arch specific code for tlb_remove_page.
98  */
99 struct mmu_gather {
100 	struct mm_struct	*mm;
101 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
102 	struct mmu_table_batch	*batch;
103 #endif
104 	unsigned long		start;
105 	unsigned long		end;
106 	/* we are in the middle of an operation to clear
107 	 * a full mm and can make some optimizations */
108 	unsigned int		fullmm : 1,
109 	/* we have performed an operation which
110 	 * requires a complete flush of the tlb */
111 				need_flush_all : 1;
112 
113 	struct mmu_gather_batch *active;
114 	struct mmu_gather_batch	local;
115 	struct page		*__pages[MMU_GATHER_BUNDLE];
116 	unsigned int		batch_count;
117 	int page_size;
118 };
119 
120 #define HAVE_GENERIC_MMU_GATHER
121 
122 void arch_tlb_gather_mmu(struct mmu_gather *tlb,
123 	struct mm_struct *mm, unsigned long start, unsigned long end);
124 void tlb_flush_mmu(struct mmu_gather *tlb);
125 void arch_tlb_finish_mmu(struct mmu_gather *tlb,
126 			 unsigned long start, unsigned long end, bool force);
127 void tlb_flush_pmd_range(struct mmu_gather *tlb, unsigned long address,
128 			 unsigned long size);
129 extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
130 				   int page_size);
131 
__tlb_adjust_range(struct mmu_gather * tlb,unsigned long address,unsigned int range_size)132 static inline void __tlb_adjust_range(struct mmu_gather *tlb,
133 				      unsigned long address,
134 				      unsigned int range_size)
135 {
136 	tlb->start = min(tlb->start, address);
137 	tlb->end = max(tlb->end, address + range_size);
138 }
139 
__tlb_reset_range(struct mmu_gather * tlb)140 static inline void __tlb_reset_range(struct mmu_gather *tlb)
141 {
142 	if (tlb->fullmm) {
143 		tlb->start = tlb->end = ~0;
144 	} else {
145 		tlb->start = TASK_SIZE;
146 		tlb->end = 0;
147 	}
148 }
149 
tlb_flush_mmu_tlbonly(struct mmu_gather * tlb)150 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
151 {
152 	if (!tlb->end)
153 		return;
154 
155 	tlb_flush(tlb);
156 	mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
157 	__tlb_reset_range(tlb);
158 }
159 
tlb_remove_page_size(struct mmu_gather * tlb,struct page * page,int page_size)160 static inline void tlb_remove_page_size(struct mmu_gather *tlb,
161 					struct page *page, int page_size)
162 {
163 	if (__tlb_remove_page_size(tlb, page, page_size))
164 		tlb_flush_mmu(tlb);
165 }
166 
__tlb_remove_page(struct mmu_gather * tlb,struct page * page)167 static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
168 {
169 	return __tlb_remove_page_size(tlb, page, PAGE_SIZE);
170 }
171 
172 /* tlb_remove_page
173  *	Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
174  *	required.
175  */
tlb_remove_page(struct mmu_gather * tlb,struct page * page)176 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
177 {
178 	return tlb_remove_page_size(tlb, page, PAGE_SIZE);
179 }
180 
181 #ifndef tlb_remove_check_page_size_change
182 #define tlb_remove_check_page_size_change tlb_remove_check_page_size_change
tlb_remove_check_page_size_change(struct mmu_gather * tlb,unsigned int page_size)183 static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
184 						     unsigned int page_size)
185 {
186 	/*
187 	 * We don't care about page size change, just update
188 	 * mmu_gather page size here so that debug checks
189 	 * doesn't throw false warning.
190 	 */
191 #ifdef CONFIG_DEBUG_VM
192 	tlb->page_size = page_size;
193 #endif
194 }
195 #endif
196 
197 /*
198  * In the case of tlb vma handling, we can optimise these away in the
199  * case where we're doing a full MM flush.  When we're doing a munmap,
200  * the vmas are adjusted to only cover the region to be torn down.
201  */
202 #ifndef tlb_start_vma
203 #define tlb_start_vma(tlb, vma) do { } while (0)
204 #endif
205 
206 #define __tlb_end_vma(tlb, vma)					\
207 	do {							\
208 		if (!tlb->fullmm)				\
209 			tlb_flush_mmu_tlbonly(tlb);		\
210 	} while (0)
211 
212 #ifndef tlb_end_vma
213 #define tlb_end_vma	__tlb_end_vma
214 #endif
215 
216 #ifndef __tlb_remove_tlb_entry
217 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
218 #endif
219 
220 /**
221  * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
222  *
223  * Record the fact that pte's were really unmapped by updating the range,
224  * so we can later optimise away the tlb invalidate.   This helps when
225  * userspace is unmapping already-unmapped pages, which happens quite a lot.
226  */
227 #define tlb_remove_tlb_entry(tlb, ptep, address)		\
228 	do {							\
229 		__tlb_adjust_range(tlb, address, PAGE_SIZE);	\
230 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
231 	} while (0)
232 
233 #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address)	     \
234 	do {							     \
235 		__tlb_adjust_range(tlb, address, huge_page_size(h)); \
236 		__tlb_remove_tlb_entry(tlb, ptep, address);	     \
237 	} while (0)
238 
239 /**
240  * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
241  * This is a nop so far, because only x86 needs it.
242  */
243 #ifndef __tlb_remove_pmd_tlb_entry
244 #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
245 #endif
246 
247 #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address)			\
248 	do {								\
249 		__tlb_adjust_range(tlb, address, HPAGE_PMD_SIZE);	\
250 		__tlb_remove_pmd_tlb_entry(tlb, pmdp, address);		\
251 	} while (0)
252 
253 /**
254  * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
255  * invalidation. This is a nop so far, because only x86 needs it.
256  */
257 #ifndef __tlb_remove_pud_tlb_entry
258 #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
259 #endif
260 
261 #define tlb_remove_pud_tlb_entry(tlb, pudp, address)			\
262 	do {								\
263 		__tlb_adjust_range(tlb, address, HPAGE_PUD_SIZE);	\
264 		__tlb_remove_pud_tlb_entry(tlb, pudp, address);		\
265 	} while (0)
266 
267 /*
268  * For things like page tables caches (ie caching addresses "inside" the
269  * page tables, like x86 does), for legacy reasons, flushing an
270  * individual page had better flush the page table caches behind it. This
271  * is definitely how x86 works, for example. And if you have an
272  * architected non-legacy page table cache (which I'm not aware of
273  * anybody actually doing), you're going to have some architecturally
274  * explicit flushing for that, likely *separate* from a regular TLB entry
275  * flush, and thus you'd need more than just some range expansion..
276  *
277  * So if we ever find an architecture
278  * that would want something that odd, I think it is up to that
279  * architecture to do its own odd thing, not cause pain for others
280  * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
281  *
282  * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
283  */
284 
285 #ifndef pte_free_tlb
286 #define pte_free_tlb(tlb, ptep, address)			\
287 	do {							\
288 		__tlb_adjust_range(tlb, address, PAGE_SIZE);	\
289 		__pte_free_tlb(tlb, ptep, address);		\
290 	} while (0)
291 #endif
292 
293 #ifndef pmd_free_tlb
294 #define pmd_free_tlb(tlb, pmdp, address)			\
295 	do {							\
296 		__tlb_adjust_range(tlb, address, PAGE_SIZE);		\
297 		__pmd_free_tlb(tlb, pmdp, address);		\
298 	} while (0)
299 #endif
300 
301 #ifndef __ARCH_HAS_4LEVEL_HACK
302 #ifndef pud_free_tlb
303 #define pud_free_tlb(tlb, pudp, address)			\
304 	do {							\
305 		__tlb_adjust_range(tlb, address, PAGE_SIZE);	\
306 		__pud_free_tlb(tlb, pudp, address);		\
307 	} while (0)
308 #endif
309 #endif
310 
311 #ifndef __ARCH_HAS_5LEVEL_HACK
312 #ifndef p4d_free_tlb
313 #define p4d_free_tlb(tlb, pudp, address)			\
314 	do {							\
315 		__tlb_adjust_range(tlb, address, PAGE_SIZE);		\
316 		__p4d_free_tlb(tlb, pudp, address);		\
317 	} while (0)
318 #endif
319 #endif
320 
321 #define tlb_migrate_finish(mm) do {} while (0)
322 
323 #endif /* _ASM_GENERIC__TLB_H */
324