pgtable-generic.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mm/pgtable-generic.c
  4. *
  5. * Generic pgtable methods declared in linux/pgtable.h
  6. *
  7. * Copyright (C) 2010 Linus Torvalds
  8. */
  9. #include <linux/pagemap.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/pgtable.h>
  12. #include <linux/mm_inline.h>
  13. #include <asm/tlb.h>
  14. /*
  15. * If a p?d_bad entry is found while walking page tables, report
  16. * the error, before resetting entry to p?d_none. Usually (but
  17. * very seldom) called out from the p?d_none_or_clear_bad macros.
  18. */
  19. void pgd_clear_bad(pgd_t *pgd)
  20. {
  21. pgd_ERROR(*pgd);
  22. pgd_clear(pgd);
  23. }
  24. #ifndef __PAGETABLE_P4D_FOLDED
  25. void p4d_clear_bad(p4d_t *p4d)
  26. {
  27. p4d_ERROR(*p4d);
  28. p4d_clear(p4d);
  29. }
  30. #endif
  31. #ifndef __PAGETABLE_PUD_FOLDED
  32. void pud_clear_bad(pud_t *pud)
  33. {
  34. pud_ERROR(*pud);
  35. pud_clear(pud);
  36. }
  37. #endif
  38. /*
  39. * Note that the pmd variant below can't be stub'ed out just as for p4d/pud
  40. * above. pmd folding is special and typically pmd_* macros refer to upper
  41. * level even when folded
  42. */
  43. void pmd_clear_bad(pmd_t *pmd)
  44. {
  45. pmd_ERROR(*pmd);
  46. pmd_clear(pmd);
  47. }
  48. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  49. /*
  50. * Only sets the access flags (dirty, accessed), as well as write
  51. * permission. Furthermore, we know it always gets set to a "more
  52. * permissive" setting, which allows most architectures to optimize
  53. * this. We return whether the PTE actually changed, which in turn
  54. * instructs the caller to do things like update__mmu_cache. This
  55. * used to be done in the caller, but sparc needs minor faults to
  56. * force that call on sun4c so we changed this macro slightly
  57. */
  58. int ptep_set_access_flags(struct vm_area_struct *vma,
  59. unsigned long address, pte_t *ptep,
  60. pte_t entry, int dirty)
  61. {
  62. int changed = !pte_same(*ptep, entry);
  63. if (changed) {
  64. set_pte_at(vma->vm_mm, address, ptep, entry);
  65. flush_tlb_fix_spurious_fault(vma, address);
  66. }
  67. return changed;
  68. }
  69. #endif
  70. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  71. int ptep_clear_flush_young(struct vm_area_struct *vma,
  72. unsigned long address, pte_t *ptep)
  73. {
  74. int young;
  75. young = ptep_test_and_clear_young(vma, address, ptep);
  76. if (young)
  77. flush_tlb_page(vma, address);
  78. return young;
  79. }
  80. #endif
  81. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  82. pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address,
  83. pte_t *ptep)
  84. {
  85. struct mm_struct *mm = (vma)->vm_mm;
  86. pte_t pte;
  87. pte = ptep_get_and_clear(mm, address, ptep);
  88. if (pte_accessible(mm, pte))
  89. flush_tlb_page(vma, address);
  90. return pte;
  91. }
  92. #endif
  93. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  94. #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  95. int pmdp_set_access_flags(struct vm_area_struct *vma,
  96. unsigned long address, pmd_t *pmdp,
  97. pmd_t entry, int dirty)
  98. {
  99. int changed = !pmd_same(*pmdp, entry);
  100. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  101. if (changed) {
  102. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  103. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  104. }
  105. return changed;
  106. }
  107. #endif
  108. #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  109. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  110. unsigned long address, pmd_t *pmdp)
  111. {
  112. int young;
  113. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  114. young = pmdp_test_and_clear_young(vma, address, pmdp);
  115. if (young)
  116. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  117. return young;
  118. }
  119. #endif
  120. #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
  121. pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
  122. pmd_t *pmdp)
  123. {
  124. pmd_t pmd;
  125. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  126. VM_BUG_ON(pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) &&
  127. !pmd_devmap(*pmdp));
  128. pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
  129. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  130. return pmd;
  131. }
  132. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  133. pud_t pudp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
  134. pud_t *pudp)
  135. {
  136. pud_t pud;
  137. VM_BUG_ON(address & ~HPAGE_PUD_MASK);
  138. VM_BUG_ON(!pud_trans_huge(*pudp) && !pud_devmap(*pudp));
  139. pud = pudp_huge_get_and_clear(vma->vm_mm, address, pudp);
  140. flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE);
  141. return pud;
  142. }
  143. #endif
  144. #endif
  145. #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT
  146. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  147. pgtable_t pgtable)
  148. {
  149. assert_spin_locked(pmd_lockptr(mm, pmdp));
  150. /* FIFO */
  151. if (!pmd_huge_pte(mm, pmdp))
  152. INIT_LIST_HEAD(&pgtable->lru);
  153. else
  154. list_add(&pgtable->lru, &pmd_huge_pte(mm, pmdp)->lru);
  155. pmd_huge_pte(mm, pmdp) = pgtable;
  156. }
  157. #endif
  158. #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW
  159. /* no "address" argument so destroys page coloring of some arch */
  160. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  161. {
  162. pgtable_t pgtable;
  163. assert_spin_locked(pmd_lockptr(mm, pmdp));
  164. /* FIFO */
  165. pgtable = pmd_huge_pte(mm, pmdp);
  166. pmd_huge_pte(mm, pmdp) = list_first_entry_or_null(&pgtable->lru,
  167. struct page, lru);
  168. if (pmd_huge_pte(mm, pmdp))
  169. list_del(&pgtable->lru);
  170. return pgtable;
  171. }
  172. #endif
  173. #ifndef __HAVE_ARCH_PMDP_INVALIDATE
  174. pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  175. pmd_t *pmdp)
  176. {
  177. pmd_t old = pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp));
  178. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  179. return old;
  180. }
  181. #endif
  182. #ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD
  183. pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
  184. pmd_t *pmdp)
  185. {
  186. return pmdp_invalidate(vma, address, pmdp);
  187. }
  188. #endif
  189. #ifndef pmdp_collapse_flush
  190. pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  191. pmd_t *pmdp)
  192. {
  193. /*
  194. * pmd and hugepage pte format are same. So we could
  195. * use the same function.
  196. */
  197. pmd_t pmd;
  198. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  199. VM_BUG_ON(pmd_trans_huge(*pmdp));
  200. pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
  201. /* collapse entails shooting down ptes not pmd */
  202. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  203. return pmd;
  204. }
  205. #endif
  206. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */