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