at v6.5 7.0 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * Mutexes: blocking mutual exclusion locks 4 * 5 * started by Ingo Molnar: 6 * 7 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 8 * 9 * This file contains the main data structure and API definitions. 10 */ 11#ifndef __LINUX_MUTEX_H 12#define __LINUX_MUTEX_H 13 14#include <asm/current.h> 15#include <linux/list.h> 16#include <linux/spinlock_types.h> 17#include <linux/lockdep.h> 18#include <linux/atomic.h> 19#include <asm/processor.h> 20#include <linux/osq_lock.h> 21#include <linux/debug_locks.h> 22#include <linux/cleanup.h> 23 24#ifdef CONFIG_DEBUG_LOCK_ALLOC 25# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \ 26 , .dep_map = { \ 27 .name = #lockname, \ 28 .wait_type_inner = LD_WAIT_SLEEP, \ 29 } 30#else 31# define __DEP_MAP_MUTEX_INITIALIZER(lockname) 32#endif 33 34#ifndef CONFIG_PREEMPT_RT 35 36/* 37 * Simple, straightforward mutexes with strict semantics: 38 * 39 * - only one task can hold the mutex at a time 40 * - only the owner can unlock the mutex 41 * - multiple unlocks are not permitted 42 * - recursive locking is not permitted 43 * - a mutex object must be initialized via the API 44 * - a mutex object must not be initialized via memset or copying 45 * - task may not exit with mutex held 46 * - memory areas where held locks reside must not be freed 47 * - held mutexes must not be reinitialized 48 * - mutexes may not be used in hardware or software interrupt 49 * contexts such as tasklets and timers 50 * 51 * These semantics are fully enforced when DEBUG_MUTEXES is 52 * enabled. Furthermore, besides enforcing the above rules, the mutex 53 * debugging code also implements a number of additional features 54 * that make lock debugging easier and faster: 55 * 56 * - uses symbolic names of mutexes, whenever they are printed in debug output 57 * - point-of-acquire tracking, symbolic lookup of function names 58 * - list of all locks held in the system, printout of them 59 * - owner tracking 60 * - detects self-recursing locks and prints out all relevant info 61 * - detects multi-task circular deadlocks and prints out all affected 62 * locks and tasks (and only those tasks) 63 */ 64struct mutex { 65 atomic_long_t owner; 66 raw_spinlock_t wait_lock; 67#ifdef CONFIG_MUTEX_SPIN_ON_OWNER 68 struct optimistic_spin_queue osq; /* Spinner MCS lock */ 69#endif 70 struct list_head wait_list; 71#ifdef CONFIG_DEBUG_MUTEXES 72 void *magic; 73#endif 74#ifdef CONFIG_DEBUG_LOCK_ALLOC 75 struct lockdep_map dep_map; 76#endif 77}; 78 79#ifdef CONFIG_DEBUG_MUTEXES 80 81#define __DEBUG_MUTEX_INITIALIZER(lockname) \ 82 , .magic = &lockname 83 84extern void mutex_destroy(struct mutex *lock); 85 86#else 87 88# define __DEBUG_MUTEX_INITIALIZER(lockname) 89 90static inline void mutex_destroy(struct mutex *lock) {} 91 92#endif 93 94/** 95 * mutex_init - initialize the mutex 96 * @mutex: the mutex to be initialized 97 * 98 * Initialize the mutex to unlocked state. 99 * 100 * It is not allowed to initialize an already locked mutex. 101 */ 102#define mutex_init(mutex) \ 103do { \ 104 static struct lock_class_key __key; \ 105 \ 106 __mutex_init((mutex), #mutex, &__key); \ 107} while (0) 108 109#define __MUTEX_INITIALIZER(lockname) \ 110 { .owner = ATOMIC_LONG_INIT(0) \ 111 , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \ 112 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \ 113 __DEBUG_MUTEX_INITIALIZER(lockname) \ 114 __DEP_MAP_MUTEX_INITIALIZER(lockname) } 115 116#define DEFINE_MUTEX(mutexname) \ 117 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) 118 119extern void __mutex_init(struct mutex *lock, const char *name, 120 struct lock_class_key *key); 121 122/** 123 * mutex_is_locked - is the mutex locked 124 * @lock: the mutex to be queried 125 * 126 * Returns true if the mutex is locked, false if unlocked. 127 */ 128extern bool mutex_is_locked(struct mutex *lock); 129 130#else /* !CONFIG_PREEMPT_RT */ 131/* 132 * Preempt-RT variant based on rtmutexes. 133 */ 134#include <linux/rtmutex.h> 135 136struct mutex { 137 struct rt_mutex_base rtmutex; 138#ifdef CONFIG_DEBUG_LOCK_ALLOC 139 struct lockdep_map dep_map; 140#endif 141}; 142 143#define __MUTEX_INITIALIZER(mutexname) \ 144{ \ 145 .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex) \ 146 __DEP_MAP_MUTEX_INITIALIZER(mutexname) \ 147} 148 149#define DEFINE_MUTEX(mutexname) \ 150 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) 151 152extern void __mutex_rt_init(struct mutex *lock, const char *name, 153 struct lock_class_key *key); 154extern int mutex_trylock(struct mutex *lock); 155 156static inline void mutex_destroy(struct mutex *lock) { } 157 158#define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex) 159 160#define __mutex_init(mutex, name, key) \ 161do { \ 162 rt_mutex_base_init(&(mutex)->rtmutex); \ 163 __mutex_rt_init((mutex), name, key); \ 164} while (0) 165 166#define mutex_init(mutex) \ 167do { \ 168 static struct lock_class_key __key; \ 169 \ 170 __mutex_init((mutex), #mutex, &__key); \ 171} while (0) 172#endif /* CONFIG_PREEMPT_RT */ 173 174/* 175 * See kernel/locking/mutex.c for detailed documentation of these APIs. 176 * Also see Documentation/locking/mutex-design.rst. 177 */ 178#ifdef CONFIG_DEBUG_LOCK_ALLOC 179extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass); 180extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock); 181 182extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock, 183 unsigned int subclass); 184extern int __must_check mutex_lock_killable_nested(struct mutex *lock, 185 unsigned int subclass); 186extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass); 187 188#define mutex_lock(lock) mutex_lock_nested(lock, 0) 189#define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0) 190#define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0) 191#define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0) 192 193#define mutex_lock_nest_lock(lock, nest_lock) \ 194do { \ 195 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ 196 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \ 197} while (0) 198 199#else 200extern void mutex_lock(struct mutex *lock); 201extern int __must_check mutex_lock_interruptible(struct mutex *lock); 202extern int __must_check mutex_lock_killable(struct mutex *lock); 203extern void mutex_lock_io(struct mutex *lock); 204 205# define mutex_lock_nested(lock, subclass) mutex_lock(lock) 206# define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock) 207# define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock) 208# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock) 209# define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock) 210#endif 211 212/* 213 * NOTE: mutex_trylock() follows the spin_trylock() convention, 214 * not the down_trylock() convention! 215 * 216 * Returns 1 if the mutex has been acquired successfully, and 0 on contention. 217 */ 218extern int mutex_trylock(struct mutex *lock); 219extern void mutex_unlock(struct mutex *lock); 220 221extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); 222 223DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T)) 224DEFINE_FREE(mutex, struct mutex *, if (_T) mutex_unlock(_T)) 225 226#endif /* __LINUX_MUTEX_H */