mutex.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  96. unsigned deadlock_inject_interval;
  97. unsigned deadlock_inject_countdown;
  98. #endif
  99. };
  100. struct ww_mutex {
  101. struct mutex base;
  102. struct ww_acquire_ctx *ctx;
  103. #ifdef CONFIG_DEBUG_MUTEXES
  104. struct ww_class *ww_class;
  105. #endif
  106. };
  107. #ifdef CONFIG_DEBUG_MUTEXES
  108. # include <linux/mutex-debug.h>
  109. #else
  110. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  111. /**
  112. * mutex_init - initialize the mutex
  113. * @mutex: the mutex to be initialized
  114. *
  115. * Initialize the mutex to unlocked state.
  116. *
  117. * It is not allowed to initialize an already locked mutex.
  118. */
  119. # define mutex_init(mutex) \
  120. do { \
  121. static struct lock_class_key __key; \
  122. \
  123. __mutex_init((mutex), #mutex, &__key); \
  124. } while (0)
  125. static inline void mutex_destroy(struct mutex *lock) {}
  126. #endif
  127. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  128. # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  129. , .dep_map = { .name = #lockname }
  130. # define __WW_CLASS_MUTEX_INITIALIZER(lockname, ww_class) \
  131. , .ww_class = &ww_class
  132. #else
  133. # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  134. # define __WW_CLASS_MUTEX_INITIALIZER(lockname, ww_class)
  135. #endif
  136. #define __MUTEX_INITIALIZER(lockname) \
  137. { .count = ATOMIC_INIT(1) \
  138. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  139. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  140. __DEBUG_MUTEX_INITIALIZER(lockname) \
  141. __DEP_MAP_MUTEX_INITIALIZER(lockname) }
  142. #define __WW_CLASS_INITIALIZER(ww_class) \
  143. { .stamp = ATOMIC_LONG_INIT(0) \
  144. , .acquire_name = #ww_class "_acquire" \
  145. , .mutex_name = #ww_class "_mutex" }
  146. #define __WW_MUTEX_INITIALIZER(lockname, class) \
  147. { .base = { \__MUTEX_INITIALIZER(lockname) } \
  148. __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
  149. #define DEFINE_MUTEX(mutexname) \
  150. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  151. #define DEFINE_WW_CLASS(classname) \
  152. struct ww_class classname = __WW_CLASS_INITIALIZER(classname)
  153. #define DEFINE_WW_MUTEX(mutexname, ww_class) \
  154. struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
  155. extern void __mutex_init(struct mutex *lock, const char *name,
  156. struct lock_class_key *key);
  157. /**
  158. * ww_mutex_init - initialize the w/w mutex
  159. * @lock: the mutex to be initialized
  160. * @ww_class: the w/w class the mutex should belong to
  161. *
  162. * Initialize the w/w mutex to unlocked state and associate it with the given
  163. * class.
  164. *
  165. * It is not allowed to initialize an already locked mutex.
  166. */
  167. static inline void ww_mutex_init(struct ww_mutex *lock,
  168. struct ww_class *ww_class)
  169. {
  170. __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
  171. lock->ctx = NULL;
  172. #ifdef CONFIG_DEBUG_MUTEXES
  173. lock->ww_class = ww_class;
  174. #endif
  175. }
  176. /**
  177. * mutex_is_locked - is the mutex locked
  178. * @lock: the mutex to be queried
  179. *
  180. * Returns 1 if the mutex is locked, 0 if unlocked.
  181. */
  182. static inline int mutex_is_locked(struct mutex *lock)
  183. {
  184. return atomic_read(&lock->count) != 1;
  185. }
  186. /*
  187. * See kernel/mutex.c for detailed documentation of these APIs.
  188. * Also see Documentation/mutex-design.txt.
  189. */
  190. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  191. extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
  192. extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  193. extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
  194. unsigned int subclass);
  195. extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
  196. unsigned int subclass);
  197. #define mutex_lock(lock) mutex_lock_nested(lock, 0)
  198. #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
  199. #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
  200. #define mutex_lock_nest_lock(lock, nest_lock) \
  201. do { \
  202. typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  203. _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  204. } while (0)
  205. #else
  206. extern void mutex_lock(struct mutex *lock);
  207. extern int __must_check mutex_lock_interruptible(struct mutex *lock);
  208. extern int __must_check mutex_lock_killable(struct mutex *lock);
  209. # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
  210. # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
  211. # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
  212. # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  213. #endif
  214. /*
  215. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  216. * not the down_trylock() convention!
  217. *
  218. * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  219. */
  220. extern int mutex_trylock(struct mutex *lock);
  221. extern void mutex_unlock(struct mutex *lock);
  222. /**
  223. * ww_acquire_init - initialize a w/w acquire context
  224. * @ctx: w/w acquire context to initialize
  225. * @ww_class: w/w class of the context
  226. *
  227. * Initializes an context to acquire multiple mutexes of the given w/w class.
  228. *
  229. * Context-based w/w mutex acquiring can be done in any order whatsoever within
  230. * a given lock class. Deadlocks will be detected and handled with the
  231. * wait/wound logic.
  232. *
  233. * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
  234. * result in undetected deadlocks and is so forbidden. Mixing different contexts
  235. * for the same w/w class when acquiring mutexes can also result in undetected
  236. * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
  237. * enabling CONFIG_PROVE_LOCKING.
  238. *
  239. * Nesting of acquire contexts for _different_ w/w classes is possible, subject
  240. * to the usual locking rules between different lock classes.
  241. *
  242. * An acquire context must be released with ww_acquire_fini by the same task
  243. * before the memory is freed. It is recommended to allocate the context itself
  244. * on the stack.
  245. */
  246. static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
  247. struct ww_class *ww_class)
  248. {
  249. ctx->task = current;
  250. ctx->stamp = atomic_long_inc_return(&ww_class->stamp);
  251. ctx->acquired = 0;
  252. #ifdef CONFIG_DEBUG_MUTEXES
  253. ctx->ww_class = ww_class;
  254. ctx->done_acquire = 0;
  255. ctx->contending_lock = NULL;
  256. #endif
  257. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  258. debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
  259. lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
  260. &ww_class->acquire_key, 0);
  261. mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
  262. #endif
  263. #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  264. ctx->deadlock_inject_interval = 1;
  265. ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
  266. #endif
  267. }
  268. /**
  269. * ww_acquire_done - marks the end of the acquire phase
  270. * @ctx: the acquire context
  271. *
  272. * Marks the end of the acquire phase, any further w/w mutex lock calls using
  273. * this context are forbidden.
  274. *
  275. * Calling this function is optional, it is just useful to document w/w mutex
  276. * code and clearly designated the acquire phase from actually using the locked
  277. * data structures.
  278. */
  279. static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
  280. {
  281. #ifdef CONFIG_DEBUG_MUTEXES
  282. lockdep_assert_held(ctx);
  283. DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
  284. ctx->done_acquire = 1;
  285. #endif
  286. }
  287. /**
  288. * ww_acquire_fini - releases a w/w acquire context
  289. * @ctx: the acquire context to free
  290. *
  291. * Releases a w/w acquire context. This must be called _after_ all acquired w/w
  292. * mutexes have been released with ww_mutex_unlock.
  293. */
  294. static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
  295. {
  296. #ifdef CONFIG_DEBUG_MUTEXES
  297. mutex_release(&ctx->dep_map, 0, _THIS_IP_);
  298. DEBUG_LOCKS_WARN_ON(ctx->acquired);
  299. if (!config_enabled(CONFIG_PROVE_LOCKING))
  300. /*
  301. * lockdep will normally handle this,
  302. * but fail without anyway
  303. */
  304. ctx->done_acquire = 1;
  305. if (!config_enabled(CONFIG_DEBUG_LOCK_ALLOC))
  306. /* ensure ww_acquire_fini will still fail if called twice */
  307. ctx->acquired = ~0U;
  308. #endif
  309. }
  310. extern int __must_check __ww_mutex_lock(struct ww_mutex *lock,
  311. struct ww_acquire_ctx *ctx);
  312. extern int __must_check __ww_mutex_lock_interruptible(struct ww_mutex *lock,
  313. struct ww_acquire_ctx *ctx);
  314. /**
  315. * ww_mutex_lock - acquire the w/w mutex
  316. * @lock: the mutex to be acquired
  317. * @ctx: w/w acquire context, or NULL to acquire only a single lock.
  318. *
  319. * Lock the w/w mutex exclusively for this task.
  320. *
  321. * Deadlocks within a given w/w class of locks are detected and handled with the
  322. * wait/wound algorithm. If the lock isn't immediately avaiable this function
  323. * will either sleep until it is (wait case). Or it selects the current context
  324. * for backing off by returning -EDEADLK (wound case). Trying to acquire the
  325. * same lock with the same context twice is also detected and signalled by
  326. * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
  327. *
  328. * In the wound case the caller must release all currently held w/w mutexes for
  329. * the given context and then wait for this contending lock to be available by
  330. * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
  331. * lock and proceed with trying to acquire further w/w mutexes (e.g. when
  332. * scanning through lru lists trying to free resources).
  333. *
  334. * The mutex must later on be released by the same task that
  335. * acquired it. The task may not exit without first unlocking the mutex. Also,
  336. * kernel memory where the mutex resides must not be freed with the mutex still
  337. * locked. The mutex must first be initialized (or statically defined) before it
  338. * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
  339. * of the same w/w lock class as was used to initialize the acquire context.
  340. *
  341. * A mutex acquired with this function must be released with ww_mutex_unlock.
  342. */
  343. static inline int ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  344. {
  345. if (ctx)
  346. return __ww_mutex_lock(lock, ctx);
  347. else {
  348. mutex_lock(&lock->base);
  349. return 0;
  350. }
  351. }
  352. /**
  353. * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
  354. * @lock: the mutex to be acquired
  355. * @ctx: w/w acquire context
  356. *
  357. * Lock the w/w mutex exclusively for this task.
  358. *
  359. * Deadlocks within a given w/w class of locks are detected and handled with the
  360. * wait/wound algorithm. If the lock isn't immediately avaiable this function
  361. * will either sleep until it is (wait case). Or it selects the current context
  362. * for backing off by returning -EDEADLK (wound case). Trying to acquire the
  363. * same lock with the same context twice is also detected and signalled by
  364. * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
  365. * signal arrives while waiting for the lock then this function returns -EINTR.
  366. *
  367. * In the wound case the caller must release all currently held w/w mutexes for
  368. * the given context and then wait for this contending lock to be available by
  369. * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
  370. * not acquire this lock and proceed with trying to acquire further w/w mutexes
  371. * (e.g. when scanning through lru lists trying to free resources).
  372. *
  373. * The mutex must later on be released by the same task that
  374. * acquired it. The task may not exit without first unlocking the mutex. Also,
  375. * kernel memory where the mutex resides must not be freed with the mutex still
  376. * locked. The mutex must first be initialized (or statically defined) before it
  377. * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
  378. * of the same w/w lock class as was used to initialize the acquire context.
  379. *
  380. * A mutex acquired with this function must be released with ww_mutex_unlock.
  381. */
  382. static inline int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
  383. struct ww_acquire_ctx *ctx)
  384. {
  385. if (ctx)
  386. return __ww_mutex_lock_interruptible(lock, ctx);
  387. else
  388. return mutex_lock_interruptible(&lock->base);
  389. }
  390. /**
  391. * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
  392. * @lock: the mutex to be acquired
  393. * @ctx: w/w acquire context
  394. *
  395. * Acquires a w/w mutex with the given context after a wound case. This function
  396. * will sleep until the lock becomes available.
  397. *
  398. * The caller must have released all w/w mutexes already acquired with the
  399. * context and then call this function on the contended lock.
  400. *
  401. * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
  402. * needs with ww_mutex_lock. Note that the -EALREADY return code from
  403. * ww_mutex_lock can be used to avoid locking this contended mutex twice.
  404. *
  405. * It is forbidden to call this function with any other w/w mutexes associated
  406. * with the context held. It is forbidden to call this on anything else than the
  407. * contending mutex.
  408. *
  409. * Note that the slowpath lock acquiring can also be done by calling
  410. * ww_mutex_lock directly. This function here is simply to help w/w mutex
  411. * locking code readability by clearly denoting the slowpath.
  412. */
  413. static inline void
  414. ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  415. {
  416. int ret;
  417. #ifdef CONFIG_DEBUG_MUTEXES
  418. DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
  419. #endif
  420. ret = ww_mutex_lock(lock, ctx);
  421. (void)ret;
  422. }
  423. /**
  424. * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex,
  425. * interruptible
  426. * @lock: the mutex to be acquired
  427. * @ctx: w/w acquire context
  428. *
  429. * Acquires a w/w mutex with the given context after a wound case. This function
  430. * will sleep until the lock becomes available and returns 0 when the lock has
  431. * been acquired. If a signal arrives while waiting for the lock then this
  432. * function returns -EINTR.
  433. *
  434. * The caller must have released all w/w mutexes already acquired with the
  435. * context and then call this function on the contended lock.
  436. *
  437. * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
  438. * needs with ww_mutex_lock. Note that the -EALREADY return code from
  439. * ww_mutex_lock can be used to avoid locking this contended mutex twice.
  440. *
  441. * It is forbidden to call this function with any other w/w mutexes associated
  442. * with the given context held. It is forbidden to call this on anything else
  443. * than the contending mutex.
  444. *
  445. * Note that the slowpath lock acquiring can also be done by calling
  446. * ww_mutex_lock_interruptible directly. This function here is simply to help
  447. * w/w mutex locking code readability by clearly denoting the slowpath.
  448. */
  449. static inline int __must_check
  450. ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
  451. struct ww_acquire_ctx *ctx)
  452. {
  453. #ifdef CONFIG_DEBUG_MUTEXES
  454. DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
  455. #endif
  456. return ww_mutex_lock_interruptible(lock, ctx);
  457. }
  458. extern void ww_mutex_unlock(struct ww_mutex *lock);
  459. /**
  460. * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
  461. * @lock: mutex to lock
  462. *
  463. * Trylocks a mutex without acquire context, so no deadlock detection is
  464. * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
  465. */
  466. static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
  467. {
  468. return mutex_trylock(&lock->base);
  469. }
  470. /***
  471. * ww_mutex_destroy - mark a w/w mutex unusable
  472. * @lock: the mutex to be destroyed
  473. *
  474. * This function marks the mutex uninitialized, and any subsequent
  475. * use of the mutex is forbidden. The mutex must not be locked when
  476. * this function is called.
  477. */
  478. static inline void ww_mutex_destroy(struct ww_mutex *lock)
  479. {
  480. mutex_destroy(&lock->base);
  481. }
  482. /**
  483. * ww_mutex_is_locked - is the w/w mutex locked
  484. * @lock: the mutex to be queried
  485. *
  486. * Returns 1 if the mutex is locked, 0 if unlocked.
  487. */
  488. static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
  489. {
  490. return mutex_is_locked(&lock->base);
  491. }
  492. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  493. #ifndef CONFIG_HAVE_ARCH_MUTEX_CPU_RELAX
  494. #define arch_mutex_cpu_relax() cpu_relax()
  495. #endif
  496. #endif