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