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