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