mutex.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Mutexes: blocking mutual exclusion locks
  4. *
  5. * started by Ingo Molnar:
  6. *
  7. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]>
  8. *
  9. * This file contains the main data structure and API definitions.
  10. */
  11. #ifndef __LINUX_MUTEX_H
  12. #define __LINUX_MUTEX_H
  13. #include <asm/current.h>
  14. #include <linux/list.h>
  15. #include <linux/spinlock_types.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/atomic.h>
  18. #include <asm/processor.h>
  19. #include <linux/osq_lock.h>
  20. #include <linux/debug_locks.h>
  21. #include <linux/android_vendor.h>
  22. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  23. # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  24. , .dep_map = { \
  25. .name = #lockname, \
  26. .wait_type_inner = LD_WAIT_SLEEP, \
  27. }
  28. #else
  29. # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  30. #endif
  31. #ifndef CONFIG_PREEMPT_RT
  32. /*
  33. * Simple, straightforward mutexes with strict semantics:
  34. *
  35. * - only one task can hold the mutex at a time
  36. * - only the owner can unlock the mutex
  37. * - multiple unlocks are not permitted
  38. * - recursive locking is not permitted
  39. * - a mutex object must be initialized via the API
  40. * - a mutex object must not be initialized via memset or copying
  41. * - task may not exit with mutex held
  42. * - memory areas where held locks reside must not be freed
  43. * - held mutexes must not be reinitialized
  44. * - mutexes may not be used in hardware or software interrupt
  45. * contexts such as tasklets and timers
  46. *
  47. * These semantics are fully enforced when DEBUG_MUTEXES is
  48. * enabled. Furthermore, besides enforcing the above rules, the mutex
  49. * debugging code also implements a number of additional features
  50. * that make lock debugging easier and faster:
  51. *
  52. * - uses symbolic names of mutexes, whenever they are printed in debug output
  53. * - point-of-acquire tracking, symbolic lookup of function names
  54. * - list of all locks held in the system, printout of them
  55. * - owner tracking
  56. * - detects self-recursing locks and prints out all relevant info
  57. * - detects multi-task circular deadlocks and prints out all affected
  58. * locks and tasks (and only those tasks)
  59. */
  60. struct mutex {
  61. atomic_long_t owner;
  62. raw_spinlock_t wait_lock;
  63. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  64. struct optimistic_spin_queue osq; /* Spinner MCS lock */
  65. #endif
  66. struct list_head wait_list;
  67. #ifdef CONFIG_DEBUG_MUTEXES
  68. void *magic;
  69. #endif
  70. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  71. struct lockdep_map dep_map;
  72. #endif
  73. ANDROID_OEM_DATA_ARRAY(1, 2);
  74. };
  75. #ifdef CONFIG_DEBUG_MUTEXES
  76. #define __DEBUG_MUTEX_INITIALIZER(lockname) \
  77. , .magic = &lockname
  78. extern void mutex_destroy(struct mutex *lock);
  79. #else
  80. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  81. static inline void mutex_destroy(struct mutex *lock) {}
  82. #endif
  83. /**
  84. * mutex_init - initialize the mutex
  85. * @mutex: the mutex to be initialized
  86. *
  87. * Initialize the mutex to unlocked state.
  88. *
  89. * It is not allowed to initialize an already locked mutex.
  90. */
  91. #define mutex_init(mutex) \
  92. do { \
  93. static struct lock_class_key __key; \
  94. \
  95. __mutex_init((mutex), #mutex, &__key); \
  96. } while (0)
  97. #define __MUTEX_INITIALIZER(lockname) \
  98. { .owner = ATOMIC_LONG_INIT(0) \
  99. , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  100. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  101. __DEBUG_MUTEX_INITIALIZER(lockname) \
  102. __DEP_MAP_MUTEX_INITIALIZER(lockname) }
  103. #define DEFINE_MUTEX(mutexname) \
  104. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  105. extern void __mutex_init(struct mutex *lock, const char *name,
  106. struct lock_class_key *key);
  107. /**
  108. * mutex_is_locked - is the mutex locked
  109. * @lock: the mutex to be queried
  110. *
  111. * Returns true if the mutex is locked, false if unlocked.
  112. */
  113. extern bool mutex_is_locked(struct mutex *lock);
  114. #else /* !CONFIG_PREEMPT_RT */
  115. /*
  116. * Preempt-RT variant based on rtmutexes.
  117. */
  118. #include <linux/rtmutex.h>
  119. struct mutex {
  120. struct rt_mutex_base rtmutex;
  121. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  122. struct lockdep_map dep_map;
  123. #endif
  124. };
  125. #define __MUTEX_INITIALIZER(mutexname) \
  126. { \
  127. .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex) \
  128. __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  129. }
  130. #define DEFINE_MUTEX(mutexname) \
  131. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  132. extern void __mutex_rt_init(struct mutex *lock, const char *name,
  133. struct lock_class_key *key);
  134. extern int mutex_trylock(struct mutex *lock);
  135. static inline void mutex_destroy(struct mutex *lock) { }
  136. #define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex)
  137. #define __mutex_init(mutex, name, key) \
  138. do { \
  139. rt_mutex_base_init(&(mutex)->rtmutex); \
  140. __mutex_rt_init((mutex), name, key); \
  141. } while (0)
  142. #define mutex_init(mutex) \
  143. do { \
  144. static struct lock_class_key __key; \
  145. \
  146. __mutex_init((mutex), #mutex, &__key); \
  147. } while (0)
  148. #endif /* CONFIG_PREEMPT_RT */
  149. /*
  150. * See kernel/locking/mutex.c for detailed documentation of these APIs.
  151. * Also see Documentation/locking/mutex-design.rst.
  152. */
  153. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  154. extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
  155. extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  156. extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
  157. unsigned int subclass);
  158. extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
  159. unsigned int subclass);
  160. extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
  161. #define mutex_lock(lock) mutex_lock_nested(lock, 0)
  162. #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
  163. #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
  164. #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
  165. #define mutex_lock_nest_lock(lock, nest_lock) \
  166. do { \
  167. typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  168. _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  169. } while (0)
  170. #else
  171. extern void mutex_lock(struct mutex *lock);
  172. extern int __must_check mutex_lock_interruptible(struct mutex *lock);
  173. extern int __must_check mutex_lock_killable(struct mutex *lock);
  174. extern void mutex_lock_io(struct mutex *lock);
  175. # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
  176. # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
  177. # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
  178. # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  179. # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
  180. #endif
  181. /*
  182. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  183. * not the down_trylock() convention!
  184. *
  185. * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  186. */
  187. extern int mutex_trylock(struct mutex *lock);
  188. extern void mutex_unlock(struct mutex *lock);
  189. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  190. #endif /* __LINUX_MUTEX_H */