kern_mutex.c revision 93692
1/*- 2 * Copyright (c) 1998 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_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 29 * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 30 * $FreeBSD: head/sys/kern/kern_mutex.c 93692 2002-04-02 20:44:30Z jhb $ 31 */ 32 33/* 34 * Machine independent bits of mutex implementation. 35 */ 36 37#include "opt_ddb.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/bus.h> 42#include <sys/kernel.h> 43#include <sys/ktr.h> 44#include <sys/lock.h> 45#include <sys/malloc.h> 46#include <sys/mutex.h> 47#include <sys/proc.h> 48#include <sys/resourcevar.h> 49#include <sys/sbuf.h> 50#include <sys/sysctl.h> 51#include <sys/vmmeter.h> 52 53#include <machine/atomic.h> 54#include <machine/bus.h> 55#include <machine/clock.h> 56#include <machine/cpu.h> 57 58#include <ddb/ddb.h> 59 60#include <vm/vm.h> 61#include <vm/vm_extern.h> 62 63/* 64 * Internal utility macros. 65 */ 66#define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 67 68#define mtx_owner(m) (mtx_unowned((m)) ? NULL \ 69 : (struct thread *)((m)->mtx_lock & MTX_FLAGMASK)) 70 71/* 72 * Lock classes for sleep and spin mutexes. 73 */ 74struct lock_class lock_class_mtx_sleep = { 75 "sleep mutex", 76 LC_SLEEPLOCK | LC_RECURSABLE 77}; 78struct lock_class lock_class_mtx_spin = { 79 "spin mutex", 80 LC_SPINLOCK | LC_RECURSABLE 81}; 82 83/* 84 * Prototypes for non-exported routines. 85 */ 86static void propagate_priority(struct thread *); 87 88static void 89propagate_priority(struct thread *td) 90{ 91 int pri = td->td_priority; 92 struct mtx *m = td->td_blocked; 93 94 mtx_assert(&sched_lock, MA_OWNED); 95 for (;;) { 96 struct thread *td1; 97 98 td = mtx_owner(m); 99 100 if (td == NULL) { 101 /* 102 * This really isn't quite right. Really 103 * ought to bump priority of thread that 104 * next acquires the mutex. 105 */ 106 MPASS(m->mtx_lock == MTX_CONTESTED); 107 return; 108 } 109 110 MPASS(td->td_proc->p_magic == P_MAGIC); 111 KASSERT(td->td_proc->p_stat != SSLEEP, ("sleeping thread owns a mutex")); 112 if (td->td_priority <= pri) /* lower is higher priority */ 113 return; 114 115 /* 116 * Bump this thread's priority. 117 */ 118 td->td_priority = pri; 119 120 /* 121 * If lock holder is actually running, just bump priority. 122 */ 123 /* XXXKSE this test is not sufficient */ 124 if (td->td_kse && (td->td_kse->ke_oncpu != NOCPU)) { 125 MPASS(td->td_proc->p_stat == SRUN 126 || td->td_proc->p_stat == SZOMB 127 || td->td_proc->p_stat == SSTOP); 128 return; 129 } 130 131#ifndef SMP 132 /* 133 * For UP, we check to see if td is curthread (this shouldn't 134 * ever happen however as it would mean we are in a deadlock.) 135 */ 136 KASSERT(td != curthread, ("Deadlock detected")); 137#endif 138 139 /* 140 * If on run queue move to new run queue, and quit. 141 * XXXKSE this gets a lot more complicated under threads 142 * but try anyhow. 143 */ 144 if (td->td_proc->p_stat == SRUN) { 145 MPASS(td->td_blocked == NULL); 146 remrunqueue(td); 147 setrunqueue(td); 148 return; 149 } 150 151 /* 152 * If we aren't blocked on a mutex, we should be. 153 */ 154 KASSERT(td->td_proc->p_stat == SMTX, ( 155 "process %d(%s):%d holds %s but isn't blocked on a mutex\n", 156 td->td_proc->p_pid, td->td_proc->p_comm, td->td_proc->p_stat, 157 m->mtx_object.lo_name)); 158 159 /* 160 * Pick up the mutex that td is blocked on. 161 */ 162 m = td->td_blocked; 163 MPASS(m != NULL); 164 165 /* 166 * Check if the thread needs to be moved up on 167 * the blocked chain 168 */ 169 if (td == TAILQ_FIRST(&m->mtx_blocked)) { 170 continue; 171 } 172 173 td1 = TAILQ_PREV(td, threadqueue, td_blkq); 174 if (td1->td_priority <= pri) { 175 continue; 176 } 177 178 /* 179 * Remove thread from blocked chain and determine where 180 * it should be moved up to. Since we know that td1 has 181 * a lower priority than td, we know that at least one 182 * thread in the chain has a lower priority and that 183 * td1 will thus not be NULL after the loop. 184 */ 185 TAILQ_REMOVE(&m->mtx_blocked, td, td_blkq); 186 TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) { 187 MPASS(td1->td_proc->p_magic == P_MAGIC); 188 if (td1->td_priority > pri) 189 break; 190 } 191 192 MPASS(td1 != NULL); 193 TAILQ_INSERT_BEFORE(td1, td, td_blkq); 194 CTR4(KTR_LOCK, 195 "propagate_priority: p %p moved before %p on [%p] %s", 196 td, td1, m, m->mtx_object.lo_name); 197 } 198} 199 200#ifdef MUTEX_PROFILING 201SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging"); 202SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling"); 203static int mutex_prof_enable = 0; 204SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW, 205 &mutex_prof_enable, 0, "Enable tracing of mutex holdtime"); 206 207struct mutex_prof { 208 const char *name; 209 const char *file; 210 int line; 211#define MPROF_MAX 0 212#define MPROF_TOT 1 213#define MPROF_CNT 2 214#define MPROF_AVG 3 215 u_int64_t counter[4]; 216}; 217 218/* 219 * mprof_buf is a static pool of profiling records to avoid possible 220 * reentrance of the memory allocation functions. 221 * 222 * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 223 */ 224#define NUM_MPROF_BUFFERS 4096 225static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 226static int first_free_mprof_buf; 227#define MPROF_HASH_SIZE 32771 228static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 229 230static int mutex_prof_acquisitions; 231SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 232 &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 233static int mutex_prof_records; 234SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 235 &mutex_prof_records, 0, "Number of profiling records"); 236static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 237SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 238 &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 239static int mutex_prof_rejected; 240SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 241 &mutex_prof_rejected, 0, "Number of rejected profiling records"); 242static int mutex_prof_hashsize = MPROF_HASH_SIZE; 243SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 244 &mutex_prof_hashsize, 0, "Hash size"); 245static int mutex_prof_collisions = 0; 246SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 247 &mutex_prof_collisions, 0, "Number of hash collisions"); 248 249/* 250 * mprof_mtx protects the profiling buffers and the hash. 251 */ 252static struct mtx mprof_mtx; 253 254static void 255mprof_init(void *arg __unused) 256{ 257 mtx_init(&mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 258} 259SYSINIT(mprofinit, SI_SUB_LOCK, SI_ORDER_ANY, mprof_init, NULL); 260 261static u_int64_t 262nanoseconds(void) 263{ 264 struct timespec tv; 265 266 nanotime(&tv); 267 return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 268} 269 270static int 271dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 272{ 273 struct sbuf *sb; 274 int error, i; 275 276 if (first_free_mprof_buf == 0) 277 return SYSCTL_OUT(req, "No locking recorded", 278 sizeof("No locking recorded")); 279 280 sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND); 281 sbuf_printf(sb, "%12s %12s %12s %12s %s\n", 282 "max", "total", "count", "average", "name"); 283 mtx_lock_spin(&mprof_mtx); 284 for (i = 0; i < first_free_mprof_buf; ++i) 285 sbuf_printf(sb, "%12llu %12llu %12llu %12llu %s:%d (%s)\n", 286 mprof_buf[i].counter[MPROF_MAX] / 1000, 287 mprof_buf[i].counter[MPROF_TOT] / 1000, 288 mprof_buf[i].counter[MPROF_CNT], 289 mprof_buf[i].counter[MPROF_AVG] / 1000, 290 mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 291 mtx_unlock_spin(&mprof_mtx); 292 sbuf_finish(sb); 293 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 294 sbuf_delete(sb); 295 return (error); 296} 297SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD, 298 NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 299#endif 300 301/* 302 * Function versions of the inlined __mtx_* macros. These are used by 303 * modules and can also be called from assembly language if needed. 304 */ 305void 306_mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 307{ 308 309 MPASS(curthread != NULL); 310 _get_sleep_lock(m, curthread, opts, file, line); 311 LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 312 line); 313 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 314#ifdef MUTEX_PROFILING 315 /* don't reset the timer when/if recursing */ 316 if (m->acqtime == 0) { 317 m->file = file; 318 m->line = line; 319 m->acqtime = mutex_prof_enable ? nanoseconds() : 0; 320 ++mutex_prof_acquisitions; 321 } 322#endif 323} 324 325void 326_mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 327{ 328 329 MPASS(curthread != NULL); 330 mtx_assert(m, MA_OWNED); 331#ifdef MUTEX_PROFILING 332 if (m->acqtime != 0) { 333 static const char *unknown = "(unknown)"; 334 struct mutex_prof *mpp; 335 u_int64_t acqtime, now; 336 const char *p, *q; 337 volatile u_int hash, n; 338 339 now = nanoseconds(); 340 acqtime = m->acqtime; 341 m->acqtime = 0; 342 if (now <= acqtime) 343 goto out; 344 for (p = file; strncmp(p, "../", 3) == 0; p += 3) 345 /* nothing */ ; 346 if (p == NULL || *p == '\0') 347 p = unknown; 348 for (hash = line, q = p; *q != '\0'; ++q) 349 hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 350 mtx_lock_spin(&mprof_mtx); 351 n = hash; 352 while ((mpp = mprof_hash[n]) != NULL) { 353 if (mpp->line == line && strcmp(mpp->file, p) == 0) 354 break; 355 n = (n + 1) % MPROF_HASH_SIZE; 356 } 357 if (mpp == NULL) { 358 /* Just exit if we cannot get a trace buffer */ 359 if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 360 ++mutex_prof_rejected; 361 goto unlock; 362 } 363 mpp = &mprof_buf[first_free_mprof_buf++]; 364 mpp->name = mtx_name(m); 365 mpp->file = p; 366 mpp->line = line; 367 mutex_prof_collisions += n - hash; 368 ++mutex_prof_records; 369 mprof_hash[hash] = mpp; 370 } 371 /* 372 * Record if the mutex has been held longer now than ever 373 * before 374 */ 375 if ((now - acqtime) > mpp->counter[MPROF_MAX]) 376 mpp->counter[MPROF_MAX] = now - acqtime; 377 mpp->counter[MPROF_TOT] += now - acqtime; 378 mpp->counter[MPROF_CNT] += 1; 379 mpp->counter[MPROF_AVG] = 380 mpp->counter[MPROF_TOT] / mpp->counter[MPROF_CNT]; 381unlock: 382 mtx_unlock_spin(&mprof_mtx); 383 } 384out: 385#endif 386 WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 387 LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 388 line); 389 _rel_sleep_lock(m, curthread, opts, file, line); 390} 391 392void 393_mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 394{ 395 396 MPASS(curthread != NULL); 397 _get_spin_lock(m, curthread, opts, file, line); 398 LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 399 line); 400 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 401} 402 403void 404_mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 405{ 406 407 MPASS(curthread != NULL); 408 mtx_assert(m, MA_OWNED); 409 WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 410 LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 411 line); 412 _rel_spin_lock(m); 413} 414 415/* 416 * The important part of mtx_trylock{,_flags}() 417 * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that 418 * if we're called, it's because we know we don't already own this lock. 419 */ 420int 421_mtx_trylock(struct mtx *m, int opts, const char *file, int line) 422{ 423 int rval; 424 425 MPASS(curthread != NULL); 426 427 rval = _obtain_lock(m, curthread); 428 429 LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 430 if (rval) { 431 /* 432 * We do not handle recursion in _mtx_trylock; see the 433 * note at the top of the routine. 434 */ 435 KASSERT(!mtx_recursed(m), 436 ("mtx_trylock() called on a recursed mutex")); 437 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 438 file, line); 439 } 440 441 return (rval); 442} 443 444/* 445 * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 446 * 447 * We call this if the lock is either contested (i.e. we need to go to 448 * sleep waiting for it), or if we need to recurse on it. 449 */ 450void 451_mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line) 452{ 453 struct thread *td = curthread; 454 455 if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) { 456 m->mtx_recurse++; 457 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 458 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 459 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 460 return; 461 } 462 463 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 464 CTR4(KTR_LOCK, 465 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 466 m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 467 468 while (!_obtain_lock(m, td)) { 469 uintptr_t v; 470 struct thread *td1; 471 472 mtx_lock_spin(&sched_lock); 473 /* 474 * Check if the lock has been released while spinning for 475 * the sched_lock. 476 */ 477 if ((v = m->mtx_lock) == MTX_UNOWNED) { 478 mtx_unlock_spin(&sched_lock); 479 continue; 480 } 481 482 /* 483 * The mutex was marked contested on release. This means that 484 * there are threads blocked on it. 485 */ 486 if (v == MTX_CONTESTED) { 487 td1 = TAILQ_FIRST(&m->mtx_blocked); 488 MPASS(td1 != NULL); 489 m->mtx_lock = (uintptr_t)td | MTX_CONTESTED; 490 491 if (td1->td_priority < td->td_priority) 492 td->td_priority = td1->td_priority; 493 mtx_unlock_spin(&sched_lock); 494 return; 495 } 496 497 /* 498 * If the mutex isn't already contested and a failure occurs 499 * setting the contested bit, the mutex was either released 500 * or the state of the MTX_RECURSED bit changed. 501 */ 502 if ((v & MTX_CONTESTED) == 0 && 503 !atomic_cmpset_ptr(&m->mtx_lock, (void *)v, 504 (void *)(v | MTX_CONTESTED))) { 505 mtx_unlock_spin(&sched_lock); 506 continue; 507 } 508 509 /* 510 * We definitely must sleep for this lock. 511 */ 512 mtx_assert(m, MA_NOTOWNED); 513 514#ifdef notyet 515 /* 516 * If we're borrowing an interrupted thread's VM context, we 517 * must clean up before going to sleep. 518 */ 519 if (td->td_ithd != NULL) { 520 struct ithd *it = td->td_ithd; 521 522 if (it->it_interrupted) { 523 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 524 CTR2(KTR_LOCK, 525 "_mtx_lock_sleep: %p interrupted %p", 526 it, it->it_interrupted); 527 intr_thd_fixup(it); 528 } 529 } 530#endif 531 532 /* 533 * Put us on the list of threads blocked on this mutex. 534 */ 535 if (TAILQ_EMPTY(&m->mtx_blocked)) { 536 td1 = mtx_owner(m); 537 LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested); 538 TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq); 539 } else { 540 TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) 541 if (td1->td_priority > td->td_priority) 542 break; 543 if (td1) 544 TAILQ_INSERT_BEFORE(td1, td, td_blkq); 545 else 546 TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq); 547 } 548 549 /* 550 * Save who we're blocked on. 551 */ 552 td->td_blocked = m; 553 td->td_mtxname = m->mtx_object.lo_name; 554 td->td_proc->p_stat = SMTX; 555 propagate_priority(td); 556 557 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 558 CTR3(KTR_LOCK, 559 "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m, 560 m->mtx_object.lo_name); 561 562 td->td_proc->p_stats->p_ru.ru_nvcsw++; 563 mi_switch(); 564 565 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 566 CTR3(KTR_LOCK, 567 "_mtx_lock_sleep: p %p free from blocked on [%p] %s", 568 td, m, m->mtx_object.lo_name); 569 570 mtx_unlock_spin(&sched_lock); 571 } 572 573 return; 574} 575 576/* 577 * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 578 * 579 * This is only called if we need to actually spin for the lock. Recursion 580 * is handled inline. 581 */ 582void 583_mtx_lock_spin(struct mtx *m, int opts, const char *file, int line) 584{ 585 int i = 0; 586 587 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 588 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 589 590 for (;;) { 591 if (_obtain_lock(m, curthread)) 592 break; 593 594 /* Give interrupts a chance while we spin. */ 595 critical_exit(); 596 while (m->mtx_lock != MTX_UNOWNED) { 597 if (i++ < 10000000) 598 continue; 599 if (i++ < 60000000) 600 DELAY(1); 601#ifdef DDB 602 else if (!db_active) 603#else 604 else 605#endif 606 panic("spin lock %s held by %p for > 5 seconds", 607 m->mtx_object.lo_name, (void *)m->mtx_lock); 608 } 609 critical_enter(); 610 } 611 612 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 613 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 614 615 return; 616} 617 618/* 619 * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 620 * 621 * We are only called here if the lock is recursed or contested (i.e. we 622 * need to wake up a blocked thread). 623 */ 624void 625_mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 626{ 627 struct thread *td, *td1; 628 struct mtx *m1; 629 int pri; 630 631 td = curthread; 632 633 if (mtx_recursed(m)) { 634 if (--(m->mtx_recurse) == 0) 635 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 636 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 637 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 638 return; 639 } 640 641 mtx_lock_spin(&sched_lock); 642 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 643 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 644 645 td1 = TAILQ_FIRST(&m->mtx_blocked); 646 MPASS(td->td_proc->p_magic == P_MAGIC); 647 MPASS(td1->td_proc->p_magic == P_MAGIC); 648 649 TAILQ_REMOVE(&m->mtx_blocked, td1, td_blkq); 650 651 if (TAILQ_EMPTY(&m->mtx_blocked)) { 652 LIST_REMOVE(m, mtx_contested); 653 _release_lock_quick(m); 654 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 655 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 656 } else 657 atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED); 658 659 pri = PRI_MAX; 660 LIST_FOREACH(m1, &td->td_contested, mtx_contested) { 661 int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority; 662 if (cp < pri) 663 pri = cp; 664 } 665 666 if (pri > td->td_base_pri) 667 pri = td->td_base_pri; 668 td->td_priority = pri; 669 670 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 671 CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p", 672 m, td1); 673 674 td1->td_blocked = NULL; 675 td1->td_proc->p_stat = SRUN; 676 setrunqueue(td1); 677 678 if (td->td_critnest == 1 && td1->td_priority < pri) { 679#ifdef notyet 680 if (td->td_ithd != NULL) { 681 struct ithd *it = td->td_ithd; 682 683 if (it->it_interrupted) { 684 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 685 CTR2(KTR_LOCK, 686 "_mtx_unlock_sleep: %p interrupted %p", 687 it, it->it_interrupted); 688 intr_thd_fixup(it); 689 } 690 } 691#endif 692 setrunqueue(td); 693 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 694 CTR2(KTR_LOCK, 695 "_mtx_unlock_sleep: %p switching out lock=%p", m, 696 (void *)m->mtx_lock); 697 698 td->td_proc->p_stats->p_ru.ru_nivcsw++; 699 mi_switch(); 700 if (LOCK_LOG_TEST(&m->mtx_object, opts)) 701 CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 702 m, (void *)m->mtx_lock); 703 } 704 705 mtx_unlock_spin(&sched_lock); 706 707 return; 708} 709 710/* 711 * All the unlocking of MTX_SPIN locks is done inline. 712 * See the _rel_spin_lock() macro for the details. 713 */ 714 715/* 716 * The backing function for the INVARIANTS-enabled mtx_assert() 717 */ 718#ifdef INVARIANT_SUPPORT 719void 720_mtx_assert(struct mtx *m, int what, const char *file, int line) 721{ 722 723 if (panicstr != NULL) 724 return; 725 switch (what) { 726 case MA_OWNED: 727 case MA_OWNED | MA_RECURSED: 728 case MA_OWNED | MA_NOTRECURSED: 729 if (!mtx_owned(m)) 730 panic("mutex %s not owned at %s:%d", 731 m->mtx_object.lo_name, file, line); 732 if (mtx_recursed(m)) { 733 if ((what & MA_NOTRECURSED) != 0) 734 panic("mutex %s recursed at %s:%d", 735 m->mtx_object.lo_name, file, line); 736 } else if ((what & MA_RECURSED) != 0) { 737 panic("mutex %s unrecursed at %s:%d", 738 m->mtx_object.lo_name, file, line); 739 } 740 break; 741 case MA_NOTOWNED: 742 if (mtx_owned(m)) 743 panic("mutex %s owned at %s:%d", 744 m->mtx_object.lo_name, file, line); 745 break; 746 default: 747 panic("unknown mtx_assert at %s:%d", file, line); 748 } 749} 750#endif 751 752/* 753 * The MUTEX_DEBUG-enabled mtx_validate() 754 * 755 * Most of these checks have been moved off into the LO_INITIALIZED flag 756 * maintained by the witness code. 757 */ 758#ifdef MUTEX_DEBUG 759 760void mtx_validate(struct mtx *); 761 762void 763mtx_validate(struct mtx *m) 764{ 765 766/* 767 * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 768 * we can re-enable the kernacc() checks. 769 */ 770#ifndef __alpha__ 771 /* 772 * Can't call kernacc() from early init386(), especially when 773 * initializing Giant mutex, because some stuff in kernacc() 774 * requires Giant itself. 775 */ 776 if (!cold) 777 if (!kernacc((caddr_t)m, sizeof(m), 778 VM_PROT_READ | VM_PROT_WRITE)) 779 panic("Can't read and write to mutex %p", m); 780#endif 781} 782#endif 783 784/* 785 * General init routine used by the MTX_SYSINIT() macro. 786 */ 787void 788mtx_sysinit(void *arg) 789{ 790 struct mtx_args *margs = arg; 791 792 mtx_init(margs->ma_mtx, margs->ma_desc, margs->ma_opts); 793} 794 795/* 796 * Mutex initialization routine; initialize lock `m' of type contained in 797 * `opts' with options contained in `opts' and description `description.' 798 */ 799void 800mtx_init(struct mtx *m, const char *description, int opts) 801{ 802 struct lock_object *lock; 803 804 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 805 MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0); 806 807#ifdef MUTEX_DEBUG 808 /* Diagnostic and error correction */ 809 mtx_validate(m); 810#endif 811 812 lock = &m->mtx_object; 813 KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 814 ("mutex %s %p already initialized", description, m)); 815 bzero(m, sizeof(*m)); 816 if (opts & MTX_SPIN) 817 lock->lo_class = &lock_class_mtx_spin; 818 else 819 lock->lo_class = &lock_class_mtx_sleep; 820 lock->lo_name = description; 821 if (opts & MTX_QUIET) 822 lock->lo_flags = LO_QUIET; 823 if (opts & MTX_RECURSE) 824 lock->lo_flags |= LO_RECURSABLE; 825 if (opts & MTX_SLEEPABLE) 826 lock->lo_flags |= LO_SLEEPABLE; 827 if ((opts & MTX_NOWITNESS) == 0) 828 lock->lo_flags |= LO_WITNESS; 829 if (opts & MTX_DUPOK) 830 lock->lo_flags |= LO_DUPOK; 831 832 m->mtx_lock = MTX_UNOWNED; 833 TAILQ_INIT(&m->mtx_blocked); 834 835 LOCK_LOG_INIT(lock, opts); 836 837 WITNESS_INIT(lock); 838} 839 840/* 841 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 842 * passed in as a flag here because if the corresponding mtx_init() was 843 * called with MTX_QUIET set, then it will already be set in the mutex's 844 * flags. 845 */ 846void 847mtx_destroy(struct mtx *m) 848{ 849 850 LOCK_LOG_DESTROY(&m->mtx_object, 0); 851 852 if (!mtx_owned(m)) 853 MPASS(mtx_unowned(m)); 854 else { 855 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 856 857 /* Tell witness this isn't locked to make it happy. */ 858 WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 859 __LINE__); 860 } 861 862 WITNESS_DESTROY(&m->mtx_object); 863} 864 865/* 866 * Encapsulated Giant mutex routines. These routines provide encapsulation 867 * control for the Giant mutex, allowing sysctls to be used to turn on and 868 * off Giant around certain subsystems. The default value for the sysctls 869 * are set to what developers believe is stable and working in regards to 870 * the Giant pushdown. Developers should not turn off Giant via these 871 * sysctls unless they know what they are doing. 872 * 873 * Callers of mtx_lock_giant() are expected to pass the return value to an 874 * accompanying mtx_unlock_giant() later on. If multiple subsystems are 875 * effected by a Giant wrap, all related sysctl variables must be zero for 876 * the subsystem call to operate without Giant (as determined by the caller). 877 */ 878 879SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation"); 880 881static int kern_giant_all = 0; 882SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, ""); 883 884int kern_giant_proc = 1; /* Giant around PROC locks */ 885int kern_giant_file = 1; /* Giant around struct file & filedesc */ 886int kern_giant_ucred = 1; /* Giant around ucred */ 887SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, ""); 888SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, ""); 889SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, ""); 890 891int 892mtx_lock_giant(int sysctlvar) 893{ 894 if (sysctlvar || kern_giant_all) { 895 mtx_lock(&Giant); 896 return(1); 897 } 898 return(0); 899} 900 901void 902mtx_unlock_giant(int s) 903{ 904 if (s) 905 mtx_unlock(&Giant); 906} 907 908