mmu_gather.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include <linux/gfp.h>
  2. #include <linux/highmem.h>
  3. #include <linux/kernel.h>
  4. #include <linux/mmdebug.h>
  5. #include <linux/mm_types.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/rcupdate.h>
  8. #include <linux/smp.h>
  9. #include <linux/swap.h>
  10. #include <asm/pgalloc.h>
  11. #include <asm/tlb.h>
  12. #ifdef HAVE_GENERIC_MMU_GATHER
  13. static bool tlb_next_batch(struct mmu_gather *tlb)
  14. {
  15. struct mmu_gather_batch *batch;
  16. batch = tlb->active;
  17. if (batch->next) {
  18. tlb->active = batch->next;
  19. return true;
  20. }
  21. if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
  22. return false;
  23. batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  24. if (!batch)
  25. return false;
  26. tlb->batch_count++;
  27. batch->next = NULL;
  28. batch->nr = 0;
  29. batch->max = MAX_GATHER_BATCH;
  30. tlb->active->next = batch;
  31. tlb->active = batch;
  32. return true;
  33. }
  34. void arch_tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
  35. unsigned long start, unsigned long end)
  36. {
  37. tlb->mm = mm;
  38. /* Is it from 0 to ~0? */
  39. tlb->fullmm = !(start | (end+1));
  40. tlb->need_flush_all = 0;
  41. tlb->local.next = NULL;
  42. tlb->local.nr = 0;
  43. tlb->local.max = ARRAY_SIZE(tlb->__pages);
  44. tlb->active = &tlb->local;
  45. tlb->batch_count = 0;
  46. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  47. tlb->batch = NULL;
  48. #endif
  49. tlb->page_size = 0;
  50. __tlb_reset_range(tlb);
  51. }
  52. void tlb_flush_mmu_free(struct mmu_gather *tlb)
  53. {
  54. struct mmu_gather_batch *batch;
  55. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  56. tlb_table_flush(tlb);
  57. #endif
  58. for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
  59. free_pages_and_swap_cache(batch->pages, batch->nr);
  60. batch->nr = 0;
  61. }
  62. tlb->active = &tlb->local;
  63. }
  64. void tlb_flush_mmu(struct mmu_gather *tlb)
  65. {
  66. tlb_flush_mmu_tlbonly(tlb);
  67. tlb_flush_mmu_free(tlb);
  68. }
  69. /* tlb_finish_mmu
  70. * Called at the end of the shootdown operation to free up any resources
  71. * that were required.
  72. */
  73. void arch_tlb_finish_mmu(struct mmu_gather *tlb,
  74. unsigned long start, unsigned long end, bool force)
  75. {
  76. struct mmu_gather_batch *batch, *next;
  77. if (force) {
  78. __tlb_reset_range(tlb);
  79. __tlb_adjust_range(tlb, start, end - start);
  80. }
  81. tlb_flush_mmu(tlb);
  82. /* keep the page table cache within bounds */
  83. check_pgt_cache();
  84. for (batch = tlb->local.next; batch; batch = next) {
  85. next = batch->next;
  86. free_pages((unsigned long)batch, 0);
  87. }
  88. tlb->local.next = NULL;
  89. }
  90. /* __tlb_remove_page
  91. * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
  92. * handling the additional races in SMP caused by other CPUs caching valid
  93. * mappings in their TLBs. Returns the number of free page slots left.
  94. * When out of page slots we must call tlb_flush_mmu().
  95. *returns true if the caller should flush.
  96. */
  97. bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
  98. {
  99. struct mmu_gather_batch *batch;
  100. VM_BUG_ON(!tlb->end);
  101. VM_WARN_ON(tlb->page_size != page_size);
  102. batch = tlb->active;
  103. /*
  104. * Add the page and check if we are full. If so
  105. * force a flush.
  106. */
  107. batch->pages[batch->nr++] = page;
  108. if (batch->nr == batch->max) {
  109. if (!tlb_next_batch(tlb))
  110. return true;
  111. batch = tlb->active;
  112. }
  113. VM_BUG_ON_PAGE(batch->nr > batch->max, page);
  114. return false;
  115. }
  116. #endif /* HAVE_GENERIC_MMU_GATHER */
  117. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  118. /*
  119. * See the comment near struct mmu_table_batch.
  120. */
  121. /*
  122. * If we want tlb_remove_table() to imply TLB invalidates.
  123. */
  124. static inline void tlb_table_invalidate(struct mmu_gather *tlb)
  125. {
  126. #ifdef CONFIG_HAVE_RCU_TABLE_INVALIDATE
  127. /*
  128. * Invalidate page-table caches used by hardware walkers. Then we still
  129. * need to RCU-sched wait while freeing the pages because software
  130. * walkers can still be in-flight.
  131. */
  132. tlb_flush_mmu_tlbonly(tlb);
  133. #endif
  134. }
  135. static void tlb_remove_table_smp_sync(void *arg)
  136. {
  137. /* Simply deliver the interrupt */
  138. }
  139. static void tlb_remove_table_one(void *table)
  140. {
  141. /*
  142. * This isn't an RCU grace period and hence the page-tables cannot be
  143. * assumed to be actually RCU-freed.
  144. *
  145. * It is however sufficient for software page-table walkers that rely on
  146. * IRQ disabling. See the comment near struct mmu_table_batch.
  147. */
  148. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  149. __tlb_remove_table(table);
  150. }
  151. static void tlb_remove_table_rcu(struct rcu_head *head)
  152. {
  153. struct mmu_table_batch *batch;
  154. int i;
  155. batch = container_of(head, struct mmu_table_batch, rcu);
  156. for (i = 0; i < batch->nr; i++)
  157. __tlb_remove_table(batch->tables[i]);
  158. free_page((unsigned long)batch);
  159. }
  160. void tlb_table_flush(struct mmu_gather *tlb)
  161. {
  162. struct mmu_table_batch **batch = &tlb->batch;
  163. if (*batch) {
  164. tlb_table_invalidate(tlb);
  165. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  166. *batch = NULL;
  167. }
  168. }
  169. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  170. {
  171. struct mmu_table_batch **batch = &tlb->batch;
  172. if (*batch == NULL) {
  173. *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  174. if (*batch == NULL) {
  175. tlb_table_invalidate(tlb);
  176. tlb_remove_table_one(table);
  177. return;
  178. }
  179. (*batch)->nr = 0;
  180. }
  181. (*batch)->tables[(*batch)->nr++] = table;
  182. if ((*batch)->nr == MAX_TABLE_BATCH)
  183. tlb_table_flush(tlb);
  184. }
  185. #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
  186. /**
  187. * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
  188. * @tlb: the mmu_gather structure to initialize
  189. * @mm: the mm_struct of the target address space
  190. * @start: start of the region that will be removed from the page-table
  191. * @end: end of the region that will be removed from the page-table
  192. *
  193. * Called to initialize an (on-stack) mmu_gather structure for page-table
  194. * tear-down from @mm. The @start and @end are set to 0 and -1
  195. * respectively when @mm is without users and we're going to destroy
  196. * the full address space (exit/execve).
  197. */
  198. void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
  199. unsigned long start, unsigned long end)
  200. {
  201. arch_tlb_gather_mmu(tlb, mm, start, end);
  202. inc_tlb_flush_pending(tlb->mm);
  203. }
  204. void tlb_finish_mmu(struct mmu_gather *tlb,
  205. unsigned long start, unsigned long end)
  206. {
  207. /*
  208. * If there are parallel threads are doing PTE changes on same range
  209. * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
  210. * flush by batching, a thread has stable TLB entry can fail to flush
  211. * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
  212. * forcefully if we detect parallel PTE batching threads.
  213. */
  214. bool force = mm_tlb_flush_nested(tlb->mm);
  215. arch_tlb_finish_mmu(tlb, start, end, force);
  216. dec_tlb_flush_pending(tlb->mm);
  217. }