blob: 14235671a1a706a195f772e7857f6107458b2ce8 [file] [log] [blame]
Ingo Molnarfb1c8f92005-09-10 00:25:56 -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
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07009#include <linux/spinlock.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010010#include <linux/nmi.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070011#include <linux/interrupt.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070012#include <linux/debug_locks.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070013#include <linux/delay.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050014#include <linux/export.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070015
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010016void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
Peter Zijlstrade8f5e42020-03-21 12:26:01 +010017 struct lock_class_key *key, short inner)
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070018{
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 Zijlstrade8f5e42020-03-21 12:26:01 +010024 lockdep_init_map_wait(&lock->dep_map, name, key, 0, inner);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070025#endif
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010026 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070027 lock->magic = SPINLOCK_MAGIC;
28 lock->owner = SPINLOCK_OWNER_INIT;
29 lock->owner_cpu = -1;
30}
31
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010032EXPORT_SYMBOL(__raw_spin_lock_init);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070033
Thomas Gleixner82829472021-08-15 23:28:28 +020034#ifndef CONFIG_PREEMPT_RT
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070035void __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));
Peter Zijlstrade8f5e42020-03-21 12:26:01 +010043 lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_CONFIG);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070044#endif
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +010045 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070046 lock->magic = RWLOCK_MAGIC;
47 lock->owner = SPINLOCK_OWNER_INIT;
48 lock->owner_cpu = -1;
49}
50
51EXPORT_SYMBOL(__rwlock_init);
Thomas Gleixner82829472021-08-15 23:28:28 +020052#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070053
Akinobu Mita4e101b02011-10-31 17:12:29 -070054static void spin_dump(raw_spinlock_t *lock, const char *msg)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070055{
Marco Elver1a365e82019-11-20 16:57:15 +010056 struct task_struct *owner = READ_ONCE(lock->owner);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070057
Marco Elver1a365e82019-11-20 16:57:15 +010058 if (owner == SPINLOCK_OWNER_INIT)
59 owner = NULL;
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070060 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
61 msg, raw_smp_processor_id(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070062 current->comm, task_pid_nr(current));
Stephen Boyd4b068142012-07-30 14:41:11 -070063 printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, "
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070064 ".owner_cpu: %d\n",
Marco Elver1a365e82019-11-20 16:57:15 +010065 lock, READ_ONCE(lock->magic),
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070066 owner ? owner->comm : "<none>",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070067 owner ? task_pid_nr(owner) : -1,
Marco Elver1a365e82019-11-20 16:57:15 +010068 READ_ONCE(lock->owner_cpu));
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070069 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070070}
71
Akinobu Mita4e101b02011-10-31 17:12:29 -070072static 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
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070080#define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
81
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070082static inline void
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010083debug_spin_lock_before(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070084{
Marco Elver1a365e82019-11-20 16:57:15 +010085 SPIN_BUG_ON(READ_ONCE(lock->magic) != SPINLOCK_MAGIC, lock, "bad magic");
86 SPIN_BUG_ON(READ_ONCE(lock->owner) == current, lock, "recursion");
87 SPIN_BUG_ON(READ_ONCE(lock->owner_cpu) == raw_smp_processor_id(),
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070088 lock, "cpu recursion");
89}
90
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010091static inline void debug_spin_lock_after(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070092{
Marco Elver1a365e82019-11-20 16:57:15 +010093 WRITE_ONCE(lock->owner_cpu, raw_smp_processor_id());
94 WRITE_ONCE(lock->owner, current);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070095}
96
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010097static inline void debug_spin_unlock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070098{
99 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +0100100 SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700101 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
102 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
103 lock, "wrong CPU");
Marco Elver1a365e82019-11-20 16:57:15 +0100104 WRITE_ONCE(lock->owner, SPINLOCK_OWNER_INIT);
105 WRITE_ONCE(lock->owner_cpu, -1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700106}
107
Waiman Longbc88c102017-02-08 14:46:48 -0500108/*
109 * We are now relying on the NMI watchdog to detect lockup instead of doing
110 * the detection here with an unfair lock which can cause problem of its own.
111 */
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100112void do_raw_spin_lock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700113{
114 debug_spin_lock_before(lock);
Waiman Longbc88c102017-02-08 14:46:48 -0500115 arch_spin_lock(&lock->raw_lock);
Will Deacon60ca1e52019-02-22 12:59:59 +0000116 mmiowb_spin_lock();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700117 debug_spin_lock_after(lock);
118}
119
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100120int do_raw_spin_trylock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700121{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100122 int ret = arch_spin_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700123
Will Deacon60ca1e52019-02-22 12:59:59 +0000124 if (ret) {
125 mmiowb_spin_lock();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700126 debug_spin_lock_after(lock);
Will Deacon60ca1e52019-02-22 12:59:59 +0000127 }
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700128#ifndef CONFIG_SMP
129 /*
130 * Must not happen on UP:
131 */
132 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
133#endif
134 return ret;
135}
136
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100137void do_raw_spin_unlock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700138{
Will Deacon60ca1e52019-02-22 12:59:59 +0000139 mmiowb_spin_unlock();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700140 debug_spin_unlock(lock);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100141 arch_spin_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700142}
143
Thomas Gleixner82829472021-08-15 23:28:28 +0200144#ifndef CONFIG_PREEMPT_RT
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700145static void rwlock_bug(rwlock_t *lock, const char *msg)
146{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700147 if (!debug_locks_off())
148 return;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700149
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700150 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
151 msg, raw_smp_processor_id(), current->comm,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700152 task_pid_nr(current), lock);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700153 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700154}
155
156#define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
157
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100158void do_raw_read_lock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700159{
160 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnere5931942009-12-03 20:08:46 +0100161 arch_read_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700162}
163
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100164int do_raw_read_trylock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700165{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100166 int ret = arch_read_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700167
168#ifndef CONFIG_SMP
169 /*
170 * Must not happen on UP:
171 */
172 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
173#endif
174 return ret;
175}
176
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100177void do_raw_read_unlock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700178{
179 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnere5931942009-12-03 20:08:46 +0100180 arch_read_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700181}
182
183static inline void debug_write_lock_before(rwlock_t *lock)
184{
185 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
186 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
187 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
188 lock, "cpu recursion");
189}
190
191static inline void debug_write_lock_after(rwlock_t *lock)
192{
Marco Elver1a365e82019-11-20 16:57:15 +0100193 WRITE_ONCE(lock->owner_cpu, raw_smp_processor_id());
194 WRITE_ONCE(lock->owner, current);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700195}
196
197static inline void debug_write_unlock(rwlock_t *lock)
198{
199 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
200 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
201 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
202 lock, "wrong CPU");
Marco Elver1a365e82019-11-20 16:57:15 +0100203 WRITE_ONCE(lock->owner, SPINLOCK_OWNER_INIT);
204 WRITE_ONCE(lock->owner_cpu, -1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700205}
206
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100207void do_raw_write_lock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700208{
209 debug_write_lock_before(lock);
Thomas Gleixnere5931942009-12-03 20:08:46 +0100210 arch_write_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700211 debug_write_lock_after(lock);
212}
213
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100214int do_raw_write_trylock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700215{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100216 int ret = arch_write_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700217
218 if (ret)
219 debug_write_lock_after(lock);
220#ifndef CONFIG_SMP
221 /*
222 * Must not happen on UP:
223 */
224 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
225#endif
226 return ret;
227}
228
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100229void do_raw_write_unlock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700230{
231 debug_write_unlock(lock);
Thomas Gleixnere5931942009-12-03 20:08:46 +0100232 arch_write_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700233}
Thomas Gleixner82829472021-08-15 23:28:28 +0200234
235#endif /* !CONFIG_PREEMPT_RT */