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