xref: /freebsd/sys/contrib/openzfs/include/os/linux/spl/sys/rwlock.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
4  *  Copyright (C) 2007 The Regents of the University of California.
5  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
7  *  UCRL-CODE-235197
8  *
9  *  This file is part of the SPL, Solaris Porting Layer.
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/sched.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 typedef struct {
45 	struct rw_semaphore rw_rwlock;
46 	kthread_t *rw_owner;
47 #ifdef CONFIG_LOCKDEP
48 	krw_type_t	rw_type;
49 #endif /* CONFIG_LOCKDEP */
50 } krwlock_t;
51 
52 #define	SEM(rwp)	(&(rwp)->rw_rwlock)
53 
54 static inline void
spl_rw_set_owner(krwlock_t * rwp)55 spl_rw_set_owner(krwlock_t *rwp)
56 {
57 	rwp->rw_owner = current;
58 }
59 
60 static inline void
spl_rw_clear_owner(krwlock_t * rwp)61 spl_rw_clear_owner(krwlock_t *rwp)
62 {
63 	rwp->rw_owner = NULL;
64 }
65 
66 static inline kthread_t *
rw_owner(krwlock_t * rwp)67 rw_owner(krwlock_t *rwp)
68 {
69 	return (rwp->rw_owner);
70 }
71 
72 #ifdef CONFIG_LOCKDEP
73 static inline void
spl_rw_set_type(krwlock_t * rwp,krw_type_t type)74 spl_rw_set_type(krwlock_t *rwp, krw_type_t type)
75 {
76 	rwp->rw_type = type;
77 }
78 static inline void
spl_rw_lockdep_off_maybe(krwlock_t * rwp)79 spl_rw_lockdep_off_maybe(krwlock_t *rwp)		\
80 {							\
81 	if (rwp && rwp->rw_type == RW_NOLOCKDEP)	\
82 		lockdep_off();				\
83 }
84 static inline void
spl_rw_lockdep_on_maybe(krwlock_t * rwp)85 spl_rw_lockdep_on_maybe(krwlock_t *rwp)			\
86 {							\
87 	if (rwp && rwp->rw_type == RW_NOLOCKDEP)	\
88 		lockdep_on();				\
89 }
90 #else  /* CONFIG_LOCKDEP */
91 #define	spl_rw_set_type(rwp, type)
92 #define	spl_rw_lockdep_off_maybe(rwp)
93 #define	spl_rw_lockdep_on_maybe(rwp)
94 #endif /* CONFIG_LOCKDEP */
95 
96 static inline int
RW_LOCK_HELD(krwlock_t * rwp)97 RW_LOCK_HELD(krwlock_t *rwp)
98 {
99 	return (rwsem_is_locked(SEM(rwp)));
100 }
101 
102 static inline int
RW_WRITE_HELD(krwlock_t * rwp)103 RW_WRITE_HELD(krwlock_t *rwp)
104 {
105 	return (rw_owner(rwp) == current);
106 }
107 
108 static inline int
RW_READ_HELD(krwlock_t * rwp)109 RW_READ_HELD(krwlock_t *rwp)
110 {
111 	return (RW_LOCK_HELD(rwp) && rw_owner(rwp) == NULL);
112 }
113 
114 /*
115  * The following functions must be a #define and not static inline.
116  * This ensures that the native linux semaphore functions (down/up)
117  * will be correctly located in the users code which is important
118  * for the built in kernel lock analysis tools
119  */
120 #define	rw_init(rwp, name, type, arg) /* CSTYLED */			\
121 ({									\
122 	static struct lock_class_key __key;				\
123 	ASSERT(type == RW_DEFAULT || type == RW_NOLOCKDEP);		\
124 									\
125 	__init_rwsem(SEM(rwp), #rwp, &__key);				\
126 	spl_rw_clear_owner(rwp);					\
127 	spl_rw_set_type(rwp, type);					\
128 })
129 
130 /*
131  * The Linux rwsem implementation does not require a matching destroy.
132  */
133 #define	rw_destroy(rwp)		((void) 0)
134 
135 /*
136  * Upgrading a rwsem from a reader to a writer is not supported by the
137  * Linux kernel.  The lock must be dropped and reacquired as a writer.
138  */
139 #define	rw_tryupgrade(rwp)	RW_WRITE_HELD(rwp)
140 
141 #define	rw_tryenter(rwp, rw) /* CSTYLED */				\
142 ({									\
143 	int _rc_ = 0;							\
144 									\
145 	spl_rw_lockdep_off_maybe(rwp);					\
146 	switch (rw) {							\
147 	case RW_READER:							\
148 		_rc_ = down_read_trylock(SEM(rwp));			\
149 		break;							\
150 	case RW_WRITER:							\
151 		if ((_rc_ = down_write_trylock(SEM(rwp))))		\
152 			spl_rw_set_owner(rwp);				\
153 		break;							\
154 	default:							\
155 		VERIFY(0);						\
156 	}								\
157 	spl_rw_lockdep_on_maybe(rwp);					\
158 	_rc_;								\
159 })
160 
161 #define	rw_enter(rwp, rw) /* CSTYLED */					\
162 ({									\
163 	spl_rw_lockdep_off_maybe(rwp);					\
164 	switch (rw) {							\
165 	case RW_READER:							\
166 		down_read(SEM(rwp));					\
167 		break;							\
168 	case RW_WRITER:							\
169 		down_write(SEM(rwp));					\
170 		spl_rw_set_owner(rwp);					\
171 		break;							\
172 	default:							\
173 		VERIFY(0);						\
174 	}								\
175 	spl_rw_lockdep_on_maybe(rwp);					\
176 })
177 
178 #define	rw_exit(rwp) /* CSTYLED */					\
179 ({									\
180 	spl_rw_lockdep_off_maybe(rwp);					\
181 	if (RW_WRITE_HELD(rwp)) {					\
182 		spl_rw_clear_owner(rwp);				\
183 		up_write(SEM(rwp));					\
184 	} else {							\
185 		ASSERT(RW_READ_HELD(rwp));				\
186 		up_read(SEM(rwp));					\
187 	}								\
188 	spl_rw_lockdep_on_maybe(rwp);					\
189 })
190 
191 #define	rw_downgrade(rwp) /* CSTYLED */					\
192 ({									\
193 	spl_rw_lockdep_off_maybe(rwp);					\
194 	spl_rw_clear_owner(rwp);					\
195 	downgrade_write(SEM(rwp));					\
196 	spl_rw_lockdep_on_maybe(rwp);					\
197 })
198 
199 #endif /* _SPL_RWLOCK_H */
200