]>
git.proxmox.com Git - mirror_spl.git/blob - 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>.
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/>.
23 \*****************************************************************************/
28 #include <sys/types.h>
29 #include <linux/mutex.h>
30 #include <linux/compiler_compat.h>
38 #if defined(HAVE_MUTEX_OWNER) && defined(CONFIG_SMP) && \
39 !defined(CONFIG_DEBUG_MUTEXES)
42 * We define a 1-field struct rather than a straight typedef to enforce type
47 spinlock_t m_lock
; /* used for serializing mutex_exit */
50 static inline kthread_t
*
51 mutex_owner(kmutex_t
*mp
)
53 #if defined(HAVE_MUTEX_OWNER_TASK_STRUCT)
54 return ACCESS_ONCE(mp
->m
.owner
);
56 struct thread_info
*owner
= ACCESS_ONCE(mp
->m
.owner
);
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))
68 #define mutex_init(mp, name, type, ibc) \
70 static struct lock_class_key __key; \
71 ASSERT(type == MUTEX_DEFAULT); \
73 __mutex_init(&(mp)->m, #mp, &__key); \
74 spin_lock_init(&(mp)->m_lock); \
78 #define mutex_destroy(mp) \
80 VERIFY3P(mutex_owner(mp), ==, NULL); \
83 #define mutex_tryenter(mp) mutex_trylock(&(mp)->m)
84 #define mutex_enter(mp) \
86 ASSERT3P(mutex_owner(mp), !=, current); \
87 mutex_lock(&(mp)->m); \
90 * The reason for the spinlock:
92 * The Linux mutex is designed with a fast-path/slow-path design such that it
93 * does not guarantee serialization upon itself, allowing a race where latter
94 * acquirers finish mutex_unlock before former ones.
96 * The race renders it unsafe to be used for serializing the freeing of an
97 * object in which the mutex is embedded, where the latter acquirer could go
98 * on to free the object while the former one is still doing mutex_unlock and
99 * causing memory corruption.
101 * However, there are many places in ZFS where the mutex is used for
102 * serializing object freeing, and the code is shared among other OSes without
103 * this issue. Thus, we need the spinlock to force the serialization on
106 * See http://lwn.net/Articles/575477/ for the information about the race.
108 #define mutex_exit(mp) \
110 spin_lock(&(mp)->m_lock); \
111 mutex_unlock(&(mp)->m); \
112 spin_unlock(&(mp)->m_lock); \
115 #else /* HAVE_MUTEX_OWNER */
118 struct mutex m_mutex
;
123 #define MUTEX(mp) (&((mp)->m_mutex))
126 spl_mutex_set_owner(kmutex_t
*mp
)
128 mp
->m_owner
= current
;
132 spl_mutex_clear_owner(kmutex_t
*mp
)
137 #define mutex_owner(mp) (ACCESS_ONCE((mp)->m_owner))
138 #define mutex_owned(mp) (mutex_owner(mp) == current)
139 #define MUTEX_HELD(mp) mutex_owned(mp)
140 #define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
143 * The following functions must be a #define and not static inline.
144 * This ensures that the native linux mutex functions (lock/unlock)
145 * will be correctly located in the users code which is important
146 * for the built in kernel lock analysis tools
149 #define mutex_init(mp, name, type, ibc) \
151 static struct lock_class_key __key; \
152 ASSERT(type == MUTEX_DEFAULT); \
154 __mutex_init(MUTEX(mp), #mp, &__key); \
155 spin_lock_init(&(mp)->m_lock); \
156 spl_mutex_clear_owner(mp); \
160 #define mutex_destroy(mp) \
162 VERIFY3P(mutex_owner(mp), ==, NULL); \
165 #define mutex_tryenter(mp) \
169 if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) \
170 spl_mutex_set_owner(mp); \
175 #define mutex_enter(mp) \
177 ASSERT3P(mutex_owner(mp), !=, current); \
178 mutex_lock(MUTEX(mp)); \
179 spl_mutex_set_owner(mp); \
182 #define mutex_exit(mp) \
184 spin_lock(&(mp)->m_lock); \
185 spl_mutex_clear_owner(mp); \
186 mutex_unlock(MUTEX(mp)); \
187 spin_unlock(&(mp)->m_lock); \
190 #endif /* HAVE_MUTEX_OWNER */
192 int spl_mutex_init(void);
193 void spl_mutex_fini(void);
195 #endif /* _SPL_MUTEX_H */