2 #include <linux/highmem.h>
3 #include <linux/kernel.h>
4 #include <linux/mmdebug.h>
5 #include <linux/mm_types.h>
6 #include <linux/mm_inline.h>
7 #include <linux/pagemap.h>
8 #include <linux/rcupdate.h>
10 #include <linux/swap.h>
12 #include <asm/pgalloc.h>
15 #ifndef CONFIG_MMU_GATHER_NO_GATHER
17 static bool tlb_next_batch(struct mmu_gather *tlb)
19 struct mmu_gather_batch *batch;
23 tlb->active = batch->next;
27 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
30 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
37 batch->max = MAX_GATHER_BATCH;
39 tlb->active->next = batch;
45 static void tlb_batch_pages_flush(struct mmu_gather *tlb)
47 struct mmu_gather_batch *batch;
49 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
50 struct page **pages = batch->pages;
54 * limit free batch count when PAGE_SIZE > 4K
56 unsigned int nr = min(512U, batch->nr);
58 free_pages_and_swap_cache(pages, nr);
65 tlb->active = &tlb->local;
68 static void tlb_batch_list_free(struct mmu_gather *tlb)
70 struct mmu_gather_batch *batch, *next;
72 for (batch = tlb->local.next; batch; batch = next) {
74 free_pages((unsigned long)batch, 0);
76 tlb->local.next = NULL;
79 bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
81 struct mmu_gather_batch *batch;
85 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
86 VM_WARN_ON(tlb->page_size != page_size);
91 * Add the page and check if we are full. If so
94 batch->pages[batch->nr++] = page;
95 if (batch->nr == batch->max) {
96 if (!tlb_next_batch(tlb))
100 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
105 #endif /* MMU_GATHER_NO_GATHER */
107 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
109 static void __tlb_remove_table_free(struct mmu_table_batch *batch)
113 for (i = 0; i < batch->nr; i++)
114 __tlb_remove_table(batch->tables[i]);
116 free_page((unsigned long)batch);
119 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
122 * Semi RCU freeing of the page directories.
124 * This is needed by some architectures to implement software pagetable walkers.
126 * gup_fast() and other software pagetable walkers do a lockless page-table
127 * walk and therefore needs some synchronization with the freeing of the page
128 * directories. The chosen means to accomplish that is by disabling IRQs over
131 * Architectures that use IPIs to flush TLBs will then automagically DTRT,
132 * since we unlink the page, flush TLBs, free the page. Since the disabling of
133 * IRQs delays the completion of the TLB flush we can never observe an already
136 * Architectures that do not have this (PPC) need to delay the freeing by some
137 * other means, this is that means.
139 * What we do is batch the freed directory pages (tables) and RCU free them.
140 * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
141 * holds off grace periods.
143 * However, in order to batch these pages we need to allocate storage, this
144 * allocation is deep inside the MM code and can thus easily fail on memory
145 * pressure. To guarantee progress we fall back to single table freeing, see
146 * the implementation of tlb_remove_table_one().
150 static void tlb_remove_table_smp_sync(void *arg)
152 /* Simply deliver the interrupt */
155 static void tlb_remove_table_sync_one(void)
158 * This isn't an RCU grace period and hence the page-tables cannot be
159 * assumed to be actually RCU-freed.
161 * It is however sufficient for software page-table walkers that rely on
164 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
167 static void tlb_remove_table_rcu(struct rcu_head *head)
169 __tlb_remove_table_free(container_of(head, struct mmu_table_batch, rcu));
172 static void tlb_remove_table_free(struct mmu_table_batch *batch)
174 call_rcu(&batch->rcu, tlb_remove_table_rcu);
177 #else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */
179 static void tlb_remove_table_sync_one(void) { }
181 static void tlb_remove_table_free(struct mmu_table_batch *batch)
183 __tlb_remove_table_free(batch);
186 #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
189 * If we want tlb_remove_table() to imply TLB invalidates.
191 static inline void tlb_table_invalidate(struct mmu_gather *tlb)
193 if (tlb_needs_table_invalidate()) {
195 * Invalidate page-table caches used by hardware walkers. Then
196 * we still need to RCU-sched wait while freeing the pages
197 * because software walkers can still be in-flight.
199 tlb_flush_mmu_tlbonly(tlb);
203 static void tlb_remove_table_one(void *table)
205 tlb_remove_table_sync_one();
206 __tlb_remove_table(table);
209 static void tlb_table_flush(struct mmu_gather *tlb)
211 struct mmu_table_batch **batch = &tlb->batch;
214 tlb_table_invalidate(tlb);
215 tlb_remove_table_free(*batch);
220 void tlb_remove_table(struct mmu_gather *tlb, void *table)
222 struct mmu_table_batch **batch = &tlb->batch;
224 if (*batch == NULL) {
225 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
226 if (*batch == NULL) {
227 tlb_table_invalidate(tlb);
228 tlb_remove_table_one(table);
234 (*batch)->tables[(*batch)->nr++] = table;
235 if ((*batch)->nr == MAX_TABLE_BATCH)
236 tlb_table_flush(tlb);
239 static inline void tlb_table_init(struct mmu_gather *tlb)
244 #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
246 static inline void tlb_table_flush(struct mmu_gather *tlb) { }
247 static inline void tlb_table_init(struct mmu_gather *tlb) { }
249 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
251 static void tlb_flush_mmu_free(struct mmu_gather *tlb)
253 tlb_table_flush(tlb);
254 #ifndef CONFIG_MMU_GATHER_NO_GATHER
255 tlb_batch_pages_flush(tlb);
259 void tlb_flush_mmu(struct mmu_gather *tlb)
261 tlb_flush_mmu_tlbonly(tlb);
262 tlb_flush_mmu_free(tlb);
265 static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
269 tlb->fullmm = fullmm;
271 #ifndef CONFIG_MMU_GATHER_NO_GATHER
272 tlb->need_flush_all = 0;
273 tlb->local.next = NULL;
275 tlb->local.max = ARRAY_SIZE(tlb->__pages);
276 tlb->active = &tlb->local;
277 tlb->batch_count = 0;
281 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
285 __tlb_reset_range(tlb);
286 inc_tlb_flush_pending(tlb->mm);
290 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
291 * @tlb: the mmu_gather structure to initialize
292 * @mm: the mm_struct of the target address space
294 * Called to initialize an (on-stack) mmu_gather structure for page-table
295 * tear-down from @mm.
297 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm)
299 __tlb_gather_mmu(tlb, mm, false);
303 * tlb_gather_mmu_fullmm - initialize an mmu_gather structure for page-table tear-down
304 * @tlb: the mmu_gather structure to initialize
305 * @mm: the mm_struct of the target address space
307 * In this case, @mm is without users and we're going to destroy the
308 * full address space (exit/execve).
310 * Called to initialize an (on-stack) mmu_gather structure for page-table
311 * tear-down from @mm.
313 void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm)
315 __tlb_gather_mmu(tlb, mm, true);
319 * tlb_finish_mmu - finish an mmu_gather structure
320 * @tlb: the mmu_gather structure to finish
322 * Called at the end of the shootdown operation to free up any resources that
325 void tlb_finish_mmu(struct mmu_gather *tlb)
328 * If there are parallel threads are doing PTE changes on same range
329 * under non-exclusive lock (e.g., mmap_lock read-side) but defer TLB
330 * flush by batching, one thread may end up seeing inconsistent PTEs
331 * and result in having stale TLB entries. So flush TLB forcefully
332 * if we detect parallel PTE batching threads.
334 * However, some syscalls, e.g. munmap(), may free page tables, this
335 * needs force flush everything in the given range. Otherwise this
336 * may result in having stale TLB entries for some architectures,
337 * e.g. aarch64, that could specify flush what level TLB.
339 if (mm_tlb_flush_nested(tlb->mm)) {
341 * The aarch64 yields better performance with fullmm by
342 * avoiding multiple CPUs spamming TLBI messages at the
345 * On x86 non-fullmm doesn't yield significant difference
349 __tlb_reset_range(tlb);
350 tlb->freed_tables = 1;
355 #ifndef CONFIG_MMU_GATHER_NO_GATHER
356 tlb_batch_list_free(tlb);
358 dec_tlb_flush_pending(tlb->mm);