1 /* 2 * Copyright (c) 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Copyright (C) 1997 6 * John S. Dyson. All rights reserved. 7 * 8 * This code contains ideas from software contributed to Berkeley by 9 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating 10 * System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95 41 * $FreeBSD$ 42 */ 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/ktr.h> 47 #include <sys/lock.h> 48 #include <sys/lockmgr.h> 49 #include <sys/mutex.h> 50 #include <sys/proc.h> 51 #include <sys/systm.h> 52 53 /* 54 * Locking primitives implementation. 55 * Locks provide shared/exclusive sychronization. 56 */ 57 58 #define LOCK_WAIT_TIME 100 59 #define LOCK_SAMPLE_WAIT 7 60 61 #if defined(DIAGNOSTIC) 62 #define LOCK_INLINE 63 #else 64 #define LOCK_INLINE __inline 65 #endif 66 67 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \ 68 LK_SHARE_NONZERO | LK_WAIT_NONZERO) 69 70 /* 71 * Mutex array variables. Rather than each lockmgr lock having its own mutex, 72 * share a fixed (at boot time) number of mutexes across all lockmgr locks in 73 * order to keep sizeof(struct lock) down. 74 */ 75 int lock_mtx_valid; 76 static struct mtx lock_mtx; 77 78 static int acquire(struct lock **lkpp, int extflags, int wanted); 79 static int apause(struct lock *lkp, int flags); 80 static int acquiredrain(struct lock *lkp, int extflags) ; 81 82 static void 83 lockmgr_init(void *dummy __unused) 84 { 85 /* 86 * Initialize the lockmgr protection mutex if it hasn't already been 87 * done. Unless something changes about kernel startup order, VM 88 * initialization will always cause this mutex to already be 89 * initialized in a call to lockinit(). 90 */ 91 if (lock_mtx_valid == 0) { 92 mtx_init(&lock_mtx, "lockmgr", NULL, MTX_DEF); 93 lock_mtx_valid = 1; 94 } 95 } 96 SYSINIT(lmgrinit, SI_SUB_LOCK, SI_ORDER_FIRST, lockmgr_init, NULL) 97 98 static LOCK_INLINE void 99 sharelock(struct lock *lkp, int incr) { 100 lkp->lk_flags |= LK_SHARE_NONZERO; 101 lkp->lk_sharecount += incr; 102 } 103 104 static LOCK_INLINE void 105 shareunlock(struct lock *lkp, int decr) { 106 107 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr")); 108 109 if (lkp->lk_sharecount == decr) { 110 lkp->lk_flags &= ~LK_SHARE_NONZERO; 111 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) { 112 wakeup(lkp); 113 } 114 lkp->lk_sharecount = 0; 115 } else { 116 lkp->lk_sharecount -= decr; 117 } 118 } 119 120 /* 121 * This is the waitloop optimization. 122 */ 123 static int 124 apause(struct lock *lkp, int flags) 125 { 126 #ifdef SMP 127 int i, lock_wait; 128 #endif 129 130 if ((lkp->lk_flags & flags) == 0) 131 return 0; 132 #ifdef SMP 133 for (lock_wait = LOCK_WAIT_TIME; lock_wait > 0; lock_wait--) { 134 mtx_unlock(lkp->lk_interlock); 135 for (i = LOCK_SAMPLE_WAIT; i > 0; i--) 136 if ((lkp->lk_flags & flags) == 0) 137 break; 138 mtx_lock(lkp->lk_interlock); 139 if ((lkp->lk_flags & flags) == 0) 140 return 0; 141 } 142 #endif 143 return 1; 144 } 145 146 static int 147 acquire(struct lock **lkpp, int extflags, int wanted) { 148 struct lock *lkp = *lkpp; 149 int s, error; 150 151 CTR3(KTR_LOCKMGR, 152 "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x\n", 153 lkp, extflags, wanted); 154 155 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) { 156 return EBUSY; 157 } 158 159 if (((lkp->lk_flags | extflags) & LK_NOPAUSE) == 0) { 160 error = apause(lkp, wanted); 161 if (error == 0) 162 return 0; 163 } 164 165 s = splhigh(); 166 while ((lkp->lk_flags & wanted) != 0) { 167 lkp->lk_flags |= LK_WAIT_NONZERO; 168 lkp->lk_waitcount++; 169 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio, 170 lkp->lk_wmesg, 171 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0)); 172 if (lkp->lk_waitcount == 1) { 173 lkp->lk_flags &= ~LK_WAIT_NONZERO; 174 lkp->lk_waitcount = 0; 175 } else { 176 lkp->lk_waitcount--; 177 } 178 if (error) { 179 splx(s); 180 return error; 181 } 182 if (extflags & LK_SLEEPFAIL) { 183 splx(s); 184 return ENOLCK; 185 } 186 if (lkp->lk_newlock != NULL) { 187 mtx_lock(lkp->lk_newlock->lk_interlock); 188 mtx_unlock(lkp->lk_interlock); 189 if (lkp->lk_waitcount == 0) 190 wakeup((void *)(&lkp->lk_newlock)); 191 *lkpp = lkp = lkp->lk_newlock; 192 } 193 } 194 splx(s); 195 return 0; 196 } 197 198 /* 199 * Set, change, or release a lock. 200 * 201 * Shared requests increment the shared count. Exclusive requests set the 202 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already 203 * accepted shared locks and shared-to-exclusive upgrades to go away. 204 */ 205 int 206 #ifndef DEBUG_LOCKS 207 lockmgr(lkp, flags, interlkp, td) 208 #else 209 debuglockmgr(lkp, flags, interlkp, td, name, file, line) 210 #endif 211 struct lock *lkp; 212 u_int flags; 213 struct mtx *interlkp; 214 struct thread *td; 215 #ifdef DEBUG_LOCKS 216 const char *name; /* Name of lock function */ 217 const char *file; /* Name of file call is from */ 218 int line; /* Line number in file */ 219 #endif 220 { 221 int error; 222 struct thread *thr; 223 int extflags, lockflags; 224 225 CTR5(KTR_LOCKMGR, 226 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), flags == 0x%x, " 227 "interlkp == %p, td == %p", lkp, lkp->lk_wmesg, flags, interlkp, td); 228 229 error = 0; 230 if (td == NULL) 231 thr = LK_KERNPROC; 232 else 233 thr = td; 234 235 if ((flags & LK_INTERNAL) == 0) 236 mtx_lock(lkp->lk_interlock); 237 if (flags & LK_INTERLOCK) { 238 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED); 239 mtx_unlock(interlkp); 240 } 241 242 if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0) 243 WITNESS_SLEEP(1, &lkp->lk_interlock->mtx_object); 244 245 if (panicstr != NULL) { 246 mtx_unlock(lkp->lk_interlock); 247 return (0); 248 } 249 250 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK; 251 252 switch (flags & LK_TYPE_MASK) { 253 254 case LK_SHARED: 255 /* 256 * If we are not the exclusive lock holder, we have to block 257 * while there is an exclusive lock holder or while an 258 * exclusive lock request or upgrade request is in progress. 259 * 260 * However, if TDF_DEADLKTREAT is set, we override exclusive 261 * lock requests or upgrade requests ( but not the exclusive 262 * lock itself ). 263 */ 264 if (lkp->lk_lockholder != thr) { 265 lockflags = LK_HAVE_EXCL; 266 mtx_lock_spin(&sched_lock); 267 if (td != NULL && !(td->td_flags & TDF_DEADLKTREAT)) 268 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE; 269 mtx_unlock_spin(&sched_lock); 270 error = acquire(&lkp, extflags, lockflags); 271 if (error) 272 break; 273 sharelock(lkp, 1); 274 #if defined(DEBUG_LOCKS) 275 lkp->lk_slockholder = thr; 276 lkp->lk_sfilename = file; 277 lkp->lk_slineno = line; 278 lkp->lk_slockername = name; 279 #endif 280 break; 281 } 282 /* 283 * We hold an exclusive lock, so downgrade it to shared. 284 * An alternative would be to fail with EDEADLK. 285 */ 286 sharelock(lkp, 1); 287 /* FALLTHROUGH downgrade */ 288 289 case LK_DOWNGRADE: 290 KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0, 291 ("lockmgr: not holding exclusive lock " 292 "(owner thread (%p) != thread (%p), exlcnt (%d) != 0", 293 lkp->lk_lockholder, thr, lkp->lk_exclusivecount)); 294 sharelock(lkp, lkp->lk_exclusivecount); 295 lkp->lk_exclusivecount = 0; 296 lkp->lk_flags &= ~LK_HAVE_EXCL; 297 lkp->lk_lockholder = LK_NOPROC; 298 if (lkp->lk_waitcount) 299 wakeup((void *)lkp); 300 break; 301 302 case LK_EXCLUPGRADE: 303 /* 304 * If another process is ahead of us to get an upgrade, 305 * then we want to fail rather than have an intervening 306 * exclusive access. 307 */ 308 if (lkp->lk_flags & LK_WANT_UPGRADE) { 309 shareunlock(lkp, 1); 310 error = EBUSY; 311 break; 312 } 313 /* FALLTHROUGH normal upgrade */ 314 315 case LK_UPGRADE: 316 /* 317 * Upgrade a shared lock to an exclusive one. If another 318 * shared lock has already requested an upgrade to an 319 * exclusive lock, our shared lock is released and an 320 * exclusive lock is requested (which will be granted 321 * after the upgrade). If we return an error, the file 322 * will always be unlocked. 323 */ 324 if ((lkp->lk_lockholder == thr) || (lkp->lk_sharecount <= 0)) 325 panic("lockmgr: upgrade exclusive lock"); 326 shareunlock(lkp, 1); 327 /* 328 * If we are just polling, check to see if we will block. 329 */ 330 if ((extflags & LK_NOWAIT) && 331 ((lkp->lk_flags & LK_WANT_UPGRADE) || 332 lkp->lk_sharecount > 1)) { 333 error = EBUSY; 334 break; 335 } 336 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) { 337 /* 338 * We are first shared lock to request an upgrade, so 339 * request upgrade and wait for the shared count to 340 * drop to zero, then take exclusive lock. 341 */ 342 lkp->lk_flags |= LK_WANT_UPGRADE; 343 error = acquire(&lkp, extflags, LK_SHARE_NONZERO); 344 lkp->lk_flags &= ~LK_WANT_UPGRADE; 345 346 if (error) 347 break; 348 lkp->lk_flags |= LK_HAVE_EXCL; 349 lkp->lk_lockholder = thr; 350 if (lkp->lk_exclusivecount != 0) 351 panic("lockmgr: non-zero exclusive count"); 352 lkp->lk_exclusivecount = 1; 353 #if defined(DEBUG_LOCKS) 354 lkp->lk_filename = file; 355 lkp->lk_lineno = line; 356 lkp->lk_lockername = name; 357 #endif 358 break; 359 } 360 /* 361 * Someone else has requested upgrade. Release our shared 362 * lock, awaken upgrade requestor if we are the last shared 363 * lock, then request an exclusive lock. 364 */ 365 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) == 366 LK_WAIT_NONZERO) 367 wakeup((void *)lkp); 368 /* FALLTHROUGH exclusive request */ 369 370 case LK_EXCLUSIVE: 371 if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) { 372 /* 373 * Recursive lock. 374 */ 375 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0) 376 panic("lockmgr: locking against myself"); 377 if ((extflags & LK_CANRECURSE) != 0) { 378 lkp->lk_exclusivecount++; 379 break; 380 } 381 } 382 /* 383 * If we are just polling, check to see if we will sleep. 384 */ 385 if ((extflags & LK_NOWAIT) && 386 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) { 387 error = EBUSY; 388 break; 389 } 390 /* 391 * Try to acquire the want_exclusive flag. 392 */ 393 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL)); 394 if (error) 395 break; 396 lkp->lk_flags |= LK_WANT_EXCL; 397 /* 398 * Wait for shared locks and upgrades to finish. 399 */ 400 error = acquire(&lkp, extflags, LK_WANT_UPGRADE | LK_SHARE_NONZERO); 401 lkp->lk_flags &= ~LK_WANT_EXCL; 402 if (error) 403 break; 404 lkp->lk_flags |= LK_HAVE_EXCL; 405 lkp->lk_lockholder = thr; 406 if (lkp->lk_exclusivecount != 0) 407 panic("lockmgr: non-zero exclusive count"); 408 lkp->lk_exclusivecount = 1; 409 #if defined(DEBUG_LOCKS) 410 lkp->lk_filename = file; 411 lkp->lk_lineno = line; 412 lkp->lk_lockername = name; 413 #endif 414 break; 415 416 case LK_RELEASE: 417 if (lkp->lk_exclusivecount != 0) { 418 if (lkp->lk_lockholder != thr && 419 lkp->lk_lockholder != LK_KERNPROC) { 420 panic("lockmgr: thread %p, not %s %p unlocking", 421 thr, "exclusive lock holder", 422 lkp->lk_lockholder); 423 } 424 if (lkp->lk_exclusivecount == 1) { 425 lkp->lk_flags &= ~LK_HAVE_EXCL; 426 lkp->lk_lockholder = LK_NOPROC; 427 lkp->lk_exclusivecount = 0; 428 } else { 429 lkp->lk_exclusivecount--; 430 } 431 } else if (lkp->lk_flags & LK_SHARE_NONZERO) 432 shareunlock(lkp, 1); 433 if (lkp->lk_flags & LK_WAIT_NONZERO) 434 wakeup((void *)lkp); 435 break; 436 437 case LK_DRAIN: 438 /* 439 * Check that we do not already hold the lock, as it can 440 * never drain if we do. Unfortunately, we have no way to 441 * check for holding a shared lock, but at least we can 442 * check for an exclusive one. 443 */ 444 if (lkp->lk_lockholder == thr) 445 panic("lockmgr: draining against myself"); 446 447 error = acquiredrain(lkp, extflags); 448 if (error) 449 break; 450 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL; 451 lkp->lk_lockholder = thr; 452 lkp->lk_exclusivecount = 1; 453 #if defined(DEBUG_LOCKS) 454 lkp->lk_filename = file; 455 lkp->lk_lineno = line; 456 lkp->lk_lockername = name; 457 #endif 458 break; 459 460 default: 461 mtx_unlock(lkp->lk_interlock); 462 panic("lockmgr: unknown locktype request %d", 463 flags & LK_TYPE_MASK); 464 /* NOTREACHED */ 465 } 466 if ((lkp->lk_flags & LK_WAITDRAIN) && 467 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | 468 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) { 469 lkp->lk_flags &= ~LK_WAITDRAIN; 470 wakeup((void *)&lkp->lk_flags); 471 } 472 mtx_unlock(lkp->lk_interlock); 473 return (error); 474 } 475 476 static int 477 acquiredrain(struct lock *lkp, int extflags) { 478 int error; 479 480 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) { 481 return EBUSY; 482 } 483 484 error = apause(lkp, LK_ALL); 485 if (error == 0) 486 return 0; 487 488 while (lkp->lk_flags & LK_ALL) { 489 lkp->lk_flags |= LK_WAITDRAIN; 490 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio, 491 lkp->lk_wmesg, 492 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0)); 493 if (error) 494 return error; 495 if (extflags & LK_SLEEPFAIL) { 496 return ENOLCK; 497 } 498 } 499 return 0; 500 } 501 502 /* 503 * Transfer any waiting processes from one lock to another. 504 */ 505 void 506 transferlockers(from, to) 507 struct lock *from; 508 struct lock *to; 509 { 510 511 KASSERT(from != to, ("lock transfer to self")); 512 KASSERT((from->lk_flags&LK_WAITDRAIN) == 0, ("transfer draining lock")); 513 if (from->lk_waitcount == 0) 514 return; 515 from->lk_newlock = to; 516 wakeup((void *)from); 517 msleep(&from->lk_newlock, NULL, from->lk_prio, "lkxfer", 0); 518 from->lk_newlock = NULL; 519 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE); 520 KASSERT(from->lk_waitcount == 0, ("active lock")); 521 } 522 523 524 /* 525 * Initialize a lock; required before use. 526 */ 527 void 528 lockinit(lkp, prio, wmesg, timo, flags) 529 struct lock *lkp; 530 int prio; 531 const char *wmesg; 532 int timo; 533 int flags; 534 { 535 CTR5(KTR_LOCKMGR, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", " 536 "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags); 537 538 if (lock_mtx_valid == 0) { 539 mtx_init(&lock_mtx, "lockmgr", NULL, MTX_DEF); 540 lock_mtx_valid = 1; 541 } 542 /* 543 * XXX cleanup - make sure mtxpool is always initialized before 544 * this is ever called. 545 */ 546 if (mtx_pool_valid) { 547 mtx_lock(&lock_mtx); 548 lkp->lk_interlock = mtx_pool_alloc(); 549 mtx_unlock(&lock_mtx); 550 } else { 551 lkp->lk_interlock = &lock_mtx; 552 } 553 lkp->lk_flags = (flags & LK_EXTFLG_MASK); 554 lkp->lk_sharecount = 0; 555 lkp->lk_waitcount = 0; 556 lkp->lk_exclusivecount = 0; 557 lkp->lk_prio = prio; 558 lkp->lk_wmesg = wmesg; 559 lkp->lk_timo = timo; 560 lkp->lk_lockholder = LK_NOPROC; 561 lkp->lk_newlock = NULL; 562 #ifdef DEBUG_LOCKS 563 lkp->lk_filename = "none"; 564 lkp->lk_lockername = "never exclusive locked"; 565 lkp->lk_lineno = 0; 566 lkp->lk_slockholder = LK_NOPROC; 567 lkp->lk_sfilename = "none"; 568 lkp->lk_slockername = "never share locked"; 569 lkp->lk_slineno = 0; 570 #endif 571 } 572 573 /* 574 * Destroy a lock. 575 */ 576 void 577 lockdestroy(lkp) 578 struct lock *lkp; 579 { 580 CTR2(KTR_LOCKMGR, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")", 581 lkp, lkp->lk_wmesg); 582 } 583 584 /* 585 * Determine the status of a lock. 586 */ 587 int 588 lockstatus(lkp, td) 589 struct lock *lkp; 590 struct thread *td; 591 { 592 int lock_type = 0; 593 594 mtx_lock(lkp->lk_interlock); 595 if (lkp->lk_exclusivecount != 0) { 596 if (td == NULL || lkp->lk_lockholder == td) 597 lock_type = LK_EXCLUSIVE; 598 else 599 lock_type = LK_EXCLOTHER; 600 } else if (lkp->lk_sharecount != 0) 601 lock_type = LK_SHARED; 602 mtx_unlock(lkp->lk_interlock); 603 return (lock_type); 604 } 605 606 /* 607 * Determine the number of holders of a lock. 608 */ 609 int 610 lockcount(lkp) 611 struct lock *lkp; 612 { 613 int count; 614 615 mtx_lock(lkp->lk_interlock); 616 count = lkp->lk_exclusivecount + lkp->lk_sharecount; 617 mtx_unlock(lkp->lk_interlock); 618 return (count); 619 } 620 621 /* 622 * Print out information about state of a lock. Used by VOP_PRINT 623 * routines to display status about contained locks. 624 */ 625 void 626 lockmgr_printinfo(lkp) 627 struct lock *lkp; 628 { 629 630 if (lkp->lk_sharecount) 631 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg, 632 lkp->lk_sharecount); 633 else if (lkp->lk_flags & LK_HAVE_EXCL) 634 printf(" lock type %s: EXCL (count %d) by thread %p", 635 lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder); 636 if (lkp->lk_waitcount > 0) 637 printf(" with %d pending", lkp->lk_waitcount); 638 } 639