2 * Copyright (2004) Linus Torvalds
4 * Author: Zwane Mwaikambo <zwane@fsmlabs.com>
6 * Copyright (2004, 2005) Ingo Molnar
8 * This file contains the spinlock/rwlock implementations for the
9 * SMP and the DEBUG_SPINLOCK cases. (UP-nondebug inlines them)
11 * Note that some architectures have special knowledge about the
12 * stack frames of these functions in their profile_pc. If you
13 * change anything significant here that could change the stack
14 * frame contact the architecture maintainers.
17 #include <linux/linkage.h>
18 #include <linux/preempt.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <linux/debug_locks.h>
22 #include <linux/export.h>
25 * If lockdep is enabled then we use the non-preemption spin-ops
26 * even on CONFIG_PREEMPT, because lockdep assumes that interrupts are
27 * not re-enabled during lock-acquire (which the preempt-spin-ops do):
29 #if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC)
31 * The __lock_function inlines are taken from
32 * include/linux/spinlock_api_smp.h
35 #define raw_read_can_lock(l) read_can_lock(l)
36 #define raw_write_can_lock(l) write_can_lock(l)
39 * Some architectures can relax in favour of the CPU owning the lock.
41 #ifndef arch_read_relax
42 # define arch_read_relax(l) cpu_relax()
44 #ifndef arch_write_relax
45 # define arch_write_relax(l) cpu_relax()
47 #ifndef arch_spin_relax
48 # define arch_spin_relax(l) cpu_relax()
52 * We build the __lock_function inlines here. They are too large for
53 * inlining all over the place, but here is only one user per function
54 * which embedds them into the calling _lock_function below.
56 * This could be a long-held lock. We both prepare to spin for a long
57 * time (making _this_ CPU preemptable if possible), and we also signal
58 * towards that other CPU that it should break the lock ASAP.
60 #define BUILD_LOCK_OPS(op, locktype) \
61 void __lockfunc __raw_##op##_lock(locktype##_t *lock) \
65 if (likely(do_raw_##op##_trylock(lock))) \
69 if (!(lock)->break_lock) \
70 (lock)->break_lock = 1; \
71 while (!raw_##op##_can_lock(lock) && (lock)->break_lock)\
72 arch_##op##_relax(&lock->raw_lock); \
74 (lock)->break_lock = 0; \
77 unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \
79 unsigned long flags; \
83 local_irq_save(flags); \
84 if (likely(do_raw_##op##_trylock(lock))) \
86 local_irq_restore(flags); \
89 if (!(lock)->break_lock) \
90 (lock)->break_lock = 1; \
91 while (!raw_##op##_can_lock(lock) && (lock)->break_lock)\
92 arch_##op##_relax(&lock->raw_lock); \
94 (lock)->break_lock = 0; \
98 void __lockfunc __raw_##op##_lock_irq(locktype##_t *lock) \
100 _raw_##op##_lock_irqsave(lock); \
103 void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
105 unsigned long flags; \
108 /* Careful: we must exclude softirqs too, hence the */ \
109 /* irq-disabling. We use the generic preemption-aware */ \
112 flags = _raw_##op##_lock_irqsave(lock); \
113 local_bh_disable(); \
114 local_irq_restore(flags); \
118 * Build preemption-friendly versions of the following
119 * lock-spinning functions:
121 * __[spin|read|write]_lock()
122 * __[spin|read|write]_lock_irq()
123 * __[spin|read|write]_lock_irqsave()
124 * __[spin|read|write]_lock_bh()
126 BUILD_LOCK_OPS(spin, raw_spinlock);
127 BUILD_LOCK_OPS(read, rwlock);
128 BUILD_LOCK_OPS(write, rwlock);
132 #ifndef CONFIG_INLINE_SPIN_TRYLOCK
133 int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock)
135 return __raw_spin_trylock(lock);
137 EXPORT_SYMBOL(_raw_spin_trylock);
140 #ifndef CONFIG_INLINE_SPIN_TRYLOCK_BH
141 int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock)
143 return __raw_spin_trylock_bh(lock);
145 EXPORT_SYMBOL(_raw_spin_trylock_bh);
148 #ifndef CONFIG_INLINE_SPIN_LOCK
149 void __lockfunc _raw_spin_lock(raw_spinlock_t *lock)
151 __raw_spin_lock(lock);
153 EXPORT_SYMBOL(_raw_spin_lock);
156 #ifndef CONFIG_INLINE_SPIN_LOCK_IRQSAVE
157 unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock)
159 return __raw_spin_lock_irqsave(lock);
161 EXPORT_SYMBOL(_raw_spin_lock_irqsave);
164 #ifndef CONFIG_INLINE_SPIN_LOCK_IRQ
165 void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock)
167 __raw_spin_lock_irq(lock);
169 EXPORT_SYMBOL(_raw_spin_lock_irq);
172 #ifndef CONFIG_INLINE_SPIN_LOCK_BH
173 void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock)
175 __raw_spin_lock_bh(lock);
177 EXPORT_SYMBOL(_raw_spin_lock_bh);
180 #ifdef CONFIG_UNINLINE_SPIN_UNLOCK
181 void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock)
183 __raw_spin_unlock(lock);
185 EXPORT_SYMBOL(_raw_spin_unlock);
188 #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE
189 void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
191 __raw_spin_unlock_irqrestore(lock, flags);
193 EXPORT_SYMBOL(_raw_spin_unlock_irqrestore);
196 #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ
197 void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock)
199 __raw_spin_unlock_irq(lock);
201 EXPORT_SYMBOL(_raw_spin_unlock_irq);
204 #ifndef CONFIG_INLINE_SPIN_UNLOCK_BH
205 void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
207 __raw_spin_unlock_bh(lock);
209 EXPORT_SYMBOL(_raw_spin_unlock_bh);
212 #ifndef CONFIG_INLINE_READ_TRYLOCK
213 int __lockfunc _raw_read_trylock(rwlock_t *lock)
215 return __raw_read_trylock(lock);
217 EXPORT_SYMBOL(_raw_read_trylock);
220 #ifndef CONFIG_INLINE_READ_LOCK
221 void __lockfunc _raw_read_lock(rwlock_t *lock)
223 __raw_read_lock(lock);
225 EXPORT_SYMBOL(_raw_read_lock);
228 #ifndef CONFIG_INLINE_READ_LOCK_IRQSAVE
229 unsigned long __lockfunc _raw_read_lock_irqsave(rwlock_t *lock)
231 return __raw_read_lock_irqsave(lock);
233 EXPORT_SYMBOL(_raw_read_lock_irqsave);
236 #ifndef CONFIG_INLINE_READ_LOCK_IRQ
237 void __lockfunc _raw_read_lock_irq(rwlock_t *lock)
239 __raw_read_lock_irq(lock);
241 EXPORT_SYMBOL(_raw_read_lock_irq);
244 #ifndef CONFIG_INLINE_READ_LOCK_BH
245 void __lockfunc _raw_read_lock_bh(rwlock_t *lock)
247 __raw_read_lock_bh(lock);
249 EXPORT_SYMBOL(_raw_read_lock_bh);
252 #ifndef CONFIG_INLINE_READ_UNLOCK
253 void __lockfunc _raw_read_unlock(rwlock_t *lock)
255 __raw_read_unlock(lock);
257 EXPORT_SYMBOL(_raw_read_unlock);
260 #ifndef CONFIG_INLINE_READ_UNLOCK_IRQRESTORE
261 void __lockfunc _raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
263 __raw_read_unlock_irqrestore(lock, flags);
265 EXPORT_SYMBOL(_raw_read_unlock_irqrestore);
268 #ifndef CONFIG_INLINE_READ_UNLOCK_IRQ
269 void __lockfunc _raw_read_unlock_irq(rwlock_t *lock)
271 __raw_read_unlock_irq(lock);
273 EXPORT_SYMBOL(_raw_read_unlock_irq);
276 #ifndef CONFIG_INLINE_READ_UNLOCK_BH
277 void __lockfunc _raw_read_unlock_bh(rwlock_t *lock)
279 __raw_read_unlock_bh(lock);
281 EXPORT_SYMBOL(_raw_read_unlock_bh);
284 #ifndef CONFIG_INLINE_WRITE_TRYLOCK
285 int __lockfunc _raw_write_trylock(rwlock_t *lock)
287 return __raw_write_trylock(lock);
289 EXPORT_SYMBOL(_raw_write_trylock);
292 #ifndef CONFIG_INLINE_WRITE_LOCK
293 void __lockfunc _raw_write_lock(rwlock_t *lock)
295 __raw_write_lock(lock);
297 EXPORT_SYMBOL(_raw_write_lock);
300 #ifndef CONFIG_INLINE_WRITE_LOCK_IRQSAVE
301 unsigned long __lockfunc _raw_write_lock_irqsave(rwlock_t *lock)
303 return __raw_write_lock_irqsave(lock);
305 EXPORT_SYMBOL(_raw_write_lock_irqsave);
308 #ifndef CONFIG_INLINE_WRITE_LOCK_IRQ
309 void __lockfunc _raw_write_lock_irq(rwlock_t *lock)
311 __raw_write_lock_irq(lock);
313 EXPORT_SYMBOL(_raw_write_lock_irq);
316 #ifndef CONFIG_INLINE_WRITE_LOCK_BH
317 void __lockfunc _raw_write_lock_bh(rwlock_t *lock)
319 __raw_write_lock_bh(lock);
321 EXPORT_SYMBOL(_raw_write_lock_bh);
324 #ifndef CONFIG_INLINE_WRITE_UNLOCK
325 void __lockfunc _raw_write_unlock(rwlock_t *lock)
327 __raw_write_unlock(lock);
329 EXPORT_SYMBOL(_raw_write_unlock);
332 #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE
333 void __lockfunc _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
335 __raw_write_unlock_irqrestore(lock, flags);
337 EXPORT_SYMBOL(_raw_write_unlock_irqrestore);
340 #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQ
341 void __lockfunc _raw_write_unlock_irq(rwlock_t *lock)
343 __raw_write_unlock_irq(lock);
345 EXPORT_SYMBOL(_raw_write_unlock_irq);
348 #ifndef CONFIG_INLINE_WRITE_UNLOCK_BH
349 void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
351 __raw_write_unlock_bh(lock);
353 EXPORT_SYMBOL(_raw_write_unlock_bh);
356 #ifdef CONFIG_DEBUG_LOCK_ALLOC
358 void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
361 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
362 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
364 EXPORT_SYMBOL(_raw_spin_lock_nested);
366 void __lockfunc _raw_spin_lock_bh_nested(raw_spinlock_t *lock, int subclass)
368 __local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
369 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
370 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
372 EXPORT_SYMBOL(_raw_spin_lock_bh_nested);
374 unsigned long __lockfunc _raw_spin_lock_irqsave_nested(raw_spinlock_t *lock,
379 local_irq_save(flags);
381 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
382 LOCK_CONTENDED_FLAGS(lock, do_raw_spin_trylock, do_raw_spin_lock,
383 do_raw_spin_lock_flags, &flags);
386 EXPORT_SYMBOL(_raw_spin_lock_irqsave_nested);
388 void __lockfunc _raw_spin_lock_nest_lock(raw_spinlock_t *lock,
389 struct lockdep_map *nest_lock)
392 spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_);
393 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
395 EXPORT_SYMBOL(_raw_spin_lock_nest_lock);
399 notrace int in_lock_functions(unsigned long addr)
401 /* Linker adds these: start and end of __lockfunc functions */
402 extern char __lock_text_start[], __lock_text_end[];
404 return addr >= (unsigned long)__lock_text_start
405 && addr < (unsigned long)__lock_text_end;
407 EXPORT_SYMBOL(in_lock_functions);