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>
58 struct indefinite_info {
66 #if !defined(KTR_SPIN_CONTENTION)
67 #define KTR_SPIN_CONTENTION KTR_ALL
69 #define SPIN_STRING "spin=%p type=%c"
70 #define SPIN_ARG_SIZE (sizeof(void *) + sizeof(int))
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);
76 #define logspin(name, spin, type) \
77 KTR_LOG(spin_ ## name, spin, type)
80 static int spin_lock_test_mode;
82 struct spinlock pmap_spin = SPINLOCK_INITIALIZER(pmap_spin);
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");
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");
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");
99 #define SPINLOCK_NUM_POOL (1024)
100 static struct spinlock pool_spinlocks[SPINLOCK_NUM_POOL];
102 static int spin_indefinite_check(struct spinlock *spin,
103 struct indefinite_info *info);
106 * We contested due to another exclusive lock holder. We lose.
108 * We have to unwind the attempt and may acquire the spinlock
109 * anyway while doing so. countb was incremented on our behalf.
112 spin_trylock_contested(struct spinlock *spin)
114 globaldata_t gd = mycpu;
116 /*++spinlocks_contested1;*/
117 --gd->gd_spinlocks_wr;
118 --gd->gd_curthread->td_critcount;
123 * The spin_lock() inline was unable to acquire the lock.
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.
130 spin_lock_contested(struct spinlock *spin)
135 while (atomic_swap_int(&spin->counta, 1)) {
137 if (i == spinlocks_hardloops) {
138 struct indefinite_info info = { 0, 0 };
140 logspin(beg, spin, 'w');
141 while (atomic_swap_int(&spin->counta, 1)) {
144 if ((++i & 0x7F) == 0x7F) {
145 if (spin_indefinite_check(spin, &info))
149 logspin(end, spin, 'w');
158 _spin_pool_hash(void *ptr)
161 i = ((int) (uintptr_t) ptr >> 2) ^ ((int) (uintptr_t) ptr >> 12);
162 i &= (SPINLOCK_NUM_POOL - 1);
167 _spin_pool_lock(void *chan)
171 sp = &pool_spinlocks[_spin_pool_hash(chan)];
176 _spin_pool_unlock(void *chan)
180 sp = &pool_spinlocks[_spin_pool_hash(chan)];
187 spin_indefinite_check(struct spinlock *spin, struct indefinite_info *info)
191 cpu_spinlock_contested();
193 count = sys_cputimer->count();
194 if (info->secs == 0) {
197 } else if (count - info->base > sys_cputimer->freq) {
198 kprintf("spin_lock: %p, indefinite wait (%d secs)!\n",
204 #if defined(INVARIANTS)
205 if (spin_lock_test_mode) {
210 #if defined(INVARIANTS)
211 if (info->secs == 11)
214 if (info->secs == 60)
215 panic("spin_lock: %p, indefinite wait!\n", spin);
221 * If INVARIANTS is enabled various spinlock timing tests can be run
222 * by setting debug.spin_lock_test:
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)
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");
236 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS)
238 struct spinlock spin;
243 if ((error = priv_check(curthread, PRIV_ROOT)) != 0)
245 if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0)
249 * Indefinite wait test
253 spin_lock(&spin); /* force an indefinite wait */
254 spin_lock_test_mode = 1;
256 spin_unlock(&spin); /* Clean up the spinlock count */
258 spin_lock_test_mode = 0;
262 * Time best-case exclusive spinlocks
265 globaldata_t gd = mycpu;
268 for (i = spin_test_count; i > 0; --i) {
269 spin_lock_quick(gd, &spin);
270 spin_unlock_quick(gd, &spin);
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");
280 #endif /* INVARIANTS */