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