tlb_32.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * We have to send the IPI only to
  142. * CPUs affected.
  143. */
  144. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR);
  145. while (!cpus_empty(flush_cpumask))
  146. /* nothing. lockup detection does not belong here */
  147. cpu_relax();
  148. flush_mm = NULL;
  149. flush_va = 0;
  150. spin_unlock(&tlbstate_lock);
  151. }
  152. void flush_tlb_current_task(void)
  153. {
  154. struct mm_struct *mm = current->mm;
  155. cpumask_t cpu_mask;
  156. preempt_disable();
  157. cpu_mask = mm->cpu_vm_mask;
  158. cpu_clear(smp_processor_id(), cpu_mask);
  159. local_flush_tlb();
  160. if (!cpus_empty(cpu_mask))
  161. flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
  162. preempt_enable();
  163. }
  164. void flush_tlb_mm(struct mm_struct *mm)
  165. {
  166. cpumask_t cpu_mask;
  167. preempt_disable();
  168. cpu_mask = mm->cpu_vm_mask;
  169. cpu_clear(smp_processor_id(), cpu_mask);
  170. if (current->active_mm == mm) {
  171. if (current->mm)
  172. local_flush_tlb();
  173. else
  174. leave_mm(smp_processor_id());
  175. }
  176. if (!cpus_empty(cpu_mask))
  177. flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
  178. preempt_enable();
  179. }
  180. void flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
  181. {
  182. struct mm_struct *mm = vma->vm_mm;
  183. cpumask_t cpu_mask;
  184. preempt_disable();
  185. cpu_mask = mm->cpu_vm_mask;
  186. cpu_clear(smp_processor_id(), cpu_mask);
  187. if (current->active_mm == mm) {
  188. if (current->mm)
  189. __flush_tlb_one(va);
  190. else
  191. leave_mm(smp_processor_id());
  192. }
  193. if (!cpus_empty(cpu_mask))
  194. flush_tlb_others(cpu_mask, mm, va);
  195. preempt_enable();
  196. }
  197. EXPORT_SYMBOL(flush_tlb_page);
  198. static void do_flush_tlb_all(void *info)
  199. {
  200. unsigned long cpu = smp_processor_id();
  201. __flush_tlb_all();
  202. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_LAZY)
  203. leave_mm(cpu);
  204. }
  205. void flush_tlb_all(void)
  206. {
  207. on_each_cpu(do_flush_tlb_all, NULL, 1);
  208. }