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>
25 * Generic declaration of the raw read_trylock() function,
26 * architectures are supposed to optimize this:
28 int __lockfunc
generic__raw_read_trylock(raw_rwlock_t
*lock
)
30 __raw_read_lock(lock
);
33 EXPORT_SYMBOL(generic__raw_read_trylock
);
35 int __lockfunc
_spin_trylock(spinlock_t
*lock
)
38 if (_raw_spin_trylock(lock
)) {
39 spin_acquire(&lock
->dep_map
, 0, 1, _RET_IP_
);
46 EXPORT_SYMBOL(_spin_trylock
);
48 int __lockfunc
_read_trylock(rwlock_t
*lock
)
51 if (_raw_read_trylock(lock
)) {
52 rwlock_acquire_read(&lock
->dep_map
, 0, 1, _RET_IP_
);
59 EXPORT_SYMBOL(_read_trylock
);
61 int __lockfunc
_write_trylock(rwlock_t
*lock
)
64 if (_raw_write_trylock(lock
)) {
65 rwlock_acquire(&lock
->dep_map
, 0, 1, _RET_IP_
);
72 EXPORT_SYMBOL(_write_trylock
);
75 * If lockdep is enabled then we use the non-preemption spin-ops
76 * even on CONFIG_PREEMPT, because lockdep assumes that interrupts are
77 * not re-enabled during lock-acquire (which the preempt-spin-ops do):
79 #if !defined(CONFIG_PREEMPT) || !defined(CONFIG_SMP) || \
80 defined(CONFIG_DEBUG_LOCK_ALLOC)
82 void __lockfunc
_read_lock(rwlock_t
*lock
)
85 rwlock_acquire_read(&lock
->dep_map
, 0, 0, _RET_IP_
);
88 EXPORT_SYMBOL(_read_lock
);
90 unsigned long __lockfunc
_spin_lock_irqsave(spinlock_t
*lock
)
94 local_irq_save(flags
);
96 spin_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
98 * On lockdep we dont want the hand-coded irq-enable of
99 * _raw_spin_lock_flags() code, because lockdep assumes
100 * that interrupts are not re-enabled during lock-acquire:
102 #ifdef CONFIG_PROVE_LOCKING
103 _raw_spin_lock(lock
);
105 _raw_spin_lock_flags(lock
, &flags
);
109 EXPORT_SYMBOL(_spin_lock_irqsave
);
111 void __lockfunc
_spin_lock_irq(spinlock_t
*lock
)
115 spin_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
116 _raw_spin_lock(lock
);
118 EXPORT_SYMBOL(_spin_lock_irq
);
120 void __lockfunc
_spin_lock_bh(spinlock_t
*lock
)
124 spin_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
125 _raw_spin_lock(lock
);
127 EXPORT_SYMBOL(_spin_lock_bh
);
129 unsigned long __lockfunc
_read_lock_irqsave(rwlock_t
*lock
)
133 local_irq_save(flags
);
135 rwlock_acquire_read(&lock
->dep_map
, 0, 0, _RET_IP_
);
136 _raw_read_lock(lock
);
139 EXPORT_SYMBOL(_read_lock_irqsave
);
141 void __lockfunc
_read_lock_irq(rwlock_t
*lock
)
145 rwlock_acquire_read(&lock
->dep_map
, 0, 0, _RET_IP_
);
146 _raw_read_lock(lock
);
148 EXPORT_SYMBOL(_read_lock_irq
);
150 void __lockfunc
_read_lock_bh(rwlock_t
*lock
)
154 rwlock_acquire_read(&lock
->dep_map
, 0, 0, _RET_IP_
);
155 _raw_read_lock(lock
);
157 EXPORT_SYMBOL(_read_lock_bh
);
159 unsigned long __lockfunc
_write_lock_irqsave(rwlock_t
*lock
)
163 local_irq_save(flags
);
165 rwlock_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
166 _raw_write_lock(lock
);
169 EXPORT_SYMBOL(_write_lock_irqsave
);
171 void __lockfunc
_write_lock_irq(rwlock_t
*lock
)
175 rwlock_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
176 _raw_write_lock(lock
);
178 EXPORT_SYMBOL(_write_lock_irq
);
180 void __lockfunc
_write_lock_bh(rwlock_t
*lock
)
184 rwlock_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
185 _raw_write_lock(lock
);
187 EXPORT_SYMBOL(_write_lock_bh
);
189 void __lockfunc
_spin_lock(spinlock_t
*lock
)
192 spin_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
193 _raw_spin_lock(lock
);
196 EXPORT_SYMBOL(_spin_lock
);
198 void __lockfunc
_write_lock(rwlock_t
*lock
)
201 rwlock_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
202 _raw_write_lock(lock
);
205 EXPORT_SYMBOL(_write_lock
);
207 #else /* CONFIG_PREEMPT: */
210 * This could be a long-held lock. We both prepare to spin for a long
211 * time (making _this_ CPU preemptable if possible), and we also signal
212 * towards that other CPU that it should break the lock ASAP.
214 * (We do this in a function because inlining it would be excessive.)
217 #define BUILD_LOCK_OPS(op, locktype) \
218 void __lockfunc _##op##_lock(locktype##_t *lock) \
222 if (likely(_raw_##op##_trylock(lock))) \
226 if (!(lock)->break_lock) \
227 (lock)->break_lock = 1; \
228 while (!op##_can_lock(lock) && (lock)->break_lock) \
231 (lock)->break_lock = 0; \
234 EXPORT_SYMBOL(_##op##_lock); \
236 unsigned long __lockfunc _##op##_lock_irqsave(locktype##_t *lock) \
238 unsigned long flags; \
242 local_irq_save(flags); \
243 if (likely(_raw_##op##_trylock(lock))) \
245 local_irq_restore(flags); \
248 if (!(lock)->break_lock) \
249 (lock)->break_lock = 1; \
250 while (!op##_can_lock(lock) && (lock)->break_lock) \
253 (lock)->break_lock = 0; \
257 EXPORT_SYMBOL(_##op##_lock_irqsave); \
259 void __lockfunc _##op##_lock_irq(locktype##_t *lock) \
261 _##op##_lock_irqsave(lock); \
264 EXPORT_SYMBOL(_##op##_lock_irq); \
266 void __lockfunc _##op##_lock_bh(locktype##_t *lock) \
268 unsigned long flags; \
271 /* Careful: we must exclude softirqs too, hence the */ \
272 /* irq-disabling. We use the generic preemption-aware */ \
275 flags = _##op##_lock_irqsave(lock); \
276 local_bh_disable(); \
277 local_irq_restore(flags); \
280 EXPORT_SYMBOL(_##op##_lock_bh)
283 * Build preemption-friendly versions of the following
284 * lock-spinning functions:
286 * _[spin|read|write]_lock()
287 * _[spin|read|write]_lock_irq()
288 * _[spin|read|write]_lock_irqsave()
289 * _[spin|read|write]_lock_bh()
291 BUILD_LOCK_OPS(spin
, spinlock
);
292 BUILD_LOCK_OPS(read
, rwlock
);
293 BUILD_LOCK_OPS(write
, rwlock
);
295 #endif /* CONFIG_PREEMPT */
297 #ifdef CONFIG_DEBUG_LOCK_ALLOC
299 void __lockfunc
_spin_lock_nested(spinlock_t
*lock
, int subclass
)
302 spin_acquire(&lock
->dep_map
, subclass
, 0, _RET_IP_
);
303 _raw_spin_lock(lock
);
306 EXPORT_SYMBOL(_spin_lock_nested
);
310 void __lockfunc
_spin_unlock(spinlock_t
*lock
)
312 spin_release(&lock
->dep_map
, 1, _RET_IP_
);
313 _raw_spin_unlock(lock
);
316 EXPORT_SYMBOL(_spin_unlock
);
318 void __lockfunc
_write_unlock(rwlock_t
*lock
)
320 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
321 _raw_write_unlock(lock
);
324 EXPORT_SYMBOL(_write_unlock
);
326 void __lockfunc
_read_unlock(rwlock_t
*lock
)
328 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
329 _raw_read_unlock(lock
);
332 EXPORT_SYMBOL(_read_unlock
);
334 void __lockfunc
_spin_unlock_irqrestore(spinlock_t
*lock
, unsigned long flags
)
336 spin_release(&lock
->dep_map
, 1, _RET_IP_
);
337 _raw_spin_unlock(lock
);
338 local_irq_restore(flags
);
341 EXPORT_SYMBOL(_spin_unlock_irqrestore
);
343 void __lockfunc
_spin_unlock_irq(spinlock_t
*lock
)
345 spin_release(&lock
->dep_map
, 1, _RET_IP_
);
346 _raw_spin_unlock(lock
);
350 EXPORT_SYMBOL(_spin_unlock_irq
);
352 void __lockfunc
_spin_unlock_bh(spinlock_t
*lock
)
354 spin_release(&lock
->dep_map
, 1, _RET_IP_
);
355 _raw_spin_unlock(lock
);
356 preempt_enable_no_resched();
357 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
359 EXPORT_SYMBOL(_spin_unlock_bh
);
361 void __lockfunc
_read_unlock_irqrestore(rwlock_t
*lock
, unsigned long flags
)
363 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
364 _raw_read_unlock(lock
);
365 local_irq_restore(flags
);
368 EXPORT_SYMBOL(_read_unlock_irqrestore
);
370 void __lockfunc
_read_unlock_irq(rwlock_t
*lock
)
372 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
373 _raw_read_unlock(lock
);
377 EXPORT_SYMBOL(_read_unlock_irq
);
379 void __lockfunc
_read_unlock_bh(rwlock_t
*lock
)
381 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
382 _raw_read_unlock(lock
);
383 preempt_enable_no_resched();
384 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
386 EXPORT_SYMBOL(_read_unlock_bh
);
388 void __lockfunc
_write_unlock_irqrestore(rwlock_t
*lock
, unsigned long flags
)
390 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
391 _raw_write_unlock(lock
);
392 local_irq_restore(flags
);
395 EXPORT_SYMBOL(_write_unlock_irqrestore
);
397 void __lockfunc
_write_unlock_irq(rwlock_t
*lock
)
399 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
400 _raw_write_unlock(lock
);
404 EXPORT_SYMBOL(_write_unlock_irq
);
406 void __lockfunc
_write_unlock_bh(rwlock_t
*lock
)
408 rwlock_release(&lock
->dep_map
, 1, _RET_IP_
);
409 _raw_write_unlock(lock
);
410 preempt_enable_no_resched();
411 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
413 EXPORT_SYMBOL(_write_unlock_bh
);
415 int __lockfunc
_spin_trylock_bh(spinlock_t
*lock
)
419 if (_raw_spin_trylock(lock
)) {
420 spin_acquire(&lock
->dep_map
, 0, 1, _RET_IP_
);
424 preempt_enable_no_resched();
425 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
428 EXPORT_SYMBOL(_spin_trylock_bh
);
430 int in_lock_functions(unsigned long addr
)
432 /* Linker adds these: start and end of __lockfunc functions */
433 extern char __lock_text_start
[], __lock_text_end
[];
435 return addr
>= (unsigned long)__lock_text_start
436 && addr
< (unsigned long)__lock_text_end
;
438 EXPORT_SYMBOL(in_lock_functions
);