1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PGTABLE_H
3 #define _LINUX_PGTABLE_H
4
5 #include <linux/pfn.h>
6 #include <asm/pgtable.h>
7
8 #define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
9 #define PUD_ORDER (PUD_SHIFT - PAGE_SHIFT)
10
11 #ifndef __ASSEMBLY__
12 #ifdef CONFIG_MMU
13
14 #include <linux/mm_types.h>
15 #include <linux/bug.h>
16 #include <linux/errno.h>
17 #include <asm-generic/pgtable_uffd.h>
18 #include <linux/page_table_check.h>
19
20 #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
21 defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
22 #error CONFIG_PGTABLE_LEVELS is not consistent with __PAGETABLE_{P4D,PUD,PMD}_FOLDED
23 #endif
24
25 /*
26 * On almost all architectures and configurations, 0 can be used as the
27 * upper ceiling to free_pgtables(): on many architectures it has the same
28 * effect as using TASK_SIZE. However, there is one configuration which
29 * must impose a more careful limit, to avoid freeing kernel pgtables.
30 */
31 #ifndef USER_PGTABLES_CEILING
32 #define USER_PGTABLES_CEILING 0UL
33 #endif
34
35 /*
36 * This defines the first usable user address. Platforms
37 * can override its value with custom FIRST_USER_ADDRESS
38 * defined in their respective <asm/pgtable.h>.
39 */
40 #ifndef FIRST_USER_ADDRESS
41 #define FIRST_USER_ADDRESS 0UL
42 #endif
43
44 /*
45 * This defines the generic helper for accessing PMD page
46 * table page. Although platforms can still override this
47 * via their respective <asm/pgtable.h>.
48 */
49 #ifndef pmd_pgtable
50 #define pmd_pgtable(pmd) pmd_page(pmd)
51 #endif
52
53 #define pmd_folio(pmd) page_folio(pmd_page(pmd))
54
55 /*
56 * A page table page can be thought of an array like this: pXd_t[PTRS_PER_PxD]
57 *
58 * The pXx_index() functions return the index of the entry in the page
59 * table page which would control the given virtual address
60 *
61 * As these functions may be used by the same code for different levels of
62 * the page table folding, they are always available, regardless of
63 * CONFIG_PGTABLE_LEVELS value. For the folded levels they simply return 0
64 * because in such cases PTRS_PER_PxD equals 1.
65 */
66
pte_index(unsigned long address)67 static inline unsigned long pte_index(unsigned long address)
68 {
69 return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
70 }
71
72 #ifndef pmd_index
pmd_index(unsigned long address)73 static inline unsigned long pmd_index(unsigned long address)
74 {
75 return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
76 }
77 #define pmd_index pmd_index
78 #endif
79
80 #ifndef pud_index
pud_index(unsigned long address)81 static inline unsigned long pud_index(unsigned long address)
82 {
83 return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
84 }
85 #define pud_index pud_index
86 #endif
87
88 #ifndef pgd_index
89 /* Must be a compile-time constant, so implement it as a macro */
90 #define pgd_index(a) (((a) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
91 #endif
92
93 #ifndef pte_offset_kernel
pte_offset_kernel(pmd_t * pmd,unsigned long address)94 static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address)
95 {
96 return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address);
97 }
98 #define pte_offset_kernel pte_offset_kernel
99 #endif
100
101 #ifdef CONFIG_HIGHPTE
102 #define __pte_map(pmd, address) \
103 ((pte_t *)kmap_local_page(pmd_page(*(pmd))) + pte_index((address)))
104 #define pte_unmap(pte) do { \
105 kunmap_local((pte)); \
106 rcu_read_unlock(); \
107 } while (0)
108 #else
__pte_map(pmd_t * pmd,unsigned long address)109 static inline pte_t *__pte_map(pmd_t *pmd, unsigned long address)
110 {
111 return pte_offset_kernel(pmd, address);
112 }
pte_unmap(pte_t * pte)113 static inline void pte_unmap(pte_t *pte)
114 {
115 rcu_read_unlock();
116 }
117 #endif
118
119 void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable);
120
121 /* Find an entry in the second-level page table.. */
122 #ifndef pmd_offset
pmd_offset(pud_t * pud,unsigned long address)123 static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
124 {
125 return pud_pgtable(*pud) + pmd_index(address);
126 }
127 #define pmd_offset pmd_offset
128 #endif
129
130 #ifndef pud_offset
pud_offset(p4d_t * p4d,unsigned long address)131 static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
132 {
133 return p4d_pgtable(*p4d) + pud_index(address);
134 }
135 #define pud_offset pud_offset
136 #endif
137
pgd_offset_pgd(pgd_t * pgd,unsigned long address)138 static inline pgd_t *pgd_offset_pgd(pgd_t *pgd, unsigned long address)
139 {
140 return (pgd + pgd_index(address));
141 };
142
143 /*
144 * a shortcut to get a pgd_t in a given mm
145 */
146 #ifndef pgd_offset
147 #define pgd_offset(mm, address) pgd_offset_pgd((mm)->pgd, (address))
148 #endif
149
150 /*
151 * a shortcut which implies the use of the kernel's pgd, instead
152 * of a process's
153 */
154 #define pgd_offset_k(address) pgd_offset(&init_mm, (address))
155
156 /*
157 * In many cases it is known that a virtual address is mapped at PMD or PTE
158 * level, so instead of traversing all the page table levels, we can get a
159 * pointer to the PMD entry in user or kernel page table or translate a virtual
160 * address to the pointer in the PTE in the kernel page tables with simple
161 * helpers.
162 */
pmd_off(struct mm_struct * mm,unsigned long va)163 static inline pmd_t *pmd_off(struct mm_struct *mm, unsigned long va)
164 {
165 return pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, va), va), va), va);
166 }
167
pmd_off_k(unsigned long va)168 static inline pmd_t *pmd_off_k(unsigned long va)
169 {
170 return pmd_offset(pud_offset(p4d_offset(pgd_offset_k(va), va), va), va);
171 }
172
virt_to_kpte(unsigned long vaddr)173 static inline pte_t *virt_to_kpte(unsigned long vaddr)
174 {
175 pmd_t *pmd = pmd_off_k(vaddr);
176
177 return pmd_none(*pmd) ? NULL : pte_offset_kernel(pmd, vaddr);
178 }
179
180 #ifndef pmd_young
pmd_young(pmd_t pmd)181 static inline int pmd_young(pmd_t pmd)
182 {
183 return 0;
184 }
185 #endif
186
187 #ifndef pmd_dirty
pmd_dirty(pmd_t pmd)188 static inline int pmd_dirty(pmd_t pmd)
189 {
190 return 0;
191 }
192 #endif
193
194 /*
195 * A facility to provide lazy MMU batching. This allows PTE updates and
196 * page invalidations to be delayed until a call to leave lazy MMU mode
197 * is issued. Some architectures may benefit from doing this, and it is
198 * beneficial for both shadow and direct mode hypervisors, which may batch
199 * the PTE updates which happen during this window. Note that using this
200 * interface requires that read hazards be removed from the code. A read
201 * hazard could result in the direct mode hypervisor case, since the actual
202 * write to the page tables may not yet have taken place, so reads though
203 * a raw PTE pointer after it has been modified are not guaranteed to be
204 * up to date.
205 *
206 * In the general case, no lock is guaranteed to be held between entry and exit
207 * of the lazy mode. So the implementation must assume preemption may be enabled
208 * and cpu migration is possible; it must take steps to be robust against this.
209 * (In practice, for user PTE updates, the appropriate page table lock(s) are
210 * held, but for kernel PTE updates, no lock is held). Nesting is not permitted
211 * and the mode cannot be used in interrupt context.
212 */
213 #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
214 #define arch_enter_lazy_mmu_mode() do {} while (0)
215 #define arch_leave_lazy_mmu_mode() do {} while (0)
216 #define arch_flush_lazy_mmu_mode() do {} while (0)
217 #endif
218
219 #ifndef pte_batch_hint
220 /**
221 * pte_batch_hint - Number of pages that can be added to batch without scanning.
222 * @ptep: Page table pointer for the entry.
223 * @pte: Page table entry.
224 *
225 * Some architectures know that a set of contiguous ptes all map the same
226 * contiguous memory with the same permissions. In this case, it can provide a
227 * hint to aid pte batching without the core code needing to scan every pte.
228 *
229 * An architecture implementation may ignore the PTE accessed state. Further,
230 * the dirty state must apply atomically to all the PTEs described by the hint.
231 *
232 * May be overridden by the architecture, else pte_batch_hint is always 1.
233 */
pte_batch_hint(pte_t * ptep,pte_t pte)234 static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte)
235 {
236 return 1;
237 }
238 #endif
239
240 #ifndef pte_advance_pfn
pte_advance_pfn(pte_t pte,unsigned long nr)241 static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr)
242 {
243 return __pte(pte_val(pte) + (nr << PFN_PTE_SHIFT));
244 }
245 #endif
246
247 #define pte_next_pfn(pte) pte_advance_pfn(pte, 1)
248
249 #ifndef set_ptes
250 /**
251 * set_ptes - Map consecutive pages to a contiguous range of addresses.
252 * @mm: Address space to map the pages into.
253 * @addr: Address to map the first page at.
254 * @ptep: Page table pointer for the first entry.
255 * @pte: Page table entry for the first page.
256 * @nr: Number of pages to map.
257 *
258 * When nr==1, initial state of pte may be present or not present, and new state
259 * may be present or not present. When nr>1, initial state of all ptes must be
260 * not present, and new state must be present.
261 *
262 * May be overridden by the architecture, or the architecture can define
263 * set_pte() and PFN_PTE_SHIFT.
264 *
265 * Context: The caller holds the page table lock. The pages all belong
266 * to the same folio. The PTEs are all in the same PMD.
267 */
set_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte,unsigned int nr)268 static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
269 pte_t *ptep, pte_t pte, unsigned int nr)
270 {
271 page_table_check_ptes_set(mm, ptep, pte, nr);
272
273 for (;;) {
274 set_pte(ptep, pte);
275 if (--nr == 0)
276 break;
277 ptep++;
278 pte = pte_next_pfn(pte);
279 }
280 }
281 #endif
282 #define set_pte_at(mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1)
283
284 #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
285 extern int ptep_set_access_flags(struct vm_area_struct *vma,
286 unsigned long address, pte_t *ptep,
287 pte_t entry, int dirty);
288 #endif
289
290 #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
291 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
292 extern int pmdp_set_access_flags(struct vm_area_struct *vma,
293 unsigned long address, pmd_t *pmdp,
294 pmd_t entry, int dirty);
295 extern int pudp_set_access_flags(struct vm_area_struct *vma,
296 unsigned long address, pud_t *pudp,
297 pud_t entry, int dirty);
298 #else
pmdp_set_access_flags(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp,pmd_t entry,int dirty)299 static inline int pmdp_set_access_flags(struct vm_area_struct *vma,
300 unsigned long address, pmd_t *pmdp,
301 pmd_t entry, int dirty)
302 {
303 BUILD_BUG();
304 return 0;
305 }
pudp_set_access_flags(struct vm_area_struct * vma,unsigned long address,pud_t * pudp,pud_t entry,int dirty)306 static inline int pudp_set_access_flags(struct vm_area_struct *vma,
307 unsigned long address, pud_t *pudp,
308 pud_t entry, int dirty)
309 {
310 BUILD_BUG();
311 return 0;
312 }
313 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
314 #endif
315
316 #ifndef ptep_get
ptep_get(pte_t * ptep)317 static inline pte_t ptep_get(pte_t *ptep)
318 {
319 return READ_ONCE(*ptep);
320 }
321 #endif
322
323 #ifndef pmdp_get
pmdp_get(pmd_t * pmdp)324 static inline pmd_t pmdp_get(pmd_t *pmdp)
325 {
326 return READ_ONCE(*pmdp);
327 }
328 #endif
329
330 #ifndef pudp_get
pudp_get(pud_t * pudp)331 static inline pud_t pudp_get(pud_t *pudp)
332 {
333 return READ_ONCE(*pudp);
334 }
335 #endif
336
337 #ifndef p4dp_get
p4dp_get(p4d_t * p4dp)338 static inline p4d_t p4dp_get(p4d_t *p4dp)
339 {
340 return READ_ONCE(*p4dp);
341 }
342 #endif
343
344 #ifndef pgdp_get
pgdp_get(pgd_t * pgdp)345 static inline pgd_t pgdp_get(pgd_t *pgdp)
346 {
347 return READ_ONCE(*pgdp);
348 }
349 #endif
350
351 #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
ptep_test_and_clear_young(struct vm_area_struct * vma,unsigned long address,pte_t * ptep)352 static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
353 unsigned long address,
354 pte_t *ptep)
355 {
356 pte_t pte = ptep_get(ptep);
357 int r = 1;
358 if (!pte_young(pte))
359 r = 0;
360 else
361 set_pte_at(vma->vm_mm, address, ptep, pte_mkold(pte));
362 return r;
363 }
364 #endif
365
366 #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
367 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
pmdp_test_and_clear_young(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)368 static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
369 unsigned long address,
370 pmd_t *pmdp)
371 {
372 pmd_t pmd = *pmdp;
373 int r = 1;
374 if (!pmd_young(pmd))
375 r = 0;
376 else
377 set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
378 return r;
379 }
380 #else
pmdp_test_and_clear_young(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)381 static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
382 unsigned long address,
383 pmd_t *pmdp)
384 {
385 BUILD_BUG();
386 return 0;
387 }
388 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
389 #endif
390
391 #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
392 int ptep_clear_flush_young(struct vm_area_struct *vma,
393 unsigned long address, pte_t *ptep);
394 #endif
395
396 #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
397 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
398 extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
399 unsigned long address, pmd_t *pmdp);
400 #else
401 /*
402 * Despite relevant to THP only, this API is called from generic rmap code
403 * under PageTransHuge(), hence needs a dummy implementation for !THP
404 */
pmdp_clear_flush_young(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)405 static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
406 unsigned long address, pmd_t *pmdp)
407 {
408 BUILD_BUG();
409 return 0;
410 }
411 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
412 #endif
413
414 #ifndef arch_has_hw_nonleaf_pmd_young
415 /*
416 * Return whether the accessed bit in non-leaf PMD entries is supported on the
417 * local CPU.
418 */
arch_has_hw_nonleaf_pmd_young(void)419 static inline bool arch_has_hw_nonleaf_pmd_young(void)
420 {
421 return IS_ENABLED(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG);
422 }
423 #endif
424
425 #ifndef arch_has_hw_pte_young
426 /*
427 * Return whether the accessed bit is supported on the local CPU.
428 *
429 * This stub assumes accessing through an old PTE triggers a page fault.
430 * Architectures that automatically set the access bit should overwrite it.
431 */
arch_has_hw_pte_young(void)432 static inline bool arch_has_hw_pte_young(void)
433 {
434 return IS_ENABLED(CONFIG_ARCH_HAS_HW_PTE_YOUNG);
435 }
436 #endif
437
438 #ifndef arch_check_zapped_pte
arch_check_zapped_pte(struct vm_area_struct * vma,pte_t pte)439 static inline void arch_check_zapped_pte(struct vm_area_struct *vma,
440 pte_t pte)
441 {
442 }
443 #endif
444
445 #ifndef arch_check_zapped_pmd
arch_check_zapped_pmd(struct vm_area_struct * vma,pmd_t pmd)446 static inline void arch_check_zapped_pmd(struct vm_area_struct *vma,
447 pmd_t pmd)
448 {
449 }
450 #endif
451
452 #ifndef arch_check_zapped_pud
arch_check_zapped_pud(struct vm_area_struct * vma,pud_t pud)453 static inline void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud)
454 {
455 }
456 #endif
457
458 #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
ptep_get_and_clear(struct mm_struct * mm,unsigned long address,pte_t * ptep)459 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
460 unsigned long address,
461 pte_t *ptep)
462 {
463 pte_t pte = ptep_get(ptep);
464 pte_clear(mm, address, ptep);
465 page_table_check_pte_clear(mm, pte);
466 return pte;
467 }
468 #endif
469
470 #ifndef clear_young_dirty_ptes
471 /**
472 * clear_young_dirty_ptes - Mark PTEs that map consecutive pages of the
473 * same folio as old/clean.
474 * @mm: Address space the pages are mapped into.
475 * @addr: Address the first page is mapped at.
476 * @ptep: Page table pointer for the first entry.
477 * @nr: Number of entries to mark old/clean.
478 * @flags: Flags to modify the PTE batch semantics.
479 *
480 * May be overridden by the architecture; otherwise, implemented by
481 * get_and_clear/modify/set for each pte in the range.
482 *
483 * Note that PTE bits in the PTE range besides the PFN can differ. For example,
484 * some PTEs might be write-protected.
485 *
486 * Context: The caller holds the page table lock. The PTEs map consecutive
487 * pages that belong to the same folio. The PTEs are all in the same PMD.
488 */
clear_young_dirty_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr,cydp_t flags)489 static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,
490 unsigned long addr, pte_t *ptep,
491 unsigned int nr, cydp_t flags)
492 {
493 pte_t pte;
494
495 for (;;) {
496 if (flags == CYDP_CLEAR_YOUNG)
497 ptep_test_and_clear_young(vma, addr, ptep);
498 else {
499 pte = ptep_get_and_clear(vma->vm_mm, addr, ptep);
500 if (flags & CYDP_CLEAR_YOUNG)
501 pte = pte_mkold(pte);
502 if (flags & CYDP_CLEAR_DIRTY)
503 pte = pte_mkclean(pte);
504 set_pte_at(vma->vm_mm, addr, ptep, pte);
505 }
506 if (--nr == 0)
507 break;
508 ptep++;
509 addr += PAGE_SIZE;
510 }
511 }
512 #endif
513
ptep_clear(struct mm_struct * mm,unsigned long addr,pte_t * ptep)514 static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
515 pte_t *ptep)
516 {
517 ptep_get_and_clear(mm, addr, ptep);
518 }
519
520 #ifdef CONFIG_GUP_GET_PXX_LOW_HIGH
521 /*
522 * For walking the pagetables without holding any locks. Some architectures
523 * (eg x86-32 PAE) cannot load the entries atomically without using expensive
524 * instructions. We are guaranteed that a PTE will only either go from not
525 * present to present, or present to not present -- it will not switch to a
526 * completely different present page without a TLB flush inbetween; which we
527 * are blocking by holding interrupts off.
528 *
529 * Setting ptes from not present to present goes:
530 *
531 * ptep->pte_high = h;
532 * smp_wmb();
533 * ptep->pte_low = l;
534 *
535 * And present to not present goes:
536 *
537 * ptep->pte_low = 0;
538 * smp_wmb();
539 * ptep->pte_high = 0;
540 *
541 * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'.
542 * We load pte_high *after* loading pte_low, which ensures we don't see an older
543 * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't
544 * picked up a changed pte high. We might have gotten rubbish values from
545 * pte_low and pte_high, but we are guaranteed that pte_low will not have the
546 * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
547 * operates on present ptes we're safe.
548 */
ptep_get_lockless(pte_t * ptep)549 static inline pte_t ptep_get_lockless(pte_t *ptep)
550 {
551 pte_t pte;
552
553 do {
554 pte.pte_low = ptep->pte_low;
555 smp_rmb();
556 pte.pte_high = ptep->pte_high;
557 smp_rmb();
558 } while (unlikely(pte.pte_low != ptep->pte_low));
559
560 return pte;
561 }
562 #define ptep_get_lockless ptep_get_lockless
563
564 #if CONFIG_PGTABLE_LEVELS > 2
pmdp_get_lockless(pmd_t * pmdp)565 static inline pmd_t pmdp_get_lockless(pmd_t *pmdp)
566 {
567 pmd_t pmd;
568
569 do {
570 pmd.pmd_low = pmdp->pmd_low;
571 smp_rmb();
572 pmd.pmd_high = pmdp->pmd_high;
573 smp_rmb();
574 } while (unlikely(pmd.pmd_low != pmdp->pmd_low));
575
576 return pmd;
577 }
578 #define pmdp_get_lockless pmdp_get_lockless
579 #define pmdp_get_lockless_sync() tlb_remove_table_sync_one()
580 #endif /* CONFIG_PGTABLE_LEVELS > 2 */
581 #endif /* CONFIG_GUP_GET_PXX_LOW_HIGH */
582
583 /*
584 * We require that the PTE can be read atomically.
585 */
586 #ifndef ptep_get_lockless
ptep_get_lockless(pte_t * ptep)587 static inline pte_t ptep_get_lockless(pte_t *ptep)
588 {
589 return ptep_get(ptep);
590 }
591 #endif
592
593 #ifndef pmdp_get_lockless
pmdp_get_lockless(pmd_t * pmdp)594 static inline pmd_t pmdp_get_lockless(pmd_t *pmdp)
595 {
596 return pmdp_get(pmdp);
597 }
pmdp_get_lockless_sync(void)598 static inline void pmdp_get_lockless_sync(void)
599 {
600 }
601 #endif
602
603 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
604 #ifndef __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
pmdp_huge_get_and_clear(struct mm_struct * mm,unsigned long address,pmd_t * pmdp)605 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
606 unsigned long address,
607 pmd_t *pmdp)
608 {
609 pmd_t pmd = *pmdp;
610
611 pmd_clear(pmdp);
612 page_table_check_pmd_clear(mm, pmd);
613
614 return pmd;
615 }
616 #endif /* __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR */
617 #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
pudp_huge_get_and_clear(struct mm_struct * mm,unsigned long address,pud_t * pudp)618 static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
619 unsigned long address,
620 pud_t *pudp)
621 {
622 pud_t pud = *pudp;
623
624 pud_clear(pudp);
625 page_table_check_pud_clear(mm, pud);
626
627 return pud;
628 }
629 #endif /* __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR */
630 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
631
632 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
633 #ifndef __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR_FULL
pmdp_huge_get_and_clear_full(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp,int full)634 static inline pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma,
635 unsigned long address, pmd_t *pmdp,
636 int full)
637 {
638 return pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
639 }
640 #endif
641
642 #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR_FULL
pudp_huge_get_and_clear_full(struct vm_area_struct * vma,unsigned long address,pud_t * pudp,int full)643 static inline pud_t pudp_huge_get_and_clear_full(struct vm_area_struct *vma,
644 unsigned long address, pud_t *pudp,
645 int full)
646 {
647 return pudp_huge_get_and_clear(vma->vm_mm, address, pudp);
648 }
649 #endif
650 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
651
652 #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
ptep_get_and_clear_full(struct mm_struct * mm,unsigned long address,pte_t * ptep,int full)653 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
654 unsigned long address, pte_t *ptep,
655 int full)
656 {
657 return ptep_get_and_clear(mm, address, ptep);
658 }
659 #endif
660
661 #ifndef get_and_clear_full_ptes
662 /**
663 * get_and_clear_full_ptes - Clear present PTEs that map consecutive pages of
664 * the same folio, collecting dirty/accessed bits.
665 * @mm: Address space the pages are mapped into.
666 * @addr: Address the first page is mapped at.
667 * @ptep: Page table pointer for the first entry.
668 * @nr: Number of entries to clear.
669 * @full: Whether we are clearing a full mm.
670 *
671 * May be overridden by the architecture; otherwise, implemented as a simple
672 * loop over ptep_get_and_clear_full(), merging dirty/accessed bits into the
673 * returned PTE.
674 *
675 * Note that PTE bits in the PTE range besides the PFN can differ. For example,
676 * some PTEs might be write-protected.
677 *
678 * Context: The caller holds the page table lock. The PTEs map consecutive
679 * pages that belong to the same folio. The PTEs are all in the same PMD.
680 */
get_and_clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)681 static inline pte_t get_and_clear_full_ptes(struct mm_struct *mm,
682 unsigned long addr, pte_t *ptep, unsigned int nr, int full)
683 {
684 pte_t pte, tmp_pte;
685
686 pte = ptep_get_and_clear_full(mm, addr, ptep, full);
687 while (--nr) {
688 ptep++;
689 addr += PAGE_SIZE;
690 tmp_pte = ptep_get_and_clear_full(mm, addr, ptep, full);
691 if (pte_dirty(tmp_pte))
692 pte = pte_mkdirty(pte);
693 if (pte_young(tmp_pte))
694 pte = pte_mkyoung(pte);
695 }
696 return pte;
697 }
698 #endif
699
700 #ifndef clear_full_ptes
701 /**
702 * clear_full_ptes - Clear present PTEs that map consecutive pages of the same
703 * folio.
704 * @mm: Address space the pages are mapped into.
705 * @addr: Address the first page is mapped at.
706 * @ptep: Page table pointer for the first entry.
707 * @nr: Number of entries to clear.
708 * @full: Whether we are clearing a full mm.
709 *
710 * May be overridden by the architecture; otherwise, implemented as a simple
711 * loop over ptep_get_and_clear_full().
712 *
713 * Note that PTE bits in the PTE range besides the PFN can differ. For example,
714 * some PTEs might be write-protected.
715 *
716 * Context: The caller holds the page table lock. The PTEs map consecutive
717 * pages that belong to the same folio. The PTEs are all in the same PMD.
718 */
clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)719 static inline void clear_full_ptes(struct mm_struct *mm, unsigned long addr,
720 pte_t *ptep, unsigned int nr, int full)
721 {
722 for (;;) {
723 ptep_get_and_clear_full(mm, addr, ptep, full);
724 if (--nr == 0)
725 break;
726 ptep++;
727 addr += PAGE_SIZE;
728 }
729 }
730 #endif
731
732 /*
733 * If two threads concurrently fault at the same page, the thread that
734 * won the race updates the PTE and its local TLB/Cache. The other thread
735 * gives up, simply does nothing, and continues; on architectures where
736 * software can update TLB, local TLB can be updated here to avoid next page
737 * fault. This function updates TLB only, do nothing with cache or others.
738 * It is the difference with function update_mmu_cache.
739 */
740 #ifndef update_mmu_tlb_range
update_mmu_tlb_range(struct vm_area_struct * vma,unsigned long address,pte_t * ptep,unsigned int nr)741 static inline void update_mmu_tlb_range(struct vm_area_struct *vma,
742 unsigned long address, pte_t *ptep, unsigned int nr)
743 {
744 }
745 #endif
746
update_mmu_tlb(struct vm_area_struct * vma,unsigned long address,pte_t * ptep)747 static inline void update_mmu_tlb(struct vm_area_struct *vma,
748 unsigned long address, pte_t *ptep)
749 {
750 update_mmu_tlb_range(vma, address, ptep, 1);
751 }
752
753 /*
754 * Some architectures may be able to avoid expensive synchronization
755 * primitives when modifications are made to PTE's which are already
756 * not present, or in the process of an address space destruction.
757 */
758 #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
pte_clear_not_present_full(struct mm_struct * mm,unsigned long address,pte_t * ptep,int full)759 static inline void pte_clear_not_present_full(struct mm_struct *mm,
760 unsigned long address,
761 pte_t *ptep,
762 int full)
763 {
764 pte_clear(mm, address, ptep);
765 }
766 #endif
767
768 #ifndef clear_not_present_full_ptes
769 /**
770 * clear_not_present_full_ptes - Clear multiple not present PTEs which are
771 * consecutive in the pgtable.
772 * @mm: Address space the ptes represent.
773 * @addr: Address of the first pte.
774 * @ptep: Page table pointer for the first entry.
775 * @nr: Number of entries to clear.
776 * @full: Whether we are clearing a full mm.
777 *
778 * May be overridden by the architecture; otherwise, implemented as a simple
779 * loop over pte_clear_not_present_full().
780 *
781 * Context: The caller holds the page table lock. The PTEs are all not present.
782 * The PTEs are all in the same PMD.
783 */
clear_not_present_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)784 static inline void clear_not_present_full_ptes(struct mm_struct *mm,
785 unsigned long addr, pte_t *ptep, unsigned int nr, int full)
786 {
787 for (;;) {
788 pte_clear_not_present_full(mm, addr, ptep, full);
789 if (--nr == 0)
790 break;
791 ptep++;
792 addr += PAGE_SIZE;
793 }
794 }
795 #endif
796
797 #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
798 extern pte_t ptep_clear_flush(struct vm_area_struct *vma,
799 unsigned long address,
800 pte_t *ptep);
801 #endif
802
803 #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
804 extern pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma,
805 unsigned long address,
806 pmd_t *pmdp);
807 extern pud_t pudp_huge_clear_flush(struct vm_area_struct *vma,
808 unsigned long address,
809 pud_t *pudp);
810 #endif
811
812 #ifndef pte_mkwrite
pte_mkwrite(pte_t pte,struct vm_area_struct * vma)813 static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma)
814 {
815 return pte_mkwrite_novma(pte);
816 }
817 #endif
818
819 #if defined(CONFIG_ARCH_WANT_PMD_MKWRITE) && !defined(pmd_mkwrite)
pmd_mkwrite(pmd_t pmd,struct vm_area_struct * vma)820 static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
821 {
822 return pmd_mkwrite_novma(pmd);
823 }
824 #endif
825
826 #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
827 struct mm_struct;
ptep_set_wrprotect(struct mm_struct * mm,unsigned long address,pte_t * ptep)828 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
829 {
830 pte_t old_pte = ptep_get(ptep);
831 set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
832 }
833 #endif
834
835 #ifndef wrprotect_ptes
836 /**
837 * wrprotect_ptes - Write-protect PTEs that map consecutive pages of the same
838 * folio.
839 * @mm: Address space the pages are mapped into.
840 * @addr: Address the first page is mapped at.
841 * @ptep: Page table pointer for the first entry.
842 * @nr: Number of entries to write-protect.
843 *
844 * May be overridden by the architecture; otherwise, implemented as a simple
845 * loop over ptep_set_wrprotect().
846 *
847 * Note that PTE bits in the PTE range besides the PFN can differ. For example,
848 * some PTEs might be write-protected.
849 *
850 * Context: The caller holds the page table lock. The PTEs map consecutive
851 * pages that belong to the same folio. The PTEs are all in the same PMD.
852 */
wrprotect_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr)853 static inline void wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
854 pte_t *ptep, unsigned int nr)
855 {
856 for (;;) {
857 ptep_set_wrprotect(mm, addr, ptep);
858 if (--nr == 0)
859 break;
860 ptep++;
861 addr += PAGE_SIZE;
862 }
863 }
864 #endif
865
866 /*
867 * On some architectures hardware does not set page access bit when accessing
868 * memory page, it is responsibility of software setting this bit. It brings
869 * out extra page fault penalty to track page access bit. For optimization page
870 * access bit can be set during all page fault flow on these arches.
871 * To be differentiate with macro pte_mkyoung, this macro is used on platforms
872 * where software maintains page access bit.
873 */
874 #ifndef pte_sw_mkyoung
pte_sw_mkyoung(pte_t pte)875 static inline pte_t pte_sw_mkyoung(pte_t pte)
876 {
877 return pte;
878 }
879 #define pte_sw_mkyoung pte_sw_mkyoung
880 #endif
881
882 #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT
883 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmdp_set_wrprotect(struct mm_struct * mm,unsigned long address,pmd_t * pmdp)884 static inline void pmdp_set_wrprotect(struct mm_struct *mm,
885 unsigned long address, pmd_t *pmdp)
886 {
887 pmd_t old_pmd = *pmdp;
888 set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
889 }
890 #else
pmdp_set_wrprotect(struct mm_struct * mm,unsigned long address,pmd_t * pmdp)891 static inline void pmdp_set_wrprotect(struct mm_struct *mm,
892 unsigned long address, pmd_t *pmdp)
893 {
894 BUILD_BUG();
895 }
896 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
897 #endif
898 #ifndef __HAVE_ARCH_PUDP_SET_WRPROTECT
899 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
900 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pudp_set_wrprotect(struct mm_struct * mm,unsigned long address,pud_t * pudp)901 static inline void pudp_set_wrprotect(struct mm_struct *mm,
902 unsigned long address, pud_t *pudp)
903 {
904 pud_t old_pud = *pudp;
905
906 set_pud_at(mm, address, pudp, pud_wrprotect(old_pud));
907 }
908 #else
pudp_set_wrprotect(struct mm_struct * mm,unsigned long address,pud_t * pudp)909 static inline void pudp_set_wrprotect(struct mm_struct *mm,
910 unsigned long address, pud_t *pudp)
911 {
912 BUILD_BUG();
913 }
914 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
915 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
916 #endif
917
918 #ifndef pmdp_collapse_flush
919 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
920 extern pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
921 unsigned long address, pmd_t *pmdp);
922 #else
pmdp_collapse_flush(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)923 static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
924 unsigned long address,
925 pmd_t *pmdp)
926 {
927 BUILD_BUG();
928 return *pmdp;
929 }
930 #define pmdp_collapse_flush pmdp_collapse_flush
931 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
932 #endif
933
934 #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT
935 extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
936 pgtable_t pgtable);
937 #endif
938
939 #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW
940 extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
941 #endif
942
943 #ifndef arch_needs_pgtable_deposit
944 #define arch_needs_pgtable_deposit() (false)
945 #endif
946
947 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
948 /*
949 * This is an implementation of pmdp_establish() that is only suitable for an
950 * architecture that doesn't have hardware dirty/accessed bits. In this case we
951 * can't race with CPU which sets these bits and non-atomic approach is fine.
952 */
generic_pmdp_establish(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp,pmd_t pmd)953 static inline pmd_t generic_pmdp_establish(struct vm_area_struct *vma,
954 unsigned long address, pmd_t *pmdp, pmd_t pmd)
955 {
956 pmd_t old_pmd = *pmdp;
957 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
958 return old_pmd;
959 }
960 #endif
961
962 #ifndef __HAVE_ARCH_PMDP_INVALIDATE
963 extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
964 pmd_t *pmdp);
965 #endif
966
967 #ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD
968
969 /*
970 * pmdp_invalidate_ad() invalidates the PMD while changing a transparent
971 * hugepage mapping in the page tables. This function is similar to
972 * pmdp_invalidate(), but should only be used if the access and dirty bits would
973 * not be cleared by the software in the new PMD value. The function ensures
974 * that hardware changes of the access and dirty bits updates would not be lost.
975 *
976 * Doing so can allow in certain architectures to avoid a TLB flush in most
977 * cases. Yet, another TLB flush might be necessary later if the PMD update
978 * itself requires such flush (e.g., if protection was set to be stricter). Yet,
979 * even when a TLB flush is needed because of the update, the caller may be able
980 * to batch these TLB flushing operations, so fewer TLB flush operations are
981 * needed.
982 */
983 extern pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma,
984 unsigned long address, pmd_t *pmdp);
985 #endif
986
987 #ifndef __HAVE_ARCH_PTE_SAME
pte_same(pte_t pte_a,pte_t pte_b)988 static inline int pte_same(pte_t pte_a, pte_t pte_b)
989 {
990 return pte_val(pte_a) == pte_val(pte_b);
991 }
992 #endif
993
994 #ifndef __HAVE_ARCH_PTE_UNUSED
995 /*
996 * Some architectures provide facilities to virtualization guests
997 * so that they can flag allocated pages as unused. This allows the
998 * host to transparently reclaim unused pages. This function returns
999 * whether the pte's page is unused.
1000 */
pte_unused(pte_t pte)1001 static inline int pte_unused(pte_t pte)
1002 {
1003 return 0;
1004 }
1005 #endif
1006
1007 #ifndef pte_access_permitted
1008 #define pte_access_permitted(pte, write) \
1009 (pte_present(pte) && (!(write) || pte_write(pte)))
1010 #endif
1011
1012 #ifndef pmd_access_permitted
1013 #define pmd_access_permitted(pmd, write) \
1014 (pmd_present(pmd) && (!(write) || pmd_write(pmd)))
1015 #endif
1016
1017 #ifndef pud_access_permitted
1018 #define pud_access_permitted(pud, write) \
1019 (pud_present(pud) && (!(write) || pud_write(pud)))
1020 #endif
1021
1022 #ifndef p4d_access_permitted
1023 #define p4d_access_permitted(p4d, write) \
1024 (p4d_present(p4d) && (!(write) || p4d_write(p4d)))
1025 #endif
1026
1027 #ifndef pgd_access_permitted
1028 #define pgd_access_permitted(pgd, write) \
1029 (pgd_present(pgd) && (!(write) || pgd_write(pgd)))
1030 #endif
1031
1032 #ifndef __HAVE_ARCH_PMD_SAME
pmd_same(pmd_t pmd_a,pmd_t pmd_b)1033 static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
1034 {
1035 return pmd_val(pmd_a) == pmd_val(pmd_b);
1036 }
1037 #endif
1038
1039 #ifndef pud_same
pud_same(pud_t pud_a,pud_t pud_b)1040 static inline int pud_same(pud_t pud_a, pud_t pud_b)
1041 {
1042 return pud_val(pud_a) == pud_val(pud_b);
1043 }
1044 #define pud_same pud_same
1045 #endif
1046
1047 #ifndef __HAVE_ARCH_P4D_SAME
p4d_same(p4d_t p4d_a,p4d_t p4d_b)1048 static inline int p4d_same(p4d_t p4d_a, p4d_t p4d_b)
1049 {
1050 return p4d_val(p4d_a) == p4d_val(p4d_b);
1051 }
1052 #endif
1053
1054 #ifndef __HAVE_ARCH_PGD_SAME
pgd_same(pgd_t pgd_a,pgd_t pgd_b)1055 static inline int pgd_same(pgd_t pgd_a, pgd_t pgd_b)
1056 {
1057 return pgd_val(pgd_a) == pgd_val(pgd_b);
1058 }
1059 #endif
1060
1061 /*
1062 * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
1063 * TLB flush will be required as a result of the "set". For example, use
1064 * in scenarios where it is known ahead of time that the routine is
1065 * setting non-present entries, or re-setting an existing entry to the
1066 * same value. Otherwise, use the typical "set" helpers and flush the
1067 * TLB.
1068 */
1069 #define set_pte_safe(ptep, pte) \
1070 ({ \
1071 WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
1072 set_pte(ptep, pte); \
1073 })
1074
1075 #define set_pmd_safe(pmdp, pmd) \
1076 ({ \
1077 WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
1078 set_pmd(pmdp, pmd); \
1079 })
1080
1081 #define set_pud_safe(pudp, pud) \
1082 ({ \
1083 WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
1084 set_pud(pudp, pud); \
1085 })
1086
1087 #define set_p4d_safe(p4dp, p4d) \
1088 ({ \
1089 WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
1090 set_p4d(p4dp, p4d); \
1091 })
1092
1093 #define set_pgd_safe(pgdp, pgd) \
1094 ({ \
1095 WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
1096 set_pgd(pgdp, pgd); \
1097 })
1098
1099 #ifndef __HAVE_ARCH_DO_SWAP_PAGE
arch_do_swap_page_nr(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,pte_t pte,pte_t oldpte,int nr)1100 static inline void arch_do_swap_page_nr(struct mm_struct *mm,
1101 struct vm_area_struct *vma,
1102 unsigned long addr,
1103 pte_t pte, pte_t oldpte,
1104 int nr)
1105 {
1106
1107 }
1108 #else
1109 /*
1110 * Some architectures support metadata associated with a page. When a
1111 * page is being swapped out, this metadata must be saved so it can be
1112 * restored when the page is swapped back in. SPARC M7 and newer
1113 * processors support an ADI (Application Data Integrity) tag for the
1114 * page as metadata for the page. arch_do_swap_page() can restore this
1115 * metadata when a page is swapped back in.
1116 */
arch_do_swap_page_nr(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,pte_t pte,pte_t oldpte,int nr)1117 static inline void arch_do_swap_page_nr(struct mm_struct *mm,
1118 struct vm_area_struct *vma,
1119 unsigned long addr,
1120 pte_t pte, pte_t oldpte,
1121 int nr)
1122 {
1123 for (int i = 0; i < nr; i++) {
1124 arch_do_swap_page(vma->vm_mm, vma, addr + i * PAGE_SIZE,
1125 pte_advance_pfn(pte, i),
1126 pte_advance_pfn(oldpte, i));
1127 }
1128 }
1129 #endif
1130
1131 #ifndef __HAVE_ARCH_UNMAP_ONE
1132 /*
1133 * Some architectures support metadata associated with a page. When a
1134 * page is being swapped out, this metadata must be saved so it can be
1135 * restored when the page is swapped back in. SPARC M7 and newer
1136 * processors support an ADI (Application Data Integrity) tag for the
1137 * page as metadata for the page. arch_unmap_one() can save this
1138 * metadata on a swap-out of a page.
1139 */
arch_unmap_one(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,pte_t orig_pte)1140 static inline int arch_unmap_one(struct mm_struct *mm,
1141 struct vm_area_struct *vma,
1142 unsigned long addr,
1143 pte_t orig_pte)
1144 {
1145 return 0;
1146 }
1147 #endif
1148
1149 /*
1150 * Allow architectures to preserve additional metadata associated with
1151 * swapped-out pages. The corresponding __HAVE_ARCH_SWAP_* macros and function
1152 * prototypes must be defined in the arch-specific asm/pgtable.h file.
1153 */
1154 #ifndef __HAVE_ARCH_PREPARE_TO_SWAP
arch_prepare_to_swap(struct folio * folio)1155 static inline int arch_prepare_to_swap(struct folio *folio)
1156 {
1157 return 0;
1158 }
1159 #endif
1160
1161 #ifndef __HAVE_ARCH_SWAP_INVALIDATE
arch_swap_invalidate_page(int type,pgoff_t offset)1162 static inline void arch_swap_invalidate_page(int type, pgoff_t offset)
1163 {
1164 }
1165
arch_swap_invalidate_area(int type)1166 static inline void arch_swap_invalidate_area(int type)
1167 {
1168 }
1169 #endif
1170
1171 #ifndef __HAVE_ARCH_SWAP_RESTORE
arch_swap_restore(swp_entry_t entry,struct folio * folio)1172 static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio)
1173 {
1174 }
1175 #endif
1176
1177 #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
1178 #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
1179 #endif
1180
1181 #ifndef __HAVE_ARCH_MOVE_PTE
1182 #define move_pte(pte, old_addr, new_addr) (pte)
1183 #endif
1184
1185 #ifndef pte_accessible
1186 # define pte_accessible(mm, pte) ((void)(pte), 1)
1187 #endif
1188
1189 #ifndef flush_tlb_fix_spurious_fault
1190 #define flush_tlb_fix_spurious_fault(vma, address, ptep) flush_tlb_page(vma, address)
1191 #endif
1192
1193 /*
1194 * When walking page tables, get the address of the next boundary,
1195 * or the end address of the range if that comes earlier. Although no
1196 * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
1197 */
1198
1199 #define pgd_addr_end(addr, end) \
1200 ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
1201 (__boundary - 1 < (end) - 1)? __boundary: (end); \
1202 })
1203
1204 #ifndef p4d_addr_end
1205 #define p4d_addr_end(addr, end) \
1206 ({ unsigned long __boundary = ((addr) + P4D_SIZE) & P4D_MASK; \
1207 (__boundary - 1 < (end) - 1)? __boundary: (end); \
1208 })
1209 #endif
1210
1211 #ifndef pud_addr_end
1212 #define pud_addr_end(addr, end) \
1213 ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
1214 (__boundary - 1 < (end) - 1)? __boundary: (end); \
1215 })
1216 #endif
1217
1218 #ifndef pmd_addr_end
1219 #define pmd_addr_end(addr, end) \
1220 ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
1221 (__boundary - 1 < (end) - 1)? __boundary: (end); \
1222 })
1223 #endif
1224
1225 /*
1226 * When walking page tables, we usually want to skip any p?d_none entries;
1227 * and any p?d_bad entries - reporting the error before resetting to none.
1228 * Do the tests inline, but report and clear the bad entry in mm/memory.c.
1229 */
1230 void pgd_clear_bad(pgd_t *);
1231
1232 #ifndef __PAGETABLE_P4D_FOLDED
1233 void p4d_clear_bad(p4d_t *);
1234 #else
1235 #define p4d_clear_bad(p4d) do { } while (0)
1236 #endif
1237
1238 #ifndef __PAGETABLE_PUD_FOLDED
1239 void pud_clear_bad(pud_t *);
1240 #else
1241 #define pud_clear_bad(p4d) do { } while (0)
1242 #endif
1243
1244 void pmd_clear_bad(pmd_t *);
1245
pgd_none_or_clear_bad(pgd_t * pgd)1246 static inline int pgd_none_or_clear_bad(pgd_t *pgd)
1247 {
1248 if (pgd_none(*pgd))
1249 return 1;
1250 if (unlikely(pgd_bad(*pgd))) {
1251 pgd_clear_bad(pgd);
1252 return 1;
1253 }
1254 return 0;
1255 }
1256
p4d_none_or_clear_bad(p4d_t * p4d)1257 static inline int p4d_none_or_clear_bad(p4d_t *p4d)
1258 {
1259 if (p4d_none(*p4d))
1260 return 1;
1261 if (unlikely(p4d_bad(*p4d))) {
1262 p4d_clear_bad(p4d);
1263 return 1;
1264 }
1265 return 0;
1266 }
1267
pud_none_or_clear_bad(pud_t * pud)1268 static inline int pud_none_or_clear_bad(pud_t *pud)
1269 {
1270 if (pud_none(*pud))
1271 return 1;
1272 if (unlikely(pud_bad(*pud))) {
1273 pud_clear_bad(pud);
1274 return 1;
1275 }
1276 return 0;
1277 }
1278
pmd_none_or_clear_bad(pmd_t * pmd)1279 static inline int pmd_none_or_clear_bad(pmd_t *pmd)
1280 {
1281 if (pmd_none(*pmd))
1282 return 1;
1283 if (unlikely(pmd_bad(*pmd))) {
1284 pmd_clear_bad(pmd);
1285 return 1;
1286 }
1287 return 0;
1288 }
1289
__ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)1290 static inline pte_t __ptep_modify_prot_start(struct vm_area_struct *vma,
1291 unsigned long addr,
1292 pte_t *ptep)
1293 {
1294 /*
1295 * Get the current pte state, but zero it out to make it
1296 * non-present, preventing the hardware from asynchronously
1297 * updating it.
1298 */
1299 return ptep_get_and_clear(vma->vm_mm, addr, ptep);
1300 }
1301
__ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t pte)1302 static inline void __ptep_modify_prot_commit(struct vm_area_struct *vma,
1303 unsigned long addr,
1304 pte_t *ptep, pte_t pte)
1305 {
1306 /*
1307 * The pte is non-present, so there's no hardware state to
1308 * preserve.
1309 */
1310 set_pte_at(vma->vm_mm, addr, ptep, pte);
1311 }
1312
1313 #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
1314 /*
1315 * Start a pte protection read-modify-write transaction, which
1316 * protects against asynchronous hardware modifications to the pte.
1317 * The intention is not to prevent the hardware from making pte
1318 * updates, but to prevent any updates it may make from being lost.
1319 *
1320 * This does not protect against other software modifications of the
1321 * pte; the appropriate pte lock must be held over the transaction.
1322 *
1323 * Note that this interface is intended to be batchable, meaning that
1324 * ptep_modify_prot_commit may not actually update the pte, but merely
1325 * queue the update to be done at some later time. The update must be
1326 * actually committed before the pte lock is released, however.
1327 */
ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)1328 static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
1329 unsigned long addr,
1330 pte_t *ptep)
1331 {
1332 return __ptep_modify_prot_start(vma, addr, ptep);
1333 }
1334
1335 /*
1336 * Commit an update to a pte, leaving any hardware-controlled bits in
1337 * the PTE unmodified.
1338 */
ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t old_pte,pte_t pte)1339 static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
1340 unsigned long addr,
1341 pte_t *ptep, pte_t old_pte, pte_t pte)
1342 {
1343 __ptep_modify_prot_commit(vma, addr, ptep, pte);
1344 }
1345 #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
1346 #endif /* CONFIG_MMU */
1347
1348 /*
1349 * No-op macros that just return the current protection value. Defined here
1350 * because these macros can be used even if CONFIG_MMU is not defined.
1351 */
1352
1353 #ifndef pgprot_nx
1354 #define pgprot_nx(prot) (prot)
1355 #endif
1356
1357 #ifndef pgprot_noncached
1358 #define pgprot_noncached(prot) (prot)
1359 #endif
1360
1361 #ifndef pgprot_writecombine
1362 #define pgprot_writecombine pgprot_noncached
1363 #endif
1364
1365 #ifndef pgprot_writethrough
1366 #define pgprot_writethrough pgprot_noncached
1367 #endif
1368
1369 #ifndef pgprot_device
1370 #define pgprot_device pgprot_noncached
1371 #endif
1372
1373 #ifndef pgprot_mhp
1374 #define pgprot_mhp(prot) (prot)
1375 #endif
1376
1377 #ifdef CONFIG_MMU
1378 #ifndef pgprot_modify
1379 #define pgprot_modify pgprot_modify
pgprot_modify(pgprot_t oldprot,pgprot_t newprot)1380 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
1381 {
1382 if (pgprot_val(oldprot) == pgprot_val(pgprot_noncached(oldprot)))
1383 newprot = pgprot_noncached(newprot);
1384 if (pgprot_val(oldprot) == pgprot_val(pgprot_writecombine(oldprot)))
1385 newprot = pgprot_writecombine(newprot);
1386 if (pgprot_val(oldprot) == pgprot_val(pgprot_device(oldprot)))
1387 newprot = pgprot_device(newprot);
1388 return newprot;
1389 }
1390 #endif
1391 #endif /* CONFIG_MMU */
1392
1393 #ifndef pgprot_encrypted
1394 #define pgprot_encrypted(prot) (prot)
1395 #endif
1396
1397 #ifndef pgprot_decrypted
1398 #define pgprot_decrypted(prot) (prot)
1399 #endif
1400
1401 /*
1402 * A facility to provide batching of the reload of page tables and
1403 * other process state with the actual context switch code for
1404 * paravirtualized guests. By convention, only one of the batched
1405 * update (lazy) modes (CPU, MMU) should be active at any given time,
1406 * entry should never be nested, and entry and exits should always be
1407 * paired. This is for sanity of maintaining and reasoning about the
1408 * kernel code. In this case, the exit (end of the context switch) is
1409 * in architecture-specific code, and so doesn't need a generic
1410 * definition.
1411 */
1412 #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
1413 #define arch_start_context_switch(prev) do {} while (0)
1414 #endif
1415
1416 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
1417 #ifndef CONFIG_ARCH_ENABLE_THP_MIGRATION
pmd_swp_mksoft_dirty(pmd_t pmd)1418 static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
1419 {
1420 return pmd;
1421 }
1422
pmd_swp_soft_dirty(pmd_t pmd)1423 static inline int pmd_swp_soft_dirty(pmd_t pmd)
1424 {
1425 return 0;
1426 }
1427
pmd_swp_clear_soft_dirty(pmd_t pmd)1428 static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
1429 {
1430 return pmd;
1431 }
1432 #endif
1433 #else /* !CONFIG_HAVE_ARCH_SOFT_DIRTY */
pte_soft_dirty(pte_t pte)1434 static inline int pte_soft_dirty(pte_t pte)
1435 {
1436 return 0;
1437 }
1438
pmd_soft_dirty(pmd_t pmd)1439 static inline int pmd_soft_dirty(pmd_t pmd)
1440 {
1441 return 0;
1442 }
1443
pte_mksoft_dirty(pte_t pte)1444 static inline pte_t pte_mksoft_dirty(pte_t pte)
1445 {
1446 return pte;
1447 }
1448
pmd_mksoft_dirty(pmd_t pmd)1449 static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
1450 {
1451 return pmd;
1452 }
1453
pte_clear_soft_dirty(pte_t pte)1454 static inline pte_t pte_clear_soft_dirty(pte_t pte)
1455 {
1456 return pte;
1457 }
1458
pmd_clear_soft_dirty(pmd_t pmd)1459 static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
1460 {
1461 return pmd;
1462 }
1463
pte_swp_mksoft_dirty(pte_t pte)1464 static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
1465 {
1466 return pte;
1467 }
1468
pte_swp_soft_dirty(pte_t pte)1469 static inline int pte_swp_soft_dirty(pte_t pte)
1470 {
1471 return 0;
1472 }
1473
pte_swp_clear_soft_dirty(pte_t pte)1474 static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
1475 {
1476 return pte;
1477 }
1478
pmd_swp_mksoft_dirty(pmd_t pmd)1479 static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
1480 {
1481 return pmd;
1482 }
1483
pmd_swp_soft_dirty(pmd_t pmd)1484 static inline int pmd_swp_soft_dirty(pmd_t pmd)
1485 {
1486 return 0;
1487 }
1488
pmd_swp_clear_soft_dirty(pmd_t pmd)1489 static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
1490 {
1491 return pmd;
1492 }
1493 #endif
1494
1495 #ifndef __HAVE_PFNMAP_TRACKING
1496 /*
1497 * Interfaces that can be used by architecture code to keep track of
1498 * memory type of pfn mappings specified by the remap_pfn_range,
1499 * vmf_insert_pfn.
1500 */
1501
1502 /*
1503 * track_pfn_remap is called when a _new_ pfn mapping is being established
1504 * by remap_pfn_range() for physical range indicated by pfn and size.
1505 */
track_pfn_remap(struct vm_area_struct * vma,pgprot_t * prot,unsigned long pfn,unsigned long addr,unsigned long size)1506 static inline int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
1507 unsigned long pfn, unsigned long addr,
1508 unsigned long size)
1509 {
1510 return 0;
1511 }
1512
1513 /*
1514 * track_pfn_insert is called when a _new_ single pfn is established
1515 * by vmf_insert_pfn().
1516 */
track_pfn_insert(struct vm_area_struct * vma,pgprot_t * prot,pfn_t pfn)1517 static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
1518 pfn_t pfn)
1519 {
1520 }
1521
1522 /*
1523 * track_pfn_copy is called when a VM_PFNMAP VMA is about to get the page
1524 * tables copied during copy_page_range(). On success, stores the pfn to be
1525 * passed to untrack_pfn_copy().
1526 */
track_pfn_copy(struct vm_area_struct * dst_vma,struct vm_area_struct * src_vma,unsigned long * pfn)1527 static inline int track_pfn_copy(struct vm_area_struct *dst_vma,
1528 struct vm_area_struct *src_vma, unsigned long *pfn)
1529 {
1530 return 0;
1531 }
1532
1533 /*
1534 * untrack_pfn_copy is called when a VM_PFNMAP VMA failed to copy during
1535 * copy_page_range(), but after track_pfn_copy() was already called.
1536 */
untrack_pfn_copy(struct vm_area_struct * dst_vma,unsigned long pfn)1537 static inline void untrack_pfn_copy(struct vm_area_struct *dst_vma,
1538 unsigned long pfn)
1539 {
1540 }
1541
1542 /*
1543 * untrack_pfn is called while unmapping a pfnmap for a region.
1544 * untrack can be called for a specific region indicated by pfn and size or
1545 * can be for the entire vma (in which case pfn, size are zero).
1546 */
untrack_pfn(struct vm_area_struct * vma,unsigned long pfn,unsigned long size,bool mm_wr_locked)1547 static inline void untrack_pfn(struct vm_area_struct *vma,
1548 unsigned long pfn, unsigned long size,
1549 bool mm_wr_locked)
1550 {
1551 }
1552
1553 /*
1554 * untrack_pfn_clear is called in the following cases on a VM_PFNMAP VMA:
1555 *
1556 * 1) During mremap() on the src VMA after the page tables were moved.
1557 * 2) During fork() on the dst VMA, immediately after duplicating the src VMA.
1558 */
untrack_pfn_clear(struct vm_area_struct * vma)1559 static inline void untrack_pfn_clear(struct vm_area_struct *vma)
1560 {
1561 }
1562 #else
1563 extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
1564 unsigned long pfn, unsigned long addr,
1565 unsigned long size);
1566 extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
1567 pfn_t pfn);
1568 extern int track_pfn_copy(struct vm_area_struct *dst_vma,
1569 struct vm_area_struct *src_vma, unsigned long *pfn);
1570 extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,
1571 unsigned long pfn);
1572 extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
1573 unsigned long size, bool mm_wr_locked);
1574 extern void untrack_pfn_clear(struct vm_area_struct *vma);
1575 #endif
1576
1577 #ifdef CONFIG_MMU
1578 #ifdef __HAVE_COLOR_ZERO_PAGE
is_zero_pfn(unsigned long pfn)1579 static inline int is_zero_pfn(unsigned long pfn)
1580 {
1581 extern unsigned long zero_pfn;
1582 unsigned long offset_from_zero_pfn = pfn - zero_pfn;
1583 return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
1584 }
1585
1586 #define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
1587
1588 #else
is_zero_pfn(unsigned long pfn)1589 static inline int is_zero_pfn(unsigned long pfn)
1590 {
1591 extern unsigned long zero_pfn;
1592 return pfn == zero_pfn;
1593 }
1594
my_zero_pfn(unsigned long addr)1595 static inline unsigned long my_zero_pfn(unsigned long addr)
1596 {
1597 extern unsigned long zero_pfn;
1598 return zero_pfn;
1599 }
1600 #endif
1601 #else
is_zero_pfn(unsigned long pfn)1602 static inline int is_zero_pfn(unsigned long pfn)
1603 {
1604 return 0;
1605 }
1606
my_zero_pfn(unsigned long addr)1607 static inline unsigned long my_zero_pfn(unsigned long addr)
1608 {
1609 return 0;
1610 }
1611 #endif /* CONFIG_MMU */
1612
1613 #ifdef CONFIG_MMU
1614
1615 #ifndef CONFIG_TRANSPARENT_HUGEPAGE
pmd_trans_huge(pmd_t pmd)1616 static inline int pmd_trans_huge(pmd_t pmd)
1617 {
1618 return 0;
1619 }
1620 #ifndef pmd_write
pmd_write(pmd_t pmd)1621 static inline int pmd_write(pmd_t pmd)
1622 {
1623 BUG();
1624 return 0;
1625 }
1626 #endif /* pmd_write */
1627 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1628
1629 #ifndef pud_write
pud_write(pud_t pud)1630 static inline int pud_write(pud_t pud)
1631 {
1632 BUG();
1633 return 0;
1634 }
1635 #endif /* pud_write */
1636
1637 #if !defined(CONFIG_ARCH_HAS_PTE_DEVMAP) || !defined(CONFIG_TRANSPARENT_HUGEPAGE)
pmd_devmap(pmd_t pmd)1638 static inline int pmd_devmap(pmd_t pmd)
1639 {
1640 return 0;
1641 }
pud_devmap(pud_t pud)1642 static inline int pud_devmap(pud_t pud)
1643 {
1644 return 0;
1645 }
pgd_devmap(pgd_t pgd)1646 static inline int pgd_devmap(pgd_t pgd)
1647 {
1648 return 0;
1649 }
1650 #endif
1651
1652 #if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
1653 !defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
pud_trans_huge(pud_t pud)1654 static inline int pud_trans_huge(pud_t pud)
1655 {
1656 return 0;
1657 }
1658 #endif
1659
pud_trans_unstable(pud_t * pud)1660 static inline int pud_trans_unstable(pud_t *pud)
1661 {
1662 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
1663 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
1664 pud_t pudval = READ_ONCE(*pud);
1665
1666 if (pud_none(pudval) || pud_trans_huge(pudval) || pud_devmap(pudval))
1667 return 1;
1668 if (unlikely(pud_bad(pudval))) {
1669 pud_clear_bad(pud);
1670 return 1;
1671 }
1672 #endif
1673 return 0;
1674 }
1675
1676 #ifndef CONFIG_NUMA_BALANCING
1677 /*
1678 * In an inaccessible (PROT_NONE) VMA, pte_protnone() may indicate "yes". It is
1679 * perfectly valid to indicate "no" in that case, which is why our default
1680 * implementation defaults to "always no".
1681 *
1682 * In an accessible VMA, however, pte_protnone() reliably indicates PROT_NONE
1683 * page protection due to NUMA hinting. NUMA hinting faults only apply in
1684 * accessible VMAs.
1685 *
1686 * So, to reliably identify PROT_NONE PTEs that require a NUMA hinting fault,
1687 * looking at the VMA accessibility is sufficient.
1688 */
pte_protnone(pte_t pte)1689 static inline int pte_protnone(pte_t pte)
1690 {
1691 return 0;
1692 }
1693
pmd_protnone(pmd_t pmd)1694 static inline int pmd_protnone(pmd_t pmd)
1695 {
1696 return 0;
1697 }
1698 #endif /* CONFIG_NUMA_BALANCING */
1699
1700 /*
1701 * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values
1702 * and let generic vmalloc, ioremap and page table update code know when
1703 * arch_sync_kernel_mappings() needs to be called.
1704 */
1705 #ifndef ARCH_PAGE_TABLE_SYNC_MASK
1706 #define ARCH_PAGE_TABLE_SYNC_MASK 0
1707 #endif
1708
1709 /*
1710 * There is no default implementation for arch_sync_kernel_mappings(). It is
1711 * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK
1712 * is 0.
1713 */
1714 void arch_sync_kernel_mappings(unsigned long start, unsigned long end);
1715
1716 #endif /* CONFIG_MMU */
1717
1718 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
1719
1720 #ifndef __PAGETABLE_P4D_FOLDED
1721 int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot);
1722 void p4d_clear_huge(p4d_t *p4d);
1723 #else
p4d_set_huge(p4d_t * p4d,phys_addr_t addr,pgprot_t prot)1724 static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
1725 {
1726 return 0;
1727 }
p4d_clear_huge(p4d_t * p4d)1728 static inline void p4d_clear_huge(p4d_t *p4d) { }
1729 #endif /* !__PAGETABLE_P4D_FOLDED */
1730
1731 int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot);
1732 int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot);
1733 int pud_clear_huge(pud_t *pud);
1734 int pmd_clear_huge(pmd_t *pmd);
1735 int p4d_free_pud_page(p4d_t *p4d, unsigned long addr);
1736 int pud_free_pmd_page(pud_t *pud, unsigned long addr);
1737 int pmd_free_pte_page(pmd_t *pmd, unsigned long addr);
1738 #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
p4d_set_huge(p4d_t * p4d,phys_addr_t addr,pgprot_t prot)1739 static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
1740 {
1741 return 0;
1742 }
pud_set_huge(pud_t * pud,phys_addr_t addr,pgprot_t prot)1743 static inline int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
1744 {
1745 return 0;
1746 }
pmd_set_huge(pmd_t * pmd,phys_addr_t addr,pgprot_t prot)1747 static inline int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
1748 {
1749 return 0;
1750 }
p4d_clear_huge(p4d_t * p4d)1751 static inline void p4d_clear_huge(p4d_t *p4d) { }
pud_clear_huge(pud_t * pud)1752 static inline int pud_clear_huge(pud_t *pud)
1753 {
1754 return 0;
1755 }
pmd_clear_huge(pmd_t * pmd)1756 static inline int pmd_clear_huge(pmd_t *pmd)
1757 {
1758 return 0;
1759 }
p4d_free_pud_page(p4d_t * p4d,unsigned long addr)1760 static inline int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
1761 {
1762 return 0;
1763 }
pud_free_pmd_page(pud_t * pud,unsigned long addr)1764 static inline int pud_free_pmd_page(pud_t *pud, unsigned long addr)
1765 {
1766 return 0;
1767 }
pmd_free_pte_page(pmd_t * pmd,unsigned long addr)1768 static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
1769 {
1770 return 0;
1771 }
1772 #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
1773
1774 #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
1775 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1776 /*
1777 * ARCHes with special requirements for evicting THP backing TLB entries can
1778 * implement this. Otherwise also, it can help optimize normal TLB flush in
1779 * THP regime. Stock flush_tlb_range() typically has optimization to nuke the
1780 * entire TLB if flush span is greater than a threshold, which will
1781 * likely be true for a single huge page. Thus a single THP flush will
1782 * invalidate the entire TLB which is not desirable.
1783 * e.g. see arch/arc: flush_pmd_tlb_range
1784 */
1785 #define flush_pmd_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1786 #define flush_pud_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1787 #else
1788 #define flush_pmd_tlb_range(vma, addr, end) BUILD_BUG()
1789 #define flush_pud_tlb_range(vma, addr, end) BUILD_BUG()
1790 #endif
1791 #endif
1792
1793 struct file;
1794 int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
1795 unsigned long size, pgprot_t *vma_prot);
1796
1797 #ifndef CONFIG_X86_ESPFIX64
init_espfix_bsp(void)1798 static inline void init_espfix_bsp(void) { }
1799 #endif
1800
1801 extern void __init pgtable_cache_init(void);
1802
1803 #ifndef __HAVE_ARCH_PFN_MODIFY_ALLOWED
pfn_modify_allowed(unsigned long pfn,pgprot_t prot)1804 static inline bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot)
1805 {
1806 return true;
1807 }
1808
arch_has_pfn_modify_check(void)1809 static inline bool arch_has_pfn_modify_check(void)
1810 {
1811 return false;
1812 }
1813 #endif /* !_HAVE_ARCH_PFN_MODIFY_ALLOWED */
1814
1815 /*
1816 * Architecture PAGE_KERNEL_* fallbacks
1817 *
1818 * Some architectures don't define certain PAGE_KERNEL_* flags. This is either
1819 * because they really don't support them, or the port needs to be updated to
1820 * reflect the required functionality. Below are a set of relatively safe
1821 * fallbacks, as best effort, which we can count on in lieu of the architectures
1822 * not defining them on their own yet.
1823 */
1824
1825 #ifndef PAGE_KERNEL_RO
1826 # define PAGE_KERNEL_RO PAGE_KERNEL
1827 #endif
1828
1829 #ifndef PAGE_KERNEL_EXEC
1830 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1831 #endif
1832
1833 /*
1834 * Page Table Modification bits for pgtbl_mod_mask.
1835 *
1836 * These are used by the p?d_alloc_track*() and p*d_populate_kernel()
1837 * functions in the generic vmalloc, ioremap and page table update code
1838 * to track at which page-table levels entries have been modified.
1839 * Based on that the code can better decide when page table changes need
1840 * to be synchronized to other page-tables in the system.
1841 */
1842 #define __PGTBL_PGD_MODIFIED 0
1843 #define __PGTBL_P4D_MODIFIED 1
1844 #define __PGTBL_PUD_MODIFIED 2
1845 #define __PGTBL_PMD_MODIFIED 3
1846 #define __PGTBL_PTE_MODIFIED 4
1847
1848 #define PGTBL_PGD_MODIFIED BIT(__PGTBL_PGD_MODIFIED)
1849 #define PGTBL_P4D_MODIFIED BIT(__PGTBL_P4D_MODIFIED)
1850 #define PGTBL_PUD_MODIFIED BIT(__PGTBL_PUD_MODIFIED)
1851 #define PGTBL_PMD_MODIFIED BIT(__PGTBL_PMD_MODIFIED)
1852 #define PGTBL_PTE_MODIFIED BIT(__PGTBL_PTE_MODIFIED)
1853
1854 /* Page-Table Modification Mask */
1855 typedef unsigned int pgtbl_mod_mask;
1856
1857 #endif /* !__ASSEMBLY__ */
1858
1859 #if !defined(MAX_POSSIBLE_PHYSMEM_BITS) && !defined(CONFIG_64BIT)
1860 #ifdef CONFIG_PHYS_ADDR_T_64BIT
1861 /*
1862 * ZSMALLOC needs to know the highest PFN on 32-bit architectures
1863 * with physical address space extension, but falls back to
1864 * BITS_PER_LONG otherwise.
1865 */
1866 #error Missing MAX_POSSIBLE_PHYSMEM_BITS definition
1867 #else
1868 #define MAX_POSSIBLE_PHYSMEM_BITS 32
1869 #endif
1870 #endif
1871
1872 #ifndef has_transparent_hugepage
1873 #define has_transparent_hugepage() IS_BUILTIN(CONFIG_TRANSPARENT_HUGEPAGE)
1874 #endif
1875
1876 #ifndef has_transparent_pud_hugepage
1877 #define has_transparent_pud_hugepage() IS_BUILTIN(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
1878 #endif
1879 /*
1880 * On some architectures it depends on the mm if the p4d/pud or pmd
1881 * layer of the page table hierarchy is folded or not.
1882 */
1883 #ifndef mm_p4d_folded
1884 #define mm_p4d_folded(mm) __is_defined(__PAGETABLE_P4D_FOLDED)
1885 #endif
1886
1887 #ifndef mm_pud_folded
1888 #define mm_pud_folded(mm) __is_defined(__PAGETABLE_PUD_FOLDED)
1889 #endif
1890
1891 #ifndef mm_pmd_folded
1892 #define mm_pmd_folded(mm) __is_defined(__PAGETABLE_PMD_FOLDED)
1893 #endif
1894
1895 #ifndef p4d_offset_lockless
1896 #define p4d_offset_lockless(pgdp, pgd, address) p4d_offset(&(pgd), address)
1897 #endif
1898 #ifndef pud_offset_lockless
1899 #define pud_offset_lockless(p4dp, p4d, address) pud_offset(&(p4d), address)
1900 #endif
1901 #ifndef pmd_offset_lockless
1902 #define pmd_offset_lockless(pudp, pud, address) pmd_offset(&(pud), address)
1903 #endif
1904
1905 /*
1906 * pXd_leaf() is the API to check whether a pgtable entry is a huge page
1907 * mapping. It should work globally across all archs, without any
1908 * dependency on CONFIG_* options. For architectures that do not support
1909 * huge mappings on specific levels, below fallbacks will be used.
1910 *
1911 * A leaf pgtable entry should always imply the following:
1912 *
1913 * - It is a "present" entry. IOW, before using this API, please check it
1914 * with pXd_present() first. NOTE: it may not always mean the "present
1915 * bit" is set. For example, PROT_NONE entries are always "present".
1916 *
1917 * - It should _never_ be a swap entry of any type. Above "present" check
1918 * should have guarded this, but let's be crystal clear on this.
1919 *
1920 * - It should contain a huge PFN, which points to a huge page larger than
1921 * PAGE_SIZE of the platform. The PFN format isn't important here.
1922 *
1923 * - It should cover all kinds of huge mappings (e.g., pXd_trans_huge(),
1924 * pXd_devmap(), or hugetlb mappings).
1925 */
1926 #ifndef pgd_leaf
1927 #define pgd_leaf(x) false
1928 #endif
1929 #ifndef p4d_leaf
1930 #define p4d_leaf(x) false
1931 #endif
1932 #ifndef pud_leaf
1933 #define pud_leaf(x) false
1934 #endif
1935 #ifndef pmd_leaf
1936 #define pmd_leaf(x) false
1937 #endif
1938
1939 #ifndef pgd_leaf_size
1940 #define pgd_leaf_size(x) (1ULL << PGDIR_SHIFT)
1941 #endif
1942 #ifndef p4d_leaf_size
1943 #define p4d_leaf_size(x) P4D_SIZE
1944 #endif
1945 #ifndef pud_leaf_size
1946 #define pud_leaf_size(x) PUD_SIZE
1947 #endif
1948 #ifndef pmd_leaf_size
1949 #define pmd_leaf_size(x) PMD_SIZE
1950 #endif
1951 #ifndef __pte_leaf_size
1952 #ifndef pte_leaf_size
1953 #define pte_leaf_size(x) PAGE_SIZE
1954 #endif
1955 #define __pte_leaf_size(x,y) pte_leaf_size(y)
1956 #endif
1957
1958 /*
1959 * We always define pmd_pfn for all archs as it's used in lots of generic
1960 * code. Now it happens too for pud_pfn (and can happen for larger
1961 * mappings too in the future; we're not there yet). Instead of defining
1962 * it for all archs (like pmd_pfn), provide a fallback.
1963 *
1964 * Note that returning 0 here means any arch that didn't define this can
1965 * get severely wrong when it hits a real pud leaf. It's arch's
1966 * responsibility to properly define it when a huge pud is possible.
1967 */
1968 #ifndef pud_pfn
1969 #define pud_pfn(x) 0
1970 #endif
1971
1972 /*
1973 * Some architectures have MMUs that are configurable or selectable at boot
1974 * time. These lead to variable PTRS_PER_x. For statically allocated arrays it
1975 * helps to have a static maximum value.
1976 */
1977
1978 #ifndef MAX_PTRS_PER_PTE
1979 #define MAX_PTRS_PER_PTE PTRS_PER_PTE
1980 #endif
1981
1982 #ifndef MAX_PTRS_PER_PMD
1983 #define MAX_PTRS_PER_PMD PTRS_PER_PMD
1984 #endif
1985
1986 #ifndef MAX_PTRS_PER_PUD
1987 #define MAX_PTRS_PER_PUD PTRS_PER_PUD
1988 #endif
1989
1990 #ifndef MAX_PTRS_PER_P4D
1991 #define MAX_PTRS_PER_P4D PTRS_PER_P4D
1992 #endif
1993
1994 #ifndef pte_pgprot
1995 #define pte_pgprot(x) ((pgprot_t) {0})
1996 #endif
1997
1998 #ifndef pmd_pgprot
1999 #define pmd_pgprot(x) ((pgprot_t) {0})
2000 #endif
2001
2002 #ifndef pud_pgprot
2003 #define pud_pgprot(x) ((pgprot_t) {0})
2004 #endif
2005
2006 /* description of effects of mapping type and prot in current implementation.
2007 * this is due to the limited x86 page protection hardware. The expected
2008 * behavior is in parens:
2009 *
2010 * map_type prot
2011 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
2012 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
2013 * w: (no) no w: (no) no w: (yes) yes w: (no) no
2014 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
2015 *
2016 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
2017 * w: (no) no w: (no) no w: (copy) copy w: (no) no
2018 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
2019 *
2020 * On arm64, PROT_EXEC has the following behaviour for both MAP_SHARED and
2021 * MAP_PRIVATE (with Enhanced PAN supported):
2022 * r: (no) no
2023 * w: (no) no
2024 * x: (yes) yes
2025 */
2026 #define DECLARE_VM_GET_PAGE_PROT \
2027 pgprot_t vm_get_page_prot(unsigned long vm_flags) \
2028 { \
2029 return protection_map[vm_flags & \
2030 (VM_READ | VM_WRITE | VM_EXEC | VM_SHARED)]; \
2031 } \
2032 EXPORT_SYMBOL(vm_get_page_prot);
2033
2034 #endif /* _LINUX_PGTABLE_H */
2035