xref: /freebsd/sys/sys/lockmgr.h (revision 1d97ad676d586aa5d91227ebaa39bd9c3b1d68fe)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice(s), this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified other than the possible
13  *    addition of one or more copyright notices.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice(s), this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28  * DAMAGE.
29  */
30 
31 #ifndef	_SYS_LOCKMGR_H_
32 #define	_SYS_LOCKMGR_H_
33 
34 #include <sys/_lock.h>
35 #include <sys/_lockmgr.h>
36 #include <sys/_mutex.h>
37 #include <sys/_rwlock.h>
38 
39 #define	LK_SHARE			0x01
40 #define	LK_SHARED_WAITERS		0x02
41 #define	LK_EXCLUSIVE_WAITERS		0x04
42 #define	LK_EXCLUSIVE_SPINNERS		0x08
43 #define	LK_WRITER_RECURSED		0x10
44 #define	LK_ALL_WAITERS							\
45 	(LK_SHARED_WAITERS | LK_EXCLUSIVE_WAITERS)
46 #define	LK_FLAGMASK							\
47 	(LK_SHARE | LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS | LK_WRITER_RECURSED)
48 
49 #define	LK_HOLDER(x)			((x) & ~LK_FLAGMASK)
50 #define	LK_SHARERS_SHIFT		5
51 #define	LK_SHARERS(x)			(LK_HOLDER(x) >> LK_SHARERS_SHIFT)
52 #define	LK_SHARERS_LOCK(x)		((x) << LK_SHARERS_SHIFT | LK_SHARE)
53 #define	LK_ONE_SHARER			(1 << LK_SHARERS_SHIFT)
54 #define	LK_UNLOCKED			LK_SHARERS_LOCK(0)
55 #define	LK_KERNPROC			((uintptr_t)(-1) & ~LK_FLAGMASK)
56 
57 #ifdef _KERNEL
58 
59 #if !defined(LOCK_FILE) || !defined(LOCK_LINE)
60 #error	"LOCK_FILE and LOCK_LINE not defined, include <sys/lock.h> before"
61 #endif
62 
63 struct thread;
64 #define	lk_recurse	lock_object.lo_data
65 
66 /*
67  * Function prototipes.  Routines that start with an underscore are not part
68  * of the public interface and might be wrappered with a macro.
69  */
70 int	 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
71 	    const char *wmesg, int prio, int timo, const char *file, int line);
72 int	 lockmgr_lock_flags(struct lock *lk, u_int flags,
73 	    struct lock_object *ilk, const char *file, int line);
74 int	lockmgr_slock(struct lock *lk, u_int flags, const char *file, int line);
75 int	lockmgr_xlock(struct lock *lk, u_int flags, const char *file, int line);
76 int	lockmgr_unlock(struct lock *lk);
77 
78 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
79 void	 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line);
80 #endif
81 void	 _lockmgr_disown(struct lock *lk, const char *file, int line);
82 
83 void	 lockallowrecurse(struct lock *lk);
84 void	 lockallowshare(struct lock *lk);
85 bool	 lockcanrecurse(struct lock *lk);
86 void	 lockdestroy(struct lock *lk);
87 void	 lockdisablerecurse(struct lock *lk);
88 void	 lockdisableshare(struct lock *lk);
89 void	 lockinit(struct lock *lk, int prio, const char *wmesg, int timo,
90 	    int flags);
91 #ifdef DDB
92 int	 lockmgr_chain(struct thread *td, struct thread **ownerp);
93 #endif
94 void	 lockmgr_printinfo(const struct lock *lk);
95 int	 lockstatus(const struct lock *lk);
96 
97 /*
98  * As far as the ilk can be a static NULL pointer these functions need a
99  * strict prototype in order to safely use the lock_object member.
100  */
101 static __inline int
_lockmgr_args(struct lock * lk,u_int flags,struct mtx * ilk,const char * wmesg,int prio,int timo,const char * file,int line)102 _lockmgr_args(struct lock *lk, u_int flags, struct mtx *ilk, const char *wmesg,
103     int prio, int timo, const char *file, int line)
104 {
105 
106 	return (__lockmgr_args(lk, flags, (ilk != NULL) ? &ilk->lock_object :
107 	    NULL, wmesg, prio, timo, file, line));
108 }
109 
110 static __inline int
_lockmgr_args_rw(struct lock * lk,u_int flags,struct rwlock * ilk,const char * wmesg,int prio,int timo,const char * file,int line)111 _lockmgr_args_rw(struct lock *lk, u_int flags, struct rwlock *ilk,
112     const char *wmesg, int prio, int timo, const char *file, int line)
113 {
114 
115 	return (__lockmgr_args(lk, flags, (ilk != NULL) ? &ilk->lock_object :
116 	    NULL, wmesg, prio, timo, file, line));
117 }
118 
119 /*
120  * Define aliases in order to complete lockmgr KPI.
121  */
122 #define	lockmgr_read_value(lk)	((lk)->lk_lock)
123 #define	lockmgr(lk, flags, ilk)						\
124 	_lockmgr_args((lk), (flags), (ilk), LK_WMESG_DEFAULT,		\
125 	    LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, LOCK_FILE, LOCK_LINE)
126 #define	lockmgr_args(lk, flags, ilk, wmesg, prio, timo)			\
127 	_lockmgr_args((lk), (flags), (ilk), (wmesg), (prio), (timo),	\
128 	    LOCK_FILE, LOCK_LINE)
129 #define	lockmgr_args_rw(lk, flags, ilk, wmesg, prio, timo)		\
130 	_lockmgr_args_rw((lk), (flags), (ilk), (wmesg), (prio), (timo),	\
131 	    LOCK_FILE, LOCK_LINE)
132 #define	lockmgr_disown(lk)						\
133 	_lockmgr_disown((lk), LOCK_FILE, LOCK_LINE)
134 #define	lockmgr_disowned_v(v)						\
135 	(LK_HOLDER((v)) == LK_KERNPROC)
136 #define	lockmgr_disowned(lk)						\
137 	lockmgr_disowned_v(lockmgr_read_value((lk)))
138 #define	lockmgr_recursed_v(v)						\
139 	(v & LK_WRITER_RECURSED)
140 #define	lockmgr_recursed(lk)						\
141 	lockmgr_recursed_v((lk)->lk_lock)
142 #define	lockmgr_rw(lk, flags, ilk)					\
143 	_lockmgr_args_rw((lk), (flags), (ilk), LK_WMESG_DEFAULT,	\
144 	    LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, LOCK_FILE, LOCK_LINE)
145 #ifdef INVARIANTS
146 #define	lockmgr_assert(lk, what)					\
147 	_lockmgr_assert((lk), (what), LOCK_FILE, LOCK_LINE)
148 #else
149 #define	lockmgr_assert(lk, what)
150 #endif
151 
152 /*
153  * Flags for lockinit().
154  */
155 #define	LK_INIT_MASK	0x0001FF
156 #define	LK_CANRECURSE	0x000001
157 #define	LK_NODUP	0x000002
158 #define	LK_NOPROFILE	0x000004
159 #define	LK_NOSHARE	0x000008
160 #define	LK_NOWITNESS	0x000010
161 #define	LK_QUIET	0x000020
162 #define	LK_UNUSED0	0x000040	/* Was LK_ADAPTIVE */
163 #define	LK_IS_VNODE	0x000080	/* Tell WITNESS about a VNODE lock */
164 #define	LK_NEW		0x000100
165 
166 /*
167  * Additional attributes to be used in lockmgr().
168  */
169 #define	LK_EATTR_MASK	0x00FF00
170 #define	LK_INTERLOCK	0x000100
171 #define	LK_NOWAIT	0x000200
172 #define	LK_RETRY	0x000400
173 #define	LK_SLEEPFAIL	0x000800
174 #define	LK_TIMELOCK	0x001000
175 #define	LK_NODDLKTREAT	0x002000
176 #define	LK_ADAPTIVE	0x004000
177 
178 /*
179  * Operations for lockmgr().
180  */
181 #define	LK_TYPE_MASK	0xFF0000
182 #define	LK_DOWNGRADE	0x010000
183 #define	LK_DRAIN	0x020000
184 #define	LK_EXCLOTHER	0x040000
185 #define	LK_EXCLUSIVE	0x080000
186 #define	LK_RELEASE	0x100000
187 #define	LK_SHARED	0x200000
188 #define	LK_UPGRADE	0x400000
189 #define	LK_TRYUPGRADE	0x800000
190 
191 #define	LK_TOTAL_MASK	(LK_INIT_MASK | LK_EATTR_MASK | LK_TYPE_MASK)
192 
193 /*
194  * Default values for lockmgr_args().
195  */
196 #define	LK_WMESG_DEFAULT	(NULL)
197 #define	LK_PRIO_DEFAULT		(0)
198 #define	LK_TIMO_DEFAULT		(0)
199 
200 /*
201  * Assertion flags.
202  */
203 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
204 #define	KA_LOCKED	LA_LOCKED
205 #define	KA_SLOCKED	LA_SLOCKED
206 #define	KA_XLOCKED	LA_XLOCKED
207 #define	KA_UNLOCKED	LA_UNLOCKED
208 #define	KA_RECURSED	LA_RECURSED
209 #define	KA_NOTRECURSED	LA_NOTRECURSED
210 #endif
211 
212 #endif /* _KERNEL */
213 
214 #endif /* !_SYS_LOCKMGR_H_ */
215