locking/mutex: Make mutex::wait_lock raw
[linux-2.6-microblaze.git] / include / linux / mutex.h
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
23 /*
24  * Simple, straightforward mutexes with strict semantics:
25  *
26  * - only one task can hold the mutex at a time
27  * - only the owner can unlock the mutex
28  * - multiple unlocks are not permitted
29  * - recursive locking is not permitted
30  * - a mutex object must be initialized via the API
31  * - a mutex object must not be initialized via memset or copying
32  * - task may not exit with mutex held
33  * - memory areas where held locks reside must not be freed
34  * - held mutexes must not be reinitialized
35  * - mutexes may not be used in hardware or software interrupt
36  *   contexts such as tasklets and timers
37  *
38  * These semantics are fully enforced when DEBUG_MUTEXES is
39  * enabled. Furthermore, besides enforcing the above rules, the mutex
40  * debugging code also implements a number of additional features
41  * that make lock debugging easier and faster:
42  *
43  * - uses symbolic names of mutexes, whenever they are printed in debug output
44  * - point-of-acquire tracking, symbolic lookup of function names
45  * - list of all locks held in the system, printout of them
46  * - owner tracking
47  * - detects self-recursing locks and prints out all relevant info
48  * - detects multi-task circular deadlocks and prints out all affected
49  *   locks and tasks (and only those tasks)
50  */
51 struct mutex {
52         atomic_long_t           owner;
53         raw_spinlock_t          wait_lock;
54 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
55         struct optimistic_spin_queue osq; /* Spinner MCS lock */
56 #endif
57         struct list_head        wait_list;
58 #ifdef CONFIG_DEBUG_MUTEXES
59         void                    *magic;
60 #endif
61 #ifdef CONFIG_DEBUG_LOCK_ALLOC
62         struct lockdep_map      dep_map;
63 #endif
64 };
65
66 #ifdef CONFIG_DEBUG_MUTEXES
67
68 #define __DEBUG_MUTEX_INITIALIZER(lockname)                             \
69         , .magic = &lockname
70
71 extern void mutex_destroy(struct mutex *lock);
72
73 #else
74
75 # define __DEBUG_MUTEX_INITIALIZER(lockname)
76
77 static inline void mutex_destroy(struct mutex *lock) {}
78
79 #endif
80
81 /**
82  * mutex_init - initialize the mutex
83  * @mutex: the mutex to be initialized
84  *
85  * Initialize the mutex to unlocked state.
86  *
87  * It is not allowed to initialize an already locked mutex.
88  */
89 #define mutex_init(mutex)                                               \
90 do {                                                                    \
91         static struct lock_class_key __key;                             \
92                                                                         \
93         __mutex_init((mutex), #mutex, &__key);                          \
94 } while (0)
95
96 #ifdef CONFIG_DEBUG_LOCK_ALLOC
97 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)                  \
98                 , .dep_map = {                                  \
99                         .name = #lockname,                      \
100                         .wait_type_inner = LD_WAIT_SLEEP,       \
101                 }
102 #else
103 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
104 #endif
105
106 #define __MUTEX_INITIALIZER(lockname) \
107                 { .owner = ATOMIC_LONG_INIT(0) \
108                 , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
109                 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
110                 __DEBUG_MUTEX_INITIALIZER(lockname) \
111                 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
112
113 #define DEFINE_MUTEX(mutexname) \
114         struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
115
116 extern void __mutex_init(struct mutex *lock, const char *name,
117                          struct lock_class_key *key);
118
119 /**
120  * mutex_is_locked - is the mutex locked
121  * @lock: the mutex to be queried
122  *
123  * Returns true if the mutex is locked, false if unlocked.
124  */
125 extern bool mutex_is_locked(struct mutex *lock);
126
127 /*
128  * See kernel/locking/mutex.c for detailed documentation of these APIs.
129  * Also see Documentation/locking/mutex-design.rst.
130  */
131 #ifdef CONFIG_DEBUG_LOCK_ALLOC
132 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
133 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
134
135 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
136                                         unsigned int subclass);
137 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
138                                         unsigned int subclass);
139 extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
140
141 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
142 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
143 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
144 #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
145
146 #define mutex_lock_nest_lock(lock, nest_lock)                           \
147 do {                                                                    \
148         typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
149         _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map);             \
150 } while (0)
151
152 #else
153 extern void mutex_lock(struct mutex *lock);
154 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
155 extern int __must_check mutex_lock_killable(struct mutex *lock);
156 extern void mutex_lock_io(struct mutex *lock);
157
158 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
159 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
160 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
161 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
162 # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
163 #endif
164
165 /*
166  * NOTE: mutex_trylock() follows the spin_trylock() convention,
167  *       not the down_trylock() convention!
168  *
169  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
170  */
171 extern int mutex_trylock(struct mutex *lock);
172 extern void mutex_unlock(struct mutex *lock);
173
174 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
175
176 #endif /* __LINUX_MUTEX_H */