5e7bc8176c89fd1d1e886bcdf76acac0dfb40b34
[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 /*
33  * The spinlock code utilizes two counters to form a virtual FIFO, allowing
34  * a spinlock to allocate a slot and then only issue memory read operations
35  * until it is handed the lock (if it is not the next owner for the lock).
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #ifdef INVARIANTS
44 #include <sys/proc.h>
45 #endif
46 #include <sys/priv.h>
47 #include <machine/atomic.h>
48 #include <machine/cpu.h>
49 #include <machine/cpufunc.h>
50 #include <machine/specialreg.h>
51 #include <machine/clock.h>
52 #include <sys/spinlock.h>
53 #include <sys/spinlock2.h>
54 #include <sys/ktr.h>
55
56 struct spinlock pmap_spin = SPINLOCK_INITIALIZER(pmap_spin);
57
58 #ifdef SMP
59
60 struct indefinite_info {
61         sysclock_t      base;
62         int             secs;
63 };
64
65 /*
66  * Kernal Trace
67  */
68 #if !defined(KTR_SPIN_CONTENTION)
69 #define KTR_SPIN_CONTENTION     KTR_ALL
70 #endif
71 #define SPIN_STRING     "spin=%p type=%c"
72 #define SPIN_ARG_SIZE   (sizeof(void *) + sizeof(int))
73
74 KTR_INFO_MASTER(spin);
75 KTR_INFO(KTR_SPIN_CONTENTION, spin, beg, 0, SPIN_STRING, SPIN_ARG_SIZE);
76 KTR_INFO(KTR_SPIN_CONTENTION, spin, end, 1, SPIN_STRING, SPIN_ARG_SIZE);
77
78 #define logspin(name, spin, type)                       \
79         KTR_LOG(spin_ ## name, spin, type)
80
81 #ifdef INVARIANTS
82 static int spin_lock_test_mode;
83 #endif
84
85 static int64_t spinlocks_contested1;
86 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested1, CTLFLAG_RD,
87     &spinlocks_contested1, 0,
88     "Spinlock contention count due to collisions with exclusive lock holders");
89
90 static int64_t spinlocks_contested2;
91 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested2, CTLFLAG_RD,
92     &spinlocks_contested2, 0,
93     "Serious spinlock contention count");
94
95 static int spinlocks_hardloops = 40;
96 SYSCTL_INT(_debug, OID_AUTO, spinlocks_hardloops, CTLFLAG_RW,
97     &spinlocks_hardloops, 0,
98     "Hard loops waiting for spinlock");
99
100 #define SPINLOCK_NUM_POOL       (1024)
101 static struct spinlock pool_spinlocks[SPINLOCK_NUM_POOL];
102
103 static int spin_indefinite_check(struct spinlock *spin,
104                                   struct indefinite_info *info);
105
106 /*
107  * We contested due to another exclusive lock holder.  We lose.
108  *
109  * We have to unwind the attempt and may acquire the spinlock
110  * anyway while doing so.  countb was incremented on our behalf.
111  */
112 int
113 spin_trylock_contested(struct spinlock *spin)
114 {
115         globaldata_t gd = mycpu;
116
117         /*++spinlocks_contested1;*/
118         --gd->gd_spinlocks_wr;
119         --gd->gd_curthread->td_critcount;
120         return (FALSE);
121 }
122
123 /*
124  * The spin_lock() inline was unable to acquire the lock.
125  *
126  * atomic_swap_int() is the absolute fastest spinlock instruction, at
127  * least on multi-socket systems.  All instructions seem to be about
128  * the same on single-socket multi-core systems.
129  */
130 void
131 spin_lock_contested(struct spinlock *spin)
132 {
133         int i;
134
135         i = 0;
136         while (atomic_swap_int(&spin->counta, 1)) {
137                 cpu_pause();
138                 if (i == spinlocks_hardloops) {
139                         struct indefinite_info info = { 0, 0 };
140
141                         logspin(beg, spin, 'w');
142                         while (atomic_swap_int(&spin->counta, 1)) {
143                                 cpu_pause();
144                                 ++spin->countb;
145                                 if ((++i & 0x7F) == 0x7F) {
146                                         if (spin_indefinite_check(spin, &info))
147                                                 break;
148                                 }
149                         }
150                         logspin(end, spin, 'w');
151                         return;
152                 }
153                 ++spin->countb;
154                 ++i;
155         }
156 }
157
158 static __inline int
159 _spin_pool_hash(void *ptr)
160 {
161         int i;
162         i = ((int) (uintptr_t) ptr >> 2) ^ ((int) (uintptr_t) ptr >> 12);
163         i &= (SPINLOCK_NUM_POOL - 1);
164         return (i);
165 }
166
167 void
168 _spin_pool_lock(void *chan)
169 {
170         struct spinlock *sp;
171
172         sp = &pool_spinlocks[_spin_pool_hash(chan)];
173         spin_lock(sp);
174 }
175
176 void
177 _spin_pool_unlock(void *chan)
178 {
179         struct spinlock *sp;
180
181         sp = &pool_spinlocks[_spin_pool_hash(chan)];
182         spin_unlock(sp);
183 }
184
185
186 static
187 int
188 spin_indefinite_check(struct spinlock *spin, struct indefinite_info *info)
189 {
190         sysclock_t count;
191
192         cpu_spinlock_contested();
193
194         count = sys_cputimer->count();
195         if (info->secs == 0) {
196                 info->base = count;
197                 ++info->secs;
198         } else if (count - info->base > sys_cputimer->freq) {
199                 kprintf("spin_lock: %p, indefinite wait (%d secs)!\n",
200                         spin, info->secs);
201                 info->base = count;
202                 ++info->secs;
203                 if (panicstr)
204                         return (TRUE);
205 #if defined(INVARIANTS)
206                 if (spin_lock_test_mode) {
207                         print_backtrace(-1);
208                         return (TRUE);
209                 }
210 #endif
211 #if defined(INVARIANTS)
212                 if (info->secs == 11)
213                         print_backtrace(-1);
214 #endif
215                 if (info->secs == 60)
216                         panic("spin_lock: %p, indefinite wait!\n", spin);
217         }
218         return (FALSE);
219 }
220
221 /*
222  * If INVARIANTS is enabled various spinlock timing tests can be run
223  * by setting debug.spin_lock_test:
224  *
225  *      1       Test the indefinite wait code
226  *      2       Time the best-case exclusive lock overhead (spin_test_count)
227  *      3       Time the best-case shared lock overhead (spin_test_count)
228  */
229
230 #ifdef INVARIANTS
231
232 static int spin_test_count = 10000000;
233 SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0,
234     "Number of iterations to use for spinlock wait code test");
235
236 static int
237 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS)
238 {
239         struct spinlock spin;
240         int error;
241         int value = 0;
242         int i;
243
244         if ((error = priv_check(curthread, PRIV_ROOT)) != 0)
245                 return (error);
246         if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0)
247                 return (error);
248
249         /*
250          * Indefinite wait test
251          */
252         if (value == 1) {
253                 spin_init(&spin);
254                 spin_lock(&spin);       /* force an indefinite wait */
255                 spin_lock_test_mode = 1;
256                 spin_lock(&spin);
257                 spin_unlock(&spin);     /* Clean up the spinlock count */
258                 spin_unlock(&spin);
259                 spin_lock_test_mode = 0;
260         }
261
262         /*
263          * Time best-case exclusive spinlocks
264          */
265         if (value == 2) {
266                 globaldata_t gd = mycpu;
267
268                 spin_init(&spin);
269                 for (i = spin_test_count; i > 0; --i) {
270                     spin_lock_quick(gd, &spin);
271                     spin_unlock_quick(gd, &spin);
272                 }
273         }
274
275         return (0);
276 }
277
278 SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT,
279         0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code");
280
281 #endif  /* INVARIANTS */
282 #endif  /* SMP */