tlb_64.c 6.9 KB

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