tlb.h 4.9 KB

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