pgtable-generic.c 5.3 KB

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