sys/kern/kern_spinlock.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | /* * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Jeffrey M. Hsu. and Matthew Dillon * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * The implementation is designed to avoid looping when compatible operations * are executed. * * To acquire a spinlock we first increment lock. Then we check if lock * meets our requirements. For an exclusive spinlock it must be 1, of a * shared spinlock it must either be 1 or the SHARED_SPINLOCK bit must be set. * * Shared spinlock failure case: Decrement the count, loop until we can * transition from 0 to SHARED_SPINLOCK|1, or until we find SHARED_SPINLOCK * is set and increment the count. * * Exclusive spinlock failure case: While maintaining the count, clear the * SHARED_SPINLOCK flag unconditionally. Then use an atomic add to transfer * the count from the low bits to the high bits of lock. Then loop until * all low bits are 0. Once the low bits drop to 0 we can transfer the * count back with an atomic_cmpset_int(), atomically, and return. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/types.h> #include <sys/kernel.h> #include <sys/sysctl.h> #ifdef INVARIANTS #include <sys/proc.h> #endif #include <sys/caps.h> #include <machine/atomic.h> #include <machine/cpu.h> #include <machine/cpufunc.h> #include <machine/specialreg.h> #include <machine/clock.h> #include <sys/indefinite2.h> #include <sys/spinlock.h> #include <sys/spinlock2.h> #include <sys/ktr.h> struct spinlock pmap_spin = SPINLOCK_INITIALIZER(pmap_spin, "pmap_spin"); /* * Kernal Trace */ #if !defined(KTR_SPIN_CONTENTION) #define KTR_SPIN_CONTENTION KTR_ALL #endif #define SPIN_STRING "spin=%p type=%c" #define SPIN_ARG_SIZE (sizeof(void *) + sizeof(int)) KTR_INFO_MASTER(spin); #if 0 KTR_INFO(KTR_SPIN_CONTENTION, spin, beg, 0, SPIN_STRING, SPIN_ARG_SIZE); KTR_INFO(KTR_SPIN_CONTENTION, spin, end, 1, SPIN_STRING, SPIN_ARG_SIZE); #endif #define logspin(name, spin, type) \ KTR_LOG(spin_ ## name, spin, type) #ifdef INVARIANTS static int spin_lock_test_mode; #endif #ifdef DEBUG_LOCKS_LATENCY __read_frequently static long spinlocks_add_latency; SYSCTL_LONG(_debug, OID_AUTO, spinlocks_add_latency, CTLFLAG_RW, &spinlocks_add_latency, 0, "Add spinlock latency"); #endif __read_frequently static long spin_backoff_max = 4096; SYSCTL_LONG(_debug, OID_AUTO, spin_backoff_max, CTLFLAG_RW, &spin_backoff_max, 0, "Spinlock exponential backoff limit"); /* 1 << n clock cycles, approx */ __read_frequently static long spin_window_shift = 8; SYSCTL_LONG(_debug, OID_AUTO, spin_window_shift, CTLFLAG_RW, &spin_window_shift, 0, "Spinlock TSC windowing"); __read_frequently int indefinite_uses_rdtsc = 1; SYSCTL_INT(_debug, OID_AUTO, indefinite_uses_rdtsc, CTLFLAG_RW, &indefinite_uses_rdtsc, 0, "Indefinite code uses RDTSC"); /* * We contested due to another exclusive lock holder. We lose. * * We have to unwind the attempt and may acquire the spinlock * anyway while doing so. */ int spin_trylock_contested(struct spinlock *spin) { globaldata_t gd = mycpu; /* * Handle degenerate case, else fail. */ if (atomic_cmpset_int(&spin->lock, SPINLOCK_SHARED|0, 1)) return TRUE; /*atomic_add_int(&spin->lock, -1);*/ --gd->gd_spinlocks; crit_exit_quick(gd->gd_curthread); return (FALSE); } /* * The spin_lock() inline was unable to acquire the lock and calls this * function with spin->lock already incremented, passing (spin->lock - 1) * to the function (the result of the inline's fetchadd). * * Note that we implement both exclusive and shared spinlocks, so we cannot * use atomic_swap_int(). Instead, we try to use atomic_fetchadd_int() * to put most of the burden on the cpu. Atomic_cmpset_int() (cmpxchg) * can cause a lot of unnecessary looping in situations where it is just * trying to increment the count. * * Similarly, we leave the SHARED flag intact and incur slightly more * overhead when switching from shared to exclusive. This allows us to * use atomic_fetchadd_int() for both spinlock types in the critical * path. * * The exponential (n^1.5) backoff algorithm is designed to both reduce * cache bus contention between cpu cores and sockets, and to allow some * bursting of exclusive locks in heavily contended situations to improve * performance. * * The exclusive lock priority mechanism prevents even heavily contended * exclusive locks from being starved by shared locks */ void _spin_lock_contested(struct spinlock *spin, const char *ident, int value) { indefinite_info_t info; uint32_t ovalue; long expbackoff; long loop; /* * WARNING! Caller has already incremented the lock. We must * increment the count value (from the inline's fetch-add) * to match. * * Handle the degenerate case where the spinlock is flagged SHARED * with only our reference. We can convert it to EXCLUSIVE. */ if (value == (SPINLOCK_SHARED | 1) - 1) { if (atomic_cmpset_int(&spin->lock, SPINLOCK_SHARED | 1, 1)) return; } /* ++value; value not used after this */ info.type = 0; /* avoid improper gcc warning */ info.ident = NULL; /* avoid improper gcc warning */ info.secs = 0; /* avoid improper gcc warning */ info.base = 0; /* avoid improper gcc warning */ info.count = 0; /* avoid improper gcc warning */ expbackoff = 0; /* * Transfer our exclusive request to the high bits and clear the * SPINLOCK_SHARED bit if it was set. This makes the spinlock * appear exclusive, preventing any NEW shared or exclusive * spinlocks from being obtained while we wait for existing * shared or exclusive holders to unlock. * * Don't tread on earlier exclusive waiters by stealing the lock * away early if the low bits happen to now be 1. * * The shared unlock understands that this may occur. */ ovalue = atomic_fetchadd_int(&spin->lock, SPINLOCK_EXCLWAIT - 1); ovalue += SPINLOCK_EXCLWAIT - 1; if (ovalue & SPINLOCK_SHARED) { atomic_clear_int(&spin->lock, SPINLOCK_SHARED); ovalue &= ~SPINLOCK_SHARED; } for (;;) { expbackoff = (expbackoff + 1) * 3 / 2; if (expbackoff == 6) /* 1, 3, 6, 10, ... */ indefinite_init(&info, spin, ident, 0, 'S'); if (indefinite_uses_rdtsc) { if ((rdtsc() >> spin_window_shift) % ncpus != mycpuid) { for (loop = expbackoff; loop; --loop) cpu_pause(); } } /*cpu_lfence();*/ /* * If the low bits are zero, try to acquire the exclusive lock * by transfering our high bit reservation to the low bits. * * NOTE: Avoid unconditional atomic op by testing ovalue, * otherwise we get cache bus armageddon. * * NOTE: We must also ensure that the SHARED bit is cleared. * It is possible for it to wind up being set on a * shared lock override of the EXCLWAIT bits. */ ovalue = spin->lock; cpu_ccfence(); if ((ovalue & (SPINLOCK_EXCLWAIT - 1)) == 0) { uint32_t nvalue; nvalue= ((ovalue - SPINLOCK_EXCLWAIT) | 1) & ~SPINLOCK_SHARED; if (atomic_fcmpset_int(&spin->lock, &ovalue, nvalue)) break; continue; } if (expbackoff > 6 + spin_backoff_max) expbackoff = 6 + spin_backoff_max; if (expbackoff >= 6) { if (indefinite_check(&info)) break; } } if (expbackoff >= 6) indefinite_done(&info); } /* * The spin_lock_shared() inline was unable to acquire the lock and calls * this function with spin->lock already incremented. * * This is not in the critical path unless there is contention between * shared and exclusive holders. * * Exclusive locks have priority over shared locks. However, this can * cause shared locks to be starved when large numbers of threads are * competing for exclusive locks so the shared lock code uses TSC-windowing * to selectively ignore the exclusive priority mechanism. This has the * effect of allowing a limited number of shared locks to compete against * exclusive waiters at any given moment. * * Note that shared locks do not implement exponential backoff. Instead, * the shared lock simply polls the lock value. One cpu_pause() is built * into indefinite_check(). */ void _spin_lock_shared_contested(struct spinlock *spin, const char *ident) { indefinite_info_t info; uint32_t ovalue; /* * Undo the inline's increment. */ ovalue = atomic_fetchadd_int(&spin->lock, -1) - 1; indefinite_init(&info, spin, ident, 0, 's'); cpu_pause(); #ifdef DEBUG_LOCKS_LATENCY long j; for (j = spinlocks_add_latency; j > 0; --j) cpu_ccfence(); #endif for (;;) { /* * Loop until we can acquire the shared spinlock. Note that * the low bits can be zero while the high EXCLWAIT bits are * non-zero. In this situation exclusive requesters have * priority (otherwise shared users on multiple cpus can hog * the spinlnock). * * NOTE: Reading spin->lock prior to the swap is extremely * important on multi-chip/many-core boxes. On 48-core * this one change improves fully concurrent all-cores * compiles by 100% or better. * * I can't emphasize enough how important the pre-read * is in preventing hw cache bus armageddon on * multi-chip systems. And on single-chip/multi-core * systems it just doesn't hurt. */ cpu_ccfence(); /* * Ignore the EXCLWAIT bits if we are inside our window. * * We must always use a windowing approach here or the * EXCLWAIT bits can prevent the shared lock from ever * resolving... permanent starvation. * * In addition, if we were to always ignore the EXCLWAIT * bits overlapping shared locks can prevent an exclusive * lock from ever resolving... permanent starvation again. */ if (/*indefinite_uses_rdtsc &&*/ (ovalue & (SPINLOCK_EXCLWAIT - 1)) == 0 && (rdtsc() >> spin_window_shift) % ncpus == mycpuid) { if (atomic_fcmpset_int(&spin->lock, &ovalue, ovalue | SPINLOCK_SHARED | 1)) { break; } continue; } /* * Check ovalue tightly (no exponential backoff for shared * locks, that would result in horrible performance. Instead, * shared locks depend on the exclusive priority mechanism * to avoid starving exclusive locks). */ if (ovalue == 0) { if (atomic_fcmpset_int(&spin->lock, &ovalue, SPINLOCK_SHARED | 1)) { break; } continue; } /* * If SHARED is already set, go for the increment, improving * the exclusive to multiple-readers transition. */ if (ovalue & SPINLOCK_SHARED) { ovalue = atomic_fetchadd_int(&spin->lock, 1); /* ovalue += 1; NOT NEEDED */ if (ovalue & SPINLOCK_SHARED) break; ovalue = atomic_fetchadd_int(&spin->lock, -1); ovalue += -1; continue; } if (indefinite_check(&info)) break; /* * ovalue was wrong anyway, just reload */ ovalue = spin->lock; } indefinite_done(&info); } /* * Automatically avoid use of rdtsc when running in a VM */ static void spinlock_sysinit(void *dummy __unused) { if (vmm_guest) indefinite_uses_rdtsc = 0; } SYSINIT(spinsysinit, SI_BOOT2_PROC0, SI_ORDER_FIRST, spinlock_sysinit, NULL); /* * If INVARIANTS is enabled various spinlock timing tests can be run * by setting debug.spin_lock_test: * * 1 Test the indefinite wait code * 2 Time the best-case exclusive lock overhead (spin_test_count) * 3 Time the best-case shared lock overhead (spin_test_count) */ #ifdef INVARIANTS static int spin_test_count = 10000000; SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0, "Number of iterations to use for spinlock wait code test"); static int sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS) { struct spinlock spin; int error; int value = 0; int i; if ((error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT)) != 0) return (error); if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0) return (error); /* * Indefinite wait test */ if (value == 1) { spin_init(&spin, "sysctllock"); spin_lock(&spin); /* force an indefinite wait */ spin_lock_test_mode = 1; spin_lock(&spin); spin_unlock(&spin); /* Clean up the spinlock count */ spin_unlock(&spin); spin_lock_test_mode = 0; } /* * Time best-case exclusive spinlocks */ if (value == 2) { globaldata_t gd = mycpu; spin_init(&spin, "sysctllocktest"); for (i = spin_test_count; i > 0; --i) { _spin_lock_quick(gd, &spin, "test"); spin_unlock_quick(gd, &spin); } } return (0); } SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT, 0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code"); #endif /* INVARIANTS */ |