kern_lock.c revision 1.110.2.5 1 /* $NetBSD: kern_lock.c,v 1.110.2.5 2007/04/10 13:26:39 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Ross Harvey.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 /*
44 * Copyright (c) 1995
45 * The Regents of the University of California. All rights reserved.
46 *
47 * This code contains ideas from software contributed to Berkeley by
48 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
49 * System project at Carnegie-Mellon University.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.110.2.5 2007/04/10 13:26:39 ad Exp $");
80
81 #include "opt_multiprocessor.h"
82 #include "opt_ddb.h"
83
84 #define __MUTEX_PRIVATE
85
86 #include <sys/param.h>
87 #include <sys/proc.h>
88 #include <sys/lock.h>
89 #include <sys/systm.h>
90 #include <sys/lockdebug.h>
91
92 #include <machine/cpu.h>
93 #include <machine/stdarg.h>
94
95 #include <dev/lockstat.h>
96
97 #if defined(LOCKDEBUG)
98 #include <sys/syslog.h>
99 /*
100 * note that stdarg.h and the ansi style va_start macro is used for both
101 * ansi and traditional c compiles.
102 * XXX: this requires that stdarg.h define: va_alist and va_dcl
103 */
104 #include <machine/stdarg.h>
105
106 void lock_printf(const char *fmt, ...)
107 __attribute__((__format__(__printf__,1,2)));
108
109 static int acquire(volatile struct lock **, int *, int, int, int, uintptr_t);
110
111 int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
112
113 #ifdef DDB
114 #include <ddb/ddbvar.h>
115 #include <machine/db_machdep.h>
116 #include <ddb/db_command.h>
117 #include <ddb/db_interface.h>
118 #endif
119 #endif /* defined(LOCKDEBUG) */
120
121 #if defined(MULTIPROCESSOR)
122 /*
123 * IPL_BIGLOCK: block IPLs which need to grab kernel_mutex.
124 * XXX IPL_VM or IPL_AUDIO should be enough.
125 */
126 #if !defined(__HAVE_SPLBIGLOCK)
127 #define splbiglock splclock
128 #endif
129 int kernel_lock_id;
130 #endif
131
132 __cpu_simple_lock_t kernel_lock;
133
134 /*
135 * Locking primitives implementation.
136 * Locks provide shared/exclusive synchronization.
137 */
138
139 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
140 #define COUNT(lkp, l, cpu_id, x) (l)->l_locks += (x)
141 #else
142 #define COUNT(lkp, p, cpu_id, x)
143 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
144
145 #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
146
147 /*
148 * Acquire a resource.
149 */
150 static int
151 acquire(volatile struct lock **lkpp, int *s, int extflags,
152 int drain, int wanted, uintptr_t ra)
153 {
154 int error;
155 volatile struct lock *lkp = *lkpp;
156 LOCKSTAT_TIMER(slptime);
157 LOCKSTAT_FLAG(lsflag);
158
159 KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
160
161 LOCKSTAT_ENTER(lsflag);
162
163 for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
164 if (drain)
165 lkp->lk_flags |= LK_WAITDRAIN;
166 else {
167 lkp->lk_waitcount++;
168 lkp->lk_flags |= LK_WAIT_NONZERO;
169 }
170 /* XXX Cast away volatile. */
171 LOCKSTAT_START_TIMER(lsflag, slptime);
172 error = mtsleep(drain ?
173 (volatile const void *)&lkp->lk_flags :
174 (volatile const void *)lkp, lkp->lk_prio,
175 lkp->lk_wmesg, lkp->lk_timo,
176 __UNVOLATILE(&lkp->lk_interlock));
177 LOCKSTAT_STOP_TIMER(lsflag, slptime);
178 LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
179 LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
180 if (!drain) {
181 lkp->lk_waitcount--;
182 if (lkp->lk_waitcount == 0)
183 lkp->lk_flags &= ~LK_WAIT_NONZERO;
184 }
185 if (error)
186 break;
187 if (extflags & LK_SLEEPFAIL) {
188 error = ENOLCK;
189 break;
190 }
191 if (lkp->lk_newlock != NULL) {
192 mutex_enter(__UNVOLATILE
193 (&lkp->lk_newlock->lk_interlock));
194 mutex_exit(__UNVOLATILE
195 (&lkp->lk_interlock));
196 if (lkp->lk_waitcount == 0)
197 wakeup(&lkp->lk_newlock);
198 *lkpp = lkp = lkp->lk_newlock;
199 }
200 }
201
202 LOCKSTAT_EXIT(lsflag);
203
204 return error;
205 }
206
207 #define SETHOLDER(lkp, pid, lid, cpu_id) \
208 do { \
209 (lkp)->lk_lockholder = pid; \
210 (lkp)->lk_locklwp = lid; \
211 } while (/*CONSTCOND*/0)
212
213 #define WEHOLDIT(lkp, pid, lid, cpu_id) \
214 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
215
216 #define WAKEUP_WAITER(lkp) \
217 do { \
218 if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) { \
219 wakeup((lkp)); \
220 } \
221 } while (/*CONSTCOND*/0)
222
223 #if defined(LOCKDEBUG)
224 /*
225 * Lock debug printing routine; can be configured to print to console
226 * or log to syslog.
227 */
228 void
229 lock_printf(const char *fmt, ...)
230 {
231 char b[150];
232 va_list ap;
233
234 va_start(ap, fmt);
235 if (lock_debug_syslog)
236 vlog(LOG_DEBUG, fmt, ap);
237 else {
238 vsnprintf(b, sizeof(b), fmt, ap);
239 printf_nolog("%s", b);
240 }
241 va_end(ap);
242 }
243 #endif /* LOCKDEBUG */
244
245 static void
246 lockpanic(volatile struct lock *lkp, const char *fmt, ...)
247 {
248 char s[150], b[150];
249 #ifdef LOCKDEBUG
250 static const char *locktype[] = {
251 "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
252 "downgrade", "release", "drain", "exclother", "*9*",
253 "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
254 };
255 #endif
256
257 va_list ap;
258 va_start(ap, fmt);
259 vsnprintf(s, sizeof(s), fmt, ap);
260 va_end(ap);
261 bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
262 panic("%s ("
263 #ifdef LOCKDEBUG
264 "type %s "
265 #endif
266 "flags %s, sharecount %d, exclusivecount %d, "
267 "recurselevel %d, waitcount %d, wmesg %s"
268 #ifdef LOCKDEBUG
269 ", lock_file %s, unlock_file %s, lock_line %d, unlock_line %d"
270 #endif
271 ")\n",
272 s,
273 #ifdef LOCKDEBUG
274 locktype[lkp->lk_flags & LK_TYPE_MASK],
275 #endif
276 b, lkp->lk_sharecount, lkp->lk_exclusivecount,
277 lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg
278 #ifdef LOCKDEBUG
279 , lkp->lk_lock_file, lkp->lk_unlock_file, lkp->lk_lock_line,
280 lkp->lk_unlock_line
281 #endif
282 );
283 }
284
285 /*
286 * Transfer any waiting processes from one lock to another.
287 */
288 void
289 transferlockers(struct lock *from, struct lock *to)
290 {
291
292 KASSERT(from != to);
293 KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
294 if (from->lk_waitcount == 0)
295 return;
296 from->lk_newlock = to;
297 wakeup((void *)from);
298 tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
299 from->lk_newlock = NULL;
300 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
301 KASSERT(from->lk_waitcount == 0);
302 }
303
304
305 /*
306 * Initialize a lock; required before use.
307 */
308 void
309 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
310 {
311
312 memset(lkp, 0, sizeof(struct lock));
313 lkp->lk_flags = flags & LK_EXTFLG_MASK;
314 mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
315 lkp->lk_lockholder = LK_NOPROC;
316 lkp->lk_newlock = NULL;
317 lkp->lk_prio = prio;
318 lkp->lk_timo = timo;
319 lkp->lk_wmesg = wmesg;
320 #if defined(LOCKDEBUG)
321 lkp->lk_lock_file = NULL;
322 lkp->lk_unlock_file = NULL;
323 #endif
324 }
325
326 /*
327 * Determine the status of a lock.
328 */
329 int
330 lockstatus(struct lock *lkp)
331 {
332 int lock_type = 0;
333 struct lwp *l = curlwp; /* XXX */
334 pid_t pid;
335 lwpid_t lid;
336 cpuid_t cpu_num;
337
338 if (l == NULL) {
339 cpu_num = cpu_number();
340 pid = LK_KERNPROC;
341 lid = 0;
342 } else {
343 cpu_num = LK_NOCPU;
344 pid = l->l_proc->p_pid;
345 lid = l->l_lid;
346 }
347
348 mutex_enter(&lkp->lk_interlock);
349 if (lkp->lk_exclusivecount != 0) {
350 if (WEHOLDIT(lkp, pid, lid, cpu_num))
351 lock_type = LK_EXCLUSIVE;
352 else
353 lock_type = LK_EXCLOTHER;
354 } else if (lkp->lk_sharecount != 0)
355 lock_type = LK_SHARED;
356 else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
357 lock_type = LK_EXCLOTHER;
358 mutex_exit(__UNVOLATILE(&lkp->lk_interlock));
359 return (lock_type);
360 }
361
362 /*
363 * XXX XXX kludge around another kludge..
364 *
365 * vfs_shutdown() may be called from interrupt context, either as a result
366 * of a panic, or from the debugger. It proceeds to call
367 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
368 *
369 * We would like to make an attempt to sync the filesystems in this case, so
370 * if this happens, we treat attempts to acquire locks specially.
371 * All locks are acquired on behalf of proc0.
372 *
373 * If we've already paniced, we don't block waiting for locks, but
374 * just barge right ahead since we're already going down in flames.
375 */
376
377 /*
378 * Set, change, or release a lock.
379 *
380 * Shared requests increment the shared count. Exclusive requests set the
381 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
382 * accepted shared locks and shared-to-exclusive upgrades to go away.
383 */
384 int
385 #if defined(LOCKDEBUG)
386 _lockmgr(volatile struct lock *lkp, u_int flags,
387 kmutex_t *interlkp, const char *file, int line)
388 #else
389 lockmgr(volatile struct lock *lkp, u_int flags,
390 kmutex_t *interlkp)
391 #endif
392 {
393 int error;
394 pid_t pid;
395 lwpid_t lid;
396 int extflags;
397 cpuid_t cpu_num;
398 struct lwp *l = curlwp;
399 int lock_shutdown_noblock = 0;
400 kmutex_t *mutex;
401 int s = 0;
402
403 error = 0;
404 mutex = __UNVOLATILE(&lkp->lk_interlock);
405
406 /* LK_RETRY is for vn_lock, not for lockmgr. */
407 KASSERT((flags & LK_RETRY) == 0);
408
409 mutex_enter(mutex);
410 if (flags & LK_INTERLOCK)
411 mutex_exit(__UNVOLATILE(interlkp));
412 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
413
414 if (l == NULL) {
415 if (!doing_shutdown) {
416 panic("lockmgr: no context");
417 } else {
418 l = &lwp0;
419 if (panicstr && (!(flags & LK_NOWAIT))) {
420 flags |= LK_NOWAIT;
421 lock_shutdown_noblock = 1;
422 }
423 }
424 }
425 lid = l->l_lid;
426 pid = l->l_proc->p_pid;
427 cpu_num = cpu_number();
428
429 /*
430 * Once a lock has drained, the LK_DRAINING flag is set and an
431 * exclusive lock is returned. The only valid operation thereafter
432 * is a single release of that exclusive lock. This final release
433 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
434 * further requests of any sort will result in a panic. The bits
435 * selected for these two flags are chosen so that they will be set
436 * in memory that is freed (freed memory is filled with 0xdeadbeef).
437 * The final release is permitted to give a new lease on life to
438 * the lock by specifying LK_REENABLE.
439 */
440 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
441 #ifdef DIAGNOSTIC /* { */
442 if (lkp->lk_flags & LK_DRAINED)
443 lockpanic(lkp, "lockmgr: using decommissioned lock");
444 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
445 WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
446 lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
447 flags & LK_TYPE_MASK);
448 #endif /* DIAGNOSTIC */ /* } */
449 lkp->lk_flags &= ~LK_DRAINING;
450 if ((flags & LK_REENABLE) == 0)
451 lkp->lk_flags |= LK_DRAINED;
452 }
453
454 switch (flags & LK_TYPE_MASK) {
455
456 case LK_SHARED:
457 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
458 /*
459 * If just polling, check to see if we will block.
460 */
461 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
462 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
463 error = EBUSY;
464 break;
465 }
466 /*
467 * Wait for exclusive locks and upgrades to clear.
468 */
469 error = acquire(&lkp, &s, extflags, 0,
470 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
471 RETURN_ADDRESS);
472 if (error)
473 break;
474 lkp->lk_sharecount++;
475 lkp->lk_flags |= LK_SHARE_NONZERO;
476 COUNT(lkp, l, cpu_num, 1);
477 break;
478 }
479 /*
480 * We hold an exclusive lock, so downgrade it to shared.
481 * An alternative would be to fail with EDEADLK.
482 */
483 lkp->lk_sharecount++;
484 lkp->lk_flags |= LK_SHARE_NONZERO;
485 COUNT(lkp, l, cpu_num, 1);
486 /* fall into downgrade */
487
488 case LK_DOWNGRADE:
489 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
490 lkp->lk_exclusivecount == 0)
491 lockpanic(lkp, "lockmgr: not holding exclusive lock");
492 lkp->lk_sharecount += lkp->lk_exclusivecount;
493 lkp->lk_flags |= LK_SHARE_NONZERO;
494 lkp->lk_exclusivecount = 0;
495 lkp->lk_recurselevel = 0;
496 lkp->lk_flags &= ~LK_HAVE_EXCL;
497 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
498 #if defined(LOCKDEBUG)
499 lkp->lk_unlock_file = file;
500 lkp->lk_unlock_line = line;
501 #endif
502 WAKEUP_WAITER(lkp);
503 break;
504
505 case LK_EXCLUPGRADE:
506 /*
507 * If another process is ahead of us to get an upgrade,
508 * then we want to fail rather than have an intervening
509 * exclusive access.
510 */
511 if (lkp->lk_flags & LK_WANT_UPGRADE) {
512 lkp->lk_sharecount--;
513 if (lkp->lk_sharecount == 0)
514 lkp->lk_flags &= ~LK_SHARE_NONZERO;
515 COUNT(lkp, l, cpu_num, -1);
516 error = EBUSY;
517 break;
518 }
519 /* fall into normal upgrade */
520
521 case LK_UPGRADE:
522 /*
523 * Upgrade a shared lock to an exclusive one. If another
524 * shared lock has already requested an upgrade to an
525 * exclusive lock, our shared lock is released and an
526 * exclusive lock is requested (which will be granted
527 * after the upgrade). If we return an error, the file
528 * will always be unlocked.
529 */
530 if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
531 lockpanic(lkp, "lockmgr: upgrade exclusive lock");
532 lkp->lk_sharecount--;
533 if (lkp->lk_sharecount == 0)
534 lkp->lk_flags &= ~LK_SHARE_NONZERO;
535 COUNT(lkp, l, cpu_num, -1);
536 /*
537 * If we are just polling, check to see if we will block.
538 */
539 if ((extflags & LK_NOWAIT) &&
540 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
541 lkp->lk_sharecount > 1)) {
542 error = EBUSY;
543 break;
544 }
545 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
546 /*
547 * We are first shared lock to request an upgrade, so
548 * request upgrade and wait for the shared count to
549 * drop to zero, then take exclusive lock.
550 */
551 lkp->lk_flags |= LK_WANT_UPGRADE;
552 error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
553 RETURN_ADDRESS);
554 lkp->lk_flags &= ~LK_WANT_UPGRADE;
555 if (error) {
556 WAKEUP_WAITER(lkp);
557 break;
558 }
559 lkp->lk_flags |= LK_HAVE_EXCL;
560 SETHOLDER(lkp, pid, lid, cpu_num);
561 #if defined(LOCKDEBUG)
562 lkp->lk_lock_file = file;
563 lkp->lk_lock_line = line;
564 #endif
565 if (lkp->lk_exclusivecount != 0)
566 lockpanic(lkp, "lockmgr: non-zero exclusive count");
567 lkp->lk_exclusivecount = 1;
568 if (extflags & LK_SETRECURSE)
569 lkp->lk_recurselevel = 1;
570 COUNT(lkp, l, cpu_num, 1);
571 break;
572 }
573 /*
574 * Someone else has requested upgrade. Release our shared
575 * lock, awaken upgrade requestor if we are the last shared
576 * lock, then request an exclusive lock.
577 */
578 if (lkp->lk_sharecount == 0)
579 WAKEUP_WAITER(lkp);
580 /* fall into exclusive request */
581
582 case LK_EXCLUSIVE:
583 if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
584 /*
585 * Recursive lock.
586 */
587 if ((extflags & LK_CANRECURSE) == 0 &&
588 lkp->lk_recurselevel == 0) {
589 if (extflags & LK_RECURSEFAIL) {
590 error = EDEADLK;
591 break;
592 } else
593 lockpanic(lkp, "lockmgr: locking against myself");
594 }
595 lkp->lk_exclusivecount++;
596 if (extflags & LK_SETRECURSE &&
597 lkp->lk_recurselevel == 0)
598 lkp->lk_recurselevel = lkp->lk_exclusivecount;
599 COUNT(lkp, l, cpu_num, 1);
600 break;
601 }
602 /*
603 * If we are just polling, check to see if we will sleep.
604 */
605 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
606 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
607 LK_SHARE_NONZERO))) {
608 error = EBUSY;
609 break;
610 }
611 /*
612 * Try to acquire the want_exclusive flag.
613 */
614 error = acquire(&lkp, &s, extflags, 0,
615 LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
616 if (error)
617 break;
618 lkp->lk_flags |= LK_WANT_EXCL;
619 /*
620 * Wait for shared locks and upgrades to finish.
621 */
622 error = acquire(&lkp, &s, extflags, 0,
623 LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
624 RETURN_ADDRESS);
625 lkp->lk_flags &= ~LK_WANT_EXCL;
626 if (error) {
627 WAKEUP_WAITER(lkp);
628 break;
629 }
630 lkp->lk_flags |= LK_HAVE_EXCL;
631 SETHOLDER(lkp, pid, lid, cpu_num);
632 #if defined(LOCKDEBUG)
633 lkp->lk_lock_file = file;
634 lkp->lk_lock_line = line;
635 #endif
636 if (lkp->lk_exclusivecount != 0)
637 lockpanic(lkp, "lockmgr: non-zero exclusive count");
638 lkp->lk_exclusivecount = 1;
639 if (extflags & LK_SETRECURSE)
640 lkp->lk_recurselevel = 1;
641 COUNT(lkp, l, cpu_num, 1);
642 break;
643
644 case LK_RELEASE:
645 if (lkp->lk_exclusivecount != 0) {
646 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
647 lockpanic(lkp, "lockmgr: pid %d, not "
648 "exclusive lock holder %d "
649 "unlocking", pid,
650 lkp->lk_lockholder);
651 }
652 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
653 lkp->lk_recurselevel = 0;
654 lkp->lk_exclusivecount--;
655 COUNT(lkp, l, cpu_num, -1);
656 if (lkp->lk_exclusivecount == 0) {
657 lkp->lk_flags &= ~LK_HAVE_EXCL;
658 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
659 #if defined(LOCKDEBUG)
660 lkp->lk_unlock_file = file;
661 lkp->lk_unlock_line = line;
662 #endif
663 }
664 } else if (lkp->lk_sharecount != 0) {
665 lkp->lk_sharecount--;
666 if (lkp->lk_sharecount == 0)
667 lkp->lk_flags &= ~LK_SHARE_NONZERO;
668 COUNT(lkp, l, cpu_num, -1);
669 }
670 #ifdef DIAGNOSTIC
671 else
672 lockpanic(lkp, "lockmgr: release of unlocked lock!");
673 #endif
674 WAKEUP_WAITER(lkp);
675 break;
676
677 case LK_DRAIN:
678 /*
679 * Check that we do not already hold the lock, as it can
680 * never drain if we do. Unfortunately, we have no way to
681 * check for holding a shared lock, but at least we can
682 * check for an exclusive one.
683 */
684 if (WEHOLDIT(lkp, pid, lid, cpu_num))
685 lockpanic(lkp, "lockmgr: draining against myself");
686 /*
687 * If we are just polling, check to see if we will sleep.
688 */
689 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
690 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
691 LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
692 error = EBUSY;
693 break;
694 }
695 error = acquire(&lkp, &s, extflags, 1,
696 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
697 LK_SHARE_NONZERO | LK_WAIT_NONZERO,
698 RETURN_ADDRESS);
699 if (error)
700 break;
701 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
702 SETHOLDER(lkp, pid, lid, cpu_num);
703 #if defined(LOCKDEBUG)
704 lkp->lk_lock_file = file;
705 lkp->lk_lock_line = line;
706 #endif
707 lkp->lk_exclusivecount = 1;
708 /* XXX unlikely that we'd want this */
709 if (extflags & LK_SETRECURSE)
710 lkp->lk_recurselevel = 1;
711 COUNT(lkp, l, cpu_num, 1);
712 break;
713
714 default:
715 mutex_exit(mutex);
716 lockpanic(lkp, "lockmgr: unknown locktype request %d",
717 flags & LK_TYPE_MASK);
718 /* NOTREACHED */
719 }
720 if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
721 ((lkp->lk_flags &
722 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
723 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
724 lkp->lk_flags &= ~LK_WAITDRAIN;
725 wakeup(&lkp->lk_flags);
726 }
727 /*
728 * Note that this panic will be a recursive panic, since
729 * we only set lock_shutdown_noblock above if panicstr != NULL.
730 */
731 if (error && lock_shutdown_noblock)
732 lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
733
734 mutex_exit(mutex);
735 return (error);
736 }
737
738 /*
739 * Print out information about state of a lock. Used by VOP_PRINT
740 * routines to display ststus about contained locks.
741 */
742 void
743 lockmgr_printinfo(volatile struct lock *lkp)
744 {
745
746 if (lkp->lk_sharecount)
747 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
748 lkp->lk_sharecount);
749 else if (lkp->lk_flags & LK_HAVE_EXCL) {
750 printf(" lock type %s: EXCL (count %d) by ",
751 lkp->lk_wmesg, lkp->lk_exclusivecount);
752 printf("pid %d.%d", lkp->lk_lockholder,
753 lkp->lk_locklwp);
754 } else
755 printf(" not locked");
756 if (lkp->lk_waitcount > 0)
757 printf(" with %d pending", lkp->lk_waitcount);
758 }
759
760 #if defined(LOCKDEBUG)
761 void
762 assert_sleepable(struct simplelock *interlock, const char *msg)
763 {
764
765 LOCKDEBUG_BARRIER(&kernel_lock, 1);
766 if (curlwp == NULL) {
767 panic("assert_sleepable: NULL curlwp");
768 }
769 }
770 #endif
771
772 #if defined(MULTIPROCESSOR)
773
774 /*
775 * Functions for manipulating the kernel_lock. We put them here
776 * so that they show up in profiles.
777 */
778
779 #define _KERNEL_LOCK_ABORT(msg) \
780 LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops, \
781 __FUNCTION__, msg)
782
783 #ifdef LOCKDEBUG
784 #define _KERNEL_LOCK_ASSERT(cond) \
785 do { \
786 if (!(cond)) \
787 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
788 } while (/* CONSTCOND */ 0)
789 #else
790 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
791 #endif
792
793 void _kernel_lock_dump(volatile void *);
794
795 lockops_t _kernel_lock_ops = {
796 "Kernel lock",
797 0,
798 _kernel_lock_dump
799 };
800
801 /*
802 * Initialize the kernel lock.
803 */
804 void
805 _kernel_lock_init(void)
806 {
807
808 __cpu_simple_lock_init(&kernel_lock);
809 kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
810 }
811
812 /*
813 * Print debugging information about the kernel lock.
814 */
815 void
816 _kernel_lock_dump(volatile void *junk)
817 {
818 struct cpu_info *ci = curcpu();
819
820 (void)junk;
821
822 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
823 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
824 }
825
826 /*
827 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
828 * acquisition is from process context.
829 */
830 void
831 _kernel_lock(int nlocks, struct lwp *l)
832 {
833 struct cpu_info *ci = curcpu();
834 LOCKSTAT_TIMER(spintime);
835 LOCKSTAT_FLAG(lsflag);
836 struct lwp *owant;
837 #ifdef LOCKDEBUG
838 u_int spins;
839 #endif
840 int s;
841
842 (void)l;
843
844 if (nlocks == 0)
845 return;
846 _KERNEL_LOCK_ASSERT(nlocks > 0);
847
848 s = splbiglock();
849
850 if (ci->ci_biglock_count != 0) {
851 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
852 ci->ci_biglock_count += nlocks;
853 splx(s);
854 return;
855 }
856
857 LOCKDEBUG_WANTLOCK(kernel_lock_id,
858 (uintptr_t)__builtin_return_address(0), 0);
859
860 if (__cpu_simple_lock_try(&kernel_lock)) {
861 ci->ci_biglock_count = nlocks;
862 LOCKDEBUG_LOCKED(kernel_lock_id,
863 (uintptr_t)__builtin_return_address(0), 0);
864 splx(s);
865 return;
866 }
867
868 LOCKSTAT_ENTER(lsflag);
869 LOCKSTAT_START_TIMER(lsflag, spintime);
870
871 /*
872 * Before setting ci_biglock_wanted we must post a store
873 * fence (see kern_mutex.c). This is accomplished by the
874 * __cpu_simple_lock_try() above.
875 */
876 owant = ci->ci_biglock_wanted;
877 ci->ci_biglock_wanted = curlwp; /* XXXAD */
878
879 #ifdef LOCKDEBUG
880 spins = 0;
881 #endif
882
883 do {
884 while (kernel_lock == __SIMPLELOCK_LOCKED) {
885 #ifdef LOCKDEBUG
886 if (SPINLOCK_SPINOUT(spins))
887 _KERNEL_LOCK_ABORT("spinout");
888 #endif
889 splx(s);
890 SPINLOCK_SPIN_HOOK;
891 (void)splbiglock();
892 }
893 } while (!__cpu_simple_lock_try(&kernel_lock));
894
895 ci->ci_biglock_wanted = owant;
896 ci->ci_biglock_count += nlocks;
897 LOCKSTAT_STOP_TIMER(lsflag, spintime);
898 LOCKDEBUG_LOCKED(kernel_lock_id,
899 (uintptr_t)__builtin_return_address(0), 0);
900 splx(s);
901
902 /*
903 * Again, another store fence is required (see kern_mutex.c).
904 */
905 mb_write();
906 if (owant == NULL) {
907 LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
908 1, spintime);
909 }
910 LOCKSTAT_EXIT(lsflag);
911 }
912
913 /*
914 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
915 * all holds. If 'l' is non-null, the release is from process context.
916 */
917 void
918 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
919 {
920 struct cpu_info *ci = curcpu();
921 u_int olocks;
922 int s;
923
924 (void)l;
925
926 _KERNEL_LOCK_ASSERT(nlocks < 2);
927
928 olocks = ci->ci_biglock_count;
929
930 if (olocks == 0) {
931 _KERNEL_LOCK_ASSERT(nlocks <= 0);
932 if (countp != NULL)
933 *countp = 0;
934 return;
935 }
936
937 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
938
939 if (nlocks == 0)
940 nlocks = olocks;
941 else if (nlocks == -1) {
942 nlocks = 1;
943 _KERNEL_LOCK_ASSERT(olocks == 1);
944 }
945
946 s = splbiglock();
947 if ((ci->ci_biglock_count -= nlocks) == 0) {
948 LOCKDEBUG_UNLOCKED(kernel_lock_id,
949 (uintptr_t)__builtin_return_address(0), 0);
950 __cpu_simple_unlock(&kernel_lock);
951 }
952 splx(s);
953
954 if (countp != NULL)
955 *countp = olocks;
956 }
957
958 #if defined(DEBUG)
959 /*
960 * Assert that the kernel lock is held.
961 */
962 void
963 _kernel_lock_assert_locked(void)
964 {
965
966 if (kernel_lock != __SIMPLELOCK_LOCKED ||
967 curcpu()->ci_biglock_count == 0)
968 _KERNEL_LOCK_ABORT("not locked");
969 }
970
971 void
972 _kernel_lock_assert_unlocked()
973 {
974
975 if (curcpu()->ci_biglock_count != 0)
976 _KERNEL_LOCK_ABORT("locked");
977 }
978 #endif
979
980 #endif /* MULTIPROCESSOR || LOCKDEBUG */
981