spinlock.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #ifndef __LINUX_SPINLOCK_H
  2. #define __LINUX_SPINLOCK_H
  3. /*
  4. * include/linux/spinlock.h - generic spinlock/rwlock declarations
  5. *
  6. * here's the role of the various spinlock/rwlock related include files:
  7. *
  8. * on SMP builds:
  9. *
  10. * asm/spinlock_types.h: contains the arch_spinlock_t/arch_rwlock_t and the
  11. * initializers
  12. *
  13. * linux/spinlock_types.h:
  14. * defines the generic type and initializers
  15. *
  16. * asm/spinlock.h: contains the arch_spin_*()/etc. lowlevel
  17. * implementations, mostly inline assembly code
  18. *
  19. * (also included on UP-debug builds:)
  20. *
  21. * linux/spinlock_api_smp.h:
  22. * contains the prototypes for the _spin_*() APIs.
  23. *
  24. * linux/spinlock.h: builds the final spin_*() APIs.
  25. *
  26. * on UP builds:
  27. *
  28. * linux/spinlock_type_up.h:
  29. * contains the generic, simplified UP spinlock type.
  30. * (which is an empty structure on non-debug builds)
  31. *
  32. * linux/spinlock_types.h:
  33. * defines the generic type and initializers
  34. *
  35. * linux/spinlock_up.h:
  36. * contains the arch_spin_*()/etc. version of UP
  37. * builds. (which are NOPs on non-debug, non-preempt
  38. * builds)
  39. *
  40. * (included on UP-non-debug builds:)
  41. *
  42. * linux/spinlock_api_up.h:
  43. * builds the _spin_*() APIs.
  44. *
  45. * linux/spinlock.h: builds the final spin_*() APIs.
  46. */
  47. #include <linux/typecheck.h>
  48. #include <linux/preempt.h>
  49. #include <linux/linkage.h>
  50. #include <linux/compiler.h>
  51. #include <linux/irqflags.h>
  52. #include <linux/thread_info.h>
  53. #include <linux/kernel.h>
  54. #include <linux/stringify.h>
  55. #include <linux/bottom_half.h>
  56. #include <asm/barrier.h>
  57. /*
  58. * Must define these before including other files, inline functions need them
  59. */
  60. #define LOCK_SECTION_NAME ".text..lock."KBUILD_BASENAME
  61. #define LOCK_SECTION_START(extra) \
  62. ".subsection 1\n\t" \
  63. extra \
  64. ".ifndef " LOCK_SECTION_NAME "\n\t" \
  65. LOCK_SECTION_NAME ":\n\t" \
  66. ".endif\n"
  67. #define LOCK_SECTION_END \
  68. ".previous\n\t"
  69. #define __lockfunc __attribute__((section(".spinlock.text")))
  70. /*
  71. * Pull the arch_spinlock_t and arch_rwlock_t definitions:
  72. */
  73. #include <linux/spinlock_types.h>
  74. /*
  75. * Pull the arch_spin*() functions/declarations (UP-nondebug doesn't need them):
  76. */
  77. #ifdef CONFIG_SMP
  78. # include <asm/spinlock.h>
  79. #else
  80. # include <linux/spinlock_up.h>
  81. #endif
  82. #ifdef CONFIG_DEBUG_SPINLOCK
  83. extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  84. struct lock_class_key *key);
  85. # define raw_spin_lock_init(lock) \
  86. do { \
  87. static struct lock_class_key __key; \
  88. \
  89. __raw_spin_lock_init((lock), #lock, &__key); \
  90. } while (0)
  91. #else
  92. # define raw_spin_lock_init(lock) \
  93. do { *(lock) = __RAW_SPIN_LOCK_UNLOCKED(lock); } while (0)
  94. #endif
  95. #define raw_spin_is_locked(lock) arch_spin_is_locked(&(lock)->raw_lock)
  96. #ifdef CONFIG_GENERIC_LOCKBREAK
  97. #define raw_spin_is_contended(lock) ((lock)->break_lock)
  98. #else
  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. #endif
  105. /*
  106. * This barrier must provide two things:
  107. *
  108. * - it must guarantee a STORE before the spin_lock() is ordered against a
  109. * LOAD after it, see the comments at its two usage sites.
  110. *
  111. * - it must ensure the critical section is RCsc.
  112. *
  113. * The latter is important for cases where we observe values written by other
  114. * CPUs in spin-loops, without barriers, while being subject to scheduling.
  115. *
  116. * CPU0 CPU1 CPU2
  117. *
  118. * for (;;) {
  119. * if (READ_ONCE(X))
  120. * break;
  121. * }
  122. * X=1
  123. * <sched-out>
  124. * <sched-in>
  125. * r = X;
  126. *
  127. * without transitivity it could be that CPU1 observes X!=0 breaks the loop,
  128. * we get migrated and CPU2 sees X==0.
  129. *
  130. * Since most load-store architectures implement ACQUIRE with an smp_mb() after
  131. * the LL/SC loop, they need no further barriers. Similarly all our TSO
  132. * architectures imply an smp_mb() for each atomic instruction and equally don't
  133. * need more.
  134. *
  135. * Architectures that can implement ACQUIRE better need to take care.
  136. */
  137. #ifndef smp_mb__after_spinlock
  138. #define smp_mb__after_spinlock() do { } while (0)
  139. #endif
  140. /**
  141. * raw_spin_unlock_wait - wait until the spinlock gets unlocked
  142. * @lock: the spinlock in question.
  143. */
  144. #define raw_spin_unlock_wait(lock) arch_spin_unlock_wait(&(lock)->raw_lock)
  145. #ifdef CONFIG_DEBUG_SPINLOCK
  146. extern void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock);
  147. #define do_raw_spin_lock_flags(lock, flags) do_raw_spin_lock(lock)
  148. extern int do_raw_spin_trylock(raw_spinlock_t *lock);
  149. extern void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock);
  150. #else
  151. static inline void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock)
  152. {
  153. __acquire(lock);
  154. arch_spin_lock(&lock->raw_lock);
  155. }
  156. static inline void
  157. do_raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long *flags) __acquires(lock)
  158. {
  159. __acquire(lock);
  160. arch_spin_lock_flags(&lock->raw_lock, *flags);
  161. }
  162. static inline int do_raw_spin_trylock(raw_spinlock_t *lock)
  163. {
  164. return arch_spin_trylock(&(lock)->raw_lock);
  165. }
  166. static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  167. {
  168. arch_spin_unlock(&lock->raw_lock);
  169. __release(lock);
  170. }
  171. #endif
  172. /*
  173. * Define the various spin_lock methods. Note we define these
  174. * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The
  175. * various methods are defined as nops in the case they are not
  176. * required.
  177. */
  178. #define raw_spin_trylock(lock) __cond_lock(lock, _raw_spin_trylock(lock))
  179. #define raw_spin_lock(lock) _raw_spin_lock(lock)
  180. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  181. # define raw_spin_lock_nested(lock, subclass) \
  182. _raw_spin_lock_nested(lock, subclass)
  183. # define raw_spin_lock_nest_lock(lock, nest_lock) \
  184. do { \
  185. typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\
  186. _raw_spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  187. } while (0)
  188. #else
  189. /*
  190. * Always evaluate the 'subclass' argument to avoid that the compiler
  191. * warns about set-but-not-used variables when building with
  192. * CONFIG_DEBUG_LOCK_ALLOC=n and with W=1.
  193. */
  194. # define raw_spin_lock_nested(lock, subclass) \
  195. _raw_spin_lock(((void)(subclass), (lock)))
  196. # define raw_spin_lock_nest_lock(lock, nest_lock) _raw_spin_lock(lock)
  197. #endif
  198. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  199. #define raw_spin_lock_irqsave(lock, flags) \
  200. do { \
  201. typecheck(unsigned long, flags); \
  202. flags = _raw_spin_lock_irqsave(lock); \
  203. } while (0)
  204. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  205. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  206. do { \
  207. typecheck(unsigned long, flags); \
  208. flags = _raw_spin_lock_irqsave_nested(lock, subclass); \
  209. } while (0)
  210. #else
  211. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  212. do { \
  213. typecheck(unsigned long, flags); \
  214. flags = _raw_spin_lock_irqsave(lock); \
  215. } while (0)
  216. #endif
  217. #else
  218. #define raw_spin_lock_irqsave(lock, flags) \
  219. do { \
  220. typecheck(unsigned long, flags); \
  221. _raw_spin_lock_irqsave(lock, flags); \
  222. } while (0)
  223. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  224. raw_spin_lock_irqsave(lock, flags)
  225. #endif
  226. #define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock)
  227. #define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock)
  228. #define raw_spin_unlock(lock) _raw_spin_unlock(lock)
  229. #define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock)
  230. #define raw_spin_unlock_irqrestore(lock, flags) \
  231. do { \
  232. typecheck(unsigned long, flags); \
  233. _raw_spin_unlock_irqrestore(lock, flags); \
  234. } while (0)
  235. #define raw_spin_unlock_bh(lock) _raw_spin_unlock_bh(lock)
  236. #define raw_spin_trylock_bh(lock) \
  237. __cond_lock(lock, _raw_spin_trylock_bh(lock))
  238. #define raw_spin_trylock_irq(lock) \
  239. ({ \
  240. local_irq_disable(); \
  241. raw_spin_trylock(lock) ? \
  242. 1 : ({ local_irq_enable(); 0; }); \
  243. })
  244. #define raw_spin_trylock_irqsave(lock, flags) \
  245. ({ \
  246. local_irq_save(flags); \
  247. raw_spin_trylock(lock) ? \
  248. 1 : ({ local_irq_restore(flags); 0; }); \
  249. })
  250. /**
  251. * raw_spin_can_lock - would raw_spin_trylock() succeed?
  252. * @lock: the spinlock in question.
  253. */
  254. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  255. /* Include rwlock functions */
  256. #include <linux/rwlock.h>
  257. /*
  258. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  259. */
  260. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  261. # include <linux/spinlock_api_smp.h>
  262. #else
  263. # include <linux/spinlock_api_up.h>
  264. #endif
  265. /*
  266. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  267. */
  268. static __always_inline raw_spinlock_t *spinlock_check(spinlock_t *lock)
  269. {
  270. return &lock->rlock;
  271. }
  272. #define spin_lock_init(_lock) \
  273. do { \
  274. spinlock_check(_lock); \
  275. raw_spin_lock_init(&(_lock)->rlock); \
  276. } while (0)
  277. static __always_inline void spin_lock(spinlock_t *lock)
  278. {
  279. raw_spin_lock(&lock->rlock);
  280. }
  281. static __always_inline void spin_lock_bh(spinlock_t *lock)
  282. {
  283. raw_spin_lock_bh(&lock->rlock);
  284. }
  285. static __always_inline int spin_trylock(spinlock_t *lock)
  286. {
  287. return raw_spin_trylock(&lock->rlock);
  288. }
  289. #define spin_lock_nested(lock, subclass) \
  290. do { \
  291. raw_spin_lock_nested(spinlock_check(lock), subclass); \
  292. } while (0)
  293. #define spin_lock_nest_lock(lock, nest_lock) \
  294. do { \
  295. raw_spin_lock_nest_lock(spinlock_check(lock), nest_lock); \
  296. } while (0)
  297. static __always_inline void spin_lock_irq(spinlock_t *lock)
  298. {
  299. raw_spin_lock_irq(&lock->rlock);
  300. }
  301. #define spin_lock_irqsave(lock, flags) \
  302. do { \
  303. raw_spin_lock_irqsave(spinlock_check(lock), flags); \
  304. } while (0)
  305. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  306. do { \
  307. raw_spin_lock_irqsave_nested(spinlock_check(lock), flags, subclass); \
  308. } while (0)
  309. static __always_inline void spin_unlock(spinlock_t *lock)
  310. {
  311. raw_spin_unlock(&lock->rlock);
  312. }
  313. static __always_inline void spin_unlock_bh(spinlock_t *lock)
  314. {
  315. raw_spin_unlock_bh(&lock->rlock);
  316. }
  317. static __always_inline void spin_unlock_irq(spinlock_t *lock)
  318. {
  319. raw_spin_unlock_irq(&lock->rlock);
  320. }
  321. static __always_inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
  322. {
  323. raw_spin_unlock_irqrestore(&lock->rlock, flags);
  324. }
  325. static __always_inline int spin_trylock_bh(spinlock_t *lock)
  326. {
  327. return raw_spin_trylock_bh(&lock->rlock);
  328. }
  329. static __always_inline int spin_trylock_irq(spinlock_t *lock)
  330. {
  331. return raw_spin_trylock_irq(&lock->rlock);
  332. }
  333. #define spin_trylock_irqsave(lock, flags) \
  334. ({ \
  335. raw_spin_trylock_irqsave(spinlock_check(lock), flags); \
  336. })
  337. /**
  338. * spin_unlock_wait - Interpose between successive critical sections
  339. * @lock: the spinlock whose critical sections are to be interposed.
  340. *
  341. * Semantically this is equivalent to a spin_lock() immediately
  342. * followed by a spin_unlock(). However, most architectures have
  343. * more efficient implementations in which the spin_unlock_wait()
  344. * cannot block concurrent lock acquisition, and in some cases
  345. * where spin_unlock_wait() does not write to the lock variable.
  346. * Nevertheless, spin_unlock_wait() can have high overhead, so if
  347. * you feel the need to use it, please check to see if there is
  348. * a better way to get your job done.
  349. *
  350. * The ordering guarantees provided by spin_unlock_wait() are:
  351. *
  352. * 1. All accesses preceding the spin_unlock_wait() happen before
  353. * any accesses in later critical sections for this same lock.
  354. * 2. All accesses following the spin_unlock_wait() happen after
  355. * any accesses in earlier critical sections for this same lock.
  356. */
  357. static __always_inline void spin_unlock_wait(spinlock_t *lock)
  358. {
  359. raw_spin_unlock_wait(&lock->rlock);
  360. }
  361. static __always_inline int spin_is_locked(spinlock_t *lock)
  362. {
  363. return raw_spin_is_locked(&lock->rlock);
  364. }
  365. static __always_inline int spin_is_contended(spinlock_t *lock)
  366. {
  367. return raw_spin_is_contended(&lock->rlock);
  368. }
  369. static __always_inline int spin_can_lock(spinlock_t *lock)
  370. {
  371. return raw_spin_can_lock(&lock->rlock);
  372. }
  373. #define assert_spin_locked(lock) assert_raw_spin_locked(&(lock)->rlock)
  374. /*
  375. * Pull the atomic_t declaration:
  376. * (asm-mips/atomic.h needs above definitions)
  377. */
  378. #include <linux/atomic.h>
  379. /**
  380. * atomic_dec_and_lock - lock on reaching reference count zero
  381. * @atomic: the atomic counter
  382. * @lock: the spinlock in question
  383. *
  384. * Decrements @atomic by 1. If the result is 0, returns true and locks
  385. * @lock. Returns false for all other cases.
  386. */
  387. extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  388. #define atomic_dec_and_lock(atomic, lock) \
  389. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  390. #endif /* __LINUX_SPINLOCK_H */