kern_lock.c revision 1.87 1 1.87 christos /* $NetBSD: kern_lock.c,v 1.87 2005/05/29 21:16:14 christos Exp $ */
2 1.19 thorpej
3 1.19 thorpej /*-
4 1.28 thorpej * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 1.19 thorpej * All rights reserved.
6 1.19 thorpej *
7 1.19 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.19 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.19 thorpej * NASA Ames Research Center.
10 1.19 thorpej *
11 1.19 thorpej * This code is derived from software contributed to The NetBSD Foundation
12 1.19 thorpej * by Ross Harvey.
13 1.19 thorpej *
14 1.19 thorpej * Redistribution and use in source and binary forms, with or without
15 1.19 thorpej * modification, are permitted provided that the following conditions
16 1.19 thorpej * are met:
17 1.19 thorpej * 1. Redistributions of source code must retain the above copyright
18 1.19 thorpej * notice, this list of conditions and the following disclaimer.
19 1.19 thorpej * 2. Redistributions in binary form must reproduce the above copyright
20 1.19 thorpej * notice, this list of conditions and the following disclaimer in the
21 1.19 thorpej * documentation and/or other materials provided with the distribution.
22 1.19 thorpej * 3. All advertising materials mentioning features or use of this software
23 1.19 thorpej * must display the following acknowledgement:
24 1.19 thorpej * This product includes software developed by the NetBSD
25 1.19 thorpej * Foundation, Inc. and its contributors.
26 1.19 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
27 1.19 thorpej * contributors may be used to endorse or promote products derived
28 1.19 thorpej * from this software without specific prior written permission.
29 1.19 thorpej *
30 1.19 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 1.19 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 1.19 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 1.19 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 1.19 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 1.19 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 1.19 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 1.19 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 1.19 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.19 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.19 thorpej * POSSIBILITY OF SUCH DAMAGE.
41 1.19 thorpej */
42 1.2 fvdl
43 1.86 perry /*
44 1.1 fvdl * Copyright (c) 1995
45 1.1 fvdl * The Regents of the University of California. All rights reserved.
46 1.1 fvdl *
47 1.1 fvdl * This code contains ideas from software contributed to Berkeley by
48 1.1 fvdl * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
49 1.1 fvdl * System project at Carnegie-Mellon University.
50 1.1 fvdl *
51 1.1 fvdl * Redistribution and use in source and binary forms, with or without
52 1.1 fvdl * modification, are permitted provided that the following conditions
53 1.1 fvdl * are met:
54 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
55 1.1 fvdl * notice, this list of conditions and the following disclaimer.
56 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
57 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
58 1.1 fvdl * documentation and/or other materials provided with the distribution.
59 1.72 agc * 3. Neither the name of the University nor the names of its contributors
60 1.1 fvdl * may be used to endorse or promote products derived from this software
61 1.1 fvdl * without specific prior written permission.
62 1.1 fvdl *
63 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 fvdl * SUCH DAMAGE.
74 1.1 fvdl *
75 1.1 fvdl * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
76 1.1 fvdl */
77 1.60 lukem
78 1.60 lukem #include <sys/cdefs.h>
79 1.87 christos __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.87 2005/05/29 21:16:14 christos Exp $");
80 1.7 thorpej
81 1.21 thorpej #include "opt_multiprocessor.h"
82 1.7 thorpej #include "opt_lockdebug.h"
83 1.18 chs #include "opt_ddb.h"
84 1.1 fvdl
85 1.1 fvdl #include <sys/param.h>
86 1.1 fvdl #include <sys/proc.h>
87 1.1 fvdl #include <sys/lock.h>
88 1.2 fvdl #include <sys/systm.h>
89 1.1 fvdl #include <machine/cpu.h>
90 1.1 fvdl
91 1.25 thorpej #if defined(LOCKDEBUG)
92 1.25 thorpej #include <sys/syslog.h>
93 1.25 thorpej /*
94 1.25 thorpej * note that stdarg.h and the ansi style va_start macro is used for both
95 1.25 thorpej * ansi and traditional c compiles.
96 1.25 thorpej * XXX: this requires that stdarg.h define: va_alist and va_dcl
97 1.25 thorpej */
98 1.25 thorpej #include <machine/stdarg.h>
99 1.25 thorpej
100 1.36 thorpej void lock_printf(const char *fmt, ...)
101 1.37 eeh __attribute__((__format__(__printf__,1,2)));
102 1.25 thorpej
103 1.78 hannken static int acquire(__volatile struct lock **, int *, int, int, int);
104 1.73 yamt
105 1.57 sommerfe int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
106 1.55 thorpej
107 1.55 thorpej #ifdef DDB
108 1.55 thorpej #include <ddb/ddbvar.h>
109 1.55 thorpej #include <machine/db_machdep.h>
110 1.55 thorpej #include <ddb/db_command.h>
111 1.55 thorpej #include <ddb/db_interface.h>
112 1.55 thorpej #endif
113 1.85 yamt #endif /* defined(LOCKDEBUG) */
114 1.85 yamt
115 1.85 yamt #if defined(MULTIPROCESSOR)
116 1.86 perry struct simplelock kernel_lock;
117 1.25 thorpej #endif
118 1.25 thorpej
119 1.1 fvdl /*
120 1.1 fvdl * Locking primitives implementation.
121 1.56 wiz * Locks provide shared/exclusive synchronization.
122 1.1 fvdl */
123 1.1 fvdl
124 1.21 thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
125 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
126 1.21 thorpej #define COUNT_CPU(cpu_id, x) \
127 1.47 sommerfe curcpu()->ci_spin_locks += (x)
128 1.21 thorpej #else
129 1.21 thorpej u_long spin_locks;
130 1.21 thorpej #define COUNT_CPU(cpu_id, x) spin_locks += (x)
131 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
132 1.21 thorpej
133 1.69 thorpej #define COUNT(lkp, l, cpu_id, x) \
134 1.21 thorpej do { \
135 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) \
136 1.21 thorpej COUNT_CPU((cpu_id), (x)); \
137 1.21 thorpej else \
138 1.69 thorpej (l)->l_locks += (x); \
139 1.30 thorpej } while (/*CONSTCOND*/0)
140 1.1 fvdl #else
141 1.22 mellon #define COUNT(lkp, p, cpu_id, x)
142 1.48 sommerfe #define COUNT_CPU(cpu_id, x)
143 1.21 thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
144 1.1 fvdl
145 1.52 thorpej #ifndef SPINLOCK_SPIN_HOOK /* from <machine/lock.h> */
146 1.52 thorpej #define SPINLOCK_SPIN_HOOK /* nothing */
147 1.49 thorpej #endif
148 1.49 thorpej
149 1.43 thorpej #define INTERLOCK_ACQUIRE(lkp, flags, s) \
150 1.40 thorpej do { \
151 1.43 thorpej if ((flags) & LK_SPIN) \
152 1.65 fvdl s = spllock(); \
153 1.40 thorpej simple_lock(&(lkp)->lk_interlock); \
154 1.66 perry } while (/*CONSTCOND*/ 0)
155 1.40 thorpej
156 1.43 thorpej #define INTERLOCK_RELEASE(lkp, flags, s) \
157 1.40 thorpej do { \
158 1.40 thorpej simple_unlock(&(lkp)->lk_interlock); \
159 1.52 thorpej if ((flags) & LK_SPIN) \
160 1.40 thorpej splx(s); \
161 1.66 perry } while (/*CONSTCOND*/ 0)
162 1.40 thorpej
163 1.63 chs #ifdef DDB /* { */
164 1.63 chs #ifdef MULTIPROCESSOR
165 1.63 chs int simple_lock_debugger = 1; /* more serious on MP */
166 1.63 chs #else
167 1.63 chs int simple_lock_debugger = 0;
168 1.63 chs #endif
169 1.63 chs #define SLOCK_DEBUGGER() if (simple_lock_debugger) Debugger()
170 1.63 chs #define SLOCK_TRACE() \
171 1.63 chs db_stack_trace_print((db_expr_t)__builtin_frame_address(0), \
172 1.71 pk TRUE, 65535, "", lock_printf);
173 1.63 chs #else
174 1.63 chs #define SLOCK_DEBUGGER() /* nothing */
175 1.63 chs #define SLOCK_TRACE() /* nothing */
176 1.63 chs #endif /* } */
177 1.63 chs
178 1.50 thorpej #if defined(LOCKDEBUG)
179 1.50 thorpej #if defined(DDB)
180 1.50 thorpej #define SPINLOCK_SPINCHECK_DEBUGGER Debugger()
181 1.50 thorpej #else
182 1.50 thorpej #define SPINLOCK_SPINCHECK_DEBUGGER /* nothing */
183 1.50 thorpej #endif
184 1.50 thorpej
185 1.50 thorpej #define SPINLOCK_SPINCHECK_DECL \
186 1.50 thorpej /* 32-bits of count -- wrap constitutes a "spinout" */ \
187 1.50 thorpej uint32_t __spinc = 0
188 1.50 thorpej
189 1.50 thorpej #define SPINLOCK_SPINCHECK \
190 1.50 thorpej do { \
191 1.50 thorpej if (++__spinc == 0) { \
192 1.71 pk lock_printf("LK_SPIN spinout, excl %d, share %d\n", \
193 1.50 thorpej lkp->lk_exclusivecount, lkp->lk_sharecount); \
194 1.50 thorpej if (lkp->lk_exclusivecount) \
195 1.71 pk lock_printf("held by CPU %lu\n", \
196 1.50 thorpej (u_long) lkp->lk_cpu); \
197 1.50 thorpej if (lkp->lk_lock_file) \
198 1.71 pk lock_printf("last locked at %s:%d\n", \
199 1.50 thorpej lkp->lk_lock_file, lkp->lk_lock_line); \
200 1.50 thorpej if (lkp->lk_unlock_file) \
201 1.71 pk lock_printf("last unlocked at %s:%d\n", \
202 1.50 thorpej lkp->lk_unlock_file, lkp->lk_unlock_line); \
203 1.63 chs SLOCK_TRACE(); \
204 1.50 thorpej SPINLOCK_SPINCHECK_DEBUGGER; \
205 1.50 thorpej } \
206 1.66 perry } while (/*CONSTCOND*/ 0)
207 1.50 thorpej #else
208 1.50 thorpej #define SPINLOCK_SPINCHECK_DECL /* nothing */
209 1.50 thorpej #define SPINLOCK_SPINCHECK /* nothing */
210 1.50 thorpej #endif /* LOCKDEBUG && DDB */
211 1.50 thorpej
212 1.1 fvdl /*
213 1.1 fvdl * Acquire a resource.
214 1.1 fvdl */
215 1.73 yamt static int
216 1.78 hannken acquire(__volatile struct lock **lkpp, int *s, int extflags,
217 1.74 hannken int drain, int wanted)
218 1.73 yamt {
219 1.73 yamt int error;
220 1.78 hannken __volatile struct lock *lkp = *lkpp;
221 1.73 yamt
222 1.73 yamt KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
223 1.73 yamt
224 1.73 yamt if (extflags & LK_SPIN) {
225 1.73 yamt int interlocked;
226 1.73 yamt
227 1.73 yamt SPINLOCK_SPINCHECK_DECL;
228 1.73 yamt
229 1.73 yamt if (!drain) {
230 1.73 yamt lkp->lk_waitcount++;
231 1.73 yamt lkp->lk_flags |= LK_WAIT_NONZERO;
232 1.73 yamt }
233 1.73 yamt for (interlocked = 1;;) {
234 1.73 yamt SPINLOCK_SPINCHECK;
235 1.73 yamt if ((lkp->lk_flags & wanted) != 0) {
236 1.73 yamt if (interlocked) {
237 1.74 hannken INTERLOCK_RELEASE(lkp, LK_SPIN, *s);
238 1.73 yamt interlocked = 0;
239 1.73 yamt }
240 1.73 yamt SPINLOCK_SPIN_HOOK;
241 1.73 yamt } else if (interlocked) {
242 1.73 yamt break;
243 1.73 yamt } else {
244 1.74 hannken INTERLOCK_ACQUIRE(lkp, LK_SPIN, *s);
245 1.73 yamt interlocked = 1;
246 1.73 yamt }
247 1.73 yamt }
248 1.73 yamt if (!drain) {
249 1.73 yamt lkp->lk_waitcount--;
250 1.73 yamt if (lkp->lk_waitcount == 0)
251 1.73 yamt lkp->lk_flags &= ~LK_WAIT_NONZERO;
252 1.73 yamt }
253 1.73 yamt KASSERT((lkp->lk_flags & wanted) == 0);
254 1.73 yamt error = 0; /* sanity */
255 1.73 yamt } else {
256 1.73 yamt for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
257 1.73 yamt if (drain)
258 1.73 yamt lkp->lk_flags |= LK_WAITDRAIN;
259 1.73 yamt else {
260 1.73 yamt lkp->lk_waitcount++;
261 1.73 yamt lkp->lk_flags |= LK_WAIT_NONZERO;
262 1.73 yamt }
263 1.73 yamt /* XXX Cast away volatile. */
264 1.73 yamt error = ltsleep(drain ?
265 1.87 christos (volatile const void *)&lkp->lk_flags :
266 1.87 christos (volatile const void *)lkp, lkp->lk_prio,
267 1.73 yamt lkp->lk_wmesg, lkp->lk_timo, &lkp->lk_interlock);
268 1.73 yamt if (!drain) {
269 1.73 yamt lkp->lk_waitcount--;
270 1.73 yamt if (lkp->lk_waitcount == 0)
271 1.73 yamt lkp->lk_flags &= ~LK_WAIT_NONZERO;
272 1.73 yamt }
273 1.73 yamt if (error)
274 1.73 yamt break;
275 1.73 yamt if (extflags & LK_SLEEPFAIL) {
276 1.73 yamt error = ENOLCK;
277 1.73 yamt break;
278 1.73 yamt }
279 1.78 hannken if (lkp->lk_newlock != NULL) {
280 1.78 hannken simple_lock(&lkp->lk_newlock->lk_interlock);
281 1.78 hannken simple_unlock(&lkp->lk_interlock);
282 1.78 hannken if (lkp->lk_waitcount == 0)
283 1.87 christos wakeup(&lkp->lk_newlock);
284 1.78 hannken *lkpp = lkp = lkp->lk_newlock;
285 1.78 hannken }
286 1.73 yamt }
287 1.1 fvdl }
288 1.1 fvdl
289 1.73 yamt return error;
290 1.73 yamt }
291 1.73 yamt
292 1.69 thorpej #define SETHOLDER(lkp, pid, lid, cpu_id) \
293 1.19 thorpej do { \
294 1.19 thorpej if ((lkp)->lk_flags & LK_SPIN) \
295 1.19 thorpej (lkp)->lk_cpu = cpu_id; \
296 1.69 thorpej else { \
297 1.19 thorpej (lkp)->lk_lockholder = pid; \
298 1.69 thorpej (lkp)->lk_locklwp = lid; \
299 1.69 thorpej } \
300 1.30 thorpej } while (/*CONSTCOND*/0)
301 1.19 thorpej
302 1.69 thorpej #define WEHOLDIT(lkp, pid, lid, cpu_id) \
303 1.19 thorpej (((lkp)->lk_flags & LK_SPIN) != 0 ? \
304 1.69 thorpej ((lkp)->lk_cpu == (cpu_id)) : \
305 1.69 thorpej ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid)))
306 1.19 thorpej
307 1.23 thorpej #define WAKEUP_WAITER(lkp) \
308 1.23 thorpej do { \
309 1.73 yamt if (((lkp)->lk_flags & (LK_SPIN | LK_WAIT_NONZERO)) == \
310 1.73 yamt LK_WAIT_NONZERO) { \
311 1.87 christos wakeup((lkp)); \
312 1.23 thorpej } \
313 1.30 thorpej } while (/*CONSTCOND*/0)
314 1.23 thorpej
315 1.21 thorpej #if defined(LOCKDEBUG) /* { */
316 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
317 1.21 thorpej struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
318 1.21 thorpej
319 1.27 thorpej #define SPINLOCK_LIST_LOCK() \
320 1.29 sommerfe __cpu_simple_lock(&spinlock_list_slock.lock_data)
321 1.21 thorpej
322 1.27 thorpej #define SPINLOCK_LIST_UNLOCK() \
323 1.29 sommerfe __cpu_simple_unlock(&spinlock_list_slock.lock_data)
324 1.21 thorpej #else
325 1.21 thorpej #define SPINLOCK_LIST_LOCK() /* nothing */
326 1.21 thorpej
327 1.21 thorpej #define SPINLOCK_LIST_UNLOCK() /* nothing */
328 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
329 1.21 thorpej
330 1.87 christos _TAILQ_HEAD(, struct lock, __volatile) spinlock_list =
331 1.21 thorpej TAILQ_HEAD_INITIALIZER(spinlock_list);
332 1.21 thorpej
333 1.21 thorpej #define HAVEIT(lkp) \
334 1.21 thorpej do { \
335 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) { \
336 1.87 christos int sp = spllock(); \
337 1.21 thorpej SPINLOCK_LIST_LOCK(); \
338 1.87 christos TAILQ_INSERT_TAIL(&spinlock_list, (lkp), lk_list); \
339 1.21 thorpej SPINLOCK_LIST_UNLOCK(); \
340 1.87 christos splx(sp); \
341 1.21 thorpej } \
342 1.30 thorpej } while (/*CONSTCOND*/0)
343 1.21 thorpej
344 1.21 thorpej #define DONTHAVEIT(lkp) \
345 1.21 thorpej do { \
346 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) { \
347 1.87 christos int sp = spllock(); \
348 1.21 thorpej SPINLOCK_LIST_LOCK(); \
349 1.87 christos TAILQ_REMOVE(&spinlock_list, (lkp), lk_list); \
350 1.21 thorpej SPINLOCK_LIST_UNLOCK(); \
351 1.87 christos splx(sp); \
352 1.21 thorpej } \
353 1.30 thorpej } while (/*CONSTCOND*/0)
354 1.21 thorpej #else
355 1.21 thorpej #define HAVEIT(lkp) /* nothing */
356 1.21 thorpej
357 1.21 thorpej #define DONTHAVEIT(lkp) /* nothing */
358 1.21 thorpej #endif /* LOCKDEBUG */ /* } */
359 1.21 thorpej
360 1.25 thorpej #if defined(LOCKDEBUG)
361 1.25 thorpej /*
362 1.25 thorpej * Lock debug printing routine; can be configured to print to console
363 1.25 thorpej * or log to syslog.
364 1.25 thorpej */
365 1.25 thorpej void
366 1.25 thorpej lock_printf(const char *fmt, ...)
367 1.25 thorpej {
368 1.68 pk char b[150];
369 1.25 thorpej va_list ap;
370 1.25 thorpej
371 1.25 thorpej va_start(ap, fmt);
372 1.25 thorpej if (lock_debug_syslog)
373 1.25 thorpej vlog(LOG_DEBUG, fmt, ap);
374 1.68 pk else {
375 1.68 pk vsnprintf(b, sizeof(b), fmt, ap);
376 1.68 pk printf_nolog("%s", b);
377 1.68 pk }
378 1.25 thorpej va_end(ap);
379 1.25 thorpej }
380 1.25 thorpej #endif /* LOCKDEBUG */
381 1.25 thorpej
382 1.1 fvdl /*
383 1.78 hannken * Transfer any waiting processes from one lock to another.
384 1.78 hannken */
385 1.78 hannken void
386 1.78 hannken transferlockers(struct lock *from, struct lock *to)
387 1.78 hannken {
388 1.78 hannken
389 1.78 hannken KASSERT(from != to);
390 1.78 hannken KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
391 1.78 hannken if (from->lk_waitcount == 0)
392 1.78 hannken return;
393 1.78 hannken from->lk_newlock = to;
394 1.78 hannken wakeup((void *)from);
395 1.78 hannken tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
396 1.78 hannken from->lk_newlock = NULL;
397 1.78 hannken from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
398 1.78 hannken KASSERT(from->lk_waitcount == 0);
399 1.78 hannken }
400 1.78 hannken
401 1.78 hannken
402 1.78 hannken /*
403 1.1 fvdl * Initialize a lock; required before use.
404 1.1 fvdl */
405 1.1 fvdl void
406 1.33 thorpej lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
407 1.1 fvdl {
408 1.1 fvdl
409 1.8 perry memset(lkp, 0, sizeof(struct lock));
410 1.1 fvdl simple_lock_init(&lkp->lk_interlock);
411 1.1 fvdl lkp->lk_flags = flags & LK_EXTFLG_MASK;
412 1.19 thorpej if (flags & LK_SPIN)
413 1.19 thorpej lkp->lk_cpu = LK_NOCPU;
414 1.19 thorpej else {
415 1.19 thorpej lkp->lk_lockholder = LK_NOPROC;
416 1.78 hannken lkp->lk_newlock = NULL;
417 1.19 thorpej lkp->lk_prio = prio;
418 1.19 thorpej lkp->lk_timo = timo;
419 1.19 thorpej }
420 1.19 thorpej lkp->lk_wmesg = wmesg; /* just a name for spin locks */
421 1.50 thorpej #if defined(LOCKDEBUG)
422 1.50 thorpej lkp->lk_lock_file = NULL;
423 1.50 thorpej lkp->lk_unlock_file = NULL;
424 1.50 thorpej #endif
425 1.1 fvdl }
426 1.1 fvdl
427 1.1 fvdl /*
428 1.1 fvdl * Determine the status of a lock.
429 1.1 fvdl */
430 1.1 fvdl int
431 1.33 thorpej lockstatus(struct lock *lkp)
432 1.1 fvdl {
433 1.76 yamt int s = 0; /* XXX: gcc */
434 1.76 yamt int lock_type = 0;
435 1.76 yamt struct lwp *l = curlwp; /* XXX */
436 1.76 yamt pid_t pid;
437 1.76 yamt lwpid_t lid;
438 1.76 yamt cpuid_t cpu_id;
439 1.76 yamt
440 1.76 yamt if ((lkp->lk_flags & LK_SPIN) || l == NULL) {
441 1.76 yamt cpu_id = cpu_number();
442 1.76 yamt pid = LK_KERNPROC;
443 1.76 yamt lid = 0;
444 1.76 yamt } else {
445 1.76 yamt cpu_id = LK_NOCPU;
446 1.76 yamt pid = l->l_proc->p_pid;
447 1.76 yamt lid = l->l_lid;
448 1.76 yamt }
449 1.1 fvdl
450 1.43 thorpej INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
451 1.76 yamt if (lkp->lk_exclusivecount != 0) {
452 1.76 yamt if (WEHOLDIT(lkp, pid, lid, cpu_id))
453 1.76 yamt lock_type = LK_EXCLUSIVE;
454 1.76 yamt else
455 1.76 yamt lock_type = LK_EXCLOTHER;
456 1.76 yamt } else if (lkp->lk_sharecount != 0)
457 1.1 fvdl lock_type = LK_SHARED;
458 1.43 thorpej INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
459 1.1 fvdl return (lock_type);
460 1.1 fvdl }
461 1.35 thorpej
462 1.35 thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
463 1.35 thorpej /*
464 1.35 thorpej * Make sure no spin locks are held by a CPU that is about
465 1.35 thorpej * to context switch.
466 1.35 thorpej */
467 1.35 thorpej void
468 1.35 thorpej spinlock_switchcheck(void)
469 1.35 thorpej {
470 1.35 thorpej u_long cnt;
471 1.35 thorpej int s;
472 1.35 thorpej
473 1.44 thorpej s = spllock();
474 1.35 thorpej #if defined(MULTIPROCESSOR)
475 1.35 thorpej cnt = curcpu()->ci_spin_locks;
476 1.35 thorpej #else
477 1.35 thorpej cnt = spin_locks;
478 1.35 thorpej #endif
479 1.35 thorpej splx(s);
480 1.35 thorpej
481 1.35 thorpej if (cnt != 0)
482 1.35 thorpej panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
483 1.35 thorpej (u_long) cpu_number(), cnt);
484 1.35 thorpej }
485 1.35 thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */
486 1.1 fvdl
487 1.1 fvdl /*
488 1.44 thorpej * Locks and IPLs (interrupt priority levels):
489 1.44 thorpej *
490 1.44 thorpej * Locks which may be taken from interrupt context must be handled
491 1.44 thorpej * very carefully; you must spl to the highest IPL where the lock
492 1.44 thorpej * is needed before acquiring the lock.
493 1.44 thorpej *
494 1.44 thorpej * It is also important to avoid deadlock, since certain (very high
495 1.44 thorpej * priority) interrupts are often needed to keep the system as a whole
496 1.44 thorpej * from deadlocking, and must not be blocked while you are spinning
497 1.44 thorpej * waiting for a lower-priority lock.
498 1.44 thorpej *
499 1.44 thorpej * In addition, the lock-debugging hooks themselves need to use locks!
500 1.44 thorpej *
501 1.44 thorpej * A raw __cpu_simple_lock may be used from interrupts are long as it
502 1.44 thorpej * is acquired and held at a single IPL.
503 1.44 thorpej *
504 1.44 thorpej * A simple_lock (which is a __cpu_simple_lock wrapped with some
505 1.44 thorpej * debugging hooks) may be used at or below spllock(), which is
506 1.44 thorpej * typically at or just below splhigh() (i.e. blocks everything
507 1.44 thorpej * but certain machine-dependent extremely high priority interrupts).
508 1.44 thorpej *
509 1.44 thorpej * spinlockmgr spinlocks should be used at or below splsched().
510 1.44 thorpej *
511 1.44 thorpej * Some platforms may have interrupts of higher priority than splsched(),
512 1.44 thorpej * including hard serial interrupts, inter-processor interrupts, and
513 1.44 thorpej * kernel debugger traps.
514 1.44 thorpej */
515 1.44 thorpej
516 1.44 thorpej /*
517 1.32 sommerfe * XXX XXX kludge around another kludge..
518 1.32 sommerfe *
519 1.32 sommerfe * vfs_shutdown() may be called from interrupt context, either as a result
520 1.32 sommerfe * of a panic, or from the debugger. It proceeds to call
521 1.32 sommerfe * sys_sync(&proc0, ...), pretending its running on behalf of proc0
522 1.32 sommerfe *
523 1.32 sommerfe * We would like to make an attempt to sync the filesystems in this case, so
524 1.32 sommerfe * if this happens, we treat attempts to acquire locks specially.
525 1.32 sommerfe * All locks are acquired on behalf of proc0.
526 1.32 sommerfe *
527 1.32 sommerfe * If we've already paniced, we don't block waiting for locks, but
528 1.32 sommerfe * just barge right ahead since we're already going down in flames.
529 1.32 sommerfe */
530 1.32 sommerfe
531 1.32 sommerfe /*
532 1.1 fvdl * Set, change, or release a lock.
533 1.1 fvdl *
534 1.1 fvdl * Shared requests increment the shared count. Exclusive requests set the
535 1.1 fvdl * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
536 1.1 fvdl * accepted shared locks and shared-to-exclusive upgrades to go away.
537 1.1 fvdl */
538 1.1 fvdl int
539 1.50 thorpej #if defined(LOCKDEBUG)
540 1.50 thorpej _lockmgr(__volatile struct lock *lkp, u_int flags,
541 1.50 thorpej struct simplelock *interlkp, const char *file, int line)
542 1.50 thorpej #else
543 1.33 thorpej lockmgr(__volatile struct lock *lkp, u_int flags,
544 1.33 thorpej struct simplelock *interlkp)
545 1.50 thorpej #endif
546 1.1 fvdl {
547 1.1 fvdl int error;
548 1.1 fvdl pid_t pid;
549 1.69 thorpej lwpid_t lid;
550 1.1 fvdl int extflags;
551 1.24 thorpej cpuid_t cpu_id;
552 1.69 thorpej struct lwp *l = curlwp;
553 1.32 sommerfe int lock_shutdown_noblock = 0;
554 1.67 scw int s = 0;
555 1.1 fvdl
556 1.1 fvdl error = 0;
557 1.19 thorpej
558 1.80 yamt /* LK_RETRY is for vn_lock, not for lockmgr. */
559 1.79 yamt KASSERT((flags & LK_RETRY) == 0);
560 1.79 yamt
561 1.43 thorpej INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
562 1.1 fvdl if (flags & LK_INTERLOCK)
563 1.1 fvdl simple_unlock(interlkp);
564 1.1 fvdl extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
565 1.19 thorpej
566 1.21 thorpej #ifdef DIAGNOSTIC /* { */
567 1.19 thorpej /*
568 1.19 thorpej * Don't allow spins on sleep locks and don't allow sleeps
569 1.19 thorpej * on spin locks.
570 1.19 thorpej */
571 1.19 thorpej if ((flags ^ lkp->lk_flags) & LK_SPIN)
572 1.64 provos panic("lockmgr: sleep/spin mismatch");
573 1.21 thorpej #endif /* } */
574 1.19 thorpej
575 1.69 thorpej if (extflags & LK_SPIN) {
576 1.19 thorpej pid = LK_KERNPROC;
577 1.69 thorpej lid = 0;
578 1.69 thorpej } else {
579 1.69 thorpej if (l == NULL) {
580 1.32 sommerfe if (!doing_shutdown) {
581 1.32 sommerfe panic("lockmgr: no context");
582 1.32 sommerfe } else {
583 1.69 thorpej l = &lwp0;
584 1.32 sommerfe if (panicstr && (!(flags & LK_NOWAIT))) {
585 1.32 sommerfe flags |= LK_NOWAIT;
586 1.32 sommerfe lock_shutdown_noblock = 1;
587 1.32 sommerfe }
588 1.32 sommerfe }
589 1.32 sommerfe }
590 1.69 thorpej lid = l->l_lid;
591 1.69 thorpej pid = l->l_proc->p_pid;
592 1.19 thorpej }
593 1.24 thorpej cpu_id = cpu_number();
594 1.19 thorpej
595 1.1 fvdl /*
596 1.1 fvdl * Once a lock has drained, the LK_DRAINING flag is set and an
597 1.1 fvdl * exclusive lock is returned. The only valid operation thereafter
598 1.1 fvdl * is a single release of that exclusive lock. This final release
599 1.1 fvdl * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
600 1.1 fvdl * further requests of any sort will result in a panic. The bits
601 1.1 fvdl * selected for these two flags are chosen so that they will be set
602 1.1 fvdl * in memory that is freed (freed memory is filled with 0xdeadbeef).
603 1.1 fvdl * The final release is permitted to give a new lease on life to
604 1.1 fvdl * the lock by specifying LK_REENABLE.
605 1.1 fvdl */
606 1.1 fvdl if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
607 1.28 thorpej #ifdef DIAGNOSTIC /* { */
608 1.1 fvdl if (lkp->lk_flags & LK_DRAINED)
609 1.1 fvdl panic("lockmgr: using decommissioned lock");
610 1.1 fvdl if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
611 1.69 thorpej WEHOLDIT(lkp, pid, lid, cpu_id) == 0)
612 1.64 provos panic("lockmgr: non-release on draining lock: %d",
613 1.1 fvdl flags & LK_TYPE_MASK);
614 1.28 thorpej #endif /* DIAGNOSTIC */ /* } */
615 1.1 fvdl lkp->lk_flags &= ~LK_DRAINING;
616 1.1 fvdl if ((flags & LK_REENABLE) == 0)
617 1.1 fvdl lkp->lk_flags |= LK_DRAINED;
618 1.1 fvdl }
619 1.1 fvdl
620 1.1 fvdl switch (flags & LK_TYPE_MASK) {
621 1.1 fvdl
622 1.1 fvdl case LK_SHARED:
623 1.69 thorpej if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0) {
624 1.1 fvdl /*
625 1.1 fvdl * If just polling, check to see if we will block.
626 1.1 fvdl */
627 1.1 fvdl if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
628 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
629 1.1 fvdl error = EBUSY;
630 1.1 fvdl break;
631 1.1 fvdl }
632 1.1 fvdl /*
633 1.1 fvdl * Wait for exclusive locks and upgrades to clear.
634 1.1 fvdl */
635 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
636 1.73 yamt LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE);
637 1.1 fvdl if (error)
638 1.1 fvdl break;
639 1.1 fvdl lkp->lk_sharecount++;
640 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
641 1.69 thorpej COUNT(lkp, l, cpu_id, 1);
642 1.1 fvdl break;
643 1.1 fvdl }
644 1.1 fvdl /*
645 1.1 fvdl * We hold an exclusive lock, so downgrade it to shared.
646 1.1 fvdl * An alternative would be to fail with EDEADLK.
647 1.1 fvdl */
648 1.1 fvdl lkp->lk_sharecount++;
649 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
650 1.69 thorpej COUNT(lkp, l, cpu_id, 1);
651 1.1 fvdl /* fall into downgrade */
652 1.1 fvdl
653 1.1 fvdl case LK_DOWNGRADE:
654 1.69 thorpej if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0 ||
655 1.19 thorpej lkp->lk_exclusivecount == 0)
656 1.1 fvdl panic("lockmgr: not holding exclusive lock");
657 1.1 fvdl lkp->lk_sharecount += lkp->lk_exclusivecount;
658 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
659 1.1 fvdl lkp->lk_exclusivecount = 0;
660 1.15 fvdl lkp->lk_recurselevel = 0;
661 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
662 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
663 1.50 thorpej #if defined(LOCKDEBUG)
664 1.50 thorpej lkp->lk_unlock_file = file;
665 1.50 thorpej lkp->lk_unlock_line = line;
666 1.50 thorpej #endif
667 1.21 thorpej DONTHAVEIT(lkp);
668 1.23 thorpej WAKEUP_WAITER(lkp);
669 1.1 fvdl break;
670 1.1 fvdl
671 1.1 fvdl case LK_EXCLUPGRADE:
672 1.1 fvdl /*
673 1.1 fvdl * If another process is ahead of us to get an upgrade,
674 1.1 fvdl * then we want to fail rather than have an intervening
675 1.1 fvdl * exclusive access.
676 1.1 fvdl */
677 1.1 fvdl if (lkp->lk_flags & LK_WANT_UPGRADE) {
678 1.1 fvdl lkp->lk_sharecount--;
679 1.73 yamt if (lkp->lk_sharecount == 0)
680 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
681 1.69 thorpej COUNT(lkp, l, cpu_id, -1);
682 1.1 fvdl error = EBUSY;
683 1.1 fvdl break;
684 1.1 fvdl }
685 1.1 fvdl /* fall into normal upgrade */
686 1.1 fvdl
687 1.1 fvdl case LK_UPGRADE:
688 1.1 fvdl /*
689 1.1 fvdl * Upgrade a shared lock to an exclusive one. If another
690 1.1 fvdl * shared lock has already requested an upgrade to an
691 1.1 fvdl * exclusive lock, our shared lock is released and an
692 1.1 fvdl * exclusive lock is requested (which will be granted
693 1.1 fvdl * after the upgrade). If we return an error, the file
694 1.1 fvdl * will always be unlocked.
695 1.1 fvdl */
696 1.69 thorpej if (WEHOLDIT(lkp, pid, lid, cpu_id) || lkp->lk_sharecount <= 0)
697 1.1 fvdl panic("lockmgr: upgrade exclusive lock");
698 1.1 fvdl lkp->lk_sharecount--;
699 1.73 yamt if (lkp->lk_sharecount == 0)
700 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
701 1.69 thorpej COUNT(lkp, l, cpu_id, -1);
702 1.1 fvdl /*
703 1.1 fvdl * If we are just polling, check to see if we will block.
704 1.1 fvdl */
705 1.1 fvdl if ((extflags & LK_NOWAIT) &&
706 1.1 fvdl ((lkp->lk_flags & LK_WANT_UPGRADE) ||
707 1.1 fvdl lkp->lk_sharecount > 1)) {
708 1.1 fvdl error = EBUSY;
709 1.1 fvdl break;
710 1.1 fvdl }
711 1.1 fvdl if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
712 1.1 fvdl /*
713 1.1 fvdl * We are first shared lock to request an upgrade, so
714 1.1 fvdl * request upgrade and wait for the shared count to
715 1.1 fvdl * drop to zero, then take exclusive lock.
716 1.1 fvdl */
717 1.1 fvdl lkp->lk_flags |= LK_WANT_UPGRADE;
718 1.78 hannken error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO);
719 1.1 fvdl lkp->lk_flags &= ~LK_WANT_UPGRADE;
720 1.83 yamt if (error) {
721 1.83 yamt WAKEUP_WAITER(lkp);
722 1.1 fvdl break;
723 1.83 yamt }
724 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
725 1.69 thorpej SETHOLDER(lkp, pid, lid, cpu_id);
726 1.50 thorpej #if defined(LOCKDEBUG)
727 1.50 thorpej lkp->lk_lock_file = file;
728 1.50 thorpej lkp->lk_lock_line = line;
729 1.50 thorpej #endif
730 1.21 thorpej HAVEIT(lkp);
731 1.1 fvdl if (lkp->lk_exclusivecount != 0)
732 1.1 fvdl panic("lockmgr: non-zero exclusive count");
733 1.1 fvdl lkp->lk_exclusivecount = 1;
734 1.15 fvdl if (extflags & LK_SETRECURSE)
735 1.15 fvdl lkp->lk_recurselevel = 1;
736 1.69 thorpej COUNT(lkp, l, cpu_id, 1);
737 1.1 fvdl break;
738 1.1 fvdl }
739 1.1 fvdl /*
740 1.1 fvdl * Someone else has requested upgrade. Release our shared
741 1.1 fvdl * lock, awaken upgrade requestor if we are the last shared
742 1.1 fvdl * lock, then request an exclusive lock.
743 1.1 fvdl */
744 1.23 thorpej if (lkp->lk_sharecount == 0)
745 1.23 thorpej WAKEUP_WAITER(lkp);
746 1.1 fvdl /* fall into exclusive request */
747 1.1 fvdl
748 1.1 fvdl case LK_EXCLUSIVE:
749 1.69 thorpej if (WEHOLDIT(lkp, pid, lid, cpu_id)) {
750 1.1 fvdl /*
751 1.19 thorpej * Recursive lock.
752 1.1 fvdl */
753 1.15 fvdl if ((extflags & LK_CANRECURSE) == 0 &&
754 1.16 sommerfe lkp->lk_recurselevel == 0) {
755 1.16 sommerfe if (extflags & LK_RECURSEFAIL) {
756 1.16 sommerfe error = EDEADLK;
757 1.16 sommerfe break;
758 1.16 sommerfe } else
759 1.16 sommerfe panic("lockmgr: locking against myself");
760 1.16 sommerfe }
761 1.1 fvdl lkp->lk_exclusivecount++;
762 1.15 fvdl if (extflags & LK_SETRECURSE &&
763 1.15 fvdl lkp->lk_recurselevel == 0)
764 1.15 fvdl lkp->lk_recurselevel = lkp->lk_exclusivecount;
765 1.69 thorpej COUNT(lkp, l, cpu_id, 1);
766 1.1 fvdl break;
767 1.1 fvdl }
768 1.1 fvdl /*
769 1.1 fvdl * If we are just polling, check to see if we will sleep.
770 1.1 fvdl */
771 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
772 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
773 1.73 yamt LK_SHARE_NONZERO))) {
774 1.1 fvdl error = EBUSY;
775 1.1 fvdl break;
776 1.1 fvdl }
777 1.1 fvdl /*
778 1.1 fvdl * Try to acquire the want_exclusive flag.
779 1.1 fvdl */
780 1.82 yamt error = acquire(&lkp, &s, extflags, 0,
781 1.82 yamt LK_HAVE_EXCL | LK_WANT_EXCL);
782 1.1 fvdl if (error)
783 1.1 fvdl break;
784 1.1 fvdl lkp->lk_flags |= LK_WANT_EXCL;
785 1.1 fvdl /*
786 1.1 fvdl * Wait for shared locks and upgrades to finish.
787 1.1 fvdl */
788 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
789 1.81 yamt LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO);
790 1.1 fvdl lkp->lk_flags &= ~LK_WANT_EXCL;
791 1.83 yamt if (error) {
792 1.83 yamt WAKEUP_WAITER(lkp);
793 1.1 fvdl break;
794 1.83 yamt }
795 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
796 1.69 thorpej SETHOLDER(lkp, pid, lid, cpu_id);
797 1.50 thorpej #if defined(LOCKDEBUG)
798 1.50 thorpej lkp->lk_lock_file = file;
799 1.50 thorpej lkp->lk_lock_line = line;
800 1.50 thorpej #endif
801 1.21 thorpej HAVEIT(lkp);
802 1.1 fvdl if (lkp->lk_exclusivecount != 0)
803 1.1 fvdl panic("lockmgr: non-zero exclusive count");
804 1.1 fvdl lkp->lk_exclusivecount = 1;
805 1.15 fvdl if (extflags & LK_SETRECURSE)
806 1.15 fvdl lkp->lk_recurselevel = 1;
807 1.69 thorpej COUNT(lkp, l, cpu_id, 1);
808 1.1 fvdl break;
809 1.1 fvdl
810 1.1 fvdl case LK_RELEASE:
811 1.1 fvdl if (lkp->lk_exclusivecount != 0) {
812 1.69 thorpej if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0) {
813 1.19 thorpej if (lkp->lk_flags & LK_SPIN) {
814 1.19 thorpej panic("lockmgr: processor %lu, not "
815 1.19 thorpej "exclusive lock holder %lu "
816 1.19 thorpej "unlocking", cpu_id, lkp->lk_cpu);
817 1.19 thorpej } else {
818 1.19 thorpej panic("lockmgr: pid %d, not "
819 1.19 thorpej "exclusive lock holder %d "
820 1.19 thorpej "unlocking", pid,
821 1.19 thorpej lkp->lk_lockholder);
822 1.19 thorpej }
823 1.19 thorpej }
824 1.15 fvdl if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
825 1.15 fvdl lkp->lk_recurselevel = 0;
826 1.1 fvdl lkp->lk_exclusivecount--;
827 1.69 thorpej COUNT(lkp, l, cpu_id, -1);
828 1.1 fvdl if (lkp->lk_exclusivecount == 0) {
829 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
830 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
831 1.50 thorpej #if defined(LOCKDEBUG)
832 1.50 thorpej lkp->lk_unlock_file = file;
833 1.50 thorpej lkp->lk_unlock_line = line;
834 1.50 thorpej #endif
835 1.21 thorpej DONTHAVEIT(lkp);
836 1.1 fvdl }
837 1.1 fvdl } else if (lkp->lk_sharecount != 0) {
838 1.1 fvdl lkp->lk_sharecount--;
839 1.73 yamt if (lkp->lk_sharecount == 0)
840 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
841 1.69 thorpej COUNT(lkp, l, cpu_id, -1);
842 1.1 fvdl }
843 1.39 thorpej #ifdef DIAGNOSTIC
844 1.39 thorpej else
845 1.39 thorpej panic("lockmgr: release of unlocked lock!");
846 1.39 thorpej #endif
847 1.23 thorpej WAKEUP_WAITER(lkp);
848 1.1 fvdl break;
849 1.1 fvdl
850 1.1 fvdl case LK_DRAIN:
851 1.1 fvdl /*
852 1.86 perry * Check that we do not already hold the lock, as it can
853 1.1 fvdl * never drain if we do. Unfortunately, we have no way to
854 1.1 fvdl * check for holding a shared lock, but at least we can
855 1.1 fvdl * check for an exclusive one.
856 1.1 fvdl */
857 1.69 thorpej if (WEHOLDIT(lkp, pid, lid, cpu_id))
858 1.1 fvdl panic("lockmgr: draining against myself");
859 1.1 fvdl /*
860 1.1 fvdl * If we are just polling, check to see if we will sleep.
861 1.1 fvdl */
862 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
863 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
864 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
865 1.1 fvdl error = EBUSY;
866 1.1 fvdl break;
867 1.1 fvdl }
868 1.78 hannken error = acquire(&lkp, &s, extflags, 1,
869 1.73 yamt LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
870 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO);
871 1.23 thorpej if (error)
872 1.23 thorpej break;
873 1.1 fvdl lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
874 1.69 thorpej SETHOLDER(lkp, pid, lid, cpu_id);
875 1.50 thorpej #if defined(LOCKDEBUG)
876 1.50 thorpej lkp->lk_lock_file = file;
877 1.50 thorpej lkp->lk_lock_line = line;
878 1.50 thorpej #endif
879 1.21 thorpej HAVEIT(lkp);
880 1.1 fvdl lkp->lk_exclusivecount = 1;
881 1.15 fvdl /* XXX unlikely that we'd want this */
882 1.15 fvdl if (extflags & LK_SETRECURSE)
883 1.15 fvdl lkp->lk_recurselevel = 1;
884 1.69 thorpej COUNT(lkp, l, cpu_id, 1);
885 1.1 fvdl break;
886 1.1 fvdl
887 1.1 fvdl default:
888 1.43 thorpej INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
889 1.1 fvdl panic("lockmgr: unknown locktype request %d",
890 1.1 fvdl flags & LK_TYPE_MASK);
891 1.1 fvdl /* NOTREACHED */
892 1.1 fvdl }
893 1.23 thorpej if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
894 1.23 thorpej ((lkp->lk_flags &
895 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
896 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
897 1.1 fvdl lkp->lk_flags &= ~LK_WAITDRAIN;
898 1.87 christos wakeup(&lkp->lk_flags);
899 1.1 fvdl }
900 1.32 sommerfe /*
901 1.32 sommerfe * Note that this panic will be a recursive panic, since
902 1.32 sommerfe * we only set lock_shutdown_noblock above if panicstr != NULL.
903 1.32 sommerfe */
904 1.32 sommerfe if (error && lock_shutdown_noblock)
905 1.32 sommerfe panic("lockmgr: deadlock (see previous panic)");
906 1.86 perry
907 1.43 thorpej INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
908 1.1 fvdl return (error);
909 1.1 fvdl }
910 1.1 fvdl
911 1.1 fvdl /*
912 1.47 sommerfe * For a recursive spinlock held one or more times by the current CPU,
913 1.47 sommerfe * release all N locks, and return N.
914 1.47 sommerfe * Intended for use in mi_switch() shortly before context switching.
915 1.47 sommerfe */
916 1.47 sommerfe
917 1.47 sommerfe int
918 1.50 thorpej #if defined(LOCKDEBUG)
919 1.50 thorpej _spinlock_release_all(__volatile struct lock *lkp, const char *file, int line)
920 1.50 thorpej #else
921 1.47 sommerfe spinlock_release_all(__volatile struct lock *lkp)
922 1.50 thorpej #endif
923 1.47 sommerfe {
924 1.47 sommerfe int s, count;
925 1.47 sommerfe cpuid_t cpu_id;
926 1.86 perry
927 1.47 sommerfe KASSERT(lkp->lk_flags & LK_SPIN);
928 1.86 perry
929 1.47 sommerfe INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
930 1.47 sommerfe
931 1.47 sommerfe cpu_id = cpu_number();
932 1.47 sommerfe count = lkp->lk_exclusivecount;
933 1.86 perry
934 1.47 sommerfe if (count != 0) {
935 1.86 perry #ifdef DIAGNOSTIC
936 1.69 thorpej if (WEHOLDIT(lkp, 0, 0, cpu_id) == 0) {
937 1.47 sommerfe panic("spinlock_release_all: processor %lu, not "
938 1.47 sommerfe "exclusive lock holder %lu "
939 1.47 sommerfe "unlocking", (long)cpu_id, lkp->lk_cpu);
940 1.47 sommerfe }
941 1.47 sommerfe #endif
942 1.47 sommerfe lkp->lk_recurselevel = 0;
943 1.47 sommerfe lkp->lk_exclusivecount = 0;
944 1.47 sommerfe COUNT_CPU(cpu_id, -count);
945 1.47 sommerfe lkp->lk_flags &= ~LK_HAVE_EXCL;
946 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
947 1.50 thorpej #if defined(LOCKDEBUG)
948 1.50 thorpej lkp->lk_unlock_file = file;
949 1.50 thorpej lkp->lk_unlock_line = line;
950 1.50 thorpej #endif
951 1.47 sommerfe DONTHAVEIT(lkp);
952 1.47 sommerfe }
953 1.47 sommerfe #ifdef DIAGNOSTIC
954 1.47 sommerfe else if (lkp->lk_sharecount != 0)
955 1.47 sommerfe panic("spinlock_release_all: release of shared lock!");
956 1.47 sommerfe else
957 1.47 sommerfe panic("spinlock_release_all: release of unlocked lock!");
958 1.47 sommerfe #endif
959 1.86 perry INTERLOCK_RELEASE(lkp, LK_SPIN, s);
960 1.47 sommerfe
961 1.47 sommerfe return (count);
962 1.47 sommerfe }
963 1.47 sommerfe
964 1.47 sommerfe /*
965 1.47 sommerfe * For a recursive spinlock held one or more times by the current CPU,
966 1.47 sommerfe * release all N locks, and return N.
967 1.47 sommerfe * Intended for use in mi_switch() right after resuming execution.
968 1.47 sommerfe */
969 1.47 sommerfe
970 1.47 sommerfe void
971 1.50 thorpej #if defined(LOCKDEBUG)
972 1.50 thorpej _spinlock_acquire_count(__volatile struct lock *lkp, int count,
973 1.50 thorpej const char *file, int line)
974 1.50 thorpej #else
975 1.47 sommerfe spinlock_acquire_count(__volatile struct lock *lkp, int count)
976 1.50 thorpej #endif
977 1.47 sommerfe {
978 1.47 sommerfe int s, error;
979 1.47 sommerfe cpuid_t cpu_id;
980 1.86 perry
981 1.47 sommerfe KASSERT(lkp->lk_flags & LK_SPIN);
982 1.86 perry
983 1.47 sommerfe INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
984 1.47 sommerfe
985 1.47 sommerfe cpu_id = cpu_number();
986 1.47 sommerfe
987 1.47 sommerfe #ifdef DIAGNOSTIC
988 1.69 thorpej if (WEHOLDIT(lkp, LK_NOPROC, 0, cpu_id))
989 1.64 provos panic("spinlock_acquire_count: processor %lu already holds lock", (long)cpu_id);
990 1.47 sommerfe #endif
991 1.47 sommerfe /*
992 1.47 sommerfe * Try to acquire the want_exclusive flag.
993 1.47 sommerfe */
994 1.78 hannken error = acquire(&lkp, &s, LK_SPIN, 0, LK_HAVE_EXCL | LK_WANT_EXCL);
995 1.47 sommerfe lkp->lk_flags |= LK_WANT_EXCL;
996 1.47 sommerfe /*
997 1.47 sommerfe * Wait for shared locks and upgrades to finish.
998 1.47 sommerfe */
999 1.78 hannken error = acquire(&lkp, &s, LK_SPIN, 0,
1000 1.82 yamt LK_HAVE_EXCL | LK_SHARE_NONZERO | LK_WANT_UPGRADE);
1001 1.47 sommerfe lkp->lk_flags &= ~LK_WANT_EXCL;
1002 1.47 sommerfe lkp->lk_flags |= LK_HAVE_EXCL;
1003 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, cpu_id);
1004 1.50 thorpej #if defined(LOCKDEBUG)
1005 1.50 thorpej lkp->lk_lock_file = file;
1006 1.50 thorpej lkp->lk_lock_line = line;
1007 1.50 thorpej #endif
1008 1.47 sommerfe HAVEIT(lkp);
1009 1.47 sommerfe if (lkp->lk_exclusivecount != 0)
1010 1.47 sommerfe panic("lockmgr: non-zero exclusive count");
1011 1.47 sommerfe lkp->lk_exclusivecount = count;
1012 1.47 sommerfe lkp->lk_recurselevel = 1;
1013 1.47 sommerfe COUNT_CPU(cpu_id, count);
1014 1.47 sommerfe
1015 1.86 perry INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
1016 1.47 sommerfe }
1017 1.47 sommerfe
1018 1.47 sommerfe
1019 1.47 sommerfe
1020 1.47 sommerfe /*
1021 1.1 fvdl * Print out information about state of a lock. Used by VOP_PRINT
1022 1.1 fvdl * routines to display ststus about contained locks.
1023 1.1 fvdl */
1024 1.2 fvdl void
1025 1.33 thorpej lockmgr_printinfo(__volatile struct lock *lkp)
1026 1.1 fvdl {
1027 1.1 fvdl
1028 1.1 fvdl if (lkp->lk_sharecount)
1029 1.1 fvdl printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
1030 1.1 fvdl lkp->lk_sharecount);
1031 1.19 thorpej else if (lkp->lk_flags & LK_HAVE_EXCL) {
1032 1.19 thorpej printf(" lock type %s: EXCL (count %d) by ",
1033 1.19 thorpej lkp->lk_wmesg, lkp->lk_exclusivecount);
1034 1.19 thorpej if (lkp->lk_flags & LK_SPIN)
1035 1.19 thorpej printf("processor %lu", lkp->lk_cpu);
1036 1.19 thorpej else
1037 1.69 thorpej printf("pid %d.%d", lkp->lk_lockholder,
1038 1.69 thorpej lkp->lk_locklwp);
1039 1.19 thorpej } else
1040 1.19 thorpej printf(" not locked");
1041 1.19 thorpej if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
1042 1.1 fvdl printf(" with %d pending", lkp->lk_waitcount);
1043 1.1 fvdl }
1044 1.1 fvdl
1045 1.21 thorpej #if defined(LOCKDEBUG) /* { */
1046 1.87 christos _TAILQ_HEAD(, struct simplelock, __volatile) simplelock_list =
1047 1.21 thorpej TAILQ_HEAD_INITIALIZER(simplelock_list);
1048 1.21 thorpej
1049 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1050 1.21 thorpej struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
1051 1.21 thorpej
1052 1.21 thorpej #define SLOCK_LIST_LOCK() \
1053 1.29 sommerfe __cpu_simple_lock(&simplelock_list_slock.lock_data)
1054 1.21 thorpej
1055 1.21 thorpej #define SLOCK_LIST_UNLOCK() \
1056 1.29 sommerfe __cpu_simple_unlock(&simplelock_list_slock.lock_data)
1057 1.21 thorpej
1058 1.21 thorpej #define SLOCK_COUNT(x) \
1059 1.47 sommerfe curcpu()->ci_simple_locks += (x)
1060 1.21 thorpej #else
1061 1.21 thorpej u_long simple_locks;
1062 1.21 thorpej
1063 1.21 thorpej #define SLOCK_LIST_LOCK() /* nothing */
1064 1.21 thorpej
1065 1.21 thorpej #define SLOCK_LIST_UNLOCK() /* nothing */
1066 1.21 thorpej
1067 1.21 thorpej #define SLOCK_COUNT(x) simple_locks += (x)
1068 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
1069 1.21 thorpej
1070 1.26 sommerfe #ifdef MULTIPROCESSOR
1071 1.75 wiz #define SLOCK_MP() lock_printf("on CPU %ld\n", \
1072 1.46 thorpej (u_long) cpu_number())
1073 1.26 sommerfe #else
1074 1.26 sommerfe #define SLOCK_MP() /* nothing */
1075 1.26 sommerfe #endif
1076 1.26 sommerfe
1077 1.21 thorpej #define SLOCK_WHERE(str, alp, id, l) \
1078 1.21 thorpej do { \
1079 1.58 chs lock_printf("\n"); \
1080 1.25 thorpej lock_printf(str); \
1081 1.33 thorpej lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
1082 1.26 sommerfe SLOCK_MP(); \
1083 1.21 thorpej if ((alp)->lock_file != NULL) \
1084 1.25 thorpej lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
1085 1.21 thorpej (alp)->lock_line); \
1086 1.21 thorpej if ((alp)->unlock_file != NULL) \
1087 1.25 thorpej lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
1088 1.21 thorpej (alp)->unlock_line); \
1089 1.58 chs SLOCK_TRACE() \
1090 1.21 thorpej SLOCK_DEBUGGER(); \
1091 1.30 thorpej } while (/*CONSTCOND*/0)
1092 1.12 chs
1093 1.1 fvdl /*
1094 1.1 fvdl * Simple lock functions so that the debugger can see from whence
1095 1.1 fvdl * they are being called.
1096 1.1 fvdl */
1097 1.1 fvdl void
1098 1.87 christos simple_lock_init(__volatile struct simplelock *alp)
1099 1.1 fvdl {
1100 1.21 thorpej
1101 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1102 1.27 thorpej __cpu_simple_lock_init(&alp->lock_data);
1103 1.21 thorpej #else
1104 1.27 thorpej alp->lock_data = __SIMPLELOCK_UNLOCKED;
1105 1.21 thorpej #endif /* } */
1106 1.5 chs alp->lock_file = NULL;
1107 1.5 chs alp->lock_line = 0;
1108 1.5 chs alp->unlock_file = NULL;
1109 1.5 chs alp->unlock_line = 0;
1110 1.41 thorpej alp->lock_holder = LK_NOCPU;
1111 1.1 fvdl }
1112 1.1 fvdl
1113 1.1 fvdl void
1114 1.33 thorpej _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
1115 1.1 fvdl {
1116 1.24 thorpej cpuid_t cpu_id = cpu_number();
1117 1.12 chs int s;
1118 1.12 chs
1119 1.44 thorpej s = spllock();
1120 1.21 thorpej
1121 1.21 thorpej /*
1122 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1123 1.21 thorpej * don't take any action, and just fall into the normal spin case.
1124 1.21 thorpej */
1125 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1126 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1127 1.21 thorpej if (alp->lock_holder == cpu_id) {
1128 1.21 thorpej SLOCK_WHERE("simple_lock: locking against myself\n",
1129 1.21 thorpej alp, id, l);
1130 1.21 thorpej goto out;
1131 1.1 fvdl }
1132 1.21 thorpej #else
1133 1.21 thorpej SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
1134 1.21 thorpej goto out;
1135 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
1136 1.1 fvdl }
1137 1.21 thorpej
1138 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1139 1.21 thorpej /* Acquire the lock before modifying any fields. */
1140 1.70 pk splx(s);
1141 1.27 thorpej __cpu_simple_lock(&alp->lock_data);
1142 1.70 pk s = spllock();
1143 1.21 thorpej #else
1144 1.27 thorpej alp->lock_data = __SIMPLELOCK_LOCKED;
1145 1.21 thorpej #endif /* } */
1146 1.21 thorpej
1147 1.45 sommerfe if (alp->lock_holder != LK_NOCPU) {
1148 1.45 sommerfe SLOCK_WHERE("simple_lock: uninitialized lock\n",
1149 1.45 sommerfe alp, id, l);
1150 1.45 sommerfe }
1151 1.5 chs alp->lock_file = id;
1152 1.5 chs alp->lock_line = l;
1153 1.21 thorpej alp->lock_holder = cpu_id;
1154 1.21 thorpej
1155 1.21 thorpej SLOCK_LIST_LOCK();
1156 1.87 christos TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
1157 1.21 thorpej SLOCK_LIST_UNLOCK();
1158 1.21 thorpej
1159 1.21 thorpej SLOCK_COUNT(1);
1160 1.21 thorpej
1161 1.21 thorpej out:
1162 1.18 chs splx(s);
1163 1.38 thorpej }
1164 1.38 thorpej
1165 1.38 thorpej int
1166 1.38 thorpej _simple_lock_held(__volatile struct simplelock *alp)
1167 1.38 thorpej {
1168 1.54 enami #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
1169 1.38 thorpej cpuid_t cpu_id = cpu_number();
1170 1.54 enami #endif
1171 1.38 thorpej int s, locked = 0;
1172 1.38 thorpej
1173 1.44 thorpej s = spllock();
1174 1.42 thorpej
1175 1.42 thorpej #if defined(MULTIPROCESSOR)
1176 1.38 thorpej if (__cpu_simple_lock_try(&alp->lock_data) == 0)
1177 1.38 thorpej locked = (alp->lock_holder == cpu_id);
1178 1.38 thorpej else
1179 1.38 thorpej __cpu_simple_unlock(&alp->lock_data);
1180 1.38 thorpej #else
1181 1.42 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1182 1.42 thorpej locked = 1;
1183 1.42 thorpej KASSERT(alp->lock_holder == cpu_id);
1184 1.42 thorpej }
1185 1.42 thorpej #endif
1186 1.38 thorpej
1187 1.38 thorpej splx(s);
1188 1.42 thorpej
1189 1.38 thorpej return (locked);
1190 1.1 fvdl }
1191 1.1 fvdl
1192 1.1 fvdl int
1193 1.33 thorpej _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
1194 1.1 fvdl {
1195 1.24 thorpej cpuid_t cpu_id = cpu_number();
1196 1.21 thorpej int s, rv = 0;
1197 1.1 fvdl
1198 1.44 thorpej s = spllock();
1199 1.21 thorpej
1200 1.21 thorpej /*
1201 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1202 1.21 thorpej * don't take any action.
1203 1.21 thorpej */
1204 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1205 1.27 thorpej if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
1206 1.21 thorpej if (alp->lock_holder == cpu_id)
1207 1.21 thorpej SLOCK_WHERE("simple_lock_try: locking against myself\n",
1208 1.26 sommerfe alp, id, l);
1209 1.21 thorpej goto out;
1210 1.21 thorpej }
1211 1.21 thorpej #else
1212 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1213 1.21 thorpej SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
1214 1.21 thorpej goto out;
1215 1.18 chs }
1216 1.27 thorpej alp->lock_data = __SIMPLELOCK_LOCKED;
1217 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
1218 1.21 thorpej
1219 1.21 thorpej /*
1220 1.21 thorpej * At this point, we have acquired the lock.
1221 1.21 thorpej */
1222 1.21 thorpej
1223 1.21 thorpej rv = 1;
1224 1.18 chs
1225 1.5 chs alp->lock_file = id;
1226 1.5 chs alp->lock_line = l;
1227 1.21 thorpej alp->lock_holder = cpu_id;
1228 1.21 thorpej
1229 1.21 thorpej SLOCK_LIST_LOCK();
1230 1.87 christos TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
1231 1.21 thorpej SLOCK_LIST_UNLOCK();
1232 1.21 thorpej
1233 1.21 thorpej SLOCK_COUNT(1);
1234 1.21 thorpej
1235 1.21 thorpej out:
1236 1.12 chs splx(s);
1237 1.21 thorpej return (rv);
1238 1.1 fvdl }
1239 1.1 fvdl
1240 1.1 fvdl void
1241 1.33 thorpej _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
1242 1.1 fvdl {
1243 1.12 chs int s;
1244 1.1 fvdl
1245 1.44 thorpej s = spllock();
1246 1.21 thorpej
1247 1.21 thorpej /*
1248 1.21 thorpej * MULTIPROCESSOR case: This is `safe' because we think we hold
1249 1.21 thorpej * the lock, and if we don't, we don't take any action.
1250 1.21 thorpej */
1251 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
1252 1.21 thorpej SLOCK_WHERE("simple_unlock: lock not held\n",
1253 1.21 thorpej alp, id, l);
1254 1.21 thorpej goto out;
1255 1.21 thorpej }
1256 1.21 thorpej
1257 1.21 thorpej SLOCK_LIST_LOCK();
1258 1.21 thorpej TAILQ_REMOVE(&simplelock_list, alp, list);
1259 1.21 thorpej SLOCK_LIST_UNLOCK();
1260 1.21 thorpej
1261 1.21 thorpej SLOCK_COUNT(-1);
1262 1.21 thorpej
1263 1.21 thorpej alp->list.tqe_next = NULL; /* sanity */
1264 1.21 thorpej alp->list.tqe_prev = NULL; /* sanity */
1265 1.21 thorpej
1266 1.5 chs alp->unlock_file = id;
1267 1.5 chs alp->unlock_line = l;
1268 1.21 thorpej
1269 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1270 1.26 sommerfe alp->lock_holder = LK_NOCPU;
1271 1.21 thorpej /* Now that we've modified all fields, release the lock. */
1272 1.27 thorpej __cpu_simple_unlock(&alp->lock_data);
1273 1.21 thorpej #else
1274 1.27 thorpej alp->lock_data = __SIMPLELOCK_UNLOCKED;
1275 1.41 thorpej KASSERT(alp->lock_holder == cpu_number());
1276 1.41 thorpej alp->lock_holder = LK_NOCPU;
1277 1.21 thorpej #endif /* } */
1278 1.21 thorpej
1279 1.21 thorpej out:
1280 1.18 chs splx(s);
1281 1.12 chs }
1282 1.12 chs
1283 1.12 chs void
1284 1.33 thorpej simple_lock_dump(void)
1285 1.12 chs {
1286 1.87 christos __volatile struct simplelock *alp;
1287 1.12 chs int s;
1288 1.12 chs
1289 1.44 thorpej s = spllock();
1290 1.21 thorpej SLOCK_LIST_LOCK();
1291 1.25 thorpej lock_printf("all simple locks:\n");
1292 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1293 1.25 thorpej lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
1294 1.21 thorpej alp->lock_file, alp->lock_line);
1295 1.12 chs }
1296 1.21 thorpej SLOCK_LIST_UNLOCK();
1297 1.12 chs splx(s);
1298 1.12 chs }
1299 1.12 chs
1300 1.12 chs void
1301 1.33 thorpej simple_lock_freecheck(void *start, void *end)
1302 1.12 chs {
1303 1.87 christos __volatile struct simplelock *alp;
1304 1.12 chs int s;
1305 1.12 chs
1306 1.44 thorpej s = spllock();
1307 1.21 thorpej SLOCK_LIST_LOCK();
1308 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1309 1.87 christos if ((__volatile void *)alp >= start &&
1310 1.87 christos (__volatile void *)alp < end) {
1311 1.25 thorpej lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
1312 1.34 thorpej alp, alp->lock_holder, alp->lock_file,
1313 1.34 thorpej alp->lock_line);
1314 1.34 thorpej SLOCK_DEBUGGER();
1315 1.34 thorpej }
1316 1.34 thorpej }
1317 1.34 thorpej SLOCK_LIST_UNLOCK();
1318 1.34 thorpej splx(s);
1319 1.34 thorpej }
1320 1.34 thorpej
1321 1.55 thorpej /*
1322 1.55 thorpej * We must be holding exactly one lock: the sched_lock.
1323 1.55 thorpej */
1324 1.55 thorpej
1325 1.34 thorpej void
1326 1.34 thorpej simple_lock_switchcheck(void)
1327 1.34 thorpej {
1328 1.55 thorpej
1329 1.55 thorpej simple_lock_only_held(&sched_lock, "switching");
1330 1.55 thorpej }
1331 1.55 thorpej
1332 1.55 thorpej void
1333 1.55 thorpej simple_lock_only_held(volatile struct simplelock *lp, const char *where)
1334 1.55 thorpej {
1335 1.87 christos __volatile struct simplelock *alp;
1336 1.34 thorpej cpuid_t cpu_id = cpu_number();
1337 1.34 thorpej int s;
1338 1.34 thorpej
1339 1.55 thorpej if (lp) {
1340 1.55 thorpej LOCK_ASSERT(simple_lock_held(lp));
1341 1.55 thorpej }
1342 1.44 thorpej s = spllock();
1343 1.34 thorpej SLOCK_LIST_LOCK();
1344 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1345 1.55 thorpej if (alp == lp)
1346 1.42 thorpej continue;
1347 1.85 yamt #if defined(MULTIPROCESSOR)
1348 1.85 yamt if (alp == &kernel_lock)
1349 1.85 yamt continue;
1350 1.85 yamt #endif /* defined(MULTIPROCESSOR) */
1351 1.55 thorpej if (alp->lock_holder == cpu_id)
1352 1.55 thorpej break;
1353 1.12 chs }
1354 1.21 thorpej SLOCK_LIST_UNLOCK();
1355 1.12 chs splx(s);
1356 1.55 thorpej
1357 1.55 thorpej if (alp != NULL) {
1358 1.58 chs lock_printf("\n%s with held simple_lock %p "
1359 1.55 thorpej "CPU %lu %s:%d\n",
1360 1.55 thorpej where, alp, alp->lock_holder, alp->lock_file,
1361 1.55 thorpej alp->lock_line);
1362 1.58 chs SLOCK_TRACE();
1363 1.55 thorpej SLOCK_DEBUGGER();
1364 1.55 thorpej }
1365 1.1 fvdl }
1366 1.21 thorpej #endif /* LOCKDEBUG */ /* } */
1367 1.62 thorpej
1368 1.62 thorpej #if defined(MULTIPROCESSOR)
1369 1.62 thorpej /*
1370 1.62 thorpej * Functions for manipulating the kernel_lock. We put them here
1371 1.62 thorpej * so that they show up in profiles.
1372 1.62 thorpej */
1373 1.62 thorpej
1374 1.85 yamt /*
1375 1.85 yamt * splbiglock: block IPLs which need to grab kernel_lock.
1376 1.85 yamt * XXX splvm or splaudio should be enough.
1377 1.85 yamt */
1378 1.85 yamt #if !defined(__HAVE_SPLBIGLOCK)
1379 1.85 yamt #define splbiglock() splclock()
1380 1.85 yamt #endif
1381 1.62 thorpej
1382 1.62 thorpej void
1383 1.62 thorpej _kernel_lock_init(void)
1384 1.62 thorpej {
1385 1.62 thorpej
1386 1.85 yamt simple_lock_init(&kernel_lock);
1387 1.62 thorpej }
1388 1.62 thorpej
1389 1.62 thorpej /*
1390 1.62 thorpej * Acquire/release the kernel lock. Intended for use in the scheduler
1391 1.62 thorpej * and the lower half of the kernel.
1392 1.62 thorpej */
1393 1.62 thorpej void
1394 1.62 thorpej _kernel_lock(int flag)
1395 1.62 thorpej {
1396 1.85 yamt struct cpu_info *ci = curcpu();
1397 1.62 thorpej
1398 1.62 thorpej SCHED_ASSERT_UNLOCKED();
1399 1.85 yamt
1400 1.85 yamt if (ci->ci_data.cpu_biglock_count > 0) {
1401 1.85 yamt LOCK_ASSERT(simple_lock_held(&kernel_lock));
1402 1.85 yamt ci->ci_data.cpu_biglock_count++;
1403 1.85 yamt } else {
1404 1.85 yamt int s;
1405 1.85 yamt
1406 1.85 yamt s = splbiglock();
1407 1.85 yamt while (!simple_lock_try(&kernel_lock)) {
1408 1.85 yamt splx(s);
1409 1.85 yamt SPINLOCK_SPIN_HOOK;
1410 1.85 yamt s = splbiglock();
1411 1.85 yamt }
1412 1.85 yamt ci->ci_data.cpu_biglock_count++;
1413 1.85 yamt splx(s);
1414 1.85 yamt }
1415 1.62 thorpej }
1416 1.62 thorpej
1417 1.62 thorpej void
1418 1.62 thorpej _kernel_unlock(void)
1419 1.62 thorpej {
1420 1.85 yamt struct cpu_info *ci = curcpu();
1421 1.85 yamt int s;
1422 1.85 yamt
1423 1.85 yamt KASSERT(ci->ci_data.cpu_biglock_count > 0);
1424 1.62 thorpej
1425 1.85 yamt s = splbiglock();
1426 1.85 yamt if ((--ci->ci_data.cpu_biglock_count) == 0)
1427 1.85 yamt simple_unlock(&kernel_lock);
1428 1.85 yamt splx(s);
1429 1.62 thorpej }
1430 1.62 thorpej
1431 1.62 thorpej /*
1432 1.62 thorpej * Acquire/release the kernel_lock on behalf of a process. Intended for
1433 1.62 thorpej * use in the top half of the kernel.
1434 1.62 thorpej */
1435 1.62 thorpej void
1436 1.69 thorpej _kernel_proc_lock(struct lwp *l)
1437 1.62 thorpej {
1438 1.62 thorpej
1439 1.62 thorpej SCHED_ASSERT_UNLOCKED();
1440 1.85 yamt _kernel_lock(0);
1441 1.62 thorpej }
1442 1.62 thorpej
1443 1.62 thorpej void
1444 1.69 thorpej _kernel_proc_unlock(struct lwp *l)
1445 1.62 thorpej {
1446 1.62 thorpej
1447 1.85 yamt _kernel_unlock();
1448 1.62 thorpej }
1449 1.77 yamt
1450 1.77 yamt int
1451 1.77 yamt _kernel_lock_release_all()
1452 1.77 yamt {
1453 1.85 yamt struct cpu_info *ci = curcpu();
1454 1.77 yamt int hold_count;
1455 1.77 yamt
1456 1.85 yamt hold_count = ci->ci_data.cpu_biglock_count;
1457 1.85 yamt
1458 1.85 yamt if (hold_count) {
1459 1.85 yamt int s;
1460 1.85 yamt
1461 1.85 yamt s = splbiglock();
1462 1.85 yamt ci->ci_data.cpu_biglock_count = 0;
1463 1.85 yamt simple_unlock(&kernel_lock);
1464 1.85 yamt splx(s);
1465 1.85 yamt }
1466 1.77 yamt
1467 1.77 yamt return hold_count;
1468 1.77 yamt }
1469 1.77 yamt
1470 1.77 yamt void
1471 1.77 yamt _kernel_lock_acquire_count(int hold_count)
1472 1.77 yamt {
1473 1.77 yamt
1474 1.85 yamt KASSERT(curcpu()->ci_data.cpu_biglock_count == 0);
1475 1.85 yamt
1476 1.85 yamt if (hold_count != 0) {
1477 1.85 yamt struct cpu_info *ci = curcpu();
1478 1.85 yamt int s;
1479 1.85 yamt
1480 1.85 yamt s = splbiglock();
1481 1.85 yamt while (!simple_lock_try(&kernel_lock)) {
1482 1.85 yamt splx(s);
1483 1.85 yamt SPINLOCK_SPIN_HOOK;
1484 1.85 yamt s = splbiglock();
1485 1.85 yamt }
1486 1.85 yamt ci->ci_data.cpu_biglock_count = hold_count;
1487 1.85 yamt splx(s);
1488 1.85 yamt }
1489 1.77 yamt }
1490 1.84 yamt #if defined(DEBUG)
1491 1.84 yamt void
1492 1.84 yamt _kernel_lock_assert_locked()
1493 1.84 yamt {
1494 1.84 yamt
1495 1.85 yamt LOCK_ASSERT(simple_lock_held(&kernel_lock));
1496 1.84 yamt }
1497 1.84 yamt #endif
1498 1.62 thorpej #endif /* MULTIPROCESSOR */
1499