blob: 60ef38d200c07300bb4470c8e261cdfb7011e2d8 [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
Martin Schwidefsky952a31c2018-09-18 14:51:50 +020096void tlb_flush_mmu_free(struct mmu_gather *tlb)
97{
98#ifdef CONFIG_HAVE_RCU_TABLE_FREE
99 tlb_table_flush(tlb);
100#endif
101#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
102 tlb_batch_pages_flush(tlb);
103#endif
104}
105
106void tlb_flush_mmu(struct mmu_gather *tlb)
107{
108 tlb_flush_mmu_tlbonly(tlb);
109 tlb_flush_mmu_free(tlb);
110}
111
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100112#endif /* HAVE_GENERIC_MMU_GATHER */
113
114#ifdef CONFIG_HAVE_RCU_TABLE_FREE
115
116/*
117 * See the comment near struct mmu_table_batch.
118 */
119
120/*
121 * If we want tlb_remove_table() to imply TLB invalidates.
122 */
123static inline void tlb_table_invalidate(struct mmu_gather *tlb)
124{
Peter Zijlstra96bc9562018-09-19 13:24:41 +0200125#ifndef CONFIG_HAVE_RCU_TABLE_NO_INVALIDATE
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100126 /*
127 * Invalidate page-table caches used by hardware walkers. Then we still
128 * need to RCU-sched wait while freeing the pages because software
129 * walkers can still be in-flight.
130 */
131 tlb_flush_mmu_tlbonly(tlb);
132#endif
133}
134
135static void tlb_remove_table_smp_sync(void *arg)
136{
137 /* Simply deliver the interrupt */
138}
139
140static void tlb_remove_table_one(void *table)
141{
142 /*
143 * This isn't an RCU grace period and hence the page-tables cannot be
144 * assumed to be actually RCU-freed.
145 *
146 * It is however sufficient for software page-table walkers that rely on
147 * IRQ disabling. See the comment near struct mmu_table_batch.
148 */
149 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
150 __tlb_remove_table(table);
151}
152
153static void tlb_remove_table_rcu(struct rcu_head *head)
154{
155 struct mmu_table_batch *batch;
156 int i;
157
158 batch = container_of(head, struct mmu_table_batch, rcu);
159
160 for (i = 0; i < batch->nr; i++)
161 __tlb_remove_table(batch->tables[i]);
162
163 free_page((unsigned long)batch);
164}
165
166void tlb_table_flush(struct mmu_gather *tlb)
167{
168 struct mmu_table_batch **batch = &tlb->batch;
169
170 if (*batch) {
171 tlb_table_invalidate(tlb);
Paul E. McKenneyb401ec12018-11-06 19:30:34 -0800172 call_rcu(&(*batch)->rcu, tlb_remove_table_rcu);
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100173 *batch = NULL;
174 }
175}
176
177void tlb_remove_table(struct mmu_gather *tlb, void *table)
178{
179 struct mmu_table_batch **batch = &tlb->batch;
180
181 if (*batch == NULL) {
182 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
183 if (*batch == NULL) {
184 tlb_table_invalidate(tlb);
185 tlb_remove_table_one(table);
186 return;
187 }
188 (*batch)->nr = 0;
189 }
190
191 (*batch)->tables[(*batch)->nr++] = table;
192 if ((*batch)->nr == MAX_TABLE_BATCH)
193 tlb_table_flush(tlb);
194}
195
196#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
197
198/**
199 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
200 * @tlb: the mmu_gather structure to initialize
201 * @mm: the mm_struct of the target address space
202 * @start: start of the region that will be removed from the page-table
203 * @end: end of the region that will be removed from the page-table
204 *
205 * Called to initialize an (on-stack) mmu_gather structure for page-table
206 * tear-down from @mm. The @start and @end are set to 0 and -1
207 * respectively when @mm is without users and we're going to destroy
208 * the full address space (exit/execve).
209 */
210void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
211 unsigned long start, unsigned long end)
212{
Peter Zijlstra1808d652018-09-20 10:50:11 +0200213 tlb->mm = mm;
214
215 /* Is it from 0 to ~0? */
216 tlb->fullmm = !(start | (end+1));
217
218#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
219 tlb->need_flush_all = 0;
220 tlb->local.next = NULL;
221 tlb->local.nr = 0;
222 tlb->local.max = ARRAY_SIZE(tlb->__pages);
223 tlb->active = &tlb->local;
224 tlb->batch_count = 0;
225#endif
226
227#ifdef CONFIG_HAVE_RCU_TABLE_FREE
228 tlb->batch = NULL;
229#endif
230#ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
231 tlb->page_size = 0;
232#endif
233
234 __tlb_reset_range(tlb);
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100235 inc_tlb_flush_pending(tlb->mm);
236}
237
Peter Zijlstra1808d652018-09-20 10:50:11 +0200238/**
239 * tlb_finish_mmu - finish an mmu_gather structure
240 * @tlb: the mmu_gather structure to finish
241 * @start: start of the region that will be removed from the page-table
242 * @end: end of the region that will be removed from the page-table
243 *
244 * Called at the end of the shootdown operation to free up any resources that
245 * were required.
246 */
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100247void tlb_finish_mmu(struct mmu_gather *tlb,
248 unsigned long start, unsigned long end)
249{
250 /*
251 * If there are parallel threads are doing PTE changes on same range
252 * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
253 * flush by batching, a thread has stable TLB entry can fail to flush
254 * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
255 * forcefully if we detect parallel PTE batching threads.
256 */
Peter Zijlstra1808d652018-09-20 10:50:11 +0200257 if (mm_tlb_flush_nested(tlb->mm)) {
258 __tlb_reset_range(tlb);
259 __tlb_adjust_range(tlb, start, end - start);
260 }
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100261
Peter Zijlstra1808d652018-09-20 10:50:11 +0200262 tlb_flush_mmu(tlb);
263
264 /* keep the page table cache within bounds */
265 check_pgt_cache();
266#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
267 tlb_batch_list_free(tlb);
268#endif
Peter Zijlstra196d9d82018-09-03 15:07:36 +0100269 dec_tlb_flush_pending(tlb->mm);
270}