pgtable.h 22 KB

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