xen, pvticketlocks: Add xen_nopvspin parameter to disable xen pv ticketlocks
[linux-2.6-block.git] / arch / x86 / xen / spinlock.c
CommitLineData
d5de8841
JF
1/*
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>
994025ca
JF
7#include <linux/debugfs.h>
8#include <linux/log2.h>
5a0e3ad6 9#include <linux/gfp.h>
354e7b76 10#include <linux/slab.h>
d5de8841
JF
11
12#include <asm/paravirt.h>
13
14#include <xen/interface/xen.h>
15#include <xen/events.h>
16
17#include "xen-ops.h"
994025ca
JF
18#include "debugfs.h"
19
80bd58fe
JF
20enum 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};
994025ca 28
994025ca 29
80bd58fe 30#ifdef CONFIG_XEN_DEBUG_FS
f8eca41f 31#define HISTO_BUCKETS 30
80bd58fe
JF
32static struct xen_spinlock_stats
33{
34 u32 contention_stats[NR_CONTENTION_STATS];
f8eca41f 35 u32 histo_spin_blocked[HISTO_BUCKETS+1];
f8eca41f 36 u64 time_blocked;
994025ca
JF
37} spinlock_stats;
38
39static u8 zero_stats;
40
994025ca
JF
41static inline void check_zero(void)
42{
80bd58fe
JF
43 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));
994025ca
JF
50 }
51}
52
80bd58fe
JF
53static inline void add_stats(enum xen_contention_stat var, u32 val)
54{
55 check_zero();
56 spinlock_stats.contention_stats[var] += val;
57}
994025ca
JF
58
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
f8eca41f 76static inline void spin_time_accum_blocked(u64 start)
994025ca
JF
77{
78 u32 delta = xen_clocksource_read() - start;
79
f8eca41f
JF
80 __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
81 spinlock_stats.time_blocked += delta;
994025ca
JF
82}
83#else /* !CONFIG_XEN_DEBUG_FS */
84#define TIMEOUT (1 << 10)
80bd58fe
JF
85static inline void add_stats(enum xen_contention_stat var, u32 val)
86{
87}
994025ca
JF
88
89static inline u64 spin_time_start(void)
90{
91 return 0;
92}
93
f8eca41f 94static inline void spin_time_accum_blocked(u64 start)
994025ca
JF
95{
96}
97#endif /* CONFIG_XEN_DEBUG_FS */
d5de8841 98
7a7546b3
DV
99/*
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
80bd58fe
JF
116struct xen_lock_waiting {
117 struct arch_spinlock *lock;
118 __ticket_t want;
d5de8841
JF
119};
120
545ac138 121static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
354e7b76 122static DEFINE_PER_CPU(char *, irq_name);
80bd58fe
JF
123static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
124static cpumask_t waiting_cpus;
d5de8841 125
80bd58fe 126static void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
d5de8841 127{
780f36d8 128 int irq = __this_cpu_read(lock_kicker_irq);
80bd58fe
JF
129 struct xen_lock_waiting *w = &__get_cpu_var(lock_waiting);
130 int cpu = smp_processor_id();
f8eca41f 131 u64 start;
80bd58fe 132 unsigned long flags;
d5de8841
JF
133
134 /* If kicker interrupts not initialized yet, just spin */
135 if (irq == -1)
80bd58fe 136 return;
d5de8841 137
f8eca41f
JF
138 start = spin_time_start();
139
80bd58fe
JF
140 /*
141 * Make sure an interrupt handler can't upset things in a
142 * partially setup state.
143 */
144 local_irq_save(flags);
d5de8841 145
80bd58fe
JF
146 w->want = want;
147 smp_wmb();
148 w->lock = lock;
4d576b57 149
80bd58fe
JF
150 /* This uses set_bit, which atomic and therefore a barrier */
151 cpumask_set_cpu(cpu, &waiting_cpus);
152 add_stats(TAKEN_SLOW, 1);
4d576b57 153
80bd58fe
JF
154 /* clear pending */
155 xen_clear_irq_pending(irq);
4d576b57 156
80bd58fe
JF
157 /* Only check lock once pending cleared */
158 barrier();
d5de8841 159
80bd58fe
JF
160 /* check again make sure it didn't become free while
161 we weren't looking */
162 if (ACCESS_ONCE(lock->tickets.head) == want) {
163 add_stats(TAKEN_SLOW_PICKUP, 1);
164 goto out;
165 }
166 /* Block until irq becomes pending (or perhaps a spurious wakeup) */
167 xen_poll_irq(irq);
168 add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
d6c88a50 169 kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
d5de8841 170out:
80bd58fe
JF
171 cpumask_clear_cpu(cpu, &waiting_cpus);
172 w->lock = NULL;
173 local_irq_restore(flags);
f8eca41f 174 spin_time_accum_blocked(start);
d5de8841
JF
175}
176
80bd58fe 177static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
d5de8841
JF
178{
179 int cpu;
180
80bd58fe
JF
181 add_stats(RELEASED_SLOW, 1);
182
183 for_each_cpu(cpu, &waiting_cpus) {
184 const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
994025ca 185
80bd58fe
JF
186 if (w->lock == lock && w->want == next) {
187 add_stats(RELEASED_SLOW_KICKED, 1);
d5de8841 188 xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
80bd58fe 189 break;
d5de8841
JF
190 }
191 }
192}
193
d5de8841
JF
194static irqreturn_t dummy_handler(int irq, void *dev_id)
195{
196 BUG();
197 return IRQ_HANDLED;
198}
199
148f9bb8 200void xen_init_lock_cpu(int cpu)
d5de8841
JF
201{
202 int irq;
354e7b76 203 char *name;
d5de8841 204
cb91f8f4 205 WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
cb9c6f15
KRW
206 cpu, per_cpu(lock_kicker_irq, cpu));
207
70dd4998
KRW
208 /*
209 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
210 * (xen: disable PV spinlocks on HVM)
211 */
212 if (xen_hvm_domain())
213 return;
214
d5de8841
JF
215 name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
216 irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
217 cpu,
218 dummy_handler,
219 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
220 name,
221 NULL);
222
223 if (irq >= 0) {
224 disable_irq(irq); /* make sure it's never delivered */
225 per_cpu(lock_kicker_irq, cpu) = irq;
354e7b76 226 per_cpu(irq_name, cpu) = name;
d5de8841
JF
227 }
228
229 printk("cpu %d spinlock event irq %d\n", cpu, irq);
230}
231
d68d82af
AN
232void xen_uninit_lock_cpu(int cpu)
233{
70dd4998
KRW
234 /*
235 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
236 * (xen: disable PV spinlocks on HVM)
237 */
238 if (xen_hvm_domain())
239 return;
240
d68d82af 241 unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
cb9c6f15 242 per_cpu(lock_kicker_irq, cpu) = -1;
354e7b76
KRW
243 kfree(per_cpu(irq_name, cpu));
244 per_cpu(irq_name, cpu) = NULL;
d68d82af
AN
245}
246
b8fa70b5
JF
247static bool xen_pvspin __initdata = true;
248
d5de8841
JF
249void __init xen_init_spinlocks(void)
250{
70dd4998
KRW
251 /*
252 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
253 * (xen: disable PV spinlocks on HVM)
254 */
255 if (xen_hvm_domain())
256 return;
257
b8fa70b5
JF
258 if (!xen_pvspin) {
259 printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
260 return;
261 }
262
80bd58fe
JF
263 pv_lock_ops.lock_spinning = xen_lock_spinning;
264 pv_lock_ops.unlock_kick = xen_unlock_kick;
d5de8841 265}
994025ca 266
b8fa70b5
JF
267static __init int xen_parse_nopvspin(char *arg)
268{
269 xen_pvspin = false;
270 return 0;
271}
272early_param("xen_nopvspin", xen_parse_nopvspin);
273
994025ca
JF
274#ifdef CONFIG_XEN_DEBUG_FS
275
276static struct dentry *d_spin_debug;
277
278static int __init xen_spinlock_debugfs(void)
279{
280 struct dentry *d_xen = xen_init_debugfs();
281
282 if (d_xen == NULL)
283 return -ENOMEM;
284
285 d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
286
287 debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
288
994025ca 289 debugfs_create_u32("taken_slow", 0444, d_spin_debug,
80bd58fe 290 &spinlock_stats.contention_stats[TAKEN_SLOW]);
994025ca 291 debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
80bd58fe 292 &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
994025ca 293 debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
80bd58fe 294 &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
994025ca 295
994025ca 296 debugfs_create_u32("released_slow", 0444, d_spin_debug,
80bd58fe 297 &spinlock_stats.contention_stats[RELEASED_SLOW]);
994025ca 298 debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
80bd58fe 299 &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
994025ca 300
f8eca41f
JF
301 debugfs_create_u64("time_blocked", 0444, d_spin_debug,
302 &spinlock_stats.time_blocked);
994025ca 303
9fe2a701
SV
304 debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
305 spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
994025ca
JF
306
307 return 0;
308}
309fs_initcall(xen_spinlock_debugfs);
310
311#endif /* CONFIG_XEN_DEBUG_FS */