| Commit | Line | Data |
|---|---|---|
| b1af91cb JH |
1 | /* |
| 2 | * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved. | |
| 3 | * | |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| d666840a | 5 | * by Jeffrey M. Hsu. and Matthew Dillon |
| b1af91cb JH |
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 | * | |
| 1541028a | 32 | * $DragonFly: src/sys/kern/kern_spinlock.c,v 1.16 2008/09/11 01:11:42 y0netan1 Exp $ |
| b1af91cb JH |
33 | */ |
| 34 | ||
| 35 | #include <sys/param.h> | |
| b02926de | 36 | #include <sys/systm.h> |
| b1af91cb | 37 | #include <sys/types.h> |
| b02926de MD |
38 | #include <sys/kernel.h> |
| 39 | #include <sys/sysctl.h> | |
| 40 | #ifdef INVARIANTS | |
| 41 | #include <sys/proc.h> | |
| 42 | #endif | |
| 895c1f85 | 43 | #include <sys/priv.h> |
| b1af91cb | 44 | #include <machine/atomic.h> |
| b02926de | 45 | #include <machine/cpufunc.h> |
| 10c66d57 | 46 | #include <machine/specialreg.h> |
| b02926de | 47 | #include <machine/clock.h> |
| b1af91cb | 48 | #include <sys/spinlock.h> |
| 35a832df | 49 | #include <sys/spinlock2.h> |
| 57aa743c | 50 | #include <sys/ktr.h> |
| b1af91cb JH |
51 | |
| 52 | #define BACKOFF_INITIAL 1 | |
| 53 | #define BACKOFF_LIMIT 256 | |
| 54 | ||
| 55 | #ifdef SMP | |
| 56 | ||
| 57aa743c MD |
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); | |
| 10c66d57 SZ |
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); | |
| 57aa743c MD |
73 | |
| 74 | #define logspin(name, mtx, type) \ | |
| 75 | KTR_LOG(spin_ ## name, mtx, type) | |
| 76 | ||
| 10c66d57 SZ |
77 | #define logspin_backoff(mtx, bo1, thr, bo) \ |
| 78 | KTR_LOG(spin_backoff, mtx, bo1, thr, bo) | |
| 79 | ||
| b02926de MD |
80 | #ifdef INVARIANTS |
| 81 | static int spin_lock_test_mode; | |
| 82 | #endif | |
| 83 | ||
| 84 | static int64_t spinlocks_contested1; | |
| 10c66d57 | 85 | SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested1, CTLFLAG_RD, |
| 0c52fa62 SG |
86 | &spinlocks_contested1, 0, |
| 87 | "Spinlock contention count due to collisions with exclusive lock holders"); | |
| 10c66d57 | 88 | |
| b02926de | 89 | static int64_t spinlocks_contested2; |
| 10c66d57 | 90 | SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested2, CTLFLAG_RD, |
| 0c52fa62 SG |
91 | &spinlocks_contested2, 0, |
| 92 | "Serious spinlock contention count"); | |
| 10c66d57 SZ |
93 | |
| 94 | static int spinlocks_backoff_limit = BACKOFF_LIMIT; | |
| 95 | SYSCTL_INT(_debug, OID_AUTO, spinlocks_bolim, CTLFLAG_RW, | |
| 0c52fa62 SG |
96 | &spinlocks_backoff_limit, 0, |
| 97 | "Contested spinlock backoff limit"); | |
| b02926de | 98 | |
| d666840a MD |
99 | struct exponential_backoff { |
| 100 | int backoff; | |
| 101 | int nsec; | |
| 102 | struct spinlock *mtx; | |
| 103 | sysclock_t base; | |
| 104 | }; | |
| 105 | static int exponential_backoff(struct exponential_backoff *bo); | |
| 106 | ||
| 107 | static __inline | |
| b1af91cb | 108 | void |
| d666840a | 109 | exponential_init(struct exponential_backoff *bo, struct spinlock *mtx) |
| b1af91cb | 110 | { |
| d666840a MD |
111 | bo->backoff = BACKOFF_INITIAL; |
| 112 | bo->nsec = 0; | |
| 113 | bo->mtx = mtx; | |
| 8f165b8c | 114 | bo->base = 0; /* silence gcc */ |
| d666840a MD |
115 | } |
| 116 | ||
| 117 | /* | |
| 8f165b8c | 118 | * We contested due to another exclusive lock holder. We lose. |
| d666840a MD |
119 | */ |
| 120 | int | |
| 8f165b8c | 121 | spin_trylock_wr_contested2(globaldata_t gd) |
| d666840a | 122 | { |
| b02926de | 123 | ++spinlocks_contested1; |
| 74af985e | 124 | --gd->gd_spinlocks_wr; |
| 77912481 | 125 | --gd->gd_curthread->td_critcount; |
| d666840a | 126 | return (FALSE); |
| b1af91cb JH |
127 | } |
| 128 | ||
| d666840a MD |
129 | /* |
| 130 | * We were either contested due to another exclusive lock holder, | |
| 131 | * or due to the presence of shared locks | |
| 132 | * | |
| 133 | * NOTE: If value indicates an exclusively held mutex, no shared bits | |
| 134 | * would have been set and we can throw away value. | |
| 135 | */ | |
| 136 | void | |
| 8f165b8c | 137 | spin_lock_wr_contested2(struct spinlock *mtx) |
| d666840a MD |
138 | { |
| 139 | struct exponential_backoff backoff; | |
| 8f165b8c | 140 | int value; |
| d666840a MD |
141 | |
| 142 | /* | |
| 143 | * Wait until we can gain exclusive access vs another exclusive | |
| 144 | * holder. | |
| 145 | */ | |
| d666840a | 146 | ++spinlocks_contested1; |
| 8f165b8c | 147 | exponential_init(&backoff, mtx); |
| d666840a | 148 | |
| 8f165b8c MD |
149 | logspin(beg, mtx, 'w'); |
| 150 | do { | |
| 151 | if (exponential_backoff(&backoff)) | |
| d666840a | 152 | break; |
| 8f165b8c MD |
153 | value = atomic_swap_int(&mtx->lock, SPINLOCK_EXCLUSIVE); |
| 154 | } while (value & SPINLOCK_EXCLUSIVE); | |
| 57aa743c | 155 | logspin(end, mtx, 'w'); |
| d666840a | 156 | } |
| b02926de MD |
157 | |
| 158 | /* | |
| d666840a MD |
159 | * Handle exponential backoff and indefinite waits. |
| 160 | * | |
| 161 | * If the system is handling a panic we hand the spinlock over to the caller | |
| 162 | * after 1 second. After 10 seconds we attempt to print a debugger | |
| 163 | * backtrace. We also run pending interrupts in order to allow a console | |
| 164 | * break into DDB. | |
| 165 | */ | |
| 166 | static | |
| 167 | int | |
| 168 | exponential_backoff(struct exponential_backoff *bo) | |
| b02926de | 169 | { |
| b02926de | 170 | sysclock_t count; |
| 10c66d57 SZ |
171 | int backoff; |
| 172 | ||
| 173 | #ifdef _RDTSC_SUPPORTED_ | |
| 174 | if (cpu_feature & CPUID_TSC) { | |
| 175 | backoff = | |
| 58668add SZ |
176 | (((u_long)rdtsc() ^ (((u_long)curthread) >> 5)) & |
| 177 | (bo->backoff - 1)) + BACKOFF_INITIAL; | |
| 10c66d57 SZ |
178 | } else |
| 179 | #endif | |
| 180 | backoff = bo->backoff; | |
| 181 | logspin_backoff(bo->mtx, bo->backoff, curthread, backoff); | |
| d666840a MD |
182 | |
| 183 | /* | |
| 184 | * Quick backoff | |
| 185 | */ | |
| 10c66d57 | 186 | for (; backoff; --backoff) |
| 14dd663d | 187 | cpu_pause(); |
| 10c66d57 | 188 | if (bo->backoff < spinlocks_backoff_limit) { |
| d666840a MD |
189 | bo->backoff <<= 1; |
| 190 | return (FALSE); | |
| 10c66d57 SZ |
191 | } else { |
| 192 | bo->backoff = BACKOFF_INITIAL; | |
| d666840a | 193 | } |
| b02926de | 194 | |
| 10c66d57 SZ |
195 | logspin(bofail, bo->mtx, 'u'); |
| 196 | ||
| d666840a MD |
197 | /* |
| 198 | * Indefinite | |
| 199 | */ | |
| b02926de | 200 | ++spinlocks_contested2; |
| 06615ccb | 201 | cpu_spinlock_contested(); |
| d666840a MD |
202 | if (bo->nsec == 0) { |
| 203 | bo->base = sys_cputimer->count(); | |
| 204 | bo->nsec = 1; | |
| 205 | } | |
| 206 | ||
| 207 | count = sys_cputimer->count(); | |
| 208 | if (count - bo->base > sys_cputimer->freq) { | |
| 6ea70f76 | 209 | kprintf("spin_lock: %p, indefinite wait!\n", bo->mtx); |
| d666840a MD |
210 | if (panicstr) |
| 211 | return (TRUE); | |
| 1e5fb84b | 212 | #if defined(INVARIANTS) |
| d666840a | 213 | if (spin_lock_test_mode) { |
| 7ce2998e | 214 | print_backtrace(-1); |
| d666840a | 215 | return (TRUE); |
| b02926de | 216 | } |
| d666840a | 217 | #endif |
| b526356c | 218 | ++bo->nsec; |
| 1e5fb84b | 219 | #if defined(INVARIANTS) |
| b526356c | 220 | if (bo->nsec == 11) |
| 7ce2998e | 221 | print_backtrace(-1); |
| b526356c | 222 | #endif |
| d666840a MD |
223 | if (bo->nsec == 60) |
| 224 | panic("spin_lock: %p, indefinite wait!\n", bo->mtx); | |
| d666840a | 225 | bo->base = count; |
| b02926de | 226 | } |
| d666840a | 227 | return (FALSE); |
| b02926de MD |
228 | } |
| 229 | ||
| 230 | /* | |
| d666840a MD |
231 | * If INVARIANTS is enabled various spinlock timing tests can be run |
| 232 | * by setting debug.spin_lock_test: | |
| 233 | * | |
| 234 | * 1 Test the indefinite wait code | |
| 235 | * 2 Time the best-case exclusive lock overhead (spin_test_count) | |
| 236 | * 3 Time the best-case shared lock overhead (spin_test_count) | |
| b02926de MD |
237 | */ |
| 238 | ||
| 239 | #ifdef INVARIANTS | |
| 240 | ||
| d666840a | 241 | static int spin_test_count = 10000000; |
| 0c52fa62 SG |
242 | SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0, |
| 243 | "Number of iterations to use for spinlock wait code test"); | |
| d666840a | 244 | |
| b02926de MD |
245 | static int |
| 246 | sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS) | |
| 247 | { | |
| 248 | struct spinlock mtx; | |
| 249 | int error; | |
| 250 | int value = 0; | |
| d666840a | 251 | int i; |
| b02926de | 252 | |
| 895c1f85 | 253 | if ((error = priv_check(curthread, PRIV_ROOT)) != 0) |
| b02926de MD |
254 | return (error); |
| 255 | if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0) | |
| 256 | return (error); | |
| 257 | ||
| d666840a MD |
258 | /* |
| 259 | * Indefinite wait test | |
| 260 | */ | |
| b02926de | 261 | if (value == 1) { |
| d666840a | 262 | spin_init(&mtx); |
| 287a8577 | 263 | spin_lock(&mtx); /* force an indefinite wait */ |
| b02926de | 264 | spin_lock_test_mode = 1; |
| 287a8577 AH |
265 | spin_lock(&mtx); |
| 266 | spin_unlock(&mtx); /* Clean up the spinlock count */ | |
| 267 | spin_unlock(&mtx); | |
| b02926de MD |
268 | spin_lock_test_mode = 0; |
| 269 | } | |
| d666840a MD |
270 | |
| 271 | /* | |
| 272 | * Time best-case exclusive spinlocks | |
| 273 | */ | |
| 274 | if (value == 2) { | |
| 275 | globaldata_t gd = mycpu; | |
| 276 | ||
| 277 | spin_init(&mtx); | |
| 278 | for (i = spin_test_count; i > 0; --i) { | |
| 7cfe2b28 AH |
279 | spin_lock_quick(gd, &mtx); |
| 280 | spin_unlock_quick(gd, &mtx); | |
| d666840a MD |
281 | } |
| 282 | } | |
| 283 | ||
| b02926de MD |
284 | return (0); |
| 285 | } | |
| 286 | ||
| 287 | SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT, | |
| 288 | 0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code"); | |
| 289 | ||
| d666840a MD |
290 | #endif /* INVARIANTS */ |
| 291 | #endif /* SMP */ |