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