lock.h revision 192853
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 192853 2009-05-26 20:28:22Z sson $ 30 */ 31 32#ifndef _SYS_LOCK_H_ 33#define _SYS_LOCK_H_ 34 35#include <sys/queue.h> 36#include <sys/_lock.h> 37 38struct lock_list_entry; 39struct thread; 40 41/* 42 * Lock classes. Each lock has a class which describes characteristics 43 * common to all types of locks of a given class. 44 * 45 * Spin locks in general must always protect against preemption, as it is 46 * an error to perform any type of context switch while holding a spin lock. 47 * Also, for an individual lock to be recursable, its class must allow 48 * recursion and the lock itself must explicitly allow recursion. 49 * 50 * The 'lc_ddb_show' function pointer is used to dump class-specific 51 * data for the 'show lock' DDB command. The 'lc_lock' and 52 * 'lc_unlock' function pointers are used in sleep(9) and cv_wait(9) 53 * to lock and unlock locks while blocking on a sleep queue. The 54 * return value of 'lc_unlock' will be passed to 'lc_lock' on resume 55 * to allow communication of state between the two routines. 56 */ 57 58struct lock_class { 59 const char *lc_name; 60 u_int lc_flags; 61 void (*lc_assert)(struct lock_object *lock, int what); 62 void (*lc_ddb_show)(struct lock_object *lock); 63 void (*lc_lock)(struct lock_object *lock, int how); 64 int (*lc_owner)(struct lock_object *lock, struct thread **owner); 65 int (*lc_unlock)(struct lock_object *lock); 66}; 67 68#define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */ 69#define LC_SPINLOCK 0x00000002 /* Spin lock. */ 70#define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */ 71#define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */ 72#define LC_UPGRADABLE 0x00000010 /* Upgrades and downgrades permitted. */ 73 74#define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */ 75#define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */ 76#define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */ 77#define LO_QUIET 0x00040000 /* Don't log locking operations. */ 78#define LO_RECURSABLE 0x00080000 /* Lock may recurse. */ 79#define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */ 80#define LO_UPGRADABLE 0x00200000 /* Lock may be upgraded/downgraded. */ 81#define LO_DUPOK 0x00400000 /* Don't check for duplicate acquires */ 82#define LO_CLASSMASK 0x0f000000 /* Class index bitmask. */ 83#define LO_NOPROFILE 0x10000000 /* Don't profile this lock */ 84 85/* 86 * Lock classes are statically assigned an index into the gobal lock_classes 87 * array. Debugging code looks up the lock class for a given lock object 88 * by indexing the array. 89 */ 90#define LO_CLASSSHIFT 24 91#define LO_CLASSINDEX(lock) ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT) 92#define LOCK_CLASS(lock) (lock_classes[LO_CLASSINDEX((lock))]) 93#define LOCK_CLASS_MAX (LO_CLASSMASK >> LO_CLASSSHIFT) 94 95/* 96 * Option flags passed to lock operations that witness also needs to know 97 * about or that are generic across all locks. 98 */ 99#define LOP_NEWORDER 0x00000001 /* Define a new lock order. */ 100#define LOP_QUIET 0x00000002 /* Don't log locking operations. */ 101#define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */ 102#define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */ 103#define LOP_DUPOK 0x00000010 /* Don't check for duplicate acquires */ 104 105/* Flags passed to witness_assert. */ 106#define LA_MASKASSERT 0x000000ff /* Mask for witness defined asserts. */ 107#define LA_UNLOCKED 0x00000000 /* Lock is unlocked. */ 108#define LA_LOCKED 0x00000001 /* Lock is at least share locked. */ 109#define LA_SLOCKED 0x00000002 /* Lock is exactly share locked. */ 110#define LA_XLOCKED 0x00000004 /* Lock is exclusively locked. */ 111#define LA_RECURSED 0x00000008 /* Lock is recursed. */ 112#define LA_NOTRECURSED 0x00000010 /* Lock is not recursed. */ 113 114#ifdef _KERNEL 115/* 116 * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled, 117 * then turn on LOCK_DEBUG. When this option is on, extra debugging 118 * facilities such as tracking the file and line number of lock operations 119 * are enabled. Also, mutex locking operations are not inlined to avoid 120 * bloat from all the extra debugging code. We also have to turn on all the 121 * calling conventions for this debugging code in modules so that modules can 122 * work with both debug and non-debug kernels. 123 */ 124#if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || defined(INVARIANT_SUPPORT) || defined(KTR) || defined(LOCK_PROFILING) 125#define LOCK_DEBUG 1 126#else 127#define LOCK_DEBUG 0 128#endif 129 130/* 131 * In the LOCK_DEBUG case, use the filename and line numbers for debugging 132 * operations. Otherwise, use default values to avoid the unneeded bloat. 133 */ 134#if LOCK_DEBUG > 0 135#define LOCK_FILE __FILE__ 136#define LOCK_LINE __LINE__ 137#else 138#define LOCK_FILE NULL 139#define LOCK_LINE 0 140#endif 141 142/* 143 * Macros for KTR_LOCK tracing. 144 * 145 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.) 146 * lo - struct lock_object * for this lock 147 * flags - flags passed to the lock operation 148 * recurse - this locks recursion level (or 0 if class is not recursable) 149 * result - result of a try lock operation 150 * file - file name 151 * line - line number 152 */ 153#define LOCK_LOG_TEST(lo, flags) \ 154 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0) 155 156#define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \ 157 if (LOCK_LOG_TEST((lo), (flags))) \ 158 CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d", \ 159 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \ 160 (u_int)(recurse), (file), (line)); \ 161} while (0) 162 163#define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \ 164 if (LOCK_LOG_TEST((lo), (flags))) \ 165 CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\ 166 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \ 167 (u_int)(result), (file), (line)); \ 168} while (0) 169 170#define LOCK_LOG_INIT(lo, flags) do { \ 171 if (LOCK_LOG_TEST((lo), (flags))) \ 172 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo), \ 173 LOCK_CLASS(lo)->lc_name, (lo)->lo_name); \ 174} while (0) 175 176#define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags) 177 178#define lock_initalized(lo) ((lo)->lo_flags & LO_INITIALIZED) 179 180/* 181 * Helpful macros for quickly coming up with assertions with informative 182 * panic messages. 183 */ 184#define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__) 185#define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__) 186#define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line) 187#define MPASS4(ex, what, file, line) \ 188 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line)) 189 190extern struct lock_class lock_class_mtx_sleep; 191extern struct lock_class lock_class_mtx_spin; 192extern struct lock_class lock_class_sx; 193extern struct lock_class lock_class_rw; 194extern struct lock_class lock_class_rm; 195extern struct lock_class lock_class_lockmgr; 196 197extern struct lock_class *lock_classes[]; 198 199void lock_init(struct lock_object *, struct lock_class *, 200 const char *, const char *, int); 201void lock_destroy(struct lock_object *); 202void spinlock_enter(void); 203void spinlock_exit(void); 204void witness_init(struct lock_object *, const char *); 205void witness_destroy(struct lock_object *); 206int witness_defineorder(struct lock_object *, struct lock_object *); 207void witness_checkorder(struct lock_object *, int, const char *, int, 208 struct lock_object *); 209void witness_lock(struct lock_object *, int, const char *, int); 210void witness_upgrade(struct lock_object *, int, const char *, int); 211void witness_downgrade(struct lock_object *, int, const char *, int); 212void witness_unlock(struct lock_object *, int, const char *, int); 213void witness_save(struct lock_object *, const char **, int *); 214void witness_restore(struct lock_object *, const char *, int); 215int witness_list_locks(struct lock_list_entry **); 216int witness_warn(int, struct lock_object *, const char *, ...); 217void witness_assert(struct lock_object *, int, const char *, int); 218void witness_display_spinlock(struct lock_object *, struct thread *); 219int witness_line(struct lock_object *); 220void witness_norelease(struct lock_object *); 221void witness_releaseok(struct lock_object *); 222const char *witness_file(struct lock_object *); 223void witness_thread_exit(struct thread *); 224 225#ifdef WITNESS 226 227/* Flags for witness_warn(). */ 228#define WARN_GIANTOK 0x01 /* Giant is exempt from this check. */ 229#define WARN_PANIC 0x02 /* Panic if check fails. */ 230#define WARN_SLEEPOK 0x04 /* Sleepable locks are exempt from check. */ 231 232#define WITNESS_INIT(lock, type) \ 233 witness_init((lock), (type)) 234 235#define WITNESS_DESTROY(lock) \ 236 witness_destroy(lock) 237 238#define WITNESS_CHECKORDER(lock, flags, file, line, interlock) \ 239 witness_checkorder((lock), (flags), (file), (line), (interlock)) 240 241#define WITNESS_DEFINEORDER(lock1, lock2) \ 242 witness_defineorder((struct lock_object *)(lock1), \ 243 (struct lock_object *)(lock2)) 244 245#define WITNESS_LOCK(lock, flags, file, line) \ 246 witness_lock((lock), (flags), (file), (line)) 247 248#define WITNESS_UPGRADE(lock, flags, file, line) \ 249 witness_upgrade((lock), (flags), (file), (line)) 250 251#define WITNESS_DOWNGRADE(lock, flags, file, line) \ 252 witness_downgrade((lock), (flags), (file), (line)) 253 254#define WITNESS_UNLOCK(lock, flags, file, line) \ 255 witness_unlock((lock), (flags), (file), (line)) 256 257#define WITNESS_CHECK(flags, lock, fmt, ...) \ 258 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__) 259 260#define WITNESS_WARN(flags, lock, fmt, ...) \ 261 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__) 262 263#define WITNESS_SAVE_DECL(n) \ 264 const char * __CONCAT(n, __wf); \ 265 int __CONCAT(n, __wl) 266 267#define WITNESS_SAVE(lock, n) \ 268 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl)) 269 270#define WITNESS_RESTORE(lock, n) \ 271 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl)) 272 273#define WITNESS_NORELEASE(lock) \ 274 witness_norelease(&(lock)->lock_object) 275 276#define WITNESS_RELEASEOK(lock) \ 277 witness_releaseok(&(lock)->lock_object) 278 279#define WITNESS_FILE(lock) \ 280 witness_file(lock) 281 282#define WITNESS_LINE(lock) \ 283 witness_line(lock) 284 285#else /* WITNESS */ 286#define WITNESS_INIT(lock, type) 287#define WITNESS_DESTROY(lock) 288#define WITNESS_DEFINEORDER(lock1, lock2) 0 289#define WITNESS_CHECKORDER(lock, flags, file, line, interlock) 290#define WITNESS_LOCK(lock, flags, file, line) 291#define WITNESS_UPGRADE(lock, flags, file, line) 292#define WITNESS_DOWNGRADE(lock, flags, file, line) 293#define WITNESS_UNLOCK(lock, flags, file, line) 294#define WITNESS_CHECK(flags, lock, fmt, ...) 0 295#define WITNESS_WARN(flags, lock, fmt, ...) 296#define WITNESS_SAVE_DECL(n) 297#define WITNESS_SAVE(lock, n) 298#define WITNESS_RESTORE(lock, n) 299#define WITNESS_NORELEASE(lock) 300#define WITNESS_RELEASEOK(lock) 301#define WITNESS_FILE(lock) ("?") 302#define WITNESS_LINE(lock) (0) 303#endif /* WITNESS */ 304 305/* 306 * Helper macros to allow developers to add explicit lock order checks 307 * wherever they please without having to actually grab a lock to do so. 308 */ 309#define witness_check(l) \ 310 WITNESS_CHECKORDER(&(l)->lock_object, LOP_EXCLUSIVE, LOCK_FILE, \ 311 LOCK_LINE, NULL) 312 313#define witness_check_shared(l) \ 314 WITNESS_CHECKORDER(&(l)->lock_object, 0, LOCK_FILE, LOCK_LINE, NULL) 315 316#endif /* _KERNEL */ 317#endif /* _SYS_LOCK_H_ */ 318