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