Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (2004) Linus Torvalds | |
3 | * | |
4 | * Author: Zwane Mwaikambo <zwane@fsmlabs.com> | |
5 | * | |
fb1c8f93 IM |
6 | * Copyright (2004, 2005) Ingo Molnar |
7 | * | |
8 | * This file contains the spinlock/rwlock implementations for the | |
9 | * SMP and the DEBUG_SPINLOCK cases. (UP-nondebug inlines them) | |
0cb91a22 AK |
10 | * |
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. | |
1da177e4 LT |
15 | */ |
16 | ||
1da177e4 LT |
17 | #include <linux/linkage.h> |
18 | #include <linux/preempt.h> | |
19 | #include <linux/spinlock.h> | |
20 | #include <linux/interrupt.h> | |
8a25d5de | 21 | #include <linux/debug_locks.h> |
1da177e4 LT |
22 | #include <linux/module.h> |
23 | ||
1da177e4 LT |
24 | int __lockfunc _spin_trylock(spinlock_t *lock) |
25 | { | |
26 | preempt_disable(); | |
8a25d5de IM |
27 | if (_raw_spin_trylock(lock)) { |
28 | spin_acquire(&lock->dep_map, 0, 1, _RET_IP_); | |
1da177e4 | 29 | return 1; |
8a25d5de | 30 | } |
1da177e4 LT |
31 | |
32 | preempt_enable(); | |
33 | return 0; | |
34 | } | |
35 | EXPORT_SYMBOL(_spin_trylock); | |
36 | ||
37 | int __lockfunc _read_trylock(rwlock_t *lock) | |
38 | { | |
39 | preempt_disable(); | |
8a25d5de IM |
40 | if (_raw_read_trylock(lock)) { |
41 | rwlock_acquire_read(&lock->dep_map, 0, 1, _RET_IP_); | |
1da177e4 | 42 | return 1; |
8a25d5de | 43 | } |
1da177e4 LT |
44 | |
45 | preempt_enable(); | |
46 | return 0; | |
47 | } | |
48 | EXPORT_SYMBOL(_read_trylock); | |
49 | ||
50 | int __lockfunc _write_trylock(rwlock_t *lock) | |
51 | { | |
52 | preempt_disable(); | |
8a25d5de IM |
53 | if (_raw_write_trylock(lock)) { |
54 | rwlock_acquire(&lock->dep_map, 0, 1, _RET_IP_); | |
1da177e4 | 55 | return 1; |
8a25d5de | 56 | } |
1da177e4 LT |
57 | |
58 | preempt_enable(); | |
59 | return 0; | |
60 | } | |
61 | EXPORT_SYMBOL(_write_trylock); | |
62 | ||
8a25d5de IM |
63 | /* |
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): | |
67 | */ | |
95c354fe | 68 | #if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC) |
1da177e4 LT |
69 | |
70 | void __lockfunc _read_lock(rwlock_t *lock) | |
71 | { | |
72 | preempt_disable(); | |
8a25d5de | 73 | rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 74 | LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock); |
1da177e4 LT |
75 | } |
76 | EXPORT_SYMBOL(_read_lock); | |
77 | ||
78 | unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock) | |
79 | { | |
80 | unsigned long flags; | |
81 | ||
82 | local_irq_save(flags); | |
83 | preempt_disable(); | |
8a25d5de IM |
84 | spin_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
85 | /* | |
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: | |
89 | */ | |
ca58abcb | 90 | #ifdef CONFIG_LOCKDEP |
4fe87745 | 91 | LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock); |
8a25d5de | 92 | #else |
fb1c8f93 | 93 | _raw_spin_lock_flags(lock, &flags); |
8a25d5de | 94 | #endif |
1da177e4 LT |
95 | return flags; |
96 | } | |
97 | EXPORT_SYMBOL(_spin_lock_irqsave); | |
98 | ||
99 | void __lockfunc _spin_lock_irq(spinlock_t *lock) | |
100 | { | |
101 | local_irq_disable(); | |
102 | preempt_disable(); | |
8a25d5de | 103 | spin_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 104 | LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock); |
1da177e4 LT |
105 | } |
106 | EXPORT_SYMBOL(_spin_lock_irq); | |
107 | ||
108 | void __lockfunc _spin_lock_bh(spinlock_t *lock) | |
109 | { | |
110 | local_bh_disable(); | |
111 | preempt_disable(); | |
8a25d5de | 112 | spin_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 113 | LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock); |
1da177e4 LT |
114 | } |
115 | EXPORT_SYMBOL(_spin_lock_bh); | |
116 | ||
117 | unsigned long __lockfunc _read_lock_irqsave(rwlock_t *lock) | |
118 | { | |
119 | unsigned long flags; | |
120 | ||
121 | local_irq_save(flags); | |
122 | preempt_disable(); | |
8a25d5de | 123 | rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 124 | LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock); |
1da177e4 LT |
125 | return flags; |
126 | } | |
127 | EXPORT_SYMBOL(_read_lock_irqsave); | |
128 | ||
129 | void __lockfunc _read_lock_irq(rwlock_t *lock) | |
130 | { | |
131 | local_irq_disable(); | |
132 | preempt_disable(); | |
8a25d5de | 133 | rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 134 | LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock); |
1da177e4 LT |
135 | } |
136 | EXPORT_SYMBOL(_read_lock_irq); | |
137 | ||
138 | void __lockfunc _read_lock_bh(rwlock_t *lock) | |
139 | { | |
140 | local_bh_disable(); | |
141 | preempt_disable(); | |
8a25d5de | 142 | rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 143 | LOCK_CONTENDED(lock, _raw_read_trylock, _raw_read_lock); |
1da177e4 LT |
144 | } |
145 | EXPORT_SYMBOL(_read_lock_bh); | |
146 | ||
147 | unsigned long __lockfunc _write_lock_irqsave(rwlock_t *lock) | |
148 | { | |
149 | unsigned long flags; | |
150 | ||
151 | local_irq_save(flags); | |
152 | preempt_disable(); | |
8a25d5de | 153 | rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 154 | LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock); |
1da177e4 LT |
155 | return flags; |
156 | } | |
157 | EXPORT_SYMBOL(_write_lock_irqsave); | |
158 | ||
159 | void __lockfunc _write_lock_irq(rwlock_t *lock) | |
160 | { | |
161 | local_irq_disable(); | |
162 | preempt_disable(); | |
8a25d5de | 163 | rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 164 | LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock); |
1da177e4 LT |
165 | } |
166 | EXPORT_SYMBOL(_write_lock_irq); | |
167 | ||
168 | void __lockfunc _write_lock_bh(rwlock_t *lock) | |
169 | { | |
170 | local_bh_disable(); | |
171 | preempt_disable(); | |
8a25d5de | 172 | rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 173 | LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock); |
1da177e4 LT |
174 | } |
175 | EXPORT_SYMBOL(_write_lock_bh); | |
176 | ||
177 | void __lockfunc _spin_lock(spinlock_t *lock) | |
178 | { | |
179 | preempt_disable(); | |
8a25d5de | 180 | spin_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 181 | LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock); |
1da177e4 LT |
182 | } |
183 | ||
184 | EXPORT_SYMBOL(_spin_lock); | |
185 | ||
186 | void __lockfunc _write_lock(rwlock_t *lock) | |
187 | { | |
188 | preempt_disable(); | |
8a25d5de | 189 | rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_); |
4fe87745 | 190 | LOCK_CONTENDED(lock, _raw_write_trylock, _raw_write_lock); |
1da177e4 LT |
191 | } |
192 | ||
193 | EXPORT_SYMBOL(_write_lock); | |
194 | ||
195 | #else /* CONFIG_PREEMPT: */ | |
196 | ||
197 | /* | |
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. | |
201 | * | |
202 | * (We do this in a function because inlining it would be excessive.) | |
203 | */ | |
204 | ||
205 | #define BUILD_LOCK_OPS(op, locktype) \ | |
206 | void __lockfunc _##op##_lock(locktype##_t *lock) \ | |
207 | { \ | |
1da177e4 | 208 | for (;;) { \ |
ee25e96f | 209 | preempt_disable(); \ |
1da177e4 LT |
210 | if (likely(_raw_##op##_trylock(lock))) \ |
211 | break; \ | |
212 | preempt_enable(); \ | |
ee25e96f | 213 | \ |
1da177e4 LT |
214 | if (!(lock)->break_lock) \ |
215 | (lock)->break_lock = 1; \ | |
216 | while (!op##_can_lock(lock) && (lock)->break_lock) \ | |
ef6edc97 | 217 | _raw_##op##_relax(&lock->raw_lock); \ |
1da177e4 LT |
218 | } \ |
219 | (lock)->break_lock = 0; \ | |
220 | } \ | |
221 | \ | |
222 | EXPORT_SYMBOL(_##op##_lock); \ | |
223 | \ | |
224 | unsigned long __lockfunc _##op##_lock_irqsave(locktype##_t *lock) \ | |
225 | { \ | |
226 | unsigned long flags; \ | |
227 | \ | |
1da177e4 | 228 | for (;;) { \ |
ee25e96f | 229 | preempt_disable(); \ |
1da177e4 LT |
230 | local_irq_save(flags); \ |
231 | if (likely(_raw_##op##_trylock(lock))) \ | |
232 | break; \ | |
233 | local_irq_restore(flags); \ | |
1da177e4 | 234 | preempt_enable(); \ |
ee25e96f | 235 | \ |
1da177e4 LT |
236 | if (!(lock)->break_lock) \ |
237 | (lock)->break_lock = 1; \ | |
238 | while (!op##_can_lock(lock) && (lock)->break_lock) \ | |
ef6edc97 | 239 | _raw_##op##_relax(&lock->raw_lock); \ |
1da177e4 LT |
240 | } \ |
241 | (lock)->break_lock = 0; \ | |
242 | return flags; \ | |
243 | } \ | |
244 | \ | |
245 | EXPORT_SYMBOL(_##op##_lock_irqsave); \ | |
246 | \ | |
247 | void __lockfunc _##op##_lock_irq(locktype##_t *lock) \ | |
248 | { \ | |
249 | _##op##_lock_irqsave(lock); \ | |
250 | } \ | |
251 | \ | |
252 | EXPORT_SYMBOL(_##op##_lock_irq); \ | |
253 | \ | |
254 | void __lockfunc _##op##_lock_bh(locktype##_t *lock) \ | |
255 | { \ | |
256 | unsigned long flags; \ | |
257 | \ | |
258 | /* */ \ | |
259 | /* Careful: we must exclude softirqs too, hence the */ \ | |
260 | /* irq-disabling. We use the generic preemption-aware */ \ | |
261 | /* function: */ \ | |
262 | /**/ \ | |
263 | flags = _##op##_lock_irqsave(lock); \ | |
264 | local_bh_disable(); \ | |
265 | local_irq_restore(flags); \ | |
266 | } \ | |
267 | \ | |
268 | EXPORT_SYMBOL(_##op##_lock_bh) | |
269 | ||
270 | /* | |
271 | * Build preemption-friendly versions of the following | |
272 | * lock-spinning functions: | |
273 | * | |
274 | * _[spin|read|write]_lock() | |
275 | * _[spin|read|write]_lock_irq() | |
276 | * _[spin|read|write]_lock_irqsave() | |
277 | * _[spin|read|write]_lock_bh() | |
278 | */ | |
279 | BUILD_LOCK_OPS(spin, spinlock); | |
280 | BUILD_LOCK_OPS(read, rwlock); | |
281 | BUILD_LOCK_OPS(write, rwlock); | |
282 | ||
283 | #endif /* CONFIG_PREEMPT */ | |
284 | ||
8a25d5de IM |
285 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
286 | ||
287 | void __lockfunc _spin_lock_nested(spinlock_t *lock, int subclass) | |
288 | { | |
289 | preempt_disable(); | |
290 | spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_); | |
4fe87745 | 291 | LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock); |
8a25d5de | 292 | } |
8a25d5de | 293 | EXPORT_SYMBOL(_spin_lock_nested); |
b7d39aff | 294 | |
cfd3ef23 AV |
295 | unsigned long __lockfunc _spin_lock_irqsave_nested(spinlock_t *lock, int subclass) |
296 | { | |
297 | unsigned long flags; | |
298 | ||
299 | local_irq_save(flags); | |
300 | preempt_disable(); | |
301 | spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_); | |
e8c158bb RH |
302 | LOCK_CONTENDED_FLAGS(lock, _raw_spin_trylock, _raw_spin_lock, |
303 | _raw_spin_lock_flags, &flags); | |
cfd3ef23 AV |
304 | return flags; |
305 | } | |
cfd3ef23 | 306 | EXPORT_SYMBOL(_spin_lock_irqsave_nested); |
8a25d5de | 307 | |
b7d39aff PZ |
308 | void __lockfunc _spin_lock_nest_lock(spinlock_t *lock, |
309 | struct lockdep_map *nest_lock) | |
310 | { | |
311 | preempt_disable(); | |
312 | spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_); | |
313 | LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock); | |
314 | } | |
b7d39aff PZ |
315 | EXPORT_SYMBOL(_spin_lock_nest_lock); |
316 | ||
8a25d5de IM |
317 | #endif |
318 | ||
1da177e4 LT |
319 | void __lockfunc _spin_unlock(spinlock_t *lock) |
320 | { | |
8a25d5de | 321 | spin_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
322 | _raw_spin_unlock(lock); |
323 | preempt_enable(); | |
324 | } | |
325 | EXPORT_SYMBOL(_spin_unlock); | |
326 | ||
327 | void __lockfunc _write_unlock(rwlock_t *lock) | |
328 | { | |
8a25d5de | 329 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
330 | _raw_write_unlock(lock); |
331 | preempt_enable(); | |
332 | } | |
333 | EXPORT_SYMBOL(_write_unlock); | |
334 | ||
335 | void __lockfunc _read_unlock(rwlock_t *lock) | |
336 | { | |
8a25d5de | 337 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
338 | _raw_read_unlock(lock); |
339 | preempt_enable(); | |
340 | } | |
341 | EXPORT_SYMBOL(_read_unlock); | |
342 | ||
343 | void __lockfunc _spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags) | |
344 | { | |
8a25d5de | 345 | spin_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
346 | _raw_spin_unlock(lock); |
347 | local_irq_restore(flags); | |
348 | preempt_enable(); | |
349 | } | |
350 | EXPORT_SYMBOL(_spin_unlock_irqrestore); | |
351 | ||
352 | void __lockfunc _spin_unlock_irq(spinlock_t *lock) | |
353 | { | |
8a25d5de | 354 | spin_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
355 | _raw_spin_unlock(lock); |
356 | local_irq_enable(); | |
357 | preempt_enable(); | |
358 | } | |
359 | EXPORT_SYMBOL(_spin_unlock_irq); | |
360 | ||
361 | void __lockfunc _spin_unlock_bh(spinlock_t *lock) | |
362 | { | |
8a25d5de | 363 | spin_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 | 364 | _raw_spin_unlock(lock); |
10f02d1c | 365 | preempt_enable_no_resched(); |
8a25d5de | 366 | local_bh_enable_ip((unsigned long)__builtin_return_address(0)); |
1da177e4 LT |
367 | } |
368 | EXPORT_SYMBOL(_spin_unlock_bh); | |
369 | ||
370 | void __lockfunc _read_unlock_irqrestore(rwlock_t *lock, unsigned long flags) | |
371 | { | |
8a25d5de | 372 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
373 | _raw_read_unlock(lock); |
374 | local_irq_restore(flags); | |
375 | preempt_enable(); | |
376 | } | |
377 | EXPORT_SYMBOL(_read_unlock_irqrestore); | |
378 | ||
379 | void __lockfunc _read_unlock_irq(rwlock_t *lock) | |
380 | { | |
8a25d5de | 381 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
382 | _raw_read_unlock(lock); |
383 | local_irq_enable(); | |
384 | preempt_enable(); | |
385 | } | |
386 | EXPORT_SYMBOL(_read_unlock_irq); | |
387 | ||
388 | void __lockfunc _read_unlock_bh(rwlock_t *lock) | |
389 | { | |
8a25d5de | 390 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 | 391 | _raw_read_unlock(lock); |
10f02d1c | 392 | preempt_enable_no_resched(); |
8a25d5de | 393 | local_bh_enable_ip((unsigned long)__builtin_return_address(0)); |
1da177e4 LT |
394 | } |
395 | EXPORT_SYMBOL(_read_unlock_bh); | |
396 | ||
397 | void __lockfunc _write_unlock_irqrestore(rwlock_t *lock, unsigned long flags) | |
398 | { | |
8a25d5de | 399 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
400 | _raw_write_unlock(lock); |
401 | local_irq_restore(flags); | |
402 | preempt_enable(); | |
403 | } | |
404 | EXPORT_SYMBOL(_write_unlock_irqrestore); | |
405 | ||
406 | void __lockfunc _write_unlock_irq(rwlock_t *lock) | |
407 | { | |
8a25d5de | 408 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 LT |
409 | _raw_write_unlock(lock); |
410 | local_irq_enable(); | |
411 | preempt_enable(); | |
412 | } | |
413 | EXPORT_SYMBOL(_write_unlock_irq); | |
414 | ||
415 | void __lockfunc _write_unlock_bh(rwlock_t *lock) | |
416 | { | |
8a25d5de | 417 | rwlock_release(&lock->dep_map, 1, _RET_IP_); |
1da177e4 | 418 | _raw_write_unlock(lock); |
10f02d1c | 419 | preempt_enable_no_resched(); |
8a25d5de | 420 | local_bh_enable_ip((unsigned long)__builtin_return_address(0)); |
1da177e4 LT |
421 | } |
422 | EXPORT_SYMBOL(_write_unlock_bh); | |
423 | ||
424 | int __lockfunc _spin_trylock_bh(spinlock_t *lock) | |
425 | { | |
426 | local_bh_disable(); | |
427 | preempt_disable(); | |
8a25d5de IM |
428 | if (_raw_spin_trylock(lock)) { |
429 | spin_acquire(&lock->dep_map, 0, 1, _RET_IP_); | |
1da177e4 | 430 | return 1; |
8a25d5de | 431 | } |
1da177e4 | 432 | |
10f02d1c | 433 | preempt_enable_no_resched(); |
8a25d5de | 434 | local_bh_enable_ip((unsigned long)__builtin_return_address(0)); |
1da177e4 LT |
435 | return 0; |
436 | } | |
437 | EXPORT_SYMBOL(_spin_trylock_bh); | |
438 | ||
0764d23c | 439 | notrace int in_lock_functions(unsigned long addr) |
1da177e4 LT |
440 | { |
441 | /* Linker adds these: start and end of __lockfunc functions */ | |
442 | extern char __lock_text_start[], __lock_text_end[]; | |
443 | ||
444 | return addr >= (unsigned long)__lock_text_start | |
445 | && addr < (unsigned long)__lock_text_end; | |
446 | } | |
447 | EXPORT_SYMBOL(in_lock_functions); |