kernel -- Add pool spinlocks interface.
[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.16 2008/09/11 01:11:42 y0netan1 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 <sys/priv.h>
44 #include <machine/atomic.h>
45 #include <machine/cpufunc.h>
46 #include <machine/specialreg.h>
47 #include <machine/clock.h>
48 #include <sys/spinlock.h>
49 #include <sys/spinlock2.h>
50 #include <sys/ktr.h>
51
52 #define BACKOFF_INITIAL 1
53 #define BACKOFF_LIMIT   256
54
55 #ifdef SMP
56
57 /*
58  * Kernal Trace
59  */
60 #if !defined(KTR_SPIN_CONTENTION)
61 #define KTR_SPIN_CONTENTION     KTR_ALL
62 #endif
63 #define SPIN_STRING     "spin=%p type=%c"
64 #define SPIN_ARG_SIZE   (sizeof(void *) + sizeof(int))
65
66 KTR_INFO_MASTER(spin);
67 KTR_INFO(KTR_SPIN_CONTENTION, spin, beg, 0, SPIN_STRING, SPIN_ARG_SIZE);
68 KTR_INFO(KTR_SPIN_CONTENTION, spin, end, 1, SPIN_STRING, SPIN_ARG_SIZE);
69 KTR_INFO(KTR_SPIN_CONTENTION, spin, backoff, 2,
70          "spin=%p bo1=%d thr=%p bo=%d",
71          ((2 * sizeof(void *)) + (2 * sizeof(int))));
72 KTR_INFO(KTR_SPIN_CONTENTION, spin, bofail, 3, SPIN_STRING, SPIN_ARG_SIZE);
73
74 #define logspin(name, mtx, type)                        \
75         KTR_LOG(spin_ ## name, mtx, type)
76
77 #define logspin_backoff(mtx, bo1, thr, bo)              \
78         KTR_LOG(spin_backoff, mtx, bo1, thr, bo)
79
80 #ifdef INVARIANTS
81 static int spin_lock_test_mode;
82 #endif
83
84 static int64_t spinlocks_contested1;
85 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested1, CTLFLAG_RD,
86     &spinlocks_contested1, 0,
87     "Spinlock contention count due to collisions with exclusive lock holders");
88
89 static int64_t spinlocks_contested2;
90 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested2, CTLFLAG_RD,
91     &spinlocks_contested2, 0,
92     "Serious spinlock contention count");
93
94 static int spinlocks_backoff_limit = BACKOFF_LIMIT;
95 SYSCTL_INT(_debug, OID_AUTO, spinlocks_bolim, CTLFLAG_RW,
96     &spinlocks_backoff_limit, 0,
97     "Contested spinlock backoff limit");
98
99 #define SPINLOCK_NUM_POOL       (1024)
100 static struct spinlock pool_spinlocks[SPINLOCK_NUM_POOL];
101
102 struct exponential_backoff {
103         int backoff;
104         int nsec;
105         struct spinlock *mtx;
106         sysclock_t base;
107 };
108 static int exponential_backoff(struct exponential_backoff *bo);
109
110 static __inline
111 void
112 exponential_init(struct exponential_backoff *bo, struct spinlock *mtx)
113 {
114         bo->backoff = BACKOFF_INITIAL;
115         bo->nsec = 0;
116         bo->mtx = mtx;
117         bo->base = 0;   /* silence gcc */
118 }
119
120 /*
121  * We contested due to another exclusive lock holder.  We lose.
122  */
123 int
124 spin_trylock_wr_contested2(globaldata_t gd)
125 {
126         ++spinlocks_contested1;
127         --gd->gd_spinlocks_wr;
128         --gd->gd_curthread->td_critcount;
129         return (FALSE);
130 }
131
132 /*
133  * We were either contested due to another exclusive lock holder,
134  * or due to the presence of shared locks
135  *
136  * NOTE: If value indicates an exclusively held mutex, no shared bits
137  * would have been set and we can throw away value. 
138  */
139 void
140 spin_lock_wr_contested2(struct spinlock *mtx)
141 {
142         struct exponential_backoff backoff;
143         int value;
144
145         /*
146          * Wait until we can gain exclusive access vs another exclusive
147          * holder.
148          */
149         ++spinlocks_contested1;
150         exponential_init(&backoff, mtx);
151
152         logspin(beg, mtx, 'w');
153         do {
154                 if (exponential_backoff(&backoff))
155                         break;
156                 value = atomic_swap_int(&mtx->lock, SPINLOCK_EXCLUSIVE);
157         } while (value & SPINLOCK_EXCLUSIVE);
158         logspin(end, mtx, 'w');
159 }
160
161 static __inline int
162 _spin_pool_hash(void *ptr)
163 {
164         int i;
165         i = ((int) (uintptr_t) ptr >> 2) ^ ((int) (uintptr_t) ptr >> 12);
166         i &= (SPINLOCK_NUM_POOL - 1);
167         return (i);
168 }
169
170 struct spinlock *
171 spin_pool_lock(void *chan)
172 {
173         struct spinlock *sp;
174
175         sp = &pool_spinlocks[_spin_pool_hash(chan)];
176         spin_lock(sp);
177
178         return (sp);
179 }
180
181 void
182 spin_pool_unlock(void *chan)
183 {
184         struct spinlock *sp;
185
186         sp = &pool_spinlocks[_spin_pool_hash(chan)];
187         spin_unlock(sp);
188 }
189
190 /*
191  * Handle exponential backoff and indefinite waits.
192  *
193  * If the system is handling a panic we hand the spinlock over to the caller
194  * after 1 second.  After 10 seconds we attempt to print a debugger
195  * backtrace.  We also run pending interrupts in order to allow a console
196  * break into DDB.
197  */
198 static
199 int
200 exponential_backoff(struct exponential_backoff *bo)
201 {
202         sysclock_t count;
203         int backoff;
204
205 #ifdef _RDTSC_SUPPORTED_
206         if (cpu_feature & CPUID_TSC) {
207                 backoff =
208                 (((u_long)rdtsc() ^ (((u_long)curthread) >> 5)) &
209                  (bo->backoff - 1)) + BACKOFF_INITIAL;
210         } else
211 #endif
212                 backoff = bo->backoff;
213         logspin_backoff(bo->mtx, bo->backoff, curthread, backoff);
214
215         /*
216          * Quick backoff
217          */
218         for (; backoff; --backoff)
219                 cpu_pause();
220         if (bo->backoff < spinlocks_backoff_limit) {
221                 bo->backoff <<= 1;
222                 return (FALSE);
223         } else {
224                 bo->backoff = BACKOFF_INITIAL;
225         }
226
227         logspin(bofail, bo->mtx, 'u');
228
229         /*
230          * Indefinite
231          */
232         ++spinlocks_contested2;
233         cpu_spinlock_contested();
234         if (bo->nsec == 0) {
235                 bo->base = sys_cputimer->count();
236                 bo->nsec = 1;
237         }
238
239         count = sys_cputimer->count();
240         if (count - bo->base > sys_cputimer->freq) {
241                 kprintf("spin_lock: %p, indefinite wait!\n", bo->mtx);
242                 if (panicstr)
243                         return (TRUE);
244 #if defined(INVARIANTS)
245                 if (spin_lock_test_mode) {
246                         print_backtrace(-1);
247                         return (TRUE);
248                 }
249 #endif
250                 ++bo->nsec;
251 #if defined(INVARIANTS)
252                 if (bo->nsec == 11)
253                         print_backtrace(-1);
254 #endif
255                 if (bo->nsec == 60)
256                         panic("spin_lock: %p, indefinite wait!\n", bo->mtx);
257                 bo->base = count;
258         }
259         return (FALSE);
260 }
261
262 /*
263  * If INVARIANTS is enabled various spinlock timing tests can be run
264  * by setting debug.spin_lock_test:
265  *
266  *      1       Test the indefinite wait code
267  *      2       Time the best-case exclusive lock overhead (spin_test_count)
268  *      3       Time the best-case shared lock overhead (spin_test_count)
269  */
270
271 #ifdef INVARIANTS
272
273 static int spin_test_count = 10000000;
274 SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0,
275     "Number of iterations to use for spinlock wait code test");
276
277 static int
278 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS)
279 {
280         struct spinlock mtx;
281         int error;
282         int value = 0;
283         int i;
284
285         if ((error = priv_check(curthread, PRIV_ROOT)) != 0)
286                 return (error);
287         if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0)
288                 return (error);
289
290         /*
291          * Indefinite wait test
292          */
293         if (value == 1) {
294                 spin_init(&mtx);
295                 spin_lock(&mtx);        /* force an indefinite wait */
296                 spin_lock_test_mode = 1;
297                 spin_lock(&mtx);
298                 spin_unlock(&mtx);      /* Clean up the spinlock count */
299                 spin_unlock(&mtx);
300                 spin_lock_test_mode = 0;
301         }
302
303         /*
304          * Time best-case exclusive spinlocks
305          */
306         if (value == 2) {
307                 globaldata_t gd = mycpu;
308
309                 spin_init(&mtx);
310                 for (i = spin_test_count; i > 0; --i) {
311                     spin_lock_quick(gd, &mtx);
312                     spin_unlock_quick(gd, &mtx);
313                 }
314         }
315
316         return (0);
317 }
318
319 SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT,
320         0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code");
321
322 #endif  /* INVARIANTS */
323 #endif  /* SMP */