ww_mutex.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Wound/Wait Mutexes: blocking mutual exclusion locks with deadlock avoidance
  4. *
  5. * Original mutex implementation started by Ingo Molnar:
  6. *
  7. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]>
  8. *
  9. * Wait/Die implementation:
  10. * Copyright (C) 2013 Canonical Ltd.
  11. * Choice of algorithm:
  12. * Copyright (C) 2018 WMWare Inc.
  13. *
  14. * This file contains the main data structure and API definitions.
  15. */
  16. #ifndef __LINUX_WW_MUTEX_H
  17. #define __LINUX_WW_MUTEX_H
  18. #include <linux/mutex.h>
  19. #include <linux/rtmutex.h>
  20. #if defined(CONFIG_DEBUG_MUTEXES) || \
  21. (defined(CONFIG_PREEMPT_RT) && defined(CONFIG_DEBUG_RT_MUTEXES))
  22. #define DEBUG_WW_MUTEXES
  23. #endif
  24. #ifndef CONFIG_PREEMPT_RT
  25. #define WW_MUTEX_BASE mutex
  26. #define ww_mutex_base_init(l,n,k) __mutex_init(l,n,k)
  27. #define ww_mutex_base_is_locked(b) mutex_is_locked((b))
  28. #else
  29. #define WW_MUTEX_BASE rt_mutex
  30. #define ww_mutex_base_init(l,n,k) __rt_mutex_init(l,n,k)
  31. #define ww_mutex_base_is_locked(b) rt_mutex_base_is_locked(&(b)->rtmutex)
  32. #endif
  33. struct ww_class {
  34. atomic_long_t stamp;
  35. struct lock_class_key acquire_key;
  36. struct lock_class_key mutex_key;
  37. const char *acquire_name;
  38. const char *mutex_name;
  39. unsigned int is_wait_die;
  40. };
  41. struct ww_mutex {
  42. struct WW_MUTEX_BASE base;
  43. struct ww_acquire_ctx *ctx;
  44. #ifdef DEBUG_WW_MUTEXES
  45. struct ww_class *ww_class;
  46. #endif
  47. };
  48. struct ww_acquire_ctx {
  49. struct task_struct *task;
  50. unsigned long stamp;
  51. unsigned int acquired;
  52. unsigned short wounded;
  53. unsigned short is_wait_die;
  54. #ifdef DEBUG_WW_MUTEXES
  55. unsigned int done_acquire;
  56. struct ww_class *ww_class;
  57. void *contending_lock;
  58. #endif
  59. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  60. struct lockdep_map dep_map;
  61. #endif
  62. #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  63. unsigned int deadlock_inject_interval;
  64. unsigned int deadlock_inject_countdown;
  65. #endif
  66. };
  67. #define __WW_CLASS_INITIALIZER(ww_class, _is_wait_die) \
  68. { .stamp = ATOMIC_LONG_INIT(0) \
  69. , .acquire_name = #ww_class "_acquire" \
  70. , .mutex_name = #ww_class "_mutex" \
  71. , .is_wait_die = _is_wait_die }
  72. #define DEFINE_WD_CLASS(classname) \
  73. struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1)
  74. #define DEFINE_WW_CLASS(classname) \
  75. struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0)
  76. /**
  77. * ww_mutex_init - initialize the w/w mutex
  78. * @lock: the mutex to be initialized
  79. * @ww_class: the w/w class the mutex should belong to
  80. *
  81. * Initialize the w/w mutex to unlocked state and associate it with the given
  82. * class. Static define macro for w/w mutex is not provided and this function
  83. * is the only way to properly initialize the w/w mutex.
  84. *
  85. * It is not allowed to initialize an already locked mutex.
  86. */
  87. static inline void ww_mutex_init(struct ww_mutex *lock,
  88. struct ww_class *ww_class)
  89. {
  90. ww_mutex_base_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
  91. lock->ctx = NULL;
  92. #ifdef DEBUG_WW_MUTEXES
  93. lock->ww_class = ww_class;
  94. #endif
  95. }
  96. /**
  97. * ww_acquire_init - initialize a w/w acquire context
  98. * @ctx: w/w acquire context to initialize
  99. * @ww_class: w/w class of the context
  100. *
  101. * Initializes an context to acquire multiple mutexes of the given w/w class.
  102. *
  103. * Context-based w/w mutex acquiring can be done in any order whatsoever within
  104. * a given lock class. Deadlocks will be detected and handled with the
  105. * wait/die logic.
  106. *
  107. * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
  108. * result in undetected deadlocks and is so forbidden. Mixing different contexts
  109. * for the same w/w class when acquiring mutexes can also result in undetected
  110. * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
  111. * enabling CONFIG_PROVE_LOCKING.
  112. *
  113. * Nesting of acquire contexts for _different_ w/w classes is possible, subject
  114. * to the usual locking rules between different lock classes.
  115. *
  116. * An acquire context must be released with ww_acquire_fini by the same task
  117. * before the memory is freed. It is recommended to allocate the context itself
  118. * on the stack.
  119. */
  120. static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
  121. struct ww_class *ww_class)
  122. {
  123. ctx->task = current;
  124. ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
  125. ctx->acquired = 0;
  126. ctx->wounded = false;
  127. ctx->is_wait_die = ww_class->is_wait_die;
  128. #ifdef DEBUG_WW_MUTEXES
  129. ctx->ww_class = ww_class;
  130. ctx->done_acquire = 0;
  131. ctx->contending_lock = NULL;
  132. #endif
  133. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  134. debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
  135. lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
  136. &ww_class->acquire_key, 0);
  137. mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
  138. #endif
  139. #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  140. ctx->deadlock_inject_interval = 1;
  141. ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
  142. #endif
  143. }
  144. /**
  145. * ww_acquire_done - marks the end of the acquire phase
  146. * @ctx: the acquire context
  147. *
  148. * Marks the end of the acquire phase, any further w/w mutex lock calls using
  149. * this context are forbidden.
  150. *
  151. * Calling this function is optional, it is just useful to document w/w mutex
  152. * code and clearly designated the acquire phase from actually using the locked
  153. * data structures.
  154. */
  155. static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
  156. {
  157. #ifdef DEBUG_WW_MUTEXES
  158. lockdep_assert_held(ctx);
  159. DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
  160. ctx->done_acquire = 1;
  161. #endif
  162. }
  163. /**
  164. * ww_acquire_fini - releases a w/w acquire context
  165. * @ctx: the acquire context to free
  166. *
  167. * Releases a w/w acquire context. This must be called _after_ all acquired w/w
  168. * mutexes have been released with ww_mutex_unlock.
  169. */
  170. static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
  171. {
  172. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  173. mutex_release(&ctx->dep_map, _THIS_IP_);
  174. #endif
  175. #ifdef DEBUG_WW_MUTEXES
  176. DEBUG_LOCKS_WARN_ON(ctx->acquired);
  177. if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
  178. /*
  179. * lockdep will normally handle this,
  180. * but fail without anyway
  181. */
  182. ctx->done_acquire = 1;
  183. if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
  184. /* ensure ww_acquire_fini will still fail if called twice */
  185. ctx->acquired = ~0U;
  186. #endif
  187. }
  188. /**
  189. * ww_mutex_lock - acquire the w/w mutex
  190. * @lock: the mutex to be acquired
  191. * @ctx: w/w acquire context, or NULL to acquire only a single lock.
  192. *
  193. * Lock the w/w mutex exclusively for this task.
  194. *
  195. * Deadlocks within a given w/w class of locks are detected and handled with the
  196. * wait/die algorithm. If the lock isn't immediately available this function
  197. * will either sleep until it is (wait case). Or it selects the current context
  198. * for backing off by returning -EDEADLK (die case). Trying to acquire the
  199. * same lock with the same context twice is also detected and signalled by
  200. * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
  201. *
  202. * In the die case the caller must release all currently held w/w mutexes for
  203. * the given context and then wait for this contending lock to be available by
  204. * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
  205. * lock and proceed with trying to acquire further w/w mutexes (e.g. when
  206. * scanning through lru lists trying to free resources).
  207. *
  208. * The mutex must later on be released by the same task that
  209. * acquired it. The task may not exit without first unlocking the mutex. Also,
  210. * kernel memory where the mutex resides must not be freed with the mutex still
  211. * locked. The mutex must first be initialized (or statically defined) before it
  212. * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
  213. * of the same w/w lock class as was used to initialize the acquire context.
  214. *
  215. * A mutex acquired with this function must be released with ww_mutex_unlock.
  216. */
  217. extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx);
  218. /**
  219. * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
  220. * @lock: the mutex to be acquired
  221. * @ctx: w/w acquire context
  222. *
  223. * Lock the w/w mutex exclusively for this task.
  224. *
  225. * Deadlocks within a given w/w class of locks are detected and handled with the
  226. * wait/die algorithm. If the lock isn't immediately available this function
  227. * will either sleep until it is (wait case). Or it selects the current context
  228. * for backing off by returning -EDEADLK (die case). Trying to acquire the
  229. * same lock with the same context twice is also detected and signalled by
  230. * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
  231. * signal arrives while waiting for the lock then this function returns -EINTR.
  232. *
  233. * In the die case the caller must release all currently held w/w mutexes for
  234. * the given context and then wait for this contending lock to be available by
  235. * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
  236. * not acquire this lock and proceed with trying to acquire further w/w mutexes
  237. * (e.g. when scanning through lru lists trying to free resources).
  238. *
  239. * The mutex must later on be released by the same task that
  240. * acquired it. The task may not exit without first unlocking the mutex. Also,
  241. * kernel memory where the mutex resides must not be freed with the mutex still
  242. * locked. The mutex must first be initialized (or statically defined) before it
  243. * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
  244. * of the same w/w lock class as was used to initialize the acquire context.
  245. *
  246. * A mutex acquired with this function must be released with ww_mutex_unlock.
  247. */
  248. extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
  249. struct ww_acquire_ctx *ctx);
  250. /**
  251. * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
  252. * @lock: the mutex to be acquired
  253. * @ctx: w/w acquire context
  254. *
  255. * Acquires a w/w mutex with the given context after a die case. This function
  256. * will sleep until the lock becomes available.
  257. *
  258. * The caller must have released all w/w mutexes already acquired with the
  259. * context and then call this function on the contended lock.
  260. *
  261. * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
  262. * needs with ww_mutex_lock. Note that the -EALREADY return code from
  263. * ww_mutex_lock can be used to avoid locking this contended mutex twice.
  264. *
  265. * It is forbidden to call this function with any other w/w mutexes associated
  266. * with the context held. It is forbidden to call this on anything else than the
  267. * contending mutex.
  268. *
  269. * Note that the slowpath lock acquiring can also be done by calling
  270. * ww_mutex_lock directly. This function here is simply to help w/w mutex
  271. * locking code readability by clearly denoting the slowpath.
  272. */
  273. static inline void
  274. ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  275. {
  276. int ret;
  277. #ifdef DEBUG_WW_MUTEXES
  278. DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
  279. #endif
  280. ret = ww_mutex_lock(lock, ctx);
  281. (void)ret;
  282. }
  283. /**
  284. * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible
  285. * @lock: the mutex to be acquired
  286. * @ctx: w/w acquire context
  287. *
  288. * Acquires a w/w mutex with the given context after a die case. This function
  289. * will sleep until the lock becomes available and returns 0 when the lock has
  290. * been acquired. If a signal arrives while waiting for the lock then this
  291. * function returns -EINTR.
  292. *
  293. * The caller must have released all w/w mutexes already acquired with the
  294. * context and then call this function on the contended lock.
  295. *
  296. * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
  297. * needs with ww_mutex_lock. Note that the -EALREADY return code from
  298. * ww_mutex_lock can be used to avoid locking this contended mutex twice.
  299. *
  300. * It is forbidden to call this function with any other w/w mutexes associated
  301. * with the given context held. It is forbidden to call this on anything else
  302. * than the contending mutex.
  303. *
  304. * Note that the slowpath lock acquiring can also be done by calling
  305. * ww_mutex_lock_interruptible directly. This function here is simply to help
  306. * w/w mutex locking code readability by clearly denoting the slowpath.
  307. */
  308. static inline int __must_check
  309. ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
  310. struct ww_acquire_ctx *ctx)
  311. {
  312. #ifdef DEBUG_WW_MUTEXES
  313. DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
  314. #endif
  315. return ww_mutex_lock_interruptible(lock, ctx);
  316. }
  317. extern void ww_mutex_unlock(struct ww_mutex *lock);
  318. extern int __must_check ww_mutex_trylock(struct ww_mutex *lock,
  319. struct ww_acquire_ctx *ctx);
  320. /***
  321. * ww_mutex_destroy - mark a w/w mutex unusable
  322. * @lock: the mutex to be destroyed
  323. *
  324. * This function marks the mutex uninitialized, and any subsequent
  325. * use of the mutex is forbidden. The mutex must not be locked when
  326. * this function is called.
  327. */
  328. static inline void ww_mutex_destroy(struct ww_mutex *lock)
  329. {
  330. #ifndef CONFIG_PREEMPT_RT
  331. mutex_destroy(&lock->base);
  332. #endif
  333. }
  334. /**
  335. * ww_mutex_is_locked - is the w/w mutex locked
  336. * @lock: the mutex to be queried
  337. *
  338. * Returns 1 if the mutex is locked, 0 if unlocked.
  339. */
  340. static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
  341. {
  342. return ww_mutex_base_is_locked(&lock->base);
  343. }
  344. #endif