lock.h revision 181695
1214518Srpaulo/*- 2190214Srpaulo * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved. 3190214Srpaulo * 4190214Srpaulo * Redistribution and use in source and binary forms, with or without 5190214Srpaulo * modification, are permitted provided that the following conditions 6190214Srpaulo * are met: 7190214Srpaulo * 1. Redistributions of source code must retain the above copyright 8190214Srpaulo * notice, this list of conditions and the following disclaimer. 9190214Srpaulo * 2. Redistributions in binary form must reproduce the above copyright 10190214Srpaulo * notice, this list of conditions and the following disclaimer in the 11190214Srpaulo * documentation and/or other materials provided with the distribution. 12190214Srpaulo * 3. Berkeley Software Design Inc's name may not be used to endorse or 13190214Srpaulo * promote products derived from this software without specific prior 14190214Srpaulo * written permission. 15190214Srpaulo * 16190214Srpaulo * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 17190214Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18190214Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19190214Srpaulo * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 20190214Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21190214Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22190214Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23190214Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24190214Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25190214Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26190214Srpaulo * SUCH DAMAGE. 27190214Srpaulo * 28190214Srpaulo * from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $ 29190214Srpaulo * $FreeBSD: head/sys/sys/lock.h 181695 2008-08-13 18:24:22Z attilio $ 30190214Srpaulo */ 31190214Srpaulo 32190214Srpaulo#ifndef _SYS_LOCK_H_ 33190214Srpaulo#define _SYS_LOCK_H_ 34190214Srpaulo 35190214Srpaulo#include <sys/queue.h> 36190214Srpaulo#include <sys/_lock.h> 37190214Srpaulo 38190214Srpaulostruct lock_list_entry; 39190214Srpaulostruct thread; 40190214Srpaulo 41190214Srpaulo/* 42190214Srpaulo * Lock classes. Each lock has a class which describes characteristics 43190214Srpaulo * common to all types of locks of a given class. 44190214Srpaulo * 45190214Srpaulo * 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_unlock)(struct lock_object *lock); 65}; 66 67#define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */ 68#define LC_SPINLOCK 0x00000002 /* Spin lock. */ 69#define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */ 70#define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */ 71#define LC_UPGRADABLE 0x00000010 /* Upgrades and downgrades permitted. */ 72 73#define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */ 74#define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */ 75#define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */ 76#define LO_QUIET 0x00040000 /* Don't log locking operations. */ 77#define LO_RECURSABLE 0x00080000 /* Lock may recurse. */ 78#define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */ 79#define LO_UPGRADABLE 0x00200000 /* Lock may be upgraded/downgraded. */ 80#define LO_DUPOK 0x00400000 /* Don't check for duplicate acquires */ 81#define LO_CLASSMASK 0x0f000000 /* Class index bitmask. */ 82#define LO_NOPROFILE 0x10000000 /* Don't profile this lock */ 83 84/* 85 * Lock classes are statically assigned an index into the gobal lock_classes 86 * array. Debugging code looks up the lock class for a given lock object 87 * by indexing the array. 88 */ 89#define LO_CLASSSHIFT 24 90#define LO_CLASSINDEX(lock) ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT) 91#define LOCK_CLASS(lock) (lock_classes[LO_CLASSINDEX((lock))]) 92#define LOCK_CLASS_MAX (LO_CLASSMASK >> LO_CLASSSHIFT) 93 94/* 95 * Option flags passed to lock operations that witness also needs to know 96 * about or that are generic across all locks. 97 */ 98#define LOP_NEWORDER 0x00000001 /* Define a new lock order. */ 99#define LOP_QUIET 0x00000002 /* Don't log locking operations. */ 100#define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */ 101#define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */ 102#define LOP_DUPOK 0x00000010 /* Don't check for duplicate acquires */ 103 104/* Flags passed to witness_assert. */ 105#define LA_MASKASSERT 0x000000ff /* Mask for witness defined asserts. */ 106#define LA_UNLOCKED 0x00000000 /* Lock is unlocked. */ 107#define LA_LOCKED 0x00000001 /* Lock is at least share locked. */ 108#define LA_SLOCKED 0x00000002 /* Lock is exactly share locked. */ 109#define LA_XLOCKED 0x00000004 /* Lock is exclusively locked. */ 110#define LA_RECURSED 0x00000008 /* Lock is recursed. */ 111#define LA_NOTRECURSED 0x00000010 /* Lock is not recursed. */ 112 113#ifdef _KERNEL 114/* 115 * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled, 116 * then turn on LOCK_DEBUG. When this option is on, extra debugging 117 * facilities such as tracking the file and line number of lock operations 118 * are enabled. Also, mutex locking operations are not inlined to avoid 119 * bloat from all the extra debugging code. We also have to turn on all the 120 * calling conventions for this debugging code in modules so that modules can 121 * work with both debug and non-debug kernels. 122 */ 123#if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || defined(INVARIANT_SUPPORT) || defined(KTR) || defined(LOCK_PROFILING) 124#define LOCK_DEBUG 1 125#else 126#define LOCK_DEBUG 0 127#endif 128 129/* 130 * In the LOCK_DEBUG case, use the filename and line numbers for debugging 131 * operations. Otherwise, use default values to avoid the unneeded bloat. 132 */ 133#if LOCK_DEBUG > 0 134#define LOCK_FILE __FILE__ 135#define LOCK_LINE __LINE__ 136#else 137#define LOCK_FILE NULL 138#define LOCK_LINE 0 139#endif 140 141/* 142 * Macros for KTR_LOCK tracing. 143 * 144 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.) 145 * lo - struct lock_object * for this lock 146 * flags - flags passed to the lock operation 147 * recurse - this locks recursion level (or 0 if class is not recursable) 148 * result - result of a try lock operation 149 * file - file name 150 * line - line number 151 */ 152#define LOCK_LOG_TEST(lo, flags) \ 153 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0) 154 155#define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \ 156 if (LOCK_LOG_TEST((lo), (flags))) \ 157 CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d", \ 158 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \ 159 (u_int)(recurse), (file), (line)); \ 160} while (0) 161 162#define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \ 163 if (LOCK_LOG_TEST((lo), (flags))) \ 164 CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\ 165 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \ 166 (u_int)(result), (file), (line)); \ 167} while (0) 168 169#define LOCK_LOG_INIT(lo, flags) do { \ 170 if (LOCK_LOG_TEST((lo), (flags))) \ 171 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo), \ 172 LOCK_CLASS(lo)->lc_name, (lo)->lo_name); \ 173} while (0) 174 175#define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags) 176 177#define lock_initalized(lo) ((lo)->lo_flags & LO_INITIALIZED) 178 179/* 180 * Helpful macros for quickly coming up with assertions with informative 181 * panic messages. 182 */ 183#define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__) 184#define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__) 185#define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line) 186#define MPASS4(ex, what, file, line) \ 187 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line)) 188 189extern struct lock_class lock_class_mtx_sleep; 190extern struct lock_class lock_class_mtx_spin; 191extern struct lock_class lock_class_sx; 192extern struct lock_class lock_class_rw; 193extern struct lock_class lock_class_rm; 194extern struct lock_class lock_class_lockmgr; 195 196extern struct lock_class *lock_classes[]; 197 198void lock_init(struct lock_object *, struct lock_class *, 199 const char *, const char *, int); 200void lock_destroy(struct lock_object *); 201void spinlock_enter(void); 202void spinlock_exit(void); 203void witness_init(struct lock_object *, const char *); 204void witness_destroy(struct lock_object *); 205int witness_defineorder(struct lock_object *, struct lock_object *); 206void witness_checkorder(struct lock_object *, int, const char *, int); 207void witness_lock(struct lock_object *, int, const char *, int); 208void witness_upgrade(struct lock_object *, int, const char *, int); 209void witness_downgrade(struct lock_object *, int, const char *, int); 210void witness_unlock(struct lock_object *, int, const char *, int); 211void witness_save(struct lock_object *, const char **, int *); 212void witness_restore(struct lock_object *, const char *, int); 213int witness_list_locks(struct lock_list_entry **); 214int witness_warn(int, struct lock_object *, const char *, ...); 215void witness_assert(struct lock_object *, int, const char *, int); 216void witness_display_spinlock(struct lock_object *, struct thread *); 217int witness_line(struct lock_object *); 218const char *witness_file(struct lock_object *); 219void witness_thread_exit(struct thread *); 220 221#ifdef WITNESS 222 223/* Flags for witness_warn(). */ 224#define WARN_GIANTOK 0x01 /* Giant is exempt from this check. */ 225#define WARN_PANIC 0x02 /* Panic if check fails. */ 226#define WARN_SLEEPOK 0x04 /* Sleepable locks are exempt from check. */ 227 228#define WITNESS_INIT(lock, type) \ 229 witness_init((lock), (type)) 230 231#define WITNESS_DESTROY(lock) \ 232 witness_destroy(lock) 233 234#define WITNESS_CHECKORDER(lock, flags, file, line) \ 235 witness_checkorder((lock), (flags), (file), (line)) 236 237#define WITNESS_DEFINEORDER(lock1, lock2) \ 238 witness_defineorder((struct lock_object *)(lock1), \ 239 (struct lock_object *)(lock2)) 240 241#define WITNESS_LOCK(lock, flags, file, line) \ 242 witness_lock((lock), (flags), (file), (line)) 243 244#define WITNESS_UPGRADE(lock, flags, file, line) \ 245 witness_upgrade((lock), (flags), (file), (line)) 246 247#define WITNESS_DOWNGRADE(lock, flags, file, line) \ 248 witness_downgrade((lock), (flags), (file), (line)) 249 250#define WITNESS_UNLOCK(lock, flags, file, line) \ 251 witness_unlock((lock), (flags), (file), (line)) 252 253#define WITNESS_CHECK(flags, lock, fmt, ...) \ 254 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__) 255 256#define WITNESS_WARN(flags, lock, fmt, ...) \ 257 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__) 258 259#define WITNESS_SAVE_DECL(n) \ 260 const char * __CONCAT(n, __wf); \ 261 int __CONCAT(n, __wl) 262 263#define WITNESS_SAVE(lock, n) \ 264 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl)) 265 266#define WITNESS_RESTORE(lock, n) \ 267 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl)) 268 269#define WITNESS_FILE(lock) \ 270 witness_file(lock) 271 272#define WITNESS_LINE(lock) \ 273 witness_line(lock) 274 275#else /* WITNESS */ 276#define WITNESS_INIT(lock, type) 277#define WITNESS_DESTROY(lock) 278#define WITNESS_DEFINEORDER(lock1, lock2) 0 279#define WITNESS_CHECKORDER(lock, flags, file, line) 280#define WITNESS_LOCK(lock, flags, file, line) 281#define WITNESS_UPGRADE(lock, flags, file, line) 282#define WITNESS_DOWNGRADE(lock, flags, file, line) 283#define WITNESS_UNLOCK(lock, flags, file, line) 284#define WITNESS_CHECK(flags, lock, fmt, ...) 0 285#define WITNESS_WARN(flags, lock, fmt, ...) 286#define WITNESS_SAVE_DECL(n) 287#define WITNESS_SAVE(lock, n) 288#define WITNESS_RESTORE(lock, n) 289#define WITNESS_FILE(lock) ("?") 290#define WITNESS_LINE(lock) (0) 291#endif /* WITNESS */ 292 293/* 294 * Helper macros to allow developers to add explicit lock order checks 295 * wherever they please without having to actually grab a lock to do so. 296 */ 297#define witness_check(l) \ 298 WITNESS_CHECKORDER(&(l)->lock_object, LOP_EXCLUSIVE, LOCK_FILE, \ 299 LOCK_LINE) 300 301#define witness_check_shared(l) \ 302 WITNESS_CHECKORDER(&(l)->lock_object, 0, LOCK_FILE, LOCK_LINE) 303 304#endif /* _KERNEL */ 305#endif /* _SYS_LOCK_H_ */ 306