spinlock.c 9.5 KB

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