FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_lock.c
1 /*
2 * Copyright (c) 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Copyright (C) 1997
6 * John S. Dyson. All rights reserved.
7 *
8 * This code contains ideas from software contributed to Berkeley by
9 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
10 * System project at Carnegie-Mellon University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
41 */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: releng/5.2/sys/kern/kern_lock.c 117660 2003-07-16 01:00:39Z truckman $");
45
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/ktr.h>
49 #include <sys/lock.h>
50 #include <sys/lockmgr.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/systm.h>
54
55 /*
56 * Locking primitives implementation.
57 * Locks provide shared/exclusive sychronization.
58 */
59
60 #define LOCK_WAIT_TIME 100
61 #define LOCK_SAMPLE_WAIT 7
62
63 #if defined(DIAGNOSTIC)
64 #define LOCK_INLINE
65 #else
66 #define LOCK_INLINE __inline
67 #endif
68
69 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
70 LK_SHARE_NONZERO | LK_WAIT_NONZERO)
71
72 /*
73 * Mutex array variables. Rather than each lockmgr lock having its own mutex,
74 * share a fixed (at boot time) number of mutexes across all lockmgr locks in
75 * order to keep sizeof(struct lock) down.
76 */
77 static struct mtx lock_mtx;
78
79 static int acquire(struct lock **lkpp, int extflags, int wanted);
80 static int apause(struct lock *lkp, int flags);
81 static int acquiredrain(struct lock *lkp, int extflags) ;
82
83 static void
84 lockmgr_init(void *dummy __unused)
85 {
86 mtx_init(&lock_mtx, "lockmgr", NULL, MTX_DEF);
87 }
88 SYSINIT(lmgrinit, SI_SUB_LOCKMGR, SI_ORDER_FIRST, lockmgr_init, NULL)
89
90 static LOCK_INLINE void
91 sharelock(struct lock *lkp, int incr) {
92 lkp->lk_flags |= LK_SHARE_NONZERO;
93 lkp->lk_sharecount += incr;
94 }
95
96 static LOCK_INLINE void
97 shareunlock(struct lock *lkp, int decr) {
98
99 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
100
101 if (lkp->lk_sharecount == decr) {
102 lkp->lk_flags &= ~LK_SHARE_NONZERO;
103 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
104 wakeup(lkp);
105 }
106 lkp->lk_sharecount = 0;
107 } else {
108 lkp->lk_sharecount -= decr;
109 }
110 }
111
112 /*
113 * This is the waitloop optimization.
114 */
115 static int
116 apause(struct lock *lkp, int flags)
117 {
118 #ifdef SMP
119 int i, lock_wait;
120 #endif
121
122 if ((lkp->lk_flags & flags) == 0)
123 return 0;
124 #ifdef SMP
125 for (lock_wait = LOCK_WAIT_TIME; lock_wait > 0; lock_wait--) {
126 mtx_unlock(lkp->lk_interlock);
127 for (i = LOCK_SAMPLE_WAIT; i > 0; i--)
128 if ((lkp->lk_flags & flags) == 0)
129 break;
130 mtx_lock(lkp->lk_interlock);
131 if ((lkp->lk_flags & flags) == 0)
132 return 0;
133 }
134 #endif
135 return 1;
136 }
137
138 static int
139 acquire(struct lock **lkpp, int extflags, int wanted) {
140 struct lock *lkp = *lkpp;
141 int s, error;
142
143 CTR3(KTR_LOCK,
144 "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x\n",
145 lkp, extflags, wanted);
146
147 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) {
148 return EBUSY;
149 }
150
151 if (((lkp->lk_flags | extflags) & LK_NOPAUSE) == 0) {
152 error = apause(lkp, wanted);
153 if (error == 0)
154 return 0;
155 }
156
157 s = splhigh();
158 while ((lkp->lk_flags & wanted) != 0) {
159 lkp->lk_flags |= LK_WAIT_NONZERO;
160 lkp->lk_waitcount++;
161 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio,
162 lkp->lk_wmesg,
163 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
164 if (lkp->lk_waitcount == 1) {
165 lkp->lk_flags &= ~LK_WAIT_NONZERO;
166 lkp->lk_waitcount = 0;
167 } else {
168 lkp->lk_waitcount--;
169 }
170 if (error) {
171 splx(s);
172 return error;
173 }
174 if (extflags & LK_SLEEPFAIL) {
175 splx(s);
176 return ENOLCK;
177 }
178 if (lkp->lk_newlock != NULL) {
179 mtx_lock(lkp->lk_newlock->lk_interlock);
180 mtx_unlock(lkp->lk_interlock);
181 if (lkp->lk_waitcount == 0)
182 wakeup((void *)(&lkp->lk_newlock));
183 *lkpp = lkp = lkp->lk_newlock;
184 }
185 }
186 splx(s);
187 return 0;
188 }
189
190 /*
191 * Set, change, or release a lock.
192 *
193 * Shared requests increment the shared count. Exclusive requests set the
194 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
195 * accepted shared locks and shared-to-exclusive upgrades to go away.
196 */
197 int
198 #ifndef DEBUG_LOCKS
199 lockmgr(lkp, flags, interlkp, td)
200 #else
201 debuglockmgr(lkp, flags, interlkp, td, name, file, line)
202 #endif
203 struct lock *lkp;
204 u_int flags;
205 struct mtx *interlkp;
206 struct thread *td;
207 #ifdef DEBUG_LOCKS
208 const char *name; /* Name of lock function */
209 const char *file; /* Name of file call is from */
210 int line; /* Line number in file */
211 #endif
212 {
213 int error;
214 struct thread *thr;
215 int extflags, lockflags;
216
217 CTR5(KTR_LOCK,
218 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), flags == 0x%x, "
219 "interlkp == %p, td == %p", lkp, lkp->lk_wmesg, flags, interlkp, td);
220
221 error = 0;
222 if (td == NULL)
223 thr = LK_KERNPROC;
224 else
225 thr = td;
226
227 if ((flags & LK_INTERNAL) == 0)
228 mtx_lock(lkp->lk_interlock);
229 if (flags & LK_INTERLOCK) {
230 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED);
231 mtx_unlock(interlkp);
232 }
233
234 if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0)
235 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
236 &lkp->lk_interlock->mtx_object,
237 "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg);
238
239 if (panicstr != NULL) {
240 mtx_unlock(lkp->lk_interlock);
241 return (0);
242 }
243
244 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
245
246 switch (flags & LK_TYPE_MASK) {
247
248 case LK_SHARED:
249 /*
250 * If we are not the exclusive lock holder, we have to block
251 * while there is an exclusive lock holder or while an
252 * exclusive lock request or upgrade request is in progress.
253 *
254 * However, if TDF_DEADLKTREAT is set, we override exclusive
255 * lock requests or upgrade requests ( but not the exclusive
256 * lock itself ).
257 */
258 if (lkp->lk_lockholder != thr) {
259 lockflags = LK_HAVE_EXCL;
260 mtx_lock_spin(&sched_lock);
261 if (td != NULL && !(td->td_flags & TDF_DEADLKTREAT))
262 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE;
263 mtx_unlock_spin(&sched_lock);
264 error = acquire(&lkp, extflags, lockflags);
265 if (error)
266 break;
267 sharelock(lkp, 1);
268 #if defined(DEBUG_LOCKS)
269 lkp->lk_slockholder = thr;
270 lkp->lk_sfilename = file;
271 lkp->lk_slineno = line;
272 lkp->lk_slockername = name;
273 #endif
274 break;
275 }
276 /*
277 * We hold an exclusive lock, so downgrade it to shared.
278 * An alternative would be to fail with EDEADLK.
279 */
280 sharelock(lkp, 1);
281 /* FALLTHROUGH downgrade */
282
283 case LK_DOWNGRADE:
284 KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0,
285 ("lockmgr: not holding exclusive lock "
286 "(owner thread (%p) != thread (%p), exlcnt (%d) != 0",
287 lkp->lk_lockholder, thr, lkp->lk_exclusivecount));
288 sharelock(lkp, lkp->lk_exclusivecount);
289 lkp->lk_exclusivecount = 0;
290 lkp->lk_flags &= ~LK_HAVE_EXCL;
291 lkp->lk_lockholder = LK_NOPROC;
292 if (lkp->lk_waitcount)
293 wakeup((void *)lkp);
294 break;
295
296 case LK_EXCLUPGRADE:
297 /*
298 * If another process is ahead of us to get an upgrade,
299 * then we want to fail rather than have an intervening
300 * exclusive access.
301 */
302 if (lkp->lk_flags & LK_WANT_UPGRADE) {
303 shareunlock(lkp, 1);
304 error = EBUSY;
305 break;
306 }
307 /* FALLTHROUGH normal upgrade */
308
309 case LK_UPGRADE:
310 /*
311 * Upgrade a shared lock to an exclusive one. If another
312 * shared lock has already requested an upgrade to an
313 * exclusive lock, our shared lock is released and an
314 * exclusive lock is requested (which will be granted
315 * after the upgrade). If we return an error, the file
316 * will always be unlocked.
317 */
318 if ((lkp->lk_lockholder == thr) || (lkp->lk_sharecount <= 0))
319 panic("lockmgr: upgrade exclusive lock");
320 shareunlock(lkp, 1);
321 /*
322 * If we are just polling, check to see if we will block.
323 */
324 if ((extflags & LK_NOWAIT) &&
325 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
326 lkp->lk_sharecount > 1)) {
327 error = EBUSY;
328 break;
329 }
330 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
331 /*
332 * We are first shared lock to request an upgrade, so
333 * request upgrade and wait for the shared count to
334 * drop to zero, then take exclusive lock.
335 */
336 lkp->lk_flags |= LK_WANT_UPGRADE;
337 error = acquire(&lkp, extflags, LK_SHARE_NONZERO);
338 lkp->lk_flags &= ~LK_WANT_UPGRADE;
339
340 if (error)
341 break;
342 lkp->lk_flags |= LK_HAVE_EXCL;
343 lkp->lk_lockholder = thr;
344 if (lkp->lk_exclusivecount != 0)
345 panic("lockmgr: non-zero exclusive count");
346 lkp->lk_exclusivecount = 1;
347 #if defined(DEBUG_LOCKS)
348 lkp->lk_filename = file;
349 lkp->lk_lineno = line;
350 lkp->lk_lockername = name;
351 #endif
352 break;
353 }
354 /*
355 * Someone else has requested upgrade. Release our shared
356 * lock, awaken upgrade requestor if we are the last shared
357 * lock, then request an exclusive lock.
358 */
359 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
360 LK_WAIT_NONZERO)
361 wakeup((void *)lkp);
362 /* FALLTHROUGH exclusive request */
363
364 case LK_EXCLUSIVE:
365 if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) {
366 /*
367 * Recursive lock.
368 */
369 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
370 panic("lockmgr: locking against myself");
371 if ((extflags & LK_CANRECURSE) != 0) {
372 lkp->lk_exclusivecount++;
373 break;
374 }
375 }
376 /*
377 * If we are just polling, check to see if we will sleep.
378 */
379 if ((extflags & LK_NOWAIT) &&
380 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
381 error = EBUSY;
382 break;
383 }
384 /*
385 * Try to acquire the want_exclusive flag.
386 */
387 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL));
388 if (error)
389 break;
390 lkp->lk_flags |= LK_WANT_EXCL;
391 /*
392 * Wait for shared locks and upgrades to finish.
393 */
394 error = acquire(&lkp, extflags, LK_WANT_UPGRADE | LK_SHARE_NONZERO);
395 lkp->lk_flags &= ~LK_WANT_EXCL;
396 if (error)
397 break;
398 lkp->lk_flags |= LK_HAVE_EXCL;
399 lkp->lk_lockholder = thr;
400 if (lkp->lk_exclusivecount != 0)
401 panic("lockmgr: non-zero exclusive count");
402 lkp->lk_exclusivecount = 1;
403 #if defined(DEBUG_LOCKS)
404 lkp->lk_filename = file;
405 lkp->lk_lineno = line;
406 lkp->lk_lockername = name;
407 #endif
408 break;
409
410 case LK_RELEASE:
411 if (lkp->lk_exclusivecount != 0) {
412 if (lkp->lk_lockholder != thr &&
413 lkp->lk_lockholder != LK_KERNPROC) {
414 panic("lockmgr: thread %p, not %s %p unlocking",
415 thr, "exclusive lock holder",
416 lkp->lk_lockholder);
417 }
418 if (lkp->lk_exclusivecount == 1) {
419 lkp->lk_flags &= ~LK_HAVE_EXCL;
420 lkp->lk_lockholder = LK_NOPROC;
421 lkp->lk_exclusivecount = 0;
422 } else {
423 lkp->lk_exclusivecount--;
424 }
425 } else if (lkp->lk_flags & LK_SHARE_NONZERO)
426 shareunlock(lkp, 1);
427 if (lkp->lk_flags & LK_WAIT_NONZERO)
428 wakeup((void *)lkp);
429 break;
430
431 case LK_DRAIN:
432 /*
433 * Check that we do not already hold the lock, as it can
434 * never drain if we do. Unfortunately, we have no way to
435 * check for holding a shared lock, but at least we can
436 * check for an exclusive one.
437 */
438 if (lkp->lk_lockholder == thr)
439 panic("lockmgr: draining against myself");
440
441 error = acquiredrain(lkp, extflags);
442 if (error)
443 break;
444 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
445 lkp->lk_lockholder = thr;
446 lkp->lk_exclusivecount = 1;
447 #if defined(DEBUG_LOCKS)
448 lkp->lk_filename = file;
449 lkp->lk_lineno = line;
450 lkp->lk_lockername = name;
451 #endif
452 break;
453
454 default:
455 mtx_unlock(lkp->lk_interlock);
456 panic("lockmgr: unknown locktype request %d",
457 flags & LK_TYPE_MASK);
458 /* NOTREACHED */
459 }
460 if ((lkp->lk_flags & LK_WAITDRAIN) &&
461 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
462 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
463 lkp->lk_flags &= ~LK_WAITDRAIN;
464 wakeup((void *)&lkp->lk_flags);
465 }
466 mtx_unlock(lkp->lk_interlock);
467 return (error);
468 }
469
470 static int
471 acquiredrain(struct lock *lkp, int extflags) {
472 int error;
473
474 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
475 return EBUSY;
476 }
477
478 error = apause(lkp, LK_ALL);
479 if (error == 0)
480 return 0;
481
482 while (lkp->lk_flags & LK_ALL) {
483 lkp->lk_flags |= LK_WAITDRAIN;
484 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio,
485 lkp->lk_wmesg,
486 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
487 if (error)
488 return error;
489 if (extflags & LK_SLEEPFAIL) {
490 return ENOLCK;
491 }
492 }
493 return 0;
494 }
495
496 /*
497 * Transfer any waiting processes from one lock to another.
498 */
499 void
500 transferlockers(from, to)
501 struct lock *from;
502 struct lock *to;
503 {
504
505 KASSERT(from != to, ("lock transfer to self"));
506 KASSERT((from->lk_flags&LK_WAITDRAIN) == 0, ("transfer draining lock"));
507 if (from->lk_waitcount == 0)
508 return;
509 from->lk_newlock = to;
510 wakeup((void *)from);
511 msleep(&from->lk_newlock, NULL, from->lk_prio, "lkxfer", 0);
512 from->lk_newlock = NULL;
513 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
514 KASSERT(from->lk_waitcount == 0, ("active lock"));
515 }
516
517
518 /*
519 * Initialize a lock; required before use.
520 */
521 void
522 lockinit(lkp, prio, wmesg, timo, flags)
523 struct lock *lkp;
524 int prio;
525 const char *wmesg;
526 int timo;
527 int flags;
528 {
529 CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", "
530 "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags);
531
532 lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder);
533 lkp->lk_flags = (flags & LK_EXTFLG_MASK);
534 lkp->lk_sharecount = 0;
535 lkp->lk_waitcount = 0;
536 lkp->lk_exclusivecount = 0;
537 lkp->lk_prio = prio;
538 lkp->lk_wmesg = wmesg;
539 lkp->lk_timo = timo;
540 lkp->lk_lockholder = LK_NOPROC;
541 lkp->lk_newlock = NULL;
542 #ifdef DEBUG_LOCKS
543 lkp->lk_filename = "none";
544 lkp->lk_lockername = "never exclusive locked";
545 lkp->lk_lineno = 0;
546 lkp->lk_slockholder = LK_NOPROC;
547 lkp->lk_sfilename = "none";
548 lkp->lk_slockername = "never share locked";
549 lkp->lk_slineno = 0;
550 #endif
551 }
552
553 /*
554 * Destroy a lock.
555 */
556 void
557 lockdestroy(lkp)
558 struct lock *lkp;
559 {
560 CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")",
561 lkp, lkp->lk_wmesg);
562 }
563
564 /*
565 * Determine the status of a lock.
566 */
567 int
568 lockstatus(lkp, td)
569 struct lock *lkp;
570 struct thread *td;
571 {
572 int lock_type = 0;
573
574 mtx_lock(lkp->lk_interlock);
575 if (lkp->lk_exclusivecount != 0) {
576 if (td == NULL || lkp->lk_lockholder == td)
577 lock_type = LK_EXCLUSIVE;
578 else
579 lock_type = LK_EXCLOTHER;
580 } else if (lkp->lk_sharecount != 0)
581 lock_type = LK_SHARED;
582 mtx_unlock(lkp->lk_interlock);
583 return (lock_type);
584 }
585
586 /*
587 * Determine the number of holders of a lock.
588 */
589 int
590 lockcount(lkp)
591 struct lock *lkp;
592 {
593 int count;
594
595 mtx_lock(lkp->lk_interlock);
596 count = lkp->lk_exclusivecount + lkp->lk_sharecount;
597 mtx_unlock(lkp->lk_interlock);
598 return (count);
599 }
600
601 /*
602 * Print out information about state of a lock. Used by VOP_PRINT
603 * routines to display status about contained locks.
604 */
605 void
606 lockmgr_printinfo(lkp)
607 struct lock *lkp;
608 {
609
610 if (lkp->lk_sharecount)
611 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
612 lkp->lk_sharecount);
613 else if (lkp->lk_flags & LK_HAVE_EXCL)
614 printf(" lock type %s: EXCL (count %d) by thread %p",
615 lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
616 if (lkp->lk_waitcount > 0)
617 printf(" with %d pending", lkp->lk_waitcount);
618 }
Cache object: d7af75b237d7a535d2ec1d80842a145b
|