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 9 #include <linux/spinlock.h> 10 #include <linux/nmi.h> 11 #include <linux/interrupt.h> 12 #include <linux/debug_locks.h> 13 #include <linux/delay.h> 14 #include <linux/export.h> 15 16 void __raw_spin_lock_init(raw_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)); 24 lockdep_init_map(&lock->dep_map, name, key, 0); 25 #endif 26 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 27 lock->magic = SPINLOCK_MAGIC; 28 lock->owner = SPINLOCK_OWNER_INIT; 29 lock->owner_cpu = -1; 30 } 31 32 EXPORT_SYMBOL(__raw_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)); 42 lockdep_init_map(&lock->dep_map, name, key, 0); 43 #endif 44 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED; 45 lock->magic = RWLOCK_MAGIC; 46 lock->owner = SPINLOCK_OWNER_INIT; 47 lock->owner_cpu = -1; 48 } 49 50 EXPORT_SYMBOL(__rwlock_init); 51 52 static void spin_dump(raw_spinlock_t *lock, const char *msg) 53 { 54 struct task_struct *owner = NULL; 55 56 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT) 57 owner = lock->owner; 58 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n", 59 msg, raw_smp_processor_id(), 60 current->comm, task_pid_nr(current)); 61 printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, " 62 ".owner_cpu: %d\n", 63 lock, lock->magic, 64 owner ? owner->comm : "<none>", 65 owner ? task_pid_nr(owner) : -1, 66 lock->owner_cpu); 67 dump_stack(); 68 } 69 70 static void spin_bug(raw_spinlock_t *lock, const char *msg) 71 { 72 if (!debug_locks_off()) 73 return; 74 75 spin_dump(lock, msg); 76 } 77 78 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg) 79 80 static inline void 81 debug_spin_lock_before(raw_spinlock_t *lock) 82 { 83 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic"); 84 SPIN_BUG_ON(lock->owner == current, lock, "recursion"); 85 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(), 86 lock, "cpu recursion"); 87 } 88 89 static inline void debug_spin_lock_after(raw_spinlock_t *lock) 90 { 91 lock->owner_cpu = raw_smp_processor_id(); 92 lock->owner = current; 93 } 94 95 static inline void debug_spin_unlock(raw_spinlock_t *lock) 96 { 97 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic"); 98 SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked"); 99 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner"); 100 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(), 101 lock, "wrong CPU"); 102 lock->owner = SPINLOCK_OWNER_INIT; 103 lock->owner_cpu = -1; 104 } 105 106 /* 107 * We are now relying on the NMI watchdog to detect lockup instead of doing 108 * the detection here with an unfair lock which can cause problem of its own. 109 */ 110 void do_raw_spin_lock(raw_spinlock_t *lock) 111 { 112 debug_spin_lock_before(lock); 113 arch_spin_lock(&lock->raw_lock); 114 debug_spin_lock_after(lock); 115 } 116 117 int do_raw_spin_trylock(raw_spinlock_t *lock) 118 { 119 int ret = arch_spin_trylock(&lock->raw_lock); 120 121 if (ret) 122 debug_spin_lock_after(lock); 123 #ifndef CONFIG_SMP 124 /* 125 * Must not happen on UP: 126 */ 127 SPIN_BUG_ON(!ret, lock, "trylock failure on UP"); 128 #endif 129 return ret; 130 } 131 132 void do_raw_spin_unlock(raw_spinlock_t *lock) 133 { 134 debug_spin_unlock(lock); 135 arch_spin_unlock(&lock->raw_lock); 136 } 137 138 static void rwlock_bug(rwlock_t *lock, const char *msg) 139 { 140 if (!debug_locks_off()) 141 return; 142 143 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n", 144 msg, raw_smp_processor_id(), current->comm, 145 task_pid_nr(current), lock); 146 dump_stack(); 147 } 148 149 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg) 150 151 void do_raw_read_lock(rwlock_t *lock) 152 { 153 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); 154 arch_read_lock(&lock->raw_lock); 155 } 156 157 int do_raw_read_trylock(rwlock_t *lock) 158 { 159 int ret = arch_read_trylock(&lock->raw_lock); 160 161 #ifndef CONFIG_SMP 162 /* 163 * Must not happen on UP: 164 */ 165 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP"); 166 #endif 167 return ret; 168 } 169 170 void do_raw_read_unlock(rwlock_t *lock) 171 { 172 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); 173 arch_read_unlock(&lock->raw_lock); 174 } 175 176 static inline void debug_write_lock_before(rwlock_t *lock) 177 { 178 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); 179 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion"); 180 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(), 181 lock, "cpu recursion"); 182 } 183 184 static inline void debug_write_lock_after(rwlock_t *lock) 185 { 186 lock->owner_cpu = raw_smp_processor_id(); 187 lock->owner = current; 188 } 189 190 static inline void debug_write_unlock(rwlock_t *lock) 191 { 192 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); 193 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner"); 194 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(), 195 lock, "wrong CPU"); 196 lock->owner = SPINLOCK_OWNER_INIT; 197 lock->owner_cpu = -1; 198 } 199 200 void do_raw_write_lock(rwlock_t *lock) 201 { 202 debug_write_lock_before(lock); 203 arch_write_lock(&lock->raw_lock); 204 debug_write_lock_after(lock); 205 } 206 207 int do_raw_write_trylock(rwlock_t *lock) 208 { 209 int ret = arch_write_trylock(&lock->raw_lock); 210 211 if (ret) 212 debug_write_lock_after(lock); 213 #ifndef CONFIG_SMP 214 /* 215 * Must not happen on UP: 216 */ 217 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP"); 218 #endif 219 return ret; 220 } 221 222 void do_raw_write_unlock(rwlock_t *lock) 223 { 224 debug_write_unlock(lock); 225 arch_write_unlock(&lock->raw_lock); 226 } 227