]> git.proxmox.com Git - mirror_spl-debian.git/blob - include/sys/rwlock.h
New upstream version 0.7.11
[mirror_spl-debian.git] / include / sys / rwlock.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>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
15 *
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
19 * for more details.
20 *
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 \*****************************************************************************/
24
25 #ifndef _SPL_RWLOCK_H
26 #define _SPL_RWLOCK_H
27
28 #include <sys/types.h>
29 #include <linux/rwsem.h>
30 #include <linux/rwsem_compat.h>
31
32 typedef enum {
33 RW_DRIVER = 2,
34 RW_DEFAULT = 4,
35 RW_NOLOCKDEP = 5
36 } krw_type_t;
37
38 typedef enum {
39 RW_NONE = 0,
40 RW_WRITER = 1,
41 RW_READER = 2
42 } krw_t;
43
44 /*
45 * If CONFIG_RWSEM_SPIN_ON_OWNER is defined, rw_semaphore will have an owner
46 * field, so we don't need our own.
47 */
48 typedef struct {
49 struct rw_semaphore rw_rwlock;
50 #ifndef CONFIG_RWSEM_SPIN_ON_OWNER
51 kthread_t *rw_owner;
52 #endif
53 #ifdef CONFIG_LOCKDEP
54 krw_type_t rw_type;
55 #endif /* CONFIG_LOCKDEP */
56 } krwlock_t;
57
58 #define SEM(rwp) (&(rwp)->rw_rwlock)
59
60 static inline void
61 spl_rw_set_owner(krwlock_t *rwp)
62 {
63 /*
64 * If CONFIG_RWSEM_SPIN_ON_OWNER is defined, down_write, up_write,
65 * downgrade_write and __init_rwsem will set/clear owner for us.
66 */
67 #ifndef CONFIG_RWSEM_SPIN_ON_OWNER
68 rwp->rw_owner = current;
69 #endif
70 }
71
72 static inline void
73 spl_rw_clear_owner(krwlock_t *rwp)
74 {
75 #ifndef CONFIG_RWSEM_SPIN_ON_OWNER
76 rwp->rw_owner = NULL;
77 #endif
78 }
79
80 static inline kthread_t *
81 rw_owner(krwlock_t *rwp)
82 {
83 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
84 return (SEM(rwp)->owner);
85 #else
86 return (rwp->rw_owner);
87 #endif
88 }
89
90 #ifdef CONFIG_LOCKDEP
91 static inline void
92 spl_rw_set_type(krwlock_t *rwp, krw_type_t type)
93 {
94 rwp->rw_type = type;
95 }
96 static inline void
97 spl_rw_lockdep_off_maybe(krwlock_t *rwp) \
98 { \
99 if (rwp && rwp->rw_type == RW_NOLOCKDEP) \
100 lockdep_off(); \
101 }
102 static inline void
103 spl_rw_lockdep_on_maybe(krwlock_t *rwp) \
104 { \
105 if (rwp && rwp->rw_type == RW_NOLOCKDEP) \
106 lockdep_on(); \
107 }
108 #else /* CONFIG_LOCKDEP */
109 #define spl_rw_set_type(rwp, type)
110 #define spl_rw_lockdep_off_maybe(rwp)
111 #define spl_rw_lockdep_on_maybe(rwp)
112 #endif /* CONFIG_LOCKDEP */
113
114 static inline int
115 RW_READ_HELD(krwlock_t *rwp)
116 {
117 /*
118 * Linux 4.8 will set owner to 1 when read held instead of leave it
119 * NULL. So we check whether owner <= 1.
120 */
121 return (spl_rwsem_is_locked(SEM(rwp)) &&
122 (unsigned long)rw_owner(rwp) <= 1);
123 }
124
125 static inline int
126 RW_WRITE_HELD(krwlock_t *rwp)
127 {
128 return (rw_owner(rwp) == current);
129 }
130
131 static inline int
132 RW_LOCK_HELD(krwlock_t *rwp)
133 {
134 return (spl_rwsem_is_locked(SEM(rwp)));
135 }
136
137 /*
138 * The following functions must be a #define and not static inline.
139 * This ensures that the native linux semaphore functions (down/up)
140 * will be correctly located in the users code which is important
141 * for the built in kernel lock analysis tools
142 */
143 /* BEGIN CSTYLED */
144 #define rw_init(rwp, name, type, arg) \
145 ({ \
146 static struct lock_class_key __key; \
147 ASSERT(type == RW_DEFAULT || type == RW_NOLOCKDEP); \
148 \
149 __init_rwsem(SEM(rwp), #rwp, &__key); \
150 spl_rw_clear_owner(rwp); \
151 spl_rw_set_type(rwp, type); \
152 })
153
154 /*
155 * The Linux rwsem implementation does not require a matching destroy.
156 */
157 #define rw_destroy(rwp) ((void) 0)
158
159 #define rw_tryenter(rwp, rw) \
160 ({ \
161 int _rc_ = 0; \
162 \
163 spl_rw_lockdep_off_maybe(rwp); \
164 switch (rw) { \
165 case RW_READER: \
166 _rc_ = down_read_trylock(SEM(rwp)); \
167 break; \
168 case RW_WRITER: \
169 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
170 spl_rw_set_owner(rwp); \
171 break; \
172 default: \
173 VERIFY(0); \
174 } \
175 spl_rw_lockdep_on_maybe(rwp); \
176 _rc_; \
177 })
178
179 #define rw_enter(rwp, rw) \
180 ({ \
181 spl_rw_lockdep_off_maybe(rwp); \
182 switch (rw) { \
183 case RW_READER: \
184 down_read(SEM(rwp)); \
185 break; \
186 case RW_WRITER: \
187 down_write(SEM(rwp)); \
188 spl_rw_set_owner(rwp); \
189 break; \
190 default: \
191 VERIFY(0); \
192 } \
193 spl_rw_lockdep_on_maybe(rwp); \
194 })
195
196 #define rw_exit(rwp) \
197 ({ \
198 spl_rw_lockdep_off_maybe(rwp); \
199 if (RW_WRITE_HELD(rwp)) { \
200 spl_rw_clear_owner(rwp); \
201 up_write(SEM(rwp)); \
202 } else { \
203 ASSERT(RW_READ_HELD(rwp)); \
204 up_read(SEM(rwp)); \
205 } \
206 spl_rw_lockdep_on_maybe(rwp); \
207 })
208
209 #define rw_downgrade(rwp) \
210 ({ \
211 spl_rw_lockdep_off_maybe(rwp); \
212 spl_rw_clear_owner(rwp); \
213 downgrade_write(SEM(rwp)); \
214 spl_rw_lockdep_on_maybe(rwp); \
215 })
216
217 #define rw_tryupgrade(rwp) \
218 ({ \
219 int _rc_ = 0; \
220 \
221 if (RW_WRITE_HELD(rwp)) { \
222 _rc_ = 1; \
223 } else { \
224 spl_rw_lockdep_off_maybe(rwp); \
225 if ((_rc_ = rwsem_tryupgrade(SEM(rwp)))) \
226 spl_rw_set_owner(rwp); \
227 spl_rw_lockdep_on_maybe(rwp); \
228 } \
229 _rc_; \
230 })
231 /* END CSTYLED */
232
233 int spl_rw_init(void);
234 void spl_rw_fini(void);
235
236 #endif /* _SPL_RWLOCK_H */