spinlock.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_SPINLOCK_H
  3. #define __LINUX_SPINLOCK_H
  4. /*
  5. * include/linux/spinlock.h - generic spinlock/rwlock declarations
  6. *
  7. * here's the role of the various spinlock/rwlock related include files:
  8. *
  9. * on SMP builds:
  10. *
  11. * asm/spinlock_types.h: contains the arch_spinlock_t/arch_rwlock_t and the
  12. * initializers
  13. *
  14. * linux/spinlock_types.h:
  15. * defines the generic type and initializers
  16. *
  17. * asm/spinlock.h: contains the arch_spin_*()/etc. lowlevel
  18. * implementations, mostly inline assembly code
  19. *
  20. * (also included on UP-debug builds:)
  21. *
  22. * linux/spinlock_api_smp.h:
  23. * contains the prototypes for the _spin_*() APIs.
  24. *
  25. * linux/spinlock.h: builds the final spin_*() APIs.
  26. *
  27. * on UP builds:
  28. *
  29. * linux/spinlock_type_up.h:
  30. * contains the generic, simplified UP spinlock type.
  31. * (which is an empty structure on non-debug builds)
  32. *
  33. * linux/spinlock_types.h:
  34. * defines the generic type and initializers
  35. *
  36. * linux/spinlock_up.h:
  37. * contains the arch_spin_*()/etc. version of UP
  38. * builds. (which are NOPs on non-debug, non-preempt
  39. * builds)
  40. *
  41. * (included on UP-non-debug builds:)
  42. *
  43. * linux/spinlock_api_up.h:
  44. * builds the _spin_*() APIs.
  45. *
  46. * linux/spinlock.h: builds the final spin_*() APIs.
  47. */
  48. #include <linux/typecheck.h>
  49. #include <linux/preempt.h>
  50. #include <linux/linkage.h>
  51. #include <linux/compiler.h>
  52. #include <linux/irqflags.h>
  53. #include <linux/thread_info.h>
  54. #include <linux/kernel.h>
  55. #include <linux/stringify.h>
  56. #include <linux/bottom_half.h>
  57. #include <linux/lockdep.h>
  58. #include <asm/barrier.h>
  59. #include <asm/mmiowb.h>
  60. /*
  61. * Must define these before including other files, inline functions need them
  62. */
  63. #define LOCK_SECTION_NAME ".text..lock."KBUILD_BASENAME
  64. #define LOCK_SECTION_START(extra) \
  65. ".subsection 1\n\t" \
  66. extra \
  67. ".ifndef " LOCK_SECTION_NAME "\n\t" \
  68. LOCK_SECTION_NAME ":\n\t" \
  69. ".endif\n"
  70. #define LOCK_SECTION_END \
  71. ".previous\n\t"
  72. #define __lockfunc __section(".spinlock.text")
  73. /*
  74. * Pull the arch_spinlock_t and arch_rwlock_t definitions:
  75. */
  76. #include <linux/spinlock_types.h>
  77. /*
  78. * Pull the arch_spin*() functions/declarations (UP-nondebug doesn't need them):
  79. */
  80. #ifdef CONFIG_SMP
  81. # include <asm/spinlock.h>
  82. #else
  83. # include <linux/spinlock_up.h>
  84. #endif
  85. #ifdef CONFIG_DEBUG_SPINLOCK
  86. extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  87. struct lock_class_key *key, short inner);
  88. # define raw_spin_lock_init(lock) \
  89. do { \
  90. static struct lock_class_key __key; \
  91. \
  92. __raw_spin_lock_init((lock), #lock, &__key, LD_WAIT_SPIN); \
  93. } while (0)
  94. #else
  95. # define raw_spin_lock_init(lock) \
  96. do { *(lock) = __RAW_SPIN_LOCK_UNLOCKED(lock); } while (0)
  97. #endif
  98. #define raw_spin_is_locked(lock) arch_spin_is_locked(&(lock)->raw_lock)
  99. #ifdef arch_spin_is_contended
  100. #define raw_spin_is_contended(lock) arch_spin_is_contended(&(lock)->raw_lock)
  101. #else
  102. #define raw_spin_is_contended(lock) (((void)(lock), 0))
  103. #endif /*arch_spin_is_contended*/
  104. /*
  105. * smp_mb__after_spinlock() provides the equivalent of a full memory barrier
  106. * between program-order earlier lock acquisitions and program-order later
  107. * memory accesses.
  108. *
  109. * This guarantees that the following two properties hold:
  110. *
  111. * 1) Given the snippet:
  112. *
  113. * { X = 0; Y = 0; }
  114. *
  115. * CPU0 CPU1
  116. *
  117. * WRITE_ONCE(X, 1); WRITE_ONCE(Y, 1);
  118. * spin_lock(S); smp_mb();
  119. * smp_mb__after_spinlock(); r1 = READ_ONCE(X);
  120. * r0 = READ_ONCE(Y);
  121. * spin_unlock(S);
  122. *
  123. * it is forbidden that CPU0 does not observe CPU1's store to Y (r0 = 0)
  124. * and CPU1 does not observe CPU0's store to X (r1 = 0); see the comments
  125. * preceding the call to smp_mb__after_spinlock() in __schedule() and in
  126. * try_to_wake_up().
  127. *
  128. * 2) Given the snippet:
  129. *
  130. * { X = 0; Y = 0; }
  131. *
  132. * CPU0 CPU1 CPU2
  133. *
  134. * spin_lock(S); spin_lock(S); r1 = READ_ONCE(Y);
  135. * WRITE_ONCE(X, 1); smp_mb__after_spinlock(); smp_rmb();
  136. * spin_unlock(S); r0 = READ_ONCE(X); r2 = READ_ONCE(X);
  137. * WRITE_ONCE(Y, 1);
  138. * spin_unlock(S);
  139. *
  140. * it is forbidden that CPU0's critical section executes before CPU1's
  141. * critical section (r0 = 1), CPU2 observes CPU1's store to Y (r1 = 1)
  142. * and CPU2 does not observe CPU0's store to X (r2 = 0); see the comments
  143. * preceding the calls to smp_rmb() in try_to_wake_up() for similar
  144. * snippets but "projected" onto two CPUs.
  145. *
  146. * Property (2) upgrades the lock to an RCsc lock.
  147. *
  148. * Since most load-store architectures implement ACQUIRE with an smp_mb() after
  149. * the LL/SC loop, they need no further barriers. Similarly all our TSO
  150. * architectures imply an smp_mb() for each atomic instruction and equally don't
  151. * need more.
  152. *
  153. * Architectures that can implement ACQUIRE better need to take care.
  154. */
  155. #ifndef smp_mb__after_spinlock
  156. #define smp_mb__after_spinlock() do { } while (0)
  157. #endif
  158. #ifdef CONFIG_DEBUG_SPINLOCK
  159. extern void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock);
  160. #define do_raw_spin_lock_flags(lock, flags) do_raw_spin_lock(lock)
  161. extern int do_raw_spin_trylock(raw_spinlock_t *lock);
  162. extern void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock);
  163. #else
  164. static inline void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock)
  165. {
  166. __acquire(lock);
  167. arch_spin_lock(&lock->raw_lock);
  168. mmiowb_spin_lock();
  169. }
  170. #ifndef arch_spin_lock_flags
  171. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  172. #endif
  173. static inline void
  174. do_raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long *flags) __acquires(lock)
  175. {
  176. __acquire(lock);
  177. arch_spin_lock_flags(&lock->raw_lock, *flags);
  178. mmiowb_spin_lock();
  179. }
  180. static inline int do_raw_spin_trylock(raw_spinlock_t *lock)
  181. {
  182. int ret = arch_spin_trylock(&(lock)->raw_lock);
  183. if (ret)
  184. mmiowb_spin_lock();
  185. return ret;
  186. }
  187. static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  188. {
  189. mmiowb_spin_unlock();
  190. arch_spin_unlock(&lock->raw_lock);
  191. __release(lock);
  192. }
  193. #endif
  194. /*
  195. * Define the various spin_lock methods. Note we define these
  196. * regardless of whether CONFIG_SMP or CONFIG_PREEMPTION are set. The
  197. * various methods are defined as nops in the case they are not
  198. * required.
  199. */
  200. #define raw_spin_trylock(lock) __cond_lock(lock, _raw_spin_trylock(lock))
  201. #define raw_spin_lock(lock) _raw_spin_lock(lock)
  202. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  203. # define raw_spin_lock_nested(lock, subclass) \
  204. _raw_spin_lock_nested(lock, subclass)
  205. # define raw_spin_lock_nest_lock(lock, nest_lock) \
  206. do { \
  207. typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\
  208. _raw_spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  209. } while (0)
  210. #else
  211. /*
  212. * Always evaluate the 'subclass' argument to avoid that the compiler
  213. * warns about set-but-not-used variables when building with
  214. * CONFIG_DEBUG_LOCK_ALLOC=n and with W=1.
  215. */
  216. # define raw_spin_lock_nested(lock, subclass) \
  217. _raw_spin_lock(((void)(subclass), (lock)))
  218. # define raw_spin_lock_nest_lock(lock, nest_lock) _raw_spin_lock(lock)
  219. #endif
  220. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  221. #define raw_spin_lock_irqsave(lock, flags) \
  222. do { \
  223. typecheck(unsigned long, flags); \
  224. flags = _raw_spin_lock_irqsave(lock); \
  225. } while (0)
  226. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  227. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  228. do { \
  229. typecheck(unsigned long, flags); \
  230. flags = _raw_spin_lock_irqsave_nested(lock, subclass); \
  231. } while (0)
  232. #else
  233. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  234. do { \
  235. typecheck(unsigned long, flags); \
  236. flags = _raw_spin_lock_irqsave(lock); \
  237. } while (0)
  238. #endif
  239. #else
  240. #define raw_spin_lock_irqsave(lock, flags) \
  241. do { \
  242. typecheck(unsigned long, flags); \
  243. _raw_spin_lock_irqsave(lock, flags); \
  244. } while (0)
  245. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  246. raw_spin_lock_irqsave(lock, flags)
  247. #endif
  248. #define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock)
  249. #define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock)
  250. #define raw_spin_unlock(lock) _raw_spin_unlock(lock)
  251. #define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock)
  252. #define raw_spin_unlock_irqrestore(lock, flags) \
  253. do { \
  254. typecheck(unsigned long, flags); \
  255. _raw_spin_unlock_irqrestore(lock, flags); \
  256. } while (0)
  257. #define raw_spin_unlock_bh(lock) _raw_spin_unlock_bh(lock)
  258. #define raw_spin_trylock_bh(lock) \
  259. __cond_lock(lock, _raw_spin_trylock_bh(lock))
  260. #define raw_spin_trylock_irq(lock) \
  261. ({ \
  262. local_irq_disable(); \
  263. raw_spin_trylock(lock) ? \
  264. 1 : ({ local_irq_enable(); 0; }); \
  265. })
  266. #define raw_spin_trylock_irqsave(lock, flags) \
  267. ({ \
  268. local_irq_save(flags); \
  269. raw_spin_trylock(lock) ? \
  270. 1 : ({ local_irq_restore(flags); 0; }); \
  271. })
  272. /* Include rwlock functions */
  273. #include <linux/rwlock.h>
  274. /*
  275. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  276. */
  277. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  278. # include <linux/spinlock_api_smp.h>
  279. #else
  280. # include <linux/spinlock_api_up.h>
  281. #endif
  282. /*
  283. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  284. */
  285. static __always_inline raw_spinlock_t *spinlock_check(spinlock_t *lock)
  286. {
  287. return &lock->rlock;
  288. }
  289. #ifdef CONFIG_DEBUG_SPINLOCK
  290. # define spin_lock_init(lock) \
  291. do { \
  292. static struct lock_class_key __key; \
  293. \
  294. __raw_spin_lock_init(spinlock_check(lock), \
  295. #lock, &__key, LD_WAIT_CONFIG); \
  296. } while (0)
  297. #else
  298. # define spin_lock_init(_lock) \
  299. do { \
  300. spinlock_check(_lock); \
  301. *(_lock) = __SPIN_LOCK_UNLOCKED(_lock); \
  302. } while (0)
  303. #endif
  304. static __always_inline void spin_lock(spinlock_t *lock)
  305. {
  306. raw_spin_lock(&lock->rlock);
  307. }
  308. static __always_inline void spin_lock_bh(spinlock_t *lock)
  309. {
  310. raw_spin_lock_bh(&lock->rlock);
  311. }
  312. static __always_inline int spin_trylock(spinlock_t *lock)
  313. {
  314. return raw_spin_trylock(&lock->rlock);
  315. }
  316. #define spin_lock_nested(lock, subclass) \
  317. do { \
  318. raw_spin_lock_nested(spinlock_check(lock), subclass); \
  319. } while (0)
  320. #define spin_lock_nest_lock(lock, nest_lock) \
  321. do { \
  322. raw_spin_lock_nest_lock(spinlock_check(lock), nest_lock); \
  323. } while (0)
  324. static __always_inline void spin_lock_irq(spinlock_t *lock)
  325. {
  326. raw_spin_lock_irq(&lock->rlock);
  327. }
  328. #define spin_lock_irqsave(lock, flags) \
  329. do { \
  330. raw_spin_lock_irqsave(spinlock_check(lock), flags); \
  331. } while (0)
  332. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  333. do { \
  334. raw_spin_lock_irqsave_nested(spinlock_check(lock), flags, subclass); \
  335. } while (0)
  336. static __always_inline void spin_unlock(spinlock_t *lock)
  337. {
  338. raw_spin_unlock(&lock->rlock);
  339. }
  340. static __always_inline void spin_unlock_bh(spinlock_t *lock)
  341. {
  342. raw_spin_unlock_bh(&lock->rlock);
  343. }
  344. static __always_inline void spin_unlock_irq(spinlock_t *lock)
  345. {
  346. raw_spin_unlock_irq(&lock->rlock);
  347. }
  348. static __always_inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
  349. {
  350. raw_spin_unlock_irqrestore(&lock->rlock, flags);
  351. }
  352. static __always_inline int spin_trylock_bh(spinlock_t *lock)
  353. {
  354. return raw_spin_trylock_bh(&lock->rlock);
  355. }
  356. static __always_inline int spin_trylock_irq(spinlock_t *lock)
  357. {
  358. return raw_spin_trylock_irq(&lock->rlock);
  359. }
  360. #define spin_trylock_irqsave(lock, flags) \
  361. ({ \
  362. raw_spin_trylock_irqsave(spinlock_check(lock), flags); \
  363. })
  364. /**
  365. * spin_is_locked() - Check whether a spinlock is locked.
  366. * @lock: Pointer to the spinlock.
  367. *
  368. * This function is NOT required to provide any memory ordering
  369. * guarantees; it could be used for debugging purposes or, when
  370. * additional synchronization is needed, accompanied with other
  371. * constructs (memory barriers) enforcing the synchronization.
  372. *
  373. * Returns: 1 if @lock is locked, 0 otherwise.
  374. *
  375. * Note that the function only tells you that the spinlock is
  376. * seen to be locked, not that it is locked on your CPU.
  377. *
  378. * Further, on CONFIG_SMP=n builds with CONFIG_DEBUG_SPINLOCK=n,
  379. * the return value is always 0 (see include/linux/spinlock_up.h).
  380. * Therefore you should not rely heavily on the return value.
  381. */
  382. static __always_inline int spin_is_locked(spinlock_t *lock)
  383. {
  384. return raw_spin_is_locked(&lock->rlock);
  385. }
  386. static __always_inline int spin_is_contended(spinlock_t *lock)
  387. {
  388. return raw_spin_is_contended(&lock->rlock);
  389. }
  390. #define assert_spin_locked(lock) assert_raw_spin_locked(&(lock)->rlock)
  391. /*
  392. * Pull the atomic_t declaration:
  393. * (asm-mips/atomic.h needs above definitions)
  394. */
  395. #include <linux/atomic.h>
  396. /**
  397. * atomic_dec_and_lock - lock on reaching reference count zero
  398. * @atomic: the atomic counter
  399. * @lock: the spinlock in question
  400. *
  401. * Decrements @atomic by 1. If the result is 0, returns true and locks
  402. * @lock. Returns false for all other cases.
  403. */
  404. extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  405. #define atomic_dec_and_lock(atomic, lock) \
  406. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  407. extern int _atomic_dec_and_lock_irqsave(atomic_t *atomic, spinlock_t *lock,
  408. unsigned long *flags);
  409. #define atomic_dec_and_lock_irqsave(atomic, lock, flags) \
  410. __cond_lock(lock, _atomic_dec_and_lock_irqsave(atomic, lock, &(flags)))
  411. int __alloc_bucket_spinlocks(spinlock_t **locks, unsigned int *lock_mask,
  412. size_t max_size, unsigned int cpu_mult,
  413. gfp_t gfp, const char *name,
  414. struct lock_class_key *key);
  415. #define alloc_bucket_spinlocks(locks, lock_mask, max_size, cpu_mult, gfp) \
  416. ({ \
  417. static struct lock_class_key key; \
  418. int ret; \
  419. \
  420. ret = __alloc_bucket_spinlocks(locks, lock_mask, max_size, \
  421. cpu_mult, gfp, #locks, &key); \
  422. ret; \
  423. })
  424. void free_bucket_spinlocks(spinlock_t *locks);
  425. #endif /* __LINUX_SPINLOCK_H */