pgtable.h 41 KB

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