spinlock.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. /*
  34. * We build the __lock_function inlines here. They are too large for
  35. * inlining all over the place, but here is only one user per function
  36. * which embedds them into the calling _lock_function below.
  37. *
  38. * This could be a long-held lock. We both prepare to spin for a long
  39. * time (making _this_ CPU preemptable if possible), and we also signal
  40. * towards that other CPU that it should break the lock ASAP.
  41. */
  42. #define BUILD_LOCK_OPS(op, locktype) \
  43. void __lockfunc __##op##_lock(locktype##_t *lock) \
  44. { \
  45. for (;;) { \
  46. preempt_disable(); \
  47. if (likely(_raw_##op##_trylock(lock))) \
  48. break; \
  49. preempt_enable(); \
  50. \
  51. if (!(lock)->break_lock) \
  52. (lock)->break_lock = 1; \
  53. while (!op##_can_lock(lock) && (lock)->break_lock) \
  54. arch_##op##_relax(&lock->raw_lock); \
  55. } \
  56. (lock)->break_lock = 0; \
  57. } \
  58. \
  59. unsigned long __lockfunc __##op##_lock_irqsave(locktype##_t *lock) \
  60. { \
  61. unsigned long flags; \
  62. \
  63. for (;;) { \
  64. preempt_disable(); \
  65. local_irq_save(flags); \
  66. if (likely(_raw_##op##_trylock(lock))) \
  67. break; \
  68. local_irq_restore(flags); \
  69. preempt_enable(); \
  70. \
  71. if (!(lock)->break_lock) \
  72. (lock)->break_lock = 1; \
  73. while (!op##_can_lock(lock) && (lock)->break_lock) \
  74. arch_##op##_relax(&lock->raw_lock); \
  75. } \
  76. (lock)->break_lock = 0; \
  77. return flags; \
  78. } \
  79. \
  80. void __lockfunc __##op##_lock_irq(locktype##_t *lock) \
  81. { \
  82. _##op##_lock_irqsave(lock); \
  83. } \
  84. \
  85. void __lockfunc __##op##_lock_bh(locktype##_t *lock) \
  86. { \
  87. unsigned long flags; \
  88. \
  89. /* */ \
  90. /* Careful: we must exclude softirqs too, hence the */ \
  91. /* irq-disabling. We use the generic preemption-aware */ \
  92. /* function: */ \
  93. /**/ \
  94. flags = _##op##_lock_irqsave(lock); \
  95. local_bh_disable(); \
  96. local_irq_restore(flags); \
  97. } \
  98. /*
  99. * Build preemption-friendly versions of the following
  100. * lock-spinning functions:
  101. *
  102. * __[spin|read|write]_lock()
  103. * __[spin|read|write]_lock_irq()
  104. * __[spin|read|write]_lock_irqsave()
  105. * __[spin|read|write]_lock_bh()
  106. */
  107. BUILD_LOCK_OPS(spin, spinlock);
  108. BUILD_LOCK_OPS(read, rwlock);
  109. BUILD_LOCK_OPS(write, rwlock);
  110. #endif
  111. #ifndef CONFIG_INLINE_SPIN_TRYLOCK
  112. int __lockfunc _spin_trylock(spinlock_t *lock)
  113. {
  114. return __spin_trylock(lock);
  115. }
  116. EXPORT_SYMBOL(_spin_trylock);
  117. #endif
  118. #ifndef CONFIG_INLINE_SPIN_TRYLOCK_BH
  119. int __lockfunc _spin_trylock_bh(spinlock_t *lock)
  120. {
  121. return __spin_trylock_bh(lock);
  122. }
  123. EXPORT_SYMBOL(_spin_trylock_bh);
  124. #endif
  125. #ifndef CONFIG_INLINE_SPIN_LOCK
  126. void __lockfunc _spin_lock(spinlock_t *lock)
  127. {
  128. __spin_lock(lock);
  129. }
  130. EXPORT_SYMBOL(_spin_lock);
  131. #endif
  132. #ifndef CONFIG_INLINE_SPIN_LOCK_IRQSAVE
  133. unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock)
  134. {
  135. return __spin_lock_irqsave(lock);
  136. }
  137. EXPORT_SYMBOL(_spin_lock_irqsave);
  138. #endif
  139. #ifndef CONFIG_INLINE_SPIN_LOCK_IRQ
  140. void __lockfunc _spin_lock_irq(spinlock_t *lock)
  141. {
  142. __spin_lock_irq(lock);
  143. }
  144. EXPORT_SYMBOL(_spin_lock_irq);
  145. #endif
  146. #ifndef CONFIG_INLINE_SPIN_LOCK_BH
  147. void __lockfunc _spin_lock_bh(spinlock_t *lock)
  148. {
  149. __spin_lock_bh(lock);
  150. }
  151. EXPORT_SYMBOL(_spin_lock_bh);
  152. #endif
  153. #ifndef CONFIG_INLINE_SPIN_UNLOCK
  154. void __lockfunc _spin_unlock(spinlock_t *lock)
  155. {
  156. __spin_unlock(lock);
  157. }
  158. EXPORT_SYMBOL(_spin_unlock);
  159. #endif
  160. #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE
  161. void __lockfunc _spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
  162. {
  163. __spin_unlock_irqrestore(lock, flags);
  164. }
  165. EXPORT_SYMBOL(_spin_unlock_irqrestore);
  166. #endif
  167. #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ
  168. void __lockfunc _spin_unlock_irq(spinlock_t *lock)
  169. {
  170. __spin_unlock_irq(lock);
  171. }
  172. EXPORT_SYMBOL(_spin_unlock_irq);
  173. #endif
  174. #ifndef CONFIG_INLINE_SPIN_UNLOCK_BH
  175. void __lockfunc _spin_unlock_bh(spinlock_t *lock)
  176. {
  177. __spin_unlock_bh(lock);
  178. }
  179. EXPORT_SYMBOL(_spin_unlock_bh);
  180. #endif
  181. #ifndef CONFIG_INLINE_READ_TRYLOCK
  182. int __lockfunc _read_trylock(rwlock_t *lock)
  183. {
  184. return __read_trylock(lock);
  185. }
  186. EXPORT_SYMBOL(_read_trylock);
  187. #endif
  188. #ifndef CONFIG_INLINE_READ_LOCK
  189. void __lockfunc _read_lock(rwlock_t *lock)
  190. {
  191. __read_lock(lock);
  192. }
  193. EXPORT_SYMBOL(_read_lock);
  194. #endif
  195. #ifndef CONFIG_INLINE_READ_LOCK_IRQSAVE
  196. unsigned long __lockfunc _read_lock_irqsave(rwlock_t *lock)
  197. {
  198. return __read_lock_irqsave(lock);
  199. }
  200. EXPORT_SYMBOL(_read_lock_irqsave);
  201. #endif
  202. #ifndef CONFIG_INLINE_READ_LOCK_IRQ
  203. void __lockfunc _read_lock_irq(rwlock_t *lock)
  204. {
  205. __read_lock_irq(lock);
  206. }
  207. EXPORT_SYMBOL(_read_lock_irq);
  208. #endif
  209. #ifndef CONFIG_INLINE_READ_LOCK_BH
  210. void __lockfunc _read_lock_bh(rwlock_t *lock)
  211. {
  212. __read_lock_bh(lock);
  213. }
  214. EXPORT_SYMBOL(_read_lock_bh);
  215. #endif
  216. #ifndef CONFIG_INLINE_READ_UNLOCK
  217. void __lockfunc _read_unlock(rwlock_t *lock)
  218. {
  219. __read_unlock(lock);
  220. }
  221. EXPORT_SYMBOL(_read_unlock);
  222. #endif
  223. #ifndef CONFIG_INLINE_READ_UNLOCK_IRQRESTORE
  224. void __lockfunc _read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
  225. {
  226. __read_unlock_irqrestore(lock, flags);
  227. }
  228. EXPORT_SYMBOL(_read_unlock_irqrestore);
  229. #endif
  230. #ifndef CONFIG_INLINE_READ_UNLOCK_IRQ
  231. void __lockfunc _read_unlock_irq(rwlock_t *lock)
  232. {
  233. __read_unlock_irq(lock);
  234. }
  235. EXPORT_SYMBOL(_read_unlock_irq);
  236. #endif
  237. #ifndef CONFIG_INLINE_READ_UNLOCK_BH
  238. void __lockfunc _read_unlock_bh(rwlock_t *lock)
  239. {
  240. __read_unlock_bh(lock);
  241. }
  242. EXPORT_SYMBOL(_read_unlock_bh);
  243. #endif
  244. #ifndef CONFIG_INLINE_WRITE_TRYLOCK
  245. int __lockfunc _write_trylock(rwlock_t *lock)
  246. {
  247. return __write_trylock(lock);
  248. }
  249. EXPORT_SYMBOL(_write_trylock);
  250. #endif
  251. #ifndef CONFIG_INLINE_WRITE_LOCK
  252. void __lockfunc _write_lock(rwlock_t *lock)
  253. {
  254. __write_lock(lock);
  255. }
  256. EXPORT_SYMBOL(_write_lock);
  257. #endif
  258. #ifndef CONFIG_INLINE_WRITE_LOCK_IRQSAVE
  259. unsigned long __lockfunc _write_lock_irqsave(rwlock_t *lock)
  260. {
  261. return __write_lock_irqsave(lock);
  262. }
  263. EXPORT_SYMBOL(_write_lock_irqsave);
  264. #endif
  265. #ifndef CONFIG_INLINE_WRITE_LOCK_IRQ
  266. void __lockfunc _write_lock_irq(rwlock_t *lock)
  267. {
  268. __write_lock_irq(lock);
  269. }
  270. EXPORT_SYMBOL(_write_lock_irq);
  271. #endif
  272. #ifndef CONFIG_INLINE_WRITE_LOCK_BH
  273. void __lockfunc _write_lock_bh(rwlock_t *lock)
  274. {
  275. __write_lock_bh(lock);
  276. }
  277. EXPORT_SYMBOL(_write_lock_bh);
  278. #endif
  279. #ifndef CONFIG_INLINE_WRITE_UNLOCK
  280. void __lockfunc _write_unlock(rwlock_t *lock)
  281. {
  282. __write_unlock(lock);
  283. }
  284. EXPORT_SYMBOL(_write_unlock);
  285. #endif
  286. #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE
  287. void __lockfunc _write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
  288. {
  289. __write_unlock_irqrestore(lock, flags);
  290. }
  291. EXPORT_SYMBOL(_write_unlock_irqrestore);
  292. #endif
  293. #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQ
  294. void __lockfunc _write_unlock_irq(rwlock_t *lock)
  295. {
  296. __write_unlock_irq(lock);
  297. }
  298. EXPORT_SYMBOL(_write_unlock_irq);
  299. #endif
  300. #ifndef CONFIG_INLINE_WRITE_UNLOCK_BH
  301. void __lockfunc _write_unlock_bh(rwlock_t *lock)
  302. {
  303. __write_unlock_bh(lock);
  304. }
  305. EXPORT_SYMBOL(_write_unlock_bh);
  306. #endif
  307. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  308. void __lockfunc _spin_lock_nested(spinlock_t *lock, int subclass)
  309. {
  310. preempt_disable();
  311. spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  312. LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
  313. }
  314. EXPORT_SYMBOL(_spin_lock_nested);
  315. unsigned long __lockfunc _spin_lock_irqsave_nested(spinlock_t *lock,
  316. int subclass)
  317. {
  318. unsigned long flags;
  319. local_irq_save(flags);
  320. preempt_disable();
  321. spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  322. LOCK_CONTENDED_FLAGS(lock, _raw_spin_trylock, _raw_spin_lock,
  323. _raw_spin_lock_flags, &flags);
  324. return flags;
  325. }
  326. EXPORT_SYMBOL(_spin_lock_irqsave_nested);
  327. void __lockfunc _spin_lock_nest_lock(spinlock_t *lock,
  328. struct lockdep_map *nest_lock)
  329. {
  330. preempt_disable();
  331. spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_);
  332. LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
  333. }
  334. EXPORT_SYMBOL(_spin_lock_nest_lock);
  335. #endif
  336. notrace int in_lock_functions(unsigned long addr)
  337. {
  338. /* Linker adds these: start and end of __lockfunc functions */
  339. extern char __lock_text_start[], __lock_text_end[];
  340. return addr >= (unsigned long)__lock_text_start
  341. && addr < (unsigned long)__lock_text_end;
  342. }
  343. EXPORT_SYMBOL(in_lock_functions);