2 * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey M. Hsu. and Matthew Dillon
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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).
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>
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>
56 struct spinlock pmap_spin = SPINLOCK_INITIALIZER(pmap_spin);
60 struct indefinite_info {
68 #if !defined(KTR_SPIN_CONTENTION)
69 #define KTR_SPIN_CONTENTION KTR_ALL
71 #define SPIN_STRING "spin=%p type=%c"
72 #define SPIN_ARG_SIZE (sizeof(void *) + sizeof(int))
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);
78 #define logspin(name, spin, type) \
79 KTR_LOG(spin_ ## name, spin, type)
82 static int spin_lock_test_mode;
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");
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");
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");
100 #define SPINLOCK_NUM_POOL (1024)
101 static struct spinlock pool_spinlocks[SPINLOCK_NUM_POOL];
103 static int spin_indefinite_check(struct spinlock *spin,
104 struct indefinite_info *info);
107 * We contested due to another exclusive lock holder. We lose.
109 * We have to unwind the attempt and may acquire the spinlock
110 * anyway while doing so. countb was incremented on our behalf.
113 spin_trylock_contested(struct spinlock *spin)
115 globaldata_t gd = mycpu;
117 /*++spinlocks_contested1;*/
118 --gd->gd_spinlocks_wr;
119 --gd->gd_curthread->td_critcount;
124 * The spin_lock() inline was unable to acquire the lock.
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.
131 spin_lock_contested(struct spinlock *spin)
136 while (atomic_swap_int(&spin->counta, 1)) {
138 if (i == spinlocks_hardloops) {
139 struct indefinite_info info = { 0, 0 };
141 logspin(beg, spin, 'w');
142 while (atomic_swap_int(&spin->counta, 1)) {
145 if ((++i & 0x7F) == 0x7F) {
146 if (spin_indefinite_check(spin, &info))
150 logspin(end, spin, 'w');
159 _spin_pool_hash(void *ptr)
162 i = ((int) (uintptr_t) ptr >> 2) ^ ((int) (uintptr_t) ptr >> 12);
163 i &= (SPINLOCK_NUM_POOL - 1);
168 _spin_pool_lock(void *chan)
172 sp = &pool_spinlocks[_spin_pool_hash(chan)];
177 _spin_pool_unlock(void *chan)
181 sp = &pool_spinlocks[_spin_pool_hash(chan)];
188 spin_indefinite_check(struct spinlock *spin, struct indefinite_info *info)
192 cpu_spinlock_contested();
194 count = sys_cputimer->count();
195 if (info->secs == 0) {
198 } else if (count - info->base > sys_cputimer->freq) {
199 kprintf("spin_lock: %p, indefinite wait (%d secs)!\n",
205 #if defined(INVARIANTS)
206 if (spin_lock_test_mode) {
211 #if defined(INVARIANTS)
212 if (info->secs == 11)
215 if (info->secs == 60)
216 panic("spin_lock: %p, indefinite wait!\n", spin);
222 * If INVARIANTS is enabled various spinlock timing tests can be run
223 * by setting debug.spin_lock_test:
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)
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");
237 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS)
239 struct spinlock spin;
244 if ((error = priv_check(curthread, PRIV_ROOT)) != 0)
246 if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0)
250 * Indefinite wait test
254 spin_lock(&spin); /* force an indefinite wait */
255 spin_lock_test_mode = 1;
257 spin_unlock(&spin); /* Clean up the spinlock count */
259 spin_lock_test_mode = 0;
263 * Time best-case exclusive spinlocks
266 globaldata_t gd = mycpu;
269 for (i = spin_test_count; i > 0; --i) {
270 spin_lock_quick(gd, &spin);
271 spin_unlock_quick(gd, &spin);
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");
281 #endif /* INVARIANTS */