pgtable-generic.c 5.7 KB

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