spinlock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. * Despite its name it doesn't necessarily has to be a full barrier.
  107. * It should only guarantee that a STORE before the critical section
  108. * can not be reordered with LOADs and STOREs inside this section.
  109. * spin_lock() is the one-way barrier, this LOAD can not escape out
  110. * of the region. So the default implementation simply ensures that
  111. * a STORE can not move into the critical section, smp_wmb() should
  112. * serialize it with another STORE done by spin_lock().
  113. */
  114. #ifndef smp_mb__before_spinlock
  115. #define smp_mb__before_spinlock() smp_wmb()
  116. #endif
  117. /*
  118. * Place this after a lock-acquisition primitive to guarantee that
  119. * an UNLOCK+LOCK pair act as a full barrier. This guarantee applies
  120. * if the UNLOCK and LOCK are executed by the same CPU or if the
  121. * UNLOCK and LOCK operate on the same lock variable.
  122. */
  123. #ifndef smp_mb__after_unlock_lock
  124. #define smp_mb__after_unlock_lock() do { } while (0)
  125. #endif
  126. /**
  127. * raw_spin_unlock_wait - wait until the spinlock gets unlocked
  128. * @lock: the spinlock in question.
  129. */
  130. #define raw_spin_unlock_wait(lock) arch_spin_unlock_wait(&(lock)->raw_lock)
  131. #ifdef CONFIG_DEBUG_SPINLOCK
  132. extern void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock);
  133. #define do_raw_spin_lock_flags(lock, flags) do_raw_spin_lock(lock)
  134. extern int do_raw_spin_trylock(raw_spinlock_t *lock);
  135. extern void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock);
  136. #else
  137. static inline void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock)
  138. {
  139. __acquire(lock);
  140. arch_spin_lock(&lock->raw_lock);
  141. }
  142. static inline void
  143. do_raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long *flags) __acquires(lock)
  144. {
  145. __acquire(lock);
  146. arch_spin_lock_flags(&lock->raw_lock, *flags);
  147. }
  148. static inline int do_raw_spin_trylock(raw_spinlock_t *lock)
  149. {
  150. return arch_spin_trylock(&(lock)->raw_lock);
  151. }
  152. static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  153. {
  154. arch_spin_unlock(&lock->raw_lock);
  155. __release(lock);
  156. }
  157. #endif
  158. /*
  159. * Define the various spin_lock methods. Note we define these
  160. * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The
  161. * various methods are defined as nops in the case they are not
  162. * required.
  163. */
  164. #define raw_spin_trylock(lock) __cond_lock(lock, _raw_spin_trylock(lock))
  165. #define raw_spin_lock(lock) _raw_spin_lock(lock)
  166. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  167. # define raw_spin_lock_nested(lock, subclass) \
  168. _raw_spin_lock_nested(lock, subclass)
  169. # define raw_spin_lock_bh_nested(lock, subclass) \
  170. _raw_spin_lock_bh_nested(lock, subclass)
  171. # define raw_spin_lock_nest_lock(lock, nest_lock) \
  172. do { \
  173. typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\
  174. _raw_spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  175. } while (0)
  176. #else
  177. /*
  178. * Always evaluate the 'subclass' argument to avoid that the compiler
  179. * warns about set-but-not-used variables when building with
  180. * CONFIG_DEBUG_LOCK_ALLOC=n and with W=1.
  181. */
  182. # define raw_spin_lock_nested(lock, subclass) \
  183. _raw_spin_lock(((void)(subclass), (lock)))
  184. # define raw_spin_lock_nest_lock(lock, nest_lock) _raw_spin_lock(lock)
  185. # define raw_spin_lock_bh_nested(lock, subclass) _raw_spin_lock_bh(lock)
  186. #endif
  187. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  188. #define raw_spin_lock_irqsave(lock, flags) \
  189. do { \
  190. typecheck(unsigned long, flags); \
  191. flags = _raw_spin_lock_irqsave(lock); \
  192. } while (0)
  193. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  194. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  195. do { \
  196. typecheck(unsigned long, flags); \
  197. flags = _raw_spin_lock_irqsave_nested(lock, subclass); \
  198. } while (0)
  199. #else
  200. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  201. do { \
  202. typecheck(unsigned long, flags); \
  203. flags = _raw_spin_lock_irqsave(lock); \
  204. } while (0)
  205. #endif
  206. #else
  207. #define raw_spin_lock_irqsave(lock, flags) \
  208. do { \
  209. typecheck(unsigned long, flags); \
  210. _raw_spin_lock_irqsave(lock, flags); \
  211. } while (0)
  212. #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
  213. raw_spin_lock_irqsave(lock, flags)
  214. #endif
  215. #define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock)
  216. #define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock)
  217. #define raw_spin_unlock(lock) _raw_spin_unlock(lock)
  218. #define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock)
  219. #define raw_spin_unlock_irqrestore(lock, flags) \
  220. do { \
  221. typecheck(unsigned long, flags); \
  222. _raw_spin_unlock_irqrestore(lock, flags); \
  223. } while (0)
  224. #define raw_spin_unlock_bh(lock) _raw_spin_unlock_bh(lock)
  225. #define raw_spin_trylock_bh(lock) \
  226. __cond_lock(lock, _raw_spin_trylock_bh(lock))
  227. #define raw_spin_trylock_irq(lock) \
  228. ({ \
  229. local_irq_disable(); \
  230. raw_spin_trylock(lock) ? \
  231. 1 : ({ local_irq_enable(); 0; }); \
  232. })
  233. #define raw_spin_trylock_irqsave(lock, flags) \
  234. ({ \
  235. local_irq_save(flags); \
  236. raw_spin_trylock(lock) ? \
  237. 1 : ({ local_irq_restore(flags); 0; }); \
  238. })
  239. /**
  240. * raw_spin_can_lock - would raw_spin_trylock() succeed?
  241. * @lock: the spinlock in question.
  242. */
  243. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  244. /* Include rwlock functions */
  245. #include <linux/rwlock.h>
  246. /*
  247. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  248. */
  249. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  250. # include <linux/spinlock_api_smp.h>
  251. #else
  252. # include <linux/spinlock_api_up.h>
  253. #endif
  254. /*
  255. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  256. */
  257. static inline raw_spinlock_t *spinlock_check(spinlock_t *lock)
  258. {
  259. return &lock->rlock;
  260. }
  261. #define spin_lock_init(_lock) \
  262. do { \
  263. spinlock_check(_lock); \
  264. raw_spin_lock_init(&(_lock)->rlock); \
  265. } while (0)
  266. static inline void spin_lock(spinlock_t *lock)
  267. {
  268. raw_spin_lock(&lock->rlock);
  269. }
  270. static inline void spin_lock_bh(spinlock_t *lock)
  271. {
  272. raw_spin_lock_bh(&lock->rlock);
  273. }
  274. static inline int spin_trylock(spinlock_t *lock)
  275. {
  276. return raw_spin_trylock(&lock->rlock);
  277. }
  278. #define spin_lock_nested(lock, subclass) \
  279. do { \
  280. raw_spin_lock_nested(spinlock_check(lock), subclass); \
  281. } while (0)
  282. #define spin_lock_bh_nested(lock, subclass) \
  283. do { \
  284. raw_spin_lock_bh_nested(spinlock_check(lock), subclass);\
  285. } while (0)
  286. #define spin_lock_nest_lock(lock, nest_lock) \
  287. do { \
  288. raw_spin_lock_nest_lock(spinlock_check(lock), nest_lock); \
  289. } while (0)
  290. static inline void spin_lock_irq(spinlock_t *lock)
  291. {
  292. raw_spin_lock_irq(&lock->rlock);
  293. }
  294. #define spin_lock_irqsave(lock, flags) \
  295. do { \
  296. raw_spin_lock_irqsave(spinlock_check(lock), flags); \
  297. } while (0)
  298. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  299. do { \
  300. raw_spin_lock_irqsave_nested(spinlock_check(lock), flags, subclass); \
  301. } while (0)
  302. static inline void spin_unlock(spinlock_t *lock)
  303. {
  304. raw_spin_unlock(&lock->rlock);
  305. }
  306. static inline void spin_unlock_bh(spinlock_t *lock)
  307. {
  308. raw_spin_unlock_bh(&lock->rlock);
  309. }
  310. static inline void spin_unlock_irq(spinlock_t *lock)
  311. {
  312. raw_spin_unlock_irq(&lock->rlock);
  313. }
  314. static inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
  315. {
  316. raw_spin_unlock_irqrestore(&lock->rlock, flags);
  317. }
  318. static inline int spin_trylock_bh(spinlock_t *lock)
  319. {
  320. return raw_spin_trylock_bh(&lock->rlock);
  321. }
  322. static inline int spin_trylock_irq(spinlock_t *lock)
  323. {
  324. return raw_spin_trylock_irq(&lock->rlock);
  325. }
  326. #define spin_trylock_irqsave(lock, flags) \
  327. ({ \
  328. raw_spin_trylock_irqsave(spinlock_check(lock), flags); \
  329. })
  330. static inline void spin_unlock_wait(spinlock_t *lock)
  331. {
  332. raw_spin_unlock_wait(&lock->rlock);
  333. }
  334. static inline int spin_is_locked(spinlock_t *lock)
  335. {
  336. return raw_spin_is_locked(&lock->rlock);
  337. }
  338. static inline int spin_is_contended(spinlock_t *lock)
  339. {
  340. return raw_spin_is_contended(&lock->rlock);
  341. }
  342. static inline int spin_can_lock(spinlock_t *lock)
  343. {
  344. return raw_spin_can_lock(&lock->rlock);
  345. }
  346. #define assert_spin_locked(lock) assert_raw_spin_locked(&(lock)->rlock)
  347. /*
  348. * Pull the atomic_t declaration:
  349. * (asm-mips/atomic.h needs above definitions)
  350. */
  351. #include <linux/atomic.h>
  352. /**
  353. * atomic_dec_and_lock - lock on reaching reference count zero
  354. * @atomic: the atomic counter
  355. * @lock: the spinlock in question
  356. *
  357. * Decrements @atomic by 1. If the result is 0, returns true and locks
  358. * @lock. Returns false for all other cases.
  359. */
  360. extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  361. #define atomic_dec_and_lock(atomic, lock) \
  362. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  363. #endif /* __LINUX_SPINLOCK_H */