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