kern_lock.c revision 1.16 1 /* $NetBSD: kern_lock.c,v 1.16 1999/03/25 00:20:35 sommerfe Exp $ */
2
3 /*
4 * Copyright (c) 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code contains ideas from software contributed to Berkeley by
8 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
9 * System project at Carnegie-Mellon University.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
40 */
41
42 #include "opt_lockdebug.h"
43
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/lock.h>
47 #include <sys/systm.h>
48 #include <machine/cpu.h>
49
50 /*
51 * Locking primitives implementation.
52 * Locks provide shared/exclusive sychronization.
53 */
54
55 #ifdef LOCKDEBUG
56 #define COUNT(p, x) if (p) (p)->p_locks += (x)
57 #else
58 #define COUNT(p, x)
59 #endif
60
61 #if 0 /*#was defined(MULTIPROCESSOR)*/
62 /*-
63
64 This macro is Bad Style and it doesn't work either... [pk, 10-14-1998]
65
66 -*
67 * For multiprocessor system, try spin lock first.
68 *
69 * This should be inline expanded below, but we cannot have #if
70 * inside a multiline define.
71 */
72
73 int lock_wait_time = 100;
74 #define PAUSE(lkp, wanted) \
75 if (lock_wait_time > 0) { \
76 int i; \
77 \
78 simple_unlock(&lkp->lk_interlock); \
79 for (i = lock_wait_time; i > 0; i--) \
80 if (!(wanted)) \
81 break; \
82 simple_lock(&lkp->lk_interlock); \
83 } \
84 if (!(wanted)) \
85 break;
86
87 #else /* ! MULTIPROCESSOR */
88
89 /*
90 * It is an error to spin on a uniprocessor as nothing will ever cause
91 * the simple lock to clear while we are executing.
92 */
93 #define PAUSE(lkp, wanted)
94
95 #endif /* MULTIPROCESSOR */
96
97 /*
98 * Acquire a resource.
99 */
100 #define ACQUIRE(lkp, error, extflags, wanted) \
101 PAUSE(lkp, wanted); \
102 for (error = 0; wanted; ) { \
103 (lkp)->lk_waitcount++; \
104 simple_unlock(&(lkp)->lk_interlock); \
105 error = tsleep((void *)lkp, (lkp)->lk_prio, \
106 (lkp)->lk_wmesg, (lkp)->lk_timo); \
107 simple_lock(&(lkp)->lk_interlock); \
108 (lkp)->lk_waitcount--; \
109 if (error) \
110 break; \
111 if ((extflags) & LK_SLEEPFAIL) { \
112 error = ENOLCK; \
113 break; \
114 } \
115 }
116
117 /*
118 * Initialize a lock; required before use.
119 */
120 void
121 lockinit(lkp, prio, wmesg, timo, flags)
122 struct lock *lkp;
123 int prio;
124 const char *wmesg;
125 int timo;
126 int flags;
127 {
128
129 memset(lkp, 0, sizeof(struct lock));
130 simple_lock_init(&lkp->lk_interlock);
131 lkp->lk_flags = flags & LK_EXTFLG_MASK;
132 lkp->lk_prio = prio;
133 lkp->lk_timo = timo;
134 lkp->lk_wmesg = wmesg;
135 lkp->lk_lockholder = LK_NOPROC;
136 }
137
138 /*
139 * Determine the status of a lock.
140 */
141 int
142 lockstatus(lkp)
143 struct lock *lkp;
144 {
145 int lock_type = 0;
146
147 simple_lock(&lkp->lk_interlock);
148 if (lkp->lk_exclusivecount != 0)
149 lock_type = LK_EXCLUSIVE;
150 else if (lkp->lk_sharecount != 0)
151 lock_type = LK_SHARED;
152 simple_unlock(&lkp->lk_interlock);
153 return (lock_type);
154 }
155
156 /*
157 * Set, change, or release a lock.
158 *
159 * Shared requests increment the shared count. Exclusive requests set the
160 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
161 * accepted shared locks and shared-to-exclusive upgrades to go away.
162 */
163 int
164 lockmgr(lkp, flags, interlkp)
165 __volatile struct lock *lkp;
166 u_int flags;
167 struct simplelock *interlkp;
168 {
169 int error;
170 pid_t pid;
171 int extflags;
172 struct proc *p = curproc;
173
174 error = 0;
175 if (p)
176 pid = p->p_pid;
177 else
178 pid = LK_KERNPROC;
179 simple_lock(&lkp->lk_interlock);
180 if (flags & LK_INTERLOCK)
181 simple_unlock(interlkp);
182 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
183 #ifdef DIAGNOSTIC
184 /*
185 * Once a lock has drained, the LK_DRAINING flag is set and an
186 * exclusive lock is returned. The only valid operation thereafter
187 * is a single release of that exclusive lock. This final release
188 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
189 * further requests of any sort will result in a panic. The bits
190 * selected for these two flags are chosen so that they will be set
191 * in memory that is freed (freed memory is filled with 0xdeadbeef).
192 * The final release is permitted to give a new lease on life to
193 * the lock by specifying LK_REENABLE.
194 */
195 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
196 if (lkp->lk_flags & LK_DRAINED)
197 panic("lockmgr: using decommissioned lock");
198 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
199 lkp->lk_lockholder != pid)
200 panic("lockmgr: non-release on draining lock: %d\n",
201 flags & LK_TYPE_MASK);
202 lkp->lk_flags &= ~LK_DRAINING;
203 if ((flags & LK_REENABLE) == 0)
204 lkp->lk_flags |= LK_DRAINED;
205 }
206 #endif DIAGNOSTIC
207
208 switch (flags & LK_TYPE_MASK) {
209
210 case LK_SHARED:
211 if (lkp->lk_lockholder != pid) {
212 /*
213 * If just polling, check to see if we will block.
214 */
215 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
216 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
217 error = EBUSY;
218 break;
219 }
220 /*
221 * Wait for exclusive locks and upgrades to clear.
222 */
223 ACQUIRE(lkp, error, extflags, lkp->lk_flags &
224 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
225 if (error)
226 break;
227 lkp->lk_sharecount++;
228 COUNT(p, 1);
229 break;
230 }
231 /*
232 * We hold an exclusive lock, so downgrade it to shared.
233 * An alternative would be to fail with EDEADLK.
234 */
235 lkp->lk_sharecount++;
236 COUNT(p, 1);
237 /* fall into downgrade */
238
239 case LK_DOWNGRADE:
240 if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
241 panic("lockmgr: not holding exclusive lock");
242 lkp->lk_sharecount += lkp->lk_exclusivecount;
243 lkp->lk_exclusivecount = 0;
244 lkp->lk_recurselevel = 0;
245 lkp->lk_flags &= ~LK_HAVE_EXCL;
246 lkp->lk_lockholder = LK_NOPROC;
247 if (lkp->lk_waitcount)
248 wakeup((void *)lkp);
249 break;
250
251 case LK_EXCLUPGRADE:
252 /*
253 * If another process is ahead of us to get an upgrade,
254 * then we want to fail rather than have an intervening
255 * exclusive access.
256 */
257 if (lkp->lk_flags & LK_WANT_UPGRADE) {
258 lkp->lk_sharecount--;
259 COUNT(p, -1);
260 error = EBUSY;
261 break;
262 }
263 /* fall into normal upgrade */
264
265 case LK_UPGRADE:
266 /*
267 * Upgrade a shared lock to an exclusive one. If another
268 * shared lock has already requested an upgrade to an
269 * exclusive lock, our shared lock is released and an
270 * exclusive lock is requested (which will be granted
271 * after the upgrade). If we return an error, the file
272 * will always be unlocked.
273 */
274 if (lkp->lk_lockholder == pid || lkp->lk_sharecount <= 0)
275 panic("lockmgr: upgrade exclusive lock");
276 lkp->lk_sharecount--;
277 COUNT(p, -1);
278 /*
279 * If we are just polling, check to see if we will block.
280 */
281 if ((extflags & LK_NOWAIT) &&
282 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
283 lkp->lk_sharecount > 1)) {
284 error = EBUSY;
285 break;
286 }
287 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
288 /*
289 * We are first shared lock to request an upgrade, so
290 * request upgrade and wait for the shared count to
291 * drop to zero, then take exclusive lock.
292 */
293 lkp->lk_flags |= LK_WANT_UPGRADE;
294 ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
295 lkp->lk_flags &= ~LK_WANT_UPGRADE;
296 if (error)
297 break;
298 lkp->lk_flags |= LK_HAVE_EXCL;
299 lkp->lk_lockholder = pid;
300 if (lkp->lk_exclusivecount != 0)
301 panic("lockmgr: non-zero exclusive count");
302 lkp->lk_exclusivecount = 1;
303 if (extflags & LK_SETRECURSE)
304 lkp->lk_recurselevel = 1;
305 COUNT(p, 1);
306 break;
307 }
308 /*
309 * Someone else has requested upgrade. Release our shared
310 * lock, awaken upgrade requestor if we are the last shared
311 * lock, then request an exclusive lock.
312 */
313 if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
314 wakeup((void *)lkp);
315 /* fall into exclusive request */
316
317 case LK_EXCLUSIVE:
318 if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
319 /*
320 * Recursive lock.
321 */
322 if ((extflags & LK_CANRECURSE) == 0 &&
323 lkp->lk_recurselevel == 0) {
324 if (extflags & LK_RECURSEFAIL) {
325 error = EDEADLK;
326 break;
327 } else
328 panic("lockmgr: locking against myself");
329 }
330 lkp->lk_exclusivecount++;
331 if (extflags & LK_SETRECURSE &&
332 lkp->lk_recurselevel == 0)
333 lkp->lk_recurselevel = lkp->lk_exclusivecount;
334 COUNT(p, 1);
335 break;
336 }
337 /*
338 * If we are just polling, check to see if we will sleep.
339 */
340 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
341 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
342 lkp->lk_sharecount != 0)) {
343 error = EBUSY;
344 break;
345 }
346 /*
347 * Try to acquire the want_exclusive flag.
348 */
349 ACQUIRE(lkp, error, extflags, lkp->lk_flags &
350 (LK_HAVE_EXCL | LK_WANT_EXCL));
351 if (error)
352 break;
353 lkp->lk_flags |= LK_WANT_EXCL;
354 /*
355 * Wait for shared locks and upgrades to finish.
356 */
357 ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
358 (lkp->lk_flags & LK_WANT_UPGRADE));
359 lkp->lk_flags &= ~LK_WANT_EXCL;
360 if (error)
361 break;
362 lkp->lk_flags |= LK_HAVE_EXCL;
363 lkp->lk_lockholder = pid;
364 if (lkp->lk_exclusivecount != 0)
365 panic("lockmgr: non-zero exclusive count");
366 lkp->lk_exclusivecount = 1;
367 if (extflags & LK_SETRECURSE)
368 lkp->lk_recurselevel = 1;
369 COUNT(p, 1);
370 break;
371
372 case LK_RELEASE:
373 if (lkp->lk_exclusivecount != 0) {
374 if (pid != lkp->lk_lockholder)
375 panic("lockmgr: pid %d, not exclusive lock "
376 "holder %d unlocking", pid,
377 lkp->lk_lockholder);
378 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
379 lkp->lk_recurselevel = 0;
380 lkp->lk_exclusivecount--;
381 COUNT(p, -1);
382 if (lkp->lk_exclusivecount == 0) {
383 lkp->lk_flags &= ~LK_HAVE_EXCL;
384 lkp->lk_lockholder = LK_NOPROC;
385 }
386 } else if (lkp->lk_sharecount != 0) {
387 lkp->lk_sharecount--;
388 COUNT(p, -1);
389 }
390 if (lkp->lk_waitcount)
391 wakeup((void *)lkp);
392 break;
393
394 case LK_DRAIN:
395 /*
396 * Check that we do not already hold the lock, as it can
397 * never drain if we do. Unfortunately, we have no way to
398 * check for holding a shared lock, but at least we can
399 * check for an exclusive one.
400 */
401 if (lkp->lk_lockholder == pid)
402 panic("lockmgr: draining against myself");
403 /*
404 * If we are just polling, check to see if we will sleep.
405 */
406 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
407 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
408 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
409 error = EBUSY;
410 break;
411 }
412 PAUSE(lkp, ((lkp->lk_flags &
413 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
414 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0));
415 for (error = 0; ((lkp->lk_flags &
416 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
417 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) {
418 lkp->lk_flags |= LK_WAITDRAIN;
419 simple_unlock(&lkp->lk_interlock);
420 if ((error = tsleep((void *)&lkp->lk_flags,
421 lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
422 return (error);
423 if ((extflags) & LK_SLEEPFAIL)
424 return (ENOLCK);
425 simple_lock(&lkp->lk_interlock);
426 }
427 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
428 lkp->lk_lockholder = pid;
429 lkp->lk_exclusivecount = 1;
430 /* XXX unlikely that we'd want this */
431 if (extflags & LK_SETRECURSE)
432 lkp->lk_recurselevel = 1;
433 COUNT(p, 1);
434 break;
435
436 default:
437 simple_unlock(&lkp->lk_interlock);
438 panic("lockmgr: unknown locktype request %d",
439 flags & LK_TYPE_MASK);
440 /* NOTREACHED */
441 }
442 if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
443 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
444 lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
445 lkp->lk_flags &= ~LK_WAITDRAIN;
446 wakeup((void *)&lkp->lk_flags);
447 }
448 simple_unlock(&lkp->lk_interlock);
449 return (error);
450 }
451
452 /*
453 * Print out information about state of a lock. Used by VOP_PRINT
454 * routines to display ststus about contained locks.
455 */
456 void
457 lockmgr_printinfo(lkp)
458 struct lock *lkp;
459 {
460
461 if (lkp->lk_sharecount)
462 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
463 lkp->lk_sharecount);
464 else if (lkp->lk_flags & LK_HAVE_EXCL)
465 printf(" lock type %s: EXCL (count %d) by pid %d",
466 lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
467 if (lkp->lk_waitcount > 0)
468 printf(" with %d pending", lkp->lk_waitcount);
469 }
470
471 #if defined(LOCKDEBUG) && !defined(MULTIPROCESSOR)
472 #include <sys/kernel.h>
473 #include <vm/vm.h>
474 #include <sys/sysctl.h>
475 int lockpausetime = 0;
476 struct ctldebug debug2 = { "lockpausetime", &lockpausetime };
477 int simplelockrecurse;
478 LIST_HEAD(slocklist, simplelock) slockdebuglist;
479
480 /*
481 * Simple lock functions so that the debugger can see from whence
482 * they are being called.
483 */
484 void
485 simple_lock_init(alp)
486 struct simplelock *alp;
487 {
488 alp->lock_data = 0;
489 alp->lock_file = NULL;
490 alp->lock_line = 0;
491 alp->unlock_file = NULL;
492 alp->unlock_line = 0;
493 alp->lock_holder = 0;
494 }
495
496 void
497 _simple_lock(alp, id, l)
498 __volatile struct simplelock *alp;
499 const char *id;
500 int l;
501 {
502 int s;
503
504 if (simplelockrecurse)
505 return;
506 if (alp->lock_data == 1) {
507 printf("simple_lock: lock held\n");
508 printf("currently at: %s:%d\n", id, l);
509 printf("last locked: %s:%d\n",
510 alp->lock_file, alp->lock_line);
511 printf("last unlocked: %s:%d\n",
512 alp->unlock_file, alp->unlock_line);
513 if (lockpausetime == -1)
514 panic("simple_lock: lock held");
515 if (lockpausetime == 1) {
516 #ifdef BACKTRACE
517 BACKTRACE(curproc);
518 #endif
519 } else if (lockpausetime > 1) {
520 printf("simple_lock: lock held, pausing...");
521 tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
522 lockpausetime * hz);
523 printf(" continuing\n");
524 }
525 return;
526 }
527
528 s = splhigh();
529 LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
530 splx(s);
531
532 alp->lock_data = 1;
533 alp->lock_file = id;
534 alp->lock_line = l;
535 if (curproc)
536 curproc->p_simple_locks++;
537 }
538
539 int
540 _simple_lock_try(alp, id, l)
541 __volatile struct simplelock *alp;
542 const char *id;
543 int l;
544 {
545 int s;
546
547 if (alp->lock_data)
548 return (0);
549 if (simplelockrecurse)
550 return (1);
551 alp->lock_data = 1;
552 alp->lock_file = id;
553 alp->lock_line = l;
554
555 s = splhigh();
556 LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
557 splx(s);
558
559 if (curproc)
560 curproc->p_simple_locks++;
561 return (1);
562 }
563
564 void
565 _simple_unlock(alp, id, l)
566 __volatile struct simplelock *alp;
567 const char *id;
568 int l;
569 {
570 int s;
571
572 if (simplelockrecurse)
573 return;
574 if (alp->lock_data == 0) {
575 printf("simple_unlock: lock not held\n");
576 printf("currently at: %s:%d\n", id, l);
577 printf("last locked: %s:%d\n",
578 alp->lock_file, alp->lock_line);
579 printf("last unlocked: %s:%d\n",
580 alp->unlock_file, alp->unlock_line);
581 if (lockpausetime == -1)
582 panic("simple_unlock: lock not held");
583 if (lockpausetime == 1) {
584 #ifdef BACKTRACE
585 BACKTRACE(curproc);
586 #endif
587 } else if (lockpausetime > 1) {
588 printf("simple_unlock: lock not held, pausing...");
589 tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
590 lockpausetime * hz);
591 printf(" continuing\n");
592 }
593 return;
594 }
595
596 s = splhigh();
597 LIST_REMOVE(alp, list);
598 alp->list.le_next = NULL;
599 alp->list.le_prev = NULL;
600 splx(s);
601
602 alp->lock_data = 0;
603 alp->unlock_file = id;
604 alp->unlock_line = l;
605 if (curproc)
606 curproc->p_simple_locks--;
607 }
608
609 void
610 simple_lock_dump()
611 {
612 struct simplelock *alp;
613 int s;
614
615 s = splhigh();
616 printf("all simple locks:\n");
617 for (alp = LIST_FIRST(&slockdebuglist);
618 alp != NULL;
619 alp = LIST_NEXT(alp, list)) {
620 printf("%p %s:%d\n", alp, alp->lock_file, alp->lock_line);
621 }
622 splx(s);
623 }
624
625 void
626 simple_lock_freecheck(start, end)
627 void *start, *end;
628 {
629 struct simplelock *alp;
630 int s;
631
632 s = splhigh();
633 for (alp = LIST_FIRST(&slockdebuglist);
634 alp != NULL;
635 alp = LIST_NEXT(alp, list)) {
636 if ((void *)alp >= start && (void *)alp < end) {
637 printf("freeing simple_lock %p %s:%d\n",
638 alp, alp->lock_file, alp->lock_line);
639 #ifdef DDB
640 Debugger();
641 #endif
642 }
643 }
644 splx(s);
645 }
646 #endif /* LOCKDEBUG && ! MULTIPROCESSOR */
647