kern_lock.c revision 1.51.2.1 1 /* $NetBSD: kern_lock.c,v 1.51.2.1 2001/03/05 22:49:40 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 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.
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. All advertising materials mentioning features or use of this software
60 * must display the following acknowledgement:
61 * This product includes software developed by the University of
62 * California, Berkeley and its contributors.
63 * 4. Neither the name of the University nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * SUCH DAMAGE.
78 *
79 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
80 */
81
82 #include "opt_multiprocessor.h"
83 #include "opt_lockdebug.h"
84 #include "opt_ddb.h"
85
86 #include <sys/param.h>
87 #include <sys/lwp.h>
88 #include <sys/proc.h>
89 #include <sys/lock.h>
90 #include <sys/systm.h>
91 #include <machine/cpu.h>
92
93 #if defined(LOCKDEBUG)
94 #include <sys/syslog.h>
95 /*
96 * note that stdarg.h and the ansi style va_start macro is used for both
97 * ansi and traditional c compiles.
98 * XXX: this requires that stdarg.h define: va_alist and va_dcl
99 */
100 #include <machine/stdarg.h>
101
102 void lock_printf(const char *fmt, ...)
103 __attribute__((__format__(__printf__,1,2)));
104
105 int lock_debug_syslog = 1; /* defaults to syslog, but can be patched */
106 #endif
107
108 /*
109 * Locking primitives implementation.
110 * Locks provide shared/exclusive sychronization.
111 */
112
113 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
114 #if defined(MULTIPROCESSOR) /* { */
115 #define COUNT_CPU(cpu_id, x) \
116 curcpu()->ci_spin_locks += (x)
117 #else
118 u_long spin_locks;
119 #define COUNT_CPU(cpu_id, x) spin_locks += (x)
120 #endif /* MULTIPROCESSOR */ /* } */
121
122 #define COUNT(lkp, p, cpu_id, x) \
123 do { \
124 if ((lkp)->lk_flags & LK_SPIN) \
125 COUNT_CPU((cpu_id), (x)); \
126 else \
127 (p)->p_locks += (x); \
128 } while (/*CONSTCOND*/0)
129 #else
130 #define COUNT(lkp, p, cpu_id, x)
131 #define COUNT_CPU(cpu_id, x)
132 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
133
134 #ifndef SPINLOCK_INTERLOCK_RELEASE_HOOK /* from <machine/lock.h> */
135 #define SPINLOCK_INTERLOCK_RELEASE_HOOK /* nothing */
136 #endif
137
138 #define INTERLOCK_ACQUIRE(lkp, flags, s) \
139 do { \
140 if ((flags) & LK_SPIN) \
141 s = splsched(); \
142 simple_lock(&(lkp)->lk_interlock); \
143 } while (0)
144
145 #define INTERLOCK_RELEASE(lkp, flags, s) \
146 do { \
147 simple_unlock(&(lkp)->lk_interlock); \
148 if ((flags) & LK_SPIN) { \
149 splx(s); \
150 SPINLOCK_INTERLOCK_RELEASE_HOOK; \
151 } \
152 } while (0)
153
154 #if defined(LOCKDEBUG)
155 #if defined(DDB)
156 #define SPINLOCK_SPINCHECK_DEBUGGER Debugger()
157 #else
158 #define SPINLOCK_SPINCHECK_DEBUGGER /* nothing */
159 #endif
160
161 #define SPINLOCK_SPINCHECK_DECL \
162 /* 32-bits of count -- wrap constitutes a "spinout" */ \
163 uint32_t __spinc = 0
164
165 #define SPINLOCK_SPINCHECK \
166 do { \
167 if (++__spinc == 0) { \
168 printf("LK_SPIN spinout, excl %d, share %d\n", \
169 lkp->lk_exclusivecount, lkp->lk_sharecount); \
170 if (lkp->lk_exclusivecount) \
171 printf("held by CPU %lu\n", \
172 (u_long) lkp->lk_cpu); \
173 if (lkp->lk_lock_file) \
174 printf("last locked at %s:%d\n", \
175 lkp->lk_lock_file, lkp->lk_lock_line); \
176 if (lkp->lk_unlock_file) \
177 printf("last unlocked at %s:%d\n", \
178 lkp->lk_unlock_file, lkp->lk_unlock_line); \
179 SPINLOCK_SPINCHECK_DEBUGGER; \
180 } \
181 } while (0)
182 #else
183 #define SPINLOCK_SPINCHECK_DECL /* nothing */
184 #define SPINLOCK_SPINCHECK /* nothing */
185 #endif /* LOCKDEBUG && DDB */
186
187 /*
188 * Acquire a resource.
189 */
190 #define ACQUIRE(lkp, error, extflags, drain, wanted) \
191 if ((extflags) & LK_SPIN) { \
192 int interlocked; \
193 SPINLOCK_SPINCHECK_DECL; \
194 \
195 if ((drain) == 0) \
196 (lkp)->lk_waitcount++; \
197 for (interlocked = 1;;) { \
198 SPINLOCK_SPINCHECK; \
199 if (wanted) { \
200 if (interlocked) { \
201 INTERLOCK_RELEASE((lkp), \
202 LK_SPIN, s); \
203 interlocked = 0; \
204 } \
205 } else if (interlocked) { \
206 break; \
207 } else { \
208 INTERLOCK_ACQUIRE((lkp), LK_SPIN, s); \
209 interlocked = 1; \
210 } \
211 } \
212 if ((drain) == 0) \
213 (lkp)->lk_waitcount--; \
214 KASSERT((wanted) == 0); \
215 error = 0; /* sanity */ \
216 } else { \
217 for (error = 0; wanted; ) { \
218 if ((drain)) \
219 (lkp)->lk_flags |= LK_WAITDRAIN; \
220 else \
221 (lkp)->lk_waitcount++; \
222 /* XXX Cast away volatile. */ \
223 error = ltsleep((drain) ? &(lkp)->lk_flags : \
224 (void *)(lkp), (lkp)->lk_prio, \
225 (lkp)->lk_wmesg, (lkp)->lk_timo, \
226 &(lkp)->lk_interlock); \
227 if ((drain) == 0) \
228 (lkp)->lk_waitcount--; \
229 if (error) \
230 break; \
231 if ((extflags) & LK_SLEEPFAIL) { \
232 error = ENOLCK; \
233 break; \
234 } \
235 } \
236 }
237
238 #define SETHOLDER(lkp, pid, cpu_id) \
239 do { \
240 if ((lkp)->lk_flags & LK_SPIN) \
241 (lkp)->lk_cpu = cpu_id; \
242 else \
243 (lkp)->lk_lockholder = pid; \
244 } while (/*CONSTCOND*/0)
245
246 #define WEHOLDIT(lkp, pid, cpu_id) \
247 (((lkp)->lk_flags & LK_SPIN) != 0 ? \
248 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
249
250 #define WAKEUP_WAITER(lkp) \
251 do { \
252 if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) { \
253 /* XXX Cast away volatile. */ \
254 wakeup_one((void *)(lkp)); \
255 } \
256 } while (/*CONSTCOND*/0)
257
258 #if defined(LOCKDEBUG) /* { */
259 #if defined(MULTIPROCESSOR) /* { */
260 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
261
262 #define SPINLOCK_LIST_LOCK() \
263 __cpu_simple_lock(&spinlock_list_slock.lock_data)
264
265 #define SPINLOCK_LIST_UNLOCK() \
266 __cpu_simple_unlock(&spinlock_list_slock.lock_data)
267 #else
268 #define SPINLOCK_LIST_LOCK() /* nothing */
269
270 #define SPINLOCK_LIST_UNLOCK() /* nothing */
271 #endif /* MULTIPROCESSOR */ /* } */
272
273 TAILQ_HEAD(, lock) spinlock_list =
274 TAILQ_HEAD_INITIALIZER(spinlock_list);
275
276 #define HAVEIT(lkp) \
277 do { \
278 if ((lkp)->lk_flags & LK_SPIN) { \
279 int s = spllock(); \
280 SPINLOCK_LIST_LOCK(); \
281 /* XXX Cast away volatile. */ \
282 TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp), \
283 lk_list); \
284 SPINLOCK_LIST_UNLOCK(); \
285 splx(s); \
286 } \
287 } while (/*CONSTCOND*/0)
288
289 #define DONTHAVEIT(lkp) \
290 do { \
291 if ((lkp)->lk_flags & LK_SPIN) { \
292 int s = spllock(); \
293 SPINLOCK_LIST_LOCK(); \
294 /* XXX Cast away volatile. */ \
295 TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp), \
296 lk_list); \
297 SPINLOCK_LIST_UNLOCK(); \
298 splx(s); \
299 } \
300 } while (/*CONSTCOND*/0)
301 #else
302 #define HAVEIT(lkp) /* nothing */
303
304 #define DONTHAVEIT(lkp) /* nothing */
305 #endif /* LOCKDEBUG */ /* } */
306
307 #if defined(LOCKDEBUG)
308 /*
309 * Lock debug printing routine; can be configured to print to console
310 * or log to syslog.
311 */
312 void
313 lock_printf(const char *fmt, ...)
314 {
315 va_list ap;
316
317 va_start(ap, fmt);
318 if (lock_debug_syslog)
319 vlog(LOG_DEBUG, fmt, ap);
320 else
321 vprintf(fmt, ap);
322 va_end(ap);
323 }
324 #endif /* LOCKDEBUG */
325
326 /*
327 * Initialize a lock; required before use.
328 */
329 void
330 lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
331 {
332
333 memset(lkp, 0, sizeof(struct lock));
334 simple_lock_init(&lkp->lk_interlock);
335 lkp->lk_flags = flags & LK_EXTFLG_MASK;
336 if (flags & LK_SPIN)
337 lkp->lk_cpu = LK_NOCPU;
338 else {
339 lkp->lk_lockholder = LK_NOPROC;
340 lkp->lk_prio = prio;
341 lkp->lk_timo = timo;
342 }
343 lkp->lk_wmesg = wmesg; /* just a name for spin locks */
344 #if defined(LOCKDEBUG)
345 lkp->lk_lock_file = NULL;
346 lkp->lk_unlock_file = NULL;
347 #endif
348 }
349
350 /*
351 * Determine the status of a lock.
352 */
353 int
354 lockstatus(struct lock *lkp)
355 {
356 int s, lock_type = 0;
357
358 INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
359 if (lkp->lk_exclusivecount != 0)
360 lock_type = LK_EXCLUSIVE;
361 else if (lkp->lk_sharecount != 0)
362 lock_type = LK_SHARED;
363 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
364 return (lock_type);
365 }
366
367 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
368 /*
369 * Make sure no spin locks are held by a CPU that is about
370 * to context switch.
371 */
372 void
373 spinlock_switchcheck(void)
374 {
375 u_long cnt;
376 int s;
377
378 s = spllock();
379 #if defined(MULTIPROCESSOR)
380 cnt = curcpu()->ci_spin_locks;
381 #else
382 cnt = spin_locks;
383 #endif
384 splx(s);
385
386 if (cnt != 0)
387 panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
388 (u_long) cpu_number(), cnt);
389 }
390 #endif /* LOCKDEBUG || DIAGNOSTIC */
391
392 /*
393 * Locks and IPLs (interrupt priority levels):
394 *
395 * Locks which may be taken from interrupt context must be handled
396 * very carefully; you must spl to the highest IPL where the lock
397 * is needed before acquiring the lock.
398 *
399 * It is also important to avoid deadlock, since certain (very high
400 * priority) interrupts are often needed to keep the system as a whole
401 * from deadlocking, and must not be blocked while you are spinning
402 * waiting for a lower-priority lock.
403 *
404 * In addition, the lock-debugging hooks themselves need to use locks!
405 *
406 * A raw __cpu_simple_lock may be used from interrupts are long as it
407 * is acquired and held at a single IPL.
408 *
409 * A simple_lock (which is a __cpu_simple_lock wrapped with some
410 * debugging hooks) may be used at or below spllock(), which is
411 * typically at or just below splhigh() (i.e. blocks everything
412 * but certain machine-dependent extremely high priority interrupts).
413 *
414 * spinlockmgr spinlocks should be used at or below splsched().
415 *
416 * Some platforms may have interrupts of higher priority than splsched(),
417 * including hard serial interrupts, inter-processor interrupts, and
418 * kernel debugger traps.
419 */
420
421 /*
422 * XXX XXX kludge around another kludge..
423 *
424 * vfs_shutdown() may be called from interrupt context, either as a result
425 * of a panic, or from the debugger. It proceeds to call
426 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
427 *
428 * We would like to make an attempt to sync the filesystems in this case, so
429 * if this happens, we treat attempts to acquire locks specially.
430 * All locks are acquired on behalf of proc0.
431 *
432 * If we've already paniced, we don't block waiting for locks, but
433 * just barge right ahead since we're already going down in flames.
434 */
435
436 /*
437 * Set, change, or release a lock.
438 *
439 * Shared requests increment the shared count. Exclusive requests set the
440 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
441 * accepted shared locks and shared-to-exclusive upgrades to go away.
442 */
443 int
444 #if defined(LOCKDEBUG)
445 _lockmgr(__volatile struct lock *lkp, u_int flags,
446 struct simplelock *interlkp, const char *file, int line)
447 #else
448 lockmgr(__volatile struct lock *lkp, u_int flags,
449 struct simplelock *interlkp)
450 #endif
451 {
452 int error;
453 pid_t pid;
454 int extflags;
455 cpuid_t cpu_id;
456 struct lwp *l = curproc;
457 struct proc *p = (l == NULL) ? NULL : l->l_proc;
458 int lock_shutdown_noblock = 0;
459 int s;
460
461 error = 0;
462
463 INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
464 if (flags & LK_INTERLOCK)
465 simple_unlock(interlkp);
466 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
467
468 #ifdef DIAGNOSTIC /* { */
469 /*
470 * Don't allow spins on sleep locks and don't allow sleeps
471 * on spin locks.
472 */
473 if ((flags ^ lkp->lk_flags) & LK_SPIN)
474 panic("lockmgr: sleep/spin mismatch\n");
475 #endif /* } */
476
477 if (extflags & LK_SPIN)
478 pid = LK_KERNPROC;
479 else {
480 if (p == NULL) {
481 if (!doing_shutdown) {
482 #ifdef DIAGNOSTIC
483 panic("lockmgr: no context");
484 #endif
485 } else {
486 p = &proc0;
487 if (panicstr && (!(flags & LK_NOWAIT))) {
488 flags |= LK_NOWAIT;
489 lock_shutdown_noblock = 1;
490 }
491 }
492 }
493 pid = p->p_pid;
494 }
495 cpu_id = cpu_number();
496
497 /*
498 * Once a lock has drained, the LK_DRAINING flag is set and an
499 * exclusive lock is returned. The only valid operation thereafter
500 * is a single release of that exclusive lock. This final release
501 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
502 * further requests of any sort will result in a panic. The bits
503 * selected for these two flags are chosen so that they will be set
504 * in memory that is freed (freed memory is filled with 0xdeadbeef).
505 * The final release is permitted to give a new lease on life to
506 * the lock by specifying LK_REENABLE.
507 */
508 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
509 #ifdef DIAGNOSTIC /* { */
510 if (lkp->lk_flags & LK_DRAINED)
511 panic("lockmgr: using decommissioned lock");
512 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
513 WEHOLDIT(lkp, pid, cpu_id) == 0)
514 panic("lockmgr: non-release on draining lock: %d\n",
515 flags & LK_TYPE_MASK);
516 #endif /* DIAGNOSTIC */ /* } */
517 lkp->lk_flags &= ~LK_DRAINING;
518 if ((flags & LK_REENABLE) == 0)
519 lkp->lk_flags |= LK_DRAINED;
520 }
521
522 switch (flags & LK_TYPE_MASK) {
523
524 case LK_SHARED:
525 if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
526 /*
527 * If just polling, check to see if we will block.
528 */
529 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
530 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
531 error = EBUSY;
532 break;
533 }
534 /*
535 * Wait for exclusive locks and upgrades to clear.
536 */
537 ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
538 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
539 if (error)
540 break;
541 lkp->lk_sharecount++;
542 COUNT(lkp, p, cpu_id, 1);
543 break;
544 }
545 /*
546 * We hold an exclusive lock, so downgrade it to shared.
547 * An alternative would be to fail with EDEADLK.
548 */
549 lkp->lk_sharecount++;
550 COUNT(lkp, p, cpu_id, 1);
551 /* fall into downgrade */
552
553 case LK_DOWNGRADE:
554 if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
555 lkp->lk_exclusivecount == 0)
556 panic("lockmgr: not holding exclusive lock");
557 lkp->lk_sharecount += lkp->lk_exclusivecount;
558 lkp->lk_exclusivecount = 0;
559 lkp->lk_recurselevel = 0;
560 lkp->lk_flags &= ~LK_HAVE_EXCL;
561 SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
562 #if defined(LOCKDEBUG)
563 lkp->lk_unlock_file = file;
564 lkp->lk_unlock_line = line;
565 #endif
566 DONTHAVEIT(lkp);
567 WAKEUP_WAITER(lkp);
568 break;
569
570 case LK_EXCLUPGRADE:
571 /*
572 * If another process is ahead of us to get an upgrade,
573 * then we want to fail rather than have an intervening
574 * exclusive access.
575 */
576 if (lkp->lk_flags & LK_WANT_UPGRADE) {
577 lkp->lk_sharecount--;
578 COUNT(lkp, p, cpu_id, -1);
579 error = EBUSY;
580 break;
581 }
582 /* fall into normal upgrade */
583
584 case LK_UPGRADE:
585 /*
586 * Upgrade a shared lock to an exclusive one. If another
587 * shared lock has already requested an upgrade to an
588 * exclusive lock, our shared lock is released and an
589 * exclusive lock is requested (which will be granted
590 * after the upgrade). If we return an error, the file
591 * will always be unlocked.
592 */
593 if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
594 panic("lockmgr: upgrade exclusive lock");
595 lkp->lk_sharecount--;
596 COUNT(lkp, p, cpu_id, -1);
597 /*
598 * If we are just polling, check to see if we will block.
599 */
600 if ((extflags & LK_NOWAIT) &&
601 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
602 lkp->lk_sharecount > 1)) {
603 error = EBUSY;
604 break;
605 }
606 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
607 /*
608 * We are first shared lock to request an upgrade, so
609 * request upgrade and wait for the shared count to
610 * drop to zero, then take exclusive lock.
611 */
612 lkp->lk_flags |= LK_WANT_UPGRADE;
613 ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
614 lkp->lk_flags &= ~LK_WANT_UPGRADE;
615 if (error)
616 break;
617 lkp->lk_flags |= LK_HAVE_EXCL;
618 SETHOLDER(lkp, pid, cpu_id);
619 #if defined(LOCKDEBUG)
620 lkp->lk_lock_file = file;
621 lkp->lk_lock_line = line;
622 #endif
623 HAVEIT(lkp);
624 if (lkp->lk_exclusivecount != 0)
625 panic("lockmgr: non-zero exclusive count");
626 lkp->lk_exclusivecount = 1;
627 if (extflags & LK_SETRECURSE)
628 lkp->lk_recurselevel = 1;
629 COUNT(lkp, p, cpu_id, 1);
630 break;
631 }
632 /*
633 * Someone else has requested upgrade. Release our shared
634 * lock, awaken upgrade requestor if we are the last shared
635 * lock, then request an exclusive lock.
636 */
637 if (lkp->lk_sharecount == 0)
638 WAKEUP_WAITER(lkp);
639 /* fall into exclusive request */
640
641 case LK_EXCLUSIVE:
642 if (WEHOLDIT(lkp, pid, cpu_id)) {
643 /*
644 * Recursive lock.
645 */
646 if ((extflags & LK_CANRECURSE) == 0 &&
647 lkp->lk_recurselevel == 0) {
648 if (extflags & LK_RECURSEFAIL) {
649 error = EDEADLK;
650 break;
651 } else
652 panic("lockmgr: locking against myself");
653 }
654 lkp->lk_exclusivecount++;
655 if (extflags & LK_SETRECURSE &&
656 lkp->lk_recurselevel == 0)
657 lkp->lk_recurselevel = lkp->lk_exclusivecount;
658 COUNT(lkp, p, cpu_id, 1);
659 break;
660 }
661 /*
662 * If we are just polling, check to see if we will sleep.
663 */
664 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
665 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
666 lkp->lk_sharecount != 0)) {
667 error = EBUSY;
668 break;
669 }
670 /*
671 * Try to acquire the want_exclusive flag.
672 */
673 ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
674 (LK_HAVE_EXCL | LK_WANT_EXCL));
675 if (error)
676 break;
677 lkp->lk_flags |= LK_WANT_EXCL;
678 /*
679 * Wait for shared locks and upgrades to finish.
680 */
681 ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
682 (lkp->lk_flags & LK_WANT_UPGRADE));
683 lkp->lk_flags &= ~LK_WANT_EXCL;
684 if (error)
685 break;
686 lkp->lk_flags |= LK_HAVE_EXCL;
687 SETHOLDER(lkp, pid, cpu_id);
688 #if defined(LOCKDEBUG)
689 lkp->lk_lock_file = file;
690 lkp->lk_lock_line = line;
691 #endif
692 HAVEIT(lkp);
693 if (lkp->lk_exclusivecount != 0)
694 panic("lockmgr: non-zero exclusive count");
695 lkp->lk_exclusivecount = 1;
696 if (extflags & LK_SETRECURSE)
697 lkp->lk_recurselevel = 1;
698 COUNT(lkp, p, cpu_id, 1);
699 break;
700
701 case LK_RELEASE:
702 if (lkp->lk_exclusivecount != 0) {
703 if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
704 if (lkp->lk_flags & LK_SPIN) {
705 panic("lockmgr: processor %lu, not "
706 "exclusive lock holder %lu "
707 "unlocking", cpu_id, lkp->lk_cpu);
708 } else {
709 panic("lockmgr: pid %d, not "
710 "exclusive lock holder %d "
711 "unlocking", pid,
712 lkp->lk_lockholder);
713 }
714 }
715 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
716 lkp->lk_recurselevel = 0;
717 lkp->lk_exclusivecount--;
718 COUNT(lkp, p, cpu_id, -1);
719 if (lkp->lk_exclusivecount == 0) {
720 lkp->lk_flags &= ~LK_HAVE_EXCL;
721 SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
722 #if defined(LOCKDEBUG)
723 lkp->lk_unlock_file = file;
724 lkp->lk_unlock_line = line;
725 #endif
726 DONTHAVEIT(lkp);
727 }
728 } else if (lkp->lk_sharecount != 0) {
729 lkp->lk_sharecount--;
730 COUNT(lkp, p, cpu_id, -1);
731 }
732 #ifdef DIAGNOSTIC
733 else
734 panic("lockmgr: release of unlocked lock!");
735 #endif
736 WAKEUP_WAITER(lkp);
737 break;
738
739 case LK_DRAIN:
740 /*
741 * Check that we do not already hold the lock, as it can
742 * never drain if we do. Unfortunately, we have no way to
743 * check for holding a shared lock, but at least we can
744 * check for an exclusive one.
745 */
746 if (WEHOLDIT(lkp, pid, cpu_id))
747 panic("lockmgr: draining against myself");
748 /*
749 * If we are just polling, check to see if we will sleep.
750 */
751 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
752 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
753 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
754 error = EBUSY;
755 break;
756 }
757 ACQUIRE(lkp, error, extflags, 1,
758 ((lkp->lk_flags &
759 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
760 lkp->lk_sharecount != 0 ||
761 lkp->lk_waitcount != 0));
762 if (error)
763 break;
764 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
765 SETHOLDER(lkp, pid, cpu_id);
766 #if defined(LOCKDEBUG)
767 lkp->lk_lock_file = file;
768 lkp->lk_lock_line = line;
769 #endif
770 HAVEIT(lkp);
771 lkp->lk_exclusivecount = 1;
772 /* XXX unlikely that we'd want this */
773 if (extflags & LK_SETRECURSE)
774 lkp->lk_recurselevel = 1;
775 COUNT(lkp, p, cpu_id, 1);
776 break;
777
778 default:
779 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
780 panic("lockmgr: unknown locktype request %d",
781 flags & LK_TYPE_MASK);
782 /* NOTREACHED */
783 }
784 if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
785 ((lkp->lk_flags &
786 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
787 lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
788 lkp->lk_flags &= ~LK_WAITDRAIN;
789 wakeup_one((void *)&lkp->lk_flags);
790 }
791 /*
792 * Note that this panic will be a recursive panic, since
793 * we only set lock_shutdown_noblock above if panicstr != NULL.
794 */
795 if (error && lock_shutdown_noblock)
796 panic("lockmgr: deadlock (see previous panic)");
797
798 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
799 return (error);
800 }
801
802 /*
803 * For a recursive spinlock held one or more times by the current CPU,
804 * release all N locks, and return N.
805 * Intended for use in mi_switch() shortly before context switching.
806 */
807
808 int
809 #if defined(LOCKDEBUG)
810 _spinlock_release_all(__volatile struct lock *lkp, const char *file, int line)
811 #else
812 spinlock_release_all(__volatile struct lock *lkp)
813 #endif
814 {
815 int s, count;
816 cpuid_t cpu_id;
817
818 KASSERT(lkp->lk_flags & LK_SPIN);
819
820 INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
821
822 cpu_id = cpu_number();
823 count = lkp->lk_exclusivecount;
824
825 if (count != 0) {
826 #ifdef DIAGNOSTIC
827 if (WEHOLDIT(lkp, 0, cpu_id) == 0) {
828 panic("spinlock_release_all: processor %lu, not "
829 "exclusive lock holder %lu "
830 "unlocking", (long)cpu_id, lkp->lk_cpu);
831 }
832 #endif
833 lkp->lk_recurselevel = 0;
834 lkp->lk_exclusivecount = 0;
835 COUNT_CPU(cpu_id, -count);
836 lkp->lk_flags &= ~LK_HAVE_EXCL;
837 SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
838 #if defined(LOCKDEBUG)
839 lkp->lk_unlock_file = file;
840 lkp->lk_unlock_line = line;
841 #endif
842 DONTHAVEIT(lkp);
843 }
844 #ifdef DIAGNOSTIC
845 else if (lkp->lk_sharecount != 0)
846 panic("spinlock_release_all: release of shared lock!");
847 else
848 panic("spinlock_release_all: release of unlocked lock!");
849 #endif
850 INTERLOCK_RELEASE(lkp, LK_SPIN, s);
851
852 return (count);
853 }
854
855 /*
856 * For a recursive spinlock held one or more times by the current CPU,
857 * release all N locks, and return N.
858 * Intended for use in mi_switch() right after resuming execution.
859 */
860
861 void
862 #if defined(LOCKDEBUG)
863 _spinlock_acquire_count(__volatile struct lock *lkp, int count,
864 const char *file, int line)
865 #else
866 spinlock_acquire_count(__volatile struct lock *lkp, int count)
867 #endif
868 {
869 int s, error;
870 cpuid_t cpu_id;
871
872 KASSERT(lkp->lk_flags & LK_SPIN);
873
874 INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
875
876 cpu_id = cpu_number();
877
878 #ifdef DIAGNOSTIC
879 if (WEHOLDIT(lkp, LK_NOPROC, cpu_id))
880 panic("spinlock_acquire_count: processor %lu already holds lock\n", (long)cpu_id);
881 #endif
882 /*
883 * Try to acquire the want_exclusive flag.
884 */
885 ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_flags &
886 (LK_HAVE_EXCL | LK_WANT_EXCL));
887 lkp->lk_flags |= LK_WANT_EXCL;
888 /*
889 * Wait for shared locks and upgrades to finish.
890 */
891 ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_sharecount != 0 ||
892 (lkp->lk_flags & LK_WANT_UPGRADE));
893 lkp->lk_flags &= ~LK_WANT_EXCL;
894 lkp->lk_flags |= LK_HAVE_EXCL;
895 SETHOLDER(lkp, LK_NOPROC, cpu_id);
896 #if defined(LOCKDEBUG)
897 lkp->lk_lock_file = file;
898 lkp->lk_lock_line = line;
899 #endif
900 HAVEIT(lkp);
901 if (lkp->lk_exclusivecount != 0)
902 panic("lockmgr: non-zero exclusive count");
903 lkp->lk_exclusivecount = count;
904 lkp->lk_recurselevel = 1;
905 COUNT_CPU(cpu_id, count);
906
907 INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
908 }
909
910
911
912 /*
913 * Print out information about state of a lock. Used by VOP_PRINT
914 * routines to display ststus about contained locks.
915 */
916 void
917 lockmgr_printinfo(__volatile struct lock *lkp)
918 {
919
920 if (lkp->lk_sharecount)
921 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
922 lkp->lk_sharecount);
923 else if (lkp->lk_flags & LK_HAVE_EXCL) {
924 printf(" lock type %s: EXCL (count %d) by ",
925 lkp->lk_wmesg, lkp->lk_exclusivecount);
926 if (lkp->lk_flags & LK_SPIN)
927 printf("processor %lu", lkp->lk_cpu);
928 else
929 printf("pid %d", lkp->lk_lockholder);
930 } else
931 printf(" not locked");
932 if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
933 printf(" with %d pending", lkp->lk_waitcount);
934 }
935
936 #if defined(LOCKDEBUG) /* { */
937 TAILQ_HEAD(, simplelock) simplelock_list =
938 TAILQ_HEAD_INITIALIZER(simplelock_list);
939
940 #if defined(MULTIPROCESSOR) /* { */
941 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
942
943 #define SLOCK_LIST_LOCK() \
944 __cpu_simple_lock(&simplelock_list_slock.lock_data)
945
946 #define SLOCK_LIST_UNLOCK() \
947 __cpu_simple_unlock(&simplelock_list_slock.lock_data)
948
949 #define SLOCK_COUNT(x) \
950 curcpu()->ci_simple_locks += (x)
951 #else
952 u_long simple_locks;
953
954 #define SLOCK_LIST_LOCK() /* nothing */
955
956 #define SLOCK_LIST_UNLOCK() /* nothing */
957
958 #define SLOCK_COUNT(x) simple_locks += (x)
959 #endif /* MULTIPROCESSOR */ /* } */
960
961 #ifdef DDB /* { */
962 #ifdef MULTIPROCESSOR
963 int simple_lock_debugger = 1; /* more serious on MP */
964 #else
965 int simple_lock_debugger = 0;
966 #endif
967 #define SLOCK_DEBUGGER() if (simple_lock_debugger) Debugger()
968 #else
969 #define SLOCK_DEBUGGER() /* nothing */
970 #endif /* } */
971
972 #ifdef MULTIPROCESSOR
973 #define SLOCK_MP() lock_printf("on cpu %ld\n", \
974 (u_long) cpu_number())
975 #else
976 #define SLOCK_MP() /* nothing */
977 #endif
978
979 #define SLOCK_WHERE(str, alp, id, l) \
980 do { \
981 lock_printf(str); \
982 lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
983 SLOCK_MP(); \
984 if ((alp)->lock_file != NULL) \
985 lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
986 (alp)->lock_line); \
987 if ((alp)->unlock_file != NULL) \
988 lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
989 (alp)->unlock_line); \
990 SLOCK_DEBUGGER(); \
991 } while (/*CONSTCOND*/0)
992
993 /*
994 * Simple lock functions so that the debugger can see from whence
995 * they are being called.
996 */
997 void
998 simple_lock_init(struct simplelock *alp)
999 {
1000
1001 #if defined(MULTIPROCESSOR) /* { */
1002 __cpu_simple_lock_init(&alp->lock_data);
1003 #else
1004 alp->lock_data = __SIMPLELOCK_UNLOCKED;
1005 #endif /* } */
1006 alp->lock_file = NULL;
1007 alp->lock_line = 0;
1008 alp->unlock_file = NULL;
1009 alp->unlock_line = 0;
1010 alp->lock_holder = LK_NOCPU;
1011 }
1012
1013 void
1014 _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
1015 {
1016 cpuid_t cpu_id = cpu_number();
1017 int s;
1018
1019 s = spllock();
1020
1021 /*
1022 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1023 * don't take any action, and just fall into the normal spin case.
1024 */
1025 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1026 #if defined(MULTIPROCESSOR) /* { */
1027 if (alp->lock_holder == cpu_id) {
1028 SLOCK_WHERE("simple_lock: locking against myself\n",
1029 alp, id, l);
1030 goto out;
1031 }
1032 #else
1033 SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
1034 goto out;
1035 #endif /* MULTIPROCESSOR */ /* } */
1036 }
1037
1038 #if defined(MULTIPROCESSOR) /* { */
1039 /* Acquire the lock before modifying any fields. */
1040 __cpu_simple_lock(&alp->lock_data);
1041 #else
1042 alp->lock_data = __SIMPLELOCK_LOCKED;
1043 #endif /* } */
1044
1045 if (alp->lock_holder != LK_NOCPU) {
1046 SLOCK_WHERE("simple_lock: uninitialized lock\n",
1047 alp, id, l);
1048 }
1049 alp->lock_file = id;
1050 alp->lock_line = l;
1051 alp->lock_holder = cpu_id;
1052
1053 SLOCK_LIST_LOCK();
1054 /* XXX Cast away volatile */
1055 TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
1056 SLOCK_LIST_UNLOCK();
1057
1058 SLOCK_COUNT(1);
1059
1060 out:
1061 splx(s);
1062 }
1063
1064 int
1065 _simple_lock_held(__volatile struct simplelock *alp)
1066 {
1067 cpuid_t cpu_id = cpu_number();
1068 int s, locked = 0;
1069
1070 s = spllock();
1071
1072 #if defined(MULTIPROCESSOR)
1073 if (__cpu_simple_lock_try(&alp->lock_data) == 0)
1074 locked = (alp->lock_holder == cpu_id);
1075 else
1076 __cpu_simple_unlock(&alp->lock_data);
1077 #else
1078 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1079 locked = 1;
1080 KASSERT(alp->lock_holder == cpu_id);
1081 }
1082 #endif
1083
1084 splx(s);
1085
1086 return (locked);
1087 }
1088
1089 int
1090 _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
1091 {
1092 cpuid_t cpu_id = cpu_number();
1093 int s, rv = 0;
1094
1095 s = spllock();
1096
1097 /*
1098 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1099 * don't take any action.
1100 */
1101 #if defined(MULTIPROCESSOR) /* { */
1102 if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
1103 if (alp->lock_holder == cpu_id)
1104 SLOCK_WHERE("simple_lock_try: locking against myself\n",
1105 alp, id, l);
1106 goto out;
1107 }
1108 #else
1109 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1110 SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
1111 goto out;
1112 }
1113 alp->lock_data = __SIMPLELOCK_LOCKED;
1114 #endif /* MULTIPROCESSOR */ /* } */
1115
1116 /*
1117 * At this point, we have acquired the lock.
1118 */
1119
1120 rv = 1;
1121
1122 alp->lock_file = id;
1123 alp->lock_line = l;
1124 alp->lock_holder = cpu_id;
1125
1126 SLOCK_LIST_LOCK();
1127 /* XXX Cast away volatile. */
1128 TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
1129 SLOCK_LIST_UNLOCK();
1130
1131 SLOCK_COUNT(1);
1132
1133 out:
1134 splx(s);
1135 return (rv);
1136 }
1137
1138 void
1139 _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
1140 {
1141 int s;
1142
1143 s = spllock();
1144
1145 /*
1146 * MULTIPROCESSOR case: This is `safe' because we think we hold
1147 * the lock, and if we don't, we don't take any action.
1148 */
1149 if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
1150 SLOCK_WHERE("simple_unlock: lock not held\n",
1151 alp, id, l);
1152 goto out;
1153 }
1154
1155 SLOCK_LIST_LOCK();
1156 TAILQ_REMOVE(&simplelock_list, alp, list);
1157 SLOCK_LIST_UNLOCK();
1158
1159 SLOCK_COUNT(-1);
1160
1161 alp->list.tqe_next = NULL; /* sanity */
1162 alp->list.tqe_prev = NULL; /* sanity */
1163
1164 alp->unlock_file = id;
1165 alp->unlock_line = l;
1166
1167 #if defined(MULTIPROCESSOR) /* { */
1168 alp->lock_holder = LK_NOCPU;
1169 /* Now that we've modified all fields, release the lock. */
1170 __cpu_simple_unlock(&alp->lock_data);
1171 #else
1172 alp->lock_data = __SIMPLELOCK_UNLOCKED;
1173 KASSERT(alp->lock_holder == cpu_number());
1174 alp->lock_holder = LK_NOCPU;
1175 #endif /* } */
1176
1177 out:
1178 splx(s);
1179 }
1180
1181 void
1182 simple_lock_dump(void)
1183 {
1184 struct simplelock *alp;
1185 int s;
1186
1187 s = spllock();
1188 SLOCK_LIST_LOCK();
1189 lock_printf("all simple locks:\n");
1190 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
1191 alp = TAILQ_NEXT(alp, list)) {
1192 lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
1193 alp->lock_file, alp->lock_line);
1194 }
1195 SLOCK_LIST_UNLOCK();
1196 splx(s);
1197 }
1198
1199 void
1200 simple_lock_freecheck(void *start, void *end)
1201 {
1202 struct simplelock *alp;
1203 int s;
1204
1205 s = spllock();
1206 SLOCK_LIST_LOCK();
1207 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
1208 alp = TAILQ_NEXT(alp, list)) {
1209 if ((void *)alp >= start && (void *)alp < end) {
1210 lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
1211 alp, alp->lock_holder, alp->lock_file,
1212 alp->lock_line);
1213 SLOCK_DEBUGGER();
1214 }
1215 }
1216 SLOCK_LIST_UNLOCK();
1217 splx(s);
1218 }
1219
1220 void
1221 simple_lock_switchcheck(void)
1222 {
1223 struct simplelock *alp;
1224 cpuid_t cpu_id = cpu_number();
1225 int s;
1226
1227 /*
1228 * We must be holding exactly one lock: the sched_lock.
1229 */
1230
1231 SCHED_ASSERT_LOCKED();
1232
1233 s = spllock();
1234 SLOCK_LIST_LOCK();
1235 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
1236 alp = TAILQ_NEXT(alp, list)) {
1237 if (alp == &sched_lock)
1238 continue;
1239 if (alp->lock_holder == cpu_id) {
1240 lock_printf("switching with held simple_lock %p "
1241 "CPU %lu %s:%d\n",
1242 alp, alp->lock_holder, alp->lock_file,
1243 alp->lock_line);
1244 SLOCK_DEBUGGER();
1245 }
1246 }
1247 SLOCK_LIST_UNLOCK();
1248 splx(s);
1249 }
1250 #endif /* LOCKDEBUG */ /* } */
1251