spinlock.h 14 KB

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