]> git.proxmox.com Git - mirror_spl.git/blob - include/sys/mutex.h
Fix race condition in mutex_exit()
[mirror_spl.git] / include / sys / mutex.h
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 \*****************************************************************************/
24
25 #ifndef _SPL_MUTEX_H
26 #define _SPL_MUTEX_H
27
28 #include <sys/types.h>
29 #include <linux/mutex.h>
30 #include <linux/compiler_compat.h>
31
32 typedef enum {
33 MUTEX_DEFAULT = 0,
34 MUTEX_SPIN = 1,
35 MUTEX_ADAPTIVE = 2
36 } kmutex_type_t;
37
38 #if defined(HAVE_MUTEX_OWNER) && defined(CONFIG_SMP) && !defined(CONFIG_DEBUG_MUTEXES)
39
40 /*
41 * We define a 1-field struct rather than a straight typedef to enforce type
42 * safety.
43 */
44 typedef struct {
45 struct mutex m;
46 } kmutex_t;
47
48 static inline kthread_t *
49 mutex_owner(kmutex_t *mp)
50 {
51 #if defined(HAVE_MUTEX_OWNER_TASK_STRUCT)
52 return ACCESS_ONCE(mp->m.owner);
53 #else
54 struct thread_info *owner = ACCESS_ONCE(mp->m.owner);
55 if (owner)
56 return owner->task;
57
58 return NULL;
59 #endif
60 }
61
62 #define mutex_owned(mp) (mutex_owner(mp) == current)
63 #define MUTEX_HELD(mp) mutex_owned(mp)
64 #define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
65 #undef mutex_init
66 #define mutex_init(mp, name, type, ibc) \
67 ({ \
68 static struct lock_class_key __key; \
69 ASSERT(type == MUTEX_DEFAULT); \
70 \
71 __mutex_init(&(mp)->m, #mp, &__key); \
72 })
73
74 #undef mutex_destroy
75 #define mutex_destroy(mp) \
76 ({ \
77 VERIFY3P(mutex_owner(mp), ==, NULL); \
78 })
79
80 #define mutex_tryenter(mp) mutex_trylock(&(mp)->m)
81 #define mutex_enter(mp) mutex_lock(&(mp)->m)
82 #define mutex_exit(mp) mutex_unlock(&(mp)->m)
83
84 #ifdef HAVE_GPL_ONLY_SYMBOLS
85 # define mutex_enter_nested(mp, sc) mutex_lock_nested(&(mp)->m, sc)
86 #else
87 # define mutex_enter_nested(mp, sc) mutex_enter(mp)
88 #endif /* HAVE_GPL_ONLY_SYMBOLS */
89
90 #else /* HAVE_MUTEX_OWNER */
91
92 typedef struct {
93 struct mutex m_mutex;
94 kthread_t *m_owner;
95 } kmutex_t;
96
97 #ifdef HAVE_TASK_CURR
98 extern int spl_mutex_spin_max(void);
99 #else /* HAVE_TASK_CURR */
100 # define task_curr(owner) 0
101 # define spl_mutex_spin_max() 0
102 #endif /* HAVE_TASK_CURR */
103
104 #define MUTEX(mp) ((struct mutex *)(mp))
105
106 static inline kthread_t *
107 spl_mutex_get_owner(kmutex_t *mp)
108 {
109 return mp->m_owner;
110 }
111
112 static inline void
113 spl_mutex_set_owner(kmutex_t *mp)
114 {
115 unsigned long flags;
116
117 spin_lock_irqsave(&MUTEX(mp)->wait_lock, flags);
118 mp->m_owner = current;
119 spin_unlock_irqrestore(&MUTEX(mp)->wait_lock, flags);
120 }
121
122 static inline void
123 spl_mutex_clear_owner(kmutex_t *mp)
124 {
125 unsigned long flags;
126
127 spin_lock_irqsave(&MUTEX(mp)->wait_lock, flags);
128 mp->m_owner = NULL;
129 spin_unlock_irqrestore(&MUTEX(mp)->wait_lock, flags);
130 }
131
132 static inline kthread_t *
133 mutex_owner(kmutex_t *mp)
134 {
135 unsigned long flags;
136 kthread_t *owner;
137
138 spin_lock_irqsave(&MUTEX(mp)->wait_lock, flags);
139 owner = spl_mutex_get_owner(mp);
140 spin_unlock_irqrestore(&MUTEX(mp)->wait_lock, flags);
141
142 return owner;
143 }
144
145 #define mutex_owned(mp) (mutex_owner(mp) == current)
146 #define MUTEX_HELD(mp) mutex_owned(mp)
147 #define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
148
149 /*
150 * The following functions must be a #define and not static inline.
151 * This ensures that the native linux mutex functions (lock/unlock)
152 * will be correctly located in the users code which is important
153 * for the built in kernel lock analysis tools
154 */
155 #undef mutex_init
156 #define mutex_init(mp, name, type, ibc) \
157 ({ \
158 static struct lock_class_key __key; \
159 ASSERT(type == MUTEX_DEFAULT); \
160 \
161 __mutex_init(MUTEX(mp), #mp, &__key); \
162 spl_mutex_clear_owner(mp); \
163 })
164
165 #undef mutex_destroy
166 #define mutex_destroy(mp) \
167 ({ \
168 VERIFY3P(mutex_owner(mp), ==, NULL); \
169 })
170
171 #define mutex_tryenter(mp) \
172 ({ \
173 int _rc_; \
174 \
175 if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) \
176 spl_mutex_set_owner(mp); \
177 \
178 _rc_; \
179 })
180
181 /*
182 * Adaptive mutexs assume that the lock may be held by a task running
183 * on a different cpu. The expectation is that the task will drop the
184 * lock before leaving the head of the run queue. So the ideal thing
185 * to do is spin until we acquire the lock and avoid a context switch.
186 * However it is also possible the task holding the lock yields the
187 * processor with out dropping lock. In this case, we know it's going
188 * to be a while so we stop spinning and go to sleep waiting for the
189 * lock to be available. This should strike the optimum balance
190 * between spinning and sleeping waiting for a lock.
191 */
192 #define mutex_enter(mp) \
193 ({ \
194 kthread_t *_owner_; \
195 int _rc_, _count_; \
196 \
197 _rc_ = 0; \
198 _count_ = 0; \
199 _owner_ = mutex_owner(mp); \
200 \
201 while (_owner_ && task_curr(_owner_) && \
202 _count_ <= spl_mutex_spin_max()) { \
203 if ((_rc_ = mutex_trylock(MUTEX(mp)))) \
204 break; \
205 \
206 _count_++; \
207 } \
208 \
209 if (!_rc_) \
210 mutex_lock(MUTEX(mp)); \
211 \
212 spl_mutex_set_owner(mp); \
213 })
214
215 #define mutex_exit(mp) \
216 ({ \
217 spl_mutex_clear_owner(mp); \
218 mutex_unlock(MUTEX(mp)); \
219 })
220
221 #ifdef HAVE_GPL_ONLY_SYMBOLS
222 # define mutex_enter_nested(mp, sc) \
223 ({ \
224 mutex_lock_nested(MUTEX(mp, sc)); \
225 spl_mutex_set_owner(mp); \
226 })
227 #else
228 # define mutex_enter_nested(mp, sc) \
229 ({ \
230 mutex_enter(mp); \
231 })
232 #endif
233
234 #endif /* HAVE_MUTEX_OWNER */
235
236 int spl_mutex_init(void);
237 void spl_mutex_fini(void);
238
239 #endif /* _SPL_MUTEX_H */