mutex.h revision 74900
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/mutex.h 74900 2001-03-28 02:40:47Z jhb $
30 */
31
32#ifndef _SYS_MUTEX_H_
33#define _SYS_MUTEX_H_
34
35#ifndef LOCORE
36#include <sys/queue.h>
37
38#ifdef _KERNEL
39#include <sys/ktr.h>
40#include <sys/systm.h>
41#include <machine/atomic.h>
42#include <machine/cpufunc.h>
43#include <machine/globals.h>
44#endif	/* _KERNEL_ */
45#endif	/* !LOCORE */
46
47#include <machine/mutex.h>
48
49#ifdef _KERNEL
50
51/*
52 * Mutex types and options stored in mutex->mtx_flags
53 */
54#define	MTX_DEF		0x00000000	/* DEFAULT (sleep) lock */
55#define MTX_SPIN	0x00000001	/* Spin lock (disables interrupts) */
56#define MTX_RECURSE	0x00000002	/* Option: lock allowed to recurse */
57
58/*
59 * Option flags passed to certain lock/unlock routines, through the use
60 * of corresponding mtx_{lock,unlock}_flags() interface macros.
61 *
62 * XXX: The only reason we make these bits not interfere with the above "types
63 *	and options" bits is because we have to pass both to the witness
64 *	routines right now; if/when we clean up the witness interface to
65 *	not check for mutex type from the passed in flag, but rather from
66 *	the mutex lock's mtx_flags field, then we can change these values to
67 *	0x1, 0x2, ...
68 */
69#define	MTX_NOSWITCH	0x00000004	/* Do not switch on release */
70#define	MTX_QUIET	0x00000008	/* Don't log a mutex event */
71
72/*
73 * State bits kept in mutex->mtx_lock, for the DEFAULT lock type. None of this,
74 * with the exception of MTX_UNOWNED, applies to spin locks.
75 */
76#define	MTX_RECURSED	0x00000001	/* lock recursed (for MTX_DEF only) */
77#define	MTX_CONTESTED	0x00000002	/* lock contested (for MTX_DEF only) */
78#define MTX_UNOWNED	0x00000004	/* Cookie for free mutex */
79#define	MTX_FLAGMASK	~(MTX_RECURSED | MTX_CONTESTED)
80
81#endif	/* _KERNEL */
82
83#ifndef LOCORE
84
85struct mtx_debug;
86
87/*
88 * Sleep/spin mutex
89 */
90struct mtx {
91	volatile uintptr_t mtx_lock;	/* owner (and state for sleep locks) */
92	volatile u_int	mtx_recurse;	/* number of recursive holds */
93	u_int		mtx_savecrit;	/* saved flags (for spin locks) */
94	int		mtx_flags;	/* flags passed to mtx_init() */
95	const char	*mtx_description;
96	TAILQ_HEAD(, proc) mtx_blocked;	/* threads blocked on this lock */
97	LIST_ENTRY(mtx)	mtx_contested;	/* list of all contested locks */
98	struct mtx	*mtx_next;	/* all existing locks 	*/
99	struct mtx	*mtx_prev;	/*  in system...	*/
100	struct mtx_debug *mtx_debug;	/* debugging information... */
101};
102
103/*
104 * XXX: Friendly reminder to fix things in MP code that is presently being
105 * XXX: worked on.
106 */
107#define mp_fixme(string)
108
109#ifdef _KERNEL
110
111/*
112 * Strings for KTR_LOCK tracing.
113 */
114extern char	STR_mtx_lock_slp[];
115extern char	STR_mtx_lock_spn[];
116extern char	STR_mtx_unlock_slp[];
117extern char	STR_mtx_unlock_spn[];
118
119/*
120 * Prototypes
121 *
122 * NOTE: Functions prepended with `_' (underscore) are exported to other parts
123 *	 of the kernel via macros, thus allowing us to use the cpp __FILE__
124 *	 and __LINE__. These functions should not be called directly by any
125 *	 code using the IPI. Their macros cover their functionality.
126 *
127 * [See below for descriptions]
128 *
129 */
130void	mtx_init(struct mtx *m, const char *description, int opts);
131void	mtx_destroy(struct mtx *m);
132void	_mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line);
133void	_mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line);
134void	_mtx_lock_spin(struct mtx *m, int opts, critical_t mtx_crit,
135		       const char *file, int line);
136void	_mtx_unlock_spin(struct mtx *m, int opts, const char *file, int line);
137int	_mtx_trylock(struct mtx *m, int opts, const char *file, int line);
138void	_mtx_lock_flags(struct mtx *m, int opts, const char *file, int line);
139void	_mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line);
140void	_mtx_lock_spin_flags(struct mtx *m, int opts, const char *file,
141			     int line);
142void	_mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file,
143			     int line);
144#ifdef INVARIANT_SUPPORT
145void	_mtx_assert(struct mtx *m, int what, const char *file, int line);
146#endif
147
148/*
149 * We define our machine-independent (unoptimized) mutex micro-operations
150 * here, if they are not already defined in the machine-dependent mutex.h
151 */
152
153/* Actually obtain mtx_lock */
154#ifndef _obtain_lock
155#define _obtain_lock(mp, tid)						\
156	atomic_cmpset_acq_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED, (tid))
157#endif
158
159/* Actually release mtx_lock */
160#ifndef _release_lock
161#define _release_lock(mp, tid)						\
162	atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), (void *)MTX_UNOWNED)
163#endif
164
165/* Actually release mtx_lock quickly, assuming we own it. */
166#ifndef _release_lock_quick
167#define _release_lock_quick(mp)						\
168	atomic_store_rel_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED)
169#endif
170
171/*
172 * Obtain a sleep lock inline, or call the "hard" function if we can't get it
173 * easy.
174 */
175#ifndef _get_sleep_lock
176#define _get_sleep_lock(mp, tid, opts, file, line) do {			\
177	if (!_obtain_lock((mp), (tid)))					\
178		_mtx_lock_sleep((mp), (opts), (file), (line));		\
179} while (0)
180#endif
181
182/*
183 * Obtain a spin lock inline, or call the "hard" function if we can't get it
184 * easy. For spinlocks, we handle recursion inline (it turns out that function
185 * calls can be significantly expensive on some architectures).
186 * Since spin locks are not _too_ common, inlining this code is not too big
187 * a deal.
188 */
189#ifndef _get_spin_lock
190#define _get_spin_lock(mp, tid, opts, file, line) do {			\
191	critical_t _mtx_crit;						\
192	_mtx_crit = critical_enter();					\
193	if (!_obtain_lock((mp), (tid))) {				\
194		if ((mp)->mtx_lock == (uintptr_t)(tid))			\
195			(mp)->mtx_recurse++;				\
196		else							\
197			_mtx_lock_spin((mp), (opts), _mtx_crit, (file),	\
198			    (line));					\
199	} else								\
200		(mp)->mtx_savecrit = _mtx_crit;				\
201} while (0)
202#endif
203
204/*
205 * Release a sleep lock inline, or call the "hard" function if we can't do it
206 * easy.
207 */
208#ifndef _rel_sleep_lock
209#define _rel_sleep_lock(mp, tid, opts, file, line) do {			\
210	if (!_release_lock((mp), (tid)))				\
211		_mtx_unlock_sleep((mp), (opts), (file), (line));	\
212} while (0)
213#endif
214
215/*
216 * For spinlocks, we can handle everything inline, as it's pretty simple and
217 * a function call would be too expensive (at least on some architectures).
218 * Since spin locks are not _too_ common, inlining this code is not too big
219 * a deal.
220 */
221#ifndef _rel_spin_lock
222#define _rel_spin_lock(mp) do {						\
223	critical_t _mtx_crit = (mp)->mtx_savecrit;			\
224	if (mtx_recursed((mp)))						\
225		(mp)->mtx_recurse--;					\
226	else {								\
227		_release_lock_quick((mp));				\
228		critical_exit(_mtx_crit);				\
229	}								\
230} while (0)
231#endif
232
233/*
234 * Exported lock manipulation interface.
235 *
236 * mtx_lock(m) locks MTX_DEF mutex `m'
237 *
238 * mtx_lock_spin(m) locks MTX_SPIN mutex `m'
239 *
240 * mtx_unlock(m) unlocks MTX_DEF mutex `m'
241 *
242 * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m'
243 *
244 * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m'
245 *     and passes option flags `opts' to the "hard" function, if required.
246 *     With these routines, it is possible to pass flags such as MTX_QUIET
247 *     and/or MTX_NOSWITCH to the appropriate lock manipulation routines.
248 *
249 * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
250 *     it cannot. Rather, it returns 0 on failure and non-zero on success.
251 *     It does NOT handle recursion as we assume that if a caller is properly
252 *     using this part of the interface, he will know that the lock in question
253 *     is _not_ recursed.
254 *
255 * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts
256 *     relevant option flags `opts.'
257 *
258 * mtx_owned(m) returns non-zero if the current thread owns the lock `m'
259 *
260 * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed.
261 */
262#define mtx_lock(m)		mtx_lock_flags((m), 0)
263#define mtx_lock_spin(m)	mtx_lock_spin_flags((m), 0)
264#define mtx_trylock(m)		mtx_trylock_flags((m), 0)
265#define mtx_unlock(m)		mtx_unlock_flags((m), 0)
266#define mtx_unlock_spin(m)	mtx_unlock_spin_flags((m), 0)
267
268#ifdef KLD_MODULE
269#define	mtx_lock_flags(m, opts)						\
270	_mtx_lock_flags((m), (opts), __FILE__, __LINE__)
271#define	mtx_unlock_flags(m, opts)					\
272	_mtx_unlock_flags((m), (opts), __FILE__, __LINE__)
273#define	mtx_lock_spin_flags(m, opts)					\
274	_mtx_lock_spin_flags((m), (opts), __FILE__, __LINE__)
275#define	mtx_unlock_spin_flags(m, opts)					\
276	_mtx_unlock_spin_flags((m), (opts), __FILE__, __LINE__)
277#else
278#define	mtx_lock_flags(m, opts)						\
279	__mtx_lock_flags((m), (opts), __FILE__, __LINE__)
280#define	mtx_unlock_flags(m, opts)					\
281	__mtx_unlock_flags((m), (opts), __FILE__, __LINE__)
282#define	mtx_lock_spin_flags(m, opts)					\
283	__mtx_lock_spin_flags((m), (opts), __FILE__, __LINE__)
284#define	mtx_unlock_spin_flags(m, opts)					\
285	__mtx_unlock_spin_flags((m), (opts), __FILE__, __LINE__)
286#endif
287
288#define __mtx_lock_flags(m, opts, file, line) do {			\
289	MPASS(curproc != NULL);						\
290	KASSERT(((opts) & MTX_NOSWITCH) == 0,				\
291	    ("MTX_NOSWITCH used at %s:%d", (file), (line)));		\
292	_get_sleep_lock((m), curproc, (opts), (file), (line));		\
293	if (((opts) & MTX_QUIET) == 0)					\
294		CTR5(KTR_LOCK, STR_mtx_lock_slp,			\
295		    (m)->mtx_description, (m), (m)->mtx_recurse, 	\
296		    (file), (line));					\
297	WITNESS_ENTER((m), ((m)->mtx_flags | (opts)), (file), (line));	\
298} while (0)
299
300#define __mtx_lock_spin_flags(m, opts, file, line) do {			\
301	MPASS(curproc != NULL);						\
302	_get_spin_lock((m), curproc, (opts), (file), (line));		\
303	if (((opts) & MTX_QUIET) == 0)					\
304		CTR5(KTR_LOCK, STR_mtx_lock_spn,			\
305		    (m)->mtx_description, (m), (m)->mtx_recurse, 	\
306		    (file), (line));					\
307	WITNESS_ENTER((m), ((m)->mtx_flags | (opts)), (file), (line));	\
308} while (0)
309
310#define __mtx_unlock_flags(m, opts, file, line) do {			\
311	MPASS(curproc != NULL);						\
312	mtx_assert((m), MA_OWNED);					\
313	WITNESS_EXIT((m), ((m)->mtx_flags | (opts)), (file), (line));	\
314	_rel_sleep_lock((m), curproc, (opts), (file), (line));		\
315	if (((opts) & MTX_QUIET) == 0)					\
316		CTR5(KTR_LOCK, STR_mtx_unlock_slp,			\
317		    (m)->mtx_description, (m), (m)->mtx_recurse, 	\
318		    (file), (line));					\
319} while (0)
320
321#define __mtx_unlock_spin_flags(m, opts, file, line) do {		\
322	MPASS(curproc != NULL);						\
323	mtx_assert((m), MA_OWNED);					\
324	WITNESS_EXIT((m), ((m)->mtx_flags | (opts)), (file), (line));	\
325	_rel_spin_lock((m));						\
326	if (((opts) & MTX_QUIET) == 0)					\
327		CTR5(KTR_LOCK, STR_mtx_unlock_spn,			\
328		    (m)->mtx_description, (m), (m)->mtx_recurse, 	\
329		    (file), (line));					\
330} while (0)
331
332#define mtx_trylock_flags(m, opts)					\
333	_mtx_trylock((m), (opts), __FILE__, __LINE__)
334
335#define mtx_owned(m)	(((m)->mtx_lock & MTX_FLAGMASK) == (uintptr_t)curproc)
336
337#define mtx_recursed(m)	((m)->mtx_recurse != 0)
338
339/*
340 * Global locks.
341 */
342extern struct mtx	sched_lock;
343extern struct mtx	Giant;
344
345/*
346 * Giant lock manipulation and clean exit macros.
347 * Used to replace return with an exit Giant and return.
348 *
349 * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT()
350 */
351#define DROP_GIANT_NOSWITCH()						\
352do {									\
353	int _giantcnt;							\
354	WITNESS_SAVE_DECL(Giant);					\
355									\
356	if (mtx_owned(&Giant))						\
357		WITNESS_SAVE(&Giant, Giant);				\
358	for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++)		\
359		mtx_unlock_flags(&Giant, MTX_NOSWITCH)
360
361#define DROP_GIANT()							\
362do {									\
363	int _giantcnt;							\
364	WITNESS_SAVE_DECL(Giant);					\
365									\
366	if (mtx_owned(&Giant))						\
367		WITNESS_SAVE(&Giant, Giant);				\
368	for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++)		\
369		mtx_unlock(&Giant)
370
371#define PICKUP_GIANT()							\
372	mtx_assert(&Giant, MA_NOTOWNED);				\
373	while (_giantcnt--)						\
374		mtx_lock(&Giant);					\
375	if (mtx_owned(&Giant))						\
376		WITNESS_RESTORE(&Giant, Giant);				\
377} while (0)
378
379#define PARTIAL_PICKUP_GIANT()						\
380	mtx_assert(&Giant, MA_NOTOWNED);				\
381	while (_giantcnt--)						\
382		mtx_lock(&Giant);					\
383	if (mtx_owned(&Giant))						\
384		WITNESS_RESTORE(&Giant, Giant)
385
386/*
387 * The INVARIANTS-enabled mtx_assert() functionality.
388 *
389 * The constants need to be defined for INVARIANT_SUPPORT infrastructure
390 * support as _mtx_assert() itself uses them and the latter implies that
391 * _mtx_assert() must build.
392 */
393#ifdef INVARIANT_SUPPORT
394#define MA_OWNED	0x01
395#define MA_NOTOWNED	0x02
396#define MA_RECURSED	0x04
397#define MA_NOTRECURSED	0x08
398#endif /* INVARIANT_SUPPORT */
399
400#ifdef INVARIANTS
401#define	mtx_assert(m, what)						\
402	_mtx_assert((m), (what), __FILE__, __LINE__)
403
404#else	/* INVARIANTS */
405#define mtx_assert(m, what)
406#endif	/* INVARIANTS */
407
408#define MPASS(ex)		MPASS4(ex, #ex, __FILE__, __LINE__)
409#define MPASS2(ex, what)	MPASS4(ex, what, __FILE__, __LINE__)
410#define MPASS3(ex, file, line)	MPASS4(ex, #ex, file, line)
411#define MPASS4(ex, what, file, line)					\
412	KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
413
414/*
415 * Exported WITNESS-enabled functions and corresponding wrapper macros.
416 */
417#ifdef	WITNESS
418void	witness_save(struct mtx *, const char **, int *);
419void	witness_restore(struct mtx *, const char *, int);
420void	witness_enter(struct mtx *, int, const char *, int);
421void	witness_try_enter(struct mtx *, int, const char *, int);
422void	witness_exit(struct mtx *, int, const char *, int);
423int	witness_list(struct proc *);
424int	witness_sleep(int, struct mtx *, const char *, int);
425
426#define WITNESS_ENTER(m, t, f, l)					\
427	witness_enter((m), (t), (f), (l))
428
429#define WITNESS_EXIT(m, t, f, l)					\
430	witness_exit((m), (t), (f), (l))
431
432#define	WITNESS_SLEEP(check, m) 					\
433	witness_sleep(check, (m), __FILE__, __LINE__)
434
435#define	WITNESS_SAVE_DECL(n)						\
436	const char * __CONCAT(n, __wf);					\
437	int __CONCAT(n, __wl)
438
439#define	WITNESS_SAVE(m, n) 						\
440	witness_save(m, &__CONCAT(n, __wf), &__CONCAT(n, __wl))
441
442#define	WITNESS_RESTORE(m, n) 						\
443	witness_restore(m, __CONCAT(n, __wf), __CONCAT(n, __wl))
444
445#else	/* WITNESS */
446#define witness_enter(m, t, f, l)
447#define witness_tryenter(m, t, f, l)
448#define witness_exit(m, t, f, l)
449#define	witness_list(p)
450#define	witness_sleep(c, m, f, l)
451
452#define WITNESS_ENTER(m, t, f, l)
453#define WITNESS_EXIT(m, t, f, l)
454#define	WITNESS_SLEEP(check, m)
455#define	WITNESS_SAVE_DECL(n)
456#define	WITNESS_SAVE(m, n)
457#define	WITNESS_RESTORE(m, n)
458#endif	/* WITNESS */
459
460#endif	/* _KERNEL */
461#endif	/* !LOCORE */
462#endif	/* _SYS_MUTEX_H_ */
463