kern_lock.c revision 1.133 1 1.133 ad /* $NetBSD: kern_lock.c,v 1.133 2008/01/26 14:29:31 ad Exp $ */
2 1.19 thorpej
3 1.19 thorpej /*-
4 1.133 ad * Copyright (c) 1999, 2000, 2006, 2007, 2008 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.105 ad * NASA Ames Research Center, and by Andrew Doran.
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.133 ad __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.133 2008/01/26 14:29:31 ad Exp $");
80 1.7 thorpej
81 1.21 thorpej #include "opt_multiprocessor.h"
82 1.105 ad
83 1.1 fvdl #include <sys/param.h>
84 1.1 fvdl #include <sys/proc.h>
85 1.1 fvdl #include <sys/lock.h>
86 1.2 fvdl #include <sys/systm.h>
87 1.125 ad #include <sys/kernel.h>
88 1.105 ad #include <sys/lockdebug.h>
89 1.122 ad #include <sys/cpu.h>
90 1.122 ad #include <sys/syslog.h>
91 1.128 ad #include <sys/atomic.h>
92 1.105 ad
93 1.110 christos #include <machine/stdarg.h>
94 1.131 ad #include <machine/lock.h>
95 1.1 fvdl
96 1.98 ad #include <dev/lockstat.h>
97 1.98 ad
98 1.25 thorpej /*
99 1.25 thorpej * note that stdarg.h and the ansi style va_start macro is used for both
100 1.25 thorpej * ansi and traditional c compiles.
101 1.25 thorpej * XXX: this requires that stdarg.h define: va_alist and va_dcl
102 1.25 thorpej */
103 1.36 thorpej void lock_printf(const char *fmt, ...)
104 1.37 eeh __attribute__((__format__(__printf__,1,2)));
105 1.25 thorpej
106 1.122 ad static int acquire(struct lock **, int *, int, int, int, uintptr_t);
107 1.73 yamt
108 1.57 sommerfe int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
109 1.127 yamt bool kernel_lock_dodebug;
110 1.132 ad
111 1.132 ad __cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
112 1.132 ad __aligned(CACHE_LINE_SIZE);
113 1.1 fvdl
114 1.130 ad #ifdef LOCKDEBUG
115 1.130 ad static lockops_t lockmgr_lockops = {
116 1.130 ad "lockmgr",
117 1.130 ad 1,
118 1.130 ad (void *)nullop
119 1.130 ad };
120 1.130 ad #endif
121 1.130 ad
122 1.21 thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
123 1.122 ad #define COUNT(lkp, l, cpu_id, x) (l)->l_locks += (x)
124 1.1 fvdl #else
125 1.22 mellon #define COUNT(lkp, p, cpu_id, x)
126 1.21 thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
127 1.1 fvdl
128 1.98 ad #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
129 1.98 ad
130 1.1 fvdl /*
131 1.1 fvdl * Acquire a resource.
132 1.1 fvdl */
133 1.73 yamt static int
134 1.122 ad acquire(struct lock **lkpp, int *s, int extflags,
135 1.122 ad int drain, int wanted, uintptr_t ra)
136 1.73 yamt {
137 1.73 yamt int error;
138 1.122 ad struct lock *lkp = *lkpp;
139 1.73 yamt
140 1.73 yamt KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
141 1.73 yamt
142 1.122 ad for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
143 1.122 ad if (drain)
144 1.122 ad lkp->lk_flags |= LK_WAITDRAIN;
145 1.122 ad else {
146 1.73 yamt lkp->lk_waitcount++;
147 1.73 yamt lkp->lk_flags |= LK_WAIT_NONZERO;
148 1.73 yamt }
149 1.130 ad error = mtsleep(drain ? (void *)&lkp->lk_flags : (void *)lkp,
150 1.122 ad lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo,
151 1.130 ad __UNVOLATILE(&lkp->lk_interlock));
152 1.73 yamt if (!drain) {
153 1.73 yamt lkp->lk_waitcount--;
154 1.73 yamt if (lkp->lk_waitcount == 0)
155 1.73 yamt lkp->lk_flags &= ~LK_WAIT_NONZERO;
156 1.73 yamt }
157 1.122 ad if (error)
158 1.122 ad break;
159 1.122 ad if (extflags & LK_SLEEPFAIL) {
160 1.122 ad error = ENOLCK;
161 1.122 ad break;
162 1.73 yamt }
163 1.122 ad }
164 1.105 ad
165 1.73 yamt return error;
166 1.73 yamt }
167 1.73 yamt
168 1.69 thorpej #define SETHOLDER(lkp, pid, lid, cpu_id) \
169 1.19 thorpej do { \
170 1.122 ad (lkp)->lk_lockholder = pid; \
171 1.122 ad (lkp)->lk_locklwp = lid; \
172 1.30 thorpej } while (/*CONSTCOND*/0)
173 1.19 thorpej
174 1.69 thorpej #define WEHOLDIT(lkp, pid, lid, cpu_id) \
175 1.122 ad ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
176 1.19 thorpej
177 1.23 thorpej #define WAKEUP_WAITER(lkp) \
178 1.23 thorpej do { \
179 1.122 ad if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) { \
180 1.87 christos wakeup((lkp)); \
181 1.23 thorpej } \
182 1.30 thorpej } while (/*CONSTCOND*/0)
183 1.23 thorpej
184 1.25 thorpej #if defined(LOCKDEBUG)
185 1.25 thorpej /*
186 1.25 thorpej * Lock debug printing routine; can be configured to print to console
187 1.25 thorpej * or log to syslog.
188 1.25 thorpej */
189 1.25 thorpej void
190 1.25 thorpej lock_printf(const char *fmt, ...)
191 1.25 thorpej {
192 1.68 pk char b[150];
193 1.25 thorpej va_list ap;
194 1.25 thorpej
195 1.25 thorpej va_start(ap, fmt);
196 1.25 thorpej if (lock_debug_syslog)
197 1.25 thorpej vlog(LOG_DEBUG, fmt, ap);
198 1.68 pk else {
199 1.68 pk vsnprintf(b, sizeof(b), fmt, ap);
200 1.68 pk printf_nolog("%s", b);
201 1.68 pk }
202 1.25 thorpej va_end(ap);
203 1.25 thorpej }
204 1.25 thorpej #endif /* LOCKDEBUG */
205 1.25 thorpej
206 1.110 christos static void
207 1.122 ad lockpanic(struct lock *lkp, const char *fmt, ...)
208 1.110 christos {
209 1.110 christos char s[150], b[150];
210 1.110 christos static const char *locktype[] = {
211 1.129 ad "*0*", "shared", "exclusive", "*3*", "*4*", "downgrade",
212 1.129 ad "*release*", "drain", "exclother", "*9*", "*10*",
213 1.129 ad "*11*", "*12*", "*13*", "*14*", "*15*"
214 1.110 christos };
215 1.110 christos va_list ap;
216 1.110 christos va_start(ap, fmt);
217 1.110 christos vsnprintf(s, sizeof(s), fmt, ap);
218 1.110 christos va_end(ap);
219 1.110 christos bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
220 1.110 christos panic("%s ("
221 1.122 ad "type %s flags %s, sharecount %d, exclusivecount %d, "
222 1.110 christos "recurselevel %d, waitcount %d, wmesg %s"
223 1.122 ad ", lock_addr %p, unlock_addr %p"
224 1.110 christos ")\n",
225 1.122 ad s, locktype[lkp->lk_flags & LK_TYPE_MASK],
226 1.110 christos b, lkp->lk_sharecount, lkp->lk_exclusivecount,
227 1.122 ad lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg,
228 1.122 ad (void *)lkp->lk_lock_addr, (void *)lkp->lk_unlock_addr
229 1.110 christos );
230 1.110 christos }
231 1.110 christos
232 1.1 fvdl /*
233 1.1 fvdl * Initialize a lock; required before use.
234 1.1 fvdl */
235 1.1 fvdl void
236 1.109 yamt lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
237 1.1 fvdl {
238 1.1 fvdl
239 1.8 perry memset(lkp, 0, sizeof(struct lock));
240 1.122 ad lkp->lk_flags = flags & LK_EXTFLG_MASK;
241 1.130 ad mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
242 1.122 ad lkp->lk_lockholder = LK_NOPROC;
243 1.122 ad lkp->lk_prio = prio;
244 1.122 ad lkp->lk_timo = timo;
245 1.122 ad lkp->lk_wmesg = wmesg;
246 1.122 ad lkp->lk_lock_addr = 0;
247 1.122 ad lkp->lk_unlock_addr = 0;
248 1.130 ad
249 1.130 ad if (LOCKDEBUG_ALLOC(lkp, &lockmgr_lockops,
250 1.130 ad (uintptr_t)__builtin_return_address(0))) {
251 1.130 ad lkp->lk_flags |= LK_DODEBUG;
252 1.130 ad }
253 1.122 ad }
254 1.122 ad
255 1.122 ad void
256 1.122 ad lockdestroy(struct lock *lkp)
257 1.122 ad {
258 1.122 ad
259 1.130 ad LOCKDEBUG_FREE(((lkp->lk_flags & LK_DODEBUG) != 0), lkp);
260 1.130 ad mutex_destroy(&lkp->lk_interlock);
261 1.1 fvdl }
262 1.1 fvdl
263 1.1 fvdl /*
264 1.1 fvdl * Determine the status of a lock.
265 1.1 fvdl */
266 1.1 fvdl int
267 1.33 thorpej lockstatus(struct lock *lkp)
268 1.1 fvdl {
269 1.76 yamt int lock_type = 0;
270 1.76 yamt struct lwp *l = curlwp; /* XXX */
271 1.76 yamt pid_t pid;
272 1.76 yamt lwpid_t lid;
273 1.88 blymn cpuid_t cpu_num;
274 1.76 yamt
275 1.122 ad if (l == NULL) {
276 1.88 blymn cpu_num = cpu_number();
277 1.76 yamt pid = LK_KERNPROC;
278 1.76 yamt lid = 0;
279 1.76 yamt } else {
280 1.88 blymn cpu_num = LK_NOCPU;
281 1.76 yamt pid = l->l_proc->p_pid;
282 1.76 yamt lid = l->l_lid;
283 1.76 yamt }
284 1.1 fvdl
285 1.130 ad mutex_enter(&lkp->lk_interlock);
286 1.76 yamt if (lkp->lk_exclusivecount != 0) {
287 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num))
288 1.76 yamt lock_type = LK_EXCLUSIVE;
289 1.76 yamt else
290 1.76 yamt lock_type = LK_EXCLOTHER;
291 1.76 yamt } else if (lkp->lk_sharecount != 0)
292 1.1 fvdl lock_type = LK_SHARED;
293 1.129 ad else if (lkp->lk_flags & LK_WANT_EXCL)
294 1.103 chs lock_type = LK_EXCLOTHER;
295 1.130 ad mutex_exit(&lkp->lk_interlock);
296 1.1 fvdl return (lock_type);
297 1.1 fvdl }
298 1.35 thorpej
299 1.44 thorpej /*
300 1.32 sommerfe * XXX XXX kludge around another kludge..
301 1.32 sommerfe *
302 1.32 sommerfe * vfs_shutdown() may be called from interrupt context, either as a result
303 1.32 sommerfe * of a panic, or from the debugger. It proceeds to call
304 1.32 sommerfe * sys_sync(&proc0, ...), pretending its running on behalf of proc0
305 1.32 sommerfe *
306 1.32 sommerfe * We would like to make an attempt to sync the filesystems in this case, so
307 1.32 sommerfe * if this happens, we treat attempts to acquire locks specially.
308 1.32 sommerfe * All locks are acquired on behalf of proc0.
309 1.32 sommerfe *
310 1.32 sommerfe * If we've already paniced, we don't block waiting for locks, but
311 1.32 sommerfe * just barge right ahead since we're already going down in flames.
312 1.32 sommerfe */
313 1.32 sommerfe
314 1.32 sommerfe /*
315 1.1 fvdl * Set, change, or release a lock.
316 1.1 fvdl *
317 1.1 fvdl * Shared requests increment the shared count. Exclusive requests set the
318 1.1 fvdl * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
319 1.129 ad * accepted shared locks to go away.
320 1.1 fvdl */
321 1.1 fvdl int
322 1.130 ad lockmgr(struct lock *lkp, u_int flags, kmutex_t *interlkp)
323 1.1 fvdl {
324 1.1 fvdl int error;
325 1.1 fvdl pid_t pid;
326 1.69 thorpej lwpid_t lid;
327 1.1 fvdl int extflags;
328 1.88 blymn cpuid_t cpu_num;
329 1.69 thorpej struct lwp *l = curlwp;
330 1.32 sommerfe int lock_shutdown_noblock = 0;
331 1.67 scw int s = 0;
332 1.1 fvdl
333 1.1 fvdl error = 0;
334 1.19 thorpej
335 1.80 yamt /* LK_RETRY is for vn_lock, not for lockmgr. */
336 1.79 yamt KASSERT((flags & LK_RETRY) == 0);
337 1.125 ad KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
338 1.79 yamt
339 1.130 ad mutex_enter(&lkp->lk_interlock);
340 1.1 fvdl if (flags & LK_INTERLOCK)
341 1.130 ad mutex_exit(interlkp);
342 1.1 fvdl extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
343 1.19 thorpej
344 1.122 ad if (l == NULL) {
345 1.122 ad if (!doing_shutdown) {
346 1.122 ad panic("lockmgr: no context");
347 1.122 ad } else {
348 1.122 ad l = &lwp0;
349 1.122 ad if (panicstr && (!(flags & LK_NOWAIT))) {
350 1.122 ad flags |= LK_NOWAIT;
351 1.122 ad lock_shutdown_noblock = 1;
352 1.32 sommerfe }
353 1.32 sommerfe }
354 1.19 thorpej }
355 1.122 ad lid = l->l_lid;
356 1.122 ad pid = l->l_proc->p_pid;
357 1.88 blymn cpu_num = cpu_number();
358 1.19 thorpej
359 1.1 fvdl /*
360 1.1 fvdl * Once a lock has drained, the LK_DRAINING flag is set and an
361 1.1 fvdl * exclusive lock is returned. The only valid operation thereafter
362 1.1 fvdl * is a single release of that exclusive lock. This final release
363 1.1 fvdl * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
364 1.1 fvdl * further requests of any sort will result in a panic. The bits
365 1.1 fvdl * selected for these two flags are chosen so that they will be set
366 1.1 fvdl * in memory that is freed (freed memory is filled with 0xdeadbeef).
367 1.1 fvdl * The final release is permitted to give a new lease on life to
368 1.1 fvdl * the lock by specifying LK_REENABLE.
369 1.1 fvdl */
370 1.1 fvdl if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
371 1.28 thorpej #ifdef DIAGNOSTIC /* { */
372 1.1 fvdl if (lkp->lk_flags & LK_DRAINED)
373 1.110 christos lockpanic(lkp, "lockmgr: using decommissioned lock");
374 1.1 fvdl if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
375 1.88 blymn WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
376 1.110 christos lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
377 1.1 fvdl flags & LK_TYPE_MASK);
378 1.28 thorpej #endif /* DIAGNOSTIC */ /* } */
379 1.1 fvdl lkp->lk_flags &= ~LK_DRAINING;
380 1.1 fvdl if ((flags & LK_REENABLE) == 0)
381 1.1 fvdl lkp->lk_flags |= LK_DRAINED;
382 1.1 fvdl }
383 1.1 fvdl
384 1.1 fvdl switch (flags & LK_TYPE_MASK) {
385 1.1 fvdl
386 1.1 fvdl case LK_SHARED:
387 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
388 1.1 fvdl /*
389 1.1 fvdl * If just polling, check to see if we will block.
390 1.1 fvdl */
391 1.1 fvdl if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
392 1.129 ad (LK_HAVE_EXCL | LK_WANT_EXCL))) {
393 1.1 fvdl error = EBUSY;
394 1.1 fvdl break;
395 1.1 fvdl }
396 1.1 fvdl /*
397 1.129 ad * Wait for exclusive locks to clear.
398 1.1 fvdl */
399 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
400 1.129 ad LK_HAVE_EXCL | LK_WANT_EXCL,
401 1.98 ad RETURN_ADDRESS);
402 1.1 fvdl if (error)
403 1.1 fvdl break;
404 1.1 fvdl lkp->lk_sharecount++;
405 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
406 1.88 blymn COUNT(lkp, l, cpu_num, 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.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
415 1.88 blymn COUNT(lkp, l, cpu_num, 1);
416 1.1 fvdl /* fall into downgrade */
417 1.1 fvdl
418 1.1 fvdl case LK_DOWNGRADE:
419 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
420 1.19 thorpej lkp->lk_exclusivecount == 0)
421 1.110 christos lockpanic(lkp, "lockmgr: not holding exclusive lock");
422 1.1 fvdl lkp->lk_sharecount += lkp->lk_exclusivecount;
423 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
424 1.1 fvdl lkp->lk_exclusivecount = 0;
425 1.15 fvdl lkp->lk_recurselevel = 0;
426 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
427 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
428 1.50 thorpej #if defined(LOCKDEBUG)
429 1.122 ad lkp->lk_unlock_addr = RETURN_ADDRESS;
430 1.50 thorpej #endif
431 1.23 thorpej WAKEUP_WAITER(lkp);
432 1.1 fvdl break;
433 1.1 fvdl
434 1.1 fvdl case LK_EXCLUSIVE:
435 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
436 1.1 fvdl /*
437 1.19 thorpej * Recursive lock.
438 1.1 fvdl */
439 1.15 fvdl if ((extflags & LK_CANRECURSE) == 0 &&
440 1.16 sommerfe lkp->lk_recurselevel == 0) {
441 1.16 sommerfe if (extflags & LK_RECURSEFAIL) {
442 1.16 sommerfe error = EDEADLK;
443 1.16 sommerfe break;
444 1.16 sommerfe } else
445 1.110 christos lockpanic(lkp, "lockmgr: locking against myself");
446 1.16 sommerfe }
447 1.1 fvdl lkp->lk_exclusivecount++;
448 1.88 blymn COUNT(lkp, l, cpu_num, 1);
449 1.1 fvdl break;
450 1.1 fvdl }
451 1.1 fvdl /*
452 1.1 fvdl * If we are just polling, check to see if we will sleep.
453 1.1 fvdl */
454 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
455 1.129 ad (LK_HAVE_EXCL | LK_WANT_EXCL | LK_SHARE_NONZERO))) {
456 1.1 fvdl error = EBUSY;
457 1.1 fvdl break;
458 1.1 fvdl }
459 1.1 fvdl /*
460 1.1 fvdl * Try to acquire the want_exclusive flag.
461 1.1 fvdl */
462 1.82 yamt error = acquire(&lkp, &s, extflags, 0,
463 1.98 ad LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
464 1.1 fvdl if (error)
465 1.1 fvdl break;
466 1.1 fvdl lkp->lk_flags |= LK_WANT_EXCL;
467 1.1 fvdl /*
468 1.129 ad * Wait for shared locks to finish.
469 1.1 fvdl */
470 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
471 1.129 ad LK_HAVE_EXCL | LK_SHARE_NONZERO,
472 1.98 ad RETURN_ADDRESS);
473 1.1 fvdl lkp->lk_flags &= ~LK_WANT_EXCL;
474 1.83 yamt if (error) {
475 1.83 yamt WAKEUP_WAITER(lkp);
476 1.1 fvdl break;
477 1.83 yamt }
478 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
479 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
480 1.50 thorpej #if defined(LOCKDEBUG)
481 1.122 ad lkp->lk_lock_addr = RETURN_ADDRESS;
482 1.50 thorpej #endif
483 1.1 fvdl if (lkp->lk_exclusivecount != 0)
484 1.110 christos lockpanic(lkp, "lockmgr: non-zero exclusive count");
485 1.1 fvdl lkp->lk_exclusivecount = 1;
486 1.88 blymn COUNT(lkp, l, cpu_num, 1);
487 1.1 fvdl break;
488 1.1 fvdl
489 1.1 fvdl case LK_RELEASE:
490 1.1 fvdl if (lkp->lk_exclusivecount != 0) {
491 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
492 1.122 ad lockpanic(lkp, "lockmgr: pid %d.%d, not "
493 1.122 ad "exclusive lock holder %d.%d "
494 1.122 ad "unlocking", pid, lid,
495 1.122 ad lkp->lk_lockholder,
496 1.122 ad lkp->lk_locklwp);
497 1.19 thorpej }
498 1.15 fvdl if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
499 1.15 fvdl lkp->lk_recurselevel = 0;
500 1.1 fvdl lkp->lk_exclusivecount--;
501 1.88 blymn COUNT(lkp, l, cpu_num, -1);
502 1.1 fvdl if (lkp->lk_exclusivecount == 0) {
503 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
504 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
505 1.50 thorpej #if defined(LOCKDEBUG)
506 1.122 ad lkp->lk_unlock_addr = RETURN_ADDRESS;
507 1.50 thorpej #endif
508 1.1 fvdl }
509 1.1 fvdl } else if (lkp->lk_sharecount != 0) {
510 1.1 fvdl lkp->lk_sharecount--;
511 1.73 yamt if (lkp->lk_sharecount == 0)
512 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
513 1.88 blymn COUNT(lkp, l, cpu_num, -1);
514 1.1 fvdl }
515 1.39 thorpej #ifdef DIAGNOSTIC
516 1.39 thorpej else
517 1.110 christos lockpanic(lkp, "lockmgr: release of unlocked lock!");
518 1.39 thorpej #endif
519 1.23 thorpej WAKEUP_WAITER(lkp);
520 1.1 fvdl break;
521 1.1 fvdl
522 1.1 fvdl case LK_DRAIN:
523 1.1 fvdl /*
524 1.86 perry * Check that we do not already hold the lock, as it can
525 1.1 fvdl * never drain if we do. Unfortunately, we have no way to
526 1.1 fvdl * check for holding a shared lock, but at least we can
527 1.1 fvdl * check for an exclusive one.
528 1.1 fvdl */
529 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num))
530 1.110 christos lockpanic(lkp, "lockmgr: draining against myself");
531 1.1 fvdl /*
532 1.1 fvdl * If we are just polling, check to see if we will sleep.
533 1.1 fvdl */
534 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
535 1.129 ad (LK_HAVE_EXCL | LK_WANT_EXCL |
536 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
537 1.1 fvdl error = EBUSY;
538 1.1 fvdl break;
539 1.1 fvdl }
540 1.78 hannken error = acquire(&lkp, &s, extflags, 1,
541 1.129 ad LK_HAVE_EXCL | LK_WANT_EXCL |
542 1.98 ad LK_SHARE_NONZERO | LK_WAIT_NONZERO,
543 1.98 ad RETURN_ADDRESS);
544 1.23 thorpej if (error)
545 1.23 thorpej break;
546 1.118 pooka lkp->lk_flags |= LK_HAVE_EXCL;
547 1.118 pooka if ((extflags & LK_RESURRECT) == 0)
548 1.118 pooka lkp->lk_flags |= LK_DRAINING;
549 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
550 1.50 thorpej #if defined(LOCKDEBUG)
551 1.122 ad lkp->lk_lock_addr = RETURN_ADDRESS;
552 1.50 thorpej #endif
553 1.1 fvdl lkp->lk_exclusivecount = 1;
554 1.88 blymn COUNT(lkp, l, cpu_num, 1);
555 1.1 fvdl break;
556 1.1 fvdl
557 1.1 fvdl default:
558 1.130 ad mutex_exit(&lkp->lk_interlock);
559 1.110 christos lockpanic(lkp, "lockmgr: unknown locktype request %d",
560 1.1 fvdl flags & LK_TYPE_MASK);
561 1.1 fvdl /* NOTREACHED */
562 1.1 fvdl }
563 1.122 ad if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
564 1.23 thorpej ((lkp->lk_flags &
565 1.129 ad (LK_HAVE_EXCL | LK_WANT_EXCL |
566 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
567 1.1 fvdl lkp->lk_flags &= ~LK_WAITDRAIN;
568 1.87 christos wakeup(&lkp->lk_flags);
569 1.1 fvdl }
570 1.32 sommerfe /*
571 1.32 sommerfe * Note that this panic will be a recursive panic, since
572 1.32 sommerfe * we only set lock_shutdown_noblock above if panicstr != NULL.
573 1.32 sommerfe */
574 1.32 sommerfe if (error && lock_shutdown_noblock)
575 1.110 christos lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
576 1.86 perry
577 1.130 ad mutex_exit(&lkp->lk_interlock);
578 1.1 fvdl return (error);
579 1.1 fvdl }
580 1.1 fvdl
581 1.1 fvdl /*
582 1.1 fvdl * Print out information about state of a lock. Used by VOP_PRINT
583 1.1 fvdl * routines to display ststus about contained locks.
584 1.1 fvdl */
585 1.2 fvdl void
586 1.122 ad lockmgr_printinfo(struct lock *lkp)
587 1.1 fvdl {
588 1.1 fvdl
589 1.1 fvdl if (lkp->lk_sharecount)
590 1.1 fvdl printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
591 1.1 fvdl lkp->lk_sharecount);
592 1.19 thorpej else if (lkp->lk_flags & LK_HAVE_EXCL) {
593 1.19 thorpej printf(" lock type %s: EXCL (count %d) by ",
594 1.19 thorpej lkp->lk_wmesg, lkp->lk_exclusivecount);
595 1.122 ad printf("pid %d.%d", lkp->lk_lockholder,
596 1.122 ad lkp->lk_locklwp);
597 1.19 thorpej } else
598 1.19 thorpej printf(" not locked");
599 1.122 ad if (lkp->lk_waitcount > 0)
600 1.1 fvdl printf(" with %d pending", lkp->lk_waitcount);
601 1.1 fvdl }
602 1.1 fvdl
603 1.122 ad #if defined(LOCKDEBUG)
604 1.96 yamt void
605 1.96 yamt assert_sleepable(struct simplelock *interlock, const char *msg)
606 1.96 yamt {
607 1.96 yamt
608 1.117 ad if (panicstr != NULL)
609 1.117 ad return;
610 1.132 ad LOCKDEBUG_BARRIER(kernel_lock, 1);
611 1.125 ad if (CURCPU_IDLE_P() && !cold) {
612 1.113 yamt panic("assert_sleepable: idle");
613 1.97 yamt }
614 1.96 yamt }
615 1.122 ad #endif
616 1.105 ad
617 1.62 thorpej /*
618 1.124 pooka * rump doesn't need the kernel lock so force it out. We cannot
619 1.124 pooka * currently easily include it for compilation because of
620 1.128 ad * a) SPINLOCK_* b) membar_producer(). They are defined in different
621 1.124 pooka * places / way for each arch, so just simply do not bother to
622 1.124 pooka * fight a lot for no gain (i.e. pain but still no gain).
623 1.124 pooka */
624 1.124 pooka #ifndef _RUMPKERNEL
625 1.124 pooka /*
626 1.62 thorpej * Functions for manipulating the kernel_lock. We put them here
627 1.62 thorpej * so that they show up in profiles.
628 1.62 thorpej */
629 1.62 thorpej
630 1.105 ad #define _KERNEL_LOCK_ABORT(msg) \
631 1.132 ad LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
632 1.105 ad
633 1.105 ad #ifdef LOCKDEBUG
634 1.105 ad #define _KERNEL_LOCK_ASSERT(cond) \
635 1.105 ad do { \
636 1.105 ad if (!(cond)) \
637 1.105 ad _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
638 1.105 ad } while (/* CONSTCOND */ 0)
639 1.105 ad #else
640 1.105 ad #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
641 1.105 ad #endif
642 1.105 ad
643 1.105 ad void _kernel_lock_dump(volatile void *);
644 1.105 ad
645 1.105 ad lockops_t _kernel_lock_ops = {
646 1.105 ad "Kernel lock",
647 1.105 ad 0,
648 1.105 ad _kernel_lock_dump
649 1.105 ad };
650 1.105 ad
651 1.85 yamt /*
652 1.105 ad * Initialize the kernel lock.
653 1.85 yamt */
654 1.62 thorpej void
655 1.122 ad kernel_lock_init(void)
656 1.62 thorpej {
657 1.62 thorpej
658 1.132 ad KASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
659 1.132 ad __cpu_simple_lock_init(kernel_lock);
660 1.132 ad kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
661 1.122 ad RETURN_ADDRESS);
662 1.62 thorpej }
663 1.62 thorpej
664 1.62 thorpej /*
665 1.105 ad * Print debugging information about the kernel lock.
666 1.62 thorpej */
667 1.62 thorpej void
668 1.105 ad _kernel_lock_dump(volatile void *junk)
669 1.62 thorpej {
670 1.85 yamt struct cpu_info *ci = curcpu();
671 1.62 thorpej
672 1.105 ad (void)junk;
673 1.85 yamt
674 1.105 ad printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
675 1.105 ad ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
676 1.62 thorpej }
677 1.62 thorpej
678 1.105 ad /*
679 1.105 ad * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
680 1.105 ad * acquisition is from process context.
681 1.105 ad */
682 1.62 thorpej void
683 1.105 ad _kernel_lock(int nlocks, struct lwp *l)
684 1.62 thorpej {
685 1.85 yamt struct cpu_info *ci = curcpu();
686 1.105 ad LOCKSTAT_TIMER(spintime);
687 1.105 ad LOCKSTAT_FLAG(lsflag);
688 1.105 ad struct lwp *owant;
689 1.105 ad u_int spins;
690 1.85 yamt int s;
691 1.85 yamt
692 1.105 ad if (nlocks == 0)
693 1.105 ad return;
694 1.105 ad _KERNEL_LOCK_ASSERT(nlocks > 0);
695 1.62 thorpej
696 1.122 ad l = curlwp;
697 1.105 ad
698 1.105 ad if (ci->ci_biglock_count != 0) {
699 1.132 ad _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
700 1.105 ad ci->ci_biglock_count += nlocks;
701 1.122 ad l->l_blcnt += nlocks;
702 1.105 ad return;
703 1.105 ad }
704 1.105 ad
705 1.122 ad _KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
706 1.132 ad LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
707 1.127 yamt 0);
708 1.107 ad
709 1.122 ad s = splvm();
710 1.132 ad if (__cpu_simple_lock_try(kernel_lock)) {
711 1.105 ad ci->ci_biglock_count = nlocks;
712 1.122 ad l->l_blcnt = nlocks;
713 1.132 ad LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock,
714 1.127 yamt RETURN_ADDRESS, 0);
715 1.105 ad splx(s);
716 1.105 ad return;
717 1.105 ad }
718 1.105 ad
719 1.132 ad /*
720 1.132 ad * To remove the ordering constraint between adaptive mutexes
721 1.132 ad * and kernel_lock we must make it appear as if this thread is
722 1.132 ad * blocking. For non-interlocked mutex release, a store fence
723 1.132 ad * is required to ensure that the result of any mutex_exit()
724 1.132 ad * by the current LWP becomes visible on the bus before the set
725 1.132 ad * of ci->ci_biglock_wanted becomes visible.
726 1.132 ad */
727 1.132 ad membar_producer();
728 1.132 ad owant = ci->ci_biglock_wanted;
729 1.132 ad ci->ci_biglock_wanted = l;
730 1.105 ad
731 1.105 ad /*
732 1.132 ad * Spin until we acquire the lock. Once we have it, record the
733 1.132 ad * time spent with lockstat.
734 1.105 ad */
735 1.132 ad LOCKSTAT_ENTER(lsflag);
736 1.132 ad LOCKSTAT_START_TIMER(lsflag, spintime);
737 1.105 ad
738 1.105 ad spins = 0;
739 1.105 ad do {
740 1.122 ad splx(s);
741 1.132 ad while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
742 1.132 ad if (SPINLOCK_SPINOUT(spins)) {
743 1.105 ad _KERNEL_LOCK_ABORT("spinout");
744 1.132 ad }
745 1.122 ad SPINLOCK_BACKOFF_HOOK;
746 1.105 ad SPINLOCK_SPIN_HOOK;
747 1.105 ad }
748 1.132 ad s = splvm();
749 1.132 ad } while (!__cpu_simple_lock_try(kernel_lock));
750 1.105 ad
751 1.122 ad ci->ci_biglock_count = nlocks;
752 1.122 ad l->l_blcnt = nlocks;
753 1.107 ad LOCKSTAT_STOP_TIMER(lsflag, spintime);
754 1.132 ad LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS, 0);
755 1.132 ad if (owant == NULL) {
756 1.132 ad LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
757 1.132 ad LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
758 1.132 ad }
759 1.132 ad LOCKSTAT_EXIT(lsflag);
760 1.85 yamt splx(s);
761 1.105 ad
762 1.105 ad /*
763 1.132 ad * Now that we have kernel_lock, reset ci_biglock_wanted. This
764 1.132 ad * store must be unbuffered (immediately visible on the bus) in
765 1.132 ad * order for non-interlocked mutex release to work correctly.
766 1.132 ad * It must be visible before a mutex_exit() can execute on this
767 1.132 ad * processor.
768 1.132 ad *
769 1.132 ad * Note: only where CAS is available in hardware will this be
770 1.132 ad * an unbuffered write, but non-interlocked release cannot be
771 1.132 ad * done on CPUs without CAS in hardware.
772 1.105 ad */
773 1.132 ad (void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
774 1.132 ad
775 1.132 ad /*
776 1.132 ad * Issue a memory barrier as we have acquired a lock. This also
777 1.132 ad * prevents stores from a following mutex_exit() being reordered
778 1.132 ad * to occur before our store to ci_biglock_wanted above.
779 1.132 ad */
780 1.132 ad membar_enter();
781 1.62 thorpej }
782 1.62 thorpej
783 1.62 thorpej /*
784 1.105 ad * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
785 1.105 ad * all holds. If 'l' is non-null, the release is from process context.
786 1.62 thorpej */
787 1.62 thorpej void
788 1.105 ad _kernel_unlock(int nlocks, struct lwp *l, int *countp)
789 1.62 thorpej {
790 1.105 ad struct cpu_info *ci = curcpu();
791 1.105 ad u_int olocks;
792 1.105 ad int s;
793 1.62 thorpej
794 1.122 ad l = curlwp;
795 1.62 thorpej
796 1.105 ad _KERNEL_LOCK_ASSERT(nlocks < 2);
797 1.62 thorpej
798 1.122 ad olocks = l->l_blcnt;
799 1.77 yamt
800 1.105 ad if (olocks == 0) {
801 1.105 ad _KERNEL_LOCK_ASSERT(nlocks <= 0);
802 1.105 ad if (countp != NULL)
803 1.105 ad *countp = 0;
804 1.105 ad return;
805 1.105 ad }
806 1.77 yamt
807 1.132 ad _KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
808 1.85 yamt
809 1.105 ad if (nlocks == 0)
810 1.105 ad nlocks = olocks;
811 1.105 ad else if (nlocks == -1) {
812 1.105 ad nlocks = 1;
813 1.105 ad _KERNEL_LOCK_ASSERT(olocks == 1);
814 1.105 ad }
815 1.85 yamt
816 1.122 ad _KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
817 1.122 ad
818 1.122 ad l->l_blcnt -= nlocks;
819 1.122 ad if (ci->ci_biglock_count == nlocks) {
820 1.122 ad s = splvm();
821 1.132 ad LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
822 1.127 yamt RETURN_ADDRESS, 0);
823 1.122 ad ci->ci_biglock_count = 0;
824 1.132 ad __cpu_simple_unlock(kernel_lock);
825 1.122 ad splx(s);
826 1.122 ad } else
827 1.122 ad ci->ci_biglock_count -= nlocks;
828 1.77 yamt
829 1.105 ad if (countp != NULL)
830 1.105 ad *countp = olocks;
831 1.77 yamt }
832 1.124 pooka #endif /* !_RUMPKERNEL */
833