blob: ab220edcd7ef578d4dbf426301f2ad0a2a30f2cb [file] [log] [blame]
Peter Zijlstra196d9d82018-09-03 15:07:36 +01001#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#ifdef HAVE_GENERIC_MMU_GATHER
15
Martin Schwidefsky952a31c2018-09-18 14:51:50 +020016#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
17
Peter Zijlstra196d9d82018-09-03 15:07:36 +010018static bool tlb_next_batch(struct mmu_gather *tlb)
19{
20 struct mmu_gather_batch *batch;
21
22 batch = tlb->active;
23 if (batch->next) {
24 tlb->active = batch->next;
25 return true;
26 }
27
28 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
29 return false;
30
31 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
32 if (!batch)
33 return false;
34
35 tlb->batch_count++;
36 batch->next = NULL;
37 batch->nr = 0;
38 batch->max = MAX_GATHER_BATCH;
39
40 tlb->active->next = batch;
41 tlb->active = batch;
42
43 return true;
44}
45
Martin Schwidefsky952a31c2018-09-18 14:51:50 +020046static void tlb_batch_pages_flush(struct mmu_gather *tlb)
Peter Zijlstra196d9d82018-09-03 15:07:36 +010047{
48 struct mmu_gather_batch *batch;
49
Peter Zijlstra196d9d82018-09-03 15:07:36 +010050 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
51 free_pages_and_swap_cache(batch->pages, batch->nr);
52 batch->nr = 0;
53 }
54 tlb->active = &tlb->local;
55}
56
Martin Schwidefsky952a31c2018-09-18 14:51:50 +020057static void tlb_batch_list_free(struct mmu_gather *tlb)
Peter Zijlstra196d9d82018-09-03 15:07:36 +010058{
59 struct mmu_gather_batch *batch, *next;
60
Peter Zijlstra196d9d82018-09-03 15:07:36 +010061 for (batch = tlb->local.next; batch; batch = next) {
62 next = batch->next;
63 free_pages((unsigned long)batch, 0);
64 }
65 tlb->local.next = NULL;
66}
67
Peter Zijlstra196d9d82018-09-03 15:07:36 +010068bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
69{
70 struct mmu_gather_batch *batch;
71
72 VM_BUG_ON(!tlb->end);
Peter Zijlstraed6a7932018-08-31 14:46:08 +020073
74#ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
Peter Zijlstra196d9d82018-09-03 15:07:36 +010075 VM_WARN_ON(tlb->page_size != page_size);
Peter Zijlstraed6a7932018-08-31 14:46:08 +020076#endif
Peter Zijlstra196d9d82018-09-03 15:07:36 +010077
78 batch = tlb->active;
79 /*
80 * Add the page and check if we are full. If so
81 * force a flush.
82 */
83 batch->pages[batch->nr++] = page;
84 if (batch->nr == batch->max) {
85 if (!tlb_next_batch(tlb))
86 return true;
87 batch = tlb->active;
88 }
89 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
90
91 return false;
92}
93
Martin Schwidefsky952a31c2018-09-18 14:51:50 +020094#endif /* HAVE_MMU_GATHER_NO_GATHER */
95
96void arch_tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
97 unsigned long start, unsigned long end)
98{
99 tlb->mm = mm;
100
101 /* Is it from 0 to ~0? */
102 tlb->fullmm = !(start | (end+1));
103
104#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
105 tlb->need_flush_all = 0;
106 tlb->local.next = NULL;
107 tlb->local.nr = 0;
108 tlb->local.max = ARRAY_SIZE(tlb->__pages);
109 tlb->active = &tlb->local;
110 tlb->batch_count = 0;
111#endif
112
113#ifdef CONFIG_HAVE_RCU_TABLE_FREE
114 tlb->batch = NULL;
115#endif
116#ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
117 tlb->page_size = 0;
118#endif
119
120 __tlb_reset_range(tlb);
121}
122
123void tlb_flush_mmu_free(struct mmu_gather *tlb)
124{
125#ifdef CONFIG_HAVE_RCU_TABLE_FREE
126 tlb_table_flush(tlb);
127#endif
128#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
129 tlb_batch_pages_flush(tlb);
130#endif
131}
132
133void tlb_flush_mmu(struct mmu_gather *tlb)
134{
135 tlb_flush_mmu_tlbonly(tlb);
136 tlb_flush_mmu_free(tlb);
137}
138
139/* tlb_finish_mmu
140 * Called at the end of the shootdown operation to free up any resources
141 * that were required.
142 */
143void arch_tlb_finish_mmu(struct mmu_gather *tlb,
144 unsigned long start, unsigned long end, bool force)
145{
146 if (force) {
147 __tlb_reset_range(tlb);
148 __tlb_adjust_range(tlb, start, end - start);
149 }
150
151 tlb_flush_mmu(tlb);
152
153 /* keep the page table cache within bounds */
154 check_pgt_cache();
155#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
156 tlb_batch_list_free(tlb);
157#endif
158}
159
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100160#endif /* HAVE_GENERIC_MMU_GATHER */
161
162#ifdef CONFIG_HAVE_RCU_TABLE_FREE
163
164/*
165 * See the comment near struct mmu_table_batch.
166 */
167
168/*
169 * If we want tlb_remove_table() to imply TLB invalidates.
170 */
171static inline void tlb_table_invalidate(struct mmu_gather *tlb)
172{
Peter Zijlstra96bc9562018-09-19 13:24:41 +0200173#ifndef CONFIG_HAVE_RCU_TABLE_NO_INVALIDATE
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100174 /*
175 * Invalidate page-table caches used by hardware walkers. Then we still
176 * need to RCU-sched wait while freeing the pages because software
177 * walkers can still be in-flight.
178 */
179 tlb_flush_mmu_tlbonly(tlb);
180#endif
181}
182
183static void tlb_remove_table_smp_sync(void *arg)
184{
185 /* Simply deliver the interrupt */
186}
187
188static void tlb_remove_table_one(void *table)
189{
190 /*
191 * This isn't an RCU grace period and hence the page-tables cannot be
192 * assumed to be actually RCU-freed.
193 *
194 * It is however sufficient for software page-table walkers that rely on
195 * IRQ disabling. See the comment near struct mmu_table_batch.
196 */
197 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
198 __tlb_remove_table(table);
199}
200
201static void tlb_remove_table_rcu(struct rcu_head *head)
202{
203 struct mmu_table_batch *batch;
204 int i;
205
206 batch = container_of(head, struct mmu_table_batch, rcu);
207
208 for (i = 0; i < batch->nr; i++)
209 __tlb_remove_table(batch->tables[i]);
210
211 free_page((unsigned long)batch);
212}
213
214void tlb_table_flush(struct mmu_gather *tlb)
215{
216 struct mmu_table_batch **batch = &tlb->batch;
217
218 if (*batch) {
219 tlb_table_invalidate(tlb);
Paul E. McKenneyb401ec12018-11-06 19:30:34 -0800220 call_rcu(&(*batch)->rcu, tlb_remove_table_rcu);
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100221 *batch = NULL;
222 }
223}
224
225void tlb_remove_table(struct mmu_gather *tlb, void *table)
226{
227 struct mmu_table_batch **batch = &tlb->batch;
228
229 if (*batch == NULL) {
230 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
231 if (*batch == NULL) {
232 tlb_table_invalidate(tlb);
233 tlb_remove_table_one(table);
234 return;
235 }
236 (*batch)->nr = 0;
237 }
238
239 (*batch)->tables[(*batch)->nr++] = table;
240 if ((*batch)->nr == MAX_TABLE_BATCH)
241 tlb_table_flush(tlb);
242}
243
244#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
245
246/**
247 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
248 * @tlb: the mmu_gather structure to initialize
249 * @mm: the mm_struct of the target address space
250 * @start: start of the region that will be removed from the page-table
251 * @end: end of the region that will be removed from the page-table
252 *
253 * Called to initialize an (on-stack) mmu_gather structure for page-table
254 * tear-down from @mm. The @start and @end are set to 0 and -1
255 * respectively when @mm is without users and we're going to destroy
256 * the full address space (exit/execve).
257 */
258void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
259 unsigned long start, unsigned long end)
260{
261 arch_tlb_gather_mmu(tlb, mm, start, end);
262 inc_tlb_flush_pending(tlb->mm);
263}
264
265void tlb_finish_mmu(struct mmu_gather *tlb,
266 unsigned long start, unsigned long end)
267{
268 /*
269 * If there are parallel threads are doing PTE changes on same range
270 * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
271 * flush by batching, a thread has stable TLB entry can fail to flush
272 * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
273 * forcefully if we detect parallel PTE batching threads.
274 */
275 bool force = mm_tlb_flush_nested(tlb->mm);
276
277 arch_tlb_finish_mmu(tlb, start, end, force);
278 dec_tlb_flush_pending(tlb->mm);
279}