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 #ifndef __ASSEMBLY__
9 #ifdef CONFIG_MMU
10
11 #include <linux/mm_types.h>
12 #include <linux/bug.h>
13 #include <linux/errno.h>
14 #include <asm-generic/pgtable_uffd.h>
15
16 #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
17 defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
18 #error CONFIG_PGTABLE_LEVELS is not consistent with __PAGETABLE_{P4D,PUD,PMD}_FOLDED
19 #endif
20
21 /*
22 * On almost all architectures and configurations, 0 can be used as the
23 * upper ceiling to free_pgtables(): on many architectures it has the same
24 * effect as using TASK_SIZE. However, there is one configuration which
25 * must impose a more careful limit, to avoid freeing kernel pgtables.
26 */
27 #ifndef USER_PGTABLES_CEILING
28 #define USER_PGTABLES_CEILING 0UL
29 #endif
30
31 /*
32 * This defines the first usable user address. Platforms
33 * can override its value with custom FIRST_USER_ADDRESS
34 * defined in their respective <asm/pgtable.h>.
35 */
36 #ifndef FIRST_USER_ADDRESS
37 #define FIRST_USER_ADDRESS 0UL
38 #endif
39
40 /*
41 * This defines the generic helper for accessing PMD page
42 * table page. Although platforms can still override this
43 * via their respective <asm/pgtable.h>.
44 */
45 #ifndef pmd_pgtable
46 #define pmd_pgtable(pmd) pmd_page(pmd)
47 #endif
48
49 /*
50 * A page table page can be thought of an array like this: pXd_t[PTRS_PER_PxD]
51 *
52 * The pXx_index() functions return the index of the entry in the page
53 * table page which would control the given virtual address
54 *
55 * As these functions may be used by the same code for different levels of
56 * the page table folding, they are always available, regardless of
57 * CONFIG_PGTABLE_LEVELS value. For the folded levels they simply return 0
58 * because in such cases PTRS_PER_PxD equals 1.
59 */
60
pte_index(unsigned long address)61 static inline unsigned long pte_index(unsigned long address)
62 {
63 return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
64 }
65 #define pte_index pte_index
66
67 #ifndef pmd_index
pmd_index(unsigned long address)68 static inline unsigned long pmd_index(unsigned long address)
69 {
70 return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
71 }
72 #define pmd_index pmd_index
73 #endif
74
75 #ifndef pud_index
pud_index(unsigned long address)76 static inline unsigned long pud_index(unsigned long address)
77 {
78 return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
79 }
80 #define pud_index pud_index
81 #endif
82
83 #ifndef pgd_index
84 /* Must be a compile-time constant, so implement it as a macro */
85 #define pgd_index(a) (((a) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
86 #endif
87
88 #ifndef pte_offset_kernel
pte_offset_kernel(pmd_t * pmd,unsigned long address)89 static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address)
90 {
91 return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address);
92 }
93 #define pte_offset_kernel pte_offset_kernel
94 #endif
95
96 #if defined(CONFIG_HIGHPTE)
97 #define pte_offset_map(dir, address) \
98 ((pte_t *)kmap_atomic(pmd_page(*(dir))) + \
99 pte_index((address)))
100 #define pte_unmap(pte) kunmap_atomic((pte))
101 #else
102 #define pte_offset_map(dir, address) pte_offset_kernel((dir), (address))
103 #define pte_unmap(pte) ((void)(pte)) /* NOP */
104 #endif
105
106 /* Find an entry in the second-level page table.. */
107 #ifndef pmd_offset
pmd_offset(pud_t * pud,unsigned long address)108 static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
109 {
110 return pud_pgtable(*pud) + pmd_index(address);
111 }
112 #define pmd_offset pmd_offset
113 #endif
114
115 #ifndef pud_offset
pud_offset(p4d_t * p4d,unsigned long address)116 static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
117 {
118 return p4d_pgtable(*p4d) + pud_index(address);
119 }
120 #define pud_offset pud_offset
121 #endif
122
pgd_offset_pgd(pgd_t * pgd,unsigned long address)123 static inline pgd_t *pgd_offset_pgd(pgd_t *pgd, unsigned long address)
124 {
125 return (pgd + pgd_index(address));
126 };
127
128 /*
129 * a shortcut to get a pgd_t in a given mm
130 */
131 #ifndef pgd_offset
132 #define pgd_offset(mm, address) pgd_offset_pgd((mm)->pgd, (address))
133 #endif
134
135 /*
136 * a shortcut which implies the use of the kernel's pgd, instead
137 * of a process's
138 */
139 #ifndef pgd_offset_k
140 #define pgd_offset_k(address) pgd_offset(&init_mm, (address))
141 #endif
142
143 /*
144 * In many cases it is known that a virtual address is mapped at PMD or PTE
145 * level, so instead of traversing all the page table levels, we can get a
146 * pointer to the PMD entry in user or kernel page table or translate a virtual
147 * address to the pointer in the PTE in the kernel page tables with simple
148 * helpers.
149 */
pmd_off(struct mm_struct * mm,unsigned long va)150 static inline pmd_t *pmd_off(struct mm_struct *mm, unsigned long va)
151 {
152 return pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, va), va), va), va);
153 }
154
pmd_off_k(unsigned long va)155 static inline pmd_t *pmd_off_k(unsigned long va)
156 {
157 return pmd_offset(pud_offset(p4d_offset(pgd_offset_k(va), va), va), va);
158 }
159
virt_to_kpte(unsigned long vaddr)160 static inline pte_t *virt_to_kpte(unsigned long vaddr)
161 {
162 pmd_t *pmd = pmd_off_k(vaddr);
163
164 return pmd_none(*pmd) ? NULL : pte_offset_kernel(pmd, vaddr);
165 }
166
167 #ifndef pmd_young
pmd_young(pmd_t pmd)168 static inline int pmd_young(pmd_t pmd)
169 {
170 return 0;
171 }
172 #endif
173
174 #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
175 extern int ptep_set_access_flags(struct vm_area_struct *vma,
176 unsigned long address, pte_t *ptep,
177 pte_t entry, int dirty);
178 #endif
179
180 #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
181 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
182 extern int pmdp_set_access_flags(struct vm_area_struct *vma,
183 unsigned long address, pmd_t *pmdp,
184 pmd_t entry, int dirty);
185 extern int pudp_set_access_flags(struct vm_area_struct *vma,
186 unsigned long address, pud_t *pudp,
187 pud_t entry, int dirty);
188 #else
pmdp_set_access_flags(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp,pmd_t entry,int dirty)189 static inline int pmdp_set_access_flags(struct vm_area_struct *vma,
190 unsigned long address, pmd_t *pmdp,
191 pmd_t entry, int dirty)
192 {
193 BUILD_BUG();
194 return 0;
195 }
pudp_set_access_flags(struct vm_area_struct * vma,unsigned long address,pud_t * pudp,pud_t entry,int dirty)196 static inline int pudp_set_access_flags(struct vm_area_struct *vma,
197 unsigned long address, pud_t *pudp,
198 pud_t entry, int dirty)
199 {
200 BUILD_BUG();
201 return 0;
202 }
203 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
204 #endif
205
206 #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
ptep_test_and_clear_young(struct vm_area_struct * vma,unsigned long address,pte_t * ptep)207 static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
208 unsigned long address,
209 pte_t *ptep)
210 {
211 pte_t pte = *ptep;
212 int r = 1;
213 if (!pte_young(pte))
214 r = 0;
215 else
216 set_pte_at(vma->vm_mm, address, ptep, pte_mkold(pte));
217 return r;
218 }
219 #endif
220
221 #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
222 #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)223 static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
224 unsigned long address,
225 pmd_t *pmdp)
226 {
227 pmd_t pmd = *pmdp;
228 int r = 1;
229 if (!pmd_young(pmd))
230 r = 0;
231 else
232 set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
233 return r;
234 }
235 #else
pmdp_test_and_clear_young(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)236 static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
237 unsigned long address,
238 pmd_t *pmdp)
239 {
240 BUILD_BUG();
241 return 0;
242 }
243 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
244 #endif
245
246 #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
247 int ptep_clear_flush_young(struct vm_area_struct *vma,
248 unsigned long address, pte_t *ptep);
249 #endif
250
251 #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
252 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
253 extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
254 unsigned long address, pmd_t *pmdp);
255 #else
256 /*
257 * Despite relevant to THP only, this API is called from generic rmap code
258 * under PageTransHuge(), hence needs a dummy implementation for !THP
259 */
pmdp_clear_flush_young(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)260 static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
261 unsigned long address, pmd_t *pmdp)
262 {
263 BUILD_BUG();
264 return 0;
265 }
266 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
267 #endif
268
269 #ifndef arch_has_hw_nonleaf_pmd_young
270 /*
271 * Return whether the accessed bit in non-leaf PMD entries is supported on the
272 * local CPU.
273 */
arch_has_hw_nonleaf_pmd_young(void)274 static inline bool arch_has_hw_nonleaf_pmd_young(void)
275 {
276 return IS_ENABLED(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG);
277 }
278 #endif
279
280 #ifndef arch_has_hw_pte_young
281 /*
282 * Return whether the accessed bit is supported on the local CPU.
283 *
284 * This stub assumes accessing through an old PTE triggers a page fault.
285 * Architectures that automatically set the access bit should overwrite it.
286 */
arch_has_hw_pte_young(void)287 static inline bool arch_has_hw_pte_young(void)
288 {
289 return false;
290 }
291 #endif
292
293 #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
ptep_get_and_clear(struct mm_struct * mm,unsigned long address,pte_t * ptep)294 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
295 unsigned long address,
296 pte_t *ptep)
297 {
298 pte_t pte = *ptep;
299 pte_clear(mm, address, ptep);
300 return pte;
301 }
302 #endif
303
304 #ifndef __HAVE_ARCH_PTEP_GET
ptep_get(pte_t * ptep)305 static inline pte_t ptep_get(pte_t *ptep)
306 {
307 return READ_ONCE(*ptep);
308 }
309 #endif
310
311 #ifdef CONFIG_GUP_GET_PTE_LOW_HIGH
312 /*
313 * WARNING: only to be used in the get_user_pages_fast() implementation.
314 *
315 * With get_user_pages_fast(), we walk down the pagetables without taking any
316 * locks. For this we would like to load the pointers atomically, but sometimes
317 * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What
318 * we do have is the guarantee that a PTE will only either go from not present
319 * to present, or present to not present or both -- it will not switch to a
320 * completely different present page without a TLB flush in between; something
321 * that we are blocking by holding interrupts off.
322 *
323 * Setting ptes from not present to present goes:
324 *
325 * ptep->pte_high = h;
326 * smp_wmb();
327 * ptep->pte_low = l;
328 *
329 * And present to not present goes:
330 *
331 * ptep->pte_low = 0;
332 * smp_wmb();
333 * ptep->pte_high = 0;
334 *
335 * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'.
336 * We load pte_high *after* loading pte_low, which ensures we don't see an older
337 * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't
338 * picked up a changed pte high. We might have gotten rubbish values from
339 * pte_low and pte_high, but we are guaranteed that pte_low will not have the
340 * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
341 * operates on present ptes we're safe.
342 */
ptep_get_lockless(pte_t * ptep)343 static inline pte_t ptep_get_lockless(pte_t *ptep)
344 {
345 pte_t pte;
346
347 do {
348 pte.pte_low = ptep->pte_low;
349 smp_rmb();
350 pte.pte_high = ptep->pte_high;
351 smp_rmb();
352 } while (unlikely(pte.pte_low != ptep->pte_low));
353
354 return pte;
355 }
356 #else /* CONFIG_GUP_GET_PTE_LOW_HIGH */
357 /*
358 * We require that the PTE can be read atomically.
359 */
ptep_get_lockless(pte_t * ptep)360 static inline pte_t ptep_get_lockless(pte_t *ptep)
361 {
362 return ptep_get(ptep);
363 }
364 #endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */
365
366 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
367 #ifndef __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
pmdp_huge_get_and_clear(struct mm_struct * mm,unsigned long address,pmd_t * pmdp)368 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
369 unsigned long address,
370 pmd_t *pmdp)
371 {
372 pmd_t pmd = *pmdp;
373 pmd_clear(pmdp);
374 return pmd;
375 }
376 #endif /* __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR */
377 #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
pudp_huge_get_and_clear(struct mm_struct * mm,unsigned long address,pud_t * pudp)378 static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
379 unsigned long address,
380 pud_t *pudp)
381 {
382 pud_t pud = *pudp;
383
384 pud_clear(pudp);
385 return pud;
386 }
387 #endif /* __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR */
388 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
389
390 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
391 #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)392 static inline pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma,
393 unsigned long address, pmd_t *pmdp,
394 int full)
395 {
396 return pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
397 }
398 #endif
399
400 #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR_FULL
pudp_huge_get_and_clear_full(struct mm_struct * mm,unsigned long address,pud_t * pudp,int full)401 static inline pud_t pudp_huge_get_and_clear_full(struct mm_struct *mm,
402 unsigned long address, pud_t *pudp,
403 int full)
404 {
405 return pudp_huge_get_and_clear(mm, address, pudp);
406 }
407 #endif
408 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
409
410 #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)411 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
412 unsigned long address, pte_t *ptep,
413 int full)
414 {
415 pte_t pte;
416 pte = ptep_get_and_clear(mm, address, ptep);
417 return pte;
418 }
419 #endif
420
421
422 /*
423 * If two threads concurrently fault at the same page, the thread that
424 * won the race updates the PTE and its local TLB/Cache. The other thread
425 * gives up, simply does nothing, and continues; on architectures where
426 * software can update TLB, local TLB can be updated here to avoid next page
427 * fault. This function updates TLB only, do nothing with cache or others.
428 * It is the difference with function update_mmu_cache.
429 */
430 #ifndef __HAVE_ARCH_UPDATE_MMU_TLB
update_mmu_tlb(struct vm_area_struct * vma,unsigned long address,pte_t * ptep)431 static inline void update_mmu_tlb(struct vm_area_struct *vma,
432 unsigned long address, pte_t *ptep)
433 {
434 }
435 #define __HAVE_ARCH_UPDATE_MMU_TLB
436 #endif
437
438 /*
439 * Some architectures may be able to avoid expensive synchronization
440 * primitives when modifications are made to PTE's which are already
441 * not present, or in the process of an address space destruction.
442 */
443 #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)444 static inline void pte_clear_not_present_full(struct mm_struct *mm,
445 unsigned long address,
446 pte_t *ptep,
447 int full)
448 {
449 pte_clear(mm, address, ptep);
450 }
451 #endif
452
453 #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
454 extern pte_t ptep_clear_flush(struct vm_area_struct *vma,
455 unsigned long address,
456 pte_t *ptep);
457 #endif
458
459 #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
460 extern pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma,
461 unsigned long address,
462 pmd_t *pmdp);
463 extern pud_t pudp_huge_clear_flush(struct vm_area_struct *vma,
464 unsigned long address,
465 pud_t *pudp);
466 #endif
467
468 #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
469 struct mm_struct;
ptep_set_wrprotect(struct mm_struct * mm,unsigned long address,pte_t * ptep)470 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
471 {
472 pte_t old_pte = *ptep;
473 set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
474 }
475 #endif
476
477 /*
478 * On some architectures hardware does not set page access bit when accessing
479 * memory page, it is responsibility of software setting this bit. It brings
480 * out extra page fault penalty to track page access bit. For optimization page
481 * access bit can be set during all page fault flow on these arches.
482 * To be differentiate with macro pte_mkyoung, this macro is used on platforms
483 * where software maintains page access bit.
484 */
485 #ifndef pte_sw_mkyoung
pte_sw_mkyoung(pte_t pte)486 static inline pte_t pte_sw_mkyoung(pte_t pte)
487 {
488 return pte;
489 }
490 #define pte_sw_mkyoung pte_sw_mkyoung
491 #endif
492
493 #ifndef pte_savedwrite
494 #define pte_savedwrite pte_write
495 #endif
496
497 #ifndef pte_mk_savedwrite
498 #define pte_mk_savedwrite pte_mkwrite
499 #endif
500
501 #ifndef pte_clear_savedwrite
502 #define pte_clear_savedwrite pte_wrprotect
503 #endif
504
505 #ifndef pmd_savedwrite
506 #define pmd_savedwrite pmd_write
507 #endif
508
509 #ifndef pmd_mk_savedwrite
510 #define pmd_mk_savedwrite pmd_mkwrite
511 #endif
512
513 #ifndef pmd_clear_savedwrite
514 #define pmd_clear_savedwrite pmd_wrprotect
515 #endif
516
517 #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT
518 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmdp_set_wrprotect(struct mm_struct * mm,unsigned long address,pmd_t * pmdp)519 static inline void pmdp_set_wrprotect(struct mm_struct *mm,
520 unsigned long address, pmd_t *pmdp)
521 {
522 pmd_t old_pmd = *pmdp;
523 set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
524 }
525 #else
pmdp_set_wrprotect(struct mm_struct * mm,unsigned long address,pmd_t * pmdp)526 static inline void pmdp_set_wrprotect(struct mm_struct *mm,
527 unsigned long address, pmd_t *pmdp)
528 {
529 BUILD_BUG();
530 }
531 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
532 #endif
533 #ifndef __HAVE_ARCH_PUDP_SET_WRPROTECT
534 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
pudp_set_wrprotect(struct mm_struct * mm,unsigned long address,pud_t * pudp)535 static inline void pudp_set_wrprotect(struct mm_struct *mm,
536 unsigned long address, pud_t *pudp)
537 {
538 pud_t old_pud = *pudp;
539
540 set_pud_at(mm, address, pudp, pud_wrprotect(old_pud));
541 }
542 #else
pudp_set_wrprotect(struct mm_struct * mm,unsigned long address,pud_t * pudp)543 static inline void pudp_set_wrprotect(struct mm_struct *mm,
544 unsigned long address, pud_t *pudp)
545 {
546 BUILD_BUG();
547 }
548 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
549 #endif
550
551 #ifndef pmdp_collapse_flush
552 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
553 extern pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
554 unsigned long address, pmd_t *pmdp);
555 #else
pmdp_collapse_flush(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)556 static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
557 unsigned long address,
558 pmd_t *pmdp)
559 {
560 BUILD_BUG();
561 return *pmdp;
562 }
563 #define pmdp_collapse_flush pmdp_collapse_flush
564 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
565 #endif
566
567 #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT
568 extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
569 pgtable_t pgtable);
570 #endif
571
572 #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW
573 extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
574 #endif
575
576 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
577 /*
578 * This is an implementation of pmdp_establish() that is only suitable for an
579 * architecture that doesn't have hardware dirty/accessed bits. In this case we
580 * can't race with CPU which sets these bits and non-atomic approach is fine.
581 */
generic_pmdp_establish(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp,pmd_t pmd)582 static inline pmd_t generic_pmdp_establish(struct vm_area_struct *vma,
583 unsigned long address, pmd_t *pmdp, pmd_t pmd)
584 {
585 pmd_t old_pmd = *pmdp;
586 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
587 return old_pmd;
588 }
589 #endif
590
591 #ifndef __HAVE_ARCH_PMDP_INVALIDATE
592 extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
593 pmd_t *pmdp);
594 #endif
595
596 #ifndef __HAVE_ARCH_PTE_SAME
pte_same(pte_t pte_a,pte_t pte_b)597 static inline int pte_same(pte_t pte_a, pte_t pte_b)
598 {
599 return pte_val(pte_a) == pte_val(pte_b);
600 }
601 #endif
602
603 #ifndef __HAVE_ARCH_PTE_UNUSED
604 /*
605 * Some architectures provide facilities to virtualization guests
606 * so that they can flag allocated pages as unused. This allows the
607 * host to transparently reclaim unused pages. This function returns
608 * whether the pte's page is unused.
609 */
pte_unused(pte_t pte)610 static inline int pte_unused(pte_t pte)
611 {
612 return 0;
613 }
614 #endif
615
616 #ifndef pte_access_permitted
617 #define pte_access_permitted(pte, write) \
618 (pte_present(pte) && (!(write) || pte_write(pte)))
619 #endif
620
621 #ifndef pmd_access_permitted
622 #define pmd_access_permitted(pmd, write) \
623 (pmd_present(pmd) && (!(write) || pmd_write(pmd)))
624 #endif
625
626 #ifndef pud_access_permitted
627 #define pud_access_permitted(pud, write) \
628 (pud_present(pud) && (!(write) || pud_write(pud)))
629 #endif
630
631 #ifndef p4d_access_permitted
632 #define p4d_access_permitted(p4d, write) \
633 (p4d_present(p4d) && (!(write) || p4d_write(p4d)))
634 #endif
635
636 #ifndef pgd_access_permitted
637 #define pgd_access_permitted(pgd, write) \
638 (pgd_present(pgd) && (!(write) || pgd_write(pgd)))
639 #endif
640
641 #ifndef __HAVE_ARCH_PMD_SAME
pmd_same(pmd_t pmd_a,pmd_t pmd_b)642 static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
643 {
644 return pmd_val(pmd_a) == pmd_val(pmd_b);
645 }
646
pud_same(pud_t pud_a,pud_t pud_b)647 static inline int pud_same(pud_t pud_a, pud_t pud_b)
648 {
649 return pud_val(pud_a) == pud_val(pud_b);
650 }
651 #endif
652
653 #ifndef __HAVE_ARCH_P4D_SAME
p4d_same(p4d_t p4d_a,p4d_t p4d_b)654 static inline int p4d_same(p4d_t p4d_a, p4d_t p4d_b)
655 {
656 return p4d_val(p4d_a) == p4d_val(p4d_b);
657 }
658 #endif
659
660 #ifndef __HAVE_ARCH_PGD_SAME
pgd_same(pgd_t pgd_a,pgd_t pgd_b)661 static inline int pgd_same(pgd_t pgd_a, pgd_t pgd_b)
662 {
663 return pgd_val(pgd_a) == pgd_val(pgd_b);
664 }
665 #endif
666
667 /*
668 * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
669 * TLB flush will be required as a result of the "set". For example, use
670 * in scenarios where it is known ahead of time that the routine is
671 * setting non-present entries, or re-setting an existing entry to the
672 * same value. Otherwise, use the typical "set" helpers and flush the
673 * TLB.
674 */
675 #define set_pte_safe(ptep, pte) \
676 ({ \
677 WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
678 set_pte(ptep, pte); \
679 })
680
681 #define set_pmd_safe(pmdp, pmd) \
682 ({ \
683 WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
684 set_pmd(pmdp, pmd); \
685 })
686
687 #define set_pud_safe(pudp, pud) \
688 ({ \
689 WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
690 set_pud(pudp, pud); \
691 })
692
693 #define set_p4d_safe(p4dp, p4d) \
694 ({ \
695 WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
696 set_p4d(p4dp, p4d); \
697 })
698
699 #define set_pgd_safe(pgdp, pgd) \
700 ({ \
701 WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
702 set_pgd(pgdp, pgd); \
703 })
704
705 #ifndef __HAVE_ARCH_DO_SWAP_PAGE
706 /*
707 * Some architectures support metadata associated with a page. When a
708 * page is being swapped out, this metadata must be saved so it can be
709 * restored when the page is swapped back in. SPARC M7 and newer
710 * processors support an ADI (Application Data Integrity) tag for the
711 * page as metadata for the page. arch_do_swap_page() can restore this
712 * metadata when a page is swapped back in.
713 */
arch_do_swap_page(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,pte_t pte,pte_t oldpte)714 static inline void arch_do_swap_page(struct mm_struct *mm,
715 struct vm_area_struct *vma,
716 unsigned long addr,
717 pte_t pte, pte_t oldpte)
718 {
719
720 }
721 #endif
722
723 #ifndef __HAVE_ARCH_UNMAP_ONE
724 /*
725 * Some architectures support metadata associated with a page. When a
726 * page is being swapped out, this metadata must be saved so it can be
727 * restored when the page is swapped back in. SPARC M7 and newer
728 * processors support an ADI (Application Data Integrity) tag for the
729 * page as metadata for the page. arch_unmap_one() can save this
730 * metadata on a swap-out of a page.
731 */
arch_unmap_one(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,pte_t orig_pte)732 static inline int arch_unmap_one(struct mm_struct *mm,
733 struct vm_area_struct *vma,
734 unsigned long addr,
735 pte_t orig_pte)
736 {
737 return 0;
738 }
739 #endif
740
741 /*
742 * Allow architectures to preserve additional metadata associated with
743 * swapped-out pages. The corresponding __HAVE_ARCH_SWAP_* macros and function
744 * prototypes must be defined in the arch-specific asm/pgtable.h file.
745 */
746 #ifndef __HAVE_ARCH_PREPARE_TO_SWAP
arch_prepare_to_swap(struct page * page)747 static inline int arch_prepare_to_swap(struct page *page)
748 {
749 return 0;
750 }
751 #endif
752
753 #ifndef __HAVE_ARCH_SWAP_INVALIDATE
arch_swap_invalidate_page(int type,pgoff_t offset)754 static inline void arch_swap_invalidate_page(int type, pgoff_t offset)
755 {
756 }
757
arch_swap_invalidate_area(int type)758 static inline void arch_swap_invalidate_area(int type)
759 {
760 }
761 #endif
762
763 #ifndef __HAVE_ARCH_SWAP_RESTORE
arch_swap_restore(swp_entry_t entry,struct page * page)764 static inline void arch_swap_restore(swp_entry_t entry, struct page *page)
765 {
766 }
767 #endif
768
769 #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
770 #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
771 #endif
772
773 #ifndef __HAVE_ARCH_MOVE_PTE
774 #define move_pte(pte, prot, old_addr, new_addr) (pte)
775 #endif
776
777 #ifndef pte_accessible
778 # define pte_accessible(mm, pte) ((void)(pte), 1)
779 #endif
780
781 #ifndef flush_tlb_fix_spurious_fault
782 #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
783 #endif
784
785 /*
786 * When walking page tables, get the address of the next boundary,
787 * or the end address of the range if that comes earlier. Although no
788 * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
789 */
790
791 #define pgd_addr_end(addr, end) \
792 ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
793 (__boundary - 1 < (end) - 1)? __boundary: (end); \
794 })
795
796 #ifndef p4d_addr_end
797 #define p4d_addr_end(addr, end) \
798 ({ unsigned long __boundary = ((addr) + P4D_SIZE) & P4D_MASK; \
799 (__boundary - 1 < (end) - 1)? __boundary: (end); \
800 })
801 #endif
802
803 #ifndef pud_addr_end
804 #define pud_addr_end(addr, end) \
805 ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
806 (__boundary - 1 < (end) - 1)? __boundary: (end); \
807 })
808 #endif
809
810 #ifndef pmd_addr_end
811 #define pmd_addr_end(addr, end) \
812 ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
813 (__boundary - 1 < (end) - 1)? __boundary: (end); \
814 })
815 #endif
816
817 /*
818 * When walking page tables, we usually want to skip any p?d_none entries;
819 * and any p?d_bad entries - reporting the error before resetting to none.
820 * Do the tests inline, but report and clear the bad entry in mm/memory.c.
821 */
822 void pgd_clear_bad(pgd_t *);
823
824 #ifndef __PAGETABLE_P4D_FOLDED
825 void p4d_clear_bad(p4d_t *);
826 #else
827 #define p4d_clear_bad(p4d) do { } while (0)
828 #endif
829
830 #ifndef __PAGETABLE_PUD_FOLDED
831 void pud_clear_bad(pud_t *);
832 #else
833 #define pud_clear_bad(p4d) do { } while (0)
834 #endif
835
836 void pmd_clear_bad(pmd_t *);
837
pgd_none_or_clear_bad(pgd_t * pgd)838 static inline int pgd_none_or_clear_bad(pgd_t *pgd)
839 {
840 if (pgd_none(*pgd))
841 return 1;
842 if (unlikely(pgd_bad(*pgd))) {
843 pgd_clear_bad(pgd);
844 return 1;
845 }
846 return 0;
847 }
848
p4d_none_or_clear_bad(p4d_t * p4d)849 static inline int p4d_none_or_clear_bad(p4d_t *p4d)
850 {
851 if (p4d_none(*p4d))
852 return 1;
853 if (unlikely(p4d_bad(*p4d))) {
854 p4d_clear_bad(p4d);
855 return 1;
856 }
857 return 0;
858 }
859
pud_none_or_clear_bad(pud_t * pud)860 static inline int pud_none_or_clear_bad(pud_t *pud)
861 {
862 if (pud_none(*pud))
863 return 1;
864 if (unlikely(pud_bad(*pud))) {
865 pud_clear_bad(pud);
866 return 1;
867 }
868 return 0;
869 }
870
pmd_none_or_clear_bad(pmd_t * pmd)871 static inline int pmd_none_or_clear_bad(pmd_t *pmd)
872 {
873 if (pmd_none(*pmd))
874 return 1;
875 if (unlikely(pmd_bad(*pmd))) {
876 pmd_clear_bad(pmd);
877 return 1;
878 }
879 return 0;
880 }
881
__ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)882 static inline pte_t __ptep_modify_prot_start(struct vm_area_struct *vma,
883 unsigned long addr,
884 pte_t *ptep)
885 {
886 /*
887 * Get the current pte state, but zero it out to make it
888 * non-present, preventing the hardware from asynchronously
889 * updating it.
890 */
891 return ptep_get_and_clear(vma->vm_mm, addr, ptep);
892 }
893
__ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t pte)894 static inline void __ptep_modify_prot_commit(struct vm_area_struct *vma,
895 unsigned long addr,
896 pte_t *ptep, pte_t pte)
897 {
898 /*
899 * The pte is non-present, so there's no hardware state to
900 * preserve.
901 */
902 set_pte_at(vma->vm_mm, addr, ptep, pte);
903 }
904
905 #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
906 /*
907 * Start a pte protection read-modify-write transaction, which
908 * protects against asynchronous hardware modifications to the pte.
909 * The intention is not to prevent the hardware from making pte
910 * updates, but to prevent any updates it may make from being lost.
911 *
912 * This does not protect against other software modifications of the
913 * pte; the appropriate pte lock must be held over the transaction.
914 *
915 * Note that this interface is intended to be batchable, meaning that
916 * ptep_modify_prot_commit may not actually update the pte, but merely
917 * queue the update to be done at some later time. The update must be
918 * actually committed before the pte lock is released, however.
919 */
ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)920 static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
921 unsigned long addr,
922 pte_t *ptep)
923 {
924 return __ptep_modify_prot_start(vma, addr, ptep);
925 }
926
927 /*
928 * Commit an update to a pte, leaving any hardware-controlled bits in
929 * the PTE unmodified.
930 */
ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t old_pte,pte_t pte)931 static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
932 unsigned long addr,
933 pte_t *ptep, pte_t old_pte, pte_t pte)
934 {
935 __ptep_modify_prot_commit(vma, addr, ptep, pte);
936 }
937 #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
938 #endif /* CONFIG_MMU */
939
940 /*
941 * No-op macros that just return the current protection value. Defined here
942 * because these macros can be used even if CONFIG_MMU is not defined.
943 */
944
945 #ifndef pgprot_nx
946 #define pgprot_nx(prot) (prot)
947 #endif
948
949 #ifndef pgprot_noncached
950 #define pgprot_noncached(prot) (prot)
951 #endif
952
953 #ifndef pgprot_writecombine
954 #define pgprot_writecombine pgprot_noncached
955 #endif
956
957 #ifndef pgprot_writethrough
958 #define pgprot_writethrough pgprot_noncached
959 #endif
960
961 #ifndef pgprot_device
962 #define pgprot_device pgprot_noncached
963 #endif
964
965 #ifndef pgprot_mhp
966 #define pgprot_mhp(prot) (prot)
967 #endif
968
969 #ifdef CONFIG_MMU
970 #ifndef pgprot_modify
971 #define pgprot_modify pgprot_modify
pgprot_modify(pgprot_t oldprot,pgprot_t newprot)972 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
973 {
974 if (pgprot_val(oldprot) == pgprot_val(pgprot_noncached(oldprot)))
975 newprot = pgprot_noncached(newprot);
976 if (pgprot_val(oldprot) == pgprot_val(pgprot_writecombine(oldprot)))
977 newprot = pgprot_writecombine(newprot);
978 if (pgprot_val(oldprot) == pgprot_val(pgprot_device(oldprot)))
979 newprot = pgprot_device(newprot);
980 return newprot;
981 }
982 #endif
983 #endif /* CONFIG_MMU */
984
985 #ifndef pgprot_encrypted
986 #define pgprot_encrypted(prot) (prot)
987 #endif
988
989 #ifndef pgprot_decrypted
990 #define pgprot_decrypted(prot) (prot)
991 #endif
992
993 /*
994 * A facility to provide lazy MMU batching. This allows PTE updates and
995 * page invalidations to be delayed until a call to leave lazy MMU mode
996 * is issued. Some architectures may benefit from doing this, and it is
997 * beneficial for both shadow and direct mode hypervisors, which may batch
998 * the PTE updates which happen during this window. Note that using this
999 * interface requires that read hazards be removed from the code. A read
1000 * hazard could result in the direct mode hypervisor case, since the actual
1001 * write to the page tables may not yet have taken place, so reads though
1002 * a raw PTE pointer after it has been modified are not guaranteed to be
1003 * up to date. This mode can only be entered and left under the protection of
1004 * the page table locks for all page tables which may be modified. In the UP
1005 * case, this is required so that preemption is disabled, and in the SMP case,
1006 * it must synchronize the delayed page table writes properly on other CPUs.
1007 */
1008 #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
1009 #define arch_enter_lazy_mmu_mode() do {} while (0)
1010 #define arch_leave_lazy_mmu_mode() do {} while (0)
1011 #define arch_flush_lazy_mmu_mode() do {} while (0)
1012 #endif
1013
1014 /*
1015 * A facility to provide batching of the reload of page tables and
1016 * other process state with the actual context switch code for
1017 * paravirtualized guests. By convention, only one of the batched
1018 * update (lazy) modes (CPU, MMU) should be active at any given time,
1019 * entry should never be nested, and entry and exits should always be
1020 * paired. This is for sanity of maintaining and reasoning about the
1021 * kernel code. In this case, the exit (end of the context switch) is
1022 * in architecture-specific code, and so doesn't need a generic
1023 * definition.
1024 */
1025 #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
1026 #define arch_start_context_switch(prev) do {} while (0)
1027 #endif
1028
1029 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
1030 #ifndef CONFIG_ARCH_ENABLE_THP_MIGRATION
pmd_swp_mksoft_dirty(pmd_t pmd)1031 static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
1032 {
1033 return pmd;
1034 }
1035
pmd_swp_soft_dirty(pmd_t pmd)1036 static inline int pmd_swp_soft_dirty(pmd_t pmd)
1037 {
1038 return 0;
1039 }
1040
pmd_swp_clear_soft_dirty(pmd_t pmd)1041 static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
1042 {
1043 return pmd;
1044 }
1045 #endif
1046 #else /* !CONFIG_HAVE_ARCH_SOFT_DIRTY */
pte_soft_dirty(pte_t pte)1047 static inline int pte_soft_dirty(pte_t pte)
1048 {
1049 return 0;
1050 }
1051
pmd_soft_dirty(pmd_t pmd)1052 static inline int pmd_soft_dirty(pmd_t pmd)
1053 {
1054 return 0;
1055 }
1056
pte_mksoft_dirty(pte_t pte)1057 static inline pte_t pte_mksoft_dirty(pte_t pte)
1058 {
1059 return pte;
1060 }
1061
pmd_mksoft_dirty(pmd_t pmd)1062 static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
1063 {
1064 return pmd;
1065 }
1066
pte_clear_soft_dirty(pte_t pte)1067 static inline pte_t pte_clear_soft_dirty(pte_t pte)
1068 {
1069 return pte;
1070 }
1071
pmd_clear_soft_dirty(pmd_t pmd)1072 static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
1073 {
1074 return pmd;
1075 }
1076
pte_swp_mksoft_dirty(pte_t pte)1077 static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
1078 {
1079 return pte;
1080 }
1081
pte_swp_soft_dirty(pte_t pte)1082 static inline int pte_swp_soft_dirty(pte_t pte)
1083 {
1084 return 0;
1085 }
1086
pte_swp_clear_soft_dirty(pte_t pte)1087 static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
1088 {
1089 return pte;
1090 }
1091
pmd_swp_mksoft_dirty(pmd_t pmd)1092 static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
1093 {
1094 return pmd;
1095 }
1096
pmd_swp_soft_dirty(pmd_t pmd)1097 static inline int pmd_swp_soft_dirty(pmd_t pmd)
1098 {
1099 return 0;
1100 }
1101
pmd_swp_clear_soft_dirty(pmd_t pmd)1102 static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
1103 {
1104 return pmd;
1105 }
1106 #endif
1107
1108 #ifndef __HAVE_PFNMAP_TRACKING
1109 /*
1110 * Interfaces that can be used by architecture code to keep track of
1111 * memory type of pfn mappings specified by the remap_pfn_range,
1112 * vmf_insert_pfn.
1113 */
1114
1115 /*
1116 * track_pfn_remap is called when a _new_ pfn mapping is being established
1117 * by remap_pfn_range() for physical range indicated by pfn and size.
1118 */
track_pfn_remap(struct vm_area_struct * vma,pgprot_t * prot,unsigned long pfn,unsigned long addr,unsigned long size)1119 static inline int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
1120 unsigned long pfn, unsigned long addr,
1121 unsigned long size)
1122 {
1123 return 0;
1124 }
1125
1126 /*
1127 * track_pfn_insert is called when a _new_ single pfn is established
1128 * by vmf_insert_pfn().
1129 */
track_pfn_insert(struct vm_area_struct * vma,pgprot_t * prot,pfn_t pfn)1130 static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
1131 pfn_t pfn)
1132 {
1133 }
1134
1135 /*
1136 * track_pfn_copy is called when vma that is covering the pfnmap gets
1137 * copied through copy_page_range().
1138 */
track_pfn_copy(struct vm_area_struct * vma)1139 static inline int track_pfn_copy(struct vm_area_struct *vma)
1140 {
1141 return 0;
1142 }
1143
1144 /*
1145 * untrack_pfn is called while unmapping a pfnmap for a region.
1146 * untrack can be called for a specific region indicated by pfn and size or
1147 * can be for the entire vma (in which case pfn, size are zero).
1148 */
untrack_pfn(struct vm_area_struct * vma,unsigned long pfn,unsigned long size)1149 static inline void untrack_pfn(struct vm_area_struct *vma,
1150 unsigned long pfn, unsigned long size)
1151 {
1152 }
1153
1154 /*
1155 * untrack_pfn_moved is called while mremapping a pfnmap for a new region.
1156 */
untrack_pfn_moved(struct vm_area_struct * vma)1157 static inline void untrack_pfn_moved(struct vm_area_struct *vma)
1158 {
1159 }
1160 #else
1161 extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
1162 unsigned long pfn, unsigned long addr,
1163 unsigned long size);
1164 extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
1165 pfn_t pfn);
1166 extern int track_pfn_copy(struct vm_area_struct *vma);
1167 extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
1168 unsigned long size);
1169 extern void untrack_pfn_moved(struct vm_area_struct *vma);
1170 #endif
1171
1172 #ifdef CONFIG_MMU
1173 #ifdef __HAVE_COLOR_ZERO_PAGE
is_zero_pfn(unsigned long pfn)1174 static inline int is_zero_pfn(unsigned long pfn)
1175 {
1176 extern unsigned long zero_pfn;
1177 unsigned long offset_from_zero_pfn = pfn - zero_pfn;
1178 return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
1179 }
1180
1181 #define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
1182
1183 #else
is_zero_pfn(unsigned long pfn)1184 static inline int is_zero_pfn(unsigned long pfn)
1185 {
1186 extern unsigned long zero_pfn;
1187 return pfn == zero_pfn;
1188 }
1189
my_zero_pfn(unsigned long addr)1190 static inline unsigned long my_zero_pfn(unsigned long addr)
1191 {
1192 extern unsigned long zero_pfn;
1193 return zero_pfn;
1194 }
1195 #endif
1196 #else
is_zero_pfn(unsigned long pfn)1197 static inline int is_zero_pfn(unsigned long pfn)
1198 {
1199 return 0;
1200 }
1201
my_zero_pfn(unsigned long addr)1202 static inline unsigned long my_zero_pfn(unsigned long addr)
1203 {
1204 return 0;
1205 }
1206 #endif /* CONFIG_MMU */
1207
1208 #ifdef CONFIG_MMU
1209
1210 #ifndef CONFIG_TRANSPARENT_HUGEPAGE
pmd_trans_huge(pmd_t pmd)1211 static inline int pmd_trans_huge(pmd_t pmd)
1212 {
1213 return 0;
1214 }
1215 #ifndef pmd_write
pmd_write(pmd_t pmd)1216 static inline int pmd_write(pmd_t pmd)
1217 {
1218 BUG();
1219 return 0;
1220 }
1221 #endif /* pmd_write */
1222 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1223
1224 #ifndef pud_write
pud_write(pud_t pud)1225 static inline int pud_write(pud_t pud)
1226 {
1227 BUG();
1228 return 0;
1229 }
1230 #endif /* pud_write */
1231
1232 #if !defined(CONFIG_ARCH_HAS_PTE_DEVMAP) || !defined(CONFIG_TRANSPARENT_HUGEPAGE)
pmd_devmap(pmd_t pmd)1233 static inline int pmd_devmap(pmd_t pmd)
1234 {
1235 return 0;
1236 }
pud_devmap(pud_t pud)1237 static inline int pud_devmap(pud_t pud)
1238 {
1239 return 0;
1240 }
pgd_devmap(pgd_t pgd)1241 static inline int pgd_devmap(pgd_t pgd)
1242 {
1243 return 0;
1244 }
1245 #endif
1246
1247 #if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
1248 (defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
1249 !defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
pud_trans_huge(pud_t pud)1250 static inline int pud_trans_huge(pud_t pud)
1251 {
1252 return 0;
1253 }
1254 #endif
1255
1256 /* See pmd_none_or_trans_huge_or_clear_bad for discussion. */
pud_none_or_trans_huge_or_dev_or_clear_bad(pud_t * pud)1257 static inline int pud_none_or_trans_huge_or_dev_or_clear_bad(pud_t *pud)
1258 {
1259 pud_t pudval = READ_ONCE(*pud);
1260
1261 if (pud_none(pudval) || pud_trans_huge(pudval) || pud_devmap(pudval))
1262 return 1;
1263 if (unlikely(pud_bad(pudval))) {
1264 pud_clear_bad(pud);
1265 return 1;
1266 }
1267 return 0;
1268 }
1269
1270 /* See pmd_trans_unstable for discussion. */
pud_trans_unstable(pud_t * pud)1271 static inline int pud_trans_unstable(pud_t *pud)
1272 {
1273 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
1274 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
1275 return pud_none_or_trans_huge_or_dev_or_clear_bad(pud);
1276 #else
1277 return 0;
1278 #endif
1279 }
1280
1281 #ifndef pmd_read_atomic
pmd_read_atomic(pmd_t * pmdp)1282 static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
1283 {
1284 /*
1285 * Depend on compiler for an atomic pmd read. NOTE: this is
1286 * only going to work, if the pmdval_t isn't larger than
1287 * an unsigned long.
1288 */
1289 return *pmdp;
1290 }
1291 #endif
1292
1293 #ifndef arch_needs_pgtable_deposit
1294 #define arch_needs_pgtable_deposit() (false)
1295 #endif
1296 /*
1297 * This function is meant to be used by sites walking pagetables with
1298 * the mmap_lock held in read mode to protect against MADV_DONTNEED and
1299 * transhuge page faults. MADV_DONTNEED can convert a transhuge pmd
1300 * into a null pmd and the transhuge page fault can convert a null pmd
1301 * into an hugepmd or into a regular pmd (if the hugepage allocation
1302 * fails). While holding the mmap_lock in read mode the pmd becomes
1303 * stable and stops changing under us only if it's not null and not a
1304 * transhuge pmd. When those races occurs and this function makes a
1305 * difference vs the standard pmd_none_or_clear_bad, the result is
1306 * undefined so behaving like if the pmd was none is safe (because it
1307 * can return none anyway). The compiler level barrier() is critically
1308 * important to compute the two checks atomically on the same pmdval.
1309 *
1310 * For 32bit kernels with a 64bit large pmd_t this automatically takes
1311 * care of reading the pmd atomically to avoid SMP race conditions
1312 * against pmd_populate() when the mmap_lock is hold for reading by the
1313 * caller (a special atomic read not done by "gcc" as in the generic
1314 * version above, is also needed when THP is disabled because the page
1315 * fault can populate the pmd from under us).
1316 */
pmd_none_or_trans_huge_or_clear_bad(pmd_t * pmd)1317 static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd)
1318 {
1319 pmd_t pmdval = pmd_read_atomic(pmd);
1320 /*
1321 * The barrier will stabilize the pmdval in a register or on
1322 * the stack so that it will stop changing under the code.
1323 *
1324 * When CONFIG_TRANSPARENT_HUGEPAGE=y on x86 32bit PAE,
1325 * pmd_read_atomic is allowed to return a not atomic pmdval
1326 * (for example pointing to an hugepage that has never been
1327 * mapped in the pmd). The below checks will only care about
1328 * the low part of the pmd with 32bit PAE x86 anyway, with the
1329 * exception of pmd_none(). So the important thing is that if
1330 * the low part of the pmd is found null, the high part will
1331 * be also null or the pmd_none() check below would be
1332 * confused.
1333 */
1334 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1335 barrier();
1336 #endif
1337 /*
1338 * !pmd_present() checks for pmd migration entries
1339 *
1340 * The complete check uses is_pmd_migration_entry() in linux/swapops.h
1341 * But using that requires moving current function and pmd_trans_unstable()
1342 * to linux/swapops.h to resolve dependency, which is too much code move.
1343 *
1344 * !pmd_present() is equivalent to is_pmd_migration_entry() currently,
1345 * because !pmd_present() pages can only be under migration not swapped
1346 * out.
1347 *
1348 * pmd_none() is preserved for future condition checks on pmd migration
1349 * entries and not confusing with this function name, although it is
1350 * redundant with !pmd_present().
1351 */
1352 if (pmd_none(pmdval) || pmd_trans_huge(pmdval) ||
1353 (IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION) && !pmd_present(pmdval)))
1354 return 1;
1355 if (unlikely(pmd_bad(pmdval))) {
1356 pmd_clear_bad(pmd);
1357 return 1;
1358 }
1359 return 0;
1360 }
1361
1362 /*
1363 * This is a noop if Transparent Hugepage Support is not built into
1364 * the kernel. Otherwise it is equivalent to
1365 * pmd_none_or_trans_huge_or_clear_bad(), and shall only be called in
1366 * places that already verified the pmd is not none and they want to
1367 * walk ptes while holding the mmap sem in read mode (write mode don't
1368 * need this). If THP is not enabled, the pmd can't go away under the
1369 * code even if MADV_DONTNEED runs, but if THP is enabled we need to
1370 * run a pmd_trans_unstable before walking the ptes after
1371 * split_huge_pmd returns (because it may have run when the pmd become
1372 * null, but then a page fault can map in a THP and not a regular page).
1373 */
pmd_trans_unstable(pmd_t * pmd)1374 static inline int pmd_trans_unstable(pmd_t *pmd)
1375 {
1376 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1377 return pmd_none_or_trans_huge_or_clear_bad(pmd);
1378 #else
1379 return 0;
1380 #endif
1381 }
1382
1383 /*
1384 * the ordering of these checks is important for pmds with _page_devmap set.
1385 * if we check pmd_trans_unstable() first we will trip the bad_pmd() check
1386 * inside of pmd_none_or_trans_huge_or_clear_bad(). this will end up correctly
1387 * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
1388 */
pmd_devmap_trans_unstable(pmd_t * pmd)1389 static inline int pmd_devmap_trans_unstable(pmd_t *pmd)
1390 {
1391 return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
1392 }
1393
1394 #ifndef CONFIG_NUMA_BALANCING
1395 /*
1396 * Technically a PTE can be PROTNONE even when not doing NUMA balancing but
1397 * the only case the kernel cares is for NUMA balancing and is only ever set
1398 * when the VMA is accessible. For PROT_NONE VMAs, the PTEs are not marked
1399 * _PAGE_PROTNONE so by default, implement the helper as "always no". It
1400 * is the responsibility of the caller to distinguish between PROT_NONE
1401 * protections and NUMA hinting fault protections.
1402 */
pte_protnone(pte_t pte)1403 static inline int pte_protnone(pte_t pte)
1404 {
1405 return 0;
1406 }
1407
pmd_protnone(pmd_t pmd)1408 static inline int pmd_protnone(pmd_t pmd)
1409 {
1410 return 0;
1411 }
1412 #endif /* CONFIG_NUMA_BALANCING */
1413
1414 #endif /* CONFIG_MMU */
1415
1416 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
1417
1418 #ifndef __PAGETABLE_P4D_FOLDED
1419 int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot);
1420 int p4d_clear_huge(p4d_t *p4d);
1421 #else
p4d_set_huge(p4d_t * p4d,phys_addr_t addr,pgprot_t prot)1422 static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
1423 {
1424 return 0;
1425 }
p4d_clear_huge(p4d_t * p4d)1426 static inline int p4d_clear_huge(p4d_t *p4d)
1427 {
1428 return 0;
1429 }
1430 #endif /* !__PAGETABLE_P4D_FOLDED */
1431
1432 int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot);
1433 int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot);
1434 int pud_clear_huge(pud_t *pud);
1435 int pmd_clear_huge(pmd_t *pmd);
1436 int p4d_free_pud_page(p4d_t *p4d, unsigned long addr);
1437 int pud_free_pmd_page(pud_t *pud, unsigned long addr);
1438 int pmd_free_pte_page(pmd_t *pmd, unsigned long addr);
1439 #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
p4d_set_huge(p4d_t * p4d,phys_addr_t addr,pgprot_t prot)1440 static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
1441 {
1442 return 0;
1443 }
pud_set_huge(pud_t * pud,phys_addr_t addr,pgprot_t prot)1444 static inline int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
1445 {
1446 return 0;
1447 }
pmd_set_huge(pmd_t * pmd,phys_addr_t addr,pgprot_t prot)1448 static inline int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
1449 {
1450 return 0;
1451 }
p4d_clear_huge(p4d_t * p4d)1452 static inline int p4d_clear_huge(p4d_t *p4d)
1453 {
1454 return 0;
1455 }
pud_clear_huge(pud_t * pud)1456 static inline int pud_clear_huge(pud_t *pud)
1457 {
1458 return 0;
1459 }
pmd_clear_huge(pmd_t * pmd)1460 static inline int pmd_clear_huge(pmd_t *pmd)
1461 {
1462 return 0;
1463 }
p4d_free_pud_page(p4d_t * p4d,unsigned long addr)1464 static inline int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
1465 {
1466 return 0;
1467 }
pud_free_pmd_page(pud_t * pud,unsigned long addr)1468 static inline int pud_free_pmd_page(pud_t *pud, unsigned long addr)
1469 {
1470 return 0;
1471 }
pmd_free_pte_page(pmd_t * pmd,unsigned long addr)1472 static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
1473 {
1474 return 0;
1475 }
1476 #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
1477
1478 #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
1479 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1480 /*
1481 * ARCHes with special requirements for evicting THP backing TLB entries can
1482 * implement this. Otherwise also, it can help optimize normal TLB flush in
1483 * THP regime. Stock flush_tlb_range() typically has optimization to nuke the
1484 * entire TLB if flush span is greater than a threshold, which will
1485 * likely be true for a single huge page. Thus a single THP flush will
1486 * invalidate the entire TLB which is not desirable.
1487 * e.g. see arch/arc: flush_pmd_tlb_range
1488 */
1489 #define flush_pmd_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1490 #define flush_pud_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1491 #else
1492 #define flush_pmd_tlb_range(vma, addr, end) BUILD_BUG()
1493 #define flush_pud_tlb_range(vma, addr, end) BUILD_BUG()
1494 #endif
1495 #endif
1496
1497 struct file;
1498 int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
1499 unsigned long size, pgprot_t *vma_prot);
1500
1501 #ifndef CONFIG_X86_ESPFIX64
init_espfix_bsp(void)1502 static inline void init_espfix_bsp(void) { }
1503 #endif
1504
1505 extern void __init pgtable_cache_init(void);
1506
1507 #ifndef __HAVE_ARCH_PFN_MODIFY_ALLOWED
pfn_modify_allowed(unsigned long pfn,pgprot_t prot)1508 static inline bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot)
1509 {
1510 return true;
1511 }
1512
arch_has_pfn_modify_check(void)1513 static inline bool arch_has_pfn_modify_check(void)
1514 {
1515 return false;
1516 }
1517 #endif /* !_HAVE_ARCH_PFN_MODIFY_ALLOWED */
1518
1519 /*
1520 * Architecture PAGE_KERNEL_* fallbacks
1521 *
1522 * Some architectures don't define certain PAGE_KERNEL_* flags. This is either
1523 * because they really don't support them, or the port needs to be updated to
1524 * reflect the required functionality. Below are a set of relatively safe
1525 * fallbacks, as best effort, which we can count on in lieu of the architectures
1526 * not defining them on their own yet.
1527 */
1528
1529 #ifndef PAGE_KERNEL_RO
1530 # define PAGE_KERNEL_RO PAGE_KERNEL
1531 #endif
1532
1533 #ifndef PAGE_KERNEL_EXEC
1534 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1535 #endif
1536
1537 /*
1538 * Page Table Modification bits for pgtbl_mod_mask.
1539 *
1540 * These are used by the p?d_alloc_track*() set of functions an in the generic
1541 * vmalloc/ioremap code to track at which page-table levels entries have been
1542 * modified. Based on that the code can better decide when vmalloc and ioremap
1543 * mapping changes need to be synchronized to other page-tables in the system.
1544 */
1545 #define __PGTBL_PGD_MODIFIED 0
1546 #define __PGTBL_P4D_MODIFIED 1
1547 #define __PGTBL_PUD_MODIFIED 2
1548 #define __PGTBL_PMD_MODIFIED 3
1549 #define __PGTBL_PTE_MODIFIED 4
1550
1551 #define PGTBL_PGD_MODIFIED BIT(__PGTBL_PGD_MODIFIED)
1552 #define PGTBL_P4D_MODIFIED BIT(__PGTBL_P4D_MODIFIED)
1553 #define PGTBL_PUD_MODIFIED BIT(__PGTBL_PUD_MODIFIED)
1554 #define PGTBL_PMD_MODIFIED BIT(__PGTBL_PMD_MODIFIED)
1555 #define PGTBL_PTE_MODIFIED BIT(__PGTBL_PTE_MODIFIED)
1556
1557 /* Page-Table Modification Mask */
1558 typedef unsigned int pgtbl_mod_mask;
1559
1560 #endif /* !__ASSEMBLY__ */
1561
1562 #if !defined(MAX_POSSIBLE_PHYSMEM_BITS) && !defined(CONFIG_64BIT)
1563 #ifdef CONFIG_PHYS_ADDR_T_64BIT
1564 /*
1565 * ZSMALLOC needs to know the highest PFN on 32-bit architectures
1566 * with physical address space extension, but falls back to
1567 * BITS_PER_LONG otherwise.
1568 */
1569 #error Missing MAX_POSSIBLE_PHYSMEM_BITS definition
1570 #else
1571 #define MAX_POSSIBLE_PHYSMEM_BITS 32
1572 #endif
1573 #endif
1574
1575 #ifndef has_transparent_hugepage
1576 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1577 #define has_transparent_hugepage() 1
1578 #else
1579 #define has_transparent_hugepage() 0
1580 #endif
1581 #endif
1582
1583 /*
1584 * On some architectures it depends on the mm if the p4d/pud or pmd
1585 * layer of the page table hierarchy is folded or not.
1586 */
1587 #ifndef mm_p4d_folded
1588 #define mm_p4d_folded(mm) __is_defined(__PAGETABLE_P4D_FOLDED)
1589 #endif
1590
1591 #ifndef mm_pud_folded
1592 #define mm_pud_folded(mm) __is_defined(__PAGETABLE_PUD_FOLDED)
1593 #endif
1594
1595 #ifndef mm_pmd_folded
1596 #define mm_pmd_folded(mm) __is_defined(__PAGETABLE_PMD_FOLDED)
1597 #endif
1598
1599 #ifndef p4d_offset_lockless
1600 #define p4d_offset_lockless(pgdp, pgd, address) p4d_offset(&(pgd), address)
1601 #endif
1602 #ifndef pud_offset_lockless
1603 #define pud_offset_lockless(p4dp, p4d, address) pud_offset(&(p4d), address)
1604 #endif
1605 #ifndef pmd_offset_lockless
1606 #define pmd_offset_lockless(pudp, pud, address) pmd_offset(&(pud), address)
1607 #endif
1608
1609 /*
1610 * p?d_leaf() - true if this entry is a final mapping to a physical address.
1611 * This differs from p?d_huge() by the fact that they are always available (if
1612 * the architecture supports large pages at the appropriate level) even
1613 * if CONFIG_HUGETLB_PAGE is not defined.
1614 * Only meaningful when called on a valid entry.
1615 */
1616 #ifndef pgd_leaf
1617 #define pgd_leaf(x) 0
1618 #endif
1619 #ifndef p4d_leaf
1620 #define p4d_leaf(x) 0
1621 #endif
1622 #ifndef pud_leaf
1623 #define pud_leaf(x) 0
1624 #endif
1625 #ifndef pmd_leaf
1626 #define pmd_leaf(x) 0
1627 #endif
1628
1629 #ifndef pgd_leaf_size
1630 #define pgd_leaf_size(x) (1ULL << PGDIR_SHIFT)
1631 #endif
1632 #ifndef p4d_leaf_size
1633 #define p4d_leaf_size(x) P4D_SIZE
1634 #endif
1635 #ifndef pud_leaf_size
1636 #define pud_leaf_size(x) PUD_SIZE
1637 #endif
1638 #ifndef pmd_leaf_size
1639 #define pmd_leaf_size(x) PMD_SIZE
1640 #endif
1641 #ifndef pte_leaf_size
1642 #define pte_leaf_size(x) PAGE_SIZE
1643 #endif
1644
1645 /*
1646 * Some architectures have MMUs that are configurable or selectable at boot
1647 * time. These lead to variable PTRS_PER_x. For statically allocated arrays it
1648 * helps to have a static maximum value.
1649 */
1650
1651 #ifndef MAX_PTRS_PER_PTE
1652 #define MAX_PTRS_PER_PTE PTRS_PER_PTE
1653 #endif
1654
1655 #ifndef MAX_PTRS_PER_PMD
1656 #define MAX_PTRS_PER_PMD PTRS_PER_PMD
1657 #endif
1658
1659 #ifndef MAX_PTRS_PER_PUD
1660 #define MAX_PTRS_PER_PUD PTRS_PER_PUD
1661 #endif
1662
1663 #ifndef MAX_PTRS_PER_P4D
1664 #define MAX_PTRS_PER_P4D PTRS_PER_P4D
1665 #endif
1666
1667 #endif /* _LINUX_PGTABLE_H */
1668