kern_lock.c revision 1.19 1 1.19 thorpej /* $NetBSD: kern_lock.c,v 1.19 1999/07/25 06:24:23 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.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.1 fvdl /*
92 1.1 fvdl * Locking primitives implementation.
93 1.1 fvdl * Locks provide shared/exclusive sychronization.
94 1.1 fvdl */
95 1.1 fvdl
96 1.17 sommerfe #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
97 1.1 fvdl #define COUNT(p, x) if (p) (p)->p_locks += (x)
98 1.1 fvdl #else
99 1.1 fvdl #define COUNT(p, x)
100 1.1 fvdl #endif
101 1.1 fvdl
102 1.1 fvdl /*
103 1.1 fvdl * Acquire a resource.
104 1.1 fvdl */
105 1.1 fvdl #define ACQUIRE(lkp, error, extflags, wanted) \
106 1.19 thorpej if ((extflags) & LK_SPIN) { \
107 1.19 thorpej int interlocked; \
108 1.19 thorpej \
109 1.19 thorpej for (interlocked = 1;;) { \
110 1.19 thorpej if (wanted) { \
111 1.19 thorpej if (interlocked) { \
112 1.19 thorpej simple_unlock(&(lkp)->lk_interlock); \
113 1.19 thorpej interlocked = 0; \
114 1.19 thorpej } \
115 1.19 thorpej } else if (interlocked) { \
116 1.19 thorpej break; \
117 1.19 thorpej } else { \
118 1.19 thorpej simple_lock(&(lkp)->lk_interlock); \
119 1.19 thorpej interlocked = 1; \
120 1.19 thorpej } \
121 1.19 thorpej } \
122 1.19 thorpej KASSERT((wanted) == 0); \
123 1.19 thorpej error = 0; /* sanity */ \
124 1.19 thorpej } else { \
125 1.19 thorpej for (error = 0; wanted; ) { \
126 1.19 thorpej (lkp)->lk_waitcount++; \
127 1.19 thorpej simple_unlock(&(lkp)->lk_interlock); \
128 1.19 thorpej error = tsleep((void *)lkp, (lkp)->lk_prio, \
129 1.19 thorpej (lkp)->lk_wmesg, (lkp)->lk_timo); \
130 1.19 thorpej simple_lock(&(lkp)->lk_interlock); \
131 1.19 thorpej (lkp)->lk_waitcount--; \
132 1.19 thorpej if (error) \
133 1.19 thorpej break; \
134 1.19 thorpej if ((extflags) & LK_SLEEPFAIL) { \
135 1.19 thorpej error = ENOLCK; \
136 1.19 thorpej break; \
137 1.19 thorpej } \
138 1.1 fvdl } \
139 1.1 fvdl }
140 1.1 fvdl
141 1.19 thorpej #define SETHOLDER(lkp, pid, cpu_id) \
142 1.19 thorpej do { \
143 1.19 thorpej if ((lkp)->lk_flags & LK_SPIN) \
144 1.19 thorpej (lkp)->lk_cpu = cpu_id; \
145 1.19 thorpej else \
146 1.19 thorpej (lkp)->lk_lockholder = pid; \
147 1.19 thorpej } while (0)
148 1.19 thorpej
149 1.19 thorpej #define WEHOLDIT(lkp, pid, cpu_id) \
150 1.19 thorpej (((lkp)->lk_flags & LK_SPIN) != 0 ? \
151 1.19 thorpej ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
152 1.19 thorpej
153 1.1 fvdl /*
154 1.1 fvdl * Initialize a lock; required before use.
155 1.1 fvdl */
156 1.1 fvdl void
157 1.1 fvdl lockinit(lkp, prio, wmesg, timo, flags)
158 1.1 fvdl struct lock *lkp;
159 1.1 fvdl int prio;
160 1.4 mycroft const char *wmesg;
161 1.1 fvdl int timo;
162 1.1 fvdl int flags;
163 1.1 fvdl {
164 1.1 fvdl
165 1.8 perry memset(lkp, 0, sizeof(struct lock));
166 1.1 fvdl simple_lock_init(&lkp->lk_interlock);
167 1.1 fvdl lkp->lk_flags = flags & LK_EXTFLG_MASK;
168 1.19 thorpej if (flags & LK_SPIN)
169 1.19 thorpej lkp->lk_cpu = LK_NOCPU;
170 1.19 thorpej else {
171 1.19 thorpej lkp->lk_lockholder = LK_NOPROC;
172 1.19 thorpej lkp->lk_prio = prio;
173 1.19 thorpej lkp->lk_timo = timo;
174 1.19 thorpej }
175 1.19 thorpej lkp->lk_wmesg = wmesg; /* just a name for spin locks */
176 1.1 fvdl }
177 1.1 fvdl
178 1.1 fvdl /*
179 1.1 fvdl * Determine the status of a lock.
180 1.1 fvdl */
181 1.1 fvdl int
182 1.1 fvdl lockstatus(lkp)
183 1.1 fvdl struct lock *lkp;
184 1.1 fvdl {
185 1.1 fvdl int lock_type = 0;
186 1.1 fvdl
187 1.1 fvdl simple_lock(&lkp->lk_interlock);
188 1.1 fvdl if (lkp->lk_exclusivecount != 0)
189 1.1 fvdl lock_type = LK_EXCLUSIVE;
190 1.1 fvdl else if (lkp->lk_sharecount != 0)
191 1.1 fvdl lock_type = LK_SHARED;
192 1.1 fvdl simple_unlock(&lkp->lk_interlock);
193 1.1 fvdl return (lock_type);
194 1.1 fvdl }
195 1.1 fvdl
196 1.1 fvdl /*
197 1.1 fvdl * Set, change, or release a lock.
198 1.1 fvdl *
199 1.1 fvdl * Shared requests increment the shared count. Exclusive requests set the
200 1.1 fvdl * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
201 1.1 fvdl * accepted shared locks and shared-to-exclusive upgrades to go away.
202 1.1 fvdl */
203 1.1 fvdl int
204 1.6 fvdl lockmgr(lkp, flags, interlkp)
205 1.1 fvdl __volatile struct lock *lkp;
206 1.1 fvdl u_int flags;
207 1.1 fvdl struct simplelock *interlkp;
208 1.1 fvdl {
209 1.1 fvdl int error;
210 1.1 fvdl pid_t pid;
211 1.1 fvdl int extflags;
212 1.19 thorpej u_long cpu_id;
213 1.6 fvdl struct proc *p = curproc;
214 1.1 fvdl
215 1.1 fvdl error = 0;
216 1.19 thorpej
217 1.1 fvdl simple_lock(&lkp->lk_interlock);
218 1.1 fvdl if (flags & LK_INTERLOCK)
219 1.1 fvdl simple_unlock(interlkp);
220 1.1 fvdl extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
221 1.19 thorpej
222 1.19 thorpej #ifdef DIAGNOSTIC
223 1.19 thorpej /*
224 1.19 thorpej * Don't allow spins on sleep locks and don't allow sleeps
225 1.19 thorpej * on spin locks.
226 1.19 thorpej */
227 1.19 thorpej if ((flags ^ lkp->lk_flags) & LK_SPIN)
228 1.19 thorpej panic("lockmgr: sleep/spin mismatch\n");
229 1.19 thorpej #endif
230 1.19 thorpej
231 1.19 thorpej if (extflags & LK_SPIN)
232 1.19 thorpej pid = LK_KERNPROC;
233 1.19 thorpej else {
234 1.19 thorpej #ifdef DIAGNOSTIC
235 1.19 thorpej if (p == NULL)
236 1.19 thorpej panic("lockmgr: no context");
237 1.19 thorpej #endif
238 1.19 thorpej pid = p->p_pid;
239 1.19 thorpej }
240 1.19 thorpej cpu_id = 0; /* XXX cpu_number() XXX */
241 1.19 thorpej
242 1.1 fvdl #ifdef DIAGNOSTIC
243 1.1 fvdl /*
244 1.1 fvdl * Once a lock has drained, the LK_DRAINING flag is set and an
245 1.1 fvdl * exclusive lock is returned. The only valid operation thereafter
246 1.1 fvdl * is a single release of that exclusive lock. This final release
247 1.1 fvdl * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
248 1.1 fvdl * further requests of any sort will result in a panic. The bits
249 1.1 fvdl * selected for these two flags are chosen so that they will be set
250 1.1 fvdl * in memory that is freed (freed memory is filled with 0xdeadbeef).
251 1.1 fvdl * The final release is permitted to give a new lease on life to
252 1.1 fvdl * the lock by specifying LK_REENABLE.
253 1.1 fvdl */
254 1.1 fvdl if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
255 1.1 fvdl if (lkp->lk_flags & LK_DRAINED)
256 1.1 fvdl panic("lockmgr: using decommissioned lock");
257 1.1 fvdl if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
258 1.19 thorpej WEHOLDIT(lkp, pid, cpu_id) == 0)
259 1.1 fvdl panic("lockmgr: non-release on draining lock: %d\n",
260 1.1 fvdl flags & LK_TYPE_MASK);
261 1.1 fvdl lkp->lk_flags &= ~LK_DRAINING;
262 1.1 fvdl if ((flags & LK_REENABLE) == 0)
263 1.1 fvdl lkp->lk_flags |= LK_DRAINED;
264 1.1 fvdl }
265 1.19 thorpej #endif /* DIAGNOSTIC */
266 1.1 fvdl
267 1.1 fvdl switch (flags & LK_TYPE_MASK) {
268 1.1 fvdl
269 1.1 fvdl case LK_SHARED:
270 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
271 1.1 fvdl /*
272 1.1 fvdl * If just polling, check to see if we will block.
273 1.1 fvdl */
274 1.1 fvdl if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
275 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
276 1.1 fvdl error = EBUSY;
277 1.1 fvdl break;
278 1.1 fvdl }
279 1.1 fvdl /*
280 1.1 fvdl * Wait for exclusive locks and upgrades to clear.
281 1.1 fvdl */
282 1.1 fvdl ACQUIRE(lkp, error, extflags, lkp->lk_flags &
283 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
284 1.1 fvdl if (error)
285 1.1 fvdl break;
286 1.1 fvdl lkp->lk_sharecount++;
287 1.1 fvdl COUNT(p, 1);
288 1.1 fvdl break;
289 1.1 fvdl }
290 1.1 fvdl /*
291 1.1 fvdl * We hold an exclusive lock, so downgrade it to shared.
292 1.1 fvdl * An alternative would be to fail with EDEADLK.
293 1.1 fvdl */
294 1.1 fvdl lkp->lk_sharecount++;
295 1.1 fvdl COUNT(p, 1);
296 1.1 fvdl /* fall into downgrade */
297 1.1 fvdl
298 1.1 fvdl case LK_DOWNGRADE:
299 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
300 1.19 thorpej lkp->lk_exclusivecount == 0)
301 1.1 fvdl panic("lockmgr: not holding exclusive lock");
302 1.1 fvdl lkp->lk_sharecount += lkp->lk_exclusivecount;
303 1.1 fvdl lkp->lk_exclusivecount = 0;
304 1.15 fvdl lkp->lk_recurselevel = 0;
305 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
306 1.19 thorpej SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
307 1.1 fvdl if (lkp->lk_waitcount)
308 1.1 fvdl wakeup((void *)lkp);
309 1.1 fvdl break;
310 1.1 fvdl
311 1.1 fvdl case LK_EXCLUPGRADE:
312 1.1 fvdl /*
313 1.1 fvdl * If another process is ahead of us to get an upgrade,
314 1.1 fvdl * then we want to fail rather than have an intervening
315 1.1 fvdl * exclusive access.
316 1.1 fvdl */
317 1.1 fvdl if (lkp->lk_flags & LK_WANT_UPGRADE) {
318 1.1 fvdl lkp->lk_sharecount--;
319 1.1 fvdl COUNT(p, -1);
320 1.1 fvdl error = EBUSY;
321 1.1 fvdl break;
322 1.1 fvdl }
323 1.1 fvdl /* fall into normal upgrade */
324 1.1 fvdl
325 1.1 fvdl case LK_UPGRADE:
326 1.1 fvdl /*
327 1.1 fvdl * Upgrade a shared lock to an exclusive one. If another
328 1.1 fvdl * shared lock has already requested an upgrade to an
329 1.1 fvdl * exclusive lock, our shared lock is released and an
330 1.1 fvdl * exclusive lock is requested (which will be granted
331 1.1 fvdl * after the upgrade). If we return an error, the file
332 1.1 fvdl * will always be unlocked.
333 1.1 fvdl */
334 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
335 1.1 fvdl panic("lockmgr: upgrade exclusive lock");
336 1.1 fvdl lkp->lk_sharecount--;
337 1.1 fvdl COUNT(p, -1);
338 1.1 fvdl /*
339 1.1 fvdl * If we are just polling, check to see if we will block.
340 1.1 fvdl */
341 1.1 fvdl if ((extflags & LK_NOWAIT) &&
342 1.1 fvdl ((lkp->lk_flags & LK_WANT_UPGRADE) ||
343 1.1 fvdl lkp->lk_sharecount > 1)) {
344 1.1 fvdl error = EBUSY;
345 1.1 fvdl break;
346 1.1 fvdl }
347 1.1 fvdl if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
348 1.1 fvdl /*
349 1.1 fvdl * We are first shared lock to request an upgrade, so
350 1.1 fvdl * request upgrade and wait for the shared count to
351 1.1 fvdl * drop to zero, then take exclusive lock.
352 1.1 fvdl */
353 1.1 fvdl lkp->lk_flags |= LK_WANT_UPGRADE;
354 1.1 fvdl ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
355 1.1 fvdl lkp->lk_flags &= ~LK_WANT_UPGRADE;
356 1.1 fvdl if (error)
357 1.1 fvdl break;
358 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
359 1.19 thorpej SETHOLDER(lkp, pid, cpu_id);
360 1.1 fvdl if (lkp->lk_exclusivecount != 0)
361 1.1 fvdl panic("lockmgr: non-zero exclusive count");
362 1.1 fvdl lkp->lk_exclusivecount = 1;
363 1.15 fvdl if (extflags & LK_SETRECURSE)
364 1.15 fvdl lkp->lk_recurselevel = 1;
365 1.1 fvdl COUNT(p, 1);
366 1.1 fvdl break;
367 1.1 fvdl }
368 1.1 fvdl /*
369 1.1 fvdl * Someone else has requested upgrade. Release our shared
370 1.1 fvdl * lock, awaken upgrade requestor if we are the last shared
371 1.1 fvdl * lock, then request an exclusive lock.
372 1.1 fvdl */
373 1.1 fvdl if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
374 1.1 fvdl wakeup((void *)lkp);
375 1.1 fvdl /* fall into exclusive request */
376 1.1 fvdl
377 1.1 fvdl case LK_EXCLUSIVE:
378 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id)) {
379 1.1 fvdl /*
380 1.19 thorpej * Recursive lock.
381 1.1 fvdl */
382 1.15 fvdl if ((extflags & LK_CANRECURSE) == 0 &&
383 1.16 sommerfe lkp->lk_recurselevel == 0) {
384 1.16 sommerfe if (extflags & LK_RECURSEFAIL) {
385 1.16 sommerfe error = EDEADLK;
386 1.16 sommerfe break;
387 1.16 sommerfe } else
388 1.16 sommerfe panic("lockmgr: locking against myself");
389 1.16 sommerfe }
390 1.1 fvdl lkp->lk_exclusivecount++;
391 1.15 fvdl if (extflags & LK_SETRECURSE &&
392 1.15 fvdl lkp->lk_recurselevel == 0)
393 1.15 fvdl lkp->lk_recurselevel = lkp->lk_exclusivecount;
394 1.1 fvdl COUNT(p, 1);
395 1.1 fvdl break;
396 1.1 fvdl }
397 1.1 fvdl /*
398 1.1 fvdl * If we are just polling, check to see if we will sleep.
399 1.1 fvdl */
400 1.1 fvdl if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
401 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
402 1.1 fvdl lkp->lk_sharecount != 0)) {
403 1.1 fvdl error = EBUSY;
404 1.1 fvdl break;
405 1.1 fvdl }
406 1.1 fvdl /*
407 1.1 fvdl * Try to acquire the want_exclusive flag.
408 1.1 fvdl */
409 1.1 fvdl ACQUIRE(lkp, error, extflags, lkp->lk_flags &
410 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL));
411 1.1 fvdl if (error)
412 1.1 fvdl break;
413 1.1 fvdl lkp->lk_flags |= LK_WANT_EXCL;
414 1.1 fvdl /*
415 1.1 fvdl * Wait for shared locks and upgrades to finish.
416 1.1 fvdl */
417 1.1 fvdl ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
418 1.1 fvdl (lkp->lk_flags & LK_WANT_UPGRADE));
419 1.1 fvdl lkp->lk_flags &= ~LK_WANT_EXCL;
420 1.1 fvdl if (error)
421 1.1 fvdl break;
422 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
423 1.19 thorpej SETHOLDER(lkp, pid, cpu_id);
424 1.1 fvdl if (lkp->lk_exclusivecount != 0)
425 1.1 fvdl panic("lockmgr: non-zero exclusive count");
426 1.1 fvdl lkp->lk_exclusivecount = 1;
427 1.15 fvdl if (extflags & LK_SETRECURSE)
428 1.15 fvdl lkp->lk_recurselevel = 1;
429 1.1 fvdl COUNT(p, 1);
430 1.1 fvdl break;
431 1.1 fvdl
432 1.1 fvdl case LK_RELEASE:
433 1.1 fvdl if (lkp->lk_exclusivecount != 0) {
434 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
435 1.19 thorpej if (lkp->lk_flags & LK_SPIN) {
436 1.19 thorpej panic("lockmgr: processor %lu, not "
437 1.19 thorpej "exclusive lock holder %lu "
438 1.19 thorpej "unlocking", cpu_id, lkp->lk_cpu);
439 1.19 thorpej } else {
440 1.19 thorpej panic("lockmgr: pid %d, not "
441 1.19 thorpej "exclusive lock holder %d "
442 1.19 thorpej "unlocking", pid,
443 1.19 thorpej lkp->lk_lockholder);
444 1.19 thorpej }
445 1.19 thorpej }
446 1.15 fvdl if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
447 1.15 fvdl lkp->lk_recurselevel = 0;
448 1.1 fvdl lkp->lk_exclusivecount--;
449 1.1 fvdl COUNT(p, -1);
450 1.1 fvdl if (lkp->lk_exclusivecount == 0) {
451 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
452 1.19 thorpej SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
453 1.1 fvdl }
454 1.1 fvdl } else if (lkp->lk_sharecount != 0) {
455 1.1 fvdl lkp->lk_sharecount--;
456 1.1 fvdl COUNT(p, -1);
457 1.1 fvdl }
458 1.1 fvdl if (lkp->lk_waitcount)
459 1.1 fvdl wakeup((void *)lkp);
460 1.1 fvdl break;
461 1.1 fvdl
462 1.1 fvdl case LK_DRAIN:
463 1.1 fvdl /*
464 1.1 fvdl * Check that we do not already hold the lock, as it can
465 1.1 fvdl * never drain if we do. Unfortunately, we have no way to
466 1.1 fvdl * check for holding a shared lock, but at least we can
467 1.1 fvdl * check for an exclusive one.
468 1.1 fvdl */
469 1.19 thorpej if (WEHOLDIT(lkp, pid, cpu_id))
470 1.1 fvdl panic("lockmgr: draining against myself");
471 1.1 fvdl /*
472 1.1 fvdl * If we are just polling, check to see if we will sleep.
473 1.1 fvdl */
474 1.1 fvdl if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
475 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
476 1.1 fvdl lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
477 1.1 fvdl error = EBUSY;
478 1.1 fvdl break;
479 1.1 fvdl }
480 1.19 thorpej if (lkp->lk_flags & LK_SPIN) {
481 1.19 thorpej ACQUIRE(lkp, error, extflags,
482 1.19 thorpej ((lkp->lk_flags &
483 1.19 thorpej (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
484 1.19 thorpej lkp->lk_sharecount != 0 ||
485 1.19 thorpej lkp->lk_waitcount != 0));
486 1.19 thorpej } else {
487 1.19 thorpej /*
488 1.19 thorpej * This is just a special cause of the sleep case
489 1.19 thorpej * in ACQUIRE(). We set WANTDRAIN instead of
490 1.19 thorpej * incrementing waitcount.
491 1.19 thorpej */
492 1.19 thorpej for (error = 0; ((lkp->lk_flags &
493 1.19 thorpej (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
494 1.19 thorpej lkp->lk_sharecount != 0 ||
495 1.19 thorpej lkp->lk_waitcount != 0); ) {
496 1.19 thorpej lkp->lk_flags |= LK_WAITDRAIN;
497 1.19 thorpej simple_unlock(&lkp->lk_interlock);
498 1.19 thorpej if ((error = tsleep((void *)&lkp->lk_flags,
499 1.19 thorpej lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
500 1.19 thorpej return (error);
501 1.19 thorpej if ((extflags) & LK_SLEEPFAIL)
502 1.19 thorpej return (ENOLCK);
503 1.19 thorpej simple_lock(&lkp->lk_interlock);
504 1.19 thorpej }
505 1.1 fvdl }
506 1.1 fvdl lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
507 1.19 thorpej SETHOLDER(lkp, pid, cpu_id);
508 1.1 fvdl lkp->lk_exclusivecount = 1;
509 1.15 fvdl /* XXX unlikely that we'd want this */
510 1.15 fvdl if (extflags & LK_SETRECURSE)
511 1.15 fvdl lkp->lk_recurselevel = 1;
512 1.1 fvdl COUNT(p, 1);
513 1.1 fvdl break;
514 1.1 fvdl
515 1.1 fvdl default:
516 1.1 fvdl simple_unlock(&lkp->lk_interlock);
517 1.1 fvdl panic("lockmgr: unknown locktype request %d",
518 1.1 fvdl flags & LK_TYPE_MASK);
519 1.1 fvdl /* NOTREACHED */
520 1.1 fvdl }
521 1.1 fvdl if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
522 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
523 1.1 fvdl lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
524 1.1 fvdl lkp->lk_flags &= ~LK_WAITDRAIN;
525 1.1 fvdl wakeup((void *)&lkp->lk_flags);
526 1.1 fvdl }
527 1.1 fvdl simple_unlock(&lkp->lk_interlock);
528 1.1 fvdl return (error);
529 1.1 fvdl }
530 1.1 fvdl
531 1.1 fvdl /*
532 1.1 fvdl * Print out information about state of a lock. Used by VOP_PRINT
533 1.1 fvdl * routines to display ststus about contained locks.
534 1.1 fvdl */
535 1.2 fvdl void
536 1.1 fvdl lockmgr_printinfo(lkp)
537 1.19 thorpej __volatile struct lock *lkp;
538 1.1 fvdl {
539 1.1 fvdl
540 1.1 fvdl if (lkp->lk_sharecount)
541 1.1 fvdl printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
542 1.1 fvdl lkp->lk_sharecount);
543 1.19 thorpej else if (lkp->lk_flags & LK_HAVE_EXCL) {
544 1.19 thorpej printf(" lock type %s: EXCL (count %d) by ",
545 1.19 thorpej lkp->lk_wmesg, lkp->lk_exclusivecount);
546 1.19 thorpej if (lkp->lk_flags & LK_SPIN)
547 1.19 thorpej printf("processor %lu", lkp->lk_cpu);
548 1.19 thorpej else
549 1.19 thorpej printf("pid %d", lkp->lk_lockholder);
550 1.19 thorpej } else
551 1.19 thorpej printf(" not locked");
552 1.19 thorpej if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
553 1.1 fvdl printf(" with %d pending", lkp->lk_waitcount);
554 1.1 fvdl }
555 1.1 fvdl
556 1.9 thorpej #if defined(LOCKDEBUG) && !defined(MULTIPROCESSOR)
557 1.12 chs LIST_HEAD(slocklist, simplelock) slockdebuglist;
558 1.18 chs int simple_lock_debugger = 0;
559 1.12 chs
560 1.1 fvdl /*
561 1.1 fvdl * Simple lock functions so that the debugger can see from whence
562 1.1 fvdl * they are being called.
563 1.1 fvdl */
564 1.1 fvdl void
565 1.1 fvdl simple_lock_init(alp)
566 1.1 fvdl struct simplelock *alp;
567 1.1 fvdl {
568 1.1 fvdl alp->lock_data = 0;
569 1.5 chs alp->lock_file = NULL;
570 1.5 chs alp->lock_line = 0;
571 1.5 chs alp->unlock_file = NULL;
572 1.5 chs alp->unlock_line = 0;
573 1.10 thorpej alp->lock_holder = 0;
574 1.1 fvdl }
575 1.1 fvdl
576 1.1 fvdl void
577 1.1 fvdl _simple_lock(alp, id, l)
578 1.1 fvdl __volatile struct simplelock *alp;
579 1.1 fvdl const char *id;
580 1.1 fvdl int l;
581 1.1 fvdl {
582 1.12 chs int s;
583 1.12 chs
584 1.18 chs s = splhigh();
585 1.1 fvdl if (alp->lock_data == 1) {
586 1.5 chs printf("simple_lock: lock held\n");
587 1.5 chs printf("currently at: %s:%d\n", id, l);
588 1.5 chs printf("last locked: %s:%d\n",
589 1.5 chs alp->lock_file, alp->lock_line);
590 1.5 chs printf("last unlocked: %s:%d\n",
591 1.5 chs alp->unlock_file, alp->unlock_line);
592 1.18 chs if (simple_lock_debugger) {
593 1.18 chs Debugger();
594 1.1 fvdl }
595 1.18 chs splx(s);
596 1.12 chs return;
597 1.1 fvdl }
598 1.12 chs LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
599 1.1 fvdl alp->lock_data = 1;
600 1.5 chs alp->lock_file = id;
601 1.5 chs alp->lock_line = l;
602 1.1 fvdl if (curproc)
603 1.1 fvdl curproc->p_simple_locks++;
604 1.18 chs splx(s);
605 1.1 fvdl }
606 1.1 fvdl
607 1.1 fvdl int
608 1.1 fvdl _simple_lock_try(alp, id, l)
609 1.1 fvdl __volatile struct simplelock *alp;
610 1.1 fvdl const char *id;
611 1.1 fvdl int l;
612 1.1 fvdl {
613 1.12 chs int s;
614 1.1 fvdl
615 1.18 chs s = splhigh();
616 1.18 chs if (alp->lock_data != 0) {
617 1.18 chs printf("simple_lock_try: lock held\n");
618 1.18 chs printf("currently at: %s:%d\n", id, l);
619 1.18 chs printf("last locked: %s:%d\n",
620 1.18 chs alp->lock_file, alp->lock_line);
621 1.18 chs printf("last unlocked: %s:%d\n",
622 1.18 chs alp->unlock_file, alp->unlock_line);
623 1.18 chs if (simple_lock_debugger) {
624 1.18 chs Debugger();
625 1.18 chs }
626 1.18 chs splx(s);
627 1.1 fvdl return (0);
628 1.18 chs }
629 1.18 chs
630 1.1 fvdl alp->lock_data = 1;
631 1.5 chs alp->lock_file = id;
632 1.5 chs alp->lock_line = l;
633 1.12 chs LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
634 1.18 chs if (curproc)
635 1.18 chs curproc->p_simple_locks++;
636 1.12 chs splx(s);
637 1.12 chs
638 1.1 fvdl return (1);
639 1.1 fvdl }
640 1.1 fvdl
641 1.1 fvdl void
642 1.1 fvdl _simple_unlock(alp, id, l)
643 1.1 fvdl __volatile struct simplelock *alp;
644 1.1 fvdl const char *id;
645 1.1 fvdl int l;
646 1.1 fvdl {
647 1.12 chs int s;
648 1.1 fvdl
649 1.18 chs s = splhigh();
650 1.1 fvdl if (alp->lock_data == 0) {
651 1.5 chs printf("simple_unlock: lock not held\n");
652 1.5 chs printf("currently at: %s:%d\n", id, l);
653 1.5 chs printf("last locked: %s:%d\n",
654 1.5 chs alp->lock_file, alp->lock_line);
655 1.5 chs printf("last unlocked: %s:%d\n",
656 1.5 chs alp->unlock_file, alp->unlock_line);
657 1.18 chs if (simple_lock_debugger) {
658 1.18 chs Debugger();
659 1.1 fvdl }
660 1.18 chs splx(s);
661 1.12 chs return;
662 1.1 fvdl }
663 1.12 chs
664 1.12 chs LIST_REMOVE(alp, list);
665 1.12 chs alp->list.le_next = NULL;
666 1.12 chs alp->list.le_prev = NULL;
667 1.1 fvdl alp->lock_data = 0;
668 1.5 chs alp->unlock_file = id;
669 1.5 chs alp->unlock_line = l;
670 1.1 fvdl if (curproc)
671 1.1 fvdl curproc->p_simple_locks--;
672 1.18 chs splx(s);
673 1.12 chs }
674 1.12 chs
675 1.12 chs void
676 1.12 chs simple_lock_dump()
677 1.12 chs {
678 1.12 chs struct simplelock *alp;
679 1.12 chs int s;
680 1.12 chs
681 1.12 chs s = splhigh();
682 1.12 chs printf("all simple locks:\n");
683 1.12 chs for (alp = LIST_FIRST(&slockdebuglist);
684 1.12 chs alp != NULL;
685 1.12 chs alp = LIST_NEXT(alp, list)) {
686 1.12 chs printf("%p %s:%d\n", alp, alp->lock_file, alp->lock_line);
687 1.12 chs }
688 1.12 chs splx(s);
689 1.12 chs }
690 1.12 chs
691 1.12 chs void
692 1.12 chs simple_lock_freecheck(start, end)
693 1.12 chs void *start, *end;
694 1.12 chs {
695 1.12 chs struct simplelock *alp;
696 1.12 chs int s;
697 1.12 chs
698 1.12 chs s = splhigh();
699 1.12 chs for (alp = LIST_FIRST(&slockdebuglist);
700 1.12 chs alp != NULL;
701 1.12 chs alp = LIST_NEXT(alp, list)) {
702 1.12 chs if ((void *)alp >= start && (void *)alp < end) {
703 1.14 chs printf("freeing simple_lock %p %s:%d\n",
704 1.14 chs alp, alp->lock_file, alp->lock_line);
705 1.12 chs #ifdef DDB
706 1.12 chs Debugger();
707 1.12 chs #endif
708 1.12 chs }
709 1.12 chs }
710 1.12 chs splx(s);
711 1.1 fvdl }
712 1.9 thorpej #endif /* LOCKDEBUG && ! MULTIPROCESSOR */
713