kern_lock.c revision 1.110.2.6 1 /* $NetBSD: kern_lock.c,v 1.110.2.6 2007/06/08 14:17:19 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.6 2007/06/08 14:17:19 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.%d, not "
648 "exclusive lock holder %d.%d "
649 "unlocking", pid, lid,
650 lkp->lk_lockholder,
651 lkp->lk_locklwp);
652 }
653 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
654 lkp->lk_recurselevel = 0;
655 lkp->lk_exclusivecount--;
656 COUNT(lkp, l, cpu_num, -1);
657 if (lkp->lk_exclusivecount == 0) {
658 lkp->lk_flags &= ~LK_HAVE_EXCL;
659 SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
660 #if defined(LOCKDEBUG)
661 lkp->lk_unlock_file = file;
662 lkp->lk_unlock_line = line;
663 #endif
664 }
665 } else if (lkp->lk_sharecount != 0) {
666 lkp->lk_sharecount--;
667 if (lkp->lk_sharecount == 0)
668 lkp->lk_flags &= ~LK_SHARE_NONZERO;
669 COUNT(lkp, l, cpu_num, -1);
670 }
671 #ifdef DIAGNOSTIC
672 else
673 lockpanic(lkp, "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, lid, cpu_num))
686 lockpanic(lkp, "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 LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
693 error = EBUSY;
694 break;
695 }
696 error = acquire(&lkp, &s, extflags, 1,
697 LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
698 LK_SHARE_NONZERO | LK_WAIT_NONZERO,
699 RETURN_ADDRESS);
700 if (error)
701 break;
702 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
703 SETHOLDER(lkp, pid, lid, cpu_num);
704 #if defined(LOCKDEBUG)
705 lkp->lk_lock_file = file;
706 lkp->lk_lock_line = line;
707 #endif
708 lkp->lk_exclusivecount = 1;
709 /* XXX unlikely that we'd want this */
710 if (extflags & LK_SETRECURSE)
711 lkp->lk_recurselevel = 1;
712 COUNT(lkp, l, cpu_num, 1);
713 break;
714
715 default:
716 mutex_exit(mutex);
717 lockpanic(lkp, "lockmgr: unknown locktype request %d",
718 flags & LK_TYPE_MASK);
719 /* NOTREACHED */
720 }
721 if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
722 ((lkp->lk_flags &
723 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
724 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
725 lkp->lk_flags &= ~LK_WAITDRAIN;
726 wakeup(&lkp->lk_flags);
727 }
728 /*
729 * Note that this panic will be a recursive panic, since
730 * we only set lock_shutdown_noblock above if panicstr != NULL.
731 */
732 if (error && lock_shutdown_noblock)
733 lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
734
735 mutex_exit(mutex);
736 return (error);
737 }
738
739 /*
740 * Print out information about state of a lock. Used by VOP_PRINT
741 * routines to display ststus about contained locks.
742 */
743 void
744 lockmgr_printinfo(volatile struct lock *lkp)
745 {
746
747 if (lkp->lk_sharecount)
748 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
749 lkp->lk_sharecount);
750 else if (lkp->lk_flags & LK_HAVE_EXCL) {
751 printf(" lock type %s: EXCL (count %d) by ",
752 lkp->lk_wmesg, lkp->lk_exclusivecount);
753 printf("pid %d.%d", lkp->lk_lockholder,
754 lkp->lk_locklwp);
755 } else
756 printf(" not locked");
757 if (lkp->lk_waitcount > 0)
758 printf(" with %d pending", lkp->lk_waitcount);
759 }
760
761 #if defined(LOCKDEBUG)
762 void
763 assert_sleepable(struct simplelock *interlock, const char *msg)
764 {
765
766 LOCKDEBUG_BARRIER(&kernel_lock, 1);
767 if (CURCPU_IDLE_P()) {
768 panic("assert_sleepable: idle");
769 }
770 }
771 #endif
772
773 #if defined(MULTIPROCESSOR)
774
775 /*
776 * Functions for manipulating the kernel_lock. We put them here
777 * so that they show up in profiles.
778 */
779
780 #define _KERNEL_LOCK_ABORT(msg) \
781 LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops, \
782 __FUNCTION__, msg)
783
784 #ifdef LOCKDEBUG
785 #define _KERNEL_LOCK_ASSERT(cond) \
786 do { \
787 if (!(cond)) \
788 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
789 } while (/* CONSTCOND */ 0)
790 #else
791 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
792 #endif
793
794 void _kernel_lock_dump(volatile void *);
795
796 lockops_t _kernel_lock_ops = {
797 "Kernel lock",
798 0,
799 _kernel_lock_dump
800 };
801
802 /*
803 * Initialize the kernel lock.
804 */
805 void
806 _kernel_lock_init(void)
807 {
808
809 __cpu_simple_lock_init(&kernel_lock);
810 kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
811 }
812
813 /*
814 * Print debugging information about the kernel lock.
815 */
816 void
817 _kernel_lock_dump(volatile void *junk)
818 {
819 struct cpu_info *ci = curcpu();
820
821 (void)junk;
822
823 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
824 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
825 }
826
827 /*
828 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
829 * acquisition is from process context.
830 */
831 void
832 _kernel_lock(int nlocks, struct lwp *l)
833 {
834 struct cpu_info *ci = curcpu();
835 LOCKSTAT_TIMER(spintime);
836 LOCKSTAT_FLAG(lsflag);
837 struct lwp *owant;
838 #ifdef LOCKDEBUG
839 u_int spins;
840 #endif
841 int s;
842
843 (void)l;
844
845 if (nlocks == 0)
846 return;
847 _KERNEL_LOCK_ASSERT(nlocks > 0);
848
849 s = splbiglock();
850
851 if (ci->ci_biglock_count != 0) {
852 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
853 ci->ci_biglock_count += nlocks;
854 splx(s);
855 return;
856 }
857
858 LOCKDEBUG_WANTLOCK(kernel_lock_id,
859 (uintptr_t)__builtin_return_address(0), 0);
860
861 if (__cpu_simple_lock_try(&kernel_lock)) {
862 ci->ci_biglock_count = nlocks;
863 LOCKDEBUG_LOCKED(kernel_lock_id,
864 (uintptr_t)__builtin_return_address(0), 0);
865 splx(s);
866 return;
867 }
868
869 LOCKSTAT_ENTER(lsflag);
870 LOCKSTAT_START_TIMER(lsflag, spintime);
871
872 /*
873 * Before setting ci_biglock_wanted we must post a store
874 * fence (see kern_mutex.c). This is accomplished by the
875 * __cpu_simple_lock_try() above.
876 */
877 owant = ci->ci_biglock_wanted;
878 ci->ci_biglock_wanted = curlwp; /* XXXAD */
879
880 #ifdef LOCKDEBUG
881 spins = 0;
882 #endif
883
884 do {
885 while (kernel_lock == __SIMPLELOCK_LOCKED) {
886 #ifdef LOCKDEBUG
887 if (SPINLOCK_SPINOUT(spins))
888 _KERNEL_LOCK_ABORT("spinout");
889 #endif
890 splx(s);
891 SPINLOCK_SPIN_HOOK;
892 (void)splbiglock();
893 }
894 } while (!__cpu_simple_lock_try(&kernel_lock));
895
896 ci->ci_biglock_wanted = owant;
897 ci->ci_biglock_count += nlocks;
898 LOCKSTAT_STOP_TIMER(lsflag, spintime);
899 LOCKDEBUG_LOCKED(kernel_lock_id,
900 (uintptr_t)__builtin_return_address(0), 0);
901 splx(s);
902
903 /*
904 * Again, another store fence is required (see kern_mutex.c).
905 */
906 mb_write();
907 if (owant == NULL) {
908 LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
909 1, spintime);
910 }
911 LOCKSTAT_EXIT(lsflag);
912 }
913
914 /*
915 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
916 * all holds. If 'l' is non-null, the release is from process context.
917 */
918 void
919 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
920 {
921 struct cpu_info *ci = curcpu();
922 u_int olocks;
923 int s;
924
925 (void)l;
926
927 _KERNEL_LOCK_ASSERT(nlocks < 2);
928
929 olocks = ci->ci_biglock_count;
930
931 if (olocks == 0) {
932 _KERNEL_LOCK_ASSERT(nlocks <= 0);
933 if (countp != NULL)
934 *countp = 0;
935 return;
936 }
937
938 _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
939
940 if (nlocks == 0)
941 nlocks = olocks;
942 else if (nlocks == -1) {
943 nlocks = 1;
944 _KERNEL_LOCK_ASSERT(olocks == 1);
945 }
946
947 s = splbiglock();
948 if ((ci->ci_biglock_count -= nlocks) == 0) {
949 LOCKDEBUG_UNLOCKED(kernel_lock_id,
950 (uintptr_t)__builtin_return_address(0), 0);
951 __cpu_simple_unlock(&kernel_lock);
952 }
953 splx(s);
954
955 if (countp != NULL)
956 *countp = olocks;
957 }
958
959 #if defined(DEBUG)
960 /*
961 * Assert that the kernel lock is held.
962 */
963 void
964 _kernel_lock_assert_locked(void)
965 {
966
967 if (kernel_lock != __SIMPLELOCK_LOCKED ||
968 curcpu()->ci_biglock_count == 0)
969 _KERNEL_LOCK_ABORT("not locked");
970 }
971
972 void
973 _kernel_lock_assert_unlocked()
974 {
975
976 if (curcpu()->ci_biglock_count != 0)
977 _KERNEL_LOCK_ABORT("locked");
978 }
979 #endif
980
981 #endif /* MULTIPROCESSOR || LOCKDEBUG */
982