blob: 546112ed463f0419119e88316557f5fa438a5944 [file] [log] [blame]
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -07001/*
2 * Split spinlock implementation out into its own file, so it can be
3 * compiled in a FTRACE-compatible way.
4 */
5#include <linux/kernel_stat.h>
6#include <linux/spinlock.h>
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -07007#include <linux/debugfs.h>
8#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -040010#include <linux/slab.h>
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070011
12#include <asm/paravirt.h>
13
14#include <xen/interface/xen.h>
15#include <xen/events.h>
16
17#include "xen-ops.h"
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070018#include "debugfs.h"
19
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053020enum xen_contention_stat {
21 TAKEN_SLOW,
22 TAKEN_SLOW_PICKUP,
23 TAKEN_SLOW_SPURIOUS,
24 RELEASED_SLOW,
25 RELEASED_SLOW_KICKED,
26 NR_CONTENTION_STATS
27};
28
29
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070030#ifdef CONFIG_XEN_DEBUG_FS
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053031#define HISTO_BUCKETS 30
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070032static struct xen_spinlock_stats
33{
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053034 u32 contention_stats[NR_CONTENTION_STATS];
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070035 u32 histo_spin_blocked[HISTO_BUCKETS+1];
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070036 u64 time_blocked;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070037} spinlock_stats;
38
39static u8 zero_stats;
40
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070041static inline void check_zero(void)
42{
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053043 u8 ret;
44 u8 old = ACCESS_ONCE(zero_stats);
45 if (unlikely(old)) {
46 ret = cmpxchg(&zero_stats, old, 0);
47 /* This ensures only one fellow resets the stat */
48 if (ret == old)
49 memset(&spinlock_stats, 0, sizeof(spinlock_stats));
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070050 }
51}
52
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053053static inline void add_stats(enum xen_contention_stat var, u32 val)
54{
55 check_zero();
56 spinlock_stats.contention_stats[var] += val;
57}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070058
59static inline u64 spin_time_start(void)
60{
61 return xen_clocksource_read();
62}
63
64static void __spin_time_accum(u64 delta, u32 *array)
65{
66 unsigned index = ilog2(delta);
67
68 check_zero();
69
70 if (index < HISTO_BUCKETS)
71 array[index]++;
72 else
73 array[HISTO_BUCKETS]++;
74}
75
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070076static inline void spin_time_accum_blocked(u64 start)
77{
78 u32 delta = xen_clocksource_read() - start;
79
80 __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
81 spinlock_stats.time_blocked += delta;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070082}
83#else /* !CONFIG_XEN_DEBUG_FS */
84#define TIMEOUT (1 << 10)
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053085static inline void add_stats(enum xen_contention_stat var, u32 val)
86{
87}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070088
89static inline u64 spin_time_start(void)
90{
91 return 0;
92}
93
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070094static inline void spin_time_accum_blocked(u64 start)
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070095{
96}
97#endif /* CONFIG_XEN_DEBUG_FS */
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070098
David Vrabel7a7546b2012-01-23 19:32:25 +000099/*
100 * Size struct xen_spinlock so it's the same as arch_spinlock_t.
101 */
102#if NR_CPUS < 256
103typedef u8 xen_spinners_t;
104# define inc_spinners(xl) \
105 asm(LOCK_PREFIX " incb %0" : "+m" ((xl)->spinners) : : "memory");
106# define dec_spinners(xl) \
107 asm(LOCK_PREFIX " decb %0" : "+m" ((xl)->spinners) : : "memory");
108#else
109typedef u16 xen_spinners_t;
110# define inc_spinners(xl) \
111 asm(LOCK_PREFIX " incw %0" : "+m" ((xl)->spinners) : : "memory");
112# define dec_spinners(xl) \
113 asm(LOCK_PREFIX " decw %0" : "+m" ((xl)->spinners) : : "memory");
114#endif
115
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530116struct xen_lock_waiting {
117 struct arch_spinlock *lock;
118 __ticket_t want;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700119};
120
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530121static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400122static DEFINE_PER_CPU(char *, irq_name);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530123static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
124static cpumask_t waiting_cpus;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700125
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530126static void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700127{
Christoph Lameter780f36d2010-12-06 11:16:29 -0600128 int irq = __this_cpu_read(lock_kicker_irq);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530129 struct xen_lock_waiting *w = &__get_cpu_var(lock_waiting);
130 int cpu = smp_processor_id();
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700131 u64 start;
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530132 unsigned long flags;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700133
134 /* If kicker interrupts not initialized yet, just spin */
135 if (irq == -1)
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530136 return;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700137
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700138 start = spin_time_start();
139
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530140 /*
141 * Make sure an interrupt handler can't upset things in a
142 * partially setup state.
143 */
144 local_irq_save(flags);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700145
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530146 w->want = want;
147 smp_wmb();
148 w->lock = lock;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700149
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530150 /* This uses set_bit, which atomic and therefore a barrier */
151 cpumask_set_cpu(cpu, &waiting_cpus);
152 add_stats(TAKEN_SLOW, 1);
Jeremy Fitzhardinge4d576b52009-09-09 12:33:51 -0700153
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530154 /* clear pending */
155 xen_clear_irq_pending(irq);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700156
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530157 /* Only check lock once pending cleared */
158 barrier();
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700159
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530160 /* Mark entry to slowpath before doing the pickup test to make
161 sure we don't deadlock with an unlocker. */
162 __ticket_enter_slowpath(lock);
163
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530164 /* check again make sure it didn't become free while
165 we weren't looking */
166 if (ACCESS_ONCE(lock->tickets.head) == want) {
167 add_stats(TAKEN_SLOW_PICKUP, 1);
168 goto out;
169 }
170 /* Block until irq becomes pending (or perhaps a spurious wakeup) */
171 xen_poll_irq(irq);
172 add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200173 kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700174out:
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530175 cpumask_clear_cpu(cpu, &waiting_cpus);
176 w->lock = NULL;
177 local_irq_restore(flags);
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700178 spin_time_accum_blocked(start);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700179}
Jeremy Fitzhardinge354714d2013-08-09 19:51:55 +0530180PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700181
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530182static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700183{
184 int cpu;
185
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530186 add_stats(RELEASED_SLOW, 1);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700187
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530188 for_each_cpu(cpu, &waiting_cpus) {
189 const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
190
191 if (w->lock == lock && w->want == next) {
192 add_stats(RELEASED_SLOW_KICKED, 1);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700193 xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530194 break;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700195 }
196 }
197}
198
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700199static irqreturn_t dummy_handler(int irq, void *dev_id)
200{
201 BUG();
202 return IRQ_HANDLED;
203}
204
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400205void xen_init_lock_cpu(int cpu)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700206{
207 int irq;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400208 char *name;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700209
Konrad Rzeszutek Wilkcb91f8f2013-05-06 08:33:15 -0400210 WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
Konrad Rzeszutek Wilkcb9c6f12013-04-16 14:33:20 -0400211 cpu, per_cpu(lock_kicker_irq, cpu));
212
Konrad Rzeszutek Wilk70dd4992013-04-16 14:34:45 -0400213 /*
214 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
215 * (xen: disable PV spinlocks on HVM)
216 */
217 if (xen_hvm_domain())
218 return;
219
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700220 name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
221 irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
222 cpu,
223 dummy_handler,
224 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
225 name,
226 NULL);
227
228 if (irq >= 0) {
229 disable_irq(irq); /* make sure it's never delivered */
230 per_cpu(lock_kicker_irq, cpu) = irq;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400231 per_cpu(irq_name, cpu) = name;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700232 }
233
234 printk("cpu %d spinlock event irq %d\n", cpu, irq);
235}
236
Alex Nixond68d82a2008-08-22 11:52:15 +0100237void xen_uninit_lock_cpu(int cpu)
238{
Konrad Rzeszutek Wilk70dd4992013-04-16 14:34:45 -0400239 /*
240 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
241 * (xen: disable PV spinlocks on HVM)
242 */
243 if (xen_hvm_domain())
244 return;
245
Alex Nixond68d82a2008-08-22 11:52:15 +0100246 unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
Konrad Rzeszutek Wilkcb9c6f12013-04-16 14:33:20 -0400247 per_cpu(lock_kicker_irq, cpu) = -1;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400248 kfree(per_cpu(irq_name, cpu));
249 per_cpu(irq_name, cpu) = NULL;
Alex Nixond68d82a2008-08-22 11:52:15 +0100250}
251
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530252static bool xen_pvspin __initdata = true;
253
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700254void __init xen_init_spinlocks(void)
255{
Konrad Rzeszutek Wilk70dd4992013-04-16 14:34:45 -0400256 /*
257 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
258 * (xen: disable PV spinlocks on HVM)
259 */
260 if (xen_hvm_domain())
261 return;
262
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530263 if (!xen_pvspin) {
264 printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
265 return;
266 }
267
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530268 static_key_slow_inc(&paravirt_ticketlocks_enabled);
269
Jeremy Fitzhardinge354714d2013-08-09 19:51:55 +0530270 pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530271 pv_lock_ops.unlock_kick = xen_unlock_kick;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700272}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700273
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530274static __init int xen_parse_nopvspin(char *arg)
275{
276 xen_pvspin = false;
277 return 0;
278}
279early_param("xen_nopvspin", xen_parse_nopvspin);
280
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700281#ifdef CONFIG_XEN_DEBUG_FS
282
283static struct dentry *d_spin_debug;
284
285static int __init xen_spinlock_debugfs(void)
286{
287 struct dentry *d_xen = xen_init_debugfs();
288
289 if (d_xen == NULL)
290 return -ENOMEM;
291
292 d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
293
294 debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
295
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700296 debugfs_create_u32("taken_slow", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530297 &spinlock_stats.contention_stats[TAKEN_SLOW]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700298 debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530299 &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700300 debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530301 &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700302
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700303 debugfs_create_u32("released_slow", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530304 &spinlock_stats.contention_stats[RELEASED_SLOW]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700305 debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530306 &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700307
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700308 debugfs_create_u64("time_blocked", 0444, d_spin_debug,
309 &spinlock_stats.time_blocked);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700310
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530311 debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
312 spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700313
314 return 0;
315}
316fs_initcall(xen_spinlock_debugfs);
317
318#endif /* CONFIG_XEN_DEBUG_FS */