tlb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/export.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. if (IS_ENABLED(CONFIG_VMAP_STACK)) {
  118. /*
  119. * If our current stack is in vmalloc space and isn't
  120. * mapped in the new pgd, we'll double-fault. Forcibly
  121. * map it.
  122. */
  123. unsigned int stack_pgd_index = pgd_index(current_stack_pointer);
  124. pgd_t *pgd = next->pgd + stack_pgd_index;
  125. if (unlikely(pgd_none(*pgd)))
  126. set_pgd(pgd, init_mm.pgd[stack_pgd_index]);
  127. }
  128. /*
  129. * Record last user mm's context id, so we can avoid
  130. * flushing branch buffer with IBPB if we switch back
  131. * to the same user.
  132. */
  133. if (next != &init_mm)
  134. this_cpu_write(cpu_tlbstate.last_ctx_id, next->context.ctx_id);
  135. this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
  136. this_cpu_write(cpu_tlbstate.active_mm, next);
  137. cpumask_set_cpu(cpu, mm_cpumask(next));
  138. /*
  139. * Re-load page tables.
  140. *
  141. * This logic has an ordering constraint:
  142. *
  143. * CPU 0: Write to a PTE for 'next'
  144. * CPU 0: load bit 1 in mm_cpumask. if nonzero, send IPI.
  145. * CPU 1: set bit 1 in next's mm_cpumask
  146. * CPU 1: load from the PTE that CPU 0 writes (implicit)
  147. *
  148. * We need to prevent an outcome in which CPU 1 observes
  149. * the new PTE value and CPU 0 observes bit 1 clear in
  150. * mm_cpumask. (If that occurs, then the IPI will never
  151. * be sent, and CPU 0's TLB will contain a stale entry.)
  152. *
  153. * The bad outcome can occur if either CPU's load is
  154. * reordered before that CPU's store, so both CPUs must
  155. * execute full barriers to prevent this from happening.
  156. *
  157. * Thus, switch_mm needs a full barrier between the
  158. * store to mm_cpumask and any operation that could load
  159. * from next->pgd. TLB fills are special and can happen
  160. * due to instruction fetches or for no reason at all,
  161. * and neither LOCK nor MFENCE orders them.
  162. * Fortunately, load_cr3() is serializing and gives the
  163. * ordering guarantee we need.
  164. *
  165. */
  166. load_new_mm_cr3(next->pgd);
  167. trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  168. /* Stop flush ipis for the previous mm */
  169. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  170. /* Load per-mm CR4 state */
  171. load_mm_cr4(next);
  172. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  173. /*
  174. * Load the LDT, if the LDT is different.
  175. *
  176. * It's possible that prev->context.ldt doesn't match
  177. * the LDT register. This can happen if leave_mm(prev)
  178. * was called and then modify_ldt changed
  179. * prev->context.ldt but suppressed an IPI to this CPU.
  180. * In this case, prev->context.ldt != NULL, because we
  181. * never set context.ldt to NULL while the mm still
  182. * exists. That means that next->context.ldt !=
  183. * prev->context.ldt, because mms never share an LDT.
  184. */
  185. if (unlikely(prev->context.ldt != next->context.ldt))
  186. load_mm_ldt(next);
  187. #endif
  188. } else {
  189. this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
  190. BUG_ON(this_cpu_read(cpu_tlbstate.active_mm) != next);
  191. if (!cpumask_test_cpu(cpu, mm_cpumask(next))) {
  192. /*
  193. * On established mms, the mm_cpumask is only changed
  194. * from irq context, from ptep_clear_flush() while in
  195. * lazy tlb mode, and here. Irqs are blocked during
  196. * schedule, protecting us from simultaneous changes.
  197. */
  198. cpumask_set_cpu(cpu, mm_cpumask(next));
  199. /*
  200. * We were in lazy tlb mode and leave_mm disabled
  201. * tlb flush IPI delivery. We must reload CR3
  202. * to make sure to use no freed page tables.
  203. *
  204. * As above, load_cr3() is serializing and orders TLB
  205. * fills with respect to the mm_cpumask write.
  206. */
  207. load_new_mm_cr3(next->pgd);
  208. trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  209. load_mm_cr4(next);
  210. load_mm_ldt(next);
  211. }
  212. }
  213. }
  214. /*
  215. * The flush IPI assumes that a thread switch happens in this order:
  216. * [cpu0: the cpu that switches]
  217. * 1) switch_mm() either 1a) or 1b)
  218. * 1a) thread switch to a different mm
  219. * 1a1) set cpu_tlbstate to TLBSTATE_OK
  220. * Now the tlb flush NMI handler flush_tlb_func won't call leave_mm
  221. * if cpu0 was in lazy tlb mode.
  222. * 1a2) update cpu active_mm
  223. * Now cpu0 accepts tlb flushes for the new mm.
  224. * 1a3) cpu_set(cpu, new_mm->cpu_vm_mask);
  225. * Now the other cpus will send tlb flush ipis.
  226. * 1a4) change cr3.
  227. * 1a5) cpu_clear(cpu, old_mm->cpu_vm_mask);
  228. * Stop ipi delivery for the old mm. This is not synchronized with
  229. * the other cpus, but flush_tlb_func ignore flush ipis for the wrong
  230. * mm, and in the worst case we perform a superfluous tlb flush.
  231. * 1b) thread switch without mm change
  232. * cpu active_mm is correct, cpu0 already handles flush ipis.
  233. * 1b1) set cpu_tlbstate to TLBSTATE_OK
  234. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  235. * Atomically set the bit [other cpus will start sending flush ipis],
  236. * and test the bit.
  237. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  238. * 2) switch %%esp, ie current
  239. *
  240. * The interrupt must handle 2 special cases:
  241. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  242. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  243. * runs in kernel space, the cpu could load tlb entries for user space
  244. * pages.
  245. *
  246. * The good news is that cpu_tlbstate is local to each cpu, no
  247. * write/read ordering problems.
  248. */
  249. /*
  250. * TLB flush funcation:
  251. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  252. * 2) Leave the mm if we are in the lazy tlb mode.
  253. */
  254. static void flush_tlb_func(void *info)
  255. {
  256. struct flush_tlb_info *f = info;
  257. inc_irq_stat(irq_tlb_count);
  258. if (f->flush_mm && f->flush_mm != this_cpu_read(cpu_tlbstate.active_mm))
  259. return;
  260. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  261. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
  262. if (f->flush_end == TLB_FLUSH_ALL) {
  263. local_flush_tlb();
  264. trace_tlb_flush(TLB_REMOTE_SHOOTDOWN, TLB_FLUSH_ALL);
  265. } else {
  266. unsigned long addr;
  267. unsigned long nr_pages =
  268. (f->flush_end - f->flush_start) / PAGE_SIZE;
  269. addr = f->flush_start;
  270. while (addr < f->flush_end) {
  271. __flush_tlb_single(addr);
  272. addr += PAGE_SIZE;
  273. }
  274. trace_tlb_flush(TLB_REMOTE_SHOOTDOWN, nr_pages);
  275. }
  276. } else
  277. leave_mm(smp_processor_id());
  278. }
  279. void native_flush_tlb_others(const struct cpumask *cpumask,
  280. struct mm_struct *mm, unsigned long start,
  281. unsigned long end)
  282. {
  283. struct flush_tlb_info info;
  284. info.flush_mm = mm;
  285. info.flush_start = start;
  286. info.flush_end = end;
  287. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  288. if (end == TLB_FLUSH_ALL)
  289. trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL);
  290. else
  291. trace_tlb_flush(TLB_REMOTE_SEND_IPI,
  292. (end - start) >> PAGE_SHIFT);
  293. if (is_uv_system()) {
  294. unsigned int cpu;
  295. cpu = smp_processor_id();
  296. cpumask = uv_flush_tlb_others(cpumask, mm, start, end, cpu);
  297. if (cpumask)
  298. smp_call_function_many(cpumask, flush_tlb_func,
  299. &info, 1);
  300. return;
  301. }
  302. smp_call_function_many(cpumask, flush_tlb_func, &info, 1);
  303. }
  304. /*
  305. * See Documentation/x86/tlb.txt for details. We choose 33
  306. * because it is large enough to cover the vast majority (at
  307. * least 95%) of allocations, and is small enough that we are
  308. * confident it will not cause too much overhead. Each single
  309. * flush is about 100 ns, so this caps the maximum overhead at
  310. * _about_ 3,000 ns.
  311. *
  312. * This is in units of pages.
  313. */
  314. static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
  315. void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  316. unsigned long end, unsigned long vmflag)
  317. {
  318. unsigned long addr;
  319. /* do a global flush by default */
  320. unsigned long base_pages_to_flush = TLB_FLUSH_ALL;
  321. preempt_disable();
  322. if ((end != TLB_FLUSH_ALL) && !(vmflag & VM_HUGETLB))
  323. base_pages_to_flush = (end - start) >> PAGE_SHIFT;
  324. if (base_pages_to_flush > tlb_single_page_flush_ceiling)
  325. base_pages_to_flush = TLB_FLUSH_ALL;
  326. if (current->active_mm != mm) {
  327. /* Synchronize with switch_mm. */
  328. smp_mb();
  329. goto out;
  330. }
  331. if (!current->mm) {
  332. leave_mm(smp_processor_id());
  333. /* Synchronize with switch_mm. */
  334. smp_mb();
  335. goto out;
  336. }
  337. /*
  338. * Both branches below are implicit full barriers (MOV to CR or
  339. * INVLPG) that synchronize with switch_mm.
  340. */
  341. if (base_pages_to_flush == TLB_FLUSH_ALL) {
  342. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  343. local_flush_tlb();
  344. } else {
  345. /* flush range by one by one 'invlpg' */
  346. for (addr = start; addr < end; addr += PAGE_SIZE) {
  347. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
  348. __flush_tlb_single(addr);
  349. }
  350. }
  351. trace_tlb_flush(TLB_LOCAL_MM_SHOOTDOWN, base_pages_to_flush);
  352. out:
  353. if (base_pages_to_flush == TLB_FLUSH_ALL) {
  354. start = 0UL;
  355. end = TLB_FLUSH_ALL;
  356. }
  357. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  358. flush_tlb_others(mm_cpumask(mm), mm, start, end);
  359. preempt_enable();
  360. }
  361. static void do_flush_tlb_all(void *info)
  362. {
  363. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  364. __flush_tlb_all();
  365. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
  366. leave_mm(smp_processor_id());
  367. }
  368. void flush_tlb_all(void)
  369. {
  370. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  371. on_each_cpu(do_flush_tlb_all, NULL, 1);
  372. }
  373. static void do_kernel_range_flush(void *info)
  374. {
  375. struct flush_tlb_info *f = info;
  376. unsigned long addr;
  377. /* flush range by one by one 'invlpg' */
  378. for (addr = f->flush_start; addr < f->flush_end; addr += PAGE_SIZE)
  379. __flush_tlb_single(addr);
  380. }
  381. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  382. {
  383. /* Balance as user space task's flush, a bit conservative */
  384. if (end == TLB_FLUSH_ALL ||
  385. (end - start) > tlb_single_page_flush_ceiling * PAGE_SIZE) {
  386. on_each_cpu(do_flush_tlb_all, NULL, 1);
  387. } else {
  388. struct flush_tlb_info info;
  389. info.flush_start = start;
  390. info.flush_end = end;
  391. on_each_cpu(do_kernel_range_flush, &info, 1);
  392. }
  393. }
  394. static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
  395. size_t count, loff_t *ppos)
  396. {
  397. char buf[32];
  398. unsigned int len;
  399. len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling);
  400. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  401. }
  402. static ssize_t tlbflush_write_file(struct file *file,
  403. const char __user *user_buf, size_t count, loff_t *ppos)
  404. {
  405. char buf[32];
  406. ssize_t len;
  407. int ceiling;
  408. len = min(count, sizeof(buf) - 1);
  409. if (copy_from_user(buf, user_buf, len))
  410. return -EFAULT;
  411. buf[len] = '\0';
  412. if (kstrtoint(buf, 0, &ceiling))
  413. return -EINVAL;
  414. if (ceiling < 0)
  415. return -EINVAL;
  416. tlb_single_page_flush_ceiling = ceiling;
  417. return count;
  418. }
  419. static const struct file_operations fops_tlbflush = {
  420. .read = tlbflush_read_file,
  421. .write = tlbflush_write_file,
  422. .llseek = default_llseek,
  423. };
  424. static int __init create_tlb_single_page_flush_ceiling(void)
  425. {
  426. debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR,
  427. arch_debugfs_dir, NULL, &fops_tlbflush);
  428. return 0;
  429. }
  430. late_initcall(create_tlb_single_page_flush_ceiling);