1 /* $NetBSD: kern_lock.c,v 1.149 2009/07/17 22:17:37 dyoung Exp $ */
4 * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.149 2009/07/17 22:17:37 dyoung Exp $");
36 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/lockdebug.h>
43 #include <sys/syslog.h>
44 #include <sys/atomic.h>
47 #include <machine/stdarg.h>
48 #include <machine/lock.h>
50 #include <dev/lockstat.h>
52 #define RETURN_ADDRESS (uintptr_t)__builtin_return_address(0)
54 bool kernel_lock_dodebug
;
56 __cpu_simple_lock_t kernel_lock
[CACHE_LINE_SIZE
/ sizeof(__cpu_simple_lock_t
)]
57 __aligned(CACHE_LINE_SIZE
);
60 assert_sleepable(void)
66 if (panicstr
!= NULL
) {
70 LOCKDEBUG_BARRIER(kernel_lock
, 1);
73 * Avoid disabling/re-enabling preemption here since this
74 * routine may be called in delicate situations.
78 idle
= CURCPU_IDLE_P();
79 } while (pctr
!= lwp_pctr());
88 if (cpu_softintr_p()) {
93 panic("%s: %s caller=%p", __func__
, reason
,
94 (void *)RETURN_ADDRESS
);
99 * Functions for manipulating the kernel_lock. We put them here
100 * so that they show up in profiles.
103 #define _KERNEL_LOCK_ABORT(msg) \
104 LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
107 #define _KERNEL_LOCK_ASSERT(cond) \
110 _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
111 } while (/* CONSTCOND */ 0)
113 #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
116 void _kernel_lock_dump(volatile void *);
118 lockops_t _kernel_lock_ops
= {
125 * Initialize the kernel lock.
128 kernel_lock_init(void)
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
,
138 * Print debugging information about the kernel lock.
141 _kernel_lock_dump(volatile void *junk
)
143 struct cpu_info
*ci
= curcpu();
147 printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
148 ci
->ci_biglock_count
, (long)ci
->ci_biglock_wanted
);
152 * Acquire 'nlocks' holds on the kernel lock.
155 _kernel_lock(int nlocks
)
158 LOCKSTAT_TIMER(spintime
);
159 LOCKSTAT_FLAG(lsflag
);
163 struct lwp
*l
= curlwp
;
165 _KERNEL_LOCK_ASSERT(nlocks
> 0);
169 if (ci
->ci_biglock_count
!= 0) {
170 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock
));
171 ci
->ci_biglock_count
+= nlocks
;
172 l
->l_blcnt
+= nlocks
;
177 _KERNEL_LOCK_ASSERT(l
->l_blcnt
== 0);
178 LOCKDEBUG_WANTLOCK(kernel_lock_dodebug
, kernel_lock
, RETURN_ADDRESS
,
181 if (__cpu_simple_lock_try(kernel_lock
)) {
182 ci
->ci_biglock_count
= nlocks
;
184 LOCKDEBUG_LOCKED(kernel_lock_dodebug
, kernel_lock
, NULL
,
191 * To remove the ordering constraint between adaptive mutexes
192 * and kernel_lock we must make it appear as if this thread is
193 * blocking. For non-interlocked mutex release, a store fence
194 * is required to ensure that the result of any mutex_exit()
195 * by the current LWP becomes visible on the bus before the set
196 * of ci->ci_biglock_wanted becomes visible.
199 owant
= ci
->ci_biglock_wanted
;
200 ci
->ci_biglock_wanted
= l
;
203 * Spin until we acquire the lock. Once we have it, record the
204 * time spent with lockstat.
206 LOCKSTAT_ENTER(lsflag
);
207 LOCKSTAT_START_TIMER(lsflag
, spintime
);
212 while (__SIMPLELOCK_LOCKED_P(kernel_lock
)) {
213 if (SPINLOCK_SPINOUT(spins
)) {
214 extern int start_init_exec
;
215 if (!start_init_exec
)
216 _KERNEL_LOCK_ABORT("spinout");
218 SPINLOCK_BACKOFF_HOOK
;
222 } while (!__cpu_simple_lock_try(kernel_lock
));
224 ci
->ci_biglock_count
= nlocks
;
226 LOCKSTAT_STOP_TIMER(lsflag
, spintime
);
227 LOCKDEBUG_LOCKED(kernel_lock_dodebug
, kernel_lock
, NULL
,
230 LOCKSTAT_EVENT_RA(lsflag
, kernel_lock
,
231 LB_KERNEL_LOCK
| LB_SPIN
, 1, spintime
, RETURN_ADDRESS
);
233 LOCKSTAT_EXIT(lsflag
);
237 * Now that we have kernel_lock, reset ci_biglock_wanted. This
238 * store must be unbuffered (immediately visible on the bus) in
239 * order for non-interlocked mutex release to work correctly.
240 * It must be visible before a mutex_exit() can execute on this
243 * Note: only where CAS is available in hardware will this be
244 * an unbuffered write, but non-interlocked release cannot be
245 * done on CPUs without CAS in hardware.
247 (void)atomic_swap_ptr(&ci
->ci_biglock_wanted
, owant
);
250 * Issue a memory barrier as we have acquired a lock. This also
251 * prevents stores from a following mutex_exit() being reordered
252 * to occur before our store to ci_biglock_wanted above.
258 * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
262 _kernel_unlock(int nlocks
, int *countp
)
267 struct lwp
*l
= curlwp
;
269 _KERNEL_LOCK_ASSERT(nlocks
< 2);
274 _KERNEL_LOCK_ASSERT(nlocks
<= 0);
280 _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock
));
284 else if (nlocks
== -1) {
286 _KERNEL_LOCK_ASSERT(olocks
== 1);
290 _KERNEL_LOCK_ASSERT(ci
->ci_biglock_count
>= l
->l_blcnt
);
291 if (ci
->ci_biglock_count
== nlocks
) {
292 LOCKDEBUG_UNLOCKED(kernel_lock_dodebug
, kernel_lock
,
294 ci
->ci_biglock_count
= 0;
295 __cpu_simple_unlock(kernel_lock
);
296 l
->l_blcnt
-= nlocks
;
301 ci
->ci_biglock_count
-= nlocks
;
302 l
->l_blcnt
-= nlocks
;