Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2005, Red Hat, Inc., Ingo Molnar |
| 3 | * Released under the General Public License (GPL). |
| 4 | * |
| 5 | * This file contains the spinlock/rwlock implementations for |
| 6 | * DEBUG_SPINLOCK. |
| 7 | */ |
| 8 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 9 | #include <linux/spinlock.h> |
Andrew Morton | bb81a09 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 10 | #include <linux/nmi.h> |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 12 | #include <linux/debug_locks.h> |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 13 | #include <linux/delay.h> |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 15 | |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 16 | void __spin_lock_init(spinlock_t *lock, const char *name, |
| 17 | struct lock_class_key *key) |
| 18 | { |
| 19 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 20 | /* |
| 21 | * Make sure we are not reinitializing a held lock: |
| 22 | */ |
| 23 | debug_check_no_locks_freed((void *)lock, sizeof(*lock)); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 24 | lockdep_init_map(&lock->dep_map, name, key, 0); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 25 | #endif |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 26 | lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 27 | lock->magic = SPINLOCK_MAGIC; |
| 28 | lock->owner = SPINLOCK_OWNER_INIT; |
| 29 | lock->owner_cpu = -1; |
| 30 | } |
| 31 | |
| 32 | EXPORT_SYMBOL(__spin_lock_init); |
| 33 | |
| 34 | void __rwlock_init(rwlock_t *lock, const char *name, |
| 35 | struct lock_class_key *key) |
| 36 | { |
| 37 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 38 | /* |
| 39 | * Make sure we are not reinitializing a held lock: |
| 40 | */ |
| 41 | debug_check_no_locks_freed((void *)lock, sizeof(*lock)); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 42 | lockdep_init_map(&lock->dep_map, name, key, 0); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 43 | #endif |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 44 | lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED; |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 45 | lock->magic = RWLOCK_MAGIC; |
| 46 | lock->owner = SPINLOCK_OWNER_INIT; |
| 47 | lock->owner_cpu = -1; |
| 48 | } |
| 49 | |
| 50 | EXPORT_SYMBOL(__rwlock_init); |
| 51 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 52 | static void spin_bug(spinlock_t *lock, const char *msg) |
| 53 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 54 | struct task_struct *owner = NULL; |
| 55 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 56 | if (!debug_locks_off()) |
| 57 | return; |
| 58 | |
| 59 | if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT) |
| 60 | owner = lock->owner; |
| 61 | printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n", |
| 62 | msg, raw_smp_processor_id(), |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 63 | current->comm, task_pid_nr(current)); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 64 | printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, " |
| 65 | ".owner_cpu: %d\n", |
| 66 | lock, lock->magic, |
| 67 | owner ? owner->comm : "<none>", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 68 | owner ? task_pid_nr(owner) : -1, |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 69 | lock->owner_cpu); |
| 70 | dump_stack(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg) |
| 74 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 75 | static inline void |
| 76 | debug_spin_lock_before(spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 77 | { |
| 78 | SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic"); |
| 79 | SPIN_BUG_ON(lock->owner == current, lock, "recursion"); |
| 80 | SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(), |
| 81 | lock, "cpu recursion"); |
| 82 | } |
| 83 | |
| 84 | static inline void debug_spin_lock_after(spinlock_t *lock) |
| 85 | { |
| 86 | lock->owner_cpu = raw_smp_processor_id(); |
| 87 | lock->owner = current; |
| 88 | } |
| 89 | |
| 90 | static inline void debug_spin_unlock(spinlock_t *lock) |
| 91 | { |
| 92 | SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic"); |
| 93 | SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked"); |
| 94 | SPIN_BUG_ON(lock->owner != current, lock, "wrong owner"); |
| 95 | SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(), |
| 96 | lock, "wrong CPU"); |
| 97 | lock->owner = SPINLOCK_OWNER_INIT; |
| 98 | lock->owner_cpu = -1; |
| 99 | } |
| 100 | |
| 101 | static void __spin_lock_debug(spinlock_t *lock) |
| 102 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 103 | u64 i; |
Chuck Ebbert | c22f008b | 2006-09-29 01:59:14 -0700 | [diff] [blame] | 104 | u64 loops = loops_per_jiffy * HZ; |
| 105 | int print_once = 1; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 106 | |
| 107 | for (;;) { |
Chuck Ebbert | c22f008b | 2006-09-29 01:59:14 -0700 | [diff] [blame] | 108 | for (i = 0; i < loops; i++) { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 109 | if (arch_spin_trylock(&lock->raw_lock)) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 110 | return; |
Ingo Molnar | e0a6029 | 2006-02-07 12:58:54 -0800 | [diff] [blame] | 111 | __delay(1); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 112 | } |
| 113 | /* lockup suspected: */ |
| 114 | if (print_once) { |
| 115 | print_once = 0; |
Dave Jones | 51989b9 | 2006-01-09 20:51:32 -0800 | [diff] [blame] | 116 | printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, " |
| 117 | "%s/%d, %p\n", |
Ingo Molnar | bb44f11 | 2005-12-20 11:54:17 +0100 | [diff] [blame] | 118 | raw_smp_processor_id(), current->comm, |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 119 | task_pid_nr(current), lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 120 | dump_stack(); |
Andrew Morton | bb81a09 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 121 | #ifdef CONFIG_SMP |
| 122 | trigger_all_cpu_backtrace(); |
| 123 | #endif |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void _raw_spin_lock(spinlock_t *lock) |
| 129 | { |
| 130 | debug_spin_lock_before(lock); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 131 | if (unlikely(!arch_spin_trylock(&lock->raw_lock))) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 132 | __spin_lock_debug(lock); |
| 133 | debug_spin_lock_after(lock); |
| 134 | } |
| 135 | |
| 136 | int _raw_spin_trylock(spinlock_t *lock) |
| 137 | { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 138 | int ret = arch_spin_trylock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 139 | |
| 140 | if (ret) |
| 141 | debug_spin_lock_after(lock); |
| 142 | #ifndef CONFIG_SMP |
| 143 | /* |
| 144 | * Must not happen on UP: |
| 145 | */ |
| 146 | SPIN_BUG_ON(!ret, lock, "trylock failure on UP"); |
| 147 | #endif |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | void _raw_spin_unlock(spinlock_t *lock) |
| 152 | { |
| 153 | debug_spin_unlock(lock); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 154 | arch_spin_unlock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void rwlock_bug(rwlock_t *lock, const char *msg) |
| 158 | { |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 159 | if (!debug_locks_off()) |
| 160 | return; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 161 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 162 | printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n", |
| 163 | msg, raw_smp_processor_id(), current->comm, |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 164 | task_pid_nr(current), lock); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 165 | dump_stack(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg) |
| 169 | |
Andrew Morton | 72f0b4e | 2006-08-05 12:13:47 -0700 | [diff] [blame] | 170 | #if 0 /* __write_lock_debug() can lock up - maybe this can too? */ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 171 | static void __read_lock_debug(rwlock_t *lock) |
| 172 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 173 | u64 i; |
Chuck Ebbert | c22f008b | 2006-09-29 01:59:14 -0700 | [diff] [blame] | 174 | u64 loops = loops_per_jiffy * HZ; |
| 175 | int print_once = 1; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 176 | |
| 177 | for (;;) { |
Chuck Ebbert | c22f008b | 2006-09-29 01:59:14 -0700 | [diff] [blame] | 178 | for (i = 0; i < loops; i++) { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 179 | if (arch_read_trylock(&lock->raw_lock)) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 180 | return; |
Ingo Molnar | e0a6029 | 2006-02-07 12:58:54 -0800 | [diff] [blame] | 181 | __delay(1); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 182 | } |
| 183 | /* lockup suspected: */ |
| 184 | if (print_once) { |
| 185 | print_once = 0; |
Dave Jones | 51989b9 | 2006-01-09 20:51:32 -0800 | [diff] [blame] | 186 | printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, " |
| 187 | "%s/%d, %p\n", |
Ingo Molnar | bb44f11 | 2005-12-20 11:54:17 +0100 | [diff] [blame] | 188 | raw_smp_processor_id(), current->comm, |
| 189 | current->pid, lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 190 | dump_stack(); |
| 191 | } |
| 192 | } |
| 193 | } |
Andrew Morton | 72f0b4e | 2006-08-05 12:13:47 -0700 | [diff] [blame] | 194 | #endif |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 195 | |
| 196 | void _raw_read_lock(rwlock_t *lock) |
| 197 | { |
| 198 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 199 | arch_read_lock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int _raw_read_trylock(rwlock_t *lock) |
| 203 | { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 204 | int ret = arch_read_trylock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 205 | |
| 206 | #ifndef CONFIG_SMP |
| 207 | /* |
| 208 | * Must not happen on UP: |
| 209 | */ |
| 210 | RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP"); |
| 211 | #endif |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | void _raw_read_unlock(rwlock_t *lock) |
| 216 | { |
| 217 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 218 | arch_read_unlock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static inline void debug_write_lock_before(rwlock_t *lock) |
| 222 | { |
| 223 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); |
| 224 | RWLOCK_BUG_ON(lock->owner == current, lock, "recursion"); |
| 225 | RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(), |
| 226 | lock, "cpu recursion"); |
| 227 | } |
| 228 | |
| 229 | static inline void debug_write_lock_after(rwlock_t *lock) |
| 230 | { |
| 231 | lock->owner_cpu = raw_smp_processor_id(); |
| 232 | lock->owner = current; |
| 233 | } |
| 234 | |
| 235 | static inline void debug_write_unlock(rwlock_t *lock) |
| 236 | { |
| 237 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); |
| 238 | RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner"); |
| 239 | RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(), |
| 240 | lock, "wrong CPU"); |
| 241 | lock->owner = SPINLOCK_OWNER_INIT; |
| 242 | lock->owner_cpu = -1; |
| 243 | } |
| 244 | |
Andrew Morton | 72f0b4e | 2006-08-05 12:13:47 -0700 | [diff] [blame] | 245 | #if 0 /* This can cause lockups */ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 246 | static void __write_lock_debug(rwlock_t *lock) |
| 247 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 248 | u64 i; |
Chuck Ebbert | c22f008b | 2006-09-29 01:59:14 -0700 | [diff] [blame] | 249 | u64 loops = loops_per_jiffy * HZ; |
| 250 | int print_once = 1; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 251 | |
| 252 | for (;;) { |
Chuck Ebbert | c22f008b | 2006-09-29 01:59:14 -0700 | [diff] [blame] | 253 | for (i = 0; i < loops; i++) { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 254 | if (arch_write_trylock(&lock->raw_lock)) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 255 | return; |
Ingo Molnar | e0a6029 | 2006-02-07 12:58:54 -0800 | [diff] [blame] | 256 | __delay(1); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 257 | } |
| 258 | /* lockup suspected: */ |
| 259 | if (print_once) { |
| 260 | print_once = 0; |
Dave Jones | 51989b9 | 2006-01-09 20:51:32 -0800 | [diff] [blame] | 261 | printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, " |
| 262 | "%s/%d, %p\n", |
Ingo Molnar | bb44f11 | 2005-12-20 11:54:17 +0100 | [diff] [blame] | 263 | raw_smp_processor_id(), current->comm, |
| 264 | current->pid, lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 265 | dump_stack(); |
| 266 | } |
| 267 | } |
| 268 | } |
Andrew Morton | 72f0b4e | 2006-08-05 12:13:47 -0700 | [diff] [blame] | 269 | #endif |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 270 | |
| 271 | void _raw_write_lock(rwlock_t *lock) |
| 272 | { |
| 273 | debug_write_lock_before(lock); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 274 | arch_write_lock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 275 | debug_write_lock_after(lock); |
| 276 | } |
| 277 | |
| 278 | int _raw_write_trylock(rwlock_t *lock) |
| 279 | { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 280 | int ret = arch_write_trylock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 281 | |
| 282 | if (ret) |
| 283 | debug_write_lock_after(lock); |
| 284 | #ifndef CONFIG_SMP |
| 285 | /* |
| 286 | * Must not happen on UP: |
| 287 | */ |
| 288 | RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP"); |
| 289 | #endif |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | void _raw_write_unlock(rwlock_t *lock) |
| 294 | { |
| 295 | debug_write_unlock(lock); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame^] | 296 | arch_write_unlock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 297 | } |