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