• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/gfp.h>
2 #include <linux/highmem.h>
3 #include <linux/kernel.h>
4 #include <linux/mmdebug.h>
5 #include <linux/mm_types.h>
6 #include <linux/pagemap.h>
7 #include <linux/rcupdate.h>
8 #include <linux/smp.h>
9 #include <linux/swap.h>
10 
11 #include <asm/pgalloc.h>
12 #include <asm/tlb.h>
13 
14 #ifndef CONFIG_MMU_GATHER_NO_GATHER
15 
tlb_next_batch(struct mmu_gather * tlb)16 static bool tlb_next_batch(struct mmu_gather *tlb)
17 {
18 	struct mmu_gather_batch *batch;
19 
20 	batch = tlb->active;
21 	if (batch->next) {
22 		tlb->active = batch->next;
23 		return true;
24 	}
25 
26 	if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
27 		return false;
28 
29 	batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
30 	if (!batch)
31 		return false;
32 
33 	tlb->batch_count++;
34 	batch->next = NULL;
35 	batch->nr   = 0;
36 	batch->max  = MAX_GATHER_BATCH;
37 
38 	tlb->active->next = batch;
39 	tlb->active = batch;
40 
41 	return true;
42 }
43 
tlb_batch_pages_flush(struct mmu_gather * tlb)44 static void tlb_batch_pages_flush(struct mmu_gather *tlb)
45 {
46 	struct mmu_gather_batch *batch;
47 
48 	for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
49 		free_pages_and_swap_cache(batch->pages, batch->nr);
50 		batch->nr = 0;
51 	}
52 	tlb->active = &tlb->local;
53 }
54 
tlb_batch_list_free(struct mmu_gather * tlb)55 static void tlb_batch_list_free(struct mmu_gather *tlb)
56 {
57 	struct mmu_gather_batch *batch, *next;
58 
59 	for (batch = tlb->local.next; batch; batch = next) {
60 		next = batch->next;
61 		free_pages((unsigned long)batch, 0);
62 	}
63 	tlb->local.next = NULL;
64 }
65 
__tlb_remove_page_size(struct mmu_gather * tlb,struct page * page,int page_size)66 bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
67 {
68 	struct mmu_gather_batch *batch;
69 
70 	VM_BUG_ON(!tlb->end);
71 
72 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
73 	VM_WARN_ON(tlb->page_size != page_size);
74 #endif
75 
76 	batch = tlb->active;
77 	/*
78 	 * Add the page and check if we are full. If so
79 	 * force a flush.
80 	 */
81 	batch->pages[batch->nr++] = page;
82 	if (batch->nr == batch->max) {
83 		if (!tlb_next_batch(tlb))
84 			return true;
85 		batch = tlb->active;
86 	}
87 	VM_BUG_ON_PAGE(batch->nr > batch->max, page);
88 
89 	return false;
90 }
91 
92 #endif /* MMU_GATHER_NO_GATHER */
93 
94 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
95 
__tlb_remove_table_free(struct mmu_table_batch * batch)96 static void __tlb_remove_table_free(struct mmu_table_batch *batch)
97 {
98 	int i;
99 
100 	for (i = 0; i < batch->nr; i++)
101 		__tlb_remove_table(batch->tables[i]);
102 
103 	free_page((unsigned long)batch);
104 }
105 
106 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
107 
108 /*
109  * Semi RCU freeing of the page directories.
110  *
111  * This is needed by some architectures to implement software pagetable walkers.
112  *
113  * gup_fast() and other software pagetable walkers do a lockless page-table
114  * walk and therefore needs some synchronization with the freeing of the page
115  * directories. The chosen means to accomplish that is by disabling IRQs over
116  * the walk.
117  *
118  * Architectures that use IPIs to flush TLBs will then automagically DTRT,
119  * since we unlink the page, flush TLBs, free the page. Since the disabling of
120  * IRQs delays the completion of the TLB flush we can never observe an already
121  * freed page.
122  *
123  * Architectures that do not have this (PPC) need to delay the freeing by some
124  * other means, this is that means.
125  *
126  * What we do is batch the freed directory pages (tables) and RCU free them.
127  * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
128  * holds off grace periods.
129  *
130  * However, in order to batch these pages we need to allocate storage, this
131  * allocation is deep inside the MM code and can thus easily fail on memory
132  * pressure. To guarantee progress we fall back to single table freeing, see
133  * the implementation of tlb_remove_table_one().
134  *
135  */
136 
tlb_remove_table_smp_sync(void * arg)137 static void tlb_remove_table_smp_sync(void *arg)
138 {
139 	/* Simply deliver the interrupt */
140 }
141 
tlb_remove_table_sync_one(void)142 void tlb_remove_table_sync_one(void)
143 {
144 	/*
145 	 * This isn't an RCU grace period and hence the page-tables cannot be
146 	 * assumed to be actually RCU-freed.
147 	 *
148 	 * It is however sufficient for software page-table walkers that rely on
149 	 * IRQ disabling.
150 	 */
151 	smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
152 }
153 
tlb_remove_table_rcu(struct rcu_head * head)154 static void tlb_remove_table_rcu(struct rcu_head *head)
155 {
156 	__tlb_remove_table_free(container_of(head, struct mmu_table_batch, rcu));
157 }
158 
tlb_remove_table_free(struct mmu_table_batch * batch)159 static void tlb_remove_table_free(struct mmu_table_batch *batch)
160 {
161 	call_rcu(&batch->rcu, tlb_remove_table_rcu);
162 }
163 
164 #else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */
165 
tlb_remove_table_free(struct mmu_table_batch * batch)166 static void tlb_remove_table_free(struct mmu_table_batch *batch)
167 {
168 	__tlb_remove_table_free(batch);
169 }
170 
171 #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
172 
173 /*
174  * If we want tlb_remove_table() to imply TLB invalidates.
175  */
tlb_table_invalidate(struct mmu_gather * tlb)176 static inline void tlb_table_invalidate(struct mmu_gather *tlb)
177 {
178 	if (tlb_needs_table_invalidate()) {
179 		/*
180 		 * Invalidate page-table caches used by hardware walkers. Then
181 		 * we still need to RCU-sched wait while freeing the pages
182 		 * because software walkers can still be in-flight.
183 		 */
184 		tlb_flush_mmu_tlbonly(tlb);
185 	}
186 }
187 
tlb_remove_table_one(void * table)188 static void tlb_remove_table_one(void *table)
189 {
190 	tlb_remove_table_sync_one();
191 	__tlb_remove_table(table);
192 }
193 
tlb_table_flush(struct mmu_gather * tlb)194 static void tlb_table_flush(struct mmu_gather *tlb)
195 {
196 	struct mmu_table_batch **batch = &tlb->batch;
197 
198 	if (*batch) {
199 		tlb_table_invalidate(tlb);
200 		tlb_remove_table_free(*batch);
201 		*batch = NULL;
202 	}
203 }
204 
tlb_remove_table(struct mmu_gather * tlb,void * table)205 void tlb_remove_table(struct mmu_gather *tlb, void *table)
206 {
207 	struct mmu_table_batch **batch = &tlb->batch;
208 
209 	if (*batch == NULL) {
210 		*batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
211 		if (*batch == NULL) {
212 			tlb_table_invalidate(tlb);
213 			tlb_remove_table_one(table);
214 			return;
215 		}
216 		(*batch)->nr = 0;
217 	}
218 
219 	(*batch)->tables[(*batch)->nr++] = table;
220 	if ((*batch)->nr == MAX_TABLE_BATCH)
221 		tlb_table_flush(tlb);
222 }
223 
tlb_table_init(struct mmu_gather * tlb)224 static inline void tlb_table_init(struct mmu_gather *tlb)
225 {
226 	tlb->batch = NULL;
227 }
228 
229 #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
230 
tlb_table_flush(struct mmu_gather * tlb)231 static inline void tlb_table_flush(struct mmu_gather *tlb) { }
tlb_table_init(struct mmu_gather * tlb)232 static inline void tlb_table_init(struct mmu_gather *tlb) { }
233 
234 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
235 
tlb_flush_mmu_free(struct mmu_gather * tlb)236 static void tlb_flush_mmu_free(struct mmu_gather *tlb)
237 {
238 	tlb_table_flush(tlb);
239 #ifndef CONFIG_MMU_GATHER_NO_GATHER
240 	tlb_batch_pages_flush(tlb);
241 #endif
242 }
243 
tlb_flush_mmu(struct mmu_gather * tlb)244 void tlb_flush_mmu(struct mmu_gather *tlb)
245 {
246 	tlb_flush_mmu_tlbonly(tlb);
247 	tlb_flush_mmu_free(tlb);
248 }
249 
250 /**
251  * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
252  * @tlb: the mmu_gather structure to initialize
253  * @mm: the mm_struct of the target address space
254  * @start: start of the region that will be removed from the page-table
255  * @end: end of the region that will be removed from the page-table
256  *
257  * Called to initialize an (on-stack) mmu_gather structure for page-table
258  * tear-down from @mm. The @start and @end are set to 0 and -1
259  * respectively when @mm is without users and we're going to destroy
260  * the full address space (exit/execve).
261  */
tlb_gather_mmu(struct mmu_gather * tlb,struct mm_struct * mm,unsigned long start,unsigned long end)262 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
263 			unsigned long start, unsigned long end)
264 {
265 	tlb->mm = mm;
266 
267 	/* Is it from 0 to ~0? */
268 	tlb->fullmm     = !(start | (end+1));
269 
270 #ifndef CONFIG_MMU_GATHER_NO_GATHER
271 	tlb->need_flush_all = 0;
272 	tlb->local.next = NULL;
273 	tlb->local.nr   = 0;
274 	tlb->local.max  = ARRAY_SIZE(tlb->__pages);
275 	tlb->active     = &tlb->local;
276 	tlb->batch_count = 0;
277 #endif
278 
279 	tlb_table_init(tlb);
280 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
281 	tlb->page_size = 0;
282 #endif
283 
284 	__tlb_reset_range(tlb);
285 	inc_tlb_flush_pending(tlb->mm);
286 }
287 
288 /**
289  * tlb_finish_mmu - finish an mmu_gather structure
290  * @tlb: the mmu_gather structure to finish
291  * @start: start of the region that will be removed from the page-table
292  * @end: end of the region that will be removed from the page-table
293  *
294  * Called at the end of the shootdown operation to free up any resources that
295  * were required.
296  */
tlb_finish_mmu(struct mmu_gather * tlb,unsigned long start,unsigned long end)297 void tlb_finish_mmu(struct mmu_gather *tlb,
298 		unsigned long start, unsigned long end)
299 {
300 	/*
301 	 * If there are parallel threads are doing PTE changes on same range
302 	 * under non-exclusive lock (e.g., mmap_lock read-side) but defer TLB
303 	 * flush by batching, one thread may end up seeing inconsistent PTEs
304 	 * and result in having stale TLB entries.  So flush TLB forcefully
305 	 * if we detect parallel PTE batching threads.
306 	 *
307 	 * However, some syscalls, e.g. munmap(), may free page tables, this
308 	 * needs force flush everything in the given range. Otherwise this
309 	 * may result in having stale TLB entries for some architectures,
310 	 * e.g. aarch64, that could specify flush what level TLB.
311 	 */
312 	if (mm_tlb_flush_nested(tlb->mm)) {
313 		/*
314 		 * The aarch64 yields better performance with fullmm by
315 		 * avoiding multiple CPUs spamming TLBI messages at the
316 		 * same time.
317 		 *
318 		 * On x86 non-fullmm doesn't yield significant difference
319 		 * against fullmm.
320 		 */
321 		tlb->fullmm = 1;
322 		__tlb_reset_range(tlb);
323 		tlb->freed_tables = 1;
324 	}
325 
326 	tlb_flush_mmu(tlb);
327 
328 #ifndef CONFIG_MMU_GATHER_NO_GATHER
329 	tlb_batch_list_free(tlb);
330 #endif
331 	dec_tlb_flush_pending(tlb->mm);
332 }
333