Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | |
| 3 | #include <linux/mm.h> |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 4 | #include <linux/spinlock.h> |
| 5 | #include <linux/smp.h> |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 6 | #include <linux/interrupt.h> |
Paul Gortmaker | 4b599fe | 2016-07-13 20:18:55 -0400 | [diff] [blame] | 7 | #include <linux/export.h> |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 8 | #include <linux/cpu.h> |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 9 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 10 | #include <asm/tlbflush.h> |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 11 | #include <asm/mmu_context.h> |
Jan Beulich | 350f8f5 | 2009-11-13 11:54:40 +0000 | [diff] [blame] | 12 | #include <asm/cache.h> |
Tejun Heo | 6dd01be | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 13 | #include <asm/apic.h> |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 14 | #include <asm/uv/uv.h> |
Alex Shi | 3df3212 | 2012-06-28 09:02:20 +0800 | [diff] [blame] | 15 | #include <linux/debugfs.h> |
Glauber Costa | 5af5573 | 2008-03-25 13:28:56 -0300 | [diff] [blame] | 16 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 17 | /* |
Andy Lutomirski | ce4a4e56 | 2017-05-28 10:00:14 -0700 | [diff] [blame] | 18 | * TLB flushing, formerly SMP-only |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 19 | * c/o Linus Torvalds. |
| 20 | * |
| 21 | * These mean you can really definitely utterly forget about |
| 22 | * writing to user space from interrupts. (Its not allowed anyway). |
| 23 | * |
| 24 | * Optimizations Manfred Spraul <manfred@colorfullife.com> |
| 25 | * |
| 26 | * More scalable flush, from Andi Kleen |
| 27 | * |
Alex Shi | 52aec33 | 2012-06-28 09:02:23 +0800 | [diff] [blame] | 28 | * Implement flush IPI by CALL_FUNCTION_VECTOR, Alex Shi |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 29 | */ |
| 30 | |
Andy Lutomirski | f39681e | 2017-06-29 08:53:15 -0700 | [diff] [blame] | 31 | atomic64_t last_mm_ctx_id = ATOMIC64_INIT(1); |
| 32 | |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 33 | static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, |
| 34 | u16 *new_asid, bool *need_flush) |
| 35 | { |
| 36 | u16 asid; |
| 37 | |
| 38 | if (!static_cpu_has(X86_FEATURE_PCID)) { |
| 39 | *new_asid = 0; |
| 40 | *need_flush = true; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | for (asid = 0; asid < TLB_NR_DYN_ASIDS; asid++) { |
| 45 | if (this_cpu_read(cpu_tlbstate.ctxs[asid].ctx_id) != |
| 46 | next->context.ctx_id) |
| 47 | continue; |
| 48 | |
| 49 | *new_asid = asid; |
| 50 | *need_flush = (this_cpu_read(cpu_tlbstate.ctxs[asid].tlb_gen) < |
| 51 | next_tlb_gen); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * We don't currently own an ASID slot on this CPU. |
| 57 | * Allocate a slot. |
| 58 | */ |
| 59 | *new_asid = this_cpu_add_return(cpu_tlbstate.next_asid, 1) - 1; |
| 60 | if (*new_asid >= TLB_NR_DYN_ASIDS) { |
| 61 | *new_asid = 0; |
| 62 | this_cpu_write(cpu_tlbstate.next_asid, 1); |
| 63 | } |
| 64 | *need_flush = true; |
| 65 | } |
| 66 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 67 | void leave_mm(int cpu) |
| 68 | { |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 69 | struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm); |
| 70 | |
| 71 | /* |
| 72 | * It's plausible that we're in lazy TLB mode while our mm is init_mm. |
| 73 | * If so, our callers still expect us to flush the TLB, but there |
| 74 | * aren't any user TLB entries in init_mm to worry about. |
| 75 | * |
| 76 | * This needs to happen before any other sanity checks due to |
| 77 | * intel_idle's shenanigans. |
| 78 | */ |
| 79 | if (loaded_mm == &init_mm) |
| 80 | return; |
| 81 | |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 82 | /* Warn if we're not lazy. */ |
| 83 | WARN_ON(cpumask_test_cpu(smp_processor_id(), mm_cpumask(loaded_mm))); |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 84 | |
| 85 | switch_mm(NULL, &init_mm, NULL); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 86 | } |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 87 | |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 88 | void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
| 89 | struct task_struct *tsk) |
| 90 | { |
Andy Lutomirski | 078194f | 2016-04-26 09:39:09 -0700 | [diff] [blame] | 91 | unsigned long flags; |
| 92 | |
| 93 | local_irq_save(flags); |
| 94 | switch_mm_irqs_off(prev, next, tsk); |
| 95 | local_irq_restore(flags); |
| 96 | } |
| 97 | |
| 98 | void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, |
| 99 | struct task_struct *tsk) |
| 100 | { |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 101 | struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm); |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 102 | u16 prev_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 103 | unsigned cpu = smp_processor_id(); |
| 104 | u64 next_tlb_gen; |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 105 | |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 106 | /* |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 107 | * NB: The scheduler will call us with prev == next when switching |
| 108 | * from lazy TLB mode to normal mode if active_mm isn't changing. |
| 109 | * When this happens, we don't assume that CR3 (and hence |
| 110 | * cpu_tlbstate.loaded_mm) matches next. |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 111 | * |
| 112 | * NB: leave_mm() calls us with prev == NULL and tsk == NULL. |
| 113 | */ |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 114 | |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 115 | /* We don't want flush_tlb_func_* to run concurrently with us. */ |
| 116 | if (IS_ENABLED(CONFIG_PROVE_LOCKING)) |
| 117 | WARN_ON_ONCE(!irqs_disabled()); |
| 118 | |
| 119 | /* |
| 120 | * Verify that CR3 is what we think it is. This will catch |
| 121 | * hypothetical buggy code that directly switches to swapper_pg_dir |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 122 | * without going through leave_mm() / switch_mm_irqs_off() or that |
| 123 | * does something like write_cr3(read_cr3_pa()). |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 124 | */ |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 125 | VM_BUG_ON(__read_cr3() != (__sme_pa(real_prev->pgd) | prev_asid)); |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 126 | |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 127 | if (real_prev == next) { |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 128 | VM_BUG_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) != |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 129 | next->context.ctx_id); |
| 130 | |
| 131 | if (cpumask_test_cpu(cpu, mm_cpumask(next))) { |
| 132 | /* |
| 133 | * There's nothing to do: we weren't lazy, and we |
| 134 | * aren't changing our mm. We don't need to flush |
| 135 | * anything, nor do we need to update CR3, CR4, or |
| 136 | * LDTR. |
| 137 | */ |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | /* Resume remote flushes and then read tlb_gen. */ |
| 142 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
| 143 | next_tlb_gen = atomic64_read(&next->context.tlb_gen); |
| 144 | |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 145 | if (this_cpu_read(cpu_tlbstate.ctxs[prev_asid].tlb_gen) < |
| 146 | next_tlb_gen) { |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 147 | /* |
| 148 | * Ideally, we'd have a flush_tlb() variant that |
| 149 | * takes the known CR3 value as input. This would |
| 150 | * be faster on Xen PV and on hypothetical CPUs |
| 151 | * on which INVPCID is fast. |
| 152 | */ |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 153 | this_cpu_write(cpu_tlbstate.ctxs[prev_asid].tlb_gen, |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 154 | next_tlb_gen); |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 155 | write_cr3(__sme_pa(next->pgd) | prev_asid); |
Andy Lutomirski | 43858b4 | 2017-06-29 08:53:18 -0700 | [diff] [blame] | 156 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, |
| 157 | TLB_FLUSH_ALL); |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 160 | /* |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 161 | * We just exited lazy mode, which means that CR4 and/or LDTR |
| 162 | * may be stale. (Changes to the required CR4 and LDTR states |
| 163 | * are not reflected in tlb_gen.) |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 164 | */ |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 165 | } else { |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 166 | u16 new_asid; |
| 167 | bool need_flush; |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 168 | |
| 169 | if (IS_ENABLED(CONFIG_VMAP_STACK)) { |
| 170 | /* |
| 171 | * If our current stack is in vmalloc space and isn't |
| 172 | * mapped in the new pgd, we'll double-fault. Forcibly |
| 173 | * map it. |
| 174 | */ |
| 175 | unsigned int index = pgd_index(current_stack_pointer()); |
| 176 | pgd_t *pgd = next->pgd + index; |
| 177 | |
| 178 | if (unlikely(pgd_none(*pgd))) |
| 179 | set_pgd(pgd, init_mm.pgd[index]); |
| 180 | } |
| 181 | |
| 182 | /* Stop remote flushes for the previous mm */ |
| 183 | if (cpumask_test_cpu(cpu, mm_cpumask(real_prev))) |
| 184 | cpumask_clear_cpu(cpu, mm_cpumask(real_prev)); |
| 185 | |
| 186 | VM_WARN_ON_ONCE(cpumask_test_cpu(cpu, mm_cpumask(next))); |
| 187 | |
| 188 | /* |
| 189 | * Start remote flushes and then read tlb_gen. |
| 190 | */ |
| 191 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
| 192 | next_tlb_gen = atomic64_read(&next->context.tlb_gen); |
| 193 | |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 194 | choose_new_asid(next, next_tlb_gen, &new_asid, &need_flush); |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 195 | |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 196 | if (need_flush) { |
| 197 | this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id); |
| 198 | this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); |
| 199 | write_cr3(__sme_pa(next->pgd) | new_asid); |
| 200 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, |
| 201 | TLB_FLUSH_ALL); |
| 202 | } else { |
| 203 | /* The new ASID is already up to date. */ |
| 204 | write_cr3(__sme_pa(next->pgd) | new_asid | CR3_NOFLUSH); |
| 205 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, 0); |
| 206 | } |
| 207 | |
| 208 | this_cpu_write(cpu_tlbstate.loaded_mm, next); |
| 209 | this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid); |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 210 | } |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 211 | |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 212 | load_mm_cr4(next); |
Andy Lutomirski | 7353425 | 2017-06-20 22:22:08 -0700 | [diff] [blame] | 213 | switch_ldt(real_prev, next); |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 216 | /* |
Andy Lutomirski | 72c0098 | 2017-09-06 19:54:53 -0700 | [diff] [blame^] | 217 | * Call this when reinitializing a CPU. It fixes the following potential |
| 218 | * problems: |
| 219 | * |
| 220 | * - The ASID changed from what cpu_tlbstate thinks it is (most likely |
| 221 | * because the CPU was taken down and came back up with CR3's PCID |
| 222 | * bits clear. CPU hotplug can do this. |
| 223 | * |
| 224 | * - The TLB contains junk in slots corresponding to inactive ASIDs. |
| 225 | * |
| 226 | * - The CPU went so far out to lunch that it may have missed a TLB |
| 227 | * flush. |
| 228 | */ |
| 229 | void initialize_tlbstate_and_flush(void) |
| 230 | { |
| 231 | int i; |
| 232 | struct mm_struct *mm = this_cpu_read(cpu_tlbstate.loaded_mm); |
| 233 | u64 tlb_gen = atomic64_read(&init_mm.context.tlb_gen); |
| 234 | unsigned long cr3 = __read_cr3(); |
| 235 | |
| 236 | /* Assert that CR3 already references the right mm. */ |
| 237 | WARN_ON((cr3 & CR3_ADDR_MASK) != __pa(mm->pgd)); |
| 238 | |
| 239 | /* |
| 240 | * Assert that CR4.PCIDE is set if needed. (CR4.PCIDE initialization |
| 241 | * doesn't work like other CR4 bits because it can only be set from |
| 242 | * long mode.) |
| 243 | */ |
| 244 | WARN_ON(boot_cpu_has(X86_CR4_PCIDE) && |
| 245 | !(cr4_read_shadow() & X86_CR4_PCIDE)); |
| 246 | |
| 247 | /* Force ASID 0 and force a TLB flush. */ |
| 248 | write_cr3(cr3 & ~CR3_PCID_MASK); |
| 249 | |
| 250 | /* Reinitialize tlbstate. */ |
| 251 | this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0); |
| 252 | this_cpu_write(cpu_tlbstate.next_asid, 1); |
| 253 | this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id); |
| 254 | this_cpu_write(cpu_tlbstate.ctxs[0].tlb_gen, tlb_gen); |
| 255 | |
| 256 | for (i = 1; i < TLB_NR_DYN_ASIDS; i++) |
| 257 | this_cpu_write(cpu_tlbstate.ctxs[i].ctx_id, 0); |
| 258 | } |
| 259 | |
| 260 | /* |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 261 | * flush_tlb_func_common()'s memory ordering requirement is that any |
| 262 | * TLB fills that happen after we flush the TLB are ordered after we |
| 263 | * read active_mm's tlb_gen. We don't need any explicit barriers |
| 264 | * because all x86 flush operations are serializing and the |
| 265 | * atomic64_read operation won't be reordered by the compiler. |
| 266 | */ |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 267 | static void flush_tlb_func_common(const struct flush_tlb_info *f, |
| 268 | bool local, enum tlb_flush_reason reason) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 269 | { |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 270 | /* |
| 271 | * We have three different tlb_gen values in here. They are: |
| 272 | * |
| 273 | * - mm_tlb_gen: the latest generation. |
| 274 | * - local_tlb_gen: the generation that this CPU has already caught |
| 275 | * up to. |
| 276 | * - f->new_tlb_gen: the generation that the requester of the flush |
| 277 | * wants us to catch up to. |
| 278 | */ |
| 279 | struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm); |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 280 | u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 281 | u64 mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen); |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 282 | u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen); |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 283 | |
Andy Lutomirski | bc0d5a8 | 2017-06-29 08:53:13 -0700 | [diff] [blame] | 284 | /* This code cannot presently handle being reentered. */ |
| 285 | VM_WARN_ON(!irqs_disabled()); |
| 286 | |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 287 | VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) != |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 288 | loaded_mm->context.ctx_id); |
| 289 | |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 290 | if (!cpumask_test_cpu(smp_processor_id(), mm_cpumask(loaded_mm))) { |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 291 | /* |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 292 | * We're in lazy mode -- don't flush. We can get here on |
| 293 | * remote flushes due to races and on local flushes if a |
| 294 | * kernel thread coincidentally flushes the mm it's lazily |
| 295 | * still using. |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 296 | */ |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 300 | if (unlikely(local_tlb_gen == mm_tlb_gen)) { |
| 301 | /* |
| 302 | * There's nothing to do: we're already up to date. This can |
| 303 | * happen if two concurrent flushes happen -- the first flush to |
| 304 | * be handled can catch us all the way up, leaving no work for |
| 305 | * the second flush. |
| 306 | */ |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 307 | trace_tlb_flush(reason, 0); |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
| 311 | WARN_ON_ONCE(local_tlb_gen > mm_tlb_gen); |
| 312 | WARN_ON_ONCE(f->new_tlb_gen > mm_tlb_gen); |
| 313 | |
| 314 | /* |
| 315 | * If we get to this point, we know that our TLB is out of date. |
| 316 | * This does not strictly imply that we need to flush (it's |
| 317 | * possible that f->new_tlb_gen <= local_tlb_gen), but we're |
| 318 | * going to need to flush in the very near future, so we might |
| 319 | * as well get it over with. |
| 320 | * |
| 321 | * The only question is whether to do a full or partial flush. |
| 322 | * |
| 323 | * We do a partial flush if requested and two extra conditions |
| 324 | * are met: |
| 325 | * |
| 326 | * 1. f->new_tlb_gen == local_tlb_gen + 1. We have an invariant that |
| 327 | * we've always done all needed flushes to catch up to |
| 328 | * local_tlb_gen. If, for example, local_tlb_gen == 2 and |
| 329 | * f->new_tlb_gen == 3, then we know that the flush needed to bring |
| 330 | * us up to date for tlb_gen 3 is the partial flush we're |
| 331 | * processing. |
| 332 | * |
| 333 | * As an example of why this check is needed, suppose that there |
| 334 | * are two concurrent flushes. The first is a full flush that |
| 335 | * changes context.tlb_gen from 1 to 2. The second is a partial |
| 336 | * flush that changes context.tlb_gen from 2 to 3. If they get |
| 337 | * processed on this CPU in reverse order, we'll see |
| 338 | * local_tlb_gen == 1, mm_tlb_gen == 3, and end != TLB_FLUSH_ALL. |
| 339 | * If we were to use __flush_tlb_single() and set local_tlb_gen to |
| 340 | * 3, we'd be break the invariant: we'd update local_tlb_gen above |
| 341 | * 1 without the full flush that's needed for tlb_gen 2. |
| 342 | * |
| 343 | * 2. f->new_tlb_gen == mm_tlb_gen. This is purely an optimiation. |
| 344 | * Partial TLB flushes are not all that much cheaper than full TLB |
| 345 | * flushes, so it seems unlikely that it would be a performance win |
| 346 | * to do a partial flush if that won't bring our TLB fully up to |
| 347 | * date. By doing a full flush instead, we can increase |
| 348 | * local_tlb_gen all the way to mm_tlb_gen and we can probably |
| 349 | * avoid another flush in the very near future. |
| 350 | */ |
| 351 | if (f->end != TLB_FLUSH_ALL && |
| 352 | f->new_tlb_gen == local_tlb_gen + 1 && |
| 353 | f->new_tlb_gen == mm_tlb_gen) { |
| 354 | /* Partial flush */ |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 355 | unsigned long addr; |
Andy Lutomirski | be4ffc0 | 2017-05-28 10:00:16 -0700 | [diff] [blame] | 356 | unsigned long nr_pages = (f->end - f->start) >> PAGE_SHIFT; |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 357 | |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 358 | addr = f->start; |
| 359 | while (addr < f->end) { |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 360 | __flush_tlb_single(addr); |
| 361 | addr += PAGE_SIZE; |
| 362 | } |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 363 | if (local) |
| 364 | count_vm_tlb_events(NR_TLB_LOCAL_FLUSH_ONE, nr_pages); |
| 365 | trace_tlb_flush(reason, nr_pages); |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 366 | } else { |
| 367 | /* Full flush. */ |
| 368 | local_flush_tlb(); |
| 369 | if (local) |
| 370 | count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL); |
| 371 | trace_tlb_flush(reason, TLB_FLUSH_ALL); |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 372 | } |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 373 | |
| 374 | /* Both paths above update our state to mm_tlb_gen. */ |
Andy Lutomirski | 10af623 | 2017-07-24 21:41:38 -0700 | [diff] [blame] | 375 | this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 376 | } |
| 377 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 378 | static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason) |
| 379 | { |
| 380 | const struct flush_tlb_info *f = info; |
| 381 | |
| 382 | flush_tlb_func_common(f, true, reason); |
| 383 | } |
| 384 | |
| 385 | static void flush_tlb_func_remote(void *info) |
| 386 | { |
| 387 | const struct flush_tlb_info *f = info; |
| 388 | |
| 389 | inc_irq_stat(irq_tlb_count); |
| 390 | |
Andy Lutomirski | 3d28ebc | 2017-05-28 10:00:15 -0700 | [diff] [blame] | 391 | if (f->mm && f->mm != this_cpu_read(cpu_tlbstate.loaded_mm)) |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 392 | return; |
| 393 | |
| 394 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED); |
| 395 | flush_tlb_func_common(f, false, TLB_REMOTE_SHOOTDOWN); |
| 396 | } |
| 397 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 398 | void native_flush_tlb_others(const struct cpumask *cpumask, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 399 | const struct flush_tlb_info *info) |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 400 | { |
Mel Gorman | ec65993 | 2014-01-21 14:33:16 -0800 | [diff] [blame] | 401 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH); |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 402 | if (info->end == TLB_FLUSH_ALL) |
Nadav Amit | 18c9824 | 2016-04-01 14:31:23 -0700 | [diff] [blame] | 403 | trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL); |
| 404 | else |
| 405 | trace_tlb_flush(TLB_REMOTE_SEND_IPI, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 406 | (info->end - info->start) >> PAGE_SHIFT); |
Nadav Amit | 18c9824 | 2016-04-01 14:31:23 -0700 | [diff] [blame] | 407 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 408 | if (is_uv_system()) { |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 409 | /* |
| 410 | * This whole special case is confused. UV has a "Broadcast |
| 411 | * Assist Unit", which seems to be a fancy way to send IPIs. |
| 412 | * Back when x86 used an explicit TLB flush IPI, UV was |
| 413 | * optimized to use its own mechanism. These days, x86 uses |
| 414 | * smp_call_function_many(), but UV still uses a manual IPI, |
| 415 | * and that IPI's action is out of date -- it does a manual |
| 416 | * flush instead of calling flush_tlb_func_remote(). This |
| 417 | * means that the percpu tlb_gen variables won't be updated |
| 418 | * and we'll do pointless flushes on future context switches. |
| 419 | * |
| 420 | * Rather than hooking native_flush_tlb_others() here, I think |
| 421 | * that UV should be updated so that smp_call_function_many(), |
| 422 | * etc, are optimal on UV. |
| 423 | */ |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 424 | unsigned int cpu; |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 425 | |
Xiao Guangrong | 25542c6 | 2011-03-15 09:57:37 +0800 | [diff] [blame] | 426 | cpu = smp_processor_id(); |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 427 | cpumask = uv_flush_tlb_others(cpumask, info); |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 428 | if (cpumask) |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 429 | smp_call_function_many(cpumask, flush_tlb_func_remote, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 430 | (void *)info, 1); |
Mike Travis | 0e21990 | 2009-01-10 21:58:10 -0800 | [diff] [blame] | 431 | return; |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 432 | } |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 433 | smp_call_function_many(cpumask, flush_tlb_func_remote, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 434 | (void *)info, 1); |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Dave Hansen | a510247 | 2014-07-31 08:41:03 -0700 | [diff] [blame] | 437 | /* |
| 438 | * See Documentation/x86/tlb.txt for details. We choose 33 |
| 439 | * because it is large enough to cover the vast majority (at |
| 440 | * least 95%) of allocations, and is small enough that we are |
| 441 | * confident it will not cause too much overhead. Each single |
| 442 | * flush is about 100 ns, so this caps the maximum overhead at |
| 443 | * _about_ 3,000 ns. |
| 444 | * |
| 445 | * This is in units of pages. |
| 446 | */ |
Jeremiah Mahler | 8642685 | 2014-08-09 00:38:33 -0700 | [diff] [blame] | 447 | static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33; |
Dave Hansen | e9f4e0a | 2014-07-31 08:40:55 -0700 | [diff] [blame] | 448 | |
Alex Shi | 611ae8e | 2012-06-28 09:02:22 +0800 | [diff] [blame] | 449 | void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, |
| 450 | unsigned long end, unsigned long vmflag) |
| 451 | { |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 452 | int cpu; |
Alex Shi | 611ae8e | 2012-06-28 09:02:22 +0800 | [diff] [blame] | 453 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 454 | struct flush_tlb_info info = { |
| 455 | .mm = mm, |
| 456 | }; |
Andy Lutomirski | ce27374 | 2017-04-22 00:01:21 -0700 | [diff] [blame] | 457 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 458 | cpu = get_cpu(); |
Andy Lutomirski | ce27374 | 2017-04-22 00:01:21 -0700 | [diff] [blame] | 459 | |
Andy Lutomirski | f39681e | 2017-06-29 08:53:15 -0700 | [diff] [blame] | 460 | /* This is also a barrier that synchronizes with switch_mm(). */ |
Andy Lutomirski | b0579ad | 2017-06-29 08:53:16 -0700 | [diff] [blame] | 461 | info.new_tlb_gen = inc_mm_tlb_gen(mm); |
Andy Lutomirski | 71b3c12 | 2016-01-06 12:21:01 -0800 | [diff] [blame] | 462 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 463 | /* Should we flush just the requested range? */ |
| 464 | if ((end != TLB_FLUSH_ALL) && |
| 465 | !(vmflag & VM_HUGETLB) && |
| 466 | ((end - start) >> PAGE_SHIFT) <= tlb_single_page_flush_ceiling) { |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 467 | info.start = start; |
| 468 | info.end = end; |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 469 | } else { |
| 470 | info.start = 0UL; |
| 471 | info.end = TLB_FLUSH_ALL; |
Dave Hansen | 4995ab9 | 2014-07-31 08:40:54 -0700 | [diff] [blame] | 472 | } |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 473 | |
Andy Lutomirski | bc0d5a8 | 2017-06-29 08:53:13 -0700 | [diff] [blame] | 474 | if (mm == this_cpu_read(cpu_tlbstate.loaded_mm)) { |
| 475 | VM_WARN_ON(irqs_disabled()); |
| 476 | local_irq_disable(); |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 477 | flush_tlb_func_local(&info, TLB_LOCAL_MM_SHOOTDOWN); |
Andy Lutomirski | bc0d5a8 | 2017-06-29 08:53:13 -0700 | [diff] [blame] | 478 | local_irq_enable(); |
| 479 | } |
| 480 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 481 | if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids) |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 482 | flush_tlb_others(mm_cpumask(mm), &info); |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 483 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 484 | put_cpu(); |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 485 | } |
| 486 | |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 487 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 488 | static void do_flush_tlb_all(void *info) |
| 489 | { |
Mel Gorman | ec65993 | 2014-01-21 14:33:16 -0800 | [diff] [blame] | 490 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 491 | __flush_tlb_all(); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void flush_tlb_all(void) |
| 495 | { |
Mel Gorman | ec65993 | 2014-01-21 14:33:16 -0800 | [diff] [blame] | 496 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 497 | on_each_cpu(do_flush_tlb_all, NULL, 1); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 498 | } |
Alex Shi | 3df3212 | 2012-06-28 09:02:20 +0800 | [diff] [blame] | 499 | |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 500 | static void do_kernel_range_flush(void *info) |
| 501 | { |
| 502 | struct flush_tlb_info *f = info; |
| 503 | unsigned long addr; |
| 504 | |
| 505 | /* flush range by one by one 'invlpg' */ |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 506 | for (addr = f->start; addr < f->end; addr += PAGE_SIZE) |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 507 | __flush_tlb_single(addr); |
| 508 | } |
| 509 | |
| 510 | void flush_tlb_kernel_range(unsigned long start, unsigned long end) |
| 511 | { |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 512 | |
| 513 | /* Balance as user space task's flush, a bit conservative */ |
Dave Hansen | e9f4e0a | 2014-07-31 08:40:55 -0700 | [diff] [blame] | 514 | if (end == TLB_FLUSH_ALL || |
Andy Lutomirski | be4ffc0 | 2017-05-28 10:00:16 -0700 | [diff] [blame] | 515 | (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) { |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 516 | on_each_cpu(do_flush_tlb_all, NULL, 1); |
Dave Hansen | e9f4e0a | 2014-07-31 08:40:55 -0700 | [diff] [blame] | 517 | } else { |
| 518 | struct flush_tlb_info info; |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 519 | info.start = start; |
| 520 | info.end = end; |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 521 | on_each_cpu(do_kernel_range_flush, &info, 1); |
| 522 | } |
| 523 | } |
Dave Hansen | 2d040a1 | 2014-07-31 08:41:01 -0700 | [diff] [blame] | 524 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 525 | void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) |
| 526 | { |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 527 | struct flush_tlb_info info = { |
| 528 | .mm = NULL, |
| 529 | .start = 0UL, |
| 530 | .end = TLB_FLUSH_ALL, |
| 531 | }; |
| 532 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 533 | int cpu = get_cpu(); |
| 534 | |
Andy Lutomirski | bc0d5a8 | 2017-06-29 08:53:13 -0700 | [diff] [blame] | 535 | if (cpumask_test_cpu(cpu, &batch->cpumask)) { |
| 536 | VM_WARN_ON(irqs_disabled()); |
| 537 | local_irq_disable(); |
Andy Lutomirski | 3f79e4c | 2017-05-28 10:00:13 -0700 | [diff] [blame] | 538 | flush_tlb_func_local(&info, TLB_LOCAL_SHOOTDOWN); |
Andy Lutomirski | bc0d5a8 | 2017-06-29 08:53:13 -0700 | [diff] [blame] | 539 | local_irq_enable(); |
| 540 | } |
| 541 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 542 | if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 543 | flush_tlb_others(&batch->cpumask, &info); |
Andy Lutomirski | 94b1b03 | 2017-06-29 08:53:17 -0700 | [diff] [blame] | 544 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 545 | cpumask_clear(&batch->cpumask); |
| 546 | |
| 547 | put_cpu(); |
| 548 | } |
| 549 | |
Dave Hansen | 2d040a1 | 2014-07-31 08:41:01 -0700 | [diff] [blame] | 550 | static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf, |
| 551 | size_t count, loff_t *ppos) |
| 552 | { |
| 553 | char buf[32]; |
| 554 | unsigned int len; |
| 555 | |
| 556 | len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling); |
| 557 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 558 | } |
| 559 | |
| 560 | static ssize_t tlbflush_write_file(struct file *file, |
| 561 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 562 | { |
| 563 | char buf[32]; |
| 564 | ssize_t len; |
| 565 | int ceiling; |
| 566 | |
| 567 | len = min(count, sizeof(buf) - 1); |
| 568 | if (copy_from_user(buf, user_buf, len)) |
| 569 | return -EFAULT; |
| 570 | |
| 571 | buf[len] = '\0'; |
| 572 | if (kstrtoint(buf, 0, &ceiling)) |
| 573 | return -EINVAL; |
| 574 | |
| 575 | if (ceiling < 0) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | tlb_single_page_flush_ceiling = ceiling; |
| 579 | return count; |
| 580 | } |
| 581 | |
| 582 | static const struct file_operations fops_tlbflush = { |
| 583 | .read = tlbflush_read_file, |
| 584 | .write = tlbflush_write_file, |
| 585 | .llseek = default_llseek, |
| 586 | }; |
| 587 | |
| 588 | static int __init create_tlb_single_page_flush_ceiling(void) |
| 589 | { |
| 590 | debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR, |
| 591 | arch_debugfs_dir, NULL, &fops_tlbflush); |
| 592 | return 0; |
| 593 | } |
| 594 | late_initcall(create_tlb_single_page_flush_ceiling); |