tlb.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #define MMU_GATHER_BUNDLE 8
  29. /*
  30. * TLB handling. This allows us to remove pages from the page
  31. * tables, and efficiently handle the TLB issues.
  32. */
  33. struct mmu_gather {
  34. struct mm_struct *mm;
  35. unsigned int fullmm;
  36. struct vm_area_struct *vma;
  37. unsigned long start, end;
  38. unsigned long range_start;
  39. unsigned long range_end;
  40. unsigned int nr;
  41. unsigned int max;
  42. struct page **pages;
  43. struct page *local[MMU_GATHER_BUNDLE];
  44. };
  45. DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
  46. /*
  47. * This is unnecessarily complex. There's three ways the TLB shootdown
  48. * code is used:
  49. * 1. Unmapping a range of vmas. See zap_page_range(), unmap_region().
  50. * tlb->fullmm = 0, and tlb_start_vma/tlb_end_vma will be called.
  51. * tlb->vma will be non-NULL.
  52. * 2. Unmapping all vmas. See exit_mmap().
  53. * tlb->fullmm = 1, and tlb_start_vma/tlb_end_vma will be called.
  54. * tlb->vma will be non-NULL. Additionally, page tables will be freed.
  55. * 3. Unmapping argument pages. See shift_arg_pages().
  56. * tlb->fullmm = 0, but tlb_start_vma/tlb_end_vma will not be called.
  57. * tlb->vma will be NULL.
  58. */
  59. static inline void tlb_flush(struct mmu_gather *tlb)
  60. {
  61. if (tlb->fullmm || !tlb->vma)
  62. flush_tlb_mm(tlb->mm);
  63. else if (tlb->range_end > 0) {
  64. flush_tlb_range(tlb->vma, tlb->range_start, tlb->range_end);
  65. tlb->range_start = TASK_SIZE;
  66. tlb->range_end = 0;
  67. }
  68. }
  69. static inline void tlb_add_flush(struct mmu_gather *tlb, unsigned long addr)
  70. {
  71. if (!tlb->fullmm) {
  72. if (addr < tlb->range_start)
  73. tlb->range_start = addr;
  74. if (addr + PAGE_SIZE > tlb->range_end)
  75. tlb->range_end = addr + PAGE_SIZE;
  76. }
  77. }
  78. static inline void __tlb_alloc_page(struct mmu_gather *tlb)
  79. {
  80. unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  81. if (addr) {
  82. tlb->pages = (void *)addr;
  83. tlb->max = PAGE_SIZE / sizeof(struct page *);
  84. }
  85. }
  86. static inline void tlb_flush_mmu(struct mmu_gather *tlb)
  87. {
  88. tlb_flush(tlb);
  89. free_pages_and_swap_cache(tlb->pages, tlb->nr);
  90. tlb->nr = 0;
  91. if (tlb->pages == tlb->local)
  92. __tlb_alloc_page(tlb);
  93. }
  94. static inline void
  95. tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
  96. {
  97. tlb->mm = mm;
  98. tlb->fullmm = !(start | (end+1));
  99. tlb->start = start;
  100. tlb->end = end;
  101. tlb->vma = NULL;
  102. tlb->max = ARRAY_SIZE(tlb->local);
  103. tlb->pages = tlb->local;
  104. tlb->nr = 0;
  105. __tlb_alloc_page(tlb);
  106. }
  107. static inline void
  108. tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  109. {
  110. tlb_flush_mmu(tlb);
  111. /* keep the page table cache within bounds */
  112. check_pgt_cache();
  113. if (tlb->pages != tlb->local)
  114. free_pages((unsigned long)tlb->pages, 0);
  115. }
  116. /*
  117. * Memorize the range for the TLB flush.
  118. */
  119. static inline void
  120. tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long addr)
  121. {
  122. tlb_add_flush(tlb, addr);
  123. }
  124. /*
  125. * In the case of tlb vma handling, we can optimise these away in the
  126. * case where we're doing a full MM flush. When we're doing a munmap,
  127. * the vmas are adjusted to only cover the region to be torn down.
  128. */
  129. static inline void
  130. tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  131. {
  132. if (!tlb->fullmm) {
  133. flush_cache_range(vma, vma->vm_start, vma->vm_end);
  134. tlb->vma = vma;
  135. tlb->range_start = TASK_SIZE;
  136. tlb->range_end = 0;
  137. }
  138. }
  139. static inline void
  140. tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  141. {
  142. if (!tlb->fullmm)
  143. tlb_flush(tlb);
  144. }
  145. static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  146. {
  147. tlb->pages[tlb->nr++] = page;
  148. VM_BUG_ON(tlb->nr > tlb->max);
  149. return tlb->max - tlb->nr;
  150. }
  151. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  152. {
  153. if (!__tlb_remove_page(tlb, page))
  154. tlb_flush_mmu(tlb);
  155. }
  156. static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
  157. unsigned long addr)
  158. {
  159. pgtable_page_dtor(pte);
  160. #ifdef CONFIG_ARM_LPAE
  161. tlb_add_flush(tlb, addr);
  162. #else
  163. /*
  164. * With the classic ARM MMU, a pte page has two corresponding pmd
  165. * entries, each covering 1MB.
  166. */
  167. addr &= PMD_MASK;
  168. tlb_add_flush(tlb, addr + SZ_1M - PAGE_SIZE);
  169. tlb_add_flush(tlb, addr + SZ_1M);
  170. #endif
  171. tlb_remove_page(tlb, pte);
  172. }
  173. static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
  174. unsigned long addr)
  175. {
  176. #ifdef CONFIG_ARM_LPAE
  177. tlb_add_flush(tlb, addr);
  178. tlb_remove_page(tlb, virt_to_page(pmdp));
  179. #endif
  180. }
  181. static inline void
  182. tlb_remove_pmd_tlb_entry(struct mmu_gather *tlb, pmd_t *pmdp, unsigned long addr)
  183. {
  184. tlb_add_flush(tlb, addr);
  185. }
  186. #define pte_free_tlb(tlb, ptep, addr) __pte_free_tlb(tlb, ptep, addr)
  187. #define pmd_free_tlb(tlb, pmdp, addr) __pmd_free_tlb(tlb, pmdp, addr)
  188. #define pud_free_tlb(tlb, pudp, addr) pud_free((tlb)->mm, pudp)
  189. #define tlb_migrate_finish(mm) do { } while (0)
  190. #endif /* CONFIG_MMU */
  191. #endif