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/module.h>
24 int __lockfunc _spin_trylock(spinlock_t *lock)
27 if (_raw_spin_trylock(lock)) {
28 spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
35 EXPORT_SYMBOL(_spin_trylock);
37 int __lockfunc _read_trylock(rwlock_t *lock)
40 if (_raw_read_trylock(lock)) {
41 rwlock_acquire_read(&lock->dep_map, 0, 1, _RET_IP_);
48 EXPORT_SYMBOL(_read_trylock);
50 int __lockfunc _write_trylock(rwlock_t *lock)
53 if (_raw_write_trylock(lock)) {
54 rwlock_acquire(&lock->dep_map, 0, 1, _RET_IP_);
61 EXPORT_SYMBOL(_write_trylock);
64 * If lockdep is enabled then we use the non-preemption spin-ops
65 * even on CONFIG_PREEMPT, because lockdep assumes that interrupts are
66 * not re-enabled during lock-acquire (which the preempt-spin-ops do):
68 #if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC)
70 void __lockfunc _read_lock(rwlock_t *lock)
73 rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_);
74 LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock);
76 EXPORT_SYMBOL(_read_lock);
78 unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock)
82 local_irq_save(flags);
84 spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
86 * On lockdep we dont want the hand-coded irq-enable of
87 * _raw_spin_lock_flags() code, because lockdep assumes
88 * that interrupts are not re-enabled during lock-acquire:
91 LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
93 _raw_spin_lock_flags(lock, &flags);
97 EXPORT_SYMBOL(_spin_lock_irqsave);
99 void __lockfunc _spin_lock_irq(spinlock_t *lock)
103 spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
104 LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
106 EXPORT_SYMBOL(_spin_lock_irq);
108 void __lockfunc _spin_lock_bh(spinlock_t *lock)
112 spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
113 LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
115 EXPORT_SYMBOL(_spin_lock_bh);
117 unsigned long __lockfunc _read_lock_irqsave(rwlock_t *lock)
121 local_irq_save(flags);
123 rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_);
124 LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock);
127 EXPORT_SYMBOL(_read_lock_irqsave);
129 void __lockfunc _read_lock_irq(rwlock_t *lock)
133 rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_);
134 LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock);
136 EXPORT_SYMBOL(_read_lock_irq);
138 void __lockfunc _read_lock_bh(rwlock_t *lock)
142 rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_);
143 LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock);
145 EXPORT_SYMBOL(_read_lock_bh);
147 unsigned long __lockfunc _write_lock_irqsave(rwlock_t *lock)
151 local_irq_save(flags);
153 rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_);
154 LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock);
157 EXPORT_SYMBOL(_write_lock_irqsave);
159 void __lockfunc _write_lock_irq(rwlock_t *lock)
163 rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_);
164 LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock);
166 EXPORT_SYMBOL(_write_lock_irq);
168 void __lockfunc _write_lock_bh(rwlock_t *lock)
172 rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_);
173 LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock);
175 EXPORT_SYMBOL(_write_lock_bh);
177 void __lockfunc _spin_lock(spinlock_t *lock)
180 spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
181 LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
184 EXPORT_SYMBOL(_spin_lock);
186 void __lockfunc _write_lock(rwlock_t *lock)
189 rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_);
190 LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock);
193 EXPORT_SYMBOL(_write_lock);
195 #else /* CONFIG_PREEMPT: */
198 * This could be a long-held lock. We both prepare to spin for a long
199 * time (making _this_ CPU preemptable if possible), and we also signal
200 * towards that other CPU that it should break the lock ASAP.
202 * (We do this in a function because inlining it would be excessive.)
205 #define BUILD_LOCK_OPS(op, locktype) \
206 void __lockfunc _##op##_lock(locktype##_t *lock) \
210 if (likely(_raw_##op##_trylock(lock))) \
214 if (!(lock)->break_lock) \
215 (lock)->break_lock = 1; \
216 while (!op##_can_lock(lock) && (lock)->break_lock) \
217 _raw_##op##_relax(&lock->raw_lock); \
219 (lock)->break_lock = 0; \
222 EXPORT_SYMBOL(_##op##_lock); \
224 unsigned long __lockfunc _##op##_lock_irqsave(locktype##_t *lock) \
226 unsigned long flags; \
230 local_irq_save(flags); \
231 if (likely(_raw_##op##_trylock(lock))) \
233 local_irq_restore(flags); \
236 if (!(lock)->break_lock) \
237 (lock)->break_lock = 1; \
238 while (!op##_can_lock(lock) && (lock)->break_lock) \
239 _raw_##op##_relax(&lock->raw_lock); \
241 (lock)->break_lock = 0; \
245 EXPORT_SYMBOL(_##op##_lock_irqsave); \
247 void __lockfunc _##op##_lock_irq(locktype##_t *lock) \
249 _##op##_lock_irqsave(lock); \
252 EXPORT_SYMBOL(_##op##_lock_irq); \
254 void __lockfunc _##op##_lock_bh(locktype##_t *lock) \
256 unsigned long flags; \
259 /* Careful: we must exclude softirqs too, hence the */ \
260 /* irq-disabling. We use the generic preemption-aware */ \
263 flags = _##op##_lock_irqsave(lock); \
264 local_bh_disable(); \
265 local_irq_restore(flags); \
268 EXPORT_SYMBOL(_##op##_lock_bh)
271 * Build preemption-friendly versions of the following
272 * lock-spinning functions:
274 * _[spin|read|write]_lock()
275 * _[spin|read|write]_lock_irq()
276 * _[spin|read|write]_lock_irqsave()
277 * _[spin|read|write]_lock_bh()
279 BUILD_LOCK_OPS(spin, spinlock);
280 BUILD_LOCK_OPS(read, rwlock);
281 BUILD_LOCK_OPS(write, rwlock);
283 #endif /* CONFIG_PREEMPT */
285 #ifdef CONFIG_DEBUG_LOCK_ALLOC
287 void __lockfunc _spin_lock_nested(spinlock_t *lock, int subclass)
290 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
291 LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
293 EXPORT_SYMBOL(_spin_lock_nested);
295 unsigned long __lockfunc _spin_lock_irqsave_nested(spinlock_t *lock, int subclass)
299 local_irq_save(flags);
301 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
303 * On lockdep we dont want the hand-coded irq-enable of
304 * _raw_spin_lock_flags() code, because lockdep assumes
305 * that interrupts are not re-enabled during lock-acquire:
307 #ifdef CONFIG_LOCKDEP
308 LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
310 _raw_spin_lock_flags(lock, &flags);
314 EXPORT_SYMBOL(_spin_lock_irqsave_nested);
316 void __lockfunc _spin_lock_nest_lock(spinlock_t *lock,
317 struct lockdep_map *nest_lock)
320 spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_);
321 LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
323 EXPORT_SYMBOL(_spin_lock_nest_lock);
327 void __lockfunc _spin_unlock(spinlock_t *lock)
329 spin_release(&lock->dep_map, 1, _RET_IP_);
330 _raw_spin_unlock(lock);
333 EXPORT_SYMBOL(_spin_unlock);
335 void __lockfunc _write_unlock(rwlock_t *lock)
337 rwlock_release(&lock->dep_map, 1, _RET_IP_);
338 _raw_write_unlock(lock);
341 EXPORT_SYMBOL(_write_unlock);
343 void __lockfunc _read_unlock(rwlock_t *lock)
345 rwlock_release(&lock->dep_map, 1, _RET_IP_);
346 _raw_read_unlock(lock);
349 EXPORT_SYMBOL(_read_unlock);
351 void __lockfunc _spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
353 spin_release(&lock->dep_map, 1, _RET_IP_);
354 _raw_spin_unlock(lock);
355 local_irq_restore(flags);
358 EXPORT_SYMBOL(_spin_unlock_irqrestore);
360 void __lockfunc _spin_unlock_irq(spinlock_t *lock)
362 spin_release(&lock->dep_map, 1, _RET_IP_);
363 _raw_spin_unlock(lock);
367 EXPORT_SYMBOL(_spin_unlock_irq);
369 void __lockfunc _spin_unlock_bh(spinlock_t *lock)
371 spin_release(&lock->dep_map, 1, _RET_IP_);
372 _raw_spin_unlock(lock);
373 preempt_enable_no_resched();
374 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
376 EXPORT_SYMBOL(_spin_unlock_bh);
378 void __lockfunc _read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
380 rwlock_release(&lock->dep_map, 1, _RET_IP_);
381 _raw_read_unlock(lock);
382 local_irq_restore(flags);
385 EXPORT_SYMBOL(_read_unlock_irqrestore);
387 void __lockfunc _read_unlock_irq(rwlock_t *lock)
389 rwlock_release(&lock->dep_map, 1, _RET_IP_);
390 _raw_read_unlock(lock);
394 EXPORT_SYMBOL(_read_unlock_irq);
396 void __lockfunc _read_unlock_bh(rwlock_t *lock)
398 rwlock_release(&lock->dep_map, 1, _RET_IP_);
399 _raw_read_unlock(lock);
400 preempt_enable_no_resched();
401 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
403 EXPORT_SYMBOL(_read_unlock_bh);
405 void __lockfunc _write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
407 rwlock_release(&lock->dep_map, 1, _RET_IP_);
408 _raw_write_unlock(lock);
409 local_irq_restore(flags);
412 EXPORT_SYMBOL(_write_unlock_irqrestore);
414 void __lockfunc _write_unlock_irq(rwlock_t *lock)
416 rwlock_release(&lock->dep_map, 1, _RET_IP_);
417 _raw_write_unlock(lock);
421 EXPORT_SYMBOL(_write_unlock_irq);
423 void __lockfunc _write_unlock_bh(rwlock_t *lock)
425 rwlock_release(&lock->dep_map, 1, _RET_IP_);
426 _raw_write_unlock(lock);
427 preempt_enable_no_resched();
428 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
430 EXPORT_SYMBOL(_write_unlock_bh);
432 int __lockfunc _spin_trylock_bh(spinlock_t *lock)
436 if (_raw_spin_trylock(lock)) {
437 spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
441 preempt_enable_no_resched();
442 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
445 EXPORT_SYMBOL(_spin_trylock_bh);
447 notrace int in_lock_functions(unsigned long addr)
449 /* Linker adds these: start and end of __lockfunc functions */
450 extern char __lock_text_start[], __lock_text_end[];
452 return addr >= (unsigned long)__lock_text_start
453 && addr < (unsigned long)__lock_text_end;
455 EXPORT_SYMBOL(in_lock_functions);