xref: /original-bsd/sys/kern/kern_lock.c (revision f17085de)
1 /*
2  * Copyright (c) 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code contains ideas from software contributed to Berkeley by
6  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
7  * System project at Carnegie-Mellon University.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)kern_lock.c	8.13 (Berkeley) 05/17/95
12  */
13 
14 #include <sys/param.h>
15 #include <sys/proc.h>
16 #include <sys/lock.h>
17 #include <machine/cpu.h>
18 
19 /*
20  * Locking primitives implementation.
21  * Locks provide shared/exclusive sychronization.
22  */
23 
24 #ifdef DEBUG
25 #define COUNT(p, x) if (p) (p)->p_locks += (x)
26 #else
27 #define COUNT(p, x)
28 #endif
29 
30 #if NCPUS > 1
31 
32 /*
33  * For multiprocessor system, try spin lock first.
34  *
35  * This should be inline expanded below, but we cannot have #if
36  * inside a multiline define.
37  */
38 int lock_wait_time = 100;
39 #define PAUSE(lkp, wanted)						\
40 		if (lock_wait_time > 0) {				\
41 			int i;						\
42 									\
43 			simple_unlock(&lkp->lk_interlock);		\
44 			for (i = lock_wait_time; i > 0; i--)		\
45 				if (!(wanted))				\
46 					break;				\
47 			simple_lock(&lkp->lk_interlock);		\
48 		}							\
49 		if (!(wanted))						\
50 			break;
51 
52 #else /* NCPUS == 1 */
53 
54 /*
55  * It is an error to spin on a uniprocessor as nothing will ever cause
56  * the simple lock to clear while we are executing.
57  */
58 #define PAUSE(lkp, wanted)
59 
60 #endif /* NCPUS == 1 */
61 
62 /*
63  * Acquire a resource.
64  */
65 #define ACQUIRE(lkp, error, extflags, wanted)				\
66 	PAUSE(lkp, wanted);						\
67 	for (error = 0; wanted; ) {					\
68 		(lkp)->lk_waitcount++;					\
69 		simple_unlock(&(lkp)->lk_interlock);			\
70 		error = tsleep((void *)lkp, (lkp)->lk_prio,		\
71 		    (lkp)->lk_wmesg, (lkp)->lk_timo);			\
72 		simple_lock(&(lkp)->lk_interlock);			\
73 		(lkp)->lk_waitcount--;					\
74 		if (error)						\
75 			break;						\
76 		if ((extflags) & LK_SLEEPFAIL) {			\
77 			error = ENOLCK;					\
78 			break;						\
79 		}							\
80 	}
81 
82 /*
83  * Initialize a lock; required before use.
84  */
85 void
86 lockinit(lkp, prio, wmesg, timo, flags)
87 	struct lock *lkp;
88 	int prio;
89 	char *wmesg;
90 	int timo;
91 	int flags;
92 {
93 
94 	bzero(lkp, sizeof(struct lock));
95 	simple_lock_init(&lkp->lk_interlock);
96 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
97 	lkp->lk_prio = prio;
98 	lkp->lk_timo = timo;
99 	lkp->lk_wmesg = wmesg;
100 	lkp->lk_lockholder = LK_NOPROC;
101 }
102 
103 /*
104  * Determine the status of a lock.
105  */
106 int
107 lockstatus(lkp)
108 	struct lock *lkp;
109 {
110 	int lock_type = 0;
111 
112 	simple_lock(&lkp->lk_interlock);
113 	if (lkp->lk_exclusivecount != 0)
114 		lock_type = LK_EXCLUSIVE;
115 	else if (lkp->lk_sharecount != 0)
116 		lock_type = LK_SHARED;
117 	simple_unlock(&lkp->lk_interlock);
118 	return (lock_type);
119 }
120 
121 /*
122  * Set, change, or release a lock.
123  *
124  * Shared requests increment the shared count. Exclusive requests set the
125  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
126  * accepted shared locks and shared-to-exclusive upgrades to go away.
127  */
128 int
129 lockmgr(lkp, flags, interlkp, p)
130 	__volatile struct lock *lkp;
131 	u_int flags;
132 	struct simplelock *interlkp;
133 	struct proc *p;
134 {
135 	int error;
136 	pid_t pid;
137 	int extflags;
138 
139 	error = 0;
140 	if (p)
141 		pid = p->p_pid;
142 	else
143 		pid = LK_KERNPROC;
144 	simple_lock(&lkp->lk_interlock);
145 	if (flags & LK_INTERLOCK)
146 		simple_unlock(interlkp);
147 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
148 #ifdef DIAGNOSTIC
149 	/*
150 	 * Once a lock has drained, the LK_DRAINING flag is set and an
151 	 * exclusive lock is returned. The only valid operation thereafter
152 	 * is a single release of that exclusive lock. This final release
153 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
154 	 * further requests of any sort will result in a panic. The bits
155 	 * selected for these two flags are chosen so that they will be set
156 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
157 	 */
158 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
159 		if (lkp->lk_flags & LK_DRAINED)
160 			panic("lockmgr: using decommissioned lock");
161 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
162 		    lkp->lk_lockholder != pid)
163 			panic("lockmgr: non-release on draining lock: %d\n",
164 			    flags & LK_TYPE_MASK);
165 		lkp->lk_flags &= ~LK_DRAINING;
166 		lkp->lk_flags |= LK_DRAINED;
167 	}
168 #endif DIAGNOSTIC
169 
170 	switch (flags & LK_TYPE_MASK) {
171 
172 	case LK_SHARED:
173 		if (lkp->lk_lockholder != pid) {
174 			/*
175 			 * If just polling, check to see if we will block.
176 			 */
177 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
178 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
179 				error = EBUSY;
180 				break;
181 			}
182 			/*
183 			 * Wait for exclusive locks and upgrades to clear.
184 			 */
185 			ACQUIRE(lkp, error, extflags, lkp->lk_flags &
186 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
187 			if (error)
188 				break;
189 			lkp->lk_sharecount++;
190 			COUNT(p, 1);
191 			break;
192 		}
193 		/*
194 		 * We hold an exclusive lock, so downgrade it to shared.
195 		 * An alternative would be to fail with EDEADLK.
196 		 */
197 		lkp->lk_sharecount++;
198 		COUNT(p, 1);
199 		/* fall into downgrade */
200 
201 	case LK_DOWNGRADE:
202 		if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
203 			panic("lockmgr: not holding exclusive lock");
204 		lkp->lk_sharecount += lkp->lk_exclusivecount;
205 		lkp->lk_exclusivecount = 0;
206 		lkp->lk_flags &= ~LK_HAVE_EXCL;
207 		lkp->lk_lockholder = LK_NOPROC;
208 		if (lkp->lk_waitcount)
209 			wakeup((void *)lkp);
210 		break;
211 
212 	case LK_EXCLUPGRADE:
213 		/*
214 		 * If another process is ahead of us to get an upgrade,
215 		 * then we want to fail rather than have an intervening
216 		 * exclusive access.
217 		 */
218 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
219 			lkp->lk_sharecount--;
220 			COUNT(p, -1);
221 			error = EBUSY;
222 			break;
223 		}
224 		/* fall into normal upgrade */
225 
226 	case LK_UPGRADE:
227 		/*
228 		 * Upgrade a shared lock to an exclusive one. If another
229 		 * shared lock has already requested an upgrade to an
230 		 * exclusive lock, our shared lock is released and an
231 		 * exclusive lock is requested (which will be granted
232 		 * after the upgrade). If we return an error, the file
233 		 * will always be unlocked.
234 		 */
235 		if (lkp->lk_lockholder == pid || lkp->lk_sharecount <= 0)
236 			panic("lockmgr: upgrade exclusive lock");
237 		lkp->lk_sharecount--;
238 		COUNT(p, -1);
239 		/*
240 		 * If we are just polling, check to see if we will block.
241 		 */
242 		if ((extflags & LK_NOWAIT) &&
243 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
244 		     lkp->lk_sharecount > 1)) {
245 			error = EBUSY;
246 			break;
247 		}
248 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
249 			/*
250 			 * We are first shared lock to request an upgrade, so
251 			 * request upgrade and wait for the shared count to
252 			 * drop to zero, then take exclusive lock.
253 			 */
254 			lkp->lk_flags |= LK_WANT_UPGRADE;
255 			ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
256 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
257 			if (error)
258 				break;
259 			lkp->lk_flags |= LK_HAVE_EXCL;
260 			lkp->lk_lockholder = pid;
261 			if (lkp->lk_exclusivecount != 0)
262 				panic("lockmgr: non-zero exclusive count");
263 			lkp->lk_exclusivecount = 1;
264 			COUNT(p, 1);
265 			break;
266 		}
267 		/*
268 		 * Someone else has requested upgrade. Release our shared
269 		 * lock, awaken upgrade requestor if we are the last shared
270 		 * lock, then request an exclusive lock.
271 		 */
272 		if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
273 			wakeup((void *)lkp);
274 		/* fall into exclusive request */
275 
276 	case LK_EXCLUSIVE:
277 		if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
278 			/*
279 			 *	Recursive lock.
280 			 */
281 			if ((extflags & LK_CANRECURSE) == 0)
282 				panic("lockmgr: locking against myself");
283 			lkp->lk_exclusivecount++;
284 			COUNT(p, 1);
285 			break;
286 		}
287 		/*
288 		 * If we are just polling, check to see if we will sleep.
289 		 */
290 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
291 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
292 		     lkp->lk_sharecount != 0)) {
293 			error = EBUSY;
294 			break;
295 		}
296 		/*
297 		 * Try to acquire the want_exclusive flag.
298 		 */
299 		ACQUIRE(lkp, error, extflags, lkp->lk_flags &
300 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
301 		if (error)
302 			break;
303 		lkp->lk_flags |= LK_WANT_EXCL;
304 		/*
305 		 * Wait for shared locks and upgrades to finish.
306 		 */
307 		ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
308 		       (lkp->lk_flags & LK_WANT_UPGRADE));
309 		lkp->lk_flags &= ~LK_WANT_EXCL;
310 		if (error)
311 			break;
312 		lkp->lk_flags |= LK_HAVE_EXCL;
313 		lkp->lk_lockholder = pid;
314 		if (lkp->lk_exclusivecount != 0)
315 			panic("lockmgr: non-zero exclusive count");
316 		lkp->lk_exclusivecount = 1;
317 		COUNT(p, 1);
318 		break;
319 
320 	case LK_RELEASE:
321 		if (lkp->lk_exclusivecount != 0) {
322 			if (pid != lkp->lk_lockholder)
323 				panic("lockmgr: pid %d, not %s %d unlocking",
324 				    pid, "exclusive lock holder",
325 				    lkp->lk_lockholder);
326 			lkp->lk_exclusivecount--;
327 			COUNT(p, -1);
328 			if (lkp->lk_exclusivecount == 0) {
329 				lkp->lk_flags &= ~LK_HAVE_EXCL;
330 				lkp->lk_lockholder = LK_NOPROC;
331 			}
332 		} else if (lkp->lk_sharecount != 0) {
333 			lkp->lk_sharecount--;
334 			COUNT(p, -1);
335 		}
336 		if (lkp->lk_waitcount)
337 			wakeup((void *)lkp);
338 		break;
339 
340 	case LK_DRAIN:
341 		/*
342 		 * Check that we do not already hold the lock, as it can
343 		 * never drain if we do. Unfortunately, we have no way to
344 		 * check for holding a shared lock, but at least we can
345 		 * check for an exclusive one.
346 		 */
347 		if (lkp->lk_lockholder == pid)
348 			panic("lockmgr: draining against myself");
349 		/*
350 		 * If we are just polling, check to see if we will sleep.
351 		 */
352 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
353 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
354 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
355 			error = EBUSY;
356 			break;
357 		}
358 		PAUSE(lkp, ((lkp->lk_flags &
359 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
360 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0));
361 		for (error = 0; ((lkp->lk_flags &
362 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
363 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) {
364 			lkp->lk_flags |= LK_WAITDRAIN;
365 			simple_unlock(&lkp->lk_interlock);
366 			if (error = tsleep((void *)&lkp->lk_flags, lkp->lk_prio,
367 			    lkp->lk_wmesg, lkp->lk_timo))
368 				return (error);
369 			if ((extflags) & LK_SLEEPFAIL)
370 				return (ENOLCK);
371 			simple_lock(&lkp->lk_interlock);
372 		}
373 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
374 		lkp->lk_lockholder = pid;
375 		lkp->lk_exclusivecount = 1;
376 		COUNT(p, 1);
377 		break;
378 
379 	default:
380 		simple_unlock(&lkp->lk_interlock);
381 		panic("lockmgr: unknown locktype request %d",
382 		    flags & LK_TYPE_MASK);
383 		/* NOTREACHED */
384 	}
385 	if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
386 	     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
387 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
388 		lkp->lk_flags &= ~LK_WAITDRAIN;
389 		wakeup((void *)&lkp->lk_flags);
390 	}
391 	simple_unlock(&lkp->lk_interlock);
392 	return (error);
393 }
394 
395 /*
396  * Print out information about state of a lock. Used by VOP_PRINT
397  * routines to display ststus about contained locks.
398  */
399 lockmgr_printinfo(lkp)
400 	struct lock *lkp;
401 {
402 
403 	if (lkp->lk_sharecount)
404 		printf(" lock type %s: SHARED", lkp->lk_wmesg);
405 	else if (lkp->lk_flags & LK_HAVE_EXCL)
406 		printf(" lock type %s: EXCL by pid %d", lkp->lk_wmesg,
407 		    lkp->lk_lockholder);
408 	if (lkp->lk_waitcount > 0)
409 		printf(" with %d pending", lkp->lk_waitcount);
410 }
411 
412 #if defined(DEBUG) && NCPUS == 1
413 #include <sys/kernel.h>
414 #include <vm/vm.h>
415 #include <sys/sysctl.h>
416 int lockpausetime = 1;
417 struct ctldebug debug2 = { "lockpausetime", &lockpausetime };
418 /*
419  * Simple lock functions so that the debugger can see from whence
420  * they are being called.
421  */
422 void
423 simple_lock_init(alp)
424 	struct simplelock *alp;
425 {
426 
427 	alp->lock_data = 0;
428 }
429 
430 void
431 _simple_lock(alp, id, l)
432 	__volatile struct simplelock *alp;
433 	const char *id;
434 	int l;
435 {
436 
437 	if (alp->lock_data == 1) {
438 		if (lockpausetime == -1)
439 			panic("%s:%d: simple_lock: lock held", id, l);
440 		if (lockpausetime == 0) {
441 			printf("%s:%d: simple_lock: lock held\n", id, l);
442 			BACKTRACE(curproc);
443 		} else if (lockpausetime > 0) {
444 			printf("%s:%d: simple_lock: lock held...", id, l);
445 			tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
446 			    lockpausetime * hz);
447 			printf(" continuing\n");
448 		}
449 	}
450 	alp->lock_data = 1;
451 }
452 
453 int
454 _simple_lock_try(alp, id, l)
455 	__volatile struct simplelock *alp;
456 	const char *id;
457 	int l;
458 {
459 
460 	/*
461 	if (alp->lock_data == 1) {
462 		if (lockpausetime == -1)
463 			panic("%s:%d: simple_lock_try: lock held", id, l);
464 		if (lockpausetime == 0) {
465 			printf("%s:%d: simple_lock_try: lock held\n", id, l);
466 			BACKTRACE(curproc);
467 		} else if (lockpausetime > 0) {
468 			printf("%s:%d: simple_lock_try: lock held...", id, l);
469 			tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
470 			    lockpausetime * hz);
471 			printf(" continuing\n");
472 		}
473 	}
474 	*/
475 	if (alp->lock_data)
476 		return (0);
477 
478 	alp->lock_data = 1;
479 	return (1);
480 }
481 
482 void
483 _simple_unlock(alp, id, l)
484 	__volatile struct simplelock *alp;
485 	const char *id;
486 	int l;
487 {
488 
489 	if (alp->lock_data == 0) {
490 		if (lockpausetime == -1)
491 			panic("%s:%d: simple_unlock: lock not held", id, l);
492 		if (lockpausetime == 0) {
493 			printf("%s:%d: simple_unlock: lock not held\n", id, l);
494 			BACKTRACE(curproc);
495 		} else if (lockpausetime > 0) {
496 			printf("%s:%d: simple_unlock: lock not held...", id, l);
497 			tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
498 			    lockpausetime * hz);
499 			printf(" continuing\n");
500 		}
501 	}
502 	alp->lock_data = 0;
503 }
504 #endif /* DEBUG && NCPUS == 1 */
505