Mop up remains of the ibcs2/streams/svr4 removal:
[dragonfly.git] / sys / kern / kern_spinlock.c
1 /*
2  * Copyright (c) 2005 Jeffrey M. Hsu.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Jeffrey M. Hsu. and Matthew Dillon
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of The DragonFly Project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific, prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $DragonFly: src/sys/kern/kern_spinlock.c,v 1.4 2006/05/21 20:23:25 dillon Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #ifdef INVARIANTS
41 #include <sys/proc.h>
42 #endif
43 #include <ddb/ddb.h>
44 #include <machine/atomic.h>
45 #include <machine/cpufunc.h>
46 #include <machine/clock.h>
47 #include <sys/spinlock.h>
48 #include <sys/spinlock2.h>
49
50 #define BACKOFF_INITIAL 1
51 #define BACKOFF_LIMIT   256
52
53 #ifdef SMP
54
55 #ifdef INVARIANTS
56 static int spin_lock_test_mode;
57 #endif
58
59 static int64_t spinlocks_contested1;
60 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested1, CTLFLAG_RD, &spinlocks_contested1, 0, "");
61 static int64_t spinlocks_contested2;
62 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested2, CTLFLAG_RD, &spinlocks_contested2, 0, "");
63
64 struct exponential_backoff {
65         int backoff;
66         int nsec;
67         struct spinlock *mtx;
68         sysclock_t base;
69 };
70 static int exponential_backoff(struct exponential_backoff *bo);
71
72 static __inline
73 void
74 exponential_init(struct exponential_backoff *bo, struct spinlock *mtx)
75 {
76         bo->backoff = BACKOFF_INITIAL;
77         bo->nsec = 0;
78         bo->mtx = mtx;
79 }
80
81 /*
82  * We were either contested due to another exclusive lock holder,
83  * or due to the presence of shared locks.  We have to undo the mess
84  * we created by returning the shared locks.
85  *
86  * If there was another exclusive lock holder only the exclusive bit
87  * in value will be the only bit set.  We don't have to do anything since
88  * restoration does not involve any work.  
89  *
90  * Otherwise we successfully obtained the exclusive bit.  Attempt to
91  * clear the shared bits.  If we are able to clear the shared bits 
92  * we win.  Otherwise we lose and we have to restore the shared bits
93  * we couldn't clear (and also clear our exclusive bit).
94  */
95 int
96 spin_trylock_wr_contested(struct spinlock *mtx, int value)
97 {
98         int bit;
99
100         ++spinlocks_contested1;
101         if ((value & SPINLOCK_EXCLUSIVE) == 0) {
102                 while (value) {
103                         bit = bsfl(value);
104                         if (globaldata_find(bit)->gd_spinlocks_rd != 0) {
105                                 atomic_swap_int(&mtx->lock, value);
106                                 return (FALSE);
107                         }
108                         value &= ~(1 << bit);
109                 }
110                 return (TRUE);
111         }
112         return (FALSE);
113 }
114
115 /*
116  * We were either contested due to another exclusive lock holder,
117  * or due to the presence of shared locks
118  *
119  * NOTE: If value indicates an exclusively held mutex, no shared bits
120  * would have been set and we can throw away value. 
121  */
122 void
123 spin_lock_wr_contested(struct spinlock *mtx, int value)
124 {
125         struct exponential_backoff backoff;
126         globaldata_t gd = mycpu;
127         int bit;
128         int mask;
129
130         /*
131          * Wait until we can gain exclusive access vs another exclusive
132          * holder.
133          */
134         exponential_init(&backoff, mtx);
135         ++spinlocks_contested1;
136
137         while (value & SPINLOCK_EXCLUSIVE) {
138                 value = atomic_swap_int(&mtx->lock, 0x80000000);
139                 if (exponential_backoff(&backoff)) {
140                         value &= ~SPINLOCK_EXCLUSIVE;
141                         break;
142                 }
143         }
144
145         /*
146          * Kill the cached shared bit for our own cpu.  This is the most
147          * common case and there's no sense wasting cpu on it.  Since
148          * spinlocks aren't recursive, we can't own a shared ref on the
149          * spinlock while trying to get an exclusive one.
150          *
151          * If multiple bits are set do not stall on any single cpu.  Check
152          * all cpus that have the cache bit set, then loop and check again,
153          * until we've cleaned all the bits.
154          */
155         value &= ~gd->gd_cpumask;
156
157         while ((mask = value) != 0) {
158                 while (mask) {
159                         bit = bsfl(value);
160                         if (globaldata_find(bit)->gd_spinlocks_rd == 0) {
161                                 value &= ~(1 << bit);
162                         } else if (exponential_backoff(&backoff)) {
163                                 value = 0;
164                                 break;
165                         }
166                         mask &= ~(1 << bit);
167                 }
168         }
169 }
170
171 /*
172  * The cache bit wasn't set for our cpu.  Loop until we can set the bit.
173  * As with the spin_lock_rd() inline we need a memory fence after incrementing
174  * gd_spinlocks_rd to interlock against exclusive spinlocks waiting for
175  * that field to clear.
176  */
177 void
178 spin_lock_rd_contested(struct spinlock *mtx)
179 {
180         struct exponential_backoff backoff;
181         globaldata_t gd = mycpu;
182         int value = mtx->lock;
183
184         exponential_init(&backoff, mtx);
185         ++spinlocks_contested1;
186
187         while ((value & gd->gd_cpumask) == 0) {
188                 if (value & SPINLOCK_EXCLUSIVE) {
189                         --gd->gd_spinlocks_rd;
190                         if (exponential_backoff(&backoff)) {
191                                 ++gd->gd_spinlocks_rd;
192                                 break;
193                         }
194                         ++gd->gd_spinlocks_rd;
195                         cpu_mfence();
196                 } else {
197                         if (atomic_cmpset_int(&mtx->lock, value, value|gd->gd_cpumask))
198                                 break;
199                 }
200                 value = mtx->lock;
201         }
202 }
203
204 /*
205  * Handle exponential backoff and indefinite waits.
206  *
207  * If the system is handling a panic we hand the spinlock over to the caller
208  * after 1 second.  After 10 seconds we attempt to print a debugger
209  * backtrace.  We also run pending interrupts in order to allow a console
210  * break into DDB.
211  */
212 static
213 int
214 exponential_backoff(struct exponential_backoff *bo)
215 {
216         sysclock_t count;
217         int i;
218
219         /*
220          * Quick backoff
221          */
222         for (i = 0; i < bo->backoff; ++i)
223                 cpu_nop();
224         if (bo->backoff < BACKOFF_LIMIT) {
225                 bo->backoff <<= 1;
226                 return (FALSE);
227         }
228
229         /*
230          * Indefinite
231          */
232         ++spinlocks_contested2;
233         if (bo->nsec == 0) {
234                 bo->base = sys_cputimer->count();
235                 bo->nsec = 1;
236         }
237
238         count = sys_cputimer->count();
239         if (count - bo->base > sys_cputimer->freq) {
240                 printf("spin_lock: %p, indefinite wait!\n", bo->mtx);
241                 if (panicstr)
242                         return (TRUE);
243 #ifdef INVARIANTS
244                 if (spin_lock_test_mode) {
245                         db_print_backtrace();
246                         return (TRUE);
247                 }
248 #endif
249                 if (++bo->nsec == 11)
250                         db_print_backtrace();
251                 if (bo->nsec == 60)
252                         panic("spin_lock: %p, indefinite wait!\n", bo->mtx);
253                 splz();
254                 bo->base = count;
255         }
256         return (FALSE);
257 }
258
259 /*
260  * If INVARIANTS is enabled various spinlock timing tests can be run
261  * by setting debug.spin_lock_test:
262  *
263  *      1       Test the indefinite wait code
264  *      2       Time the best-case exclusive lock overhead (spin_test_count)
265  *      3       Time the best-case shared lock overhead (spin_test_count)
266  */
267
268 #ifdef INVARIANTS
269
270 static int spin_test_count = 10000000;
271 SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0, "");
272
273 static int
274 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS)
275 {
276         struct spinlock mtx;
277         int error;
278         int value = 0;
279         int i;
280
281         if ((error = suser(curthread)) != 0)
282                 return (error);
283         if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0)
284                 return (error);
285
286         /*
287          * Indefinite wait test
288          */
289         if (value == 1) {
290                 spin_init(&mtx);
291                 spin_lock_wr(&mtx);     /* force an indefinite wait */
292                 spin_lock_test_mode = 1;
293                 spin_lock_wr(&mtx);
294                 spin_unlock_wr(&mtx);   /* Clean up the spinlock count */
295                 spin_unlock_wr(&mtx);
296                 spin_lock_test_mode = 0;
297         }
298
299         /*
300          * Time best-case exclusive spinlocks
301          */
302         if (value == 2) {
303                 globaldata_t gd = mycpu;
304
305                 spin_init(&mtx);
306                 for (i = spin_test_count; i > 0; --i) {
307                     spin_lock_wr_quick(gd, &mtx);
308                     spin_unlock_wr_quick(gd, &mtx);
309                 }
310         }
311
312         /*
313          * Time best-case shared spinlocks
314          */
315         if (value == 3) {
316                 globaldata_t gd = mycpu;
317
318                 spin_init(&mtx);
319                 for (i = spin_test_count; i > 0; --i) {
320                     spin_lock_rd_quick(gd, &mtx);
321                     spin_unlock_rd_quick(gd, &mtx);
322                 }
323         }
324         return (0);
325 }
326
327 SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT,
328         0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code");
329
330 #endif  /* INVARIANTS */
331 #endif  /* SMP */