![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 /* 0003 * Wound/Wait Mutexes: blocking mutual exclusion locks with deadlock avoidance 0004 * 0005 * Original mutex implementation started by Ingo Molnar: 0006 * 0007 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 0008 * 0009 * Wait/Die implementation: 0010 * Copyright (C) 2013 Canonical Ltd. 0011 * Choice of algorithm: 0012 * Copyright (C) 2018 WMWare Inc. 0013 * 0014 * This file contains the main data structure and API definitions. 0015 */ 0016 0017 #ifndef __LINUX_WW_MUTEX_H 0018 #define __LINUX_WW_MUTEX_H 0019 0020 #include <linux/mutex.h> 0021 #include <linux/rtmutex.h> 0022 0023 #if defined(CONFIG_DEBUG_MUTEXES) || \ 0024 (defined(CONFIG_PREEMPT_RT) && defined(CONFIG_DEBUG_RT_MUTEXES)) 0025 #define DEBUG_WW_MUTEXES 0026 #endif 0027 0028 #ifndef CONFIG_PREEMPT_RT 0029 #define WW_MUTEX_BASE mutex 0030 #define ww_mutex_base_init(l,n,k) __mutex_init(l,n,k) 0031 #define ww_mutex_base_is_locked(b) mutex_is_locked((b)) 0032 #else 0033 #define WW_MUTEX_BASE rt_mutex 0034 #define ww_mutex_base_init(l,n,k) __rt_mutex_init(l,n,k) 0035 #define ww_mutex_base_is_locked(b) rt_mutex_base_is_locked(&(b)->rtmutex) 0036 #endif 0037 0038 struct ww_class { 0039 atomic_long_t stamp; 0040 struct lock_class_key acquire_key; 0041 struct lock_class_key mutex_key; 0042 const char *acquire_name; 0043 const char *mutex_name; 0044 unsigned int is_wait_die; 0045 }; 0046 0047 struct ww_mutex { 0048 struct WW_MUTEX_BASE base; 0049 struct ww_acquire_ctx *ctx; 0050 #ifdef DEBUG_WW_MUTEXES 0051 struct ww_class *ww_class; 0052 #endif 0053 }; 0054 0055 struct ww_acquire_ctx { 0056 struct task_struct *task; 0057 unsigned long stamp; 0058 unsigned int acquired; 0059 unsigned short wounded; 0060 unsigned short is_wait_die; 0061 #ifdef DEBUG_WW_MUTEXES 0062 unsigned int done_acquire; 0063 struct ww_class *ww_class; 0064 void *contending_lock; 0065 #endif 0066 #ifdef CONFIG_DEBUG_LOCK_ALLOC 0067 struct lockdep_map dep_map; 0068 #endif 0069 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 0070 unsigned int deadlock_inject_interval; 0071 unsigned int deadlock_inject_countdown; 0072 #endif 0073 }; 0074 0075 #define __WW_CLASS_INITIALIZER(ww_class, _is_wait_die) \ 0076 { .stamp = ATOMIC_LONG_INIT(0) \ 0077 , .acquire_name = #ww_class "_acquire" \ 0078 , .mutex_name = #ww_class "_mutex" \ 0079 , .is_wait_die = _is_wait_die } 0080 0081 #define DEFINE_WD_CLASS(classname) \ 0082 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1) 0083 0084 #define DEFINE_WW_CLASS(classname) \ 0085 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0) 0086 0087 /** 0088 * ww_mutex_init - initialize the w/w mutex 0089 * @lock: the mutex to be initialized 0090 * @ww_class: the w/w class the mutex should belong to 0091 * 0092 * Initialize the w/w mutex to unlocked state and associate it with the given 0093 * class. Static define macro for w/w mutex is not provided and this function 0094 * is the only way to properly initialize the w/w mutex. 0095 * 0096 * It is not allowed to initialize an already locked mutex. 0097 */ 0098 static inline void ww_mutex_init(struct ww_mutex *lock, 0099 struct ww_class *ww_class) 0100 { 0101 ww_mutex_base_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key); 0102 lock->ctx = NULL; 0103 #ifdef DEBUG_WW_MUTEXES 0104 lock->ww_class = ww_class; 0105 #endif 0106 } 0107 0108 /** 0109 * ww_acquire_init - initialize a w/w acquire context 0110 * @ctx: w/w acquire context to initialize 0111 * @ww_class: w/w class of the context 0112 * 0113 * Initializes an context to acquire multiple mutexes of the given w/w class. 0114 * 0115 * Context-based w/w mutex acquiring can be done in any order whatsoever within 0116 * a given lock class. Deadlocks will be detected and handled with the 0117 * wait/die logic. 0118 * 0119 * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can 0120 * result in undetected deadlocks and is so forbidden. Mixing different contexts 0121 * for the same w/w class when acquiring mutexes can also result in undetected 0122 * deadlocks, and is hence also forbidden. Both types of abuse will be caught by 0123 * enabling CONFIG_PROVE_LOCKING. 0124 * 0125 * Nesting of acquire contexts for _different_ w/w classes is possible, subject 0126 * to the usual locking rules between different lock classes. 0127 * 0128 * An acquire context must be released with ww_acquire_fini by the same task 0129 * before the memory is freed. It is recommended to allocate the context itself 0130 * on the stack. 0131 */ 0132 static inline void ww_acquire_init(struct ww_acquire_ctx *ctx, 0133 struct ww_class *ww_class) 0134 { 0135 ctx->task = current; 0136 ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp); 0137 ctx->acquired = 0; 0138 ctx->wounded = false; 0139 ctx->is_wait_die = ww_class->is_wait_die; 0140 #ifdef DEBUG_WW_MUTEXES 0141 ctx->ww_class = ww_class; 0142 ctx->done_acquire = 0; 0143 ctx->contending_lock = NULL; 0144 #endif 0145 #ifdef CONFIG_DEBUG_LOCK_ALLOC 0146 debug_check_no_locks_freed((void *)ctx, sizeof(*ctx)); 0147 lockdep_init_map(&ctx->dep_map, ww_class->acquire_name, 0148 &ww_class->acquire_key, 0); 0149 mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_); 0150 #endif 0151 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 0152 ctx->deadlock_inject_interval = 1; 0153 ctx->deadlock_inject_countdown = ctx->stamp & 0xf; 0154 #endif 0155 } 0156 0157 /** 0158 * ww_acquire_done - marks the end of the acquire phase 0159 * @ctx: the acquire context 0160 * 0161 * Marks the end of the acquire phase, any further w/w mutex lock calls using 0162 * this context are forbidden. 0163 * 0164 * Calling this function is optional, it is just useful to document w/w mutex 0165 * code and clearly designated the acquire phase from actually using the locked 0166 * data structures. 0167 */ 0168 static inline void ww_acquire_done(struct ww_acquire_ctx *ctx) 0169 { 0170 #ifdef DEBUG_WW_MUTEXES 0171 lockdep_assert_held(ctx); 0172 0173 DEBUG_LOCKS_WARN_ON(ctx->done_acquire); 0174 ctx->done_acquire = 1; 0175 #endif 0176 } 0177 0178 /** 0179 * ww_acquire_fini - releases a w/w acquire context 0180 * @ctx: the acquire context to free 0181 * 0182 * Releases a w/w acquire context. This must be called _after_ all acquired w/w 0183 * mutexes have been released with ww_mutex_unlock. 0184 */ 0185 static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx) 0186 { 0187 #ifdef CONFIG_DEBUG_LOCK_ALLOC 0188 mutex_release(&ctx->dep_map, _THIS_IP_); 0189 #endif 0190 #ifdef DEBUG_WW_MUTEXES 0191 DEBUG_LOCKS_WARN_ON(ctx->acquired); 0192 if (!IS_ENABLED(CONFIG_PROVE_LOCKING)) 0193 /* 0194 * lockdep will normally handle this, 0195 * but fail without anyway 0196 */ 0197 ctx->done_acquire = 1; 0198 0199 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC)) 0200 /* ensure ww_acquire_fini will still fail if called twice */ 0201 ctx->acquired = ~0U; 0202 #endif 0203 } 0204 0205 /** 0206 * ww_mutex_lock - acquire the w/w mutex 0207 * @lock: the mutex to be acquired 0208 * @ctx: w/w acquire context, or NULL to acquire only a single lock. 0209 * 0210 * Lock the w/w mutex exclusively for this task. 0211 * 0212 * Deadlocks within a given w/w class of locks are detected and handled with the 0213 * wait/die algorithm. If the lock isn't immediately available this function 0214 * will either sleep until it is (wait case). Or it selects the current context 0215 * for backing off by returning -EDEADLK (die case). Trying to acquire the 0216 * same lock with the same context twice is also detected and signalled by 0217 * returning -EALREADY. Returns 0 if the mutex was successfully acquired. 0218 * 0219 * In the die case the caller must release all currently held w/w mutexes for 0220 * the given context and then wait for this contending lock to be available by 0221 * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this 0222 * lock and proceed with trying to acquire further w/w mutexes (e.g. when 0223 * scanning through lru lists trying to free resources). 0224 * 0225 * The mutex must later on be released by the same task that 0226 * acquired it. The task may not exit without first unlocking the mutex. Also, 0227 * kernel memory where the mutex resides must not be freed with the mutex still 0228 * locked. The mutex must first be initialized (or statically defined) before it 0229 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be 0230 * of the same w/w lock class as was used to initialize the acquire context. 0231 * 0232 * A mutex acquired with this function must be released with ww_mutex_unlock. 0233 */ 0234 extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx); 0235 0236 /** 0237 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible 0238 * @lock: the mutex to be acquired 0239 * @ctx: w/w acquire context 0240 * 0241 * Lock the w/w mutex exclusively for this task. 0242 * 0243 * Deadlocks within a given w/w class of locks are detected and handled with the 0244 * wait/die algorithm. If the lock isn't immediately available this function 0245 * will either sleep until it is (wait case). Or it selects the current context 0246 * for backing off by returning -EDEADLK (die case). Trying to acquire the 0247 * same lock with the same context twice is also detected and signalled by 0248 * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a 0249 * signal arrives while waiting for the lock then this function returns -EINTR. 0250 * 0251 * In the die case the caller must release all currently held w/w mutexes for 0252 * the given context and then wait for this contending lock to be available by 0253 * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to 0254 * not acquire this lock and proceed with trying to acquire further w/w mutexes 0255 * (e.g. when scanning through lru lists trying to free resources). 0256 * 0257 * The mutex must later on be released by the same task that 0258 * acquired it. The task may not exit without first unlocking the mutex. Also, 0259 * kernel memory where the mutex resides must not be freed with the mutex still 0260 * locked. The mutex must first be initialized (or statically defined) before it 0261 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be 0262 * of the same w/w lock class as was used to initialize the acquire context. 0263 * 0264 * A mutex acquired with this function must be released with ww_mutex_unlock. 0265 */ 0266 extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock, 0267 struct ww_acquire_ctx *ctx); 0268 0269 /** 0270 * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex 0271 * @lock: the mutex to be acquired 0272 * @ctx: w/w acquire context 0273 * 0274 * Acquires a w/w mutex with the given context after a die case. This function 0275 * will sleep until the lock becomes available. 0276 * 0277 * The caller must have released all w/w mutexes already acquired with the 0278 * context and then call this function on the contended lock. 0279 * 0280 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it 0281 * needs with ww_mutex_lock. Note that the -EALREADY return code from 0282 * ww_mutex_lock can be used to avoid locking this contended mutex twice. 0283 * 0284 * It is forbidden to call this function with any other w/w mutexes associated 0285 * with the context held. It is forbidden to call this on anything else than the 0286 * contending mutex. 0287 * 0288 * Note that the slowpath lock acquiring can also be done by calling 0289 * ww_mutex_lock directly. This function here is simply to help w/w mutex 0290 * locking code readability by clearly denoting the slowpath. 0291 */ 0292 static inline void 0293 ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 0294 { 0295 int ret; 0296 #ifdef DEBUG_WW_MUTEXES 0297 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock); 0298 #endif 0299 ret = ww_mutex_lock(lock, ctx); 0300 (void)ret; 0301 } 0302 0303 /** 0304 * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible 0305 * @lock: the mutex to be acquired 0306 * @ctx: w/w acquire context 0307 * 0308 * Acquires a w/w mutex with the given context after a die case. This function 0309 * will sleep until the lock becomes available and returns 0 when the lock has 0310 * been acquired. If a signal arrives while waiting for the lock then this 0311 * function returns -EINTR. 0312 * 0313 * The caller must have released all w/w mutexes already acquired with the 0314 * context and then call this function on the contended lock. 0315 * 0316 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it 0317 * needs with ww_mutex_lock. Note that the -EALREADY return code from 0318 * ww_mutex_lock can be used to avoid locking this contended mutex twice. 0319 * 0320 * It is forbidden to call this function with any other w/w mutexes associated 0321 * with the given context held. It is forbidden to call this on anything else 0322 * than the contending mutex. 0323 * 0324 * Note that the slowpath lock acquiring can also be done by calling 0325 * ww_mutex_lock_interruptible directly. This function here is simply to help 0326 * w/w mutex locking code readability by clearly denoting the slowpath. 0327 */ 0328 static inline int __must_check 0329 ww_mutex_lock_slow_interruptible(struct ww_mutex *lock, 0330 struct ww_acquire_ctx *ctx) 0331 { 0332 #ifdef DEBUG_WW_MUTEXES 0333 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock); 0334 #endif 0335 return ww_mutex_lock_interruptible(lock, ctx); 0336 } 0337 0338 extern void ww_mutex_unlock(struct ww_mutex *lock); 0339 0340 extern int __must_check ww_mutex_trylock(struct ww_mutex *lock, 0341 struct ww_acquire_ctx *ctx); 0342 0343 /*** 0344 * ww_mutex_destroy - mark a w/w mutex unusable 0345 * @lock: the mutex to be destroyed 0346 * 0347 * This function marks the mutex uninitialized, and any subsequent 0348 * use of the mutex is forbidden. The mutex must not be locked when 0349 * this function is called. 0350 */ 0351 static inline void ww_mutex_destroy(struct ww_mutex *lock) 0352 { 0353 #ifndef CONFIG_PREEMPT_RT 0354 mutex_destroy(&lock->base); 0355 #endif 0356 } 0357 0358 /** 0359 * ww_mutex_is_locked - is the w/w mutex locked 0360 * @lock: the mutex to be queried 0361 * 0362 * Returns 1 if the mutex is locked, 0 if unlocked. 0363 */ 0364 static inline bool ww_mutex_is_locked(struct ww_mutex *lock) 0365 { 0366 return ww_mutex_base_is_locked(&lock->base); 0367 } 0368 0369 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |