]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/mutex.h
mutex: force serialization on mutex_exit() to fix races
[mirror_spl.git] / include / sys / mutex.h
CommitLineData
716154c5
BB
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>.
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
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23\*****************************************************************************/
715f6251 24
09b414e8 25#ifndef _SPL_MUTEX_H
4d54fdee 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>
4d54fdee
BB
31
32typedef enum {
33 MUTEX_DEFAULT = 0,
34 MUTEX_SPIN = 1,
35 MUTEX_ADAPTIVE = 2
36} kmutex_type_t;
f1b59d26 37
a80d69ca
BB
38#if defined(HAVE_MUTEX_OWNER) && defined(CONFIG_SMP) && \
39 !defined(CONFIG_DEBUG_MUTEXES)
9ab1ac14 40
c2f997b0
RC
41/*
42 * We define a 1-field struct rather than a straight typedef to enforce type
43 * safety.
44 */
45typedef struct {
46 struct mutex m;
a3c1eb77 47 spinlock_t m_lock; /* used for serializing mutex_exit */
c2f997b0 48} kmutex_t;
f1ca4da6 49
4d54fdee
BB
50static inline kthread_t *
51mutex_owner(kmutex_t *mp)
52{
86fd39f3
BB
53#if defined(HAVE_MUTEX_OWNER_TASK_STRUCT)
54 return ACCESS_ONCE(mp->m.owner);
55#else
56 struct thread_info *owner = ACCESS_ONCE(mp->m.owner);
57 if (owner)
58 return owner->task;
ede0bdff 59
86fd39f3
BB
60 return NULL;
61#endif
ede0bdff
BB
62}
63
86fd39f3 64#define mutex_owned(mp) (mutex_owner(mp) == current)
d85e28ad
BB
65#define MUTEX_HELD(mp) mutex_owned(mp)
66#define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
4d54fdee
BB
67#undef mutex_init
68#define mutex_init(mp, name, type, ibc) \
05b48408 69({ \
4d54fdee
BB
70 static struct lock_class_key __key; \
71 ASSERT(type == MUTEX_DEFAULT); \
72 \
c2f997b0 73 __mutex_init(&(mp)->m, #mp, &__key); \
a3c1eb77 74 spin_lock_init(&(mp)->m_lock); \
4d54fdee 75})
05b48408 76
1273cf28
BB
77#undef mutex_destroy
78#define mutex_destroy(mp) \
79({ \
c2f997b0 80 VERIFY3P(mutex_owner(mp), ==, NULL); \
1273cf28
BB
81})
82
c2f997b0 83#define mutex_tryenter(mp) mutex_trylock(&(mp)->m)
3e904f40
BB
84#define mutex_enter(mp) \
85({ \
86 ASSERT3P(mutex_owner(mp), !=, current); \
87 mutex_lock(&(mp)->m); \
a80d69ca 88})
a3c1eb77
CC
89/*
90 * The reason for the spinlock:
91 *
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.
95 *
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.
100 *
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
104 * mutex_exit().
105 *
106 * See http://lwn.net/Articles/575477/ for the information about the race.
107 */
108#define mutex_exit(mp) \
109({ \
110 spin_lock(&(mp)->m_lock); \
111 mutex_unlock(&(mp)->m); \
112 spin_unlock(&(mp)->m_lock); \
113})
1273cf28 114
4d54fdee 115#else /* HAVE_MUTEX_OWNER */
f1b59d26 116
f1ca4da6 117typedef struct {
4d54fdee 118 struct mutex m_mutex;
a3c1eb77 119 spinlock_t m_lock;
4d54fdee 120 kthread_t *m_owner;
f1ca4da6 121} kmutex_t;
122
f5e76dea 123#define MUTEX(mp) (&((mp)->m_mutex))
d61e12af 124
4d54fdee
BB
125static inline void
126spl_mutex_set_owner(kmutex_t *mp)
127{
4d54fdee 128 mp->m_owner = current;
4d54fdee
BB
129}
130
131static inline void
132spl_mutex_clear_owner(kmutex_t *mp)
133{
4d54fdee 134 mp->m_owner = NULL;
4d54fdee
BB
135}
136
66cdc93b 137#define mutex_owner(mp) (ACCESS_ONCE((mp)->m_owner))
4d54fdee
BB
138#define mutex_owned(mp) (mutex_owner(mp) == current)
139#define MUTEX_HELD(mp) mutex_owned(mp)
d85e28ad 140#define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
f1ca4da6 141
4d54fdee
BB
142/*
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
147 */
9ab1ac14 148#undef mutex_init
4d54fdee
BB
149#define mutex_init(mp, name, type, ibc) \
150({ \
151 static struct lock_class_key __key; \
152 ASSERT(type == MUTEX_DEFAULT); \
153 \
154 __mutex_init(MUTEX(mp), #mp, &__key); \
a3c1eb77 155 spin_lock_init(&(mp)->m_lock); \
4d54fdee
BB
156 spl_mutex_clear_owner(mp); \
157})
158
9ab1ac14 159#undef mutex_destroy
4d54fdee
BB
160#define mutex_destroy(mp) \
161({ \
c2f997b0 162 VERIFY3P(mutex_owner(mp), ==, NULL); \
4d54fdee 163})
d61e12af 164
4d54fdee
BB
165#define mutex_tryenter(mp) \
166({ \
167 int _rc_; \
168 \
169 if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) \
170 spl_mutex_set_owner(mp); \
171 \
172 _rc_; \
9ab1ac14 173})
f1ca4da6 174
4d54fdee
BB
175#define mutex_enter(mp) \
176({ \
a80d69ca
BB
177 ASSERT3P(mutex_owner(mp), !=, current); \
178 mutex_lock(MUTEX(mp)); \
179 spl_mutex_set_owner(mp); \
4d54fdee
BB
180})
181
182#define mutex_exit(mp) \
183({ \
a3c1eb77 184 spin_lock(&(mp)->m_lock); \
4d54fdee
BB
185 spl_mutex_clear_owner(mp); \
186 mutex_unlock(MUTEX(mp)); \
a3c1eb77 187 spin_unlock(&(mp)->m_lock); \
4d54fdee
BB
188})
189
190#endif /* HAVE_MUTEX_OWNER */
191
192int spl_mutex_init(void);
193void spl_mutex_fini(void);
194
195#endif /* _SPL_MUTEX_H */