kern_lock.c revision 1.135 1 /* $NetBSD: kern_lock.c,v 1.135 2008/03/17 08:27:50 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.135 2008/03/17 08:27:50 yamt Exp $");
42
43 #include "opt_multiprocessor.h"
44
45 #include <sys/param.h>
46 #include <sys/proc.h>
47 #include <sys/lock.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/lockdebug.h>
51 #include <sys/cpu.h>
52 #include <sys/syslog.h>
53 #include <sys/atomic.h>
54
55 #include <machine/stdarg.h>
56 #include <machine/lock.h>
57
58 #include <dev/lockstat.h>
59
60 #define RETURN_ADDRESS (uintptr_t)__builtin_return_address(0)
61
62 bool kernel_lock_dodebug;
63
64 __cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
65 __aligned(CACHE_LINE_SIZE);
66
67 #if defined(DEBUG) || defined(LKM)
68 void
69 assert_sleepable(void)
70 {
71 #if !defined(_RUMPKERNEL)
72 const char *reason;
73
74 if (panicstr != NULL) {
75 return;
76 }
77
78 LOCKDEBUG_BARRIER(kernel_lock, 1);
79
80 reason = NULL;
81 if (CURCPU_IDLE_P() && !cold) {
82 reason = "idle";
83 }
84 if (cpu_intr_p()) {
85 reason = "interrupt";
86 }
87 if ((curlwp->l_pflag & LP_INTR) != 0) {
88 reason = "softint";
89 }
90
91 if (reason) {
92 panic("%s: %s caller=%p", __func__, reason,
93 (void *)RETURN_ADDRESS);
94 }
95 #endif /* !defined(_RUMPKERNEL) */
96 }
97 #endif /* defined(DEBUG) || defined(LKM) */
98
99 /*
100 * rump doesn't need the kernel lock so force it out. We cannot
101 * currently easily include it for compilation because of
102 * a) SPINLOCK_* b) membar_producer(). They are defined in different
103 * places / way for each arch, so just simply do not bother to
104 * fight a lot for no gain (i.e. pain but still no gain).
105 */
106 #ifndef _RUMPKERNEL
107 /*
108 * Functions for manipulating the kernel_lock. We put them here
109 * so that they show up in profiles.
110 */
111
112 #define _KERNEL_LOCK_ABORT(msg) \
113 LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
114
115 #ifdef LOCKDEBUG
116 #define _KERNEL_LOCK_ASSERT(cond) \
117 do { \
118 if (!(cond)) \
119 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
120 } while (/* CONSTCOND */ 0)
121 #else
122 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
123 #endif
124
125 void _kernel_lock_dump(volatile void *);
126
127 lockops_t _kernel_lock_ops = {
128 "Kernel lock",
129 0,
130 _kernel_lock_dump
131 };
132
133 /*
134 * Initialize the kernel lock.
135 */
136 void
137 kernel_lock_init(void)
138 {
139
140 KASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
141 __cpu_simple_lock_init(kernel_lock);
142 kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
143 RETURN_ADDRESS);
144 }
145
146 /*
147 * Print debugging information about the kernel lock.
148 */
149 void
150 _kernel_lock_dump(volatile void *junk)
151 {
152 struct cpu_info *ci = curcpu();
153
154 (void)junk;
155
156 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
157 ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
158 }
159
160 /*
161 * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
162 * acquisition is from process context.
163 */
164 void
165 _kernel_lock(int nlocks, struct lwp *l)
166 {
167 struct cpu_info *ci = curcpu();
168 LOCKSTAT_TIMER(spintime);
169 LOCKSTAT_FLAG(lsflag);
170 struct lwp *owant;
171 u_int spins;
172 int s;
173
174 if (nlocks == 0)
175 return;
176 _KERNEL_LOCK_ASSERT(nlocks > 0);
177
178 l = curlwp;
179
180 if (ci->ci_biglock_count != 0) {
181 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
182 ci->ci_biglock_count += nlocks;
183 l->l_blcnt += nlocks;
184 return;
185 }
186
187 _KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
188 LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
189 0);
190
191 s = splvm();
192 if (__cpu_simple_lock_try(kernel_lock)) {
193 ci->ci_biglock_count = nlocks;
194 l->l_blcnt = nlocks;
195 LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock,
196 RETURN_ADDRESS, 0);
197 splx(s);
198 return;
199 }
200
201 /*
202 * To remove the ordering constraint between adaptive mutexes
203 * and kernel_lock we must make it appear as if this thread is
204 * blocking. For non-interlocked mutex release, a store fence
205 * is required to ensure that the result of any mutex_exit()
206 * by the current LWP becomes visible on the bus before the set
207 * of ci->ci_biglock_wanted becomes visible.
208 */
209 membar_producer();
210 owant = ci->ci_biglock_wanted;
211 ci->ci_biglock_wanted = l;
212
213 /*
214 * Spin until we acquire the lock. Once we have it, record the
215 * time spent with lockstat.
216 */
217 LOCKSTAT_ENTER(lsflag);
218 LOCKSTAT_START_TIMER(lsflag, spintime);
219
220 spins = 0;
221 do {
222 splx(s);
223 while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
224 if (SPINLOCK_SPINOUT(spins)) {
225 _KERNEL_LOCK_ABORT("spinout");
226 }
227 SPINLOCK_BACKOFF_HOOK;
228 SPINLOCK_SPIN_HOOK;
229 }
230 s = splvm();
231 } while (!__cpu_simple_lock_try(kernel_lock));
232
233 ci->ci_biglock_count = nlocks;
234 l->l_blcnt = nlocks;
235 LOCKSTAT_STOP_TIMER(lsflag, spintime);
236 LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS, 0);
237 if (owant == NULL) {
238 LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
239 LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
240 }
241 LOCKSTAT_EXIT(lsflag);
242 splx(s);
243
244 /*
245 * Now that we have kernel_lock, reset ci_biglock_wanted. This
246 * store must be unbuffered (immediately visible on the bus) in
247 * order for non-interlocked mutex release to work correctly.
248 * It must be visible before a mutex_exit() can execute on this
249 * processor.
250 *
251 * Note: only where CAS is available in hardware will this be
252 * an unbuffered write, but non-interlocked release cannot be
253 * done on CPUs without CAS in hardware.
254 */
255 (void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
256
257 /*
258 * Issue a memory barrier as we have acquired a lock. This also
259 * prevents stores from a following mutex_exit() being reordered
260 * to occur before our store to ci_biglock_wanted above.
261 */
262 membar_enter();
263 }
264
265 /*
266 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
267 * all holds. If 'l' is non-null, the release is from process context.
268 */
269 void
270 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
271 {
272 struct cpu_info *ci = curcpu();
273 u_int olocks;
274 int s;
275
276 l = curlwp;
277
278 _KERNEL_LOCK_ASSERT(nlocks < 2);
279
280 olocks = l->l_blcnt;
281
282 if (olocks == 0) {
283 _KERNEL_LOCK_ASSERT(nlocks <= 0);
284 if (countp != NULL)
285 *countp = 0;
286 return;
287 }
288
289 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
290
291 if (nlocks == 0)
292 nlocks = olocks;
293 else if (nlocks == -1) {
294 nlocks = 1;
295 _KERNEL_LOCK_ASSERT(olocks == 1);
296 }
297
298 _KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
299
300 l->l_blcnt -= nlocks;
301 if (ci->ci_biglock_count == nlocks) {
302 s = splvm();
303 LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
304 RETURN_ADDRESS, 0);
305 ci->ci_biglock_count = 0;
306 __cpu_simple_unlock(kernel_lock);
307 splx(s);
308 } else
309 ci->ci_biglock_count -= nlocks;
310
311 if (countp != NULL)
312 *countp = olocks;
313 }
314 #endif /* !_RUMPKERNEL */
315