kern_lock.c revision 1.110.2.1 1 /* $NetBSD: kern_lock.c,v 1.110.2.1 2007/03/13 17:50:52 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2006 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.110.2.1 2007/03/13 17:50:52 ad Exp $");
80
81 #include "opt_multiprocessor.h"
82 #include "opt_ddb.h"
83
84 #define __MUTEX_PRIVATE
85
86 #include <sys/param.h>
87 #include <sys/proc.h>
88 #include <sys/lock.h>
89 #include <sys/systm.h>
90 #include <sys/lockdebug.h>
91
92 #include <machine/cpu.h>
93 #include <machine/stdarg.h>
94
95 #include <dev/lockstat.h>
96
97 #if defined(LOCKDEBUG)
98 #include <sys/syslog.h>
99 /*
100 * note that stdarg.h and the ansi style va_start macro is used for both
101 * ansi and traditional c compiles.
102 * XXX: this requires that stdarg.h define: va_alist and va_dcl
103 */
104 #include <machine/stdarg.h>
105
106 void lock_printf(const char *fmt, ...)
107 __attribute__((__format__(__printf__,1,2)));
108
109 static int acquire(volatile struct lock **, int *, int, int, int, uintptr_t);
110
111 int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
112
113 #ifdef DDB
114 #include <ddb/ddbvar.h>
115 #include <machine/db_machdep.h>
116 #include <ddb/db_command.h>
117 #include <ddb/db_interface.h>
118 #endif
119 #endif /* defined(LOCKDEBUG) */
120
121 #if defined(MULTIPROCESSOR)
122 /*
123 * IPL_BIGLOCK: block IPLs which need to grab kernel_mutex.
124 * XXX IPL_VM or IPL_AUDIO should be enough.
125 */
126 #if !defined(__HAVE_SPLBIGLOCK)
127 #define splbiglock splclock
128 #endif
129 __cpu_simple_lock_t kernel_lock;
130 int kernel_lock_id;
131 #endif
132
133 /*
134 * Locking primitives implementation.
135 * Locks provide shared/exclusive synchronization.
136 */
137
138 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
139 #define COUNT(lkp, l, cpu_id, x) (l)->l_locks += (x)
140 #else
141 #define COUNT(lkp, p, cpu_id, x)
142 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
143
144 #ifdef DDB /* { */
145 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
146 int simple_lock_debugger = 1; /* more serious on MP */
147 #else
148 int simple_lock_debugger = 0;
149 #endif
150 #define SLOCK_DEBUGGER() if (simple_lock_debugger && db_onpanic) Debugger()
151 #define SLOCK_TRACE() \
152 db_stack_trace_print((db_expr_t)__builtin_frame_address(0), \
153 true, 65535, "", lock_printf);
154 #else
155 #define SLOCK_DEBUGGER() /* nothing */
156 #define SLOCK_TRACE() /* nothing */
157 #endif /* } */
158
159 #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
160
161 /*
162 * Acquire a resource.
163 */
164 static int
165 acquire(volatile struct lock **lkpp, int *s, int extflags,
166 int drain, int wanted, uintptr_t ra)
167 {
168 int error;
169 volatile struct lock *lkp = *lkpp;
170 LOCKSTAT_TIMER(slptime);
171 LOCKSTAT_FLAG(lsflag);
172
173 KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
174
175 LOCKSTAT_ENTER(lsflag);
176
177 for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
178 if (drain)
179 lkp->lk_flags |= LK_WAITDRAIN;
180 else {
181 lkp->lk_waitcount++;
182 lkp->lk_flags |= LK_WAIT_NONZERO;
183 }
184 /* XXX Cast away volatile. */
185 LOCKSTAT_START_TIMER(lsflag, slptime);
186 error = mtsleep(drain ?
187 (volatile const void *)&lkp->lk_flags :
188 (volatile const void *)lkp, lkp->lk_prio,
189 lkp->lk_wmesg, lkp->lk_timo,
190 __UNVOLATILE(&lkp->lk_interlock));
191 LOCKSTAT_STOP_TIMER(lsflag, slptime);
192 LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
193 LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
194 if (!drain) {
195 lkp->lk_waitcount--;
196 if (lkp->lk_waitcount == 0)
197 lkp->lk_flags &= ~LK_WAIT_NONZERO;
198 }
199 if (error)
200 break;
201 if (extflags & LK_SLEEPFAIL) {
202 error = ENOLCK;
203 break;
204 }
205 if (lkp->lk_newlock != NULL) {
206 mutex_enter(__UNVOLATILE
207 (&lkp->lk_newlock->lk_interlock));
208 mutex_exit(__UNVOLATILE
209 (&lkp->lk_interlock));
210 if (lkp->lk_waitcount == 0)
211 wakeup(&lkp->lk_newlock);
212 *lkpp = lkp = lkp->lk_newlock;
213 }
214 }
215
216 LOCKSTAT_EXIT(lsflag);
217
218 return error;
219 }
220
221 #define SETHOLDER(lkp, pid, lid, cpu_id) \
222 do { \
223 (lkp)->lk_lockholder = pid; \
224 (lkp)->lk_locklwp = lid; \
225 } while (/*CONSTCOND*/0)
226
227 #define WEHOLDIT(lkp, pid, lid, cpu_id) \
228 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
229
230 #define WAKEUP_WAITER(lkp) \
231 do { \
232 if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) { \
233 wakeup((lkp)); \
234 } \
235 } while (/*CONSTCOND*/0)
236
237 #if defined(LOCKDEBUG)
238 /*
239 * Lock debug printing routine; can be configured to print to console
240 * or log to syslog.
241 */
242 void
243 lock_printf(const char *fmt, ...)
244 {
245 char b[150];
246 va_list ap;
247
248 va_start(ap, fmt);
249 if (lock_debug_syslog)
250 vlog(LOG_DEBUG, fmt, ap);
251 else {
252 vsnprintf(b, sizeof(b), fmt, ap);
253 printf_nolog("%s", b);
254 }
255 va_end(ap);
256 }
257 #endif /* LOCKDEBUG */
258
259 static void
260 lockpanic(volatile struct lock *lkp, const char *fmt, ...)
261 {
262 char s[150], b[150];
263 #ifdef LOCKDEBUG
264 static const char *locktype[] = {
265 "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
266 "downgrade", "release", "drain", "exclother", "*9*",
267 "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
268 };
269 #endif
270
271 va_list ap;
272 va_start(ap, fmt);
273 vsnprintf(s, sizeof(s), fmt, ap);
274 va_end(ap);
275 bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
276 panic("%s ("
277 #ifdef LOCKDEBUG
278 "type %s "
279 #endif
280 "flags %s, sharecount %d, exclusivecount %d, "
281 "recurselevel %d, waitcount %d, wmesg %s"
282 #ifdef LOCKDEBUG
283 ", lock_file %s, unlock_file %s, lock_line %d, unlock_line %d"
284 #endif
285 ")\n",
286 s,
287 #ifdef LOCKDEBUG
288 locktype[lkp->lk_flags & LK_TYPE_MASK],
289 #endif
290 b, lkp->lk_sharecount, lkp->lk_exclusivecount,
291 lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg
292 #ifdef LOCKDEBUG
293 , lkp->lk_lock_file, lkp->lk_unlock_file, lkp->lk_lock_line,
294 lkp->lk_unlock_line
295 #endif
296 );
297 }
298
299 /*
300 * Transfer any waiting processes from one lock to another.
301 */
302 void
303 transferlockers(struct lock *from, struct lock *to)
304 {
305
306 KASSERT(from != to);
307 KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
308 if (from->lk_waitcount == 0)
309 return;
310 from->lk_newlock = to;
311 wakeup((void *)from);
312 tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
313 from->lk_newlock = NULL;
314 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
315 KASSERT(from->lk_waitcount == 0);
316 }
317
318
319 /*
320 * Initialize a lock; required before use.
321 */
322 void
323 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
324 {
325
326 memset(lkp, 0, sizeof(struct lock));
327 lkp->lk_flags = flags & LK_EXTFLG_MASK;
328 mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
329 lkp->lk_lockholder = LK_NOPROC;
330 lkp->lk_newlock = NULL;
331 lkp->lk_prio = prio;
332 lkp->lk_timo = timo;
333 lkp->lk_wmesg = wmesg;
334 #if defined(LOCKDEBUG)
335 lkp->lk_lock_file = NULL;
336 lkp->lk_unlock_file = NULL;
337 #endif
338 }
339
340 /*
341 * Determine the status of a lock.
342 */
343 int
344 lockstatus(struct lock *lkp)
345 {
346 int lock_type = 0;
347 struct lwp *l = curlwp; /* XXX */
348 pid_t pid;
349 lwpid_t lid;
350 cpuid_t cpu_num;
351
352 if (l == NULL) {
353 cpu_num = cpu_number();
354 pid = LK_KERNPROC;
355 lid = 0;
356 } else {
357 cpu_num = LK_NOCPU;
358 pid = l->l_proc->p_pid;
359 lid = l->l_lid;
360 }
361
362 mutex_enter(&lkp->lk_interlock);
363 if (lkp->lk_exclusivecount != 0) {
364 if (WEHOLDIT(lkp, pid, lid, cpu_num))
365 lock_type = LK_EXCLUSIVE;
366 else
367 lock_type = LK_EXCLOTHER;
368 } else if (lkp->lk_sharecount != 0)
369 lock_type = LK_SHARED;
370 else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
371 lock_type = LK_EXCLOTHER;
372 mutex_exit(__UNVOLATILE(&lkp->lk_interlock));
373 return (lock_type);
374 }
375
376 /*
377 * Locks and IPLs (interrupt priority levels):
378 *
379 * Locks which may be taken from interrupt context must be handled
380 * very carefully; you must spl to the highest IPL where the lock
381 * is needed before acquiring the lock.
382 *
383 * In addition, the lock-debugging hooks themselves need to use locks!
384 *
385 * A raw __cpu_simple_lock may be used from interrupts are long as it
386 * is acquired and held at a single IPL.
387 *
388 * A simple_lock (which is a __cpu_simple_lock wrapped with some
389 * debugging hooks) may be used at or below spllock(), which is
390 * typically at or just below splhigh() (i.e. blocks everything
391 * but certain machine-dependent extremely high priority interrupts).
392 *
393 * Some platforms may have interrupts of higher priority than splsched(),
394 * including hard serial interrupts, inter-processor interrupts, and
395 * kernel debugger traps.
396 */
397
398 /*
399 * XXX XXX kludge around another kludge..
400 *
401 * vfs_shutdown() may be called from interrupt context, either as a result
402 * of a panic, or from the debugger. It proceeds to call
403 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
404 *
405 * We would like to make an attempt to sync the filesystems in this case, so
406 * if this happens, we treat attempts to acquire locks specially.
407 * All locks are acquired on behalf of proc0.
408 *
409 * If we've already paniced, we don't block waiting for locks, but
410 * just barge right ahead since we're already going down in flames.
411 */
412
413 /*
414 * Set, change, or release a lock.
415 *
416 * Shared requests increment the shared count. Exclusive requests set the
417 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
418 * accepted shared locks and shared-to-exclusive upgrades to go away.
419 */
420 int
421 #if defined(LOCKDEBUG)
422 _lockmgr(volatile struct lock *lkp, u_int flags,
423 kmutex_t *interlkp, const char *file, int line)
424 #else
425 lockmgr(volatile struct lock *lkp, u_int flags,
426 kmutex_t *interlkp)
427 #endif
428 {
429 int error;
430 pid_t pid;
431 lwpid_t lid;
432 int extflags;
433 cpuid_t cpu_num;
434 struct lwp *l = curlwp;
435 int lock_shutdown_noblock = 0;
436 kmutex_t *mutex;
437 int s = 0;
438
439 error = 0;
440 mutex = __UNVOLATILE(&lkp->lk_interlock);
441
442 /* LK_RETRY is for vn_lock, not for lockmgr. */
443 KASSERT((flags & LK_RETRY) == 0);
444
445 mutex_enter(mutex);
446 if (flags & LK_INTERLOCK)
447 mutex_exit(__UNVOLATILE(interlkp));
448 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
449
450 if (l == NULL) {
451 if (!doing_shutdown) {
452 panic("lockmgr: no context");
453 } else {
454 l = &lwp0;
455 if (panicstr && (!(flags & LK_NOWAIT))) {
456 flags |= LK_NOWAIT;
457 lock_shutdown_noblock = 1;
458 }
459 }
460 }
461 lid = l->l_lid;
462 pid = l->l_proc->p_pid;
463 cpu_num = cpu_number();
464
465 /*
466 * Once a lock has drained, the LK_DRAINING flag is set and an
467 * exclusive lock is returned. The only valid operation thereafter
468 * is a single release of that exclusive lock. This final release
469 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
470 * further requests of any sort will result in a panic. The bits
471 * selected for these two flags are chosen so that they will be set
472 * in memory that is freed (freed memory is filled with 0xdeadbeef).
473 * The final release is permitted to give a new lease on life to
474 * the lock by specifying LK_REENABLE.
475 */
476 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
477 #ifdef DIAGNOSTIC /* { */
478 if (lkp->lk_flags & LK_DRAINED)
479 lockpanic(lkp, "lockmgr: using decommissioned lock");
480 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
481 WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
482 lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
483 flags & LK_TYPE_MASK);
484 #endif /* DIAGNOSTIC */ /* } */
485 lkp->lk_flags &= ~LK_DRAINING;
486 if ((flags & LK_REENABLE) == 0)
487 lkp->lk_flags |= LK_DRAINED;
488 }
489
490 switch (flags & LK_TYPE_MASK) {
491
492 case LK_SHARED:
493 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
494 /*
495 * If just polling, check to see if we will block.
496 */
497 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
498 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
499 error = EBUSY;
500 break;
501 }
502 /*
503 * Wait for exclusive locks and upgrades to clear.
504 */
505 error = acquire(&lkp, &s, extflags, 0,
506 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
507 RETURN_ADDRESS);
508 if (error)
509 break;
510 lkp->lk_sharecount++;
511 lkp->lk_flags |= LK_SHARE_NONZERO;
512 COUNT(lkp, l, cpu_num, 1);
513 break;
514 }
515 /*
516 * We hold an exclusive lock, so downgrade it to shared.
517 * An alternative would be to fail with EDEADLK.
518 */
519 lkp->lk_sharecount++;
520 lkp->lk_flags |= LK_SHARE_NONZERO;
521 COUNT(lkp, l, cpu_num, 1);
522 /* fall into downgrade */
523
524 case LK_DOWNGRADE:
525 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
526 lkp->lk_exclusivecount == 0)
527 lockpanic(lkp, "lockmgr: not holding exclusive lock");
528 lkp->lk_sharecount += lkp->lk_exclusivecount;
529 lkp->lk_flags |= LK_SHARE_NONZERO;
530 lkp->lk_exclusivecount = 0;
531 lkp->lk_recurselevel = 0;
532 lkp->lk_flags &= ~LK_HAVE_EXCL;
533 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
534 #if defined(LOCKDEBUG)
535 lkp->lk_unlock_file = file;
536 lkp->lk_unlock_line = line;
537 #endif
538 WAKEUP_WAITER(lkp);
539 break;
540
541 case LK_EXCLUPGRADE:
542 /*
543 * If another process is ahead of us to get an upgrade,
544 * then we want to fail rather than have an intervening
545 * exclusive access.
546 */
547 if (lkp->lk_flags & LK_WANT_UPGRADE) {
548 lkp->lk_sharecount--;
549 if (lkp->lk_sharecount == 0)
550 lkp->lk_flags &= ~LK_SHARE_NONZERO;
551 COUNT(lkp, l, cpu_num, -1);
552 error = EBUSY;
553 break;
554 }
555 /* fall into normal upgrade */
556
557 case LK_UPGRADE:
558 /*
559 * Upgrade a shared lock to an exclusive one. If another
560 * shared lock has already requested an upgrade to an
561 * exclusive lock, our shared lock is released and an
562 * exclusive lock is requested (which will be granted
563 * after the upgrade). If we return an error, the file
564 * will always be unlocked.
565 */
566 if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
567 lockpanic(lkp, "lockmgr: upgrade exclusive lock");
568 lkp->lk_sharecount--;
569 if (lkp->lk_sharecount == 0)
570 lkp->lk_flags &= ~LK_SHARE_NONZERO;
571 COUNT(lkp, l, cpu_num, -1);
572 /*
573 * If we are just polling, check to see if we will block.
574 */
575 if ((extflags & LK_NOWAIT) &&
576 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
577 lkp->lk_sharecount > 1)) {
578 error = EBUSY;
579 break;
580 }
581 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
582 /*
583 * We are first shared lock to request an upgrade, so
584 * request upgrade and wait for the shared count to
585 * drop to zero, then take exclusive lock.
586 */
587 lkp->lk_flags |= LK_WANT_UPGRADE;
588 error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
589 RETURN_ADDRESS);
590 lkp->lk_flags &= ~LK_WANT_UPGRADE;
591 if (error) {
592 WAKEUP_WAITER(lkp);
593 break;
594 }
595 lkp->lk_flags |= LK_HAVE_EXCL;
596 SETHOLDER(lkp, pid, lid, cpu_num);
597 #if defined(LOCKDEBUG)
598 lkp->lk_lock_file = file;
599 lkp->lk_lock_line = line;
600 #endif
601 if (lkp->lk_exclusivecount != 0)
602 lockpanic(lkp, "lockmgr: non-zero exclusive count");
603 lkp->lk_exclusivecount = 1;
604 if (extflags & LK_SETRECURSE)
605 lkp->lk_recurselevel = 1;
606 COUNT(lkp, l, cpu_num, 1);
607 break;
608 }
609 /*
610 * Someone else has requested upgrade. Release our shared
611 * lock, awaken upgrade requestor if we are the last shared
612 * lock, then request an exclusive lock.
613 */
614 if (lkp->lk_sharecount == 0)
615 WAKEUP_WAITER(lkp);
616 /* fall into exclusive request */
617
618 case LK_EXCLUSIVE:
619 if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
620 /*
621 * Recursive lock.
622 */
623 if ((extflags & LK_CANRECURSE) == 0 &&
624 lkp->lk_recurselevel == 0) {
625 if (extflags & LK_RECURSEFAIL) {
626 error = EDEADLK;
627 break;
628 } else
629 lockpanic(lkp, "lockmgr: locking against myself");
630 }
631 lkp->lk_exclusivecount++;
632 if (extflags & LK_SETRECURSE &&
633 lkp->lk_recurselevel == 0)
634 lkp->lk_recurselevel = lkp->lk_exclusivecount;
635 COUNT(lkp, l, cpu_num, 1);
636 break;
637 }
638 /*
639 * If we are just polling, check to see if we will sleep.
640 */
641 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
642 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
643 LK_SHARE_NONZERO))) {
644 error = EBUSY;
645 break;
646 }
647 /*
648 * Try to acquire the want_exclusive flag.
649 */
650 error = acquire(&lkp, &s, extflags, 0,
651 LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
652 if (error)
653 break;
654 lkp->lk_flags |= LK_WANT_EXCL;
655 /*
656 * Wait for shared locks and upgrades to finish.
657 */
658 error = acquire(&lkp, &s, extflags, 0,
659 LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
660 RETURN_ADDRESS);
661 lkp->lk_flags &= ~LK_WANT_EXCL;
662 if (error) {
663 WAKEUP_WAITER(lkp);
664 break;
665 }
666 lkp->lk_flags |= LK_HAVE_EXCL;
667 SETHOLDER(lkp, pid, lid, cpu_num);
668 #if defined(LOCKDEBUG)
669 lkp->lk_lock_file = file;
670 lkp->lk_lock_line = line;
671 #endif
672 if (lkp->lk_exclusivecount != 0)
673 lockpanic(lkp, "lockmgr: non-zero exclusive count");
674 lkp->lk_exclusivecount = 1;
675 if (extflags & LK_SETRECURSE)
676 lkp->lk_recurselevel = 1;
677 COUNT(lkp, l, cpu_num, 1);
678 break;
679
680 case LK_RELEASE:
681 if (lkp->lk_exclusivecount != 0) {
682 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
683 lockpanic(lkp, "lockmgr: pid %d, not "
684 "exclusive lock holder %d "
685 "unlocking", pid,
686 lkp->lk_lockholder);
687 }
688 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
689 lkp->lk_recurselevel = 0;
690 lkp->lk_exclusivecount--;
691 COUNT(lkp, l, cpu_num, -1);
692 if (lkp->lk_exclusivecount == 0) {
693 lkp->lk_flags &= ~LK_HAVE_EXCL;
694 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
695 #if defined(LOCKDEBUG)
696 lkp->lk_unlock_file = file;
697 lkp->lk_unlock_line = line;
698 #endif
699 }
700 } else if (lkp->lk_sharecount != 0) {
701 lkp->lk_sharecount--;
702 if (lkp->lk_sharecount == 0)
703 lkp->lk_flags &= ~LK_SHARE_NONZERO;
704 COUNT(lkp, l, cpu_num, -1);
705 }
706 #ifdef DIAGNOSTIC
707 else
708 lockpanic(lkp, "lockmgr: release of unlocked lock!");
709 #endif
710 WAKEUP_WAITER(lkp);
711 break;
712
713 case LK_DRAIN:
714 /*
715 * Check that we do not already hold the lock, as it can
716 * never drain if we do. Unfortunately, we have no way to
717 * check for holding a shared lock, but at least we can
718 * check for an exclusive one.
719 */
720 if (WEHOLDIT(lkp, pid, lid, cpu_num))
721 lockpanic(lkp, "lockmgr: draining against myself");
722 /*
723 * If we are just polling, check to see if we will sleep.
724 */
725 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
726 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
727 LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
728 error = EBUSY;
729 break;
730 }
731 error = acquire(&lkp, &s, extflags, 1,
732 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
733 LK_SHARE_NONZERO | LK_WAIT_NONZERO,
734 RETURN_ADDRESS);
735 if (error)
736 break;
737 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
738 SETHOLDER(lkp, pid, lid, cpu_num);
739 #if defined(LOCKDEBUG)
740 lkp->lk_lock_file = file;
741 lkp->lk_lock_line = line;
742 #endif
743 lkp->lk_exclusivecount = 1;
744 /* XXX unlikely that we'd want this */
745 if (extflags & LK_SETRECURSE)
746 lkp->lk_recurselevel = 1;
747 COUNT(lkp, l, cpu_num, 1);
748 break;
749
750 default:
751 mutex_exit(mutex);
752 lockpanic(lkp, "lockmgr: unknown locktype request %d",
753 flags & LK_TYPE_MASK);
754 /* NOTREACHED */
755 }
756 if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
757 ((lkp->lk_flags &
758 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
759 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
760 lkp->lk_flags &= ~LK_WAITDRAIN;
761 wakeup(&lkp->lk_flags);
762 }
763 /*
764 * Note that this panic will be a recursive panic, since
765 * we only set lock_shutdown_noblock above if panicstr != NULL.
766 */
767 if (error && lock_shutdown_noblock)
768 lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
769
770 mutex_exit(mutex);
771 return (error);
772 }
773
774 /*
775 * Print out information about state of a lock. Used by VOP_PRINT
776 * routines to display ststus about contained locks.
777 */
778 void
779 lockmgr_printinfo(volatile struct lock *lkp)
780 {
781
782 if (lkp->lk_sharecount)
783 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
784 lkp->lk_sharecount);
785 else if (lkp->lk_flags & LK_HAVE_EXCL) {
786 printf(" lock type %s: EXCL (count %d) by ",
787 lkp->lk_wmesg, lkp->lk_exclusivecount);
788 printf("pid %d.%d", lkp->lk_lockholder,
789 lkp->lk_locklwp);
790 } else
791 printf(" not locked");
792 if (lkp->lk_waitcount > 0)
793 printf(" with %d pending", lkp->lk_waitcount);
794 }
795
796 #if defined(LOCKDEBUG) /* { */
797 _TAILQ_HEAD(, struct simplelock, volatile) simplelock_list =
798 TAILQ_HEAD_INITIALIZER(simplelock_list);
799
800 #if defined(MULTIPROCESSOR) /* { */
801 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
802
803 #define SLOCK_LIST_LOCK() \
804 __cpu_simple_lock(&simplelock_list_slock.lock_data)
805
806 #define SLOCK_LIST_UNLOCK() \
807 __cpu_simple_unlock(&simplelock_list_slock.lock_data)
808
809 #define SLOCK_COUNT(x) \
810 curcpu()->ci_simple_locks += (x)
811 #else
812 u_long simple_locks;
813
814 #define SLOCK_LIST_LOCK() /* nothing */
815
816 #define SLOCK_LIST_UNLOCK() /* nothing */
817
818 #define SLOCK_COUNT(x) simple_locks += (x)
819 #endif /* MULTIPROCESSOR */ /* } */
820
821 #ifdef MULTIPROCESSOR
822 #define SLOCK_MP() lock_printf("on CPU %ld\n", \
823 (u_long) cpu_number())
824 #else
825 #define SLOCK_MP() /* nothing */
826 #endif
827
828 #define SLOCK_WHERE(str, alp, id, l) \
829 do { \
830 lock_printf("\n"); \
831 lock_printf(str); \
832 lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
833 SLOCK_MP(); \
834 if ((alp)->lock_file != NULL) \
835 lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
836 (alp)->lock_line); \
837 if ((alp)->unlock_file != NULL) \
838 lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
839 (alp)->unlock_line); \
840 SLOCK_TRACE() \
841 SLOCK_DEBUGGER(); \
842 } while (/*CONSTCOND*/0)
843
844 /*
845 * Simple lock functions so that the debugger can see from whence
846 * they are being called.
847 */
848 void
849 simple_lock_init(volatile struct simplelock *alp)
850 {
851
852 #if defined(MULTIPROCESSOR) /* { */
853 __cpu_simple_lock_init(&alp->lock_data);
854 #else
855 alp->lock_data = __SIMPLELOCK_UNLOCKED;
856 #endif /* } */
857 alp->lock_file = NULL;
858 alp->lock_line = 0;
859 alp->unlock_file = NULL;
860 alp->unlock_line = 0;
861 alp->lock_holder = LK_NOCPU;
862 }
863
864 void
865 _simple_lock(volatile struct simplelock *alp, const char *id, int l)
866 {
867 cpuid_t cpu_num = cpu_number();
868 int s;
869
870 s = spllock();
871
872 /*
873 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
874 * don't take any action, and just fall into the normal spin case.
875 */
876 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
877 #if defined(MULTIPROCESSOR) /* { */
878 if (alp->lock_holder == cpu_num) {
879 SLOCK_WHERE("simple_lock: locking against myself\n",
880 alp, id, l);
881 goto out;
882 }
883 #else
884 SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
885 goto out;
886 #endif /* MULTIPROCESSOR */ /* } */
887 }
888
889 #if defined(MULTIPROCESSOR) /* { */
890 /* Acquire the lock before modifying any fields. */
891 splx(s);
892 __cpu_simple_lock(&alp->lock_data);
893 s = spllock();
894 #else
895 alp->lock_data = __SIMPLELOCK_LOCKED;
896 #endif /* } */
897
898 if (alp->lock_holder != LK_NOCPU) {
899 SLOCK_WHERE("simple_lock: uninitialized lock\n",
900 alp, id, l);
901 }
902 alp->lock_file = id;
903 alp->lock_line = l;
904 alp->lock_holder = cpu_num;
905
906 SLOCK_LIST_LOCK();
907 TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
908 SLOCK_LIST_UNLOCK();
909
910 SLOCK_COUNT(1);
911
912 out:
913 splx(s);
914 }
915
916 int
917 _simple_lock_held(volatile struct simplelock *alp)
918 {
919 #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
920 cpuid_t cpu_num = cpu_number();
921 #endif
922 int s, locked = 0;
923
924 s = spllock();
925
926 #if defined(MULTIPROCESSOR)
927 if (__cpu_simple_lock_try(&alp->lock_data) == 0)
928 locked = (alp->lock_holder == cpu_num);
929 else
930 __cpu_simple_unlock(&alp->lock_data);
931 #else
932 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
933 locked = 1;
934 KASSERT(alp->lock_holder == cpu_num);
935 }
936 #endif
937
938 splx(s);
939
940 return (locked);
941 }
942
943 int
944 _simple_lock_try(volatile struct simplelock *alp, const char *id, int l)
945 {
946 cpuid_t cpu_num = cpu_number();
947 int s, rv = 0;
948
949 s = spllock();
950
951 /*
952 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
953 * don't take any action.
954 */
955 #if defined(MULTIPROCESSOR) /* { */
956 if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
957 if (alp->lock_holder == cpu_num)
958 SLOCK_WHERE("simple_lock_try: locking against myself\n",
959 alp, id, l);
960 goto out;
961 }
962 #else
963 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
964 SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
965 goto out;
966 }
967 alp->lock_data = __SIMPLELOCK_LOCKED;
968 #endif /* MULTIPROCESSOR */ /* } */
969
970 /*
971 * At this point, we have acquired the lock.
972 */
973
974 rv = 1;
975
976 alp->lock_file = id;
977 alp->lock_line = l;
978 alp->lock_holder = cpu_num;
979
980 SLOCK_LIST_LOCK();
981 TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
982 SLOCK_LIST_UNLOCK();
983
984 SLOCK_COUNT(1);
985
986 out:
987 splx(s);
988 return (rv);
989 }
990
991 void
992 _simple_unlock(volatile struct simplelock *alp, const char *id, int l)
993 {
994 int s;
995
996 s = spllock();
997
998 /*
999 * MULTIPROCESSOR case: This is `safe' because we think we hold
1000 * the lock, and if we don't, we don't take any action.
1001 */
1002 if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
1003 SLOCK_WHERE("simple_unlock: lock not held\n",
1004 alp, id, l);
1005 goto out;
1006 }
1007
1008 SLOCK_LIST_LOCK();
1009 TAILQ_REMOVE(&simplelock_list, alp, list);
1010 SLOCK_LIST_UNLOCK();
1011
1012 SLOCK_COUNT(-1);
1013
1014 alp->list.tqe_next = NULL; /* sanity */
1015 alp->list.tqe_prev = NULL; /* sanity */
1016
1017 alp->unlock_file = id;
1018 alp->unlock_line = l;
1019
1020 #if defined(MULTIPROCESSOR) /* { */
1021 alp->lock_holder = LK_NOCPU;
1022 /* Now that we've modified all fields, release the lock. */
1023 __cpu_simple_unlock(&alp->lock_data);
1024 #else
1025 alp->lock_data = __SIMPLELOCK_UNLOCKED;
1026 KASSERT(alp->lock_holder == cpu_number());
1027 alp->lock_holder = LK_NOCPU;
1028 #endif /* } */
1029
1030 out:
1031 splx(s);
1032 }
1033
1034 void
1035 simple_lock_dump(void)
1036 {
1037 volatile struct simplelock *alp;
1038 int s;
1039
1040 s = spllock();
1041 SLOCK_LIST_LOCK();
1042 lock_printf("all simple locks:\n");
1043 TAILQ_FOREACH(alp, &simplelock_list, list) {
1044 lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
1045 alp->lock_file, alp->lock_line);
1046 }
1047 SLOCK_LIST_UNLOCK();
1048 splx(s);
1049 }
1050
1051 void
1052 simple_lock_freecheck(void *start, void *end)
1053 {
1054 volatile struct simplelock *alp;
1055 int s;
1056
1057 s = spllock();
1058 SLOCK_LIST_LOCK();
1059 TAILQ_FOREACH(alp, &simplelock_list, list) {
1060 if ((volatile void *)alp >= start &&
1061 (volatile void *)alp < end) {
1062 lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
1063 alp, alp->lock_holder, alp->lock_file,
1064 alp->lock_line);
1065 SLOCK_DEBUGGER();
1066 }
1067 }
1068 SLOCK_LIST_UNLOCK();
1069 splx(s);
1070 }
1071
1072 /*
1073 * We must be holding exactly one lock: the sched_lock.
1074 */
1075
1076 void
1077 simple_lock_switchcheck(void)
1078 {
1079
1080 simple_lock_only_held(NULL, "switching");
1081 }
1082
1083 /*
1084 * Drop into the debugger if lp isn't the only lock held.
1085 * lp may be NULL.
1086 */
1087 void
1088 simple_lock_only_held(volatile struct simplelock *lp, const char *where)
1089 {
1090 volatile struct simplelock *alp;
1091 cpuid_t cpu_num = cpu_number();
1092 int s;
1093
1094 if (lp) {
1095 LOCK_ASSERT(simple_lock_held(lp));
1096 }
1097 s = spllock();
1098 SLOCK_LIST_LOCK();
1099 TAILQ_FOREACH(alp, &simplelock_list, list) {
1100 if (alp == lp)
1101 continue;
1102 if (alp->lock_holder == cpu_num)
1103 break;
1104 }
1105 SLOCK_LIST_UNLOCK();
1106 splx(s);
1107
1108 if (alp != NULL) {
1109 lock_printf("\n%s with held simple_lock %p "
1110 "CPU %lu %s:%d\n",
1111 where, alp, alp->lock_holder, alp->lock_file,
1112 alp->lock_line);
1113 SLOCK_TRACE();
1114 SLOCK_DEBUGGER();
1115 }
1116 }
1117
1118 /*
1119 * Set to 1 by simple_lock_assert_*().
1120 * Can be cleared from ddb to avoid a panic.
1121 */
1122 int slock_assert_will_panic;
1123
1124 /*
1125 * If the lock isn't held, print a traceback, optionally drop into the
1126 * debugger, then panic.
1127 * The panic can be avoided by clearing slock_assert_with_panic from the
1128 * debugger.
1129 */
1130 void
1131 _simple_lock_assert_locked(volatile struct simplelock *alp,
1132 const char *lockname, const char *id, int l)
1133 {
1134 if (simple_lock_held(alp) == 0) {
1135 slock_assert_will_panic = 1;
1136 lock_printf("%s lock not held\n", lockname);
1137 SLOCK_WHERE("lock not held", alp, id, l);
1138 if (slock_assert_will_panic)
1139 panic("%s: not locked", lockname);
1140 }
1141 }
1142
1143 void
1144 _simple_lock_assert_unlocked(volatile struct simplelock *alp,
1145 const char *lockname, const char *id, int l)
1146 {
1147 if (simple_lock_held(alp)) {
1148 slock_assert_will_panic = 1;
1149 lock_printf("%s lock held\n", lockname);
1150 SLOCK_WHERE("lock held", alp, id, l);
1151 if (slock_assert_will_panic)
1152 panic("%s: locked", lockname);
1153 }
1154 }
1155
1156 void
1157 assert_sleepable(struct simplelock *interlock, const char *msg)
1158 {
1159
1160 if (curlwp == NULL) {
1161 panic("assert_sleepable: NULL curlwp");
1162 }
1163 simple_lock_only_held(interlock, msg);
1164 }
1165
1166 #endif /* LOCKDEBUG */ /* } */
1167
1168 #if defined(MULTIPROCESSOR)
1169
1170 /*
1171 * Functions for manipulating the kernel_lock. We put them here
1172 * so that they show up in profiles.
1173 */
1174
1175 #define _KERNEL_LOCK_ABORT(msg) \
1176 LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops, \
1177 __FUNCTION__, msg)
1178
1179 #ifdef LOCKDEBUG
1180 #define _KERNEL_LOCK_ASSERT(cond) \
1181 do { \
1182 if (!(cond)) \
1183 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
1184 } while (/* CONSTCOND */ 0)
1185 #else
1186 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
1187 #endif
1188
1189 void _kernel_lock_dump(volatile void *);
1190
1191 lockops_t _kernel_lock_ops = {
1192 "Kernel lock",
1193 0,
1194 _kernel_lock_dump
1195 };
1196
1197 /*
1198 * Initialize the kernel lock.
1199 */
1200 void
1201 _kernel_lock_init(void)
1202 {
1203
1204 __cpu_simple_lock_init(&kernel_lock);
1205 kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
1206 }
1207
1208 /*
1209 * Print debugging information about the kernel lock.
1210 */
1211 void
1212 _kernel_lock_dump(volatile void *junk)
1213 {
1214 struct cpu_info *ci = curcpu();
1215
1216 (void)junk;
1217
1218 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
1219 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
1220 }
1221
1222 /*
1223 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
1224 * acquisition is from process context.
1225 */
1226 void
1227 _kernel_lock(int nlocks, struct lwp *l)
1228 {
1229 struct cpu_info *ci = curcpu();
1230 LOCKSTAT_TIMER(spintime);
1231 LOCKSTAT_FLAG(lsflag);
1232 struct lwp *owant;
1233 #ifdef LOCKDEBUG
1234 u_int spins;
1235 #endif
1236 int s;
1237
1238 (void)l;
1239
1240 if (nlocks == 0)
1241 return;
1242 _KERNEL_LOCK_ASSERT(nlocks > 0);
1243
1244 s = splbiglock();
1245
1246 if (ci->ci_biglock_count != 0) {
1247 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
1248 ci->ci_biglock_count += nlocks;
1249 splx(s);
1250 return;
1251 }
1252
1253 LOCKDEBUG_WANTLOCK(kernel_lock_id,
1254 (uintptr_t)__builtin_return_address(0), 0);
1255
1256 if (__cpu_simple_lock_try(&kernel_lock)) {
1257 ci->ci_biglock_count = nlocks;
1258 LOCKDEBUG_LOCKED(kernel_lock_id,
1259 (uintptr_t)__builtin_return_address(0), 0);
1260 splx(s);
1261 return;
1262 }
1263
1264 LOCKSTAT_ENTER(lsflag);
1265 LOCKSTAT_START_TIMER(lsflag, spintime);
1266
1267 /*
1268 * Before setting ci_biglock_wanted we must post a store
1269 * fence (see kern_mutex.c). This is accomplished by the
1270 * __cpu_simple_lock_try() above.
1271 */
1272 owant = ci->ci_biglock_wanted;
1273 ci->ci_biglock_wanted = curlwp; /* XXXAD */
1274
1275 #ifdef LOCKDEBUG
1276 spins = 0;
1277 #endif
1278
1279 do {
1280 while (kernel_lock == __SIMPLELOCK_LOCKED) {
1281 #ifdef LOCKDEBUG
1282 if (SPINLOCK_SPINOUT(spins))
1283 _KERNEL_LOCK_ABORT("spinout");
1284 #endif
1285 splx(s);
1286 SPINLOCK_SPIN_HOOK;
1287 (void)splbiglock();
1288 }
1289 } while (!__cpu_simple_lock_try(&kernel_lock));
1290
1291 ci->ci_biglock_wanted = owant;
1292 ci->ci_biglock_count += nlocks;
1293 LOCKSTAT_STOP_TIMER(lsflag, spintime);
1294 LOCKDEBUG_LOCKED(kernel_lock_id,
1295 (uintptr_t)__builtin_return_address(0), 0);
1296 splx(s);
1297
1298 /*
1299 * Again, another store fence is required (see kern_mutex.c).
1300 */
1301 mb_write();
1302 if (owant == NULL) {
1303 LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
1304 1, spintime);
1305 }
1306 LOCKSTAT_EXIT(lsflag);
1307 }
1308
1309 /*
1310 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
1311 * all holds. If 'l' is non-null, the release is from process context.
1312 */
1313 void
1314 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
1315 {
1316 struct cpu_info *ci = curcpu();
1317 u_int olocks;
1318 int s;
1319
1320 (void)l;
1321
1322 _KERNEL_LOCK_ASSERT(nlocks < 2);
1323
1324 olocks = ci->ci_biglock_count;
1325
1326 if (olocks == 0) {
1327 _KERNEL_LOCK_ASSERT(nlocks <= 0);
1328 if (countp != NULL)
1329 *countp = 0;
1330 return;
1331 }
1332
1333 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
1334
1335 if (nlocks == 0)
1336 nlocks = olocks;
1337 else if (nlocks == -1) {
1338 nlocks = 1;
1339 _KERNEL_LOCK_ASSERT(olocks == 1);
1340 }
1341
1342 s = splbiglock();
1343 if ((ci->ci_biglock_count -= nlocks) == 0) {
1344 LOCKDEBUG_UNLOCKED(kernel_lock_id,
1345 (uintptr_t)__builtin_return_address(0), 0);
1346 __cpu_simple_unlock(&kernel_lock);
1347 }
1348 splx(s);
1349
1350 if (countp != NULL)
1351 *countp = olocks;
1352 }
1353
1354 #if defined(DEBUG)
1355 /*
1356 * Assert that the kernel lock is held.
1357 */
1358 void
1359 _kernel_lock_assert_locked(void)
1360 {
1361
1362 if (kernel_lock != __SIMPLELOCK_LOCKED ||
1363 curcpu()->ci_biglock_count == 0)
1364 _KERNEL_LOCK_ABORT("not locked");
1365 }
1366
1367 void
1368 _kernel_lock_assert_unlocked()
1369 {
1370
1371 if (curcpu()->ci_biglock_count != 0)
1372 _KERNEL_LOCK_ABORT("locked");
1373 }
1374 #endif
1375
1376 #endif /* MULTIPROCESSOR || LOCKDEBUG */
1377