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