tlb.c 26 KB

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