xref: /linux/mm/pgtable-generic.c (revision a5c0a39d0e573740b124cfabebef6f5f3573bf24)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  mm/pgtable-generic.c
4  *
5  *  Generic pgtable methods declared in linux/pgtable.h
6  *
7  *  Copyright (C) 2010  Linus Torvalds
8  */
9 
10 #include <linux/pagemap.h>
11 #include <linux/hugetlb.h>
12 #include <linux/pgtable.h>
13 #include <linux/swap.h>
14 #include <linux/swapops.h>
15 #include <linux/mm_inline.h>
16 #include <linux/iommu.h>
17 #include <linux/pgalloc.h>
18 
19 #include <asm/tlb.h>
20 
21 /*
22  * If a p?d_bad entry is found while walking page tables, report
23  * the error, before resetting entry to p?d_none.  Usually (but
24  * very seldom) called out from the p?d_none_or_clear_bad macros.
25  */
26 
27 void pgd_clear_bad(pgd_t *pgd)
28 {
29 	char str[PTVAL_STR_MAX];
30 
31 	ptval_to_str(str, pgd_val(*pgd));
32 	pr_err("bad pgd %s.\n", str);
33 	pgd_clear(pgd);
34 }
35 
36 #ifndef __PAGETABLE_P4D_FOLDED
37 void p4d_clear_bad(p4d_t *p4d)
38 {
39 	char str[PTVAL_STR_MAX];
40 
41 	ptval_to_str(str, p4d_val(*p4d));
42 	pr_err("bad p4d %s.\n", str);
43 	p4d_clear(p4d);
44 }
45 #endif
46 
47 #ifndef __PAGETABLE_PUD_FOLDED
48 void pud_clear_bad(pud_t *pud)
49 {
50 	char str[PTVAL_STR_MAX];
51 
52 	ptval_to_str(str, pud_val(*pud));
53 	pr_err("bad pud %s.\n", str);
54 	pud_clear(pud);
55 }
56 #endif
57 
58 /*
59  * Note that the pmd variant below can't be stub'ed out just as for p4d/pud
60  * above. pmd folding is special and typically pmd_* macros refer to upper
61  * level even when folded
62  */
63 void pmd_clear_bad(pmd_t *pmd)
64 {
65 	char str[PTVAL_STR_MAX];
66 
67 	ptval_to_str(str, pmd_val(*pmd));
68 	pr_err("bad pmd %s.\n", str);
69 	pmd_clear(pmd);
70 }
71 
72 #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
73 /*
74  * Only sets the access flags (dirty, accessed), as well as write
75  * permission. Furthermore, we know it always gets set to a "more
76  * permissive" setting, which allows most architectures to optimize
77  * this. We return whether the PTE actually changed, which in turn
78  * instructs the caller to do things like update__mmu_cache.  This
79  * used to be done in the caller, but sparc needs minor faults to
80  * force that call on sun4c so we changed this macro slightly
81  */
82 int ptep_set_access_flags(struct vm_area_struct *vma,
83 			  unsigned long address, pte_t *ptep,
84 			  pte_t entry, int dirty)
85 {
86 	int changed = !pte_same(ptep_get(ptep), entry);
87 	if (changed) {
88 		set_pte_at(vma->vm_mm, address, ptep, entry);
89 		flush_tlb_fix_spurious_fault(vma, address, ptep);
90 	}
91 	return changed;
92 }
93 #endif
94 
95 #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
96 bool ptep_clear_flush_young(struct vm_area_struct *vma,
97 		unsigned long address, pte_t *ptep)
98 {
99 	bool young;
100 
101 	young = ptep_test_and_clear_young(vma, address, ptep);
102 	if (young)
103 		flush_tlb_page(vma, address);
104 	return young;
105 }
106 #endif
107 
108 #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
109 pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address,
110 		       pte_t *ptep)
111 {
112 	struct mm_struct *mm = (vma)->vm_mm;
113 	pte_t pte;
114 	pte = ptep_get_and_clear(mm, address, ptep);
115 	if (pte_accessible(mm, pte))
116 		flush_tlb_page(vma, address);
117 	return pte;
118 }
119 #endif
120 
121 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
122 
123 #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
124 int pmdp_set_access_flags(struct vm_area_struct *vma,
125 			  unsigned long address, pmd_t *pmdp,
126 			  pmd_t entry, int dirty)
127 {
128 	int changed = !pmd_same(*pmdp, entry);
129 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
130 	if (changed) {
131 		set_pmd_at(vma->vm_mm, address, pmdp, entry);
132 		flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
133 	}
134 	return changed;
135 }
136 #endif
137 
138 #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
139 bool pmdp_clear_flush_young(struct vm_area_struct *vma,
140 		unsigned long address, pmd_t *pmdp)
141 {
142 	bool young;
143 
144 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
145 	young = pmdp_test_and_clear_young(vma, address, pmdp);
146 	if (young)
147 		flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
148 	return young;
149 }
150 #endif
151 
152 #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
153 pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
154 			    pmd_t *pmdp)
155 {
156 	pmd_t pmd;
157 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
158 	VM_BUG_ON(pmd_present(*pmdp) && !pmd_trans_huge(*pmdp));
159 	pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
160 	flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
161 	return pmd;
162 }
163 
164 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
165 pud_t pudp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
166 			    pud_t *pudp)
167 {
168 	pud_t pud;
169 
170 	VM_BUG_ON(address & ~HPAGE_PUD_MASK);
171 	VM_BUG_ON(!pud_trans_huge(*pudp));
172 	pud = pudp_huge_get_and_clear(vma->vm_mm, address, pudp);
173 	flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE);
174 	return pud;
175 }
176 #endif
177 #endif
178 
179 #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT
180 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
181 				pgtable_t pgtable)
182 {
183 	assert_spin_locked(pmd_lockptr(mm, pmdp));
184 
185 	/* FIFO */
186 	if (!pmd_huge_pte(mm, pmdp))
187 		INIT_LIST_HEAD(&pgtable->lru);
188 	else
189 		list_add(&pgtable->lru, &pmd_huge_pte(mm, pmdp)->lru);
190 	pmd_huge_pte(mm, pmdp) = pgtable;
191 }
192 #endif
193 
194 #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW
195 /* no "address" argument so destroys page coloring of some arch */
196 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
197 {
198 	pgtable_t pgtable;
199 
200 	assert_spin_locked(pmd_lockptr(mm, pmdp));
201 
202 	/* FIFO */
203 	pgtable = pmd_huge_pte(mm, pmdp);
204 	pmd_huge_pte(mm, pmdp) = list_first_entry_or_null(&pgtable->lru,
205 							  struct page, lru);
206 	if (pmd_huge_pte(mm, pmdp))
207 		list_del(&pgtable->lru);
208 	return pgtable;
209 }
210 #endif
211 
212 #ifndef __HAVE_ARCH_PMDP_INVALIDATE
213 pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
214 		     pmd_t *pmdp)
215 {
216 	VM_WARN_ON_ONCE(!pmd_present(*pmdp));
217 	pmd_t old = pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp));
218 	flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
219 	return old;
220 }
221 #endif
222 
223 #ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD
224 pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
225 			 pmd_t *pmdp)
226 {
227 	VM_WARN_ON_ONCE(!pmd_present(*pmdp));
228 	return pmdp_invalidate(vma, address, pmdp);
229 }
230 #endif
231 
232 #ifndef pmdp_collapse_flush
233 pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
234 			  pmd_t *pmdp)
235 {
236 	/*
237 	 * pmd and hugepage pte format are same. So we could
238 	 * use the same function.
239 	 */
240 	pmd_t pmd;
241 
242 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
243 	VM_BUG_ON(pmd_trans_huge(*pmdp));
244 	pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
245 
246 	/* collapse entails shooting down ptes not pmd */
247 	flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
248 	return pmd;
249 }
250 #endif
251 
252 /* arch define pte_free_defer in asm/pgalloc.h for its own implementation */
253 #ifndef pte_free_defer
254 static void pte_free_now(struct rcu_head *head)
255 {
256 	struct page *page;
257 
258 	page = container_of(head, struct page, rcu_head);
259 	pte_free(NULL /* mm not passed and not used */, (pgtable_t)page);
260 }
261 
262 void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable)
263 {
264 	struct page *page;
265 
266 	page = pgtable;
267 	call_rcu(&page->rcu_head, pte_free_now);
268 }
269 #endif /* pte_free_defer */
270 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
271 
272 #if defined(CONFIG_GUP_GET_PXX_LOW_HIGH) && \
273 	(defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RCU))
274 /*
275  * See the comment above ptep_get_lockless() in include/linux/pgtable.h:
276  * the barriers in pmdp_get_lockless() cannot guarantee that the value in
277  * pmd_high actually belongs with the value in pmd_low; but holding interrupts
278  * off blocks the TLB flush between present updates, which guarantees that a
279  * successful __pte_offset_map() points to a page from matched halves.
280  */
281 static unsigned long pmdp_get_lockless_start(void)
282 {
283 	unsigned long irqflags;
284 
285 	local_irq_save(irqflags);
286 	return irqflags;
287 }
288 static void pmdp_get_lockless_end(unsigned long irqflags)
289 {
290 	local_irq_restore(irqflags);
291 }
292 #else
293 static unsigned long pmdp_get_lockless_start(void) { return 0; }
294 static void pmdp_get_lockless_end(unsigned long irqflags) { }
295 #endif
296 
297 pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
298 {
299 	unsigned long irqflags;
300 	pmd_t pmdval;
301 
302 	rcu_read_lock();
303 	irqflags = pmdp_get_lockless_start();
304 	pmdval = pmdp_get_lockless(pmd);
305 	pmdp_get_lockless_end(irqflags);
306 
307 	if (pmdvalp)
308 		*pmdvalp = pmdval;
309 	if (unlikely(pmd_none(pmdval) || !pmd_present(pmdval)))
310 		goto nomap;
311 	if (unlikely(pmd_trans_huge(pmdval)))
312 		goto nomap;
313 	if (unlikely(pmd_bad(pmdval))) {
314 		pmd_clear_bad(pmd);
315 		goto nomap;
316 	}
317 	return __pte_map(&pmdval, addr);
318 nomap:
319 	rcu_read_unlock();
320 	return NULL;
321 }
322 
323 pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
324 				unsigned long addr, spinlock_t **ptlp)
325 {
326 	pmd_t pmdval;
327 	pte_t *pte;
328 
329 	pte = __pte_offset_map(pmd, addr, &pmdval);
330 	if (likely(pte))
331 		*ptlp = pte_lockptr(mm, &pmdval);
332 	return pte;
333 }
334 
335 pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
336 				unsigned long addr, pmd_t *pmdvalp,
337 				spinlock_t **ptlp)
338 {
339 	pte_t *pte;
340 
341 	VM_WARN_ON_ONCE(!pmdvalp);
342 	pte = __pte_offset_map(pmd, addr, pmdvalp);
343 	if (likely(pte))
344 		*ptlp = pte_lockptr(mm, pmdvalp);
345 	return pte;
346 }
347 
348 /*
349  * pte_offset_map_lock(mm, pmd, addr, ptlp) is usually called with the pmd
350  * pointer for addr, reached by walking down the mm's pgd, p4d, pud for addr:
351  * either while holding mmap_lock or vma lock for read or for write; or in
352  * truncate or rmap context, while holding file's i_mmap_lock or anon_vma lock
353  * for read (or for write). In a few cases, it may be used with pmd pointing to
354  * a pmd_t already copied to or constructed on the stack.
355  *
356  * When successful, it returns the pte pointer for addr, with its page table
357  * kmapped if necessary (when CONFIG_HIGHPTE), and locked against concurrent
358  * modification by software, with a pointer to that spinlock in ptlp (in some
359  * configs mm->page_table_lock, in SPLIT_PTLOCK configs a spinlock in table's
360  * struct page).  pte_unmap_unlock(pte, ptl) to unlock and unmap afterwards.
361  *
362  * But it is unsuccessful, returning NULL with *ptlp unchanged, if there is no
363  * page table at *pmd: if, for example, the page table has just been removed,
364  * or replaced by the huge pmd of a THP.  (When successful, *pmd is rechecked
365  * after acquiring the ptlock, and retried internally if it changed: so that a
366  * page table can be safely removed or replaced by THP while holding its lock.)
367  *
368  * pte_offset_map(pmd, addr), and its internal helper __pte_offset_map() above,
369  * just returns the pte pointer for addr, its page table kmapped if necessary;
370  * or NULL if there is no page table at *pmd.  It does not attempt to lock the
371  * page table, so cannot normally be used when the page table is to be updated,
372  * or when entries read must be stable.  But it does take rcu_read_lock(): so
373  * that even when page table is racily removed, it remains a valid though empty
374  * and disconnected table.  Until pte_unmap(pte) unmaps and rcu_read_unlock()s
375  * afterwards.
376  *
377  * pte_offset_map_ro_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map();
378  * but when successful, it also outputs a pointer to the spinlock in ptlp - as
379  * pte_offset_map_lock() does, but in this case without locking it.  This helps
380  * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that time
381  * act on a changed *pmd: pte_offset_map_ro_nolock() provides the correct spinlock
382  * pointer for the page table that it returns. Even after grabbing the spinlock,
383  * we might be looking either at a page table that is still mapped or one that
384  * was unmapped and is about to get freed. But for R/O access this is sufficient.
385  * So it is only applicable for read-only cases where any modification operations
386  * to the page table are not allowed even if the corresponding spinlock is held
387  * afterwards.
388  *
389  * pte_offset_map_rw_nolock(mm, pmd, addr, pmdvalp, ptlp), above, is like
390  * pte_offset_map_ro_nolock(); but when successful, it also outputs the pdmval.
391  * It is applicable for may-write cases where any modification operations to the
392  * page table may happen after the corresponding spinlock is held afterwards.
393  * But the users should make sure the page table is stable like checking pte_same()
394  * or checking pmd_same() by using the output pmdval before performing the write
395  * operations.
396  *
397  * Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will
398  * be read-only/read-write protected.
399  *
400  * Note that free_pgtables(), used after unmapping detached vmas or when exiting
401  * the whole mm, does not take a page table lock before freeing a page table.
402  *
403  * As page table freeing itself is RCU-safe, page table readers can safely run
404  * concurrently with page table teardown.
405  *
406  * However, writers CANNOT as, without a lock being held, nothing prevents
407  * concurrent teardown.
408  *
409  * Also note that the PGD itself is freed at mmdrop() time, not under RCU - so
410  * the walker must keep the mm alive either by pinning the mm or the VMA.
411  */
412 pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
413 			   unsigned long addr, spinlock_t **ptlp)
414 {
415 	spinlock_t *ptl;
416 	pmd_t pmdval;
417 	pte_t *pte;
418 again:
419 	pte = __pte_offset_map(pmd, addr, &pmdval);
420 	if (unlikely(!pte))
421 		return pte;
422 	ptl = pte_lockptr(mm, &pmdval);
423 	spin_lock(ptl);
424 	if (likely(pmd_same(pmdval, pmdp_get_lockless(pmd)))) {
425 		*ptlp = ptl;
426 		return pte;
427 	}
428 	pte_unmap_unlock(pte, ptl);
429 	goto again;
430 }
431 
432 #ifdef CONFIG_ASYNC_KERNEL_PGTABLE_FREE
433 static void kernel_pgtable_work_func(struct work_struct *work);
434 
435 static struct {
436 	struct list_head list;
437 	/* protect above ptdesc lists */
438 	spinlock_t lock;
439 	struct work_struct work;
440 } kernel_pgtable_work = {
441 	.list = LIST_HEAD_INIT(kernel_pgtable_work.list),
442 	.lock = __SPIN_LOCK_UNLOCKED(kernel_pgtable_work.lock),
443 	.work = __WORK_INITIALIZER(kernel_pgtable_work.work, kernel_pgtable_work_func),
444 };
445 
446 static void kernel_pgtable_work_func(struct work_struct *work)
447 {
448 	struct ptdesc *pt, *next;
449 	LIST_HEAD(page_list);
450 
451 	spin_lock(&kernel_pgtable_work.lock);
452 	list_splice_tail_init(&kernel_pgtable_work.list, &page_list);
453 	spin_unlock(&kernel_pgtable_work.lock);
454 
455 	iommu_sva_invalidate_kva_range(PAGE_OFFSET, TLB_FLUSH_ALL);
456 	list_for_each_entry_safe(pt, next, &page_list, pt_list)
457 		__pagetable_free(pt);
458 }
459 
460 static void schedule_kernel_pgtable_free(void)
461 {
462 	schedule_work(&kernel_pgtable_work.work);
463 }
464 
465 void pagetable_free_kernel(struct ptdesc *pt)
466 {
467 	spin_lock(&kernel_pgtable_work.lock);
468 	list_add(&pt->pt_list, &kernel_pgtable_work.list);
469 	spin_unlock(&kernel_pgtable_work.lock);
470 
471 	/*
472 	 * The workqueue may not exist yet while the system is booting.
473 	 * kernel_pgtable_drain_early() schedules the work once it does.
474 	 */
475 	if (system_state != SYSTEM_BOOTING)
476 		schedule_kernel_pgtable_free();
477 }
478 
479 static int __init kernel_pgtable_drain_early(void)
480 {
481 	/* Free the kernel page tables queued while booting. */
482 	schedule_kernel_pgtable_free();
483 	return 0;
484 }
485 core_initcall(kernel_pgtable_drain_early);
486 #endif
487