xref: /netbsd-src/sys/kern/kern_lock.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: kern_lock.c,v 1.147 2008/11/12 12:36:16 ad 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  *
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.147 2008/11/12 12:36:16 ad 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 
46 #include <machine/stdarg.h>
47 #include <machine/lock.h>
48 
49 #include <dev/lockstat.h>
50 
51 #define	RETURN_ADDRESS	(uintptr_t)__builtin_return_address(0)
52 
53 bool	kernel_lock_dodebug;
54 
55 __cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
56     __aligned(CACHE_LINE_SIZE);
57 
58 void
59 assert_sleepable(void)
60 {
61 	const char *reason;
62 
63 	if (panicstr != NULL) {
64 		return;
65 	}
66 
67 	LOCKDEBUG_BARRIER(kernel_lock, 1);
68 
69 	reason = NULL;
70 	if (CURCPU_IDLE_P() && !cold) {
71 		reason = "idle";
72 	}
73 	if (cpu_intr_p()) {
74 		reason = "interrupt";
75 	}
76 	if ((curlwp->l_pflag & LP_INTR) != 0) {
77 		reason = "softint";
78 	}
79 
80 	if (reason) {
81 		panic("%s: %s caller=%p", __func__, reason,
82 		    (void *)RETURN_ADDRESS);
83 	}
84 }
85 
86 /*
87  * Functions for manipulating the kernel_lock.  We put them here
88  * so that they show up in profiles.
89  */
90 
91 #define	_KERNEL_LOCK_ABORT(msg)						\
92     LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
93 
94 #ifdef LOCKDEBUG
95 #define	_KERNEL_LOCK_ASSERT(cond)					\
96 do {									\
97 	if (!(cond))							\
98 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
99 } while (/* CONSTCOND */ 0)
100 #else
101 #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
102 #endif
103 
104 void	_kernel_lock_dump(volatile void *);
105 
106 lockops_t _kernel_lock_ops = {
107 	"Kernel lock",
108 	LOCKOPS_SPIN,
109 	_kernel_lock_dump
110 };
111 
112 /*
113  * Initialize the kernel lock.
114  */
115 void
116 kernel_lock_init(void)
117 {
118 
119 	CTASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
120 	__cpu_simple_lock_init(kernel_lock);
121 	kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
122 	    RETURN_ADDRESS);
123 }
124 
125 /*
126  * Print debugging information about the kernel lock.
127  */
128 void
129 _kernel_lock_dump(volatile void *junk)
130 {
131 	struct cpu_info *ci = curcpu();
132 
133 	(void)junk;
134 
135 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
136 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
137 }
138 
139 /*
140  * Acquire 'nlocks' holds on the kernel lock.  If 'l' is non-null, the
141  * acquisition is from process context.
142  */
143 void
144 _kernel_lock(int nlocks)
145 {
146 	struct cpu_info *ci;
147 	LOCKSTAT_TIMER(spintime);
148 	LOCKSTAT_FLAG(lsflag);
149 	struct lwp *owant;
150 	u_int spins;
151 	int s;
152 	struct lwp *l = curlwp;
153 
154 	_KERNEL_LOCK_ASSERT(nlocks > 0);
155 
156 	s = splvm();
157 	ci = curcpu();
158 	if (ci->ci_biglock_count != 0) {
159 		_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
160 		ci->ci_biglock_count += nlocks;
161 		l->l_blcnt += nlocks;
162 		splx(s);
163 		return;
164 	}
165 
166 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
167 	LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
168 	    false, false);
169 
170 	if (__cpu_simple_lock_try(kernel_lock)) {
171 		ci->ci_biglock_count = nlocks;
172 		l->l_blcnt = nlocks;
173 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
174 		    RETURN_ADDRESS, 0);
175 		splx(s);
176 		return;
177 	}
178 
179 	/*
180 	 * To remove the ordering constraint between adaptive mutexes
181 	 * and kernel_lock we must make it appear as if this thread is
182 	 * blocking.  For non-interlocked mutex release, a store fence
183 	 * is required to ensure that the result of any mutex_exit()
184 	 * by the current LWP becomes visible on the bus before the set
185 	 * of ci->ci_biglock_wanted becomes visible.
186 	 */
187 	membar_producer();
188 	owant = ci->ci_biglock_wanted;
189 	ci->ci_biglock_wanted = l;
190 
191 	/*
192 	 * Spin until we acquire the lock.  Once we have it, record the
193 	 * time spent with lockstat.
194 	 */
195 	LOCKSTAT_ENTER(lsflag);
196 	LOCKSTAT_START_TIMER(lsflag, spintime);
197 
198 	spins = 0;
199 	do {
200 		splx(s);
201 		while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
202 			if (SPINLOCK_SPINOUT(spins)) {
203 				extern int start_init_exec;
204 				if (!start_init_exec)
205 					_KERNEL_LOCK_ABORT("spinout");
206 			}
207 			SPINLOCK_BACKOFF_HOOK;
208 			SPINLOCK_SPIN_HOOK;
209 		}
210 		s = splvm();
211 	} while (!__cpu_simple_lock_try(kernel_lock));
212 
213 	ci->ci_biglock_count = nlocks;
214 	l->l_blcnt = nlocks;
215 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
216 	LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
217 	    RETURN_ADDRESS, 0);
218 	if (owant == NULL) {
219 		LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
220 		    LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
221 	}
222 	LOCKSTAT_EXIT(lsflag);
223 	splx(s);
224 
225 	/*
226 	 * Now that we have kernel_lock, reset ci_biglock_wanted.  This
227 	 * store must be unbuffered (immediately visible on the bus) in
228 	 * order for non-interlocked mutex release to work correctly.
229 	 * It must be visible before a mutex_exit() can execute on this
230 	 * processor.
231 	 *
232 	 * Note: only where CAS is available in hardware will this be
233 	 * an unbuffered write, but non-interlocked release cannot be
234 	 * done on CPUs without CAS in hardware.
235 	 */
236 	(void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
237 
238 	/*
239 	 * Issue a memory barrier as we have acquired a lock.  This also
240 	 * prevents stores from a following mutex_exit() being reordered
241 	 * to occur before our store to ci_biglock_wanted above.
242 	 */
243 	membar_enter();
244 }
245 
246 /*
247  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
248  * all holds.  If 'l' is non-null, the release is from process context.
249  */
250 void
251 _kernel_unlock(int nlocks, int *countp)
252 {
253 	struct cpu_info *ci;
254 	u_int olocks;
255 	int s;
256 	struct lwp *l = curlwp;
257 
258 	_KERNEL_LOCK_ASSERT(nlocks < 2);
259 
260 	olocks = l->l_blcnt;
261 
262 	if (olocks == 0) {
263 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
264 		if (countp != NULL)
265 			*countp = 0;
266 		return;
267 	}
268 
269 	_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
270 
271 	if (nlocks == 0)
272 		nlocks = olocks;
273 	else if (nlocks == -1) {
274 		nlocks = 1;
275 		_KERNEL_LOCK_ASSERT(olocks == 1);
276 	}
277 	s = splvm();
278 	ci = curcpu();
279 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
280 	if (ci->ci_biglock_count == nlocks) {
281 		LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
282 		    RETURN_ADDRESS, 0);
283 		ci->ci_biglock_count = 0;
284 		__cpu_simple_unlock(kernel_lock);
285 		l->l_blcnt -= nlocks;
286 		splx(s);
287 		if (l->l_dopreempt)
288 			kpreempt(0);
289 	} else {
290 		ci->ci_biglock_count -= nlocks;
291 		l->l_blcnt -= nlocks;
292 		splx(s);
293 	}
294 
295 	if (countp != NULL)
296 		*countp = olocks;
297 }
298