tlb.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Based on arch/arm/include/asm/tlb.h
  3. *
  4. * Copyright (C) 2002 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __ASM_TLB_H
  20. #define __ASM_TLB_H
  21. #include <linux/pagemap.h>
  22. #include <linux/swap.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/tlbflush.h>
  25. #define MMU_GATHER_BUNDLE 8
  26. /*
  27. * TLB handling. This allows us to remove pages from the page
  28. * tables, and efficiently handle the TLB issues.
  29. */
  30. struct mmu_gather {
  31. struct mm_struct *mm;
  32. unsigned int fullmm;
  33. struct vm_area_struct *vma;
  34. unsigned long start, end;
  35. unsigned long range_start;
  36. unsigned long range_end;
  37. unsigned int nr;
  38. unsigned int max;
  39. struct page **pages;
  40. struct page *local[MMU_GATHER_BUNDLE];
  41. };
  42. /*
  43. * This is unnecessarily complex. There's three ways the TLB shootdown
  44. * code is used:
  45. * 1. Unmapping a range of vmas. See zap_page_range(), unmap_region().
  46. * tlb->fullmm = 0, and tlb_start_vma/tlb_end_vma will be called.
  47. * tlb->vma will be non-NULL.
  48. * 2. Unmapping all vmas. See exit_mmap().
  49. * tlb->fullmm = 1, and tlb_start_vma/tlb_end_vma will be called.
  50. * tlb->vma will be non-NULL. Additionally, page tables will be freed.
  51. * 3. Unmapping argument pages. See shift_arg_pages().
  52. * tlb->fullmm = 0, but tlb_start_vma/tlb_end_vma will not be called.
  53. * tlb->vma will be NULL.
  54. */
  55. static inline void tlb_flush(struct mmu_gather *tlb)
  56. {
  57. if (tlb->fullmm || !tlb->vma)
  58. flush_tlb_mm(tlb->mm);
  59. else if (tlb->range_end > 0) {
  60. flush_tlb_range(tlb->vma, tlb->range_start, tlb->range_end);
  61. tlb->range_start = TASK_SIZE;
  62. tlb->range_end = 0;
  63. }
  64. }
  65. static inline void tlb_add_flush(struct mmu_gather *tlb, unsigned long addr)
  66. {
  67. if (!tlb->fullmm) {
  68. if (addr < tlb->range_start)
  69. tlb->range_start = addr;
  70. if (addr + PAGE_SIZE > tlb->range_end)
  71. tlb->range_end = addr + PAGE_SIZE;
  72. }
  73. }
  74. static inline void __tlb_alloc_page(struct mmu_gather *tlb)
  75. {
  76. unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  77. if (addr) {
  78. tlb->pages = (void *)addr;
  79. tlb->max = PAGE_SIZE / sizeof(struct page *);
  80. }
  81. }
  82. static inline void tlb_flush_mmu(struct mmu_gather *tlb)
  83. {
  84. tlb_flush(tlb);
  85. free_pages_and_swap_cache(tlb->pages, tlb->nr);
  86. tlb->nr = 0;
  87. if (tlb->pages == tlb->local)
  88. __tlb_alloc_page(tlb);
  89. }
  90. static inline void
  91. tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
  92. {
  93. tlb->mm = mm;
  94. tlb->fullmm = !(start | (end+1));
  95. tlb->start = start;
  96. tlb->end = end;
  97. tlb->vma = NULL;
  98. tlb->max = ARRAY_SIZE(tlb->local);
  99. tlb->pages = tlb->local;
  100. tlb->nr = 0;
  101. __tlb_alloc_page(tlb);
  102. }
  103. static inline void
  104. tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  105. {
  106. tlb_flush_mmu(tlb);
  107. /* keep the page table cache within bounds */
  108. check_pgt_cache();
  109. if (tlb->pages != tlb->local)
  110. free_pages((unsigned long)tlb->pages, 0);
  111. }
  112. /*
  113. * Memorize the range for the TLB flush.
  114. */
  115. static inline void
  116. tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long addr)
  117. {
  118. tlb_add_flush(tlb, addr);
  119. }
  120. /*
  121. * In the case of tlb vma handling, we can optimise these away in the
  122. * case where we're doing a full MM flush. When we're doing a munmap,
  123. * the vmas are adjusted to only cover the region to be torn down.
  124. */
  125. static inline void
  126. tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  127. {
  128. if (!tlb->fullmm) {
  129. tlb->vma = vma;
  130. tlb->range_start = TASK_SIZE;
  131. tlb->range_end = 0;
  132. }
  133. }
  134. static inline void
  135. tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  136. {
  137. if (!tlb->fullmm)
  138. tlb_flush(tlb);
  139. }
  140. static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  141. {
  142. tlb->pages[tlb->nr++] = page;
  143. VM_BUG_ON(tlb->nr > tlb->max);
  144. return tlb->max - tlb->nr;
  145. }
  146. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  147. {
  148. if (!__tlb_remove_page(tlb, page))
  149. tlb_flush_mmu(tlb);
  150. }
  151. static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
  152. unsigned long addr)
  153. {
  154. pgtable_page_dtor(pte);
  155. tlb_add_flush(tlb, addr);
  156. tlb_remove_page(tlb, pte);
  157. }
  158. #ifndef CONFIG_ARM64_64K_PAGES
  159. static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
  160. unsigned long addr)
  161. {
  162. tlb_add_flush(tlb, addr);
  163. tlb_remove_page(tlb, virt_to_page(pmdp));
  164. }
  165. #endif
  166. #define pte_free_tlb(tlb, ptep, addr) __pte_free_tlb(tlb, ptep, addr)
  167. #define pmd_free_tlb(tlb, pmdp, addr) __pmd_free_tlb(tlb, pmdp, addr)
  168. #define pud_free_tlb(tlb, pudp, addr) pud_free((tlb)->mm, pudp)
  169. #define tlb_migrate_finish(mm) do { } while (0)
  170. static inline void
  171. tlb_remove_pmd_tlb_entry(struct mmu_gather *tlb, pmd_t *pmdp, unsigned long addr)
  172. {
  173. tlb_add_flush(tlb, addr);
  174. }
  175. #endif