]>
Commit | Line | Data |
---|---|---|
fb1c8f93 IM |
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 | ||
fb1c8f93 | 9 | #include <linux/spinlock.h> |
bb81a09e | 10 | #include <linux/nmi.h> |
fb1c8f93 | 11 | #include <linux/interrupt.h> |
9a11b49a | 12 | #include <linux/debug_locks.h> |
fb1c8f93 | 13 | #include <linux/delay.h> |
8bc3bcc9 | 14 | #include <linux/export.h> |
fb1c8f93 | 15 | |
c2f21ce2 TG |
16 | void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name, |
17 | struct lock_class_key *key) | |
8a25d5de IM |
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)); | |
4dfbb9d8 | 24 | lockdep_init_map(&lock->dep_map, name, key, 0); |
8a25d5de | 25 | #endif |
edc35bd7 | 26 | lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
8a25d5de IM |
27 | lock->magic = SPINLOCK_MAGIC; |
28 | lock->owner = SPINLOCK_OWNER_INIT; | |
29 | lock->owner_cpu = -1; | |
30 | } | |
31 | ||
c2f21ce2 | 32 | EXPORT_SYMBOL(__raw_spin_lock_init); |
8a25d5de IM |
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)); | |
4dfbb9d8 | 42 | lockdep_init_map(&lock->dep_map, name, key, 0); |
8a25d5de | 43 | #endif |
fb3a6bbc | 44 | lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED; |
8a25d5de IM |
45 | lock->magic = RWLOCK_MAGIC; |
46 | lock->owner = SPINLOCK_OWNER_INIT; | |
47 | lock->owner_cpu = -1; | |
48 | } | |
49 | ||
50 | EXPORT_SYMBOL(__rwlock_init); | |
51 | ||
4e101b0e | 52 | static void spin_dump(raw_spinlock_t *lock, const char *msg) |
fb1c8f93 | 53 | { |
fb1c8f93 IM |
54 | struct task_struct *owner = NULL; |
55 | ||
9a11b49a IM |
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(), | |
ba25f9dc | 60 | current->comm, task_pid_nr(current)); |
4b068148 | 61 | printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, " |
9a11b49a IM |
62 | ".owner_cpu: %d\n", |
63 | lock, lock->magic, | |
64 | owner ? owner->comm : "<none>", | |
ba25f9dc | 65 | owner ? task_pid_nr(owner) : -1, |
9a11b49a IM |
66 | lock->owner_cpu); |
67 | dump_stack(); | |
fb1c8f93 IM |
68 | } |
69 | ||
4e101b0e AM |
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 | ||
fb1c8f93 IM |
78 | #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg) |
79 | ||
9a11b49a | 80 | static inline void |
c2f21ce2 | 81 | debug_spin_lock_before(raw_spinlock_t *lock) |
fb1c8f93 IM |
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 | ||
c2f21ce2 | 89 | static inline void debug_spin_lock_after(raw_spinlock_t *lock) |
fb1c8f93 IM |
90 | { |
91 | lock->owner_cpu = raw_smp_processor_id(); | |
92 | lock->owner = current; | |
93 | } | |
94 | ||
c2f21ce2 | 95 | static inline void debug_spin_unlock(raw_spinlock_t *lock) |
fb1c8f93 IM |
96 | { |
97 | SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic"); | |
c2f21ce2 | 98 | SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked"); |
fb1c8f93 IM |
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 | ||
bc88c10d WL |
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 | */ | |
9828ea9d | 110 | void do_raw_spin_lock(raw_spinlock_t *lock) |
fb1c8f93 IM |
111 | { |
112 | debug_spin_lock_before(lock); | |
bc88c10d | 113 | arch_spin_lock(&lock->raw_lock); |
60ca1e5a | 114 | mmiowb_spin_lock(); |
fb1c8f93 IM |
115 | debug_spin_lock_after(lock); |
116 | } | |
117 | ||
9828ea9d | 118 | int do_raw_spin_trylock(raw_spinlock_t *lock) |
fb1c8f93 | 119 | { |
0199c4e6 | 120 | int ret = arch_spin_trylock(&lock->raw_lock); |
fb1c8f93 | 121 | |
60ca1e5a WD |
122 | if (ret) { |
123 | mmiowb_spin_lock(); | |
fb1c8f93 | 124 | debug_spin_lock_after(lock); |
60ca1e5a | 125 | } |
fb1c8f93 IM |
126 | #ifndef CONFIG_SMP |
127 | /* | |
128 | * Must not happen on UP: | |
129 | */ | |
130 | SPIN_BUG_ON(!ret, lock, "trylock failure on UP"); | |
131 | #endif | |
132 | return ret; | |
133 | } | |
134 | ||
9828ea9d | 135 | void do_raw_spin_unlock(raw_spinlock_t *lock) |
fb1c8f93 | 136 | { |
60ca1e5a | 137 | mmiowb_spin_unlock(); |
fb1c8f93 | 138 | debug_spin_unlock(lock); |
0199c4e6 | 139 | arch_spin_unlock(&lock->raw_lock); |
fb1c8f93 IM |
140 | } |
141 | ||
142 | static void rwlock_bug(rwlock_t *lock, const char *msg) | |
143 | { | |
9a11b49a IM |
144 | if (!debug_locks_off()) |
145 | return; | |
146 | ||
147 | printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n", | |
148 | msg, raw_smp_processor_id(), current->comm, | |
ba25f9dc | 149 | task_pid_nr(current), lock); |
9a11b49a | 150 | dump_stack(); |
fb1c8f93 IM |
151 | } |
152 | ||
153 | #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg) | |
154 | ||
9828ea9d | 155 | void do_raw_read_lock(rwlock_t *lock) |
fb1c8f93 IM |
156 | { |
157 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); | |
e5931943 | 158 | arch_read_lock(&lock->raw_lock); |
fb1c8f93 IM |
159 | } |
160 | ||
9828ea9d | 161 | int do_raw_read_trylock(rwlock_t *lock) |
fb1c8f93 | 162 | { |
e5931943 | 163 | int ret = arch_read_trylock(&lock->raw_lock); |
fb1c8f93 IM |
164 | |
165 | #ifndef CONFIG_SMP | |
166 | /* | |
167 | * Must not happen on UP: | |
168 | */ | |
169 | RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP"); | |
170 | #endif | |
171 | return ret; | |
172 | } | |
173 | ||
9828ea9d | 174 | void do_raw_read_unlock(rwlock_t *lock) |
fb1c8f93 IM |
175 | { |
176 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); | |
e5931943 | 177 | arch_read_unlock(&lock->raw_lock); |
fb1c8f93 IM |
178 | } |
179 | ||
180 | static inline void debug_write_lock_before(rwlock_t *lock) | |
181 | { | |
182 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); | |
183 | RWLOCK_BUG_ON(lock->owner == current, lock, "recursion"); | |
184 | RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(), | |
185 | lock, "cpu recursion"); | |
186 | } | |
187 | ||
188 | static inline void debug_write_lock_after(rwlock_t *lock) | |
189 | { | |
190 | lock->owner_cpu = raw_smp_processor_id(); | |
191 | lock->owner = current; | |
192 | } | |
193 | ||
194 | static inline void debug_write_unlock(rwlock_t *lock) | |
195 | { | |
196 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); | |
197 | RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner"); | |
198 | RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(), | |
199 | lock, "wrong CPU"); | |
200 | lock->owner = SPINLOCK_OWNER_INIT; | |
201 | lock->owner_cpu = -1; | |
202 | } | |
203 | ||
9828ea9d | 204 | void do_raw_write_lock(rwlock_t *lock) |
fb1c8f93 IM |
205 | { |
206 | debug_write_lock_before(lock); | |
e5931943 | 207 | arch_write_lock(&lock->raw_lock); |
fb1c8f93 IM |
208 | debug_write_lock_after(lock); |
209 | } | |
210 | ||
9828ea9d | 211 | int do_raw_write_trylock(rwlock_t *lock) |
fb1c8f93 | 212 | { |
e5931943 | 213 | int ret = arch_write_trylock(&lock->raw_lock); |
fb1c8f93 IM |
214 | |
215 | if (ret) | |
216 | debug_write_lock_after(lock); | |
217 | #ifndef CONFIG_SMP | |
218 | /* | |
219 | * Must not happen on UP: | |
220 | */ | |
221 | RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP"); | |
222 | #endif | |
223 | return ret; | |
224 | } | |
225 | ||
9828ea9d | 226 | void do_raw_write_unlock(rwlock_t *lock) |
fb1c8f93 IM |
227 | { |
228 | debug_write_unlock(lock); | |
e5931943 | 229 | arch_write_unlock(&lock->raw_lock); |
fb1c8f93 | 230 | } |