GNU Linux-libre 5.4.257-gnu1
[releases.git] / mm / mmu_gather.c
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
14 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
15
16 static 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
44 static 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
55 static 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
66 bool __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
94 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
95
96 /*
97  * See the comment near struct mmu_table_batch.
98  */
99
100 /*
101  * If we want tlb_remove_table() to imply TLB invalidates.
102  */
103 static inline void tlb_table_invalidate(struct mmu_gather *tlb)
104 {
105         if (tlb_needs_table_invalidate()) {
106                 /*
107                  * Invalidate page-table caches used by hardware walkers. Then
108                  * we still need to RCU-sched wait while freeing the pages
109                  * because software walkers can still be in-flight.
110                  */
111                 tlb_flush_mmu_tlbonly(tlb);
112         }
113 }
114
115 static void tlb_remove_table_smp_sync(void *arg)
116 {
117         /* Simply deliver the interrupt */
118 }
119
120 void tlb_remove_table_sync_one(void)
121 {
122         smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
123 }
124
125 static void tlb_remove_table_one(void *table)
126 {
127         /*
128          * This isn't an RCU grace period and hence the page-tables cannot be
129          * assumed to be actually RCU-freed.
130          *
131          * It is however sufficient for software page-table walkers that rely on
132          * IRQ disabling. See the comment near struct mmu_table_batch.
133          */
134         smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
135         __tlb_remove_table(table);
136 }
137
138 static void tlb_remove_table_rcu(struct rcu_head *head)
139 {
140         struct mmu_table_batch *batch;
141         int i;
142
143         batch = container_of(head, struct mmu_table_batch, rcu);
144
145         for (i = 0; i < batch->nr; i++)
146                 __tlb_remove_table(batch->tables[i]);
147
148         free_page((unsigned long)batch);
149 }
150
151 static void tlb_table_flush(struct mmu_gather *tlb)
152 {
153         struct mmu_table_batch **batch = &tlb->batch;
154
155         if (*batch) {
156                 tlb_table_invalidate(tlb);
157                 call_rcu(&(*batch)->rcu, tlb_remove_table_rcu);
158                 *batch = NULL;
159         }
160 }
161
162 void tlb_remove_table(struct mmu_gather *tlb, void *table)
163 {
164         struct mmu_table_batch **batch = &tlb->batch;
165
166         if (*batch == NULL) {
167                 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
168                 if (*batch == NULL) {
169                         tlb_table_invalidate(tlb);
170                         tlb_remove_table_one(table);
171                         return;
172                 }
173                 (*batch)->nr = 0;
174         }
175
176         (*batch)->tables[(*batch)->nr++] = table;
177         if ((*batch)->nr == MAX_TABLE_BATCH)
178                 tlb_table_flush(tlb);
179 }
180
181 #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
182
183 static void tlb_flush_mmu_free(struct mmu_gather *tlb)
184 {
185 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
186         tlb_table_flush(tlb);
187 #endif
188 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
189         tlb_batch_pages_flush(tlb);
190 #endif
191 }
192
193 void tlb_flush_mmu(struct mmu_gather *tlb)
194 {
195         tlb_flush_mmu_tlbonly(tlb);
196         tlb_flush_mmu_free(tlb);
197 }
198
199 /**
200  * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
201  * @tlb: the mmu_gather structure to initialize
202  * @mm: the mm_struct of the target address space
203  * @start: start of the region that will be removed from the page-table
204  * @end: end of the region that will be removed from the page-table
205  *
206  * Called to initialize an (on-stack) mmu_gather structure for page-table
207  * tear-down from @mm. The @start and @end are set to 0 and -1
208  * respectively when @mm is without users and we're going to destroy
209  * the full address space (exit/execve).
210  */
211 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
212                         unsigned long start, unsigned long end)
213 {
214         tlb->mm = mm;
215
216         /* Is it from 0 to ~0? */
217         tlb->fullmm     = !(start | (end+1));
218
219 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
220         tlb->need_flush_all = 0;
221         tlb->local.next = NULL;
222         tlb->local.nr   = 0;
223         tlb->local.max  = ARRAY_SIZE(tlb->__pages);
224         tlb->active     = &tlb->local;
225         tlb->batch_count = 0;
226 #endif
227
228 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
229         tlb->batch = NULL;
230 #endif
231 #ifdef CONFIG_HAVE_MMU_GATHER_PAGE_SIZE
232         tlb->page_size = 0;
233 #endif
234
235         __tlb_reset_range(tlb);
236         inc_tlb_flush_pending(tlb->mm);
237 }
238
239 /**
240  * tlb_finish_mmu - finish an mmu_gather structure
241  * @tlb: the mmu_gather structure to finish
242  * @start: start of the region that will be removed from the page-table
243  * @end: end of the region that will be removed from the page-table
244  *
245  * Called at the end of the shootdown operation to free up any resources that
246  * were required.
247  */
248 void tlb_finish_mmu(struct mmu_gather *tlb,
249                 unsigned long start, unsigned long end)
250 {
251         /*
252          * If there are parallel threads are doing PTE changes on same range
253          * under non-exclusive lock (e.g., mmap_sem read-side) but defer TLB
254          * flush by batching, one thread may end up seeing inconsistent PTEs
255          * and result in having stale TLB entries.  So flush TLB forcefully
256          * if we detect parallel PTE batching threads.
257          *
258          * However, some syscalls, e.g. munmap(), may free page tables, this
259          * needs force flush everything in the given range. Otherwise this
260          * may result in having stale TLB entries for some architectures,
261          * e.g. aarch64, that could specify flush what level TLB.
262          */
263         if (mm_tlb_flush_nested(tlb->mm)) {
264                 /*
265                  * The aarch64 yields better performance with fullmm by
266                  * avoiding multiple CPUs spamming TLBI messages at the
267                  * same time.
268                  *
269                  * On x86 non-fullmm doesn't yield significant difference
270                  * against fullmm.
271                  */
272                 tlb->fullmm = 1;
273                 __tlb_reset_range(tlb);
274                 tlb->freed_tables = 1;
275         }
276
277         tlb_flush_mmu(tlb);
278
279 #ifndef CONFIG_HAVE_MMU_GATHER_NO_GATHER
280         tlb_batch_list_free(tlb);
281 #endif
282         dec_tlb_flush_pending(tlb->mm);
283 }