ww_mutex.h 13 KB

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