spinlock.c 10 KB

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