pgtable-3level.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_PGTABLE_3LEVEL_H
  3. #define _ASM_X86_PGTABLE_3LEVEL_H
  4. /*
  5. * Intel Physical Address Extension (PAE) Mode - three-level page
  6. * tables on PPro+ CPUs.
  7. *
  8. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  9. */
  10. #define pte_ERROR(e) \
  11. pr_err("%s:%d: bad pte %p(%08lx%08lx)\n", \
  12. __FILE__, __LINE__, &(e), (e).pte_high, (e).pte_low)
  13. #define pmd_ERROR(e) \
  14. pr_err("%s:%d: bad pmd %p(%016Lx)\n", \
  15. __FILE__, __LINE__, &(e), pmd_val(e))
  16. #define pgd_ERROR(e) \
  17. pr_err("%s:%d: bad pgd %p(%016Lx)\n", \
  18. __FILE__, __LINE__, &(e), pgd_val(e))
  19. /* Rules for using set_pte: the pte being assigned *must* be
  20. * either not present or in a state where the hardware will
  21. * not attempt to update the pte. In places where this is
  22. * not possible, use pte_get_and_clear to obtain the old pte
  23. * value and then use set_pte to update it. -ben
  24. */
  25. static inline void native_set_pte(pte_t *ptep, pte_t pte)
  26. {
  27. ptep->pte_high = pte.pte_high;
  28. smp_wmb();
  29. ptep->pte_low = pte.pte_low;
  30. }
  31. #define pmd_read_atomic pmd_read_atomic
  32. /*
  33. * pte_offset_map_lock on 32bit PAE kernels was reading the pmd_t with
  34. * a "*pmdp" dereference done by gcc. Problem is, in certain places
  35. * where pte_offset_map_lock is called, concurrent page faults are
  36. * allowed, if the mmap_sem is hold for reading. An example is mincore
  37. * vs page faults vs MADV_DONTNEED. On the page fault side
  38. * pmd_populate rightfully does a set_64bit, but if we're reading the
  39. * pmd_t with a "*pmdp" on the mincore side, a SMP race can happen
  40. * because gcc will not read the 64bit of the pmd atomically. To fix
  41. * this all places running pmd_offset_map_lock() while holding the
  42. * mmap_sem in read mode, shall read the pmdp pointer using this
  43. * function to know if the pmd is null nor not, and in turn to know if
  44. * they can run pmd_offset_map_lock or pmd_trans_huge or other pmd
  45. * operations.
  46. *
  47. * Without THP if the mmap_sem is hold for reading, the pmd can only
  48. * transition from null to not null while pmd_read_atomic runs. So
  49. * we can always return atomic pmd values with this function.
  50. *
  51. * With THP if the mmap_sem is hold for reading, the pmd can become
  52. * trans_huge or none or point to a pte (and in turn become "stable")
  53. * at any time under pmd_read_atomic. We could read it really
  54. * atomically here with a atomic64_read for the THP enabled case (and
  55. * it would be a whole lot simpler), but to avoid using cmpxchg8b we
  56. * only return an atomic pmdval if the low part of the pmdval is later
  57. * found stable (i.e. pointing to a pte). And we're returning a none
  58. * pmdval if the low part of the pmd is none. In some cases the high
  59. * and low part of the pmdval returned may not be consistent if THP is
  60. * enabled (the low part may point to previously mapped hugepage,
  61. * while the high part may point to a more recently mapped hugepage),
  62. * but pmd_none_or_trans_huge_or_clear_bad() only needs the low part
  63. * of the pmd to be read atomically to decide if the pmd is unstable
  64. * or not, with the only exception of when the low part of the pmd is
  65. * zero in which case we return a none pmd.
  66. */
  67. static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
  68. {
  69. pmdval_t ret;
  70. u32 *tmp = (u32 *)pmdp;
  71. ret = (pmdval_t) (*tmp);
  72. if (ret) {
  73. /*
  74. * If the low part is null, we must not read the high part
  75. * or we can end up with a partial pmd.
  76. */
  77. smp_rmb();
  78. ret |= ((pmdval_t)*(tmp + 1)) << 32;
  79. }
  80. return (pmd_t) { ret };
  81. }
  82. static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
  83. {
  84. set_64bit((unsigned long long *)(ptep), native_pte_val(pte));
  85. }
  86. static inline void native_set_pmd(pmd_t *pmdp, pmd_t pmd)
  87. {
  88. set_64bit((unsigned long long *)(pmdp), native_pmd_val(pmd));
  89. }
  90. static inline void native_set_pud(pud_t *pudp, pud_t pud)
  91. {
  92. set_64bit((unsigned long long *)(pudp), native_pud_val(pud));
  93. }
  94. /*
  95. * For PTEs and PDEs, we must clear the P-bit first when clearing a page table
  96. * entry, so clear the bottom half first and enforce ordering with a compiler
  97. * barrier.
  98. */
  99. static inline void native_pte_clear(struct mm_struct *mm, unsigned long addr,
  100. pte_t *ptep)
  101. {
  102. ptep->pte_low = 0;
  103. smp_wmb();
  104. ptep->pte_high = 0;
  105. }
  106. static inline void native_pmd_clear(pmd_t *pmd)
  107. {
  108. u32 *tmp = (u32 *)pmd;
  109. *tmp = 0;
  110. smp_wmb();
  111. *(tmp + 1) = 0;
  112. }
  113. static inline void native_pud_clear(pud_t *pudp)
  114. {
  115. }
  116. static inline void pud_clear(pud_t *pudp)
  117. {
  118. set_pud(pudp, __pud(0));
  119. /*
  120. * According to Intel App note "TLBs, Paging-Structure Caches,
  121. * and Their Invalidation", April 2007, document 317080-001,
  122. * section 8.1: in PAE mode we explicitly have to flush the
  123. * TLB via cr3 if the top-level pgd is changed...
  124. *
  125. * Currently all places where pud_clear() is called either have
  126. * flush_tlb_mm() followed or don't need TLB flush (x86_64 code or
  127. * pud_clear_bad()), so we don't need TLB flush here.
  128. */
  129. }
  130. #ifdef CONFIG_SMP
  131. static inline pte_t native_ptep_get_and_clear(pte_t *ptep)
  132. {
  133. pte_t res;
  134. /* xchg acts as a barrier before the setting of the high bits */
  135. res.pte_low = xchg(&ptep->pte_low, 0);
  136. res.pte_high = ptep->pte_high;
  137. ptep->pte_high = 0;
  138. return res;
  139. }
  140. #else
  141. #define native_ptep_get_and_clear(xp) native_local_ptep_get_and_clear(xp)
  142. #endif
  143. #ifdef CONFIG_SMP
  144. union split_pmd {
  145. struct {
  146. u32 pmd_low;
  147. u32 pmd_high;
  148. };
  149. pmd_t pmd;
  150. };
  151. static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)
  152. {
  153. union split_pmd res, *orig = (union split_pmd *)pmdp;
  154. /* xchg acts as a barrier before setting of the high bits */
  155. res.pmd_low = xchg(&orig->pmd_low, 0);
  156. res.pmd_high = orig->pmd_high;
  157. orig->pmd_high = 0;
  158. return res.pmd;
  159. }
  160. #else
  161. #define native_pmdp_get_and_clear(xp) native_local_pmdp_get_and_clear(xp)
  162. #endif
  163. #ifdef CONFIG_SMP
  164. union split_pud {
  165. struct {
  166. u32 pud_low;
  167. u32 pud_high;
  168. };
  169. pud_t pud;
  170. };
  171. static inline pud_t native_pudp_get_and_clear(pud_t *pudp)
  172. {
  173. union split_pud res, *orig = (union split_pud *)pudp;
  174. /* xchg acts as a barrier before setting of the high bits */
  175. res.pud_low = xchg(&orig->pud_low, 0);
  176. res.pud_high = orig->pud_high;
  177. orig->pud_high = 0;
  178. return res.pud;
  179. }
  180. #else
  181. #define native_pudp_get_and_clear(xp) native_local_pudp_get_and_clear(xp)
  182. #endif
  183. /* Encode and de-code a swap entry */
  184. #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > 5)
  185. #define __swp_type(x) (((x).val) & 0x1f)
  186. #define __swp_offset(x) ((x).val >> 5)
  187. #define __swp_entry(type, offset) ((swp_entry_t){(type) | (offset) << 5})
  188. #define __pte_to_swp_entry(pte) ((swp_entry_t){ (pte).pte_high })
  189. #define __swp_entry_to_pte(x) ((pte_t){ { .pte_high = (x).val } })
  190. #define gup_get_pte gup_get_pte
  191. /*
  192. * WARNING: only to be used in the get_user_pages_fast() implementation.
  193. *
  194. * With get_user_pages_fast(), we walk down the pagetables without taking
  195. * any locks. For this we would like to load the pointers atomically,
  196. * but that is not possible (without expensive cmpxchg8b) on PAE. What
  197. * we do have is the guarantee that a PTE will only either go from not
  198. * present to present, or present to not present or both -- it will not
  199. * switch to a completely different present page without a TLB flush in
  200. * between; something that we are blocking by holding interrupts off.
  201. *
  202. * Setting ptes from not present to present goes:
  203. *
  204. * ptep->pte_high = h;
  205. * smp_wmb();
  206. * ptep->pte_low = l;
  207. *
  208. * And present to not present goes:
  209. *
  210. * ptep->pte_low = 0;
  211. * smp_wmb();
  212. * ptep->pte_high = 0;
  213. *
  214. * We must ensure here that the load of pte_low sees 'l' iff pte_high
  215. * sees 'h'. We load pte_high *after* loading pte_low, which ensures we
  216. * don't see an older value of pte_high. *Then* we recheck pte_low,
  217. * which ensures that we haven't picked up a changed pte high. We might
  218. * have gotten rubbish values from pte_low and pte_high, but we are
  219. * guaranteed that pte_low will not have the present bit set *unless*
  220. * it is 'l'. Because get_user_pages_fast() only operates on present ptes
  221. * we're safe.
  222. */
  223. static inline pte_t gup_get_pte(pte_t *ptep)
  224. {
  225. pte_t pte;
  226. do {
  227. pte.pte_low = ptep->pte_low;
  228. smp_rmb();
  229. pte.pte_high = ptep->pte_high;
  230. smp_rmb();
  231. } while (unlikely(pte.pte_low != ptep->pte_low));
  232. return pte;
  233. }
  234. #endif /* _ASM_X86_PGTABLE_3LEVEL_H */