kern_lock.c revision 1.132 1 /* $NetBSD: kern_lock.c,v 1.132 2008/01/10 20:14:12 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 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 of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Ross Harvey.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 /*
44 * Copyright (c) 1995
45 * The Regents of the University of California. All rights reserved.
46 *
47 * This code contains ideas from software contributed to Berkeley by
48 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
49 * System project at Carnegie-Mellon University.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.132 2008/01/10 20:14:12 ad Exp $");
80
81 #include "opt_multiprocessor.h"
82
83 #include <sys/param.h>
84 #include <sys/proc.h>
85 #include <sys/lock.h>
86 #include <sys/systm.h>
87 #include <sys/kernel.h>
88 #include <sys/lockdebug.h>
89 #include <sys/cpu.h>
90 #include <sys/syslog.h>
91 #include <sys/atomic.h>
92
93 #include <machine/stdarg.h>
94 #include <machine/lock.h>
95
96 #include <dev/lockstat.h>
97
98 /*
99 * note that stdarg.h and the ansi style va_start macro is used for both
100 * ansi and traditional c compiles.
101 * XXX: this requires that stdarg.h define: va_alist and va_dcl
102 */
103 void lock_printf(const char *fmt, ...)
104 __attribute__((__format__(__printf__,1,2)));
105
106 static int acquire(struct lock **, int *, int, int, int, uintptr_t);
107
108 int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
109 bool kernel_lock_dodebug;
110
111 __cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
112 __aligned(CACHE_LINE_SIZE);
113
114 #ifdef LOCKDEBUG
115 static lockops_t lockmgr_lockops = {
116 "lockmgr",
117 1,
118 (void *)nullop
119 };
120 #endif
121
122 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
123 #define COUNT(lkp, l, cpu_id, x) (l)->l_locks += (x)
124 #else
125 #define COUNT(lkp, p, cpu_id, x)
126 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
127
128 #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
129
130 /*
131 * Acquire a resource.
132 */
133 static int
134 acquire(struct lock **lkpp, int *s, int extflags,
135 int drain, int wanted, uintptr_t ra)
136 {
137 int error;
138 struct lock *lkp = *lkpp;
139 LOCKSTAT_TIMER(slptime);
140 LOCKSTAT_FLAG(lsflag);
141
142 KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
143
144 LOCKSTAT_ENTER(lsflag);
145
146 for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
147 if (drain)
148 lkp->lk_flags |= LK_WAITDRAIN;
149 else {
150 lkp->lk_waitcount++;
151 lkp->lk_flags |= LK_WAIT_NONZERO;
152 }
153 LOCKSTAT_START_TIMER(lsflag, slptime);
154 error = mtsleep(drain ? (void *)&lkp->lk_flags : (void *)lkp,
155 lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo,
156 __UNVOLATILE(&lkp->lk_interlock));
157 LOCKSTAT_STOP_TIMER(lsflag, slptime);
158 LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
159 LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
160 if (!drain) {
161 lkp->lk_waitcount--;
162 if (lkp->lk_waitcount == 0)
163 lkp->lk_flags &= ~LK_WAIT_NONZERO;
164 }
165 if (error)
166 break;
167 if (extflags & LK_SLEEPFAIL) {
168 error = ENOLCK;
169 break;
170 }
171 }
172
173 LOCKSTAT_EXIT(lsflag);
174
175 return error;
176 }
177
178 #define SETHOLDER(lkp, pid, lid, cpu_id) \
179 do { \
180 (lkp)->lk_lockholder = pid; \
181 (lkp)->lk_locklwp = lid; \
182 } while (/*CONSTCOND*/0)
183
184 #define WEHOLDIT(lkp, pid, lid, cpu_id) \
185 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
186
187 #define WAKEUP_WAITER(lkp) \
188 do { \
189 if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) { \
190 wakeup((lkp)); \
191 } \
192 } while (/*CONSTCOND*/0)
193
194 #if defined(LOCKDEBUG)
195 /*
196 * Lock debug printing routine; can be configured to print to console
197 * or log to syslog.
198 */
199 void
200 lock_printf(const char *fmt, ...)
201 {
202 char b[150];
203 va_list ap;
204
205 va_start(ap, fmt);
206 if (lock_debug_syslog)
207 vlog(LOG_DEBUG, fmt, ap);
208 else {
209 vsnprintf(b, sizeof(b), fmt, ap);
210 printf_nolog("%s", b);
211 }
212 va_end(ap);
213 }
214 #endif /* LOCKDEBUG */
215
216 static void
217 lockpanic(struct lock *lkp, const char *fmt, ...)
218 {
219 char s[150], b[150];
220 static const char *locktype[] = {
221 "*0*", "shared", "exclusive", "*3*", "*4*", "downgrade",
222 "*release*", "drain", "exclother", "*9*", "*10*",
223 "*11*", "*12*", "*13*", "*14*", "*15*"
224 };
225 va_list ap;
226 va_start(ap, fmt);
227 vsnprintf(s, sizeof(s), fmt, ap);
228 va_end(ap);
229 bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
230 panic("%s ("
231 "type %s flags %s, sharecount %d, exclusivecount %d, "
232 "recurselevel %d, waitcount %d, wmesg %s"
233 ", lock_addr %p, unlock_addr %p"
234 ")\n",
235 s, locktype[lkp->lk_flags & LK_TYPE_MASK],
236 b, lkp->lk_sharecount, lkp->lk_exclusivecount,
237 lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg,
238 (void *)lkp->lk_lock_addr, (void *)lkp->lk_unlock_addr
239 );
240 }
241
242 /*
243 * Initialize a lock; required before use.
244 */
245 void
246 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
247 {
248
249 memset(lkp, 0, sizeof(struct lock));
250 lkp->lk_flags = flags & LK_EXTFLG_MASK;
251 mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
252 lkp->lk_lockholder = LK_NOPROC;
253 lkp->lk_prio = prio;
254 lkp->lk_timo = timo;
255 lkp->lk_wmesg = wmesg;
256 lkp->lk_lock_addr = 0;
257 lkp->lk_unlock_addr = 0;
258
259 if (LOCKDEBUG_ALLOC(lkp, &lockmgr_lockops,
260 (uintptr_t)__builtin_return_address(0))) {
261 lkp->lk_flags |= LK_DODEBUG;
262 }
263 }
264
265 void
266 lockdestroy(struct lock *lkp)
267 {
268
269 LOCKDEBUG_FREE(((lkp->lk_flags & LK_DODEBUG) != 0), lkp);
270 mutex_destroy(&lkp->lk_interlock);
271 }
272
273 /*
274 * Determine the status of a lock.
275 */
276 int
277 lockstatus(struct lock *lkp)
278 {
279 int lock_type = 0;
280 struct lwp *l = curlwp; /* XXX */
281 pid_t pid;
282 lwpid_t lid;
283 cpuid_t cpu_num;
284
285 if (l == NULL) {
286 cpu_num = cpu_number();
287 pid = LK_KERNPROC;
288 lid = 0;
289 } else {
290 cpu_num = LK_NOCPU;
291 pid = l->l_proc->p_pid;
292 lid = l->l_lid;
293 }
294
295 mutex_enter(&lkp->lk_interlock);
296 if (lkp->lk_exclusivecount != 0) {
297 if (WEHOLDIT(lkp, pid, lid, cpu_num))
298 lock_type = LK_EXCLUSIVE;
299 else
300 lock_type = LK_EXCLOTHER;
301 } else if (lkp->lk_sharecount != 0)
302 lock_type = LK_SHARED;
303 else if (lkp->lk_flags & LK_WANT_EXCL)
304 lock_type = LK_EXCLOTHER;
305 mutex_exit(&lkp->lk_interlock);
306 return (lock_type);
307 }
308
309 /*
310 * XXX XXX kludge around another kludge..
311 *
312 * vfs_shutdown() may be called from interrupt context, either as a result
313 * of a panic, or from the debugger. It proceeds to call
314 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
315 *
316 * We would like to make an attempt to sync the filesystems in this case, so
317 * if this happens, we treat attempts to acquire locks specially.
318 * All locks are acquired on behalf of proc0.
319 *
320 * If we've already paniced, we don't block waiting for locks, but
321 * just barge right ahead since we're already going down in flames.
322 */
323
324 /*
325 * Set, change, or release a lock.
326 *
327 * Shared requests increment the shared count. Exclusive requests set the
328 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
329 * accepted shared locks to go away.
330 */
331 int
332 lockmgr(struct lock *lkp, u_int flags, kmutex_t *interlkp)
333 {
334 int error;
335 pid_t pid;
336 lwpid_t lid;
337 int extflags;
338 cpuid_t cpu_num;
339 struct lwp *l = curlwp;
340 int lock_shutdown_noblock = 0;
341 int s = 0;
342
343 error = 0;
344
345 /* LK_RETRY is for vn_lock, not for lockmgr. */
346 KASSERT((flags & LK_RETRY) == 0);
347 KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
348
349 mutex_enter(&lkp->lk_interlock);
350 if (flags & LK_INTERLOCK)
351 mutex_exit(interlkp);
352 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
353
354 if (l == NULL) {
355 if (!doing_shutdown) {
356 panic("lockmgr: no context");
357 } else {
358 l = &lwp0;
359 if (panicstr && (!(flags & LK_NOWAIT))) {
360 flags |= LK_NOWAIT;
361 lock_shutdown_noblock = 1;
362 }
363 }
364 }
365 lid = l->l_lid;
366 pid = l->l_proc->p_pid;
367 cpu_num = cpu_number();
368
369 /*
370 * Once a lock has drained, the LK_DRAINING flag is set and an
371 * exclusive lock is returned. The only valid operation thereafter
372 * is a single release of that exclusive lock. This final release
373 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
374 * further requests of any sort will result in a panic. The bits
375 * selected for these two flags are chosen so that they will be set
376 * in memory that is freed (freed memory is filled with 0xdeadbeef).
377 * The final release is permitted to give a new lease on life to
378 * the lock by specifying LK_REENABLE.
379 */
380 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
381 #ifdef DIAGNOSTIC /* { */
382 if (lkp->lk_flags & LK_DRAINED)
383 lockpanic(lkp, "lockmgr: using decommissioned lock");
384 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
385 WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
386 lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
387 flags & LK_TYPE_MASK);
388 #endif /* DIAGNOSTIC */ /* } */
389 lkp->lk_flags &= ~LK_DRAINING;
390 if ((flags & LK_REENABLE) == 0)
391 lkp->lk_flags |= LK_DRAINED;
392 }
393
394 switch (flags & LK_TYPE_MASK) {
395
396 case LK_SHARED:
397 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
398 /*
399 * If just polling, check to see if we will block.
400 */
401 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
402 (LK_HAVE_EXCL | LK_WANT_EXCL))) {
403 error = EBUSY;
404 break;
405 }
406 /*
407 * Wait for exclusive locks to clear.
408 */
409 error = acquire(&lkp, &s, extflags, 0,
410 LK_HAVE_EXCL | LK_WANT_EXCL,
411 RETURN_ADDRESS);
412 if (error)
413 break;
414 lkp->lk_sharecount++;
415 lkp->lk_flags |= LK_SHARE_NONZERO;
416 COUNT(lkp, l, cpu_num, 1);
417 break;
418 }
419 /*
420 * We hold an exclusive lock, so downgrade it to shared.
421 * An alternative would be to fail with EDEADLK.
422 */
423 lkp->lk_sharecount++;
424 lkp->lk_flags |= LK_SHARE_NONZERO;
425 COUNT(lkp, l, cpu_num, 1);
426 /* fall into downgrade */
427
428 case LK_DOWNGRADE:
429 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
430 lkp->lk_exclusivecount == 0)
431 lockpanic(lkp, "lockmgr: not holding exclusive lock");
432 lkp->lk_sharecount += lkp->lk_exclusivecount;
433 lkp->lk_flags |= LK_SHARE_NONZERO;
434 lkp->lk_exclusivecount = 0;
435 lkp->lk_recurselevel = 0;
436 lkp->lk_flags &= ~LK_HAVE_EXCL;
437 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
438 #if defined(LOCKDEBUG)
439 lkp->lk_unlock_addr = RETURN_ADDRESS;
440 #endif
441 WAKEUP_WAITER(lkp);
442 break;
443
444 case LK_EXCLUSIVE:
445 if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
446 /*
447 * Recursive lock.
448 */
449 if ((extflags & LK_CANRECURSE) == 0 &&
450 lkp->lk_recurselevel == 0) {
451 if (extflags & LK_RECURSEFAIL) {
452 error = EDEADLK;
453 break;
454 } else
455 lockpanic(lkp, "lockmgr: locking against myself");
456 }
457 lkp->lk_exclusivecount++;
458 COUNT(lkp, l, cpu_num, 1);
459 break;
460 }
461 /*
462 * If we are just polling, check to see if we will sleep.
463 */
464 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
465 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_SHARE_NONZERO))) {
466 error = EBUSY;
467 break;
468 }
469 /*
470 * Try to acquire the want_exclusive flag.
471 */
472 error = acquire(&lkp, &s, extflags, 0,
473 LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
474 if (error)
475 break;
476 lkp->lk_flags |= LK_WANT_EXCL;
477 /*
478 * Wait for shared locks to finish.
479 */
480 error = acquire(&lkp, &s, extflags, 0,
481 LK_HAVE_EXCL | LK_SHARE_NONZERO,
482 RETURN_ADDRESS);
483 lkp->lk_flags &= ~LK_WANT_EXCL;
484 if (error) {
485 WAKEUP_WAITER(lkp);
486 break;
487 }
488 lkp->lk_flags |= LK_HAVE_EXCL;
489 SETHOLDER(lkp, pid, lid, cpu_num);
490 #if defined(LOCKDEBUG)
491 lkp->lk_lock_addr = RETURN_ADDRESS;
492 #endif
493 if (lkp->lk_exclusivecount != 0)
494 lockpanic(lkp, "lockmgr: non-zero exclusive count");
495 lkp->lk_exclusivecount = 1;
496 COUNT(lkp, l, cpu_num, 1);
497 break;
498
499 case LK_RELEASE:
500 if (lkp->lk_exclusivecount != 0) {
501 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
502 lockpanic(lkp, "lockmgr: pid %d.%d, not "
503 "exclusive lock holder %d.%d "
504 "unlocking", pid, lid,
505 lkp->lk_lockholder,
506 lkp->lk_locklwp);
507 }
508 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
509 lkp->lk_recurselevel = 0;
510 lkp->lk_exclusivecount--;
511 COUNT(lkp, l, cpu_num, -1);
512 if (lkp->lk_exclusivecount == 0) {
513 lkp->lk_flags &= ~LK_HAVE_EXCL;
514 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
515 #if defined(LOCKDEBUG)
516 lkp->lk_unlock_addr = RETURN_ADDRESS;
517 #endif
518 }
519 } else if (lkp->lk_sharecount != 0) {
520 lkp->lk_sharecount--;
521 if (lkp->lk_sharecount == 0)
522 lkp->lk_flags &= ~LK_SHARE_NONZERO;
523 COUNT(lkp, l, cpu_num, -1);
524 }
525 #ifdef DIAGNOSTIC
526 else
527 lockpanic(lkp, "lockmgr: release of unlocked lock!");
528 #endif
529 WAKEUP_WAITER(lkp);
530 break;
531
532 case LK_DRAIN:
533 /*
534 * Check that we do not already hold the lock, as it can
535 * never drain if we do. Unfortunately, we have no way to
536 * check for holding a shared lock, but at least we can
537 * check for an exclusive one.
538 */
539 if (WEHOLDIT(lkp, pid, lid, cpu_num))
540 lockpanic(lkp, "lockmgr: draining against myself");
541 /*
542 * If we are just polling, check to see if we will sleep.
543 */
544 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
545 (LK_HAVE_EXCL | LK_WANT_EXCL |
546 LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
547 error = EBUSY;
548 break;
549 }
550 error = acquire(&lkp, &s, extflags, 1,
551 LK_HAVE_EXCL | LK_WANT_EXCL |
552 LK_SHARE_NONZERO | LK_WAIT_NONZERO,
553 RETURN_ADDRESS);
554 if (error)
555 break;
556 lkp->lk_flags |= LK_HAVE_EXCL;
557 if ((extflags & LK_RESURRECT) == 0)
558 lkp->lk_flags |= LK_DRAINING;
559 SETHOLDER(lkp, pid, lid, cpu_num);
560 #if defined(LOCKDEBUG)
561 lkp->lk_lock_addr = RETURN_ADDRESS;
562 #endif
563 lkp->lk_exclusivecount = 1;
564 COUNT(lkp, l, cpu_num, 1);
565 break;
566
567 default:
568 mutex_exit(&lkp->lk_interlock);
569 lockpanic(lkp, "lockmgr: unknown locktype request %d",
570 flags & LK_TYPE_MASK);
571 /* NOTREACHED */
572 }
573 if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
574 ((lkp->lk_flags &
575 (LK_HAVE_EXCL | LK_WANT_EXCL |
576 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
577 lkp->lk_flags &= ~LK_WAITDRAIN;
578 wakeup(&lkp->lk_flags);
579 }
580 /*
581 * Note that this panic will be a recursive panic, since
582 * we only set lock_shutdown_noblock above if panicstr != NULL.
583 */
584 if (error && lock_shutdown_noblock)
585 lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
586
587 mutex_exit(&lkp->lk_interlock);
588 return (error);
589 }
590
591 /*
592 * Print out information about state of a lock. Used by VOP_PRINT
593 * routines to display ststus about contained locks.
594 */
595 void
596 lockmgr_printinfo(struct lock *lkp)
597 {
598
599 if (lkp->lk_sharecount)
600 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
601 lkp->lk_sharecount);
602 else if (lkp->lk_flags & LK_HAVE_EXCL) {
603 printf(" lock type %s: EXCL (count %d) by ",
604 lkp->lk_wmesg, lkp->lk_exclusivecount);
605 printf("pid %d.%d", lkp->lk_lockholder,
606 lkp->lk_locklwp);
607 } else
608 printf(" not locked");
609 if (lkp->lk_waitcount > 0)
610 printf(" with %d pending", lkp->lk_waitcount);
611 }
612
613 #if defined(LOCKDEBUG)
614 void
615 assert_sleepable(struct simplelock *interlock, const char *msg)
616 {
617
618 if (panicstr != NULL)
619 return;
620 LOCKDEBUG_BARRIER(kernel_lock, 1);
621 if (CURCPU_IDLE_P() && !cold) {
622 panic("assert_sleepable: idle");
623 }
624 }
625 #endif
626
627 /*
628 * rump doesn't need the kernel lock so force it out. We cannot
629 * currently easily include it for compilation because of
630 * a) SPINLOCK_* b) membar_producer(). They are defined in different
631 * places / way for each arch, so just simply do not bother to
632 * fight a lot for no gain (i.e. pain but still no gain).
633 */
634 #ifndef _RUMPKERNEL
635 /*
636 * Functions for manipulating the kernel_lock. We put them here
637 * so that they show up in profiles.
638 */
639
640 #define _KERNEL_LOCK_ABORT(msg) \
641 LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
642
643 #ifdef LOCKDEBUG
644 #define _KERNEL_LOCK_ASSERT(cond) \
645 do { \
646 if (!(cond)) \
647 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
648 } while (/* CONSTCOND */ 0)
649 #else
650 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
651 #endif
652
653 void _kernel_lock_dump(volatile void *);
654
655 lockops_t _kernel_lock_ops = {
656 "Kernel lock",
657 0,
658 _kernel_lock_dump
659 };
660
661 /*
662 * Initialize the kernel lock.
663 */
664 void
665 kernel_lock_init(void)
666 {
667
668 KASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
669 __cpu_simple_lock_init(kernel_lock);
670 kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
671 RETURN_ADDRESS);
672 }
673
674 /*
675 * Print debugging information about the kernel lock.
676 */
677 void
678 _kernel_lock_dump(volatile void *junk)
679 {
680 struct cpu_info *ci = curcpu();
681
682 (void)junk;
683
684 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
685 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
686 }
687
688 /*
689 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
690 * acquisition is from process context.
691 */
692 void
693 _kernel_lock(int nlocks, struct lwp *l)
694 {
695 struct cpu_info *ci = curcpu();
696 LOCKSTAT_TIMER(spintime);
697 LOCKSTAT_FLAG(lsflag);
698 struct lwp *owant;
699 u_int spins;
700 int s;
701
702 if (nlocks == 0)
703 return;
704 _KERNEL_LOCK_ASSERT(nlocks > 0);
705
706 l = curlwp;
707
708 if (ci->ci_biglock_count != 0) {
709 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
710 ci->ci_biglock_count += nlocks;
711 l->l_blcnt += nlocks;
712 return;
713 }
714
715 _KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
716 LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
717 0);
718
719 s = splvm();
720 if (__cpu_simple_lock_try(kernel_lock)) {
721 ci->ci_biglock_count = nlocks;
722 l->l_blcnt = nlocks;
723 LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock,
724 RETURN_ADDRESS, 0);
725 splx(s);
726 return;
727 }
728
729 /*
730 * To remove the ordering constraint between adaptive mutexes
731 * and kernel_lock we must make it appear as if this thread is
732 * blocking. For non-interlocked mutex release, a store fence
733 * is required to ensure that the result of any mutex_exit()
734 * by the current LWP becomes visible on the bus before the set
735 * of ci->ci_biglock_wanted becomes visible.
736 */
737 membar_producer();
738 owant = ci->ci_biglock_wanted;
739 ci->ci_biglock_wanted = l;
740
741 /*
742 * Spin until we acquire the lock. Once we have it, record the
743 * time spent with lockstat.
744 */
745 LOCKSTAT_ENTER(lsflag);
746 LOCKSTAT_START_TIMER(lsflag, spintime);
747
748 spins = 0;
749 do {
750 splx(s);
751 while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
752 if (SPINLOCK_SPINOUT(spins)) {
753 _KERNEL_LOCK_ABORT("spinout");
754 }
755 SPINLOCK_BACKOFF_HOOK;
756 SPINLOCK_SPIN_HOOK;
757 }
758 s = splvm();
759 } while (!__cpu_simple_lock_try(kernel_lock));
760
761 ci->ci_biglock_count = nlocks;
762 l->l_blcnt = nlocks;
763 LOCKSTAT_STOP_TIMER(lsflag, spintime);
764 LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS, 0);
765 if (owant == NULL) {
766 LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
767 LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
768 }
769 LOCKSTAT_EXIT(lsflag);
770 splx(s);
771
772 /*
773 * Now that we have kernel_lock, reset ci_biglock_wanted. This
774 * store must be unbuffered (immediately visible on the bus) in
775 * order for non-interlocked mutex release to work correctly.
776 * It must be visible before a mutex_exit() can execute on this
777 * processor.
778 *
779 * Note: only where CAS is available in hardware will this be
780 * an unbuffered write, but non-interlocked release cannot be
781 * done on CPUs without CAS in hardware.
782 */
783 (void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
784
785 /*
786 * Issue a memory barrier as we have acquired a lock. This also
787 * prevents stores from a following mutex_exit() being reordered
788 * to occur before our store to ci_biglock_wanted above.
789 */
790 membar_enter();
791 }
792
793 /*
794 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
795 * all holds. If 'l' is non-null, the release is from process context.
796 */
797 void
798 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
799 {
800 struct cpu_info *ci = curcpu();
801 u_int olocks;
802 int s;
803
804 l = curlwp;
805
806 _KERNEL_LOCK_ASSERT(nlocks < 2);
807
808 olocks = l->l_blcnt;
809
810 if (olocks == 0) {
811 _KERNEL_LOCK_ASSERT(nlocks <= 0);
812 if (countp != NULL)
813 *countp = 0;
814 return;
815 }
816
817 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
818
819 if (nlocks == 0)
820 nlocks = olocks;
821 else if (nlocks == -1) {
822 nlocks = 1;
823 _KERNEL_LOCK_ASSERT(olocks == 1);
824 }
825
826 _KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
827
828 l->l_blcnt -= nlocks;
829 if (ci->ci_biglock_count == nlocks) {
830 s = splvm();
831 LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
832 RETURN_ADDRESS, 0);
833 ci->ci_biglock_count = 0;
834 __cpu_simple_unlock(kernel_lock);
835 splx(s);
836 } else
837 ci->ci_biglock_count -= nlocks;
838
839 if (countp != NULL)
840 *countp = olocks;
841 }
842 #endif /* !_RUMPKERNEL */
843