blob: 24ded31b5aeceeabfadc05fc5c5ff7aa95fa2b97 [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>
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -07009
10#include <asm/paravirt.h>
11
12#include <xen/interface/xen.h>
13#include <xen/events.h>
14
15#include "xen-ops.h"
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070016#include "debugfs.h"
17
18#ifdef CONFIG_XEN_DEBUG_FS
19static struct xen_spinlock_stats
20{
21 u64 taken;
22 u32 taken_slow;
23 u32 taken_slow_nested;
24 u32 taken_slow_pickup;
25 u32 taken_slow_spurious;
Jeremy Fitzhardinge1e696f62008-08-20 17:02:20 -070026 u32 taken_slow_irqenable;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070027
28 u64 released;
29 u32 released_slow;
30 u32 released_slow_kicked;
31
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070032#define HISTO_BUCKETS 30
33 u32 histo_spin_total[HISTO_BUCKETS+1];
34 u32 histo_spin_spinning[HISTO_BUCKETS+1];
35 u32 histo_spin_blocked[HISTO_BUCKETS+1];
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070036
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070037 u64 time_total;
38 u64 time_spinning;
39 u64 time_blocked;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070040} spinlock_stats;
41
42static u8 zero_stats;
43
44static unsigned lock_timeout = 1 << 10;
45#define TIMEOUT lock_timeout
46
47static inline void check_zero(void)
48{
49 if (unlikely(zero_stats)) {
50 memset(&spinlock_stats, 0, sizeof(spinlock_stats));
51 zero_stats = 0;
52 }
53}
54
55#define ADD_STATS(elem, val) \
56 do { check_zero(); spinlock_stats.elem += (val); } while(0)
57
58static inline u64 spin_time_start(void)
59{
60 return xen_clocksource_read();
61}
62
63static void __spin_time_accum(u64 delta, u32 *array)
64{
65 unsigned index = ilog2(delta);
66
67 check_zero();
68
69 if (index < HISTO_BUCKETS)
70 array[index]++;
71 else
72 array[HISTO_BUCKETS]++;
73}
74
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070075static inline void spin_time_accum_spinning(u64 start)
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070076{
77 u32 delta = xen_clocksource_read() - start;
78
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070079 __spin_time_accum(delta, spinlock_stats.histo_spin_spinning);
80 spinlock_stats.time_spinning += delta;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070081}
82
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070083static inline void spin_time_accum_total(u64 start)
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070084{
85 u32 delta = xen_clocksource_read() - start;
86
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070087 __spin_time_accum(delta, spinlock_stats.histo_spin_total);
88 spinlock_stats.time_total += delta;
89}
90
91static inline void spin_time_accum_blocked(u64 start)
92{
93 u32 delta = xen_clocksource_read() - start;
94
95 __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
96 spinlock_stats.time_blocked += delta;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070097}
98#else /* !CONFIG_XEN_DEBUG_FS */
99#define TIMEOUT (1 << 10)
100#define ADD_STATS(elem, val) do { (void)(val); } while(0)
101
102static inline u64 spin_time_start(void)
103{
104 return 0;
105}
106
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700107static inline void spin_time_accum_total(u64 start)
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700108{
109}
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700110static inline void spin_time_accum_spinning(u64 start)
111{
112}
113static inline void spin_time_accum_blocked(u64 start)
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700114{
115}
116#endif /* CONFIG_XEN_DEBUG_FS */
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700117
118struct xen_spinlock {
119 unsigned char lock; /* 0 -> free; 1 -> locked */
120 unsigned short spinners; /* count of waiting cpus */
121};
122
Thomas Gleixner445c8952009-12-02 19:49:50 +0100123static int xen_spin_is_locked(struct arch_spinlock *lock)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700124{
125 struct xen_spinlock *xl = (struct xen_spinlock *)lock;
126
127 return xl->lock != 0;
128}
129
Thomas Gleixner445c8952009-12-02 19:49:50 +0100130static int xen_spin_is_contended(struct arch_spinlock *lock)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700131{
132 struct xen_spinlock *xl = (struct xen_spinlock *)lock;
133
134 /* Not strictly true; this is only the count of contended
135 lock-takers entering the slow path. */
136 return xl->spinners != 0;
137}
138
Thomas Gleixner445c8952009-12-02 19:49:50 +0100139static int xen_spin_trylock(struct arch_spinlock *lock)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700140{
141 struct xen_spinlock *xl = (struct xen_spinlock *)lock;
142 u8 old = 1;
143
144 asm("xchgb %b0,%1"
145 : "+q" (old), "+m" (xl->lock) : : "memory");
146
147 return old == 0;
148}
149
150static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
151static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners);
152
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700153/*
154 * Mark a cpu as interested in a lock. Returns the CPU's previous
155 * lock of interest, in case we got preempted by an interrupt.
156 */
157static inline struct xen_spinlock *spinning_lock(struct xen_spinlock *xl)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700158{
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700159 struct xen_spinlock *prev;
160
161 prev = __get_cpu_var(lock_spinners);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700162 __get_cpu_var(lock_spinners) = xl;
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700163
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700164 wmb(); /* set lock of interest before count */
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700165
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700166 asm(LOCK_PREFIX " incw %0"
167 : "+m" (xl->spinners) : : "memory");
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700168
169 return prev;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700170}
171
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700172/*
173 * Mark a cpu as no longer interested in a lock. Restores previous
174 * lock of interest (NULL for none).
175 */
176static inline void unspinning_lock(struct xen_spinlock *xl, struct xen_spinlock *prev)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700177{
178 asm(LOCK_PREFIX " decw %0"
179 : "+m" (xl->spinners) : : "memory");
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700180 wmb(); /* decrement count before restoring lock */
181 __get_cpu_var(lock_spinners) = prev;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700182}
183
Thomas Gleixner445c8952009-12-02 19:49:50 +0100184static noinline int xen_spin_lock_slow(struct arch_spinlock *lock, bool irq_enable)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700185{
186 struct xen_spinlock *xl = (struct xen_spinlock *)lock;
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700187 struct xen_spinlock *prev;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700188 int irq = __get_cpu_var(lock_kicker_irq);
189 int ret;
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700190 u64 start;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700191
192 /* If kicker interrupts not initialized yet, just spin */
193 if (irq == -1)
194 return 0;
195
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700196 start = spin_time_start();
197
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700198 /* announce we're spinning */
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700199 prev = spinning_lock(xl);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700200
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700201 ADD_STATS(taken_slow, 1);
202 ADD_STATS(taken_slow_nested, prev != NULL);
203
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700204 do {
Jeremy Fitzhardinge4d576b52009-09-09 12:33:51 -0700205 unsigned long flags;
206
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700207 /* clear pending */
208 xen_clear_irq_pending(irq);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700209
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700210 /* check again make sure it didn't become free while
211 we weren't looking */
212 ret = xen_spin_trylock(lock);
213 if (ret) {
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700214 ADD_STATS(taken_slow_pickup, 1);
215
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700216 /*
217 * If we interrupted another spinlock while it
218 * was blocking, make sure it doesn't block
219 * without rechecking the lock.
220 */
221 if (prev != NULL)
222 xen_set_irq_pending(irq);
223 goto out;
224 }
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700225
Jeremy Fitzhardinge4d576b52009-09-09 12:33:51 -0700226 flags = __raw_local_save_flags();
227 if (irq_enable) {
228 ADD_STATS(taken_slow_irqenable, 1);
229 raw_local_irq_enable();
230 }
231
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700232 /*
233 * Block until irq becomes pending. If we're
234 * interrupted at this point (after the trylock but
235 * before entering the block), then the nested lock
236 * handler guarantees that the irq will be left
237 * pending if there's any chance the lock became free;
238 * xen_poll_irq() returns immediately if the irq is
239 * pending.
240 */
241 xen_poll_irq(irq);
Jeremy Fitzhardinge4d576b52009-09-09 12:33:51 -0700242
243 raw_local_irq_restore(flags);
244
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700245 ADD_STATS(taken_slow_spurious, !xen_test_irq_pending(irq));
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700246 } while (!xen_test_irq_pending(irq)); /* check for spurious wakeups */
247
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200248 kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700249
250out:
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700251 unspinning_lock(xl, prev);
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700252 spin_time_accum_blocked(start);
253
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700254 return ret;
255}
256
Thomas Gleixner445c8952009-12-02 19:49:50 +0100257static inline void __xen_spin_lock(struct arch_spinlock *lock, bool irq_enable)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700258{
259 struct xen_spinlock *xl = (struct xen_spinlock *)lock;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700260 unsigned timeout;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700261 u8 oldval;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700262 u64 start_spin;
263
264 ADD_STATS(taken, 1);
265
266 start_spin = spin_time_start();
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700267
268 do {
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700269 u64 start_spin_fast = spin_time_start();
270
271 timeout = TIMEOUT;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700272
273 asm("1: xchgb %1,%0\n"
274 " testb %1,%1\n"
275 " jz 3f\n"
276 "2: rep;nop\n"
277 " cmpb $0,%0\n"
278 " je 1b\n"
279 " dec %2\n"
280 " jnz 2b\n"
281 "3:\n"
282 : "+m" (xl->lock), "=q" (oldval), "+r" (timeout)
283 : "1" (1)
284 : "memory");
285
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700286 spin_time_accum_spinning(start_spin_fast);
Jeremy Fitzhardinge1e696f62008-08-20 17:02:20 -0700287
288 } while (unlikely(oldval != 0 &&
289 (TIMEOUT == ~0 || !xen_spin_lock_slow(lock, irq_enable))));
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700290
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700291 spin_time_accum_total(start_spin);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700292}
293
Thomas Gleixner445c8952009-12-02 19:49:50 +0100294static void xen_spin_lock(struct arch_spinlock *lock)
Jeremy Fitzhardinge1e696f62008-08-20 17:02:20 -0700295{
296 __xen_spin_lock(lock, false);
297}
298
Thomas Gleixner445c8952009-12-02 19:49:50 +0100299static void xen_spin_lock_flags(struct arch_spinlock *lock, unsigned long flags)
Jeremy Fitzhardinge1e696f62008-08-20 17:02:20 -0700300{
301 __xen_spin_lock(lock, !raw_irqs_disabled_flags(flags));
302}
303
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700304static noinline void xen_spin_unlock_slow(struct xen_spinlock *xl)
305{
306 int cpu;
307
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700308 ADD_STATS(released_slow, 1);
309
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700310 for_each_online_cpu(cpu) {
311 /* XXX should mix up next cpu selection */
312 if (per_cpu(lock_spinners, cpu) == xl) {
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700313 ADD_STATS(released_slow_kicked, 1);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700314 xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
315 break;
316 }
317 }
318}
319
Thomas Gleixner445c8952009-12-02 19:49:50 +0100320static void xen_spin_unlock(struct arch_spinlock *lock)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700321{
322 struct xen_spinlock *xl = (struct xen_spinlock *)lock;
323
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700324 ADD_STATS(released, 1);
325
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700326 smp_wmb(); /* make sure no writes get moved after unlock */
327 xl->lock = 0; /* release lock */
328
Yang Xiaowei2496afb2009-09-09 12:44:52 -0700329 /*
330 * Make sure unlock happens before checking for waiting
331 * spinners. We need a strong barrier to enforce the
332 * write-read ordering to different memory locations, as the
333 * CPU makes no implied guarantees about their ordering.
334 */
335 mb();
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700336
337 if (unlikely(xl->spinners))
338 xen_spin_unlock_slow(xl);
339}
340
341static irqreturn_t dummy_handler(int irq, void *dev_id)
342{
343 BUG();
344 return IRQ_HANDLED;
345}
346
347void __cpuinit xen_init_lock_cpu(int cpu)
348{
349 int irq;
350 const char *name;
351
352 name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
353 irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
354 cpu,
355 dummy_handler,
356 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
357 name,
358 NULL);
359
360 if (irq >= 0) {
361 disable_irq(irq); /* make sure it's never delivered */
362 per_cpu(lock_kicker_irq, cpu) = irq;
363 }
364
365 printk("cpu %d spinlock event irq %d\n", cpu, irq);
366}
367
Alex Nixond68d82a2008-08-22 11:52:15 +0100368void xen_uninit_lock_cpu(int cpu)
369{
370 unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
371}
372
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700373void __init xen_init_spinlocks(void)
374{
375 pv_lock_ops.spin_is_locked = xen_spin_is_locked;
376 pv_lock_ops.spin_is_contended = xen_spin_is_contended;
377 pv_lock_ops.spin_lock = xen_spin_lock;
Jeremy Fitzhardinge1e696f62008-08-20 17:02:20 -0700378 pv_lock_ops.spin_lock_flags = xen_spin_lock_flags;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700379 pv_lock_ops.spin_trylock = xen_spin_trylock;
380 pv_lock_ops.spin_unlock = xen_spin_unlock;
381}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700382
383#ifdef CONFIG_XEN_DEBUG_FS
384
385static struct dentry *d_spin_debug;
386
387static int __init xen_spinlock_debugfs(void)
388{
389 struct dentry *d_xen = xen_init_debugfs();
390
391 if (d_xen == NULL)
392 return -ENOMEM;
393
394 d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
395
396 debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
397
398 debugfs_create_u32("timeout", 0644, d_spin_debug, &lock_timeout);
399
400 debugfs_create_u64("taken", 0444, d_spin_debug, &spinlock_stats.taken);
401 debugfs_create_u32("taken_slow", 0444, d_spin_debug,
402 &spinlock_stats.taken_slow);
403 debugfs_create_u32("taken_slow_nested", 0444, d_spin_debug,
404 &spinlock_stats.taken_slow_nested);
405 debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
406 &spinlock_stats.taken_slow_pickup);
407 debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
408 &spinlock_stats.taken_slow_spurious);
Jeremy Fitzhardinge1e696f62008-08-20 17:02:20 -0700409 debugfs_create_u32("taken_slow_irqenable", 0444, d_spin_debug,
410 &spinlock_stats.taken_slow_irqenable);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700411
412 debugfs_create_u64("released", 0444, d_spin_debug, &spinlock_stats.released);
413 debugfs_create_u32("released_slow", 0444, d_spin_debug,
414 &spinlock_stats.released_slow);
415 debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
416 &spinlock_stats.released_slow_kicked);
417
418 debugfs_create_u64("time_spinning", 0444, d_spin_debug,
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700419 &spinlock_stats.time_spinning);
420 debugfs_create_u64("time_blocked", 0444, d_spin_debug,
421 &spinlock_stats.time_blocked);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700422 debugfs_create_u64("time_total", 0444, d_spin_debug,
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700423 &spinlock_stats.time_total);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700424
425 xen_debugfs_create_u32_array("histo_total", 0444, d_spin_debug,
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700426 spinlock_stats.histo_spin_total, HISTO_BUCKETS + 1);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700427 xen_debugfs_create_u32_array("histo_spinning", 0444, d_spin_debug,
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700428 spinlock_stats.histo_spin_spinning, HISTO_BUCKETS + 1);
429 xen_debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
430 spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700431
432 return 0;
433}
434fs_initcall(xen_spinlock_debugfs);
435
436#endif /* CONFIG_XEN_DEBUG_FS */