tlb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 <linux/ptrace.h>
  10. #include <asm/tlbflush.h>
  11. #include <asm/mmu_context.h>
  12. #include <asm/nospec-branch.h>
  13. #include <asm/cache.h>
  14. #include <asm/apic.h>
  15. #include <asm/uv/uv.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. /*
  30. * We get here when we do something requiring a TLB invalidation
  31. * but could not go invalidate all of the contexts. We do the
  32. * necessary invalidation by clearing out the 'ctx_id' which
  33. * forces a TLB flush when the context is loaded.
  34. */
  35. static void clear_asid_other(void)
  36. {
  37. u16 asid;
  38. /*
  39. * This is only expected to be set if we have disabled
  40. * kernel _PAGE_GLOBAL pages.
  41. */
  42. if (!static_cpu_has(X86_FEATURE_PTI)) {
  43. WARN_ON_ONCE(1);
  44. return;
  45. }
  46. for (asid = 0; asid < TLB_NR_DYN_ASIDS; asid++) {
  47. /* Do not need to flush the current asid */
  48. if (asid == this_cpu_read(cpu_tlbstate.loaded_mm_asid))
  49. continue;
  50. /*
  51. * Make sure the next time we go to switch to
  52. * this asid, we do a flush:
  53. */
  54. this_cpu_write(cpu_tlbstate.ctxs[asid].ctx_id, 0);
  55. }
  56. this_cpu_write(cpu_tlbstate.invalidate_other, false);
  57. }
  58. atomic64_t last_mm_ctx_id = ATOMIC64_INIT(1);
  59. static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen,
  60. u16 *new_asid, bool *need_flush)
  61. {
  62. u16 asid;
  63. if (!static_cpu_has(X86_FEATURE_PCID)) {
  64. *new_asid = 0;
  65. *need_flush = true;
  66. return;
  67. }
  68. if (this_cpu_read(cpu_tlbstate.invalidate_other))
  69. clear_asid_other();
  70. for (asid = 0; asid < TLB_NR_DYN_ASIDS; asid++) {
  71. if (this_cpu_read(cpu_tlbstate.ctxs[asid].ctx_id) !=
  72. next->context.ctx_id)
  73. continue;
  74. *new_asid = asid;
  75. *need_flush = (this_cpu_read(cpu_tlbstate.ctxs[asid].tlb_gen) <
  76. next_tlb_gen);
  77. return;
  78. }
  79. /*
  80. * We don't currently own an ASID slot on this CPU.
  81. * Allocate a slot.
  82. */
  83. *new_asid = this_cpu_add_return(cpu_tlbstate.next_asid, 1) - 1;
  84. if (*new_asid >= TLB_NR_DYN_ASIDS) {
  85. *new_asid = 0;
  86. this_cpu_write(cpu_tlbstate.next_asid, 1);
  87. }
  88. *need_flush = true;
  89. }
  90. static void load_new_mm_cr3(pgd_t *pgdir, u16 new_asid, bool need_flush)
  91. {
  92. unsigned long new_mm_cr3;
  93. if (need_flush) {
  94. invalidate_user_asid(new_asid);
  95. new_mm_cr3 = build_cr3(pgdir, new_asid);
  96. } else {
  97. new_mm_cr3 = build_cr3_noflush(pgdir, new_asid);
  98. }
  99. /*
  100. * Caution: many callers of this function expect
  101. * that load_cr3() is serializing and orders TLB
  102. * fills with respect to the mm_cpumask writes.
  103. */
  104. write_cr3(new_mm_cr3);
  105. }
  106. void leave_mm(int cpu)
  107. {
  108. struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
  109. /*
  110. * It's plausible that we're in lazy TLB mode while our mm is init_mm.
  111. * If so, our callers still expect us to flush the TLB, but there
  112. * aren't any user TLB entries in init_mm to worry about.
  113. *
  114. * This needs to happen before any other sanity checks due to
  115. * intel_idle's shenanigans.
  116. */
  117. if (loaded_mm == &init_mm)
  118. return;
  119. /* Warn if we're not lazy. */
  120. WARN_ON(!this_cpu_read(cpu_tlbstate.is_lazy));
  121. switch_mm(NULL, &init_mm, NULL);
  122. }
  123. EXPORT_SYMBOL_GPL(leave_mm);
  124. void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  125. struct task_struct *tsk)
  126. {
  127. unsigned long flags;
  128. local_irq_save(flags);
  129. switch_mm_irqs_off(prev, next, tsk);
  130. local_irq_restore(flags);
  131. }
  132. static void sync_current_stack_to_mm(struct mm_struct *mm)
  133. {
  134. unsigned long sp = current_stack_pointer;
  135. pgd_t *pgd = pgd_offset(mm, sp);
  136. if (pgtable_l5_enabled()) {
  137. if (unlikely(pgd_none(*pgd))) {
  138. pgd_t *pgd_ref = pgd_offset_k(sp);
  139. set_pgd(pgd, *pgd_ref);
  140. }
  141. } else {
  142. /*
  143. * "pgd" is faked. The top level entries are "p4d"s, so sync
  144. * the p4d. This compiles to approximately the same code as
  145. * the 5-level case.
  146. */
  147. p4d_t *p4d = p4d_offset(pgd, sp);
  148. if (unlikely(p4d_none(*p4d))) {
  149. pgd_t *pgd_ref = pgd_offset_k(sp);
  150. p4d_t *p4d_ref = p4d_offset(pgd_ref, sp);
  151. set_p4d(p4d, *p4d_ref);
  152. }
  153. }
  154. }
  155. static bool ibpb_needed(struct task_struct *tsk, u64 last_ctx_id)
  156. {
  157. /*
  158. * Check if the current (previous) task has access to the memory
  159. * of the @tsk (next) task. If access is denied, make sure to
  160. * issue a IBPB to stop user->user Spectre-v2 attacks.
  161. *
  162. * Note: __ptrace_may_access() returns 0 or -ERRNO.
  163. */
  164. return (tsk && tsk->mm && tsk->mm->context.ctx_id != last_ctx_id &&
  165. ptrace_may_access_sched(tsk, PTRACE_MODE_SPEC_IBPB));
  166. }
  167. void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  168. struct task_struct *tsk)
  169. {
  170. struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm);
  171. u16 prev_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
  172. bool was_lazy = this_cpu_read(cpu_tlbstate.is_lazy);
  173. unsigned cpu = smp_processor_id();
  174. u64 next_tlb_gen;
  175. bool need_flush;
  176. u16 new_asid;
  177. /*
  178. * NB: The scheduler will call us with prev == next when switching
  179. * from lazy TLB mode to normal mode if active_mm isn't changing.
  180. * When this happens, we don't assume that CR3 (and hence
  181. * cpu_tlbstate.loaded_mm) matches next.
  182. *
  183. * NB: leave_mm() calls us with prev == NULL and tsk == NULL.
  184. */
  185. /* We don't want flush_tlb_func_* to run concurrently with us. */
  186. if (IS_ENABLED(CONFIG_PROVE_LOCKING))
  187. WARN_ON_ONCE(!irqs_disabled());
  188. /*
  189. * Verify that CR3 is what we think it is. This will catch
  190. * hypothetical buggy code that directly switches to swapper_pg_dir
  191. * without going through leave_mm() / switch_mm_irqs_off() or that
  192. * does something like write_cr3(read_cr3_pa()).
  193. *
  194. * Only do this check if CONFIG_DEBUG_VM=y because __read_cr3()
  195. * isn't free.
  196. */
  197. #ifdef CONFIG_DEBUG_VM
  198. if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev->pgd, prev_asid))) {
  199. /*
  200. * If we were to BUG here, we'd be very likely to kill
  201. * the system so hard that we don't see the call trace.
  202. * Try to recover instead by ignoring the error and doing
  203. * a global flush to minimize the chance of corruption.
  204. *
  205. * (This is far from being a fully correct recovery.
  206. * Architecturally, the CPU could prefetch something
  207. * back into an incorrect ASID slot and leave it there
  208. * to cause trouble down the road. It's better than
  209. * nothing, though.)
  210. */
  211. __flush_tlb_all();
  212. }
  213. #endif
  214. this_cpu_write(cpu_tlbstate.is_lazy, false);
  215. /*
  216. * The membarrier system call requires a full memory barrier and
  217. * core serialization before returning to user-space, after
  218. * storing to rq->curr. Writing to CR3 provides that full
  219. * memory barrier and core serializing instruction.
  220. */
  221. if (real_prev == next) {
  222. VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) !=
  223. next->context.ctx_id);
  224. /*
  225. * Even in lazy TLB mode, the CPU should stay set in the
  226. * mm_cpumask. The TLB shootdown code can figure out from
  227. * from cpu_tlbstate.is_lazy whether or not to send an IPI.
  228. */
  229. if (WARN_ON_ONCE(real_prev != &init_mm &&
  230. !cpumask_test_cpu(cpu, mm_cpumask(next))))
  231. cpumask_set_cpu(cpu, mm_cpumask(next));
  232. /*
  233. * If the CPU is not in lazy TLB mode, we are just switching
  234. * from one thread in a process to another thread in the same
  235. * process. No TLB flush required.
  236. */
  237. if (!was_lazy)
  238. return;
  239. /*
  240. * Read the tlb_gen to check whether a flush is needed.
  241. * If the TLB is up to date, just use it.
  242. * The barrier synchronizes with the tlb_gen increment in
  243. * the TLB shootdown code.
  244. */
  245. smp_mb();
  246. next_tlb_gen = atomic64_read(&next->context.tlb_gen);
  247. if (this_cpu_read(cpu_tlbstate.ctxs[prev_asid].tlb_gen) ==
  248. next_tlb_gen)
  249. return;
  250. /*
  251. * TLB contents went out of date while we were in lazy
  252. * mode. Fall through to the TLB switching code below.
  253. */
  254. new_asid = prev_asid;
  255. need_flush = true;
  256. } else {
  257. u64 last_ctx_id = this_cpu_read(cpu_tlbstate.last_ctx_id);
  258. /*
  259. * Avoid user/user BTB poisoning by flushing the branch
  260. * predictor when switching between processes. This stops
  261. * one process from doing Spectre-v2 attacks on another.
  262. *
  263. * As an optimization, flush indirect branches only when
  264. * switching into a processes that can't be ptrace by the
  265. * current one (as in such case, attacker has much more
  266. * convenient way how to tamper with the next process than
  267. * branch buffer poisoning).
  268. */
  269. if (static_cpu_has(X86_FEATURE_USE_IBPB) &&
  270. ibpb_needed(tsk, last_ctx_id))
  271. indirect_branch_prediction_barrier();
  272. if (IS_ENABLED(CONFIG_VMAP_STACK)) {
  273. /*
  274. * If our current stack is in vmalloc space and isn't
  275. * mapped in the new pgd, we'll double-fault. Forcibly
  276. * map it.
  277. */
  278. sync_current_stack_to_mm(next);
  279. }
  280. /*
  281. * Stop remote flushes for the previous mm.
  282. * Skip kernel threads; we never send init_mm TLB flushing IPIs,
  283. * but the bitmap manipulation can cause cache line contention.
  284. */
  285. if (real_prev != &init_mm) {
  286. VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu,
  287. mm_cpumask(real_prev)));
  288. cpumask_clear_cpu(cpu, mm_cpumask(real_prev));
  289. }
  290. /*
  291. * Start remote flushes and then read tlb_gen.
  292. */
  293. if (next != &init_mm)
  294. cpumask_set_cpu(cpu, mm_cpumask(next));
  295. next_tlb_gen = atomic64_read(&next->context.tlb_gen);
  296. choose_new_asid(next, next_tlb_gen, &new_asid, &need_flush);
  297. /* Let nmi_uaccess_okay() know that we're changing CR3. */
  298. this_cpu_write(cpu_tlbstate.loaded_mm, LOADED_MM_SWITCHING);
  299. barrier();
  300. }
  301. if (need_flush) {
  302. this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);
  303. this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen);
  304. load_new_mm_cr3(next->pgd, new_asid, true);
  305. /*
  306. * NB: This gets called via leave_mm() in the idle path
  307. * where RCU functions differently. Tracing normally
  308. * uses RCU, so we need to use the _rcuidle variant.
  309. *
  310. * (There is no good reason for this. The idle code should
  311. * be rearranged to call this before rcu_idle_enter().)
  312. */
  313. trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  314. } else {
  315. /* The new ASID is already up to date. */
  316. load_new_mm_cr3(next->pgd, new_asid, false);
  317. /* See above wrt _rcuidle. */
  318. trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0);
  319. }
  320. /*
  321. * Record last user mm's context id, so we can avoid
  322. * flushing branch buffer with IBPB if we switch back
  323. * to the same user.
  324. */
  325. if (next != &init_mm)
  326. this_cpu_write(cpu_tlbstate.last_ctx_id, next->context.ctx_id);
  327. /* Make sure we write CR3 before loaded_mm. */
  328. barrier();
  329. this_cpu_write(cpu_tlbstate.loaded_mm, next);
  330. this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid);
  331. if (next != real_prev) {
  332. load_mm_cr4(next);
  333. switch_ldt(real_prev, next);
  334. }
  335. }
  336. /*
  337. * Please ignore the name of this function. It should be called
  338. * switch_to_kernel_thread().
  339. *
  340. * enter_lazy_tlb() is a hint from the scheduler that we are entering a
  341. * kernel thread or other context without an mm. Acceptable implementations
  342. * include doing nothing whatsoever, switching to init_mm, or various clever
  343. * lazy tricks to try to minimize TLB flushes.
  344. *
  345. * The scheduler reserves the right to call enter_lazy_tlb() several times
  346. * in a row. It will notify us that we're going back to a real mm by
  347. * calling switch_mm_irqs_off().
  348. */
  349. void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  350. {
  351. if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm)
  352. return;
  353. this_cpu_write(cpu_tlbstate.is_lazy, true);
  354. }
  355. /*
  356. * Call this when reinitializing a CPU. It fixes the following potential
  357. * problems:
  358. *
  359. * - The ASID changed from what cpu_tlbstate thinks it is (most likely
  360. * because the CPU was taken down and came back up with CR3's PCID
  361. * bits clear. CPU hotplug can do this.
  362. *
  363. * - The TLB contains junk in slots corresponding to inactive ASIDs.
  364. *
  365. * - The CPU went so far out to lunch that it may have missed a TLB
  366. * flush.
  367. */
  368. void initialize_tlbstate_and_flush(void)
  369. {
  370. int i;
  371. struct mm_struct *mm = this_cpu_read(cpu_tlbstate.loaded_mm);
  372. u64 tlb_gen = atomic64_read(&init_mm.context.tlb_gen);
  373. unsigned long cr3 = __read_cr3();
  374. /* Assert that CR3 already references the right mm. */
  375. WARN_ON((cr3 & CR3_ADDR_MASK) != __pa(mm->pgd));
  376. /*
  377. * Assert that CR4.PCIDE is set if needed. (CR4.PCIDE initialization
  378. * doesn't work like other CR4 bits because it can only be set from
  379. * long mode.)
  380. */
  381. WARN_ON(boot_cpu_has(X86_FEATURE_PCID) &&
  382. !(cr4_read_shadow() & X86_CR4_PCIDE));
  383. /* Force ASID 0 and force a TLB flush. */
  384. write_cr3(build_cr3(mm->pgd, 0));
  385. /* Reinitialize tlbstate. */
  386. this_cpu_write(cpu_tlbstate.last_ctx_id, mm->context.ctx_id);
  387. this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);
  388. this_cpu_write(cpu_tlbstate.next_asid, 1);
  389. this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id);
  390. this_cpu_write(cpu_tlbstate.ctxs[0].tlb_gen, tlb_gen);
  391. for (i = 1; i < TLB_NR_DYN_ASIDS; i++)
  392. this_cpu_write(cpu_tlbstate.ctxs[i].ctx_id, 0);
  393. }
  394. /*
  395. * flush_tlb_func_common()'s memory ordering requirement is that any
  396. * TLB fills that happen after we flush the TLB are ordered after we
  397. * read active_mm's tlb_gen. We don't need any explicit barriers
  398. * because all x86 flush operations are serializing and the
  399. * atomic64_read operation won't be reordered by the compiler.
  400. */
  401. static void flush_tlb_func_common(const struct flush_tlb_info *f,
  402. bool local, enum tlb_flush_reason reason)
  403. {
  404. /*
  405. * We have three different tlb_gen values in here. They are:
  406. *
  407. * - mm_tlb_gen: the latest generation.
  408. * - local_tlb_gen: the generation that this CPU has already caught
  409. * up to.
  410. * - f->new_tlb_gen: the generation that the requester of the flush
  411. * wants us to catch up to.
  412. */
  413. struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
  414. u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
  415. u64 mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen);
  416. u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen);
  417. /* This code cannot presently handle being reentered. */
  418. VM_WARN_ON(!irqs_disabled());
  419. if (unlikely(loaded_mm == &init_mm))
  420. return;
  421. VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) !=
  422. loaded_mm->context.ctx_id);
  423. if (this_cpu_read(cpu_tlbstate.is_lazy)) {
  424. /*
  425. * We're in lazy mode. We need to at least flush our
  426. * paging-structure cache to avoid speculatively reading
  427. * garbage into our TLB. Since switching to init_mm is barely
  428. * slower than a minimal flush, just switch to init_mm.
  429. *
  430. * This should be rare, with native_flush_tlb_others skipping
  431. * IPIs to lazy TLB mode CPUs.
  432. */
  433. switch_mm_irqs_off(NULL, &init_mm, NULL);
  434. return;
  435. }
  436. if (unlikely(local_tlb_gen == mm_tlb_gen)) {
  437. /*
  438. * There's nothing to do: we're already up to date. This can
  439. * happen if two concurrent flushes happen -- the first flush to
  440. * be handled can catch us all the way up, leaving no work for
  441. * the second flush.
  442. */
  443. trace_tlb_flush(reason, 0);
  444. return;
  445. }
  446. WARN_ON_ONCE(local_tlb_gen > mm_tlb_gen);
  447. WARN_ON_ONCE(f->new_tlb_gen > mm_tlb_gen);
  448. /*
  449. * If we get to this point, we know that our TLB is out of date.
  450. * This does not strictly imply that we need to flush (it's
  451. * possible that f->new_tlb_gen <= local_tlb_gen), but we're
  452. * going to need to flush in the very near future, so we might
  453. * as well get it over with.
  454. *
  455. * The only question is whether to do a full or partial flush.
  456. *
  457. * We do a partial flush if requested and two extra conditions
  458. * are met:
  459. *
  460. * 1. f->new_tlb_gen == local_tlb_gen + 1. We have an invariant that
  461. * we've always done all needed flushes to catch up to
  462. * local_tlb_gen. If, for example, local_tlb_gen == 2 and
  463. * f->new_tlb_gen == 3, then we know that the flush needed to bring
  464. * us up to date for tlb_gen 3 is the partial flush we're
  465. * processing.
  466. *
  467. * As an example of why this check is needed, suppose that there
  468. * are two concurrent flushes. The first is a full flush that
  469. * changes context.tlb_gen from 1 to 2. The second is a partial
  470. * flush that changes context.tlb_gen from 2 to 3. If they get
  471. * processed on this CPU in reverse order, we'll see
  472. * local_tlb_gen == 1, mm_tlb_gen == 3, and end != TLB_FLUSH_ALL.
  473. * If we were to use __flush_tlb_one_user() and set local_tlb_gen to
  474. * 3, we'd be break the invariant: we'd update local_tlb_gen above
  475. * 1 without the full flush that's needed for tlb_gen 2.
  476. *
  477. * 2. f->new_tlb_gen == mm_tlb_gen. This is purely an optimiation.
  478. * Partial TLB flushes are not all that much cheaper than full TLB
  479. * flushes, so it seems unlikely that it would be a performance win
  480. * to do a partial flush if that won't bring our TLB fully up to
  481. * date. By doing a full flush instead, we can increase
  482. * local_tlb_gen all the way to mm_tlb_gen and we can probably
  483. * avoid another flush in the very near future.
  484. */
  485. if (f->end != TLB_FLUSH_ALL &&
  486. f->new_tlb_gen == local_tlb_gen + 1 &&
  487. f->new_tlb_gen == mm_tlb_gen) {
  488. /* Partial flush */
  489. unsigned long nr_invalidate = (f->end - f->start) >> f->stride_shift;
  490. unsigned long addr = f->start;
  491. while (addr < f->end) {
  492. __flush_tlb_one_user(addr);
  493. addr += 1UL << f->stride_shift;
  494. }
  495. if (local)
  496. count_vm_tlb_events(NR_TLB_LOCAL_FLUSH_ONE, nr_invalidate);
  497. trace_tlb_flush(reason, nr_invalidate);
  498. } else {
  499. /* Full flush. */
  500. local_flush_tlb();
  501. if (local)
  502. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  503. trace_tlb_flush(reason, TLB_FLUSH_ALL);
  504. }
  505. /* Both paths above update our state to mm_tlb_gen. */
  506. this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen);
  507. }
  508. static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason)
  509. {
  510. const struct flush_tlb_info *f = info;
  511. flush_tlb_func_common(f, true, reason);
  512. }
  513. static void flush_tlb_func_remote(void *info)
  514. {
  515. const struct flush_tlb_info *f = info;
  516. inc_irq_stat(irq_tlb_count);
  517. if (f->mm && f->mm != this_cpu_read(cpu_tlbstate.loaded_mm))
  518. return;
  519. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  520. flush_tlb_func_common(f, false, TLB_REMOTE_SHOOTDOWN);
  521. }
  522. static bool tlb_is_not_lazy(int cpu, void *data)
  523. {
  524. return !per_cpu(cpu_tlbstate.is_lazy, cpu);
  525. }
  526. void native_flush_tlb_others(const struct cpumask *cpumask,
  527. const struct flush_tlb_info *info)
  528. {
  529. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  530. if (info->end == TLB_FLUSH_ALL)
  531. trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL);
  532. else
  533. trace_tlb_flush(TLB_REMOTE_SEND_IPI,
  534. (info->end - info->start) >> PAGE_SHIFT);
  535. if (is_uv_system()) {
  536. /*
  537. * This whole special case is confused. UV has a "Broadcast
  538. * Assist Unit", which seems to be a fancy way to send IPIs.
  539. * Back when x86 used an explicit TLB flush IPI, UV was
  540. * optimized to use its own mechanism. These days, x86 uses
  541. * smp_call_function_many(), but UV still uses a manual IPI,
  542. * and that IPI's action is out of date -- it does a manual
  543. * flush instead of calling flush_tlb_func_remote(). This
  544. * means that the percpu tlb_gen variables won't be updated
  545. * and we'll do pointless flushes on future context switches.
  546. *
  547. * Rather than hooking native_flush_tlb_others() here, I think
  548. * that UV should be updated so that smp_call_function_many(),
  549. * etc, are optimal on UV.
  550. */
  551. unsigned int cpu;
  552. cpu = smp_processor_id();
  553. cpumask = uv_flush_tlb_others(cpumask, info);
  554. if (cpumask)
  555. smp_call_function_many(cpumask, flush_tlb_func_remote,
  556. (void *)info, 1);
  557. return;
  558. }
  559. /*
  560. * If no page tables were freed, we can skip sending IPIs to
  561. * CPUs in lazy TLB mode. They will flush the CPU themselves
  562. * at the next context switch.
  563. *
  564. * However, if page tables are getting freed, we need to send the
  565. * IPI everywhere, to prevent CPUs in lazy TLB mode from tripping
  566. * up on the new contents of what used to be page tables, while
  567. * doing a speculative memory access.
  568. */
  569. if (info->freed_tables)
  570. smp_call_function_many(cpumask, flush_tlb_func_remote,
  571. (void *)info, 1);
  572. else
  573. on_each_cpu_cond_mask(tlb_is_not_lazy, flush_tlb_func_remote,
  574. (void *)info, 1, GFP_ATOMIC, cpumask);
  575. }
  576. /*
  577. * See Documentation/x86/tlb.txt for details. We choose 33
  578. * because it is large enough to cover the vast majority (at
  579. * least 95%) of allocations, and is small enough that we are
  580. * confident it will not cause too much overhead. Each single
  581. * flush is about 100 ns, so this caps the maximum overhead at
  582. * _about_ 3,000 ns.
  583. *
  584. * This is in units of pages.
  585. */
  586. static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
  587. void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  588. unsigned long end, unsigned int stride_shift,
  589. bool freed_tables)
  590. {
  591. int cpu;
  592. struct flush_tlb_info info __aligned(SMP_CACHE_BYTES) = {
  593. .mm = mm,
  594. .stride_shift = stride_shift,
  595. .freed_tables = freed_tables,
  596. };
  597. cpu = get_cpu();
  598. /* This is also a barrier that synchronizes with switch_mm(). */
  599. info.new_tlb_gen = inc_mm_tlb_gen(mm);
  600. /* Should we flush just the requested range? */
  601. if ((end != TLB_FLUSH_ALL) &&
  602. ((end - start) >> stride_shift) <= tlb_single_page_flush_ceiling) {
  603. info.start = start;
  604. info.end = end;
  605. } else {
  606. info.start = 0UL;
  607. info.end = TLB_FLUSH_ALL;
  608. }
  609. if (mm == this_cpu_read(cpu_tlbstate.loaded_mm)) {
  610. VM_WARN_ON(irqs_disabled());
  611. local_irq_disable();
  612. flush_tlb_func_local(&info, TLB_LOCAL_MM_SHOOTDOWN);
  613. local_irq_enable();
  614. }
  615. if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids)
  616. flush_tlb_others(mm_cpumask(mm), &info);
  617. put_cpu();
  618. }
  619. static void do_flush_tlb_all(void *info)
  620. {
  621. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  622. __flush_tlb_all();
  623. }
  624. void flush_tlb_all(void)
  625. {
  626. count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
  627. on_each_cpu(do_flush_tlb_all, NULL, 1);
  628. }
  629. static void do_kernel_range_flush(void *info)
  630. {
  631. struct flush_tlb_info *f = info;
  632. unsigned long addr;
  633. /* flush range by one by one 'invlpg' */
  634. for (addr = f->start; addr < f->end; addr += PAGE_SIZE)
  635. __flush_tlb_one_kernel(addr);
  636. }
  637. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  638. {
  639. /* Balance as user space task's flush, a bit conservative */
  640. if (end == TLB_FLUSH_ALL ||
  641. (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
  642. on_each_cpu(do_flush_tlb_all, NULL, 1);
  643. } else {
  644. struct flush_tlb_info info;
  645. info.start = start;
  646. info.end = end;
  647. on_each_cpu(do_kernel_range_flush, &info, 1);
  648. }
  649. }
  650. void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
  651. {
  652. struct flush_tlb_info info = {
  653. .mm = NULL,
  654. .start = 0UL,
  655. .end = TLB_FLUSH_ALL,
  656. };
  657. int cpu = get_cpu();
  658. if (cpumask_test_cpu(cpu, &batch->cpumask)) {
  659. VM_WARN_ON(irqs_disabled());
  660. local_irq_disable();
  661. flush_tlb_func_local(&info, TLB_LOCAL_SHOOTDOWN);
  662. local_irq_enable();
  663. }
  664. if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids)
  665. flush_tlb_others(&batch->cpumask, &info);
  666. cpumask_clear(&batch->cpumask);
  667. put_cpu();
  668. }
  669. static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
  670. size_t count, loff_t *ppos)
  671. {
  672. char buf[32];
  673. unsigned int len;
  674. len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling);
  675. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  676. }
  677. static ssize_t tlbflush_write_file(struct file *file,
  678. const char __user *user_buf, size_t count, loff_t *ppos)
  679. {
  680. char buf[32];
  681. ssize_t len;
  682. int ceiling;
  683. len = min(count, sizeof(buf) - 1);
  684. if (copy_from_user(buf, user_buf, len))
  685. return -EFAULT;
  686. buf[len] = '\0';
  687. if (kstrtoint(buf, 0, &ceiling))
  688. return -EINVAL;
  689. if (ceiling < 0)
  690. return -EINVAL;
  691. tlb_single_page_flush_ceiling = ceiling;
  692. return count;
  693. }
  694. static const struct file_operations fops_tlbflush = {
  695. .read = tlbflush_read_file,
  696. .write = tlbflush_write_file,
  697. .llseek = default_llseek,
  698. };
  699. static int __init create_tlb_single_page_flush_ceiling(void)
  700. {
  701. debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR,
  702. arch_debugfs_dir, NULL, &fops_tlbflush);
  703. return 0;
  704. }
  705. late_initcall(create_tlb_single_page_flush_ceiling);