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