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 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 31 | /* |
| 32 | * We cannot call mmdrop() because we are in interrupt context, |
| 33 | * instead update mm->cpu_vm_mask. |
| 34 | */ |
| 35 | void leave_mm(int cpu) |
| 36 | { |
Linus Torvalds | 02171b4 | 2012-05-23 11:06:59 -0700 | [diff] [blame] | 37 | struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm); |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 38 | if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 39 | BUG(); |
Suresh Siddha | a6fca40 | 2012-03-22 17:01:25 -0700 | [diff] [blame] | 40 | if (cpumask_test_cpu(cpu, mm_cpumask(active_mm))) { |
| 41 | cpumask_clear_cpu(cpu, mm_cpumask(active_mm)); |
| 42 | load_cr3(swapper_pg_dir); |
Dave Hansen | 7c7f154 | 2014-08-07 10:58:41 -0700 | [diff] [blame] | 43 | /* |
| 44 | * This gets called in the idle path where RCU |
| 45 | * functions differently. Tracing normally |
| 46 | * uses RCU, so we have to call the tracepoint |
| 47 | * specially here. |
| 48 | */ |
| 49 | trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); |
Suresh Siddha | a6fca40 | 2012-03-22 17:01:25 -0700 | [diff] [blame] | 50 | } |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 51 | } |
| 52 | EXPORT_SYMBOL_GPL(leave_mm); |
| 53 | |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 54 | void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
| 55 | struct task_struct *tsk) |
| 56 | { |
Andy Lutomirski | 078194f | 2016-04-26 09:39:09 -0700 | [diff] [blame] | 57 | unsigned long flags; |
| 58 | |
| 59 | local_irq_save(flags); |
| 60 | switch_mm_irqs_off(prev, next, tsk); |
| 61 | local_irq_restore(flags); |
| 62 | } |
| 63 | |
| 64 | void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, |
| 65 | struct task_struct *tsk) |
| 66 | { |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 67 | unsigned cpu = smp_processor_id(); |
| 68 | |
| 69 | if (likely(prev != next)) { |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 70 | if (IS_ENABLED(CONFIG_VMAP_STACK)) { |
| 71 | /* |
| 72 | * If our current stack is in vmalloc space and isn't |
| 73 | * mapped in the new pgd, we'll double-fault. Forcibly |
| 74 | * map it. |
| 75 | */ |
| 76 | unsigned int stack_pgd_index = pgd_index(current_stack_pointer()); |
| 77 | |
| 78 | pgd_t *pgd = next->pgd + stack_pgd_index; |
| 79 | |
| 80 | if (unlikely(pgd_none(*pgd))) |
| 81 | set_pgd(pgd, init_mm.pgd[stack_pgd_index]); |
| 82 | } |
| 83 | |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 84 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK); |
| 85 | this_cpu_write(cpu_tlbstate.active_mm, next); |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 86 | |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 87 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
| 88 | |
| 89 | /* |
| 90 | * Re-load page tables. |
| 91 | * |
| 92 | * This logic has an ordering constraint: |
| 93 | * |
| 94 | * CPU 0: Write to a PTE for 'next' |
| 95 | * CPU 0: load bit 1 in mm_cpumask. if nonzero, send IPI. |
| 96 | * CPU 1: set bit 1 in next's mm_cpumask |
| 97 | * CPU 1: load from the PTE that CPU 0 writes (implicit) |
| 98 | * |
| 99 | * We need to prevent an outcome in which CPU 1 observes |
| 100 | * the new PTE value and CPU 0 observes bit 1 clear in |
| 101 | * mm_cpumask. (If that occurs, then the IPI will never |
| 102 | * be sent, and CPU 0's TLB will contain a stale entry.) |
| 103 | * |
| 104 | * The bad outcome can occur if either CPU's load is |
| 105 | * reordered before that CPU's store, so both CPUs must |
| 106 | * execute full barriers to prevent this from happening. |
| 107 | * |
| 108 | * Thus, switch_mm needs a full barrier between the |
| 109 | * store to mm_cpumask and any operation that could load |
| 110 | * from next->pgd. TLB fills are special and can happen |
| 111 | * due to instruction fetches or for no reason at all, |
| 112 | * and neither LOCK nor MFENCE orders them. |
| 113 | * Fortunately, load_cr3() is serializing and gives the |
| 114 | * ordering guarantee we need. |
| 115 | * |
| 116 | */ |
| 117 | load_cr3(next->pgd); |
| 118 | |
| 119 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); |
| 120 | |
| 121 | /* Stop flush ipis for the previous mm */ |
| 122 | cpumask_clear_cpu(cpu, mm_cpumask(prev)); |
| 123 | |
| 124 | /* Load per-mm CR4 state */ |
| 125 | load_mm_cr4(next); |
| 126 | |
| 127 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 128 | /* |
| 129 | * Load the LDT, if the LDT is different. |
| 130 | * |
| 131 | * It's possible that prev->context.ldt doesn't match |
| 132 | * the LDT register. This can happen if leave_mm(prev) |
| 133 | * was called and then modify_ldt changed |
| 134 | * prev->context.ldt but suppressed an IPI to this CPU. |
| 135 | * In this case, prev->context.ldt != NULL, because we |
| 136 | * never set context.ldt to NULL while the mm still |
| 137 | * exists. That means that next->context.ldt != |
| 138 | * prev->context.ldt, because mms never share an LDT. |
| 139 | */ |
| 140 | if (unlikely(prev->context.ldt != next->context.ldt)) |
| 141 | load_mm_ldt(next); |
| 142 | #endif |
Andy Lutomirski | ce4a4e56 | 2017-05-28 10:00:14 -0700 | [diff] [blame^] | 143 | } else { |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 144 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK); |
| 145 | BUG_ON(this_cpu_read(cpu_tlbstate.active_mm) != next); |
| 146 | |
| 147 | if (!cpumask_test_cpu(cpu, mm_cpumask(next))) { |
| 148 | /* |
| 149 | * On established mms, the mm_cpumask is only changed |
| 150 | * from irq context, from ptep_clear_flush() while in |
| 151 | * lazy tlb mode, and here. Irqs are blocked during |
| 152 | * schedule, protecting us from simultaneous changes. |
| 153 | */ |
| 154 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
| 155 | |
| 156 | /* |
| 157 | * We were in lazy tlb mode and leave_mm disabled |
| 158 | * tlb flush IPI delivery. We must reload CR3 |
| 159 | * to make sure to use no freed page tables. |
| 160 | * |
| 161 | * As above, load_cr3() is serializing and orders TLB |
| 162 | * fills with respect to the mm_cpumask write. |
| 163 | */ |
| 164 | load_cr3(next->pgd); |
| 165 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); |
| 166 | load_mm_cr4(next); |
| 167 | load_mm_ldt(next); |
| 168 | } |
| 169 | } |
Andy Lutomirski | 69c0319 | 2016-04-26 09:39:08 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 172 | /* |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 173 | * The flush IPI assumes that a thread switch happens in this order: |
| 174 | * [cpu0: the cpu that switches] |
| 175 | * 1) switch_mm() either 1a) or 1b) |
| 176 | * 1a) thread switch to a different mm |
Alex Shi | 52aec33 | 2012-06-28 09:02:23 +0800 | [diff] [blame] | 177 | * 1a1) set cpu_tlbstate to TLBSTATE_OK |
| 178 | * Now the tlb flush NMI handler flush_tlb_func won't call leave_mm |
| 179 | * if cpu0 was in lazy tlb mode. |
| 180 | * 1a2) update cpu active_mm |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 181 | * Now cpu0 accepts tlb flushes for the new mm. |
Alex Shi | 52aec33 | 2012-06-28 09:02:23 +0800 | [diff] [blame] | 182 | * 1a3) cpu_set(cpu, new_mm->cpu_vm_mask); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 183 | * Now the other cpus will send tlb flush ipis. |
| 184 | * 1a4) change cr3. |
Alex Shi | 52aec33 | 2012-06-28 09:02:23 +0800 | [diff] [blame] | 185 | * 1a5) cpu_clear(cpu, old_mm->cpu_vm_mask); |
| 186 | * Stop ipi delivery for the old mm. This is not synchronized with |
| 187 | * the other cpus, but flush_tlb_func ignore flush ipis for the wrong |
| 188 | * mm, and in the worst case we perform a superfluous tlb flush. |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 189 | * 1b) thread switch without mm change |
Alex Shi | 52aec33 | 2012-06-28 09:02:23 +0800 | [diff] [blame] | 190 | * cpu active_mm is correct, cpu0 already handles flush ipis. |
| 191 | * 1b1) set cpu_tlbstate to TLBSTATE_OK |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 192 | * 1b2) test_and_set the cpu bit in cpu_vm_mask. |
| 193 | * Atomically set the bit [other cpus will start sending flush ipis], |
| 194 | * and test the bit. |
| 195 | * 1b3) if the bit was 0: leave_mm was called, flush the tlb. |
| 196 | * 2) switch %%esp, ie current |
| 197 | * |
| 198 | * The interrupt must handle 2 special cases: |
| 199 | * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm. |
| 200 | * - the cpu performs speculative tlb reads, i.e. even if the cpu only |
| 201 | * runs in kernel space, the cpu could load tlb entries for user space |
| 202 | * pages. |
| 203 | * |
Alex Shi | 52aec33 | 2012-06-28 09:02:23 +0800 | [diff] [blame] | 204 | * The good news is that cpu_tlbstate is local to each cpu, no |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 205 | * write/read ordering problems. |
| 206 | */ |
| 207 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 208 | static void flush_tlb_func_common(const struct flush_tlb_info *f, |
| 209 | bool local, enum tlb_flush_reason reason) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 210 | { |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 211 | if (this_cpu_read(cpu_tlbstate.state) != TLBSTATE_OK) { |
| 212 | leave_mm(smp_processor_id()); |
| 213 | return; |
| 214 | } |
| 215 | |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 216 | if (f->end == TLB_FLUSH_ALL) { |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 217 | local_flush_tlb(); |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 218 | if (local) |
| 219 | count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL); |
| 220 | trace_tlb_flush(reason, TLB_FLUSH_ALL); |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 221 | } else { |
| 222 | unsigned long addr; |
| 223 | unsigned long nr_pages = |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 224 | (f->end - f->start) / PAGE_SIZE; |
| 225 | addr = f->start; |
| 226 | while (addr < f->end) { |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 227 | __flush_tlb_single(addr); |
| 228 | addr += PAGE_SIZE; |
| 229 | } |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 230 | if (local) |
| 231 | count_vm_tlb_events(NR_TLB_LOCAL_FLUSH_ONE, nr_pages); |
| 232 | trace_tlb_flush(reason, nr_pages); |
Andy Lutomirski | b3b90e5 | 2017-05-22 15:30:02 -0700 | [diff] [blame] | 233 | } |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 234 | } |
| 235 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 236 | static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason) |
| 237 | { |
| 238 | const struct flush_tlb_info *f = info; |
| 239 | |
| 240 | flush_tlb_func_common(f, true, reason); |
| 241 | } |
| 242 | |
| 243 | static void flush_tlb_func_remote(void *info) |
| 244 | { |
| 245 | const struct flush_tlb_info *f = info; |
| 246 | |
| 247 | inc_irq_stat(irq_tlb_count); |
| 248 | |
| 249 | if (f->mm && f->mm != this_cpu_read(cpu_tlbstate.active_mm)) |
| 250 | return; |
| 251 | |
| 252 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED); |
| 253 | flush_tlb_func_common(f, false, TLB_REMOTE_SHOOTDOWN); |
| 254 | } |
| 255 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 256 | void native_flush_tlb_others(const struct cpumask *cpumask, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 257 | const struct flush_tlb_info *info) |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 258 | { |
Mel Gorman | ec65993 | 2014-01-21 14:33:16 -0800 | [diff] [blame] | 259 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH); |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 260 | if (info->end == TLB_FLUSH_ALL) |
Nadav Amit | 18c9824 | 2016-04-01 14:31:23 -0700 | [diff] [blame] | 261 | trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL); |
| 262 | else |
| 263 | trace_tlb_flush(TLB_REMOTE_SEND_IPI, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 264 | (info->end - info->start) >> PAGE_SHIFT); |
Nadav Amit | 18c9824 | 2016-04-01 14:31:23 -0700 | [diff] [blame] | 265 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 266 | if (is_uv_system()) { |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 267 | unsigned int cpu; |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 268 | |
Xiao Guangrong | 25542c6 | 2011-03-15 09:57:37 +0800 | [diff] [blame] | 269 | cpu = smp_processor_id(); |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 270 | cpumask = uv_flush_tlb_others(cpumask, info); |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 271 | if (cpumask) |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 272 | smp_call_function_many(cpumask, flush_tlb_func_remote, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 273 | (void *)info, 1); |
Mike Travis | 0e21990 | 2009-01-10 21:58:10 -0800 | [diff] [blame] | 274 | return; |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 275 | } |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 276 | smp_call_function_many(cpumask, flush_tlb_func_remote, |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 277 | (void *)info, 1); |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Dave Hansen | a510247 | 2014-07-31 08:41:03 -0700 | [diff] [blame] | 280 | /* |
| 281 | * See Documentation/x86/tlb.txt for details. We choose 33 |
| 282 | * because it is large enough to cover the vast majority (at |
| 283 | * least 95%) of allocations, and is small enough that we are |
| 284 | * confident it will not cause too much overhead. Each single |
| 285 | * flush is about 100 ns, so this caps the maximum overhead at |
| 286 | * _about_ 3,000 ns. |
| 287 | * |
| 288 | * This is in units of pages. |
| 289 | */ |
Jeremiah Mahler | 8642685 | 2014-08-09 00:38:33 -0700 | [diff] [blame] | 290 | static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33; |
Dave Hansen | e9f4e0a | 2014-07-31 08:40:55 -0700 | [diff] [blame] | 291 | |
Alex Shi | 611ae8e | 2012-06-28 09:02:22 +0800 | [diff] [blame] | 292 | void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, |
| 293 | unsigned long end, unsigned long vmflag) |
| 294 | { |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 295 | int cpu; |
Alex Shi | 611ae8e | 2012-06-28 09:02:22 +0800 | [diff] [blame] | 296 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 297 | struct flush_tlb_info info = { |
| 298 | .mm = mm, |
| 299 | }; |
Andy Lutomirski | ce27374 | 2017-04-22 00:01:21 -0700 | [diff] [blame] | 300 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 301 | cpu = get_cpu(); |
Andy Lutomirski | ce27374 | 2017-04-22 00:01:21 -0700 | [diff] [blame] | 302 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 303 | /* Synchronize with switch_mm. */ |
| 304 | smp_mb(); |
Andy Lutomirski | 71b3c12 | 2016-01-06 12:21:01 -0800 | [diff] [blame] | 305 | |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 306 | /* Should we flush just the requested range? */ |
| 307 | if ((end != TLB_FLUSH_ALL) && |
| 308 | !(vmflag & VM_HUGETLB) && |
| 309 | ((end - start) >> PAGE_SHIFT) <= tlb_single_page_flush_ceiling) { |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 310 | info.start = start; |
| 311 | info.end = end; |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 312 | } else { |
| 313 | info.start = 0UL; |
| 314 | info.end = TLB_FLUSH_ALL; |
Dave Hansen | 4995ab9 | 2014-07-31 08:40:54 -0700 | [diff] [blame] | 315 | } |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 316 | |
| 317 | if (mm == current->active_mm) |
| 318 | flush_tlb_func_local(&info, TLB_LOCAL_MM_SHOOTDOWN); |
| 319 | if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids) |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 320 | flush_tlb_others(mm_cpumask(mm), &info); |
Andy Lutomirski | 454bbad | 2017-05-28 10:00:12 -0700 | [diff] [blame] | 321 | put_cpu(); |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 322 | } |
| 323 | |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 324 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 325 | static void do_flush_tlb_all(void *info) |
| 326 | { |
Mel Gorman | ec65993 | 2014-01-21 14:33:16 -0800 | [diff] [blame] | 327 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 328 | __flush_tlb_all(); |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 329 | if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY) |
Borislav Petkov | 3f8afb7 | 2010-07-21 14:47:05 +0200 | [diff] [blame] | 330 | leave_mm(smp_processor_id()); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void flush_tlb_all(void) |
| 334 | { |
Mel Gorman | ec65993 | 2014-01-21 14:33:16 -0800 | [diff] [blame] | 335 | count_vm_tlb_event(NR_TLB_REMOTE_FLUSH); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 336 | on_each_cpu(do_flush_tlb_all, NULL, 1); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 337 | } |
Alex Shi | 3df3212 | 2012-06-28 09:02:20 +0800 | [diff] [blame] | 338 | |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 339 | static void do_kernel_range_flush(void *info) |
| 340 | { |
| 341 | struct flush_tlb_info *f = info; |
| 342 | unsigned long addr; |
| 343 | |
| 344 | /* flush range by one by one 'invlpg' */ |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 345 | for (addr = f->start; addr < f->end; addr += PAGE_SIZE) |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 346 | __flush_tlb_single(addr); |
| 347 | } |
| 348 | |
| 349 | void flush_tlb_kernel_range(unsigned long start, unsigned long end) |
| 350 | { |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 351 | |
| 352 | /* Balance as user space task's flush, a bit conservative */ |
Dave Hansen | e9f4e0a | 2014-07-31 08:40:55 -0700 | [diff] [blame] | 353 | if (end == TLB_FLUSH_ALL || |
| 354 | (end - start) > tlb_single_page_flush_ceiling * PAGE_SIZE) { |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 355 | on_each_cpu(do_flush_tlb_all, NULL, 1); |
Dave Hansen | e9f4e0a | 2014-07-31 08:40:55 -0700 | [diff] [blame] | 356 | } else { |
| 357 | struct flush_tlb_info info; |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 358 | info.start = start; |
| 359 | info.end = end; |
Alex Shi | effee4b | 2012-06-28 09:02:24 +0800 | [diff] [blame] | 360 | on_each_cpu(do_kernel_range_flush, &info, 1); |
| 361 | } |
| 362 | } |
Dave Hansen | 2d040a1 | 2014-07-31 08:41:01 -0700 | [diff] [blame] | 363 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 364 | void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) |
| 365 | { |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 366 | struct flush_tlb_info info = { |
| 367 | .mm = NULL, |
| 368 | .start = 0UL, |
| 369 | .end = TLB_FLUSH_ALL, |
| 370 | }; |
| 371 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 372 | int cpu = get_cpu(); |
| 373 | |
Andy Lutomirski | 3f79e4c | 2017-05-28 10:00:13 -0700 | [diff] [blame] | 374 | if (cpumask_test_cpu(cpu, &batch->cpumask)) |
| 375 | flush_tlb_func_local(&info, TLB_LOCAL_SHOOTDOWN); |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 376 | if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) |
Andy Lutomirski | a2055ab | 2017-05-28 10:00:10 -0700 | [diff] [blame] | 377 | flush_tlb_others(&batch->cpumask, &info); |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 378 | cpumask_clear(&batch->cpumask); |
| 379 | |
| 380 | put_cpu(); |
| 381 | } |
| 382 | |
Dave Hansen | 2d040a1 | 2014-07-31 08:41:01 -0700 | [diff] [blame] | 383 | static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf, |
| 384 | size_t count, loff_t *ppos) |
| 385 | { |
| 386 | char buf[32]; |
| 387 | unsigned int len; |
| 388 | |
| 389 | len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling); |
| 390 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 391 | } |
| 392 | |
| 393 | static ssize_t tlbflush_write_file(struct file *file, |
| 394 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 395 | { |
| 396 | char buf[32]; |
| 397 | ssize_t len; |
| 398 | int ceiling; |
| 399 | |
| 400 | len = min(count, sizeof(buf) - 1); |
| 401 | if (copy_from_user(buf, user_buf, len)) |
| 402 | return -EFAULT; |
| 403 | |
| 404 | buf[len] = '\0'; |
| 405 | if (kstrtoint(buf, 0, &ceiling)) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | if (ceiling < 0) |
| 409 | return -EINVAL; |
| 410 | |
| 411 | tlb_single_page_flush_ceiling = ceiling; |
| 412 | return count; |
| 413 | } |
| 414 | |
| 415 | static const struct file_operations fops_tlbflush = { |
| 416 | .read = tlbflush_read_file, |
| 417 | .write = tlbflush_write_file, |
| 418 | .llseek = default_llseek, |
| 419 | }; |
| 420 | |
| 421 | static int __init create_tlb_single_page_flush_ceiling(void) |
| 422 | { |
| 423 | debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR, |
| 424 | arch_debugfs_dir, NULL, &fops_tlbflush); |
| 425 | return 0; |
| 426 | } |
| 427 | late_initcall(create_tlb_single_page_flush_ceiling); |