1ef24e0aaSTim Bird // SPDX-License-Identifier: GPL-2.0
2196d9d8bSPeter Zijlstra #include <linux/gfp.h>
3196d9d8bSPeter Zijlstra #include <linux/highmem.h>
4196d9d8bSPeter Zijlstra #include <linux/kernel.h>
5196d9d8bSPeter Zijlstra #include <linux/mmdebug.h>
6196d9d8bSPeter Zijlstra #include <linux/mm_types.h>
736090defSArnd Bergmann #include <linux/mm_inline.h>
8196d9d8bSPeter Zijlstra #include <linux/pagemap.h>
9196d9d8bSPeter Zijlstra #include <linux/rcupdate.h>
10196d9d8bSPeter Zijlstra #include <linux/smp.h>
11196d9d8bSPeter Zijlstra #include <linux/swap.h>
125df397deSLinus Torvalds #include <linux/rmap.h>
13ad8b2e09SHarry Yoo #include <linux/pgalloc.h>
148ce720d5SDavid Hildenbrand (Red Hat) #include <linux/hugetlb.h>
15196d9d8bSPeter Zijlstra
16196d9d8bSPeter Zijlstra #include <asm/tlb.h>
17196d9d8bSPeter Zijlstra
18580a586cSPeter Zijlstra #ifndef CONFIG_MMU_GATHER_NO_GATHER
19952a31c9SMartin Schwidefsky
tlb_next_batch(struct mmu_gather * tlb)20196d9d8bSPeter Zijlstra static bool tlb_next_batch(struct mmu_gather *tlb)
21196d9d8bSPeter Zijlstra {
22196d9d8bSPeter Zijlstra struct mmu_gather_batch *batch;
23196d9d8bSPeter Zijlstra
24c4745482SLinus Torvalds /* Limit batching if we have delayed rmaps pending */
25c4745482SLinus Torvalds if (tlb->delayed_rmap && tlb->active != &tlb->local)
265df397deSLinus Torvalds return false;
275df397deSLinus Torvalds
28196d9d8bSPeter Zijlstra batch = tlb->active;
29196d9d8bSPeter Zijlstra if (batch->next) {
30196d9d8bSPeter Zijlstra tlb->active = batch->next;
31196d9d8bSPeter Zijlstra return true;
32196d9d8bSPeter Zijlstra }
33196d9d8bSPeter Zijlstra
34196d9d8bSPeter Zijlstra if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
35196d9d8bSPeter Zijlstra return false;
36196d9d8bSPeter Zijlstra
37adf085ffSQianfeng Rong batch = (void *)__get_free_page(GFP_NOWAIT);
38196d9d8bSPeter Zijlstra if (!batch)
39196d9d8bSPeter Zijlstra return false;
40196d9d8bSPeter Zijlstra
41196d9d8bSPeter Zijlstra tlb->batch_count++;
42196d9d8bSPeter Zijlstra batch->next = NULL;
43196d9d8bSPeter Zijlstra batch->nr = 0;
44196d9d8bSPeter Zijlstra batch->max = MAX_GATHER_BATCH;
45196d9d8bSPeter Zijlstra
46196d9d8bSPeter Zijlstra tlb->active->next = batch;
47196d9d8bSPeter Zijlstra tlb->active = batch;
48196d9d8bSPeter Zijlstra
49196d9d8bSPeter Zijlstra return true;
50196d9d8bSPeter Zijlstra }
51196d9d8bSPeter Zijlstra
525df397deSLinus Torvalds #ifdef CONFIG_SMP
tlb_flush_rmap_batch(struct mmu_gather_batch * batch,struct vm_area_struct * vma)53c4745482SLinus Torvalds static void tlb_flush_rmap_batch(struct mmu_gather_batch *batch, struct vm_area_struct *vma)
545df397deSLinus Torvalds {
55d7f861b9SDavid Hildenbrand struct encoded_page **pages = batch->encoded_pages;
56d7f861b9SDavid Hildenbrand
575df397deSLinus Torvalds for (int i = 0; i < batch->nr; i++) {
58d7f861b9SDavid Hildenbrand struct encoded_page *enc = pages[i];
595df397deSLinus Torvalds
60da510964SDavid Hildenbrand if (encoded_page_flags(enc) & ENCODED_PAGE_BIT_DELAY_RMAP) {
615df397deSLinus Torvalds struct page *page = encoded_page_ptr(enc);
62d7f861b9SDavid Hildenbrand unsigned int nr_pages = 1;
63d7f861b9SDavid Hildenbrand
64d7f861b9SDavid Hildenbrand if (unlikely(encoded_page_flags(enc) &
65d7f861b9SDavid Hildenbrand ENCODED_PAGE_BIT_NR_PAGES_NEXT))
66d7f861b9SDavid Hildenbrand nr_pages = encoded_nr_pages(pages[++i]);
67d7f861b9SDavid Hildenbrand
68d7f861b9SDavid Hildenbrand folio_remove_rmap_ptes(page_folio(page), page, nr_pages,
69d7f861b9SDavid Hildenbrand vma);
705df397deSLinus Torvalds }
715df397deSLinus Torvalds }
72c4745482SLinus Torvalds }
735df397deSLinus Torvalds
74c4745482SLinus Torvalds /**
75c4745482SLinus Torvalds * tlb_flush_rmaps - do pending rmap removals after we have flushed the TLB
76c4745482SLinus Torvalds * @tlb: the current mmu_gather
7719134bc2SMatthew Wilcox (Oracle) * @vma: The memory area from which the pages are being removed.
78c4745482SLinus Torvalds *
79c4745482SLinus Torvalds * Note that because of how tlb_next_batch() above works, we will
80c4745482SLinus Torvalds * never start multiple new batches with pending delayed rmaps, so
81c4745482SLinus Torvalds * we only need to walk through the current active batch and the
82c4745482SLinus Torvalds * original local one.
83c4745482SLinus Torvalds */
tlb_flush_rmaps(struct mmu_gather * tlb,struct vm_area_struct * vma)84c4745482SLinus Torvalds void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma)
85c4745482SLinus Torvalds {
86c4745482SLinus Torvalds if (!tlb->delayed_rmap)
87c4745482SLinus Torvalds return;
88c4745482SLinus Torvalds
89c4745482SLinus Torvalds tlb_flush_rmap_batch(&tlb->local, vma);
90c4745482SLinus Torvalds if (tlb->active != &tlb->local)
91c4745482SLinus Torvalds tlb_flush_rmap_batch(tlb->active, vma);
925df397deSLinus Torvalds tlb->delayed_rmap = 0;
935df397deSLinus Torvalds }
945df397deSLinus Torvalds #endif
955df397deSLinus Torvalds
96e61abd44SDavid Hildenbrand /*
97e61abd44SDavid Hildenbrand * We might end up freeing a lot of pages. Reschedule on a regular
98e61abd44SDavid Hildenbrand * basis to avoid soft lockups in configurations without full
99e61abd44SDavid Hildenbrand * preemption enabled. The magic number of 512 folios seems to work.
100e61abd44SDavid Hildenbrand */
101e61abd44SDavid Hildenbrand #define MAX_NR_FOLIOS_PER_FREE 512
102196d9d8bSPeter Zijlstra
__tlb_batch_free_encoded_pages(struct mmu_gather_batch * batch)103e61abd44SDavid Hildenbrand static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
104e61abd44SDavid Hildenbrand {
1057cc8f9c7SLinus Torvalds struct encoded_page **pages = batch->encoded_pages;
106e61abd44SDavid Hildenbrand unsigned int nr, nr_pages;
107b191c9bcSJianxing Wang
108d7f861b9SDavid Hildenbrand while (batch->nr) {
109e61abd44SDavid Hildenbrand if (!page_poisoning_enabled_static() && !want_init_on_free()) {
110e61abd44SDavid Hildenbrand nr = min(MAX_NR_FOLIOS_PER_FREE, batch->nr);
111b191c9bcSJianxing Wang
112d7f861b9SDavid Hildenbrand /*
113d7f861b9SDavid Hildenbrand * Make sure we cover page + nr_pages, and don't leave
114d7f861b9SDavid Hildenbrand * nr_pages behind when capping the number of entries.
115d7f861b9SDavid Hildenbrand */
116d7f861b9SDavid Hildenbrand if (unlikely(encoded_page_flags(pages[nr - 1]) &
117d7f861b9SDavid Hildenbrand ENCODED_PAGE_BIT_NR_PAGES_NEXT))
118d7f861b9SDavid Hildenbrand nr++;
119e61abd44SDavid Hildenbrand } else {
120e61abd44SDavid Hildenbrand /*
121e61abd44SDavid Hildenbrand * With page poisoning and init_on_free, the time it
122e61abd44SDavid Hildenbrand * takes to free memory grows proportionally with the
123e61abd44SDavid Hildenbrand * actual memory size. Therefore, limit based on the
124e61abd44SDavid Hildenbrand * actual memory size and not the number of involved
125e61abd44SDavid Hildenbrand * folios.
126e61abd44SDavid Hildenbrand */
127e61abd44SDavid Hildenbrand for (nr = 0, nr_pages = 0;
128e61abd44SDavid Hildenbrand nr < batch->nr && nr_pages < MAX_NR_FOLIOS_PER_FREE;
129e61abd44SDavid Hildenbrand nr++) {
130e61abd44SDavid Hildenbrand if (unlikely(encoded_page_flags(pages[nr]) &
131e61abd44SDavid Hildenbrand ENCODED_PAGE_BIT_NR_PAGES_NEXT))
132e61abd44SDavid Hildenbrand nr_pages += encoded_nr_pages(pages[++nr]);
133e61abd44SDavid Hildenbrand else
134e61abd44SDavid Hildenbrand nr_pages++;
135e61abd44SDavid Hildenbrand }
136e61abd44SDavid Hildenbrand }
137d7f861b9SDavid Hildenbrand
138b191c9bcSJianxing Wang free_pages_and_swap_cache(pages, nr);
139b191c9bcSJianxing Wang pages += nr;
140b191c9bcSJianxing Wang batch->nr -= nr;
141b191c9bcSJianxing Wang
142b191c9bcSJianxing Wang cond_resched();
143d7f861b9SDavid Hildenbrand }
144196d9d8bSPeter Zijlstra }
145e61abd44SDavid Hildenbrand
tlb_batch_pages_flush(struct mmu_gather * tlb)146e61abd44SDavid Hildenbrand static void tlb_batch_pages_flush(struct mmu_gather *tlb)
147e61abd44SDavid Hildenbrand {
148e61abd44SDavid Hildenbrand struct mmu_gather_batch *batch;
149e61abd44SDavid Hildenbrand
150e61abd44SDavid Hildenbrand for (batch = &tlb->local; batch && batch->nr; batch = batch->next)
151e61abd44SDavid Hildenbrand __tlb_batch_free_encoded_pages(batch);
152196d9d8bSPeter Zijlstra tlb->active = &tlb->local;
153196d9d8bSPeter Zijlstra }
154196d9d8bSPeter Zijlstra
tlb_batch_list_free(struct mmu_gather * tlb)155952a31c9SMartin Schwidefsky static void tlb_batch_list_free(struct mmu_gather *tlb)
156196d9d8bSPeter Zijlstra {
157196d9d8bSPeter Zijlstra struct mmu_gather_batch *batch, *next;
158196d9d8bSPeter Zijlstra
159196d9d8bSPeter Zijlstra for (batch = tlb->local.next; batch; batch = next) {
160196d9d8bSPeter Zijlstra next = batch->next;
161196d9d8bSPeter Zijlstra free_pages((unsigned long)batch, 0);
162196d9d8bSPeter Zijlstra }
163196d9d8bSPeter Zijlstra tlb->local.next = NULL;
164196d9d8bSPeter Zijlstra }
165196d9d8bSPeter Zijlstra
__tlb_remove_folio_pages_size(struct mmu_gather * tlb,struct page * page,unsigned int nr_pages,bool delay_rmap,int page_size)166d7f861b9SDavid Hildenbrand static bool __tlb_remove_folio_pages_size(struct mmu_gather *tlb,
167d7f861b9SDavid Hildenbrand struct page *page, unsigned int nr_pages, bool delay_rmap,
168d7f861b9SDavid Hildenbrand int page_size)
169196d9d8bSPeter Zijlstra {
170da510964SDavid Hildenbrand int flags = delay_rmap ? ENCODED_PAGE_BIT_DELAY_RMAP : 0;
171196d9d8bSPeter Zijlstra struct mmu_gather_batch *batch;
172196d9d8bSPeter Zijlstra
173196d9d8bSPeter Zijlstra VM_BUG_ON(!tlb->end);
174ed6a7935SPeter Zijlstra
1753af4bd03SPeter Zijlstra #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
176196d9d8bSPeter Zijlstra VM_WARN_ON(tlb->page_size != page_size);
177d7f861b9SDavid Hildenbrand VM_WARN_ON_ONCE(nr_pages != 1 && page_size != PAGE_SIZE);
178d7f861b9SDavid Hildenbrand VM_WARN_ON_ONCE(page_folio(page) != page_folio(page + nr_pages - 1));
179ed6a7935SPeter Zijlstra #endif
180196d9d8bSPeter Zijlstra
181196d9d8bSPeter Zijlstra batch = tlb->active;
182196d9d8bSPeter Zijlstra /*
183196d9d8bSPeter Zijlstra * Add the page and check if we are full. If so
184196d9d8bSPeter Zijlstra * force a flush.
185196d9d8bSPeter Zijlstra */
186d7f861b9SDavid Hildenbrand if (likely(nr_pages == 1)) {
187da510964SDavid Hildenbrand batch->encoded_pages[batch->nr++] = encode_page(page, flags);
188d7f861b9SDavid Hildenbrand } else {
189d7f861b9SDavid Hildenbrand flags |= ENCODED_PAGE_BIT_NR_PAGES_NEXT;
190d7f861b9SDavid Hildenbrand batch->encoded_pages[batch->nr++] = encode_page(page, flags);
191d7f861b9SDavid Hildenbrand batch->encoded_pages[batch->nr++] = encode_nr_pages(nr_pages);
192d7f861b9SDavid Hildenbrand }
193d7f861b9SDavid Hildenbrand /*
194d7f861b9SDavid Hildenbrand * Make sure that we can always add another "page" + "nr_pages",
195d7f861b9SDavid Hildenbrand * requiring two entries instead of only a single one.
196d7f861b9SDavid Hildenbrand */
197d7f861b9SDavid Hildenbrand if (batch->nr >= batch->max - 1) {
198196d9d8bSPeter Zijlstra if (!tlb_next_batch(tlb))
199196d9d8bSPeter Zijlstra return true;
200196d9d8bSPeter Zijlstra batch = tlb->active;
201196d9d8bSPeter Zijlstra }
202d7f861b9SDavid Hildenbrand VM_BUG_ON_PAGE(batch->nr > batch->max - 1, page);
203196d9d8bSPeter Zijlstra
204196d9d8bSPeter Zijlstra return false;
205196d9d8bSPeter Zijlstra }
206196d9d8bSPeter Zijlstra
__tlb_remove_folio_pages(struct mmu_gather * tlb,struct page * page,unsigned int nr_pages,bool delay_rmap)207d7f861b9SDavid Hildenbrand bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page,
208d7f861b9SDavid Hildenbrand unsigned int nr_pages, bool delay_rmap)
209d7f861b9SDavid Hildenbrand {
210d7f861b9SDavid Hildenbrand return __tlb_remove_folio_pages_size(tlb, page, nr_pages, delay_rmap,
211d7f861b9SDavid Hildenbrand PAGE_SIZE);
212d7f861b9SDavid Hildenbrand }
213d7f861b9SDavid Hildenbrand
__tlb_remove_page_size(struct mmu_gather * tlb,struct page * page,int page_size)214f9b74c13SWei Yang bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
215d7f861b9SDavid Hildenbrand {
216f9b74c13SWei Yang return __tlb_remove_folio_pages_size(tlb, page, 1, false, page_size);
217d7f861b9SDavid Hildenbrand }
218d7f861b9SDavid Hildenbrand
219580a586cSPeter Zijlstra #endif /* MMU_GATHER_NO_GATHER */
220952a31c9SMartin Schwidefsky
2210d6e24d4SPeter Zijlstra #ifdef CONFIG_MMU_GATHER_TABLE_FREE
2220d6e24d4SPeter Zijlstra
__tlb_remove_table_free(struct mmu_table_batch * batch)2230d6e24d4SPeter Zijlstra static void __tlb_remove_table_free(struct mmu_table_batch *batch)
2240d6e24d4SPeter Zijlstra {
2250d6e24d4SPeter Zijlstra int i;
2260d6e24d4SPeter Zijlstra
2270d6e24d4SPeter Zijlstra for (i = 0; i < batch->nr; i++)
2280d6e24d4SPeter Zijlstra __tlb_remove_table(batch->tables[i]);
2290d6e24d4SPeter Zijlstra
2300d6e24d4SPeter Zijlstra free_page((unsigned long)batch);
2310d6e24d4SPeter Zijlstra }
2320d6e24d4SPeter Zijlstra
233ff2e6d72SPeter Zijlstra #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
234196d9d8bSPeter Zijlstra
235196d9d8bSPeter Zijlstra /*
2360d6e24d4SPeter Zijlstra * Semi RCU freeing of the page directories.
2370d6e24d4SPeter Zijlstra *
2380d6e24d4SPeter Zijlstra * This is needed by some architectures to implement software pagetable walkers.
2390d6e24d4SPeter Zijlstra *
2400d6e24d4SPeter Zijlstra * gup_fast() and other software pagetable walkers do a lockless page-table
2410d6e24d4SPeter Zijlstra * walk and therefore needs some synchronization with the freeing of the page
2420d6e24d4SPeter Zijlstra * directories. The chosen means to accomplish that is by disabling IRQs over
2430d6e24d4SPeter Zijlstra * the walk.
2440d6e24d4SPeter Zijlstra *
2450d6e24d4SPeter Zijlstra * Architectures that use IPIs to flush TLBs will then automagically DTRT,
2460d6e24d4SPeter Zijlstra * since we unlink the page, flush TLBs, free the page. Since the disabling of
2470d6e24d4SPeter Zijlstra * IRQs delays the completion of the TLB flush we can never observe an already
2480d6e24d4SPeter Zijlstra * freed page.
2490d6e24d4SPeter Zijlstra *
250026e8b55SBrendan Jackman * Not all systems IPI every CPU for this purpose:
251026e8b55SBrendan Jackman *
252026e8b55SBrendan Jackman * - Some architectures have HW support for cross-CPU synchronisation of TLB
253026e8b55SBrendan Jackman * flushes, so there's no IPI at all.
254026e8b55SBrendan Jackman *
255026e8b55SBrendan Jackman * - Paravirt guests can do this TLB flushing in the hypervisor, or coordinate
256026e8b55SBrendan Jackman * with the hypervisor to defer flushing on preempted vCPUs.
257026e8b55SBrendan Jackman *
258026e8b55SBrendan Jackman * Such systems need to delay the freeing by some other means, this is that
259026e8b55SBrendan Jackman * means.
2600d6e24d4SPeter Zijlstra *
2610d6e24d4SPeter Zijlstra * What we do is batch the freed directory pages (tables) and RCU free them.
2620d6e24d4SPeter Zijlstra * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
2630d6e24d4SPeter Zijlstra * holds off grace periods.
2640d6e24d4SPeter Zijlstra *
2650d6e24d4SPeter Zijlstra * However, in order to batch these pages we need to allocate storage, this
2660d6e24d4SPeter Zijlstra * allocation is deep inside the MM code and can thus easily fail on memory
2670d6e24d4SPeter Zijlstra * pressure. To guarantee progress we fall back to single table freeing, see
2680d6e24d4SPeter Zijlstra * the implementation of tlb_remove_table_one().
2690d6e24d4SPeter Zijlstra *
270196d9d8bSPeter Zijlstra */
271196d9d8bSPeter Zijlstra
tlb_remove_table_smp_sync(void * arg)2720d6e24d4SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg)
2730d6e24d4SPeter Zijlstra {
2740d6e24d4SPeter Zijlstra /* Simply deliver the interrupt */
2750d6e24d4SPeter Zijlstra }
2760d6e24d4SPeter Zijlstra
tlb_remove_table_sync_one(void)2772ba99c5eSJann Horn void tlb_remove_table_sync_one(void)
2780d6e24d4SPeter Zijlstra {
2790d6e24d4SPeter Zijlstra /*
2800d6e24d4SPeter Zijlstra * This isn't an RCU grace period and hence the page-tables cannot be
2810d6e24d4SPeter Zijlstra * assumed to be actually RCU-freed.
2820d6e24d4SPeter Zijlstra *
2830d6e24d4SPeter Zijlstra * It is however sufficient for software page-table walkers that rely on
2840d6e24d4SPeter Zijlstra * IRQ disabling.
2850d6e24d4SPeter Zijlstra */
2860d6e24d4SPeter Zijlstra smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
2870d6e24d4SPeter Zijlstra }
2880d6e24d4SPeter Zijlstra
tlb_remove_table_rcu(struct rcu_head * head)2890d6e24d4SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head)
2900d6e24d4SPeter Zijlstra {
2910d6e24d4SPeter Zijlstra __tlb_remove_table_free(container_of(head, struct mmu_table_batch, rcu));
2920d6e24d4SPeter Zijlstra }
2930d6e24d4SPeter Zijlstra
tlb_remove_table_free(struct mmu_table_batch * batch)2940d6e24d4SPeter Zijlstra static void tlb_remove_table_free(struct mmu_table_batch *batch)
2950d6e24d4SPeter Zijlstra {
2960d6e24d4SPeter Zijlstra call_rcu(&batch->rcu, tlb_remove_table_rcu);
2970d6e24d4SPeter Zijlstra }
2980d6e24d4SPeter Zijlstra
299*1fb3d8c2SLance Yang /**
300*1fb3d8c2SLance Yang * tlb_remove_table_sync_rcu - synchronize with software page-table walkers
301*1fb3d8c2SLance Yang *
302*1fb3d8c2SLance Yang * Like tlb_remove_table_sync_one() but uses RCU grace period instead of IPI
303*1fb3d8c2SLance Yang * broadcast. Use in slow paths where sleeping is acceptable.
304*1fb3d8c2SLance Yang *
305*1fb3d8c2SLance Yang * Software/Lockless page-table walkers use local_irq_disable(), which is also
306*1fb3d8c2SLance Yang * an RCU read-side critical section. synchronize_rcu() waits for all such
307*1fb3d8c2SLance Yang * sections, providing the same guarantee as tlb_remove_table_sync_one() but
308*1fb3d8c2SLance Yang * without disrupting all CPUs with IPIs.
309*1fb3d8c2SLance Yang *
310*1fb3d8c2SLance Yang * Do not use for freeing memory. Use RCU callbacks instead to avoid latency
311*1fb3d8c2SLance Yang * spikes.
312*1fb3d8c2SLance Yang */
tlb_remove_table_sync_rcu(void)313*1fb3d8c2SLance Yang void tlb_remove_table_sync_rcu(void)
314*1fb3d8c2SLance Yang {
315*1fb3d8c2SLance Yang synchronize_rcu();
316*1fb3d8c2SLance Yang }
317*1fb3d8c2SLance Yang
3180d6e24d4SPeter Zijlstra #else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */
3190d6e24d4SPeter Zijlstra
tlb_remove_table_free(struct mmu_table_batch * batch)3200d6e24d4SPeter Zijlstra static void tlb_remove_table_free(struct mmu_table_batch *batch)
3210d6e24d4SPeter Zijlstra {
3220d6e24d4SPeter Zijlstra __tlb_remove_table_free(batch);
3230d6e24d4SPeter Zijlstra }
3240d6e24d4SPeter Zijlstra
3250d6e24d4SPeter Zijlstra #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
3260d6e24d4SPeter Zijlstra
327196d9d8bSPeter Zijlstra /*
328196d9d8bSPeter Zijlstra * If we want tlb_remove_table() to imply TLB invalidates.
329196d9d8bSPeter Zijlstra */
tlb_table_invalidate(struct mmu_gather * tlb)330196d9d8bSPeter Zijlstra static inline void tlb_table_invalidate(struct mmu_gather *tlb)
331196d9d8bSPeter Zijlstra {
3320ed13259SPeter Zijlstra if (tlb_needs_table_invalidate()) {
333196d9d8bSPeter Zijlstra /*
3340ed13259SPeter Zijlstra * Invalidate page-table caches used by hardware walkers. Then
3350ed13259SPeter Zijlstra * we still need to RCU-sched wait while freeing the pages
3360ed13259SPeter Zijlstra * because software walkers can still be in-flight.
337196d9d8bSPeter Zijlstra */
338196d9d8bSPeter Zijlstra tlb_flush_mmu_tlbonly(tlb);
3390ed13259SPeter Zijlstra }
340196d9d8bSPeter Zijlstra }
341196d9d8bSPeter Zijlstra
342e74e1731SQi Zheng #ifdef CONFIG_PT_RECLAIM
__tlb_remove_table_one_rcu(struct rcu_head * head)343e74e1731SQi Zheng static inline void __tlb_remove_table_one_rcu(struct rcu_head *head)
344e74e1731SQi Zheng {
345e74e1731SQi Zheng struct ptdesc *ptdesc;
346e74e1731SQi Zheng
347e74e1731SQi Zheng ptdesc = container_of(head, struct ptdesc, pt_rcu_head);
348e74e1731SQi Zheng __tlb_remove_table(ptdesc);
349e74e1731SQi Zheng }
350e74e1731SQi Zheng
__tlb_remove_table_one(void * table)351e74e1731SQi Zheng static inline void __tlb_remove_table_one(void *table)
352e74e1731SQi Zheng {
353e74e1731SQi Zheng struct ptdesc *ptdesc;
354e74e1731SQi Zheng
355e74e1731SQi Zheng ptdesc = table;
356e74e1731SQi Zheng call_rcu(&ptdesc->pt_rcu_head, __tlb_remove_table_one_rcu);
357e74e1731SQi Zheng }
358e74e1731SQi Zheng #else
__tlb_remove_table_one(void * table)359718b1386SQi Zheng static inline void __tlb_remove_table_one(void *table)
360196d9d8bSPeter Zijlstra {
361*1fb3d8c2SLance Yang tlb_remove_table_sync_rcu();
362196d9d8bSPeter Zijlstra __tlb_remove_table(table);
363196d9d8bSPeter Zijlstra }
364e74e1731SQi Zheng #endif /* CONFIG_PT_RECLAIM */
365718b1386SQi Zheng
tlb_remove_table_one(void * table)366718b1386SQi Zheng static void tlb_remove_table_one(void *table)
367718b1386SQi Zheng {
368718b1386SQi Zheng __tlb_remove_table_one(table);
369718b1386SQi Zheng }
370196d9d8bSPeter Zijlstra
tlb_table_flush(struct mmu_gather * tlb)3710a8caf21SPeter Zijlstra static void tlb_table_flush(struct mmu_gather *tlb)
372196d9d8bSPeter Zijlstra {
373196d9d8bSPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch;
374196d9d8bSPeter Zijlstra
375196d9d8bSPeter Zijlstra if (*batch) {
376196d9d8bSPeter Zijlstra tlb_table_invalidate(tlb);
3770d6e24d4SPeter Zijlstra tlb_remove_table_free(*batch);
378196d9d8bSPeter Zijlstra *batch = NULL;
379196d9d8bSPeter Zijlstra }
380196d9d8bSPeter Zijlstra }
381196d9d8bSPeter Zijlstra
tlb_remove_table(struct mmu_gather * tlb,void * table)382196d9d8bSPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table)
383196d9d8bSPeter Zijlstra {
384196d9d8bSPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch;
385196d9d8bSPeter Zijlstra
386196d9d8bSPeter Zijlstra if (*batch == NULL) {
387adf085ffSQianfeng Rong *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT);
388196d9d8bSPeter Zijlstra if (*batch == NULL) {
389196d9d8bSPeter Zijlstra tlb_table_invalidate(tlb);
390196d9d8bSPeter Zijlstra tlb_remove_table_one(table);
391196d9d8bSPeter Zijlstra return;
392196d9d8bSPeter Zijlstra }
393196d9d8bSPeter Zijlstra (*batch)->nr = 0;
394196d9d8bSPeter Zijlstra }
395196d9d8bSPeter Zijlstra
396196d9d8bSPeter Zijlstra (*batch)->tables[(*batch)->nr++] = table;
397196d9d8bSPeter Zijlstra if ((*batch)->nr == MAX_TABLE_BATCH)
398196d9d8bSPeter Zijlstra tlb_table_flush(tlb);
399196d9d8bSPeter Zijlstra }
400196d9d8bSPeter Zijlstra
tlb_table_init(struct mmu_gather * tlb)4010d6e24d4SPeter Zijlstra static inline void tlb_table_init(struct mmu_gather *tlb)
4020d6e24d4SPeter Zijlstra {
4030d6e24d4SPeter Zijlstra tlb->batch = NULL;
4040d6e24d4SPeter Zijlstra }
4050d6e24d4SPeter Zijlstra
4060d6e24d4SPeter Zijlstra #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
4070d6e24d4SPeter Zijlstra
tlb_table_flush(struct mmu_gather * tlb)4080d6e24d4SPeter Zijlstra static inline void tlb_table_flush(struct mmu_gather *tlb) { }
tlb_table_init(struct mmu_gather * tlb)4090d6e24d4SPeter Zijlstra static inline void tlb_table_init(struct mmu_gather *tlb) { }
4100d6e24d4SPeter Zijlstra
4110d6e24d4SPeter Zijlstra #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
412196d9d8bSPeter Zijlstra
tlb_flush_mmu_free(struct mmu_gather * tlb)4130a8caf21SPeter Zijlstra static void tlb_flush_mmu_free(struct mmu_gather *tlb)
4140a8caf21SPeter Zijlstra {
4150a8caf21SPeter Zijlstra tlb_table_flush(tlb);
416580a586cSPeter Zijlstra #ifndef CONFIG_MMU_GATHER_NO_GATHER
4170a8caf21SPeter Zijlstra tlb_batch_pages_flush(tlb);
4180a8caf21SPeter Zijlstra #endif
4190a8caf21SPeter Zijlstra }
4200a8caf21SPeter Zijlstra
tlb_flush_mmu(struct mmu_gather * tlb)4210a8caf21SPeter Zijlstra void tlb_flush_mmu(struct mmu_gather *tlb)
4220a8caf21SPeter Zijlstra {
4230a8caf21SPeter Zijlstra tlb_flush_mmu_tlbonly(tlb);
4240a8caf21SPeter Zijlstra tlb_flush_mmu_free(tlb);
4250a8caf21SPeter Zijlstra }
4260a8caf21SPeter Zijlstra
__tlb_gather_mmu(struct mmu_gather * tlb,struct mm_struct * mm,bool fullmm)427d8b45053SWill Deacon static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
428a72afd87SWill Deacon bool fullmm)
429196d9d8bSPeter Zijlstra {
4301808d65bSPeter Zijlstra tlb->mm = mm;
431a72afd87SWill Deacon tlb->fullmm = fullmm;
4321808d65bSPeter Zijlstra
433580a586cSPeter Zijlstra #ifndef CONFIG_MMU_GATHER_NO_GATHER
4341808d65bSPeter Zijlstra tlb->need_flush_all = 0;
4351808d65bSPeter Zijlstra tlb->local.next = NULL;
4361808d65bSPeter Zijlstra tlb->local.nr = 0;
4371808d65bSPeter Zijlstra tlb->local.max = ARRAY_SIZE(tlb->__pages);
4381808d65bSPeter Zijlstra tlb->active = &tlb->local;
4391808d65bSPeter Zijlstra tlb->batch_count = 0;
4401808d65bSPeter Zijlstra #endif
4415df397deSLinus Torvalds tlb->delayed_rmap = 0;
4421808d65bSPeter Zijlstra
4430d6e24d4SPeter Zijlstra tlb_table_init(tlb);
4443af4bd03SPeter Zijlstra #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
4451808d65bSPeter Zijlstra tlb->page_size = 0;
4461808d65bSPeter Zijlstra #endif
447bfe125f1SRoman Gushchin tlb->vma_pfn = 0;
4481808d65bSPeter Zijlstra
4498ce720d5SDavid Hildenbrand (Red Hat) tlb->fully_unshared_tables = 0;
4501808d65bSPeter Zijlstra __tlb_reset_range(tlb);
451196d9d8bSPeter Zijlstra inc_tlb_flush_pending(tlb->mm);
452196d9d8bSPeter Zijlstra }
453196d9d8bSPeter Zijlstra
454845be1cdSRandy Dunlap /**
455845be1cdSRandy Dunlap * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
456845be1cdSRandy Dunlap * @tlb: the mmu_gather structure to initialize
457845be1cdSRandy Dunlap * @mm: the mm_struct of the target address space
458845be1cdSRandy Dunlap *
459845be1cdSRandy Dunlap * Called to initialize an (on-stack) mmu_gather structure for page-table
460845be1cdSRandy Dunlap * tear-down from @mm.
461845be1cdSRandy Dunlap */
tlb_gather_mmu(struct mmu_gather * tlb,struct mm_struct * mm)462a72afd87SWill Deacon void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm)
463d8b45053SWill Deacon {
464a72afd87SWill Deacon __tlb_gather_mmu(tlb, mm, false);
465d8b45053SWill Deacon }
466d8b45053SWill Deacon
467845be1cdSRandy Dunlap /**
468845be1cdSRandy Dunlap * tlb_gather_mmu_fullmm - initialize an mmu_gather structure for page-table tear-down
469845be1cdSRandy Dunlap * @tlb: the mmu_gather structure to initialize
470845be1cdSRandy Dunlap * @mm: the mm_struct of the target address space
471845be1cdSRandy Dunlap *
472845be1cdSRandy Dunlap * In this case, @mm is without users and we're going to destroy the
473845be1cdSRandy Dunlap * full address space (exit/execve).
474845be1cdSRandy Dunlap *
475845be1cdSRandy Dunlap * Called to initialize an (on-stack) mmu_gather structure for page-table
476845be1cdSRandy Dunlap * tear-down from @mm.
477845be1cdSRandy Dunlap */
tlb_gather_mmu_fullmm(struct mmu_gather * tlb,struct mm_struct * mm)478d8b45053SWill Deacon void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm)
479d8b45053SWill Deacon {
480a72afd87SWill Deacon __tlb_gather_mmu(tlb, mm, true);
481d8b45053SWill Deacon }
482d8b45053SWill Deacon
4831808d65bSPeter Zijlstra /**
4848ce720d5SDavid Hildenbrand (Red Hat) * tlb_gather_mmu_vma - initialize an mmu_gather structure for operating on a
4858ce720d5SDavid Hildenbrand (Red Hat) * single VMA
4868ce720d5SDavid Hildenbrand (Red Hat) * @tlb: the mmu_gather structure to initialize
4878ce720d5SDavid Hildenbrand (Red Hat) * @vma: the vm_area_struct
4888ce720d5SDavid Hildenbrand (Red Hat) *
4898ce720d5SDavid Hildenbrand (Red Hat) * Called to initialize an (on-stack) mmu_gather structure for operating on
4908ce720d5SDavid Hildenbrand (Red Hat) * a single VMA. In contrast to tlb_gather_mmu(), calling this function will
4918ce720d5SDavid Hildenbrand (Red Hat) * not require another call to tlb_start_vma(). In contrast to tlb_start_vma(),
4928ce720d5SDavid Hildenbrand (Red Hat) * this function will *not* call flush_cache_range().
4938ce720d5SDavid Hildenbrand (Red Hat) *
4948ce720d5SDavid Hildenbrand (Red Hat) * For hugetlb VMAs, this function will also initialize the mmu_gather
4958ce720d5SDavid Hildenbrand (Red Hat) * page_size accordingly, not requiring a separate call to
4968ce720d5SDavid Hildenbrand (Red Hat) * tlb_change_page_size().
4978ce720d5SDavid Hildenbrand (Red Hat) *
4988ce720d5SDavid Hildenbrand (Red Hat) */
tlb_gather_mmu_vma(struct mmu_gather * tlb,struct vm_area_struct * vma)4998ce720d5SDavid Hildenbrand (Red Hat) void tlb_gather_mmu_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
5008ce720d5SDavid Hildenbrand (Red Hat) {
5018ce720d5SDavid Hildenbrand (Red Hat) tlb_gather_mmu(tlb, vma->vm_mm);
5028ce720d5SDavid Hildenbrand (Red Hat) tlb_update_vma_flags(tlb, vma);
5038ce720d5SDavid Hildenbrand (Red Hat) if (is_vm_hugetlb_page(vma))
5048ce720d5SDavid Hildenbrand (Red Hat) /* All entries have the same size. */
5058ce720d5SDavid Hildenbrand (Red Hat) tlb_change_page_size(tlb, huge_page_size(hstate_vma(vma)));
5068ce720d5SDavid Hildenbrand (Red Hat) }
5078ce720d5SDavid Hildenbrand (Red Hat)
5088ce720d5SDavid Hildenbrand (Red Hat) /**
5091808d65bSPeter Zijlstra * tlb_finish_mmu - finish an mmu_gather structure
5101808d65bSPeter Zijlstra * @tlb: the mmu_gather structure to finish
5111808d65bSPeter Zijlstra *
5121808d65bSPeter Zijlstra * Called at the end of the shootdown operation to free up any resources that
5131808d65bSPeter Zijlstra * were required.
5141808d65bSPeter Zijlstra */
tlb_finish_mmu(struct mmu_gather * tlb)515ae8eba8bSWill Deacon void tlb_finish_mmu(struct mmu_gather *tlb)
516196d9d8bSPeter Zijlstra {
517196d9d8bSPeter Zijlstra /*
5188ce720d5SDavid Hildenbrand (Red Hat) * We expect an earlier huge_pmd_unshare_flush() call to sort this out,
5198ce720d5SDavid Hildenbrand (Red Hat) * due to complicated locking requirements with page table unsharing.
5208ce720d5SDavid Hildenbrand (Red Hat) */
5218ce720d5SDavid Hildenbrand (Red Hat) VM_WARN_ON_ONCE(tlb->fully_unshared_tables);
5228ce720d5SDavid Hildenbrand (Red Hat)
5238ce720d5SDavid Hildenbrand (Red Hat) /*
524196d9d8bSPeter Zijlstra * If there are parallel threads are doing PTE changes on same range
525c1e8d7c6SMichel Lespinasse * under non-exclusive lock (e.g., mmap_lock read-side) but defer TLB
5267a30df49SYang Shi * flush by batching, one thread may end up seeing inconsistent PTEs
5277a30df49SYang Shi * and result in having stale TLB entries. So flush TLB forcefully
5287a30df49SYang Shi * if we detect parallel PTE batching threads.
5297a30df49SYang Shi *
5307a30df49SYang Shi * However, some syscalls, e.g. munmap(), may free page tables, this
5317a30df49SYang Shi * needs force flush everything in the given range. Otherwise this
5327a30df49SYang Shi * may result in having stale TLB entries for some architectures,
5337a30df49SYang Shi * e.g. aarch64, that could specify flush what level TLB.
534196d9d8bSPeter Zijlstra */
5351808d65bSPeter Zijlstra if (mm_tlb_flush_nested(tlb->mm)) {
5367a30df49SYang Shi /*
5377a30df49SYang Shi * The aarch64 yields better performance with fullmm by
5387a30df49SYang Shi * avoiding multiple CPUs spamming TLBI messages at the
5397a30df49SYang Shi * same time.
5407a30df49SYang Shi *
5417a30df49SYang Shi * On x86 non-fullmm doesn't yield significant difference
5427a30df49SYang Shi * against fullmm.
5437a30df49SYang Shi */
5447a30df49SYang Shi tlb->fullmm = 1;
5451808d65bSPeter Zijlstra __tlb_reset_range(tlb);
5467a30df49SYang Shi tlb->freed_tables = 1;
5471808d65bSPeter Zijlstra }
548196d9d8bSPeter Zijlstra
5491808d65bSPeter Zijlstra tlb_flush_mmu(tlb);
5501808d65bSPeter Zijlstra
551580a586cSPeter Zijlstra #ifndef CONFIG_MMU_GATHER_NO_GATHER
5521808d65bSPeter Zijlstra tlb_batch_list_free(tlb);
5531808d65bSPeter Zijlstra #endif
554196d9d8bSPeter Zijlstra dec_tlb_flush_pending(tlb->mm);
555196d9d8bSPeter Zijlstra }
556