pgtable.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #ifndef __ASSEMBLY__
  4. #ifdef CONFIG_MMU
  5. #include <linux/mm_types.h>
  6. #include <linux/bug.h>
  7. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  8. extern int ptep_set_access_flags(struct vm_area_struct *vma,
  9. unsigned long address, pte_t *ptep,
  10. pte_t entry, int dirty);
  11. #endif
  12. #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  13. extern int pmdp_set_access_flags(struct vm_area_struct *vma,
  14. unsigned long address, pmd_t *pmdp,
  15. pmd_t entry, int dirty);
  16. #endif
  17. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  18. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  19. unsigned long address,
  20. pte_t *ptep)
  21. {
  22. pte_t pte = *ptep;
  23. int r = 1;
  24. if (!pte_young(pte))
  25. r = 0;
  26. else
  27. set_pte_at(vma->vm_mm, address, ptep, pte_mkold(pte));
  28. return r;
  29. }
  30. #endif
  31. #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
  32. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  33. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  34. unsigned long address,
  35. pmd_t *pmdp)
  36. {
  37. pmd_t pmd = *pmdp;
  38. int r = 1;
  39. if (!pmd_young(pmd))
  40. r = 0;
  41. else
  42. set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
  43. return r;
  44. }
  45. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  46. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  47. unsigned long address,
  48. pmd_t *pmdp)
  49. {
  50. BUG();
  51. return 0;
  52. }
  53. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  54. #endif
  55. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  56. int ptep_clear_flush_young(struct vm_area_struct *vma,
  57. unsigned long address, pte_t *ptep);
  58. #endif
  59. #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  60. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  61. unsigned long address, pmd_t *pmdp);
  62. #endif
  63. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  64. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  65. unsigned long address,
  66. pte_t *ptep)
  67. {
  68. pte_t pte = *ptep;
  69. pte_clear(mm, address, ptep);
  70. return pte;
  71. }
  72. #endif
  73. #ifndef __HAVE_ARCH_PMDP_GET_AND_CLEAR
  74. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  75. static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
  76. unsigned long address,
  77. pmd_t *pmdp)
  78. {
  79. pmd_t pmd = *pmdp;
  80. pmd_clear(mm, address, pmdp);
  81. return pmd;
  82. }
  83. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  84. #endif
  85. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  86. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  87. unsigned long address, pte_t *ptep,
  88. int full)
  89. {
  90. pte_t pte;
  91. pte = ptep_get_and_clear(mm, address, ptep);
  92. return pte;
  93. }
  94. #endif
  95. /*
  96. * Some architectures may be able to avoid expensive synchronization
  97. * primitives when modifications are made to PTE's which are already
  98. * not present, or in the process of an address space destruction.
  99. */
  100. #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
  101. static inline void pte_clear_not_present_full(struct mm_struct *mm,
  102. unsigned long address,
  103. pte_t *ptep,
  104. int full)
  105. {
  106. pte_clear(mm, address, ptep);
  107. }
  108. #endif
  109. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  110. extern pte_t ptep_clear_flush(struct vm_area_struct *vma,
  111. unsigned long address,
  112. pte_t *ptep);
  113. #endif
  114. #ifndef __HAVE_ARCH_PMDP_CLEAR_FLUSH
  115. extern pmd_t pmdp_clear_flush(struct vm_area_struct *vma,
  116. unsigned long address,
  117. pmd_t *pmdp);
  118. #endif
  119. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  120. struct mm_struct;
  121. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  122. {
  123. pte_t old_pte = *ptep;
  124. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  125. }
  126. #endif
  127. #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT
  128. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  129. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  130. unsigned long address, pmd_t *pmdp)
  131. {
  132. pmd_t old_pmd = *pmdp;
  133. set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
  134. }
  135. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  136. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  137. unsigned long address, pmd_t *pmdp)
  138. {
  139. BUG();
  140. }
  141. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  142. #endif
  143. #ifndef __HAVE_ARCH_PMDP_SPLITTING_FLUSH
  144. extern pmd_t pmdp_splitting_flush(struct vm_area_struct *vma,
  145. unsigned long address,
  146. pmd_t *pmdp);
  147. #endif
  148. #ifndef __HAVE_ARCH_PTE_SAME
  149. static inline int pte_same(pte_t pte_a, pte_t pte_b)
  150. {
  151. return pte_val(pte_a) == pte_val(pte_b);
  152. }
  153. #endif
  154. #ifndef __HAVE_ARCH_PMD_SAME
  155. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  156. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  157. {
  158. return pmd_val(pmd_a) == pmd_val(pmd_b);
  159. }
  160. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  161. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  162. {
  163. BUG();
  164. return 0;
  165. }
  166. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  167. #endif
  168. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  169. #define page_test_and_clear_dirty(pfn, mapped) (0)
  170. #endif
  171. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  172. #define pte_maybe_dirty(pte) pte_dirty(pte)
  173. #else
  174. #define pte_maybe_dirty(pte) (1)
  175. #endif
  176. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  177. #define page_test_and_clear_young(pfn) (0)
  178. #endif
  179. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  180. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  181. #endif
  182. #ifndef __HAVE_ARCH_MOVE_PTE
  183. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  184. #endif
  185. #ifndef flush_tlb_fix_spurious_fault
  186. #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
  187. #endif
  188. #ifndef pgprot_noncached
  189. #define pgprot_noncached(prot) (prot)
  190. #endif
  191. #ifndef pgprot_writecombine
  192. #define pgprot_writecombine pgprot_noncached
  193. #endif
  194. /*
  195. * When walking page tables, get the address of the next boundary,
  196. * or the end address of the range if that comes earlier. Although no
  197. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  198. */
  199. #define pgd_addr_end(addr, end) \
  200. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  201. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  202. })
  203. #ifndef pud_addr_end
  204. #define pud_addr_end(addr, end) \
  205. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  206. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  207. })
  208. #endif
  209. #ifndef pmd_addr_end
  210. #define pmd_addr_end(addr, end) \
  211. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  212. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  213. })
  214. #endif
  215. /*
  216. * When walking page tables, we usually want to skip any p?d_none entries;
  217. * and any p?d_bad entries - reporting the error before resetting to none.
  218. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  219. */
  220. void pgd_clear_bad(pgd_t *);
  221. void pud_clear_bad(pud_t *);
  222. void pmd_clear_bad(pmd_t *);
  223. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  224. {
  225. if (pgd_none(*pgd))
  226. return 1;
  227. if (unlikely(pgd_bad(*pgd))) {
  228. pgd_clear_bad(pgd);
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. static inline int pud_none_or_clear_bad(pud_t *pud)
  234. {
  235. if (pud_none(*pud))
  236. return 1;
  237. if (unlikely(pud_bad(*pud))) {
  238. pud_clear_bad(pud);
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  244. {
  245. if (pmd_none(*pmd))
  246. return 1;
  247. if (unlikely(pmd_bad(*pmd))) {
  248. pmd_clear_bad(pmd);
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
  254. unsigned long addr,
  255. pte_t *ptep)
  256. {
  257. /*
  258. * Get the current pte state, but zero it out to make it
  259. * non-present, preventing the hardware from asynchronously
  260. * updating it.
  261. */
  262. return ptep_get_and_clear(mm, addr, ptep);
  263. }
  264. static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
  265. unsigned long addr,
  266. pte_t *ptep, pte_t pte)
  267. {
  268. /*
  269. * The pte is non-present, so there's no hardware state to
  270. * preserve.
  271. */
  272. set_pte_at(mm, addr, ptep, pte);
  273. }
  274. #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  275. /*
  276. * Start a pte protection read-modify-write transaction, which
  277. * protects against asynchronous hardware modifications to the pte.
  278. * The intention is not to prevent the hardware from making pte
  279. * updates, but to prevent any updates it may make from being lost.
  280. *
  281. * This does not protect against other software modifications of the
  282. * pte; the appropriate pte lock must be held over the transation.
  283. *
  284. * Note that this interface is intended to be batchable, meaning that
  285. * ptep_modify_prot_commit may not actually update the pte, but merely
  286. * queue the update to be done at some later time. The update must be
  287. * actually committed before the pte lock is released, however.
  288. */
  289. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  290. unsigned long addr,
  291. pte_t *ptep)
  292. {
  293. return __ptep_modify_prot_start(mm, addr, ptep);
  294. }
  295. /*
  296. * Commit an update to a pte, leaving any hardware-controlled bits in
  297. * the PTE unmodified.
  298. */
  299. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  300. unsigned long addr,
  301. pte_t *ptep, pte_t pte)
  302. {
  303. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  304. }
  305. #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
  306. #endif /* CONFIG_MMU */
  307. /*
  308. * A facility to provide lazy MMU batching. This allows PTE updates and
  309. * page invalidations to be delayed until a call to leave lazy MMU mode
  310. * is issued. Some architectures may benefit from doing this, and it is
  311. * beneficial for both shadow and direct mode hypervisors, which may batch
  312. * the PTE updates which happen during this window. Note that using this
  313. * interface requires that read hazards be removed from the code. A read
  314. * hazard could result in the direct mode hypervisor case, since the actual
  315. * write to the page tables may not yet have taken place, so reads though
  316. * a raw PTE pointer after it has been modified are not guaranteed to be
  317. * up to date. This mode can only be entered and left under the protection of
  318. * the page table locks for all page tables which may be modified. In the UP
  319. * case, this is required so that preemption is disabled, and in the SMP case,
  320. * it must synchronize the delayed page table writes properly on other CPUs.
  321. */
  322. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  323. #define arch_enter_lazy_mmu_mode() do {} while (0)
  324. #define arch_leave_lazy_mmu_mode() do {} while (0)
  325. #define arch_flush_lazy_mmu_mode() do {} while (0)
  326. #endif
  327. /*
  328. * A facility to provide batching of the reload of page tables and
  329. * other process state with the actual context switch code for
  330. * paravirtualized guests. By convention, only one of the batched
  331. * update (lazy) modes (CPU, MMU) should be active at any given time,
  332. * entry should never be nested, and entry and exits should always be
  333. * paired. This is for sanity of maintaining and reasoning about the
  334. * kernel code. In this case, the exit (end of the context switch) is
  335. * in architecture-specific code, and so doesn't need a generic
  336. * definition.
  337. */
  338. #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
  339. #define arch_start_context_switch(prev) do {} while (0)
  340. #endif
  341. #ifndef __HAVE_PFNMAP_TRACKING
  342. /*
  343. * Interface that can be used by architecture code to keep track of
  344. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  345. *
  346. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  347. * for physical range indicated by pfn and size.
  348. */
  349. static inline int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  350. unsigned long pfn, unsigned long size)
  351. {
  352. return 0;
  353. }
  354. /*
  355. * Interface that can be used by architecture code to keep track of
  356. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  357. *
  358. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  359. * copied through copy_page_range().
  360. */
  361. static inline int track_pfn_vma_copy(struct vm_area_struct *vma)
  362. {
  363. return 0;
  364. }
  365. /*
  366. * Interface that can be used by architecture code to keep track of
  367. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  368. *
  369. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  370. * untrack can be called for a specific region indicated by pfn and size or
  371. * can be for the entire vma (in which case size can be zero).
  372. */
  373. static inline void untrack_pfn_vma(struct vm_area_struct *vma,
  374. unsigned long pfn, unsigned long size)
  375. {
  376. }
  377. #else
  378. extern int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  379. unsigned long pfn, unsigned long size);
  380. extern int track_pfn_vma_copy(struct vm_area_struct *vma);
  381. extern void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  382. unsigned long size);
  383. #endif
  384. #ifdef CONFIG_MMU
  385. #ifndef CONFIG_TRANSPARENT_HUGEPAGE
  386. static inline int pmd_trans_huge(pmd_t pmd)
  387. {
  388. return 0;
  389. }
  390. static inline int pmd_trans_splitting(pmd_t pmd)
  391. {
  392. return 0;
  393. }
  394. #ifndef __HAVE_ARCH_PMD_WRITE
  395. static inline int pmd_write(pmd_t pmd)
  396. {
  397. BUG();
  398. return 0;
  399. }
  400. #endif /* __HAVE_ARCH_PMD_WRITE */
  401. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  402. /*
  403. * This function is meant to be used by sites walking pagetables with
  404. * the mmap_sem hold in read mode to protect against MADV_DONTNEED and
  405. * transhuge page faults. MADV_DONTNEED can convert a transhuge pmd
  406. * into a null pmd and the transhuge page fault can convert a null pmd
  407. * into an hugepmd or into a regular pmd (if the hugepage allocation
  408. * fails). While holding the mmap_sem in read mode the pmd becomes
  409. * stable and stops changing under us only if it's not null and not a
  410. * transhuge pmd. When those races occurs and this function makes a
  411. * difference vs the standard pmd_none_or_clear_bad, the result is
  412. * undefined so behaving like if the pmd was none is safe (because it
  413. * can return none anyway). The compiler level barrier() is critically
  414. * important to compute the two checks atomically on the same pmdval.
  415. */
  416. static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd)
  417. {
  418. /* depend on compiler for an atomic pmd read */
  419. pmd_t pmdval = *pmd;
  420. /*
  421. * The barrier will stabilize the pmdval in a register or on
  422. * the stack so that it will stop changing under the code.
  423. */
  424. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  425. barrier();
  426. #endif
  427. if (pmd_none(pmdval))
  428. return 1;
  429. if (unlikely(pmd_bad(pmdval))) {
  430. if (!pmd_trans_huge(pmdval))
  431. pmd_clear_bad(pmd);
  432. return 1;
  433. }
  434. return 0;
  435. }
  436. /*
  437. * This is a noop if Transparent Hugepage Support is not built into
  438. * the kernel. Otherwise it is equivalent to
  439. * pmd_none_or_trans_huge_or_clear_bad(), and shall only be called in
  440. * places that already verified the pmd is not none and they want to
  441. * walk ptes while holding the mmap sem in read mode (write mode don't
  442. * need this). If THP is not enabled, the pmd can't go away under the
  443. * code even if MADV_DONTNEED runs, but if THP is enabled we need to
  444. * run a pmd_trans_unstable before walking the ptes after
  445. * split_huge_page_pmd returns (because it may have run when the pmd
  446. * become null, but then a page fault can map in a THP and not a
  447. * regular page).
  448. */
  449. static inline int pmd_trans_unstable(pmd_t *pmd)
  450. {
  451. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  452. return pmd_none_or_trans_huge_or_clear_bad(pmd);
  453. #else
  454. return 0;
  455. #endif
  456. }
  457. #endif /* CONFIG_MMU */
  458. #endif /* !__ASSEMBLY__ */
  459. #endif /* _ASM_GENERIC_PGTABLE_H */