kern_lock.c revision 1.15 1 /* $NetBSD: kern_lock.c,v 1.15 1999/02/28 14:09:15 fvdl 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 panic("lockmgr: locking against myself");
325 lkp->lk_exclusivecount++;
326 if (extflags & LK_SETRECURSE &&
327 lkp->lk_recurselevel == 0)
328 lkp->lk_recurselevel = lkp->lk_exclusivecount;
329 COUNT(p, 1);
330 break;
331 }
332 /*
333 * If we are just polling, check to see if we will sleep.
334 */
335 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
336 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
337 lkp->lk_sharecount != 0)) {
338 error = EBUSY;
339 break;
340 }
341 /*
342 * Try to acquire the want_exclusive flag.
343 */
344 ACQUIRE(lkp, error, extflags, lkp->lk_flags &
345 (LK_HAVE_EXCL | LK_WANT_EXCL));
346 if (error)
347 break;
348 lkp->lk_flags |= LK_WANT_EXCL;
349 /*
350 * Wait for shared locks and upgrades to finish.
351 */
352 ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
353 (lkp->lk_flags & LK_WANT_UPGRADE));
354 lkp->lk_flags &= ~LK_WANT_EXCL;
355 if (error)
356 break;
357 lkp->lk_flags |= LK_HAVE_EXCL;
358 lkp->lk_lockholder = pid;
359 if (lkp->lk_exclusivecount != 0)
360 panic("lockmgr: non-zero exclusive count");
361 lkp->lk_exclusivecount = 1;
362 if (extflags & LK_SETRECURSE)
363 lkp->lk_recurselevel = 1;
364 COUNT(p, 1);
365 break;
366
367 case LK_RELEASE:
368 if (lkp->lk_exclusivecount != 0) {
369 if (pid != lkp->lk_lockholder)
370 panic("lockmgr: pid %d, not exclusive lock "
371 "holder %d unlocking", pid,
372 lkp->lk_lockholder);
373 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
374 lkp->lk_recurselevel = 0;
375 lkp->lk_exclusivecount--;
376 COUNT(p, -1);
377 if (lkp->lk_exclusivecount == 0) {
378 lkp->lk_flags &= ~LK_HAVE_EXCL;
379 lkp->lk_lockholder = LK_NOPROC;
380 }
381 } else if (lkp->lk_sharecount != 0) {
382 lkp->lk_sharecount--;
383 COUNT(p, -1);
384 }
385 if (lkp->lk_waitcount)
386 wakeup((void *)lkp);
387 break;
388
389 case LK_DRAIN:
390 /*
391 * Check that we do not already hold the lock, as it can
392 * never drain if we do. Unfortunately, we have no way to
393 * check for holding a shared lock, but at least we can
394 * check for an exclusive one.
395 */
396 if (lkp->lk_lockholder == pid)
397 panic("lockmgr: draining against myself");
398 /*
399 * If we are just polling, check to see if we will sleep.
400 */
401 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
402 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
403 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
404 error = EBUSY;
405 break;
406 }
407 PAUSE(lkp, ((lkp->lk_flags &
408 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
409 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0));
410 for (error = 0; ((lkp->lk_flags &
411 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
412 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) {
413 lkp->lk_flags |= LK_WAITDRAIN;
414 simple_unlock(&lkp->lk_interlock);
415 if ((error = tsleep((void *)&lkp->lk_flags,
416 lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
417 return (error);
418 if ((extflags) & LK_SLEEPFAIL)
419 return (ENOLCK);
420 simple_lock(&lkp->lk_interlock);
421 }
422 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
423 lkp->lk_lockholder = pid;
424 lkp->lk_exclusivecount = 1;
425 /* XXX unlikely that we'd want this */
426 if (extflags & LK_SETRECURSE)
427 lkp->lk_recurselevel = 1;
428 COUNT(p, 1);
429 break;
430
431 default:
432 simple_unlock(&lkp->lk_interlock);
433 panic("lockmgr: unknown locktype request %d",
434 flags & LK_TYPE_MASK);
435 /* NOTREACHED */
436 }
437 if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
438 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
439 lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
440 lkp->lk_flags &= ~LK_WAITDRAIN;
441 wakeup((void *)&lkp->lk_flags);
442 }
443 simple_unlock(&lkp->lk_interlock);
444 return (error);
445 }
446
447 /*
448 * Print out information about state of a lock. Used by VOP_PRINT
449 * routines to display ststus about contained locks.
450 */
451 void
452 lockmgr_printinfo(lkp)
453 struct lock *lkp;
454 {
455
456 if (lkp->lk_sharecount)
457 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
458 lkp->lk_sharecount);
459 else if (lkp->lk_flags & LK_HAVE_EXCL)
460 printf(" lock type %s: EXCL (count %d) by pid %d",
461 lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
462 if (lkp->lk_waitcount > 0)
463 printf(" with %d pending", lkp->lk_waitcount);
464 }
465
466 #if defined(LOCKDEBUG) && !defined(MULTIPROCESSOR)
467 #include <sys/kernel.h>
468 #include <vm/vm.h>
469 #include <sys/sysctl.h>
470 int lockpausetime = 0;
471 struct ctldebug debug2 = { "lockpausetime", &lockpausetime };
472 int simplelockrecurse;
473 LIST_HEAD(slocklist, simplelock) slockdebuglist;
474
475 /*
476 * Simple lock functions so that the debugger can see from whence
477 * they are being called.
478 */
479 void
480 simple_lock_init(alp)
481 struct simplelock *alp;
482 {
483 alp->lock_data = 0;
484 alp->lock_file = NULL;
485 alp->lock_line = 0;
486 alp->unlock_file = NULL;
487 alp->unlock_line = 0;
488 alp->lock_holder = 0;
489 }
490
491 void
492 _simple_lock(alp, id, l)
493 __volatile struct simplelock *alp;
494 const char *id;
495 int l;
496 {
497 int s;
498
499 if (simplelockrecurse)
500 return;
501 if (alp->lock_data == 1) {
502 printf("simple_lock: lock held\n");
503 printf("currently at: %s:%d\n", id, l);
504 printf("last locked: %s:%d\n",
505 alp->lock_file, alp->lock_line);
506 printf("last unlocked: %s:%d\n",
507 alp->unlock_file, alp->unlock_line);
508 if (lockpausetime == -1)
509 panic("simple_lock: lock held");
510 if (lockpausetime == 1) {
511 #ifdef BACKTRACE
512 BACKTRACE(curproc);
513 #endif
514 } else if (lockpausetime > 1) {
515 printf("simple_lock: lock held, pausing...");
516 tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
517 lockpausetime * hz);
518 printf(" continuing\n");
519 }
520 return;
521 }
522
523 s = splhigh();
524 LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
525 splx(s);
526
527 alp->lock_data = 1;
528 alp->lock_file = id;
529 alp->lock_line = l;
530 if (curproc)
531 curproc->p_simple_locks++;
532 }
533
534 int
535 _simple_lock_try(alp, id, l)
536 __volatile struct simplelock *alp;
537 const char *id;
538 int l;
539 {
540 int s;
541
542 if (alp->lock_data)
543 return (0);
544 if (simplelockrecurse)
545 return (1);
546 alp->lock_data = 1;
547 alp->lock_file = id;
548 alp->lock_line = l;
549
550 s = splhigh();
551 LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
552 splx(s);
553
554 if (curproc)
555 curproc->p_simple_locks++;
556 return (1);
557 }
558
559 void
560 _simple_unlock(alp, id, l)
561 __volatile struct simplelock *alp;
562 const char *id;
563 int l;
564 {
565 int s;
566
567 if (simplelockrecurse)
568 return;
569 if (alp->lock_data == 0) {
570 printf("simple_unlock: lock not held\n");
571 printf("currently at: %s:%d\n", id, l);
572 printf("last locked: %s:%d\n",
573 alp->lock_file, alp->lock_line);
574 printf("last unlocked: %s:%d\n",
575 alp->unlock_file, alp->unlock_line);
576 if (lockpausetime == -1)
577 panic("simple_unlock: lock not held");
578 if (lockpausetime == 1) {
579 #ifdef BACKTRACE
580 BACKTRACE(curproc);
581 #endif
582 } else if (lockpausetime > 1) {
583 printf("simple_unlock: lock not held, pausing...");
584 tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
585 lockpausetime * hz);
586 printf(" continuing\n");
587 }
588 return;
589 }
590
591 s = splhigh();
592 LIST_REMOVE(alp, list);
593 alp->list.le_next = NULL;
594 alp->list.le_prev = NULL;
595 splx(s);
596
597 alp->lock_data = 0;
598 alp->unlock_file = id;
599 alp->unlock_line = l;
600 if (curproc)
601 curproc->p_simple_locks--;
602 }
603
604 void
605 simple_lock_dump()
606 {
607 struct simplelock *alp;
608 int s;
609
610 s = splhigh();
611 printf("all simple locks:\n");
612 for (alp = LIST_FIRST(&slockdebuglist);
613 alp != NULL;
614 alp = LIST_NEXT(alp, list)) {
615 printf("%p %s:%d\n", alp, alp->lock_file, alp->lock_line);
616 }
617 splx(s);
618 }
619
620 void
621 simple_lock_freecheck(start, end)
622 void *start, *end;
623 {
624 struct simplelock *alp;
625 int s;
626
627 s = splhigh();
628 for (alp = LIST_FIRST(&slockdebuglist);
629 alp != NULL;
630 alp = LIST_NEXT(alp, list)) {
631 if ((void *)alp >= start && (void *)alp < end) {
632 printf("freeing simple_lock %p %s:%d\n",
633 alp, alp->lock_file, alp->lock_line);
634 #ifdef DDB
635 Debugger();
636 #endif
637 }
638 }
639 splx(s);
640 }
641 #endif /* LOCKDEBUG && ! MULTIPROCESSOR */
642