tlb.c 8.3 KB

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