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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
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
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/>.
28 #include <sys/types.h>
29 #include <linux/sched.h>
30 #include <linux/mutex.h>
31 #include <linux/lockdep.h>
32 #include <linux/compiler_compat.h>
43 spinlock_t m_lock; /* used for serializing mutex_exit */
47 #endif /* CONFIG_LOCKDEP */
50 #define MUTEX(mp) (&((mp)->m_mutex))
53 spl_mutex_set_owner(kmutex_t *mp)
55 mp->m_owner = current;
59 spl_mutex_clear_owner(kmutex_t *mp)
64 #define mutex_owner(mp) (READ_ONCE((mp)->m_owner))
65 #define mutex_owned(mp) (mutex_owner(mp) == current)
66 #define MUTEX_HELD(mp) mutex_owned(mp)
67 #define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
71 spl_mutex_set_type(kmutex_t *mp, kmutex_type_t type)
76 spl_mutex_lockdep_off_maybe(kmutex_t *mp) \
78 if (mp && mp->m_type == MUTEX_NOLOCKDEP) \
82 spl_mutex_lockdep_on_maybe(kmutex_t *mp) \
84 if (mp && mp->m_type == MUTEX_NOLOCKDEP) \
87 #else /* CONFIG_LOCKDEP */
88 #define spl_mutex_set_type(mp, type)
89 #define spl_mutex_lockdep_off_maybe(mp)
90 #define spl_mutex_lockdep_on_maybe(mp)
91 #endif /* CONFIG_LOCKDEP */
94 * The following functions must be a #define and not static inline.
95 * This ensures that the native linux mutex functions (lock/unlock)
96 * will be correctly located in the users code which is important
97 * for the built in kernel lock analysis tools
100 #define mutex_init(mp, name, type, ibc) \
102 static struct lock_class_key __key; \
103 ASSERT(type == MUTEX_DEFAULT || type == MUTEX_NOLOCKDEP); \
105 __mutex_init(MUTEX(mp), (name) ? (#name) : (#mp), &__key); \
106 spin_lock_init(&(mp)->m_lock); \
107 spl_mutex_clear_owner(mp); \
108 spl_mutex_set_type(mp, type); \
112 #define mutex_destroy(mp) \
114 VERIFY3P(mutex_owner(mp), ==, NULL); \
118 #define mutex_tryenter(mp) \
122 spl_mutex_lockdep_off_maybe(mp); \
123 if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) \
124 spl_mutex_set_owner(mp); \
125 spl_mutex_lockdep_on_maybe(mp); \
131 #define NESTED_SINGLE 1
133 #ifdef CONFIG_DEBUG_LOCK_ALLOC
134 #define mutex_enter_nested(mp, subclass) \
136 ASSERT3P(mutex_owner(mp), !=, current); \
137 spl_mutex_lockdep_off_maybe(mp); \
138 mutex_lock_nested(MUTEX(mp), (subclass)); \
139 spl_mutex_lockdep_on_maybe(mp); \
140 spl_mutex_set_owner(mp); \
142 #else /* CONFIG_DEBUG_LOCK_ALLOC */
143 #define mutex_enter_nested(mp, subclass) \
145 ASSERT3P(mutex_owner(mp), !=, current); \
146 spl_mutex_lockdep_off_maybe(mp); \
147 mutex_lock(MUTEX(mp)); \
148 spl_mutex_lockdep_on_maybe(mp); \
149 spl_mutex_set_owner(mp); \
151 #endif /* CONFIG_DEBUG_LOCK_ALLOC */
153 #define mutex_enter(mp) mutex_enter_nested((mp), 0)
156 * The reason for the spinlock:
158 * The Linux mutex is designed with a fast-path/slow-path design such that it
159 * does not guarantee serialization upon itself, allowing a race where latter
160 * acquirers finish mutex_unlock before former ones.
162 * The race renders it unsafe to be used for serializing the freeing of an
163 * object in which the mutex is embedded, where the latter acquirer could go
164 * on to free the object while the former one is still doing mutex_unlock and
165 * causing memory corruption.
167 * However, there are many places in ZFS where the mutex is used for
168 * serializing object freeing, and the code is shared among other OSes without
169 * this issue. Thus, we need the spinlock to force the serialization on
172 * See http://lwn.net/Articles/575477/ for the information about the race.
174 #define mutex_exit(mp) \
176 spl_mutex_clear_owner(mp); \
177 spin_lock(&(mp)->m_lock); \
178 spl_mutex_lockdep_off_maybe(mp); \
179 mutex_unlock(MUTEX(mp)); \
180 spl_mutex_lockdep_on_maybe(mp); \
181 spin_unlock(&(mp)->m_lock); \
182 /* NOTE: do not dereference mp after this point */ \
185 #endif /* _SPL_MUTEX_H */