2 #include <linux/highmem.h>
3 #include <linux/kernel.h>
4 #include <linux/kmsan-checks.h>
5 #include <linux/mmdebug.h>
6 #include <linux/mm_types.h>
7 #include <linux/mm_inline.h>
8 #include <linux/pagemap.h>
9 #include <linux/rcupdate.h>
10 #include <linux/smp.h>
11 #include <linux/swap.h>
13 #include <asm/pgalloc.h>
16 #ifndef CONFIG_MMU_GATHER_NO_GATHER
18 static bool tlb_next_batch(struct mmu_gather *tlb)
20 struct mmu_gather_batch *batch;
24 tlb->active = batch->next;
28 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
31 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
38 batch->max = MAX_GATHER_BATCH;
40 tlb->active->next = batch;
46 static void tlb_batch_pages_flush(struct mmu_gather *tlb)
48 struct mmu_gather_batch *batch;
50 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
51 struct page **pages = batch->pages;
55 * limit free batch count when PAGE_SIZE > 4K
57 unsigned int nr = min(512U, batch->nr);
59 free_pages_and_swap_cache(pages, nr);
66 tlb->active = &tlb->local;
69 static void tlb_batch_list_free(struct mmu_gather *tlb)
71 struct mmu_gather_batch *batch, *next;
73 for (batch = tlb->local.next; batch; batch = next) {
75 free_pages((unsigned long)batch, 0);
77 tlb->local.next = NULL;
80 bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
82 struct mmu_gather_batch *batch;
86 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
87 VM_WARN_ON(tlb->page_size != page_size);
92 * Add the page and check if we are full. If so
95 batch->pages[batch->nr++] = page;
96 if (batch->nr == batch->max) {
97 if (!tlb_next_batch(tlb))
101 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
106 #endif /* MMU_GATHER_NO_GATHER */
108 #ifdef CONFIG_MMU_GATHER_TABLE_FREE
110 static void __tlb_remove_table_free(struct mmu_table_batch *batch)
114 for (i = 0; i < batch->nr; i++)
115 __tlb_remove_table(batch->tables[i]);
117 free_page((unsigned long)batch);
120 #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
123 * Semi RCU freeing of the page directories.
125 * This is needed by some architectures to implement software pagetable walkers.
127 * gup_fast() and other software pagetable walkers do a lockless page-table
128 * walk and therefore needs some synchronization with the freeing of the page
129 * directories. The chosen means to accomplish that is by disabling IRQs over
132 * Architectures that use IPIs to flush TLBs will then automagically DTRT,
133 * since we unlink the page, flush TLBs, free the page. Since the disabling of
134 * IRQs delays the completion of the TLB flush we can never observe an already
137 * Architectures that do not have this (PPC) need to delay the freeing by some
138 * other means, this is that means.
140 * What we do is batch the freed directory pages (tables) and RCU free them.
141 * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
142 * holds off grace periods.
144 * However, in order to batch these pages we need to allocate storage, this
145 * allocation is deep inside the MM code and can thus easily fail on memory
146 * pressure. To guarantee progress we fall back to single table freeing, see
147 * the implementation of tlb_remove_table_one().
151 static void tlb_remove_table_smp_sync(void *arg)
153 /* Simply deliver the interrupt */
156 static void tlb_remove_table_sync_one(void)
159 * This isn't an RCU grace period and hence the page-tables cannot be
160 * assumed to be actually RCU-freed.
162 * It is however sufficient for software page-table walkers that rely on
165 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
168 static void tlb_remove_table_rcu(struct rcu_head *head)
170 __tlb_remove_table_free(container_of(head, struct mmu_table_batch, rcu));
173 static void tlb_remove_table_free(struct mmu_table_batch *batch)
175 call_rcu(&batch->rcu, tlb_remove_table_rcu);
178 #else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */
180 static void tlb_remove_table_sync_one(void) { }
182 static void tlb_remove_table_free(struct mmu_table_batch *batch)
184 __tlb_remove_table_free(batch);
187 #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
190 * If we want tlb_remove_table() to imply TLB invalidates.
192 static inline void tlb_table_invalidate(struct mmu_gather *tlb)
194 if (tlb_needs_table_invalidate()) {
196 * Invalidate page-table caches used by hardware walkers. Then
197 * we still need to RCU-sched wait while freeing the pages
198 * because software walkers can still be in-flight.
200 tlb_flush_mmu_tlbonly(tlb);
204 static void tlb_remove_table_one(void *table)
206 tlb_remove_table_sync_one();
207 __tlb_remove_table(table);
210 static void tlb_table_flush(struct mmu_gather *tlb)
212 struct mmu_table_batch **batch = &tlb->batch;
215 tlb_table_invalidate(tlb);
216 tlb_remove_table_free(*batch);
221 void tlb_remove_table(struct mmu_gather *tlb, void *table)
223 struct mmu_table_batch **batch = &tlb->batch;
225 if (*batch == NULL) {
226 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
227 if (*batch == NULL) {
228 tlb_table_invalidate(tlb);
229 tlb_remove_table_one(table);
235 (*batch)->tables[(*batch)->nr++] = table;
236 if ((*batch)->nr == MAX_TABLE_BATCH)
237 tlb_table_flush(tlb);
240 static inline void tlb_table_init(struct mmu_gather *tlb)
245 #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
247 static inline void tlb_table_flush(struct mmu_gather *tlb) { }
248 static inline void tlb_table_init(struct mmu_gather *tlb) { }
250 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
252 static void tlb_flush_mmu_free(struct mmu_gather *tlb)
254 tlb_table_flush(tlb);
255 #ifndef CONFIG_MMU_GATHER_NO_GATHER
256 tlb_batch_pages_flush(tlb);
260 void tlb_flush_mmu(struct mmu_gather *tlb)
262 tlb_flush_mmu_tlbonly(tlb);
263 tlb_flush_mmu_free(tlb);
266 static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
270 * struct mmu_gather contains 7 1-bit fields packed into a 32-bit
271 * unsigned int value. The remaining 25 bits remain uninitialized
272 * and are never used, but KMSAN updates the origin for them in
273 * zap_pXX_range() in mm/memory.c, thus creating very long origin
274 * chains. This is technically correct, but consumes too much memory.
275 * Unpoisoning the whole structure will prevent creating such chains.
277 kmsan_unpoison_memory(tlb, sizeof(*tlb));
279 tlb->fullmm = fullmm;
281 #ifndef CONFIG_MMU_GATHER_NO_GATHER
282 tlb->need_flush_all = 0;
283 tlb->local.next = NULL;
285 tlb->local.max = ARRAY_SIZE(tlb->__pages);
286 tlb->active = &tlb->local;
287 tlb->batch_count = 0;
291 #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
295 __tlb_reset_range(tlb);
296 inc_tlb_flush_pending(tlb->mm);
300 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
301 * @tlb: the mmu_gather structure to initialize
302 * @mm: the mm_struct of the target address space
304 * Called to initialize an (on-stack) mmu_gather structure for page-table
305 * tear-down from @mm.
307 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm)
309 __tlb_gather_mmu(tlb, mm, false);
313 * tlb_gather_mmu_fullmm - initialize an mmu_gather structure for page-table tear-down
314 * @tlb: the mmu_gather structure to initialize
315 * @mm: the mm_struct of the target address space
317 * In this case, @mm is without users and we're going to destroy the
318 * full address space (exit/execve).
320 * Called to initialize an (on-stack) mmu_gather structure for page-table
321 * tear-down from @mm.
323 void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm)
325 __tlb_gather_mmu(tlb, mm, true);
329 * tlb_finish_mmu - finish an mmu_gather structure
330 * @tlb: the mmu_gather structure to finish
332 * Called at the end of the shootdown operation to free up any resources that
335 void tlb_finish_mmu(struct mmu_gather *tlb)
338 * If there are parallel threads are doing PTE changes on same range
339 * under non-exclusive lock (e.g., mmap_lock read-side) but defer TLB
340 * flush by batching, one thread may end up seeing inconsistent PTEs
341 * and result in having stale TLB entries. So flush TLB forcefully
342 * if we detect parallel PTE batching threads.
344 * However, some syscalls, e.g. munmap(), may free page tables, this
345 * needs force flush everything in the given range. Otherwise this
346 * may result in having stale TLB entries for some architectures,
347 * e.g. aarch64, that could specify flush what level TLB.
349 if (mm_tlb_flush_nested(tlb->mm)) {
351 * The aarch64 yields better performance with fullmm by
352 * avoiding multiple CPUs spamming TLBI messages at the
355 * On x86 non-fullmm doesn't yield significant difference
359 __tlb_reset_range(tlb);
360 tlb->freed_tables = 1;
365 #ifndef CONFIG_MMU_GATHER_NO_GATHER
366 tlb_batch_list_free(tlb);
368 dec_tlb_flush_pending(tlb->mm);