spinlock.c 9.5 KB

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