]>
Commit | Line | Data |
---|---|---|
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> |
4d54fdee BB |
31 | |
32 | typedef enum { | |
a900e28e BB |
33 | MUTEX_DEFAULT = 0, |
34 | MUTEX_SPIN = 1, | |
d0d5dd71 BB |
35 | MUTEX_ADAPTIVE = 2, |
36 | MUTEX_FSTRANS = 3, | |
4d54fdee | 37 | } kmutex_type_t; |
f1b59d26 | 38 | |
f1ca4da6 | 39 | typedef struct { |
a900e28e | 40 | struct mutex m_mutex; |
d0d5dd71 | 41 | kmutex_type_t m_type; |
a900e28e BB |
42 | spinlock_t m_lock; /* used for serializing mutex_exit */ |
43 | kthread_t *m_owner; | |
d0d5dd71 | 44 | unsigned int m_saved_flags; |
f1ca4da6 | 45 | } kmutex_t; |
46 | ||
a900e28e | 47 | #define MUTEX(mp) (&((mp)->m_mutex)) |
a900e28e BB |
48 | #define mutex_owner(mp) (ACCESS_ONCE((mp)->m_owner)) |
49 | #define mutex_owned(mp) (mutex_owner(mp) == current) | |
50 | #define MUTEX_HELD(mp) mutex_owned(mp) | |
51 | #define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp)) | |
f1ca4da6 | 52 | |
4d54fdee BB |
53 | /* |
54 | * The following functions must be a #define and not static inline. | |
55 | * This ensures that the native linux mutex functions (lock/unlock) | |
56 | * will be correctly located in the users code which is important | |
57 | * for the built in kernel lock analysis tools | |
58 | */ | |
9ab1ac14 | 59 | #undef mutex_init |
a900e28e BB |
60 | #define mutex_init(mp, name, type, ibc) \ |
61 | { \ | |
62 | static struct lock_class_key __key; \ | |
d0d5dd71 BB |
63 | \ |
64 | ASSERT3P(mp, !=, NULL); \ | |
65 | ASSERT3P(ibc, ==, NULL); \ | |
66 | ASSERT((type == MUTEX_DEFAULT) || \ | |
67 | (type == MUTEX_ADAPTIVE) || \ | |
68 | (type == MUTEX_FSTRANS)); \ | |
a900e28e BB |
69 | \ |
70 | __mutex_init(MUTEX(mp), #mp, &__key); \ | |
71 | spin_lock_init(&(mp)->m_lock); \ | |
d0d5dd71 BB |
72 | (mp)->m_type = type; \ |
73 | (mp)->m_owner = NULL; \ | |
74 | (mp)->m_saved_flags = 0; \ | |
a900e28e | 75 | } |
4d54fdee | 76 | |
9ab1ac14 | 77 | #undef mutex_destroy |
a900e28e BB |
78 | #define mutex_destroy(mp) \ |
79 | { \ | |
80 | VERIFY3P(mutex_owner(mp), ==, NULL); \ | |
81 | } | |
d61e12af | 82 | |
a900e28e BB |
83 | #define mutex_tryenter(mp) \ |
84 | ({ \ | |
85 | int _rc_; \ | |
86 | \ | |
d0d5dd71 BB |
87 | if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) { \ |
88 | (mp)->m_owner = current; \ | |
89 | if ((mp)->m_type == MUTEX_FSTRANS) { \ | |
90 | (mp)->m_saved_flags = current->flags; \ | |
91 | current->flags |= PF_FSTRANS; \ | |
92 | } \ | |
93 | } \ | |
a900e28e BB |
94 | \ |
95 | _rc_; \ | |
9ab1ac14 | 96 | }) |
f1ca4da6 | 97 | |
a900e28e BB |
98 | #define mutex_enter(mp) \ |
99 | { \ | |
100 | ASSERT3P(mutex_owner(mp), !=, current); \ | |
101 | mutex_lock(MUTEX(mp)); \ | |
d0d5dd71 BB |
102 | (mp)->m_owner = current; \ |
103 | if ((mp)->m_type == MUTEX_FSTRANS) { \ | |
104 | (mp)->m_saved_flags = current->flags; \ | |
105 | current->flags |= PF_FSTRANS; \ | |
106 | } \ | |
a900e28e | 107 | } |
4d54fdee | 108 | |
5f920fbe BB |
109 | /* |
110 | * The reason for the spinlock: | |
111 | * | |
112 | * The Linux mutex is designed with a fast-path/slow-path design such that it | |
113 | * does not guarantee serialization upon itself, allowing a race where latter | |
114 | * acquirers finish mutex_unlock before former ones. | |
115 | * | |
116 | * The race renders it unsafe to be used for serializing the freeing of an | |
117 | * object in which the mutex is embedded, where the latter acquirer could go | |
118 | * on to free the object while the former one is still doing mutex_unlock and | |
119 | * causing memory corruption. | |
120 | * | |
121 | * However, there are many places in ZFS where the mutex is used for | |
122 | * serializing object freeing, and the code is shared among other OSes without | |
123 | * this issue. Thus, we need the spinlock to force the serialization on | |
124 | * mutex_exit(). | |
125 | * | |
126 | * See http://lwn.net/Articles/575477/ for the information about the race. | |
127 | */ | |
a900e28e BB |
128 | #define mutex_exit(mp) \ |
129 | { \ | |
130 | spin_lock(&(mp)->m_lock); \ | |
d0d5dd71 BB |
131 | if ((mp)->m_type == MUTEX_FSTRANS) { \ |
132 | current->flags &= ~(PF_FSTRANS); \ | |
133 | current->flags |= (mp)->m_saved_flags; \ | |
134 | } \ | |
135 | (mp)->m_owner = NULL; \ | |
a900e28e BB |
136 | mutex_unlock(MUTEX(mp)); \ |
137 | spin_unlock(&(mp)->m_lock); \ | |
138 | } | |
4d54fdee | 139 | |
4d54fdee BB |
140 | int spl_mutex_init(void); |
141 | void spl_mutex_fini(void); | |
142 | ||
143 | #endif /* _SPL_MUTEX_H */ |