ww_mutex.h 13 KB

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