[SUNRPC]: more sunrpc endianness annotations
[linux-2.6.22.y-op.git] / kernel / spinlock.c
blob9644a41e0bef60fd40773976705c6529b49c45d1
1 /*
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);
31 return 1;
33 EXPORT_SYMBOL(generic__raw_read_trylock);
35 int __lockfunc _spin_trylock(spinlock_t *lock)
37 preempt_disable();
38 if (_raw_spin_trylock(lock)) {
39 spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
40 return 1;
43 preempt_enable();
44 return 0;
46 EXPORT_SYMBOL(_spin_trylock);
48 int __lockfunc _read_trylock(rwlock_t *lock)
50 preempt_disable();
51 if (_raw_read_trylock(lock)) {
52 rwlock_acquire_read(&lock->dep_map, 0, 1, _RET_IP_);
53 return 1;
56 preempt_enable();
57 return 0;
59 EXPORT_SYMBOL(_read_trylock);
61 int __lockfunc _write_trylock(rwlock_t *lock)
63 preempt_disable();
64 if (_raw_write_trylock(lock)) {
65 rwlock_acquire(&lock->dep_map, 0, 1, _RET_IP_);
66 return 1;
69 preempt_enable();
70 return 0;
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)
84 preempt_disable();
85 rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_);
86 _raw_read_lock(lock);
88 EXPORT_SYMBOL(_read_lock);
90 unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock)
92 unsigned long flags;
94 local_irq_save(flags);
95 preempt_disable();
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);
104 #else
105 _raw_spin_lock_flags(lock, &flags);
106 #endif
107 return flags;
109 EXPORT_SYMBOL(_spin_lock_irqsave);
111 void __lockfunc _spin_lock_irq(spinlock_t *lock)
113 local_irq_disable();
114 preempt_disable();
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)
122 local_bh_disable();
123 preempt_disable();
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)
131 unsigned long flags;
133 local_irq_save(flags);
134 preempt_disable();
135 rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_);
136 _raw_read_lock(lock);
137 return flags;
139 EXPORT_SYMBOL(_read_lock_irqsave);
141 void __lockfunc _read_lock_irq(rwlock_t *lock)
143 local_irq_disable();
144 preempt_disable();
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)
152 local_bh_disable();
153 preempt_disable();
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)
161 unsigned long flags;
163 local_irq_save(flags);
164 preempt_disable();
165 rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_);
166 _raw_write_lock(lock);
167 return flags;
169 EXPORT_SYMBOL(_write_lock_irqsave);
171 void __lockfunc _write_lock_irq(rwlock_t *lock)
173 local_irq_disable();
174 preempt_disable();
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)
182 local_bh_disable();
183 preempt_disable();
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)
191 preempt_disable();
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)
200 preempt_disable();
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) \
220 for (;;) { \
221 preempt_disable(); \
222 if (likely(_raw_##op##_trylock(lock))) \
223 break; \
224 preempt_enable(); \
226 if (!(lock)->break_lock) \
227 (lock)->break_lock = 1; \
228 while (!op##_can_lock(lock) && (lock)->break_lock) \
229 cpu_relax(); \
231 (lock)->break_lock = 0; \
234 EXPORT_SYMBOL(_##op##_lock); \
236 unsigned long __lockfunc _##op##_lock_irqsave(locktype##_t *lock) \
238 unsigned long flags; \
240 for (;;) { \
241 preempt_disable(); \
242 local_irq_save(flags); \
243 if (likely(_raw_##op##_trylock(lock))) \
244 break; \
245 local_irq_restore(flags); \
246 preempt_enable(); \
248 if (!(lock)->break_lock) \
249 (lock)->break_lock = 1; \
250 while (!op##_can_lock(lock) && (lock)->break_lock) \
251 cpu_relax(); \
253 (lock)->break_lock = 0; \
254 return flags; \
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; \
270 /* */ \
271 /* Careful: we must exclude softirqs too, hence the */ \
272 /* irq-disabling. We use the generic preemption-aware */ \
273 /* function: */ \
274 /**/ \
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)
301 preempt_disable();
302 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
303 _raw_spin_lock(lock);
306 EXPORT_SYMBOL(_spin_lock_nested);
308 #endif
310 void __lockfunc _spin_unlock(spinlock_t *lock)
312 spin_release(&lock->dep_map, 1, _RET_IP_);
313 _raw_spin_unlock(lock);
314 preempt_enable();
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);
322 preempt_enable();
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);
330 preempt_enable();
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);
339 preempt_enable();
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);
347 local_irq_enable();
348 preempt_enable();
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);
366 preempt_enable();
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);
374 local_irq_enable();
375 preempt_enable();
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);
393 preempt_enable();
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);
401 local_irq_enable();
402 preempt_enable();
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)
417 local_bh_disable();
418 preempt_disable();
419 if (_raw_spin_trylock(lock)) {
420 spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
421 return 1;
424 preempt_enable_no_resched();
425 local_bh_enable_ip((unsigned long)__builtin_return_address(0));
426 return 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);