asm-generic/tlb: Remove tlb_flush_mmu_free()
[linux-block.git] / mm / mmu_gather.c
CommitLineData
196d9d8b
PZ
1#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
952a31c9
MS
14#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
15
196d9d8b
PZ
16static bool tlb_next_batch(struct mmu_gather *tlb)
17{
18 struct mmu_gather_batch *batch;
19
20 batch = tlb->active;
21 if (batch->next) {
22 tlb->active = batch->next;
23 return true;
24 }
25
26 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
27 return false;
28
29 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
30 if (!batch)
31 return false;
32
33 tlb->batch_count++;
34 batch->next = NULL;
35 batch->nr = 0;
36 batch->max = MAX_GATHER_BATCH;
37
38 tlb->active->next = batch;
39 tlb->active = batch;
40
41 return true;
42}
43
952a31c9
MS
44static void tlb_batch_pages_flush(struct mmu_gather *tlb)
45{
46 struct mmu_gather_batch *batch;
47
48 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
49 free_pages_and_swap_cache(batch->pages, batch->nr);
50 batch->nr = 0;
51 }
52 tlb->active = &tlb->local;
53}
54
55static void tlb_batch_list_free(struct mmu_gather *tlb)
56{
57 struct mmu_gather_batch *batch, *next;
58
59 for (batch = tlb->local.next; batch; batch = next) {
60 next = batch->next;
61 free_pages((unsigned long)batch, 0);
62 }
63 tlb->local.next = NULL;
64}
65
66bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
67{
68 struct mmu_gather_batch *batch;
69
70 VM_BUG_ON(!tlb->end);
71
72#ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
73 VM_WARN_ON(tlb->page_size != page_size);
74#endif
75
76 batch = tlb->active;
77 /*
78 * Add the page and check if we are full. If so
79 * force a flush.
80 */
81 batch->pages[batch->nr++] = page;
82 if (batch->nr == batch->max) {
83 if (!tlb_next_batch(tlb))
84 return true;
85 batch = tlb->active;
86 }
87 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
88
89 return false;
90}
91
92#endif /* HAVE_MMU_GATHER_NO_GATHER */
93
fa0aafb8 94static void tlb_flush_mmu_free(struct mmu_gather *tlb)
196d9d8b 95{
196d9d8b
PZ
96#ifdef CONFIG_HAVE_RCU_TABLE_FREE
97 tlb_table_flush(tlb);
98#endif
952a31c9
MS
99#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
100 tlb_batch_pages_flush(tlb);
101#endif
196d9d8b
PZ
102}
103
104void tlb_flush_mmu(struct mmu_gather *tlb)
105{
106 tlb_flush_mmu_tlbonly(tlb);
107 tlb_flush_mmu_free(tlb);
108}
109
196d9d8b
PZ
110#ifdef CONFIG_HAVE_RCU_TABLE_FREE
111
112/*
113 * See the comment near struct mmu_table_batch.
114 */
115
116/*
117 * If we want tlb_remove_table() to imply TLB invalidates.
118 */
119static inline void tlb_table_invalidate(struct mmu_gather *tlb)
120{
96bc9567 121#ifndef CONFIG_HAVE_RCU_TABLE_NO_INVALIDATE
196d9d8b
PZ
122 /*
123 * Invalidate page-table caches used by hardware walkers. Then we still
124 * need to RCU-sched wait while freeing the pages because software
125 * walkers can still be in-flight.
126 */
127 tlb_flush_mmu_tlbonly(tlb);
128#endif
129}
130
131static void tlb_remove_table_smp_sync(void *arg)
132{
133 /* Simply deliver the interrupt */
134}
135
136static void tlb_remove_table_one(void *table)
137{
138 /*
139 * This isn't an RCU grace period and hence the page-tables cannot be
140 * assumed to be actually RCU-freed.
141 *
142 * It is however sufficient for software page-table walkers that rely on
143 * IRQ disabling. See the comment near struct mmu_table_batch.
144 */
145 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
146 __tlb_remove_table(table);
147}
148
149static void tlb_remove_table_rcu(struct rcu_head *head)
150{
151 struct mmu_table_batch *batch;
152 int i;
153
154 batch = container_of(head, struct mmu_table_batch, rcu);
155
156 for (i = 0; i < batch->nr; i++)
157 __tlb_remove_table(batch->tables[i]);
158
159 free_page((unsigned long)batch);
160}
161
162void tlb_table_flush(struct mmu_gather *tlb)
163{
164 struct mmu_table_batch **batch = &tlb->batch;
165
166 if (*batch) {
167 tlb_table_invalidate(tlb);
b401ec18 168 call_rcu(&(*batch)->rcu, tlb_remove_table_rcu);
196d9d8b
PZ
169 *batch = NULL;
170 }
171}
172
173void tlb_remove_table(struct mmu_gather *tlb, void *table)
174{
175 struct mmu_table_batch **batch = &tlb->batch;
176
177 if (*batch == NULL) {
178 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
179 if (*batch == NULL) {
180 tlb_table_invalidate(tlb);
181 tlb_remove_table_one(table);
182 return;
183 }
184 (*batch)->nr = 0;
185 }
186
187 (*batch)->tables[(*batch)->nr++] = table;
188 if ((*batch)->nr == MAX_TABLE_BATCH)
189 tlb_table_flush(tlb);
190}
191
192#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
193
194/**
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
200 *
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).
205 */
206void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
207 unsigned long start, unsigned long end)
208{
1808d65b
PZ
209 tlb->mm = mm;
210
211 /* Is it from 0 to ~0? */
212 tlb->fullmm = !(start | (end+1));
213
214#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
215 tlb->need_flush_all = 0;
216 tlb->local.next = NULL;
217 tlb->local.nr = 0;
218 tlb->local.max = ARRAY_SIZE(tlb->__pages);
219 tlb->active = &tlb->local;
220 tlb->batch_count = 0;
221#endif
222
223#ifdef CONFIG_HAVE_RCU_TABLE_FREE
224 tlb->batch = NULL;
225#endif
226#ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
227 tlb->page_size = 0;
228#endif
229
230 __tlb_reset_range(tlb);
196d9d8b
PZ
231 inc_tlb_flush_pending(tlb->mm);
232}
233
1808d65b
PZ
234/**
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
239 *
240 * Called at the end of the shootdown operation to free up any resources that
241 * were required.
242 */
196d9d8b
PZ
243void tlb_finish_mmu(struct mmu_gather *tlb,
244 unsigned long start, unsigned long end)
245{
246 /*
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, a thread has stable TLB entry can fail to flush
250 * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
251 * forcefully if we detect parallel PTE batching threads.
252 */
1808d65b
PZ
253 if (mm_tlb_flush_nested(tlb->mm)) {
254 __tlb_reset_range(tlb);
255 __tlb_adjust_range(tlb, start, end - start);
256 }
196d9d8b 257
1808d65b
PZ
258 tlb_flush_mmu(tlb);
259
260 /* keep the page table cache within bounds */
261 check_pgt_cache();
262#ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
263 tlb_batch_list_free(tlb);
264#endif
196d9d8b
PZ
265 dec_tlb_flush_pending(tlb->mm);
266}