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 free_pages_and_swap_cache(batch->pages, batch->nr);
53 tlb->active = &tlb->local;
56 static void tlb_batch_list_free(struct mmu_gather *tlb)
58 struct mmu_gather_batch *batch, *next;
60 for (batch = tlb->local.next; batch; batch = next) {
62 free_pages((unsigned long)batch, 0);
64 tlb->local.next = NULL;
67 bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
69 struct mmu_gather_batch *batch;
73 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
74 VM_WARN_ON(tlb->page_size != page_size);
79 * Add the page and check if we are full. If so
82 batch->pages[batch->nr++] = page;
83 if (batch->nr == batch->max) {
84 if (!tlb_next_batch(tlb))
88 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
93 #endif /* MMU_GATHER_NO_GATHER */
95 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
97 static void __tlb_remove_table_free(struct mmu_table_batch *batch)
101 for (i = 0; i < batch->nr; i++)
102 __tlb_remove_table(batch->tables[i]);
104 free_page((unsigned long)batch);
107 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
110 * Semi RCU freeing of the page directories.
112 * This is needed by some architectures to implement software pagetable walkers.
114 * gup_fast() and other software pagetable walkers do a lockless page-table
115 * walk and therefore needs some synchronization with the freeing of the page
116 * directories. The chosen means to accomplish that is by disabling IRQs over
119 * Architectures that use IPIs to flush TLBs will then automagically DTRT,
120 * since we unlink the page, flush TLBs, free the page. Since the disabling of
121 * IRQs delays the completion of the TLB flush we can never observe an already
124 * Architectures that do not have this (PPC) need to delay the freeing by some
125 * other means, this is that means.
127 * What we do is batch the freed directory pages (tables) and RCU free them.
128 * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
129 * holds off grace periods.
131 * However, in order to batch these pages we need to allocate storage, this
132 * allocation is deep inside the MM code and can thus easily fail on memory
133 * pressure. To guarantee progress we fall back to single table freeing, see
134 * the implementation of tlb_remove_table_one().
138 static void tlb_remove_table_smp_sync(void *arg)
140 /* Simply deliver the interrupt */
143 static void tlb_remove_table_sync_one(void)
146 * This isn't an RCU grace period and hence the page-tables cannot be
147 * assumed to be actually RCU-freed.
149 * It is however sufficient for software page-table walkers that rely on
152 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
155 static void tlb_remove_table_rcu(struct rcu_head *head)
157 __tlb_remove_table_free(container_of(head, struct mmu_table_batch, rcu));
160 static void tlb_remove_table_free(struct mmu_table_batch *batch)
162 call_rcu(&batch->rcu, tlb_remove_table_rcu);
165 #else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */
167 static void tlb_remove_table_sync_one(void) { }
169 static void tlb_remove_table_free(struct mmu_table_batch *batch)
171 __tlb_remove_table_free(batch);
174 #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
177 * If we want tlb_remove_table() to imply TLB invalidates.
179 static inline void tlb_table_invalidate(struct mmu_gather *tlb)
181 if (tlb_needs_table_invalidate()) {
183 * Invalidate page-table caches used by hardware walkers. Then
184 * we still need to RCU-sched wait while freeing the pages
185 * because software walkers can still be in-flight.
187 tlb_flush_mmu_tlbonly(tlb);
191 static void tlb_remove_table_one(void *table)
193 tlb_remove_table_sync_one();
194 __tlb_remove_table(table);
197 static void tlb_table_flush(struct mmu_gather *tlb)
199 struct mmu_table_batch **batch = &tlb->batch;
202 tlb_table_invalidate(tlb);
203 tlb_remove_table_free(*batch);
208 void tlb_remove_table(struct mmu_gather *tlb, void *table)
210 struct mmu_table_batch **batch = &tlb->batch;
212 if (*batch == NULL) {
213 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
214 if (*batch == NULL) {
215 tlb_table_invalidate(tlb);
216 tlb_remove_table_one(table);
222 (*batch)->tables[(*batch)->nr++] = table;
223 if ((*batch)->nr == MAX_TABLE_BATCH)
224 tlb_table_flush(tlb);
227 static inline void tlb_table_init(struct mmu_gather *tlb)
232 #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
234 static inline void tlb_table_flush(struct mmu_gather *tlb) { }
235 static inline void tlb_table_init(struct mmu_gather *tlb) { }
237 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
239 static void tlb_flush_mmu_free(struct mmu_gather *tlb)
241 tlb_table_flush(tlb);
242 #ifndef CONFIG_MMU_GATHER_NO_GATHER
243 tlb_batch_pages_flush(tlb);
247 void tlb_flush_mmu(struct mmu_gather *tlb)
249 tlb_flush_mmu_tlbonly(tlb);
250 tlb_flush_mmu_free(tlb);
253 static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
257 tlb->fullmm = fullmm;
259 #ifndef CONFIG_MMU_GATHER_NO_GATHER
260 tlb->need_flush_all = 0;
261 tlb->local.next = NULL;
263 tlb->local.max = ARRAY_SIZE(tlb->__pages);
264 tlb->active = &tlb->local;
265 tlb->batch_count = 0;
269 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
273 __tlb_reset_range(tlb);
274 inc_tlb_flush_pending(tlb->mm);
278 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
279 * @tlb: the mmu_gather structure to initialize
280 * @mm: the mm_struct of the target address space
282 * Called to initialize an (on-stack) mmu_gather structure for page-table
283 * tear-down from @mm.
285 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm)
287 __tlb_gather_mmu(tlb, mm, false);
291 * tlb_gather_mmu_fullmm - initialize an mmu_gather structure for page-table tear-down
292 * @tlb: the mmu_gather structure to initialize
293 * @mm: the mm_struct of the target address space
295 * In this case, @mm is without users and we're going to destroy the
296 * full address space (exit/execve).
298 * Called to initialize an (on-stack) mmu_gather structure for page-table
299 * tear-down from @mm.
301 void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm)
303 __tlb_gather_mmu(tlb, mm, true);
307 * tlb_finish_mmu - finish an mmu_gather structure
308 * @tlb: the mmu_gather structure to finish
310 * Called at the end of the shootdown operation to free up any resources that
313 void tlb_finish_mmu(struct mmu_gather *tlb)
316 * If there are parallel threads are doing PTE changes on same range
317 * under non-exclusive lock (e.g., mmap_lock read-side) but defer TLB
318 * flush by batching, one thread may end up seeing inconsistent PTEs
319 * and result in having stale TLB entries. So flush TLB forcefully
320 * if we detect parallel PTE batching threads.
322 * However, some syscalls, e.g. munmap(), may free page tables, this
323 * needs force flush everything in the given range. Otherwise this
324 * may result in having stale TLB entries for some architectures,
325 * e.g. aarch64, that could specify flush what level TLB.
327 if (mm_tlb_flush_nested(tlb->mm)) {
329 * The aarch64 yields better performance with fullmm by
330 * avoiding multiple CPUs spamming TLBI messages at the
333 * On x86 non-fullmm doesn't yield significant difference
337 __tlb_reset_range(tlb);
338 tlb->freed_tables = 1;
343 #ifndef CONFIG_MMU_GATHER_NO_GATHER
344 tlb_batch_list_free(tlb);
346 dec_tlb_flush_pending(tlb->mm);