pgtable.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 4 - defined(__PAGETABLE_PUD_FOLDED) - defined(__PAGETABLE_PMD_FOLDED) != \
  10. CONFIG_PGTABLE_LEVELS
  11. #error CONFIG_PGTABLE_LEVELS is not consistent with __PAGETABLE_{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 __HAVE_ARCH_PMDP_SET_WRPROTECT
  207. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  208. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  209. unsigned long address, pmd_t *pmdp)
  210. {
  211. pmd_t old_pmd = *pmdp;
  212. set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
  213. }
  214. #else
  215. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  216. unsigned long address, pmd_t *pmdp)
  217. {
  218. BUILD_BUG();
  219. }
  220. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  221. #endif
  222. #ifndef __HAVE_ARCH_PUDP_SET_WRPROTECT
  223. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  224. static inline void pudp_set_wrprotect(struct mm_struct *mm,
  225. unsigned long address, pud_t *pudp)
  226. {
  227. pud_t old_pud = *pudp;
  228. set_pud_at(mm, address, pudp, pud_wrprotect(old_pud));
  229. }
  230. #else
  231. static inline void pudp_set_wrprotect(struct mm_struct *mm,
  232. unsigned long address, pud_t *pudp)
  233. {
  234. BUILD_BUG();
  235. }
  236. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  237. #endif
  238. #ifndef pmdp_collapse_flush
  239. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  240. extern pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
  241. unsigned long address, pmd_t *pmdp);
  242. #else
  243. static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
  244. unsigned long address,
  245. pmd_t *pmdp)
  246. {
  247. BUILD_BUG();
  248. return *pmdp;
  249. }
  250. #define pmdp_collapse_flush pmdp_collapse_flush
  251. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  252. #endif
  253. #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT
  254. extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  255. pgtable_t pgtable);
  256. #endif
  257. #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW
  258. extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
  259. #endif
  260. #ifndef __HAVE_ARCH_PMDP_INVALIDATE
  261. extern void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  262. pmd_t *pmdp);
  263. #endif
  264. #ifndef __HAVE_ARCH_PMDP_HUGE_SPLIT_PREPARE
  265. static inline void pmdp_huge_split_prepare(struct vm_area_struct *vma,
  266. unsigned long address, pmd_t *pmdp)
  267. {
  268. }
  269. #endif
  270. #ifndef __HAVE_ARCH_PTE_SAME
  271. static inline int pte_same(pte_t pte_a, pte_t pte_b)
  272. {
  273. return pte_val(pte_a) == pte_val(pte_b);
  274. }
  275. #endif
  276. #ifndef __HAVE_ARCH_PTE_UNUSED
  277. /*
  278. * Some architectures provide facilities to virtualization guests
  279. * so that they can flag allocated pages as unused. This allows the
  280. * host to transparently reclaim unused pages. This function returns
  281. * whether the pte's page is unused.
  282. */
  283. static inline int pte_unused(pte_t pte)
  284. {
  285. return 0;
  286. }
  287. #endif
  288. #ifndef __HAVE_ARCH_PMD_SAME
  289. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  290. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  291. {
  292. return pmd_val(pmd_a) == pmd_val(pmd_b);
  293. }
  294. static inline int pud_same(pud_t pud_a, pud_t pud_b)
  295. {
  296. return pud_val(pud_a) == pud_val(pud_b);
  297. }
  298. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  299. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  300. {
  301. BUILD_BUG();
  302. return 0;
  303. }
  304. static inline int pud_same(pud_t pud_a, pud_t pud_b)
  305. {
  306. BUILD_BUG();
  307. return 0;
  308. }
  309. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  310. #endif
  311. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  312. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  313. #endif
  314. #ifndef __HAVE_ARCH_MOVE_PTE
  315. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  316. #endif
  317. #ifndef pte_accessible
  318. # define pte_accessible(mm, pte) ((void)(pte), 1)
  319. #endif
  320. #ifndef flush_tlb_fix_spurious_fault
  321. #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
  322. #endif
  323. #ifndef pgprot_noncached
  324. #define pgprot_noncached(prot) (prot)
  325. #endif
  326. #ifndef pgprot_writecombine
  327. #define pgprot_writecombine pgprot_noncached
  328. #endif
  329. #ifndef pgprot_writethrough
  330. #define pgprot_writethrough pgprot_noncached
  331. #endif
  332. #ifndef pgprot_device
  333. #define pgprot_device pgprot_noncached
  334. #endif
  335. #ifndef pgprot_modify
  336. #define pgprot_modify pgprot_modify
  337. static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
  338. {
  339. if (pgprot_val(oldprot) == pgprot_val(pgprot_noncached(oldprot)))
  340. newprot = pgprot_noncached(newprot);
  341. if (pgprot_val(oldprot) == pgprot_val(pgprot_writecombine(oldprot)))
  342. newprot = pgprot_writecombine(newprot);
  343. if (pgprot_val(oldprot) == pgprot_val(pgprot_device(oldprot)))
  344. newprot = pgprot_device(newprot);
  345. return newprot;
  346. }
  347. #endif
  348. /*
  349. * When walking page tables, get the address of the next boundary,
  350. * or the end address of the range if that comes earlier. Although no
  351. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  352. */
  353. #define pgd_addr_end(addr, end) \
  354. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  355. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  356. })
  357. #ifndef pud_addr_end
  358. #define pud_addr_end(addr, end) \
  359. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  360. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  361. })
  362. #endif
  363. #ifndef pmd_addr_end
  364. #define pmd_addr_end(addr, end) \
  365. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  366. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  367. })
  368. #endif
  369. /*
  370. * When walking page tables, we usually want to skip any p?d_none entries;
  371. * and any p?d_bad entries - reporting the error before resetting to none.
  372. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  373. */
  374. void pgd_clear_bad(pgd_t *);
  375. void pud_clear_bad(pud_t *);
  376. void pmd_clear_bad(pmd_t *);
  377. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  378. {
  379. if (pgd_none(*pgd))
  380. return 1;
  381. if (unlikely(pgd_bad(*pgd))) {
  382. pgd_clear_bad(pgd);
  383. return 1;
  384. }
  385. return 0;
  386. }
  387. static inline int pud_none_or_clear_bad(pud_t *pud)
  388. {
  389. if (pud_none(*pud))
  390. return 1;
  391. if (unlikely(pud_bad(*pud))) {
  392. pud_clear_bad(pud);
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  398. {
  399. if (pmd_none(*pmd))
  400. return 1;
  401. if (unlikely(pmd_bad(*pmd))) {
  402. pmd_clear_bad(pmd);
  403. return 1;
  404. }
  405. return 0;
  406. }
  407. static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
  408. unsigned long addr,
  409. pte_t *ptep)
  410. {
  411. /*
  412. * Get the current pte state, but zero it out to make it
  413. * non-present, preventing the hardware from asynchronously
  414. * updating it.
  415. */
  416. return ptep_get_and_clear(mm, addr, ptep);
  417. }
  418. static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
  419. unsigned long addr,
  420. pte_t *ptep, pte_t pte)
  421. {
  422. /*
  423. * The pte is non-present, so there's no hardware state to
  424. * preserve.
  425. */
  426. set_pte_at(mm, addr, ptep, pte);
  427. }
  428. #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  429. /*
  430. * Start a pte protection read-modify-write transaction, which
  431. * protects against asynchronous hardware modifications to the pte.
  432. * The intention is not to prevent the hardware from making pte
  433. * updates, but to prevent any updates it may make from being lost.
  434. *
  435. * This does not protect against other software modifications of the
  436. * pte; the appropriate pte lock must be held over the transation.
  437. *
  438. * Note that this interface is intended to be batchable, meaning that
  439. * ptep_modify_prot_commit may not actually update the pte, but merely
  440. * queue the update to be done at some later time. The update must be
  441. * actually committed before the pte lock is released, however.
  442. */
  443. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  444. unsigned long addr,
  445. pte_t *ptep)
  446. {
  447. return __ptep_modify_prot_start(mm, addr, ptep);
  448. }
  449. /*
  450. * Commit an update to a pte, leaving any hardware-controlled bits in
  451. * the PTE unmodified.
  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. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  458. }
  459. #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
  460. #endif /* CONFIG_MMU */
  461. /*
  462. * A facility to provide lazy MMU batching. This allows PTE updates and
  463. * page invalidations to be delayed until a call to leave lazy MMU mode
  464. * is issued. Some architectures may benefit from doing this, and it is
  465. * beneficial for both shadow and direct mode hypervisors, which may batch
  466. * the PTE updates which happen during this window. Note that using this
  467. * interface requires that read hazards be removed from the code. A read
  468. * hazard could result in the direct mode hypervisor case, since the actual
  469. * write to the page tables may not yet have taken place, so reads though
  470. * a raw PTE pointer after it has been modified are not guaranteed to be
  471. * up to date. This mode can only be entered and left under the protection of
  472. * the page table locks for all page tables which may be modified. In the UP
  473. * case, this is required so that preemption is disabled, and in the SMP case,
  474. * it must synchronize the delayed page table writes properly on other CPUs.
  475. */
  476. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  477. #define arch_enter_lazy_mmu_mode() do {} while (0)
  478. #define arch_leave_lazy_mmu_mode() do {} while (0)
  479. #define arch_flush_lazy_mmu_mode() do {} while (0)
  480. #endif
  481. /*
  482. * A facility to provide batching of the reload of page tables and
  483. * other process state with the actual context switch code for
  484. * paravirtualized guests. By convention, only one of the batched
  485. * update (lazy) modes (CPU, MMU) should be active at any given time,
  486. * entry should never be nested, and entry and exits should always be
  487. * paired. This is for sanity of maintaining and reasoning about the
  488. * kernel code. In this case, the exit (end of the context switch) is
  489. * in architecture-specific code, and so doesn't need a generic
  490. * definition.
  491. */
  492. #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
  493. #define arch_start_context_switch(prev) do {} while (0)
  494. #endif
  495. #ifndef CONFIG_HAVE_ARCH_SOFT_DIRTY
  496. static inline int pte_soft_dirty(pte_t pte)
  497. {
  498. return 0;
  499. }
  500. static inline int pmd_soft_dirty(pmd_t pmd)
  501. {
  502. return 0;
  503. }
  504. static inline pte_t pte_mksoft_dirty(pte_t pte)
  505. {
  506. return pte;
  507. }
  508. static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
  509. {
  510. return pmd;
  511. }
  512. static inline pte_t pte_clear_soft_dirty(pte_t pte)
  513. {
  514. return pte;
  515. }
  516. static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
  517. {
  518. return pmd;
  519. }
  520. static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
  521. {
  522. return pte;
  523. }
  524. static inline int pte_swp_soft_dirty(pte_t pte)
  525. {
  526. return 0;
  527. }
  528. static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
  529. {
  530. return pte;
  531. }
  532. #endif
  533. #ifndef __HAVE_PFNMAP_TRACKING
  534. /*
  535. * Interfaces that can be used by architecture code to keep track of
  536. * memory type of pfn mappings specified by the remap_pfn_range,
  537. * vm_insert_pfn.
  538. */
  539. /*
  540. * track_pfn_remap is called when a _new_ pfn mapping is being established
  541. * by remap_pfn_range() for physical range indicated by pfn and size.
  542. */
  543. static inline int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
  544. unsigned long pfn, unsigned long addr,
  545. unsigned long size)
  546. {
  547. return 0;
  548. }
  549. /*
  550. * track_pfn_insert is called when a _new_ single pfn is established
  551. * by vm_insert_pfn().
  552. */
  553. static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
  554. pfn_t pfn)
  555. {
  556. }
  557. /*
  558. * track_pfn_copy is called when vma that is covering the pfnmap gets
  559. * copied through copy_page_range().
  560. */
  561. static inline int track_pfn_copy(struct vm_area_struct *vma)
  562. {
  563. return 0;
  564. }
  565. /*
  566. * untrack_pfn is called while unmapping a pfnmap for a region.
  567. * untrack can be called for a specific region indicated by pfn and size or
  568. * can be for the entire vma (in which case pfn, size are zero).
  569. */
  570. static inline void untrack_pfn(struct vm_area_struct *vma,
  571. unsigned long pfn, unsigned long size)
  572. {
  573. }
  574. /*
  575. * untrack_pfn_moved is called while mremapping a pfnmap for a new region.
  576. */
  577. static inline void untrack_pfn_moved(struct vm_area_struct *vma)
  578. {
  579. }
  580. #else
  581. extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
  582. unsigned long pfn, unsigned long addr,
  583. unsigned long size);
  584. extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
  585. pfn_t pfn);
  586. extern int track_pfn_copy(struct vm_area_struct *vma);
  587. extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
  588. unsigned long size);
  589. extern void untrack_pfn_moved(struct vm_area_struct *vma);
  590. #endif
  591. #ifdef __HAVE_COLOR_ZERO_PAGE
  592. static inline int is_zero_pfn(unsigned long pfn)
  593. {
  594. extern unsigned long zero_pfn;
  595. unsigned long offset_from_zero_pfn = pfn - zero_pfn;
  596. return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
  597. }
  598. #define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
  599. #else
  600. static inline int is_zero_pfn(unsigned long pfn)
  601. {
  602. extern unsigned long zero_pfn;
  603. return pfn == zero_pfn;
  604. }
  605. static inline unsigned long my_zero_pfn(unsigned long addr)
  606. {
  607. extern unsigned long zero_pfn;
  608. return zero_pfn;
  609. }
  610. #endif
  611. #ifdef CONFIG_MMU
  612. #ifndef CONFIG_TRANSPARENT_HUGEPAGE
  613. static inline int pmd_trans_huge(pmd_t pmd)
  614. {
  615. return 0;
  616. }
  617. #ifndef __HAVE_ARCH_PMD_WRITE
  618. static inline int pmd_write(pmd_t pmd)
  619. {
  620. BUG();
  621. return 0;
  622. }
  623. #endif /* __HAVE_ARCH_PMD_WRITE */
  624. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  625. #if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
  626. (defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
  627. !defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
  628. static inline int pud_trans_huge(pud_t pud)
  629. {
  630. return 0;
  631. }
  632. #endif
  633. #ifndef pmd_read_atomic
  634. static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
  635. {
  636. /*
  637. * Depend on compiler for an atomic pmd read. NOTE: this is
  638. * only going to work, if the pmdval_t isn't larger than
  639. * an unsigned long.
  640. */
  641. return *pmdp;
  642. }
  643. #endif
  644. #ifndef arch_needs_pgtable_deposit
  645. #define arch_needs_pgtable_deposit() (false)
  646. #endif
  647. /*
  648. * This function is meant to be used by sites walking pagetables with
  649. * the mmap_sem hold in read mode to protect against MADV_DONTNEED and
  650. * transhuge page faults. MADV_DONTNEED can convert a transhuge pmd
  651. * into a null pmd and the transhuge page fault can convert a null pmd
  652. * into an hugepmd or into a regular pmd (if the hugepage allocation
  653. * fails). While holding the mmap_sem in read mode the pmd becomes
  654. * stable and stops changing under us only if it's not null and not a
  655. * transhuge pmd. When those races occurs and this function makes a
  656. * difference vs the standard pmd_none_or_clear_bad, the result is
  657. * undefined so behaving like if the pmd was none is safe (because it
  658. * can return none anyway). The compiler level barrier() is critically
  659. * important to compute the two checks atomically on the same pmdval.
  660. *
  661. * For 32bit kernels with a 64bit large pmd_t this automatically takes
  662. * care of reading the pmd atomically to avoid SMP race conditions
  663. * against pmd_populate() when the mmap_sem is hold for reading by the
  664. * caller (a special atomic read not done by "gcc" as in the generic
  665. * version above, is also needed when THP is disabled because the page
  666. * fault can populate the pmd from under us).
  667. */
  668. static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd)
  669. {
  670. pmd_t pmdval = pmd_read_atomic(pmd);
  671. /*
  672. * The barrier will stabilize the pmdval in a register or on
  673. * the stack so that it will stop changing under the code.
  674. *
  675. * When CONFIG_TRANSPARENT_HUGEPAGE=y on x86 32bit PAE,
  676. * pmd_read_atomic is allowed to return a not atomic pmdval
  677. * (for example pointing to an hugepage that has never been
  678. * mapped in the pmd). The below checks will only care about
  679. * the low part of the pmd with 32bit PAE x86 anyway, with the
  680. * exception of pmd_none(). So the important thing is that if
  681. * the low part of the pmd is found null, the high part will
  682. * be also null or the pmd_none() check below would be
  683. * confused.
  684. */
  685. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  686. barrier();
  687. #endif
  688. if (pmd_none(pmdval) || pmd_trans_huge(pmdval))
  689. return 1;
  690. if (unlikely(pmd_bad(pmdval))) {
  691. pmd_clear_bad(pmd);
  692. return 1;
  693. }
  694. return 0;
  695. }
  696. /*
  697. * This is a noop if Transparent Hugepage Support is not built into
  698. * the kernel. Otherwise it is equivalent to
  699. * pmd_none_or_trans_huge_or_clear_bad(), and shall only be called in
  700. * places that already verified the pmd is not none and they want to
  701. * walk ptes while holding the mmap sem in read mode (write mode don't
  702. * need this). If THP is not enabled, the pmd can't go away under the
  703. * code even if MADV_DONTNEED runs, but if THP is enabled we need to
  704. * run a pmd_trans_unstable before walking the ptes after
  705. * split_huge_page_pmd returns (because it may have run when the pmd
  706. * become null, but then a page fault can map in a THP and not a
  707. * regular page).
  708. */
  709. static inline int pmd_trans_unstable(pmd_t *pmd)
  710. {
  711. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  712. return pmd_none_or_trans_huge_or_clear_bad(pmd);
  713. #else
  714. return 0;
  715. #endif
  716. }
  717. #ifndef CONFIG_NUMA_BALANCING
  718. /*
  719. * Technically a PTE can be PROTNONE even when not doing NUMA balancing but
  720. * the only case the kernel cares is for NUMA balancing and is only ever set
  721. * when the VMA is accessible. For PROT_NONE VMAs, the PTEs are not marked
  722. * _PAGE_PROTNONE so by by default, implement the helper as "always no". It
  723. * is the responsibility of the caller to distinguish between PROT_NONE
  724. * protections and NUMA hinting fault protections.
  725. */
  726. static inline int pte_protnone(pte_t pte)
  727. {
  728. return 0;
  729. }
  730. static inline int pmd_protnone(pmd_t pmd)
  731. {
  732. return 0;
  733. }
  734. #endif /* CONFIG_NUMA_BALANCING */
  735. #endif /* CONFIG_MMU */
  736. #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
  737. int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot);
  738. int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot);
  739. int pud_clear_huge(pud_t *pud);
  740. int pmd_clear_huge(pmd_t *pmd);
  741. #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
  742. static inline int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
  743. {
  744. return 0;
  745. }
  746. static inline int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
  747. {
  748. return 0;
  749. }
  750. static inline int pud_clear_huge(pud_t *pud)
  751. {
  752. return 0;
  753. }
  754. static inline int pmd_clear_huge(pmd_t *pmd)
  755. {
  756. return 0;
  757. }
  758. #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
  759. #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
  760. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  761. /*
  762. * ARCHes with special requirements for evicting THP backing TLB entries can
  763. * implement this. Otherwise also, it can help optimize normal TLB flush in
  764. * THP regime. stock flush_tlb_range() typically has optimization to nuke the
  765. * entire TLB TLB if flush span is greater than a threshold, which will
  766. * likely be true for a single huge page. Thus a single thp flush will
  767. * invalidate the entire TLB which is not desitable.
  768. * e.g. see arch/arc: flush_pmd_tlb_range
  769. */
  770. #define flush_pmd_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
  771. #define flush_pud_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
  772. #else
  773. #define flush_pmd_tlb_range(vma, addr, end) BUILD_BUG()
  774. #define flush_pud_tlb_range(vma, addr, end) BUILD_BUG()
  775. #endif
  776. #endif
  777. struct file;
  778. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  779. unsigned long size, pgprot_t *vma_prot);
  780. #endif /* !__ASSEMBLY__ */
  781. #ifndef io_remap_pfn_range
  782. #define io_remap_pfn_range remap_pfn_range
  783. #endif
  784. #ifndef has_transparent_hugepage
  785. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  786. #define has_transparent_hugepage() 1
  787. #else
  788. #define has_transparent_hugepage() 0
  789. #endif
  790. #endif
  791. #endif /* _ASM_GENERIC_PGTABLE_H */