kern_lock.c revision 1.128.2.3 1 /* $NetBSD: kern_lock.c,v 1.128.2.3 2007/12/10 19:28:05 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.128.2.3 2007/12/10 19:28:05 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
95 #include <dev/lockstat.h>
96
97 /*
98 * note that stdarg.h and the ansi style va_start macro is used for both
99 * ansi and traditional c compiles.
100 * XXX: this requires that stdarg.h define: va_alist and va_dcl
101 */
102 void lock_printf(const char *fmt, ...)
103 __attribute__((__format__(__printf__,1,2)));
104
105 static int acquire(struct lock **, int *, int, int, int, uintptr_t);
106
107 int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
108 bool kernel_lock_dodebug;
109 __cpu_simple_lock_t kernel_lock;
110
111 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
112 #define COUNT(lkp, l, cpu_id, x) (l)->l_locks += (x)
113 #else
114 #define COUNT(lkp, p, cpu_id, x)
115 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
116
117 #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
118
119 /*
120 * Acquire a resource.
121 */
122 static int
123 acquire(struct lock **lkpp, int *s, int extflags,
124 int drain, int wanted, uintptr_t ra)
125 {
126 int error;
127 struct lock *lkp = *lkpp;
128 LOCKSTAT_TIMER(slptime);
129 LOCKSTAT_FLAG(lsflag);
130
131 KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
132
133 LOCKSTAT_ENTER(lsflag);
134
135 for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
136 if (drain)
137 lkp->lk_flags |= LK_WAITDRAIN;
138 else {
139 lkp->lk_waitcount++;
140 lkp->lk_flags |= LK_WAIT_NONZERO;
141 }
142 LOCKSTAT_START_TIMER(lsflag, slptime);
143 error = mtsleep(drain ? (void *)&lkp->lk_flags : (void *)lkp,
144 lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo,
145 __UNVOLATILE(&lkp->lk_interlock));
146 LOCKSTAT_STOP_TIMER(lsflag, slptime);
147 LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
148 LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
149 if (!drain) {
150 lkp->lk_waitcount--;
151 if (lkp->lk_waitcount == 0)
152 lkp->lk_flags &= ~LK_WAIT_NONZERO;
153 }
154 if (error)
155 break;
156 if (extflags & LK_SLEEPFAIL) {
157 error = ENOLCK;
158 break;
159 }
160 }
161
162 LOCKSTAT_EXIT(lsflag);
163
164 return error;
165 }
166
167 #define SETHOLDER(lkp, pid, lid, cpu_id) \
168 do { \
169 (lkp)->lk_lockholder = pid; \
170 (lkp)->lk_locklwp = lid; \
171 } while (/*CONSTCOND*/0)
172
173 #define WEHOLDIT(lkp, pid, lid, cpu_id) \
174 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
175
176 #define WAKEUP_WAITER(lkp) \
177 do { \
178 if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) { \
179 wakeup((lkp)); \
180 } \
181 } while (/*CONSTCOND*/0)
182
183 #if defined(LOCKDEBUG)
184 /*
185 * Lock debug printing routine; can be configured to print to console
186 * or log to syslog.
187 */
188 void
189 lock_printf(const char *fmt, ...)
190 {
191 char b[150];
192 va_list ap;
193
194 va_start(ap, fmt);
195 if (lock_debug_syslog)
196 vlog(LOG_DEBUG, fmt, ap);
197 else {
198 vsnprintf(b, sizeof(b), fmt, ap);
199 printf_nolog("%s", b);
200 }
201 va_end(ap);
202 }
203 #endif /* LOCKDEBUG */
204
205 static void
206 lockpanic(struct lock *lkp, const char *fmt, ...)
207 {
208 char s[150], b[150];
209 static const char *locktype[] = {
210 "*0*", "shared", "exclusive", "*3*", "*4*", "downgrade",
211 "*release*", "drain", "exclother", "*9*", "*10*",
212 "*11*", "*12*", "*13*", "*14*", "*15*"
213 };
214 va_list ap;
215 va_start(ap, fmt);
216 vsnprintf(s, sizeof(s), fmt, ap);
217 va_end(ap);
218 bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
219 panic("%s ("
220 "type %s flags %s, sharecount %d, exclusivecount %d, "
221 "recurselevel %d, waitcount %d, wmesg %s"
222 ", lock_addr %p, unlock_addr %p"
223 ")\n",
224 s, locktype[lkp->lk_flags & LK_TYPE_MASK],
225 b, lkp->lk_sharecount, lkp->lk_exclusivecount,
226 lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg,
227 (void *)lkp->lk_lock_addr, (void *)lkp->lk_unlock_addr
228 );
229 }
230
231 /*
232 * Initialize a lock; required before use.
233 */
234 void
235 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
236 {
237
238 memset(lkp, 0, sizeof(struct lock));
239 lkp->lk_flags = flags & LK_EXTFLG_MASK;
240 mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
241 lkp->lk_lockholder = LK_NOPROC;
242 lkp->lk_prio = prio;
243 lkp->lk_timo = timo;
244 lkp->lk_wmesg = wmesg;
245 lkp->lk_lock_addr = 0;
246 lkp->lk_unlock_addr = 0;
247 }
248
249 void
250 lockdestroy(struct lock *lkp)
251 {
252
253 mutex_destroy(&lkp->lk_interlock);
254 }
255
256 /*
257 * Determine the status of a lock.
258 */
259 int
260 lockstatus(struct lock *lkp)
261 {
262 int lock_type = 0;
263 struct lwp *l = curlwp; /* XXX */
264 pid_t pid;
265 lwpid_t lid;
266 cpuid_t cpu_num;
267
268 if (l == NULL) {
269 cpu_num = cpu_number();
270 pid = LK_KERNPROC;
271 lid = 0;
272 } else {
273 cpu_num = LK_NOCPU;
274 pid = l->l_proc->p_pid;
275 lid = l->l_lid;
276 }
277
278 mutex_enter(&lkp->lk_interlock);
279 if (lkp->lk_exclusivecount != 0) {
280 if (WEHOLDIT(lkp, pid, lid, cpu_num))
281 lock_type = LK_EXCLUSIVE;
282 else
283 lock_type = LK_EXCLOTHER;
284 } else if (lkp->lk_sharecount != 0)
285 lock_type = LK_SHARED;
286 else if (lkp->lk_flags & LK_WANT_EXCL)
287 lock_type = LK_EXCLOTHER;
288 mutex_exit(&lkp->lk_interlock);
289 return (lock_type);
290 }
291
292 /*
293 * XXX XXX kludge around another kludge..
294 *
295 * vfs_shutdown() may be called from interrupt context, either as a result
296 * of a panic, or from the debugger. It proceeds to call
297 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
298 *
299 * We would like to make an attempt to sync the filesystems in this case, so
300 * if this happens, we treat attempts to acquire locks specially.
301 * All locks are acquired on behalf of proc0.
302 *
303 * If we've already paniced, we don't block waiting for locks, but
304 * just barge right ahead since we're already going down in flames.
305 */
306
307 /*
308 * Set, change, or release a lock.
309 *
310 * Shared requests increment the shared count. Exclusive requests set the
311 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
312 * accepted shared locks to go away.
313 */
314 int
315 lockmgr(struct lock *lkp, u_int flags, kmutex_t *interlkp)
316 {
317 int error;
318 pid_t pid;
319 lwpid_t lid;
320 int extflags;
321 cpuid_t cpu_num;
322 struct lwp *l = curlwp;
323 int lock_shutdown_noblock = 0;
324 int s = 0;
325
326 error = 0;
327
328 /* LK_RETRY is for vn_lock, not for lockmgr. */
329 KASSERT((flags & LK_RETRY) == 0);
330 KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
331
332 mutex_enter(&lkp->lk_interlock);
333 if (flags & LK_INTERLOCK)
334 mutex_exit(interlkp);
335 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
336
337 if (l == NULL) {
338 if (!doing_shutdown) {
339 panic("lockmgr: no context");
340 } else {
341 l = &lwp0;
342 if (panicstr && (!(flags & LK_NOWAIT))) {
343 flags |= LK_NOWAIT;
344 lock_shutdown_noblock = 1;
345 }
346 }
347 }
348 lid = l->l_lid;
349 pid = l->l_proc->p_pid;
350 cpu_num = cpu_number();
351
352 /*
353 * Once a lock has drained, the LK_DRAINING flag is set and an
354 * exclusive lock is returned. The only valid operation thereafter
355 * is a single release of that exclusive lock. This final release
356 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
357 * further requests of any sort will result in a panic. The bits
358 * selected for these two flags are chosen so that they will be set
359 * in memory that is freed (freed memory is filled with 0xdeadbeef).
360 * The final release is permitted to give a new lease on life to
361 * the lock by specifying LK_REENABLE.
362 */
363 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
364 #ifdef DIAGNOSTIC /* { */
365 if (lkp->lk_flags & LK_DRAINED)
366 lockpanic(lkp, "lockmgr: using decommissioned lock");
367 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
368 WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
369 lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
370 flags & LK_TYPE_MASK);
371 #endif /* DIAGNOSTIC */ /* } */
372 lkp->lk_flags &= ~LK_DRAINING;
373 if ((flags & LK_REENABLE) == 0)
374 lkp->lk_flags |= LK_DRAINED;
375 }
376
377 switch (flags & LK_TYPE_MASK) {
378
379 case LK_SHARED:
380 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
381 /*
382 * If just polling, check to see if we will block.
383 */
384 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
385 (LK_HAVE_EXCL | LK_WANT_EXCL))) {
386 error = EBUSY;
387 break;
388 }
389 /*
390 * Wait for exclusive locks to clear.
391 */
392 error = acquire(&lkp, &s, extflags, 0,
393 LK_HAVE_EXCL | LK_WANT_EXCL,
394 RETURN_ADDRESS);
395 if (error)
396 break;
397 lkp->lk_sharecount++;
398 lkp->lk_flags |= LK_SHARE_NONZERO;
399 COUNT(lkp, l, cpu_num, 1);
400 break;
401 }
402 /*
403 * We hold an exclusive lock, so downgrade it to shared.
404 * An alternative would be to fail with EDEADLK.
405 */
406 lkp->lk_sharecount++;
407 lkp->lk_flags |= LK_SHARE_NONZERO;
408 COUNT(lkp, l, cpu_num, 1);
409 /* fall into downgrade */
410
411 case LK_DOWNGRADE:
412 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
413 lkp->lk_exclusivecount == 0)
414 lockpanic(lkp, "lockmgr: not holding exclusive lock");
415 lkp->lk_sharecount += lkp->lk_exclusivecount;
416 lkp->lk_flags |= LK_SHARE_NONZERO;
417 lkp->lk_exclusivecount = 0;
418 lkp->lk_recurselevel = 0;
419 lkp->lk_flags &= ~LK_HAVE_EXCL;
420 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
421 #if defined(LOCKDEBUG)
422 lkp->lk_unlock_addr = RETURN_ADDRESS;
423 #endif
424 WAKEUP_WAITER(lkp);
425 break;
426
427 case LK_EXCLUSIVE:
428 if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
429 /*
430 * Recursive lock.
431 */
432 if ((extflags & LK_CANRECURSE) == 0 &&
433 lkp->lk_recurselevel == 0) {
434 if (extflags & LK_RECURSEFAIL) {
435 error = EDEADLK;
436 break;
437 } else
438 lockpanic(lkp, "lockmgr: locking against myself");
439 }
440 lkp->lk_exclusivecount++;
441 COUNT(lkp, l, cpu_num, 1);
442 break;
443 }
444 /*
445 * If we are just polling, check to see if we will sleep.
446 */
447 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
448 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_SHARE_NONZERO))) {
449 error = EBUSY;
450 break;
451 }
452 /*
453 * Try to acquire the want_exclusive flag.
454 */
455 error = acquire(&lkp, &s, extflags, 0,
456 LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
457 if (error)
458 break;
459 lkp->lk_flags |= LK_WANT_EXCL;
460 /*
461 * Wait for shared locks to finish.
462 */
463 error = acquire(&lkp, &s, extflags, 0,
464 LK_HAVE_EXCL | LK_SHARE_NONZERO,
465 RETURN_ADDRESS);
466 lkp->lk_flags &= ~LK_WANT_EXCL;
467 if (error) {
468 WAKEUP_WAITER(lkp);
469 break;
470 }
471 lkp->lk_flags |= LK_HAVE_EXCL;
472 SETHOLDER(lkp, pid, lid, cpu_num);
473 #if defined(LOCKDEBUG)
474 lkp->lk_lock_addr = RETURN_ADDRESS;
475 #endif
476 if (lkp->lk_exclusivecount != 0)
477 lockpanic(lkp, "lockmgr: non-zero exclusive count");
478 lkp->lk_exclusivecount = 1;
479 COUNT(lkp, l, cpu_num, 1);
480 break;
481
482 case LK_RELEASE:
483 if (lkp->lk_exclusivecount != 0) {
484 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
485 lockpanic(lkp, "lockmgr: pid %d.%d, not "
486 "exclusive lock holder %d.%d "
487 "unlocking", pid, lid,
488 lkp->lk_lockholder,
489 lkp->lk_locklwp);
490 }
491 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
492 lkp->lk_recurselevel = 0;
493 lkp->lk_exclusivecount--;
494 COUNT(lkp, l, cpu_num, -1);
495 if (lkp->lk_exclusivecount == 0) {
496 lkp->lk_flags &= ~LK_HAVE_EXCL;
497 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
498 #if defined(LOCKDEBUG)
499 lkp->lk_unlock_addr = RETURN_ADDRESS;
500 #endif
501 }
502 } else if (lkp->lk_sharecount != 0) {
503 lkp->lk_sharecount--;
504 if (lkp->lk_sharecount == 0)
505 lkp->lk_flags &= ~LK_SHARE_NONZERO;
506 COUNT(lkp, l, cpu_num, -1);
507 }
508 #ifdef DIAGNOSTIC
509 else
510 lockpanic(lkp, "lockmgr: release of unlocked lock!");
511 #endif
512 WAKEUP_WAITER(lkp);
513 break;
514
515 case LK_DRAIN:
516 /*
517 * Check that we do not already hold the lock, as it can
518 * never drain if we do. Unfortunately, we have no way to
519 * check for holding a shared lock, but at least we can
520 * check for an exclusive one.
521 */
522 if (WEHOLDIT(lkp, pid, lid, cpu_num))
523 lockpanic(lkp, "lockmgr: draining against myself");
524 /*
525 * If we are just polling, check to see if we will sleep.
526 */
527 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
528 (LK_HAVE_EXCL | LK_WANT_EXCL |
529 LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
530 error = EBUSY;
531 break;
532 }
533 error = acquire(&lkp, &s, extflags, 1,
534 LK_HAVE_EXCL | LK_WANT_EXCL |
535 LK_SHARE_NONZERO | LK_WAIT_NONZERO,
536 RETURN_ADDRESS);
537 if (error)
538 break;
539 lkp->lk_flags |= LK_HAVE_EXCL;
540 if ((extflags & LK_RESURRECT) == 0)
541 lkp->lk_flags |= LK_DRAINING;
542 SETHOLDER(lkp, pid, lid, cpu_num);
543 #if defined(LOCKDEBUG)
544 lkp->lk_lock_addr = RETURN_ADDRESS;
545 #endif
546 lkp->lk_exclusivecount = 1;
547 COUNT(lkp, l, cpu_num, 1);
548 break;
549
550 default:
551 mutex_exit(&lkp->lk_interlock);
552 lockpanic(lkp, "lockmgr: unknown locktype request %d",
553 flags & LK_TYPE_MASK);
554 /* NOTREACHED */
555 }
556 if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
557 ((lkp->lk_flags &
558 (LK_HAVE_EXCL | LK_WANT_EXCL |
559 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
560 lkp->lk_flags &= ~LK_WAITDRAIN;
561 wakeup(&lkp->lk_flags);
562 }
563 /*
564 * Note that this panic will be a recursive panic, since
565 * we only set lock_shutdown_noblock above if panicstr != NULL.
566 */
567 if (error && lock_shutdown_noblock)
568 lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
569
570 mutex_exit(&lkp->lk_interlock);
571 return (error);
572 }
573
574 /*
575 * Print out information about state of a lock. Used by VOP_PRINT
576 * routines to display ststus about contained locks.
577 */
578 void
579 lockmgr_printinfo(struct lock *lkp)
580 {
581
582 if (lkp->lk_sharecount)
583 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
584 lkp->lk_sharecount);
585 else if (lkp->lk_flags & LK_HAVE_EXCL) {
586 printf(" lock type %s: EXCL (count %d) by ",
587 lkp->lk_wmesg, lkp->lk_exclusivecount);
588 printf("pid %d.%d", lkp->lk_lockholder,
589 lkp->lk_locklwp);
590 } else
591 printf(" not locked");
592 if (lkp->lk_waitcount > 0)
593 printf(" with %d pending", lkp->lk_waitcount);
594 }
595
596 #if defined(LOCKDEBUG)
597 void
598 assert_sleepable(struct simplelock *interlock, const char *msg)
599 {
600
601 if (panicstr != NULL)
602 return;
603 LOCKDEBUG_BARRIER(&kernel_lock, 1);
604 if (CURCPU_IDLE_P() && !cold) {
605 panic("assert_sleepable: idle");
606 }
607 }
608 #endif
609
610 /*
611 * rump doesn't need the kernel lock so force it out. We cannot
612 * currently easily include it for compilation because of
613 * a) SPINLOCK_* b) membar_producer(). They are defined in different
614 * places / way for each arch, so just simply do not bother to
615 * fight a lot for no gain (i.e. pain but still no gain).
616 */
617 #ifndef _RUMPKERNEL
618 /*
619 * Functions for manipulating the kernel_lock. We put them here
620 * so that they show up in profiles.
621 */
622
623 #define _KERNEL_LOCK_ABORT(msg) \
624 LOCKDEBUG_ABORT(&kernel_lock, &_kernel_lock_ops, __func__, msg)
625
626 #ifdef LOCKDEBUG
627 #define _KERNEL_LOCK_ASSERT(cond) \
628 do { \
629 if (!(cond)) \
630 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
631 } while (/* CONSTCOND */ 0)
632 #else
633 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
634 #endif
635
636 void _kernel_lock_dump(volatile void *);
637
638 lockops_t _kernel_lock_ops = {
639 "Kernel lock",
640 0,
641 _kernel_lock_dump
642 };
643
644 /*
645 * Initialize the kernel lock.
646 */
647 void
648 kernel_lock_init(void)
649 {
650
651 __cpu_simple_lock_init(&kernel_lock);
652 kernel_lock_dodebug = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops,
653 RETURN_ADDRESS);
654 }
655
656 /*
657 * Print debugging information about the kernel lock.
658 */
659 void
660 _kernel_lock_dump(volatile void *junk)
661 {
662 struct cpu_info *ci = curcpu();
663
664 (void)junk;
665
666 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
667 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
668 }
669
670 /*
671 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
672 * acquisition is from process context.
673 */
674 void
675 _kernel_lock(int nlocks, struct lwp *l)
676 {
677 struct cpu_info *ci = curcpu();
678 LOCKSTAT_TIMER(spintime);
679 LOCKSTAT_FLAG(lsflag);
680 struct lwp *owant;
681 #ifdef LOCKDEBUG
682 u_int spins;
683 #endif
684 int s;
685
686 if (nlocks == 0)
687 return;
688 _KERNEL_LOCK_ASSERT(nlocks > 0);
689
690 l = curlwp;
691
692 if (ci->ci_biglock_count != 0) {
693 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(&kernel_lock));
694 ci->ci_biglock_count += nlocks;
695 l->l_blcnt += nlocks;
696 return;
697 }
698
699 _KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
700 LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, &kernel_lock, RETURN_ADDRESS,
701 0);
702
703 s = splvm();
704 if (__cpu_simple_lock_try(&kernel_lock)) {
705 ci->ci_biglock_count = nlocks;
706 l->l_blcnt = nlocks;
707 LOCKDEBUG_LOCKED(kernel_lock_dodebug, &kernel_lock,
708 RETURN_ADDRESS, 0);
709 splx(s);
710 return;
711 }
712
713 LOCKSTAT_ENTER(lsflag);
714 LOCKSTAT_START_TIMER(lsflag, spintime);
715
716 /*
717 * Before setting ci_biglock_wanted we must post a store
718 * fence (see kern_mutex.c). This is accomplished by the
719 * __cpu_simple_lock_try() above.
720 */
721 owant = ci->ci_biglock_wanted;
722 ci->ci_biglock_wanted = curlwp; /* XXXAD */
723
724 #ifdef LOCKDEBUG
725 spins = 0;
726 #endif
727
728 do {
729 splx(s);
730 while (__SIMPLELOCK_LOCKED_P(&kernel_lock)) {
731 #ifdef LOCKDEBUG
732 if (SPINLOCK_SPINOUT(spins))
733 _KERNEL_LOCK_ABORT("spinout");
734 #endif
735 SPINLOCK_BACKOFF_HOOK;
736 SPINLOCK_SPIN_HOOK;
737 }
738 (void)splvm();
739 } while (!__cpu_simple_lock_try(&kernel_lock));
740
741 ci->ci_biglock_wanted = owant;
742 ci->ci_biglock_count = nlocks;
743 l->l_blcnt = nlocks;
744 LOCKSTAT_STOP_TIMER(lsflag, spintime);
745 LOCKDEBUG_LOCKED(kernel_lock_dodebug, &kernel_lock, RETURN_ADDRESS, 0);
746 splx(s);
747
748 /*
749 * Again, another store fence is required (see kern_mutex.c).
750 */
751 membar_producer();
752 if (owant == NULL) {
753 LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
754 1, spintime);
755 }
756 LOCKSTAT_EXIT(lsflag);
757 }
758
759 /*
760 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
761 * all holds. If 'l' is non-null, the release is from process context.
762 */
763 void
764 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
765 {
766 struct cpu_info *ci = curcpu();
767 u_int olocks;
768 int s;
769
770 l = curlwp;
771
772 _KERNEL_LOCK_ASSERT(nlocks < 2);
773
774 olocks = l->l_blcnt;
775
776 if (olocks == 0) {
777 _KERNEL_LOCK_ASSERT(nlocks <= 0);
778 if (countp != NULL)
779 *countp = 0;
780 return;
781 }
782
783 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(&kernel_lock));
784
785 if (nlocks == 0)
786 nlocks = olocks;
787 else if (nlocks == -1) {
788 nlocks = 1;
789 _KERNEL_LOCK_ASSERT(olocks == 1);
790 }
791
792 _KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
793
794 l->l_blcnt -= nlocks;
795 if (ci->ci_biglock_count == nlocks) {
796 s = splvm();
797 LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, &kernel_lock,
798 RETURN_ADDRESS, 0);
799 ci->ci_biglock_count = 0;
800 __cpu_simple_unlock(&kernel_lock);
801 splx(s);
802 } else
803 ci->ci_biglock_count -= nlocks;
804
805 if (countp != NULL)
806 *countp = olocks;
807 }
808 #endif /* !_RUMPKERNEL */
809