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