]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/mutex.h
Fix cstyle warnings
[mirror_spl.git] / include / sys / mutex.h
CommitLineData
a900e28e 1/*
716154c5
BB
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>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
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.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 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
716154c5 22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
a900e28e 23 */
715f6251 24
09b414e8 25#ifndef _SPL_MUTEX_H
a900e28e 26#define _SPL_MUTEX_H
f1ca4da6 27
f4b37741 28#include <sys/types.h>
4d54fdee 29#include <linux/mutex.h>
22cd0f19 30#include <linux/compiler_compat.h>
692ae8d3 31#include <linux/lockdep.h>
4d54fdee
BB
32
33typedef enum {
a900e28e
BB
34 MUTEX_DEFAULT = 0,
35 MUTEX_SPIN = 1,
692ae8d3
OF
36 MUTEX_ADAPTIVE = 2,
37 MUTEX_NOLOCKDEP = 3
4d54fdee 38} kmutex_type_t;
f1b59d26 39
f1ca4da6 40typedef struct {
a900e28e
BB
41 struct mutex m_mutex;
42 spinlock_t m_lock; /* used for serializing mutex_exit */
43 kthread_t *m_owner;
692ae8d3
OF
44#ifdef CONFIG_LOCKDEP
45 kmutex_type_t m_type;
46#endif /* CONFIG_LOCKDEP */
f1ca4da6 47} kmutex_t;
48
a900e28e 49#define MUTEX(mp) (&((mp)->m_mutex))
ae26dd00
TC
50
51static inline void
52spl_mutex_set_owner(kmutex_t *mp)
53{
54 mp->m_owner = current;
55}
56
57static inline void
58spl_mutex_clear_owner(kmutex_t *mp)
59{
60 mp->m_owner = NULL;
61}
62
549423c0 63#define mutex_owner(mp) (ACCESS_ONCE((mp)->m_owner))
a900e28e
BB
64#define mutex_owned(mp) (mutex_owner(mp) == current)
65#define MUTEX_HELD(mp) mutex_owned(mp)
66#define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
f1ca4da6 67
692ae8d3
OF
68#ifdef CONFIG_LOCKDEP
69static inline void
70spl_mutex_set_type(kmutex_t *mp, kmutex_type_t type)
71{
72 mp->m_type = type;
73}
74static inline void
75spl_mutex_lockdep_off_maybe(kmutex_t *mp) \
76{ \
77 if (mp && mp->m_type == MUTEX_NOLOCKDEP) \
78 lockdep_off(); \
79}
80static inline void
81spl_mutex_lockdep_on_maybe(kmutex_t *mp) \
82{ \
83 if (mp && mp->m_type == MUTEX_NOLOCKDEP) \
84 lockdep_on(); \
85}
86#else /* CONFIG_LOCKDEP */
5461eefe
BB
87#define spl_mutex_set_type(mp, type)
88#define spl_mutex_lockdep_off_maybe(mp)
89#define spl_mutex_lockdep_on_maybe(mp)
692ae8d3
OF
90#endif /* CONFIG_LOCKDEP */
91
4d54fdee 92/*
5461eefe 93 * The following functions must be a #define and not static inline.
4d54fdee
BB
94 * This ensures that the native linux mutex functions (lock/unlock)
95 * will be correctly located in the users code which is important
96 * for the built in kernel lock analysis tools
97 */
9ab1ac14 98#undef mutex_init
a900e28e
BB
99#define mutex_init(mp, name, type, ibc) \
100{ \
101 static struct lock_class_key __key; \
692ae8d3 102 ASSERT(type == MUTEX_DEFAULT || type == MUTEX_NOLOCKDEP); \
a900e28e 103 \
79a0056e 104 __mutex_init(MUTEX(mp), (name) ? (#name) : (#mp), &__key); \
a900e28e 105 spin_lock_init(&(mp)->m_lock); \
ae26dd00 106 spl_mutex_clear_owner(mp); \
692ae8d3 107 spl_mutex_set_type(mp, type); \
a900e28e 108}
4d54fdee 109
9ab1ac14 110#undef mutex_destroy
a900e28e
BB
111#define mutex_destroy(mp) \
112{ \
113 VERIFY3P(mutex_owner(mp), ==, NULL); \
114}
d61e12af 115
5461eefe 116/* BEGIN CSTYLED */
a900e28e
BB
117#define mutex_tryenter(mp) \
118({ \
119 int _rc_; \
120 \
692ae8d3 121 spl_mutex_lockdep_off_maybe(mp); \
ae26dd00
TC
122 if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) \
123 spl_mutex_set_owner(mp); \
692ae8d3 124 spl_mutex_lockdep_on_maybe(mp); \
a900e28e
BB
125 \
126 _rc_; \
9ab1ac14 127})
5461eefe 128/* END CSTYLED */
f1ca4da6 129
79a0056e
TC
130#ifdef CONFIG_DEBUG_LOCK_ALLOC
131#define mutex_enter_nested(mp, subclass) \
132{ \
133 ASSERT3P(mutex_owner(mp), !=, current); \
692ae8d3 134 spl_mutex_lockdep_off_maybe(mp); \
79a0056e 135 mutex_lock_nested(MUTEX(mp), (subclass)); \
692ae8d3 136 spl_mutex_lockdep_on_maybe(mp); \
ae26dd00 137 spl_mutex_set_owner(mp); \
79a0056e
TC
138}
139#else /* CONFIG_DEBUG_LOCK_ALLOC */
140#define mutex_enter_nested(mp, subclass) \
a900e28e
BB
141{ \
142 ASSERT3P(mutex_owner(mp), !=, current); \
692ae8d3 143 spl_mutex_lockdep_off_maybe(mp); \
a900e28e 144 mutex_lock(MUTEX(mp)); \
692ae8d3 145 spl_mutex_lockdep_on_maybe(mp); \
ae26dd00 146 spl_mutex_set_owner(mp); \
a900e28e 147}
79a0056e
TC
148#endif /* CONFIG_DEBUG_LOCK_ALLOC */
149
150#define mutex_enter(mp) mutex_enter_nested((mp), 0)
4d54fdee 151
5f920fbe
BB
152/*
153 * The reason for the spinlock:
154 *
155 * The Linux mutex is designed with a fast-path/slow-path design such that it
156 * does not guarantee serialization upon itself, allowing a race where latter
157 * acquirers finish mutex_unlock before former ones.
158 *
159 * The race renders it unsafe to be used for serializing the freeing of an
160 * object in which the mutex is embedded, where the latter acquirer could go
161 * on to free the object while the former one is still doing mutex_unlock and
162 * causing memory corruption.
163 *
164 * However, there are many places in ZFS where the mutex is used for
165 * serializing object freeing, and the code is shared among other OSes without
166 * this issue. Thus, we need the spinlock to force the serialization on
167 * mutex_exit().
168 *
169 * See http://lwn.net/Articles/575477/ for the information about the race.
170 */
a900e28e
BB
171#define mutex_exit(mp) \
172{ \
ae26dd00 173 spl_mutex_clear_owner(mp); \
261a3151
GN
174 spin_lock(&(mp)->m_lock); \
175 spl_mutex_lockdep_off_maybe(mp); \
a900e28e 176 mutex_unlock(MUTEX(mp)); \
692ae8d3 177 spl_mutex_lockdep_on_maybe(mp); \
261a3151
GN
178 spin_unlock(&(mp)->m_lock); \
179 /* NOTE: do not dereference mp after this point */ \
a900e28e 180}
4d54fdee 181
4d54fdee
BB
182int spl_mutex_init(void);
183void spl_mutex_fini(void);
184
185#endif /* _SPL_MUTEX_H */