pgtable.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #include <linux/pfn.h>
  4. #ifndef __ASSEMBLY__
  5. #ifdef CONFIG_MMU
  6. #include <linux/mm_types.h>
  7. #include <linux/bug.h>
  8. #include <linux/errno.h>
  9. #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
  10. defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
  11. #error CONFIG_PGTABLE_LEVELS is not consistent with __PAGETABLE_{P4D,PUD,PMD}_FOLDED
  12. #endif
  13. /*
  14. * On almost all architectures and configurations, 0 can be used as the
  15. * upper ceiling to free_pgtables(): on many architectures it has the same
  16. * effect as using TASK_SIZE. However, there is one configuration which
  17. * must impose a more careful limit, to avoid freeing kernel pgtables.
  18. */
  19. #ifndef USER_PGTABLES_CEILING
  20. #define USER_PGTABLES_CEILING 0UL
  21. #endif
  22. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  23. extern int ptep_set_access_flags(struct vm_area_struct *vma,
  24. unsigned long address, pte_t *ptep,
  25. pte_t entry, int dirty);
  26. #endif
  27. #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  28. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  29. extern int pmdp_set_access_flags(struct vm_area_struct *vma,
  30. unsigned long address, pmd_t *pmdp,
  31. pmd_t entry, int dirty);
  32. extern int pudp_set_access_flags(struct vm_area_struct *vma,
  33. unsigned long address, pud_t *pudp,
  34. pud_t entry, int dirty);
  35. #else
  36. static inline int pmdp_set_access_flags(struct vm_area_struct *vma,
  37. unsigned long address, pmd_t *pmdp,
  38. pmd_t entry, int dirty)
  39. {
  40. BUILD_BUG();
  41. return 0;
  42. }
  43. static inline int pudp_set_access_flags(struct vm_area_struct *vma,
  44. unsigned long address, pud_t *pudp,
  45. pud_t entry, int dirty)
  46. {
  47. BUILD_BUG();
  48. return 0;
  49. }
  50. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  51. #endif
  52. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  53. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  54. unsigned long address,
  55. pte_t *ptep)
  56. {
  57. pte_t pte = *ptep;
  58. int r = 1;
  59. if (!pte_young(pte))
  60. r = 0;
  61. else
  62. set_pte_at(vma->vm_mm, address, ptep, pte_mkold(pte));
  63. return r;
  64. }
  65. #endif
  66. #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
  67. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  68. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  69. unsigned long address,
  70. pmd_t *pmdp)
  71. {
  72. pmd_t pmd = *pmdp;
  73. int r = 1;
  74. if (!pmd_young(pmd))
  75. r = 0;
  76. else
  77. set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
  78. return r;
  79. }
  80. #else
  81. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  82. unsigned long address,
  83. pmd_t *pmdp)
  84. {
  85. BUILD_BUG();
  86. return 0;
  87. }
  88. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  89. #endif
  90. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  91. int ptep_clear_flush_young(struct vm_area_struct *vma,
  92. unsigned long address, pte_t *ptep);
  93. #endif
  94. #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  95. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  96. extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
  97. unsigned long address, pmd_t *pmdp);
  98. #else
  99. /*
  100. * Despite relevant to THP only, this API is called from generic rmap code
  101. * under PageTransHuge(), hence needs a dummy implementation for !THP
  102. */
  103. static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
  104. unsigned long address, pmd_t *pmdp)
  105. {
  106. BUILD_BUG();
  107. return 0;
  108. }
  109. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  110. #endif
  111. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  112. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  113. unsigned long address,
  114. pte_t *ptep)
  115. {
  116. pte_t pte = *ptep;
  117. pte_clear(mm, address, ptep);
  118. return pte;
  119. }
  120. #endif
  121. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  122. #ifndef __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
  123. static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
  124. unsigned long address,
  125. pmd_t *pmdp)
  126. {
  127. pmd_t pmd = *pmdp;
  128. pmd_clear(pmdp);
  129. return pmd;
  130. }
  131. #endif /* __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR */
  132. #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
  133. static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
  134. unsigned long address,
  135. pud_t *pudp)
  136. {
  137. pud_t pud = *pudp;
  138. pud_clear(pudp);
  139. return pud;
  140. }
  141. #endif /* __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR */
  142. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  143. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  144. #ifndef __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR_FULL
  145. static inline pmd_t pmdp_huge_get_and_clear_full(struct mm_struct *mm,
  146. unsigned long address, pmd_t *pmdp,
  147. int full)
  148. {
  149. return pmdp_huge_get_and_clear(mm, address, pmdp);
  150. }
  151. #endif
  152. #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR_FULL
  153. static inline pud_t pudp_huge_get_and_clear_full(struct mm_struct *mm,
  154. unsigned long address, pud_t *pudp,
  155. int full)
  156. {
  157. return pudp_huge_get_and_clear(mm, address, pudp);
  158. }
  159. #endif
  160. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  161. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  162. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  163. unsigned long address, pte_t *ptep,
  164. int full)
  165. {
  166. pte_t pte;
  167. pte = ptep_get_and_clear(mm, address, ptep);
  168. return pte;
  169. }
  170. #endif
  171. /*
  172. * Some architectures may be able to avoid expensive synchronization
  173. * primitives when modifications are made to PTE's which are already
  174. * not present, or in the process of an address space destruction.
  175. */
  176. #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
  177. static inline void pte_clear_not_present_full(struct mm_struct *mm,
  178. unsigned long address,
  179. pte_t *ptep,
  180. int full)
  181. {
  182. pte_clear(mm, address, ptep);
  183. }
  184. #endif
  185. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  186. extern pte_t ptep_clear_flush(struct vm_area_struct *vma,
  187. unsigned long address,
  188. pte_t *ptep);
  189. #endif
  190. #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
  191. extern pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma,
  192. unsigned long address,
  193. pmd_t *pmdp);
  194. extern pud_t pudp_huge_clear_flush(struct vm_area_struct *vma,
  195. unsigned long address,
  196. pud_t *pudp);
  197. #endif
  198. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  199. struct mm_struct;
  200. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  201. {
  202. pte_t old_pte = *ptep;
  203. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  204. }
  205. #endif
  206. #ifndef pte_savedwrite
  207. #define pte_savedwrite pte_write
  208. #endif
  209. #ifndef pte_mk_savedwrite
  210. #define pte_mk_savedwrite pte_mkwrite
  211. #endif
  212. #ifndef pte_clear_savedwrite
  213. #define pte_clear_savedwrite pte_wrprotect
  214. #endif
  215. #ifndef pmd_savedwrite
  216. #define pmd_savedwrite pmd_write
  217. #endif
  218. #ifndef pmd_mk_savedwrite
  219. #define pmd_mk_savedwrite pmd_mkwrite
  220. #endif
  221. #ifndef pmd_clear_savedwrite
  222. #define pmd_clear_savedwrite pmd_wrprotect
  223. #endif
  224. #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT
  225. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  226. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  227. unsigned long address, pmd_t *pmdp)
  228. {
  229. pmd_t old_pmd = *pmdp;
  230. set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
  231. }
  232. #else
  233. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  234. unsigned long address, pmd_t *pmdp)
  235. {
  236. BUILD_BUG();
  237. }
  238. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  239. #endif
  240. #ifndef __HAVE_ARCH_PUDP_SET_WRPROTECT
  241. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  242. static inline void pudp_set_wrprotect(struct mm_struct *mm,
  243. unsigned long address, pud_t *pudp)
  244. {
  245. pud_t old_pud = *pudp;
  246. set_pud_at(mm, address, pudp, pud_wrprotect(old_pud));
  247. }
  248. #else
  249. static inline void pudp_set_wrprotect(struct mm_struct *mm,
  250. unsigned long address, pud_t *pudp)
  251. {
  252. BUILD_BUG();
  253. }
  254. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  255. #endif
  256. #ifndef pmdp_collapse_flush
  257. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  258. extern pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
  259. unsigned long address, pmd_t *pmdp);
  260. #else
  261. static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
  262. unsigned long address,
  263. pmd_t *pmdp)
  264. {
  265. BUILD_BUG();
  266. return *pmdp;
  267. }
  268. #define pmdp_collapse_flush pmdp_collapse_flush
  269. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  270. #endif
  271. #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT
  272. extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  273. pgtable_t pgtable);
  274. #endif
  275. #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW
  276. extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
  277. #endif
  278. #ifndef __HAVE_ARCH_PMDP_INVALIDATE
  279. extern void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  280. pmd_t *pmdp);
  281. #endif
  282. #ifndef __HAVE_ARCH_PMDP_HUGE_SPLIT_PREPARE
  283. static inline void pmdp_huge_split_prepare(struct vm_area_struct *vma,
  284. unsigned long address, pmd_t *pmdp)
  285. {
  286. }
  287. #endif
  288. #ifndef __HAVE_ARCH_PTE_SAME
  289. static inline int pte_same(pte_t pte_a, pte_t pte_b)
  290. {
  291. return pte_val(pte_a) == pte_val(pte_b);
  292. }
  293. #endif
  294. #ifndef __HAVE_ARCH_PTE_UNUSED
  295. /*
  296. * Some architectures provide facilities to virtualization guests
  297. * so that they can flag allocated pages as unused. This allows the
  298. * host to transparently reclaim unused pages. This function returns
  299. * whether the pte's page is unused.
  300. */
  301. static inline int pte_unused(pte_t pte)
  302. {
  303. return 0;
  304. }
  305. #endif
  306. #ifndef __HAVE_ARCH_PMD_SAME
  307. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  308. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  309. {
  310. return pmd_val(pmd_a) == pmd_val(pmd_b);
  311. }
  312. static inline int pud_same(pud_t pud_a, pud_t pud_b)
  313. {
  314. return pud_val(pud_a) == pud_val(pud_b);
  315. }
  316. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  317. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  318. {
  319. BUILD_BUG();
  320. return 0;
  321. }
  322. static inline int pud_same(pud_t pud_a, pud_t pud_b)
  323. {
  324. BUILD_BUG();
  325. return 0;
  326. }
  327. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  328. #endif
  329. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  330. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  331. #endif
  332. #ifndef __HAVE_ARCH_MOVE_PTE
  333. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  334. #endif
  335. #ifndef pte_accessible
  336. # define pte_accessible(mm, pte) ((void)(pte), 1)
  337. #endif
  338. #ifndef flush_tlb_fix_spurious_fault
  339. #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
  340. #endif
  341. #ifndef pgprot_noncached
  342. #define pgprot_noncached(prot) (prot)
  343. #endif
  344. #ifndef pgprot_writecombine
  345. #define pgprot_writecombine pgprot_noncached
  346. #endif
  347. #ifndef pgprot_writethrough
  348. #define pgprot_writethrough pgprot_noncached
  349. #endif
  350. #ifndef pgprot_device
  351. #define pgprot_device pgprot_noncached
  352. #endif
  353. #ifndef pgprot_modify
  354. #define pgprot_modify pgprot_modify
  355. static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
  356. {
  357. if (pgprot_val(oldprot) == pgprot_val(pgprot_noncached(oldprot)))
  358. newprot = pgprot_noncached(newprot);
  359. if (pgprot_val(oldprot) == pgprot_val(pgprot_writecombine(oldprot)))
  360. newprot = pgprot_writecombine(newprot);
  361. if (pgprot_val(oldprot) == pgprot_val(pgprot_device(oldprot)))
  362. newprot = pgprot_device(newprot);
  363. return newprot;
  364. }
  365. #endif
  366. /*
  367. * When walking page tables, get the address of the next boundary,
  368. * or the end address of the range if that comes earlier. Although no
  369. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  370. */
  371. #define pgd_addr_end(addr, end) \
  372. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  373. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  374. })
  375. #ifndef p4d_addr_end
  376. #define p4d_addr_end(addr, end) \
  377. ({ unsigned long __boundary = ((addr) + P4D_SIZE) & P4D_MASK; \
  378. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  379. })
  380. #endif
  381. #ifndef pud_addr_end
  382. #define pud_addr_end(addr, end) \
  383. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  384. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  385. })
  386. #endif
  387. #ifndef pmd_addr_end
  388. #define pmd_addr_end(addr, end) \
  389. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  390. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  391. })
  392. #endif
  393. /*
  394. * When walking page tables, we usually want to skip any p?d_none entries;
  395. * and any p?d_bad entries - reporting the error before resetting to none.
  396. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  397. */
  398. void pgd_clear_bad(pgd_t *);
  399. void p4d_clear_bad(p4d_t *);
  400. void pud_clear_bad(pud_t *);
  401. void pmd_clear_bad(pmd_t *);
  402. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  403. {
  404. if (pgd_none(*pgd))
  405. return 1;
  406. if (unlikely(pgd_bad(*pgd))) {
  407. pgd_clear_bad(pgd);
  408. return 1;
  409. }
  410. return 0;
  411. }
  412. static inline int p4d_none_or_clear_bad(p4d_t *p4d)
  413. {
  414. if (p4d_none(*p4d))
  415. return 1;
  416. if (unlikely(p4d_bad(*p4d))) {
  417. p4d_clear_bad(p4d);
  418. return 1;
  419. }
  420. return 0;
  421. }
  422. static inline int pud_none_or_clear_bad(pud_t *pud)
  423. {
  424. if (pud_none(*pud))
  425. return 1;
  426. if (unlikely(pud_bad(*pud))) {
  427. pud_clear_bad(pud);
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  433. {
  434. if (pmd_none(*pmd))
  435. return 1;
  436. if (unlikely(pmd_bad(*pmd))) {
  437. pmd_clear_bad(pmd);
  438. return 1;
  439. }
  440. return 0;
  441. }
  442. static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
  443. unsigned long addr,
  444. pte_t *ptep)
  445. {
  446. /*
  447. * Get the current pte state, but zero it out to make it
  448. * non-present, preventing the hardware from asynchronously
  449. * updating it.
  450. */
  451. return ptep_get_and_clear(mm, addr, ptep);
  452. }
  453. static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
  454. unsigned long addr,
  455. pte_t *ptep, pte_t pte)
  456. {
  457. /*
  458. * The pte is non-present, so there's no hardware state to
  459. * preserve.
  460. */
  461. set_pte_at(mm, addr, ptep, pte);
  462. }
  463. #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  464. /*
  465. * Start a pte protection read-modify-write transaction, which
  466. * protects against asynchronous hardware modifications to the pte.
  467. * The intention is not to prevent the hardware from making pte
  468. * updates, but to prevent any updates it may make from being lost.
  469. *
  470. * This does not protect against other software modifications of the
  471. * pte; the appropriate pte lock must be held over the transation.
  472. *
  473. * Note that this interface is intended to be batchable, meaning that
  474. * ptep_modify_prot_commit may not actually update the pte, but merely
  475. * queue the update to be done at some later time. The update must be
  476. * actually committed before the pte lock is released, however.
  477. */
  478. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  479. unsigned long addr,
  480. pte_t *ptep)
  481. {
  482. return __ptep_modify_prot_start(mm, addr, ptep);
  483. }
  484. /*
  485. * Commit an update to a pte, leaving any hardware-controlled bits in
  486. * the PTE unmodified.
  487. */
  488. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  489. unsigned long addr,
  490. pte_t *ptep, pte_t pte)
  491. {
  492. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  493. }
  494. #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
  495. #endif /* CONFIG_MMU */
  496. /*
  497. * A facility to provide lazy MMU batching. This allows PTE updates and
  498. * page invalidations to be delayed until a call to leave lazy MMU mode
  499. * is issued. Some architectures may benefit from doing this, and it is
  500. * beneficial for both shadow and direct mode hypervisors, which may batch
  501. * the PTE updates which happen during this window. Note that using this
  502. * interface requires that read hazards be removed from the code. A read
  503. * hazard could result in the direct mode hypervisor case, since the actual
  504. * write to the page tables may not yet have taken place, so reads though
  505. * a raw PTE pointer after it has been modified are not guaranteed to be
  506. * up to date. This mode can only be entered and left under the protection of
  507. * the page table locks for all page tables which may be modified. In the UP
  508. * case, this is required so that preemption is disabled, and in the SMP case,
  509. * it must synchronize the delayed page table writes properly on other CPUs.
  510. */
  511. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  512. #define arch_enter_lazy_mmu_mode() do {} while (0)
  513. #define arch_leave_lazy_mmu_mode() do {} while (0)
  514. #define arch_flush_lazy_mmu_mode() do {} while (0)
  515. #endif
  516. /*
  517. * A facility to provide batching of the reload of page tables and
  518. * other process state with the actual context switch code for
  519. * paravirtualized guests. By convention, only one of the batched
  520. * update (lazy) modes (CPU, MMU) should be active at any given time,
  521. * entry should never be nested, and entry and exits should always be
  522. * paired. This is for sanity of maintaining and reasoning about the
  523. * kernel code. In this case, the exit (end of the context switch) is
  524. * in architecture-specific code, and so doesn't need a generic
  525. * definition.
  526. */
  527. #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
  528. #define arch_start_context_switch(prev) do {} while (0)
  529. #endif
  530. #ifndef CONFIG_HAVE_ARCH_SOFT_DIRTY
  531. static inline int pte_soft_dirty(pte_t pte)
  532. {
  533. return 0;
  534. }
  535. static inline int pmd_soft_dirty(pmd_t pmd)
  536. {
  537. return 0;
  538. }
  539. static inline pte_t pte_mksoft_dirty(pte_t pte)
  540. {
  541. return pte;
  542. }
  543. static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
  544. {
  545. return pmd;
  546. }
  547. static inline pte_t pte_clear_soft_dirty(pte_t pte)
  548. {
  549. return pte;
  550. }
  551. static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
  552. {
  553. return pmd;
  554. }
  555. static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
  556. {
  557. return pte;
  558. }
  559. static inline int pte_swp_soft_dirty(pte_t pte)
  560. {
  561. return 0;
  562. }
  563. static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
  564. {
  565. return pte;
  566. }
  567. #endif
  568. #ifndef __HAVE_PFNMAP_TRACKING
  569. /*
  570. * Interfaces that can be used by architecture code to keep track of
  571. * memory type of pfn mappings specified by the remap_pfn_range,
  572. * vm_insert_pfn.
  573. */
  574. /*
  575. * track_pfn_remap is called when a _new_ pfn mapping is being established
  576. * by remap_pfn_range() for physical range indicated by pfn and size.
  577. */
  578. static inline int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
  579. unsigned long pfn, unsigned long addr,
  580. unsigned long size)
  581. {
  582. return 0;
  583. }
  584. /*
  585. * track_pfn_insert is called when a _new_ single pfn is established
  586. * by vm_insert_pfn().
  587. */
  588. static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
  589. pfn_t pfn)
  590. {
  591. }
  592. /*
  593. * track_pfn_copy is called when vma that is covering the pfnmap gets
  594. * copied through copy_page_range().
  595. */
  596. static inline int track_pfn_copy(struct vm_area_struct *vma)
  597. {
  598. return 0;
  599. }
  600. /*
  601. * untrack_pfn is called while unmapping a pfnmap for a region.
  602. * untrack can be called for a specific region indicated by pfn and size or
  603. * can be for the entire vma (in which case pfn, size are zero).
  604. */
  605. static inline void untrack_pfn(struct vm_area_struct *vma,
  606. unsigned long pfn, unsigned long size)
  607. {
  608. }
  609. /*
  610. * untrack_pfn_moved is called while mremapping a pfnmap for a new region.
  611. */
  612. static inline void untrack_pfn_moved(struct vm_area_struct *vma)
  613. {
  614. }
  615. #else
  616. extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
  617. unsigned long pfn, unsigned long addr,
  618. unsigned long size);
  619. extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
  620. pfn_t pfn);
  621. extern int track_pfn_copy(struct vm_area_struct *vma);
  622. extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
  623. unsigned long size);
  624. extern void untrack_pfn_moved(struct vm_area_struct *vma);
  625. #endif
  626. #ifdef __HAVE_COLOR_ZERO_PAGE
  627. static inline int is_zero_pfn(unsigned long pfn)
  628. {
  629. extern unsigned long zero_pfn;
  630. unsigned long offset_from_zero_pfn = pfn - zero_pfn;
  631. return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
  632. }
  633. #define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
  634. #else
  635. static inline int is_zero_pfn(unsigned long pfn)
  636. {
  637. extern unsigned long zero_pfn;
  638. return pfn == zero_pfn;
  639. }
  640. static inline unsigned long my_zero_pfn(unsigned long addr)
  641. {
  642. extern unsigned long zero_pfn;
  643. return zero_pfn;
  644. }
  645. #endif
  646. #ifdef CONFIG_MMU
  647. #ifndef CONFIG_TRANSPARENT_HUGEPAGE
  648. static inline int pmd_trans_huge(pmd_t pmd)
  649. {
  650. return 0;
  651. }
  652. #ifndef __HAVE_ARCH_PMD_WRITE
  653. static inline int pmd_write(pmd_t pmd)
  654. {
  655. BUG();
  656. return 0;
  657. }
  658. #endif /* __HAVE_ARCH_PMD_WRITE */
  659. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  660. #if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
  661. (defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
  662. !defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
  663. static inline int pud_trans_huge(pud_t pud)
  664. {
  665. return 0;
  666. }
  667. #endif
  668. #ifndef pmd_read_atomic
  669. static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
  670. {
  671. /*
  672. * Depend on compiler for an atomic pmd read. NOTE: this is
  673. * only going to work, if the pmdval_t isn't larger than
  674. * an unsigned long.
  675. */
  676. return *pmdp;
  677. }
  678. #endif
  679. #ifndef arch_needs_pgtable_deposit
  680. #define arch_needs_pgtable_deposit() (false)
  681. #endif
  682. /*
  683. * This function is meant to be used by sites walking pagetables with
  684. * the mmap_sem hold in read mode to protect against MADV_DONTNEED and
  685. * transhuge page faults. MADV_DONTNEED can convert a transhuge pmd
  686. * into a null pmd and the transhuge page fault can convert a null pmd
  687. * into an hugepmd or into a regular pmd (if the hugepage allocation
  688. * fails). While holding the mmap_sem in read mode the pmd becomes
  689. * stable and stops changing under us only if it's not null and not a
  690. * transhuge pmd. When those races occurs and this function makes a
  691. * difference vs the standard pmd_none_or_clear_bad, the result is
  692. * undefined so behaving like if the pmd was none is safe (because it
  693. * can return none anyway). The compiler level barrier() is critically
  694. * important to compute the two checks atomically on the same pmdval.
  695. *
  696. * For 32bit kernels with a 64bit large pmd_t this automatically takes
  697. * care of reading the pmd atomically to avoid SMP race conditions
  698. * against pmd_populate() when the mmap_sem is hold for reading by the
  699. * caller (a special atomic read not done by "gcc" as in the generic
  700. * version above, is also needed when THP is disabled because the page
  701. * fault can populate the pmd from under us).
  702. */
  703. static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd)
  704. {
  705. pmd_t pmdval = pmd_read_atomic(pmd);
  706. /*
  707. * The barrier will stabilize the pmdval in a register or on
  708. * the stack so that it will stop changing under the code.
  709. *
  710. * When CONFIG_TRANSPARENT_HUGEPAGE=y on x86 32bit PAE,
  711. * pmd_read_atomic is allowed to return a not atomic pmdval
  712. * (for example pointing to an hugepage that has never been
  713. * mapped in the pmd). The below checks will only care about
  714. * the low part of the pmd with 32bit PAE x86 anyway, with the
  715. * exception of pmd_none(). So the important thing is that if
  716. * the low part of the pmd is found null, the high part will
  717. * be also null or the pmd_none() check below would be
  718. * confused.
  719. */
  720. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  721. barrier();
  722. #endif
  723. if (pmd_none(pmdval) || pmd_trans_huge(pmdval))
  724. return 1;
  725. if (unlikely(pmd_bad(pmdval))) {
  726. pmd_clear_bad(pmd);
  727. return 1;
  728. }
  729. return 0;
  730. }
  731. /*
  732. * This is a noop if Transparent Hugepage Support is not built into
  733. * the kernel. Otherwise it is equivalent to
  734. * pmd_none_or_trans_huge_or_clear_bad(), and shall only be called in
  735. * places that already verified the pmd is not none and they want to
  736. * walk ptes while holding the mmap sem in read mode (write mode don't
  737. * need this). If THP is not enabled, the pmd can't go away under the
  738. * code even if MADV_DONTNEED runs, but if THP is enabled we need to
  739. * run a pmd_trans_unstable before walking the ptes after
  740. * split_huge_page_pmd returns (because it may have run when the pmd
  741. * become null, but then a page fault can map in a THP and not a
  742. * regular page).
  743. */
  744. static inline int pmd_trans_unstable(pmd_t *pmd)
  745. {
  746. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  747. return pmd_none_or_trans_huge_or_clear_bad(pmd);
  748. #else
  749. return 0;
  750. #endif
  751. }
  752. #ifndef CONFIG_NUMA_BALANCING
  753. /*
  754. * Technically a PTE can be PROTNONE even when not doing NUMA balancing but
  755. * the only case the kernel cares is for NUMA balancing and is only ever set
  756. * when the VMA is accessible. For PROT_NONE VMAs, the PTEs are not marked
  757. * _PAGE_PROTNONE so by by default, implement the helper as "always no". It
  758. * is the responsibility of the caller to distinguish between PROT_NONE
  759. * protections and NUMA hinting fault protections.
  760. */
  761. static inline int pte_protnone(pte_t pte)
  762. {
  763. return 0;
  764. }
  765. static inline int pmd_protnone(pmd_t pmd)
  766. {
  767. return 0;
  768. }
  769. #endif /* CONFIG_NUMA_BALANCING */
  770. #endif /* CONFIG_MMU */
  771. #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
  772. #ifndef __PAGETABLE_P4D_FOLDED
  773. int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot);
  774. int p4d_clear_huge(p4d_t *p4d);
  775. #else
  776. static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
  777. {
  778. return 0;
  779. }
  780. static inline int p4d_clear_huge(p4d_t *p4d)
  781. {
  782. return 0;
  783. }
  784. #endif /* !__PAGETABLE_P4D_FOLDED */
  785. int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot);
  786. int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot);
  787. int pud_clear_huge(pud_t *pud);
  788. int pmd_clear_huge(pmd_t *pmd);
  789. #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
  790. static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
  791. {
  792. return 0;
  793. }
  794. static inline int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
  795. {
  796. return 0;
  797. }
  798. static inline int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
  799. {
  800. return 0;
  801. }
  802. static inline int p4d_clear_huge(p4d_t *p4d)
  803. {
  804. return 0;
  805. }
  806. static inline int pud_clear_huge(pud_t *pud)
  807. {
  808. return 0;
  809. }
  810. static inline int pmd_clear_huge(pmd_t *pmd)
  811. {
  812. return 0;
  813. }
  814. #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
  815. #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
  816. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  817. /*
  818. * ARCHes with special requirements for evicting THP backing TLB entries can
  819. * implement this. Otherwise also, it can help optimize normal TLB flush in
  820. * THP regime. stock flush_tlb_range() typically has optimization to nuke the
  821. * entire TLB TLB if flush span is greater than a threshold, which will
  822. * likely be true for a single huge page. Thus a single thp flush will
  823. * invalidate the entire TLB which is not desitable.
  824. * e.g. see arch/arc: flush_pmd_tlb_range
  825. */
  826. #define flush_pmd_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
  827. #define flush_pud_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
  828. #else
  829. #define flush_pmd_tlb_range(vma, addr, end) BUILD_BUG()
  830. #define flush_pud_tlb_range(vma, addr, end) BUILD_BUG()
  831. #endif
  832. #endif
  833. struct file;
  834. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  835. unsigned long size, pgprot_t *vma_prot);
  836. #endif /* !__ASSEMBLY__ */
  837. #ifndef io_remap_pfn_range
  838. #define io_remap_pfn_range remap_pfn_range
  839. #endif
  840. #ifndef has_transparent_hugepage
  841. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  842. #define has_transparent_hugepage() 1
  843. #else
  844. #define has_transparent_hugepage() 0
  845. #endif
  846. #endif
  847. #endif /* _ASM_GENERIC_PGTABLE_H */