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>
9 #include <linux/swap.h>
11 #include <asm/pgalloc.h>
14 #ifdef HAVE_GENERIC_MMU_GATHER
16 static bool tlb_next_batch(struct mmu_gather
*tlb
)
18 struct mmu_gather_batch
*batch
;
22 tlb
->active
= batch
->next
;
26 if (tlb
->batch_count
== MAX_GATHER_BATCH_COUNT
)
29 batch
= (void *)__get_free_pages(GFP_NOWAIT
| __GFP_NOWARN
, 0);
36 batch
->max
= MAX_GATHER_BATCH
;
38 tlb
->active
->next
= batch
;
44 void arch_tlb_gather_mmu(struct mmu_gather
*tlb
, struct mm_struct
*mm
,
45 unsigned long start
, unsigned long end
)
49 /* Is it from 0 to ~0? */
50 tlb
->fullmm
= !(start
| (end
+1));
51 tlb
->need_flush_all
= 0;
52 tlb
->local
.next
= NULL
;
54 tlb
->local
.max
= ARRAY_SIZE(tlb
->__pages
);
55 tlb
->active
= &tlb
->local
;
58 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
63 __tlb_reset_range(tlb
);
66 void tlb_flush_mmu_free(struct mmu_gather
*tlb
)
68 struct mmu_gather_batch
*batch
;
70 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
73 for (batch
= &tlb
->local
; batch
&& batch
->nr
; batch
= batch
->next
) {
74 free_pages_and_swap_cache(batch
->pages
, batch
->nr
);
77 tlb
->active
= &tlb
->local
;
80 void tlb_flush_mmu(struct mmu_gather
*tlb
)
82 tlb_flush_mmu_tlbonly(tlb
);
83 tlb_flush_mmu_free(tlb
);
87 * Called at the end of the shootdown operation to free up any resources
90 void arch_tlb_finish_mmu(struct mmu_gather
*tlb
,
91 unsigned long start
, unsigned long end
, bool force
)
93 struct mmu_gather_batch
*batch
, *next
;
96 __tlb_reset_range(tlb
);
97 __tlb_adjust_range(tlb
, start
, end
- start
);
102 /* keep the page table cache within bounds */
105 for (batch
= tlb
->local
.next
; batch
; batch
= next
) {
107 free_pages((unsigned long)batch
, 0);
109 tlb
->local
.next
= NULL
;
113 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
114 * handling the additional races in SMP caused by other CPUs caching valid
115 * mappings in their TLBs. Returns the number of free page slots left.
116 * When out of page slots we must call tlb_flush_mmu().
117 *returns true if the caller should flush.
119 bool __tlb_remove_page_size(struct mmu_gather
*tlb
, struct page
*page
, int page_size
)
121 struct mmu_gather_batch
*batch
;
123 VM_BUG_ON(!tlb
->end
);
124 VM_WARN_ON(tlb
->page_size
!= page_size
);
128 * Add the page and check if we are full. If so
131 batch
->pages
[batch
->nr
++] = page
;
132 if (batch
->nr
== batch
->max
) {
133 if (!tlb_next_batch(tlb
))
137 VM_BUG_ON_PAGE(batch
->nr
> batch
->max
, page
);
142 #endif /* HAVE_GENERIC_MMU_GATHER */
144 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
147 * See the comment near struct mmu_table_batch.
151 * If we want tlb_remove_table() to imply TLB invalidates.
153 static inline void tlb_table_invalidate(struct mmu_gather
*tlb
)
155 #ifdef CONFIG_HAVE_RCU_TABLE_INVALIDATE
157 * Invalidate page-table caches used by hardware walkers. Then we still
158 * need to RCU-sched wait while freeing the pages because software
159 * walkers can still be in-flight.
161 tlb_flush_mmu_tlbonly(tlb
);
165 static void tlb_remove_table_smp_sync(void *arg
)
167 /* Simply deliver the interrupt */
170 static void tlb_remove_table_one(void *table
)
173 * This isn't an RCU grace period and hence the page-tables cannot be
174 * assumed to be actually RCU-freed.
176 * It is however sufficient for software page-table walkers that rely on
177 * IRQ disabling. See the comment near struct mmu_table_batch.
179 smp_call_function(tlb_remove_table_smp_sync
, NULL
, 1);
180 __tlb_remove_table(table
);
183 static void tlb_remove_table_rcu(struct rcu_head
*head
)
185 struct mmu_table_batch
*batch
;
188 batch
= container_of(head
, struct mmu_table_batch
, rcu
);
190 for (i
= 0; i
< batch
->nr
; i
++)
191 __tlb_remove_table(batch
->tables
[i
]);
193 free_page((unsigned long)batch
);
196 void tlb_table_flush(struct mmu_gather
*tlb
)
198 struct mmu_table_batch
**batch
= &tlb
->batch
;
201 tlb_table_invalidate(tlb
);
202 call_rcu_sched(&(*batch
)->rcu
, tlb_remove_table_rcu
);
207 void tlb_remove_table(struct mmu_gather
*tlb
, void *table
)
209 struct mmu_table_batch
**batch
= &tlb
->batch
;
211 if (*batch
== NULL
) {
212 *batch
= (struct mmu_table_batch
*)__get_free_page(GFP_NOWAIT
| __GFP_NOWARN
);
213 if (*batch
== NULL
) {
214 tlb_table_invalidate(tlb
);
215 tlb_remove_table_one(table
);
221 (*batch
)->tables
[(*batch
)->nr
++] = table
;
222 if ((*batch
)->nr
== MAX_TABLE_BATCH
)
223 tlb_table_flush(tlb
);
226 #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
229 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
230 * @tlb: the mmu_gather structure to initialize
231 * @mm: the mm_struct of the target address space
232 * @start: start of the region that will be removed from the page-table
233 * @end: end of the region that will be removed from the page-table
235 * Called to initialize an (on-stack) mmu_gather structure for page-table
236 * tear-down from @mm. The @start and @end are set to 0 and -1
237 * respectively when @mm is without users and we're going to destroy
238 * the full address space (exit/execve).
240 void tlb_gather_mmu(struct mmu_gather
*tlb
, struct mm_struct
*mm
,
241 unsigned long start
, unsigned long end
)
243 arch_tlb_gather_mmu(tlb
, mm
, start
, end
);
244 inc_tlb_flush_pending(tlb
->mm
);
247 void tlb_finish_mmu(struct mmu_gather
*tlb
,
248 unsigned long start
, unsigned long end
)
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.
257 bool force
= mm_tlb_flush_nested(tlb
->mm
);
259 arch_tlb_finish_mmu(tlb
, start
, end
, force
);
260 dec_tlb_flush_pending(tlb
->mm
);