kern_lock.c revision 1.18 1 /* $NetBSD: kern_lock.c,v 1.18 1999/07/19 03:21:11 chs 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 #include "opt_ddb.h"
44
45 #include <sys/param.h>
46 #include <sys/proc.h>
47 #include <sys/lock.h>
48 #include <sys/systm.h>
49 #include <machine/cpu.h>
50
51 /*
52 * Locking primitives implementation.
53 * Locks provide shared/exclusive sychronization.
54 */
55
56 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
57 #define COUNT(p, x) if (p) (p)->p_locks += (x)
58 #else
59 #define COUNT(p, x)
60 #endif
61
62 /*
63 * Acquire a resource.
64 */
65 #define ACQUIRE(lkp, error, extflags, wanted) \
66 for (error = 0; wanted; ) { \
67 (lkp)->lk_waitcount++; \
68 simple_unlock(&(lkp)->lk_interlock); \
69 error = tsleep((void *)lkp, (lkp)->lk_prio, \
70 (lkp)->lk_wmesg, (lkp)->lk_timo); \
71 simple_lock(&(lkp)->lk_interlock); \
72 (lkp)->lk_waitcount--; \
73 if (error) \
74 break; \
75 if ((extflags) & LK_SLEEPFAIL) { \
76 error = ENOLCK; \
77 break; \
78 } \
79 }
80
81 /*
82 * Initialize a lock; required before use.
83 */
84 void
85 lockinit(lkp, prio, wmesg, timo, flags)
86 struct lock *lkp;
87 int prio;
88 const char *wmesg;
89 int timo;
90 int flags;
91 {
92
93 memset(lkp, 0, sizeof(struct lock));
94 simple_lock_init(&lkp->lk_interlock);
95 lkp->lk_flags = flags & LK_EXTFLG_MASK;
96 lkp->lk_prio = prio;
97 lkp->lk_timo = timo;
98 lkp->lk_wmesg = wmesg;
99 lkp->lk_lockholder = LK_NOPROC;
100 }
101
102 /*
103 * Determine the status of a lock.
104 */
105 int
106 lockstatus(lkp)
107 struct lock *lkp;
108 {
109 int lock_type = 0;
110
111 simple_lock(&lkp->lk_interlock);
112 if (lkp->lk_exclusivecount != 0)
113 lock_type = LK_EXCLUSIVE;
114 else if (lkp->lk_sharecount != 0)
115 lock_type = LK_SHARED;
116 simple_unlock(&lkp->lk_interlock);
117 return (lock_type);
118 }
119
120 /*
121 * Set, change, or release a lock.
122 *
123 * Shared requests increment the shared count. Exclusive requests set the
124 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
125 * accepted shared locks and shared-to-exclusive upgrades to go away.
126 */
127 int
128 lockmgr(lkp, flags, interlkp)
129 __volatile struct lock *lkp;
130 u_int flags;
131 struct simplelock *interlkp;
132 {
133 int error;
134 pid_t pid;
135 int extflags;
136 struct proc *p = curproc;
137
138 error = 0;
139 if (p)
140 pid = p->p_pid;
141 else
142 pid = LK_KERNPROC;
143 simple_lock(&lkp->lk_interlock);
144 if (flags & LK_INTERLOCK)
145 simple_unlock(interlkp);
146 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
147 #ifdef DIAGNOSTIC
148 /*
149 * Once a lock has drained, the LK_DRAINING flag is set and an
150 * exclusive lock is returned. The only valid operation thereafter
151 * is a single release of that exclusive lock. This final release
152 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
153 * further requests of any sort will result in a panic. The bits
154 * selected for these two flags are chosen so that they will be set
155 * in memory that is freed (freed memory is filled with 0xdeadbeef).
156 * The final release is permitted to give a new lease on life to
157 * the lock by specifying LK_REENABLE.
158 */
159 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
160 if (lkp->lk_flags & LK_DRAINED)
161 panic("lockmgr: using decommissioned lock");
162 if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
163 lkp->lk_lockholder != pid)
164 panic("lockmgr: non-release on draining lock: %d\n",
165 flags & LK_TYPE_MASK);
166 lkp->lk_flags &= ~LK_DRAINING;
167 if ((flags & LK_REENABLE) == 0)
168 lkp->lk_flags |= LK_DRAINED;
169 }
170 #endif DIAGNOSTIC
171
172 switch (flags & LK_TYPE_MASK) {
173
174 case LK_SHARED:
175 if (lkp->lk_lockholder != pid) {
176 /*
177 * If just polling, check to see if we will block.
178 */
179 if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
180 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
181 error = EBUSY;
182 break;
183 }
184 /*
185 * Wait for exclusive locks and upgrades to clear.
186 */
187 ACQUIRE(lkp, error, extflags, lkp->lk_flags &
188 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
189 if (error)
190 break;
191 lkp->lk_sharecount++;
192 COUNT(p, 1);
193 break;
194 }
195 /*
196 * We hold an exclusive lock, so downgrade it to shared.
197 * An alternative would be to fail with EDEADLK.
198 */
199 lkp->lk_sharecount++;
200 COUNT(p, 1);
201 /* fall into downgrade */
202
203 case LK_DOWNGRADE:
204 if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
205 panic("lockmgr: not holding exclusive lock");
206 lkp->lk_sharecount += lkp->lk_exclusivecount;
207 lkp->lk_exclusivecount = 0;
208 lkp->lk_recurselevel = 0;
209 lkp->lk_flags &= ~LK_HAVE_EXCL;
210 lkp->lk_lockholder = LK_NOPROC;
211 if (lkp->lk_waitcount)
212 wakeup((void *)lkp);
213 break;
214
215 case LK_EXCLUPGRADE:
216 /*
217 * If another process is ahead of us to get an upgrade,
218 * then we want to fail rather than have an intervening
219 * exclusive access.
220 */
221 if (lkp->lk_flags & LK_WANT_UPGRADE) {
222 lkp->lk_sharecount--;
223 COUNT(p, -1);
224 error = EBUSY;
225 break;
226 }
227 /* fall into normal upgrade */
228
229 case LK_UPGRADE:
230 /*
231 * Upgrade a shared lock to an exclusive one. If another
232 * shared lock has already requested an upgrade to an
233 * exclusive lock, our shared lock is released and an
234 * exclusive lock is requested (which will be granted
235 * after the upgrade). If we return an error, the file
236 * will always be unlocked.
237 */
238 if (lkp->lk_lockholder == pid || lkp->lk_sharecount <= 0)
239 panic("lockmgr: upgrade exclusive lock");
240 lkp->lk_sharecount--;
241 COUNT(p, -1);
242 /*
243 * If we are just polling, check to see if we will block.
244 */
245 if ((extflags & LK_NOWAIT) &&
246 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
247 lkp->lk_sharecount > 1)) {
248 error = EBUSY;
249 break;
250 }
251 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
252 /*
253 * We are first shared lock to request an upgrade, so
254 * request upgrade and wait for the shared count to
255 * drop to zero, then take exclusive lock.
256 */
257 lkp->lk_flags |= LK_WANT_UPGRADE;
258 ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
259 lkp->lk_flags &= ~LK_WANT_UPGRADE;
260 if (error)
261 break;
262 lkp->lk_flags |= LK_HAVE_EXCL;
263 lkp->lk_lockholder = pid;
264 if (lkp->lk_exclusivecount != 0)
265 panic("lockmgr: non-zero exclusive count");
266 lkp->lk_exclusivecount = 1;
267 if (extflags & LK_SETRECURSE)
268 lkp->lk_recurselevel = 1;
269 COUNT(p, 1);
270 break;
271 }
272 /*
273 * Someone else has requested upgrade. Release our shared
274 * lock, awaken upgrade requestor if we are the last shared
275 * lock, then request an exclusive lock.
276 */
277 if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
278 wakeup((void *)lkp);
279 /* fall into exclusive request */
280
281 case LK_EXCLUSIVE:
282 if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
283 /*
284 * Recursive lock.
285 */
286 if ((extflags & LK_CANRECURSE) == 0 &&
287 lkp->lk_recurselevel == 0) {
288 if (extflags & LK_RECURSEFAIL) {
289 error = EDEADLK;
290 break;
291 } else
292 panic("lockmgr: locking against myself");
293 }
294 lkp->lk_exclusivecount++;
295 if (extflags & LK_SETRECURSE &&
296 lkp->lk_recurselevel == 0)
297 lkp->lk_recurselevel = lkp->lk_exclusivecount;
298 COUNT(p, 1);
299 break;
300 }
301 /*
302 * If we are just polling, check to see if we will sleep.
303 */
304 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
305 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
306 lkp->lk_sharecount != 0)) {
307 error = EBUSY;
308 break;
309 }
310 /*
311 * Try to acquire the want_exclusive flag.
312 */
313 ACQUIRE(lkp, error, extflags, lkp->lk_flags &
314 (LK_HAVE_EXCL | LK_WANT_EXCL));
315 if (error)
316 break;
317 lkp->lk_flags |= LK_WANT_EXCL;
318 /*
319 * Wait for shared locks and upgrades to finish.
320 */
321 ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
322 (lkp->lk_flags & LK_WANT_UPGRADE));
323 lkp->lk_flags &= ~LK_WANT_EXCL;
324 if (error)
325 break;
326 lkp->lk_flags |= LK_HAVE_EXCL;
327 lkp->lk_lockholder = pid;
328 if (lkp->lk_exclusivecount != 0)
329 panic("lockmgr: non-zero exclusive count");
330 lkp->lk_exclusivecount = 1;
331 if (extflags & LK_SETRECURSE)
332 lkp->lk_recurselevel = 1;
333 COUNT(p, 1);
334 break;
335
336 case LK_RELEASE:
337 if (lkp->lk_exclusivecount != 0) {
338 if (pid != lkp->lk_lockholder)
339 panic("lockmgr: pid %d, not exclusive lock "
340 "holder %d unlocking", pid,
341 lkp->lk_lockholder);
342 if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
343 lkp->lk_recurselevel = 0;
344 lkp->lk_exclusivecount--;
345 COUNT(p, -1);
346 if (lkp->lk_exclusivecount == 0) {
347 lkp->lk_flags &= ~LK_HAVE_EXCL;
348 lkp->lk_lockholder = LK_NOPROC;
349 }
350 } else if (lkp->lk_sharecount != 0) {
351 lkp->lk_sharecount--;
352 COUNT(p, -1);
353 }
354 if (lkp->lk_waitcount)
355 wakeup((void *)lkp);
356 break;
357
358 case LK_DRAIN:
359 /*
360 * Check that we do not already hold the lock, as it can
361 * never drain if we do. Unfortunately, we have no way to
362 * check for holding a shared lock, but at least we can
363 * check for an exclusive one.
364 */
365 if (lkp->lk_lockholder == pid)
366 panic("lockmgr: draining against myself");
367 /*
368 * If we are just polling, check to see if we will sleep.
369 */
370 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
371 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
372 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
373 error = EBUSY;
374 break;
375 }
376 for (error = 0; ((lkp->lk_flags &
377 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
378 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) {
379 lkp->lk_flags |= LK_WAITDRAIN;
380 simple_unlock(&lkp->lk_interlock);
381 if ((error = tsleep((void *)&lkp->lk_flags,
382 lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
383 return (error);
384 if ((extflags) & LK_SLEEPFAIL)
385 return (ENOLCK);
386 simple_lock(&lkp->lk_interlock);
387 }
388 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
389 lkp->lk_lockholder = pid;
390 lkp->lk_exclusivecount = 1;
391 /* XXX unlikely that we'd want this */
392 if (extflags & LK_SETRECURSE)
393 lkp->lk_recurselevel = 1;
394 COUNT(p, 1);
395 break;
396
397 default:
398 simple_unlock(&lkp->lk_interlock);
399 panic("lockmgr: unknown locktype request %d",
400 flags & LK_TYPE_MASK);
401 /* NOTREACHED */
402 }
403 if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
404 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
405 lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
406 lkp->lk_flags &= ~LK_WAITDRAIN;
407 wakeup((void *)&lkp->lk_flags);
408 }
409 simple_unlock(&lkp->lk_interlock);
410 return (error);
411 }
412
413 /*
414 * Print out information about state of a lock. Used by VOP_PRINT
415 * routines to display ststus about contained locks.
416 */
417 void
418 lockmgr_printinfo(lkp)
419 struct lock *lkp;
420 {
421
422 if (lkp->lk_sharecount)
423 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
424 lkp->lk_sharecount);
425 else if (lkp->lk_flags & LK_HAVE_EXCL)
426 printf(" lock type %s: EXCL (count %d) by pid %d",
427 lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
428 if (lkp->lk_waitcount > 0)
429 printf(" with %d pending", lkp->lk_waitcount);
430 }
431
432 #if defined(LOCKDEBUG) && !defined(MULTIPROCESSOR)
433 LIST_HEAD(slocklist, simplelock) slockdebuglist;
434 int simple_lock_debugger = 0;
435
436 /*
437 * Simple lock functions so that the debugger can see from whence
438 * they are being called.
439 */
440 void
441 simple_lock_init(alp)
442 struct simplelock *alp;
443 {
444 alp->lock_data = 0;
445 alp->lock_file = NULL;
446 alp->lock_line = 0;
447 alp->unlock_file = NULL;
448 alp->unlock_line = 0;
449 alp->lock_holder = 0;
450 }
451
452 void
453 _simple_lock(alp, id, l)
454 __volatile struct simplelock *alp;
455 const char *id;
456 int l;
457 {
458 int s;
459
460 s = splhigh();
461 if (alp->lock_data == 1) {
462 printf("simple_lock: lock held\n");
463 printf("currently at: %s:%d\n", id, l);
464 printf("last locked: %s:%d\n",
465 alp->lock_file, alp->lock_line);
466 printf("last unlocked: %s:%d\n",
467 alp->unlock_file, alp->unlock_line);
468 if (simple_lock_debugger) {
469 Debugger();
470 }
471 splx(s);
472 return;
473 }
474 LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
475 alp->lock_data = 1;
476 alp->lock_file = id;
477 alp->lock_line = l;
478 if (curproc)
479 curproc->p_simple_locks++;
480 splx(s);
481 }
482
483 int
484 _simple_lock_try(alp, id, l)
485 __volatile struct simplelock *alp;
486 const char *id;
487 int l;
488 {
489 int s;
490
491 s = splhigh();
492 if (alp->lock_data != 0) {
493 printf("simple_lock_try: lock held\n");
494 printf("currently at: %s:%d\n", id, l);
495 printf("last locked: %s:%d\n",
496 alp->lock_file, alp->lock_line);
497 printf("last unlocked: %s:%d\n",
498 alp->unlock_file, alp->unlock_line);
499 if (simple_lock_debugger) {
500 Debugger();
501 }
502 splx(s);
503 return (0);
504 }
505
506 alp->lock_data = 1;
507 alp->lock_file = id;
508 alp->lock_line = l;
509 LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
510 if (curproc)
511 curproc->p_simple_locks++;
512 splx(s);
513
514 return (1);
515 }
516
517 void
518 _simple_unlock(alp, id, l)
519 __volatile struct simplelock *alp;
520 const char *id;
521 int l;
522 {
523 int s;
524
525 s = splhigh();
526 if (alp->lock_data == 0) {
527 printf("simple_unlock: lock not held\n");
528 printf("currently at: %s:%d\n", id, l);
529 printf("last locked: %s:%d\n",
530 alp->lock_file, alp->lock_line);
531 printf("last unlocked: %s:%d\n",
532 alp->unlock_file, alp->unlock_line);
533 if (simple_lock_debugger) {
534 Debugger();
535 }
536 splx(s);
537 return;
538 }
539
540 LIST_REMOVE(alp, list);
541 alp->list.le_next = NULL;
542 alp->list.le_prev = NULL;
543 alp->lock_data = 0;
544 alp->unlock_file = id;
545 alp->unlock_line = l;
546 if (curproc)
547 curproc->p_simple_locks--;
548 splx(s);
549 }
550
551 void
552 simple_lock_dump()
553 {
554 struct simplelock *alp;
555 int s;
556
557 s = splhigh();
558 printf("all simple locks:\n");
559 for (alp = LIST_FIRST(&slockdebuglist);
560 alp != NULL;
561 alp = LIST_NEXT(alp, list)) {
562 printf("%p %s:%d\n", alp, alp->lock_file, alp->lock_line);
563 }
564 splx(s);
565 }
566
567 void
568 simple_lock_freecheck(start, end)
569 void *start, *end;
570 {
571 struct simplelock *alp;
572 int s;
573
574 s = splhigh();
575 for (alp = LIST_FIRST(&slockdebuglist);
576 alp != NULL;
577 alp = LIST_NEXT(alp, list)) {
578 if ((void *)alp >= start && (void *)alp < end) {
579 printf("freeing simple_lock %p %s:%d\n",
580 alp, alp->lock_file, alp->lock_line);
581 #ifdef DDB
582 Debugger();
583 #endif
584 }
585 }
586 splx(s);
587 }
588 #endif /* LOCKDEBUG && ! MULTIPROCESSOR */
589