pgtable.h 22 KB

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