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