tlb.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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
  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. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  21. /*
  22. * Semi RCU freeing of the page directories.
  23. *
  24. * This is needed by some architectures to implement software pagetable walkers.
  25. *
  26. * gup_fast() and other software pagetable walkers do a lockless page-table
  27. * walk and therefore needs some synchronization with the freeing of the page
  28. * directories. The chosen means to accomplish that is by disabling IRQs over
  29. * the walk.
  30. *
  31. * Architectures that use IPIs to flush TLBs will then automagically DTRT,
  32. * since we unlink the page, flush TLBs, free the page. Since the disabling of
  33. * IRQs delays the completion of the TLB flush we can never observe an already
  34. * freed page.
  35. *
  36. * Architectures that do not have this (PPC) need to delay the freeing by some
  37. * other means, this is that means.
  38. *
  39. * What we do is batch the freed directory pages (tables) and RCU free them.
  40. * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
  41. * holds off grace periods.
  42. *
  43. * However, in order to batch these pages we need to allocate storage, this
  44. * allocation is deep inside the MM code and can thus easily fail on memory
  45. * pressure. To guarantee progress we fall back to single table freeing, see
  46. * the implementation of tlb_remove_table_one().
  47. *
  48. */
  49. struct mmu_table_batch {
  50. struct rcu_head rcu;
  51. unsigned int nr;
  52. void *tables[0];
  53. };
  54. #define MAX_TABLE_BATCH \
  55. ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
  56. extern void tlb_table_flush(struct mmu_gather *tlb);
  57. extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
  58. #endif
  59. /*
  60. * If we can't allocate a page to make a big batch of page pointers
  61. * to work on, then just handle a few from the on-stack structure.
  62. */
  63. #define MMU_GATHER_BUNDLE 8
  64. struct mmu_gather_batch {
  65. struct mmu_gather_batch *next;
  66. unsigned int nr;
  67. unsigned int max;
  68. struct page *pages[0];
  69. };
  70. #define MAX_GATHER_BATCH \
  71. ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
  72. /*
  73. * Limit the maximum number of mmu_gather batches to reduce a risk of soft
  74. * lockups for non-preemptible kernels on huge machines when a lot of memory
  75. * is zapped during unmapping.
  76. * 10K pages freed at once should be safe even without a preemption point.
  77. */
  78. #define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH)
  79. /* struct mmu_gather is an opaque type used by the mm code for passing around
  80. * any data needed by arch specific code for tlb_remove_page.
  81. */
  82. struct mmu_gather {
  83. struct mm_struct *mm;
  84. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  85. struct mmu_table_batch *batch;
  86. #endif
  87. unsigned long start;
  88. unsigned long end;
  89. /* we are in the middle of an operation to clear
  90. * a full mm and can make some optimizations */
  91. unsigned int fullmm : 1,
  92. /* we have performed an operation which
  93. * requires a complete flush of the tlb */
  94. need_flush_all : 1;
  95. struct mmu_gather_batch *active;
  96. struct mmu_gather_batch local;
  97. struct page *__pages[MMU_GATHER_BUNDLE];
  98. unsigned int batch_count;
  99. int page_size;
  100. };
  101. #define HAVE_GENERIC_MMU_GATHER
  102. void arch_tlb_gather_mmu(struct mmu_gather *tlb,
  103. struct mm_struct *mm, unsigned long start, unsigned long end);
  104. void tlb_flush_mmu(struct mmu_gather *tlb);
  105. void arch_tlb_finish_mmu(struct mmu_gather *tlb,
  106. unsigned long start, unsigned long end, bool force);
  107. extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
  108. int page_size);
  109. static inline void __tlb_adjust_range(struct mmu_gather *tlb,
  110. unsigned long address,
  111. unsigned int range_size)
  112. {
  113. tlb->start = min(tlb->start, address);
  114. tlb->end = max(tlb->end, address + range_size);
  115. }
  116. static inline void __tlb_reset_range(struct mmu_gather *tlb)
  117. {
  118. if (tlb->fullmm) {
  119. tlb->start = tlb->end = ~0;
  120. } else {
  121. tlb->start = TASK_SIZE;
  122. tlb->end = 0;
  123. }
  124. }
  125. static inline void tlb_remove_page_size(struct mmu_gather *tlb,
  126. struct page *page, int page_size)
  127. {
  128. if (__tlb_remove_page_size(tlb, page, page_size))
  129. tlb_flush_mmu(tlb);
  130. }
  131. static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  132. {
  133. return __tlb_remove_page_size(tlb, page, PAGE_SIZE);
  134. }
  135. /* tlb_remove_page
  136. * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
  137. * required.
  138. */
  139. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  140. {
  141. return tlb_remove_page_size(tlb, page, PAGE_SIZE);
  142. }
  143. #ifndef tlb_remove_check_page_size_change
  144. #define tlb_remove_check_page_size_change tlb_remove_check_page_size_change
  145. static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
  146. unsigned int page_size)
  147. {
  148. /*
  149. * We don't care about page size change, just update
  150. * mmu_gather page size here so that debug checks
  151. * doesn't throw false warning.
  152. */
  153. #ifdef CONFIG_DEBUG_VM
  154. tlb->page_size = page_size;
  155. #endif
  156. }
  157. #endif
  158. /*
  159. * In the case of tlb vma handling, we can optimise these away in the
  160. * case where we're doing a full MM flush. When we're doing a munmap,
  161. * the vmas are adjusted to only cover the region to be torn down.
  162. */
  163. #ifndef tlb_start_vma
  164. #define tlb_start_vma(tlb, vma) do { } while (0)
  165. #endif
  166. #define __tlb_end_vma(tlb, vma) \
  167. do { \
  168. if (!tlb->fullmm && tlb->end) { \
  169. tlb_flush(tlb); \
  170. __tlb_reset_range(tlb); \
  171. } \
  172. } while (0)
  173. #ifndef tlb_end_vma
  174. #define tlb_end_vma __tlb_end_vma
  175. #endif
  176. #ifndef __tlb_remove_tlb_entry
  177. #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
  178. #endif
  179. /**
  180. * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
  181. *
  182. * Record the fact that pte's were really unmapped by updating the range,
  183. * so we can later optimise away the tlb invalidate. This helps when
  184. * userspace is unmapping already-unmapped pages, which happens quite a lot.
  185. */
  186. #define tlb_remove_tlb_entry(tlb, ptep, address) \
  187. do { \
  188. __tlb_adjust_range(tlb, address, PAGE_SIZE); \
  189. __tlb_remove_tlb_entry(tlb, ptep, address); \
  190. } while (0)
  191. #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
  192. do { \
  193. __tlb_adjust_range(tlb, address, huge_page_size(h)); \
  194. __tlb_remove_tlb_entry(tlb, ptep, address); \
  195. } while (0)
  196. /**
  197. * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
  198. * This is a nop so far, because only x86 needs it.
  199. */
  200. #ifndef __tlb_remove_pmd_tlb_entry
  201. #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
  202. #endif
  203. #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
  204. do { \
  205. __tlb_adjust_range(tlb, address, HPAGE_PMD_SIZE); \
  206. __tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
  207. } while (0)
  208. /**
  209. * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
  210. * invalidation. This is a nop so far, because only x86 needs it.
  211. */
  212. #ifndef __tlb_remove_pud_tlb_entry
  213. #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
  214. #endif
  215. #define tlb_remove_pud_tlb_entry(tlb, pudp, address) \
  216. do { \
  217. __tlb_adjust_range(tlb, address, HPAGE_PUD_SIZE); \
  218. __tlb_remove_pud_tlb_entry(tlb, pudp, address); \
  219. } while (0)
  220. /*
  221. * For things like page tables caches (ie caching addresses "inside" the
  222. * page tables, like x86 does), for legacy reasons, flushing an
  223. * individual page had better flush the page table caches behind it. This
  224. * is definitely how x86 works, for example. And if you have an
  225. * architected non-legacy page table cache (which I'm not aware of
  226. * anybody actually doing), you're going to have some architecturally
  227. * explicit flushing for that, likely *separate* from a regular TLB entry
  228. * flush, and thus you'd need more than just some range expansion..
  229. *
  230. * So if we ever find an architecture
  231. * that would want something that odd, I think it is up to that
  232. * architecture to do its own odd thing, not cause pain for others
  233. * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
  234. *
  235. * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
  236. */
  237. #define pte_free_tlb(tlb, ptep, address) \
  238. do { \
  239. __tlb_adjust_range(tlb, address, PAGE_SIZE); \
  240. __pte_free_tlb(tlb, ptep, address); \
  241. } while (0)
  242. #define pmd_free_tlb(tlb, pmdp, address) \
  243. do { \
  244. __tlb_adjust_range(tlb, address, PAGE_SIZE); \
  245. __pmd_free_tlb(tlb, pmdp, address); \
  246. } while (0)
  247. #ifndef __ARCH_HAS_4LEVEL_HACK
  248. #define pud_free_tlb(tlb, pudp, address) \
  249. do { \
  250. __tlb_adjust_range(tlb, address, PAGE_SIZE); \
  251. __pud_free_tlb(tlb, pudp, address); \
  252. } while (0)
  253. #endif
  254. #ifndef __ARCH_HAS_5LEVEL_HACK
  255. #define p4d_free_tlb(tlb, pudp, address) \
  256. do { \
  257. __tlb_adjust_range(tlb, address, PAGE_SIZE); \
  258. __p4d_free_tlb(tlb, pudp, address); \
  259. } while (0)
  260. #endif
  261. #define tlb_migrate_finish(mm) do {} while (0)
  262. #endif /* _ASM_GENERIC__TLB_H */