]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_mutex.c
general LOCK_PROFILING cleanup
[FreeBSD/FreeBSD.git] / sys / kern / kern_mutex.c
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  */
31
32 /*
33  * Machine independent bits of mutex implementation.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "opt_adaptive_mutexes.h"
40 #include "opt_ddb.h"
41 #include "opt_global.h"
42 #include "opt_mutex_wake_all.h"
43 #include "opt_sched.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/bus.h>
48 #include <sys/conf.h>
49 #include <sys/kdb.h>
50 #include <sys/kernel.h>
51 #include <sys/ktr.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
55 #include <sys/proc.h>
56 #include <sys/resourcevar.h>
57 #include <sys/sched.h>
58 #include <sys/sbuf.h>
59 #include <sys/sysctl.h>
60 #include <sys/turnstile.h>
61 #include <sys/vmmeter.h>
62 #include <sys/lock_profile.h>
63
64 #include <machine/atomic.h>
65 #include <machine/bus.h>
66 #include <machine/cpu.h>
67
68 #include <ddb/ddb.h>
69
70 #include <fs/devfs/devfs_int.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74
75 /* 
76  * Force MUTEX_WAKE_ALL for now.
77  * single thread wakeup needs fixes to avoid race conditions with 
78  * priority inheritance.
79  */
80 #ifndef MUTEX_WAKE_ALL
81 #define MUTEX_WAKE_ALL
82 #endif
83
84 /*
85  * Internal utility macros.
86  */
87 #define mtx_unowned(m)  ((m)->mtx_lock == MTX_UNOWNED)
88
89 #define mtx_owner(m)    ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
90
91 #ifdef DDB
92 static void     db_show_mtx(struct lock_object *lock);
93 #endif
94
95 /*
96  * Lock classes for sleep and spin mutexes.
97  */
98 struct lock_class lock_class_mtx_sleep = {
99         "sleep mutex",
100         LC_SLEEPLOCK | LC_RECURSABLE,
101 #ifdef DDB
102         db_show_mtx
103 #endif
104 };
105 struct lock_class lock_class_mtx_spin = {
106         "spin mutex",
107         LC_SPINLOCK | LC_RECURSABLE,
108 #ifdef DDB
109         db_show_mtx
110 #endif
111 };
112
113 /*
114  * System-wide mutexes
115  */
116 struct mtx sched_lock;
117 struct mtx Giant;
118
119 #ifdef LOCK_PROFILING
120 static inline void lock_profile_init(void)
121 {
122         int i;
123         /* Initialize the mutex profiling locks */
124         for (i = 0; i < LPROF_LOCK_SIZE; i++) {
125                 mtx_init(&lprof_locks[i], "mprof lock",
126                     NULL, MTX_SPIN|MTX_QUIET|MTX_NOPROFILE);
127         }
128 }
129 #else
130 static inline void lock_profile_init(void) {;}
131 #endif
132
133 /*
134  * Function versions of the inlined __mtx_* macros.  These are used by
135  * modules and can also be called from assembly language if needed.
136  */
137 void
138 _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
139 {
140
141         MPASS(curthread != NULL);
142         KASSERT(m->mtx_lock != MTX_DESTROYED,
143             ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
144         KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
145             ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
146             file, line));
147         WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
148             file, line);
149
150         _get_sleep_lock(m, curthread, opts, file, line);
151         LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
152             line);
153         WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
154         curthread->td_locks++;
155 }
156
157 void
158 _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
159 {
160
161         MPASS(curthread != NULL);
162         KASSERT(m->mtx_lock != MTX_DESTROYED,
163             ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
164         KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
165             ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
166             file, line));
167         curthread->td_locks--;
168         WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
169         LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
170             line);
171         mtx_assert(m, MA_OWNED);
172
173         lock_profile_release_lock(&m->mtx_object);
174         _rel_sleep_lock(m, curthread, opts, file, line);
175 }
176
177 void
178 _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
179 {
180         
181         MPASS(curthread != NULL);
182         KASSERT(m->mtx_lock != MTX_DESTROYED,
183             ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
184         KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
185             ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
186             m->mtx_object.lo_name, file, line));
187         WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
188             file, line);
189         _get_spin_lock(m, curthread, opts, file, line);
190         LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
191             line);
192         WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
193 }
194
195 void
196 _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
197 {
198
199         MPASS(curthread != NULL);
200         KASSERT(m->mtx_lock != MTX_DESTROYED,
201             ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
202         KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
203             ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
204             m->mtx_object.lo_name, file, line));
205         WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
206         LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
207             line);
208         mtx_assert(m, MA_OWNED);
209         lock_profile_release_lock(&m->mtx_object);
210         _rel_spin_lock(m);
211 }
212
213 /*
214  * The important part of mtx_trylock{,_flags}()
215  * Tries to acquire lock `m.'  If this function is called on a mutex that
216  * is already owned, it will recursively acquire the lock.
217  */
218 int
219 _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
220 {
221         int rval, contested = 0;
222         uint64_t waittime = 0;
223         
224         MPASS(curthread != NULL);
225         KASSERT(m->mtx_lock != MTX_DESTROYED,
226             ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
227         KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
228             ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
229             file, line));
230
231         if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
232                 m->mtx_recurse++;
233                 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
234                 rval = 1;
235         } else
236                 rval = _obtain_lock(m, (uintptr_t)curthread);
237
238         LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
239         if (rval) {
240                 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
241                     file, line);
242                 curthread->td_locks++;
243                 if (m->mtx_recurse == 0)
244                         lock_profile_obtain_lock_success(&m->mtx_object, contested,
245                             waittime, file, line);
246
247         }
248
249         return (rval);
250 }
251
252 /*
253  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
254  *
255  * We call this if the lock is either contested (i.e. we need to go to
256  * sleep waiting for it), or if we need to recurse on it.
257  */
258 void
259 _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
260     int line)
261 {
262 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
263         volatile struct thread *owner;
264 #endif
265 #ifdef KTR
266         int cont_logged = 0;
267 #endif
268         uintptr_t v;
269         
270         if (mtx_owned(m)) {
271                 KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
272             ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
273                     m->mtx_object.lo_name, file, line));
274                 m->mtx_recurse++;
275                 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
276                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
277                         CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
278                 return;
279         }
280
281         if (LOCK_LOG_TEST(&m->mtx_object, opts))
282                 CTR4(KTR_LOCK,
283                     "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
284                     m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
285
286         while (!_obtain_lock(m, tid)) { 
287                 turnstile_lock(&m->mtx_object);
288                 v = m->mtx_lock;
289
290                 /*
291                  * Check if the lock has been released while spinning for
292                  * the turnstile chain lock.
293                  */
294                 if (v == MTX_UNOWNED) {
295                         turnstile_release(&m->mtx_object);
296                         cpu_spinwait();
297                         continue;
298                 }
299
300 #ifdef MUTEX_WAKE_ALL
301                 MPASS(v != MTX_CONTESTED);
302 #else
303                 /*
304                  * The mutex was marked contested on release. This means that
305                  * there are other threads blocked on it.  Grab ownership of
306                  * it and propagate its priority to the current thread if
307                  * necessary.
308                  */
309                 if (v == MTX_CONTESTED) {
310                         m->mtx_lock = tid | MTX_CONTESTED;
311                         turnstile_claim(&m->mtx_object);
312                         break;
313                 }
314 #endif
315
316                 /*
317                  * If the mutex isn't already contested and a failure occurs
318                  * setting the contested bit, the mutex was either released
319                  * or the state of the MTX_RECURSED bit changed.
320                  */
321                 if ((v & MTX_CONTESTED) == 0 &&
322                     !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
323                         turnstile_release(&m->mtx_object);
324                         cpu_spinwait();
325                         continue;
326                 }
327
328 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
329                 /*
330                  * If the current owner of the lock is executing on another
331                  * CPU, spin instead of blocking.
332                  */
333                 owner = (struct thread *)(v & ~MTX_FLAGMASK);
334 #ifdef ADAPTIVE_GIANT
335                 if (TD_IS_RUNNING(owner)) 
336 #else
337                 if (m != &Giant && TD_IS_RUNNING(owner)) 
338 #endif
339                 {
340                         turnstile_release(&m->mtx_object);
341                         while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
342                                 cpu_spinwait();
343                         }
344                         continue;
345                 }
346 #endif  /* SMP && !NO_ADAPTIVE_MUTEXES */
347
348                 /*
349                  * We definitely must sleep for this lock.
350                  */
351                 mtx_assert(m, MA_NOTOWNED);
352
353 #ifdef KTR
354                 if (!cont_logged) {
355                         CTR6(KTR_CONTENTION,
356                             "contention: %p at %s:%d wants %s, taken by %s:%d",
357                             (void *)tid, file, line, m->mtx_object.lo_name,
358                             WITNESS_FILE(&m->mtx_object),
359                             WITNESS_LINE(&m->mtx_object));
360                         cont_logged = 1;
361                 }
362 #endif
363
364                 /*
365                  * Block on the turnstile.
366                  */
367                 turnstile_wait(&m->mtx_object, mtx_owner(m),
368                     TS_EXCLUSIVE_QUEUE);
369         }
370 #ifdef KTR
371         if (cont_logged) {
372                 CTR4(KTR_CONTENTION,
373                     "contention end: %s acquired by %p at %s:%d",
374                     m->mtx_object.lo_name, (void *)tid, file, line);
375         }
376 #endif
377         return;
378 }
379
380 #ifdef SMP
381 /*
382  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
383  *
384  * This is only called if we need to actually spin for the lock. Recursion
385  * is handled inline.
386  */
387 void
388 _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
389     int line)
390 {
391         int i = 0;
392         struct thread *td;
393
394         if (LOCK_LOG_TEST(&m->mtx_object, opts))
395                 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
396
397         while (!_obtain_lock(m, tid)) {
398
399                 /* Give interrupts a chance while we spin. */
400                 spinlock_exit();
401                 while (m->mtx_lock != MTX_UNOWNED) {
402                         if (i++ < 10000000) {
403                                 cpu_spinwait();
404                                 continue;
405                         }
406                         if (i < 60000000 || kdb_active || panicstr != NULL)
407                                 DELAY(1);
408                         else {
409                                 td = mtx_owner(m);
410
411                                 /* If the mutex is unlocked, try again. */
412                                 if (td == NULL)
413                                         continue;
414                                 printf(
415                         "spin lock %p (%s) held by %p (tid %d) too long\n",
416                                     m, m->mtx_object.lo_name, td, td->td_tid);
417 #ifdef WITNESS
418                                 witness_display_spinlock(&m->mtx_object, td);
419 #endif
420                                 panic("spin lock held too long");
421                         }
422                         cpu_spinwait();
423                 }
424                 spinlock_enter();
425         }
426
427         if (LOCK_LOG_TEST(&m->mtx_object, opts))
428                 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
429
430         return;
431 }
432 #endif /* SMP */
433
434 /*
435  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
436  *
437  * We are only called here if the lock is recursed or contested (i.e. we
438  * need to wake up a blocked thread).
439  */
440 void
441 _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
442 {
443         struct turnstile *ts;
444 #ifndef PREEMPTION
445         struct thread *td, *td1;
446 #endif
447
448         if (mtx_recursed(m)) {
449                 if (--(m->mtx_recurse) == 0)
450                         atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
451                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
452                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
453                 return;
454         }
455
456         turnstile_lock(&m->mtx_object);
457         ts = turnstile_lookup(&m->mtx_object);
458         if (LOCK_LOG_TEST(&m->mtx_object, opts))
459                 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
460
461 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
462         if (ts == NULL) {
463                 _release_lock_quick(m);
464                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
465                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
466                 turnstile_release(&m->mtx_object);
467                 return;
468         }
469 #else
470         MPASS(ts != NULL);
471 #endif
472 #ifndef PREEMPTION
473         /* XXX */
474         td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE);
475 #endif
476 #ifdef MUTEX_WAKE_ALL
477         turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
478         _release_lock_quick(m);
479 #else
480         if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) {
481                 _release_lock_quick(m);
482                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
483                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
484         } else {
485                 m->mtx_lock = MTX_CONTESTED;
486                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
487                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
488                             m);
489         }
490 #endif
491         turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
492
493 #ifndef PREEMPTION
494         /*
495          * XXX: This is just a hack until preemption is done.  However,
496          * once preemption is done we need to either wrap the
497          * turnstile_signal() and release of the actual lock in an
498          * extra critical section or change the preemption code to
499          * always just set a flag and never do instant-preempts.
500          */
501         td = curthread;
502         if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
503                 return;
504         mtx_lock_spin(&sched_lock);
505         if (!TD_IS_RUNNING(td1)) {
506 #ifdef notyet
507                 if (td->td_ithd != NULL) {
508                         struct ithd *it = td->td_ithd;
509
510                         if (it->it_interrupted) {
511                                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
512                                         CTR2(KTR_LOCK,
513                                     "_mtx_unlock_sleep: %p interrupted %p",
514                                             it, it->it_interrupted);
515                                 intr_thd_fixup(it);
516                         }
517                 }
518 #endif
519                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
520                         CTR2(KTR_LOCK,
521                             "_mtx_unlock_sleep: %p switching out lock=%p", m,
522                             (void *)m->mtx_lock);
523
524                 mi_switch(SW_INVOL, NULL);
525                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
526                         CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
527                             m, (void *)m->mtx_lock);
528         }
529         mtx_unlock_spin(&sched_lock);
530 #endif
531
532         return;
533 }
534
535 /*
536  * All the unlocking of MTX_SPIN locks is done inline.
537  * See the _rel_spin_lock() macro for the details.
538  */
539
540 /*
541  * The backing function for the INVARIANTS-enabled mtx_assert()
542  */
543 #ifdef INVARIANT_SUPPORT
544 void
545 _mtx_assert(struct mtx *m, int what, const char *file, int line)
546 {
547
548         if (panicstr != NULL || dumping)
549                 return;
550         switch (what) {
551         case MA_OWNED:
552         case MA_OWNED | MA_RECURSED:
553         case MA_OWNED | MA_NOTRECURSED:
554                 if (!mtx_owned(m))
555                         panic("mutex %s not owned at %s:%d",
556                             m->mtx_object.lo_name, file, line);
557                 if (mtx_recursed(m)) {
558                         if ((what & MA_NOTRECURSED) != 0)
559                                 panic("mutex %s recursed at %s:%d",
560                                     m->mtx_object.lo_name, file, line);
561                 } else if ((what & MA_RECURSED) != 0) {
562                         panic("mutex %s unrecursed at %s:%d",
563                             m->mtx_object.lo_name, file, line);
564                 }
565                 break;
566         case MA_NOTOWNED:
567                 if (mtx_owned(m))
568                         panic("mutex %s owned at %s:%d",
569                             m->mtx_object.lo_name, file, line);
570                 break;
571         default:
572                 panic("unknown mtx_assert at %s:%d", file, line);
573         }
574 }
575 #endif
576
577 /*
578  * The MUTEX_DEBUG-enabled mtx_validate()
579  *
580  * Most of these checks have been moved off into the LO_INITIALIZED flag
581  * maintained by the witness code.
582  */
583 #ifdef MUTEX_DEBUG
584
585 void    mtx_validate(struct mtx *);
586
587 void
588 mtx_validate(struct mtx *m)
589 {
590
591 /*
592  * XXX: When kernacc() does not require Giant we can reenable this check
593  */
594 #ifdef notyet
595         /*
596          * Can't call kernacc() from early init386(), especially when
597          * initializing Giant mutex, because some stuff in kernacc()
598          * requires Giant itself.
599          */
600         if (!cold)
601                 if (!kernacc((caddr_t)m, sizeof(m),
602                     VM_PROT_READ | VM_PROT_WRITE))
603                         panic("Can't read and write to mutex %p", m);
604 #endif
605 }
606 #endif
607
608 /*
609  * General init routine used by the MTX_SYSINIT() macro.
610  */
611 void
612 mtx_sysinit(void *arg)
613 {
614         struct mtx_args *margs = arg;
615
616         mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
617 }
618
619 /*
620  * Mutex initialization routine; initialize lock `m' of type contained in
621  * `opts' with options contained in `opts' and name `name.'  The optional
622  * lock type `type' is used as a general lock category name for use with
623  * witness.
624  */
625 void
626 mtx_init(struct mtx *m, const char *name, const char *type, int opts)
627 {
628         struct lock_class *class;
629         int flags;
630
631         MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
632                 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
633
634 #ifdef MUTEX_DEBUG
635         /* Diagnostic and error correction */
636         mtx_validate(m);
637 #endif
638
639         /* Determine lock class and lock flags. */
640         if (opts & MTX_SPIN)
641                 class = &lock_class_mtx_spin;
642         else
643                 class = &lock_class_mtx_sleep;
644         flags = 0;
645         if (opts & MTX_QUIET)
646                 flags |= LO_QUIET;
647         if (opts & MTX_RECURSE)
648                 flags |= LO_RECURSABLE;
649         if ((opts & MTX_NOWITNESS) == 0)
650                 flags |= LO_WITNESS;
651         if (opts & MTX_DUPOK)
652                 flags |= LO_DUPOK;
653         if (opts & MTX_NOPROFILE)
654                 flags |= LO_NOPROFILE;
655
656         /* Initialize mutex. */
657         m->mtx_lock = MTX_UNOWNED;
658         m->mtx_recurse = 0;
659
660         lock_profile_object_init(&m->mtx_object, class, name);
661         lock_init(&m->mtx_object, class, name, type, flags);
662 }
663
664 /*
665  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
666  * passed in as a flag here because if the corresponding mtx_init() was
667  * called with MTX_QUIET set, then it will already be set in the mutex's
668  * flags.
669  */
670 void
671 mtx_destroy(struct mtx *m)
672 {
673
674         if (!mtx_owned(m))
675                 MPASS(mtx_unowned(m));
676         else {
677                 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
678
679                 /* Perform the non-mtx related part of mtx_unlock_spin(). */
680                 if (LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin)
681                         spinlock_exit();
682                 else
683                         curthread->td_locks--;
684
685                 /* Tell witness this isn't locked to make it happy. */
686                 WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
687                     __LINE__);
688         }
689
690         m->mtx_lock = MTX_DESTROYED;
691         lock_profile_object_destroy(&m->mtx_object);
692         lock_destroy(&m->mtx_object);
693 }
694
695 /*
696  * Intialize the mutex code and system mutexes.  This is called from the MD
697  * startup code prior to mi_startup().  The per-CPU data space needs to be
698  * setup before this is called.
699  */
700 void
701 mutex_init(void)
702 {
703
704         /* Setup turnstiles so that sleep mutexes work. */
705         init_turnstiles();
706
707         /*
708          * Initialize mutexes.
709          */
710         mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
711         mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
712         mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
713         mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
714         mtx_lock(&Giant);
715         
716         lock_profile_init();
717 }
718
719 #ifdef DDB
720 void
721 db_show_mtx(struct lock_object *lock)
722 {
723         struct thread *td;
724         struct mtx *m;
725
726         m = (struct mtx *)lock;
727
728         db_printf(" flags: {");
729         if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
730                 db_printf("SPIN");
731         else
732                 db_printf("DEF");
733         if (m->mtx_object.lo_flags & LO_RECURSABLE)
734                 db_printf(", RECURSE");
735         if (m->mtx_object.lo_flags & LO_DUPOK)
736                 db_printf(", DUPOK");
737         db_printf("}\n");
738         db_printf(" state: {");
739         if (mtx_unowned(m))
740                 db_printf("UNOWNED");
741         else {
742                 db_printf("OWNED");
743                 if (m->mtx_lock & MTX_CONTESTED)
744                         db_printf(", CONTESTED");
745                 if (m->mtx_lock & MTX_RECURSED)
746                         db_printf(", RECURSED");
747         }
748         db_printf("}\n");
749         if (!mtx_unowned(m)) {
750                 td = mtx_owner(m);
751                 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
752                     td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
753                 if (mtx_recursed(m))
754                         db_printf(" recursed: %d\n", m->mtx_recurse);
755         }
756 }
757 #endif