tlb.c 18 KB

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