tlb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 <linux/debugfs.h>
  9. #include <asm/tlbflush.h>
  10. #include <asm/mmu_context.h>
  11. #include <asm/nospec-branch.h>
  12. #include <asm/cache.h>
  13. #include <asm/apic.h>
  14. #include <asm/uv/uv.h>
  15. #include <asm/kaiser.h>
  16. /*
  17. * TLB flushing, formerly SMP-only
  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. atomic64_t last_mm_ctx_id = ATOMIC64_INIT(1);
  30. struct flush_tlb_info {
  31. struct mm_struct *flush_mm;
  32. unsigned long flush_start;
  33. unsigned long flush_end;
  34. };
  35. static void load_new_mm_cr3(pgd_t *pgdir)
  36. {
  37. unsigned long new_mm_cr3 = __pa(pgdir);
  38. if (kaiser_enabled) {
  39. /*
  40. * We reuse the same PCID for different tasks, so we must
  41. * flush all the entries for the PCID out when we change tasks.
  42. * Flush KERN below, flush USER when returning to userspace in
  43. * kaiser's SWITCH_USER_CR3 (_SWITCH_TO_USER_CR3) macro.
  44. *
  45. * invpcid_flush_single_context(X86_CR3_PCID_ASID_USER) could
  46. * do it here, but can only be used if X86_FEATURE_INVPCID is
  47. * available - and many machines support pcid without invpcid.
  48. *
  49. * If X86_CR3_PCID_KERN_FLUSH actually added something, then it
  50. * would be needed in the write_cr3() below - if PCIDs enabled.
  51. */
  52. BUILD_BUG_ON(X86_CR3_PCID_KERN_FLUSH);
  53. kaiser_flush_tlb_on_return_to_user();
  54. }
  55. /*
  56. * Caution: many callers of this function expect
  57. * that load_cr3() is serializing and orders TLB
  58. * fills with respect to the mm_cpumask writes.
  59. */
  60. write_cr3(new_mm_cr3);
  61. }
  62. /*
  63. * We cannot call mmdrop() because we are in interrupt context,
  64. * instead update mm->cpu_vm_mask.
  65. */
  66. void leave_mm(int cpu)
  67. {
  68. struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm);
  69. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
  70. BUG();
  71. if (cpumask_test_cpu(cpu, mm_cpumask(active_mm))) {
  72. cpumask_clear_cpu(cpu, mm_cpumask(active_mm));
  73. load_new_mm_cr3(swapper_pg_dir);
  74. /*
  75. * This gets called in the idle path where RCU
  76. * functions differently. Tracing normally
  77. * uses RCU, so we have to call the tracepoint
  78. * specially here.
  79. */
  80. trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  81. }
  82. }
  83. EXPORT_SYMBOL_GPL(leave_mm);
  84. void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  85. struct task_struct *tsk)
  86. {
  87. unsigned long flags;
  88. local_irq_save(flags);
  89. switch_mm_irqs_off(prev, next, tsk);
  90. local_irq_restore(flags);
  91. }
  92. void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  93. struct task_struct *tsk)
  94. {
  95. unsigned cpu = smp_processor_id();
  96. if (likely(prev != next)) {
  97. u64 last_ctx_id = this_cpu_read(cpu_tlbstate.last_ctx_id);
  98. /*
  99. * Avoid user/user BTB poisoning by flushing the branch
  100. * predictor when switching between processes. This stops
  101. * one process from doing Spectre-v2 attacks on another.
  102. *
  103. * As an optimization, flush indirect branches only when
  104. * switching into processes that disable dumping. This
  105. * protects high value processes like gpg, without having
  106. * too high performance overhead. IBPB is *expensive*!
  107. *
  108. * This will not flush branches when switching into kernel
  109. * threads. It will also not flush if we switch to idle
  110. * thread and back to the same process. It will flush if we
  111. * switch to a different non-dumpable process.
  112. */
  113. if (tsk && tsk->mm &&
  114. tsk->mm->context.ctx_id != last_ctx_id &&
  115. get_dumpable(tsk->mm) != SUID_DUMP_USER)
  116. indirect_branch_prediction_barrier();
  117. /*
  118. * Record last user mm's context id, so we can avoid
  119. * flushing branch buffer with IBPB if we switch back
  120. * to the same user.
  121. */
  122. if (next != &init_mm)
  123. this_cpu_write(cpu_tlbstate.last_ctx_id, next->context.ctx_id);
  124. this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
  125. this_cpu_write(cpu_tlbstate.active_mm, next);
  126. cpumask_set_cpu(cpu, mm_cpumask(next));
  127. /*
  128. * Re-load page tables.
  129. *
  130. * This logic has an ordering constraint:
  131. *
  132. * CPU 0: Write to a PTE for 'next'
  133. * CPU 0: load bit 1 in mm_cpumask. if nonzero, send IPI.
  134. * CPU 1: set bit 1 in next's mm_cpumask
  135. * CPU 1: load from the PTE that CPU 0 writes (implicit)
  136. *
  137. * We need to prevent an outcome in which CPU 1 observes
  138. * the new PTE value and CPU 0 observes bit 1 clear in
  139. * mm_cpumask. (If that occurs, then the IPI will never
  140. * be sent, and CPU 0's TLB will contain a stale entry.)
  141. *
  142. * The bad outcome can occur if either CPU's load is
  143. * reordered before that CPU's store, so both CPUs must
  144. * execute full barriers to prevent this from happening.
  145. *
  146. * Thus, switch_mm needs a full barrier between the
  147. * store to mm_cpumask and any operation that could load
  148. * from next->pgd. TLB fills are special and can happen
  149. * due to instruction fetches or for no reason at all,
  150. * and neither LOCK nor MFENCE orders them.
  151. * Fortunately, load_cr3() is serializing and gives the
  152. * ordering guarantee we need.
  153. *
  154. */
  155. load_new_mm_cr3(next->pgd);
  156. trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  157. /* Stop flush ipis for the previous mm */
  158. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  159. /* Load per-mm CR4 state */
  160. load_mm_cr4(next);
  161. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  162. /*
  163. * Load the LDT, if the LDT is different.
  164. *
  165. * It's possible that prev->context.ldt doesn't match
  166. * the LDT register. This can happen if leave_mm(prev)
  167. * was called and then modify_ldt changed
  168. * prev->context.ldt but suppressed an IPI to this CPU.
  169. * In this case, prev->context.ldt != NULL, because we
  170. * never set context.ldt to NULL while the mm still
  171. * exists. That means that next->context.ldt !=
  172. * prev->context.ldt, because mms never share an LDT.
  173. */
  174. if (unlikely(prev->context.ldt != next->context.ldt))
  175. load_mm_ldt(next);
  176. #endif
  177. } else {
  178. this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
  179. BUG_ON(this_cpu_read(cpu_tlbstate.active_mm) != next);
  180. if (!cpumask_test_cpu(cpu, mm_cpumask(next))) {
  181. /*
  182. * On established mms, the mm_cpumask is only changed
  183. * from irq context, from ptep_clear_flush() while in
  184. * lazy tlb mode, and here. Irqs are blocked during
  185. * schedule, protecting us from simultaneous changes.
  186. */
  187. cpumask_set_cpu(cpu, mm_cpumask(next));
  188. /*
  189. * We were in lazy tlb mode and leave_mm disabled
  190. * tlb flush IPI delivery. We must reload CR3
  191. * to make sure to use no freed page tables.
  192. *
  193. * As above, load_cr3() is serializing and orders TLB
  194. * fills with respect to the mm_cpumask write.
  195. */
  196. load_new_mm_cr3(next->pgd);
  197. trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  198. load_mm_cr4(next);
  199. load_mm_ldt(next);
  200. }
  201. }
  202. }
  203. /*
  204. * The flush IPI assumes that a thread switch happens in this order:
  205. * [cpu0: the cpu that switches]
  206. * 1) switch_mm() either 1a) or 1b)
  207. * 1a) thread switch to a different mm
  208. * 1a1) set cpu_tlbstate to TLBSTATE_OK
  209. * Now the tlb flush NMI handler flush_tlb_func won't call leave_mm
  210. * if cpu0 was in lazy tlb mode.
  211. * 1a2) update cpu active_mm
  212. * Now cpu0 accepts tlb flushes for the new mm.
  213. * 1a3) cpu_set(cpu, new_mm->cpu_vm_mask);
  214. * Now the other cpus will send tlb flush ipis.
  215. * 1a4) change cr3.
  216. * 1a5) cpu_clear(cpu, old_mm->cpu_vm_mask);
  217. * Stop ipi delivery for the old mm. This is not synchronized with
  218. * the other cpus, but flush_tlb_func ignore flush ipis for the wrong
  219. * mm, and in the worst case we perform a superfluous tlb flush.
  220. * 1b) thread switch without mm change
  221. * cpu active_mm is correct, cpu0 already handles flush ipis.
  222. * 1b1) set cpu_tlbstate to TLBSTATE_OK
  223. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  224. * Atomically set the bit [other cpus will start sending flush ipis],
  225. * and test the bit.
  226. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  227. * 2) switch %%esp, ie current
  228. *
  229. * The interrupt must handle 2 special cases:
  230. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  231. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  232. * runs in kernel space, the cpu could load tlb entries for user space
  233. * pages.
  234. *
  235. * The good news is that cpu_tlbstate is local to each cpu, no
  236. * write/read ordering problems.
  237. */
  238. /*
  239. * TLB flush funcation:
  240. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  241. * 2) Leave the mm if we are in the lazy tlb mode.
  242. */
  243. static void flush_tlb_func(void *info)
  244. {
  245. struct flush_tlb_info *f = info;
  246. inc_irq_stat(irq_tlb_count);
  247. if (f->flush_mm && f->flush_mm != this_cpu_read(cpu_tlbstate.active_mm))
  248. return;
  249. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  250. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
  251. if (f->flush_end == TLB_FLUSH_ALL) {
  252. local_flush_tlb();
  253. trace_tlb_flush(TLB_REMOTE_SHOOTDOWN, TLB_FLUSH_ALL);
  254. } else {
  255. unsigned long addr;
  256. unsigned long nr_pages =
  257. (f->flush_end - f->flush_start) / PAGE_SIZE;
  258. addr = f->flush_start;
  259. while (addr < f->flush_end) {
  260. __flush_tlb_single(addr);
  261. addr += PAGE_SIZE;
  262. }
  263. trace_tlb_flush(TLB_REMOTE_SHOOTDOWN, nr_pages);
  264. }
  265. } else
  266. leave_mm(smp_processor_id());
  267. }
  268. void native_flush_tlb_others(const struct cpumask *cpumask,
  269. struct mm_struct *mm, unsigned long start,
  270. unsigned long end)
  271. {
  272. struct flush_tlb_info info;
  273. info.flush_mm = mm;
  274. info.flush_start = start;
  275. info.flush_end = end;
  276. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  277. if (end == TLB_FLUSH_ALL)
  278. trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL);
  279. else
  280. trace_tlb_flush(TLB_REMOTE_SEND_IPI,
  281. (end - start) >> PAGE_SHIFT);
  282. if (is_uv_system()) {
  283. unsigned int cpu;
  284. cpu = smp_processor_id();
  285. cpumask = uv_flush_tlb_others(cpumask, mm, start, end, cpu);
  286. if (cpumask)
  287. smp_call_function_many(cpumask, flush_tlb_func,
  288. &info, 1);
  289. return;
  290. }
  291. smp_call_function_many(cpumask, flush_tlb_func, &info, 1);
  292. }
  293. /*
  294. * See Documentation/x86/tlb.txt for details. We choose 33
  295. * because it is large enough to cover the vast majority (at
  296. * least 95%) of allocations, and is small enough that we are
  297. * confident it will not cause too much overhead. Each single
  298. * flush is about 100 ns, so this caps the maximum overhead at
  299. * _about_ 3,000 ns.
  300. *
  301. * This is in units of pages.
  302. */
  303. static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
  304. void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  305. unsigned long end, unsigned long vmflag)
  306. {
  307. unsigned long addr;
  308. /* do a global flush by default */
  309. unsigned long base_pages_to_flush = TLB_FLUSH_ALL;
  310. preempt_disable();
  311. if ((end != TLB_FLUSH_ALL) && !(vmflag & VM_HUGETLB))
  312. base_pages_to_flush = (end - start) >> PAGE_SHIFT;
  313. if (base_pages_to_flush > tlb_single_page_flush_ceiling)
  314. base_pages_to_flush = TLB_FLUSH_ALL;
  315. if (current->active_mm != mm) {
  316. /* Synchronize with switch_mm. */
  317. smp_mb();
  318. goto out;
  319. }
  320. if (!current->mm) {
  321. leave_mm(smp_processor_id());
  322. /* Synchronize with switch_mm. */
  323. smp_mb();
  324. goto out;
  325. }
  326. /*
  327. * Both branches below are implicit full barriers (MOV to CR or
  328. * INVLPG) that synchronize with switch_mm.
  329. */
  330. if (base_pages_to_flush == TLB_FLUSH_ALL) {
  331. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  332. local_flush_tlb();
  333. } else {
  334. /* flush range by one by one 'invlpg' */
  335. for (addr = start; addr < end; addr += PAGE_SIZE) {
  336. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
  337. __flush_tlb_single(addr);
  338. }
  339. }
  340. trace_tlb_flush(TLB_LOCAL_MM_SHOOTDOWN, base_pages_to_flush);
  341. out:
  342. if (base_pages_to_flush == TLB_FLUSH_ALL) {
  343. start = 0UL;
  344. end = TLB_FLUSH_ALL;
  345. }
  346. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  347. flush_tlb_others(mm_cpumask(mm), mm, start, end);
  348. preempt_enable();
  349. }
  350. static void do_flush_tlb_all(void *info)
  351. {
  352. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  353. __flush_tlb_all();
  354. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
  355. leave_mm(smp_processor_id());
  356. }
  357. void flush_tlb_all(void)
  358. {
  359. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  360. on_each_cpu(do_flush_tlb_all, NULL, 1);
  361. }
  362. static void do_kernel_range_flush(void *info)
  363. {
  364. struct flush_tlb_info *f = info;
  365. unsigned long addr;
  366. /* flush range by one by one 'invlpg' */
  367. for (addr = f->flush_start; addr < f->flush_end; addr += PAGE_SIZE)
  368. __flush_tlb_single(addr);
  369. }
  370. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  371. {
  372. /* Balance as user space task's flush, a bit conservative */
  373. if (end == TLB_FLUSH_ALL ||
  374. (end - start) > tlb_single_page_flush_ceiling * PAGE_SIZE) {
  375. on_each_cpu(do_flush_tlb_all, NULL, 1);
  376. } else {
  377. struct flush_tlb_info info;
  378. info.flush_start = start;
  379. info.flush_end = end;
  380. on_each_cpu(do_kernel_range_flush, &info, 1);
  381. }
  382. }
  383. static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
  384. size_t count, loff_t *ppos)
  385. {
  386. char buf[32];
  387. unsigned int len;
  388. len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling);
  389. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  390. }
  391. static ssize_t tlbflush_write_file(struct file *file,
  392. const char __user *user_buf, size_t count, loff_t *ppos)
  393. {
  394. char buf[32];
  395. ssize_t len;
  396. int ceiling;
  397. len = min(count, sizeof(buf) - 1);
  398. if (copy_from_user(buf, user_buf, len))
  399. return -EFAULT;
  400. buf[len] = '\0';
  401. if (kstrtoint(buf, 0, &ceiling))
  402. return -EINVAL;
  403. if (ceiling < 0)
  404. return -EINVAL;
  405. tlb_single_page_flush_ceiling = ceiling;
  406. return count;
  407. }
  408. static const struct file_operations fops_tlbflush = {
  409. .read = tlbflush_read_file,
  410. .write = tlbflush_write_file,
  411. .llseek = default_llseek,
  412. };
  413. static int __init create_tlb_single_page_flush_ceiling(void)
  414. {
  415. debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR,
  416. arch_debugfs_dir, NULL, &fops_tlbflush);
  417. return 0;
  418. }
  419. late_initcall(create_tlb_single_page_flush_ceiling);