kern_mutex.c revision 1.27 1 /* $NetBSD: kern_mutex.c,v 1.27 2007/12/24 14:57:56 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2006, 2007 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 and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Kernel mutex implementation, modeled after those found in Solaris,
41 * a description of which can be found in:
42 *
43 * Solaris Internals: Core Kernel Architecture, Jim Mauro and
44 * Richard McDougall.
45 */
46
47 #define __MUTEX_PRIVATE
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.27 2007/12/24 14:57:56 ad Exp $");
51
52 #include "opt_multiprocessor.h"
53
54 #include <sys/param.h>
55 #include <sys/proc.h>
56 #include <sys/mutex.h>
57 #include <sys/sched.h>
58 #include <sys/sleepq.h>
59 #include <sys/systm.h>
60 #include <sys/lockdebug.h>
61 #include <sys/kernel.h>
62 #include <sys/atomic.h>
63 #include <sys/intr.h>
64
65 #include <dev/lockstat.h>
66
67 /*
68 * When not running a debug kernel, spin mutexes are not much
69 * more than an splraiseipl() and splx() pair.
70 */
71
72 #if defined(DIAGNOSTIC) || defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
73 #define FULL
74 #endif
75
76 /*
77 * Debugging support.
78 */
79
80 #define MUTEX_WANTLOCK(mtx) \
81 LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx), \
82 (uintptr_t)__builtin_return_address(0), 0)
83 #define MUTEX_LOCKED(mtx) \
84 LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), \
85 (uintptr_t)__builtin_return_address(0), 0)
86 #define MUTEX_UNLOCKED(mtx) \
87 LOCKDEBUG_UNLOCKED(MUTEX_DEBUG_P(mtx), (mtx), \
88 (uintptr_t)__builtin_return_address(0), 0)
89 #define MUTEX_ABORT(mtx, msg) \
90 mutex_abort(mtx, __func__, msg)
91
92 #if defined(LOCKDEBUG)
93
94 #define MUTEX_DASSERT(mtx, cond) \
95 do { \
96 if (!(cond)) \
97 MUTEX_ABORT(mtx, "assertion failed: " #cond); \
98 } while (/* CONSTCOND */ 0);
99
100 #else /* LOCKDEBUG */
101
102 #define MUTEX_DASSERT(mtx, cond) /* nothing */
103
104 #endif /* LOCKDEBUG */
105
106 #if defined(DIAGNOSTIC)
107
108 #define MUTEX_ASSERT(mtx, cond) \
109 do { \
110 if (!(cond)) \
111 MUTEX_ABORT(mtx, "assertion failed: " #cond); \
112 } while (/* CONSTCOND */ 0)
113
114 #else /* DIAGNOSTIC */
115
116 #define MUTEX_ASSERT(mtx, cond) /* nothing */
117
118 #endif /* DIAGNOSTIC */
119
120 /*
121 * Spin mutex SPL save / restore.
122 */
123 #ifndef MUTEX_COUNT_BIAS
124 #define MUTEX_COUNT_BIAS 0
125 #endif
126
127 #define MUTEX_SPIN_SPLRAISE(mtx) \
128 do { \
129 struct cpu_info *x__ci = curcpu(); \
130 int x__cnt, s; \
131 x__cnt = x__ci->ci_mtx_count--; \
132 s = splraiseipl(mtx->mtx_ipl); \
133 if (x__cnt == MUTEX_COUNT_BIAS) \
134 x__ci->ci_mtx_oldspl = (s); \
135 } while (/* CONSTCOND */ 0)
136
137 #define MUTEX_SPIN_SPLRESTORE(mtx) \
138 do { \
139 struct cpu_info *x__ci = curcpu(); \
140 int s = x__ci->ci_mtx_oldspl; \
141 __insn_barrier(); \
142 if (++(x__ci->ci_mtx_count) == MUTEX_COUNT_BIAS) \
143 splx(s); \
144 } while (/* CONSTCOND */ 0)
145
146 /*
147 * For architectures that provide 'simple' mutexes: they provide a
148 * CAS function that is either MP-safe, or does not need to be MP
149 * safe. Adaptive mutexes on these architectures do not require an
150 * additional interlock.
151 */
152
153 #ifdef __HAVE_SIMPLE_MUTEXES
154
155 #define MUTEX_OWNER(owner) \
156 (owner & MUTEX_THREAD)
157 #define MUTEX_HAS_WAITERS(mtx) \
158 (((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0)
159
160 #define MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug) \
161 do { \
162 if (dodebug) \
163 (mtx)->mtx_owner |= MUTEX_BIT_DEBUG; \
164 } while (/* CONSTCOND */ 0);
165
166 #define MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl) \
167 do { \
168 (mtx)->mtx_owner = MUTEX_BIT_SPIN; \
169 if (dodebug) \
170 (mtx)->mtx_owner |= MUTEX_BIT_DEBUG; \
171 (mtx)->mtx_ipl = makeiplcookie((ipl)); \
172 __cpu_simple_lock_init(&(mtx)->mtx_lock); \
173 } while (/* CONSTCOND */ 0)
174
175 #define MUTEX_DESTROY(mtx) \
176 do { \
177 (mtx)->mtx_owner = MUTEX_THREAD; \
178 } while (/* CONSTCOND */ 0);
179
180 #define MUTEX_SPIN_P(mtx) \
181 (((mtx)->mtx_owner & MUTEX_BIT_SPIN) != 0)
182 #define MUTEX_ADAPTIVE_P(mtx) \
183 (((mtx)->mtx_owner & MUTEX_BIT_SPIN) == 0)
184
185 #define MUTEX_DEBUG_P(mtx) (((mtx)->mtx_owner & MUTEX_BIT_DEBUG) != 0)
186 #if defined(LOCKDEBUG)
187 #define MUTEX_OWNED(owner) (((owner) & ~MUTEX_BIT_DEBUG) != 0)
188 #define MUTEX_INHERITDEBUG(new, old) (new) |= (old) & MUTEX_BIT_DEBUG
189 #else /* defined(LOCKDEBUG) */
190 #define MUTEX_OWNED(owner) ((owner) != 0)
191 #define MUTEX_INHERITDEBUG(new, old) /* nothing */
192 #endif /* defined(LOCKDEBUG) */
193
194 static inline int
195 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
196 {
197 int rv;
198 uintptr_t old = 0;
199 uintptr_t new = curthread;
200
201 MUTEX_INHERITDEBUG(old, mtx->mtx_owner);
202 MUTEX_INHERITDEBUG(new, old);
203 rv = MUTEX_CAS(&mtx->mtx_owner, old, new);
204 MUTEX_RECEIVE(mtx);
205 return rv;
206 }
207
208 static inline int
209 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
210 {
211 int rv;
212 rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS);
213 MUTEX_RECEIVE(mtx);
214 return rv;
215 }
216
217 static inline void
218 MUTEX_RELEASE(kmutex_t *mtx)
219 {
220 uintptr_t new;
221
222 MUTEX_GIVE(mtx);
223 new = 0;
224 MUTEX_INHERITDEBUG(new, mtx->mtx_owner);
225 mtx->mtx_owner = new;
226 }
227
228 static inline void
229 MUTEX_CLEAR_WAITERS(kmutex_t *mtx)
230 {
231 /* nothing */
232 }
233 #endif /* __HAVE_SIMPLE_MUTEXES */
234
235 /*
236 * Patch in stubs via strong alias where they are not available.
237 */
238
239 #if defined(LOCKDEBUG)
240 #undef __HAVE_MUTEX_STUBS
241 #undef __HAVE_SPIN_MUTEX_STUBS
242 #endif
243
244 #ifndef __HAVE_MUTEX_STUBS
245 __strong_alias(mutex_enter,mutex_vector_enter);
246 __strong_alias(mutex_exit,mutex_vector_exit);
247 #endif
248
249 #ifndef __HAVE_SPIN_MUTEX_STUBS
250 __strong_alias(mutex_spin_enter,mutex_vector_enter);
251 __strong_alias(mutex_spin_exit,mutex_vector_exit);
252 #endif
253
254 void mutex_abort(kmutex_t *, const char *, const char *);
255 void mutex_dump(volatile void *);
256 int mutex_onproc(uintptr_t, struct cpu_info **);
257
258 lockops_t mutex_spin_lockops = {
259 "Mutex",
260 0,
261 mutex_dump
262 };
263
264 lockops_t mutex_adaptive_lockops = {
265 "Mutex",
266 1,
267 mutex_dump
268 };
269
270 syncobj_t mutex_syncobj = {
271 SOBJ_SLEEPQ_SORTED,
272 turnstile_unsleep,
273 turnstile_changepri,
274 sleepq_lendpri,
275 (void *)mutex_owner,
276 };
277
278 /*
279 * mutex_dump:
280 *
281 * Dump the contents of a mutex structure.
282 */
283 void
284 mutex_dump(volatile void *cookie)
285 {
286 volatile kmutex_t *mtx = cookie;
287
288 printf_nolog("owner field : %#018lx wait/spin: %16d/%d\n",
289 (long)MUTEX_OWNER(mtx->mtx_owner), MUTEX_HAS_WAITERS(mtx),
290 MUTEX_SPIN_P(mtx));
291 }
292
293 /*
294 * mutex_abort:
295 *
296 * Dump information about an error and panic the system. This
297 * generates a lot of machine code in the DIAGNOSTIC case, so
298 * we ask the compiler to not inline it.
299 */
300
301 #if __GNUC_PREREQ__(3, 0)
302 __attribute ((noinline)) __attribute ((noreturn))
303 #endif
304 void
305 mutex_abort(kmutex_t *mtx, const char *func, const char *msg)
306 {
307
308 LOCKDEBUG_ABORT(mtx, (MUTEX_SPIN_P(mtx) ?
309 &mutex_spin_lockops : &mutex_adaptive_lockops), func, msg);
310 /* NOTREACHED */
311 }
312
313 /*
314 * mutex_init:
315 *
316 * Initialize a mutex for use. Note that adaptive mutexes are in
317 * essence spin mutexes that can sleep to avoid deadlock and wasting
318 * CPU time. We can't easily provide a type of mutex that always
319 * sleeps - see comments in mutex_vector_enter() about releasing
320 * mutexes unlocked.
321 */
322 void
323 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
324 {
325 bool dodebug;
326
327 memset(mtx, 0, sizeof(*mtx));
328
329 switch (type) {
330 case MUTEX_ADAPTIVE:
331 KASSERT(ipl == IPL_NONE);
332 break;
333 case MUTEX_DEFAULT:
334 case MUTEX_DRIVER:
335 if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
336 ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
337 ipl == IPL_SOFTSERIAL) {
338 type = MUTEX_ADAPTIVE;
339 } else {
340 type = MUTEX_SPIN;
341 }
342 break;
343 default:
344 break;
345 }
346
347 switch (type) {
348 case MUTEX_NODEBUG:
349 dodebug = LOCKDEBUG_ALLOC(mtx, NULL,
350 (uintptr_t)__builtin_return_address(0));
351 MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
352 break;
353 case MUTEX_ADAPTIVE:
354 dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_adaptive_lockops,
355 (uintptr_t)__builtin_return_address(0));
356 MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug);
357 break;
358 case MUTEX_SPIN:
359 dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_spin_lockops,
360 (uintptr_t)__builtin_return_address(0));
361 MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
362 break;
363 default:
364 panic("mutex_init: impossible type");
365 break;
366 }
367 }
368
369 /*
370 * mutex_destroy:
371 *
372 * Tear down a mutex.
373 */
374 void
375 mutex_destroy(kmutex_t *mtx)
376 {
377
378 if (MUTEX_ADAPTIVE_P(mtx)) {
379 MUTEX_ASSERT(mtx, !MUTEX_OWNED(mtx->mtx_owner) &&
380 !MUTEX_HAS_WAITERS(mtx));
381 } else {
382 MUTEX_ASSERT(mtx, !__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock));
383 }
384
385 LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx), mtx);
386 MUTEX_DESTROY(mtx);
387 }
388
389 /*
390 * mutex_onproc:
391 *
392 * Return true if an adaptive mutex owner is running on a CPU in the
393 * system. If the target is waiting on the kernel big lock, then we
394 * must release it. This is necessary to avoid deadlock.
395 *
396 * Note that we can't use the mutex owner field as an LWP pointer. We
397 * don't have full control over the timing of our execution, and so the
398 * pointer could be completely invalid by the time we dereference it.
399 */
400 #ifdef MULTIPROCESSOR
401 int
402 mutex_onproc(uintptr_t owner, struct cpu_info **cip)
403 {
404 CPU_INFO_ITERATOR cii;
405 struct cpu_info *ci;
406 struct lwp *l;
407
408 if (!MUTEX_OWNED(owner))
409 return 0;
410 l = (struct lwp *)MUTEX_OWNER(owner);
411
412 /* See if the target is running on a CPU somewhere. */
413 if ((ci = *cip) != NULL && ci->ci_curlwp == l)
414 goto run;
415 for (CPU_INFO_FOREACH(cii, ci))
416 if (ci->ci_curlwp == l)
417 goto run;
418
419 /* No: it may be safe to block now. */
420 *cip = NULL;
421 return 0;
422
423 run:
424 /* Target is running; do we need to block? */
425 *cip = ci;
426 return ci->ci_biglock_wanted != l;
427 }
428 #endif /* MULTIPROCESSOR */
429
430 /*
431 * mutex_vector_enter:
432 *
433 * Support routine for mutex_enter() that must handles all cases. In
434 * the LOCKDEBUG case, mutex_enter() is always aliased here, even if
435 * fast-path stubs are available. If an mutex_spin_enter() stub is
436 * not available, then it is also aliased directly here.
437 */
438 void
439 mutex_vector_enter(kmutex_t *mtx)
440 {
441 uintptr_t owner, curthread;
442 turnstile_t *ts;
443 #ifdef MULTIPROCESSOR
444 struct cpu_info *ci = NULL;
445 u_int count;
446 #endif
447 LOCKSTAT_COUNTER(spincnt);
448 LOCKSTAT_COUNTER(slpcnt);
449 LOCKSTAT_TIMER(spintime);
450 LOCKSTAT_TIMER(slptime);
451 LOCKSTAT_FLAG(lsflag);
452
453 /*
454 * Handle spin mutexes.
455 */
456 if (MUTEX_SPIN_P(mtx)) {
457 #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR)
458 u_int spins = 0;
459 #endif
460 MUTEX_SPIN_SPLRAISE(mtx);
461 MUTEX_WANTLOCK(mtx);
462 #ifdef FULL
463 if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
464 MUTEX_LOCKED(mtx);
465 return;
466 }
467 #if !defined(MULTIPROCESSOR)
468 MUTEX_ABORT(mtx, "locking against myself");
469 #else /* !MULTIPROCESSOR */
470
471 LOCKSTAT_ENTER(lsflag);
472 LOCKSTAT_START_TIMER(lsflag, spintime);
473 count = SPINLOCK_BACKOFF_MIN;
474
475 /*
476 * Spin testing the lock word and do exponential backoff
477 * to reduce cache line ping-ponging between CPUs.
478 */
479 do {
480 if (panicstr != NULL)
481 break;
482 while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
483 SPINLOCK_BACKOFF(count);
484 #ifdef LOCKDEBUG
485 if (SPINLOCK_SPINOUT(spins))
486 MUTEX_ABORT(mtx, "spinout");
487 #endif /* LOCKDEBUG */
488 }
489 } while (!__cpu_simple_lock_try(&mtx->mtx_lock));
490
491 if (count != SPINLOCK_BACKOFF_MIN) {
492 LOCKSTAT_STOP_TIMER(lsflag, spintime);
493 LOCKSTAT_EVENT(lsflag, mtx,
494 LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
495 }
496 LOCKSTAT_EXIT(lsflag);
497 #endif /* !MULTIPROCESSOR */
498 #endif /* FULL */
499 MUTEX_LOCKED(mtx);
500 return;
501 }
502
503 curthread = (uintptr_t)curlwp;
504
505 MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
506 MUTEX_ASSERT(mtx, curthread != 0);
507 MUTEX_WANTLOCK(mtx);
508
509 #ifdef LOCKDEBUG
510 if (panicstr == NULL) {
511 simple_lock_only_held(NULL, "mutex_enter");
512 #ifdef MULTIPROCESSOR
513 LOCKDEBUG_BARRIER(&kernel_lock, 1);
514 #else
515 LOCKDEBUG_BARRIER(NULL, 1);
516 #endif
517 }
518 #endif
519
520 LOCKSTAT_ENTER(lsflag);
521
522 /*
523 * Adaptive mutex; spin trying to acquire the mutex. If we
524 * determine that the owner is not running on a processor,
525 * then we stop spinning, and sleep instead.
526 */
527 for (;;) {
528 owner = mtx->mtx_owner;
529 if (!MUTEX_OWNED(owner)) {
530 /*
531 * Mutex owner clear could mean two things:
532 *
533 * * The mutex has been released.
534 * * The owner field hasn't been set yet.
535 *
536 * Try to acquire it again. If that fails,
537 * we'll just loop again.
538 */
539 if (MUTEX_ACQUIRE(mtx, curthread))
540 break;
541 continue;
542 }
543
544 if (panicstr != NULL)
545 return;
546 if (MUTEX_OWNER(owner) == curthread)
547 MUTEX_ABORT(mtx, "locking against myself");
548
549 #ifdef MULTIPROCESSOR
550 /*
551 * Check to see if the owner is running on a processor.
552 * If so, then we should just spin, as the owner will
553 * likely release the lock very soon.
554 */
555 if (mutex_onproc(owner, &ci)) {
556 LOCKSTAT_START_TIMER(lsflag, spintime);
557 count = SPINLOCK_BACKOFF_MIN;
558 for (;;) {
559 owner = mtx->mtx_owner;
560 if (!mutex_onproc(owner, &ci))
561 break;
562 SPINLOCK_BACKOFF(count);
563 }
564 LOCKSTAT_STOP_TIMER(lsflag, spintime);
565 LOCKSTAT_COUNT(spincnt, 1);
566 if (!MUTEX_OWNED(owner))
567 continue;
568 }
569 #endif
570
571 ts = turnstile_lookup(mtx);
572
573 /*
574 * Once we have the turnstile chain interlock, mark the
575 * mutex has having waiters. If that fails, spin again:
576 * chances are that the mutex has been released.
577 */
578 if (!MUTEX_SET_WAITERS(mtx, owner)) {
579 turnstile_exit(mtx);
580 continue;
581 }
582
583 #ifdef MULTIPROCESSOR
584 /*
585 * mutex_exit() is permitted to release the mutex without
586 * any interlocking instructions, and the following can
587 * occur as a result:
588 *
589 * CPU 1: MUTEX_SET_WAITERS() CPU2: mutex_exit()
590 * ---------------------------- ----------------------------
591 * .. acquire cache line
592 * .. test for waiters
593 * acquire cache line <- lose cache line
594 * lock cache line ..
595 * verify mutex is held ..
596 * set waiters ..
597 * unlock cache line ..
598 * lose cache line -> acquire cache line
599 * .. clear lock word, waiters
600 * return success
601 *
602 * There is a another race that can occur: a third CPU could
603 * acquire the mutex as soon as it is released. Since
604 * adaptive mutexes are primarily spin mutexes, this is not
605 * something that we need to worry about too much. What we
606 * do need to ensure is that the waiters bit gets set.
607 *
608 * To allow the unlocked release, we need to make some
609 * assumptions here:
610 *
611 * o Release is the only non-atomic/unlocked operation
612 * that can be performed on the mutex. (It must still
613 * be atomic on the local CPU, e.g. in case interrupted
614 * or preempted).
615 *
616 * o At any given time, MUTEX_SET_WAITERS() can only ever
617 * be in progress on one CPU in the system - guaranteed
618 * by the turnstile chain lock.
619 *
620 * o No other operations other than MUTEX_SET_WAITERS()
621 * and release can modify a mutex with a non-zero
622 * owner field.
623 *
624 * o The result of a successful MUTEX_SET_WAITERS() call
625 * is an unbuffered write that is immediately visible
626 * to all other processors in the system.
627 *
628 * o If the holding LWP switches away, it posts a store
629 * fence before changing curlwp, ensuring that any
630 * overwrite of the mutex waiters flag by mutex_exit()
631 * completes before the modification of curlwp becomes
632 * visible to this CPU.
633 *
634 * o mi_switch() posts a store fence before setting curlwp
635 * and before resuming execution of an LWP.
636 *
637 * o _kernel_lock() posts a store fence before setting
638 * curcpu()->ci_biglock_wanted, and after clearing it.
639 * This ensures that any overwrite of the mutex waiters
640 * flag by mutex_exit() completes before the modification
641 * of ci_biglock_wanted becomes visible.
642 *
643 * We now post a read memory barrier (after setting the
644 * waiters field) and check the lock holder's status again.
645 * Some of the possible outcomes (not an exhaustive list):
646 *
647 * 1. The onproc check returns true: the holding LWP is
648 * running again. The lock may be released soon and
649 * we should spin. Importantly, we can't trust the
650 * value of the waiters flag.
651 *
652 * 2. The onproc check returns false: the holding LWP is
653 * not running. We now have the oppertunity to check
654 * if mutex_exit() has blatted the modifications made
655 * by MUTEX_SET_WAITERS().
656 *
657 * 3. The onproc check returns false: the holding LWP may
658 * or may not be running. It has context switched at
659 * some point during our check. Again, we have the
660 * chance to see if the waiters bit is still set or
661 * has been overwritten.
662 *
663 * 4. The onproc check returns false: the holding LWP is
664 * running on a CPU, but wants the big lock. It's OK
665 * to check the waiters field in this case.
666 *
667 * 5. The has-waiters check fails: the mutex has been
668 * released, the waiters flag cleared and another LWP
669 * now owns the mutex.
670 *
671 * 6. The has-waiters check fails: the mutex has been
672 * released.
673 *
674 * If the waiters bit is not set it's unsafe to go asleep,
675 * as we might never be awoken.
676 */
677 if ((membar_consumer(), mutex_onproc(owner, &ci)) ||
678 (membar_consumer(), !MUTEX_HAS_WAITERS(mtx))) {
679 turnstile_exit(mtx);
680 continue;
681 }
682 #endif /* MULTIPROCESSOR */
683
684 LOCKSTAT_START_TIMER(lsflag, slptime);
685
686 turnstile_block(ts, TS_WRITER_Q, mtx, &mutex_syncobj);
687
688 LOCKSTAT_STOP_TIMER(lsflag, slptime);
689 LOCKSTAT_COUNT(slpcnt, 1);
690 }
691
692 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1,
693 slpcnt, slptime);
694 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN,
695 spincnt, spintime);
696 LOCKSTAT_EXIT(lsflag);
697
698 MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
699 MUTEX_LOCKED(mtx);
700 }
701
702 /*
703 * mutex_vector_exit:
704 *
705 * Support routine for mutex_exit() that handles all cases.
706 */
707 void
708 mutex_vector_exit(kmutex_t *mtx)
709 {
710 turnstile_t *ts;
711 uintptr_t curthread;
712
713 if (MUTEX_SPIN_P(mtx)) {
714 #ifdef FULL
715 if (!__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock))
716 MUTEX_ABORT(mtx, "exiting unheld spin mutex");
717 MUTEX_UNLOCKED(mtx);
718 __cpu_simple_unlock(&mtx->mtx_lock);
719 #endif
720 MUTEX_SPIN_SPLRESTORE(mtx);
721 return;
722 }
723
724 if (__predict_false((uintptr_t)panicstr | cold)) {
725 MUTEX_UNLOCKED(mtx);
726 MUTEX_RELEASE(mtx);
727 return;
728 }
729
730 curthread = (uintptr_t)curlwp;
731 MUTEX_DASSERT(mtx, curthread != 0);
732 MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
733 MUTEX_UNLOCKED(mtx);
734
735 #ifdef LOCKDEBUG
736 /*
737 * Avoid having to take the turnstile chain lock every time
738 * around. Raise the priority level to splhigh() in order
739 * to disable preemption and so make the following atomic.
740 */
741 {
742 int s = splhigh();
743 if (!MUTEX_HAS_WAITERS(mtx)) {
744 MUTEX_RELEASE(mtx);
745 splx(s);
746 return;
747 }
748 splx(s);
749 }
750 #endif
751
752 /*
753 * Get this lock's turnstile. This gets the interlock on
754 * the sleep queue. Once we have that, we can clear the
755 * lock. If there was no turnstile for the lock, there
756 * were no waiters remaining.
757 */
758 ts = turnstile_lookup(mtx);
759
760 if (ts == NULL) {
761 MUTEX_RELEASE(mtx);
762 turnstile_exit(mtx);
763 } else {
764 MUTEX_RELEASE(mtx);
765 turnstile_wakeup(ts, TS_WRITER_Q,
766 TS_WAITERS(ts, TS_WRITER_Q), NULL);
767 }
768 }
769
770 #ifndef __HAVE_SIMPLE_MUTEXES
771 /*
772 * mutex_wakeup:
773 *
774 * Support routine for mutex_exit() that wakes up all waiters.
775 * We assume that the mutex has been released, but it need not
776 * be.
777 */
778 void
779 mutex_wakeup(kmutex_t *mtx)
780 {
781 turnstile_t *ts;
782
783 ts = turnstile_lookup(mtx);
784 if (ts == NULL) {
785 turnstile_exit(mtx);
786 return;
787 }
788 MUTEX_CLEAR_WAITERS(mtx);
789 turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL);
790 }
791 #endif /* !__HAVE_SIMPLE_MUTEXES */
792
793 /*
794 * mutex_owned:
795 *
796 * Return true if the current LWP (adaptive) or CPU (spin)
797 * holds the mutex.
798 */
799 int
800 mutex_owned(kmutex_t *mtx)
801 {
802
803 if (MUTEX_ADAPTIVE_P(mtx))
804 return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp;
805 #ifdef FULL
806 return __SIMPLELOCK_LOCKED_P(&mtx->mtx_lock);
807 #else
808 return 1;
809 #endif
810 }
811
812 /*
813 * mutex_owner:
814 *
815 * Return the current owner of an adaptive mutex. Used for
816 * priority inheritance.
817 */
818 lwp_t *
819 mutex_owner(kmutex_t *mtx)
820 {
821
822 MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
823 return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner);
824 }
825
826 /*
827 * mutex_tryenter:
828 *
829 * Try to acquire the mutex; return non-zero if we did.
830 */
831 int
832 mutex_tryenter(kmutex_t *mtx)
833 {
834 uintptr_t curthread;
835
836 /*
837 * Handle spin mutexes.
838 */
839 if (MUTEX_SPIN_P(mtx)) {
840 MUTEX_SPIN_SPLRAISE(mtx);
841 #ifdef FULL
842 if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
843 MUTEX_WANTLOCK(mtx);
844 MUTEX_LOCKED(mtx);
845 return 1;
846 }
847 MUTEX_SPIN_SPLRESTORE(mtx);
848 #else
849 MUTEX_WANTLOCK(mtx);
850 MUTEX_LOCKED(mtx);
851 return 1;
852 #endif
853 } else {
854 curthread = (uintptr_t)curlwp;
855 MUTEX_ASSERT(mtx, curthread != 0);
856 if (MUTEX_ACQUIRE(mtx, curthread)) {
857 MUTEX_WANTLOCK(mtx);
858 MUTEX_LOCKED(mtx);
859 MUTEX_DASSERT(mtx,
860 MUTEX_OWNER(mtx->mtx_owner) == curthread);
861 return 1;
862 }
863 }
864
865 return 0;
866 }
867
868 #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
869 /*
870 * mutex_spin_retry:
871 *
872 * Support routine for mutex_spin_enter(). Assumes that the caller
873 * has already raised the SPL, and adjusted counters.
874 */
875 void
876 mutex_spin_retry(kmutex_t *mtx)
877 {
878 #ifdef MULTIPROCESSOR
879 u_int count;
880 LOCKSTAT_TIMER(spintime);
881 LOCKSTAT_FLAG(lsflag);
882 #ifdef LOCKDEBUG
883 u_int spins = 0;
884 #endif /* LOCKDEBUG */
885
886 MUTEX_WANTLOCK(mtx);
887
888 LOCKSTAT_ENTER(lsflag);
889 LOCKSTAT_START_TIMER(lsflag, spintime);
890 count = SPINLOCK_BACKOFF_MIN;
891
892 /*
893 * Spin testing the lock word and do exponential backoff
894 * to reduce cache line ping-ponging between CPUs.
895 */
896 do {
897 if (panicstr != NULL)
898 break;
899 while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
900 SPINLOCK_BACKOFF(count);
901 #ifdef LOCKDEBUG
902 if (SPINLOCK_SPINOUT(spins))
903 MUTEX_ABORT(mtx, "spinout");
904 #endif /* LOCKDEBUG */
905 }
906 } while (!__cpu_simple_lock_try(&mtx->mtx_lock));
907
908 LOCKSTAT_STOP_TIMER(lsflag, spintime);
909 LOCKSTAT_EVENT(lsflag, mtx, LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
910 LOCKSTAT_EXIT(lsflag);
911
912 MUTEX_LOCKED(mtx);
913 #else /* MULTIPROCESSOR */
914 MUTEX_ABORT(mtx, "locking against myself");
915 #endif /* MULTIPROCESSOR */
916 }
917 #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */
918