1d5de8841SJeremy Fitzhardinge /* 2d5de8841SJeremy Fitzhardinge * Split spinlock implementation out into its own file, so it can be 3d5de8841SJeremy Fitzhardinge * compiled in a FTRACE-compatible way. 4d5de8841SJeremy Fitzhardinge */ 5d5de8841SJeremy Fitzhardinge #include <linux/kernel_stat.h> 6d5de8841SJeremy Fitzhardinge #include <linux/spinlock.h> 7994025caSJeremy Fitzhardinge #include <linux/debugfs.h> 8994025caSJeremy Fitzhardinge #include <linux/log2.h> 95a0e3ad6STejun Heo #include <linux/gfp.h> 10354e7b76SKonrad Rzeszutek Wilk #include <linux/slab.h> 11d5de8841SJeremy Fitzhardinge 12d5de8841SJeremy Fitzhardinge #include <asm/paravirt.h> 13d5de8841SJeremy Fitzhardinge 14d5de8841SJeremy Fitzhardinge #include <xen/interface/xen.h> 15d5de8841SJeremy Fitzhardinge #include <xen/events.h> 16d5de8841SJeremy Fitzhardinge 17d5de8841SJeremy Fitzhardinge #include "xen-ops.h" 18994025caSJeremy Fitzhardinge #include "debugfs.h" 19994025caSJeremy Fitzhardinge 2080bd58feSJeremy Fitzhardinge enum xen_contention_stat { 2180bd58feSJeremy Fitzhardinge TAKEN_SLOW, 2280bd58feSJeremy Fitzhardinge TAKEN_SLOW_PICKUP, 2380bd58feSJeremy Fitzhardinge TAKEN_SLOW_SPURIOUS, 2480bd58feSJeremy Fitzhardinge RELEASED_SLOW, 2580bd58feSJeremy Fitzhardinge RELEASED_SLOW_KICKED, 2680bd58feSJeremy Fitzhardinge NR_CONTENTION_STATS 2780bd58feSJeremy Fitzhardinge }; 2880bd58feSJeremy Fitzhardinge 2980bd58feSJeremy Fitzhardinge 30994025caSJeremy Fitzhardinge #ifdef CONFIG_XEN_DEBUG_FS 3180bd58feSJeremy Fitzhardinge #define HISTO_BUCKETS 30 32994025caSJeremy Fitzhardinge static struct xen_spinlock_stats 33994025caSJeremy Fitzhardinge { 3480bd58feSJeremy Fitzhardinge u32 contention_stats[NR_CONTENTION_STATS]; 35f8eca41fSJeremy Fitzhardinge u32 histo_spin_blocked[HISTO_BUCKETS+1]; 36f8eca41fSJeremy Fitzhardinge u64 time_blocked; 37994025caSJeremy Fitzhardinge } spinlock_stats; 38994025caSJeremy Fitzhardinge 39994025caSJeremy Fitzhardinge static u8 zero_stats; 40994025caSJeremy Fitzhardinge 41994025caSJeremy Fitzhardinge static inline void check_zero(void) 42994025caSJeremy Fitzhardinge { 4380bd58feSJeremy Fitzhardinge u8 ret; 4480bd58feSJeremy Fitzhardinge u8 old = ACCESS_ONCE(zero_stats); 4580bd58feSJeremy Fitzhardinge if (unlikely(old)) { 4680bd58feSJeremy Fitzhardinge ret = cmpxchg(&zero_stats, old, 0); 4780bd58feSJeremy Fitzhardinge /* This ensures only one fellow resets the stat */ 4880bd58feSJeremy Fitzhardinge if (ret == old) 49994025caSJeremy Fitzhardinge memset(&spinlock_stats, 0, sizeof(spinlock_stats)); 50994025caSJeremy Fitzhardinge } 51994025caSJeremy Fitzhardinge } 52994025caSJeremy Fitzhardinge 5380bd58feSJeremy Fitzhardinge static inline void add_stats(enum xen_contention_stat var, u32 val) 5480bd58feSJeremy Fitzhardinge { 5580bd58feSJeremy Fitzhardinge check_zero(); 5680bd58feSJeremy Fitzhardinge spinlock_stats.contention_stats[var] += val; 5780bd58feSJeremy Fitzhardinge } 58994025caSJeremy Fitzhardinge 59994025caSJeremy Fitzhardinge static inline u64 spin_time_start(void) 60994025caSJeremy Fitzhardinge { 61994025caSJeremy Fitzhardinge return xen_clocksource_read(); 62994025caSJeremy Fitzhardinge } 63994025caSJeremy Fitzhardinge 64994025caSJeremy Fitzhardinge static void __spin_time_accum(u64 delta, u32 *array) 65994025caSJeremy Fitzhardinge { 66994025caSJeremy Fitzhardinge unsigned index = ilog2(delta); 67994025caSJeremy Fitzhardinge 68994025caSJeremy Fitzhardinge check_zero(); 69994025caSJeremy Fitzhardinge 70994025caSJeremy Fitzhardinge if (index < HISTO_BUCKETS) 71994025caSJeremy Fitzhardinge array[index]++; 72994025caSJeremy Fitzhardinge else 73994025caSJeremy Fitzhardinge array[HISTO_BUCKETS]++; 74994025caSJeremy Fitzhardinge } 75994025caSJeremy Fitzhardinge 76f8eca41fSJeremy Fitzhardinge static inline void spin_time_accum_blocked(u64 start) 77f8eca41fSJeremy Fitzhardinge { 78f8eca41fSJeremy Fitzhardinge u32 delta = xen_clocksource_read() - start; 79f8eca41fSJeremy Fitzhardinge 80f8eca41fSJeremy Fitzhardinge __spin_time_accum(delta, spinlock_stats.histo_spin_blocked); 81f8eca41fSJeremy Fitzhardinge spinlock_stats.time_blocked += delta; 82994025caSJeremy Fitzhardinge } 83994025caSJeremy Fitzhardinge #else /* !CONFIG_XEN_DEBUG_FS */ 84994025caSJeremy Fitzhardinge #define TIMEOUT (1 << 10) 8580bd58feSJeremy Fitzhardinge static inline void add_stats(enum xen_contention_stat var, u32 val) 8680bd58feSJeremy Fitzhardinge { 8780bd58feSJeremy Fitzhardinge } 88994025caSJeremy Fitzhardinge 89994025caSJeremy Fitzhardinge static inline u64 spin_time_start(void) 90994025caSJeremy Fitzhardinge { 91994025caSJeremy Fitzhardinge return 0; 92994025caSJeremy Fitzhardinge } 93994025caSJeremy Fitzhardinge 94f8eca41fSJeremy Fitzhardinge static inline void spin_time_accum_blocked(u64 start) 95994025caSJeremy Fitzhardinge { 96994025caSJeremy Fitzhardinge } 97994025caSJeremy Fitzhardinge #endif /* CONFIG_XEN_DEBUG_FS */ 98d5de8841SJeremy Fitzhardinge 997a7546b3SDavid Vrabel /* 1007a7546b3SDavid Vrabel * Size struct xen_spinlock so it's the same as arch_spinlock_t. 1017a7546b3SDavid Vrabel */ 1027a7546b3SDavid Vrabel #if NR_CPUS < 256 1037a7546b3SDavid Vrabel typedef u8 xen_spinners_t; 1047a7546b3SDavid Vrabel # define inc_spinners(xl) \ 1057a7546b3SDavid Vrabel asm(LOCK_PREFIX " incb %0" : "+m" ((xl)->spinners) : : "memory"); 1067a7546b3SDavid Vrabel # define dec_spinners(xl) \ 1077a7546b3SDavid Vrabel asm(LOCK_PREFIX " decb %0" : "+m" ((xl)->spinners) : : "memory"); 1087a7546b3SDavid Vrabel #else 1097a7546b3SDavid Vrabel typedef u16 xen_spinners_t; 1107a7546b3SDavid Vrabel # define inc_spinners(xl) \ 1117a7546b3SDavid Vrabel asm(LOCK_PREFIX " incw %0" : "+m" ((xl)->spinners) : : "memory"); 1127a7546b3SDavid Vrabel # define dec_spinners(xl) \ 1137a7546b3SDavid Vrabel asm(LOCK_PREFIX " decw %0" : "+m" ((xl)->spinners) : : "memory"); 1147a7546b3SDavid Vrabel #endif 1157a7546b3SDavid Vrabel 11680bd58feSJeremy Fitzhardinge struct xen_lock_waiting { 11780bd58feSJeremy Fitzhardinge struct arch_spinlock *lock; 11880bd58feSJeremy Fitzhardinge __ticket_t want; 119d5de8841SJeremy Fitzhardinge }; 120d5de8841SJeremy Fitzhardinge 121545ac138SJeremy Fitzhardinge static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; 122354e7b76SKonrad Rzeszutek Wilk static DEFINE_PER_CPU(char *, irq_name); 12380bd58feSJeremy Fitzhardinge static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting); 12480bd58feSJeremy Fitzhardinge static cpumask_t waiting_cpus; 125d5de8841SJeremy Fitzhardinge 12680bd58feSJeremy Fitzhardinge static void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want) 127d5de8841SJeremy Fitzhardinge { 128780f36d8SChristoph Lameter int irq = __this_cpu_read(lock_kicker_irq); 12980bd58feSJeremy Fitzhardinge struct xen_lock_waiting *w = &__get_cpu_var(lock_waiting); 13080bd58feSJeremy Fitzhardinge int cpu = smp_processor_id(); 131f8eca41fSJeremy Fitzhardinge u64 start; 13280bd58feSJeremy Fitzhardinge unsigned long flags; 133d5de8841SJeremy Fitzhardinge 134d5de8841SJeremy Fitzhardinge /* If kicker interrupts not initialized yet, just spin */ 135d5de8841SJeremy Fitzhardinge if (irq == -1) 13680bd58feSJeremy Fitzhardinge return; 137d5de8841SJeremy Fitzhardinge 138f8eca41fSJeremy Fitzhardinge start = spin_time_start(); 139f8eca41fSJeremy Fitzhardinge 14080bd58feSJeremy Fitzhardinge /* 14180bd58feSJeremy Fitzhardinge * Make sure an interrupt handler can't upset things in a 14280bd58feSJeremy Fitzhardinge * partially setup state. 14380bd58feSJeremy Fitzhardinge */ 14480bd58feSJeremy Fitzhardinge local_irq_save(flags); 145d5de8841SJeremy Fitzhardinge 14680bd58feSJeremy Fitzhardinge w->want = want; 14780bd58feSJeremy Fitzhardinge smp_wmb(); 14880bd58feSJeremy Fitzhardinge w->lock = lock; 149994025caSJeremy Fitzhardinge 15080bd58feSJeremy Fitzhardinge /* This uses set_bit, which atomic and therefore a barrier */ 15180bd58feSJeremy Fitzhardinge cpumask_set_cpu(cpu, &waiting_cpus); 15280bd58feSJeremy Fitzhardinge add_stats(TAKEN_SLOW, 1); 1534d576b57SJeremy Fitzhardinge 154d5de8841SJeremy Fitzhardinge /* clear pending */ 155d5de8841SJeremy Fitzhardinge xen_clear_irq_pending(irq); 156d5de8841SJeremy Fitzhardinge 15780bd58feSJeremy Fitzhardinge /* Only check lock once pending cleared */ 15880bd58feSJeremy Fitzhardinge barrier(); 15980bd58feSJeremy Fitzhardinge 160d5de8841SJeremy Fitzhardinge /* check again make sure it didn't become free while 161d5de8841SJeremy Fitzhardinge we weren't looking */ 16280bd58feSJeremy Fitzhardinge if (ACCESS_ONCE(lock->tickets.head) == want) { 16380bd58feSJeremy Fitzhardinge add_stats(TAKEN_SLOW_PICKUP, 1); 164d5de8841SJeremy Fitzhardinge goto out; 165168d2f46SJeremy Fitzhardinge } 16680bd58feSJeremy Fitzhardinge /* Block until irq becomes pending (or perhaps a spurious wakeup) */ 167d5de8841SJeremy Fitzhardinge xen_poll_irq(irq); 16880bd58feSJeremy Fitzhardinge add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq)); 169d6c88a50SThomas Gleixner kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq)); 170d5de8841SJeremy Fitzhardinge out: 17180bd58feSJeremy Fitzhardinge cpumask_clear_cpu(cpu, &waiting_cpus); 17280bd58feSJeremy Fitzhardinge w->lock = NULL; 17380bd58feSJeremy Fitzhardinge local_irq_restore(flags); 174f8eca41fSJeremy Fitzhardinge spin_time_accum_blocked(start); 175d5de8841SJeremy Fitzhardinge } 176*354714ddSJeremy Fitzhardinge PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning); 177d5de8841SJeremy Fitzhardinge 17880bd58feSJeremy Fitzhardinge static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next) 179d5de8841SJeremy Fitzhardinge { 180d5de8841SJeremy Fitzhardinge int cpu; 181d5de8841SJeremy Fitzhardinge 18280bd58feSJeremy Fitzhardinge add_stats(RELEASED_SLOW, 1); 183994025caSJeremy Fitzhardinge 18480bd58feSJeremy Fitzhardinge for_each_cpu(cpu, &waiting_cpus) { 18580bd58feSJeremy Fitzhardinge const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu); 18680bd58feSJeremy Fitzhardinge 18780bd58feSJeremy Fitzhardinge if (w->lock == lock && w->want == next) { 18880bd58feSJeremy Fitzhardinge add_stats(RELEASED_SLOW_KICKED, 1); 189d5de8841SJeremy Fitzhardinge xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR); 19080bd58feSJeremy Fitzhardinge break; 191d5de8841SJeremy Fitzhardinge } 192d5de8841SJeremy Fitzhardinge } 193d5de8841SJeremy Fitzhardinge } 194d5de8841SJeremy Fitzhardinge 195d5de8841SJeremy Fitzhardinge static irqreturn_t dummy_handler(int irq, void *dev_id) 196d5de8841SJeremy Fitzhardinge { 197d5de8841SJeremy Fitzhardinge BUG(); 198d5de8841SJeremy Fitzhardinge return IRQ_HANDLED; 199d5de8841SJeremy Fitzhardinge } 200d5de8841SJeremy Fitzhardinge 201148f9bb8SPaul Gortmaker void xen_init_lock_cpu(int cpu) 202d5de8841SJeremy Fitzhardinge { 203d5de8841SJeremy Fitzhardinge int irq; 204354e7b76SKonrad Rzeszutek Wilk char *name; 205d5de8841SJeremy Fitzhardinge 206cb91f8f4SKonrad Rzeszutek Wilk WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n", 207cb9c6f15SKonrad Rzeszutek Wilk cpu, per_cpu(lock_kicker_irq, cpu)); 208cb9c6f15SKonrad Rzeszutek Wilk 20970dd4998SKonrad Rzeszutek Wilk /* 21070dd4998SKonrad Rzeszutek Wilk * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23 21170dd4998SKonrad Rzeszutek Wilk * (xen: disable PV spinlocks on HVM) 21270dd4998SKonrad Rzeszutek Wilk */ 21370dd4998SKonrad Rzeszutek Wilk if (xen_hvm_domain()) 21470dd4998SKonrad Rzeszutek Wilk return; 21570dd4998SKonrad Rzeszutek Wilk 216d5de8841SJeremy Fitzhardinge name = kasprintf(GFP_KERNEL, "spinlock%d", cpu); 217d5de8841SJeremy Fitzhardinge irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR, 218d5de8841SJeremy Fitzhardinge cpu, 219d5de8841SJeremy Fitzhardinge dummy_handler, 220d5de8841SJeremy Fitzhardinge IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING, 221d5de8841SJeremy Fitzhardinge name, 222d5de8841SJeremy Fitzhardinge NULL); 223d5de8841SJeremy Fitzhardinge 224d5de8841SJeremy Fitzhardinge if (irq >= 0) { 225d5de8841SJeremy Fitzhardinge disable_irq(irq); /* make sure it's never delivered */ 226d5de8841SJeremy Fitzhardinge per_cpu(lock_kicker_irq, cpu) = irq; 227354e7b76SKonrad Rzeszutek Wilk per_cpu(irq_name, cpu) = name; 228d5de8841SJeremy Fitzhardinge } 229d5de8841SJeremy Fitzhardinge 230d5de8841SJeremy Fitzhardinge printk("cpu %d spinlock event irq %d\n", cpu, irq); 231d5de8841SJeremy Fitzhardinge } 232d5de8841SJeremy Fitzhardinge 233d68d82afSAlex Nixon void xen_uninit_lock_cpu(int cpu) 234d68d82afSAlex Nixon { 23570dd4998SKonrad Rzeszutek Wilk /* 23670dd4998SKonrad Rzeszutek Wilk * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23 23770dd4998SKonrad Rzeszutek Wilk * (xen: disable PV spinlocks on HVM) 23870dd4998SKonrad Rzeszutek Wilk */ 23970dd4998SKonrad Rzeszutek Wilk if (xen_hvm_domain()) 24070dd4998SKonrad Rzeszutek Wilk return; 24170dd4998SKonrad Rzeszutek Wilk 242d68d82afSAlex Nixon unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL); 243cb9c6f15SKonrad Rzeszutek Wilk per_cpu(lock_kicker_irq, cpu) = -1; 244354e7b76SKonrad Rzeszutek Wilk kfree(per_cpu(irq_name, cpu)); 245354e7b76SKonrad Rzeszutek Wilk per_cpu(irq_name, cpu) = NULL; 246d68d82afSAlex Nixon } 247d68d82afSAlex Nixon 248b8fa70b5SJeremy Fitzhardinge static bool xen_pvspin __initdata = true; 249b8fa70b5SJeremy Fitzhardinge 250d5de8841SJeremy Fitzhardinge void __init xen_init_spinlocks(void) 251d5de8841SJeremy Fitzhardinge { 25270dd4998SKonrad Rzeszutek Wilk /* 25370dd4998SKonrad Rzeszutek Wilk * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23 25470dd4998SKonrad Rzeszutek Wilk * (xen: disable PV spinlocks on HVM) 25570dd4998SKonrad Rzeszutek Wilk */ 25670dd4998SKonrad Rzeszutek Wilk if (xen_hvm_domain()) 25770dd4998SKonrad Rzeszutek Wilk return; 25870dd4998SKonrad Rzeszutek Wilk 259b8fa70b5SJeremy Fitzhardinge if (!xen_pvspin) { 260b8fa70b5SJeremy Fitzhardinge printk(KERN_DEBUG "xen: PV spinlocks disabled\n"); 261b8fa70b5SJeremy Fitzhardinge return; 262b8fa70b5SJeremy Fitzhardinge } 263b8fa70b5SJeremy Fitzhardinge 264*354714ddSJeremy Fitzhardinge pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning); 26580bd58feSJeremy Fitzhardinge pv_lock_ops.unlock_kick = xen_unlock_kick; 266d5de8841SJeremy Fitzhardinge } 267994025caSJeremy Fitzhardinge 268b8fa70b5SJeremy Fitzhardinge static __init int xen_parse_nopvspin(char *arg) 269b8fa70b5SJeremy Fitzhardinge { 270b8fa70b5SJeremy Fitzhardinge xen_pvspin = false; 271b8fa70b5SJeremy Fitzhardinge return 0; 272b8fa70b5SJeremy Fitzhardinge } 273b8fa70b5SJeremy Fitzhardinge early_param("xen_nopvspin", xen_parse_nopvspin); 274b8fa70b5SJeremy Fitzhardinge 275994025caSJeremy Fitzhardinge #ifdef CONFIG_XEN_DEBUG_FS 276994025caSJeremy Fitzhardinge 277994025caSJeremy Fitzhardinge static struct dentry *d_spin_debug; 278994025caSJeremy Fitzhardinge 279994025caSJeremy Fitzhardinge static int __init xen_spinlock_debugfs(void) 280994025caSJeremy Fitzhardinge { 281994025caSJeremy Fitzhardinge struct dentry *d_xen = xen_init_debugfs(); 282994025caSJeremy Fitzhardinge 283994025caSJeremy Fitzhardinge if (d_xen == NULL) 284994025caSJeremy Fitzhardinge return -ENOMEM; 285994025caSJeremy Fitzhardinge 286994025caSJeremy Fitzhardinge d_spin_debug = debugfs_create_dir("spinlocks", d_xen); 287994025caSJeremy Fitzhardinge 288994025caSJeremy Fitzhardinge debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats); 289994025caSJeremy Fitzhardinge 290994025caSJeremy Fitzhardinge debugfs_create_u32("taken_slow", 0444, d_spin_debug, 29180bd58feSJeremy Fitzhardinge &spinlock_stats.contention_stats[TAKEN_SLOW]); 292994025caSJeremy Fitzhardinge debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug, 29380bd58feSJeremy Fitzhardinge &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]); 294994025caSJeremy Fitzhardinge debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug, 29580bd58feSJeremy Fitzhardinge &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]); 296994025caSJeremy Fitzhardinge 297994025caSJeremy Fitzhardinge debugfs_create_u32("released_slow", 0444, d_spin_debug, 29880bd58feSJeremy Fitzhardinge &spinlock_stats.contention_stats[RELEASED_SLOW]); 299994025caSJeremy Fitzhardinge debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug, 30080bd58feSJeremy Fitzhardinge &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]); 301994025caSJeremy Fitzhardinge 302f8eca41fSJeremy Fitzhardinge debugfs_create_u64("time_blocked", 0444, d_spin_debug, 303f8eca41fSJeremy Fitzhardinge &spinlock_stats.time_blocked); 304994025caSJeremy Fitzhardinge 3059fe2a701SSrivatsa Vaddagiri debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug, 306f8eca41fSJeremy Fitzhardinge spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1); 307994025caSJeremy Fitzhardinge 308994025caSJeremy Fitzhardinge return 0; 309994025caSJeremy Fitzhardinge } 310994025caSJeremy Fitzhardinge fs_initcall(xen_spinlock_debugfs); 311994025caSJeremy Fitzhardinge 312994025caSJeremy Fitzhardinge #endif /* CONFIG_XEN_DEBUG_FS */ 313