kern_lock.c revision 1.110 1 /* $NetBSD: kern_lock.c,v 1.110 2007/03/04 06:20:25 christos 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 2007/03/04 06:20:25 christos 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 #if defined(MULTIPROCESSOR) /* { */
140 #define COUNT_CPU(cpu_id, x) \
141 curcpu()->ci_spin_locks += (x)
142 #else
143 u_long spin_locks;
144 #define COUNT_CPU(cpu_id, x) spin_locks += (x)
145 #endif /* MULTIPROCESSOR */ /* } */
146
147 #define COUNT(lkp, l, cpu_id, x) \
148 do { \
149 if ((lkp)->lk_flags & LK_SPIN) \
150 COUNT_CPU((cpu_id), (x)); \
151 else \
152 (l)->l_locks += (x); \
153 } while (/*CONSTCOND*/0)
154 #else
155 #define COUNT(lkp, p, cpu_id, x)
156 #define COUNT_CPU(cpu_id, x)
157 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
158
159 #define INTERLOCK_ACQUIRE(lkp, flags, s) \
160 do { \
161 if ((flags) & LK_SPIN) \
162 s = spllock(); \
163 simple_lock(&(lkp)->lk_interlock); \
164 } while (/*CONSTCOND*/ 0)
165
166 #define INTERLOCK_RELEASE(lkp, flags, s) \
167 do { \
168 simple_unlock(&(lkp)->lk_interlock); \
169 if ((flags) & LK_SPIN) \
170 splx(s); \
171 } while (/*CONSTCOND*/ 0)
172
173 #ifdef DDB /* { */
174 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
175 int simple_lock_debugger = 1; /* more serious on MP */
176 #else
177 int simple_lock_debugger = 0;
178 #endif
179 #define SLOCK_DEBUGGER() if (simple_lock_debugger && db_onpanic) Debugger()
180 #define SLOCK_TRACE() \
181 db_stack_trace_print((db_expr_t)__builtin_frame_address(0), \
182 true, 65535, "", lock_printf);
183 #else
184 #define SLOCK_DEBUGGER() /* nothing */
185 #define SLOCK_TRACE() /* nothing */
186 #endif /* } */
187
188 #if defined(LOCKDEBUG)
189 #if defined(DDB)
190 #define SPINLOCK_SPINCHECK_DEBUGGER if (db_onpanic) Debugger()
191 #else
192 #define SPINLOCK_SPINCHECK_DEBUGGER /* nothing */
193 #endif
194
195 #define SPINLOCK_SPINCHECK_DECL \
196 /* 32-bits of count -- wrap constitutes a "spinout" */ \
197 uint32_t __spinc = 0
198
199 #define SPINLOCK_SPINCHECK \
200 do { \
201 if (++__spinc == 0) { \
202 lock_printf("LK_SPIN spinout, excl %d, share %d\n", \
203 lkp->lk_exclusivecount, lkp->lk_sharecount); \
204 if (lkp->lk_exclusivecount) \
205 lock_printf("held by CPU %lu\n", \
206 (u_long) lkp->lk_cpu); \
207 if (lkp->lk_lock_file) \
208 lock_printf("last locked at %s:%d\n", \
209 lkp->lk_lock_file, lkp->lk_lock_line); \
210 if (lkp->lk_unlock_file) \
211 lock_printf("last unlocked at %s:%d\n", \
212 lkp->lk_unlock_file, lkp->lk_unlock_line); \
213 SLOCK_TRACE(); \
214 SPINLOCK_SPINCHECK_DEBUGGER; \
215 } \
216 } while (/*CONSTCOND*/ 0)
217 #else
218 #define SPINLOCK_SPINCHECK_DECL /* nothing */
219 #define SPINLOCK_SPINCHECK /* nothing */
220 #endif /* LOCKDEBUG && DDB */
221
222 #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
223
224 /*
225 * Acquire a resource.
226 */
227 static int
228 acquire(volatile struct lock **lkpp, int *s, int extflags,
229 int drain, int wanted, uintptr_t ra)
230 {
231 int error;
232 volatile struct lock *lkp = *lkpp;
233 LOCKSTAT_TIMER(slptime);
234 LOCKSTAT_FLAG(lsflag);
235
236 KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
237
238 if (extflags & LK_SPIN) {
239 int interlocked;
240
241 SPINLOCK_SPINCHECK_DECL;
242
243 if (!drain) {
244 lkp->lk_waitcount++;
245 lkp->lk_flags |= LK_WAIT_NONZERO;
246 }
247 for (interlocked = 1;;) {
248 SPINLOCK_SPINCHECK;
249 if ((lkp->lk_flags & wanted) != 0) {
250 if (interlocked) {
251 INTERLOCK_RELEASE(lkp, LK_SPIN, *s);
252 interlocked = 0;
253 }
254 SPINLOCK_SPIN_HOOK;
255 } else if (interlocked) {
256 break;
257 } else {
258 INTERLOCK_ACQUIRE(lkp, LK_SPIN, *s);
259 interlocked = 1;
260 }
261 }
262 if (!drain) {
263 lkp->lk_waitcount--;
264 if (lkp->lk_waitcount == 0)
265 lkp->lk_flags &= ~LK_WAIT_NONZERO;
266 }
267 KASSERT((lkp->lk_flags & wanted) == 0);
268 error = 0; /* sanity */
269 } else {
270 LOCKSTAT_ENTER(lsflag);
271
272 for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
273 if (drain)
274 lkp->lk_flags |= LK_WAITDRAIN;
275 else {
276 lkp->lk_waitcount++;
277 lkp->lk_flags |= LK_WAIT_NONZERO;
278 }
279 /* XXX Cast away volatile. */
280 LOCKSTAT_START_TIMER(lsflag, slptime);
281 error = ltsleep(drain ?
282 (volatile const void *)&lkp->lk_flags :
283 (volatile const void *)lkp, lkp->lk_prio,
284 lkp->lk_wmesg, lkp->lk_timo, &lkp->lk_interlock);
285 LOCKSTAT_STOP_TIMER(lsflag, slptime);
286 LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
287 LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
288 if (!drain) {
289 lkp->lk_waitcount--;
290 if (lkp->lk_waitcount == 0)
291 lkp->lk_flags &= ~LK_WAIT_NONZERO;
292 }
293 if (error)
294 break;
295 if (extflags & LK_SLEEPFAIL) {
296 error = ENOLCK;
297 break;
298 }
299 if (lkp->lk_newlock != NULL) {
300 simple_lock(&lkp->lk_newlock->lk_interlock);
301 simple_unlock(&lkp->lk_interlock);
302 if (lkp->lk_waitcount == 0)
303 wakeup(&lkp->lk_newlock);
304 *lkpp = lkp = lkp->lk_newlock;
305 }
306 }
307
308 LOCKSTAT_EXIT(lsflag);
309 }
310
311 return error;
312 }
313
314 #define SETHOLDER(lkp, pid, lid, cpu_id) \
315 do { \
316 if ((lkp)->lk_flags & LK_SPIN) \
317 (lkp)->lk_cpu = cpu_id; \
318 else { \
319 (lkp)->lk_lockholder = pid; \
320 (lkp)->lk_locklwp = lid; \
321 } \
322 } while (/*CONSTCOND*/0)
323
324 #define WEHOLDIT(lkp, pid, lid, cpu_id) \
325 (((lkp)->lk_flags & LK_SPIN) != 0 ? \
326 ((lkp)->lk_cpu == (cpu_id)) : \
327 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid)))
328
329 #define WAKEUP_WAITER(lkp) \
330 do { \
331 if (((lkp)->lk_flags & (LK_SPIN | LK_WAIT_NONZERO)) == \
332 LK_WAIT_NONZERO) { \
333 wakeup((lkp)); \
334 } \
335 } while (/*CONSTCOND*/0)
336
337 #if defined(LOCKDEBUG) /* { */
338 #if defined(MULTIPROCESSOR) /* { */
339 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
340
341 #define SPINLOCK_LIST_LOCK() \
342 __cpu_simple_lock(&spinlock_list_slock.lock_data)
343
344 #define SPINLOCK_LIST_UNLOCK() \
345 __cpu_simple_unlock(&spinlock_list_slock.lock_data)
346 #else
347 #define SPINLOCK_LIST_LOCK() /* nothing */
348
349 #define SPINLOCK_LIST_UNLOCK() /* nothing */
350 #endif /* MULTIPROCESSOR */ /* } */
351
352 _TAILQ_HEAD(, struct lock, volatile) spinlock_list =
353 TAILQ_HEAD_INITIALIZER(spinlock_list);
354
355 #define HAVEIT(lkp) \
356 do { \
357 if ((lkp)->lk_flags & LK_SPIN) { \
358 int sp = spllock(); \
359 SPINLOCK_LIST_LOCK(); \
360 TAILQ_INSERT_TAIL(&spinlock_list, (lkp), lk_list); \
361 SPINLOCK_LIST_UNLOCK(); \
362 splx(sp); \
363 } \
364 } while (/*CONSTCOND*/0)
365
366 #define DONTHAVEIT(lkp) \
367 do { \
368 if ((lkp)->lk_flags & LK_SPIN) { \
369 int sp = spllock(); \
370 SPINLOCK_LIST_LOCK(); \
371 TAILQ_REMOVE(&spinlock_list, (lkp), lk_list); \
372 SPINLOCK_LIST_UNLOCK(); \
373 splx(sp); \
374 } \
375 } while (/*CONSTCOND*/0)
376 #else
377 #define HAVEIT(lkp) /* nothing */
378
379 #define DONTHAVEIT(lkp) /* nothing */
380 #endif /* LOCKDEBUG */ /* } */
381
382 #if defined(LOCKDEBUG)
383 /*
384 * Lock debug printing routine; can be configured to print to console
385 * or log to syslog.
386 */
387 void
388 lock_printf(const char *fmt, ...)
389 {
390 char b[150];
391 va_list ap;
392
393 va_start(ap, fmt);
394 if (lock_debug_syslog)
395 vlog(LOG_DEBUG, fmt, ap);
396 else {
397 vsnprintf(b, sizeof(b), fmt, ap);
398 printf_nolog("%s", b);
399 }
400 va_end(ap);
401 }
402 #endif /* LOCKDEBUG */
403
404 static void
405 lockpanic(volatile struct lock *lkp, const char *fmt, ...)
406 {
407 char s[150], b[150];
408 #ifdef LOCKDEBUG
409 static const char *locktype[] = {
410 "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
411 "downgrade", "release", "drain", "exclother", "*9*",
412 "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
413 };
414 #endif
415
416 va_list ap;
417 va_start(ap, fmt);
418 vsnprintf(s, sizeof(s), fmt, ap);
419 va_end(ap);
420 bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
421 panic("%s ("
422 #ifdef LOCKDEBUG
423 "type %s "
424 #endif
425 "flags %s, sharecount %d, exclusivecount %d, "
426 "recurselevel %d, waitcount %d, wmesg %s"
427 #ifdef LOCKDEBUG
428 ", lock_file %s, unlock_file %s, lock_line %d, unlock_line %d"
429 #endif
430 ")\n",
431 s,
432 #ifdef LOCKDEBUG
433 locktype[lkp->lk_flags & LK_TYPE_MASK],
434 #endif
435 b, lkp->lk_sharecount, lkp->lk_exclusivecount,
436 lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg
437 #ifdef LOCKDEBUG
438 , lkp->lk_lock_file, lkp->lk_unlock_file, lkp->lk_lock_line,
439 lkp->lk_unlock_line
440 #endif
441 );
442 }
443
444 /*
445 * Transfer any waiting processes from one lock to another.
446 */
447 void
448 transferlockers(struct lock *from, struct lock *to)
449 {
450
451 KASSERT(from != to);
452 KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
453 if (from->lk_waitcount == 0)
454 return;
455 from->lk_newlock = to;
456 wakeup((void *)from);
457 tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
458 from->lk_newlock = NULL;
459 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
460 KASSERT(from->lk_waitcount == 0);
461 }
462
463
464 /*
465 * Initialize a lock; required before use.
466 */
467 void
468 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
469 {
470
471 memset(lkp, 0, sizeof(struct lock));
472 simple_lock_init(&lkp->lk_interlock);
473 lkp->lk_flags = flags & LK_EXTFLG_MASK;
474 if (flags & LK_SPIN)
475 lkp->lk_cpu = LK_NOCPU;
476 else {
477 lkp->lk_lockholder = LK_NOPROC;
478 lkp->lk_newlock = NULL;
479 lkp->lk_prio = prio;
480 lkp->lk_timo = timo;
481 }
482 lkp->lk_wmesg = wmesg; /* just a name for spin locks */
483 #if defined(LOCKDEBUG)
484 lkp->lk_lock_file = NULL;
485 lkp->lk_unlock_file = NULL;
486 #endif
487 }
488
489 /*
490 * Determine the status of a lock.
491 */
492 int
493 lockstatus(struct lock *lkp)
494 {
495 int s = 0; /* XXX: gcc */
496 int lock_type = 0;
497 struct lwp *l = curlwp; /* XXX */
498 pid_t pid;
499 lwpid_t lid;
500 cpuid_t cpu_num;
501
502 if ((lkp->lk_flags & LK_SPIN) || l == NULL) {
503 cpu_num = cpu_number();
504 pid = LK_KERNPROC;
505 lid = 0;
506 } else {
507 cpu_num = LK_NOCPU;
508 pid = l->l_proc->p_pid;
509 lid = l->l_lid;
510 }
511
512 INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
513 if (lkp->lk_exclusivecount != 0) {
514 if (WEHOLDIT(lkp, pid, lid, cpu_num))
515 lock_type = LK_EXCLUSIVE;
516 else
517 lock_type = LK_EXCLOTHER;
518 } else if (lkp->lk_sharecount != 0)
519 lock_type = LK_SHARED;
520 else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
521 lock_type = LK_EXCLOTHER;
522 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
523 return (lock_type);
524 }
525
526 #if defined(LOCKDEBUG)
527 /*
528 * Make sure no spin locks are held by a CPU that is about
529 * to context switch.
530 */
531 void
532 spinlock_switchcheck(void)
533 {
534 u_long cnt;
535 int s;
536
537 s = spllock();
538 #if defined(MULTIPROCESSOR)
539 cnt = curcpu()->ci_spin_locks;
540 #else
541 cnt = spin_locks;
542 #endif
543 splx(s);
544
545 if (cnt != 0)
546 panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
547 (u_long) cpu_number(), cnt);
548 }
549 #endif /* LOCKDEBUG */
550
551 /*
552 * Locks and IPLs (interrupt priority levels):
553 *
554 * Locks which may be taken from interrupt context must be handled
555 * very carefully; you must spl to the highest IPL where the lock
556 * is needed before acquiring the lock.
557 *
558 * It is also important to avoid deadlock, since certain (very high
559 * priority) interrupts are often needed to keep the system as a whole
560 * from deadlocking, and must not be blocked while you are spinning
561 * waiting for a lower-priority lock.
562 *
563 * In addition, the lock-debugging hooks themselves need to use locks!
564 *
565 * A raw __cpu_simple_lock may be used from interrupts are long as it
566 * is acquired and held at a single IPL.
567 *
568 * A simple_lock (which is a __cpu_simple_lock wrapped with some
569 * debugging hooks) may be used at or below spllock(), which is
570 * typically at or just below splhigh() (i.e. blocks everything
571 * but certain machine-dependent extremely high priority interrupts).
572 *
573 * spinlockmgr spinlocks should be used at or below splsched().
574 *
575 * Some platforms may have interrupts of higher priority than splsched(),
576 * including hard serial interrupts, inter-processor interrupts, and
577 * kernel debugger traps.
578 */
579
580 /*
581 * XXX XXX kludge around another kludge..
582 *
583 * vfs_shutdown() may be called from interrupt context, either as a result
584 * of a panic, or from the debugger. It proceeds to call
585 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
586 *
587 * We would like to make an attempt to sync the filesystems in this case, so
588 * if this happens, we treat attempts to acquire locks specially.
589 * All locks are acquired on behalf of proc0.
590 *
591 * If we've already paniced, we don't block waiting for locks, but
592 * just barge right ahead since we're already going down in flames.
593 */
594
595 /*
596 * Set, change, or release a lock.
597 *
598 * Shared requests increment the shared count. Exclusive requests set the
599 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
600 * accepted shared locks and shared-to-exclusive upgrades to go away.
601 */
602 int
603 #if defined(LOCKDEBUG)
604 _lockmgr(volatile struct lock *lkp, u_int flags,
605 struct simplelock *interlkp, const char *file, int line)
606 #else
607 lockmgr(volatile struct lock *lkp, u_int flags,
608 struct simplelock *interlkp)
609 #endif
610 {
611 int error;
612 pid_t pid;
613 lwpid_t lid;
614 int extflags;
615 cpuid_t cpu_num;
616 struct lwp *l = curlwp;
617 int lock_shutdown_noblock = 0;
618 int s = 0;
619
620 error = 0;
621
622 /* LK_RETRY is for vn_lock, not for lockmgr. */
623 KASSERT((flags & LK_RETRY) == 0);
624
625 INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
626 if (flags & LK_INTERLOCK)
627 simple_unlock(interlkp);
628 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
629
630 #ifdef DIAGNOSTIC /* { */
631 /*
632 * Don't allow spins on sleep locks and don't allow sleeps
633 * on spin locks.
634 */
635 if ((flags ^ lkp->lk_flags) & LK_SPIN)
636 lockpanic(lkp, "lockmgr: sleep/spin mismatch");
637 #endif /* } */
638
639 if (extflags & LK_SPIN) {
640 pid = LK_KERNPROC;
641 lid = 0;
642 } else {
643 if (l == NULL) {
644 if (!doing_shutdown) {
645 panic("lockmgr: no context");
646 } else {
647 l = &lwp0;
648 if (panicstr && (!(flags & LK_NOWAIT))) {
649 flags |= LK_NOWAIT;
650 lock_shutdown_noblock = 1;
651 }
652 }
653 }
654 lid = l->l_lid;
655 pid = l->l_proc->p_pid;
656 }
657 cpu_num = cpu_number();
658
659 /*
660 * Once a lock has drained, the LK_DRAINING flag is set and an
661 * exclusive lock is returned. The only valid operation thereafter
662 * is a single release of that exclusive lock. This final release
663 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
664 * further requests of any sort will result in a panic. The bits
665 * selected for these two flags are chosen so that they will be set
666 * in memory that is freed (freed memory is filled with 0xdeadbeef).
667 * The final release is permitted to give a new lease on life to
668 * the lock by specifying LK_REENABLE.
669 */
670 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
671 #ifdef DIAGNOSTIC /* { */
672 if (lkp->lk_flags & LK_DRAINED)
673 lockpanic(lkp, "lockmgr: using decommissioned lock");
674 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
675 WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
676 lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
677 flags & LK_TYPE_MASK);
678 #endif /* DIAGNOSTIC */ /* } */
679 lkp->lk_flags &= ~LK_DRAINING;
680 if ((flags & LK_REENABLE) == 0)
681 lkp->lk_flags |= LK_DRAINED;
682 }
683
684 switch (flags & LK_TYPE_MASK) {
685
686 case LK_SHARED:
687 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
688 /*
689 * If just polling, check to see if we will block.
690 */
691 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
692 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
693 error = EBUSY;
694 break;
695 }
696 /*
697 * Wait for exclusive locks and upgrades to clear.
698 */
699 error = acquire(&lkp, &s, extflags, 0,
700 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
701 RETURN_ADDRESS);
702 if (error)
703 break;
704 lkp->lk_sharecount++;
705 lkp->lk_flags |= LK_SHARE_NONZERO;
706 COUNT(lkp, l, cpu_num, 1);
707 break;
708 }
709 /*
710 * We hold an exclusive lock, so downgrade it to shared.
711 * An alternative would be to fail with EDEADLK.
712 */
713 lkp->lk_sharecount++;
714 lkp->lk_flags |= LK_SHARE_NONZERO;
715 COUNT(lkp, l, cpu_num, 1);
716 /* fall into downgrade */
717
718 case LK_DOWNGRADE:
719 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
720 lkp->lk_exclusivecount == 0)
721 lockpanic(lkp, "lockmgr: not holding exclusive lock");
722 lkp->lk_sharecount += lkp->lk_exclusivecount;
723 lkp->lk_flags |= LK_SHARE_NONZERO;
724 lkp->lk_exclusivecount = 0;
725 lkp->lk_recurselevel = 0;
726 lkp->lk_flags &= ~LK_HAVE_EXCL;
727 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
728 #if defined(LOCKDEBUG)
729 lkp->lk_unlock_file = file;
730 lkp->lk_unlock_line = line;
731 #endif
732 DONTHAVEIT(lkp);
733 WAKEUP_WAITER(lkp);
734 break;
735
736 case LK_EXCLUPGRADE:
737 /*
738 * If another process is ahead of us to get an upgrade,
739 * then we want to fail rather than have an intervening
740 * exclusive access.
741 */
742 if (lkp->lk_flags & LK_WANT_UPGRADE) {
743 lkp->lk_sharecount--;
744 if (lkp->lk_sharecount == 0)
745 lkp->lk_flags &= ~LK_SHARE_NONZERO;
746 COUNT(lkp, l, cpu_num, -1);
747 error = EBUSY;
748 break;
749 }
750 /* fall into normal upgrade */
751
752 case LK_UPGRADE:
753 /*
754 * Upgrade a shared lock to an exclusive one. If another
755 * shared lock has already requested an upgrade to an
756 * exclusive lock, our shared lock is released and an
757 * exclusive lock is requested (which will be granted
758 * after the upgrade). If we return an error, the file
759 * will always be unlocked.
760 */
761 if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
762 lockpanic(lkp, "lockmgr: upgrade exclusive lock");
763 lkp->lk_sharecount--;
764 if (lkp->lk_sharecount == 0)
765 lkp->lk_flags &= ~LK_SHARE_NONZERO;
766 COUNT(lkp, l, cpu_num, -1);
767 /*
768 * If we are just polling, check to see if we will block.
769 */
770 if ((extflags & LK_NOWAIT) &&
771 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
772 lkp->lk_sharecount > 1)) {
773 error = EBUSY;
774 break;
775 }
776 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
777 /*
778 * We are first shared lock to request an upgrade, so
779 * request upgrade and wait for the shared count to
780 * drop to zero, then take exclusive lock.
781 */
782 lkp->lk_flags |= LK_WANT_UPGRADE;
783 error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
784 RETURN_ADDRESS);
785 lkp->lk_flags &= ~LK_WANT_UPGRADE;
786 if (error) {
787 WAKEUP_WAITER(lkp);
788 break;
789 }
790 lkp->lk_flags |= LK_HAVE_EXCL;
791 SETHOLDER(lkp, pid, lid, cpu_num);
792 #if defined(LOCKDEBUG)
793 lkp->lk_lock_file = file;
794 lkp->lk_lock_line = line;
795 #endif
796 HAVEIT(lkp);
797 if (lkp->lk_exclusivecount != 0)
798 lockpanic(lkp, "lockmgr: non-zero exclusive count");
799 lkp->lk_exclusivecount = 1;
800 if (extflags & LK_SETRECURSE)
801 lkp->lk_recurselevel = 1;
802 COUNT(lkp, l, cpu_num, 1);
803 break;
804 }
805 /*
806 * Someone else has requested upgrade. Release our shared
807 * lock, awaken upgrade requestor if we are the last shared
808 * lock, then request an exclusive lock.
809 */
810 if (lkp->lk_sharecount == 0)
811 WAKEUP_WAITER(lkp);
812 /* fall into exclusive request */
813
814 case LK_EXCLUSIVE:
815 if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
816 /*
817 * Recursive lock.
818 */
819 if ((extflags & LK_CANRECURSE) == 0 &&
820 lkp->lk_recurselevel == 0) {
821 if (extflags & LK_RECURSEFAIL) {
822 error = EDEADLK;
823 break;
824 } else
825 lockpanic(lkp, "lockmgr: locking against myself");
826 }
827 lkp->lk_exclusivecount++;
828 if (extflags & LK_SETRECURSE &&
829 lkp->lk_recurselevel == 0)
830 lkp->lk_recurselevel = lkp->lk_exclusivecount;
831 COUNT(lkp, l, cpu_num, 1);
832 break;
833 }
834 /*
835 * If we are just polling, check to see if we will sleep.
836 */
837 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
838 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
839 LK_SHARE_NONZERO))) {
840 error = EBUSY;
841 break;
842 }
843 /*
844 * Try to acquire the want_exclusive flag.
845 */
846 error = acquire(&lkp, &s, extflags, 0,
847 LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
848 if (error)
849 break;
850 lkp->lk_flags |= LK_WANT_EXCL;
851 /*
852 * Wait for shared locks and upgrades to finish.
853 */
854 error = acquire(&lkp, &s, extflags, 0,
855 LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
856 RETURN_ADDRESS);
857 lkp->lk_flags &= ~LK_WANT_EXCL;
858 if (error) {
859 WAKEUP_WAITER(lkp);
860 break;
861 }
862 lkp->lk_flags |= LK_HAVE_EXCL;
863 SETHOLDER(lkp, pid, lid, cpu_num);
864 #if defined(LOCKDEBUG)
865 lkp->lk_lock_file = file;
866 lkp->lk_lock_line = line;
867 #endif
868 HAVEIT(lkp);
869 if (lkp->lk_exclusivecount != 0)
870 lockpanic(lkp, "lockmgr: non-zero exclusive count");
871 lkp->lk_exclusivecount = 1;
872 if (extflags & LK_SETRECURSE)
873 lkp->lk_recurselevel = 1;
874 COUNT(lkp, l, cpu_num, 1);
875 break;
876
877 case LK_RELEASE:
878 if (lkp->lk_exclusivecount != 0) {
879 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
880 if (lkp->lk_flags & LK_SPIN) {
881 lockpanic(lkp,
882 "lockmgr: processor %lu, not "
883 "exclusive lock holder %lu "
884 "unlocking", cpu_num, lkp->lk_cpu);
885 } else {
886 lockpanic(lkp, "lockmgr: pid %d, not "
887 "exclusive lock holder %d "
888 "unlocking", pid,
889 lkp->lk_lockholder);
890 }
891 }
892 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
893 lkp->lk_recurselevel = 0;
894 lkp->lk_exclusivecount--;
895 COUNT(lkp, l, cpu_num, -1);
896 if (lkp->lk_exclusivecount == 0) {
897 lkp->lk_flags &= ~LK_HAVE_EXCL;
898 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
899 #if defined(LOCKDEBUG)
900 lkp->lk_unlock_file = file;
901 lkp->lk_unlock_line = line;
902 #endif
903 DONTHAVEIT(lkp);
904 }
905 } else if (lkp->lk_sharecount != 0) {
906 lkp->lk_sharecount--;
907 if (lkp->lk_sharecount == 0)
908 lkp->lk_flags &= ~LK_SHARE_NONZERO;
909 COUNT(lkp, l, cpu_num, -1);
910 }
911 #ifdef DIAGNOSTIC
912 else
913 lockpanic(lkp, "lockmgr: release of unlocked lock!");
914 #endif
915 WAKEUP_WAITER(lkp);
916 break;
917
918 case LK_DRAIN:
919 /*
920 * Check that we do not already hold the lock, as it can
921 * never drain if we do. Unfortunately, we have no way to
922 * check for holding a shared lock, but at least we can
923 * check for an exclusive one.
924 */
925 if (WEHOLDIT(lkp, pid, lid, cpu_num))
926 lockpanic(lkp, "lockmgr: draining against myself");
927 /*
928 * If we are just polling, check to see if we will sleep.
929 */
930 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
931 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
932 LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
933 error = EBUSY;
934 break;
935 }
936 error = acquire(&lkp, &s, extflags, 1,
937 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
938 LK_SHARE_NONZERO | LK_WAIT_NONZERO,
939 RETURN_ADDRESS);
940 if (error)
941 break;
942 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
943 SETHOLDER(lkp, pid, lid, cpu_num);
944 #if defined(LOCKDEBUG)
945 lkp->lk_lock_file = file;
946 lkp->lk_lock_line = line;
947 #endif
948 HAVEIT(lkp);
949 lkp->lk_exclusivecount = 1;
950 /* XXX unlikely that we'd want this */
951 if (extflags & LK_SETRECURSE)
952 lkp->lk_recurselevel = 1;
953 COUNT(lkp, l, cpu_num, 1);
954 break;
955
956 default:
957 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
958 lockpanic(lkp, "lockmgr: unknown locktype request %d",
959 flags & LK_TYPE_MASK);
960 /* NOTREACHED */
961 }
962 if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
963 ((lkp->lk_flags &
964 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
965 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
966 lkp->lk_flags &= ~LK_WAITDRAIN;
967 wakeup(&lkp->lk_flags);
968 }
969 /*
970 * Note that this panic will be a recursive panic, since
971 * we only set lock_shutdown_noblock above if panicstr != NULL.
972 */
973 if (error && lock_shutdown_noblock)
974 lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
975
976 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
977 return (error);
978 }
979
980 /*
981 * For a recursive spinlock held one or more times by the current CPU,
982 * release all N locks, and return N.
983 * Intended for use in mi_switch() shortly before context switching.
984 */
985
986 int
987 #if defined(LOCKDEBUG)
988 _spinlock_release_all(volatile struct lock *lkp, const char *file, int line)
989 #else
990 spinlock_release_all(volatile struct lock *lkp)
991 #endif
992 {
993 int s, count;
994 cpuid_t cpu_num;
995
996 KASSERT(lkp->lk_flags & LK_SPIN);
997
998 INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
999
1000 cpu_num = cpu_number();
1001 count = lkp->lk_exclusivecount;
1002
1003 if (count != 0) {
1004 #ifdef DIAGNOSTIC
1005 if (WEHOLDIT(lkp, 0, 0, cpu_num) == 0) {
1006 lockpanic(lkp, "spinlock_release_all: processor %lu, not "
1007 "exclusive lock holder %lu "
1008 "unlocking", (long)cpu_num, lkp->lk_cpu);
1009 }
1010 #endif
1011 lkp->lk_recurselevel = 0;
1012 lkp->lk_exclusivecount = 0;
1013 COUNT_CPU(cpu_num, -count);
1014 lkp->lk_flags &= ~LK_HAVE_EXCL;
1015 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
1016 #if defined(LOCKDEBUG)
1017 lkp->lk_unlock_file = file;
1018 lkp->lk_unlock_line = line;
1019 #endif
1020 DONTHAVEIT(lkp);
1021 }
1022 #ifdef DIAGNOSTIC
1023 else if (lkp->lk_sharecount != 0)
1024 lockpanic(lkp, "spinlock_release_all: release of shared lock!");
1025 else
1026 lockpanic(lkp, "spinlock_release_all: release of unlocked lock!");
1027 #endif
1028 INTERLOCK_RELEASE(lkp, LK_SPIN, s);
1029
1030 return (count);
1031 }
1032
1033 /*
1034 * For a recursive spinlock held one or more times by the current CPU,
1035 * release all N locks, and return N.
1036 * Intended for use in mi_switch() right after resuming execution.
1037 */
1038
1039 void
1040 #if defined(LOCKDEBUG)
1041 _spinlock_acquire_count(volatile struct lock *lkp, int count,
1042 const char *file, int line)
1043 #else
1044 spinlock_acquire_count(volatile struct lock *lkp, int count)
1045 #endif
1046 {
1047 int s, error;
1048 cpuid_t cpu_num;
1049
1050 KASSERT(lkp->lk_flags & LK_SPIN);
1051
1052 INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
1053
1054 cpu_num = cpu_number();
1055
1056 #ifdef DIAGNOSTIC
1057 if (WEHOLDIT(lkp, LK_NOPROC, 0, cpu_num))
1058 lockpanic(lkp, "spinlock_acquire_count: processor %lu already holds lock", (long)cpu_num);
1059 #endif
1060 /*
1061 * Try to acquire the want_exclusive flag.
1062 */
1063 error = acquire(&lkp, &s, LK_SPIN, 0, LK_HAVE_EXCL | LK_WANT_EXCL,
1064 RETURN_ADDRESS);
1065 lkp->lk_flags |= LK_WANT_EXCL;
1066 /*
1067 * Wait for shared locks and upgrades to finish.
1068 */
1069 error = acquire(&lkp, &s, LK_SPIN, 0,
1070 LK_HAVE_EXCL | LK_SHARE_NONZERO | LK_WANT_UPGRADE,
1071 RETURN_ADDRESS);
1072 lkp->lk_flags &= ~LK_WANT_EXCL;
1073 lkp->lk_flags |= LK_HAVE_EXCL;
1074 SETHOLDER(lkp, LK_NOPROC, 0, cpu_num);
1075 #if defined(LOCKDEBUG)
1076 lkp->lk_lock_file = file;
1077 lkp->lk_lock_line = line;
1078 #endif
1079 HAVEIT(lkp);
1080 if (lkp->lk_exclusivecount != 0)
1081 lockpanic(lkp, "lockmgr: non-zero exclusive count");
1082 lkp->lk_exclusivecount = count;
1083 lkp->lk_recurselevel = 1;
1084 COUNT_CPU(cpu_num, count);
1085
1086 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
1087 }
1088
1089
1090
1091 /*
1092 * Print out information about state of a lock. Used by VOP_PRINT
1093 * routines to display ststus about contained locks.
1094 */
1095 void
1096 lockmgr_printinfo(volatile struct lock *lkp)
1097 {
1098
1099 if (lkp->lk_sharecount)
1100 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
1101 lkp->lk_sharecount);
1102 else if (lkp->lk_flags & LK_HAVE_EXCL) {
1103 printf(" lock type %s: EXCL (count %d) by ",
1104 lkp->lk_wmesg, lkp->lk_exclusivecount);
1105 if (lkp->lk_flags & LK_SPIN)
1106 printf("processor %lu", lkp->lk_cpu);
1107 else
1108 printf("pid %d.%d", lkp->lk_lockholder,
1109 lkp->lk_locklwp);
1110 } else
1111 printf(" not locked");
1112 if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
1113 printf(" with %d pending", lkp->lk_waitcount);
1114 }
1115
1116 #if defined(LOCKDEBUG) /* { */
1117 _TAILQ_HEAD(, struct simplelock, volatile) simplelock_list =
1118 TAILQ_HEAD_INITIALIZER(simplelock_list);
1119
1120 #if defined(MULTIPROCESSOR) /* { */
1121 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
1122
1123 #define SLOCK_LIST_LOCK() \
1124 __cpu_simple_lock(&simplelock_list_slock.lock_data)
1125
1126 #define SLOCK_LIST_UNLOCK() \
1127 __cpu_simple_unlock(&simplelock_list_slock.lock_data)
1128
1129 #define SLOCK_COUNT(x) \
1130 curcpu()->ci_simple_locks += (x)
1131 #else
1132 u_long simple_locks;
1133
1134 #define SLOCK_LIST_LOCK() /* nothing */
1135
1136 #define SLOCK_LIST_UNLOCK() /* nothing */
1137
1138 #define SLOCK_COUNT(x) simple_locks += (x)
1139 #endif /* MULTIPROCESSOR */ /* } */
1140
1141 #ifdef MULTIPROCESSOR
1142 #define SLOCK_MP() lock_printf("on CPU %ld\n", \
1143 (u_long) cpu_number())
1144 #else
1145 #define SLOCK_MP() /* nothing */
1146 #endif
1147
1148 #define SLOCK_WHERE(str, alp, id, l) \
1149 do { \
1150 lock_printf("\n"); \
1151 lock_printf(str); \
1152 lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
1153 SLOCK_MP(); \
1154 if ((alp)->lock_file != NULL) \
1155 lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
1156 (alp)->lock_line); \
1157 if ((alp)->unlock_file != NULL) \
1158 lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
1159 (alp)->unlock_line); \
1160 SLOCK_TRACE() \
1161 SLOCK_DEBUGGER(); \
1162 } while (/*CONSTCOND*/0)
1163
1164 /*
1165 * Simple lock functions so that the debugger can see from whence
1166 * they are being called.
1167 */
1168 void
1169 simple_lock_init(volatile struct simplelock *alp)
1170 {
1171
1172 #if defined(MULTIPROCESSOR) /* { */
1173 __cpu_simple_lock_init(&alp->lock_data);
1174 #else
1175 alp->lock_data = __SIMPLELOCK_UNLOCKED;
1176 #endif /* } */
1177 alp->lock_file = NULL;
1178 alp->lock_line = 0;
1179 alp->unlock_file = NULL;
1180 alp->unlock_line = 0;
1181 alp->lock_holder = LK_NOCPU;
1182 }
1183
1184 void
1185 _simple_lock(volatile struct simplelock *alp, const char *id, int l)
1186 {
1187 cpuid_t cpu_num = cpu_number();
1188 int s;
1189
1190 s = spllock();
1191
1192 /*
1193 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1194 * don't take any action, and just fall into the normal spin case.
1195 */
1196 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1197 #if defined(MULTIPROCESSOR) /* { */
1198 if (alp->lock_holder == cpu_num) {
1199 SLOCK_WHERE("simple_lock: locking against myself\n",
1200 alp, id, l);
1201 goto out;
1202 }
1203 #else
1204 SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
1205 goto out;
1206 #endif /* MULTIPROCESSOR */ /* } */
1207 }
1208
1209 #if defined(MULTIPROCESSOR) /* { */
1210 /* Acquire the lock before modifying any fields. */
1211 splx(s);
1212 __cpu_simple_lock(&alp->lock_data);
1213 s = spllock();
1214 #else
1215 alp->lock_data = __SIMPLELOCK_LOCKED;
1216 #endif /* } */
1217
1218 if (alp->lock_holder != LK_NOCPU) {
1219 SLOCK_WHERE("simple_lock: uninitialized lock\n",
1220 alp, id, l);
1221 }
1222 alp->lock_file = id;
1223 alp->lock_line = l;
1224 alp->lock_holder = cpu_num;
1225
1226 SLOCK_LIST_LOCK();
1227 TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
1228 SLOCK_LIST_UNLOCK();
1229
1230 SLOCK_COUNT(1);
1231
1232 out:
1233 splx(s);
1234 }
1235
1236 int
1237 _simple_lock_held(volatile struct simplelock *alp)
1238 {
1239 #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
1240 cpuid_t cpu_num = cpu_number();
1241 #endif
1242 int s, locked = 0;
1243
1244 s = spllock();
1245
1246 #if defined(MULTIPROCESSOR)
1247 if (__cpu_simple_lock_try(&alp->lock_data) == 0)
1248 locked = (alp->lock_holder == cpu_num);
1249 else
1250 __cpu_simple_unlock(&alp->lock_data);
1251 #else
1252 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1253 locked = 1;
1254 KASSERT(alp->lock_holder == cpu_num);
1255 }
1256 #endif
1257
1258 splx(s);
1259
1260 return (locked);
1261 }
1262
1263 int
1264 _simple_lock_try(volatile struct simplelock *alp, const char *id, int l)
1265 {
1266 cpuid_t cpu_num = cpu_number();
1267 int s, rv = 0;
1268
1269 s = spllock();
1270
1271 /*
1272 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1273 * don't take any action.
1274 */
1275 #if defined(MULTIPROCESSOR) /* { */
1276 if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
1277 if (alp->lock_holder == cpu_num)
1278 SLOCK_WHERE("simple_lock_try: locking against myself\n",
1279 alp, id, l);
1280 goto out;
1281 }
1282 #else
1283 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1284 SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
1285 goto out;
1286 }
1287 alp->lock_data = __SIMPLELOCK_LOCKED;
1288 #endif /* MULTIPROCESSOR */ /* } */
1289
1290 /*
1291 * At this point, we have acquired the lock.
1292 */
1293
1294 rv = 1;
1295
1296 alp->lock_file = id;
1297 alp->lock_line = l;
1298 alp->lock_holder = cpu_num;
1299
1300 SLOCK_LIST_LOCK();
1301 TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
1302 SLOCK_LIST_UNLOCK();
1303
1304 SLOCK_COUNT(1);
1305
1306 out:
1307 splx(s);
1308 return (rv);
1309 }
1310
1311 void
1312 _simple_unlock(volatile struct simplelock *alp, const char *id, int l)
1313 {
1314 int s;
1315
1316 s = spllock();
1317
1318 /*
1319 * MULTIPROCESSOR case: This is `safe' because we think we hold
1320 * the lock, and if we don't, we don't take any action.
1321 */
1322 if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
1323 SLOCK_WHERE("simple_unlock: lock not held\n",
1324 alp, id, l);
1325 goto out;
1326 }
1327
1328 SLOCK_LIST_LOCK();
1329 TAILQ_REMOVE(&simplelock_list, alp, list);
1330 SLOCK_LIST_UNLOCK();
1331
1332 SLOCK_COUNT(-1);
1333
1334 alp->list.tqe_next = NULL; /* sanity */
1335 alp->list.tqe_prev = NULL; /* sanity */
1336
1337 alp->unlock_file = id;
1338 alp->unlock_line = l;
1339
1340 #if defined(MULTIPROCESSOR) /* { */
1341 alp->lock_holder = LK_NOCPU;
1342 /* Now that we've modified all fields, release the lock. */
1343 __cpu_simple_unlock(&alp->lock_data);
1344 #else
1345 alp->lock_data = __SIMPLELOCK_UNLOCKED;
1346 KASSERT(alp->lock_holder == cpu_number());
1347 alp->lock_holder = LK_NOCPU;
1348 #endif /* } */
1349
1350 out:
1351 splx(s);
1352 }
1353
1354 void
1355 simple_lock_dump(void)
1356 {
1357 volatile struct simplelock *alp;
1358 int s;
1359
1360 s = spllock();
1361 SLOCK_LIST_LOCK();
1362 lock_printf("all simple locks:\n");
1363 TAILQ_FOREACH(alp, &simplelock_list, list) {
1364 lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
1365 alp->lock_file, alp->lock_line);
1366 }
1367 SLOCK_LIST_UNLOCK();
1368 splx(s);
1369 }
1370
1371 void
1372 simple_lock_freecheck(void *start, void *end)
1373 {
1374 volatile struct simplelock *alp;
1375 int s;
1376
1377 s = spllock();
1378 SLOCK_LIST_LOCK();
1379 TAILQ_FOREACH(alp, &simplelock_list, list) {
1380 if ((volatile void *)alp >= start &&
1381 (volatile void *)alp < end) {
1382 lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
1383 alp, alp->lock_holder, alp->lock_file,
1384 alp->lock_line);
1385 SLOCK_DEBUGGER();
1386 }
1387 }
1388 SLOCK_LIST_UNLOCK();
1389 splx(s);
1390 }
1391
1392 /*
1393 * We must be holding exactly one lock: the sched_lock.
1394 */
1395
1396 void
1397 simple_lock_switchcheck(void)
1398 {
1399
1400 simple_lock_only_held(NULL, "switching");
1401 }
1402
1403 /*
1404 * Drop into the debugger if lp isn't the only lock held.
1405 * lp may be NULL.
1406 */
1407 void
1408 simple_lock_only_held(volatile struct simplelock *lp, const char *where)
1409 {
1410 volatile struct simplelock *alp;
1411 cpuid_t cpu_num = cpu_number();
1412 int s;
1413
1414 if (lp) {
1415 LOCK_ASSERT(simple_lock_held(lp));
1416 }
1417 s = spllock();
1418 SLOCK_LIST_LOCK();
1419 TAILQ_FOREACH(alp, &simplelock_list, list) {
1420 if (alp == lp)
1421 continue;
1422 if (alp->lock_holder == cpu_num)
1423 break;
1424 }
1425 SLOCK_LIST_UNLOCK();
1426 splx(s);
1427
1428 if (alp != NULL) {
1429 lock_printf("\n%s with held simple_lock %p "
1430 "CPU %lu %s:%d\n",
1431 where, alp, alp->lock_holder, alp->lock_file,
1432 alp->lock_line);
1433 SLOCK_TRACE();
1434 SLOCK_DEBUGGER();
1435 }
1436 }
1437
1438 /*
1439 * Set to 1 by simple_lock_assert_*().
1440 * Can be cleared from ddb to avoid a panic.
1441 */
1442 int slock_assert_will_panic;
1443
1444 /*
1445 * If the lock isn't held, print a traceback, optionally drop into the
1446 * debugger, then panic.
1447 * The panic can be avoided by clearing slock_assert_with_panic from the
1448 * debugger.
1449 */
1450 void
1451 _simple_lock_assert_locked(volatile struct simplelock *alp,
1452 const char *lockname, const char *id, int l)
1453 {
1454 if (simple_lock_held(alp) == 0) {
1455 slock_assert_will_panic = 1;
1456 lock_printf("%s lock not held\n", lockname);
1457 SLOCK_WHERE("lock not held", alp, id, l);
1458 if (slock_assert_will_panic)
1459 panic("%s: not locked", lockname);
1460 }
1461 }
1462
1463 void
1464 _simple_lock_assert_unlocked(volatile struct simplelock *alp,
1465 const char *lockname, const char *id, int l)
1466 {
1467 if (simple_lock_held(alp)) {
1468 slock_assert_will_panic = 1;
1469 lock_printf("%s lock held\n", lockname);
1470 SLOCK_WHERE("lock held", alp, id, l);
1471 if (slock_assert_will_panic)
1472 panic("%s: locked", lockname);
1473 }
1474 }
1475
1476 void
1477 assert_sleepable(struct simplelock *interlock, const char *msg)
1478 {
1479
1480 if (curlwp == NULL) {
1481 panic("assert_sleepable: NULL curlwp");
1482 }
1483 simple_lock_only_held(interlock, msg);
1484 }
1485
1486 #endif /* LOCKDEBUG */ /* } */
1487
1488 #if defined(MULTIPROCESSOR)
1489
1490 /*
1491 * Functions for manipulating the kernel_lock. We put them here
1492 * so that they show up in profiles.
1493 */
1494
1495 #define _KERNEL_LOCK_ABORT(msg) \
1496 LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops, \
1497 __FUNCTION__, msg)
1498
1499 #ifdef LOCKDEBUG
1500 #define _KERNEL_LOCK_ASSERT(cond) \
1501 do { \
1502 if (!(cond)) \
1503 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
1504 } while (/* CONSTCOND */ 0)
1505 #else
1506 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
1507 #endif
1508
1509 void _kernel_lock_dump(volatile void *);
1510
1511 lockops_t _kernel_lock_ops = {
1512 "Kernel lock",
1513 0,
1514 _kernel_lock_dump
1515 };
1516
1517 /*
1518 * Initialize the kernel lock.
1519 */
1520 void
1521 _kernel_lock_init(void)
1522 {
1523
1524 __cpu_simple_lock_init(&kernel_lock);
1525 kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
1526 }
1527
1528 /*
1529 * Print debugging information about the kernel lock.
1530 */
1531 void
1532 _kernel_lock_dump(volatile void *junk)
1533 {
1534 struct cpu_info *ci = curcpu();
1535
1536 (void)junk;
1537
1538 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
1539 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
1540 }
1541
1542 /*
1543 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
1544 * acquisition is from process context.
1545 */
1546 void
1547 _kernel_lock(int nlocks, struct lwp *l)
1548 {
1549 struct cpu_info *ci = curcpu();
1550 LOCKSTAT_TIMER(spintime);
1551 LOCKSTAT_FLAG(lsflag);
1552 struct lwp *owant;
1553 #ifdef LOCKDEBUG
1554 u_int spins;
1555 #endif
1556 int s;
1557
1558 (void)l;
1559
1560 if (nlocks == 0)
1561 return;
1562 _KERNEL_LOCK_ASSERT(nlocks > 0);
1563
1564 s = splbiglock();
1565
1566 if (ci->ci_biglock_count != 0) {
1567 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
1568 ci->ci_biglock_count += nlocks;
1569 splx(s);
1570 return;
1571 }
1572
1573 LOCKDEBUG_WANTLOCK(kernel_lock_id,
1574 (uintptr_t)__builtin_return_address(0), 0);
1575
1576 if (__cpu_simple_lock_try(&kernel_lock)) {
1577 ci->ci_biglock_count = nlocks;
1578 LOCKDEBUG_LOCKED(kernel_lock_id,
1579 (uintptr_t)__builtin_return_address(0), 0);
1580 splx(s);
1581 return;
1582 }
1583
1584 LOCKSTAT_ENTER(lsflag);
1585 LOCKSTAT_START_TIMER(lsflag, spintime);
1586
1587 /*
1588 * Before setting ci_biglock_wanted we must post a store
1589 * fence (see kern_mutex.c). This is accomplished by the
1590 * __cpu_simple_lock_try() above.
1591 */
1592 owant = ci->ci_biglock_wanted;
1593 ci->ci_biglock_wanted = curlwp; /* XXXAD */
1594
1595 #ifdef LOCKDEBUG
1596 spins = 0;
1597 #endif
1598
1599 do {
1600 while (kernel_lock == __SIMPLELOCK_LOCKED) {
1601 #ifdef LOCKDEBUG
1602 if (SPINLOCK_SPINOUT(spins))
1603 _KERNEL_LOCK_ABORT("spinout");
1604 #endif
1605 splx(s);
1606 SPINLOCK_SPIN_HOOK;
1607 (void)splbiglock();
1608 }
1609 } while (!__cpu_simple_lock_try(&kernel_lock));
1610
1611 ci->ci_biglock_wanted = owant;
1612 ci->ci_biglock_count += nlocks;
1613 LOCKSTAT_STOP_TIMER(lsflag, spintime);
1614 LOCKDEBUG_LOCKED(kernel_lock_id,
1615 (uintptr_t)__builtin_return_address(0), 0);
1616 splx(s);
1617
1618 /*
1619 * Again, another store fence is required (see kern_mutex.c).
1620 */
1621 mb_write();
1622 if (owant == NULL) {
1623 LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
1624 1, spintime);
1625 }
1626 LOCKSTAT_EXIT(lsflag);
1627 }
1628
1629 /*
1630 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
1631 * all holds. If 'l' is non-null, the release is from process context.
1632 */
1633 void
1634 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
1635 {
1636 struct cpu_info *ci = curcpu();
1637 u_int olocks;
1638 int s;
1639
1640 (void)l;
1641
1642 _KERNEL_LOCK_ASSERT(nlocks < 2);
1643
1644 olocks = ci->ci_biglock_count;
1645
1646 if (olocks == 0) {
1647 _KERNEL_LOCK_ASSERT(nlocks <= 0);
1648 if (countp != NULL)
1649 *countp = 0;
1650 return;
1651 }
1652
1653 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
1654
1655 if (nlocks == 0)
1656 nlocks = olocks;
1657 else if (nlocks == -1) {
1658 nlocks = 1;
1659 _KERNEL_LOCK_ASSERT(olocks == 1);
1660 }
1661
1662 s = splbiglock();
1663 if ((ci->ci_biglock_count -= nlocks) == 0) {
1664 LOCKDEBUG_UNLOCKED(kernel_lock_id,
1665 (uintptr_t)__builtin_return_address(0), 0);
1666 __cpu_simple_unlock(&kernel_lock);
1667 }
1668 splx(s);
1669
1670 if (countp != NULL)
1671 *countp = olocks;
1672 }
1673
1674 #if defined(DEBUG)
1675 /*
1676 * Assert that the kernel lock is held.
1677 */
1678 void
1679 _kernel_lock_assert_locked(void)
1680 {
1681
1682 if (kernel_lock != __SIMPLELOCK_LOCKED ||
1683 curcpu()->ci_biglock_count == 0)
1684 _KERNEL_LOCK_ABORT("not locked");
1685 }
1686
1687 void
1688 _kernel_lock_assert_unlocked()
1689 {
1690
1691 if (curcpu()->ci_biglock_count != 0)
1692 _KERNEL_LOCK_ABORT("locked");
1693 }
1694 #endif
1695
1696 #endif /* MULTIPROCESSOR || LOCKDEBUG */
1697