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 */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/kernel.h> 48 #include <sys/ktr.h> 49 #include <sys/lock.h> 50 #include <sys/lockmgr.h> 51 #include <sys/mutex.h> 52 #include <sys/proc.h> 53 #include <sys/systm.h> 54 55 /* 56 * Locking primitives implementation. 57 * Locks provide shared/exclusive sychronization. 58 */ 59 60 #define LOCK_WAIT_TIME 100 61 #define LOCK_SAMPLE_WAIT 7 62 63 #if defined(DIAGNOSTIC) 64 #define LOCK_INLINE 65 #else 66 #define LOCK_INLINE __inline 67 #endif 68 69 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \ 70 LK_SHARE_NONZERO | LK_WAIT_NONZERO) 71 72 /* 73 * Mutex array variables. Rather than each lockmgr lock having its own mutex, 74 * share a fixed (at boot time) number of mutexes across all lockmgr locks in 75 * order to keep sizeof(struct lock) down. 76 */ 77 static struct mtx lock_mtx; 78 79 static int acquire(struct lock **lkpp, int extflags, int wanted); 80 static int apause(struct lock *lkp, int flags); 81 static int acquiredrain(struct lock *lkp, int extflags) ; 82 83 static void 84 lockmgr_init(void *dummy __unused) 85 { 86 mtx_init(&lock_mtx, "lockmgr", NULL, MTX_DEF); 87 } 88 SYSINIT(lmgrinit, SI_SUB_LOCKMGR, SI_ORDER_FIRST, lockmgr_init, NULL) 89 90 static LOCK_INLINE void 91 sharelock(struct lock *lkp, int incr) { 92 lkp->lk_flags |= LK_SHARE_NONZERO; 93 lkp->lk_sharecount += incr; 94 } 95 96 static LOCK_INLINE void 97 shareunlock(struct lock *lkp, int decr) { 98 99 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr")); 100 101 if (lkp->lk_sharecount == decr) { 102 lkp->lk_flags &= ~LK_SHARE_NONZERO; 103 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) { 104 wakeup(lkp); 105 } 106 lkp->lk_sharecount = 0; 107 } else { 108 lkp->lk_sharecount -= decr; 109 } 110 } 111 112 /* 113 * This is the waitloop optimization. 114 */ 115 static int 116 apause(struct lock *lkp, int flags) 117 { 118 #ifdef SMP 119 int i, lock_wait; 120 #endif 121 122 if ((lkp->lk_flags & flags) == 0) 123 return 0; 124 #ifdef SMP 125 for (lock_wait = LOCK_WAIT_TIME; lock_wait > 0; lock_wait--) { 126 mtx_unlock(lkp->lk_interlock); 127 for (i = LOCK_SAMPLE_WAIT; i > 0; i--) 128 if ((lkp->lk_flags & flags) == 0) 129 break; 130 mtx_lock(lkp->lk_interlock); 131 if ((lkp->lk_flags & flags) == 0) 132 return 0; 133 } 134 #endif 135 return 1; 136 } 137 138 static int 139 acquire(struct lock **lkpp, int extflags, int wanted) 140 { 141 struct lock *lkp = *lkpp; 142 int s, error; 143 CTR3(KTR_LOCK, 144 "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x", 145 lkp, extflags, wanted); 146 147 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) { 148 return EBUSY; 149 } 150 151 if ((extflags & LK_INTERLOCK) == 0) { 152 error = apause(lkp, wanted); 153 if (error == 0) 154 return 0; 155 } 156 157 s = splhigh(); 158 while ((lkp->lk_flags & wanted) != 0) { 159 lkp->lk_flags |= LK_WAIT_NONZERO; 160 lkp->lk_waitcount++; 161 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio, 162 lkp->lk_wmesg, 163 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0)); 164 if (lkp->lk_waitcount == 1) { 165 lkp->lk_flags &= ~LK_WAIT_NONZERO; 166 lkp->lk_waitcount = 0; 167 } else { 168 lkp->lk_waitcount--; 169 } 170 if (error) { 171 splx(s); 172 return error; 173 } 174 if (extflags & LK_SLEEPFAIL) { 175 splx(s); 176 return ENOLCK; 177 } 178 if (lkp->lk_newlock != NULL) { 179 mtx_lock(lkp->lk_newlock->lk_interlock); 180 mtx_unlock(lkp->lk_interlock); 181 if (lkp->lk_waitcount == 0) 182 wakeup((void *)(&lkp->lk_newlock)); 183 *lkpp = lkp = lkp->lk_newlock; 184 } 185 } 186 splx(s); 187 return 0; 188 } 189 190 /* 191 * Set, change, or release a lock. 192 * 193 * Shared requests increment the shared count. Exclusive requests set the 194 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already 195 * accepted shared locks and shared-to-exclusive upgrades to go away. 196 */ 197 int 198 #ifndef DEBUG_LOCKS 199 lockmgr(lkp, flags, interlkp, td) 200 #else 201 debuglockmgr(lkp, flags, interlkp, td, name, file, line) 202 #endif 203 struct lock *lkp; 204 u_int flags; 205 struct mtx *interlkp; 206 struct thread *td; 207 #ifdef DEBUG_LOCKS 208 const char *name; /* Name of lock function */ 209 const char *file; /* Name of file call is from */ 210 int line; /* Line number in file */ 211 #endif 212 { 213 int error; 214 struct thread *thr; 215 int extflags, lockflags; 216 217 error = 0; 218 if (td == NULL) 219 thr = LK_KERNPROC; 220 else 221 thr = td; 222 223 if ((flags & LK_INTERNAL) == 0) 224 mtx_lock(lkp->lk_interlock); 225 #ifdef DEBUG_LOCKS 226 CTR6(KTR_LOCK, 227 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), flags == 0x%x, " 228 "td == %p %s:%d", lkp, lkp->lk_wmesg, flags, td, file, line); 229 #else 230 CTR6(KTR_LOCK, 231 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), owner == %p, exclusivecount == %d, flags == 0x%x, " 232 "td == %p", lkp, lkp->lk_wmesg, lkp->lk_lockholder, 233 lkp->lk_exclusivecount, flags, td); 234 #endif 235 236 if (flags & LK_INTERLOCK) { 237 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED); 238 mtx_unlock(interlkp); 239 } 240 241 if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0) 242 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 243 &lkp->lk_interlock->mtx_object, 244 "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg); 245 246 if (panicstr != NULL) { 247 mtx_unlock(lkp->lk_interlock); 248 return (0); 249 } 250 251 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK; 252 253 switch (flags & LK_TYPE_MASK) { 254 255 case LK_SHARED: 256 /* 257 * If we are not the exclusive lock holder, we have to block 258 * while there is an exclusive lock holder or while an 259 * exclusive lock request or upgrade request is in progress. 260 * 261 * However, if TDP_DEADLKTREAT is set, we override exclusive 262 * lock requests or upgrade requests ( but not the exclusive 263 * lock itself ). 264 */ 265 if (lkp->lk_lockholder != thr) { 266 lockflags = LK_HAVE_EXCL; 267 if (td != NULL && !(td->td_pflags & TDP_DEADLKTREAT)) 268 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE; 269 error = acquire(&lkp, extflags, lockflags); 270 if (error) 271 break; 272 sharelock(lkp, 1); 273 #if defined(DEBUG_LOCKS) 274 lkp->lk_slockholder = thr; 275 lkp->lk_sfilename = file; 276 lkp->lk_slineno = line; 277 lkp->lk_slockername = name; 278 #endif 279 break; 280 } 281 /* 282 * We hold an exclusive lock, so downgrade it to shared. 283 * An alternative would be to fail with EDEADLK. 284 */ 285 sharelock(lkp, 1); 286 /* FALLTHROUGH downgrade */ 287 288 case LK_DOWNGRADE: 289 KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0, 290 ("lockmgr: not holding exclusive lock " 291 "(owner thread (%p) != thread (%p), exlcnt (%d) != 0", 292 lkp->lk_lockholder, thr, lkp->lk_exclusivecount)); 293 sharelock(lkp, lkp->lk_exclusivecount); 294 lkp->lk_exclusivecount = 0; 295 lkp->lk_flags &= ~LK_HAVE_EXCL; 296 lkp->lk_lockholder = LK_NOPROC; 297 if (lkp->lk_waitcount) 298 wakeup((void *)lkp); 299 break; 300 301 case LK_EXCLUPGRADE: 302 /* 303 * If another process is ahead of us to get an upgrade, 304 * then we want to fail rather than have an intervening 305 * exclusive access. 306 */ 307 if (lkp->lk_flags & LK_WANT_UPGRADE) { 308 shareunlock(lkp, 1); 309 error = EBUSY; 310 break; 311 } 312 /* FALLTHROUGH normal upgrade */ 313 314 case LK_UPGRADE: 315 /* 316 * Upgrade a shared lock to an exclusive one. If another 317 * shared lock has already requested an upgrade to an 318 * exclusive lock, our shared lock is released and an 319 * exclusive lock is requested (which will be granted 320 * after the upgrade). If we return an error, the file 321 * will always be unlocked. 322 */ 323 if ((lkp->lk_lockholder == thr) || (lkp->lk_sharecount <= 0)) 324 panic("lockmgr: upgrade exclusive lock"); 325 shareunlock(lkp, 1); 326 /* 327 * If we are just polling, check to see if we will block. 328 */ 329 if ((extflags & LK_NOWAIT) && 330 ((lkp->lk_flags & LK_WANT_UPGRADE) || 331 lkp->lk_sharecount > 1)) { 332 error = EBUSY; 333 break; 334 } 335 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) { 336 /* 337 * We are first shared lock to request an upgrade, so 338 * request upgrade and wait for the shared count to 339 * drop to zero, then take exclusive lock. 340 */ 341 lkp->lk_flags |= LK_WANT_UPGRADE; 342 error = acquire(&lkp, extflags, LK_SHARE_NONZERO); 343 lkp->lk_flags &= ~LK_WANT_UPGRADE; 344 345 if (error) { 346 if ((lkp->lk_flags & ( LK_WANT_EXCL | LK_WAIT_NONZERO)) == (LK_WANT_EXCL | LK_WAIT_NONZERO)) 347 wakeup((void *)lkp); 348 break; 349 } 350 if (lkp->lk_exclusivecount != 0) 351 panic("lockmgr: non-zero exclusive count"); 352 lkp->lk_flags |= LK_HAVE_EXCL; 353 lkp->lk_lockholder = thr; 354 lkp->lk_exclusivecount = 1; 355 #if defined(DEBUG_LOCKS) 356 lkp->lk_filename = file; 357 lkp->lk_lineno = line; 358 lkp->lk_lockername = name; 359 #endif 360 break; 361 } 362 /* 363 * Someone else has requested upgrade. Release our shared 364 * lock, awaken upgrade requestor if we are the last shared 365 * lock, then request an exclusive lock. 366 */ 367 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) == 368 LK_WAIT_NONZERO) 369 wakeup((void *)lkp); 370 /* FALLTHROUGH exclusive request */ 371 372 case LK_EXCLUSIVE: 373 if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) { 374 /* 375 * Recursive lock. 376 */ 377 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0) 378 panic("lockmgr: locking against myself"); 379 if ((extflags & LK_CANRECURSE) != 0) { 380 lkp->lk_exclusivecount++; 381 break; 382 } 383 } 384 /* 385 * If we are just polling, check to see if we will sleep. 386 */ 387 if ((extflags & LK_NOWAIT) && 388 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) { 389 error = EBUSY; 390 break; 391 } 392 /* 393 * Try to acquire the want_exclusive flag. 394 */ 395 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL)); 396 if (error) 397 break; 398 lkp->lk_flags |= LK_WANT_EXCL; 399 /* 400 * Wait for shared locks and upgrades to finish. 401 */ 402 error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO); 403 lkp->lk_flags &= ~LK_WANT_EXCL; 404 if (error) { 405 if (lkp->lk_flags & LK_WAIT_NONZERO) 406 wakeup((void *)lkp); 407 break; 408 } 409 lkp->lk_flags |= LK_HAVE_EXCL; 410 lkp->lk_lockholder = thr; 411 if (lkp->lk_exclusivecount != 0) 412 panic("lockmgr: non-zero exclusive count"); 413 lkp->lk_exclusivecount = 1; 414 #if defined(DEBUG_LOCKS) 415 lkp->lk_filename = file; 416 lkp->lk_lineno = line; 417 lkp->lk_lockername = name; 418 #endif 419 break; 420 421 case LK_RELEASE: 422 if (lkp->lk_exclusivecount != 0) { 423 if (lkp->lk_lockholder != thr && 424 lkp->lk_lockholder != LK_KERNPROC) { 425 panic("lockmgr: thread %p, not %s %p unlocking", 426 thr, "exclusive lock holder", 427 lkp->lk_lockholder); 428 } 429 if (lkp->lk_exclusivecount == 1) { 430 lkp->lk_flags &= ~LK_HAVE_EXCL; 431 lkp->lk_lockholder = LK_NOPROC; 432 lkp->lk_exclusivecount = 0; 433 } else { 434 lkp->lk_exclusivecount--; 435 } 436 } else if (lkp->lk_flags & LK_SHARE_NONZERO) 437 shareunlock(lkp, 1); 438 if (lkp->lk_flags & LK_WAIT_NONZERO) 439 wakeup((void *)lkp); 440 break; 441 442 case LK_DRAIN: 443 /* 444 * Check that we do not already hold the lock, as it can 445 * never drain if we do. Unfortunately, we have no way to 446 * check for holding a shared lock, but at least we can 447 * check for an exclusive one. 448 */ 449 if (lkp->lk_lockholder == thr) 450 panic("lockmgr: draining against myself"); 451 452 error = acquiredrain(lkp, extflags); 453 if (error) 454 break; 455 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL; 456 lkp->lk_lockholder = thr; 457 lkp->lk_exclusivecount = 1; 458 #if defined(DEBUG_LOCKS) 459 lkp->lk_filename = file; 460 lkp->lk_lineno = line; 461 lkp->lk_lockername = name; 462 #endif 463 break; 464 465 default: 466 mtx_unlock(lkp->lk_interlock); 467 panic("lockmgr: unknown locktype request %d", 468 flags & LK_TYPE_MASK); 469 /* NOTREACHED */ 470 } 471 if ((lkp->lk_flags & LK_WAITDRAIN) && 472 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | 473 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) { 474 lkp->lk_flags &= ~LK_WAITDRAIN; 475 wakeup((void *)&lkp->lk_flags); 476 } 477 mtx_unlock(lkp->lk_interlock); 478 return (error); 479 } 480 481 static int 482 acquiredrain(struct lock *lkp, int extflags) { 483 int error; 484 485 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) { 486 return EBUSY; 487 } 488 489 if ((extflags & LK_INTERLOCK) == 0) { 490 error = apause(lkp, LK_ALL); 491 if (error == 0) 492 return 0; 493 } 494 495 while (lkp->lk_flags & LK_ALL) { 496 lkp->lk_flags |= LK_WAITDRAIN; 497 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio, 498 lkp->lk_wmesg, 499 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0)); 500 if (error) 501 return error; 502 if (extflags & LK_SLEEPFAIL) { 503 return ENOLCK; 504 } 505 } 506 return 0; 507 } 508 509 /* 510 * Transfer any waiting processes from one lock to another. 511 */ 512 void 513 transferlockers(from, to) 514 struct lock *from; 515 struct lock *to; 516 { 517 518 KASSERT(from != to, ("lock transfer to self")); 519 KASSERT((from->lk_flags&LK_WAITDRAIN) == 0, ("transfer draining lock")); 520 if (from->lk_waitcount == 0) 521 return; 522 from->lk_newlock = to; 523 wakeup((void *)from); 524 msleep(&from->lk_newlock, NULL, from->lk_prio, "lkxfer", 0); 525 from->lk_newlock = NULL; 526 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE); 527 KASSERT(from->lk_waitcount == 0, ("active lock")); 528 } 529 530 531 /* 532 * Initialize a lock; required before use. 533 */ 534 void 535 lockinit(lkp, prio, wmesg, timo, flags) 536 struct lock *lkp; 537 int prio; 538 const char *wmesg; 539 int timo; 540 int flags; 541 { 542 CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", " 543 "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags); 544 545 lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder); 546 lkp->lk_flags = (flags & LK_EXTFLG_MASK); 547 lkp->lk_sharecount = 0; 548 lkp->lk_waitcount = 0; 549 lkp->lk_exclusivecount = 0; 550 lkp->lk_prio = prio; 551 lkp->lk_wmesg = wmesg; 552 lkp->lk_timo = timo; 553 lkp->lk_lockholder = LK_NOPROC; 554 lkp->lk_newlock = NULL; 555 #ifdef DEBUG_LOCKS 556 lkp->lk_filename = "none"; 557 lkp->lk_lockername = "never exclusive locked"; 558 lkp->lk_lineno = 0; 559 lkp->lk_slockholder = LK_NOPROC; 560 lkp->lk_sfilename = "none"; 561 lkp->lk_slockername = "never share locked"; 562 lkp->lk_slineno = 0; 563 #endif 564 } 565 566 /* 567 * Destroy a lock. 568 */ 569 void 570 lockdestroy(lkp) 571 struct lock *lkp; 572 { 573 CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")", 574 lkp, lkp->lk_wmesg); 575 } 576 577 /* 578 * Determine the status of a lock. 579 */ 580 int 581 lockstatus(lkp, td) 582 struct lock *lkp; 583 struct thread *td; 584 { 585 int lock_type = 0; 586 587 mtx_lock(lkp->lk_interlock); 588 if (lkp->lk_exclusivecount != 0) { 589 if (td == NULL || lkp->lk_lockholder == td) 590 lock_type = LK_EXCLUSIVE; 591 else 592 lock_type = LK_EXCLOTHER; 593 } else if (lkp->lk_sharecount != 0) 594 lock_type = LK_SHARED; 595 mtx_unlock(lkp->lk_interlock); 596 return (lock_type); 597 } 598 599 /* 600 * Determine the number of holders of a lock. 601 */ 602 int 603 lockcount(lkp) 604 struct lock *lkp; 605 { 606 int count; 607 608 mtx_lock(lkp->lk_interlock); 609 count = lkp->lk_exclusivecount + lkp->lk_sharecount; 610 mtx_unlock(lkp->lk_interlock); 611 return (count); 612 } 613 614 /* 615 * Print out information about state of a lock. Used by VOP_PRINT 616 * routines to display status about contained locks. 617 */ 618 void 619 lockmgr_printinfo(lkp) 620 struct lock *lkp; 621 { 622 623 if (lkp->lk_sharecount) 624 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg, 625 lkp->lk_sharecount); 626 else if (lkp->lk_flags & LK_HAVE_EXCL) 627 printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)", 628 lkp->lk_wmesg, lkp->lk_exclusivecount, 629 lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid); 630 if (lkp->lk_waitcount > 0) 631 printf(" with %d pending", lkp->lk_waitcount); 632 } 633