]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * Copyright (2004) Linus Torvalds | |
4 | * | |
5 | * Author: Zwane Mwaikambo <zwane@fsmlabs.com> | |
6 | * | |
fb1c8f93 IM |
7 | * Copyright (2004, 2005) Ingo Molnar |
8 | * | |
9 | * This file contains the spinlock/rwlock implementations for the | |
10 | * SMP and the DEBUG_SPINLOCK cases. (UP-nondebug inlines them) | |
0cb91a22 AK |
11 | * |
12 | * Note that some architectures have special knowledge about the | |
13 | * stack frames of these functions in their profile_pc. If you | |
14 | * change anything significant here that could change the stack | |
15 | * frame contact the architecture maintainers. | |
1da177e4 LT |
16 | */ |
17 | ||
1da177e4 LT |
18 | #include <linux/linkage.h> |
19 | #include <linux/preempt.h> | |
20 | #include <linux/spinlock.h> | |
21 | #include <linux/interrupt.h> | |
8a25d5de | 22 | #include <linux/debug_locks.h> |
9984de1a | 23 | #include <linux/export.h> |
1da177e4 | 24 | |
8e13c7b7 TG |
25 | /* |
26 | * If lockdep is enabled then we use the non-preemption spin-ops | |
27 | * even on CONFIG_PREEMPT, because lockdep assumes that interrupts are | |
28 | * not re-enabled during lock-acquire (which the preempt-spin-ops do): | |
29 | */ | |
30 | #if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC) | |
31 | /* | |
32 | * The __lock_function inlines are taken from | |
f791dd25 CJ |
33 | * spinlock : include/linux/spinlock_api_smp.h |
34 | * rwlock : include/linux/rwlock_api_smp.h | |
8e13c7b7 TG |
35 | */ |
36 | #else | |
c14c338c WD |
37 | |
38 | /* | |
39 | * Some architectures can relax in favour of the CPU owning the lock. | |
40 | */ | |
41 | #ifndef arch_read_relax | |
42 | # define arch_read_relax(l) cpu_relax() | |
43 | #endif | |
44 | #ifndef arch_write_relax | |
45 | # define arch_write_relax(l) cpu_relax() | |
46 | #endif | |
47 | #ifndef arch_spin_relax | |
48 | # define arch_spin_relax(l) cpu_relax() | |
49 | #endif | |
50 | ||
8e13c7b7 TG |
51 | /* |
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. | |
55 | * | |
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. | |
59 | */ | |
60 | #define BUILD_LOCK_OPS(op, locktype) \ | |
9c1721aa | 61 | void __lockfunc __raw_##op##_lock(locktype##_t *lock) \ |
8e13c7b7 TG |
62 | { \ |
63 | for (;;) { \ | |
64 | preempt_disable(); \ | |
9828ea9d | 65 | if (likely(do_raw_##op##_trylock(lock))) \ |
8e13c7b7 TG |
66 | break; \ |
67 | preempt_enable(); \ | |
68 | \ | |
f87f3a32 | 69 | arch_##op##_relax(&lock->raw_lock); \ |
8e13c7b7 | 70 | } \ |
8e13c7b7 TG |
71 | } \ |
72 | \ | |
9c1721aa | 73 | unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \ |
8e13c7b7 TG |
74 | { \ |
75 | unsigned long flags; \ | |
76 | \ | |
77 | for (;;) { \ | |
78 | preempt_disable(); \ | |
79 | local_irq_save(flags); \ | |
9828ea9d | 80 | if (likely(do_raw_##op##_trylock(lock))) \ |
8e13c7b7 TG |
81 | break; \ |
82 | local_irq_restore(flags); \ | |
83 | preempt_enable(); \ | |
84 | \ | |
f87f3a32 | 85 | arch_##op##_relax(&lock->raw_lock); \ |
8e13c7b7 | 86 | } \ |
d89c7035 | 87 | \ |
8e13c7b7 TG |
88 | return flags; \ |
89 | } \ | |
90 | \ | |
9c1721aa | 91 | void __lockfunc __raw_##op##_lock_irq(locktype##_t *lock) \ |
8e13c7b7 | 92 | { \ |
9c1721aa | 93 | _raw_##op##_lock_irqsave(lock); \ |
8e13c7b7 TG |
94 | } \ |
95 | \ | |
9c1721aa | 96 | void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \ |
8e13c7b7 TG |
97 | { \ |
98 | unsigned long flags; \ | |
99 | \ | |
100 | /* */ \ | |
101 | /* Careful: we must exclude softirqs too, hence the */ \ | |
102 | /* irq-disabling. We use the generic preemption-aware */ \ | |
103 | /* function: */ \ | |
104 | /**/ \ | |
9c1721aa | 105 | flags = _raw_##op##_lock_irqsave(lock); \ |
8e13c7b7 TG |
106 | local_bh_disable(); \ |
107 | local_irq_restore(flags); \ | |
108 | } \ | |
109 | ||
110 | /* | |
111 | * Build preemption-friendly versions of the following | |
112 | * lock-spinning functions: | |
113 | * | |
114 | * __[spin|read|write]_lock() | |
115 | * __[spin|read|write]_lock_irq() | |
116 | * __[spin|read|write]_lock_irqsave() | |
117 | * __[spin|read|write]_lock_bh() | |
118 | */ | |
c2f21ce2 | 119 | BUILD_LOCK_OPS(spin, raw_spinlock); |
8e13c7b7 TG |
120 | BUILD_LOCK_OPS(read, rwlock); |
121 | BUILD_LOCK_OPS(write, rwlock); | |
122 | ||
123 | #endif | |
124 | ||
6beb0009 | 125 | #ifndef CONFIG_INLINE_SPIN_TRYLOCK |
9c1721aa | 126 | int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock) |
1da177e4 | 127 | { |
9c1721aa | 128 | return __raw_spin_trylock(lock); |
1da177e4 | 129 | } |
9c1721aa | 130 | EXPORT_SYMBOL(_raw_spin_trylock); |
892a7c67 | 131 | #endif |
1da177e4 | 132 | |
b7b40ade | 133 | #ifndef CONFIG_INLINE_SPIN_TRYLOCK_BH |
9c1721aa | 134 | int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock) |
1da177e4 | 135 | { |
9c1721aa | 136 | return __raw_spin_trylock_bh(lock); |
1da177e4 | 137 | } |
9c1721aa | 138 | EXPORT_SYMBOL(_raw_spin_trylock_bh); |
892a7c67 | 139 | #endif |
1da177e4 | 140 | |
b7b40ade | 141 | #ifndef CONFIG_INLINE_SPIN_LOCK |
9c1721aa | 142 | void __lockfunc _raw_spin_lock(raw_spinlock_t *lock) |
1da177e4 | 143 | { |
9c1721aa | 144 | __raw_spin_lock(lock); |
1da177e4 | 145 | } |
9c1721aa | 146 | EXPORT_SYMBOL(_raw_spin_lock); |
892a7c67 | 147 | #endif |
1da177e4 | 148 | |
6beb0009 | 149 | #ifndef CONFIG_INLINE_SPIN_LOCK_IRQSAVE |
9c1721aa | 150 | unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock) |
1da177e4 | 151 | { |
9c1721aa | 152 | return __raw_spin_lock_irqsave(lock); |
1da177e4 | 153 | } |
9c1721aa | 154 | EXPORT_SYMBOL(_raw_spin_lock_irqsave); |
892a7c67 | 155 | #endif |
1da177e4 | 156 | |
6beb0009 | 157 | #ifndef CONFIG_INLINE_SPIN_LOCK_IRQ |
9c1721aa | 158 | void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock) |
1da177e4 | 159 | { |
9c1721aa | 160 | __raw_spin_lock_irq(lock); |
1da177e4 | 161 | } |
9c1721aa | 162 | EXPORT_SYMBOL(_raw_spin_lock_irq); |
892a7c67 | 163 | #endif |
1da177e4 | 164 | |
6beb0009 | 165 | #ifndef CONFIG_INLINE_SPIN_LOCK_BH |
9c1721aa | 166 | void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock) |
1da177e4 | 167 | { |
9c1721aa | 168 | __raw_spin_lock_bh(lock); |
1da177e4 | 169 | } |
9c1721aa | 170 | EXPORT_SYMBOL(_raw_spin_lock_bh); |
892a7c67 | 171 | #endif |
1da177e4 | 172 | |
e335e3eb | 173 | #ifdef CONFIG_UNINLINE_SPIN_UNLOCK |
9c1721aa | 174 | void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock) |
1da177e4 | 175 | { |
9c1721aa | 176 | __raw_spin_unlock(lock); |
1da177e4 | 177 | } |
9c1721aa | 178 | EXPORT_SYMBOL(_raw_spin_unlock); |
892a7c67 | 179 | #endif |
1da177e4 | 180 | |
b7b40ade | 181 | #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE |
9c1721aa | 182 | void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags) |
1da177e4 | 183 | { |
9c1721aa | 184 | __raw_spin_unlock_irqrestore(lock, flags); |
1da177e4 | 185 | } |
9c1721aa | 186 | EXPORT_SYMBOL(_raw_spin_unlock_irqrestore); |
892a7c67 | 187 | #endif |
1da177e4 | 188 | |
b7b40ade | 189 | #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ |
9c1721aa | 190 | void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock) |
1da177e4 | 191 | { |
9c1721aa | 192 | __raw_spin_unlock_irq(lock); |
1da177e4 | 193 | } |
9c1721aa | 194 | EXPORT_SYMBOL(_raw_spin_unlock_irq); |
892a7c67 | 195 | #endif |
1da177e4 | 196 | |
b7b40ade | 197 | #ifndef CONFIG_INLINE_SPIN_UNLOCK_BH |
9c1721aa | 198 | void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock) |
1da177e4 | 199 | { |
9c1721aa | 200 | __raw_spin_unlock_bh(lock); |
1da177e4 | 201 | } |
9c1721aa | 202 | EXPORT_SYMBOL(_raw_spin_unlock_bh); |
892a7c67 | 203 | #endif |
1da177e4 | 204 | |
b7b40ade | 205 | #ifndef CONFIG_INLINE_READ_TRYLOCK |
9c1721aa | 206 | int __lockfunc _raw_read_trylock(rwlock_t *lock) |
1da177e4 | 207 | { |
9c1721aa | 208 | return __raw_read_trylock(lock); |
1da177e4 | 209 | } |
9c1721aa | 210 | EXPORT_SYMBOL(_raw_read_trylock); |
892a7c67 | 211 | #endif |
1da177e4 | 212 | |
b7b40ade | 213 | #ifndef CONFIG_INLINE_READ_LOCK |
9c1721aa | 214 | void __lockfunc _raw_read_lock(rwlock_t *lock) |
1da177e4 | 215 | { |
9c1721aa | 216 | __raw_read_lock(lock); |
1da177e4 | 217 | } |
9c1721aa | 218 | EXPORT_SYMBOL(_raw_read_lock); |
892a7c67 | 219 | #endif |
1da177e4 | 220 | |
b7b40ade | 221 | #ifndef CONFIG_INLINE_READ_LOCK_IRQSAVE |
9c1721aa | 222 | unsigned long __lockfunc _raw_read_lock_irqsave(rwlock_t *lock) |
1da177e4 | 223 | { |
9c1721aa | 224 | return __raw_read_lock_irqsave(lock); |
1da177e4 | 225 | } |
9c1721aa | 226 | EXPORT_SYMBOL(_raw_read_lock_irqsave); |
892a7c67 | 227 | #endif |
1da177e4 | 228 | |
b7b40ade | 229 | #ifndef CONFIG_INLINE_READ_LOCK_IRQ |
9c1721aa | 230 | void __lockfunc _raw_read_lock_irq(rwlock_t *lock) |
1da177e4 | 231 | { |
9c1721aa | 232 | __raw_read_lock_irq(lock); |
1da177e4 | 233 | } |
9c1721aa | 234 | EXPORT_SYMBOL(_raw_read_lock_irq); |
892a7c67 | 235 | #endif |
1da177e4 | 236 | |
b7b40ade | 237 | #ifndef CONFIG_INLINE_READ_LOCK_BH |
9c1721aa | 238 | void __lockfunc _raw_read_lock_bh(rwlock_t *lock) |
1da177e4 | 239 | { |
9c1721aa | 240 | __raw_read_lock_bh(lock); |
1da177e4 | 241 | } |
9c1721aa | 242 | EXPORT_SYMBOL(_raw_read_lock_bh); |
892a7c67 | 243 | #endif |
1da177e4 | 244 | |
6beb0009 | 245 | #ifndef CONFIG_INLINE_READ_UNLOCK |
9c1721aa | 246 | void __lockfunc _raw_read_unlock(rwlock_t *lock) |
1da177e4 | 247 | { |
9c1721aa | 248 | __raw_read_unlock(lock); |
1da177e4 | 249 | } |
9c1721aa | 250 | EXPORT_SYMBOL(_raw_read_unlock); |
892a7c67 | 251 | #endif |
1da177e4 | 252 | |
6beb0009 | 253 | #ifndef CONFIG_INLINE_READ_UNLOCK_IRQRESTORE |
9c1721aa | 254 | void __lockfunc _raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags) |
1da177e4 | 255 | { |
9c1721aa | 256 | __raw_read_unlock_irqrestore(lock, flags); |
1da177e4 | 257 | } |
9c1721aa | 258 | EXPORT_SYMBOL(_raw_read_unlock_irqrestore); |
892a7c67 | 259 | #endif |
1da177e4 | 260 | |
6beb0009 | 261 | #ifndef CONFIG_INLINE_READ_UNLOCK_IRQ |
9c1721aa | 262 | void __lockfunc _raw_read_unlock_irq(rwlock_t *lock) |
1da177e4 | 263 | { |
9c1721aa | 264 | __raw_read_unlock_irq(lock); |
1da177e4 | 265 | } |
9c1721aa | 266 | EXPORT_SYMBOL(_raw_read_unlock_irq); |
892a7c67 | 267 | #endif |
1da177e4 | 268 | |
6beb0009 | 269 | #ifndef CONFIG_INLINE_READ_UNLOCK_BH |
9c1721aa | 270 | void __lockfunc _raw_read_unlock_bh(rwlock_t *lock) |
1da177e4 | 271 | { |
9c1721aa | 272 | __raw_read_unlock_bh(lock); |
1da177e4 | 273 | } |
9c1721aa | 274 | EXPORT_SYMBOL(_raw_read_unlock_bh); |
892a7c67 | 275 | #endif |
1da177e4 | 276 | |
b7b40ade | 277 | #ifndef CONFIG_INLINE_WRITE_TRYLOCK |
9c1721aa | 278 | int __lockfunc _raw_write_trylock(rwlock_t *lock) |
b7b40ade | 279 | { |
9c1721aa | 280 | return __raw_write_trylock(lock); |
b7b40ade | 281 | } |
9c1721aa | 282 | EXPORT_SYMBOL(_raw_write_trylock); |
b7b40ade TG |
283 | #endif |
284 | ||
285 | #ifndef CONFIG_INLINE_WRITE_LOCK | |
9c1721aa | 286 | void __lockfunc _raw_write_lock(rwlock_t *lock) |
b7b40ade | 287 | { |
9c1721aa | 288 | __raw_write_lock(lock); |
b7b40ade | 289 | } |
9c1721aa | 290 | EXPORT_SYMBOL(_raw_write_lock); |
b7b40ade TG |
291 | #endif |
292 | ||
293 | #ifndef CONFIG_INLINE_WRITE_LOCK_IRQSAVE | |
9c1721aa | 294 | unsigned long __lockfunc _raw_write_lock_irqsave(rwlock_t *lock) |
b7b40ade | 295 | { |
9c1721aa | 296 | return __raw_write_lock_irqsave(lock); |
b7b40ade | 297 | } |
9c1721aa | 298 | EXPORT_SYMBOL(_raw_write_lock_irqsave); |
b7b40ade TG |
299 | #endif |
300 | ||
301 | #ifndef CONFIG_INLINE_WRITE_LOCK_IRQ | |
9c1721aa | 302 | void __lockfunc _raw_write_lock_irq(rwlock_t *lock) |
b7b40ade | 303 | { |
9c1721aa | 304 | __raw_write_lock_irq(lock); |
b7b40ade | 305 | } |
9c1721aa | 306 | EXPORT_SYMBOL(_raw_write_lock_irq); |
b7b40ade TG |
307 | #endif |
308 | ||
309 | #ifndef CONFIG_INLINE_WRITE_LOCK_BH | |
9c1721aa | 310 | void __lockfunc _raw_write_lock_bh(rwlock_t *lock) |
b7b40ade | 311 | { |
9c1721aa | 312 | __raw_write_lock_bh(lock); |
b7b40ade | 313 | } |
9c1721aa | 314 | EXPORT_SYMBOL(_raw_write_lock_bh); |
b7b40ade TG |
315 | #endif |
316 | ||
317 | #ifndef CONFIG_INLINE_WRITE_UNLOCK | |
9c1721aa | 318 | void __lockfunc _raw_write_unlock(rwlock_t *lock) |
b7b40ade | 319 | { |
9c1721aa | 320 | __raw_write_unlock(lock); |
b7b40ade | 321 | } |
9c1721aa | 322 | EXPORT_SYMBOL(_raw_write_unlock); |
b7b40ade TG |
323 | #endif |
324 | ||
6beb0009 | 325 | #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE |
9c1721aa | 326 | void __lockfunc _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags) |
1da177e4 | 327 | { |
9c1721aa | 328 | __raw_write_unlock_irqrestore(lock, flags); |
1da177e4 | 329 | } |
9c1721aa | 330 | EXPORT_SYMBOL(_raw_write_unlock_irqrestore); |
892a7c67 | 331 | #endif |
1da177e4 | 332 | |
6beb0009 | 333 | #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQ |
9c1721aa | 334 | void __lockfunc _raw_write_unlock_irq(rwlock_t *lock) |
1da177e4 | 335 | { |
9c1721aa | 336 | __raw_write_unlock_irq(lock); |
1da177e4 | 337 | } |
9c1721aa | 338 | EXPORT_SYMBOL(_raw_write_unlock_irq); |
892a7c67 | 339 | #endif |
1da177e4 | 340 | |
6beb0009 | 341 | #ifndef CONFIG_INLINE_WRITE_UNLOCK_BH |
9c1721aa | 342 | void __lockfunc _raw_write_unlock_bh(rwlock_t *lock) |
1da177e4 | 343 | { |
9c1721aa | 344 | __raw_write_unlock_bh(lock); |
1da177e4 | 345 | } |
9c1721aa | 346 | EXPORT_SYMBOL(_raw_write_unlock_bh); |
892a7c67 | 347 | #endif |
1da177e4 | 348 | |
b7b40ade TG |
349 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
350 | ||
9c1721aa | 351 | void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass) |
1da177e4 | 352 | { |
b7b40ade TG |
353 | preempt_disable(); |
354 | spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_); | |
9828ea9d | 355 | LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock); |
1da177e4 | 356 | } |
9c1721aa | 357 | EXPORT_SYMBOL(_raw_spin_lock_nested); |
b7b40ade | 358 | |
9c1721aa | 359 | unsigned long __lockfunc _raw_spin_lock_irqsave_nested(raw_spinlock_t *lock, |
b7b40ade TG |
360 | int subclass) |
361 | { | |
362 | unsigned long flags; | |
363 | ||
364 | local_irq_save(flags); | |
365 | preempt_disable(); | |
366 | spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_); | |
9828ea9d TG |
367 | LOCK_CONTENDED_FLAGS(lock, do_raw_spin_trylock, do_raw_spin_lock, |
368 | do_raw_spin_lock_flags, &flags); | |
b7b40ade TG |
369 | return flags; |
370 | } | |
9c1721aa | 371 | EXPORT_SYMBOL(_raw_spin_lock_irqsave_nested); |
b7b40ade | 372 | |
9c1721aa | 373 | void __lockfunc _raw_spin_lock_nest_lock(raw_spinlock_t *lock, |
b7b40ade TG |
374 | struct lockdep_map *nest_lock) |
375 | { | |
376 | preempt_disable(); | |
377 | spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_); | |
9828ea9d | 378 | LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock); |
b7b40ade | 379 | } |
9c1721aa | 380 | EXPORT_SYMBOL(_raw_spin_lock_nest_lock); |
b7b40ade | 381 | |
892a7c67 | 382 | #endif |
1da177e4 | 383 | |
0764d23c | 384 | notrace int in_lock_functions(unsigned long addr) |
1da177e4 LT |
385 | { |
386 | /* Linker adds these: start and end of __lockfunc functions */ | |
387 | extern char __lock_text_start[], __lock_text_end[]; | |
388 | ||
389 | return addr >= (unsigned long)__lock_text_start | |
390 | && addr < (unsigned long)__lock_text_end; | |
391 | } | |
392 | EXPORT_SYMBOL(in_lock_functions); |