Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 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> |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 7 | #include <linux/debugfs.h> |
| 8 | #include <linux/log2.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/gfp.h> |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 10 | |
| 11 | #include <asm/paravirt.h> |
| 12 | |
| 13 | #include <xen/interface/xen.h> |
| 14 | #include <xen/events.h> |
| 15 | |
| 16 | #include "xen-ops.h" |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 17 | #include "debugfs.h" |
| 18 | |
| 19 | #ifdef CONFIG_XEN_DEBUG_FS |
| 20 | static struct xen_spinlock_stats |
| 21 | { |
| 22 | u64 taken; |
| 23 | u32 taken_slow; |
| 24 | u32 taken_slow_nested; |
| 25 | u32 taken_slow_pickup; |
| 26 | u32 taken_slow_spurious; |
Jeremy Fitzhardinge | 1e696f6 | 2008-08-20 17:02:20 -0700 | [diff] [blame] | 27 | u32 taken_slow_irqenable; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 28 | |
| 29 | u64 released; |
| 30 | u32 released_slow; |
| 31 | u32 released_slow_kicked; |
| 32 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 33 | #define HISTO_BUCKETS 30 |
| 34 | u32 histo_spin_total[HISTO_BUCKETS+1]; |
| 35 | u32 histo_spin_spinning[HISTO_BUCKETS+1]; |
| 36 | u32 histo_spin_blocked[HISTO_BUCKETS+1]; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 37 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 38 | u64 time_total; |
| 39 | u64 time_spinning; |
| 40 | u64 time_blocked; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 41 | } spinlock_stats; |
| 42 | |
| 43 | static u8 zero_stats; |
| 44 | |
| 45 | static unsigned lock_timeout = 1 << 10; |
| 46 | #define TIMEOUT lock_timeout |
| 47 | |
| 48 | static inline void check_zero(void) |
| 49 | { |
| 50 | if (unlikely(zero_stats)) { |
| 51 | memset(&spinlock_stats, 0, sizeof(spinlock_stats)); |
| 52 | zero_stats = 0; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #define ADD_STATS(elem, val) \ |
| 57 | do { check_zero(); spinlock_stats.elem += (val); } while(0) |
| 58 | |
| 59 | static inline u64 spin_time_start(void) |
| 60 | { |
| 61 | return xen_clocksource_read(); |
| 62 | } |
| 63 | |
| 64 | static 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 Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 76 | static inline void spin_time_accum_spinning(u64 start) |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 77 | { |
| 78 | u32 delta = xen_clocksource_read() - start; |
| 79 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 80 | __spin_time_accum(delta, spinlock_stats.histo_spin_spinning); |
| 81 | spinlock_stats.time_spinning += delta; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 84 | static inline void spin_time_accum_total(u64 start) |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 85 | { |
| 86 | u32 delta = xen_clocksource_read() - start; |
| 87 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 88 | __spin_time_accum(delta, spinlock_stats.histo_spin_total); |
| 89 | spinlock_stats.time_total += delta; |
| 90 | } |
| 91 | |
| 92 | static inline void spin_time_accum_blocked(u64 start) |
| 93 | { |
| 94 | u32 delta = xen_clocksource_read() - start; |
| 95 | |
| 96 | __spin_time_accum(delta, spinlock_stats.histo_spin_blocked); |
| 97 | spinlock_stats.time_blocked += delta; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 98 | } |
| 99 | #else /* !CONFIG_XEN_DEBUG_FS */ |
| 100 | #define TIMEOUT (1 << 10) |
| 101 | #define ADD_STATS(elem, val) do { (void)(val); } while(0) |
| 102 | |
| 103 | static inline u64 spin_time_start(void) |
| 104 | { |
| 105 | return 0; |
| 106 | } |
| 107 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 108 | static inline void spin_time_accum_total(u64 start) |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 109 | { |
| 110 | } |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 111 | static inline void spin_time_accum_spinning(u64 start) |
| 112 | { |
| 113 | } |
| 114 | static inline void spin_time_accum_blocked(u64 start) |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 115 | { |
| 116 | } |
| 117 | #endif /* CONFIG_XEN_DEBUG_FS */ |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 118 | |
David Vrabel | 7a7546b | 2012-01-23 19:32:25 +0000 | [diff] [blame] | 119 | /* |
| 120 | * Size struct xen_spinlock so it's the same as arch_spinlock_t. |
| 121 | */ |
| 122 | #if NR_CPUS < 256 |
| 123 | typedef u8 xen_spinners_t; |
| 124 | # define inc_spinners(xl) \ |
| 125 | asm(LOCK_PREFIX " incb %0" : "+m" ((xl)->spinners) : : "memory"); |
| 126 | # define dec_spinners(xl) \ |
| 127 | asm(LOCK_PREFIX " decb %0" : "+m" ((xl)->spinners) : : "memory"); |
| 128 | #else |
| 129 | typedef u16 xen_spinners_t; |
| 130 | # define inc_spinners(xl) \ |
| 131 | asm(LOCK_PREFIX " incw %0" : "+m" ((xl)->spinners) : : "memory"); |
| 132 | # define dec_spinners(xl) \ |
| 133 | asm(LOCK_PREFIX " decw %0" : "+m" ((xl)->spinners) : : "memory"); |
| 134 | #endif |
| 135 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 136 | struct xen_spinlock { |
| 137 | unsigned char lock; /* 0 -> free; 1 -> locked */ |
David Vrabel | 7a7546b | 2012-01-23 19:32:25 +0000 | [diff] [blame] | 138 | xen_spinners_t spinners; /* count of waiting cpus */ |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 141 | static int xen_spin_is_locked(struct arch_spinlock *lock) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 142 | { |
| 143 | struct xen_spinlock *xl = (struct xen_spinlock *)lock; |
| 144 | |
| 145 | return xl->lock != 0; |
| 146 | } |
| 147 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 148 | static int xen_spin_is_contended(struct arch_spinlock *lock) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 149 | { |
| 150 | struct xen_spinlock *xl = (struct xen_spinlock *)lock; |
| 151 | |
| 152 | /* Not strictly true; this is only the count of contended |
| 153 | lock-takers entering the slow path. */ |
| 154 | return xl->spinners != 0; |
| 155 | } |
| 156 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 157 | static int xen_spin_trylock(struct arch_spinlock *lock) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 158 | { |
| 159 | struct xen_spinlock *xl = (struct xen_spinlock *)lock; |
| 160 | u8 old = 1; |
| 161 | |
| 162 | asm("xchgb %b0,%1" |
| 163 | : "+q" (old), "+m" (xl->lock) : : "memory"); |
| 164 | |
| 165 | return old == 0; |
| 166 | } |
| 167 | |
| 168 | static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; |
| 169 | static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners); |
| 170 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Mark a cpu as interested in a lock. Returns the CPU's previous |
| 173 | * lock of interest, in case we got preempted by an interrupt. |
| 174 | */ |
| 175 | static inline struct xen_spinlock *spinning_lock(struct xen_spinlock *xl) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 176 | { |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 177 | struct xen_spinlock *prev; |
| 178 | |
Christoph Lameter | 780f36d | 2010-12-06 11:16:29 -0600 | [diff] [blame] | 179 | prev = __this_cpu_read(lock_spinners); |
| 180 | __this_cpu_write(lock_spinners, xl); |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 181 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 182 | wmb(); /* set lock of interest before count */ |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 183 | |
David Vrabel | 7a7546b | 2012-01-23 19:32:25 +0000 | [diff] [blame] | 184 | inc_spinners(xl); |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 185 | |
| 186 | return prev; |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Mark a cpu as no longer interested in a lock. Restores previous |
| 191 | * lock of interest (NULL for none). |
| 192 | */ |
| 193 | static inline void unspinning_lock(struct xen_spinlock *xl, struct xen_spinlock *prev) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 194 | { |
David Vrabel | 7a7546b | 2012-01-23 19:32:25 +0000 | [diff] [blame] | 195 | dec_spinners(xl); |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 196 | wmb(); /* decrement count before restoring lock */ |
Christoph Lameter | 780f36d | 2010-12-06 11:16:29 -0600 | [diff] [blame] | 197 | __this_cpu_write(lock_spinners, prev); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 200 | static noinline int xen_spin_lock_slow(struct arch_spinlock *lock, bool irq_enable) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 201 | { |
| 202 | struct xen_spinlock *xl = (struct xen_spinlock *)lock; |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 203 | struct xen_spinlock *prev; |
Christoph Lameter | 780f36d | 2010-12-06 11:16:29 -0600 | [diff] [blame] | 204 | int irq = __this_cpu_read(lock_kicker_irq); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 205 | int ret; |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 206 | u64 start; |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 207 | |
| 208 | /* If kicker interrupts not initialized yet, just spin */ |
| 209 | if (irq == -1) |
| 210 | return 0; |
| 211 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 212 | start = spin_time_start(); |
| 213 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 214 | /* announce we're spinning */ |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 215 | prev = spinning_lock(xl); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 216 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 217 | ADD_STATS(taken_slow, 1); |
| 218 | ADD_STATS(taken_slow_nested, prev != NULL); |
| 219 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 220 | do { |
Jeremy Fitzhardinge | 4d576b5 | 2009-09-09 12:33:51 -0700 | [diff] [blame] | 221 | unsigned long flags; |
| 222 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 223 | /* clear pending */ |
| 224 | xen_clear_irq_pending(irq); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 225 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 226 | /* check again make sure it didn't become free while |
| 227 | we weren't looking */ |
| 228 | ret = xen_spin_trylock(lock); |
| 229 | if (ret) { |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 230 | ADD_STATS(taken_slow_pickup, 1); |
| 231 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 232 | /* |
| 233 | * If we interrupted another spinlock while it |
| 234 | * was blocking, make sure it doesn't block |
| 235 | * without rechecking the lock. |
| 236 | */ |
| 237 | if (prev != NULL) |
| 238 | xen_set_irq_pending(irq); |
| 239 | goto out; |
| 240 | } |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 241 | |
David Howells | df9ee29 | 2010-10-07 14:08:55 +0100 | [diff] [blame] | 242 | flags = arch_local_save_flags(); |
Jeremy Fitzhardinge | 4d576b5 | 2009-09-09 12:33:51 -0700 | [diff] [blame] | 243 | if (irq_enable) { |
| 244 | ADD_STATS(taken_slow_irqenable, 1); |
| 245 | raw_local_irq_enable(); |
| 246 | } |
| 247 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 248 | /* |
| 249 | * Block until irq becomes pending. If we're |
| 250 | * interrupted at this point (after the trylock but |
| 251 | * before entering the block), then the nested lock |
| 252 | * handler guarantees that the irq will be left |
| 253 | * pending if there's any chance the lock became free; |
| 254 | * xen_poll_irq() returns immediately if the irq is |
| 255 | * pending. |
| 256 | */ |
| 257 | xen_poll_irq(irq); |
Jeremy Fitzhardinge | 4d576b5 | 2009-09-09 12:33:51 -0700 | [diff] [blame] | 258 | |
| 259 | raw_local_irq_restore(flags); |
| 260 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 261 | ADD_STATS(taken_slow_spurious, !xen_test_irq_pending(irq)); |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 262 | } while (!xen_test_irq_pending(irq)); /* check for spurious wakeups */ |
| 263 | |
Thomas Gleixner | d6c88a5 | 2008-10-15 15:27:23 +0200 | [diff] [blame] | 264 | kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq)); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 265 | |
| 266 | out: |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 267 | unspinning_lock(xl, prev); |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 268 | spin_time_accum_blocked(start); |
| 269 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 270 | return ret; |
| 271 | } |
| 272 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 273 | static inline void __xen_spin_lock(struct arch_spinlock *lock, bool irq_enable) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 274 | { |
| 275 | struct xen_spinlock *xl = (struct xen_spinlock *)lock; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 276 | unsigned timeout; |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 277 | u8 oldval; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 278 | u64 start_spin; |
| 279 | |
| 280 | ADD_STATS(taken, 1); |
| 281 | |
| 282 | start_spin = spin_time_start(); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 283 | |
| 284 | do { |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 285 | u64 start_spin_fast = spin_time_start(); |
| 286 | |
| 287 | timeout = TIMEOUT; |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 288 | |
| 289 | asm("1: xchgb %1,%0\n" |
| 290 | " testb %1,%1\n" |
| 291 | " jz 3f\n" |
| 292 | "2: rep;nop\n" |
| 293 | " cmpb $0,%0\n" |
| 294 | " je 1b\n" |
| 295 | " dec %2\n" |
| 296 | " jnz 2b\n" |
| 297 | "3:\n" |
| 298 | : "+m" (xl->lock), "=q" (oldval), "+r" (timeout) |
| 299 | : "1" (1) |
| 300 | : "memory"); |
| 301 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 302 | spin_time_accum_spinning(start_spin_fast); |
Jeremy Fitzhardinge | 1e696f6 | 2008-08-20 17:02:20 -0700 | [diff] [blame] | 303 | |
| 304 | } while (unlikely(oldval != 0 && |
| 305 | (TIMEOUT == ~0 || !xen_spin_lock_slow(lock, irq_enable)))); |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 306 | |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 307 | spin_time_accum_total(start_spin); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 310 | static void xen_spin_lock(struct arch_spinlock *lock) |
Jeremy Fitzhardinge | 1e696f6 | 2008-08-20 17:02:20 -0700 | [diff] [blame] | 311 | { |
| 312 | __xen_spin_lock(lock, false); |
| 313 | } |
| 314 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 315 | static void xen_spin_lock_flags(struct arch_spinlock *lock, unsigned long flags) |
Jeremy Fitzhardinge | 1e696f6 | 2008-08-20 17:02:20 -0700 | [diff] [blame] | 316 | { |
| 317 | __xen_spin_lock(lock, !raw_irqs_disabled_flags(flags)); |
| 318 | } |
| 319 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 320 | static noinline void xen_spin_unlock_slow(struct xen_spinlock *xl) |
| 321 | { |
| 322 | int cpu; |
| 323 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 324 | ADD_STATS(released_slow, 1); |
| 325 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 326 | for_each_online_cpu(cpu) { |
| 327 | /* XXX should mix up next cpu selection */ |
| 328 | if (per_cpu(lock_spinners, cpu) == xl) { |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 329 | ADD_STATS(released_slow_kicked, 1); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 330 | xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 335 | static void xen_spin_unlock(struct arch_spinlock *lock) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 336 | { |
| 337 | struct xen_spinlock *xl = (struct xen_spinlock *)lock; |
| 338 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 339 | ADD_STATS(released, 1); |
| 340 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 341 | smp_wmb(); /* make sure no writes get moved after unlock */ |
| 342 | xl->lock = 0; /* release lock */ |
| 343 | |
Yang Xiaowei | 2496afb | 2009-09-09 12:44:52 -0700 | [diff] [blame] | 344 | /* |
| 345 | * Make sure unlock happens before checking for waiting |
| 346 | * spinners. We need a strong barrier to enforce the |
| 347 | * write-read ordering to different memory locations, as the |
| 348 | * CPU makes no implied guarantees about their ordering. |
| 349 | */ |
| 350 | mb(); |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 351 | |
| 352 | if (unlikely(xl->spinners)) |
| 353 | xen_spin_unlock_slow(xl); |
| 354 | } |
| 355 | |
| 356 | static irqreturn_t dummy_handler(int irq, void *dev_id) |
| 357 | { |
| 358 | BUG(); |
| 359 | return IRQ_HANDLED; |
| 360 | } |
| 361 | |
| 362 | void __cpuinit xen_init_lock_cpu(int cpu) |
| 363 | { |
| 364 | int irq; |
| 365 | const char *name; |
| 366 | |
| 367 | name = kasprintf(GFP_KERNEL, "spinlock%d", cpu); |
| 368 | irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR, |
| 369 | cpu, |
| 370 | dummy_handler, |
| 371 | IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING, |
| 372 | name, |
| 373 | NULL); |
| 374 | |
| 375 | if (irq >= 0) { |
| 376 | disable_irq(irq); /* make sure it's never delivered */ |
| 377 | per_cpu(lock_kicker_irq, cpu) = irq; |
| 378 | } |
| 379 | |
| 380 | printk("cpu %d spinlock event irq %d\n", cpu, irq); |
| 381 | } |
| 382 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 383 | void xen_uninit_lock_cpu(int cpu) |
| 384 | { |
| 385 | unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL); |
| 386 | } |
| 387 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 388 | void __init xen_init_spinlocks(void) |
| 389 | { |
David Vrabel | 7a7546b | 2012-01-23 19:32:25 +0000 | [diff] [blame] | 390 | BUILD_BUG_ON(sizeof(struct xen_spinlock) > sizeof(arch_spinlock_t)); |
| 391 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 392 | pv_lock_ops.spin_is_locked = xen_spin_is_locked; |
| 393 | pv_lock_ops.spin_is_contended = xen_spin_is_contended; |
| 394 | pv_lock_ops.spin_lock = xen_spin_lock; |
Jeremy Fitzhardinge | 1e696f6 | 2008-08-20 17:02:20 -0700 | [diff] [blame] | 395 | pv_lock_ops.spin_lock_flags = xen_spin_lock_flags; |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 396 | pv_lock_ops.spin_trylock = xen_spin_trylock; |
| 397 | pv_lock_ops.spin_unlock = xen_spin_unlock; |
| 398 | } |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 399 | |
| 400 | #ifdef CONFIG_XEN_DEBUG_FS |
| 401 | |
| 402 | static struct dentry *d_spin_debug; |
| 403 | |
| 404 | static int __init xen_spinlock_debugfs(void) |
| 405 | { |
| 406 | struct dentry *d_xen = xen_init_debugfs(); |
| 407 | |
| 408 | if (d_xen == NULL) |
| 409 | return -ENOMEM; |
| 410 | |
| 411 | d_spin_debug = debugfs_create_dir("spinlocks", d_xen); |
| 412 | |
| 413 | debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats); |
| 414 | |
| 415 | debugfs_create_u32("timeout", 0644, d_spin_debug, &lock_timeout); |
| 416 | |
| 417 | debugfs_create_u64("taken", 0444, d_spin_debug, &spinlock_stats.taken); |
| 418 | debugfs_create_u32("taken_slow", 0444, d_spin_debug, |
| 419 | &spinlock_stats.taken_slow); |
| 420 | debugfs_create_u32("taken_slow_nested", 0444, d_spin_debug, |
| 421 | &spinlock_stats.taken_slow_nested); |
| 422 | debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug, |
| 423 | &spinlock_stats.taken_slow_pickup); |
| 424 | debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug, |
| 425 | &spinlock_stats.taken_slow_spurious); |
Jeremy Fitzhardinge | 1e696f6 | 2008-08-20 17:02:20 -0700 | [diff] [blame] | 426 | debugfs_create_u32("taken_slow_irqenable", 0444, d_spin_debug, |
| 427 | &spinlock_stats.taken_slow_irqenable); |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 428 | |
| 429 | debugfs_create_u64("released", 0444, d_spin_debug, &spinlock_stats.released); |
| 430 | debugfs_create_u32("released_slow", 0444, d_spin_debug, |
| 431 | &spinlock_stats.released_slow); |
| 432 | debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug, |
| 433 | &spinlock_stats.released_slow_kicked); |
| 434 | |
| 435 | debugfs_create_u64("time_spinning", 0444, d_spin_debug, |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 436 | &spinlock_stats.time_spinning); |
| 437 | debugfs_create_u64("time_blocked", 0444, d_spin_debug, |
| 438 | &spinlock_stats.time_blocked); |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 439 | debugfs_create_u64("time_total", 0444, d_spin_debug, |
Jeremy Fitzhardinge | f8eca41 | 2008-08-20 17:02:21 -0700 | [diff] [blame] | 440 | &spinlock_stats.time_total); |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 441 | |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 442 | debugfs_create_u32_array("histo_total", 0444, d_spin_debug, |
| 443 | spinlock_stats.histo_spin_total, HISTO_BUCKETS + 1); |
| 444 | debugfs_create_u32_array("histo_spinning", 0444, d_spin_debug, |
| 445 | spinlock_stats.histo_spin_spinning, HISTO_BUCKETS + 1); |
| 446 | debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug, |
| 447 | spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1); |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | fs_initcall(xen_spinlock_debugfs); |
| 452 | |
| 453 | #endif /* CONFIG_XEN_DEBUG_FS */ |