tlb_64.c 7.5 KB

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