2 * Copyright 2005, Red Hat, Inc., Ingo Molnar
3 * Released under the General Public License (GPL).
5 * This file contains the spinlock/rwlock implementations for
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 #include <linux/pid.h>
17 void __raw_spin_lock_init(raw_spinlock_t
*lock
, const char *name
,
18 struct lock_class_key
*key
, short inner
)
20 #ifdef CONFIG_DEBUG_LOCK_ALLOC
22 * Make sure we are not reinitializing a held lock:
24 debug_check_no_locks_freed((void *)lock
, sizeof(*lock
));
25 lockdep_init_map_wait(&lock
->dep_map
, name
, key
, 0, inner
);
27 lock
->raw_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
28 lock
->magic
= SPINLOCK_MAGIC
;
29 lock
->owner
= SPINLOCK_OWNER_INIT
;
33 EXPORT_SYMBOL(__raw_spin_lock_init
);
35 #ifndef CONFIG_PREEMPT_RT
36 void __rwlock_init(rwlock_t
*lock
, const char *name
,
37 struct lock_class_key
*key
)
39 #ifdef CONFIG_DEBUG_LOCK_ALLOC
41 * Make sure we are not reinitializing a held lock:
43 debug_check_no_locks_freed((void *)lock
, sizeof(*lock
));
44 lockdep_init_map_wait(&lock
->dep_map
, name
, key
, 0, LD_WAIT_CONFIG
);
46 lock
->raw_lock
= (arch_rwlock_t
) __ARCH_RW_LOCK_UNLOCKED
;
47 lock
->magic
= RWLOCK_MAGIC
;
48 lock
->owner
= SPINLOCK_OWNER_INIT
;
52 EXPORT_SYMBOL(__rwlock_init
);
55 static void spin_dump(raw_spinlock_t
*lock
, const char *msg
)
57 struct task_struct
*owner
= READ_ONCE(lock
->owner
);
59 if (owner
== SPINLOCK_OWNER_INIT
)
61 printk(KERN_EMERG
"BUG: spinlock %s on CPU#%d, %s/%d\n",
62 msg
, raw_smp_processor_id(),
63 current
->comm
, task_pid_nr(current
));
64 printk(KERN_EMERG
" lock: %pS, .magic: %08x, .owner: %s/%d, "
66 lock
, READ_ONCE(lock
->magic
),
67 owner
? owner
->comm
: "<none>",
68 owner
? task_pid_nr(owner
) : -1,
69 READ_ONCE(lock
->owner_cpu
));
73 static void spin_bug(raw_spinlock_t
*lock
, const char *msg
)
75 if (!debug_locks_off())
81 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
84 debug_spin_lock_before(raw_spinlock_t
*lock
)
86 SPIN_BUG_ON(READ_ONCE(lock
->magic
) != SPINLOCK_MAGIC
, lock
, "bad magic");
87 SPIN_BUG_ON(READ_ONCE(lock
->owner
) == current
, lock
, "recursion");
88 SPIN_BUG_ON(READ_ONCE(lock
->owner_cpu
) == raw_smp_processor_id(),
89 lock
, "cpu recursion");
92 static inline void debug_spin_lock_after(raw_spinlock_t
*lock
)
94 WRITE_ONCE(lock
->owner_cpu
, raw_smp_processor_id());
95 WRITE_ONCE(lock
->owner
, current
);
98 static inline void debug_spin_unlock(raw_spinlock_t
*lock
)
100 SPIN_BUG_ON(lock
->magic
!= SPINLOCK_MAGIC
, lock
, "bad magic");
101 SPIN_BUG_ON(!raw_spin_is_locked(lock
), lock
, "already unlocked");
102 SPIN_BUG_ON(lock
->owner
!= current
, lock
, "wrong owner");
103 SPIN_BUG_ON(lock
->owner_cpu
!= raw_smp_processor_id(),
105 WRITE_ONCE(lock
->owner
, SPINLOCK_OWNER_INIT
);
106 WRITE_ONCE(lock
->owner_cpu
, -1);
110 * We are now relying on the NMI watchdog to detect lockup instead of doing
111 * the detection here with an unfair lock which can cause problem of its own.
113 void do_raw_spin_lock(raw_spinlock_t
*lock
)
115 debug_spin_lock_before(lock
);
116 arch_spin_lock(&lock
->raw_lock
);
118 debug_spin_lock_after(lock
);
121 int do_raw_spin_trylock(raw_spinlock_t
*lock
)
123 int ret
= arch_spin_trylock(&lock
->raw_lock
);
127 debug_spin_lock_after(lock
);
131 * Must not happen on UP:
133 SPIN_BUG_ON(!ret
, lock
, "trylock failure on UP");
138 void do_raw_spin_unlock(raw_spinlock_t
*lock
)
140 mmiowb_spin_unlock();
141 debug_spin_unlock(lock
);
142 arch_spin_unlock(&lock
->raw_lock
);
145 #ifndef CONFIG_PREEMPT_RT
146 static void rwlock_bug(rwlock_t
*lock
, const char *msg
)
148 if (!debug_locks_off())
151 printk(KERN_EMERG
"BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
152 msg
, raw_smp_processor_id(), current
->comm
,
153 task_pid_nr(current
), lock
);
157 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
159 void do_raw_read_lock(rwlock_t
*lock
)
161 RWLOCK_BUG_ON(lock
->magic
!= RWLOCK_MAGIC
, lock
, "bad magic");
162 arch_read_lock(&lock
->raw_lock
);
165 int do_raw_read_trylock(rwlock_t
*lock
)
167 int ret
= arch_read_trylock(&lock
->raw_lock
);
171 * Must not happen on UP:
173 RWLOCK_BUG_ON(!ret
, lock
, "trylock failure on UP");
178 void do_raw_read_unlock(rwlock_t
*lock
)
180 RWLOCK_BUG_ON(lock
->magic
!= RWLOCK_MAGIC
, lock
, "bad magic");
181 arch_read_unlock(&lock
->raw_lock
);
184 static inline void debug_write_lock_before(rwlock_t
*lock
)
186 RWLOCK_BUG_ON(lock
->magic
!= RWLOCK_MAGIC
, lock
, "bad magic");
187 RWLOCK_BUG_ON(lock
->owner
== current
, lock
, "recursion");
188 RWLOCK_BUG_ON(lock
->owner_cpu
== raw_smp_processor_id(),
189 lock
, "cpu recursion");
192 static inline void debug_write_lock_after(rwlock_t
*lock
)
194 WRITE_ONCE(lock
->owner_cpu
, raw_smp_processor_id());
195 WRITE_ONCE(lock
->owner
, current
);
198 static inline void debug_write_unlock(rwlock_t
*lock
)
200 RWLOCK_BUG_ON(lock
->magic
!= RWLOCK_MAGIC
, lock
, "bad magic");
201 RWLOCK_BUG_ON(lock
->owner
!= current
, lock
, "wrong owner");
202 RWLOCK_BUG_ON(lock
->owner_cpu
!= raw_smp_processor_id(),
204 WRITE_ONCE(lock
->owner
, SPINLOCK_OWNER_INIT
);
205 WRITE_ONCE(lock
->owner_cpu
, -1);
208 void do_raw_write_lock(rwlock_t
*lock
)
210 debug_write_lock_before(lock
);
211 arch_write_lock(&lock
->raw_lock
);
212 debug_write_lock_after(lock
);
215 int do_raw_write_trylock(rwlock_t
*lock
)
217 int ret
= arch_write_trylock(&lock
->raw_lock
);
220 debug_write_lock_after(lock
);
223 * Must not happen on UP:
225 RWLOCK_BUG_ON(!ret
, lock
, "trylock failure on UP");
230 void do_raw_write_unlock(rwlock_t
*lock
)
232 debug_write_unlock(lock
);
233 arch_write_unlock(&lock
->raw_lock
);
236 #endif /* !CONFIG_PREEMPT_RT */