kern_mutex.c revision 1.22 1 /* $NetBSD: kern_mutex.c,v 1.22 2007/11/07 00:23:22 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.22 2007/11/07 00:23:22 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
63 #include <dev/lockstat.h>
64
65 #include <sys/intr.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_GETID(mtx), \
82 (uintptr_t)__builtin_return_address(0), 0)
83 #define MUTEX_LOCKED(mtx) \
84 LOCKDEBUG_LOCKED(MUTEX_GETID(mtx), \
85 (uintptr_t)__builtin_return_address(0), 0)
86 #define MUTEX_UNLOCKED(mtx) \
87 LOCKDEBUG_UNLOCKED(MUTEX_GETID(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_OWNED(owner) \
158 (owner != 0)
159 #define MUTEX_HAS_WAITERS(mtx) \
160 (((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0)
161
162 #define MUTEX_INITIALIZE_ADAPTIVE(mtx, id) \
163 do { \
164 (mtx)->mtx_id = (id); \
165 } while (/* CONSTCOND */ 0);
166
167 #define MUTEX_INITIALIZE_SPIN(mtx, id, ipl) \
168 do { \
169 (mtx)->mtx_owner = MUTEX_BIT_SPIN; \
170 (mtx)->mtx_ipl = makeiplcookie((ipl)); \
171 (mtx)->mtx_id = (id); \
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 (mtx)->mtx_id = -1; \
179 } while (/* CONSTCOND */ 0);
180
181 #define MUTEX_SPIN_P(mtx) \
182 (((mtx)->mtx_owner & MUTEX_BIT_SPIN) != 0)
183 #define MUTEX_ADAPTIVE_P(mtx) \
184 (((mtx)->mtx_owner & MUTEX_BIT_SPIN) == 0)
185
186 #define MUTEX_GETID(mtx) ((mtx)->mtx_id)
187
188 static inline int
189 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
190 {
191 int rv;
192 rv = MUTEX_CAS(&mtx->mtx_owner, 0UL, curthread);
193 MUTEX_RECEIVE(mtx);
194 return rv;
195 }
196
197 static inline int
198 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
199 {
200 int rv;
201 rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS);
202 MUTEX_RECEIVE(mtx);
203 return rv;
204 }
205
206 static inline void
207 MUTEX_RELEASE(kmutex_t *mtx)
208 {
209 MUTEX_GIVE(mtx);
210 mtx->mtx_owner = 0;
211 }
212
213 static inline void
214 MUTEX_CLEAR_WAITERS(kmutex_t *mtx)
215 {
216 /* nothing */
217 }
218 #endif /* __HAVE_SIMPLE_MUTEXES */
219
220 /*
221 * Patch in stubs via strong alias where they are not available.
222 */
223
224 #if defined(LOCKDEBUG)
225 #undef __HAVE_MUTEX_STUBS
226 #undef __HAVE_SPIN_MUTEX_STUBS
227 #endif
228
229 #ifndef __HAVE_MUTEX_STUBS
230 __strong_alias(mutex_enter,mutex_vector_enter);
231 __strong_alias(mutex_exit,mutex_vector_exit);
232 #endif
233
234 #ifndef __HAVE_SPIN_MUTEX_STUBS
235 __strong_alias(mutex_spin_enter,mutex_vector_enter);
236 __strong_alias(mutex_spin_exit,mutex_vector_exit);
237 #endif
238
239 void mutex_abort(kmutex_t *, const char *, const char *);
240 void mutex_dump(volatile void *);
241 int mutex_onproc(uintptr_t, struct cpu_info **);
242 static struct lwp *mutex_owner(wchan_t);
243
244 lockops_t mutex_spin_lockops = {
245 "Mutex",
246 0,
247 mutex_dump
248 };
249
250 lockops_t mutex_adaptive_lockops = {
251 "Mutex",
252 1,
253 mutex_dump
254 };
255
256 syncobj_t mutex_syncobj = {
257 SOBJ_SLEEPQ_SORTED,
258 turnstile_unsleep,
259 turnstile_changepri,
260 sleepq_lendpri,
261 mutex_owner,
262 };
263
264 /*
265 * mutex_dump:
266 *
267 * Dump the contents of a mutex structure.
268 */
269 void
270 mutex_dump(volatile void *cookie)
271 {
272 volatile kmutex_t *mtx = cookie;
273
274 printf_nolog("owner field : %#018lx wait/spin: %16d/%d\n",
275 (long)MUTEX_OWNER(mtx->mtx_owner), MUTEX_HAS_WAITERS(mtx),
276 MUTEX_SPIN_P(mtx));
277 }
278
279 /*
280 * mutex_abort:
281 *
282 * Dump information about an error and panic the system. This
283 * generates a lot of machine code in the DIAGNOSTIC case, so
284 * we ask the compiler to not inline it.
285 */
286
287 #if __GNUC_PREREQ__(3, 0)
288 __attribute ((noinline)) __attribute ((noreturn))
289 #endif
290 void
291 mutex_abort(kmutex_t *mtx, const char *func, const char *msg)
292 {
293
294 LOCKDEBUG_ABORT(MUTEX_GETID(mtx), mtx, (MUTEX_SPIN_P(mtx) ?
295 &mutex_spin_lockops : &mutex_adaptive_lockops), func, msg);
296 /* NOTREACHED */
297 }
298
299 /*
300 * mutex_init:
301 *
302 * Initialize a mutex for use. Note that adaptive mutexes are in
303 * essence spin mutexes that can sleep to avoid deadlock and wasting
304 * CPU time. We can't easily provide a type of mutex that always
305 * sleeps - see comments in mutex_vector_enter() about releasing
306 * mutexes unlocked.
307 */
308 void
309 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
310 {
311 u_int id;
312
313 memset(mtx, 0, sizeof(*mtx));
314
315 switch (type) {
316 case MUTEX_ADAPTIVE:
317 KASSERT(ipl == IPL_NONE);
318 break;
319 case MUTEX_DEFAULT:
320 case MUTEX_DRIVER:
321 switch (ipl) {
322 case IPL_NONE:
323 type = MUTEX_ADAPTIVE;
324 break;
325 default:
326 type = MUTEX_SPIN;
327 break;
328 }
329 break;
330 default:
331 break;
332 }
333
334 switch (type) {
335 case MUTEX_NODEBUG:
336 id = LOCKDEBUG_ALLOC(mtx, NULL,
337 (uintptr_t)__builtin_return_address(0));
338 MUTEX_INITIALIZE_SPIN(mtx, id, ipl);
339 break;
340 case MUTEX_ADAPTIVE:
341 id = LOCKDEBUG_ALLOC(mtx, &mutex_adaptive_lockops,
342 (uintptr_t)__builtin_return_address(0));
343 MUTEX_INITIALIZE_ADAPTIVE(mtx, id);
344 break;
345 case MUTEX_SPIN:
346 id = LOCKDEBUG_ALLOC(mtx, &mutex_spin_lockops,
347 (uintptr_t)__builtin_return_address(0));
348 MUTEX_INITIALIZE_SPIN(mtx, id, ipl);
349 break;
350 default:
351 panic("mutex_init: impossible type");
352 break;
353 }
354 }
355
356 /*
357 * mutex_destroy:
358 *
359 * Tear down a mutex.
360 */
361 void
362 mutex_destroy(kmutex_t *mtx)
363 {
364
365 if (MUTEX_ADAPTIVE_P(mtx)) {
366 MUTEX_ASSERT(mtx, !MUTEX_OWNED(mtx->mtx_owner) &&
367 !MUTEX_HAS_WAITERS(mtx));
368 } else {
369 MUTEX_ASSERT(mtx, !__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock));
370 }
371
372 LOCKDEBUG_FREE(mtx, MUTEX_GETID(mtx));
373 MUTEX_DESTROY(mtx);
374 }
375
376 /*
377 * mutex_onproc:
378 *
379 * Return true if an adaptive mutex owner is running on a CPU in the
380 * system. If the target is waiting on the kernel big lock, then we
381 * must release it. This is necessary to avoid deadlock.
382 *
383 * Note that we can't use the mutex owner field as an LWP pointer. We
384 * don't have full control over the timing of our execution, and so the
385 * pointer could be completely invalid by the time we dereference it.
386 */
387 #ifdef MULTIPROCESSOR
388 int
389 mutex_onproc(uintptr_t owner, struct cpu_info **cip)
390 {
391 CPU_INFO_ITERATOR cii;
392 struct cpu_info *ci;
393 struct lwp *l;
394
395 if (!MUTEX_OWNED(owner))
396 return 0;
397 l = (struct lwp *)MUTEX_OWNER(owner);
398
399 /* See if the target is running on a CPU somewhere. */
400 if ((ci = *cip) != NULL && ci->ci_curlwp == l)
401 goto run;
402 for (CPU_INFO_FOREACH(cii, ci))
403 if (ci->ci_curlwp == l)
404 goto run;
405
406 /* No: it may be safe to block now. */
407 *cip = NULL;
408 return 0;
409
410 run:
411 /* Target is running; do we need to block? */
412 *cip = ci;
413 return ci->ci_biglock_wanted != l;
414 }
415 #endif /* MULTIPROCESSOR */
416
417 /*
418 * mutex_vector_enter:
419 *
420 * Support routine for mutex_enter() that must handles all cases. In
421 * the LOCKDEBUG case, mutex_enter() is always aliased here, even if
422 * fast-path stubs are available. If an mutex_spin_enter() stub is
423 * not available, then it is also aliased directly here.
424 */
425 void
426 mutex_vector_enter(kmutex_t *mtx)
427 {
428 uintptr_t owner, curthread;
429 turnstile_t *ts;
430 #ifdef MULTIPROCESSOR
431 struct cpu_info *ci = NULL;
432 u_int count;
433 #endif
434 LOCKSTAT_COUNTER(spincnt);
435 LOCKSTAT_COUNTER(slpcnt);
436 LOCKSTAT_TIMER(spintime);
437 LOCKSTAT_TIMER(slptime);
438 LOCKSTAT_FLAG(lsflag);
439
440 /*
441 * Handle spin mutexes.
442 */
443 if (MUTEX_SPIN_P(mtx)) {
444 #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR)
445 u_int spins = 0;
446 #endif
447 MUTEX_SPIN_SPLRAISE(mtx);
448 MUTEX_WANTLOCK(mtx);
449 #ifdef FULL
450 if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
451 MUTEX_LOCKED(mtx);
452 return;
453 }
454 #if !defined(MULTIPROCESSOR)
455 MUTEX_ABORT(mtx, "locking against myself");
456 #else /* !MULTIPROCESSOR */
457
458 LOCKSTAT_ENTER(lsflag);
459 LOCKSTAT_START_TIMER(lsflag, spintime);
460 count = SPINLOCK_BACKOFF_MIN;
461
462 /*
463 * Spin testing the lock word and do exponential backoff
464 * to reduce cache line ping-ponging between CPUs.
465 */
466 do {
467 if (panicstr != NULL)
468 break;
469 while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
470 SPINLOCK_BACKOFF(count);
471 #ifdef LOCKDEBUG
472 if (SPINLOCK_SPINOUT(spins))
473 MUTEX_ABORT(mtx, "spinout");
474 #endif /* LOCKDEBUG */
475 }
476 } while (!__cpu_simple_lock_try(&mtx->mtx_lock));
477
478 if (count != SPINLOCK_BACKOFF_MIN) {
479 LOCKSTAT_STOP_TIMER(lsflag, spintime);
480 LOCKSTAT_EVENT(lsflag, mtx,
481 LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
482 }
483 LOCKSTAT_EXIT(lsflag);
484 #endif /* !MULTIPROCESSOR */
485 #endif /* FULL */
486 MUTEX_LOCKED(mtx);
487 return;
488 }
489
490 curthread = (uintptr_t)curlwp;
491
492 MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
493 MUTEX_ASSERT(mtx, curthread != 0);
494 MUTEX_WANTLOCK(mtx);
495
496 #ifdef LOCKDEBUG
497 if (panicstr == NULL) {
498 simple_lock_only_held(NULL, "mutex_enter");
499 #ifdef MULTIPROCESSOR
500 LOCKDEBUG_BARRIER(&kernel_lock, 1);
501 #else
502 LOCKDEBUG_BARRIER(NULL, 1);
503 #endif
504 }
505 #endif
506
507 LOCKSTAT_ENTER(lsflag);
508
509 /*
510 * Adaptive mutex; spin trying to acquire the mutex. If we
511 * determine that the owner is not running on a processor,
512 * then we stop spinning, and sleep instead.
513 */
514 for (;;) {
515 owner = mtx->mtx_owner;
516 if (!MUTEX_OWNED(owner)) {
517 /*
518 * Mutex owner clear could mean two things:
519 *
520 * * The mutex has been released.
521 * * The owner field hasn't been set yet.
522 *
523 * Try to acquire it again. If that fails,
524 * we'll just loop again.
525 */
526 if (MUTEX_ACQUIRE(mtx, curthread))
527 break;
528 continue;
529 }
530
531 if (panicstr != NULL)
532 return;
533 if (MUTEX_OWNER(owner) == curthread)
534 MUTEX_ABORT(mtx, "locking against myself");
535
536 #ifdef MULTIPROCESSOR
537 /*
538 * Check to see if the owner is running on a processor.
539 * If so, then we should just spin, as the owner will
540 * likely release the lock very soon.
541 */
542 if (mutex_onproc(owner, &ci)) {
543 LOCKSTAT_START_TIMER(lsflag, spintime);
544 count = SPINLOCK_BACKOFF_MIN;
545 for (;;) {
546 owner = mtx->mtx_owner;
547 if (!mutex_onproc(owner, &ci))
548 break;
549 SPINLOCK_BACKOFF(count);
550 }
551 LOCKSTAT_STOP_TIMER(lsflag, spintime);
552 LOCKSTAT_COUNT(spincnt, 1);
553 if (!MUTEX_OWNED(owner))
554 continue;
555 }
556 #endif
557
558 ts = turnstile_lookup(mtx);
559
560 /*
561 * Once we have the turnstile chain interlock, mark the
562 * mutex has having waiters. If that fails, spin again:
563 * chances are that the mutex has been released.
564 */
565 if (!MUTEX_SET_WAITERS(mtx, owner)) {
566 turnstile_exit(mtx);
567 continue;
568 }
569
570 #ifdef MULTIPROCESSOR
571 /*
572 * mutex_exit() is permitted to release the mutex without
573 * any interlocking instructions, and the following can
574 * occur as a result:
575 *
576 * CPU 1: MUTEX_SET_WAITERS() CPU2: mutex_exit()
577 * ---------------------------- ----------------------------
578 * .. acquire cache line
579 * .. test for waiters
580 * acquire cache line <- lose cache line
581 * lock cache line ..
582 * verify mutex is held ..
583 * set waiters ..
584 * unlock cache line ..
585 * lose cache line -> acquire cache line
586 * .. clear lock word, waiters
587 * return success
588 *
589 * There is a another race that can occur: a third CPU could
590 * acquire the mutex as soon as it is released. Since
591 * adaptive mutexes are primarily spin mutexes, this is not
592 * something that we need to worry about too much. What we
593 * do need to ensure is that the waiters bit gets set.
594 *
595 * To allow the unlocked release, we need to make some
596 * assumptions here:
597 *
598 * o Release is the only non-atomic/unlocked operation
599 * that can be performed on the mutex. (It must still
600 * be atomic on the local CPU, e.g. in case interrupted
601 * or preempted).
602 *
603 * o At any given time, MUTEX_SET_WAITERS() can only ever
604 * be in progress on one CPU in the system - guaranteed
605 * by the turnstile chain lock.
606 *
607 * o No other operations other than MUTEX_SET_WAITERS()
608 * and release can modify a mutex with a non-zero
609 * owner field.
610 *
611 * o The result of a successful MUTEX_SET_WAITERS() call
612 * is an unbuffered write that is immediately visible
613 * to all other processors in the system.
614 *
615 * o If the holding LWP switches away, it posts a store
616 * fence before changing curlwp, ensuring that any
617 * overwrite of the mutex waiters flag by mutex_exit()
618 * completes before the modification of curlwp becomes
619 * visible to this CPU.
620 *
621 * o mi_switch() posts a store fence before setting curlwp
622 * and before resuming execution of an LWP.
623 *
624 * o _kernel_lock() posts a store fence before setting
625 * curcpu()->ci_biglock_wanted, and after clearing it.
626 * This ensures that any overwrite of the mutex waiters
627 * flag by mutex_exit() completes before the modification
628 * of ci_biglock_wanted becomes visible.
629 *
630 * We now post a read memory barrier (after setting the
631 * waiters field) and check the lock holder's status again.
632 * Some of the possible outcomes (not an exhaustive list):
633 *
634 * 1. The onproc check returns true: the holding LWP is
635 * running again. The lock may be released soon and
636 * we should spin. Importantly, we can't trust the
637 * value of the waiters flag.
638 *
639 * 2. The onproc check returns false: the holding LWP is
640 * not running. We now have the oppertunity to check
641 * if mutex_exit() has blatted the modifications made
642 * by MUTEX_SET_WAITERS().
643 *
644 * 3. The onproc check returns false: the holding LWP may
645 * or may not be running. It has context switched at
646 * some point during our check. Again, we have the
647 * chance to see if the waiters bit is still set or
648 * has been overwritten.
649 *
650 * 4. The onproc check returns false: the holding LWP is
651 * running on a CPU, but wants the big lock. It's OK
652 * to check the waiters field in this case.
653 *
654 * 5. The has-waiters check fails: the mutex has been
655 * released, the waiters flag cleared and another LWP
656 * now owns the mutex.
657 *
658 * 6. The has-waiters check fails: the mutex has been
659 * released.
660 *
661 * If the waiters bit is not set it's unsafe to go asleep,
662 * as we might never be awoken.
663 */
664 if ((mb_read(), mutex_onproc(owner, &ci)) ||
665 (mb_read(), !MUTEX_HAS_WAITERS(mtx))) {
666 turnstile_exit(mtx);
667 continue;
668 }
669 #endif /* MULTIPROCESSOR */
670
671 LOCKSTAT_START_TIMER(lsflag, slptime);
672
673 turnstile_block(ts, TS_WRITER_Q, mtx, &mutex_syncobj);
674
675 LOCKSTAT_STOP_TIMER(lsflag, slptime);
676 LOCKSTAT_COUNT(slpcnt, 1);
677 }
678
679 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1,
680 slpcnt, slptime);
681 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN,
682 spincnt, spintime);
683 LOCKSTAT_EXIT(lsflag);
684
685 MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
686 MUTEX_LOCKED(mtx);
687 }
688
689 /*
690 * mutex_vector_exit:
691 *
692 * Support routine for mutex_exit() that handles all cases.
693 */
694 void
695 mutex_vector_exit(kmutex_t *mtx)
696 {
697 turnstile_t *ts;
698 uintptr_t curthread;
699
700 if (MUTEX_SPIN_P(mtx)) {
701 #ifdef FULL
702 if (!__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock))
703 MUTEX_ABORT(mtx, "exiting unheld spin mutex");
704 MUTEX_UNLOCKED(mtx);
705 __cpu_simple_unlock(&mtx->mtx_lock);
706 #endif
707 MUTEX_SPIN_SPLRESTORE(mtx);
708 return;
709 }
710
711 if (__predict_false((uintptr_t)panicstr | cold)) {
712 MUTEX_UNLOCKED(mtx);
713 MUTEX_RELEASE(mtx);
714 return;
715 }
716
717 curthread = (uintptr_t)curlwp;
718 MUTEX_DASSERT(mtx, curthread != 0);
719 MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
720 MUTEX_UNLOCKED(mtx);
721
722 #ifdef LOCKDEBUG
723 /*
724 * Avoid having to take the turnstile chain lock every time
725 * around. Raise the priority level to splhigh() in order
726 * to disable preemption and so make the following atomic.
727 */
728 {
729 int s = splhigh();
730 if (!MUTEX_HAS_WAITERS(mtx)) {
731 MUTEX_RELEASE(mtx);
732 splx(s);
733 return;
734 }
735 splx(s);
736 }
737 #endif
738
739 /*
740 * Get this lock's turnstile. This gets the interlock on
741 * the sleep queue. Once we have that, we can clear the
742 * lock. If there was no turnstile for the lock, there
743 * were no waiters remaining.
744 */
745 ts = turnstile_lookup(mtx);
746
747 if (ts == NULL) {
748 MUTEX_RELEASE(mtx);
749 turnstile_exit(mtx);
750 } else {
751 MUTEX_RELEASE(mtx);
752 turnstile_wakeup(ts, TS_WRITER_Q,
753 TS_WAITERS(ts, TS_WRITER_Q), NULL);
754 }
755 }
756
757 #ifndef __HAVE_SIMPLE_MUTEXES
758 /*
759 * mutex_wakeup:
760 *
761 * Support routine for mutex_exit() that wakes up all waiters.
762 * We assume that the mutex has been released, but it need not
763 * be.
764 */
765 void
766 mutex_wakeup(kmutex_t *mtx)
767 {
768 turnstile_t *ts;
769
770 ts = turnstile_lookup(mtx);
771 if (ts == NULL) {
772 turnstile_exit(mtx);
773 return;
774 }
775 MUTEX_CLEAR_WAITERS(mtx);
776 turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL);
777 }
778 #endif /* !__HAVE_SIMPLE_MUTEXES */
779
780 /*
781 * mutex_owned:
782 *
783 * Return true if the current LWP (adaptive) or CPU (spin)
784 * holds the mutex.
785 */
786 int
787 mutex_owned(kmutex_t *mtx)
788 {
789
790 if (MUTEX_ADAPTIVE_P(mtx))
791 return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp;
792 #ifdef FULL
793 return __SIMPLELOCK_LOCKED_P(&mtx->mtx_lock);
794 #else
795 return 1;
796 #endif
797 }
798
799 /*
800 * mutex_owner:
801 *
802 * Return the current owner of an adaptive mutex. Used for
803 * priority inheritance.
804 */
805 static struct lwp *
806 mutex_owner(wchan_t obj)
807 {
808 kmutex_t *mtx = (void *)(uintptr_t)obj; /* discard qualifiers */
809
810 MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
811 return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner);
812 }
813
814 /*
815 * mutex_tryenter:
816 *
817 * Try to acquire the mutex; return non-zero if we did.
818 */
819 int
820 mutex_tryenter(kmutex_t *mtx)
821 {
822 uintptr_t curthread;
823
824 /*
825 * Handle spin mutexes.
826 */
827 if (MUTEX_SPIN_P(mtx)) {
828 MUTEX_SPIN_SPLRAISE(mtx);
829 #ifdef FULL
830 if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
831 MUTEX_WANTLOCK(mtx);
832 MUTEX_LOCKED(mtx);
833 return 1;
834 }
835 MUTEX_SPIN_SPLRESTORE(mtx);
836 #else
837 MUTEX_WANTLOCK(mtx);
838 MUTEX_LOCKED(mtx);
839 return 1;
840 #endif
841 } else {
842 curthread = (uintptr_t)curlwp;
843 MUTEX_ASSERT(mtx, curthread != 0);
844 if (MUTEX_ACQUIRE(mtx, curthread)) {
845 MUTEX_WANTLOCK(mtx);
846 MUTEX_LOCKED(mtx);
847 MUTEX_DASSERT(mtx,
848 MUTEX_OWNER(mtx->mtx_owner) == curthread);
849 return 1;
850 }
851 }
852
853 return 0;
854 }
855
856 #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
857 /*
858 * mutex_spin_retry:
859 *
860 * Support routine for mutex_spin_enter(). Assumes that the caller
861 * has already raised the SPL, and adjusted counters.
862 */
863 void
864 mutex_spin_retry(kmutex_t *mtx)
865 {
866 #ifdef MULTIPROCESSOR
867 u_int count;
868 LOCKSTAT_TIMER(spintime);
869 LOCKSTAT_FLAG(lsflag);
870 #ifdef LOCKDEBUG
871 u_int spins = 0;
872 #endif /* LOCKDEBUG */
873
874 MUTEX_WANTLOCK(mtx);
875
876 LOCKSTAT_ENTER(lsflag);
877 LOCKSTAT_START_TIMER(lsflag, spintime);
878 count = SPINLOCK_BACKOFF_MIN;
879
880 /*
881 * Spin testing the lock word and do exponential backoff
882 * to reduce cache line ping-ponging between CPUs.
883 */
884 do {
885 if (panicstr != NULL)
886 break;
887 while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
888 SPINLOCK_BACKOFF(count);
889 #ifdef LOCKDEBUG
890 if (SPINLOCK_SPINOUT(spins))
891 MUTEX_ABORT(mtx, "spinout");
892 #endif /* LOCKDEBUG */
893 }
894 } while (!__cpu_simple_lock_try(&mtx->mtx_lock));
895
896 LOCKSTAT_STOP_TIMER(lsflag, spintime);
897 LOCKSTAT_EVENT(lsflag, mtx, LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
898 LOCKSTAT_EXIT(lsflag);
899
900 MUTEX_LOCKED(mtx);
901 #else /* MULTIPROCESSOR */
902 MUTEX_ABORT(mtx, "locking against myself");
903 #endif /* MULTIPROCESSOR */
904 }
905 #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */
906