tlb_64.c 7.1 KB

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