blob: 5f5eae59a1ac2182eaf4067d5303714f03b5555c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
16void __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
32EXPORT_SYMBOL(__raw_spin_lock_init);
33
34#ifndef CONFIG_PREEMPT_RT_FULL
35void __rwlock_init(rwlock_t *lock, const char *name,
36 struct lock_class_key *key)
37{
38#ifdef CONFIG_DEBUG_LOCK_ALLOC
39 /*
40 * Make sure we are not reinitializing a held lock:
41 */
42 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
43 lockdep_init_map(&lock->dep_map, name, key, 0);
44#endif
45 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
46 lock->magic = RWLOCK_MAGIC;
47 lock->owner = SPINLOCK_OWNER_INIT;
48 lock->owner_cpu = -1;
49}
50
51EXPORT_SYMBOL(__rwlock_init);
52#endif
53
54static void spin_dump(raw_spinlock_t *lock, const char *msg)
55{
56 struct task_struct *owner = NULL;
57
58 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
59 owner = lock->owner;
60 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
61 msg, raw_smp_processor_id(),
62 current->comm, task_pid_nr(current));
63 printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
64 ".owner_cpu: %d\n",
65 lock, lock->magic,
66 owner ? owner->comm : "<none>",
67 owner ? task_pid_nr(owner) : -1,
68 lock->owner_cpu);
69 dump_stack();
70}
71
72static void spin_bug(raw_spinlock_t *lock, const char *msg)
73{
74 if (!debug_locks_off())
75 return;
76
77 spin_dump(lock, msg);
78}
79
80#define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
81
82static inline void
83debug_spin_lock_before(raw_spinlock_t *lock)
84{
85 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
86 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
87 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
88 lock, "cpu recursion");
89}
90
91static inline void debug_spin_lock_after(raw_spinlock_t *lock)
92{
93 lock->owner_cpu = raw_smp_processor_id();
94 lock->owner = current;
95}
96
97static inline void debug_spin_unlock(raw_spinlock_t *lock)
98{
99 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
100 SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
101 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
102 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
103 lock, "wrong CPU");
104 lock->owner = SPINLOCK_OWNER_INIT;
105 lock->owner_cpu = -1;
106}
107
108static void __spin_lock_debug(raw_spinlock_t *lock)
109{
110 u64 i;
111 u64 loops = loops_per_jiffy * HZ;
112 int print_once = 1;
113
114 for (;;) {
115 for (i = 0; i < loops; i++) {
116 if (arch_spin_trylock(&lock->raw_lock))
117 return;
118 __delay(1);
119 }
120 /* lockup suspected: */
121 if (print_once) {
122 print_once = 0;
123 spin_dump(lock, "lockup");
124#ifdef CONFIG_SMP
125 trigger_all_cpu_backtrace();
126#endif
127 }
128 }
129}
130
131void do_raw_spin_lock(raw_spinlock_t *lock)
132{
133 debug_spin_lock_before(lock);
134 if (unlikely(!arch_spin_trylock(&lock->raw_lock)))
135 __spin_lock_debug(lock);
136 debug_spin_lock_after(lock);
137}
138
139int do_raw_spin_trylock(raw_spinlock_t *lock)
140{
141 int ret = arch_spin_trylock(&lock->raw_lock);
142
143 if (ret)
144 debug_spin_lock_after(lock);
145#ifndef CONFIG_SMP
146 /*
147 * Must not happen on UP:
148 */
149 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
150#endif
151 return ret;
152}
153
154void do_raw_spin_unlock(raw_spinlock_t *lock)
155{
156 debug_spin_unlock(lock);
157 arch_spin_unlock(&lock->raw_lock);
158}
159
160#ifndef CONFIG_PREEMPT_RT_FULL
161static void rwlock_bug(rwlock_t *lock, const char *msg)
162{
163 if (!debug_locks_off())
164 return;
165
166 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
167 msg, raw_smp_processor_id(), current->comm,
168 task_pid_nr(current), lock);
169 dump_stack();
170}
171
172#define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
173
174#if 0 /* __write_lock_debug() can lock up - maybe this can too? */
175static void __read_lock_debug(rwlock_t *lock)
176{
177 u64 i;
178 u64 loops = loops_per_jiffy * HZ;
179 int print_once = 1;
180
181 for (;;) {
182 for (i = 0; i < loops; i++) {
183 if (arch_read_trylock(&lock->raw_lock))
184 return;
185 __delay(1);
186 }
187 /* lockup suspected: */
188 if (print_once) {
189 print_once = 0;
190 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
191 "%s/%d, %p\n",
192 raw_smp_processor_id(), current->comm,
193 current->pid, lock);
194 dump_stack();
195 }
196 }
197}
198#endif
199
200void do_raw_read_lock(rwlock_t *lock)
201{
202 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
203 arch_read_lock(&lock->raw_lock);
204}
205
206int do_raw_read_trylock(rwlock_t *lock)
207{
208 int ret = arch_read_trylock(&lock->raw_lock);
209
210#ifndef CONFIG_SMP
211 /*
212 * Must not happen on UP:
213 */
214 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
215#endif
216 return ret;
217}
218
219void do_raw_read_unlock(rwlock_t *lock)
220{
221 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
222 arch_read_unlock(&lock->raw_lock);
223}
224
225static inline void debug_write_lock_before(rwlock_t *lock)
226{
227 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
228 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
229 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
230 lock, "cpu recursion");
231}
232
233static inline void debug_write_lock_after(rwlock_t *lock)
234{
235 lock->owner_cpu = raw_smp_processor_id();
236 lock->owner = current;
237}
238
239static inline void debug_write_unlock(rwlock_t *lock)
240{
241 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
242 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
243 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
244 lock, "wrong CPU");
245 lock->owner = SPINLOCK_OWNER_INIT;
246 lock->owner_cpu = -1;
247}
248
249#if 0 /* This can cause lockups */
250static void __write_lock_debug(rwlock_t *lock)
251{
252 u64 i;
253 u64 loops = loops_per_jiffy * HZ;
254 int print_once = 1;
255
256 for (;;) {
257 for (i = 0; i < loops; i++) {
258 if (arch_write_trylock(&lock->raw_lock))
259 return;
260 __delay(1);
261 }
262 /* lockup suspected: */
263 if (print_once) {
264 print_once = 0;
265 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
266 "%s/%d, %p\n",
267 raw_smp_processor_id(), current->comm,
268 current->pid, lock);
269 dump_stack();
270 }
271 }
272}
273#endif
274
275void do_raw_write_lock(rwlock_t *lock)
276{
277 debug_write_lock_before(lock);
278 arch_write_lock(&lock->raw_lock);
279 debug_write_lock_after(lock);
280}
281
282int do_raw_write_trylock(rwlock_t *lock)
283{
284 int ret = arch_write_trylock(&lock->raw_lock);
285
286 if (ret)
287 debug_write_lock_after(lock);
288#ifndef CONFIG_SMP
289 /*
290 * Must not happen on UP:
291 */
292 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
293#endif
294 return ret;
295}
296
297void do_raw_write_unlock(rwlock_t *lock)
298{
299 debug_write_unlock(lock);
300 arch_write_unlock(&lock->raw_lock);
301}
302
303#endif