lock.h revision 83366
1/*-
2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 *    promote products derived from this software without specific prior
14 *    written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
29 * $FreeBSD: head/sys/sys/lock.h 83366 2001-09-12 08:38:13Z julian $
30 */
31
32#ifndef _SYS_LOCK_H_
33#define _SYS_LOCK_H_
34
35/*
36 * XXX - compatability until lockmgr() goes away or all the #includes are
37 * updated.
38 */
39#include <sys/lockmgr.h>
40
41#include <sys/queue.h>
42#include <sys/_lock.h>
43
44/*
45 * Lock classes.  Each lock has a class which describes characteristics
46 * common to all types of locks of a given class.
47 *
48 * Spin locks in general must always protect against preemption, as it is
49 * an error to perform any type of context switch while holding a spin lock.
50 * Also, for an individual lock to be recursable, its class must allow
51 * recursion and the lock itself must explicitly allow recursion.
52 */
53
54struct lock_class {
55	const	char *lc_name;
56	u_int	lc_flags;
57};
58
59#define	LC_SLEEPLOCK	0x00000001	/* Sleep lock. */
60#define	LC_SPINLOCK	0x00000002	/* Spin lock. */
61#define	LC_SLEEPABLE	0x00000004	/* Sleeping allowed with this lock. */
62#define	LC_RECURSABLE	0x00000008	/* Locks of this type may recurse. */
63#define	LC_UPGRADABLE	0x00000010	/* Upgrades and downgrades permitted. */
64
65#define	LO_CLASSFLAGS	0x0000ffff	/* Class specific flags. */
66#define	LO_INITIALIZED	0x00010000	/* Lock has been initialized. */
67#define	LO_WITNESS	0x00020000	/* Should witness monitor this lock. */
68#define	LO_QUIET	0x00040000	/* Don't log locking operations. */
69#define	LO_RECURSABLE	0x00080000	/* Lock may recurse. */
70#define	LO_SLEEPABLE	0x00100000	/* Lock may be held while sleeping. */
71#define	LO_UPGRADABLE	0x00200000	/* Lock may be upgraded/downgraded. */
72
73#define	LI_RECURSEMASK	0x0000ffff	/* Recursion depth of lock instance. */
74#define	LI_SLEPT	0x00010000	/* Lock instance has been slept with. */
75#define	LI_EXCLUSIVE	0x00020000	/* Exclusive lock instance. */
76
77/*
78 * Option flags passed to lock operations that witness also needs to know
79 * about or that are generic across all locks.
80 */
81#define	LOP_NOSWITCH	0x00000001	/* Lock doesn't switch on release. */
82#define	LOP_QUIET	0x00000002	/* Don't log locking operations. */
83#define	LOP_TRYLOCK	0x00000004	/* Don't check lock order. */
84#define	LOP_EXCLUSIVE	0x00000008	/* Exclusive lock. */
85
86/* Flags passed to witness_assert. */
87#define	LA_UNLOCKED	0x00000000	/* Lock is unlocked. */
88#define	LA_LOCKED	0x00000001	/* Lock is at least share locked. */
89#define	LA_SLOCKED	0x00000002	/* Lock is exactly share locked. */
90#define	LA_XLOCKED	0x00000004	/* Lock is exclusively locked. */
91#define	LA_RECURSED	0x00000008	/* Lock is recursed. */
92#define	LA_NOTRECURSED	0x00000010	/* Lock is not recursed. */
93
94#ifdef _KERNEL
95/*
96 * Lock instances.  A lock instance is the data associated with a lock while
97 * it is held by witness.  For example, a lock instance will hold the
98 * recursion count of a lock.  Lock instances are held in lists.  Spin locks
99 * are held in a per-cpu list while sleep locks are held in per-process list.
100 */
101struct lock_instance {
102	struct	lock_object *li_lock;
103	const	char *li_file;		/* File and line of last acquire. */
104	int	li_line;
105	u_int	li_flags;		/* Recursion count and LI_* flags. */
106};
107
108/*
109 * A simple list type used to build the list of locks held by a process
110 * or CPU.  We can't simply embed the list in struct lock_object since a
111 * lock may be held by more than one process if it is a shared lock.  Locks
112 * are added to the head of the list, so we fill up each list entry from
113 * "the back" logically.  To ease some of the arithmetic, we actually fill
114 * in each list entry the normal way (childer[0] then children[1], etc.) but
115 * when we traverse the list we read children[count-1] as the first entry
116 * down to children[0] as the final entry.
117 */
118#define	LOCK_NCHILDREN	3
119
120struct lock_list_entry {
121	struct	lock_list_entry *ll_next;
122	struct	lock_instance ll_children[LOCK_NCHILDREN];
123	u_int	ll_count;
124};
125
126/*
127 * Macros for KTR_LOCK tracing.
128 *
129 * opname  - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
130 * lo      - struct lock_object * for this lock
131 * flags   - flags passed to the lock operation
132 * recurse - this locks recursion level (or 0 if class is not recursable)
133 * result  - result of a try lock operation
134 * file    - file name
135 * line    - line number
136 */
137#define	LOCK_LOG_TEST(lo, flags)					\
138	(((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
139
140#define	LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do {	\
141	if (LOCK_LOG_TEST((lo), (flags)))				\
142		CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d",	\
143		    (lo)->lo_class->lc_name, (lo)->lo_name,		\
144		    (u_int)(recurse), (file), (line));			\
145} while (0)
146
147#define	LOCK_LOG_TRY(opname, lo, flags, result, file, line) do {	\
148	if (LOCK_LOG_TEST((lo), (flags)))				\
149		CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\
150		    (lo)->lo_class->lc_name, (lo)->lo_name,		\
151		    (u_int)(result), (file), (line));			\
152} while (0)
153
154#define	LOCK_LOG_INIT(lo, flags) do {					\
155	if (LOCK_LOG_TEST((lo), (flags)))				\
156		CTR3(KTR_LOCK, __func__ ": %p (%s) %s",	(lo),		\
157 		    (lo)->lo_class->lc_name, (lo)->lo_name);		\
158} while (0)
159
160#define	LOCK_LOG_DESTROY(lo, flags)	LOCK_LOG_INIT(lo, flags)
161
162/*
163 * Helpful macros for quickly coming up with assertions with informative
164 * panic messages.
165 */
166#define MPASS(ex)		MPASS4(ex, #ex, __FILE__, __LINE__)
167#define MPASS2(ex, what)	MPASS4(ex, what, __FILE__, __LINE__)
168#define MPASS3(ex, file, line)	MPASS4(ex, #ex, file, line)
169#define MPASS4(ex, what, file, line)					\
170	KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
171
172extern struct lock_class lock_class_mtx_sleep;
173extern struct lock_class lock_class_mtx_spin;
174extern struct lock_class lock_class_sx;
175
176void	witness_init(struct lock_object *);
177void	witness_destroy(struct lock_object *);
178void	witness_lock(struct lock_object *, int, const char *, int);
179void	witness_upgrade(struct lock_object *, int, const char *, int);
180void	witness_downgrade(struct lock_object *, int, const char *, int);
181void	witness_unlock(struct lock_object *, int, const char *, int);
182void	witness_save(struct lock_object *, const char **, int *);
183void	witness_restore(struct lock_object *, const char *, int);
184int	witness_list_locks(struct lock_list_entry **);
185int	witness_list(struct thread *);
186int	witness_sleep(int, struct lock_object *, const char *, int);
187void	witness_assert(struct lock_object *, int, const char *, int);
188
189#ifdef	WITNESS
190#define	WITNESS_INIT(lock)						\
191	witness_init((lock))
192
193#define WITNESS_DESTROY(lock)						\
194	witness_destroy(lock)
195
196#define	WITNESS_LOCK(lock, flags, file, line)				\
197	witness_lock((lock), (flags), (file), (line))
198
199#define	WITNESS_UPGRADE(lock, flags, file, line)			\
200	witness_upgrade((lock), (flags), (file), (line))
201
202#define	WITNESS_DOWNGRADE(lock, flags, file, line)			\
203	witness_downgrade((lock), (flags), (file), (line))
204
205#define	WITNESS_UNLOCK(lock, flags, file, line)				\
206	witness_unlock((lock), (flags), (file), (line))
207
208#define	WITNESS_SLEEP(check, lock) 					\
209	witness_sleep((check), (lock), __FILE__, __LINE__)
210
211#define	WITNESS_SAVE_DECL(n)						\
212	const char * __CONCAT(n, __wf);					\
213	int __CONCAT(n, __wl)
214
215#define	WITNESS_SAVE(lock, n) 						\
216	witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
217
218#define	WITNESS_RESTORE(lock, n) 					\
219	witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
220
221#else	/* WITNESS */
222#define	WITNESS_INIT(lock)	(lock)->lo_flags |= LO_INITIALIZED
223#define	WITNESS_DESTROY(lock)	(lock)->lo_flags &= ~LO_INITIALIZED
224#define	WITNESS_LOCK(lock, flags, file, line)
225#define	WITNESS_UPGRADE(lock, flags, file, line)
226#define	WITNESS_DOWNGRADE(lock, flags, file, line)
227#define	WITNESS_UNLOCK(lock, flags, file, line)
228#define	WITNESS_SLEEP(check, lock)
229#define	WITNESS_SAVE_DECL(n)
230#define	WITNESS_SAVE(lock, n)
231#define	WITNESS_RESTORE(lock, n)
232#endif	/* WITNESS */
233
234#endif	/* _KERNEL */
235#endif	/* _SYS_LOCK_H_ */
236