kern_lock.c revision 1.39 1 /* $NetBSD: kern_lock.c,v 1.39 2000/08/17 04:18:21 thorpej 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/proc.h>
88 #include <sys/lock.h>
89 #include <sys/systm.h>
90 #include <machine/cpu.h>
91
92 #if defined(__HAVE_ATOMIC_OPERATIONS)
93 #include <machine/atomic.h>
94 #endif
95
96 #if defined(LOCKDEBUG)
97 #include <sys/syslog.h>
98 /*
99 * note that stdarg.h and the ansi style va_start macro is used for both
100 * ansi and traditional c compiles.
101 * XXX: this requires that stdarg.h define: va_alist and va_dcl
102 */
103 #include <machine/stdarg.h>
104
105 void lock_printf(const char *fmt, ...)
106 __attribute__((__format__(__printf__,1,2)));
107
108 int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
109 #endif
110
111 /*
112 * Locking primitives implementation.
113 * Locks provide shared/exclusive sychronization.
114 */
115
116 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
117 #if defined(MULTIPROCESSOR) /* { */
118 #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
119 #define COUNT_CPU(cpu_id, x) \
120 atomic_add_ulong(&curcpu()->ci_spin_locks, (x))
121 #else
122 #define COUNT_CPU(cpu_id, x) /* not safe */
123 #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
124 #else
125 u_long spin_locks;
126 #define COUNT_CPU(cpu_id, x) spin_locks += (x)
127 #endif /* MULTIPROCESSOR */ /* } */
128
129 #define COUNT(lkp, p, cpu_id, x) \
130 do { \
131 if ((lkp)->lk_flags & LK_SPIN) \
132 COUNT_CPU((cpu_id), (x)); \
133 else \
134 (p)->p_locks += (x); \
135 } while (/*CONSTCOND*/0)
136 #else
137 #define COUNT(lkp, p, cpu_id, x)
138 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
139
140 /*
141 * Acquire a resource.
142 */
143 #define ACQUIRE(lkp, error, extflags, drain, wanted) \
144 if ((extflags) & LK_SPIN) { \
145 int interlocked; \
146 \
147 if ((drain) == 0) \
148 (lkp)->lk_waitcount++; \
149 for (interlocked = 1;;) { \
150 if (wanted) { \
151 if (interlocked) { \
152 simple_unlock(&(lkp)->lk_interlock); \
153 interlocked = 0; \
154 } \
155 } else if (interlocked) { \
156 break; \
157 } else { \
158 simple_lock(&(lkp)->lk_interlock); \
159 interlocked = 1; \
160 } \
161 } \
162 if ((drain) == 0) \
163 (lkp)->lk_waitcount--; \
164 KASSERT((wanted) == 0); \
165 error = 0; /* sanity */ \
166 } else { \
167 for (error = 0; wanted; ) { \
168 if ((drain)) \
169 (lkp)->lk_flags |= LK_WAITDRAIN; \
170 else \
171 (lkp)->lk_waitcount++; \
172 /* XXX Cast away volatile. */ \
173 error = ltsleep((drain) ? &(lkp)->lk_flags : \
174 (void *)(lkp), (lkp)->lk_prio, \
175 (lkp)->lk_wmesg, (lkp)->lk_timo, \
176 &(lkp)->lk_interlock); \
177 if ((drain) == 0) \
178 (lkp)->lk_waitcount--; \
179 if (error) \
180 break; \
181 if ((extflags) & LK_SLEEPFAIL) { \
182 error = ENOLCK; \
183 break; \
184 } \
185 } \
186 }
187
188 #define SETHOLDER(lkp, pid, cpu_id) \
189 do { \
190 if ((lkp)->lk_flags & LK_SPIN) \
191 (lkp)->lk_cpu = cpu_id; \
192 else \
193 (lkp)->lk_lockholder = pid; \
194 } while (/*CONSTCOND*/0)
195
196 #define WEHOLDIT(lkp, pid, cpu_id) \
197 (((lkp)->lk_flags & LK_SPIN) != 0 ? \
198 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
199
200 #define WAKEUP_WAITER(lkp) \
201 do { \
202 if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) { \
203 /* XXX Cast away volatile. */ \
204 wakeup_one((void *)(lkp)); \
205 } \
206 } while (/*CONSTCOND*/0)
207
208 #if defined(LOCKDEBUG) /* { */
209 #if defined(MULTIPROCESSOR) /* { */
210 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
211
212 #define SPINLOCK_LIST_LOCK() \
213 __cpu_simple_lock(&spinlock_list_slock.lock_data)
214
215 #define SPINLOCK_LIST_UNLOCK() \
216 __cpu_simple_unlock(&spinlock_list_slock.lock_data)
217 #else
218 #define SPINLOCK_LIST_LOCK() /* nothing */
219
220 #define SPINLOCK_LIST_UNLOCK() /* nothing */
221 #endif /* MULTIPROCESSOR */ /* } */
222
223 TAILQ_HEAD(, lock) spinlock_list =
224 TAILQ_HEAD_INITIALIZER(spinlock_list);
225
226 #define HAVEIT(lkp) \
227 do { \
228 if ((lkp)->lk_flags & LK_SPIN) { \
229 int s = splhigh(); \
230 SPINLOCK_LIST_LOCK(); \
231 /* XXX Cast away volatile. */ \
232 TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp), \
233 lk_list); \
234 SPINLOCK_LIST_UNLOCK(); \
235 splx(s); \
236 } \
237 } while (/*CONSTCOND*/0)
238
239 #define DONTHAVEIT(lkp) \
240 do { \
241 if ((lkp)->lk_flags & LK_SPIN) { \
242 int s = splhigh(); \
243 SPINLOCK_LIST_LOCK(); \
244 /* XXX Cast away volatile. */ \
245 TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp), \
246 lk_list); \
247 SPINLOCK_LIST_UNLOCK(); \
248 splx(s); \
249 } \
250 } while (/*CONSTCOND*/0)
251 #else
252 #define HAVEIT(lkp) /* nothing */
253
254 #define DONTHAVEIT(lkp) /* nothing */
255 #endif /* LOCKDEBUG */ /* } */
256
257 #if defined(LOCKDEBUG)
258 /*
259 * Lock debug printing routine; can be configured to print to console
260 * or log to syslog.
261 */
262 void
263 lock_printf(const char *fmt, ...)
264 {
265 va_list ap;
266
267 va_start(ap, fmt);
268 if (lock_debug_syslog)
269 vlog(LOG_DEBUG, fmt, ap);
270 else
271 vprintf(fmt, ap);
272 va_end(ap);
273 }
274 #endif /* LOCKDEBUG */
275
276 /*
277 * Initialize a lock; required before use.
278 */
279 void
280 lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
281 {
282
283 memset(lkp, 0, sizeof(struct lock));
284 simple_lock_init(&lkp->lk_interlock);
285 lkp->lk_flags = flags & LK_EXTFLG_MASK;
286 if (flags & LK_SPIN)
287 lkp->lk_cpu = LK_NOCPU;
288 else {
289 lkp->lk_lockholder = LK_NOPROC;
290 lkp->lk_prio = prio;
291 lkp->lk_timo = timo;
292 }
293 lkp->lk_wmesg = wmesg; /* just a name for spin locks */
294 }
295
296 /*
297 * Determine the status of a lock.
298 */
299 int
300 lockstatus(struct lock *lkp)
301 {
302 int lock_type = 0;
303
304 simple_lock(&lkp->lk_interlock);
305 if (lkp->lk_exclusivecount != 0)
306 lock_type = LK_EXCLUSIVE;
307 else if (lkp->lk_sharecount != 0)
308 lock_type = LK_SHARED;
309 simple_unlock(&lkp->lk_interlock);
310 return (lock_type);
311 }
312
313 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
314 /*
315 * Make sure no spin locks are held by a CPU that is about
316 * to context switch.
317 */
318 void
319 spinlock_switchcheck(void)
320 {
321 u_long cnt;
322 int s;
323
324 s = splhigh();
325 #if defined(MULTIPROCESSOR)
326 cnt = curcpu()->ci_spin_locks;
327 #else
328 cnt = spin_locks;
329 #endif
330 splx(s);
331
332 if (cnt != 0)
333 panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
334 (u_long) cpu_number(), cnt);
335 }
336 #endif /* LOCKDEBUG || DIAGNOSTIC */
337
338 /*
339 * XXX XXX kludge around another kludge..
340 *
341 * vfs_shutdown() may be called from interrupt context, either as a result
342 * of a panic, or from the debugger. It proceeds to call
343 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
344 *
345 * We would like to make an attempt to sync the filesystems in this case, so
346 * if this happens, we treat attempts to acquire locks specially.
347 * All locks are acquired on behalf of proc0.
348 *
349 * If we've already paniced, we don't block waiting for locks, but
350 * just barge right ahead since we're already going down in flames.
351 */
352
353 /*
354 * Set, change, or release a lock.
355 *
356 * Shared requests increment the shared count. Exclusive requests set the
357 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
358 * accepted shared locks and shared-to-exclusive upgrades to go away.
359 */
360 int
361 lockmgr(__volatile struct lock *lkp, u_int flags,
362 struct simplelock *interlkp)
363 {
364 int error;
365 pid_t pid;
366 int extflags;
367 cpuid_t cpu_id;
368 struct proc *p = curproc;
369 int lock_shutdown_noblock = 0;
370
371 error = 0;
372
373 simple_lock(&lkp->lk_interlock);
374 if (flags & LK_INTERLOCK)
375 simple_unlock(interlkp);
376 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
377
378 #ifdef DIAGNOSTIC /* { */
379 /*
380 * Don't allow spins on sleep locks and don't allow sleeps
381 * on spin locks.
382 */
383 if ((flags ^ lkp->lk_flags) & LK_SPIN)
384 panic("lockmgr: sleep/spin mismatch\n");
385 #endif /* } */
386
387 if (extflags & LK_SPIN)
388 pid = LK_KERNPROC;
389 else {
390 if (p == NULL) {
391 if (!doing_shutdown) {
392 #ifdef DIAGNOSTIC
393 panic("lockmgr: no context");
394 #endif
395 } else {
396 p = &proc0;
397 if (panicstr && (!(flags & LK_NOWAIT))) {
398 flags |= LK_NOWAIT;
399 lock_shutdown_noblock = 1;
400 }
401 }
402 }
403 pid = p->p_pid;
404 }
405 cpu_id = cpu_number();
406
407 /*
408 * Once a lock has drained, the LK_DRAINING flag is set and an
409 * exclusive lock is returned. The only valid operation thereafter
410 * is a single release of that exclusive lock. This final release
411 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
412 * further requests of any sort will result in a panic. The bits
413 * selected for these two flags are chosen so that they will be set
414 * in memory that is freed (freed memory is filled with 0xdeadbeef).
415 * The final release is permitted to give a new lease on life to
416 * the lock by specifying LK_REENABLE.
417 */
418 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
419 #ifdef DIAGNOSTIC /* { */
420 if (lkp->lk_flags & LK_DRAINED)
421 panic("lockmgr: using decommissioned lock");
422 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
423 WEHOLDIT(lkp, pid, cpu_id) == 0)
424 panic("lockmgr: non-release on draining lock: %d\n",
425 flags & LK_TYPE_MASK);
426 #endif /* DIAGNOSTIC */ /* } */
427 lkp->lk_flags &= ~LK_DRAINING;
428 if ((flags & LK_REENABLE) == 0)
429 lkp->lk_flags |= LK_DRAINED;
430 }
431
432 switch (flags & LK_TYPE_MASK) {
433
434 case LK_SHARED:
435 if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
436 /*
437 * If just polling, check to see if we will block.
438 */
439 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
440 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
441 error = EBUSY;
442 break;
443 }
444 /*
445 * Wait for exclusive locks and upgrades to clear.
446 */
447 ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
448 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
449 if (error)
450 break;
451 lkp->lk_sharecount++;
452 COUNT(lkp, p, cpu_id, 1);
453 break;
454 }
455 /*
456 * We hold an exclusive lock, so downgrade it to shared.
457 * An alternative would be to fail with EDEADLK.
458 */
459 lkp->lk_sharecount++;
460 COUNT(lkp, p, cpu_id, 1);
461 /* fall into downgrade */
462
463 case LK_DOWNGRADE:
464 if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
465 lkp->lk_exclusivecount == 0)
466 panic("lockmgr: not holding exclusive lock");
467 lkp->lk_sharecount += lkp->lk_exclusivecount;
468 lkp->lk_exclusivecount = 0;
469 lkp->lk_recurselevel = 0;
470 lkp->lk_flags &= ~LK_HAVE_EXCL;
471 SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
472 DONTHAVEIT(lkp);
473 WAKEUP_WAITER(lkp);
474 break;
475
476 case LK_EXCLUPGRADE:
477 /*
478 * If another process is ahead of us to get an upgrade,
479 * then we want to fail rather than have an intervening
480 * exclusive access.
481 */
482 if (lkp->lk_flags & LK_WANT_UPGRADE) {
483 lkp->lk_sharecount--;
484 COUNT(lkp, p, cpu_id, -1);
485 error = EBUSY;
486 break;
487 }
488 /* fall into normal upgrade */
489
490 case LK_UPGRADE:
491 /*
492 * Upgrade a shared lock to an exclusive one. If another
493 * shared lock has already requested an upgrade to an
494 * exclusive lock, our shared lock is released and an
495 * exclusive lock is requested (which will be granted
496 * after the upgrade). If we return an error, the file
497 * will always be unlocked.
498 */
499 if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
500 panic("lockmgr: upgrade exclusive lock");
501 lkp->lk_sharecount--;
502 COUNT(lkp, p, cpu_id, -1);
503 /*
504 * If we are just polling, check to see if we will block.
505 */
506 if ((extflags & LK_NOWAIT) &&
507 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
508 lkp->lk_sharecount > 1)) {
509 error = EBUSY;
510 break;
511 }
512 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
513 /*
514 * We are first shared lock to request an upgrade, so
515 * request upgrade and wait for the shared count to
516 * drop to zero, then take exclusive lock.
517 */
518 lkp->lk_flags |= LK_WANT_UPGRADE;
519 ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
520 lkp->lk_flags &= ~LK_WANT_UPGRADE;
521 if (error)
522 break;
523 lkp->lk_flags |= LK_HAVE_EXCL;
524 SETHOLDER(lkp, pid, cpu_id);
525 HAVEIT(lkp);
526 if (lkp->lk_exclusivecount != 0)
527 panic("lockmgr: non-zero exclusive count");
528 lkp->lk_exclusivecount = 1;
529 if (extflags & LK_SETRECURSE)
530 lkp->lk_recurselevel = 1;
531 COUNT(lkp, p, cpu_id, 1);
532 break;
533 }
534 /*
535 * Someone else has requested upgrade. Release our shared
536 * lock, awaken upgrade requestor if we are the last shared
537 * lock, then request an exclusive lock.
538 */
539 if (lkp->lk_sharecount == 0)
540 WAKEUP_WAITER(lkp);
541 /* fall into exclusive request */
542
543 case LK_EXCLUSIVE:
544 if (WEHOLDIT(lkp, pid, cpu_id)) {
545 /*
546 * Recursive lock.
547 */
548 if ((extflags & LK_CANRECURSE) == 0 &&
549 lkp->lk_recurselevel == 0) {
550 if (extflags & LK_RECURSEFAIL) {
551 error = EDEADLK;
552 break;
553 } else
554 panic("lockmgr: locking against myself");
555 }
556 lkp->lk_exclusivecount++;
557 if (extflags & LK_SETRECURSE &&
558 lkp->lk_recurselevel == 0)
559 lkp->lk_recurselevel = lkp->lk_exclusivecount;
560 COUNT(lkp, p, cpu_id, 1);
561 break;
562 }
563 /*
564 * If we are just polling, check to see if we will sleep.
565 */
566 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
567 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
568 lkp->lk_sharecount != 0)) {
569 error = EBUSY;
570 break;
571 }
572 /*
573 * Try to acquire the want_exclusive flag.
574 */
575 ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
576 (LK_HAVE_EXCL | LK_WANT_EXCL));
577 if (error)
578 break;
579 lkp->lk_flags |= LK_WANT_EXCL;
580 /*
581 * Wait for shared locks and upgrades to finish.
582 */
583 ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
584 (lkp->lk_flags & LK_WANT_UPGRADE));
585 lkp->lk_flags &= ~LK_WANT_EXCL;
586 if (error)
587 break;
588 lkp->lk_flags |= LK_HAVE_EXCL;
589 SETHOLDER(lkp, pid, cpu_id);
590 HAVEIT(lkp);
591 if (lkp->lk_exclusivecount != 0)
592 panic("lockmgr: non-zero exclusive count");
593 lkp->lk_exclusivecount = 1;
594 if (extflags & LK_SETRECURSE)
595 lkp->lk_recurselevel = 1;
596 COUNT(lkp, p, cpu_id, 1);
597 break;
598
599 case LK_RELEASE:
600 if (lkp->lk_exclusivecount != 0) {
601 if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
602 if (lkp->lk_flags & LK_SPIN) {
603 panic("lockmgr: processor %lu, not "
604 "exclusive lock holder %lu "
605 "unlocking", cpu_id, lkp->lk_cpu);
606 } else {
607 panic("lockmgr: pid %d, not "
608 "exclusive lock holder %d "
609 "unlocking", pid,
610 lkp->lk_lockholder);
611 }
612 }
613 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
614 lkp->lk_recurselevel = 0;
615 lkp->lk_exclusivecount--;
616 COUNT(lkp, p, cpu_id, -1);
617 if (lkp->lk_exclusivecount == 0) {
618 lkp->lk_flags &= ~LK_HAVE_EXCL;
619 SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
620 DONTHAVEIT(lkp);
621 }
622 } else if (lkp->lk_sharecount != 0) {
623 lkp->lk_sharecount--;
624 COUNT(lkp, p, cpu_id, -1);
625 }
626 #ifdef DIAGNOSTIC
627 else
628 panic("lockmgr: release of unlocked lock!");
629 #endif
630 WAKEUP_WAITER(lkp);
631 break;
632
633 case LK_DRAIN:
634 /*
635 * Check that we do not already hold the lock, as it can
636 * never drain if we do. Unfortunately, we have no way to
637 * check for holding a shared lock, but at least we can
638 * check for an exclusive one.
639 */
640 if (WEHOLDIT(lkp, pid, cpu_id))
641 panic("lockmgr: draining against myself");
642 /*
643 * If we are just polling, check to see if we will sleep.
644 */
645 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
646 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
647 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
648 error = EBUSY;
649 break;
650 }
651 ACQUIRE(lkp, error, extflags, 1,
652 ((lkp->lk_flags &
653 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
654 lkp->lk_sharecount != 0 ||
655 lkp->lk_waitcount != 0));
656 if (error)
657 break;
658 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
659 SETHOLDER(lkp, pid, cpu_id);
660 HAVEIT(lkp);
661 lkp->lk_exclusivecount = 1;
662 /* XXX unlikely that we'd want this */
663 if (extflags & LK_SETRECURSE)
664 lkp->lk_recurselevel = 1;
665 COUNT(lkp, p, cpu_id, 1);
666 break;
667
668 default:
669 simple_unlock(&lkp->lk_interlock);
670 panic("lockmgr: unknown locktype request %d",
671 flags & LK_TYPE_MASK);
672 /* NOTREACHED */
673 }
674 if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
675 ((lkp->lk_flags &
676 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
677 lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
678 lkp->lk_flags &= ~LK_WAITDRAIN;
679 wakeup_one((void *)&lkp->lk_flags);
680 }
681 /*
682 * Note that this panic will be a recursive panic, since
683 * we only set lock_shutdown_noblock above if panicstr != NULL.
684 */
685 if (error && lock_shutdown_noblock)
686 panic("lockmgr: deadlock (see previous panic)");
687
688 simple_unlock(&lkp->lk_interlock);
689 return (error);
690 }
691
692 /*
693 * Print out information about state of a lock. Used by VOP_PRINT
694 * routines to display ststus about contained locks.
695 */
696 void
697 lockmgr_printinfo(__volatile struct lock *lkp)
698 {
699
700 if (lkp->lk_sharecount)
701 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
702 lkp->lk_sharecount);
703 else if (lkp->lk_flags & LK_HAVE_EXCL) {
704 printf(" lock type %s: EXCL (count %d) by ",
705 lkp->lk_wmesg, lkp->lk_exclusivecount);
706 if (lkp->lk_flags & LK_SPIN)
707 printf("processor %lu", lkp->lk_cpu);
708 else
709 printf("pid %d", lkp->lk_lockholder);
710 } else
711 printf(" not locked");
712 if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
713 printf(" with %d pending", lkp->lk_waitcount);
714 }
715
716 #if defined(LOCKDEBUG) /* { */
717 TAILQ_HEAD(, simplelock) simplelock_list =
718 TAILQ_HEAD_INITIALIZER(simplelock_list);
719
720 #if defined(MULTIPROCESSOR) /* { */
721 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
722
723 #define SLOCK_LIST_LOCK() \
724 __cpu_simple_lock(&simplelock_list_slock.lock_data)
725
726 #define SLOCK_LIST_UNLOCK() \
727 __cpu_simple_unlock(&simplelock_list_slock.lock_data)
728
729 #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
730 #define SLOCK_COUNT(x) \
731 atomic_add_ulong(&curcpu()->ci_simple_locks, (x))
732 #else
733 #define SLOCK_COUNT(x) /* not safe */
734 #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
735 #else
736 u_long simple_locks;
737
738 #define SLOCK_LIST_LOCK() /* nothing */
739
740 #define SLOCK_LIST_UNLOCK() /* nothing */
741
742 #define SLOCK_COUNT(x) simple_locks += (x)
743 #endif /* MULTIPROCESSOR */ /* } */
744
745 #ifdef DDB /* { */
746 int simple_lock_debugger = 0;
747 #define SLOCK_DEBUGGER() if (simple_lock_debugger) Debugger()
748 #else
749 #define SLOCK_DEBUGGER() /* nothing */
750 #endif /* } */
751
752 #ifdef MULTIPROCESSOR
753 #define SLOCK_MP() lock_printf("on cpu %d\n", cpu_number())
754 #else
755 #define SLOCK_MP() /* nothing */
756 #endif
757
758 #define SLOCK_WHERE(str, alp, id, l) \
759 do { \
760 lock_printf(str); \
761 lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
762 SLOCK_MP(); \
763 if ((alp)->lock_file != NULL) \
764 lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
765 (alp)->lock_line); \
766 if ((alp)->unlock_file != NULL) \
767 lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
768 (alp)->unlock_line); \
769 SLOCK_DEBUGGER(); \
770 } while (/*CONSTCOND*/0)
771
772 /*
773 * Simple lock functions so that the debugger can see from whence
774 * they are being called.
775 */
776 void
777 simple_lock_init(struct simplelock *alp)
778 {
779
780 #if defined(MULTIPROCESSOR) /* { */
781 __cpu_simple_lock_init(&alp->lock_data);
782 #else
783 alp->lock_data = __SIMPLELOCK_UNLOCKED;
784 #endif /* } */
785 alp->lock_file = NULL;
786 alp->lock_line = 0;
787 alp->unlock_file = NULL;
788 alp->unlock_line = 0;
789 alp->lock_holder = 0;
790 }
791
792 void
793 _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
794 {
795 cpuid_t cpu_id = cpu_number();
796 int s;
797
798 s = splhigh();
799
800 /*
801 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
802 * don't take any action, and just fall into the normal spin case.
803 */
804 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
805 #if defined(MULTIPROCESSOR) /* { */
806 if (alp->lock_holder == cpu_id) {
807 SLOCK_WHERE("simple_lock: locking against myself\n",
808 alp, id, l);
809 goto out;
810 }
811 #else
812 SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
813 goto out;
814 #endif /* MULTIPROCESSOR */ /* } */
815 }
816
817 #if defined(MULTIPROCESSOR) /* { */
818 /* Acquire the lock before modifying any fields. */
819 __cpu_simple_lock(&alp->lock_data);
820 #else
821 alp->lock_data = __SIMPLELOCK_LOCKED;
822 #endif /* } */
823
824 alp->lock_file = id;
825 alp->lock_line = l;
826 alp->lock_holder = cpu_id;
827
828 SLOCK_LIST_LOCK();
829 /* XXX Cast away volatile */
830 TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
831 SLOCK_LIST_UNLOCK();
832
833 SLOCK_COUNT(1);
834
835 out:
836 splx(s);
837 }
838
839 int
840 _simple_lock_held(__volatile struct simplelock *alp)
841 {
842 #if defined(MULTIPROCESSOR)
843 cpuid_t cpu_id = cpu_number();
844 int s, locked = 0;
845
846 s = splhigh();
847 if (__cpu_simple_lock_try(&alp->lock_data) == 0)
848 locked = (alp->lock_holder == cpu_id);
849 else
850 __cpu_simple_unlock(&alp->lock_data);
851 splx(s);
852 #else
853 int s, locked;
854
855 s = splhigh();
856 locked = (alp->lock_data == __SIMPLELOCK_LOCKED);
857 splx(s);
858 #endif
859 return (locked);
860 }
861
862 int
863 _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
864 {
865 cpuid_t cpu_id = cpu_number();
866 int s, rv = 0;
867
868 s = splhigh();
869
870 /*
871 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
872 * don't take any action.
873 */
874 #if defined(MULTIPROCESSOR) /* { */
875 if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
876 if (alp->lock_holder == cpu_id)
877 SLOCK_WHERE("simple_lock_try: locking against myself\n",
878 alp, id, l);
879 goto out;
880 }
881 #else
882 if (alp->lock_data == __SIMPLELOCK_LOCKED) {
883 SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
884 goto out;
885 }
886 alp->lock_data = __SIMPLELOCK_LOCKED;
887 #endif /* MULTIPROCESSOR */ /* } */
888
889 /*
890 * At this point, we have acquired the lock.
891 */
892
893 rv = 1;
894
895 alp->lock_file = id;
896 alp->lock_line = l;
897 alp->lock_holder = cpu_id;
898
899 SLOCK_LIST_LOCK();
900 /* XXX Cast away volatile. */
901 TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
902 SLOCK_LIST_UNLOCK();
903
904 SLOCK_COUNT(1);
905
906 out:
907 splx(s);
908 return (rv);
909 }
910
911 void
912 _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
913 {
914 int s;
915
916 s = splhigh();
917
918 /*
919 * MULTIPROCESSOR case: This is `safe' because we think we hold
920 * the lock, and if we don't, we don't take any action.
921 */
922 if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
923 SLOCK_WHERE("simple_unlock: lock not held\n",
924 alp, id, l);
925 goto out;
926 }
927
928 SLOCK_LIST_LOCK();
929 TAILQ_REMOVE(&simplelock_list, alp, list);
930 SLOCK_LIST_UNLOCK();
931
932 SLOCK_COUNT(-1);
933
934 alp->list.tqe_next = NULL; /* sanity */
935 alp->list.tqe_prev = NULL; /* sanity */
936
937 alp->unlock_file = id;
938 alp->unlock_line = l;
939
940 #if defined(MULTIPROCESSOR) /* { */
941 alp->lock_holder = LK_NOCPU;
942 /* Now that we've modified all fields, release the lock. */
943 __cpu_simple_unlock(&alp->lock_data);
944 #else
945 alp->lock_data = __SIMPLELOCK_UNLOCKED;
946 #endif /* } */
947
948 out:
949 splx(s);
950 }
951
952 void
953 simple_lock_dump(void)
954 {
955 struct simplelock *alp;
956 int s;
957
958 s = splhigh();
959 SLOCK_LIST_LOCK();
960 lock_printf("all simple locks:\n");
961 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
962 alp = TAILQ_NEXT(alp, list)) {
963 lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
964 alp->lock_file, alp->lock_line);
965 }
966 SLOCK_LIST_UNLOCK();
967 splx(s);
968 }
969
970 void
971 simple_lock_freecheck(void *start, void *end)
972 {
973 struct simplelock *alp;
974 int s;
975
976 s = splhigh();
977 SLOCK_LIST_LOCK();
978 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
979 alp = TAILQ_NEXT(alp, list)) {
980 if ((void *)alp >= start && (void *)alp < end) {
981 lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
982 alp, alp->lock_holder, alp->lock_file,
983 alp->lock_line);
984 SLOCK_DEBUGGER();
985 }
986 }
987 SLOCK_LIST_UNLOCK();
988 splx(s);
989 }
990
991 void
992 simple_lock_switchcheck(void)
993 {
994 struct simplelock *alp;
995 cpuid_t cpu_id = cpu_number();
996 int s;
997
998 s = splhigh();
999 SLOCK_LIST_LOCK();
1000 for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
1001 alp = TAILQ_NEXT(alp, list)) {
1002 if (alp->lock_holder == cpu_id) {
1003 lock_printf("switching with held simple_lock %p "
1004 "CPU %lu %s:%d\n",
1005 alp, alp->lock_holder, alp->lock_file,
1006 alp->lock_line);
1007 SLOCK_DEBUGGER();
1008 }
1009 }
1010 SLOCK_LIST_UNLOCK();
1011 splx(s);
1012 }
1013 #endif /* LOCKDEBUG */ /* } */
1014