spinlock.c 10 KB

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