tlb.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * arch/arm/include/asm/tlb.h
  3. *
  4. * Copyright (C) 2002 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Experimentation shows that on a StrongARM, it appears to be faster
  11. * to use the "invalidate whole tlb" rather than "invalidate single
  12. * tlb" for this.
  13. *
  14. * This appears true for both the process fork+exit case, as well as
  15. * the munmap-large-area case.
  16. */
  17. #ifndef __ASMARM_TLB_H
  18. #define __ASMARM_TLB_H
  19. #include <asm/cacheflush.h>
  20. #ifndef CONFIG_MMU
  21. #include <linux/pagemap.h>
  22. #define tlb_flush(tlb) ((void) tlb)
  23. #include <asm-generic/tlb.h>
  24. #else /* !CONFIG_MMU */
  25. #include <linux/swap.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/tlbflush.h>
  28. /*
  29. * We need to delay page freeing for SMP as other CPUs can access pages
  30. * which have been removed but not yet had their TLB entries invalidated.
  31. * Also, as ARMv7 speculative prefetch can drag new entries into the TLB,
  32. * we need to apply this same delaying tactic to ensure correct operation.
  33. */
  34. #if defined(CONFIG_SMP) || defined(CONFIG_CPU_32v7)
  35. #define tlb_fast_mode(tlb) 0
  36. #else
  37. #define tlb_fast_mode(tlb) 1
  38. #endif
  39. #define MMU_GATHER_BUNDLE 8
  40. /*
  41. * TLB handling. This allows us to remove pages from the page
  42. * tables, and efficiently handle the TLB issues.
  43. */
  44. struct mmu_gather {
  45. struct mm_struct *mm;
  46. unsigned int fullmm;
  47. struct vm_area_struct *vma;
  48. unsigned long range_start;
  49. unsigned long range_end;
  50. unsigned int nr;
  51. unsigned int max;
  52. struct page **pages;
  53. struct page *local[MMU_GATHER_BUNDLE];
  54. };
  55. DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
  56. /*
  57. * This is unnecessarily complex. There's three ways the TLB shootdown
  58. * code is used:
  59. * 1. Unmapping a range of vmas. See zap_page_range(), unmap_region().
  60. * tlb->fullmm = 0, and tlb_start_vma/tlb_end_vma will be called.
  61. * tlb->vma will be non-NULL.
  62. * 2. Unmapping all vmas. See exit_mmap().
  63. * tlb->fullmm = 1, and tlb_start_vma/tlb_end_vma will be called.
  64. * tlb->vma will be non-NULL. Additionally, page tables will be freed.
  65. * 3. Unmapping argument pages. See shift_arg_pages().
  66. * tlb->fullmm = 0, but tlb_start_vma/tlb_end_vma will not be called.
  67. * tlb->vma will be NULL.
  68. */
  69. static inline void tlb_flush(struct mmu_gather *tlb)
  70. {
  71. if (tlb->fullmm || !tlb->vma)
  72. flush_tlb_mm(tlb->mm);
  73. else if (tlb->range_end > 0) {
  74. flush_tlb_range(tlb->vma, tlb->range_start, tlb->range_end);
  75. tlb->range_start = TASK_SIZE;
  76. tlb->range_end = 0;
  77. }
  78. }
  79. static inline void tlb_add_flush(struct mmu_gather *tlb, unsigned long addr)
  80. {
  81. if (!tlb->fullmm) {
  82. if (addr < tlb->range_start)
  83. tlb->range_start = addr;
  84. if (addr + PAGE_SIZE > tlb->range_end)
  85. tlb->range_end = addr + PAGE_SIZE;
  86. }
  87. }
  88. static inline void __tlb_alloc_page(struct mmu_gather *tlb)
  89. {
  90. unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  91. if (addr) {
  92. tlb->pages = (void *)addr;
  93. tlb->max = PAGE_SIZE / sizeof(struct page *);
  94. }
  95. }
  96. static inline void tlb_flush_mmu(struct mmu_gather *tlb)
  97. {
  98. tlb_flush(tlb);
  99. if (!tlb_fast_mode(tlb)) {
  100. free_pages_and_swap_cache(tlb->pages, tlb->nr);
  101. tlb->nr = 0;
  102. if (tlb->pages == tlb->local)
  103. __tlb_alloc_page(tlb);
  104. }
  105. }
  106. static inline void
  107. tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int fullmm)
  108. {
  109. tlb->mm = mm;
  110. tlb->fullmm = fullmm;
  111. tlb->vma = NULL;
  112. tlb->max = ARRAY_SIZE(tlb->local);
  113. tlb->pages = tlb->local;
  114. tlb->nr = 0;
  115. __tlb_alloc_page(tlb);
  116. }
  117. static inline void
  118. tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  119. {
  120. tlb_flush_mmu(tlb);
  121. /* keep the page table cache within bounds */
  122. check_pgt_cache();
  123. if (tlb->pages != tlb->local)
  124. free_pages((unsigned long)tlb->pages, 0);
  125. }
  126. /*
  127. * Memorize the range for the TLB flush.
  128. */
  129. static inline void
  130. tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long addr)
  131. {
  132. tlb_add_flush(tlb, addr);
  133. }
  134. /*
  135. * In the case of tlb vma handling, we can optimise these away in the
  136. * case where we're doing a full MM flush. When we're doing a munmap,
  137. * the vmas are adjusted to only cover the region to be torn down.
  138. */
  139. static inline void
  140. tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  141. {
  142. if (!tlb->fullmm) {
  143. flush_cache_range(vma, vma->vm_start, vma->vm_end);
  144. tlb->vma = vma;
  145. tlb->range_start = TASK_SIZE;
  146. tlb->range_end = 0;
  147. }
  148. }
  149. static inline void
  150. tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  151. {
  152. if (!tlb->fullmm)
  153. tlb_flush(tlb);
  154. }
  155. static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  156. {
  157. if (tlb_fast_mode(tlb)) {
  158. free_page_and_swap_cache(page);
  159. return 1; /* avoid calling tlb_flush_mmu */
  160. }
  161. tlb->pages[tlb->nr++] = page;
  162. VM_BUG_ON(tlb->nr > tlb->max);
  163. return tlb->max - tlb->nr;
  164. }
  165. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  166. {
  167. if (!__tlb_remove_page(tlb, page))
  168. tlb_flush_mmu(tlb);
  169. }
  170. static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
  171. unsigned long addr)
  172. {
  173. pgtable_page_dtor(pte);
  174. /*
  175. * With the classic ARM MMU, a pte page has two corresponding pmd
  176. * entries, each covering 1MB.
  177. */
  178. addr &= PMD_MASK;
  179. tlb_add_flush(tlb, addr + SZ_1M - PAGE_SIZE);
  180. tlb_add_flush(tlb, addr + SZ_1M);
  181. tlb_remove_page(tlb, pte);
  182. }
  183. static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
  184. unsigned long addr)
  185. {
  186. #ifdef CONFIG_ARM_LPAE
  187. tlb_add_flush(tlb, addr);
  188. tlb_remove_page(tlb, virt_to_page(pmdp));
  189. #endif
  190. }
  191. #define pte_free_tlb(tlb, ptep, addr) __pte_free_tlb(tlb, ptep, addr)
  192. #define pmd_free_tlb(tlb, pmdp, addr) __pmd_free_tlb(tlb, pmdp, addr)
  193. #define pud_free_tlb(tlb, pudp, addr) pud_free((tlb)->mm, pudp)
  194. #define tlb_migrate_finish(mm) do { } while (0)
  195. #endif /* CONFIG_MMU */
  196. #endif