tlb.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <linux/init.h>
  2. #include <linux/mm.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/smp.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/module.h>
  7. #include <asm/tlbflush.h>
  8. #include <asm/mmu_context.h>
  9. #include <asm/cache.h>
  10. #include <asm/apic.h>
  11. #include <asm/uv/uv.h>
  12. DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate)
  13. = { &init_mm, 0, };
  14. /*
  15. * Smarter SMP flushing macros.
  16. * c/o Linus Torvalds.
  17. *
  18. * These mean you can really definitely utterly forget about
  19. * writing to user space from interrupts. (Its not allowed anyway).
  20. *
  21. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  22. *
  23. * More scalable flush, from Andi Kleen
  24. *
  25. * To avoid global state use 8 different call vectors.
  26. * Each CPU uses a specific vector to trigger flushes on other
  27. * CPUs. Depending on the received vector the target CPUs look into
  28. * the right array slot for the flush data.
  29. *
  30. * With more than 8 CPUs they are hashed to the 8 available
  31. * vectors. The limited global vector space forces us to this right now.
  32. * In future when interrupts are split into per CPU domains this could be
  33. * fixed, at the cost of triggering multiple IPIs in some cases.
  34. */
  35. union smp_flush_state {
  36. struct {
  37. struct mm_struct *flush_mm;
  38. unsigned long flush_va;
  39. raw_spinlock_t tlbstate_lock;
  40. DECLARE_BITMAP(flush_cpumask, NR_CPUS);
  41. };
  42. char pad[INTERNODE_CACHE_BYTES];
  43. } ____cacheline_internodealigned_in_smp;
  44. /* State is put into the per CPU data section, but padded
  45. to a full cache line because other CPUs can access it and we don't
  46. want false sharing in the per cpu data segment. */
  47. static union smp_flush_state flush_state[NUM_INVALIDATE_TLB_VECTORS];
  48. /*
  49. * We cannot call mmdrop() because we are in interrupt context,
  50. * instead update mm->cpu_vm_mask.
  51. */
  52. void leave_mm(int cpu)
  53. {
  54. if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
  55. BUG();
  56. cpumask_clear_cpu(cpu,
  57. mm_cpumask(percpu_read(cpu_tlbstate.active_mm)));
  58. load_cr3(swapper_pg_dir);
  59. }
  60. EXPORT_SYMBOL_GPL(leave_mm);
  61. /*
  62. *
  63. * The flush IPI assumes that a thread switch happens in this order:
  64. * [cpu0: the cpu that switches]
  65. * 1) switch_mm() either 1a) or 1b)
  66. * 1a) thread switch to a different mm
  67. * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
  68. * Stop ipi delivery for the old mm. This is not synchronized with
  69. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  70. * for the wrong mm, and in the worst case we perform a superfluous
  71. * tlb flush.
  72. * 1a2) set cpu mmu_state to TLBSTATE_OK
  73. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  74. * was in lazy tlb mode.
  75. * 1a3) update cpu active_mm
  76. * Now cpu0 accepts tlb flushes for the new mm.
  77. * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
  78. * Now the other cpus will send tlb flush ipis.
  79. * 1a4) change cr3.
  80. * 1b) thread switch without mm change
  81. * cpu active_mm is correct, cpu0 already handles
  82. * flush ipis.
  83. * 1b1) set cpu mmu_state to TLBSTATE_OK
  84. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  85. * Atomically set the bit [other cpus will start sending flush ipis],
  86. * and test the bit.
  87. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  88. * 2) switch %%esp, ie current
  89. *
  90. * The interrupt must handle 2 special cases:
  91. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  92. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  93. * runs in kernel space, the cpu could load tlb entries for user space
  94. * pages.
  95. *
  96. * The good news is that cpu mmu_state is local to each cpu, no
  97. * write/read ordering problems.
  98. */
  99. /*
  100. * TLB flush IPI:
  101. *
  102. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  103. * 2) Leave the mm if we are in the lazy tlb mode.
  104. *
  105. * Interrupts are disabled.
  106. */
  107. /*
  108. * FIXME: use of asmlinkage is not consistent. On x86_64 it's noop
  109. * but still used for documentation purpose but the usage is slightly
  110. * inconsistent. On x86_32, asmlinkage is regparm(0) but interrupt
  111. * entry calls in with the first parameter in %eax. Maybe define
  112. * intrlinkage?
  113. */
  114. #ifdef CONFIG_X86_64
  115. asmlinkage
  116. #endif
  117. void smp_invalidate_interrupt(struct pt_regs *regs)
  118. {
  119. unsigned int cpu;
  120. unsigned int sender;
  121. union smp_flush_state *f;
  122. cpu = smp_processor_id();
  123. /*
  124. * orig_rax contains the negated interrupt vector.
  125. * Use that to determine where the sender put the data.
  126. */
  127. sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START;
  128. f = &flush_state[sender];
  129. if (!cpumask_test_cpu(cpu, to_cpumask(f->flush_cpumask)))
  130. goto out;
  131. /*
  132. * This was a BUG() but until someone can quote me the
  133. * line from the intel manual that guarantees an IPI to
  134. * multiple CPUs is retried _only_ on the erroring CPUs
  135. * its staying as a return
  136. *
  137. * BUG();
  138. */
  139. if (f->flush_mm == percpu_read(cpu_tlbstate.active_mm)) {
  140. if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
  141. if (f->flush_va == TLB_FLUSH_ALL)
  142. local_flush_tlb();
  143. else
  144. __flush_tlb_one(f->flush_va);
  145. } else
  146. leave_mm(cpu);
  147. }
  148. out:
  149. ack_APIC_irq();
  150. smp_mb__before_clear_bit();
  151. cpumask_clear_cpu(cpu, to_cpumask(f->flush_cpumask));
  152. smp_mb__after_clear_bit();
  153. inc_irq_stat(irq_tlb_count);
  154. }
  155. static void flush_tlb_others_ipi(const struct cpumask *cpumask,
  156. struct mm_struct *mm, unsigned long va)
  157. {
  158. unsigned int sender;
  159. union smp_flush_state *f;
  160. /* Caller has disabled preemption */
  161. sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
  162. f = &flush_state[sender];
  163. /*
  164. * Could avoid this lock when
  165. * num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
  166. * probably not worth checking this for a cache-hot lock.
  167. */
  168. raw_spin_lock(&f->tlbstate_lock);
  169. f->flush_mm = mm;
  170. f->flush_va = va;
  171. if (cpumask_andnot(to_cpumask(f->flush_cpumask), cpumask, cpumask_of(smp_processor_id()))) {
  172. /*
  173. * We have to send the IPI only to
  174. * CPUs affected.
  175. */
  176. apic->send_IPI_mask(to_cpumask(f->flush_cpumask),
  177. INVALIDATE_TLB_VECTOR_START + sender);
  178. while (!cpumask_empty(to_cpumask(f->flush_cpumask)))
  179. cpu_relax();
  180. }
  181. f->flush_mm = NULL;
  182. f->flush_va = 0;
  183. raw_spin_unlock(&f->tlbstate_lock);
  184. }
  185. void native_flush_tlb_others(const struct cpumask *cpumask,
  186. struct mm_struct *mm, unsigned long va)
  187. {
  188. if (is_uv_system()) {
  189. unsigned int cpu;
  190. cpu = get_cpu();
  191. cpumask = uv_flush_tlb_others(cpumask, mm, va, cpu);
  192. if (cpumask)
  193. flush_tlb_others_ipi(cpumask, mm, va);
  194. put_cpu();
  195. return;
  196. }
  197. flush_tlb_others_ipi(cpumask, mm, va);
  198. }
  199. static int __cpuinit init_smp_flush(void)
  200. {
  201. int i;
  202. for (i = 0; i < ARRAY_SIZE(flush_state); i++)
  203. raw_spin_lock_init(&flush_state[i].tlbstate_lock);
  204. return 0;
  205. }
  206. core_initcall(init_smp_flush);
  207. void flush_tlb_current_task(void)
  208. {
  209. struct mm_struct *mm = current->mm;
  210. preempt_disable();
  211. local_flush_tlb();
  212. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  213. flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);
  214. preempt_enable();
  215. }
  216. void flush_tlb_mm(struct mm_struct *mm)
  217. {
  218. preempt_disable();
  219. if (current->active_mm == mm) {
  220. if (current->mm)
  221. local_flush_tlb();
  222. else
  223. leave_mm(smp_processor_id());
  224. }
  225. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  226. flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);
  227. preempt_enable();
  228. }
  229. void flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
  230. {
  231. struct mm_struct *mm = vma->vm_mm;
  232. preempt_disable();
  233. if (current->active_mm == mm) {
  234. if (current->mm)
  235. __flush_tlb_one(va);
  236. else
  237. leave_mm(smp_processor_id());
  238. }
  239. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  240. flush_tlb_others(mm_cpumask(mm), mm, va);
  241. preempt_enable();
  242. }
  243. static void do_flush_tlb_all(void *info)
  244. {
  245. __flush_tlb_all();
  246. if (percpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
  247. leave_mm(smp_processor_id());
  248. }
  249. void flush_tlb_all(void)
  250. {
  251. on_each_cpu(do_flush_tlb_all, NULL, 1);
  252. }