kern_lock.c revision 1.110.2.2 1 /* $NetBSD: kern_lock.c,v 1.110.2.2 2007/03/21 20:10:20 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.2 2007/03/21 20:10:20 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 __cpu_simple_lock_t kernel_lock;
130 int kernel_lock_id;
131 #endif
132
133 /*
134 * Locking primitives implementation.
135 * Locks provide shared/exclusive synchronization.
136 */
137
138 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
139 #define COUNT(lkp, l, cpu_id, x) (l)->l_locks += (x)
140 #else
141 #define COUNT(lkp, p, cpu_id, x)
142 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
143
144 #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
145
146 /*
147 * Acquire a resource.
148 */
149 static int
150 acquire(volatile struct lock **lkpp, int *s, int extflags,
151 int drain, int wanted, uintptr_t ra)
152 {
153 int error;
154 volatile struct lock *lkp = *lkpp;
155 LOCKSTAT_TIMER(slptime);
156 LOCKSTAT_FLAG(lsflag);
157
158 KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
159
160 LOCKSTAT_ENTER(lsflag);
161
162 for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
163 if (drain)
164 lkp->lk_flags |= LK_WAITDRAIN;
165 else {
166 lkp->lk_waitcount++;
167 lkp->lk_flags |= LK_WAIT_NONZERO;
168 }
169 /* XXX Cast away volatile. */
170 LOCKSTAT_START_TIMER(lsflag, slptime);
171 error = mtsleep(drain ?
172 (volatile const void *)&lkp->lk_flags :
173 (volatile const void *)lkp, lkp->lk_prio,
174 lkp->lk_wmesg, lkp->lk_timo,
175 __UNVOLATILE(&lkp->lk_interlock));
176 LOCKSTAT_STOP_TIMER(lsflag, slptime);
177 LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
178 LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
179 if (!drain) {
180 lkp->lk_waitcount--;
181 if (lkp->lk_waitcount == 0)
182 lkp->lk_flags &= ~LK_WAIT_NONZERO;
183 }
184 if (error)
185 break;
186 if (extflags & LK_SLEEPFAIL) {
187 error = ENOLCK;
188 break;
189 }
190 if (lkp->lk_newlock != NULL) {
191 mutex_enter(__UNVOLATILE
192 (&lkp->lk_newlock->lk_interlock));
193 mutex_exit(__UNVOLATILE
194 (&lkp->lk_interlock));
195 if (lkp->lk_waitcount == 0)
196 wakeup(&lkp->lk_newlock);
197 *lkpp = lkp = lkp->lk_newlock;
198 }
199 }
200
201 LOCKSTAT_EXIT(lsflag);
202
203 return error;
204 }
205
206 #define SETHOLDER(lkp, pid, lid, cpu_id) \
207 do { \
208 (lkp)->lk_lockholder = pid; \
209 (lkp)->lk_locklwp = lid; \
210 } while (/*CONSTCOND*/0)
211
212 #define WEHOLDIT(lkp, pid, lid, cpu_id) \
213 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
214
215 #define WAKEUP_WAITER(lkp) \
216 do { \
217 if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) { \
218 wakeup((lkp)); \
219 } \
220 } while (/*CONSTCOND*/0)
221
222 #if defined(LOCKDEBUG)
223 /*
224 * Lock debug printing routine; can be configured to print to console
225 * or log to syslog.
226 */
227 void
228 lock_printf(const char *fmt, ...)
229 {
230 char b[150];
231 va_list ap;
232
233 va_start(ap, fmt);
234 if (lock_debug_syslog)
235 vlog(LOG_DEBUG, fmt, ap);
236 else {
237 vsnprintf(b, sizeof(b), fmt, ap);
238 printf_nolog("%s", b);
239 }
240 va_end(ap);
241 }
242 #endif /* LOCKDEBUG */
243
244 static void
245 lockpanic(volatile struct lock *lkp, const char *fmt, ...)
246 {
247 char s[150], b[150];
248 #ifdef LOCKDEBUG
249 static const char *locktype[] = {
250 "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
251 "downgrade", "release", "drain", "exclother", "*9*",
252 "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
253 };
254 #endif
255
256 va_list ap;
257 va_start(ap, fmt);
258 vsnprintf(s, sizeof(s), fmt, ap);
259 va_end(ap);
260 bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
261 panic("%s ("
262 #ifdef LOCKDEBUG
263 "type %s "
264 #endif
265 "flags %s, sharecount %d, exclusivecount %d, "
266 "recurselevel %d, waitcount %d, wmesg %s"
267 #ifdef LOCKDEBUG
268 ", lock_file %s, unlock_file %s, lock_line %d, unlock_line %d"
269 #endif
270 ")\n",
271 s,
272 #ifdef LOCKDEBUG
273 locktype[lkp->lk_flags & LK_TYPE_MASK],
274 #endif
275 b, lkp->lk_sharecount, lkp->lk_exclusivecount,
276 lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg
277 #ifdef LOCKDEBUG
278 , lkp->lk_lock_file, lkp->lk_unlock_file, lkp->lk_lock_line,
279 lkp->lk_unlock_line
280 #endif
281 );
282 }
283
284 /*
285 * Transfer any waiting processes from one lock to another.
286 */
287 void
288 transferlockers(struct lock *from, struct lock *to)
289 {
290
291 KASSERT(from != to);
292 KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
293 if (from->lk_waitcount == 0)
294 return;
295 from->lk_newlock = to;
296 wakeup((void *)from);
297 tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
298 from->lk_newlock = NULL;
299 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
300 KASSERT(from->lk_waitcount == 0);
301 }
302
303
304 /*
305 * Initialize a lock; required before use.
306 */
307 void
308 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
309 {
310
311 memset(lkp, 0, sizeof(struct lock));
312 lkp->lk_flags = flags & LK_EXTFLG_MASK;
313 mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
314 lkp->lk_lockholder = LK_NOPROC;
315 lkp->lk_newlock = NULL;
316 lkp->lk_prio = prio;
317 lkp->lk_timo = timo;
318 lkp->lk_wmesg = wmesg;
319 #if defined(LOCKDEBUG)
320 lkp->lk_lock_file = NULL;
321 lkp->lk_unlock_file = NULL;
322 #endif
323 }
324
325 /*
326 * Determine the status of a lock.
327 */
328 int
329 lockstatus(struct lock *lkp)
330 {
331 int lock_type = 0;
332 struct lwp *l = curlwp; /* XXX */
333 pid_t pid;
334 lwpid_t lid;
335 cpuid_t cpu_num;
336
337 if (l == NULL) {
338 cpu_num = cpu_number();
339 pid = LK_KERNPROC;
340 lid = 0;
341 } else {
342 cpu_num = LK_NOCPU;
343 pid = l->l_proc->p_pid;
344 lid = l->l_lid;
345 }
346
347 mutex_enter(&lkp->lk_interlock);
348 if (lkp->lk_exclusivecount != 0) {
349 if (WEHOLDIT(lkp, pid, lid, cpu_num))
350 lock_type = LK_EXCLUSIVE;
351 else
352 lock_type = LK_EXCLOTHER;
353 } else if (lkp->lk_sharecount != 0)
354 lock_type = LK_SHARED;
355 else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
356 lock_type = LK_EXCLOTHER;
357 mutex_exit(__UNVOLATILE(&lkp->lk_interlock));
358 return (lock_type);
359 }
360
361 /*
362 * XXX XXX kludge around another kludge..
363 *
364 * vfs_shutdown() may be called from interrupt context, either as a result
365 * of a panic, or from the debugger. It proceeds to call
366 * sys_sync(&proc0, ...), pretending its running on behalf of proc0
367 *
368 * We would like to make an attempt to sync the filesystems in this case, so
369 * if this happens, we treat attempts to acquire locks specially.
370 * All locks are acquired on behalf of proc0.
371 *
372 * If we've already paniced, we don't block waiting for locks, but
373 * just barge right ahead since we're already going down in flames.
374 */
375
376 /*
377 * Set, change, or release a lock.
378 *
379 * Shared requests increment the shared count. Exclusive requests set the
380 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
381 * accepted shared locks and shared-to-exclusive upgrades to go away.
382 */
383 int
384 #if defined(LOCKDEBUG)
385 _lockmgr(volatile struct lock *lkp, u_int flags,
386 kmutex_t *interlkp, const char *file, int line)
387 #else
388 lockmgr(volatile struct lock *lkp, u_int flags,
389 kmutex_t *interlkp)
390 #endif
391 {
392 int error;
393 pid_t pid;
394 lwpid_t lid;
395 int extflags;
396 cpuid_t cpu_num;
397 struct lwp *l = curlwp;
398 int lock_shutdown_noblock = 0;
399 kmutex_t *mutex;
400 int s = 0;
401
402 error = 0;
403 mutex = __UNVOLATILE(&lkp->lk_interlock);
404
405 /* LK_RETRY is for vn_lock, not for lockmgr. */
406 KASSERT((flags & LK_RETRY) == 0);
407
408 mutex_enter(mutex);
409 if (flags & LK_INTERLOCK)
410 mutex_exit(__UNVOLATILE(interlkp));
411 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
412
413 if (l == NULL) {
414 if (!doing_shutdown) {
415 panic("lockmgr: no context");
416 } else {
417 l = &lwp0;
418 if (panicstr && (!(flags & LK_NOWAIT))) {
419 flags |= LK_NOWAIT;
420 lock_shutdown_noblock = 1;
421 }
422 }
423 }
424 lid = l->l_lid;
425 pid = l->l_proc->p_pid;
426 cpu_num = cpu_number();
427
428 /*
429 * Once a lock has drained, the LK_DRAINING flag is set and an
430 * exclusive lock is returned. The only valid operation thereafter
431 * is a single release of that exclusive lock. This final release
432 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
433 * further requests of any sort will result in a panic. The bits
434 * selected for these two flags are chosen so that they will be set
435 * in memory that is freed (freed memory is filled with 0xdeadbeef).
436 * The final release is permitted to give a new lease on life to
437 * the lock by specifying LK_REENABLE.
438 */
439 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
440 #ifdef DIAGNOSTIC /* { */
441 if (lkp->lk_flags & LK_DRAINED)
442 lockpanic(lkp, "lockmgr: using decommissioned lock");
443 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
444 WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
445 lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
446 flags & LK_TYPE_MASK);
447 #endif /* DIAGNOSTIC */ /* } */
448 lkp->lk_flags &= ~LK_DRAINING;
449 if ((flags & LK_REENABLE) == 0)
450 lkp->lk_flags |= LK_DRAINED;
451 }
452
453 switch (flags & LK_TYPE_MASK) {
454
455 case LK_SHARED:
456 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
457 /*
458 * If just polling, check to see if we will block.
459 */
460 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
461 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
462 error = EBUSY;
463 break;
464 }
465 /*
466 * Wait for exclusive locks and upgrades to clear.
467 */
468 error = acquire(&lkp, &s, extflags, 0,
469 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
470 RETURN_ADDRESS);
471 if (error)
472 break;
473 lkp->lk_sharecount++;
474 lkp->lk_flags |= LK_SHARE_NONZERO;
475 COUNT(lkp, l, cpu_num, 1);
476 break;
477 }
478 /*
479 * We hold an exclusive lock, so downgrade it to shared.
480 * An alternative would be to fail with EDEADLK.
481 */
482 lkp->lk_sharecount++;
483 lkp->lk_flags |= LK_SHARE_NONZERO;
484 COUNT(lkp, l, cpu_num, 1);
485 /* fall into downgrade */
486
487 case LK_DOWNGRADE:
488 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
489 lkp->lk_exclusivecount == 0)
490 lockpanic(lkp, "lockmgr: not holding exclusive lock");
491 lkp->lk_sharecount += lkp->lk_exclusivecount;
492 lkp->lk_flags |= LK_SHARE_NONZERO;
493 lkp->lk_exclusivecount = 0;
494 lkp->lk_recurselevel = 0;
495 lkp->lk_flags &= ~LK_HAVE_EXCL;
496 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
497 #if defined(LOCKDEBUG)
498 lkp->lk_unlock_file = file;
499 lkp->lk_unlock_line = line;
500 #endif
501 WAKEUP_WAITER(lkp);
502 break;
503
504 case LK_EXCLUPGRADE:
505 /*
506 * If another process is ahead of us to get an upgrade,
507 * then we want to fail rather than have an intervening
508 * exclusive access.
509 */
510 if (lkp->lk_flags & LK_WANT_UPGRADE) {
511 lkp->lk_sharecount--;
512 if (lkp->lk_sharecount == 0)
513 lkp->lk_flags &= ~LK_SHARE_NONZERO;
514 COUNT(lkp, l, cpu_num, -1);
515 error = EBUSY;
516 break;
517 }
518 /* fall into normal upgrade */
519
520 case LK_UPGRADE:
521 /*
522 * Upgrade a shared lock to an exclusive one. If another
523 * shared lock has already requested an upgrade to an
524 * exclusive lock, our shared lock is released and an
525 * exclusive lock is requested (which will be granted
526 * after the upgrade). If we return an error, the file
527 * will always be unlocked.
528 */
529 if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
530 lockpanic(lkp, "lockmgr: upgrade exclusive lock");
531 lkp->lk_sharecount--;
532 if (lkp->lk_sharecount == 0)
533 lkp->lk_flags &= ~LK_SHARE_NONZERO;
534 COUNT(lkp, l, cpu_num, -1);
535 /*
536 * If we are just polling, check to see if we will block.
537 */
538 if ((extflags & LK_NOWAIT) &&
539 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
540 lkp->lk_sharecount > 1)) {
541 error = EBUSY;
542 break;
543 }
544 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
545 /*
546 * We are first shared lock to request an upgrade, so
547 * request upgrade and wait for the shared count to
548 * drop to zero, then take exclusive lock.
549 */
550 lkp->lk_flags |= LK_WANT_UPGRADE;
551 error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
552 RETURN_ADDRESS);
553 lkp->lk_flags &= ~LK_WANT_UPGRADE;
554 if (error) {
555 WAKEUP_WAITER(lkp);
556 break;
557 }
558 lkp->lk_flags |= LK_HAVE_EXCL;
559 SETHOLDER(lkp, pid, lid, cpu_num);
560 #if defined(LOCKDEBUG)
561 lkp->lk_lock_file = file;
562 lkp->lk_lock_line = line;
563 #endif
564 if (lkp->lk_exclusivecount != 0)
565 lockpanic(lkp, "lockmgr: non-zero exclusive count");
566 lkp->lk_exclusivecount = 1;
567 if (extflags & LK_SETRECURSE)
568 lkp->lk_recurselevel = 1;
569 COUNT(lkp, l, cpu_num, 1);
570 break;
571 }
572 /*
573 * Someone else has requested upgrade. Release our shared
574 * lock, awaken upgrade requestor if we are the last shared
575 * lock, then request an exclusive lock.
576 */
577 if (lkp->lk_sharecount == 0)
578 WAKEUP_WAITER(lkp);
579 /* fall into exclusive request */
580
581 case LK_EXCLUSIVE:
582 if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
583 /*
584 * Recursive lock.
585 */
586 if ((extflags & LK_CANRECURSE) == 0 &&
587 lkp->lk_recurselevel == 0) {
588 if (extflags & LK_RECURSEFAIL) {
589 error = EDEADLK;
590 break;
591 } else
592 lockpanic(lkp, "lockmgr: locking against myself");
593 }
594 lkp->lk_exclusivecount++;
595 if (extflags & LK_SETRECURSE &&
596 lkp->lk_recurselevel == 0)
597 lkp->lk_recurselevel = lkp->lk_exclusivecount;
598 COUNT(lkp, l, cpu_num, 1);
599 break;
600 }
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 LK_SHARE_NONZERO))) {
607 error = EBUSY;
608 break;
609 }
610 /*
611 * Try to acquire the want_exclusive flag.
612 */
613 error = acquire(&lkp, &s, extflags, 0,
614 LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
615 if (error)
616 break;
617 lkp->lk_flags |= LK_WANT_EXCL;
618 /*
619 * Wait for shared locks and upgrades to finish.
620 */
621 error = acquire(&lkp, &s, extflags, 0,
622 LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
623 RETURN_ADDRESS);
624 lkp->lk_flags &= ~LK_WANT_EXCL;
625 if (error) {
626 WAKEUP_WAITER(lkp);
627 break;
628 }
629 lkp->lk_flags |= LK_HAVE_EXCL;
630 SETHOLDER(lkp, pid, lid, cpu_num);
631 #if defined(LOCKDEBUG)
632 lkp->lk_lock_file = file;
633 lkp->lk_lock_line = line;
634 #endif
635 if (lkp->lk_exclusivecount != 0)
636 lockpanic(lkp, "lockmgr: non-zero exclusive count");
637 lkp->lk_exclusivecount = 1;
638 if (extflags & LK_SETRECURSE)
639 lkp->lk_recurselevel = 1;
640 COUNT(lkp, l, cpu_num, 1);
641 break;
642
643 case LK_RELEASE:
644 if (lkp->lk_exclusivecount != 0) {
645 if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
646 lockpanic(lkp, "lockmgr: pid %d, not "
647 "exclusive lock holder %d "
648 "unlocking", pid,
649 lkp->lk_lockholder);
650 }
651 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
652 lkp->lk_recurselevel = 0;
653 lkp->lk_exclusivecount--;
654 COUNT(lkp, l, cpu_num, -1);
655 if (lkp->lk_exclusivecount == 0) {
656 lkp->lk_flags &= ~LK_HAVE_EXCL;
657 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
658 #if defined(LOCKDEBUG)
659 lkp->lk_unlock_file = file;
660 lkp->lk_unlock_line = line;
661 #endif
662 }
663 } else if (lkp->lk_sharecount != 0) {
664 lkp->lk_sharecount--;
665 if (lkp->lk_sharecount == 0)
666 lkp->lk_flags &= ~LK_SHARE_NONZERO;
667 COUNT(lkp, l, cpu_num, -1);
668 }
669 #ifdef DIAGNOSTIC
670 else
671 lockpanic(lkp, "lockmgr: release of unlocked lock!");
672 #endif
673 WAKEUP_WAITER(lkp);
674 break;
675
676 case LK_DRAIN:
677 /*
678 * Check that we do not already hold the lock, as it can
679 * never drain if we do. Unfortunately, we have no way to
680 * check for holding a shared lock, but at least we can
681 * check for an exclusive one.
682 */
683 if (WEHOLDIT(lkp, pid, lid, cpu_num))
684 lockpanic(lkp, "lockmgr: draining against myself");
685 /*
686 * If we are just polling, check to see if we will sleep.
687 */
688 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
689 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
690 LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
691 error = EBUSY;
692 break;
693 }
694 error = acquire(&lkp, &s, extflags, 1,
695 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
696 LK_SHARE_NONZERO | LK_WAIT_NONZERO,
697 RETURN_ADDRESS);
698 if (error)
699 break;
700 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
701 SETHOLDER(lkp, pid, lid, cpu_num);
702 #if defined(LOCKDEBUG)
703 lkp->lk_lock_file = file;
704 lkp->lk_lock_line = line;
705 #endif
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, l, cpu_num, 1);
711 break;
712
713 default:
714 mutex_exit(mutex);
715 lockpanic(lkp, "lockmgr: unknown locktype request %d",
716 flags & LK_TYPE_MASK);
717 /* NOTREACHED */
718 }
719 if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
720 ((lkp->lk_flags &
721 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
722 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
723 lkp->lk_flags &= ~LK_WAITDRAIN;
724 wakeup(&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 lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
732
733 mutex_exit(mutex);
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 printf("pid %d.%d", lkp->lk_lockholder,
752 lkp->lk_locklwp);
753 } else
754 printf(" not locked");
755 if (lkp->lk_waitcount > 0)
756 printf(" with %d pending", lkp->lk_waitcount);
757 }
758
759 void
760 assert_sleepable(struct simplelock *interlock, const char *msg)
761 {
762
763 if (curlwp == NULL) {
764 panic("assert_sleepable: NULL curlwp");
765 }
766 }
767
768 #if defined(MULTIPROCESSOR)
769
770 /*
771 * Functions for manipulating the kernel_lock. We put them here
772 * so that they show up in profiles.
773 */
774
775 #define _KERNEL_LOCK_ABORT(msg) \
776 LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops, \
777 __FUNCTION__, msg)
778
779 #ifdef LOCKDEBUG
780 #define _KERNEL_LOCK_ASSERT(cond) \
781 do { \
782 if (!(cond)) \
783 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
784 } while (/* CONSTCOND */ 0)
785 #else
786 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
787 #endif
788
789 void _kernel_lock_dump(volatile void *);
790
791 lockops_t _kernel_lock_ops = {
792 "Kernel lock",
793 0,
794 _kernel_lock_dump
795 };
796
797 /*
798 * Initialize the kernel lock.
799 */
800 void
801 _kernel_lock_init(void)
802 {
803
804 __cpu_simple_lock_init(&kernel_lock);
805 kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
806 }
807
808 /*
809 * Print debugging information about the kernel lock.
810 */
811 void
812 _kernel_lock_dump(volatile void *junk)
813 {
814 struct cpu_info *ci = curcpu();
815
816 (void)junk;
817
818 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
819 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
820 }
821
822 /*
823 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
824 * acquisition is from process context.
825 */
826 void
827 _kernel_lock(int nlocks, struct lwp *l)
828 {
829 struct cpu_info *ci = curcpu();
830 LOCKSTAT_TIMER(spintime);
831 LOCKSTAT_FLAG(lsflag);
832 struct lwp *owant;
833 #ifdef LOCKDEBUG
834 u_int spins;
835 #endif
836 int s;
837
838 (void)l;
839
840 if (nlocks == 0)
841 return;
842 _KERNEL_LOCK_ASSERT(nlocks > 0);
843
844 s = splbiglock();
845
846 if (ci->ci_biglock_count != 0) {
847 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
848 ci->ci_biglock_count += nlocks;
849 splx(s);
850 return;
851 }
852
853 LOCKDEBUG_WANTLOCK(kernel_lock_id,
854 (uintptr_t)__builtin_return_address(0), 0);
855
856 if (__cpu_simple_lock_try(&kernel_lock)) {
857 ci->ci_biglock_count = nlocks;
858 LOCKDEBUG_LOCKED(kernel_lock_id,
859 (uintptr_t)__builtin_return_address(0), 0);
860 splx(s);
861 return;
862 }
863
864 LOCKSTAT_ENTER(lsflag);
865 LOCKSTAT_START_TIMER(lsflag, spintime);
866
867 /*
868 * Before setting ci_biglock_wanted we must post a store
869 * fence (see kern_mutex.c). This is accomplished by the
870 * __cpu_simple_lock_try() above.
871 */
872 owant = ci->ci_biglock_wanted;
873 ci->ci_biglock_wanted = curlwp; /* XXXAD */
874
875 #ifdef LOCKDEBUG
876 spins = 0;
877 #endif
878
879 do {
880 while (kernel_lock == __SIMPLELOCK_LOCKED) {
881 #ifdef LOCKDEBUG
882 if (SPINLOCK_SPINOUT(spins))
883 _KERNEL_LOCK_ABORT("spinout");
884 #endif
885 splx(s);
886 SPINLOCK_SPIN_HOOK;
887 (void)splbiglock();
888 }
889 } while (!__cpu_simple_lock_try(&kernel_lock));
890
891 ci->ci_biglock_wanted = owant;
892 ci->ci_biglock_count += nlocks;
893 LOCKSTAT_STOP_TIMER(lsflag, spintime);
894 LOCKDEBUG_LOCKED(kernel_lock_id,
895 (uintptr_t)__builtin_return_address(0), 0);
896 splx(s);
897
898 /*
899 * Again, another store fence is required (see kern_mutex.c).
900 */
901 mb_write();
902 if (owant == NULL) {
903 LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
904 1, spintime);
905 }
906 LOCKSTAT_EXIT(lsflag);
907 }
908
909 /*
910 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
911 * all holds. If 'l' is non-null, the release is from process context.
912 */
913 void
914 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
915 {
916 struct cpu_info *ci = curcpu();
917 u_int olocks;
918 int s;
919
920 (void)l;
921
922 _KERNEL_LOCK_ASSERT(nlocks < 2);
923
924 olocks = ci->ci_biglock_count;
925
926 if (olocks == 0) {
927 _KERNEL_LOCK_ASSERT(nlocks <= 0);
928 if (countp != NULL)
929 *countp = 0;
930 return;
931 }
932
933 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
934
935 if (nlocks == 0)
936 nlocks = olocks;
937 else if (nlocks == -1) {
938 nlocks = 1;
939 _KERNEL_LOCK_ASSERT(olocks == 1);
940 }
941
942 s = splbiglock();
943 if ((ci->ci_biglock_count -= nlocks) == 0) {
944 LOCKDEBUG_UNLOCKED(kernel_lock_id,
945 (uintptr_t)__builtin_return_address(0), 0);
946 __cpu_simple_unlock(&kernel_lock);
947 }
948 splx(s);
949
950 if (countp != NULL)
951 *countp = olocks;
952 }
953
954 #if defined(DEBUG)
955 /*
956 * Assert that the kernel lock is held.
957 */
958 void
959 _kernel_lock_assert_locked(void)
960 {
961
962 if (kernel_lock != __SIMPLELOCK_LOCKED ||
963 curcpu()->ci_biglock_count == 0)
964 _KERNEL_LOCK_ABORT("not locked");
965 }
966
967 void
968 _kernel_lock_assert_unlocked()
969 {
970
971 if (curcpu()->ci_biglock_count != 0)
972 _KERNEL_LOCK_ABORT("locked");
973 }
974 #endif
975
976 #endif /* MULTIPROCESSOR || LOCKDEBUG */
977