Initial import from FreeBSD RELENG_4:
[games.git] / sys / kern / kern_lock.c
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  */
43
44 #include "opt_lint.h"
45
46 #include <sys/param.h>
47 #include <sys/proc.h>
48 #include <sys/lock.h>
49 #include <sys/systm.h>
50
51 /*
52  * Locking primitives implementation.
53  * Locks provide shared/exclusive sychronization.
54  */
55
56 #ifdef SIMPLELOCK_DEBUG
57 #define COUNT(p, x) if (p) (p)->p_locks += (x)
58 #else
59 #define COUNT(p, x)
60 #endif
61
62 #define LOCK_WAIT_TIME 100
63 #define LOCK_SAMPLE_WAIT 7
64
65 #if defined(DIAGNOSTIC)
66 #define LOCK_INLINE
67 #else
68 #define LOCK_INLINE __inline
69 #endif
70
71 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
72         LK_SHARE_NONZERO | LK_WAIT_NONZERO)
73
74 static int acquire(struct lock *lkp, int extflags, int wanted);
75 static int apause(struct lock *lkp, int flags);
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 /*
101  * This is the waitloop optimization, and note for this to work
102  * simple_lock and simple_unlock should be subroutines to avoid
103  * optimization troubles.
104  */
105 static int
106 apause(struct lock *lkp, int flags)
107 {
108 #ifdef SMP
109         int i, lock_wait;
110 #endif
111
112         if ((lkp->lk_flags & flags) == 0)
113                 return 0;
114 #ifdef SMP
115         for (lock_wait = LOCK_WAIT_TIME; lock_wait > 0; lock_wait--) {
116                 simple_unlock(&lkp->lk_interlock);
117                 for (i = LOCK_SAMPLE_WAIT; i > 0; i--)
118                         if ((lkp->lk_flags & flags) == 0)
119                                 break;
120                 simple_lock(&lkp->lk_interlock);
121                 if ((lkp->lk_flags & flags) == 0)
122                         return 0;
123         }
124 #endif
125         return 1;
126 }
127
128 static int
129 acquire(struct lock *lkp, int extflags, int wanted) {
130         int s, error;
131
132         if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) {
133                 return EBUSY;
134         }
135
136         if (((lkp->lk_flags | extflags) & LK_NOPAUSE) == 0) {
137                 error = apause(lkp, wanted);
138                 if (error == 0)
139                         return 0;
140         }
141
142         s = splhigh();
143         while ((lkp->lk_flags & wanted) != 0) {
144                 lkp->lk_flags |= LK_WAIT_NONZERO;
145                 lkp->lk_waitcount++;
146                 simple_unlock(&lkp->lk_interlock);
147                 error = tsleep(lkp, lkp->lk_prio, lkp->lk_wmesg, 
148                             ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
149                 simple_lock(&lkp->lk_interlock);
150                 if (lkp->lk_waitcount == 1) {
151                         lkp->lk_flags &= ~LK_WAIT_NONZERO;
152                         lkp->lk_waitcount = 0;
153                 } else {
154                         lkp->lk_waitcount--;
155                 }
156                 if (error) {
157                         splx(s);
158                         return error;
159                 }
160                 if (extflags & LK_SLEEPFAIL) {
161                         splx(s);
162                         return ENOLCK;
163                 }
164         }
165         splx(s);
166         return 0;
167 }
168
169 /*
170  * Set, change, or release a lock.
171  *
172  * Shared requests increment the shared count. Exclusive requests set the
173  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
174  * accepted shared locks and shared-to-exclusive upgrades to go away.
175  */
176 int
177 #ifndef DEBUG_LOCKS
178 lockmgr(lkp, flags, interlkp, p)
179 #else
180 debuglockmgr(lkp, flags, interlkp, p, name, file, line)
181 #endif
182         struct lock *lkp;
183         u_int flags;
184         struct simplelock *interlkp;
185         struct proc *p;
186 #ifdef  DEBUG_LOCKS
187         const char *name;       /* Name of lock function */
188         const char *file;       /* Name of file call is from */
189         int line;               /* Line number in file */
190 #endif
191 {
192         int error;
193         pid_t pid;
194         int extflags;
195
196         error = 0;
197         if (p == NULL)
198                 pid = LK_KERNPROC;
199         else
200                 pid = p->p_pid;
201
202         simple_lock(&lkp->lk_interlock);
203         if (flags & LK_INTERLOCK)
204                 simple_unlock(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 != pid) {
221                         if (p && (p->p_flag & P_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(p, 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(p, 1);
247                 /* fall into downgrade */
248
249         case LK_DOWNGRADE:
250                 if (lkp->lk_lockholder != pid || 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_NOPROC;
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(p, -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 == pid) || (lkp->lk_sharecount <= 0))
284                         panic("lockmgr: upgrade exclusive lock");
285                 shareunlock(lkp, 1);
286                 COUNT(p, -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 = pid;
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(p, 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 == pid && pid != LK_KERNPROC) {
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(p, 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 = pid;
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(p, 1);
377                 break;
378
379         case LK_RELEASE:
380                 if (lkp->lk_exclusivecount != 0) {
381                         if (lkp->lk_lockholder != pid &&
382                             lkp->lk_lockholder != LK_KERNPROC) {
383                                 panic("lockmgr: pid %d, not %s %d unlocking",
384                                     pid, "exclusive lock holder",
385                                     lkp->lk_lockholder);
386                         }
387                         if (lkp->lk_lockholder != LK_KERNPROC) {
388                                 COUNT(p, -1);
389                         }
390                         if (lkp->lk_exclusivecount == 1) {
391                                 lkp->lk_flags &= ~LK_HAVE_EXCL;
392                                 lkp->lk_lockholder = LK_NOPROC;
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(p, -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 == pid)
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 = pid;
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(p, 1);
427                 break;
428
429         default:
430                 simple_unlock(&lkp->lk_interlock);
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         simple_unlock(&lkp->lk_interlock);
442         return (error);
443 }
444
445 static int
446 acquiredrain(struct lock *lkp, int extflags) {
447         int error;
448
449         if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
450                 return EBUSY;
451         }
452
453         error = apause(lkp, LK_ALL);
454         if (error == 0)
455                 return 0;
456
457         while (lkp->lk_flags & LK_ALL) {
458                 lkp->lk_flags |= LK_WAITDRAIN;
459                 simple_unlock(&lkp->lk_interlock);
460                 error = tsleep(&lkp->lk_flags, lkp->lk_prio,
461                         lkp->lk_wmesg, 
462                         ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
463                 simple_lock(&lkp->lk_interlock);
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(lkp, prio, wmesg, timo, flags)
478         struct lock *lkp;
479         int prio;
480         char *wmesg;
481         int timo;
482         int flags;
483 {
484
485         simple_lock_init(&lkp->lk_interlock);
486         lkp->lk_flags = (flags & LK_EXTFLG_MASK);
487         lkp->lk_sharecount = 0;
488         lkp->lk_waitcount = 0;
489         lkp->lk_exclusivecount = 0;
490         lkp->lk_prio = prio;
491         lkp->lk_wmesg = wmesg;
492         lkp->lk_timo = timo;
493         lkp->lk_lockholder = LK_NOPROC;
494 }
495
496 /*
497  * Determine the status of a lock.
498  */
499 int
500 lockstatus(lkp, p)
501         struct lock *lkp;
502         struct proc *p;
503 {
504         int lock_type = 0;
505
506         simple_lock(&lkp->lk_interlock);
507         if (lkp->lk_exclusivecount != 0) {
508                 if (p == NULL || lkp->lk_lockholder == p->p_pid)
509                         lock_type = LK_EXCLUSIVE;
510                 else
511                         lock_type = LK_EXCLOTHER;
512         } else if (lkp->lk_sharecount != 0)
513                 lock_type = LK_SHARED;
514         simple_unlock(&lkp->lk_interlock);
515         return (lock_type);
516 }
517
518 /*
519  * Determine the number of holders of a lock.
520  */
521 int
522 lockcount(lkp)
523         struct lock *lkp;
524 {
525         int count;
526
527         simple_lock(&lkp->lk_interlock);
528         count = lkp->lk_exclusivecount + lkp->lk_sharecount;
529         simple_unlock(&lkp->lk_interlock);
530         return (count);
531 }
532
533 /*
534  * Print out information about state of a lock. Used by VOP_PRINT
535  * routines to display status about contained locks.
536  */
537 void
538 lockmgr_printinfo(lkp)
539         struct lock *lkp;
540 {
541
542         if (lkp->lk_sharecount)
543                 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
544                     lkp->lk_sharecount);
545         else if (lkp->lk_flags & LK_HAVE_EXCL)
546                 printf(" lock type %s: EXCL (count %d) by pid %d",
547                     lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
548         if (lkp->lk_waitcount > 0)
549                 printf(" with %d pending", lkp->lk_waitcount);
550 }
551
552 #if defined(SIMPLELOCK_DEBUG) && (MAXCPU == 1 || defined(COMPILING_LINT))
553 #include <sys/kernel.h>
554 #include <sys/sysctl.h>
555
556 static int lockpausetime = 0;
557 SYSCTL_INT(_debug, OID_AUTO, lockpausetime, CTLFLAG_RW, &lockpausetime, 0, "");
558
559 static int simplelockrecurse;
560
561 /*
562  * Simple lock functions so that the debugger can see from whence
563  * they are being called.
564  */
565 void
566 simple_lock_init(alp)
567         struct simplelock *alp;
568 {
569
570         alp->lock_data = 0;
571 }
572
573 void
574 _simple_lock(alp, id, l)
575         struct simplelock *alp;
576         const char *id;
577         int l;
578 {
579
580         if (simplelockrecurse)
581                 return;
582         if (alp->lock_data == 1) {
583                 if (lockpausetime == -1)
584                         panic("%s:%d: simple_lock: lock held", id, l);
585                 printf("%s:%d: simple_lock: lock held\n", id, l);
586                 if (lockpausetime == 1) {
587                         Debugger("simple_lock");
588                         /*BACKTRACE(curproc); */
589                 } else if (lockpausetime > 1) {
590                         printf("%s:%d: simple_lock: lock held...", id, l);
591                         tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
592                             lockpausetime * hz);
593                         printf(" continuing\n");
594                 }
595         }
596         alp->lock_data = 1;
597         if (curproc)
598                 curproc->p_simple_locks++;
599 }
600
601 int
602 _simple_lock_try(alp, id, l)
603         struct simplelock *alp;
604         const char *id;
605         int l;
606 {
607
608         if (alp->lock_data)
609                 return (0);
610         if (simplelockrecurse)
611                 return (1);
612         alp->lock_data = 1;
613         if (curproc)
614                 curproc->p_simple_locks++;
615         return (1);
616 }
617
618 void
619 _simple_unlock(alp, id, l)
620         struct simplelock *alp;
621         const char *id;
622         int l;
623 {
624
625         if (simplelockrecurse)
626                 return;
627         if (alp->lock_data == 0) {
628                 if (lockpausetime == -1)
629                         panic("%s:%d: simple_unlock: lock not held", id, l);
630                 printf("%s:%d: simple_unlock: lock not held\n", id, l);
631                 if (lockpausetime == 1) {
632                         Debugger("simple_unlock");
633                         /* BACKTRACE(curproc); */
634                 } else if (lockpausetime > 1) {
635                         printf("%s:%d: simple_unlock: lock not held...", id, l);
636                         tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
637                             lockpausetime * hz);
638                         printf(" continuing\n");
639                 }
640         }
641         alp->lock_data = 0;
642         if (curproc)
643                 curproc->p_simple_locks--;
644 }
645 #elif defined(SIMPLELOCK_DEBUG)
646 #error "SIMPLELOCK_DEBUG is not compatible with SMP!"
647 #endif /* SIMPLELOCK_DEBUG && MAXCPU == 1 */