tlb.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* include/asm-generic/tlb.h
  2. *
  3. * Generic TLB shootdown code
  4. *
  5. * Copyright 2001 Red Hat, Inc.
  6. * Based on code from mm/memory.c Copyright Linus Torvalds and others.
  7. *
  8. * Copyright 2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #ifndef _ASM_GENERIC__TLB_H
  16. #define _ASM_GENERIC__TLB_H
  17. #include <linux/swap.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/tlbflush.h>
  20. /*
  21. * For UP we don't need to worry about TLB flush
  22. * and page free order so much..
  23. */
  24. #ifdef CONFIG_SMP
  25. #define tlb_fast_mode(tlb) ((tlb)->nr == ~0U)
  26. #else
  27. #define tlb_fast_mode(tlb) 1
  28. #endif
  29. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  30. /*
  31. * Semi RCU freeing of the page directories.
  32. *
  33. * This is needed by some architectures to implement software pagetable walkers.
  34. *
  35. * gup_fast() and other software pagetable walkers do a lockless page-table
  36. * walk and therefore needs some synchronization with the freeing of the page
  37. * directories. The chosen means to accomplish that is by disabling IRQs over
  38. * the walk.
  39. *
  40. * Architectures that use IPIs to flush TLBs will then automagically DTRT,
  41. * since we unlink the page, flush TLBs, free the page. Since the disabling of
  42. * IRQs delays the completion of the TLB flush we can never observe an already
  43. * freed page.
  44. *
  45. * Architectures that do not have this (PPC) need to delay the freeing by some
  46. * other means, this is that means.
  47. *
  48. * What we do is batch the freed directory pages (tables) and RCU free them.
  49. * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
  50. * holds off grace periods.
  51. *
  52. * However, in order to batch these pages we need to allocate storage, this
  53. * allocation is deep inside the MM code and can thus easily fail on memory
  54. * pressure. To guarantee progress we fall back to single table freeing, see
  55. * the implementation of tlb_remove_table_one().
  56. *
  57. */
  58. struct mmu_table_batch {
  59. struct rcu_head rcu;
  60. unsigned int nr;
  61. void *tables[0];
  62. };
  63. #define MAX_TABLE_BATCH \
  64. ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
  65. extern void tlb_table_flush(struct mmu_gather *tlb);
  66. extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
  67. #endif
  68. /*
  69. * If we can't allocate a page to make a big batch of page pointers
  70. * to work on, then just handle a few from the on-stack structure.
  71. */
  72. #define MMU_GATHER_BUNDLE 8
  73. /* struct mmu_gather is an opaque type used by the mm code for passing around
  74. * any data needed by arch specific code for tlb_remove_page.
  75. */
  76. struct mmu_gather {
  77. struct mm_struct *mm;
  78. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  79. struct mmu_table_batch *batch;
  80. #endif
  81. unsigned int nr; /* set to ~0U means fast mode */
  82. unsigned int max; /* nr < max */
  83. unsigned int need_flush;/* Really unmapped some ptes? */
  84. unsigned int fullmm; /* non-zero means full mm flush */
  85. struct page **pages;
  86. struct page *local[MMU_GATHER_BUNDLE];
  87. };
  88. static inline void __tlb_alloc_page(struct mmu_gather *tlb)
  89. {
  90. unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  91. if (addr) {
  92. tlb->pages = (void *)addr;
  93. tlb->max = PAGE_SIZE / sizeof(struct page *);
  94. }
  95. }
  96. /* tlb_gather_mmu
  97. * Called to initialize an (on-stack) mmu_gather structure for page-table
  98. * tear-down from @mm. The @fullmm argument is used when @mm is without
  99. * users and we're going to destroy the full address space (exit/execve).
  100. */
  101. static inline void
  102. tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
  103. {
  104. tlb->mm = mm;
  105. tlb->max = ARRAY_SIZE(tlb->local);
  106. tlb->pages = tlb->local;
  107. if (num_online_cpus() > 1) {
  108. tlb->nr = 0;
  109. __tlb_alloc_page(tlb);
  110. } else /* Use fast mode if only one CPU is online */
  111. tlb->nr = ~0U;
  112. tlb->fullmm = fullmm;
  113. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  114. tlb->batch = NULL;
  115. #endif
  116. }
  117. static inline void
  118. tlb_flush_mmu(struct mmu_gather *tlb)
  119. {
  120. if (!tlb->need_flush)
  121. return;
  122. tlb->need_flush = 0;
  123. tlb_flush(tlb);
  124. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  125. tlb_table_flush(tlb);
  126. #endif
  127. if (!tlb_fast_mode(tlb)) {
  128. free_pages_and_swap_cache(tlb->pages, tlb->nr);
  129. tlb->nr = 0;
  130. /*
  131. * If we are using the local on-stack array of pages for MMU
  132. * gather, try allocating an off-stack array again as we have
  133. * recently freed pages.
  134. */
  135. if (tlb->pages == tlb->local)
  136. __tlb_alloc_page(tlb);
  137. }
  138. }
  139. /* tlb_finish_mmu
  140. * Called at the end of the shootdown operation to free up any resources
  141. * that were required.
  142. */
  143. static inline void
  144. tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  145. {
  146. tlb_flush_mmu(tlb);
  147. /* keep the page table cache within bounds */
  148. check_pgt_cache();
  149. if (tlb->pages != tlb->local)
  150. free_pages((unsigned long)tlb->pages, 0);
  151. }
  152. /* __tlb_remove_page
  153. * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
  154. * handling the additional races in SMP caused by other CPUs caching valid
  155. * mappings in their TLBs. Returns the number of free page slots left.
  156. * When out of page slots we must call tlb_flush_mmu().
  157. */
  158. static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  159. {
  160. tlb->need_flush = 1;
  161. if (tlb_fast_mode(tlb)) {
  162. free_page_and_swap_cache(page);
  163. return 1; /* avoid calling tlb_flush_mmu() */
  164. }
  165. tlb->pages[tlb->nr++] = page;
  166. VM_BUG_ON(tlb->nr > tlb->max);
  167. return tlb->max - tlb->nr;
  168. }
  169. /* tlb_remove_page
  170. * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
  171. * required.
  172. */
  173. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  174. {
  175. if (!__tlb_remove_page(tlb, page))
  176. tlb_flush_mmu(tlb);
  177. }
  178. /**
  179. * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
  180. *
  181. * Record the fact that pte's were really umapped in ->need_flush, so we can
  182. * later optimise away the tlb invalidate. This helps when userspace is
  183. * unmapping already-unmapped pages, which happens quite a lot.
  184. */
  185. #define tlb_remove_tlb_entry(tlb, ptep, address) \
  186. do { \
  187. tlb->need_flush = 1; \
  188. __tlb_remove_tlb_entry(tlb, ptep, address); \
  189. } while (0)
  190. #define pte_free_tlb(tlb, ptep, address) \
  191. do { \
  192. tlb->need_flush = 1; \
  193. __pte_free_tlb(tlb, ptep, address); \
  194. } while (0)
  195. #ifndef __ARCH_HAS_4LEVEL_HACK
  196. #define pud_free_tlb(tlb, pudp, address) \
  197. do { \
  198. tlb->need_flush = 1; \
  199. __pud_free_tlb(tlb, pudp, address); \
  200. } while (0)
  201. #endif
  202. #define pmd_free_tlb(tlb, pmdp, address) \
  203. do { \
  204. tlb->need_flush = 1; \
  205. __pmd_free_tlb(tlb, pmdp, address); \
  206. } while (0)
  207. #define tlb_migrate_finish(mm) do {} while (0)
  208. #endif /* _ASM_GENERIC__TLB_H */