mutex.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Mutexes: blocking mutual exclusion locks
  3. *
  4. * started by Ingo Molnar:
  5. *
  6. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  7. *
  8. * This file contains the main data structure and API definitions.
  9. */
  10. #ifndef __LINUX_MUTEX_H
  11. #define __LINUX_MUTEX_H
  12. #include <asm/current.h>
  13. #include <linux/list.h>
  14. #include <linux/spinlock_types.h>
  15. #include <linux/linkage.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/atomic.h>
  18. /*
  19. * Simple, straightforward mutexes with strict semantics:
  20. *
  21. * - only one task can hold the mutex at a time
  22. * - only the owner can unlock the mutex
  23. * - multiple unlocks are not permitted
  24. * - recursive locking is not permitted
  25. * - a mutex object must be initialized via the API
  26. * - a mutex object must not be initialized via memset or copying
  27. * - task may not exit with mutex held
  28. * - memory areas where held locks reside must not be freed
  29. * - held mutexes must not be reinitialized
  30. * - mutexes may not be used in hardware or software interrupt
  31. * contexts such as tasklets and timers
  32. *
  33. * These semantics are fully enforced when DEBUG_MUTEXES is
  34. * enabled. Furthermore, besides enforcing the above rules, the mutex
  35. * debugging code also implements a number of additional features
  36. * that make lock debugging easier and faster:
  37. *
  38. * - uses symbolic names of mutexes, whenever they are printed in debug output
  39. * - point-of-acquire tracking, symbolic lookup of function names
  40. * - list of all locks held in the system, printout of them
  41. * - owner tracking
  42. * - detects self-recursing locks and prints out all relevant info
  43. * - detects multi-task circular deadlocks and prints out all affected
  44. * locks and tasks (and only those tasks)
  45. */
  46. struct mutex {
  47. /* 1: unlocked, 0: locked, negative: locked, possible waiters */
  48. atomic_t count;
  49. spinlock_t wait_lock;
  50. struct list_head wait_list;
  51. #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
  52. struct task_struct *owner;
  53. #endif
  54. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  55. void *spin_mlock; /* Spinner MCS lock */
  56. #endif
  57. #ifdef CONFIG_DEBUG_MUTEXES
  58. const char *name;
  59. void *magic;
  60. #endif
  61. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  62. struct lockdep_map dep_map;
  63. #endif
  64. };
  65. /*
  66. * This is the control structure for tasks blocked on mutex,
  67. * which resides on the blocked task's kernel stack:
  68. */
  69. struct mutex_waiter {
  70. struct list_head list;
  71. struct task_struct *task;
  72. #ifdef CONFIG_DEBUG_MUTEXES
  73. void *magic;
  74. #endif
  75. };
  76. struct ww_class {
  77. atomic_long_t stamp;
  78. struct lock_class_key acquire_key;
  79. struct lock_class_key mutex_key;
  80. const char *acquire_name;
  81. const char *mutex_name;
  82. };
  83. struct ww_acquire_ctx {
  84. struct task_struct *task;
  85. unsigned long stamp;
  86. unsigned acquired;
  87. #ifdef CONFIG_DEBUG_MUTEXES
  88. unsigned done_acquire;
  89. struct ww_class *ww_class;
  90. struct ww_mutex *contending_lock;
  91. #endif
  92. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  93. struct lockdep_map dep_map;
  94. #endif
  95. };
  96. struct ww_mutex {
  97. struct mutex base;
  98. struct ww_acquire_ctx *ctx;
  99. #ifdef CONFIG_DEBUG_MUTEXES
  100. struct ww_class *ww_class;
  101. #endif
  102. };
  103. #ifdef CONFIG_DEBUG_MUTEXES
  104. # include <linux/mutex-debug.h>
  105. #else
  106. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  107. /**
  108. * mutex_init - initialize the mutex
  109. * @mutex: the mutex to be initialized
  110. *
  111. * Initialize the mutex to unlocked state.
  112. *
  113. * It is not allowed to initialize an already locked mutex.
  114. */
  115. # define mutex_init(mutex) \
  116. do { \
  117. static struct lock_class_key __key; \
  118. \
  119. __mutex_init((mutex), #mutex, &__key); \
  120. } while (0)
  121. static inline void mutex_destroy(struct mutex *lock) {}
  122. #endif
  123. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  124. # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  125. , .dep_map = { .name = #lockname }
  126. # define __WW_CLASS_MUTEX_INITIALIZER(lockname, ww_class) \
  127. , .ww_class = &ww_class
  128. #else
  129. # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  130. # define __WW_CLASS_MUTEX_INITIALIZER(lockname, ww_class)
  131. #endif
  132. #define __MUTEX_INITIALIZER(lockname) \
  133. { .count = ATOMIC_INIT(1) \
  134. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  135. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  136. __DEBUG_MUTEX_INITIALIZER(lockname) \
  137. __DEP_MAP_MUTEX_INITIALIZER(lockname) }
  138. #define __WW_CLASS_INITIALIZER(ww_class) \
  139. { .stamp = ATOMIC_LONG_INIT(0) \
  140. , .acquire_name = #ww_class "_acquire" \
  141. , .mutex_name = #ww_class "_mutex" }
  142. #define __WW_MUTEX_INITIALIZER(lockname, class) \
  143. { .base = { \__MUTEX_INITIALIZER(lockname) } \
  144. __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
  145. #define DEFINE_MUTEX(mutexname) \
  146. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  147. #define DEFINE_WW_CLASS(classname) \
  148. struct ww_class classname = __WW_CLASS_INITIALIZER(classname)
  149. #define DEFINE_WW_MUTEX(mutexname, ww_class) \
  150. struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
  151. extern void __mutex_init(struct mutex *lock, const char *name,
  152. struct lock_class_key *key);
  153. /**
  154. * ww_mutex_init - initialize the w/w mutex
  155. * @lock: the mutex to be initialized
  156. * @ww_class: the w/w class the mutex should belong to
  157. *
  158. * Initialize the w/w mutex to unlocked state and associate it with the given
  159. * class.
  160. *
  161. * It is not allowed to initialize an already locked mutex.
  162. */
  163. static inline void ww_mutex_init(struct ww_mutex *lock,
  164. struct ww_class *ww_class)
  165. {
  166. __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
  167. lock->ctx = NULL;
  168. #ifdef CONFIG_DEBUG_MUTEXES
  169. lock->ww_class = ww_class;
  170. #endif
  171. }
  172. /**
  173. * mutex_is_locked - is the mutex locked
  174. * @lock: the mutex to be queried
  175. *
  176. * Returns 1 if the mutex is locked, 0 if unlocked.
  177. */
  178. static inline int mutex_is_locked(struct mutex *lock)
  179. {
  180. return atomic_read(&lock->count) != 1;
  181. }
  182. /*
  183. * See kernel/mutex.c for detailed documentation of these APIs.
  184. * Also see Documentation/mutex-design.txt.
  185. */
  186. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  187. extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
  188. extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  189. extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
  190. unsigned int subclass);
  191. extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
  192. unsigned int subclass);
  193. #define mutex_lock(lock) mutex_lock_nested(lock, 0)
  194. #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
  195. #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
  196. #define mutex_lock_nest_lock(lock, nest_lock) \
  197. do { \
  198. typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  199. _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  200. } while (0)
  201. #else
  202. extern void mutex_lock(struct mutex *lock);
  203. extern int __must_check mutex_lock_interruptible(struct mutex *lock);
  204. extern int __must_check mutex_lock_killable(struct mutex *lock);
  205. # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
  206. # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
  207. # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
  208. # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  209. #endif
  210. /*
  211. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  212. * not the down_trylock() convention!
  213. *
  214. * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  215. */
  216. extern int mutex_trylock(struct mutex *lock);
  217. extern void mutex_unlock(struct mutex *lock);
  218. /**
  219. * ww_acquire_init - initialize a w/w acquire context
  220. * @ctx: w/w acquire context to initialize
  221. * @ww_class: w/w class of the context
  222. *
  223. * Initializes an context to acquire multiple mutexes of the given w/w class.
  224. *
  225. * Context-based w/w mutex acquiring can be done in any order whatsoever within
  226. * a given lock class. Deadlocks will be detected and handled with the
  227. * wait/wound logic.
  228. *
  229. * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
  230. * result in undetected deadlocks and is so forbidden. Mixing different contexts
  231. * for the same w/w class when acquiring mutexes can also result in undetected
  232. * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
  233. * enabling CONFIG_PROVE_LOCKING.
  234. *
  235. * Nesting of acquire contexts for _different_ w/w classes is possible, subject
  236. * to the usual locking rules between different lock classes.
  237. *
  238. * An acquire context must be released with ww_acquire_fini by the same task
  239. * before the memory is freed. It is recommended to allocate the context itself
  240. * on the stack.
  241. */
  242. static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
  243. struct ww_class *ww_class)
  244. {
  245. ctx->task = current;
  246. ctx->stamp = atomic_long_inc_return(&ww_class->stamp);
  247. ctx->acquired = 0;
  248. #ifdef CONFIG_DEBUG_MUTEXES
  249. ctx->ww_class = ww_class;
  250. ctx->done_acquire = 0;
  251. ctx->contending_lock = NULL;
  252. #endif
  253. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  254. debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
  255. lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
  256. &ww_class->acquire_key, 0);
  257. mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
  258. #endif
  259. }
  260. /**
  261. * ww_acquire_done - marks the end of the acquire phase
  262. * @ctx: the acquire context
  263. *
  264. * Marks the end of the acquire phase, any further w/w mutex lock calls using
  265. * this context are forbidden.
  266. *
  267. * Calling this function is optional, it is just useful to document w/w mutex
  268. * code and clearly designated the acquire phase from actually using the locked
  269. * data structures.
  270. */
  271. static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
  272. {
  273. #ifdef CONFIG_DEBUG_MUTEXES
  274. lockdep_assert_held(ctx);
  275. DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
  276. ctx->done_acquire = 1;
  277. #endif
  278. }
  279. /**
  280. * ww_acquire_fini - releases a w/w acquire context
  281. * @ctx: the acquire context to free
  282. *
  283. * Releases a w/w acquire context. This must be called _after_ all acquired w/w
  284. * mutexes have been released with ww_mutex_unlock.
  285. */
  286. static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
  287. {
  288. #ifdef CONFIG_DEBUG_MUTEXES
  289. mutex_release(&ctx->dep_map, 0, _THIS_IP_);
  290. DEBUG_LOCKS_WARN_ON(ctx->acquired);
  291. if (!config_enabled(CONFIG_PROVE_LOCKING))
  292. /*
  293. * lockdep will normally handle this,
  294. * but fail without anyway
  295. */
  296. ctx->done_acquire = 1;
  297. if (!config_enabled(CONFIG_DEBUG_LOCK_ALLOC))
  298. /* ensure ww_acquire_fini will still fail if called twice */
  299. ctx->acquired = ~0U;
  300. #endif
  301. }
  302. extern int __must_check __ww_mutex_lock(struct ww_mutex *lock,
  303. struct ww_acquire_ctx *ctx);
  304. extern int __must_check __ww_mutex_lock_interruptible(struct ww_mutex *lock,
  305. struct ww_acquire_ctx *ctx);
  306. /**
  307. * ww_mutex_lock - acquire the w/w mutex
  308. * @lock: the mutex to be acquired
  309. * @ctx: w/w acquire context, or NULL to acquire only a single lock.
  310. *
  311. * Lock the w/w mutex exclusively for this task.
  312. *
  313. * Deadlocks within a given w/w class of locks are detected and handled with the
  314. * wait/wound algorithm. If the lock isn't immediately avaiable this function
  315. * will either sleep until it is (wait case). Or it selects the current context
  316. * for backing off by returning -EDEADLK (wound case). Trying to acquire the
  317. * same lock with the same context twice is also detected and signalled by
  318. * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
  319. *
  320. * In the wound case the caller must release all currently held w/w mutexes for
  321. * the given context and then wait for this contending lock to be available by
  322. * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
  323. * lock and proceed with trying to acquire further w/w mutexes (e.g. when
  324. * scanning through lru lists trying to free resources).
  325. *
  326. * The mutex must later on be released by the same task that
  327. * acquired it. The task may not exit without first unlocking the mutex. Also,
  328. * kernel memory where the mutex resides must not be freed with the mutex still
  329. * locked. The mutex must first be initialized (or statically defined) before it
  330. * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
  331. * of the same w/w lock class as was used to initialize the acquire context.
  332. *
  333. * A mutex acquired with this function must be released with ww_mutex_unlock.
  334. */
  335. static inline int ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  336. {
  337. if (ctx)
  338. return __ww_mutex_lock(lock, ctx);
  339. else {
  340. mutex_lock(&lock->base);
  341. return 0;
  342. }
  343. }
  344. /**
  345. * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
  346. * @lock: the mutex to be acquired
  347. * @ctx: w/w acquire context
  348. *
  349. * Lock the w/w mutex exclusively for this task.
  350. *
  351. * Deadlocks within a given w/w class of locks are detected and handled with the
  352. * wait/wound algorithm. If the lock isn't immediately avaiable this function
  353. * will either sleep until it is (wait case). Or it selects the current context
  354. * for backing off by returning -EDEADLK (wound case). Trying to acquire the
  355. * same lock with the same context twice is also detected and signalled by
  356. * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
  357. * signal arrives while waiting for the lock then this function returns -EINTR.
  358. *
  359. * In the wound case the caller must release all currently held w/w mutexes for
  360. * the given context and then wait for this contending lock to be available by
  361. * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
  362. * not acquire this lock and proceed with trying to acquire further w/w mutexes
  363. * (e.g. when scanning through lru lists trying to free resources).
  364. *
  365. * The mutex must later on be released by the same task that
  366. * acquired it. The task may not exit without first unlocking the mutex. Also,
  367. * kernel memory where the mutex resides must not be freed with the mutex still
  368. * locked. The mutex must first be initialized (or statically defined) before it
  369. * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
  370. * of the same w/w lock class as was used to initialize the acquire context.
  371. *
  372. * A mutex acquired with this function must be released with ww_mutex_unlock.
  373. */
  374. static inline int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
  375. struct ww_acquire_ctx *ctx)
  376. {
  377. if (ctx)
  378. return __ww_mutex_lock_interruptible(lock, ctx);
  379. else
  380. return mutex_lock_interruptible(&lock->base);
  381. }
  382. /**
  383. * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
  384. * @lock: the mutex to be acquired
  385. * @ctx: w/w acquire context
  386. *
  387. * Acquires a w/w mutex with the given context after a wound case. This function
  388. * will sleep until the lock becomes available.
  389. *
  390. * The caller must have released all w/w mutexes already acquired with the
  391. * context and then call this function on the contended lock.
  392. *
  393. * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
  394. * needs with ww_mutex_lock. Note that the -EALREADY return code from
  395. * ww_mutex_lock can be used to avoid locking this contended mutex twice.
  396. *
  397. * It is forbidden to call this function with any other w/w mutexes associated
  398. * with the context held. It is forbidden to call this on anything else than the
  399. * contending mutex.
  400. *
  401. * Note that the slowpath lock acquiring can also be done by calling
  402. * ww_mutex_lock directly. This function here is simply to help w/w mutex
  403. * locking code readability by clearly denoting the slowpath.
  404. */
  405. static inline void
  406. ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  407. {
  408. int ret;
  409. #ifdef CONFIG_DEBUG_MUTEXES
  410. DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
  411. #endif
  412. ret = ww_mutex_lock(lock, ctx);
  413. (void)ret;
  414. }
  415. /**
  416. * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex,
  417. * interruptible
  418. * @lock: the mutex to be acquired
  419. * @ctx: w/w acquire context
  420. *
  421. * Acquires a w/w mutex with the given context after a wound case. This function
  422. * will sleep until the lock becomes available and returns 0 when the lock has
  423. * been acquired. If a signal arrives while waiting for the lock then this
  424. * function returns -EINTR.
  425. *
  426. * The caller must have released all w/w mutexes already acquired with the
  427. * context and then call this function on the contended lock.
  428. *
  429. * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
  430. * needs with ww_mutex_lock. Note that the -EALREADY return code from
  431. * ww_mutex_lock can be used to avoid locking this contended mutex twice.
  432. *
  433. * It is forbidden to call this function with any other w/w mutexes associated
  434. * with the given context held. It is forbidden to call this on anything else
  435. * than the contending mutex.
  436. *
  437. * Note that the slowpath lock acquiring can also be done by calling
  438. * ww_mutex_lock_interruptible directly. This function here is simply to help
  439. * w/w mutex locking code readability by clearly denoting the slowpath.
  440. */
  441. static inline int __must_check
  442. ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
  443. struct ww_acquire_ctx *ctx)
  444. {
  445. #ifdef CONFIG_DEBUG_MUTEXES
  446. DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
  447. #endif
  448. return ww_mutex_lock_interruptible(lock, ctx);
  449. }
  450. extern void ww_mutex_unlock(struct ww_mutex *lock);
  451. /**
  452. * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
  453. * @lock: mutex to lock
  454. *
  455. * Trylocks a mutex without acquire context, so no deadlock detection is
  456. * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
  457. */
  458. static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
  459. {
  460. return mutex_trylock(&lock->base);
  461. }
  462. /***
  463. * ww_mutex_destroy - mark a w/w mutex unusable
  464. * @lock: the mutex to be destroyed
  465. *
  466. * This function marks the mutex uninitialized, and any subsequent
  467. * use of the mutex is forbidden. The mutex must not be locked when
  468. * this function is called.
  469. */
  470. static inline void ww_mutex_destroy(struct ww_mutex *lock)
  471. {
  472. mutex_destroy(&lock->base);
  473. }
  474. /**
  475. * ww_mutex_is_locked - is the w/w mutex locked
  476. * @lock: the mutex to be queried
  477. *
  478. * Returns 1 if the mutex is locked, 0 if unlocked.
  479. */
  480. static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
  481. {
  482. return mutex_is_locked(&lock->base);
  483. }
  484. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  485. #ifndef CONFIG_HAVE_ARCH_MUTEX_CPU_RELAX
  486. #define arch_mutex_cpu_relax() cpu_relax()
  487. #endif
  488. #endif