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> |
Tejun Heo | 6dd01be | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 7 | #include <linux/module.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 | |
Brian Gerst | 9eb912d | 2009-01-19 00:38:57 +0900 | [diff] [blame] | 17 | DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate) |
| 18 | = { &init_mm, 0, }; |
| 19 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 20 | /* |
| 21 | * Smarter SMP flushing macros. |
| 22 | * c/o Linus Torvalds. |
| 23 | * |
| 24 | * These mean you can really definitely utterly forget about |
| 25 | * writing to user space from interrupts. (Its not allowed anyway). |
| 26 | * |
| 27 | * Optimizations Manfred Spraul <manfred@colorfullife.com> |
| 28 | * |
| 29 | * More scalable flush, from Andi Kleen |
| 30 | * |
| 31 | * To avoid global state use 8 different call vectors. |
| 32 | * Each CPU uses a specific vector to trigger flushes on other |
| 33 | * CPUs. Depending on the received vector the target CPUs look into |
Frederik Deweerdt | 09b3ec7 | 2009-01-12 22:35:42 +0100 | [diff] [blame] | 34 | * the right array slot for the flush data. |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 35 | * |
| 36 | * With more than 8 CPUs they are hashed to the 8 available |
| 37 | * vectors. The limited global vector space forces us to this right now. |
| 38 | * In future when interrupts are split into per CPU domains this could be |
| 39 | * fixed, at the cost of triggering multiple IPIs in some cases. |
| 40 | */ |
| 41 | |
| 42 | union smp_flush_state { |
| 43 | struct { |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 44 | struct mm_struct *flush_mm; |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 45 | unsigned long flush_start; |
| 46 | unsigned long flush_end; |
Thomas Gleixner | 39c662f | 2009-07-25 19:15:48 +0200 | [diff] [blame] | 47 | raw_spinlock_t tlbstate_lock; |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 48 | DECLARE_BITMAP(flush_cpumask, NR_CPUS); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 49 | }; |
Jan Beulich | 350f8f5 | 2009-11-13 11:54:40 +0000 | [diff] [blame] | 50 | char pad[INTERNODE_CACHE_BYTES]; |
Frederik Deweerdt | 09b3ec7 | 2009-01-12 22:35:42 +0100 | [diff] [blame] | 51 | } ____cacheline_internodealigned_in_smp; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 52 | |
| 53 | /* State is put into the per CPU data section, but padded |
| 54 | to a full cache line because other CPUs can access it and we don't |
| 55 | want false sharing in the per cpu data segment. */ |
Frederik Deweerdt | 09b3ec7 | 2009-01-12 22:35:42 +0100 | [diff] [blame] | 56 | static union smp_flush_state flush_state[NUM_INVALIDATE_TLB_VECTORS]; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 57 | |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 58 | static DEFINE_PER_CPU_READ_MOSTLY(int, tlb_vector_offset); |
| 59 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 60 | /* |
| 61 | * We cannot call mmdrop() because we are in interrupt context, |
| 62 | * instead update mm->cpu_vm_mask. |
| 63 | */ |
| 64 | void leave_mm(int cpu) |
| 65 | { |
Linus Torvalds | 02171b4 | 2012-05-23 11:06:59 -0700 | [diff] [blame] | 66 | struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm); |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 67 | if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 68 | BUG(); |
Suresh Siddha | a6fca40 | 2012-03-22 17:01:25 -0700 | [diff] [blame] | 69 | if (cpumask_test_cpu(cpu, mm_cpumask(active_mm))) { |
| 70 | cpumask_clear_cpu(cpu, mm_cpumask(active_mm)); |
| 71 | load_cr3(swapper_pg_dir); |
| 72 | } |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL_GPL(leave_mm); |
| 75 | |
| 76 | /* |
| 77 | * |
| 78 | * The flush IPI assumes that a thread switch happens in this order: |
| 79 | * [cpu0: the cpu that switches] |
| 80 | * 1) switch_mm() either 1a) or 1b) |
| 81 | * 1a) thread switch to a different mm |
| 82 | * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask); |
| 83 | * Stop ipi delivery for the old mm. This is not synchronized with |
| 84 | * the other cpus, but smp_invalidate_interrupt ignore flush ipis |
| 85 | * for the wrong mm, and in the worst case we perform a superfluous |
| 86 | * tlb flush. |
| 87 | * 1a2) set cpu mmu_state to TLBSTATE_OK |
| 88 | * Now the smp_invalidate_interrupt won't call leave_mm if cpu0 |
| 89 | * was in lazy tlb mode. |
| 90 | * 1a3) update cpu active_mm |
| 91 | * Now cpu0 accepts tlb flushes for the new mm. |
| 92 | * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask); |
| 93 | * Now the other cpus will send tlb flush ipis. |
| 94 | * 1a4) change cr3. |
| 95 | * 1b) thread switch without mm change |
| 96 | * cpu active_mm is correct, cpu0 already handles |
| 97 | * flush ipis. |
| 98 | * 1b1) set cpu mmu_state to TLBSTATE_OK |
| 99 | * 1b2) test_and_set the cpu bit in cpu_vm_mask. |
| 100 | * Atomically set the bit [other cpus will start sending flush ipis], |
| 101 | * and test the bit. |
| 102 | * 1b3) if the bit was 0: leave_mm was called, flush the tlb. |
| 103 | * 2) switch %%esp, ie current |
| 104 | * |
| 105 | * The interrupt must handle 2 special cases: |
| 106 | * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm. |
| 107 | * - the cpu performs speculative tlb reads, i.e. even if the cpu only |
| 108 | * runs in kernel space, the cpu could load tlb entries for user space |
| 109 | * pages. |
| 110 | * |
| 111 | * The good news is that cpu mmu_state is local to each cpu, no |
| 112 | * write/read ordering problems. |
| 113 | */ |
| 114 | |
| 115 | /* |
| 116 | * TLB flush IPI: |
| 117 | * |
| 118 | * 1) Flush the tlb entries if the cpu uses the mm that's being flushed. |
| 119 | * 2) Leave the mm if we are in the lazy tlb mode. |
| 120 | * |
| 121 | * Interrupts are disabled. |
| 122 | */ |
| 123 | |
Tejun Heo | 02cf94c | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 124 | /* |
| 125 | * FIXME: use of asmlinkage is not consistent. On x86_64 it's noop |
| 126 | * but still used for documentation purpose but the usage is slightly |
| 127 | * inconsistent. On x86_32, asmlinkage is regparm(0) but interrupt |
| 128 | * entry calls in with the first parameter in %eax. Maybe define |
| 129 | * intrlinkage? |
| 130 | */ |
| 131 | #ifdef CONFIG_X86_64 |
| 132 | asmlinkage |
| 133 | #endif |
| 134 | void smp_invalidate_interrupt(struct pt_regs *regs) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 135 | { |
Tejun Heo | 6dd01be | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 136 | unsigned int cpu; |
| 137 | unsigned int sender; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 138 | union smp_flush_state *f; |
| 139 | |
| 140 | cpu = smp_processor_id(); |
| 141 | /* |
| 142 | * orig_rax contains the negated interrupt vector. |
| 143 | * Use that to determine where the sender put the data. |
| 144 | */ |
| 145 | sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START; |
Frederik Deweerdt | 09b3ec7 | 2009-01-12 22:35:42 +0100 | [diff] [blame] | 146 | f = &flush_state[sender]; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 147 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 148 | if (!cpumask_test_cpu(cpu, to_cpumask(f->flush_cpumask))) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 149 | goto out; |
| 150 | /* |
| 151 | * This was a BUG() but until someone can quote me the |
| 152 | * line from the intel manual that guarantees an IPI to |
| 153 | * multiple CPUs is retried _only_ on the erroring CPUs |
| 154 | * its staying as a return |
| 155 | * |
| 156 | * BUG(); |
| 157 | */ |
| 158 | |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 159 | if (f->flush_mm == this_cpu_read(cpu_tlbstate.active_mm)) { |
| 160 | if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) { |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 161 | if (f->flush_end == TLB_FLUSH_ALL |
| 162 | || !cpu_has_invlpg) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 163 | local_flush_tlb(); |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 164 | else if (!f->flush_end) |
| 165 | __flush_tlb_single(f->flush_start); |
| 166 | else { |
| 167 | unsigned long addr; |
| 168 | addr = f->flush_start; |
| 169 | while (addr < f->flush_end) { |
| 170 | __flush_tlb_single(addr); |
| 171 | addr += PAGE_SIZE; |
| 172 | } |
| 173 | } |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 174 | } else |
| 175 | leave_mm(cpu); |
| 176 | } |
| 177 | out: |
| 178 | ack_APIC_irq(); |
Tejun Heo | 6dd01be | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 179 | smp_mb__before_clear_bit(); |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 180 | cpumask_clear_cpu(cpu, to_cpumask(f->flush_cpumask)); |
Tejun Heo | 6dd01be | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 181 | smp_mb__after_clear_bit(); |
Hiroshi Shimamoto | 8ae9366 | 2008-12-12 15:52:26 -0800 | [diff] [blame] | 182 | inc_irq_stat(irq_tlb_count); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 183 | } |
| 184 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 185 | static void flush_tlb_others_ipi(const struct cpumask *cpumask, |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 186 | struct mm_struct *mm, unsigned long start, |
| 187 | unsigned long end) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 188 | { |
Tejun Heo | 6dd01be | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 189 | unsigned int sender; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 190 | union smp_flush_state *f; |
Cliff Wickman | 1812924 | 2008-06-02 08:56:14 -0500 | [diff] [blame] | 191 | |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 192 | /* Caller has disabled preemption */ |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 193 | sender = this_cpu_read(tlb_vector_offset); |
Frederik Deweerdt | 09b3ec7 | 2009-01-12 22:35:42 +0100 | [diff] [blame] | 194 | f = &flush_state[sender]; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 195 | |
Shaohua Li | 7064d86 | 2011-01-17 10:52:10 +0800 | [diff] [blame] | 196 | if (nr_cpu_ids > NUM_INVALIDATE_TLB_VECTORS) |
| 197 | raw_spin_lock(&f->tlbstate_lock); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 198 | |
| 199 | f->flush_mm = mm; |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 200 | f->flush_start = start; |
| 201 | f->flush_end = end; |
Linus Torvalds | b04e637 | 2009-08-21 09:48:10 -0700 | [diff] [blame] | 202 | if (cpumask_andnot(to_cpumask(f->flush_cpumask), cpumask, cpumask_of(smp_processor_id()))) { |
| 203 | /* |
| 204 | * We have to send the IPI only to |
| 205 | * CPUs affected. |
| 206 | */ |
| 207 | apic->send_IPI_mask(to_cpumask(f->flush_cpumask), |
| 208 | INVALIDATE_TLB_VECTOR_START + sender); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 209 | |
Linus Torvalds | b04e637 | 2009-08-21 09:48:10 -0700 | [diff] [blame] | 210 | while (!cpumask_empty(to_cpumask(f->flush_cpumask))) |
| 211 | cpu_relax(); |
| 212 | } |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 213 | |
| 214 | f->flush_mm = NULL; |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 215 | f->flush_start = 0; |
| 216 | f->flush_end = 0; |
Shaohua Li | 7064d86 | 2011-01-17 10:52:10 +0800 | [diff] [blame] | 217 | if (nr_cpu_ids > NUM_INVALIDATE_TLB_VECTORS) |
| 218 | raw_spin_unlock(&f->tlbstate_lock); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 219 | } |
| 220 | |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 221 | void native_flush_tlb_others(const struct cpumask *cpumask, |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 222 | struct mm_struct *mm, unsigned long start, |
| 223 | unsigned long end) |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 224 | { |
| 225 | if (is_uv_system()) { |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 226 | unsigned int cpu; |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 227 | |
Xiao Guangrong | 25542c6 | 2011-03-15 09:57:37 +0800 | [diff] [blame] | 228 | cpu = smp_processor_id(); |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 229 | cpumask = uv_flush_tlb_others(cpumask, mm, start, end, cpu); |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 230 | if (cpumask) |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 231 | flush_tlb_others_ipi(cpumask, mm, start, end); |
Mike Travis | 0e21990 | 2009-01-10 21:58:10 -0800 | [diff] [blame] | 232 | return; |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 233 | } |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 234 | flush_tlb_others_ipi(cpumask, mm, start, end); |
Rusty Russell | 4595f96 | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 237 | static void __cpuinit calculate_tlb_offset(void) |
| 238 | { |
Yinghai Lu | 9223081 | 2010-11-13 10:52:09 -0800 | [diff] [blame] | 239 | int cpu, node, nr_node_vecs, idx = 0; |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 240 | /* |
| 241 | * we are changing tlb_vector_offset for each CPU in runtime, but this |
| 242 | * will not cause inconsistency, as the write is atomic under X86. we |
| 243 | * might see more lock contentions in a short time, but after all CPU's |
| 244 | * tlb_vector_offset are changed, everything should go normal |
| 245 | * |
| 246 | * Note: if NUM_INVALIDATE_TLB_VECTORS % nr_online_nodes !=0, we might |
| 247 | * waste some vectors. |
| 248 | **/ |
| 249 | if (nr_online_nodes > NUM_INVALIDATE_TLB_VECTORS) |
| 250 | nr_node_vecs = 1; |
| 251 | else |
| 252 | nr_node_vecs = NUM_INVALIDATE_TLB_VECTORS/nr_online_nodes; |
| 253 | |
| 254 | for_each_online_node(node) { |
Yinghai Lu | 9223081 | 2010-11-13 10:52:09 -0800 | [diff] [blame] | 255 | int node_offset = (idx % NUM_INVALIDATE_TLB_VECTORS) * |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 256 | nr_node_vecs; |
| 257 | int cpu_offset = 0; |
| 258 | for_each_cpu(cpu, cpumask_of_node(node)) { |
| 259 | per_cpu(tlb_vector_offset, cpu) = node_offset + |
| 260 | cpu_offset; |
| 261 | cpu_offset++; |
| 262 | cpu_offset = cpu_offset % nr_node_vecs; |
| 263 | } |
Yinghai Lu | 9223081 | 2010-11-13 10:52:09 -0800 | [diff] [blame] | 264 | idx++; |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Rakib Mullick | cf38d0b | 2010-11-01 12:53:50 +0600 | [diff] [blame] | 268 | static int __cpuinit tlb_cpuhp_notify(struct notifier_block *n, |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 269 | unsigned long action, void *hcpu) |
| 270 | { |
| 271 | switch (action & 0xf) { |
| 272 | case CPU_ONLINE: |
| 273 | case CPU_DEAD: |
| 274 | calculate_tlb_offset(); |
| 275 | } |
| 276 | return NOTIFY_OK; |
| 277 | } |
| 278 | |
Ingo Molnar | a4928cf | 2008-04-23 13:20:56 +0200 | [diff] [blame] | 279 | static int __cpuinit init_smp_flush(void) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 280 | { |
| 281 | int i; |
| 282 | |
Frederik Deweerdt | 09b3ec7 | 2009-01-12 22:35:42 +0100 | [diff] [blame] | 283 | for (i = 0; i < ARRAY_SIZE(flush_state); i++) |
Thomas Gleixner | 39c662f | 2009-07-25 19:15:48 +0200 | [diff] [blame] | 284 | raw_spin_lock_init(&flush_state[i].tlbstate_lock); |
Akinobu Mita | 7c04e64 | 2008-04-19 23:55:17 +0900 | [diff] [blame] | 285 | |
Shaohua Li | 9329672 | 2010-10-20 11:07:03 +0800 | [diff] [blame] | 286 | calculate_tlb_offset(); |
| 287 | hotcpu_notifier(tlb_cpuhp_notify, 0); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | core_initcall(init_smp_flush); |
| 291 | |
| 292 | void flush_tlb_current_task(void) |
| 293 | { |
| 294 | struct mm_struct *mm = current->mm; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 295 | |
| 296 | preempt_disable(); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 297 | |
| 298 | local_flush_tlb(); |
Rusty Russell | 78f1c4d | 2009-09-24 09:34:51 -0600 | [diff] [blame] | 299 | if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 300 | flush_tlb_others(mm_cpumask(mm), mm, 0UL, TLB_FLUSH_ALL); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 301 | preempt_enable(); |
| 302 | } |
| 303 | |
| 304 | void flush_tlb_mm(struct mm_struct *mm) |
| 305 | { |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 306 | preempt_disable(); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 307 | |
| 308 | if (current->active_mm == mm) { |
| 309 | if (current->mm) |
| 310 | local_flush_tlb(); |
| 311 | else |
| 312 | leave_mm(smp_processor_id()); |
| 313 | } |
Rusty Russell | 78f1c4d | 2009-09-24 09:34:51 -0600 | [diff] [blame] | 314 | if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 315 | flush_tlb_others(mm_cpumask(mm), mm, 0UL, TLB_FLUSH_ALL); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 316 | |
| 317 | preempt_enable(); |
| 318 | } |
| 319 | |
Alex Shi | d8dfe60 | 2012-06-28 09:02:18 +0800 | [diff] [blame] | 320 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 321 | static inline unsigned long has_large_page(struct mm_struct *mm, |
| 322 | unsigned long start, unsigned long end) |
| 323 | { |
| 324 | pgd_t *pgd; |
| 325 | pud_t *pud; |
| 326 | pmd_t *pmd; |
| 327 | unsigned long addr = ALIGN(start, HPAGE_SIZE); |
| 328 | for (; addr < end; addr += HPAGE_SIZE) { |
| 329 | pgd = pgd_offset(mm, addr); |
| 330 | if (likely(!pgd_none(*pgd))) { |
| 331 | pud = pud_offset(pgd, addr); |
| 332 | if (likely(!pud_none(*pud))) { |
| 333 | pmd = pmd_offset(pud, addr); |
| 334 | if (likely(!pmd_none(*pmd))) |
| 335 | if (pmd_large(*pmd)) |
| 336 | return addr; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | #else |
| 343 | static inline unsigned long has_large_page(struct mm_struct *mm, |
| 344 | unsigned long start, unsigned long end) |
| 345 | { |
| 346 | return 0; |
| 347 | } |
| 348 | #endif |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 349 | void flush_tlb_range(struct vm_area_struct *vma, |
| 350 | unsigned long start, unsigned long end) |
| 351 | { |
| 352 | struct mm_struct *mm; |
| 353 | |
Alex Shi | c4211f4 | 2012-06-28 09:02:19 +0800 | [diff] [blame] | 354 | if (vma->vm_flags & VM_HUGETLB || tlb_flushall_shift == -1) { |
Alex Shi | d8dfe60 | 2012-06-28 09:02:18 +0800 | [diff] [blame] | 355 | flush_all: |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 356 | flush_tlb_mm(vma->vm_mm); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | preempt_disable(); |
| 361 | mm = vma->vm_mm; |
| 362 | if (current->active_mm == mm) { |
| 363 | if (current->mm) { |
| 364 | unsigned long addr, vmflag = vma->vm_flags; |
| 365 | unsigned act_entries, tlb_entries = 0; |
| 366 | |
| 367 | if (vmflag & VM_EXEC) |
| 368 | tlb_entries = tlb_lli_4k[ENTRIES]; |
| 369 | else |
| 370 | tlb_entries = tlb_lld_4k[ENTRIES]; |
| 371 | |
| 372 | act_entries = tlb_entries > mm->total_vm ? |
| 373 | mm->total_vm : tlb_entries; |
| 374 | |
Alex Shi | c4211f4 | 2012-06-28 09:02:19 +0800 | [diff] [blame] | 375 | if ((end - start) >> PAGE_SHIFT > |
| 376 | act_entries >> tlb_flushall_shift) |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 377 | local_flush_tlb(); |
| 378 | else { |
Alex Shi | d8dfe60 | 2012-06-28 09:02:18 +0800 | [diff] [blame] | 379 | if (has_large_page(mm, start, end)) { |
| 380 | preempt_enable(); |
| 381 | goto flush_all; |
| 382 | } |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 383 | for (addr = start; addr < end; |
| 384 | addr += PAGE_SIZE) |
| 385 | __flush_tlb_single(addr); |
| 386 | |
| 387 | if (cpumask_any_but(mm_cpumask(mm), |
| 388 | smp_processor_id()) < nr_cpu_ids) |
| 389 | flush_tlb_others(mm_cpumask(mm), mm, |
| 390 | start, end); |
| 391 | preempt_enable(); |
| 392 | return; |
| 393 | } |
| 394 | } else { |
| 395 | leave_mm(smp_processor_id()); |
| 396 | } |
| 397 | } |
| 398 | if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) |
| 399 | flush_tlb_others(mm_cpumask(mm), mm, 0UL, TLB_FLUSH_ALL); |
| 400 | preempt_enable(); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | void flush_tlb_page(struct vm_area_struct *vma, unsigned long start) |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 405 | { |
| 406 | struct mm_struct *mm = vma->vm_mm; |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 407 | |
| 408 | preempt_disable(); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 409 | |
| 410 | if (current->active_mm == mm) { |
| 411 | if (current->mm) |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 412 | __flush_tlb_one(start); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 413 | else |
| 414 | leave_mm(smp_processor_id()); |
| 415 | } |
| 416 | |
Rusty Russell | 78f1c4d | 2009-09-24 09:34:51 -0600 | [diff] [blame] | 417 | if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) |
Alex Shi | e7b52ff | 2012-06-28 09:02:17 +0800 | [diff] [blame] | 418 | flush_tlb_others(mm_cpumask(mm), mm, start, 0UL); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 419 | |
| 420 | preempt_enable(); |
| 421 | } |
| 422 | |
| 423 | static void do_flush_tlb_all(void *info) |
| 424 | { |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 425 | __flush_tlb_all(); |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 426 | if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY) |
Borislav Petkov | 3f8afb7 | 2010-07-21 14:47:05 +0200 | [diff] [blame] | 427 | leave_mm(smp_processor_id()); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void flush_tlb_all(void) |
| 431 | { |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 432 | on_each_cpu(do_flush_tlb_all, NULL, 1); |
Glauber Costa | c048fdf | 2008-03-03 14:12:54 -0300 | [diff] [blame] | 433 | } |
Alex Shi | 3df3212 | 2012-06-28 09:02:20 +0800 | [diff] [blame] | 434 | |
| 435 | #ifdef CONFIG_DEBUG_TLBFLUSH |
| 436 | static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf, |
| 437 | size_t count, loff_t *ppos) |
| 438 | { |
| 439 | char buf[32]; |
| 440 | unsigned int len; |
| 441 | |
| 442 | len = sprintf(buf, "%hd\n", tlb_flushall_shift); |
| 443 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 444 | } |
| 445 | |
| 446 | static ssize_t tlbflush_write_file(struct file *file, |
| 447 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 448 | { |
| 449 | char buf[32]; |
| 450 | ssize_t len; |
| 451 | s8 shift; |
| 452 | |
| 453 | len = min(count, sizeof(buf) - 1); |
| 454 | if (copy_from_user(buf, user_buf, len)) |
| 455 | return -EFAULT; |
| 456 | |
| 457 | buf[len] = '\0'; |
| 458 | if (kstrtos8(buf, 0, &shift)) |
| 459 | return -EINVAL; |
| 460 | |
| 461 | if (shift > 64) |
| 462 | return -EINVAL; |
| 463 | |
| 464 | tlb_flushall_shift = shift; |
| 465 | return count; |
| 466 | } |
| 467 | |
| 468 | static const struct file_operations fops_tlbflush = { |
| 469 | .read = tlbflush_read_file, |
| 470 | .write = tlbflush_write_file, |
| 471 | .llseek = default_llseek, |
| 472 | }; |
| 473 | |
| 474 | static int __cpuinit create_tlb_flushall_shift(void) |
| 475 | { |
| 476 | if (cpu_has_invlpg) { |
| 477 | debugfs_create_file("tlb_flushall_shift", S_IRUSR | S_IWUSR, |
| 478 | arch_debugfs_dir, NULL, &fops_tlbflush); |
| 479 | } |
| 480 | return 0; |
| 481 | } |
| 482 | late_initcall(create_tlb_flushall_shift); |
| 483 | #endif |