tlb.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/module.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. DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate)
  15. = { &init_mm, 0, };
  16. /*
  17. * Smarter SMP flushing macros.
  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. struct flush_tlb_info {
  30. struct mm_struct *flush_mm;
  31. unsigned long flush_start;
  32. unsigned long flush_end;
  33. };
  34. /*
  35. * We cannot call mmdrop() because we are in interrupt context,
  36. * instead update mm->cpu_vm_mask.
  37. */
  38. void leave_mm(int cpu)
  39. {
  40. struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm);
  41. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
  42. BUG();
  43. if (cpumask_test_cpu(cpu, mm_cpumask(active_mm))) {
  44. cpumask_clear_cpu(cpu, mm_cpumask(active_mm));
  45. load_cr3(swapper_pg_dir);
  46. }
  47. }
  48. EXPORT_SYMBOL_GPL(leave_mm);
  49. /*
  50. * The flush IPI assumes that a thread switch happens in this order:
  51. * [cpu0: the cpu that switches]
  52. * 1) switch_mm() either 1a) or 1b)
  53. * 1a) thread switch to a different mm
  54. * 1a1) set cpu_tlbstate to TLBSTATE_OK
  55. * Now the tlb flush NMI handler flush_tlb_func won't call leave_mm
  56. * if cpu0 was in lazy tlb mode.
  57. * 1a2) update cpu active_mm
  58. * Now cpu0 accepts tlb flushes for the new mm.
  59. * 1a3) cpu_set(cpu, new_mm->cpu_vm_mask);
  60. * Now the other cpus will send tlb flush ipis.
  61. * 1a4) change cr3.
  62. * 1a5) cpu_clear(cpu, old_mm->cpu_vm_mask);
  63. * Stop ipi delivery for the old mm. This is not synchronized with
  64. * the other cpus, but flush_tlb_func ignore flush ipis for the wrong
  65. * mm, and in the worst case we perform a superfluous tlb flush.
  66. * 1b) thread switch without mm change
  67. * cpu active_mm is correct, cpu0 already handles flush ipis.
  68. * 1b1) set cpu_tlbstate to TLBSTATE_OK
  69. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  70. * Atomically set the bit [other cpus will start sending flush ipis],
  71. * and test the bit.
  72. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  73. * 2) switch %%esp, ie current
  74. *
  75. * The interrupt must handle 2 special cases:
  76. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  77. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  78. * runs in kernel space, the cpu could load tlb entries for user space
  79. * pages.
  80. *
  81. * The good news is that cpu_tlbstate is local to each cpu, no
  82. * write/read ordering problems.
  83. */
  84. /*
  85. * TLB flush funcation:
  86. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  87. * 2) Leave the mm if we are in the lazy tlb mode.
  88. */
  89. static void flush_tlb_func(void *info)
  90. {
  91. struct flush_tlb_info *f = info;
  92. inc_irq_stat(irq_tlb_count);
  93. if (f->flush_mm != this_cpu_read(cpu_tlbstate.active_mm))
  94. return;
  95. count_vm_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  96. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
  97. if (f->flush_end == TLB_FLUSH_ALL)
  98. local_flush_tlb();
  99. else if (!f->flush_end)
  100. __flush_tlb_single(f->flush_start);
  101. else {
  102. unsigned long addr;
  103. addr = f->flush_start;
  104. while (addr < f->flush_end) {
  105. __flush_tlb_single(addr);
  106. addr += PAGE_SIZE;
  107. }
  108. }
  109. } else
  110. leave_mm(smp_processor_id());
  111. }
  112. void native_flush_tlb_others(const struct cpumask *cpumask,
  113. struct mm_struct *mm, unsigned long start,
  114. unsigned long end)
  115. {
  116. struct flush_tlb_info info;
  117. info.flush_mm = mm;
  118. info.flush_start = start;
  119. info.flush_end = end;
  120. count_vm_event(NR_TLB_REMOTE_FLUSH);
  121. if (is_uv_system()) {
  122. unsigned int cpu;
  123. cpu = smp_processor_id();
  124. cpumask = uv_flush_tlb_others(cpumask, mm, start, end, cpu);
  125. if (cpumask)
  126. smp_call_function_many(cpumask, flush_tlb_func,
  127. &info, 1);
  128. return;
  129. }
  130. smp_call_function_many(cpumask, flush_tlb_func, &info, 1);
  131. }
  132. void flush_tlb_current_task(void)
  133. {
  134. struct mm_struct *mm = current->mm;
  135. preempt_disable();
  136. count_vm_event(NR_TLB_LOCAL_FLUSH_ALL);
  137. local_flush_tlb();
  138. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  139. flush_tlb_others(mm_cpumask(mm), mm, 0UL, TLB_FLUSH_ALL);
  140. preempt_enable();
  141. }
  142. /*
  143. * It can find out the THP large page, or
  144. * HUGETLB page in tlb_flush when THP disabled
  145. */
  146. static inline unsigned long has_large_page(struct mm_struct *mm,
  147. unsigned long start, unsigned long end)
  148. {
  149. pgd_t *pgd;
  150. pud_t *pud;
  151. pmd_t *pmd;
  152. unsigned long addr = ALIGN(start, HPAGE_SIZE);
  153. for (; addr < end; addr += HPAGE_SIZE) {
  154. pgd = pgd_offset(mm, addr);
  155. if (likely(!pgd_none(*pgd))) {
  156. pud = pud_offset(pgd, addr);
  157. if (likely(!pud_none(*pud))) {
  158. pmd = pmd_offset(pud, addr);
  159. if (likely(!pmd_none(*pmd)))
  160. if (pmd_large(*pmd))
  161. return addr;
  162. }
  163. }
  164. }
  165. return 0;
  166. }
  167. void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  168. unsigned long end, unsigned long vmflag)
  169. {
  170. unsigned long addr;
  171. unsigned act_entries, tlb_entries = 0;
  172. preempt_disable();
  173. if (current->active_mm != mm)
  174. goto flush_all;
  175. if (!current->mm) {
  176. leave_mm(smp_processor_id());
  177. goto flush_all;
  178. }
  179. if (end == TLB_FLUSH_ALL || tlb_flushall_shift == -1
  180. || vmflag & VM_HUGETLB) {
  181. local_flush_tlb();
  182. goto flush_all;
  183. }
  184. /* In modern CPU, last level tlb used for both data/ins */
  185. if (vmflag & VM_EXEC)
  186. tlb_entries = tlb_lli_4k[ENTRIES];
  187. else
  188. tlb_entries = tlb_lld_4k[ENTRIES];
  189. /* Assume all of TLB entries was occupied by this task */
  190. act_entries = mm->total_vm > tlb_entries ? tlb_entries : mm->total_vm;
  191. /* tlb_flushall_shift is on balance point, details in commit log */
  192. if ((end - start) >> PAGE_SHIFT > act_entries >> tlb_flushall_shift) {
  193. count_vm_event(NR_TLB_LOCAL_FLUSH_ALL);
  194. local_flush_tlb();
  195. } else {
  196. if (has_large_page(mm, start, end)) {
  197. local_flush_tlb();
  198. goto flush_all;
  199. }
  200. /* flush range by one by one 'invlpg' */
  201. for (addr = start; addr < end; addr += PAGE_SIZE) {
  202. count_vm_event(NR_TLB_LOCAL_FLUSH_ONE);
  203. __flush_tlb_single(addr);
  204. }
  205. if (cpumask_any_but(mm_cpumask(mm),
  206. smp_processor_id()) < nr_cpu_ids)
  207. flush_tlb_others(mm_cpumask(mm), mm, start, end);
  208. preempt_enable();
  209. return;
  210. }
  211. flush_all:
  212. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  213. flush_tlb_others(mm_cpumask(mm), mm, 0UL, TLB_FLUSH_ALL);
  214. preempt_enable();
  215. }
  216. void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
  217. {
  218. struct mm_struct *mm = vma->vm_mm;
  219. preempt_disable();
  220. if (current->active_mm == mm) {
  221. if (current->mm)
  222. __flush_tlb_one(start);
  223. else
  224. leave_mm(smp_processor_id());
  225. }
  226. if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
  227. flush_tlb_others(mm_cpumask(mm), mm, start, 0UL);
  228. preempt_enable();
  229. }
  230. static void do_flush_tlb_all(void *info)
  231. {
  232. count_vm_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
  233. __flush_tlb_all();
  234. if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
  235. leave_mm(smp_processor_id());
  236. }
  237. void flush_tlb_all(void)
  238. {
  239. count_vm_event(NR_TLB_REMOTE_FLUSH);
  240. on_each_cpu(do_flush_tlb_all, NULL, 1);
  241. }
  242. static void do_kernel_range_flush(void *info)
  243. {
  244. struct flush_tlb_info *f = info;
  245. unsigned long addr;
  246. /* flush range by one by one 'invlpg' */
  247. for (addr = f->flush_start; addr < f->flush_end; addr += PAGE_SIZE)
  248. __flush_tlb_single(addr);
  249. }
  250. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  251. {
  252. unsigned act_entries;
  253. struct flush_tlb_info info;
  254. /* In modern CPU, last level tlb used for both data/ins */
  255. act_entries = tlb_lld_4k[ENTRIES];
  256. /* Balance as user space task's flush, a bit conservative */
  257. if (end == TLB_FLUSH_ALL || tlb_flushall_shift == -1 ||
  258. (end - start) >> PAGE_SHIFT > act_entries >> tlb_flushall_shift)
  259. on_each_cpu(do_flush_tlb_all, NULL, 1);
  260. else {
  261. info.flush_start = start;
  262. info.flush_end = end;
  263. on_each_cpu(do_kernel_range_flush, &info, 1);
  264. }
  265. }
  266. #ifdef CONFIG_DEBUG_TLBFLUSH
  267. static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
  268. size_t count, loff_t *ppos)
  269. {
  270. char buf[32];
  271. unsigned int len;
  272. len = sprintf(buf, "%hd\n", tlb_flushall_shift);
  273. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  274. }
  275. static ssize_t tlbflush_write_file(struct file *file,
  276. const char __user *user_buf, size_t count, loff_t *ppos)
  277. {
  278. char buf[32];
  279. ssize_t len;
  280. s8 shift;
  281. len = min(count, sizeof(buf) - 1);
  282. if (copy_from_user(buf, user_buf, len))
  283. return -EFAULT;
  284. buf[len] = '\0';
  285. if (kstrtos8(buf, 0, &shift))
  286. return -EINVAL;
  287. if (shift < -1 || shift >= BITS_PER_LONG)
  288. return -EINVAL;
  289. tlb_flushall_shift = shift;
  290. return count;
  291. }
  292. static const struct file_operations fops_tlbflush = {
  293. .read = tlbflush_read_file,
  294. .write = tlbflush_write_file,
  295. .llseek = default_llseek,
  296. };
  297. static int __init create_tlb_flushall_shift(void)
  298. {
  299. debugfs_create_file("tlb_flushall_shift", S_IRUSR | S_IWUSR,
  300. arch_debugfs_dir, NULL, &fops_tlbflush);
  301. return 0;
  302. }
  303. late_initcall(create_tlb_flushall_shift);
  304. #endif