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