tlb.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #include <linux/init.h>
  2. #include <linux/mm.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/smp.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/module.h>
  7. #include <linux/cpu.h>
  8. #include <asm/tlbflush.h>
  9. #include <asm/mmu_context.h>
  10. #include <asm/cache.h>
  11. #include <asm/apic.h>
  12. #include <asm/uv/uv.h>
  13. DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate)
  14. = { &init_mm, 0, };
  15. /*
  16. * Smarter SMP flushing macros.
  17. * c/o Linus Torvalds.
  18. *
  19. * These mean you can really definitely utterly forget about
  20. * writing to user space from interrupts. (Its not allowed anyway).
  21. *
  22. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  23. *
  24. * More scalable flush, from Andi Kleen
  25. *
  26. * To avoid global state use 8 different call vectors.
  27. * Each CPU uses a specific vector to trigger flushes on other
  28. * CPUs. Depending on the received vector the target CPUs look into
  29. * the right array slot for the flush data.
  30. *
  31. * With more than 8 CPUs they are hashed to the 8 available
  32. * vectors. The limited global vector space forces us to this right now.
  33. * In future when interrupts are split into per CPU domains this could be
  34. * fixed, at the cost of triggering multiple IPIs in some cases.
  35. */
  36. union smp_flush_state {
  37. struct {
  38. struct mm_struct *flush_mm;
  39. unsigned long flush_va;
  40. raw_spinlock_t tlbstate_lock;
  41. DECLARE_BITMAP(flush_cpumask, NR_CPUS);
  42. };
  43. char pad[INTERNODE_CACHE_BYTES];
  44. } ____cacheline_internodealigned_in_smp;
  45. /* State is put into the per CPU data section, but padded
  46. to a full cache line because other CPUs can access it and we don't
  47. want false sharing in the per cpu data segment. */
  48. static union smp_flush_state flush_state[NUM_INVALIDATE_TLB_VECTORS];
  49. static DEFINE_PER_CPU_READ_MOSTLY(int, tlb_vector_offset);
  50. /*
  51. * We cannot call mmdrop() because we are in interrupt context,
  52. * instead update mm->cpu_vm_mask.
  53. */
  54. void leave_mm(int cpu)
  55. {
  56. struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm);
  57. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
  58. BUG();
  59. if (cpumask_test_cpu(cpu, mm_cpumask(active_mm))) {
  60. cpumask_clear_cpu(cpu, mm_cpumask(active_mm));
  61. load_cr3(swapper_pg_dir);
  62. }
  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. /*
  112. * FIXME: use of asmlinkage is not consistent. On x86_64 it's noop
  113. * but still used for documentation purpose but the usage is slightly
  114. * inconsistent. On x86_32, asmlinkage is regparm(0) but interrupt
  115. * entry calls in with the first parameter in %eax. Maybe define
  116. * intrlinkage?
  117. */
  118. #ifdef CONFIG_X86_64
  119. asmlinkage
  120. #endif
  121. void smp_invalidate_interrupt(struct pt_regs *regs)
  122. {
  123. unsigned int cpu;
  124. unsigned int sender;
  125. union smp_flush_state *f;
  126. cpu = smp_processor_id();
  127. /*
  128. * orig_rax contains the negated interrupt vector.
  129. * Use that to determine where the sender put the data.
  130. */
  131. sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START;
  132. f = &flush_state[sender];
  133. if (!cpumask_test_cpu(cpu, to_cpumask(f->flush_cpumask)))
  134. goto out;
  135. /*
  136. * This was a BUG() but until someone can quote me the
  137. * line from the intel manual that guarantees an IPI to
  138. * multiple CPUs is retried _only_ on the erroring CPUs
  139. * its staying as a return
  140. *
  141. * BUG();
  142. */
  143. if (f->flush_mm == this_cpu_read(cpu_tlbstate.active_mm)) {
  144. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
  145. if (f->flush_va == TLB_FLUSH_ALL)
  146. local_flush_tlb();
  147. else
  148. __flush_tlb_one(f->flush_va);
  149. } else
  150. leave_mm(cpu);
  151. }
  152. out:
  153. ack_APIC_irq();
  154. smp_mb__before_clear_bit();
  155. cpumask_clear_cpu(cpu, to_cpumask(f->flush_cpumask));
  156. smp_mb__after_clear_bit();
  157. inc_irq_stat(irq_tlb_count);
  158. }
  159. static void flush_tlb_others_ipi(const struct cpumask *cpumask,
  160. struct mm_struct *mm, unsigned long va)
  161. {
  162. unsigned int sender;
  163. union smp_flush_state *f;
  164. /* Caller has disabled preemption */
  165. sender = this_cpu_read(tlb_vector_offset);
  166. f = &flush_state[sender];
  167. if (nr_cpu_ids > NUM_INVALIDATE_TLB_VECTORS)
  168. raw_spin_lock(&f->tlbstate_lock);
  169. f->flush_mm = mm;
  170. f->flush_va = va;
  171. if (cpumask_andnot(to_cpumask(f->flush_cpumask), cpumask, cpumask_of(smp_processor_id()))) {
  172. /*
  173. * We have to send the IPI only to
  174. * CPUs affected.
  175. */
  176. apic->send_IPI_mask(to_cpumask(f->flush_cpumask),
  177. INVALIDATE_TLB_VECTOR_START + sender);
  178. while (!cpumask_empty(to_cpumask(f->flush_cpumask)))
  179. cpu_relax();
  180. }
  181. f->flush_mm = NULL;
  182. f->flush_va = 0;
  183. if (nr_cpu_ids > NUM_INVALIDATE_TLB_VECTORS)
  184. raw_spin_unlock(&f->tlbstate_lock);
  185. }
  186. void native_flush_tlb_others(const struct cpumask *cpumask,
  187. struct mm_struct *mm, unsigned long va)
  188. {
  189. if (is_uv_system()) {
  190. unsigned int cpu;
  191. cpu = smp_processor_id();
  192. cpumask = uv_flush_tlb_others(cpumask, mm, va, cpu);
  193. if (cpumask)
  194. flush_tlb_others_ipi(cpumask, mm, va);
  195. return;
  196. }
  197. flush_tlb_others_ipi(cpumask, mm, va);
  198. }
  199. static void __cpuinit calculate_tlb_offset(void)
  200. {
  201. int cpu, node, nr_node_vecs, idx = 0;
  202. /*
  203. * we are changing tlb_vector_offset for each CPU in runtime, but this
  204. * will not cause inconsistency, as the write is atomic under X86. we
  205. * might see more lock contentions in a short time, but after all CPU's
  206. * tlb_vector_offset are changed, everything should go normal
  207. *
  208. * Note: if NUM_INVALIDATE_TLB_VECTORS % nr_online_nodes !=0, we might
  209. * waste some vectors.
  210. **/
  211. if (nr_online_nodes > NUM_INVALIDATE_TLB_VECTORS)
  212. nr_node_vecs = 1;
  213. else
  214. nr_node_vecs = NUM_INVALIDATE_TLB_VECTORS/nr_online_nodes;
  215. for_each_online_node(node) {
  216. int node_offset = (idx % NUM_INVALIDATE_TLB_VECTORS) *
  217. nr_node_vecs;
  218. int cpu_offset = 0;
  219. for_each_cpu(cpu, cpumask_of_node(node)) {
  220. per_cpu(tlb_vector_offset, cpu) = node_offset +
  221. cpu_offset;
  222. cpu_offset++;
  223. cpu_offset = cpu_offset % nr_node_vecs;
  224. }
  225. idx++;
  226. }
  227. }
  228. static int __cpuinit tlb_cpuhp_notify(struct notifier_block *n,
  229. unsigned long action, void *hcpu)
  230. {
  231. switch (action & 0xf) {
  232. case CPU_ONLINE:
  233. case CPU_DEAD:
  234. calculate_tlb_offset();
  235. }
  236. return NOTIFY_OK;
  237. }
  238. static int __cpuinit init_smp_flush(void)
  239. {
  240. int i;
  241. for (i = 0; i < ARRAY_SIZE(flush_state); i++)
  242. raw_spin_lock_init(&flush_state[i].tlbstate_lock);
  243. calculate_tlb_offset();
  244. hotcpu_notifier(tlb_cpuhp_notify, 0);
  245. return 0;
  246. }
  247. core_initcall(init_smp_flush);
  248. void flush_tlb_current_task(void)
  249. {
  250. struct mm_struct *mm = current->mm;
  251. preempt_disable();
  252. local_flush_tlb();
  253. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  254. flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);
  255. preempt_enable();
  256. }
  257. void flush_tlb_mm(struct mm_struct *mm)
  258. {
  259. preempt_disable();
  260. if (current->active_mm == mm) {
  261. if (current->mm)
  262. local_flush_tlb();
  263. else
  264. leave_mm(smp_processor_id());
  265. }
  266. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  267. flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);
  268. preempt_enable();
  269. }
  270. void flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
  271. {
  272. struct mm_struct *mm = vma->vm_mm;
  273. preempt_disable();
  274. if (current->active_mm == mm) {
  275. if (current->mm)
  276. __flush_tlb_one(va);
  277. else
  278. leave_mm(smp_processor_id());
  279. }
  280. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  281. flush_tlb_others(mm_cpumask(mm), mm, va);
  282. preempt_enable();
  283. }
  284. static void do_flush_tlb_all(void *info)
  285. {
  286. __flush_tlb_all();
  287. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
  288. leave_mm(smp_processor_id());
  289. }
  290. void flush_tlb_all(void)
  291. {
  292. on_each_cpu(do_flush_tlb_all, NULL, 1);
  293. }