spinlock.c 8.6 KB

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