]>
git.proxmox.com Git - mirror_spl-debian.git/blob - include/sys/mutex.h
be69deaab5d585915a001889b35928fbb560f00e
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/mutex.h>
30 #include <linux/compiler_compat.h>
40 spinlock_t m_lock
; /* used for serializing mutex_exit */
41 #ifndef HAVE_MUTEX_OWNER
42 /* only when kernel doesn't have owner */
47 #define MUTEX(mp) (&((mp)->m_mutex))
50 spl_mutex_set_owner(kmutex_t
*mp
)
53 * kernel will handle its owner, so we don't need to do anything if it
56 #ifndef HAVE_MUTEX_OWNER
57 mp
->m_owner
= current
;
62 spl_mutex_clear_owner(kmutex_t
*mp
)
64 #ifndef HAVE_MUTEX_OWNER
69 #ifdef HAVE_MUTEX_OWNER
70 #define mutex_owner(mp) (ACCESS_ONCE(MUTEX(mp)->owner))
72 #define mutex_owner(mp) (ACCESS_ONCE((mp)->m_owner))
74 #define mutex_owned(mp) (mutex_owner(mp) == current)
75 #define MUTEX_HELD(mp) mutex_owned(mp)
76 #define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
79 * The following functions must be a #define and not static inline.
80 * This ensures that the native linux mutex functions (lock/unlock)
81 * will be correctly located in the users code which is important
82 * for the built in kernel lock analysis tools
85 #define mutex_init(mp, name, type, ibc) \
87 static struct lock_class_key __key; \
88 ASSERT(type == MUTEX_DEFAULT); \
90 __mutex_init(MUTEX(mp), (name) ? (#name) : (#mp), &__key); \
91 spin_lock_init(&(mp)->m_lock); \
92 spl_mutex_clear_owner(mp); \
96 #define mutex_destroy(mp) \
98 VERIFY3P(mutex_owner(mp), ==, NULL); \
101 #define mutex_tryenter(mp) \
105 if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) \
106 spl_mutex_set_owner(mp); \
111 #ifdef CONFIG_DEBUG_LOCK_ALLOC
112 #define mutex_enter_nested(mp, subclass) \
114 ASSERT3P(mutex_owner(mp), !=, current); \
115 mutex_lock_nested(MUTEX(mp), (subclass)); \
116 spl_mutex_set_owner(mp); \
118 #else /* CONFIG_DEBUG_LOCK_ALLOC */
119 #define mutex_enter_nested(mp, subclass) \
121 ASSERT3P(mutex_owner(mp), !=, current); \
122 mutex_lock(MUTEX(mp)); \
123 spl_mutex_set_owner(mp); \
125 #endif /* CONFIG_DEBUG_LOCK_ALLOC */
127 #define mutex_enter(mp) mutex_enter_nested((mp), 0)
130 * The reason for the spinlock:
132 * The Linux mutex is designed with a fast-path/slow-path design such that it
133 * does not guarantee serialization upon itself, allowing a race where latter
134 * acquirers finish mutex_unlock before former ones.
136 * The race renders it unsafe to be used for serializing the freeing of an
137 * object in which the mutex is embedded, where the latter acquirer could go
138 * on to free the object while the former one is still doing mutex_unlock and
139 * causing memory corruption.
141 * However, there are many places in ZFS where the mutex is used for
142 * serializing object freeing, and the code is shared among other OSes without
143 * this issue. Thus, we need the spinlock to force the serialization on
146 * See http://lwn.net/Articles/575477/ for the information about the race.
148 #define mutex_exit(mp) \
150 spin_lock(&(mp)->m_lock); \
151 spl_mutex_clear_owner(mp); \
152 mutex_unlock(MUTEX(mp)); \
153 spin_unlock(&(mp)->m_lock); \
156 int spl_mutex_init(void);
157 void spl_mutex_fini(void);
159 #endif /* _SPL_MUTEX_H */