spinlock.h 11 KB

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