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>
9 #include <linux/swap.h>
11 #include <asm/pgalloc.h>
14 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
16 static bool tlb_next_batch(struct mmu_gather *tlb)
18 struct mmu_gather_batch *batch;
22 tlb->active = batch->next;
26 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
29 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
36 batch->max = MAX_GATHER_BATCH;
38 tlb->active->next = batch;
44 static void tlb_batch_pages_flush(struct mmu_gather *tlb)
46 struct mmu_gather_batch *batch;
48 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
49 free_pages_and_swap_cache(batch->pages, batch->nr);
52 tlb->active = &tlb->local;
55 static void tlb_batch_list_free(struct mmu_gather *tlb)
57 struct mmu_gather_batch *batch, *next;
59 for (batch = tlb->local.next; batch; batch = next) {
61 free_pages((unsigned long)batch, 0);
63 tlb->local.next = NULL;
66 bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
68 struct mmu_gather_batch *batch;
72 #ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
73 VM_WARN_ON(tlb->page_size != page_size);
78 * Add the page and check if we are full. If so
81 batch->pages[batch->nr++] = page;
82 if (batch->nr == batch->max) {
83 if (!tlb_next_batch(tlb))
87 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
92 #endif /* HAVE_MMU_GATHER_NO_GATHER */
94 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
97 * See the comment near struct mmu_table_batch.
101 * If we want tlb_remove_table() to imply TLB invalidates.
103 static inline void tlb_table_invalidate(struct mmu_gather *tlb)
105 #ifndef CONFIG_HAVE_RCU_TABLE_NO_INVALIDATE
107 * Invalidate page-table caches used by hardware walkers. Then we still
108 * need to RCU-sched wait while freeing the pages because software
109 * walkers can still be in-flight.
111 tlb_flush_mmu_tlbonly(tlb);
115 static void tlb_remove_table_smp_sync(void *arg)
117 /* Simply deliver the interrupt */
120 static void tlb_remove_table_one(void *table)
123 * This isn't an RCU grace period and hence the page-tables cannot be
124 * assumed to be actually RCU-freed.
126 * It is however sufficient for software page-table walkers that rely on
127 * IRQ disabling. See the comment near struct mmu_table_batch.
129 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
130 __tlb_remove_table(table);
133 static void tlb_remove_table_rcu(struct rcu_head *head)
135 struct mmu_table_batch *batch;
138 batch = container_of(head, struct mmu_table_batch, rcu);
140 for (i = 0; i < batch->nr; i++)
141 __tlb_remove_table(batch->tables[i]);
143 free_page((unsigned long)batch);
146 static void tlb_table_flush(struct mmu_gather *tlb)
148 struct mmu_table_batch **batch = &tlb->batch;
151 tlb_table_invalidate(tlb);
152 call_rcu(&(*batch)->rcu, tlb_remove_table_rcu);
157 void tlb_remove_table(struct mmu_gather *tlb, void *table)
159 struct mmu_table_batch **batch = &tlb->batch;
161 if (*batch == NULL) {
162 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
163 if (*batch == NULL) {
164 tlb_table_invalidate(tlb);
165 tlb_remove_table_one(table);
171 (*batch)->tables[(*batch)->nr++] = table;
172 if ((*batch)->nr == MAX_TABLE_BATCH)
173 tlb_table_flush(tlb);
176 #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
178 static void tlb_flush_mmu_free(struct mmu_gather *tlb)
180 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
181 tlb_table_flush(tlb);
183 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
184 tlb_batch_pages_flush(tlb);
188 void tlb_flush_mmu(struct mmu_gather *tlb)
190 tlb_flush_mmu_tlbonly(tlb);
191 tlb_flush_mmu_free(tlb);
195 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
196 * @tlb: the mmu_gather structure to initialize
197 * @mm: the mm_struct of the target address space
198 * @start: start of the region that will be removed from the page-table
199 * @end: end of the region that will be removed from the page-table
201 * Called to initialize an (on-stack) mmu_gather structure for page-table
202 * tear-down from @mm. The @start and @end are set to 0 and -1
203 * respectively when @mm is without users and we're going to destroy
204 * the full address space (exit/execve).
206 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
207 unsigned long start, unsigned long end)
211 /* Is it from 0 to ~0? */
212 tlb->fullmm = !(start | (end+1));
214 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
215 tlb->need_flush_all = 0;
216 tlb->local.next = NULL;
218 tlb->local.max = ARRAY_SIZE(tlb->__pages);
219 tlb->active = &tlb->local;
220 tlb->batch_count = 0;
223 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
226 #ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
230 __tlb_reset_range(tlb);
231 inc_tlb_flush_pending(tlb->mm);
235 * tlb_finish_mmu - finish an mmu_gather structure
236 * @tlb: the mmu_gather structure to finish
237 * @start: start of the region that will be removed from the page-table
238 * @end: end of the region that will be removed from the page-table
240 * Called at the end of the shootdown operation to free up any resources that
243 void tlb_finish_mmu(struct mmu_gather *tlb,
244 unsigned long start, unsigned long end)
247 * If there are parallel threads are doing PTE changes on same range
248 * under non-exclusive lock (e.g., mmap_sem read-side) but defer TLB
249 * flush by batching, one thread may end up seeing inconsistent PTEs
250 * and result in having stale TLB entries. So flush TLB forcefully
251 * if we detect parallel PTE batching threads.
253 * However, some syscalls, e.g. munmap(), may free page tables, this
254 * needs force flush everything in the given range. Otherwise this
255 * may result in having stale TLB entries for some architectures,
256 * e.g. aarch64, that could specify flush what level TLB.
258 if (mm_tlb_flush_nested(tlb->mm)) {
260 * The aarch64 yields better performance with fullmm by
261 * avoiding multiple CPUs spamming TLBI messages at the
264 * On x86 non-fullmm doesn't yield significant difference
268 __tlb_reset_range(tlb);
269 tlb->freed_tables = 1;
274 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
275 tlb_batch_list_free(tlb);
277 dec_tlb_flush_pending(tlb->mm);