pgtable.h 22 KB

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