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> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 14 | #include <linux/export.h> |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 15 | |
Thomas Gleixner | c2f21ce | 2009-12-02 20:02:59 +0100 | [diff] [blame] | 16 | void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name, |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 17 | struct lock_class_key *key, short inner) |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 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 | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 24 | lockdep_init_map_wait(&lock->dep_map, name, key, 0, inner); |
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 | |
Thomas Gleixner | c2f21ce | 2009-12-02 20:02:59 +0100 | [diff] [blame] | 32 | EXPORT_SYMBOL(__raw_spin_lock_init); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 33 | |
Thomas Gleixner | 8282947 | 2021-08-15 23:28:28 +0200 | [diff] [blame] | 34 | #ifndef CONFIG_PREEMPT_RT |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 35 | void __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 Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 43 | lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_CONFIG); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 44 | #endif |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 45 | lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED; |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 46 | lock->magic = RWLOCK_MAGIC; |
| 47 | lock->owner = SPINLOCK_OWNER_INIT; |
| 48 | lock->owner_cpu = -1; |
| 49 | } |
| 50 | |
| 51 | EXPORT_SYMBOL(__rwlock_init); |
Thomas Gleixner | 8282947 | 2021-08-15 23:28:28 +0200 | [diff] [blame] | 52 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 53 | |
Akinobu Mita | 4e101b0 | 2011-10-31 17:12:29 -0700 | [diff] [blame] | 54 | static void spin_dump(raw_spinlock_t *lock, const char *msg) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 55 | { |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 56 | struct task_struct *owner = READ_ONCE(lock->owner); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 57 | |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 58 | if (owner == SPINLOCK_OWNER_INIT) |
| 59 | owner = NULL; |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 60 | printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n", |
| 61 | msg, raw_smp_processor_id(), |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 62 | current->comm, task_pid_nr(current)); |
Stephen Boyd | 4b06814 | 2012-07-30 14:41:11 -0700 | [diff] [blame] | 63 | printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, " |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 64 | ".owner_cpu: %d\n", |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 65 | lock, READ_ONCE(lock->magic), |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 66 | owner ? owner->comm : "<none>", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 67 | owner ? task_pid_nr(owner) : -1, |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 68 | READ_ONCE(lock->owner_cpu)); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 69 | dump_stack(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Akinobu Mita | 4e101b0 | 2011-10-31 17:12:29 -0700 | [diff] [blame] | 72 | static 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 Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 80 | #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg) |
| 81 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 82 | static inline void |
Thomas Gleixner | c2f21ce | 2009-12-02 20:02:59 +0100 | [diff] [blame] | 83 | debug_spin_lock_before(raw_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 84 | { |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 85 | 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 Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 88 | lock, "cpu recursion"); |
| 89 | } |
| 90 | |
Thomas Gleixner | c2f21ce | 2009-12-02 20:02:59 +0100 | [diff] [blame] | 91 | static inline void debug_spin_lock_after(raw_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 92 | { |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 93 | WRITE_ONCE(lock->owner_cpu, raw_smp_processor_id()); |
| 94 | WRITE_ONCE(lock->owner, current); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Thomas Gleixner | c2f21ce | 2009-12-02 20:02:59 +0100 | [diff] [blame] | 97 | static inline void debug_spin_unlock(raw_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 98 | { |
| 99 | SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic"); |
Thomas Gleixner | c2f21ce | 2009-12-02 20:02:59 +0100 | [diff] [blame] | 100 | SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked"); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 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"); |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 104 | WRITE_ONCE(lock->owner, SPINLOCK_OWNER_INIT); |
| 105 | WRITE_ONCE(lock->owner_cpu, -1); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Waiman Long | bc88c10 | 2017-02-08 14:46:48 -0500 | [diff] [blame] | 108 | /* |
| 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 Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 112 | void do_raw_spin_lock(raw_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 113 | { |
| 114 | debug_spin_lock_before(lock); |
Waiman Long | bc88c10 | 2017-02-08 14:46:48 -0500 | [diff] [blame] | 115 | arch_spin_lock(&lock->raw_lock); |
Will Deacon | 60ca1e5 | 2019-02-22 12:59:59 +0000 | [diff] [blame] | 116 | mmiowb_spin_lock(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 117 | debug_spin_lock_after(lock); |
| 118 | } |
| 119 | |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 120 | int do_raw_spin_trylock(raw_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 121 | { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 122 | int ret = arch_spin_trylock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 123 | |
Will Deacon | 60ca1e5 | 2019-02-22 12:59:59 +0000 | [diff] [blame] | 124 | if (ret) { |
| 125 | mmiowb_spin_lock(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 126 | debug_spin_lock_after(lock); |
Will Deacon | 60ca1e5 | 2019-02-22 12:59:59 +0000 | [diff] [blame] | 127 | } |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 128 | #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 Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 137 | void do_raw_spin_unlock(raw_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 138 | { |
Will Deacon | 60ca1e5 | 2019-02-22 12:59:59 +0000 | [diff] [blame] | 139 | mmiowb_spin_unlock(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 140 | debug_spin_unlock(lock); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 141 | arch_spin_unlock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Thomas Gleixner | 8282947 | 2021-08-15 23:28:28 +0200 | [diff] [blame] | 144 | #ifndef CONFIG_PREEMPT_RT |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 145 | static void rwlock_bug(rwlock_t *lock, const char *msg) |
| 146 | { |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 147 | if (!debug_locks_off()) |
| 148 | return; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 149 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 150 | printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n", |
| 151 | msg, raw_smp_processor_id(), current->comm, |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 152 | task_pid_nr(current), lock); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 153 | dump_stack(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg) |
| 157 | |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 158 | void do_raw_read_lock(rwlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 159 | { |
| 160 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 161 | arch_read_lock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 164 | int do_raw_read_trylock(rwlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 165 | { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 166 | int ret = arch_read_trylock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 167 | |
| 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 Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 177 | void do_raw_read_unlock(rwlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 178 | { |
| 179 | RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic"); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 180 | arch_read_unlock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static 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 | |
| 191 | static inline void debug_write_lock_after(rwlock_t *lock) |
| 192 | { |
Marco Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 193 | WRITE_ONCE(lock->owner_cpu, raw_smp_processor_id()); |
| 194 | WRITE_ONCE(lock->owner, current); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static 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 Elver | 1a365e8 | 2019-11-20 16:57:15 +0100 | [diff] [blame] | 203 | WRITE_ONCE(lock->owner, SPINLOCK_OWNER_INIT); |
| 204 | WRITE_ONCE(lock->owner_cpu, -1); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 207 | void do_raw_write_lock(rwlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 208 | { |
| 209 | debug_write_lock_before(lock); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 210 | arch_write_lock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 211 | debug_write_lock_after(lock); |
| 212 | } |
| 213 | |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 214 | int do_raw_write_trylock(rwlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 215 | { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 216 | int ret = arch_write_trylock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 217 | |
| 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 Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 229 | void do_raw_write_unlock(rwlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 230 | { |
| 231 | debug_write_unlock(lock); |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 232 | arch_write_unlock(&lock->raw_lock); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 233 | } |
Thomas Gleixner | 8282947 | 2021-08-15 23:28:28 +0200 | [diff] [blame] | 234 | |
| 235 | #endif /* !CONFIG_PREEMPT_RT */ |