kern_lock.c revision 1.25 1 1.25 thorpej /* $NetBSD: kern_lock.c,v 1.25 1999/08/27 01:14:38 thorpej Exp $ */
2 1.19 thorpej
3 1.19 thorpej /*-
4 1.19 thorpej * Copyright (c) 1999 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.1 fvdl /*
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.1 fvdl * 3. All advertising materials mentioning features or use of this software
60 1.1 fvdl * must display the following acknowledgement:
61 1.1 fvdl * This product includes software developed by the University of
62 1.1 fvdl * California, Berkeley and its contributors.
63 1.1 fvdl * 4. Neither the name of the University nor the names of its contributors
64 1.1 fvdl * may be used to endorse or promote products derived from this software
65 1.1 fvdl * without specific prior written permission.
66 1.1 fvdl *
67 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 1.1 fvdl * SUCH DAMAGE.
78 1.1 fvdl *
79 1.1 fvdl * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
80 1.1 fvdl */
81 1.7 thorpej
82 1.21 thorpej #include "opt_multiprocessor.h"
83 1.7 thorpej #include "opt_lockdebug.h"
84 1.18 chs #include "opt_ddb.h"
85 1.1 fvdl
86 1.1 fvdl #include <sys/param.h>
87 1.1 fvdl #include <sys/proc.h>
88 1.1 fvdl #include <sys/lock.h>
89 1.2 fvdl #include <sys/systm.h>
90 1.1 fvdl #include <machine/cpu.h>
91 1.1 fvdl
92 1.25 thorpej #if defined(LOCKDEBUG)
93 1.25 thorpej #include <sys/syslog.h>
94 1.25 thorpej /*
95 1.25 thorpej * note that stdarg.h and the ansi style va_start macro is used for both
96 1.25 thorpej * ansi and traditional c compiles.
97 1.25 thorpej * XXX: this requires that stdarg.h define: va_alist and va_dcl
98 1.25 thorpej */
99 1.25 thorpej #include <machine/stdarg.h>
100 1.25 thorpej
101 1.25 thorpej void lock_printf __P((const char *fmt, ...));
102 1.25 thorpej
103 1.25 thorpej int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
104 1.25 thorpej #endif
105 1.25 thorpej
106 1.1 fvdl /*
107 1.1 fvdl * Locking primitives implementation.
108 1.1 fvdl * Locks provide shared/exclusive sychronization.
109 1.1 fvdl */
110 1.1 fvdl
111 1.21 thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
112 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
113 1.21 thorpej #define COUNT_CPU(cpu_id, x) \
114 1.21 thorpej /* atomic_add_ulong(&curcpu().ci_spin_locks, (x)) */
115 1.21 thorpej #else
116 1.21 thorpej u_long spin_locks;
117 1.21 thorpej #define COUNT_CPU(cpu_id, x) spin_locks += (x)
118 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
119 1.21 thorpej
120 1.21 thorpej #define COUNT(lkp, p, cpu_id, x) \
121 1.21 thorpej do { \
122 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) \
123 1.21 thorpej COUNT_CPU((cpu_id), (x)); \
124 1.21 thorpej else \
125 1.21 thorpej (p)->p_locks += (x); \
126 1.21 thorpej } while (0)
127 1.1 fvdl #else
128 1.22 mellon #define COUNT(lkp, p, cpu_id, x)
129 1.21 thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
130 1.1 fvdl
131 1.1 fvdl /*
132 1.1 fvdl * Acquire a resource.
133 1.1 fvdl */
134 1.23 thorpej #define ACQUIRE(lkp, error, extflags, drain, wanted) \
135 1.19 thorpej if ((extflags) & LK_SPIN) { \
136 1.19 thorpej int interlocked; \
137 1.19 thorpej \
138 1.23 thorpej if ((drain) == 0) \
139 1.23 thorpej (lkp)->lk_waitcount++; \
140 1.19 thorpej for (interlocked = 1;;) { \
141 1.19 thorpej if (wanted) { \
142 1.19 thorpej if (interlocked) { \
143 1.19 thorpej simple_unlock(&(lkp)->lk_interlock); \
144 1.19 thorpej interlocked = 0; \
145 1.19 thorpej } \
146 1.19 thorpej } else if (interlocked) { \
147 1.19 thorpej break; \
148 1.19 thorpej } else { \
149 1.19 thorpej simple_lock(&(lkp)->lk_interlock); \
150 1.19 thorpej interlocked = 1; \
151 1.19 thorpej } \
152 1.19 thorpej } \
153 1.23 thorpej if ((drain) == 0) \
154 1.23 thorpej (lkp)->lk_waitcount--; \
155 1.19 thorpej KASSERT((wanted) == 0); \
156 1.19 thorpej error = 0; /* sanity */ \
157 1.19 thorpej } else { \
158 1.19 thorpej for (error = 0; wanted; ) { \
159 1.23 thorpej if ((drain)) \
160 1.23 thorpej (lkp)->lk_flags |= LK_WAITDRAIN; \
161 1.23 thorpej else \
162 1.23 thorpej (lkp)->lk_waitcount++; \
163 1.19 thorpej simple_unlock(&(lkp)->lk_interlock); \
164 1.23 thorpej /* XXX Cast away volatile. */ \
165 1.23 thorpej error = tsleep((drain) ? &(lkp)->lk_flags : \
166 1.23 thorpej (void *)(lkp), (lkp)->lk_prio, \
167 1.19 thorpej (lkp)->lk_wmesg, (lkp)->lk_timo); \
168 1.19 thorpej simple_lock(&(lkp)->lk_interlock); \
169 1.23 thorpej if ((drain) == 0) \
170 1.23 thorpej (lkp)->lk_waitcount--; \
171 1.19 thorpej if (error) \
172 1.19 thorpej break; \
173 1.19 thorpej if ((extflags) & LK_SLEEPFAIL) { \
174 1.19 thorpej error = ENOLCK; \
175 1.19 thorpej break; \
176 1.19 thorpej } \
177 1.1 fvdl } \
178 1.1 fvdl }
179 1.1 fvdl
180 1.19 thorpej #define SETHOLDER(lkp, pid, cpu_id) \
181 1.19 thorpej do { \
182 1.19 thorpej if ((lkp)->lk_flags & LK_SPIN) \
183 1.19 thorpej (lkp)->lk_cpu = cpu_id; \
184 1.19 thorpej else \
185 1.19 thorpej (lkp)->lk_lockholder = pid; \
186 1.19 thorpej } while (0)
187 1.19 thorpej
188 1.19 thorpej #define WEHOLDIT(lkp, pid, cpu_id) \
189 1.19 thorpej (((lkp)->lk_flags & LK_SPIN) != 0 ? \
190 1.19 thorpej ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
191 1.19 thorpej
192 1.23 thorpej #define WAKEUP_WAITER(lkp) \
193 1.23 thorpej do { \
194 1.23 thorpej if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) { \
195 1.23 thorpej /* XXX Cast away volatile. */ \
196 1.23 thorpej wakeup_one((void *)(lkp)); \
197 1.23 thorpej } \
198 1.23 thorpej } while (0)
199 1.23 thorpej
200 1.21 thorpej #if defined(LOCKDEBUG) /* { */
201 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
202 1.21 thorpej struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
203 1.21 thorpej
204 1.21 thorpej #define SPINLOCK_LIST_LOCK() cpu_simple_lock(&spinlock_list_slock)
205 1.21 thorpej
206 1.21 thorpej #define SPINLOCK_LIST_UNLOCK() cpu_simple_unlock(&spinlock_list_slock)
207 1.21 thorpej #else
208 1.21 thorpej #define SPINLOCK_LIST_LOCK() /* nothing */
209 1.21 thorpej
210 1.21 thorpej #define SPINLOCK_LIST_UNLOCK() /* nothing */
211 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
212 1.21 thorpej
213 1.21 thorpej TAILQ_HEAD(, lock) spinlock_list =
214 1.21 thorpej TAILQ_HEAD_INITIALIZER(spinlock_list);
215 1.21 thorpej
216 1.21 thorpej #define HAVEIT(lkp) \
217 1.21 thorpej do { \
218 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) { \
219 1.21 thorpej int s = splhigh(); \
220 1.21 thorpej SPINLOCK_LIST_LOCK(); \
221 1.21 thorpej /* XXX Cast away volatile. */ \
222 1.21 thorpej TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp), \
223 1.21 thorpej lk_list); \
224 1.21 thorpej SPINLOCK_LIST_UNLOCK(); \
225 1.21 thorpej splx(s); \
226 1.21 thorpej } \
227 1.21 thorpej } while (0)
228 1.21 thorpej
229 1.21 thorpej #define DONTHAVEIT(lkp) \
230 1.21 thorpej do { \
231 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) { \
232 1.21 thorpej int s = splhigh(); \
233 1.21 thorpej SPINLOCK_LIST_LOCK(); \
234 1.21 thorpej /* XXX Cast away volatile. */ \
235 1.21 thorpej TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp), \
236 1.21 thorpej lk_list); \
237 1.21 thorpej SPINLOCK_LIST_UNLOCK(); \
238 1.21 thorpej splx(s); \
239 1.21 thorpej } \
240 1.21 thorpej } while (0)
241 1.21 thorpej #else
242 1.21 thorpej #define HAVEIT(lkp) /* nothing */
243 1.21 thorpej
244 1.21 thorpej #define DONTHAVEIT(lkp) /* nothing */
245 1.21 thorpej #endif /* LOCKDEBUG */ /* } */
246 1.21 thorpej
247 1.25 thorpej #if defined(LOCKDEBUG)
248 1.25 thorpej /*
249 1.25 thorpej * Lock debug printing routine; can be configured to print to console
250 1.25 thorpej * or log to syslog.
251 1.25 thorpej */
252 1.25 thorpej void
253 1.25 thorpej #ifdef __STDC__
254 1.25 thorpej lock_printf(const char *fmt, ...)
255 1.25 thorpej #else
256 1.25 thorpej lock_printf(fmt, va_alist)
257 1.25 thorpej char *fmt;
258 1.25 thorpej va_dcl
259 1.25 thorpej #endif
260 1.25 thorpej {
261 1.25 thorpej va_list ap;
262 1.25 thorpej
263 1.25 thorpej va_start(ap, fmt);
264 1.25 thorpej if (lock_debug_syslog)
265 1.25 thorpej vlog(LOG_DEBUG, fmt, ap);
266 1.25 thorpej else
267 1.25 thorpej vprintf(fmt, ap);
268 1.25 thorpej va_end(ap);
269 1.25 thorpej }
270 1.25 thorpej #endif /* LOCKDEBUG */
271 1.25 thorpej
272 1.1 fvdl /*
273 1.1 fvdl * Initialize a lock; required before use.
274 1.1 fvdl */
275 1.1 fvdl void
276 1.1 fvdl lockinit(lkp, prio, wmesg, timo, flags)
277 1.1 fvdl struct lock *lkp;
278 1.1 fvdl int prio;
279 1.4 mycroft const char *wmesg;
280 1.1 fvdl int timo;
281 1.1 fvdl int flags;
282 1.1 fvdl {
283 1.1 fvdl
284 1.8 perry memset(lkp, 0, sizeof(struct lock));
285 1.1 fvdl simple_lock_init(&lkp->lk_interlock);
286 1.1 fvdl lkp->lk_flags = flags & LK_EXTFLG_MASK;
287 1.19 thorpej if (flags & LK_SPIN)
288 1.19 thorpej lkp->lk_cpu = LK_NOCPU;
289 1.19 thorpej else {
290 1.19 thorpej lkp->lk_lockholder = LK_NOPROC;
291 1.19 thorpej lkp->lk_prio = prio;
292 1.19 thorpej lkp->lk_timo = timo;
293 1.19 thorpej }
294 1.19 thorpej lkp->lk_wmesg = wmesg; /* just a name for spin locks */
295 1.1 fvdl }
296 1.1 fvdl
297 1.1 fvdl /*
298 1.1 fvdl * Determine the status of a lock.
299 1.1 fvdl */
300 1.1 fvdl int
301 1.1 fvdl lockstatus(lkp)
302 1.1 fvdl struct lock *lkp;
303 1.1 fvdl {
304 1.1 fvdl int lock_type = 0;
305 1.1 fvdl
306 1.1 fvdl simple_lock(&lkp->lk_interlock);
307 1.1 fvdl if (lkp->lk_exclusivecount != 0)
308 1.1 fvdl lock_type = LK_EXCLUSIVE;
309 1.1 fvdl else if (lkp->lk_sharecount != 0)
310 1.1 fvdl lock_type = LK_SHARED;
311 1.1 fvdl simple_unlock(&lkp->lk_interlock);
312 1.1 fvdl return (lock_type);
313 1.1 fvdl }
314 1.1 fvdl
315 1.1 fvdl /*
316 1.1 fvdl * Set, change, or release a lock.
317 1.1 fvdl *
318 1.1 fvdl * Shared requests increment the shared count. Exclusive requests set the
319 1.1 fvdl * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
320 1.1 fvdl * accepted shared locks and shared-to-exclusive upgrades to go away.
321 1.1 fvdl */
322 1.1 fvdl int
323 1.6 fvdl lockmgr(lkp, flags, interlkp)
324 1.1 fvdl __volatile struct lock *lkp;
325 1.1 fvdl u_int flags;
326 1.1 fvdl struct simplelock *interlkp;
327 1.1 fvdl {
328 1.1 fvdl int error;
329 1.1 fvdl pid_t pid;
330 1.1 fvdl int extflags;
331 1.24 thorpej cpuid_t cpu_id;
332 1.6 fvdl struct proc *p = curproc;
333 1.1 fvdl
334 1.1 fvdl error = 0;
335 1.19 thorpej
336 1.1 fvdl simple_lock(&lkp->lk_interlock);
337 1.1 fvdl if (flags & LK_INTERLOCK)
338 1.1 fvdl simple_unlock(interlkp);
339 1.1 fvdl extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
340 1.19 thorpej
341 1.21 thorpej #ifdef DIAGNOSTIC /* { */
342 1.19 thorpej /*
343 1.19 thorpej * Don't allow spins on sleep locks and don't allow sleeps
344 1.19 thorpej * on spin locks.
345 1.19 thorpej */
346 1.19 thorpej if ((flags ^ lkp->lk_flags) & LK_SPIN)
347 1.19 thorpej panic("lockmgr: sleep/spin mismatch\n");
348 1.21 thorpej #endif /* } */
349 1.19 thorpej
350 1.19 thorpej if (extflags & LK_SPIN)
351 1.19 thorpej pid = LK_KERNPROC;
352 1.19 thorpej else {
353 1.21 thorpej #ifdef DIAGNOSTIC /* { */
354 1.19 thorpej if (p == NULL)
355 1.19 thorpej panic("lockmgr: no context");
356 1.21 thorpej #endif /* } */
357 1.19 thorpej pid = p->p_pid;
358 1.19 thorpej }
359 1.24 thorpej cpu_id = cpu_number();
360 1.19 thorpej
361 1.21 thorpej #ifdef DIAGNOSTIC /* { */
362 1.1 fvdl /*
363 1.1 fvdl * Once a lock has drained, the LK_DRAINING flag is set and an
364 1.1 fvdl * exclusive lock is returned. The only valid operation thereafter
365 1.1 fvdl * is a single release of that exclusive lock. This final release
366 1.1 fvdl * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
367 1.1 fvdl * further requests of any sort will result in a panic. The bits
368 1.1 fvdl * selected for these two flags are chosen so that they will be set
369 1.1 fvdl * in memory that is freed (freed memory is filled with 0xdeadbeef).
370 1.1 fvdl * The final release is permitted to give a new lease on life to
371 1.1 fvdl * the lock by specifying LK_REENABLE.
372 1.1 fvdl */
373 1.1 fvdl if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
374 1.1 fvdl if (lkp->lk_flags & LK_DRAINED)
375 1.1 fvdl panic("lockmgr: using decommissioned lock");
376 1.1 fvdl if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
377 1.19 thorpej WEHOLDIT(lkp, pid, cpu_id) == 0)
378 1.1 fvdl panic("lockmgr: non-release on draining lock: %d\n",
379 1.1 fvdl flags & LK_TYPE_MASK);
380 1.1 fvdl lkp->lk_flags &= ~LK_DRAINING;
381 1.1 fvdl if ((flags & LK_REENABLE) == 0)
382 1.1 fvdl lkp->lk_flags |= LK_DRAINED;
383 1.1 fvdl }
384 1.21 thorpej #endif /* DIAGNOSTIC */ /* } */
385 1.1 fvdl
386 1.1 fvdl switch (flags & LK_TYPE_MASK) {
387 1.1 fvdl
388 1.1 fvdl case LK_SHARED:
389 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
390 1.1 fvdl /*
391 1.1 fvdl * If just polling, check to see if we will block.
392 1.1 fvdl */
393 1.1 fvdl if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
394 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
395 1.1 fvdl error = EBUSY;
396 1.1 fvdl break;
397 1.1 fvdl }
398 1.1 fvdl /*
399 1.1 fvdl * Wait for exclusive locks and upgrades to clear.
400 1.1 fvdl */
401 1.23 thorpej ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
402 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
403 1.1 fvdl if (error)
404 1.1 fvdl break;
405 1.1 fvdl lkp->lk_sharecount++;
406 1.21 thorpej COUNT(lkp, p, cpu_id, 1);
407 1.1 fvdl break;
408 1.1 fvdl }
409 1.1 fvdl /*
410 1.1 fvdl * We hold an exclusive lock, so downgrade it to shared.
411 1.1 fvdl * An alternative would be to fail with EDEADLK.
412 1.1 fvdl */
413 1.1 fvdl lkp->lk_sharecount++;
414 1.21 thorpej COUNT(lkp, p, cpu_id, 1);
415 1.1 fvdl /* fall into downgrade */
416 1.1 fvdl
417 1.1 fvdl case LK_DOWNGRADE:
418 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
419 1.19 thorpej lkp->lk_exclusivecount == 0)
420 1.1 fvdl panic("lockmgr: not holding exclusive lock");
421 1.1 fvdl lkp->lk_sharecount += lkp->lk_exclusivecount;
422 1.1 fvdl lkp->lk_exclusivecount = 0;
423 1.15 fvdl lkp->lk_recurselevel = 0;
424 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
425 1.19 thorpej SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
426 1.21 thorpej DONTHAVEIT(lkp);
427 1.23 thorpej WAKEUP_WAITER(lkp);
428 1.1 fvdl break;
429 1.1 fvdl
430 1.1 fvdl case LK_EXCLUPGRADE:
431 1.1 fvdl /*
432 1.1 fvdl * If another process is ahead of us to get an upgrade,
433 1.1 fvdl * then we want to fail rather than have an intervening
434 1.1 fvdl * exclusive access.
435 1.1 fvdl */
436 1.1 fvdl if (lkp->lk_flags & LK_WANT_UPGRADE) {
437 1.1 fvdl lkp->lk_sharecount--;
438 1.21 thorpej COUNT(lkp, p, cpu_id, -1);
439 1.1 fvdl error = EBUSY;
440 1.1 fvdl break;
441 1.1 fvdl }
442 1.1 fvdl /* fall into normal upgrade */
443 1.1 fvdl
444 1.1 fvdl case LK_UPGRADE:
445 1.1 fvdl /*
446 1.1 fvdl * Upgrade a shared lock to an exclusive one. If another
447 1.1 fvdl * shared lock has already requested an upgrade to an
448 1.1 fvdl * exclusive lock, our shared lock is released and an
449 1.1 fvdl * exclusive lock is requested (which will be granted
450 1.1 fvdl * after the upgrade). If we return an error, the file
451 1.1 fvdl * will always be unlocked.
452 1.1 fvdl */
453 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
454 1.1 fvdl panic("lockmgr: upgrade exclusive lock");
455 1.1 fvdl lkp->lk_sharecount--;
456 1.21 thorpej COUNT(lkp, p, cpu_id, -1);
457 1.1 fvdl /*
458 1.1 fvdl * If we are just polling, check to see if we will block.
459 1.1 fvdl */
460 1.1 fvdl if ((extflags & LK_NOWAIT) &&
461 1.1 fvdl ((lkp->lk_flags & LK_WANT_UPGRADE) ||
462 1.1 fvdl lkp->lk_sharecount > 1)) {
463 1.1 fvdl error = EBUSY;
464 1.1 fvdl break;
465 1.1 fvdl }
466 1.1 fvdl if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
467 1.1 fvdl /*
468 1.1 fvdl * We are first shared lock to request an upgrade, so
469 1.1 fvdl * request upgrade and wait for the shared count to
470 1.1 fvdl * drop to zero, then take exclusive lock.
471 1.1 fvdl */
472 1.1 fvdl lkp->lk_flags |= LK_WANT_UPGRADE;
473 1.23 thorpej ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
474 1.1 fvdl lkp->lk_flags &= ~LK_WANT_UPGRADE;
475 1.1 fvdl if (error)
476 1.1 fvdl break;
477 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
478 1.19 thorpej SETHOLDER(lkp, pid, cpu_id);
479 1.21 thorpej HAVEIT(lkp);
480 1.1 fvdl if (lkp->lk_exclusivecount != 0)
481 1.1 fvdl panic("lockmgr: non-zero exclusive count");
482 1.1 fvdl lkp->lk_exclusivecount = 1;
483 1.15 fvdl if (extflags & LK_SETRECURSE)
484 1.15 fvdl lkp->lk_recurselevel = 1;
485 1.21 thorpej COUNT(lkp, p, cpu_id, 1);
486 1.1 fvdl break;
487 1.1 fvdl }
488 1.1 fvdl /*
489 1.1 fvdl * Someone else has requested upgrade. Release our shared
490 1.1 fvdl * lock, awaken upgrade requestor if we are the last shared
491 1.1 fvdl * lock, then request an exclusive lock.
492 1.1 fvdl */
493 1.23 thorpej if (lkp->lk_sharecount == 0)
494 1.23 thorpej WAKEUP_WAITER(lkp);
495 1.1 fvdl /* fall into exclusive request */
496 1.1 fvdl
497 1.1 fvdl case LK_EXCLUSIVE:
498 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id)) {
499 1.1 fvdl /*
500 1.19 thorpej * Recursive lock.
501 1.1 fvdl */
502 1.15 fvdl if ((extflags & LK_CANRECURSE) == 0 &&
503 1.16 sommerfe lkp->lk_recurselevel == 0) {
504 1.16 sommerfe if (extflags & LK_RECURSEFAIL) {
505 1.16 sommerfe error = EDEADLK;
506 1.16 sommerfe break;
507 1.16 sommerfe } else
508 1.16 sommerfe panic("lockmgr: locking against myself");
509 1.16 sommerfe }
510 1.1 fvdl lkp->lk_exclusivecount++;
511 1.15 fvdl if (extflags & LK_SETRECURSE &&
512 1.15 fvdl lkp->lk_recurselevel == 0)
513 1.15 fvdl lkp->lk_recurselevel = lkp->lk_exclusivecount;
514 1.21 thorpej COUNT(lkp, p, cpu_id, 1);
515 1.1 fvdl break;
516 1.1 fvdl }
517 1.1 fvdl /*
518 1.1 fvdl * If we are just polling, check to see if we will sleep.
519 1.1 fvdl */
520 1.1 fvdl if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
521 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
522 1.1 fvdl lkp->lk_sharecount != 0)) {
523 1.1 fvdl error = EBUSY;
524 1.1 fvdl break;
525 1.1 fvdl }
526 1.1 fvdl /*
527 1.1 fvdl * Try to acquire the want_exclusive flag.
528 1.1 fvdl */
529 1.23 thorpej ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
530 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL));
531 1.1 fvdl if (error)
532 1.1 fvdl break;
533 1.1 fvdl lkp->lk_flags |= LK_WANT_EXCL;
534 1.1 fvdl /*
535 1.1 fvdl * Wait for shared locks and upgrades to finish.
536 1.1 fvdl */
537 1.23 thorpej ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
538 1.1 fvdl (lkp->lk_flags & LK_WANT_UPGRADE));
539 1.1 fvdl lkp->lk_flags &= ~LK_WANT_EXCL;
540 1.1 fvdl if (error)
541 1.1 fvdl break;
542 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
543 1.19 thorpej SETHOLDER(lkp, pid, cpu_id);
544 1.21 thorpej HAVEIT(lkp);
545 1.1 fvdl if (lkp->lk_exclusivecount != 0)
546 1.1 fvdl panic("lockmgr: non-zero exclusive count");
547 1.1 fvdl lkp->lk_exclusivecount = 1;
548 1.15 fvdl if (extflags & LK_SETRECURSE)
549 1.15 fvdl lkp->lk_recurselevel = 1;
550 1.21 thorpej COUNT(lkp, p, cpu_id, 1);
551 1.1 fvdl break;
552 1.1 fvdl
553 1.1 fvdl case LK_RELEASE:
554 1.1 fvdl if (lkp->lk_exclusivecount != 0) {
555 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
556 1.19 thorpej if (lkp->lk_flags & LK_SPIN) {
557 1.19 thorpej panic("lockmgr: processor %lu, not "
558 1.19 thorpej "exclusive lock holder %lu "
559 1.19 thorpej "unlocking", cpu_id, lkp->lk_cpu);
560 1.19 thorpej } else {
561 1.19 thorpej panic("lockmgr: pid %d, not "
562 1.19 thorpej "exclusive lock holder %d "
563 1.19 thorpej "unlocking", pid,
564 1.19 thorpej lkp->lk_lockholder);
565 1.19 thorpej }
566 1.19 thorpej }
567 1.15 fvdl if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
568 1.15 fvdl lkp->lk_recurselevel = 0;
569 1.1 fvdl lkp->lk_exclusivecount--;
570 1.21 thorpej COUNT(lkp, p, cpu_id, -1);
571 1.1 fvdl if (lkp->lk_exclusivecount == 0) {
572 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
573 1.19 thorpej SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
574 1.21 thorpej DONTHAVEIT(lkp);
575 1.1 fvdl }
576 1.1 fvdl } else if (lkp->lk_sharecount != 0) {
577 1.1 fvdl lkp->lk_sharecount--;
578 1.21 thorpej COUNT(lkp, p, cpu_id, -1);
579 1.1 fvdl }
580 1.23 thorpej WAKEUP_WAITER(lkp);
581 1.1 fvdl break;
582 1.1 fvdl
583 1.1 fvdl case LK_DRAIN:
584 1.1 fvdl /*
585 1.1 fvdl * Check that we do not already hold the lock, as it can
586 1.1 fvdl * never drain if we do. Unfortunately, we have no way to
587 1.1 fvdl * check for holding a shared lock, but at least we can
588 1.1 fvdl * check for an exclusive one.
589 1.1 fvdl */
590 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id))
591 1.1 fvdl panic("lockmgr: draining against myself");
592 1.1 fvdl /*
593 1.1 fvdl * If we are just polling, check to see if we will sleep.
594 1.1 fvdl */
595 1.1 fvdl if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
596 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
597 1.1 fvdl lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
598 1.1 fvdl error = EBUSY;
599 1.1 fvdl break;
600 1.1 fvdl }
601 1.23 thorpej ACQUIRE(lkp, error, extflags, 1,
602 1.23 thorpej ((lkp->lk_flags &
603 1.23 thorpej (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
604 1.23 thorpej lkp->lk_sharecount != 0 ||
605 1.23 thorpej lkp->lk_waitcount != 0));
606 1.23 thorpej if (error)
607 1.23 thorpej break;
608 1.1 fvdl lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
609 1.19 thorpej SETHOLDER(lkp, pid, cpu_id);
610 1.21 thorpej HAVEIT(lkp);
611 1.1 fvdl lkp->lk_exclusivecount = 1;
612 1.15 fvdl /* XXX unlikely that we'd want this */
613 1.15 fvdl if (extflags & LK_SETRECURSE)
614 1.15 fvdl lkp->lk_recurselevel = 1;
615 1.21 thorpej COUNT(lkp, p, cpu_id, 1);
616 1.1 fvdl break;
617 1.1 fvdl
618 1.1 fvdl default:
619 1.1 fvdl simple_unlock(&lkp->lk_interlock);
620 1.1 fvdl panic("lockmgr: unknown locktype request %d",
621 1.1 fvdl flags & LK_TYPE_MASK);
622 1.1 fvdl /* NOTREACHED */
623 1.1 fvdl }
624 1.23 thorpej if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
625 1.23 thorpej ((lkp->lk_flags &
626 1.23 thorpej (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
627 1.1 fvdl lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
628 1.1 fvdl lkp->lk_flags &= ~LK_WAITDRAIN;
629 1.20 thorpej wakeup_one((void *)&lkp->lk_flags);
630 1.1 fvdl }
631 1.1 fvdl simple_unlock(&lkp->lk_interlock);
632 1.1 fvdl return (error);
633 1.1 fvdl }
634 1.1 fvdl
635 1.1 fvdl /*
636 1.1 fvdl * Print out information about state of a lock. Used by VOP_PRINT
637 1.1 fvdl * routines to display ststus about contained locks.
638 1.1 fvdl */
639 1.2 fvdl void
640 1.1 fvdl lockmgr_printinfo(lkp)
641 1.19 thorpej __volatile struct lock *lkp;
642 1.1 fvdl {
643 1.1 fvdl
644 1.1 fvdl if (lkp->lk_sharecount)
645 1.1 fvdl printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
646 1.1 fvdl lkp->lk_sharecount);
647 1.19 thorpej else if (lkp->lk_flags & LK_HAVE_EXCL) {
648 1.19 thorpej printf(" lock type %s: EXCL (count %d) by ",
649 1.19 thorpej lkp->lk_wmesg, lkp->lk_exclusivecount);
650 1.19 thorpej if (lkp->lk_flags & LK_SPIN)
651 1.19 thorpej printf("processor %lu", lkp->lk_cpu);
652 1.19 thorpej else
653 1.19 thorpej printf("pid %d", lkp->lk_lockholder);
654 1.19 thorpej } else
655 1.19 thorpej printf(" not locked");
656 1.19 thorpej if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
657 1.1 fvdl printf(" with %d pending", lkp->lk_waitcount);
658 1.1 fvdl }
659 1.1 fvdl
660 1.21 thorpej #if defined(LOCKDEBUG) /* { */
661 1.21 thorpej TAILQ_HEAD(, simplelock) simplelock_list =
662 1.21 thorpej TAILQ_HEAD_INITIALIZER(simplelock_list);
663 1.21 thorpej
664 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
665 1.21 thorpej struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
666 1.21 thorpej
667 1.21 thorpej #define SLOCK_LIST_LOCK() \
668 1.21 thorpej cpu_simple_lock(&simplelock_list_slock)
669 1.21 thorpej
670 1.21 thorpej #define SLOCK_LIST_UNLOCK() \
671 1.21 thorpej cpu_simple_unlock(&simplelock_list_slock)
672 1.21 thorpej
673 1.21 thorpej #define SLOCK_COUNT(x) \
674 1.21 thorpej /* atomic_add_ulong(&curcpu()->ci_simple_locks, (x)) */
675 1.21 thorpej #else
676 1.21 thorpej u_long simple_locks;
677 1.21 thorpej
678 1.21 thorpej #define SLOCK_LIST_LOCK() /* nothing */
679 1.21 thorpej
680 1.21 thorpej #define SLOCK_LIST_UNLOCK() /* nothing */
681 1.21 thorpej
682 1.21 thorpej #define SLOCK_COUNT(x) simple_locks += (x)
683 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
684 1.21 thorpej
685 1.21 thorpej #ifdef DDB /* { */
686 1.18 chs int simple_lock_debugger = 0;
687 1.21 thorpej #define SLOCK_DEBUGGER() if (simple_lock_debugger) Debugger()
688 1.21 thorpej #else
689 1.21 thorpej #define SLOCK_DEBUGGER() /* nothing */
690 1.21 thorpej #endif /* } */
691 1.21 thorpej
692 1.21 thorpej #define SLOCK_WHERE(str, alp, id, l) \
693 1.21 thorpej do { \
694 1.25 thorpej lock_printf(str); \
695 1.25 thorpej lock_printf("currently at: %s:%d\n", (id), (l)); \
696 1.21 thorpej if ((alp)->lock_file != NULL) \
697 1.25 thorpej lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
698 1.21 thorpej (alp)->lock_line); \
699 1.21 thorpej if ((alp)->unlock_file != NULL) \
700 1.25 thorpej lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
701 1.21 thorpej (alp)->unlock_line); \
702 1.21 thorpej SLOCK_DEBUGGER(); \
703 1.21 thorpej } while (0)
704 1.12 chs
705 1.1 fvdl /*
706 1.1 fvdl * Simple lock functions so that the debugger can see from whence
707 1.1 fvdl * they are being called.
708 1.1 fvdl */
709 1.1 fvdl void
710 1.1 fvdl simple_lock_init(alp)
711 1.1 fvdl struct simplelock *alp;
712 1.1 fvdl {
713 1.21 thorpej
714 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
715 1.21 thorpej cpu_simple_lock_init(alp);
716 1.21 thorpej #else
717 1.21 thorpej alp->lock_data = SIMPLELOCK_UNLOCKED;
718 1.21 thorpej #endif /* } */
719 1.5 chs alp->lock_file = NULL;
720 1.5 chs alp->lock_line = 0;
721 1.5 chs alp->unlock_file = NULL;
722 1.5 chs alp->unlock_line = 0;
723 1.10 thorpej alp->lock_holder = 0;
724 1.1 fvdl }
725 1.1 fvdl
726 1.1 fvdl void
727 1.1 fvdl _simple_lock(alp, id, l)
728 1.1 fvdl __volatile struct simplelock *alp;
729 1.1 fvdl const char *id;
730 1.1 fvdl int l;
731 1.1 fvdl {
732 1.24 thorpej cpuid_t cpu_id = cpu_number();
733 1.12 chs int s;
734 1.12 chs
735 1.18 chs s = splhigh();
736 1.21 thorpej
737 1.21 thorpej /*
738 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
739 1.21 thorpej * don't take any action, and just fall into the normal spin case.
740 1.21 thorpej */
741 1.21 thorpej if (alp->lock_data == SIMPLELOCK_LOCKED) {
742 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
743 1.21 thorpej if (alp->lock_holder == cpu_id) {
744 1.21 thorpej SLOCK_WHERE("simple_lock: locking against myself\n",
745 1.21 thorpej alp, id, l);
746 1.21 thorpej goto out;
747 1.1 fvdl }
748 1.21 thorpej #else
749 1.21 thorpej SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
750 1.21 thorpej goto out;
751 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
752 1.1 fvdl }
753 1.21 thorpej
754 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
755 1.21 thorpej /* Acquire the lock before modifying any fields. */
756 1.21 thorpej cpu_simple_lock(alp);
757 1.21 thorpej #else
758 1.21 thorpej alp->lock_data = SIMPLELOCK_LOCKED;
759 1.21 thorpej #endif /* } */
760 1.21 thorpej
761 1.5 chs alp->lock_file = id;
762 1.5 chs alp->lock_line = l;
763 1.21 thorpej alp->lock_holder = cpu_id;
764 1.21 thorpej
765 1.21 thorpej SLOCK_LIST_LOCK();
766 1.21 thorpej /* XXX Cast away volatile */
767 1.21 thorpej TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
768 1.21 thorpej SLOCK_LIST_UNLOCK();
769 1.21 thorpej
770 1.21 thorpej SLOCK_COUNT(1);
771 1.21 thorpej
772 1.21 thorpej out:
773 1.18 chs splx(s);
774 1.1 fvdl }
775 1.1 fvdl
776 1.1 fvdl int
777 1.1 fvdl _simple_lock_try(alp, id, l)
778 1.1 fvdl __volatile struct simplelock *alp;
779 1.1 fvdl const char *id;
780 1.1 fvdl int l;
781 1.1 fvdl {
782 1.24 thorpej cpuid_t cpu_id = cpu_number();
783 1.21 thorpej int s, rv = 0;
784 1.1 fvdl
785 1.18 chs s = splhigh();
786 1.21 thorpej
787 1.21 thorpej /*
788 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
789 1.21 thorpej * don't take any action.
790 1.21 thorpej */
791 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
792 1.21 thorpej if ((rv = cpu_simple_lock_try(alp)) == 0) {
793 1.21 thorpej if (alp->lock_holder == cpu_id)
794 1.21 thorpej SLOCK_WHERE("simple_lock_try: locking against myself\n",
795 1.21 thorpej alp, id l);
796 1.21 thorpej goto out;
797 1.21 thorpej }
798 1.21 thorpej #else
799 1.21 thorpej if (alp->lock_data == SIMPLELOCK_LOCKED) {
800 1.21 thorpej SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
801 1.21 thorpej goto out;
802 1.18 chs }
803 1.21 thorpej alp->lock_data = SIMPLELOCK_LOCKED;
804 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
805 1.21 thorpej
806 1.21 thorpej /*
807 1.21 thorpej * At this point, we have acquired the lock.
808 1.21 thorpej */
809 1.21 thorpej
810 1.21 thorpej rv = 1;
811 1.18 chs
812 1.5 chs alp->lock_file = id;
813 1.5 chs alp->lock_line = l;
814 1.21 thorpej alp->lock_holder = cpu_id;
815 1.21 thorpej
816 1.21 thorpej SLOCK_LIST_LOCK();
817 1.21 thorpej /* XXX Cast away volatile. */
818 1.21 thorpej TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
819 1.21 thorpej SLOCK_LIST_UNLOCK();
820 1.21 thorpej
821 1.21 thorpej SLOCK_COUNT(1);
822 1.21 thorpej
823 1.21 thorpej out:
824 1.12 chs splx(s);
825 1.21 thorpej return (rv);
826 1.1 fvdl }
827 1.1 fvdl
828 1.1 fvdl void
829 1.1 fvdl _simple_unlock(alp, id, l)
830 1.1 fvdl __volatile struct simplelock *alp;
831 1.1 fvdl const char *id;
832 1.1 fvdl int l;
833 1.1 fvdl {
834 1.12 chs int s;
835 1.1 fvdl
836 1.18 chs s = splhigh();
837 1.21 thorpej
838 1.21 thorpej /*
839 1.21 thorpej * MULTIPROCESSOR case: This is `safe' because we think we hold
840 1.21 thorpej * the lock, and if we don't, we don't take any action.
841 1.21 thorpej */
842 1.21 thorpej if (alp->lock_data == SIMPLELOCK_UNLOCKED) {
843 1.21 thorpej SLOCK_WHERE("simple_unlock: lock not held\n",
844 1.21 thorpej alp, id, l);
845 1.21 thorpej goto out;
846 1.21 thorpej }
847 1.21 thorpej
848 1.21 thorpej SLOCK_LIST_LOCK();
849 1.21 thorpej TAILQ_REMOVE(&simplelock_list, alp, list);
850 1.21 thorpej SLOCK_LIST_UNLOCK();
851 1.21 thorpej
852 1.21 thorpej SLOCK_COUNT(-1);
853 1.21 thorpej
854 1.21 thorpej alp->list.tqe_next = NULL; /* sanity */
855 1.21 thorpej alp->list.tqe_prev = NULL; /* sanity */
856 1.21 thorpej
857 1.5 chs alp->unlock_file = id;
858 1.5 chs alp->unlock_line = l;
859 1.21 thorpej
860 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
861 1.21 thorpej /* Now that we've modified all fields, release the lock. */
862 1.21 thorpej cpu_simple_unlock(alp);
863 1.21 thorpej #else
864 1.21 thorpej alp->lock_data = SIMPLELOCK_UNLOCKED;
865 1.21 thorpej #endif /* } */
866 1.21 thorpej
867 1.21 thorpej out:
868 1.18 chs splx(s);
869 1.12 chs }
870 1.12 chs
871 1.12 chs void
872 1.12 chs simple_lock_dump()
873 1.12 chs {
874 1.12 chs struct simplelock *alp;
875 1.12 chs int s;
876 1.12 chs
877 1.12 chs s = splhigh();
878 1.21 thorpej SLOCK_LIST_LOCK();
879 1.25 thorpej lock_printf("all simple locks:\n");
880 1.21 thorpej for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
881 1.21 thorpej alp = TAILQ_NEXT(alp, list)) {
882 1.25 thorpej lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
883 1.21 thorpej alp->lock_file, alp->lock_line);
884 1.12 chs }
885 1.21 thorpej SLOCK_LIST_UNLOCK();
886 1.12 chs splx(s);
887 1.12 chs }
888 1.12 chs
889 1.12 chs void
890 1.12 chs simple_lock_freecheck(start, end)
891 1.21 thorpej void *start, *end;
892 1.12 chs {
893 1.12 chs struct simplelock *alp;
894 1.12 chs int s;
895 1.12 chs
896 1.12 chs s = splhigh();
897 1.21 thorpej SLOCK_LIST_LOCK();
898 1.21 thorpej for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
899 1.21 thorpej alp = TAILQ_NEXT(alp, list)) {
900 1.12 chs if ((void *)alp >= start && (void *)alp < end) {
901 1.25 thorpej lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
902 1.21 thorpej alp, alp->lock_holder, alp->lock_file,
903 1.21 thorpej alp->lock_line);
904 1.21 thorpej SLOCK_DEBUGGER();
905 1.12 chs }
906 1.12 chs }
907 1.21 thorpej SLOCK_LIST_UNLOCK();
908 1.12 chs splx(s);
909 1.1 fvdl }
910 1.21 thorpej #endif /* LOCKDEBUG */ /* } */
911