1 /* $NetBSD: kern_lock.c,v 1.26 2000/02/09 16:46:09 sommerfeld Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * This code is derived from software contributed to The NetBSD Foundation 12 * by Ross Harvey. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the NetBSD 25 * Foundation, Inc. and its contributors. 26 * 4. Neither the name of The NetBSD Foundation nor the names of its 27 * contributors may be used to endorse or promote products derived 28 * from this software without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 * POSSIBILITY OF SUCH DAMAGE. 41 */ 42 43 /* 44 * Copyright (c) 1995 45 * The Regents of the University of California. All rights reserved. 46 * 47 * This code contains ideas from software contributed to Berkeley by 48 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating 49 * System project at Carnegie-Mellon University. 50 * 51 * Redistribution and use in source and binary forms, with or without 52 * modification, are permitted provided that the following conditions 53 * are met: 54 * 1. Redistributions of source code must retain the above copyright 55 * notice, this list of conditions and the following disclaimer. 56 * 2. Redistributions in binary form must reproduce the above copyright 57 * notice, this list of conditions and the following disclaimer in the 58 * documentation and/or other materials provided with the distribution. 59 * 3. All advertising materials mentioning features or use of this software 60 * must display the following acknowledgement: 61 * This product includes software developed by the University of 62 * California, Berkeley and its contributors. 63 * 4. Neither the name of the University nor the names of its contributors 64 * may be used to endorse or promote products derived from this software 65 * without specific prior written permission. 66 * 67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 77 * SUCH DAMAGE. 78 * 79 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95 80 */ 81 82 #include "opt_multiprocessor.h" 83 #include "opt_lockdebug.h" 84 #include "opt_ddb.h" 85 86 #include <sys/param.h> 87 #include <sys/proc.h> 88 #include <sys/lock.h> 89 #include <sys/systm.h> 90 #include <machine/cpu.h> 91 92 #if defined(LOCKDEBUG) 93 #include <sys/syslog.h> 94 /* 95 * note that stdarg.h and the ansi style va_start macro is used for both 96 * ansi and traditional c compiles. 97 * XXX: this requires that stdarg.h define: va_alist and va_dcl 98 */ 99 #include <machine/stdarg.h> 100 101 void lock_printf __P((const char *fmt, ...)); 102 103 int lock_debug_syslog = 0; /* defaults to printf, but can be patched */ 104 #endif 105 106 /* 107 * Locking primitives implementation. 108 * Locks provide shared/exclusive sychronization. 109 */ 110 111 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */ 112 #if defined(MULTIPROCESSOR) /* { */ 113 #define COUNT_CPU(cpu_id, x) \ 114 /* atomic_add_ulong(&curcpu().ci_spin_locks, (x)) */ 115 #else 116 u_long spin_locks; 117 #define COUNT_CPU(cpu_id, x) spin_locks += (x) 118 #endif /* MULTIPROCESSOR */ /* } */ 119 120 #define COUNT(lkp, p, cpu_id, x) \ 121 do { \ 122 if ((lkp)->lk_flags & LK_SPIN) \ 123 COUNT_CPU((cpu_id), (x)); \ 124 else \ 125 (p)->p_locks += (x); \ 126 } while (0) 127 #else 128 #define COUNT(lkp, p, cpu_id, x) 129 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */ 130 131 /* 132 * Acquire a resource. 133 */ 134 #define ACQUIRE(lkp, error, extflags, drain, wanted) \ 135 if ((extflags) & LK_SPIN) { \ 136 int interlocked; \ 137 \ 138 if ((drain) == 0) \ 139 (lkp)->lk_waitcount++; \ 140 for (interlocked = 1;;) { \ 141 if (wanted) { \ 142 if (interlocked) { \ 143 simple_unlock(&(lkp)->lk_interlock); \ 144 interlocked = 0; \ 145 } \ 146 } else if (interlocked) { \ 147 break; \ 148 } else { \ 149 simple_lock(&(lkp)->lk_interlock); \ 150 interlocked = 1; \ 151 } \ 152 } \ 153 if ((drain) == 0) \ 154 (lkp)->lk_waitcount--; \ 155 KASSERT((wanted) == 0); \ 156 error = 0; /* sanity */ \ 157 } else { \ 158 for (error = 0; wanted; ) { \ 159 if ((drain)) \ 160 (lkp)->lk_flags |= LK_WAITDRAIN; \ 161 else \ 162 (lkp)->lk_waitcount++; \ 163 simple_unlock(&(lkp)->lk_interlock); \ 164 /* XXX Cast away volatile. */ \ 165 error = tsleep((drain) ? &(lkp)->lk_flags : \ 166 (void *)(lkp), (lkp)->lk_prio, \ 167 (lkp)->lk_wmesg, (lkp)->lk_timo); \ 168 simple_lock(&(lkp)->lk_interlock); \ 169 if ((drain) == 0) \ 170 (lkp)->lk_waitcount--; \ 171 if (error) \ 172 break; \ 173 if ((extflags) & LK_SLEEPFAIL) { \ 174 error = ENOLCK; \ 175 break; \ 176 } \ 177 } \ 178 } 179 180 #define SETHOLDER(lkp, pid, cpu_id) \ 181 do { \ 182 if ((lkp)->lk_flags & LK_SPIN) \ 183 (lkp)->lk_cpu = cpu_id; \ 184 else \ 185 (lkp)->lk_lockholder = pid; \ 186 } while (0) 187 188 #define WEHOLDIT(lkp, pid, cpu_id) \ 189 (((lkp)->lk_flags & LK_SPIN) != 0 ? \ 190 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid))) 191 192 #define WAKEUP_WAITER(lkp) \ 193 do { \ 194 if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) { \ 195 /* XXX Cast away volatile. */ \ 196 wakeup_one((void *)(lkp)); \ 197 } \ 198 } while (0) 199 200 #if defined(LOCKDEBUG) /* { */ 201 #if defined(MULTIPROCESSOR) /* { */ 202 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER; 203 204 #define SPINLOCK_LIST_LOCK() cpu_simple_lock(&spinlock_list_slock) 205 206 #define SPINLOCK_LIST_UNLOCK() cpu_simple_unlock(&spinlock_list_slock) 207 #else 208 #define SPINLOCK_LIST_LOCK() /* nothing */ 209 210 #define SPINLOCK_LIST_UNLOCK() /* nothing */ 211 #endif /* MULTIPROCESSOR */ /* } */ 212 213 TAILQ_HEAD(, lock) spinlock_list = 214 TAILQ_HEAD_INITIALIZER(spinlock_list); 215 216 #define HAVEIT(lkp) \ 217 do { \ 218 if ((lkp)->lk_flags & LK_SPIN) { \ 219 int s = splhigh(); \ 220 SPINLOCK_LIST_LOCK(); \ 221 /* XXX Cast away volatile. */ \ 222 TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp), \ 223 lk_list); \ 224 SPINLOCK_LIST_UNLOCK(); \ 225 splx(s); \ 226 } \ 227 } while (0) 228 229 #define DONTHAVEIT(lkp) \ 230 do { \ 231 if ((lkp)->lk_flags & LK_SPIN) { \ 232 int s = splhigh(); \ 233 SPINLOCK_LIST_LOCK(); \ 234 /* XXX Cast away volatile. */ \ 235 TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp), \ 236 lk_list); \ 237 SPINLOCK_LIST_UNLOCK(); \ 238 splx(s); \ 239 } \ 240 } while (0) 241 #else 242 #define HAVEIT(lkp) /* nothing */ 243 244 #define DONTHAVEIT(lkp) /* nothing */ 245 #endif /* LOCKDEBUG */ /* } */ 246 247 #if defined(LOCKDEBUG) 248 /* 249 * Lock debug printing routine; can be configured to print to console 250 * or log to syslog. 251 */ 252 void 253 #ifdef __STDC__ 254 lock_printf(const char *fmt, ...) 255 #else 256 lock_printf(fmt, va_alist) 257 char *fmt; 258 va_dcl 259 #endif 260 { 261 va_list ap; 262 263 va_start(ap, fmt); 264 if (lock_debug_syslog) 265 vlog(LOG_DEBUG, fmt, ap); 266 else 267 vprintf(fmt, ap); 268 va_end(ap); 269 } 270 #endif /* LOCKDEBUG */ 271 272 /* 273 * Initialize a lock; required before use. 274 */ 275 void 276 lockinit(lkp, prio, wmesg, timo, flags) 277 struct lock *lkp; 278 int prio; 279 const char *wmesg; 280 int timo; 281 int flags; 282 { 283 284 memset(lkp, 0, sizeof(struct lock)); 285 simple_lock_init(&lkp->lk_interlock); 286 lkp->lk_flags = flags & LK_EXTFLG_MASK; 287 if (flags & LK_SPIN) 288 lkp->lk_cpu = LK_NOCPU; 289 else { 290 lkp->lk_lockholder = LK_NOPROC; 291 lkp->lk_prio = prio; 292 lkp->lk_timo = timo; 293 } 294 lkp->lk_wmesg = wmesg; /* just a name for spin locks */ 295 } 296 297 /* 298 * Determine the status of a lock. 299 */ 300 int 301 lockstatus(lkp) 302 struct lock *lkp; 303 { 304 int lock_type = 0; 305 306 simple_lock(&lkp->lk_interlock); 307 if (lkp->lk_exclusivecount != 0) 308 lock_type = LK_EXCLUSIVE; 309 else if (lkp->lk_sharecount != 0) 310 lock_type = LK_SHARED; 311 simple_unlock(&lkp->lk_interlock); 312 return (lock_type); 313 } 314 315 /* 316 * Set, change, or release a lock. 317 * 318 * Shared requests increment the shared count. Exclusive requests set the 319 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already 320 * accepted shared locks and shared-to-exclusive upgrades to go away. 321 */ 322 int 323 lockmgr(lkp, flags, interlkp) 324 __volatile struct lock *lkp; 325 u_int flags; 326 struct simplelock *interlkp; 327 { 328 int error; 329 pid_t pid; 330 int extflags; 331 cpuid_t cpu_id; 332 struct proc *p = curproc; 333 334 error = 0; 335 336 simple_lock(&lkp->lk_interlock); 337 if (flags & LK_INTERLOCK) 338 simple_unlock(interlkp); 339 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK; 340 341 #ifdef DIAGNOSTIC /* { */ 342 /* 343 * Don't allow spins on sleep locks and don't allow sleeps 344 * on spin locks. 345 */ 346 if ((flags ^ lkp->lk_flags) & LK_SPIN) 347 panic("lockmgr: sleep/spin mismatch\n"); 348 #endif /* } */ 349 350 if (extflags & LK_SPIN) 351 pid = LK_KERNPROC; 352 else { 353 #ifdef DIAGNOSTIC /* { */ 354 if (p == NULL) 355 panic("lockmgr: no context"); 356 #endif /* } */ 357 pid = p->p_pid; 358 } 359 cpu_id = cpu_number(); 360 361 #ifdef DIAGNOSTIC /* { */ 362 /* 363 * Once a lock has drained, the LK_DRAINING flag is set and an 364 * exclusive lock is returned. The only valid operation thereafter 365 * is a single release of that exclusive lock. This final release 366 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any 367 * further requests of any sort will result in a panic. The bits 368 * selected for these two flags are chosen so that they will be set 369 * in memory that is freed (freed memory is filled with 0xdeadbeef). 370 * The final release is permitted to give a new lease on life to 371 * the lock by specifying LK_REENABLE. 372 */ 373 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) { 374 if (lkp->lk_flags & LK_DRAINED) 375 panic("lockmgr: using decommissioned lock"); 376 if ((flags & LK_TYPE_MASK) != LK_RELEASE || 377 WEHOLDIT(lkp, pid, cpu_id) == 0) 378 panic("lockmgr: non-release on draining lock: %d\n", 379 flags & LK_TYPE_MASK); 380 lkp->lk_flags &= ~LK_DRAINING; 381 if ((flags & LK_REENABLE) == 0) 382 lkp->lk_flags |= LK_DRAINED; 383 } 384 #endif /* DIAGNOSTIC */ /* } */ 385 386 switch (flags & LK_TYPE_MASK) { 387 388 case LK_SHARED: 389 if (WEHOLDIT(lkp, pid, cpu_id) == 0) { 390 /* 391 * If just polling, check to see if we will block. 392 */ 393 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & 394 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) { 395 error = EBUSY; 396 break; 397 } 398 /* 399 * Wait for exclusive locks and upgrades to clear. 400 */ 401 ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags & 402 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)); 403 if (error) 404 break; 405 lkp->lk_sharecount++; 406 COUNT(lkp, p, cpu_id, 1); 407 break; 408 } 409 /* 410 * We hold an exclusive lock, so downgrade it to shared. 411 * An alternative would be to fail with EDEADLK. 412 */ 413 lkp->lk_sharecount++; 414 COUNT(lkp, p, cpu_id, 1); 415 /* fall into downgrade */ 416 417 case LK_DOWNGRADE: 418 if (WEHOLDIT(lkp, pid, cpu_id) == 0 || 419 lkp->lk_exclusivecount == 0) 420 panic("lockmgr: not holding exclusive lock"); 421 lkp->lk_sharecount += lkp->lk_exclusivecount; 422 lkp->lk_exclusivecount = 0; 423 lkp->lk_recurselevel = 0; 424 lkp->lk_flags &= ~LK_HAVE_EXCL; 425 SETHOLDER(lkp, LK_NOPROC, LK_NOCPU); 426 DONTHAVEIT(lkp); 427 WAKEUP_WAITER(lkp); 428 break; 429 430 case LK_EXCLUPGRADE: 431 /* 432 * If another process is ahead of us to get an upgrade, 433 * then we want to fail rather than have an intervening 434 * exclusive access. 435 */ 436 if (lkp->lk_flags & LK_WANT_UPGRADE) { 437 lkp->lk_sharecount--; 438 COUNT(lkp, p, cpu_id, -1); 439 error = EBUSY; 440 break; 441 } 442 /* fall into normal upgrade */ 443 444 case LK_UPGRADE: 445 /* 446 * Upgrade a shared lock to an exclusive one. If another 447 * shared lock has already requested an upgrade to an 448 * exclusive lock, our shared lock is released and an 449 * exclusive lock is requested (which will be granted 450 * after the upgrade). If we return an error, the file 451 * will always be unlocked. 452 */ 453 if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0) 454 panic("lockmgr: upgrade exclusive lock"); 455 lkp->lk_sharecount--; 456 COUNT(lkp, p, cpu_id, -1); 457 /* 458 * If we are just polling, check to see if we will block. 459 */ 460 if ((extflags & LK_NOWAIT) && 461 ((lkp->lk_flags & LK_WANT_UPGRADE) || 462 lkp->lk_sharecount > 1)) { 463 error = EBUSY; 464 break; 465 } 466 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) { 467 /* 468 * We are first shared lock to request an upgrade, so 469 * request upgrade and wait for the shared count to 470 * drop to zero, then take exclusive lock. 471 */ 472 lkp->lk_flags |= LK_WANT_UPGRADE; 473 ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount); 474 lkp->lk_flags &= ~LK_WANT_UPGRADE; 475 if (error) 476 break; 477 lkp->lk_flags |= LK_HAVE_EXCL; 478 SETHOLDER(lkp, pid, cpu_id); 479 HAVEIT(lkp); 480 if (lkp->lk_exclusivecount != 0) 481 panic("lockmgr: non-zero exclusive count"); 482 lkp->lk_exclusivecount = 1; 483 if (extflags & LK_SETRECURSE) 484 lkp->lk_recurselevel = 1; 485 COUNT(lkp, p, cpu_id, 1); 486 break; 487 } 488 /* 489 * Someone else has requested upgrade. Release our shared 490 * lock, awaken upgrade requestor if we are the last shared 491 * lock, then request an exclusive lock. 492 */ 493 if (lkp->lk_sharecount == 0) 494 WAKEUP_WAITER(lkp); 495 /* fall into exclusive request */ 496 497 case LK_EXCLUSIVE: 498 if (WEHOLDIT(lkp, pid, cpu_id)) { 499 /* 500 * Recursive lock. 501 */ 502 if ((extflags & LK_CANRECURSE) == 0 && 503 lkp->lk_recurselevel == 0) { 504 if (extflags & LK_RECURSEFAIL) { 505 error = EDEADLK; 506 break; 507 } else 508 panic("lockmgr: locking against myself"); 509 } 510 lkp->lk_exclusivecount++; 511 if (extflags & LK_SETRECURSE && 512 lkp->lk_recurselevel == 0) 513 lkp->lk_recurselevel = lkp->lk_exclusivecount; 514 COUNT(lkp, p, cpu_id, 1); 515 break; 516 } 517 /* 518 * If we are just polling, check to see if we will sleep. 519 */ 520 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags & 521 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) || 522 lkp->lk_sharecount != 0)) { 523 error = EBUSY; 524 break; 525 } 526 /* 527 * Try to acquire the want_exclusive flag. 528 */ 529 ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags & 530 (LK_HAVE_EXCL | LK_WANT_EXCL)); 531 if (error) 532 break; 533 lkp->lk_flags |= LK_WANT_EXCL; 534 /* 535 * Wait for shared locks and upgrades to finish. 536 */ 537 ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 || 538 (lkp->lk_flags & LK_WANT_UPGRADE)); 539 lkp->lk_flags &= ~LK_WANT_EXCL; 540 if (error) 541 break; 542 lkp->lk_flags |= LK_HAVE_EXCL; 543 SETHOLDER(lkp, pid, cpu_id); 544 HAVEIT(lkp); 545 if (lkp->lk_exclusivecount != 0) 546 panic("lockmgr: non-zero exclusive count"); 547 lkp->lk_exclusivecount = 1; 548 if (extflags & LK_SETRECURSE) 549 lkp->lk_recurselevel = 1; 550 COUNT(lkp, p, cpu_id, 1); 551 break; 552 553 case LK_RELEASE: 554 if (lkp->lk_exclusivecount != 0) { 555 if (WEHOLDIT(lkp, pid, cpu_id) == 0) { 556 if (lkp->lk_flags & LK_SPIN) { 557 panic("lockmgr: processor %lu, not " 558 "exclusive lock holder %lu " 559 "unlocking", cpu_id, lkp->lk_cpu); 560 } else { 561 panic("lockmgr: pid %d, not " 562 "exclusive lock holder %d " 563 "unlocking", pid, 564 lkp->lk_lockholder); 565 } 566 } 567 if (lkp->lk_exclusivecount == lkp->lk_recurselevel) 568 lkp->lk_recurselevel = 0; 569 lkp->lk_exclusivecount--; 570 COUNT(lkp, p, cpu_id, -1); 571 if (lkp->lk_exclusivecount == 0) { 572 lkp->lk_flags &= ~LK_HAVE_EXCL; 573 SETHOLDER(lkp, LK_NOPROC, LK_NOCPU); 574 DONTHAVEIT(lkp); 575 } 576 } else if (lkp->lk_sharecount != 0) { 577 lkp->lk_sharecount--; 578 COUNT(lkp, p, cpu_id, -1); 579 } 580 WAKEUP_WAITER(lkp); 581 break; 582 583 case LK_DRAIN: 584 /* 585 * Check that we do not already hold the lock, as it can 586 * never drain if we do. Unfortunately, we have no way to 587 * check for holding a shared lock, but at least we can 588 * check for an exclusive one. 589 */ 590 if (WEHOLDIT(lkp, pid, cpu_id)) 591 panic("lockmgr: draining against myself"); 592 /* 593 * If we are just polling, check to see if we will sleep. 594 */ 595 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags & 596 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) || 597 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) { 598 error = EBUSY; 599 break; 600 } 601 ACQUIRE(lkp, error, extflags, 1, 602 ((lkp->lk_flags & 603 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) || 604 lkp->lk_sharecount != 0 || 605 lkp->lk_waitcount != 0)); 606 if (error) 607 break; 608 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL; 609 SETHOLDER(lkp, pid, cpu_id); 610 HAVEIT(lkp); 611 lkp->lk_exclusivecount = 1; 612 /* XXX unlikely that we'd want this */ 613 if (extflags & LK_SETRECURSE) 614 lkp->lk_recurselevel = 1; 615 COUNT(lkp, p, cpu_id, 1); 616 break; 617 618 default: 619 simple_unlock(&lkp->lk_interlock); 620 panic("lockmgr: unknown locktype request %d", 621 flags & LK_TYPE_MASK); 622 /* NOTREACHED */ 623 } 624 if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN && 625 ((lkp->lk_flags & 626 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 && 627 lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) { 628 lkp->lk_flags &= ~LK_WAITDRAIN; 629 wakeup_one((void *)&lkp->lk_flags); 630 } 631 simple_unlock(&lkp->lk_interlock); 632 return (error); 633 } 634 635 /* 636 * Print out information about state of a lock. Used by VOP_PRINT 637 * routines to display ststus about contained locks. 638 */ 639 void 640 lockmgr_printinfo(lkp) 641 __volatile struct lock *lkp; 642 { 643 644 if (lkp->lk_sharecount) 645 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg, 646 lkp->lk_sharecount); 647 else if (lkp->lk_flags & LK_HAVE_EXCL) { 648 printf(" lock type %s: EXCL (count %d) by ", 649 lkp->lk_wmesg, lkp->lk_exclusivecount); 650 if (lkp->lk_flags & LK_SPIN) 651 printf("processor %lu", lkp->lk_cpu); 652 else 653 printf("pid %d", lkp->lk_lockholder); 654 } else 655 printf(" not locked"); 656 if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0) 657 printf(" with %d pending", lkp->lk_waitcount); 658 } 659 660 #if defined(LOCKDEBUG) /* { */ 661 TAILQ_HEAD(, simplelock) simplelock_list = 662 TAILQ_HEAD_INITIALIZER(simplelock_list); 663 664 #if defined(MULTIPROCESSOR) /* { */ 665 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER; 666 667 #define SLOCK_LIST_LOCK() \ 668 cpu_simple_lock(&simplelock_list_slock) 669 670 #define SLOCK_LIST_UNLOCK() \ 671 cpu_simple_unlock(&simplelock_list_slock) 672 673 #define SLOCK_COUNT(x) \ 674 /* atomic_add_ulong(&curcpu()->ci_simple_locks, (x)) */ 675 #else 676 u_long simple_locks; 677 678 #define SLOCK_LIST_LOCK() /* nothing */ 679 680 #define SLOCK_LIST_UNLOCK() /* nothing */ 681 682 #define SLOCK_COUNT(x) simple_locks += (x) 683 #endif /* MULTIPROCESSOR */ /* } */ 684 685 #ifdef DDB /* { */ 686 int simple_lock_debugger = 0; 687 #define SLOCK_DEBUGGER() if (simple_lock_debugger) Debugger() 688 #else 689 #define SLOCK_DEBUGGER() /* nothing */ 690 #endif /* } */ 691 692 #ifdef MULTIPROCESSOR 693 #define SLOCK_MP() lock_printf("on cpu %d\n", cpu_number()) 694 #else 695 #define SLOCK_MP() /* nothing */ 696 #endif 697 698 #define SLOCK_WHERE(str, alp, id, l) \ 699 do { \ 700 lock_printf(str); \ 701 lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \ 702 SLOCK_MP(); \ 703 if ((alp)->lock_file != NULL) \ 704 lock_printf("last locked: %s:%d\n", (alp)->lock_file, \ 705 (alp)->lock_line); \ 706 if ((alp)->unlock_file != NULL) \ 707 lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \ 708 (alp)->unlock_line); \ 709 SLOCK_DEBUGGER(); \ 710 } while (0) 711 712 /* 713 * Simple lock functions so that the debugger can see from whence 714 * they are being called. 715 */ 716 void 717 simple_lock_init(alp) 718 struct simplelock *alp; 719 { 720 721 #if defined(MULTIPROCESSOR) /* { */ 722 cpu_simple_lock_init(alp); 723 #else 724 alp->lock_data = SIMPLELOCK_UNLOCKED; 725 #endif /* } */ 726 alp->lock_file = NULL; 727 alp->lock_line = 0; 728 alp->unlock_file = NULL; 729 alp->unlock_line = 0; 730 alp->lock_holder = 0; 731 } 732 733 void 734 _simple_lock(alp, id, l) 735 __volatile struct simplelock *alp; 736 const char *id; 737 int l; 738 { 739 cpuid_t cpu_id = cpu_number(); 740 int s; 741 742 s = splhigh(); 743 744 /* 745 * MULTIPROCESSOR case: This is `safe' since if it's not us, we 746 * don't take any action, and just fall into the normal spin case. 747 */ 748 if (alp->lock_data == SIMPLELOCK_LOCKED) { 749 #if defined(MULTIPROCESSOR) /* { */ 750 if (alp->lock_holder == cpu_id) { 751 SLOCK_WHERE("simple_lock: locking against myself\n", 752 alp, id, l); 753 goto out; 754 } 755 #else 756 SLOCK_WHERE("simple_lock: lock held\n", alp, id, l); 757 goto out; 758 #endif /* MULTIPROCESSOR */ /* } */ 759 } 760 761 #if defined(MULTIPROCESSOR) /* { */ 762 /* Acquire the lock before modifying any fields. */ 763 cpu_simple_lock(alp); 764 #else 765 alp->lock_data = SIMPLELOCK_LOCKED; 766 #endif /* } */ 767 768 alp->lock_file = id; 769 alp->lock_line = l; 770 alp->lock_holder = cpu_id; 771 772 SLOCK_LIST_LOCK(); 773 /* XXX Cast away volatile */ 774 TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list); 775 SLOCK_LIST_UNLOCK(); 776 777 SLOCK_COUNT(1); 778 779 out: 780 splx(s); 781 } 782 783 int 784 _simple_lock_try(alp, id, l) 785 __volatile struct simplelock *alp; 786 const char *id; 787 int l; 788 { 789 cpuid_t cpu_id = cpu_number(); 790 int s, rv = 0; 791 792 s = splhigh(); 793 794 /* 795 * MULTIPROCESSOR case: This is `safe' since if it's not us, we 796 * don't take any action. 797 */ 798 #if defined(MULTIPROCESSOR) /* { */ 799 if ((rv = cpu_simple_lock_try(alp)) == 0) { 800 if (alp->lock_holder == cpu_id) 801 SLOCK_WHERE("simple_lock_try: locking against myself\n", 802 alp, id, l); 803 goto out; 804 } 805 #else 806 if (alp->lock_data == SIMPLELOCK_LOCKED) { 807 SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l); 808 goto out; 809 } 810 alp->lock_data = SIMPLELOCK_LOCKED; 811 #endif /* MULTIPROCESSOR */ /* } */ 812 813 /* 814 * At this point, we have acquired the lock. 815 */ 816 817 rv = 1; 818 819 alp->lock_file = id; 820 alp->lock_line = l; 821 alp->lock_holder = cpu_id; 822 823 SLOCK_LIST_LOCK(); 824 /* XXX Cast away volatile. */ 825 TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list); 826 SLOCK_LIST_UNLOCK(); 827 828 SLOCK_COUNT(1); 829 830 out: 831 splx(s); 832 return (rv); 833 } 834 835 void 836 _simple_unlock(alp, id, l) 837 __volatile struct simplelock *alp; 838 const char *id; 839 int l; 840 { 841 int s; 842 843 s = splhigh(); 844 845 /* 846 * MULTIPROCESSOR case: This is `safe' because we think we hold 847 * the lock, and if we don't, we don't take any action. 848 */ 849 if (alp->lock_data == SIMPLELOCK_UNLOCKED) { 850 SLOCK_WHERE("simple_unlock: lock not held\n", 851 alp, id, l); 852 goto out; 853 } 854 855 SLOCK_LIST_LOCK(); 856 TAILQ_REMOVE(&simplelock_list, alp, list); 857 SLOCK_LIST_UNLOCK(); 858 859 SLOCK_COUNT(-1); 860 861 alp->list.tqe_next = NULL; /* sanity */ 862 alp->list.tqe_prev = NULL; /* sanity */ 863 864 alp->unlock_file = id; 865 alp->unlock_line = l; 866 867 #if defined(MULTIPROCESSOR) /* { */ 868 alp->lock_holder = LK_NOCPU; 869 /* Now that we've modified all fields, release the lock. */ 870 cpu_simple_unlock(alp); 871 #else 872 alp->lock_data = SIMPLELOCK_UNLOCKED; 873 #endif /* } */ 874 875 out: 876 splx(s); 877 } 878 879 void 880 simple_lock_dump() 881 { 882 struct simplelock *alp; 883 int s; 884 885 s = splhigh(); 886 SLOCK_LIST_LOCK(); 887 lock_printf("all simple locks:\n"); 888 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL; 889 alp = TAILQ_NEXT(alp, list)) { 890 lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder, 891 alp->lock_file, alp->lock_line); 892 } 893 SLOCK_LIST_UNLOCK(); 894 splx(s); 895 } 896 897 void 898 simple_lock_freecheck(start, end) 899 void *start, *end; 900 { 901 struct simplelock *alp; 902 int s; 903 904 s = splhigh(); 905 SLOCK_LIST_LOCK(); 906 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL; 907 alp = TAILQ_NEXT(alp, list)) { 908 if ((void *)alp >= start && (void *)alp < end) { 909 lock_printf("freeing simple_lock %p CPU %lu %s:%d\n", 910 alp, alp->lock_holder, alp->lock_file, 911 alp->lock_line); 912 SLOCK_DEBUGGER(); 913 } 914 } 915 SLOCK_LIST_UNLOCK(); 916 splx(s); 917 } 918 #endif /* LOCKDEBUG */ /* } */ 919