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$");
45
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/kdb.h>
49 #include <sys/ktr.h>
50 #include <sys/lock.h>
51 #include <sys/lockmgr.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/systm.h>
55
56 /*
57 * Locking primitives implementation.
58 * Locks provide shared/exclusive sychronization.
59 */
60
61 #define LOCK_WAIT_TIME 100
62 #define LOCK_SAMPLE_WAIT 7
63
64 #if defined(DIAGNOSTIC)
65 #define LOCK_INLINE
66 #else
67 #define LOCK_INLINE __inline
68 #endif
69
70 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
71 LK_SHARE_NONZERO | LK_WAIT_NONZERO)
72
73 /*
74 * Mutex array variables. Rather than each lockmgr lock having its own mutex,
75 * share a fixed (at boot time) number of mutexes across all lockmgr locks in
76 * order to keep sizeof(struct lock) down.
77 */
78 static struct mtx lock_mtx;
79
80 static int acquire(struct lock **lkpp, int extflags, int wanted);
81 static int apause(struct lock *lkp, int flags);
82 static int acquiredrain(struct lock *lkp, int extflags) ;
83
84 static void
85 lockmgr_init(void *dummy __unused)
86 {
87 mtx_init(&lock_mtx, "lockmgr", NULL, MTX_DEF);
88 }
89 SYSINIT(lmgrinit, SI_SUB_LOCKMGR, SI_ORDER_FIRST, lockmgr_init, NULL)
90
91 static LOCK_INLINE void
92 sharelock(struct lock *lkp, int incr) {
93 lkp->lk_flags |= LK_SHARE_NONZERO;
94 lkp->lk_sharecount += incr;
95 }
96
97 static LOCK_INLINE void
98 shareunlock(struct lock *lkp, int decr) {
99
100 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
101
102 if (lkp->lk_sharecount == decr) {
103 lkp->lk_flags &= ~LK_SHARE_NONZERO;
104 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
105 wakeup(lkp);
106 }
107 lkp->lk_sharecount = 0;
108 } else {
109 lkp->lk_sharecount -= decr;
110 }
111 }
112
113 /*
114 * This is the waitloop optimization.
115 */
116 static int
117 apause(struct lock *lkp, int flags)
118 {
119 #ifdef SMP
120 int i, lock_wait;
121 #endif
122
123 if ((lkp->lk_flags & flags) == 0)
124 return 0;
125 #ifdef SMP
126 for (lock_wait = LOCK_WAIT_TIME; lock_wait > 0; lock_wait--) {
127 mtx_unlock(lkp->lk_interlock);
128 for (i = LOCK_SAMPLE_WAIT; i > 0; i--)
129 if ((lkp->lk_flags & flags) == 0)
130 break;
131 mtx_lock(lkp->lk_interlock);
132 if ((lkp->lk_flags & flags) == 0)
133 return 0;
134 }
135 #endif
136 return 1;
137 }
138
139 static int
140 acquire(struct lock **lkpp, int extflags, int wanted) {
141 struct lock *lkp = *lkpp;
142 int s, error;
143
144 CTR3(KTR_LOCK,
145 "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x",
146 lkp, extflags, wanted);
147
148 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) {
149 return EBUSY;
150 }
151
152 if (((lkp->lk_flags | extflags) & LK_NOPAUSE) == 0) {
153 error = apause(lkp, wanted);
154 if (error == 0)
155 return 0;
156 }
157
158 s = splhigh();
159 while ((lkp->lk_flags & wanted) != 0) {
160 lkp->lk_flags |= LK_WAIT_NONZERO;
161 lkp->lk_waitcount++;
162 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio,
163 lkp->lk_wmesg,
164 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
165 if (lkp->lk_waitcount == 1) {
166 lkp->lk_flags &= ~LK_WAIT_NONZERO;
167 lkp->lk_waitcount = 0;
168 } else {
169 lkp->lk_waitcount--;
170 }
171 if (error) {
172 splx(s);
173 return error;
174 }
175 if (extflags & LK_SLEEPFAIL) {
176 splx(s);
177 return ENOLCK;
178 }
179 if (lkp->lk_newlock != NULL) {
180 mtx_lock(lkp->lk_newlock->lk_interlock);
181 mtx_unlock(lkp->lk_interlock);
182 if (lkp->lk_waitcount == 0)
183 wakeup((void *)(&lkp->lk_newlock));
184 *lkpp = lkp = lkp->lk_newlock;
185 }
186 }
187 splx(s);
188 return 0;
189 }
190
191 /*
192 * Set, change, or release a lock.
193 *
194 * Shared requests increment the shared count. Exclusive requests set the
195 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
196 * accepted shared locks and shared-to-exclusive upgrades to go away.
197 */
198 int
199 #ifndef DEBUG_LOCKS
200 lockmgr(lkp, flags, interlkp, td)
201 #else
202 debuglockmgr(lkp, flags, interlkp, td, name, file, line)
203 #endif
204 struct lock *lkp;
205 u_int flags;
206 struct mtx *interlkp;
207 struct thread *td;
208 #ifdef DEBUG_LOCKS
209 const char *name; /* Name of lock function */
210 const char *file; /* Name of file call is from */
211 int line; /* Line number in file */
212 #endif
213 {
214 int error;
215 struct thread *thr;
216 int extflags, lockflags;
217
218 CTR5(KTR_LOCK,
219 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), flags == 0x%x, "
220 "interlkp == %p, td == %p", lkp, lkp->lk_wmesg, flags, interlkp, td);
221
222 error = 0;
223 if (td == NULL)
224 thr = LK_KERNPROC;
225 else
226 thr = td;
227
228 if ((flags & LK_INTERNAL) == 0)
229 mtx_lock(lkp->lk_interlock);
230 if (flags & LK_INTERLOCK) {
231 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED);
232 mtx_unlock(interlkp);
233 }
234
235 if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0)
236 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
237 &lkp->lk_interlock->mtx_object,
238 "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg);
239
240 if (panicstr != NULL) {
241 mtx_unlock(lkp->lk_interlock);
242 return (0);
243 }
244
245 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
246
247 switch (flags & LK_TYPE_MASK) {
248
249 case LK_SHARED:
250 /*
251 * If we are not the exclusive lock holder, we have to block
252 * while there is an exclusive lock holder or while an
253 * exclusive lock request or upgrade request is in progress.
254 *
255 * However, if TDP_DEADLKTREAT is set, we override exclusive
256 * lock requests or upgrade requests ( but not the exclusive
257 * lock itself ).
258 */
259 if (lkp->lk_lockholder != thr) {
260 lockflags = LK_HAVE_EXCL;
261 if (td != NULL && !(td->td_pflags & TDP_DEADLKTREAT))
262 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE;
263 error = acquire(&lkp, extflags, lockflags);
264 if (error)
265 break;
266 sharelock(lkp, 1);
267 #if defined(DEBUG_LOCKS)
268 lkp->lk_slockholder = thr;
269 lkp->lk_sfilename = file;
270 lkp->lk_slineno = line;
271 lkp->lk_slockername = name;
272 #endif
273 break;
274 }
275 /*
276 * We hold an exclusive lock, so downgrade it to shared.
277 * An alternative would be to fail with EDEADLK.
278 */
279 sharelock(lkp, 1);
280 /* FALLTHROUGH downgrade */
281
282 case LK_DOWNGRADE:
283 KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0,
284 ("lockmgr: not holding exclusive lock "
285 "(owner thread (%p) != thread (%p), exlcnt (%d) != 0",
286 lkp->lk_lockholder, thr, lkp->lk_exclusivecount));
287 sharelock(lkp, lkp->lk_exclusivecount);
288 lkp->lk_exclusivecount = 0;
289 lkp->lk_flags &= ~LK_HAVE_EXCL;
290 lkp->lk_lockholder = LK_NOPROC;
291 if (lkp->lk_waitcount)
292 wakeup((void *)lkp);
293 break;
294
295 case LK_EXCLUPGRADE:
296 /*
297 * If another process is ahead of us to get an upgrade,
298 * then we want to fail rather than have an intervening
299 * exclusive access.
300 */
301 if (lkp->lk_flags & LK_WANT_UPGRADE) {
302 shareunlock(lkp, 1);
303 error = EBUSY;
304 break;
305 }
306 /* FALLTHROUGH normal upgrade */
307
308 case LK_UPGRADE:
309 /*
310 * Upgrade a shared lock to an exclusive one. If another
311 * shared lock has already requested an upgrade to an
312 * exclusive lock, our shared lock is released and an
313 * exclusive lock is requested (which will be granted
314 * after the upgrade). If we return an error, the file
315 * will always be unlocked.
316 */
317 if ((lkp->lk_lockholder == thr) || (lkp->lk_sharecount <= 0))
318 panic("lockmgr: upgrade exclusive lock");
319 shareunlock(lkp, 1);
320 /*
321 * If we are just polling, check to see if we will block.
322 */
323 if ((extflags & LK_NOWAIT) &&
324 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
325 lkp->lk_sharecount > 1)) {
326 error = EBUSY;
327 break;
328 }
329 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
330 /*
331 * We are first shared lock to request an upgrade, so
332 * request upgrade and wait for the shared count to
333 * drop to zero, then take exclusive lock.
334 */
335 lkp->lk_flags |= LK_WANT_UPGRADE;
336 error = acquire(&lkp, extflags, LK_SHARE_NONZERO);
337 lkp->lk_flags &= ~LK_WANT_UPGRADE;
338
339 if (error) {
340 if ((lkp->lk_flags & ( LK_WANT_EXCL | LK_WAIT_NONZERO)) == (LK_WANT_EXCL | LK_WAIT_NONZERO))
341 wakeup((void *)lkp);
342 break;
343 }
344
345 lkp->lk_flags |= LK_HAVE_EXCL;
346 lkp->lk_lockholder = thr;
347 if (lkp->lk_exclusivecount != 0)
348 panic("lockmgr: non-zero exclusive count");
349 lkp->lk_exclusivecount = 1;
350 #if defined(DEBUG_LOCKS)
351 lkp->lk_filename = file;
352 lkp->lk_lineno = line;
353 lkp->lk_lockername = name;
354 #endif
355 break;
356 }
357 /*
358 * Someone else has requested upgrade. Release our shared
359 * lock, awaken upgrade requestor if we are the last shared
360 * lock, then request an exclusive lock.
361 */
362 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
363 LK_WAIT_NONZERO)
364 wakeup((void *)lkp);
365 /* FALLTHROUGH exclusive request */
366
367 case LK_EXCLUSIVE:
368 if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) {
369 /*
370 * Recursive lock.
371 */
372 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
373 panic("lockmgr: locking against myself");
374 if ((extflags & LK_CANRECURSE) != 0) {
375 lkp->lk_exclusivecount++;
376 break;
377 }
378 }
379 /*
380 * If we are just polling, check to see if we will sleep.
381 */
382 if ((extflags & LK_NOWAIT) &&
383 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
384 error = EBUSY;
385 break;
386 }
387 /*
388 * Try to acquire the want_exclusive flag.
389 */
390 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL));
391 if (error)
392 break;
393 lkp->lk_flags |= LK_WANT_EXCL;
394 /*
395 * Wait for shared locks and upgrades to finish.
396 */
397 error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO);
398 lkp->lk_flags &= ~LK_WANT_EXCL;
399 if (error) {
400 if (lkp->lk_flags & LK_WAIT_NONZERO)
401 wakeup((void *)lkp);
402 break;
403 }
404 lkp->lk_flags |= LK_HAVE_EXCL;
405 lkp->lk_lockholder = thr;
406 if (lkp->lk_exclusivecount != 0)
407 panic("lockmgr: non-zero exclusive count");
408 lkp->lk_exclusivecount = 1;
409 #if defined(DEBUG_LOCKS)
410 lkp->lk_filename = file;
411 lkp->lk_lineno = line;
412 lkp->lk_lockername = name;
413 #endif
414 break;
415
416 case LK_RELEASE:
417 if (lkp->lk_exclusivecount != 0) {
418 if (lkp->lk_lockholder != thr &&
419 lkp->lk_lockholder != LK_KERNPROC) {
420 panic("lockmgr: thread %p, not %s %p unlocking",
421 thr, "exclusive lock holder",
422 lkp->lk_lockholder);
423 }
424 if (lkp->lk_exclusivecount == 1) {
425 lkp->lk_flags &= ~LK_HAVE_EXCL;
426 lkp->lk_lockholder = LK_NOPROC;
427 lkp->lk_exclusivecount = 0;
428 } else {
429 lkp->lk_exclusivecount--;
430 }
431 } else if (lkp->lk_flags & LK_SHARE_NONZERO)
432 shareunlock(lkp, 1);
433 if (lkp->lk_flags & LK_WAIT_NONZERO)
434 wakeup((void *)lkp);
435 break;
436
437 case LK_DRAIN:
438 /*
439 * Check that we do not already hold the lock, as it can
440 * never drain if we do. Unfortunately, we have no way to
441 * check for holding a shared lock, but at least we can
442 * check for an exclusive one.
443 */
444 if (lkp->lk_lockholder == thr)
445 panic("lockmgr: draining against myself");
446
447 error = acquiredrain(lkp, extflags);
448 if (error)
449 break;
450 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
451 lkp->lk_lockholder = thr;
452 lkp->lk_exclusivecount = 1;
453 #if defined(DEBUG_LOCKS)
454 lkp->lk_filename = file;
455 lkp->lk_lineno = line;
456 lkp->lk_lockername = name;
457 #endif
458 break;
459
460 default:
461 mtx_unlock(lkp->lk_interlock);
462 panic("lockmgr: unknown locktype request %d",
463 flags & LK_TYPE_MASK);
464 /* NOTREACHED */
465 }
466 if ((lkp->lk_flags & LK_WAITDRAIN) &&
467 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
468 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
469 lkp->lk_flags &= ~LK_WAITDRAIN;
470 wakeup((void *)&lkp->lk_flags);
471 }
472 mtx_unlock(lkp->lk_interlock);
473 return (error);
474 }
475
476 static int
477 acquiredrain(struct lock *lkp, int extflags) {
478 int error;
479
480 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
481 return EBUSY;
482 }
483
484 error = apause(lkp, LK_ALL);
485 if (error == 0)
486 return 0;
487
488 while (lkp->lk_flags & LK_ALL) {
489 lkp->lk_flags |= LK_WAITDRAIN;
490 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio,
491 lkp->lk_wmesg,
492 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
493 if (error)
494 return error;
495 if (extflags & LK_SLEEPFAIL) {
496 return ENOLCK;
497 }
498 }
499 return 0;
500 }
501
502 /*
503 * Transfer any waiting processes from one lock to another.
504 */
505 void
506 transferlockers(from, to)
507 struct lock *from;
508 struct lock *to;
509 {
510
511 KASSERT(from != to, ("lock transfer to self"));
512 KASSERT((from->lk_flags&LK_WAITDRAIN) == 0, ("transfer draining lock"));
513 if (from->lk_waitcount == 0)
514 return;
515 from->lk_newlock = to;
516 wakeup((void *)from);
517 msleep(&from->lk_newlock, NULL, from->lk_prio, "lkxfer", 0);
518 from->lk_newlock = NULL;
519 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
520 KASSERT(from->lk_waitcount == 0, ("active lock"));
521 }
522
523
524 /*
525 * Initialize a lock; required before use.
526 */
527 void
528 lockinit(lkp, prio, wmesg, timo, flags)
529 struct lock *lkp;
530 int prio;
531 const char *wmesg;
532 int timo;
533 int flags;
534 {
535 CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", "
536 "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags);
537
538 lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder);
539 lkp->lk_flags = (flags & LK_EXTFLG_MASK);
540 lkp->lk_sharecount = 0;
541 lkp->lk_waitcount = 0;
542 lkp->lk_exclusivecount = 0;
543 lkp->lk_prio = prio;
544 lkp->lk_wmesg = wmesg;
545 lkp->lk_timo = timo;
546 lkp->lk_lockholder = LK_NOPROC;
547 lkp->lk_newlock = NULL;
548 #ifdef DEBUG_LOCKS
549 lkp->lk_filename = "none";
550 lkp->lk_lockername = "never exclusive locked";
551 lkp->lk_lineno = 0;
552 lkp->lk_slockholder = LK_NOPROC;
553 lkp->lk_sfilename = "none";
554 lkp->lk_slockername = "never share locked";
555 lkp->lk_slineno = 0;
556 #endif
557 }
558
559 /*
560 * Destroy a lock.
561 */
562 void
563 lockdestroy(lkp)
564 struct lock *lkp;
565 {
566 CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")",
567 lkp, lkp->lk_wmesg);
568 }
569
570 /*
571 * Determine the status of a lock.
572 */
573 int
574 lockstatus(lkp, td)
575 struct lock *lkp;
576 struct thread *td;
577 {
578 int lock_type = 0;
579 int interlocked;
580
581 if (!kdb_active) {
582 interlocked = 1;
583 mtx_lock(lkp->lk_interlock);
584 } else
585 interlocked = 0;
586 if (lkp->lk_exclusivecount != 0) {
587 if (td == NULL || lkp->lk_lockholder == td)
588 lock_type = LK_EXCLUSIVE;
589 else
590 lock_type = LK_EXCLOTHER;
591 } else if (lkp->lk_sharecount != 0)
592 lock_type = LK_SHARED;
593 if (interlocked)
594 mtx_unlock(lkp->lk_interlock);
595 return (lock_type);
596 }
597
598 /*
599 * Determine the number of holders of a lock.
600 */
601 int
602 lockcount(lkp)
603 struct lock *lkp;
604 {
605 int count;
606
607 mtx_lock(lkp->lk_interlock);
608 count = lkp->lk_exclusivecount + lkp->lk_sharecount;
609 mtx_unlock(lkp->lk_interlock);
610 return (count);
611 }
612
613 /*
614 * Print out information about state of a lock. Used by VOP_PRINT
615 * routines to display status about contained locks.
616 */
617 void
618 lockmgr_printinfo(lkp)
619 struct lock *lkp;
620 {
621
622 if (lkp->lk_sharecount)
623 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
624 lkp->lk_sharecount);
625 else if (lkp->lk_flags & LK_HAVE_EXCL)
626 printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)",
627 lkp->lk_wmesg, lkp->lk_exclusivecount,
628 lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid);
629 if (lkp->lk_waitcount > 0)
630 printf(" with %d pending", lkp->lk_waitcount);
631 }
Cache object: 6876a03a918c958e4d37ae2b76fbca76
|