tlb_32.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include <linux/spinlock.h>
  2. #include <linux/cpu.h>
  3. #include <linux/interrupt.h>
  4. #include <asm/tlbflush.h>
  5. DEFINE_PER_CPU(struct tlb_state, cpu_tlbstate)
  6. ____cacheline_aligned = { &init_mm, 0, };
  7. /* must come after the send_IPI functions above for inlining */
  8. #include <mach_ipi.h>
  9. /*
  10. * Smarter SMP flushing macros.
  11. * c/o Linus Torvalds.
  12. *
  13. * These mean you can really definitely utterly forget about
  14. * writing to user space from interrupts. (Its not allowed anyway).
  15. *
  16. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  17. */
  18. static cpumask_t flush_cpumask;
  19. static struct mm_struct *flush_mm;
  20. static unsigned long flush_va;
  21. static DEFINE_SPINLOCK(tlbstate_lock);
  22. /*
  23. * We cannot call mmdrop() because we are in interrupt context,
  24. * instead update mm->cpu_vm_mask.
  25. *
  26. * We need to reload %cr3 since the page tables may be going
  27. * away from under us..
  28. */
  29. void leave_mm(int cpu)
  30. {
  31. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK)
  32. BUG();
  33. cpu_clear(cpu, per_cpu(cpu_tlbstate, cpu).active_mm->cpu_vm_mask);
  34. load_cr3(swapper_pg_dir);
  35. }
  36. EXPORT_SYMBOL_GPL(leave_mm);
  37. /*
  38. *
  39. * The flush IPI assumes that a thread switch happens in this order:
  40. * [cpu0: the cpu that switches]
  41. * 1) switch_mm() either 1a) or 1b)
  42. * 1a) thread switch to a different mm
  43. * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
  44. * Stop ipi delivery for the old mm. This is not synchronized with
  45. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  46. * for the wrong mm, and in the worst case we perform a superfluous
  47. * tlb flush.
  48. * 1a2) set cpu_tlbstate to TLBSTATE_OK
  49. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  50. * was in lazy tlb mode.
  51. * 1a3) update cpu_tlbstate[].active_mm
  52. * Now cpu0 accepts tlb flushes for the new mm.
  53. * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
  54. * Now the other cpus will send tlb flush ipis.
  55. * 1a4) change cr3.
  56. * 1b) thread switch without mm change
  57. * cpu_tlbstate[].active_mm is correct, cpu0 already handles
  58. * flush ipis.
  59. * 1b1) set cpu_tlbstate to TLBSTATE_OK
  60. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  61. * Atomically set the bit [other cpus will start sending flush ipis],
  62. * and test the bit.
  63. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  64. * 2) switch %%esp, ie current
  65. *
  66. * The interrupt must handle 2 special cases:
  67. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  68. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  69. * runs in kernel space, the cpu could load tlb entries for user space
  70. * pages.
  71. *
  72. * The good news is that cpu_tlbstate is local to each cpu, no
  73. * write/read ordering problems.
  74. */
  75. /*
  76. * TLB flush IPI:
  77. *
  78. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  79. * 2) Leave the mm if we are in the lazy tlb mode.
  80. */
  81. void smp_invalidate_interrupt(struct pt_regs *regs)
  82. {
  83. unsigned long cpu;
  84. cpu = get_cpu();
  85. if (!cpu_isset(cpu, flush_cpumask))
  86. goto out;
  87. /*
  88. * This was a BUG() but until someone can quote me the
  89. * line from the intel manual that guarantees an IPI to
  90. * multiple CPUs is retried _only_ on the erroring CPUs
  91. * its staying as a return
  92. *
  93. * BUG();
  94. */
  95. if (flush_mm == per_cpu(cpu_tlbstate, cpu).active_mm) {
  96. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK) {
  97. if (flush_va == TLB_FLUSH_ALL)
  98. local_flush_tlb();
  99. else
  100. __flush_tlb_one(flush_va);
  101. } else
  102. leave_mm(cpu);
  103. }
  104. ack_APIC_irq();
  105. smp_mb__before_clear_bit();
  106. cpu_clear(cpu, flush_cpumask);
  107. smp_mb__after_clear_bit();
  108. out:
  109. put_cpu_no_resched();
  110. __get_cpu_var(irq_stat).irq_tlb_count++;
  111. }
  112. void native_flush_tlb_others(const cpumask_t *cpumaskp, struct mm_struct *mm,
  113. unsigned long va)
  114. {
  115. cpumask_t cpumask = *cpumaskp;
  116. /*
  117. * A couple of (to be removed) sanity checks:
  118. *
  119. * - current CPU must not be in mask
  120. * - mask must exist :)
  121. */
  122. BUG_ON(cpus_empty(cpumask));
  123. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  124. BUG_ON(!mm);
  125. #ifdef CONFIG_HOTPLUG_CPU
  126. /* If a CPU which we ran on has gone down, OK. */
  127. cpus_and(cpumask, cpumask, cpu_online_map);
  128. if (unlikely(cpus_empty(cpumask)))
  129. return;
  130. #endif
  131. /*
  132. * i'm not happy about this global shared spinlock in the
  133. * MM hot path, but we'll see how contended it is.
  134. * AK: x86-64 has a faster method that could be ported.
  135. */
  136. spin_lock(&tlbstate_lock);
  137. flush_mm = mm;
  138. flush_va = va;
  139. cpus_or(flush_cpumask, cpumask, flush_cpumask);
  140. /*
  141. * Make the above memory operations globally visible before
  142. * sending the IPI.
  143. */
  144. smp_mb();
  145. /*
  146. * We have to send the IPI only to
  147. * CPUs affected.
  148. */
  149. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR);
  150. while (!cpus_empty(flush_cpumask))
  151. /* nothing. lockup detection does not belong here */
  152. cpu_relax();
  153. flush_mm = NULL;
  154. flush_va = 0;
  155. spin_unlock(&tlbstate_lock);
  156. }
  157. void flush_tlb_current_task(void)
  158. {
  159. struct mm_struct *mm = current->mm;
  160. cpumask_t cpu_mask;
  161. preempt_disable();
  162. cpu_mask = mm->cpu_vm_mask;
  163. cpu_clear(smp_processor_id(), cpu_mask);
  164. local_flush_tlb();
  165. if (!cpus_empty(cpu_mask))
  166. flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
  167. preempt_enable();
  168. }
  169. void flush_tlb_mm(struct mm_struct *mm)
  170. {
  171. cpumask_t cpu_mask;
  172. preempt_disable();
  173. cpu_mask = mm->cpu_vm_mask;
  174. cpu_clear(smp_processor_id(), cpu_mask);
  175. if (current->active_mm == mm) {
  176. if (current->mm)
  177. local_flush_tlb();
  178. else
  179. leave_mm(smp_processor_id());
  180. }
  181. if (!cpus_empty(cpu_mask))
  182. flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
  183. preempt_enable();
  184. }
  185. void flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
  186. {
  187. struct mm_struct *mm = vma->vm_mm;
  188. cpumask_t cpu_mask;
  189. preempt_disable();
  190. cpu_mask = mm->cpu_vm_mask;
  191. cpu_clear(smp_processor_id(), cpu_mask);
  192. if (current->active_mm == mm) {
  193. if (current->mm)
  194. __flush_tlb_one(va);
  195. else
  196. leave_mm(smp_processor_id());
  197. }
  198. if (!cpus_empty(cpu_mask))
  199. flush_tlb_others(cpu_mask, mm, va);
  200. preempt_enable();
  201. }
  202. EXPORT_SYMBOL(flush_tlb_page);
  203. static void do_flush_tlb_all(void *info)
  204. {
  205. unsigned long cpu = smp_processor_id();
  206. __flush_tlb_all();
  207. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_LAZY)
  208. leave_mm(cpu);
  209. }
  210. void flush_tlb_all(void)
  211. {
  212. on_each_cpu(do_flush_tlb_all, NULL, 1);
  213. }
  214. void reset_lazy_tlbstate(void)
  215. {
  216. int cpu = raw_smp_processor_id();
  217. per_cpu(cpu_tlbstate, cpu).state = 0;
  218. per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
  219. }