3 * The Regents of the University of California. All rights reserved.
6 * John S. Dyson. All rights reserved.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
40 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/kernel.h>
50 #include <sys/lockmgr.h>
51 #include <sys/mutex.h>
53 #include <sys/systm.h>
56 * Locking primitives implementation.
57 * Locks provide shared/exclusive sychronization.
60 #define COUNT(td, x) if ((td)) (td)->td_locks += (x)
61 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
62 LK_SHARE_NONZERO | LK_WAIT_NONZERO)
64 static int acquire(struct lock **lkpp, int extflags, int wanted);
65 static int acquiredrain(struct lock *lkp, int extflags) ;
68 sharelock(struct thread *td, struct lock *lkp, int incr) {
69 lkp->lk_flags |= LK_SHARE_NONZERO;
70 lkp->lk_sharecount += incr;
75 shareunlock(struct thread *td, struct lock *lkp, int decr) {
77 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
80 if (lkp->lk_sharecount == decr) {
81 lkp->lk_flags &= ~LK_SHARE_NONZERO;
82 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
85 lkp->lk_sharecount = 0;
87 lkp->lk_sharecount -= decr;
92 acquire(struct lock **lkpp, int extflags, int wanted)
94 struct lock *lkp = *lkpp;
97 "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x",
98 lkp, extflags, wanted);
100 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted))
103 while ((lkp->lk_flags & wanted) != 0) {
105 "acquire(): lkp == %p, lk_flags == 0x%x sleeping",
107 lkp->lk_flags |= LK_WAIT_NONZERO;
109 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio,
111 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
113 if (lkp->lk_waitcount == 0)
114 lkp->lk_flags &= ~LK_WAIT_NONZERO;
117 if (extflags & LK_SLEEPFAIL) {
121 if (lkp->lk_newlock != NULL) {
122 mtx_lock(lkp->lk_newlock->lk_interlock);
123 mtx_unlock(lkp->lk_interlock);
124 if (lkp->lk_waitcount == 0)
125 wakeup((void *)(&lkp->lk_newlock));
126 *lkpp = lkp = lkp->lk_newlock;
129 mtx_assert(lkp->lk_interlock, MA_OWNED);
134 * Set, change, or release a lock.
136 * Shared requests increment the shared count. Exclusive requests set the
137 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
138 * accepted shared locks and shared-to-exclusive upgrades to go away.
142 lockmgr(lkp, flags, interlkp, td)
144 debuglockmgr(lkp, flags, interlkp, td, name, file, line)
148 struct mtx *interlkp;
151 const char *name; /* Name of lock function */
152 const char *file; /* Name of file call is from */
153 int line; /* Line number in file */
158 int extflags, lockflags;
166 if ((flags & LK_INTERNAL) == 0)
167 mtx_lock(lkp->lk_interlock);
170 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), flags == 0x%x, "
171 "td == %p %s:%d", lkp, lkp->lk_wmesg, flags, td, file, line);
174 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), owner == %p, exclusivecount == %d, flags == 0x%x, "
175 "td == %p", lkp, lkp->lk_wmesg, lkp->lk_lockholder,
176 lkp->lk_exclusivecount, flags, td);
179 if (flags & LK_INTERLOCK) {
180 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED);
181 mtx_unlock(interlkp);
184 if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0)
185 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
186 &lkp->lk_interlock->mtx_object,
187 "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg);
189 if (panicstr != NULL) {
190 mtx_unlock(lkp->lk_interlock);
193 if ((lkp->lk_flags & LK_NOSHARE) &&
194 (flags & LK_TYPE_MASK) == LK_SHARED) {
195 flags &= ~LK_TYPE_MASK;
196 flags |= LK_EXCLUSIVE;
198 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
200 switch (flags & LK_TYPE_MASK) {
204 * If we are not the exclusive lock holder, we have to block
205 * while there is an exclusive lock holder or while an
206 * exclusive lock request or upgrade request is in progress.
208 * However, if TDP_DEADLKTREAT is set, we override exclusive
209 * lock requests or upgrade requests ( but not the exclusive
212 if (lkp->lk_lockholder != thr) {
213 lockflags = LK_HAVE_EXCL;
214 if (td != NULL && !(td->td_pflags & TDP_DEADLKTREAT))
215 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE;
216 error = acquire(&lkp, extflags, lockflags);
219 sharelock(td, lkp, 1);
220 #if defined(DEBUG_LOCKS)
221 lkp->lk_slockholder = thr;
222 lkp->lk_sfilename = file;
223 lkp->lk_slineno = line;
224 lkp->lk_slockername = name;
229 * We hold an exclusive lock, so downgrade it to shared.
230 * An alternative would be to fail with EDEADLK.
232 sharelock(td, lkp, 1);
233 /* FALLTHROUGH downgrade */
236 KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0,
237 ("lockmgr: not holding exclusive lock "
238 "(owner thread (%p) != thread (%p), exlcnt (%d) != 0",
239 lkp->lk_lockholder, thr, lkp->lk_exclusivecount));
240 sharelock(td, lkp, lkp->lk_exclusivecount);
241 COUNT(td, -lkp->lk_exclusivecount);
242 lkp->lk_exclusivecount = 0;
243 lkp->lk_flags &= ~LK_HAVE_EXCL;
244 lkp->lk_lockholder = LK_NOPROC;
245 if (lkp->lk_waitcount)
251 * If another process is ahead of us to get an upgrade,
252 * then we want to fail rather than have an intervening
255 if (lkp->lk_flags & LK_WANT_UPGRADE) {
256 shareunlock(td, lkp, 1);
260 /* FALLTHROUGH normal upgrade */
264 * Upgrade a shared lock to an exclusive one. If another
265 * shared lock has already requested an upgrade to an
266 * exclusive lock, our shared lock is released and an
267 * exclusive lock is requested (which will be granted
268 * after the upgrade). If we return an error, the file
269 * will always be unlocked.
271 if (lkp->lk_lockholder == thr)
272 panic("lockmgr: upgrade exclusive lock");
273 if (lkp->lk_sharecount <= 0)
274 panic("lockmgr: upgrade without shared");
275 shareunlock(td, lkp, 1);
277 * If we are just polling, check to see if we will block.
279 if ((extflags & LK_NOWAIT) &&
280 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
281 lkp->lk_sharecount > 1)) {
285 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
287 * We are first shared lock to request an upgrade, so
288 * request upgrade and wait for the shared count to
289 * drop to zero, then take exclusive lock.
291 lkp->lk_flags |= LK_WANT_UPGRADE;
292 error = acquire(&lkp, extflags, LK_SHARE_NONZERO);
293 lkp->lk_flags &= ~LK_WANT_UPGRADE;
296 if ((lkp->lk_flags & ( LK_WANT_EXCL | LK_WAIT_NONZERO)) == (LK_WANT_EXCL | LK_WAIT_NONZERO))
300 if (lkp->lk_exclusivecount != 0)
301 panic("lockmgr: non-zero exclusive count");
302 lkp->lk_flags |= LK_HAVE_EXCL;
303 lkp->lk_lockholder = thr;
304 lkp->lk_exclusivecount = 1;
306 #if defined(DEBUG_LOCKS)
307 lkp->lk_filename = file;
308 lkp->lk_lineno = line;
309 lkp->lk_lockername = name;
314 * Someone else has requested upgrade. Release our shared
315 * lock, awaken upgrade requestor if we are the last shared
316 * lock, then request an exclusive lock.
318 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
321 /* FALLTHROUGH exclusive request */
324 if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) {
328 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
329 panic("lockmgr: locking against myself");
330 if ((extflags & LK_CANRECURSE) != 0) {
331 lkp->lk_exclusivecount++;
337 * If we are just polling, check to see if we will sleep.
339 if ((extflags & LK_NOWAIT) &&
340 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
345 * Try to acquire the want_exclusive flag.
347 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL));
350 lkp->lk_flags |= LK_WANT_EXCL;
352 * Wait for shared locks and upgrades to finish.
354 error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO);
355 lkp->lk_flags &= ~LK_WANT_EXCL;
357 if (lkp->lk_flags & LK_WAIT_NONZERO)
361 lkp->lk_flags |= LK_HAVE_EXCL;
362 lkp->lk_lockholder = thr;
363 if (lkp->lk_exclusivecount != 0)
364 panic("lockmgr: non-zero exclusive count");
365 lkp->lk_exclusivecount = 1;
367 #if defined(DEBUG_LOCKS)
368 lkp->lk_filename = file;
369 lkp->lk_lineno = line;
370 lkp->lk_lockername = name;
375 if (lkp->lk_exclusivecount != 0) {
376 if (lkp->lk_lockholder != thr &&
377 lkp->lk_lockholder != LK_KERNPROC) {
378 panic("lockmgr: thread %p, not %s %p unlocking",
379 thr, "exclusive lock holder",
382 if (lkp->lk_lockholder != LK_KERNPROC)
384 if (lkp->lk_exclusivecount == 1) {
385 lkp->lk_flags &= ~LK_HAVE_EXCL;
386 lkp->lk_lockholder = LK_NOPROC;
387 lkp->lk_exclusivecount = 0;
389 lkp->lk_exclusivecount--;
391 } else if (lkp->lk_flags & LK_SHARE_NONZERO)
392 shareunlock(td, lkp, 1);
393 if (lkp->lk_flags & LK_WAIT_NONZERO)
399 * Check that we do not already hold the lock, as it can
400 * never drain if we do. Unfortunately, we have no way to
401 * check for holding a shared lock, but at least we can
402 * check for an exclusive one.
404 if (lkp->lk_lockholder == thr)
405 panic("lockmgr: draining against myself");
407 error = acquiredrain(lkp, extflags);
410 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
411 lkp->lk_lockholder = thr;
412 lkp->lk_exclusivecount = 1;
414 #if defined(DEBUG_LOCKS)
415 lkp->lk_filename = file;
416 lkp->lk_lineno = line;
417 lkp->lk_lockername = name;
422 mtx_unlock(lkp->lk_interlock);
423 panic("lockmgr: unknown locktype request %d",
424 flags & LK_TYPE_MASK);
427 if ((lkp->lk_flags & LK_WAITDRAIN) &&
428 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
429 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
430 lkp->lk_flags &= ~LK_WAITDRAIN;
431 wakeup((void *)&lkp->lk_flags);
433 mtx_unlock(lkp->lk_interlock);
438 acquiredrain(struct lock *lkp, int extflags) {
441 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
444 while (lkp->lk_flags & LK_ALL) {
445 lkp->lk_flags |= LK_WAITDRAIN;
446 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio,
448 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
451 if (extflags & LK_SLEEPFAIL) {
459 * Transfer any waiting processes from one lock to another.
462 transferlockers(from, to)
467 KASSERT(from != to, ("lock transfer to self"));
468 KASSERT((from->lk_flags&LK_WAITDRAIN) == 0, ("transfer draining lock"));
470 mtx_lock(from->lk_interlock);
471 if (from->lk_waitcount == 0) {
472 mtx_unlock(from->lk_interlock);
475 from->lk_newlock = to;
476 wakeup((void *)from);
477 msleep(&from->lk_newlock, from->lk_interlock, from->lk_prio,
479 from->lk_newlock = NULL;
480 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
481 KASSERT(from->lk_waitcount == 0, ("active lock"));
482 mtx_unlock(from->lk_interlock);
487 * Initialize a lock; required before use.
490 lockinit(lkp, prio, wmesg, timo, flags)
497 CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", "
498 "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags);
500 lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder);
501 lkp->lk_flags = (flags & LK_EXTFLG_MASK);
502 lkp->lk_sharecount = 0;
503 lkp->lk_waitcount = 0;
504 lkp->lk_exclusivecount = 0;
506 lkp->lk_wmesg = wmesg;
508 lkp->lk_lockholder = LK_NOPROC;
509 lkp->lk_newlock = NULL;
511 lkp->lk_filename = "none";
512 lkp->lk_lockername = "never exclusive locked";
514 lkp->lk_slockholder = LK_NOPROC;
515 lkp->lk_sfilename = "none";
516 lkp->lk_slockername = "never share locked";
528 CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")",
533 * Determine the status of a lock.
542 mtx_lock(lkp->lk_interlock);
543 if (lkp->lk_exclusivecount != 0) {
544 if (td == NULL || lkp->lk_lockholder == td)
545 lock_type = LK_EXCLUSIVE;
547 lock_type = LK_EXCLOTHER;
548 } else if (lkp->lk_sharecount != 0)
549 lock_type = LK_SHARED;
550 mtx_unlock(lkp->lk_interlock);
555 * Determine the number of holders of a lock.
563 mtx_lock(lkp->lk_interlock);
564 count = lkp->lk_exclusivecount + lkp->lk_sharecount;
565 mtx_unlock(lkp->lk_interlock);
570 * Print out information about state of a lock. Used by VOP_PRINT
571 * routines to display status about contained locks.
574 lockmgr_printinfo(lkp)
578 if (lkp->lk_sharecount)
579 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
581 else if (lkp->lk_flags & LK_HAVE_EXCL)
582 printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)",
583 lkp->lk_wmesg, lkp->lk_exclusivecount,
584 lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid);
585 if (lkp->lk_waitcount > 0)
586 printf(" with %d pending", lkp->lk_waitcount);