tlb.c 22 KB

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