tlb.h 5.4 KB

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