tlb.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include <asm-generic/tlb.h>
  23. #else /* !CONFIG_MMU */
  24. #include <linux/swap.h>
  25. #include <asm/pgalloc.h>
  26. #include <asm/tlbflush.h>
  27. /*
  28. * We need to delay page freeing for SMP as other CPUs can access pages
  29. * which have been removed but not yet had their TLB entries invalidated.
  30. * Also, as ARMv7 speculative prefetch can drag new entries into the TLB,
  31. * we need to apply this same delaying tactic to ensure correct operation.
  32. */
  33. #if defined(CONFIG_SMP) || defined(CONFIG_CPU_32v7)
  34. #define tlb_fast_mode(tlb) 0
  35. #define FREE_PTE_NR 500
  36. #else
  37. #define tlb_fast_mode(tlb) 1
  38. #define FREE_PTE_NR 0
  39. #endif
  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. struct page *pages[FREE_PTE_NR];
  52. };
  53. DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
  54. /*
  55. * This is unnecessarily complex. There's three ways the TLB shootdown
  56. * code is used:
  57. * 1. Unmapping a range of vmas. See zap_page_range(), unmap_region().
  58. * tlb->fullmm = 0, and tlb_start_vma/tlb_end_vma will be called.
  59. * tlb->vma will be non-NULL.
  60. * 2. Unmapping all vmas. See exit_mmap().
  61. * tlb->fullmm = 1, and tlb_start_vma/tlb_end_vma will be called.
  62. * tlb->vma will be non-NULL. Additionally, page tables will be freed.
  63. * 3. Unmapping argument pages. See shift_arg_pages().
  64. * tlb->fullmm = 0, but tlb_start_vma/tlb_end_vma will not be called.
  65. * tlb->vma will be NULL.
  66. */
  67. static inline void tlb_flush(struct mmu_gather *tlb)
  68. {
  69. if (tlb->fullmm || !tlb->vma)
  70. flush_tlb_mm(tlb->mm);
  71. else if (tlb->range_end > 0) {
  72. flush_tlb_range(tlb->vma, tlb->range_start, tlb->range_end);
  73. tlb->range_start = TASK_SIZE;
  74. tlb->range_end = 0;
  75. }
  76. }
  77. static inline void tlb_add_flush(struct mmu_gather *tlb, unsigned long addr)
  78. {
  79. if (!tlb->fullmm) {
  80. if (addr < tlb->range_start)
  81. tlb->range_start = addr;
  82. if (addr + PAGE_SIZE > tlb->range_end)
  83. tlb->range_end = addr + PAGE_SIZE;
  84. }
  85. }
  86. static inline void tlb_flush_mmu(struct mmu_gather *tlb)
  87. {
  88. tlb_flush(tlb);
  89. if (!tlb_fast_mode(tlb)) {
  90. free_pages_and_swap_cache(tlb->pages, tlb->nr);
  91. tlb->nr = 0;
  92. }
  93. }
  94. static inline struct mmu_gather *
  95. tlb_gather_mmu(struct mm_struct *mm, unsigned int full_mm_flush)
  96. {
  97. struct mmu_gather *tlb = &get_cpu_var(mmu_gathers);
  98. tlb->mm = mm;
  99. tlb->fullmm = full_mm_flush;
  100. tlb->vma = NULL;
  101. tlb->nr = 0;
  102. return 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. put_cpu_var(mmu_gathers);
  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. flush_cache_range(vma, vma->vm_start, vma->vm_end);
  130. tlb->vma = vma;
  131. tlb->range_start = TASK_SIZE;
  132. tlb->range_end = 0;
  133. }
  134. }
  135. static inline void
  136. tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  137. {
  138. if (!tlb->fullmm)
  139. tlb_flush(tlb);
  140. }
  141. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  142. {
  143. if (tlb_fast_mode(tlb)) {
  144. free_page_and_swap_cache(page);
  145. } else {
  146. tlb->pages[tlb->nr++] = page;
  147. if (tlb->nr >= FREE_PTE_NR)
  148. tlb_flush_mmu(tlb);
  149. }
  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. #define pte_free_tlb(tlb, ptep, addr) __pte_free_tlb(tlb, ptep, addr)
  159. #define pmd_free_tlb(tlb, pmdp, addr) pmd_free((tlb)->mm, pmdp)
  160. #define tlb_migrate_finish(mm) do { } while (0)
  161. #endif /* CONFIG_MMU */
  162. #endif