#include <linux/init.h>12#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>89#include <asm/tlbflush.h>10#include <asm/mmu_context.h>11#include <asm/cache.h>12#include <asm/apic.h>13#include <asm/uv/uv.h>1415DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate)16= { &init_mm, 0, };1718/*19* Smarter SMP flushing macros.20* c/o Linus Torvalds.21*22* These mean you can really definitely utterly forget about23* writing to user space from interrupts. (Its not allowed anyway).24*25* Optimizations Manfred Spraul <[email protected]>26*27* More scalable flush, from Andi Kleen28*29* To avoid global state use 8 different call vectors.30* Each CPU uses a specific vector to trigger flushes on other31* CPUs. Depending on the received vector the target CPUs look into32* the right array slot for the flush data.33*34* With more than 8 CPUs they are hashed to the 8 available35* vectors. The limited global vector space forces us to this right now.36* In future when interrupts are split into per CPU domains this could be37* fixed, at the cost of triggering multiple IPIs in some cases.38*/3940union smp_flush_state {41struct {42struct mm_struct *flush_mm;43unsigned long flush_va;44raw_spinlock_t tlbstate_lock;45DECLARE_BITMAP(flush_cpumask, NR_CPUS);46};47char pad[INTERNODE_CACHE_BYTES];48} ____cacheline_internodealigned_in_smp;4950/* State is put into the per CPU data section, but padded51to a full cache line because other CPUs can access it and we don't52want false sharing in the per cpu data segment. */53static union smp_flush_state flush_state[NUM_INVALIDATE_TLB_VECTORS];5455static DEFINE_PER_CPU_READ_MOSTLY(int, tlb_vector_offset);5657/*58* We cannot call mmdrop() because we are in interrupt context,59* instead update mm->cpu_vm_mask.60*/61void leave_mm(int cpu)62{63if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK)64BUG();65cpumask_clear_cpu(cpu,66mm_cpumask(percpu_read(cpu_tlbstate.active_mm)));67load_cr3(swapper_pg_dir);68}69EXPORT_SYMBOL_GPL(leave_mm);7071/*72*73* The flush IPI assumes that a thread switch happens in this order:74* [cpu0: the cpu that switches]75* 1) switch_mm() either 1a) or 1b)76* 1a) thread switch to a different mm77* 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);78* Stop ipi delivery for the old mm. This is not synchronized with79* the other cpus, but smp_invalidate_interrupt ignore flush ipis80* for the wrong mm, and in the worst case we perform a superfluous81* tlb flush.82* 1a2) set cpu mmu_state to TLBSTATE_OK83* Now the smp_invalidate_interrupt won't call leave_mm if cpu084* was in lazy tlb mode.85* 1a3) update cpu active_mm86* Now cpu0 accepts tlb flushes for the new mm.87* 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);88* Now the other cpus will send tlb flush ipis.89* 1a4) change cr3.90* 1b) thread switch without mm change91* cpu active_mm is correct, cpu0 already handles92* flush ipis.93* 1b1) set cpu mmu_state to TLBSTATE_OK94* 1b2) test_and_set the cpu bit in cpu_vm_mask.95* Atomically set the bit [other cpus will start sending flush ipis],96* and test the bit.97* 1b3) if the bit was 0: leave_mm was called, flush the tlb.98* 2) switch %%esp, ie current99*100* The interrupt must handle 2 special cases:101* - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.102* - the cpu performs speculative tlb reads, i.e. even if the cpu only103* runs in kernel space, the cpu could load tlb entries for user space104* pages.105*106* The good news is that cpu mmu_state is local to each cpu, no107* write/read ordering problems.108*/109110/*111* TLB flush IPI:112*113* 1) Flush the tlb entries if the cpu uses the mm that's being flushed.114* 2) Leave the mm if we are in the lazy tlb mode.115*116* Interrupts are disabled.117*/118119/*120* FIXME: use of asmlinkage is not consistent. On x86_64 it's noop121* but still used for documentation purpose but the usage is slightly122* inconsistent. On x86_32, asmlinkage is regparm(0) but interrupt123* entry calls in with the first parameter in %eax. Maybe define124* intrlinkage?125*/126#ifdef CONFIG_X86_64127asmlinkage128#endif129void smp_invalidate_interrupt(struct pt_regs *regs)130{131unsigned int cpu;132unsigned int sender;133union smp_flush_state *f;134135cpu = smp_processor_id();136/*137* orig_rax contains the negated interrupt vector.138* Use that to determine where the sender put the data.139*/140sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START;141f = &flush_state[sender];142143if (!cpumask_test_cpu(cpu, to_cpumask(f->flush_cpumask)))144goto out;145/*146* This was a BUG() but until someone can quote me the147* line from the intel manual that guarantees an IPI to148* multiple CPUs is retried _only_ on the erroring CPUs149* its staying as a return150*151* BUG();152*/153154if (f->flush_mm == percpu_read(cpu_tlbstate.active_mm)) {155if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {156if (f->flush_va == TLB_FLUSH_ALL)157local_flush_tlb();158else159__flush_tlb_one(f->flush_va);160} else161leave_mm(cpu);162}163out:164ack_APIC_irq();165smp_mb__before_clear_bit();166cpumask_clear_cpu(cpu, to_cpumask(f->flush_cpumask));167smp_mb__after_clear_bit();168inc_irq_stat(irq_tlb_count);169}170171static void flush_tlb_others_ipi(const struct cpumask *cpumask,172struct mm_struct *mm, unsigned long va)173{174unsigned int sender;175union smp_flush_state *f;176177/* Caller has disabled preemption */178sender = this_cpu_read(tlb_vector_offset);179f = &flush_state[sender];180181if (nr_cpu_ids > NUM_INVALIDATE_TLB_VECTORS)182raw_spin_lock(&f->tlbstate_lock);183184f->flush_mm = mm;185f->flush_va = va;186if (cpumask_andnot(to_cpumask(f->flush_cpumask), cpumask, cpumask_of(smp_processor_id()))) {187/*188* We have to send the IPI only to189* CPUs affected.190*/191apic->send_IPI_mask(to_cpumask(f->flush_cpumask),192INVALIDATE_TLB_VECTOR_START + sender);193194while (!cpumask_empty(to_cpumask(f->flush_cpumask)))195cpu_relax();196}197198f->flush_mm = NULL;199f->flush_va = 0;200if (nr_cpu_ids > NUM_INVALIDATE_TLB_VECTORS)201raw_spin_unlock(&f->tlbstate_lock);202}203204void native_flush_tlb_others(const struct cpumask *cpumask,205struct mm_struct *mm, unsigned long va)206{207if (is_uv_system()) {208unsigned int cpu;209210cpu = smp_processor_id();211cpumask = uv_flush_tlb_others(cpumask, mm, va, cpu);212if (cpumask)213flush_tlb_others_ipi(cpumask, mm, va);214return;215}216flush_tlb_others_ipi(cpumask, mm, va);217}218219static void __cpuinit calculate_tlb_offset(void)220{221int cpu, node, nr_node_vecs, idx = 0;222/*223* we are changing tlb_vector_offset for each CPU in runtime, but this224* will not cause inconsistency, as the write is atomic under X86. we225* might see more lock contentions in a short time, but after all CPU's226* tlb_vector_offset are changed, everything should go normal227*228* Note: if NUM_INVALIDATE_TLB_VECTORS % nr_online_nodes !=0, we might229* waste some vectors.230**/231if (nr_online_nodes > NUM_INVALIDATE_TLB_VECTORS)232nr_node_vecs = 1;233else234nr_node_vecs = NUM_INVALIDATE_TLB_VECTORS/nr_online_nodes;235236for_each_online_node(node) {237int node_offset = (idx % NUM_INVALIDATE_TLB_VECTORS) *238nr_node_vecs;239int cpu_offset = 0;240for_each_cpu(cpu, cpumask_of_node(node)) {241per_cpu(tlb_vector_offset, cpu) = node_offset +242cpu_offset;243cpu_offset++;244cpu_offset = cpu_offset % nr_node_vecs;245}246idx++;247}248}249250static int __cpuinit tlb_cpuhp_notify(struct notifier_block *n,251unsigned long action, void *hcpu)252{253switch (action & 0xf) {254case CPU_ONLINE:255case CPU_DEAD:256calculate_tlb_offset();257}258return NOTIFY_OK;259}260261static int __cpuinit init_smp_flush(void)262{263int i;264265for (i = 0; i < ARRAY_SIZE(flush_state); i++)266raw_spin_lock_init(&flush_state[i].tlbstate_lock);267268calculate_tlb_offset();269hotcpu_notifier(tlb_cpuhp_notify, 0);270return 0;271}272core_initcall(init_smp_flush);273274void flush_tlb_current_task(void)275{276struct mm_struct *mm = current->mm;277278preempt_disable();279280local_flush_tlb();281if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)282flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);283preempt_enable();284}285286void flush_tlb_mm(struct mm_struct *mm)287{288preempt_disable();289290if (current->active_mm == mm) {291if (current->mm)292local_flush_tlb();293else294leave_mm(smp_processor_id());295}296if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)297flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);298299preempt_enable();300}301302void flush_tlb_page(struct vm_area_struct *vma, unsigned long va)303{304struct mm_struct *mm = vma->vm_mm;305306preempt_disable();307308if (current->active_mm == mm) {309if (current->mm)310__flush_tlb_one(va);311else312leave_mm(smp_processor_id());313}314315if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)316flush_tlb_others(mm_cpumask(mm), mm, va);317318preempt_enable();319}320321static void do_flush_tlb_all(void *info)322{323__flush_tlb_all();324if (percpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)325leave_mm(smp_processor_id());326}327328void flush_tlb_all(void)329{330on_each_cpu(do_flush_tlb_all, NULL, 1);331}332333334