pgtable.h 23 KB

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