tlb.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #include <linux/debugfs.h>
  14. /*
  15. * Smarter SMP flushing macros.
  16. * c/o Linus Torvalds.
  17. *
  18. * These mean you can really definitely utterly forget about
  19. * writing to user space from interrupts. (Its not allowed anyway).
  20. *
  21. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  22. *
  23. * More scalable flush, from Andi Kleen
  24. *
  25. * Implement flush IPI by CALL_FUNCTION_VECTOR, Alex Shi
  26. */
  27. struct flush_tlb_info {
  28. struct mm_struct *flush_mm;
  29. unsigned long flush_start;
  30. unsigned long flush_end;
  31. };
  32. /*
  33. * We cannot call mmdrop() because we are in interrupt context,
  34. * instead update mm->cpu_vm_mask.
  35. */
  36. void leave_mm(int cpu)
  37. {
  38. struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm);
  39. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
  40. BUG();
  41. if (cpumask_test_cpu(cpu, mm_cpumask(active_mm))) {
  42. cpumask_clear_cpu(cpu, mm_cpumask(active_mm));
  43. load_cr3(swapper_pg_dir);
  44. /*
  45. * This gets called in the idle path where RCU
  46. * functions differently. Tracing normally
  47. * uses RCU, so we have to call the tracepoint
  48. * specially here.
  49. */
  50. trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  51. }
  52. }
  53. EXPORT_SYMBOL_GPL(leave_mm);
  54. /*
  55. * The flush IPI assumes that a thread switch happens in this order:
  56. * [cpu0: the cpu that switches]
  57. * 1) switch_mm() either 1a) or 1b)
  58. * 1a) thread switch to a different mm
  59. * 1a1) set cpu_tlbstate to TLBSTATE_OK
  60. * Now the tlb flush NMI handler flush_tlb_func won't call leave_mm
  61. * if cpu0 was in lazy tlb mode.
  62. * 1a2) update cpu active_mm
  63. * Now cpu0 accepts tlb flushes for the new mm.
  64. * 1a3) cpu_set(cpu, new_mm->cpu_vm_mask);
  65. * Now the other cpus will send tlb flush ipis.
  66. * 1a4) change cr3.
  67. * 1a5) cpu_clear(cpu, old_mm->cpu_vm_mask);
  68. * Stop ipi delivery for the old mm. This is not synchronized with
  69. * the other cpus, but flush_tlb_func ignore flush ipis for the wrong
  70. * mm, and in the worst case we perform a superfluous tlb flush.
  71. * 1b) thread switch without mm change
  72. * cpu active_mm is correct, cpu0 already handles flush ipis.
  73. * 1b1) set cpu_tlbstate to TLBSTATE_OK
  74. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  75. * Atomically set the bit [other cpus will start sending flush ipis],
  76. * and test the bit.
  77. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  78. * 2) switch %%esp, ie current
  79. *
  80. * The interrupt must handle 2 special cases:
  81. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  82. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  83. * runs in kernel space, the cpu could load tlb entries for user space
  84. * pages.
  85. *
  86. * The good news is that cpu_tlbstate is local to each cpu, no
  87. * write/read ordering problems.
  88. */
  89. /*
  90. * TLB flush funcation:
  91. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  92. * 2) Leave the mm if we are in the lazy tlb mode.
  93. */
  94. static void flush_tlb_func(void *info)
  95. {
  96. struct flush_tlb_info *f = info;
  97. inc_irq_stat(irq_tlb_count);
  98. if (f->flush_mm != this_cpu_read(cpu_tlbstate.active_mm))
  99. return;
  100. if (!f->flush_end)
  101. f->flush_end = f->flush_start + PAGE_SIZE;
  102. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  103. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
  104. if (f->flush_end == TLB_FLUSH_ALL) {
  105. local_flush_tlb();
  106. trace_tlb_flush(TLB_REMOTE_SHOOTDOWN, TLB_FLUSH_ALL);
  107. } else {
  108. unsigned long addr;
  109. unsigned long nr_pages =
  110. (f->flush_end - f->flush_start) / PAGE_SIZE;
  111. addr = f->flush_start;
  112. while (addr < f->flush_end) {
  113. __flush_tlb_single(addr);
  114. addr += PAGE_SIZE;
  115. }
  116. trace_tlb_flush(TLB_REMOTE_SHOOTDOWN, nr_pages);
  117. }
  118. } else
  119. leave_mm(smp_processor_id());
  120. }
  121. void native_flush_tlb_others(const struct cpumask *cpumask,
  122. struct mm_struct *mm, unsigned long start,
  123. unsigned long end)
  124. {
  125. struct flush_tlb_info info;
  126. info.flush_mm = mm;
  127. info.flush_start = start;
  128. info.flush_end = end;
  129. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  130. trace_tlb_flush(TLB_REMOTE_SEND_IPI, end - start);
  131. if (is_uv_system()) {
  132. unsigned int cpu;
  133. cpu = smp_processor_id();
  134. cpumask = uv_flush_tlb_others(cpumask, mm, start, end, cpu);
  135. if (cpumask)
  136. smp_call_function_many(cpumask, flush_tlb_func,
  137. &info, 1);
  138. return;
  139. }
  140. smp_call_function_many(cpumask, flush_tlb_func, &info, 1);
  141. }
  142. void flush_tlb_current_task(void)
  143. {
  144. struct mm_struct *mm = current->mm;
  145. preempt_disable();
  146. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  147. /* This is an implicit full barrier that synchronizes with switch_mm. */
  148. local_flush_tlb();
  149. trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
  150. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  151. flush_tlb_others(mm_cpumask(mm), mm, 0UL, TLB_FLUSH_ALL);
  152. preempt_enable();
  153. }
  154. /*
  155. * See Documentation/x86/tlb.txt for details. We choose 33
  156. * because it is large enough to cover the vast majority (at
  157. * least 95%) of allocations, and is small enough that we are
  158. * confident it will not cause too much overhead. Each single
  159. * flush is about 100 ns, so this caps the maximum overhead at
  160. * _about_ 3,000 ns.
  161. *
  162. * This is in units of pages.
  163. */
  164. static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
  165. void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  166. unsigned long end, unsigned long vmflag)
  167. {
  168. unsigned long addr;
  169. /* do a global flush by default */
  170. unsigned long base_pages_to_flush = TLB_FLUSH_ALL;
  171. preempt_disable();
  172. if (current->active_mm != mm) {
  173. /* Synchronize with switch_mm. */
  174. smp_mb();
  175. goto out;
  176. }
  177. if (!current->mm) {
  178. leave_mm(smp_processor_id());
  179. /* Synchronize with switch_mm. */
  180. smp_mb();
  181. goto out;
  182. }
  183. if ((end != TLB_FLUSH_ALL) && !(vmflag & VM_HUGETLB))
  184. base_pages_to_flush = (end - start) >> PAGE_SHIFT;
  185. /*
  186. * Both branches below are implicit full barriers (MOV to CR or
  187. * INVLPG) that synchronize with switch_mm.
  188. */
  189. if (base_pages_to_flush > tlb_single_page_flush_ceiling) {
  190. base_pages_to_flush = TLB_FLUSH_ALL;
  191. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  192. local_flush_tlb();
  193. } else {
  194. /* flush range by one by one 'invlpg' */
  195. for (addr = start; addr < end; addr += PAGE_SIZE) {
  196. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
  197. __flush_tlb_single(addr);
  198. }
  199. }
  200. trace_tlb_flush(TLB_LOCAL_MM_SHOOTDOWN, base_pages_to_flush);
  201. out:
  202. if (base_pages_to_flush == TLB_FLUSH_ALL) {
  203. start = 0UL;
  204. end = TLB_FLUSH_ALL;
  205. }
  206. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  207. flush_tlb_others(mm_cpumask(mm), mm, start, end);
  208. preempt_enable();
  209. }
  210. void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
  211. {
  212. struct mm_struct *mm = vma->vm_mm;
  213. preempt_disable();
  214. if (current->active_mm == mm) {
  215. if (current->mm) {
  216. /*
  217. * Implicit full barrier (INVLPG) that synchronizes
  218. * with switch_mm.
  219. */
  220. __flush_tlb_one(start);
  221. } else {
  222. leave_mm(smp_processor_id());
  223. /* Synchronize with switch_mm. */
  224. smp_mb();
  225. }
  226. }
  227. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  228. flush_tlb_others(mm_cpumask(mm), mm, start, 0UL);
  229. preempt_enable();
  230. }
  231. static void do_flush_tlb_all(void *info)
  232. {
  233. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  234. __flush_tlb_all();
  235. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
  236. leave_mm(smp_processor_id());
  237. }
  238. void flush_tlb_all(void)
  239. {
  240. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  241. on_each_cpu(do_flush_tlb_all, NULL, 1);
  242. }
  243. static void do_kernel_range_flush(void *info)
  244. {
  245. struct flush_tlb_info *f = info;
  246. unsigned long addr;
  247. /* flush range by one by one 'invlpg' */
  248. for (addr = f->flush_start; addr < f->flush_end; addr += PAGE_SIZE)
  249. __flush_tlb_single(addr);
  250. }
  251. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  252. {
  253. /* Balance as user space task's flush, a bit conservative */
  254. if (end == TLB_FLUSH_ALL ||
  255. (end - start) > tlb_single_page_flush_ceiling * PAGE_SIZE) {
  256. on_each_cpu(do_flush_tlb_all, NULL, 1);
  257. } else {
  258. struct flush_tlb_info info;
  259. info.flush_start = start;
  260. info.flush_end = end;
  261. on_each_cpu(do_kernel_range_flush, &info, 1);
  262. }
  263. }
  264. static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
  265. size_t count, loff_t *ppos)
  266. {
  267. char buf[32];
  268. unsigned int len;
  269. len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling);
  270. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  271. }
  272. static ssize_t tlbflush_write_file(struct file *file,
  273. const char __user *user_buf, size_t count, loff_t *ppos)
  274. {
  275. char buf[32];
  276. ssize_t len;
  277. int ceiling;
  278. len = min(count, sizeof(buf) - 1);
  279. if (copy_from_user(buf, user_buf, len))
  280. return -EFAULT;
  281. buf[len] = '\0';
  282. if (kstrtoint(buf, 0, &ceiling))
  283. return -EINVAL;
  284. if (ceiling < 0)
  285. return -EINVAL;
  286. tlb_single_page_flush_ceiling = ceiling;
  287. return count;
  288. }
  289. static const struct file_operations fops_tlbflush = {
  290. .read = tlbflush_read_file,
  291. .write = tlbflush_write_file,
  292. .llseek = default_llseek,
  293. };
  294. static int __init create_tlb_single_page_flush_ceiling(void)
  295. {
  296. debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR,
  297. arch_debugfs_dir, NULL, &fops_tlbflush);
  298. return 0;
  299. }
  300. late_initcall(create_tlb_single_page_flush_ceiling);