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