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