]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/rwlock.h
Linux 4.8 compat: Fix RW_READ_HELD
[mirror_spl.git] / include / sys / rwlock.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_RWLOCK_H
e8b31e84 26#define _SPL_RWLOCK_H
f1ca4da6 27
f4b37741 28#include <sys/types.h>
d28db80f 29#include <linux/rwsem.h>
46aa7b39 30#include <linux/rwsem_compat.h>
f1ca4da6 31
32typedef enum {
374303a3 33 RW_DRIVER = 2,
692ae8d3
OF
34 RW_DEFAULT = 4,
35 RW_NOLOCKDEP = 5
f1ca4da6 36} krw_type_t;
37
38typedef enum {
374303a3
CC
39 RW_NONE = 0,
40 RW_WRITER = 1,
41 RW_READER = 2
f1ca4da6 42} krw_t;
43
a00b3eb5
CC
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 */
d28db80f 48typedef struct {
374303a3 49 struct rw_semaphore rw_rwlock;
a00b3eb5 50#ifndef CONFIG_RWSEM_SPIN_ON_OWNER
374303a3 51 kthread_t *rw_owner;
a00b3eb5 52#endif
692ae8d3
OF
53#ifdef CONFIG_LOCKDEP
54 krw_type_t rw_type;
55#endif /* CONFIG_LOCKDEP */
d28db80f 56} krwlock_t;
f1ca4da6 57
a00b3eb5 58#define SEM(rwp) (&(rwp)->rw_rwlock)
d28db80f 59
d28db80f
BB
60static inline void
61spl_rw_set_owner(krwlock_t *rwp)
62{
a00b3eb5
CC
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
4f8e643a 68 rwp->rw_owner = current;
a00b3eb5 69#endif
d28db80f
BB
70}
71
72static inline void
73spl_rw_clear_owner(krwlock_t *rwp)
74{
a00b3eb5 75#ifndef CONFIG_RWSEM_SPIN_ON_OWNER
4f8e643a 76 rwp->rw_owner = NULL;
a00b3eb5 77#endif
d28db80f
BB
78}
79
80static inline kthread_t *
81rw_owner(krwlock_t *rwp)
82{
a00b3eb5
CC
83#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
84 return SEM(rwp)->owner;
85#else
4f8e643a 86 return rwp->rw_owner;
a00b3eb5 87#endif
d28db80f
BB
88}
89
692ae8d3
OF
90#ifdef CONFIG_LOCKDEP
91static inline void
92spl_rw_set_type(krwlock_t *rwp, krw_type_t type)
93{
94 rwp->rw_type = type;
95}
96static inline void
97spl_rw_lockdep_off_maybe(krwlock_t *rwp) \
98{ \
99 if (rwp && rwp->rw_type == RW_NOLOCKDEP) \
100 lockdep_off(); \
101}
102static inline void
103spl_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
d28db80f
BB
114static inline int
115RW_READ_HELD(krwlock_t *rwp)
116{
2529b3a8 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);
d28db80f
BB
123}
124
125static inline int
126RW_WRITE_HELD(krwlock_t *rwp)
127{
4f8e643a 128 return (rw_owner(rwp) == current);
d28db80f
BB
129}
130
131static inline int
132RW_LOCK_HELD(krwlock_t *rwp)
133{
46aa7b39 134 return spl_rwsem_is_locked(SEM(rwp));
d28db80f
BB
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 */
374303a3
CC
143#define rw_init(rwp, name, type, arg) \
144({ \
145 static struct lock_class_key __key; \
692ae8d3 146 ASSERT(type == RW_DEFAULT || type == RW_NOLOCKDEP); \
374303a3
CC
147 \
148 __init_rwsem(SEM(rwp), #rwp, &__key); \
149 spl_rw_clear_owner(rwp); \
692ae8d3 150 spl_rw_set_type(rwp, type); \
e811949a 151})
f1ca4da6 152
374303a3
CC
153#define rw_destroy(rwp) \
154({ \
155 VERIFY(!RW_LOCK_HELD(rwp)); \
e811949a 156})
ed61a7d0 157
374303a3
CC
158#define rw_tryenter(rwp, rw) \
159({ \
160 int _rc_ = 0; \
161 \
692ae8d3 162 spl_rw_lockdep_off_maybe(rwp); \
374303a3
CC
163 switch (rw) { \
164 case RW_READER: \
165 _rc_ = down_read_trylock(SEM(rwp)); \
166 break; \
167 case RW_WRITER: \
168 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
169 spl_rw_set_owner(rwp); \
170 break; \
171 default: \
172 VERIFY(0); \
173 } \
692ae8d3 174 spl_rw_lockdep_on_maybe(rwp); \
374303a3 175 _rc_; \
e8b31e84 176})
e8b31e84 177
374303a3
CC
178#define rw_enter(rwp, rw) \
179({ \
692ae8d3 180 spl_rw_lockdep_off_maybe(rwp); \
374303a3
CC
181 switch (rw) { \
182 case RW_READER: \
183 down_read(SEM(rwp)); \
184 break; \
185 case RW_WRITER: \
186 down_write(SEM(rwp)); \
187 spl_rw_set_owner(rwp); \
188 break; \
189 default: \
190 VERIFY(0); \
191 } \
692ae8d3 192 spl_rw_lockdep_on_maybe(rwp); \
e811949a 193})
f1ca4da6 194
374303a3
CC
195#define rw_exit(rwp) \
196({ \
692ae8d3 197 spl_rw_lockdep_off_maybe(rwp); \
374303a3
CC
198 if (RW_WRITE_HELD(rwp)) { \
199 spl_rw_clear_owner(rwp); \
200 up_write(SEM(rwp)); \
201 } else { \
202 ASSERT(RW_READ_HELD(rwp)); \
203 up_read(SEM(rwp)); \
204 } \
692ae8d3 205 spl_rw_lockdep_on_maybe(rwp); \
d28db80f
BB
206})
207
374303a3
CC
208#define rw_downgrade(rwp) \
209({ \
692ae8d3 210 spl_rw_lockdep_off_maybe(rwp); \
374303a3
CC
211 spl_rw_clear_owner(rwp); \
212 downgrade_write(SEM(rwp)); \
692ae8d3 213 spl_rw_lockdep_on_maybe(rwp); \
d28db80f
BB
214})
215
32f5faff 216/*
a6ae97ca
BB
217 * This implementation of rw_tryupgrade() behaves slightly differently
218 * from its counterparts on other platforms. It drops the RW_READER lock
219 * and then acquires the RW_WRITER lock leaving a small window where no
220 * lock is held. On other platforms the lock is never released during
221 * the upgrade process. This is necessary under Linux because the kernel
222 * does not provide an upgrade function.
32f5faff 223 */
374303a3
CC
224#define rw_tryupgrade(rwp) \
225({ \
374303a3
CC
226 int _rc_ = 0; \
227 \
a6ae97ca
BB
228 if (RW_WRITE_HELD(rwp)) { \
229 _rc_ = 1; \
230 } else { \
f58040c0
CC
231 spl_rw_lockdep_off_maybe(rwp); \
232 if ((_rc_ = rwsem_tryupgrade(SEM(rwp)))) \
233 spl_rw_set_owner(rwp); \
234 spl_rw_lockdep_on_maybe(rwp); \
374303a3 235 } \
374303a3 236 _rc_; \
d28db80f
BB
237})
238
239int spl_rw_init(void);
240void spl_rw_fini(void);
241
e8b31e84 242#endif /* _SPL_RWLOCK_H */