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 #if 0 76 KTR_INFO(KTR_SPIN_CONTENTION, spin, beg, 0, SPIN_STRING, SPIN_ARG_SIZE); 77 KTR_INFO(KTR_SPIN_CONTENTION, spin, end, 1, SPIN_STRING, SPIN_ARG_SIZE); 78 #endif 79 80 #define logspin(name, spin, type) \ 81 KTR_LOG(spin_ ## name, spin, type) 82 83 #ifdef INVARIANTS 84 static int spin_lock_test_mode; 85 #endif 86 87 static int64_t spinlocks_contested1; 88 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested1, CTLFLAG_RD, 89 &spinlocks_contested1, 0, 90 "Spinlock contention count due to collisions with exclusive lock holders"); 91 92 static int64_t spinlocks_contested2; 93 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested2, CTLFLAG_RD, 94 &spinlocks_contested2, 0, 95 "Serious spinlock contention count"); 96 97 /* 98 * We need a fairly large pool to avoid contention on large SMP systems, 99 * particularly multi-chip systems. 100 */ 101 /*#define SPINLOCK_NUM_POOL 8101*/ 102 #define SPINLOCK_NUM_POOL 8192 103 #define SPINLOCK_NUM_POOL_MASK (SPINLOCK_NUM_POOL - 1) 104 105 static __cachealign struct { 106 struct spinlock spin; 107 char filler[32 - sizeof(struct spinlock)]; 108 } pool_spinlocks[SPINLOCK_NUM_POOL]; 109 110 static int spin_indefinite_check(struct spinlock *spin, 111 struct indefinite_info *info); 112 113 /* 114 * We contested due to another exclusive lock holder. We lose. 115 * 116 * We have to unwind the attempt and may acquire the spinlock 117 * anyway while doing so. countb was incremented on our behalf. 118 */ 119 int 120 spin_trylock_contested(struct spinlock *spin) 121 { 122 globaldata_t gd = mycpu; 123 124 /*++spinlocks_contested1;*/ 125 --gd->gd_spinlocks_wr; 126 --gd->gd_curthread->td_critcount; 127 return (FALSE); 128 } 129 130 /* 131 * The spin_lock() inline was unable to acquire the lock. 132 * 133 * atomic_swap_int() is the absolute fastest spinlock instruction, at 134 * least on multi-socket systems. All instructions seem to be about 135 * the same on single-socket multi-core systems. However, atomic_swap_int() 136 * does not result in an even distribution of successful acquisitions. 137 * 138 * Another problem we have is that (at least on the 48-core opteron we test 139 * with) having all 48 cores contesting the same spin lock reduces 140 * performance to around 600,000 ops/sec, verses millions when fewer cores 141 * are going after the same lock. 142 * 143 * Backoff algorithms can create even worse starvation problems, and don't 144 * really improve performance when a lot of cores are contending. 145 * 146 * Our solution is to allow the data cache to lazy-update by reading it 147 * non-atomically and only attempting to acquire the lock if the lazy read 148 * looks good. This effectively limits cache bus bandwidth. A cpu_pause() 149 * (for intel/amd anyhow) is not strictly needed as cache bus resource use 150 * is governed by the lazy update. 151 * 152 * WARNING!!!! Performance matters here, by a huge margin. 153 * 154 * 48-core test with pre-read / -j 48 no-modules kernel compile 155 * with fanned-out inactive and active queues came in at 55 seconds. 156 * 157 * 48-core test with pre-read / -j 48 no-modules kernel compile 158 * came in at 75 seconds. Without pre-read it came in at 170 seconds. 159 * 160 * 4-core test with pre-read / -j 48 no-modules kernel compile 161 * came in at 83 seconds. Without pre-read it came in at 83 seconds 162 * as well (no difference). 163 */ 164 void 165 spin_lock_contested(struct spinlock *spin) 166 { 167 struct indefinite_info info = { 0, 0 }; 168 int i; 169 170 i = 0; 171 ++spin->countb; 172 173 /*logspin(beg, spin, 'w');*/ 174 for (;;) { 175 /* 176 * NOTE: Reading spin->counta prior to the swap is extremely 177 * important on multi-chip/many-core boxes. On 48-core 178 * this one change improves fully concurrent all-cores 179 * compiles by 100% or better. 180 * 181 * I can't emphasize enough how important the pre-read is in 182 * preventing hw cache bus armageddon on multi-chip systems. 183 * And on single-chip/multi-core systems it just doesn't hurt. 184 */ 185 if (spin->counta == 0 && atomic_swap_int(&spin->counta, 1) == 0) 186 break; 187 if ((++i & 0x7F) == 0x7F) { 188 ++spin->countb; 189 if (spin_indefinite_check(spin, &info)) 190 break; 191 } 192 } 193 /*logspin(end, spin, 'w');*/ 194 } 195 196 static __inline int 197 _spin_pool_hash(void *ptr) 198 { 199 int i; 200 201 i = ((int)(uintptr_t) ptr >> 5) ^ ((int)(uintptr_t)ptr >> 12); 202 i &= SPINLOCK_NUM_POOL_MASK; 203 return (i); 204 } 205 206 void 207 _spin_pool_lock(void *chan) 208 { 209 struct spinlock *sp; 210 211 sp = &pool_spinlocks[_spin_pool_hash(chan)].spin; 212 spin_lock(sp); 213 } 214 215 void 216 _spin_pool_unlock(void *chan) 217 { 218 struct spinlock *sp; 219 220 sp = &pool_spinlocks[_spin_pool_hash(chan)].spin; 221 spin_unlock(sp); 222 } 223 224 225 static 226 int 227 spin_indefinite_check(struct spinlock *spin, struct indefinite_info *info) 228 { 229 sysclock_t count; 230 231 cpu_spinlock_contested(); 232 233 count = sys_cputimer->count(); 234 if (info->secs == 0) { 235 info->base = count; 236 ++info->secs; 237 } else if (count - info->base > sys_cputimer->freq) { 238 kprintf("spin_lock: %p, indefinite wait (%d secs)!\n", 239 spin, info->secs); 240 info->base = count; 241 ++info->secs; 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 #if defined(INVARIANTS) 251 if (info->secs == 11) 252 print_backtrace(-1); 253 #endif 254 if (info->secs == 60) 255 panic("spin_lock: %p, indefinite wait!\n", spin); 256 } 257 return (FALSE); 258 } 259 260 /* 261 * If INVARIANTS is enabled various spinlock timing tests can be run 262 * by setting debug.spin_lock_test: 263 * 264 * 1 Test the indefinite wait code 265 * 2 Time the best-case exclusive lock overhead (spin_test_count) 266 * 3 Time the best-case shared lock overhead (spin_test_count) 267 */ 268 269 #ifdef INVARIANTS 270 271 static int spin_test_count = 10000000; 272 SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0, 273 "Number of iterations to use for spinlock wait code test"); 274 275 static int 276 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS) 277 { 278 struct spinlock spin; 279 int error; 280 int value = 0; 281 int i; 282 283 if ((error = priv_check(curthread, PRIV_ROOT)) != 0) 284 return (error); 285 if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0) 286 return (error); 287 288 /* 289 * Indefinite wait test 290 */ 291 if (value == 1) { 292 spin_init(&spin); 293 spin_lock(&spin); /* force an indefinite wait */ 294 spin_lock_test_mode = 1; 295 spin_lock(&spin); 296 spin_unlock(&spin); /* Clean up the spinlock count */ 297 spin_unlock(&spin); 298 spin_lock_test_mode = 0; 299 } 300 301 /* 302 * Time best-case exclusive spinlocks 303 */ 304 if (value == 2) { 305 globaldata_t gd = mycpu; 306 307 spin_init(&spin); 308 for (i = spin_test_count; i > 0; --i) { 309 spin_lock_quick(gd, &spin); 310 spin_unlock_quick(gd, &spin); 311 } 312 } 313 314 return (0); 315 } 316 317 SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT, 318 0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code"); 319 320 #endif /* INVARIANTS */ 321 #endif /* SMP */ 322