xref: /dragonfly/sys/kern/kern_lock.c (revision 38a690d7)
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.5 2003/07/21 07:57:47 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 	lwkt_gettoken(&lkp->lk_interlock);
163 	if (flags & LK_INTERLOCK)
164 		lwkt_reltoken(interlkp);
165 
166 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
167 
168 	switch (flags & LK_TYPE_MASK) {
169 
170 	case LK_SHARED:
171 		/*
172 		 * If we are not the exclusive lock holder, we have to block
173 		 * while there is an exclusive lock holder or while an
174 		 * exclusive lock request or upgrade request is in progress.
175 		 *
176 		 * However, if P_DEADLKTREAT is set, we override exclusive
177 		 * lock requests or upgrade requests ( but not the exclusive
178 		 * lock itself ).
179 		 */
180 		if (lkp->lk_lockholder != td) {
181 			if (td->td_flags & TDF_DEADLKTREAT) {
182 				error = acquire(
183 					    lkp,
184 					    extflags,
185 					    LK_HAVE_EXCL
186 					);
187 			} else {
188 				error = acquire(
189 					    lkp,
190 					    extflags,
191 					    LK_HAVE_EXCL | LK_WANT_EXCL |
192 					     LK_WANT_UPGRADE
193 					);
194 			}
195 			if (error)
196 				break;
197 			sharelock(lkp, 1);
198 			COUNT(td, 1);
199 			break;
200 		}
201 		/*
202 		 * We hold an exclusive lock, so downgrade it to shared.
203 		 * An alternative would be to fail with EDEADLK.
204 		 */
205 		sharelock(lkp, 1);
206 		COUNT(td, 1);
207 		/* fall into downgrade */
208 
209 	case LK_DOWNGRADE:
210 		if (lkp->lk_lockholder != td || lkp->lk_exclusivecount == 0)
211 			panic("lockmgr: not holding exclusive lock");
212 		sharelock(lkp, lkp->lk_exclusivecount);
213 		lkp->lk_exclusivecount = 0;
214 		lkp->lk_flags &= ~LK_HAVE_EXCL;
215 		lkp->lk_lockholder = LK_NOTHREAD;
216 		if (lkp->lk_waitcount)
217 			wakeup((void *)lkp);
218 		break;
219 
220 	case LK_EXCLUPGRADE:
221 		/*
222 		 * If another process is ahead of us to get an upgrade,
223 		 * then we want to fail rather than have an intervening
224 		 * exclusive access.
225 		 */
226 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
227 			shareunlock(lkp, 1);
228 			COUNT(td, -1);
229 			error = EBUSY;
230 			break;
231 		}
232 		/* fall into normal upgrade */
233 
234 	case LK_UPGRADE:
235 		/*
236 		 * Upgrade a shared lock to an exclusive one. If another
237 		 * shared lock has already requested an upgrade to an
238 		 * exclusive lock, our shared lock is released and an
239 		 * exclusive lock is requested (which will be granted
240 		 * after the upgrade). If we return an error, the file
241 		 * will always be unlocked.
242 		 */
243 		if ((lkp->lk_lockholder == td) || (lkp->lk_sharecount <= 0))
244 			panic("lockmgr: upgrade exclusive lock");
245 		shareunlock(lkp, 1);
246 		COUNT(td, -1);
247 		/*
248 		 * If we are just polling, check to see if we will block.
249 		 */
250 		if ((extflags & LK_NOWAIT) &&
251 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
252 		     lkp->lk_sharecount > 1)) {
253 			error = EBUSY;
254 			break;
255 		}
256 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
257 			/*
258 			 * We are first shared lock to request an upgrade, so
259 			 * request upgrade and wait for the shared count to
260 			 * drop to zero, then take exclusive lock.
261 			 */
262 			lkp->lk_flags |= LK_WANT_UPGRADE;
263 			error = acquire(lkp, extflags, LK_SHARE_NONZERO);
264 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
265 
266 			if (error)
267 				break;
268 			lkp->lk_flags |= LK_HAVE_EXCL;
269 			lkp->lk_lockholder = td;
270 			if (lkp->lk_exclusivecount != 0)
271 				panic("lockmgr: non-zero exclusive count");
272 			lkp->lk_exclusivecount = 1;
273 #if defined(DEBUG_LOCKS)
274 			lkp->lk_filename = file;
275 			lkp->lk_lineno = line;
276 			lkp->lk_lockername = name;
277 #endif
278 			COUNT(td, 1);
279 			break;
280 		}
281 		/*
282 		 * Someone else has requested upgrade. Release our shared
283 		 * lock, awaken upgrade requestor if we are the last shared
284 		 * lock, then request an exclusive lock.
285 		 */
286 		if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
287 			LK_WAIT_NONZERO)
288 			wakeup((void *)lkp);
289 		/* fall into exclusive request */
290 
291 	case LK_EXCLUSIVE:
292 		if (lkp->lk_lockholder == td && td != LK_KERNTHREAD) {
293 			/*
294 			 *	Recursive lock.
295 			 */
296 			if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
297 				panic("lockmgr: locking against myself");
298 			if ((extflags & LK_CANRECURSE) != 0) {
299 				lkp->lk_exclusivecount++;
300 				COUNT(td, 1);
301 				break;
302 			}
303 		}
304 		/*
305 		 * If we are just polling, check to see if we will sleep.
306 		 */
307 		if ((extflags & LK_NOWAIT) &&
308 		    (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
309 			error = EBUSY;
310 			break;
311 		}
312 		/*
313 		 * Try to acquire the want_exclusive flag.
314 		 */
315 		error = acquire(lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL));
316 		if (error)
317 			break;
318 		lkp->lk_flags |= LK_WANT_EXCL;
319 		/*
320 		 * Wait for shared locks and upgrades to finish.
321 		 */
322 		error = acquire(lkp, extflags, LK_WANT_UPGRADE | LK_SHARE_NONZERO);
323 		lkp->lk_flags &= ~LK_WANT_EXCL;
324 		if (error)
325 			break;
326 		lkp->lk_flags |= LK_HAVE_EXCL;
327 		lkp->lk_lockholder = td;
328 		if (lkp->lk_exclusivecount != 0)
329 			panic("lockmgr: non-zero exclusive count");
330 		lkp->lk_exclusivecount = 1;
331 #if defined(DEBUG_LOCKS)
332 			lkp->lk_filename = file;
333 			lkp->lk_lineno = line;
334 			lkp->lk_lockername = name;
335 #endif
336 		COUNT(td, 1);
337 		break;
338 
339 	case LK_RELEASE:
340 		if (lkp->lk_exclusivecount != 0) {
341 			if (lkp->lk_lockholder != td &&
342 			    lkp->lk_lockholder != LK_KERNTHREAD) {
343 				panic("lockmgr: pid %d, not %s thr %p unlocking",
344 				    (td->td_proc ? td->td_proc->p_pid : -99),
345 				    "exclusive lock holder",
346 				    lkp->lk_lockholder);
347 			}
348 			if (lkp->lk_lockholder != LK_KERNTHREAD) {
349 				COUNT(td, -1);
350 			}
351 			if (lkp->lk_exclusivecount == 1) {
352 				lkp->lk_flags &= ~LK_HAVE_EXCL;
353 				lkp->lk_lockholder = LK_NOTHREAD;
354 				lkp->lk_exclusivecount = 0;
355 			} else {
356 				lkp->lk_exclusivecount--;
357 			}
358 		} else if (lkp->lk_flags & LK_SHARE_NONZERO) {
359 			shareunlock(lkp, 1);
360 			COUNT(td, -1);
361 		}
362 		if (lkp->lk_flags & LK_WAIT_NONZERO)
363 			wakeup((void *)lkp);
364 		break;
365 
366 	case LK_DRAIN:
367 		/*
368 		 * Check that we do not already hold the lock, as it can
369 		 * never drain if we do. Unfortunately, we have no way to
370 		 * check for holding a shared lock, but at least we can
371 		 * check for an exclusive one.
372 		 */
373 		if (lkp->lk_lockholder == td)
374 			panic("lockmgr: draining against myself");
375 
376 		error = acquiredrain(lkp, extflags);
377 		if (error)
378 			break;
379 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
380 		lkp->lk_lockholder = td;
381 		lkp->lk_exclusivecount = 1;
382 #if defined(DEBUG_LOCKS)
383 			lkp->lk_filename = file;
384 			lkp->lk_lineno = line;
385 			lkp->lk_lockername = name;
386 #endif
387 		COUNT(td, 1);
388 		break;
389 
390 	default:
391 		lwkt_reltoken(&lkp->lk_interlock);
392 		panic("lockmgr: unknown locktype request %d",
393 		    flags & LK_TYPE_MASK);
394 		/* NOTREACHED */
395 	}
396 	if ((lkp->lk_flags & LK_WAITDRAIN) &&
397 	    (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
398 		LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
399 		lkp->lk_flags &= ~LK_WAITDRAIN;
400 		wakeup((void *)&lkp->lk_flags);
401 	}
402 	lwkt_reltoken(&lkp->lk_interlock);
403 	return (error);
404 }
405 
406 static int
407 acquiredrain(struct lock *lkp, int extflags)
408 {
409 	int error;
410 
411 	if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
412 		return EBUSY;
413 	}
414 
415 	if ((lkp->lk_flags & LK_ALL) == 0)
416 		return 0;
417 
418 	while (lkp->lk_flags & LK_ALL) {
419 		lkp->lk_flags |= LK_WAITDRAIN;
420 		lwkt_reltoken(&lkp->lk_interlock);
421 		error = tsleep(&lkp->lk_flags, lkp->lk_prio,
422 			lkp->lk_wmesg,
423 			((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
424 		lwkt_gettoken(&lkp->lk_interlock);
425 		if (error)
426 			return error;
427 		if (extflags & LK_SLEEPFAIL) {
428 			return ENOLCK;
429 		}
430 	}
431 	return 0;
432 }
433 
434 /*
435  * Initialize a lock; required before use.
436  */
437 void
438 lockinit(lkp, prio, wmesg, timo, flags)
439 	struct lock *lkp;
440 	int prio;
441 	char *wmesg;
442 	int timo;
443 	int flags;
444 {
445 	lwkt_inittoken(&lkp->lk_interlock);
446 	lkp->lk_flags = (flags & LK_EXTFLG_MASK);
447 	lkp->lk_sharecount = 0;
448 	lkp->lk_waitcount = 0;
449 	lkp->lk_exclusivecount = 0;
450 	lkp->lk_prio = prio;
451 	lkp->lk_wmesg = wmesg;
452 	lkp->lk_timo = timo;
453 	lkp->lk_lockholder = LK_NOTHREAD;
454 }
455 
456 /*
457  * Determine the status of a lock.
458  */
459 int
460 lockstatus(struct lock *lkp, struct thread *td)
461 {
462 	int lock_type = 0;
463 
464 	lwkt_gettoken(&lkp->lk_interlock);
465 	if (lkp->lk_exclusivecount != 0) {
466 		if (td == NULL || lkp->lk_lockholder == td)
467 			lock_type = LK_EXCLUSIVE;
468 		else
469 			lock_type = LK_EXCLOTHER;
470 	} else if (lkp->lk_sharecount != 0) {
471 		lock_type = LK_SHARED;
472 	}
473 	lwkt_reltoken(&lkp->lk_interlock);
474 	return (lock_type);
475 }
476 
477 /*
478  * Determine the number of holders of a lock.
479  */
480 int
481 lockcount(lkp)
482 	struct lock *lkp;
483 {
484 	int count;
485 
486 	lwkt_gettoken(&lkp->lk_interlock);
487 	count = lkp->lk_exclusivecount + lkp->lk_sharecount;
488 	lwkt_reltoken(&lkp->lk_interlock);
489 	return (count);
490 }
491 
492 /*
493  * Print out information about state of a lock. Used by VOP_PRINT
494  * routines to display status about contained locks.
495  */
496 void
497 lockmgr_printinfo(lkp)
498 	struct lock *lkp;
499 {
500 	struct thread *td = lkp->lk_lockholder;
501 	struct proc *p;
502 
503 	if (td && td != LK_KERNTHREAD && td != LK_NOTHREAD)
504 		p = td->td_proc;
505 	else
506 		p = NULL;
507 
508 	if (lkp->lk_sharecount)
509 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
510 		    lkp->lk_sharecount);
511 	else if (lkp->lk_flags & LK_HAVE_EXCL)
512 		printf(" lock type %s: EXCL (count %d) by td %p pid %d",
513 		    lkp->lk_wmesg, lkp->lk_exclusivecount, td,
514 		    p ? p->p_pid : -99);
515 	if (lkp->lk_waitcount > 0)
516 		printf(" with %d pending", lkp->lk_waitcount);
517 }
518 
519