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