subr_lockdebug.c revision 1.8.8.3 1 1.8.8.3 matt /* subr_lockdebug.c,v 1.8.8.2 2008/01/09 01:56:17 matt Exp */
2 1.2 ad
3 1.2 ad /*-
4 1.8.8.3 matt * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 1.2 ad * All rights reserved.
6 1.2 ad *
7 1.2 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 ad * by Andrew Doran.
9 1.2 ad *
10 1.2 ad * Redistribution and use in source and binary forms, with or without
11 1.2 ad * modification, are permitted provided that the following conditions
12 1.2 ad * are met:
13 1.2 ad * 1. Redistributions of source code must retain the above copyright
14 1.2 ad * notice, this list of conditions and the following disclaimer.
15 1.2 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 ad * notice, this list of conditions and the following disclaimer in the
17 1.2 ad * documentation and/or other materials provided with the distribution.
18 1.2 ad * 3. All advertising materials mentioning features or use of this software
19 1.2 ad * must display the following acknowledgement:
20 1.2 ad * This product includes software developed by the NetBSD
21 1.2 ad * Foundation, Inc. and its contributors.
22 1.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 ad * contributors may be used to endorse or promote products derived
24 1.2 ad * from this software without specific prior written permission.
25 1.2 ad *
26 1.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.2 ad */
38 1.2 ad
39 1.2 ad /*
40 1.8.8.1 matt * Basic lock debugging code shared among lock primitives.
41 1.2 ad */
42 1.2 ad
43 1.8.8.1 matt #include <sys/cdefs.h>
44 1.8.8.3 matt __KERNEL_RCSID(0, "subr_lockdebug.c,v 1.8.8.2 2008/01/09 01:56:17 matt Exp");
45 1.8.8.1 matt
46 1.2 ad #include "opt_ddb.h"
47 1.2 ad
48 1.2 ad #include <sys/param.h>
49 1.2 ad #include <sys/proc.h>
50 1.2 ad #include <sys/systm.h>
51 1.8.8.1 matt #include <sys/kernel.h>
52 1.2 ad #include <sys/kmem.h>
53 1.2 ad #include <sys/lockdebug.h>
54 1.2 ad #include <sys/sleepq.h>
55 1.8.8.1 matt #include <sys/cpu.h>
56 1.8.8.2 matt #include <sys/atomic.h>
57 1.8.8.2 matt #include <sys/lock.h>
58 1.8.8.2 matt
59 1.8.8.2 matt #include <lib/libkern/rb.h>
60 1.8.8.2 matt
61 1.8.8.2 matt #include <machine/lock.h>
62 1.2 ad
63 1.8.8.3 matt unsigned int ld_panic;
64 1.8.8.3 matt
65 1.2 ad #ifdef LOCKDEBUG
66 1.2 ad
67 1.2 ad #define LD_BATCH_SHIFT 9
68 1.2 ad #define LD_BATCH (1 << LD_BATCH_SHIFT)
69 1.2 ad #define LD_BATCH_MASK (LD_BATCH - 1)
70 1.2 ad #define LD_MAX_LOCKS 1048576
71 1.2 ad #define LD_SLOP 16
72 1.2 ad
73 1.2 ad #define LD_LOCKED 0x01
74 1.2 ad #define LD_SLEEPER 0x02
75 1.2 ad
76 1.8.8.2 matt #define LD_WRITE_LOCK 0x80000000
77 1.2 ad
78 1.2 ad typedef union lockdebuglk {
79 1.2 ad struct {
80 1.8.8.2 matt u_int lku_lock;
81 1.8.8.2 matt int lku_oldspl;
82 1.2 ad } ul;
83 1.8.8.2 matt uint8_t lk_pad[CACHE_LINE_SIZE];
84 1.8.8.2 matt } volatile __aligned(CACHE_LINE_SIZE) lockdebuglk_t;
85 1.2 ad
86 1.2 ad #define lk_lock ul.lku_lock
87 1.2 ad #define lk_oldspl ul.lku_oldspl
88 1.2 ad
89 1.2 ad typedef struct lockdebug {
90 1.8.8.2 matt struct rb_node ld_rb_node; /* must be the first member */
91 1.2 ad _TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
92 1.2 ad _TAILQ_ENTRY(struct lockdebug, volatile) ld_achain;
93 1.2 ad volatile void *ld_lock;
94 1.2 ad lockops_t *ld_lockops;
95 1.2 ad struct lwp *ld_lwp;
96 1.2 ad uintptr_t ld_locked;
97 1.2 ad uintptr_t ld_unlocked;
98 1.8.8.1 matt uintptr_t ld_initaddr;
99 1.2 ad uint16_t ld_shares;
100 1.2 ad uint16_t ld_cpu;
101 1.2 ad uint8_t ld_flags;
102 1.2 ad uint8_t ld_shwant; /* advisory */
103 1.2 ad uint8_t ld_exwant; /* advisory */
104 1.2 ad uint8_t ld_unused;
105 1.2 ad } volatile lockdebug_t;
106 1.2 ad
107 1.2 ad typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
108 1.2 ad
109 1.8.8.2 matt lockdebuglk_t ld_tree_lk;
110 1.2 ad lockdebuglk_t ld_sleeper_lk;
111 1.2 ad lockdebuglk_t ld_spinner_lk;
112 1.2 ad lockdebuglk_t ld_free_lk;
113 1.2 ad
114 1.8.8.2 matt lockdebuglist_t ld_sleepers = TAILQ_HEAD_INITIALIZER(ld_sleepers);
115 1.8.8.2 matt lockdebuglist_t ld_spinners = TAILQ_HEAD_INITIALIZER(ld_spinners);
116 1.8.8.2 matt lockdebuglist_t ld_free = TAILQ_HEAD_INITIALIZER(ld_free);
117 1.8.8.2 matt lockdebuglist_t ld_all = TAILQ_HEAD_INITIALIZER(ld_all);
118 1.2 ad int ld_nfree;
119 1.2 ad int ld_freeptr;
120 1.2 ad int ld_recurse;
121 1.5 ad bool ld_nomore;
122 1.2 ad lockdebug_t *ld_table[LD_MAX_LOCKS / LD_BATCH];
123 1.2 ad
124 1.2 ad lockdebug_t ld_prime[LD_BATCH];
125 1.2 ad
126 1.5 ad static void lockdebug_abort1(lockdebug_t *, lockdebuglk_t *lk,
127 1.8.8.1 matt const char *, const char *, bool);
128 1.5 ad static void lockdebug_more(void);
129 1.5 ad static void lockdebug_init(void);
130 1.2 ad
131 1.8.8.2 matt static signed int
132 1.8.8.2 matt ld_rb_compare_nodes(const struct rb_node *n1, const struct rb_node *n2)
133 1.8.8.2 matt {
134 1.8.8.2 matt const lockdebug_t *ld1 = (const void *)n1;
135 1.8.8.2 matt const lockdebug_t *ld2 = (const void *)n2;
136 1.8.8.2 matt const uintptr_t a = (uintptr_t)ld1->ld_lock;
137 1.8.8.2 matt const uintptr_t b = (uintptr_t)ld2->ld_lock;
138 1.8.8.2 matt
139 1.8.8.2 matt if (a < b)
140 1.8.8.2 matt return 1;
141 1.8.8.2 matt if (a > b)
142 1.8.8.2 matt return -1;
143 1.8.8.2 matt return 0;
144 1.8.8.2 matt }
145 1.8.8.2 matt
146 1.8.8.2 matt static signed int
147 1.8.8.2 matt ld_rb_compare_key(const struct rb_node *n, const void *key)
148 1.8.8.2 matt {
149 1.8.8.2 matt const lockdebug_t *ld = (const void *)n;
150 1.8.8.2 matt const uintptr_t a = (uintptr_t)ld->ld_lock;
151 1.8.8.2 matt const uintptr_t b = (uintptr_t)key;
152 1.8.8.2 matt
153 1.8.8.2 matt if (a < b)
154 1.8.8.2 matt return 1;
155 1.8.8.2 matt if (a > b)
156 1.8.8.2 matt return -1;
157 1.8.8.2 matt return 0;
158 1.8.8.2 matt }
159 1.8.8.2 matt
160 1.8.8.2 matt static struct rb_tree ld_rb_tree;
161 1.8.8.2 matt
162 1.8.8.2 matt static const struct rb_tree_ops ld_rb_tree_ops = {
163 1.8.8.2 matt .rb_compare_nodes = ld_rb_compare_nodes,
164 1.8.8.2 matt .rb_compare_key = ld_rb_compare_key,
165 1.8.8.2 matt };
166 1.8.8.2 matt
167 1.8.8.2 matt static void
168 1.8.8.2 matt lockdebug_lock_init(lockdebuglk_t *lk)
169 1.8.8.2 matt {
170 1.8.8.2 matt
171 1.8.8.2 matt lk->lk_lock = 0;
172 1.8.8.2 matt }
173 1.8.8.2 matt
174 1.8.8.2 matt static void
175 1.2 ad lockdebug_lock(lockdebuglk_t *lk)
176 1.2 ad {
177 1.2 ad int s;
178 1.2 ad
179 1.8 ad s = splhigh();
180 1.8.8.2 matt do {
181 1.8.8.2 matt while (lk->lk_lock != 0) {
182 1.8.8.2 matt SPINLOCK_SPIN_HOOK;
183 1.8.8.2 matt }
184 1.8.8.2 matt } while (atomic_cas_uint(&lk->lk_lock, 0, LD_WRITE_LOCK) != 0);
185 1.2 ad lk->lk_oldspl = s;
186 1.8.8.2 matt membar_enter();
187 1.2 ad }
188 1.2 ad
189 1.8.8.2 matt static void
190 1.2 ad lockdebug_unlock(lockdebuglk_t *lk)
191 1.2 ad {
192 1.2 ad int s;
193 1.2 ad
194 1.2 ad s = lk->lk_oldspl;
195 1.8.8.2 matt membar_exit();
196 1.8.8.2 matt lk->lk_lock = 0;
197 1.2 ad splx(s);
198 1.2 ad }
199 1.2 ad
200 1.8.8.2 matt static int
201 1.8.8.2 matt lockdebug_lock_rd(lockdebuglk_t *lk)
202 1.8.8.1 matt {
203 1.8.8.2 matt u_int val;
204 1.8.8.2 matt int s;
205 1.8.8.2 matt
206 1.8.8.2 matt s = splhigh();
207 1.8.8.2 matt do {
208 1.8.8.2 matt while ((val = lk->lk_lock) == LD_WRITE_LOCK){
209 1.8.8.2 matt SPINLOCK_SPIN_HOOK;
210 1.8.8.2 matt }
211 1.8.8.2 matt } while (atomic_cas_uint(&lk->lk_lock, val, val + 1) != val);
212 1.8.8.2 matt membar_enter();
213 1.8.8.2 matt return s;
214 1.8.8.2 matt }
215 1.8.8.1 matt
216 1.8.8.2 matt static void
217 1.8.8.2 matt lockdebug_unlock_rd(lockdebuglk_t *lk, int s)
218 1.8.8.2 matt {
219 1.8.8.2 matt
220 1.8.8.2 matt membar_exit();
221 1.8.8.2 matt atomic_dec_uint(&lk->lk_lock);
222 1.8.8.2 matt splx(s);
223 1.8.8.1 matt }
224 1.8.8.1 matt
225 1.2 ad static inline lockdebug_t *
226 1.8.8.2 matt lockdebug_lookup1(volatile void *lock, lockdebuglk_t **lk)
227 1.2 ad {
228 1.8.8.2 matt lockdebug_t *ld;
229 1.8.8.2 matt int s;
230 1.2 ad
231 1.8.8.2 matt s = lockdebug_lock_rd(&ld_tree_lk);
232 1.8.8.2 matt ld = (lockdebug_t *)rb_tree_find_node(&ld_rb_tree, __UNVOLATILE(lock));
233 1.8.8.2 matt lockdebug_unlock_rd(&ld_tree_lk, s);
234 1.8.8.2 matt if (ld == NULL)
235 1.2 ad return NULL;
236 1.2 ad
237 1.2 ad if ((ld->ld_flags & LD_SLEEPER) != 0)
238 1.2 ad *lk = &ld_sleeper_lk;
239 1.2 ad else
240 1.2 ad *lk = &ld_spinner_lk;
241 1.2 ad
242 1.2 ad lockdebug_lock(*lk);
243 1.2 ad return ld;
244 1.2 ad }
245 1.2 ad
246 1.2 ad /*
247 1.8.8.2 matt * lockdebug_lookup:
248 1.8.8.2 matt *
249 1.8.8.2 matt * Find a lockdebug structure by a pointer to a lock and return it locked.
250 1.8.8.2 matt */
251 1.8.8.2 matt static inline lockdebug_t *
252 1.8.8.2 matt lockdebug_lookup(volatile void *lock, lockdebuglk_t **lk)
253 1.8.8.2 matt {
254 1.8.8.2 matt lockdebug_t *ld;
255 1.8.8.2 matt
256 1.8.8.2 matt ld = lockdebug_lookup1(lock, lk);
257 1.8.8.2 matt if (ld == NULL)
258 1.8.8.2 matt panic("lockdebug_lookup: uninitialized lock (lock=%p)", lock);
259 1.8.8.2 matt return ld;
260 1.8.8.2 matt }
261 1.8.8.2 matt
262 1.8.8.2 matt /*
263 1.2 ad * lockdebug_init:
264 1.2 ad *
265 1.2 ad * Initialize the lockdebug system. Allocate an initial pool of
266 1.2 ad * lockdebug structures before the VM system is up and running.
267 1.2 ad */
268 1.5 ad static void
269 1.2 ad lockdebug_init(void)
270 1.2 ad {
271 1.2 ad lockdebug_t *ld;
272 1.2 ad int i;
273 1.2 ad
274 1.8.8.2 matt lockdebug_lock_init(&ld_tree_lk);
275 1.8.8.2 matt lockdebug_lock_init(&ld_sleeper_lk);
276 1.8.8.2 matt lockdebug_lock_init(&ld_spinner_lk);
277 1.8.8.2 matt lockdebug_lock_init(&ld_free_lk);
278 1.8.8.2 matt
279 1.8.8.2 matt rb_tree_init(&ld_rb_tree, &ld_rb_tree_ops);
280 1.2 ad
281 1.2 ad ld = ld_prime;
282 1.2 ad ld_table[0] = ld;
283 1.2 ad for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
284 1.2 ad TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
285 1.2 ad TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
286 1.2 ad }
287 1.2 ad ld_freeptr = 1;
288 1.2 ad ld_nfree = LD_BATCH - 1;
289 1.2 ad }
290 1.2 ad
291 1.2 ad /*
292 1.2 ad * lockdebug_alloc:
293 1.2 ad *
294 1.2 ad * A lock is being initialized, so allocate an associated debug
295 1.2 ad * structure.
296 1.2 ad */
297 1.8.8.2 matt bool
298 1.8.8.1 matt lockdebug_alloc(volatile void *lock, lockops_t *lo, uintptr_t initaddr)
299 1.2 ad {
300 1.2 ad struct cpu_info *ci;
301 1.2 ad lockdebug_t *ld;
302 1.8.8.2 matt lockdebuglk_t *lk;
303 1.2 ad
304 1.8.8.3 matt if (lo == NULL || panicstr != NULL || ld_panic)
305 1.8.8.2 matt return false;
306 1.5 ad if (ld_freeptr == 0)
307 1.5 ad lockdebug_init();
308 1.2 ad
309 1.8.8.2 matt if ((ld = lockdebug_lookup1(lock, &lk)) != NULL) {
310 1.8.8.2 matt lockdebug_abort1(ld, lk, __func__, "already initialized", true);
311 1.8.8.3 matt return false;
312 1.8.8.2 matt }
313 1.2 ad
314 1.2 ad /*
315 1.2 ad * Pinch a new debug structure. We may recurse because we call
316 1.2 ad * kmem_alloc(), which may need to initialize new locks somewhere
317 1.7 skrll * down the path. If not recursing, we try to maintain at least
318 1.2 ad * LD_SLOP structures free, which should hopefully be enough to
319 1.2 ad * satisfy kmem_alloc(). If we can't provide a structure, not to
320 1.2 ad * worry: we'll just mark the lock as not having an ID.
321 1.2 ad */
322 1.2 ad lockdebug_lock(&ld_free_lk);
323 1.8.8.2 matt ci = curcpu();
324 1.2 ad ci->ci_lkdebug_recurse++;
325 1.2 ad
326 1.2 ad if (TAILQ_EMPTY(&ld_free)) {
327 1.5 ad if (ci->ci_lkdebug_recurse > 1 || ld_nomore) {
328 1.2 ad ci->ci_lkdebug_recurse--;
329 1.2 ad lockdebug_unlock(&ld_free_lk);
330 1.8.8.2 matt return false;
331 1.2 ad }
332 1.2 ad lockdebug_more();
333 1.2 ad } else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP)
334 1.2 ad lockdebug_more();
335 1.2 ad
336 1.2 ad if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
337 1.2 ad lockdebug_unlock(&ld_free_lk);
338 1.8.8.2 matt return false;
339 1.2 ad }
340 1.2 ad
341 1.2 ad TAILQ_REMOVE(&ld_free, ld, ld_chain);
342 1.2 ad ld_nfree--;
343 1.2 ad
344 1.2 ad ci->ci_lkdebug_recurse--;
345 1.2 ad lockdebug_unlock(&ld_free_lk);
346 1.2 ad
347 1.2 ad if (ld->ld_lock != NULL)
348 1.2 ad panic("lockdebug_alloc: corrupt table");
349 1.2 ad
350 1.2 ad if (lo->lo_sleeplock)
351 1.2 ad lockdebug_lock(&ld_sleeper_lk);
352 1.2 ad else
353 1.2 ad lockdebug_lock(&ld_spinner_lk);
354 1.2 ad
355 1.2 ad /* Initialise the structure. */
356 1.2 ad ld->ld_lock = lock;
357 1.2 ad ld->ld_lockops = lo;
358 1.2 ad ld->ld_locked = 0;
359 1.2 ad ld->ld_unlocked = 0;
360 1.2 ad ld->ld_lwp = NULL;
361 1.8.8.1 matt ld->ld_initaddr = initaddr;
362 1.2 ad
363 1.8.8.2 matt lockdebug_lock(&ld_tree_lk);
364 1.8.8.2 matt rb_tree_insert_node(&ld_rb_tree, __UNVOLATILE(&ld->ld_rb_node));
365 1.8.8.2 matt lockdebug_unlock(&ld_tree_lk);
366 1.8.8.2 matt
367 1.2 ad if (lo->lo_sleeplock) {
368 1.2 ad ld->ld_flags = LD_SLEEPER;
369 1.2 ad lockdebug_unlock(&ld_sleeper_lk);
370 1.2 ad } else {
371 1.2 ad ld->ld_flags = 0;
372 1.2 ad lockdebug_unlock(&ld_spinner_lk);
373 1.2 ad }
374 1.2 ad
375 1.8.8.2 matt return true;
376 1.2 ad }
377 1.2 ad
378 1.2 ad /*
379 1.2 ad * lockdebug_free:
380 1.2 ad *
381 1.2 ad * A lock is being destroyed, so release debugging resources.
382 1.2 ad */
383 1.2 ad void
384 1.8.8.2 matt lockdebug_free(volatile void *lock)
385 1.2 ad {
386 1.2 ad lockdebug_t *ld;
387 1.2 ad lockdebuglk_t *lk;
388 1.2 ad
389 1.8.8.3 matt if (panicstr != NULL || ld_panic)
390 1.2 ad return;
391 1.2 ad
392 1.8.8.2 matt ld = lockdebug_lookup(lock, &lk);
393 1.8.8.2 matt if (ld == NULL) {
394 1.2 ad panic("lockdebug_free: destroying uninitialized lock %p"
395 1.8.8.2 matt "(ld_lock=%p)", lock, ld->ld_lock);
396 1.8.8.1 matt lockdebug_abort1(ld, lk, __func__, "lock record follows",
397 1.8.8.1 matt true);
398 1.8.8.3 matt return;
399 1.2 ad }
400 1.8.8.3 matt if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
401 1.8.8.1 matt lockdebug_abort1(ld, lk, __func__, "is locked", true);
402 1.8.8.3 matt return;
403 1.8.8.3 matt }
404 1.8.8.2 matt lockdebug_lock(&ld_tree_lk);
405 1.8.8.2 matt rb_tree_remove_node(&ld_rb_tree, __UNVOLATILE(&ld->ld_rb_node));
406 1.8.8.2 matt lockdebug_unlock(&ld_tree_lk);
407 1.2 ad ld->ld_lock = NULL;
408 1.2 ad lockdebug_unlock(lk);
409 1.2 ad
410 1.2 ad lockdebug_lock(&ld_free_lk);
411 1.2 ad TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
412 1.2 ad ld_nfree++;
413 1.2 ad lockdebug_unlock(&ld_free_lk);
414 1.2 ad }
415 1.2 ad
416 1.2 ad /*
417 1.2 ad * lockdebug_more:
418 1.2 ad *
419 1.2 ad * Allocate a batch of debug structures and add to the free list.
420 1.2 ad * Must be called with ld_free_lk held.
421 1.2 ad */
422 1.5 ad static void
423 1.2 ad lockdebug_more(void)
424 1.2 ad {
425 1.2 ad lockdebug_t *ld;
426 1.2 ad void *block;
427 1.5 ad int i, base, m;
428 1.2 ad
429 1.2 ad while (ld_nfree < LD_SLOP) {
430 1.2 ad lockdebug_unlock(&ld_free_lk);
431 1.2 ad block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
432 1.2 ad lockdebug_lock(&ld_free_lk);
433 1.2 ad
434 1.2 ad if (block == NULL)
435 1.2 ad return;
436 1.2 ad
437 1.2 ad if (ld_nfree > LD_SLOP) {
438 1.2 ad /* Somebody beat us to it. */
439 1.2 ad lockdebug_unlock(&ld_free_lk);
440 1.2 ad kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
441 1.2 ad lockdebug_lock(&ld_free_lk);
442 1.2 ad continue;
443 1.2 ad }
444 1.2 ad
445 1.2 ad base = ld_freeptr;
446 1.2 ad ld_nfree += LD_BATCH;
447 1.2 ad ld = block;
448 1.2 ad base <<= LD_BATCH_SHIFT;
449 1.5 ad m = min(LD_MAX_LOCKS, base + LD_BATCH);
450 1.5 ad
451 1.5 ad if (m == LD_MAX_LOCKS)
452 1.5 ad ld_nomore = true;
453 1.2 ad
454 1.5 ad for (i = base; i < m; i++, ld++) {
455 1.2 ad TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
456 1.2 ad TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
457 1.2 ad }
458 1.2 ad
459 1.8.8.2 matt membar_producer();
460 1.2 ad ld_table[ld_freeptr++] = block;
461 1.2 ad }
462 1.2 ad }
463 1.2 ad
464 1.2 ad /*
465 1.2 ad * lockdebug_wantlock:
466 1.2 ad *
467 1.2 ad * Process the preamble to a lock acquire.
468 1.2 ad */
469 1.2 ad void
470 1.8.8.2 matt lockdebug_wantlock(volatile void *lock, uintptr_t where, int shared)
471 1.2 ad {
472 1.2 ad struct lwp *l = curlwp;
473 1.2 ad lockdebuglk_t *lk;
474 1.2 ad lockdebug_t *ld;
475 1.3 thorpej bool recurse;
476 1.2 ad
477 1.2 ad (void)shared;
478 1.4 thorpej recurse = false;
479 1.2 ad
480 1.8.8.3 matt if (panicstr != NULL || ld_panic)
481 1.2 ad return;
482 1.2 ad
483 1.8.8.2 matt if ((ld = lockdebug_lookup(lock, &lk)) == NULL)
484 1.2 ad return;
485 1.2 ad
486 1.2 ad if ((ld->ld_flags & LD_LOCKED) != 0) {
487 1.2 ad if ((ld->ld_flags & LD_SLEEPER) != 0) {
488 1.2 ad if (ld->ld_lwp == l)
489 1.4 thorpej recurse = true;
490 1.2 ad } else if (ld->ld_cpu == (uint16_t)cpu_number())
491 1.4 thorpej recurse = true;
492 1.2 ad }
493 1.2 ad
494 1.8.8.1 matt if (cpu_intr_p()) {
495 1.8.8.3 matt if ((ld->ld_flags & LD_SLEEPER) != 0) {
496 1.8.8.1 matt lockdebug_abort1(ld, lk, __func__,
497 1.8.8.1 matt "acquiring sleep lock from interrupt context",
498 1.8.8.1 matt true);
499 1.8.8.3 matt return;
500 1.8.8.3 matt }
501 1.8.8.1 matt }
502 1.8.8.1 matt
503 1.2 ad if (shared)
504 1.2 ad ld->ld_shwant++;
505 1.2 ad else
506 1.2 ad ld->ld_exwant++;
507 1.2 ad
508 1.8.8.3 matt if (recurse) {
509 1.8.8.1 matt lockdebug_abort1(ld, lk, __func__, "locking against myself",
510 1.8.8.1 matt true);
511 1.8.8.3 matt return;
512 1.8.8.3 matt }
513 1.2 ad
514 1.2 ad lockdebug_unlock(lk);
515 1.2 ad }
516 1.2 ad
517 1.2 ad /*
518 1.2 ad * lockdebug_locked:
519 1.2 ad *
520 1.2 ad * Process a lock acquire operation.
521 1.2 ad */
522 1.2 ad void
523 1.8.8.2 matt lockdebug_locked(volatile void *lock, uintptr_t where, int shared)
524 1.2 ad {
525 1.2 ad struct lwp *l = curlwp;
526 1.2 ad lockdebuglk_t *lk;
527 1.2 ad lockdebug_t *ld;
528 1.2 ad
529 1.8.8.3 matt if (panicstr != NULL || ld_panic)
530 1.2 ad return;
531 1.2 ad
532 1.8.8.2 matt if ((ld = lockdebug_lookup(lock, &lk)) == NULL)
533 1.2 ad return;
534 1.2 ad
535 1.2 ad if (shared) {
536 1.2 ad l->l_shlocks++;
537 1.2 ad ld->ld_shares++;
538 1.2 ad ld->ld_shwant--;
539 1.2 ad } else {
540 1.8.8.3 matt if ((ld->ld_flags & LD_LOCKED) != 0) {
541 1.2 ad lockdebug_abort1(ld, lk, __func__,
542 1.8.8.1 matt "already locked", true);
543 1.8.8.3 matt return;
544 1.8.8.3 matt }
545 1.2 ad
546 1.2 ad ld->ld_flags |= LD_LOCKED;
547 1.2 ad ld->ld_locked = where;
548 1.2 ad ld->ld_cpu = (uint16_t)cpu_number();
549 1.2 ad ld->ld_lwp = l;
550 1.2 ad ld->ld_exwant--;
551 1.2 ad
552 1.2 ad if ((ld->ld_flags & LD_SLEEPER) != 0) {
553 1.2 ad l->l_exlocks++;
554 1.2 ad TAILQ_INSERT_TAIL(&ld_sleepers, ld, ld_chain);
555 1.2 ad } else {
556 1.2 ad curcpu()->ci_spin_locks2++;
557 1.2 ad TAILQ_INSERT_TAIL(&ld_spinners, ld, ld_chain);
558 1.2 ad }
559 1.2 ad }
560 1.2 ad
561 1.2 ad lockdebug_unlock(lk);
562 1.2 ad }
563 1.2 ad
564 1.2 ad /*
565 1.2 ad * lockdebug_unlocked:
566 1.2 ad *
567 1.2 ad * Process a lock release operation.
568 1.2 ad */
569 1.2 ad void
570 1.8.8.2 matt lockdebug_unlocked(volatile void *lock, uintptr_t where, int shared)
571 1.2 ad {
572 1.2 ad struct lwp *l = curlwp;
573 1.2 ad lockdebuglk_t *lk;
574 1.2 ad lockdebug_t *ld;
575 1.2 ad
576 1.8.8.3 matt if (panicstr != NULL || ld_panic)
577 1.2 ad return;
578 1.2 ad
579 1.8.8.2 matt if ((ld = lockdebug_lookup(lock, &lk)) == NULL)
580 1.2 ad return;
581 1.2 ad
582 1.2 ad if (shared) {
583 1.8.8.3 matt if (l->l_shlocks == 0) {
584 1.2 ad lockdebug_abort1(ld, lk, __func__,
585 1.8.8.1 matt "no shared locks held by LWP", true);
586 1.8.8.3 matt return;
587 1.8.8.3 matt }
588 1.8.8.3 matt if (ld->ld_shares == 0) {
589 1.2 ad lockdebug_abort1(ld, lk, __func__,
590 1.8.8.1 matt "no shared holds on this lock", true);
591 1.8.8.3 matt return;
592 1.8.8.3 matt }
593 1.2 ad l->l_shlocks--;
594 1.2 ad ld->ld_shares--;
595 1.2 ad } else {
596 1.8.8.3 matt if ((ld->ld_flags & LD_LOCKED) == 0) {
597 1.8.8.1 matt lockdebug_abort1(ld, lk, __func__, "not locked",
598 1.8.8.1 matt true);
599 1.8.8.3 matt return;
600 1.8.8.3 matt }
601 1.2 ad
602 1.2 ad if ((ld->ld_flags & LD_SLEEPER) != 0) {
603 1.8.8.3 matt if (ld->ld_lwp != curlwp) {
604 1.2 ad lockdebug_abort1(ld, lk, __func__,
605 1.8.8.1 matt "not held by current LWP", true);
606 1.8.8.3 matt return;
607 1.8.8.3 matt }
608 1.2 ad ld->ld_flags &= ~LD_LOCKED;
609 1.2 ad ld->ld_unlocked = where;
610 1.2 ad ld->ld_lwp = NULL;
611 1.2 ad curlwp->l_exlocks--;
612 1.2 ad TAILQ_REMOVE(&ld_sleepers, ld, ld_chain);
613 1.2 ad } else {
614 1.8.8.3 matt if (ld->ld_cpu != (uint16_t)cpu_number()) {
615 1.2 ad lockdebug_abort1(ld, lk, __func__,
616 1.8.8.1 matt "not held by current CPU", true);
617 1.8.8.3 matt return;
618 1.8.8.3 matt }
619 1.2 ad ld->ld_flags &= ~LD_LOCKED;
620 1.2 ad ld->ld_unlocked = where;
621 1.2 ad ld->ld_lwp = NULL;
622 1.2 ad curcpu()->ci_spin_locks2--;
623 1.2 ad TAILQ_REMOVE(&ld_spinners, ld, ld_chain);
624 1.2 ad }
625 1.2 ad }
626 1.2 ad
627 1.2 ad lockdebug_unlock(lk);
628 1.2 ad }
629 1.2 ad
630 1.2 ad /*
631 1.2 ad * lockdebug_barrier:
632 1.2 ad *
633 1.2 ad * Panic if we hold more than one specified spin lock, and optionally,
634 1.2 ad * if we hold sleep locks.
635 1.2 ad */
636 1.2 ad void
637 1.2 ad lockdebug_barrier(volatile void *spinlock, int slplocks)
638 1.2 ad {
639 1.2 ad struct lwp *l = curlwp;
640 1.2 ad lockdebug_t *ld;
641 1.2 ad uint16_t cpuno;
642 1.8.8.2 matt int s;
643 1.2 ad
644 1.8.8.3 matt if (panicstr != NULL || ld_panic)
645 1.2 ad return;
646 1.2 ad
647 1.8.8.2 matt crit_enter();
648 1.8.8.2 matt
649 1.2 ad if (curcpu()->ci_spin_locks2 != 0) {
650 1.2 ad cpuno = (uint16_t)cpu_number();
651 1.2 ad
652 1.8.8.2 matt s = lockdebug_lock_rd(&ld_spinner_lk);
653 1.2 ad TAILQ_FOREACH(ld, &ld_spinners, ld_chain) {
654 1.2 ad if (ld->ld_lock == spinlock) {
655 1.8.8.3 matt if (ld->ld_cpu != cpuno) {
656 1.2 ad lockdebug_abort1(ld, &ld_spinner_lk,
657 1.2 ad __func__,
658 1.8.8.1 matt "not held by current CPU", true);
659 1.8.8.3 matt return;
660 1.8.8.3 matt }
661 1.2 ad continue;
662 1.2 ad }
663 1.8.8.3 matt if (ld->ld_cpu == cpuno && (l->l_pflag & LP_INTR) == 0) {
664 1.2 ad lockdebug_abort1(ld, &ld_spinner_lk,
665 1.8.8.1 matt __func__, "spin lock held", true);
666 1.8.8.3 matt return;
667 1.8.8.3 matt }
668 1.2 ad }
669 1.8.8.2 matt lockdebug_unlock_rd(&ld_spinner_lk, s);
670 1.2 ad }
671 1.2 ad
672 1.2 ad if (!slplocks) {
673 1.2 ad if (l->l_exlocks != 0) {
674 1.8.8.2 matt s = lockdebug_lock_rd(&ld_sleeper_lk);
675 1.2 ad TAILQ_FOREACH(ld, &ld_sleepers, ld_chain) {
676 1.8.8.3 matt if (ld->ld_lwp == l) {
677 1.2 ad lockdebug_abort1(ld, &ld_sleeper_lk,
678 1.8.8.1 matt __func__, "sleep lock held", true);
679 1.8.8.3 matt return;
680 1.8.8.3 matt }
681 1.2 ad }
682 1.8.8.2 matt lockdebug_unlock_rd(&ld_sleeper_lk, s);
683 1.2 ad }
684 1.2 ad if (l->l_shlocks != 0)
685 1.2 ad panic("lockdebug_barrier: holding %d shared locks",
686 1.2 ad l->l_shlocks);
687 1.2 ad }
688 1.8.8.2 matt
689 1.8.8.2 matt crit_exit();
690 1.2 ad }
691 1.2 ad
692 1.2 ad /*
693 1.8.8.1 matt * lockdebug_mem_check:
694 1.8.8.1 matt *
695 1.8.8.1 matt * Check for in-use locks within a memory region that is
696 1.8.8.2 matt * being freed.
697 1.8.8.1 matt */
698 1.8.8.1 matt void
699 1.8.8.1 matt lockdebug_mem_check(const char *func, void *base, size_t sz)
700 1.8.8.1 matt {
701 1.8.8.1 matt lockdebug_t *ld;
702 1.8.8.2 matt lockdebuglk_t *lk;
703 1.8.8.2 matt int s;
704 1.8.8.1 matt
705 1.8.8.3 matt if (panicstr != NULL || ld_panic)
706 1.8.8.2 matt return;
707 1.8.8.1 matt
708 1.8.8.2 matt s = lockdebug_lock_rd(&ld_tree_lk);
709 1.8.8.2 matt ld = (lockdebug_t *)rb_tree_find_node_geq(&ld_rb_tree, base);
710 1.8.8.2 matt if (ld != NULL) {
711 1.8.8.2 matt const uintptr_t lock = (uintptr_t)ld->ld_lock;
712 1.8.8.2 matt
713 1.8.8.2 matt if ((uintptr_t)base > lock)
714 1.8.8.2 matt panic("%s: corrupt tree ld=%p, base=%p, sz=%zu",
715 1.8.8.2 matt __func__, ld, base, sz);
716 1.8.8.2 matt if (lock >= (uintptr_t)base + sz)
717 1.8.8.2 matt ld = NULL;
718 1.8.8.1 matt }
719 1.8.8.2 matt lockdebug_unlock_rd(&ld_tree_lk, s);
720 1.8.8.2 matt if (ld == NULL)
721 1.8.8.2 matt return;
722 1.8.8.2 matt
723 1.8.8.2 matt if ((ld->ld_flags & LD_SLEEPER) != 0)
724 1.8.8.2 matt lk = &ld_sleeper_lk;
725 1.8.8.2 matt else
726 1.8.8.2 matt lk = &ld_spinner_lk;
727 1.8.8.2 matt
728 1.8.8.2 matt lockdebug_lock(lk);
729 1.8.8.2 matt lockdebug_abort1(ld, lk, func,
730 1.8.8.2 matt "allocation contains active lock", !cold);
731 1.8.8.1 matt }
732 1.8.8.1 matt
733 1.8.8.1 matt /*
734 1.2 ad * lockdebug_dump:
735 1.2 ad *
736 1.2 ad * Dump information about a lock on panic, or for DDB.
737 1.2 ad */
738 1.2 ad static void
739 1.2 ad lockdebug_dump(lockdebug_t *ld, void (*pr)(const char *, ...))
740 1.2 ad {
741 1.2 ad int sleeper = (ld->ld_flags & LD_SLEEPER);
742 1.2 ad
743 1.2 ad (*pr)(
744 1.2 ad "lock address : %#018lx type : %18s\n"
745 1.2 ad "shared holds : %18u exclusive: %18u\n"
746 1.2 ad "shares wanted: %18u exclusive: %18u\n"
747 1.2 ad "current cpu : %18u last held: %18u\n"
748 1.2 ad "current lwp : %#018lx last held: %#018lx\n"
749 1.8.8.1 matt "last locked : %#018lx unlocked : %#018lx\n"
750 1.8.8.1 matt "initialized : %#018lx\n",
751 1.2 ad (long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
752 1.2 ad (unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
753 1.2 ad (unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
754 1.2 ad (unsigned)cpu_number(), (unsigned)ld->ld_cpu,
755 1.2 ad (long)curlwp, (long)ld->ld_lwp,
756 1.8.8.1 matt (long)ld->ld_locked, (long)ld->ld_unlocked,
757 1.8.8.1 matt (long)ld->ld_initaddr);
758 1.2 ad
759 1.2 ad if (ld->ld_lockops->lo_dump != NULL)
760 1.2 ad (*ld->ld_lockops->lo_dump)(ld->ld_lock);
761 1.2 ad
762 1.2 ad if (sleeper) {
763 1.2 ad (*pr)("\n");
764 1.2 ad turnstile_print(ld->ld_lock, pr);
765 1.2 ad }
766 1.2 ad }
767 1.2 ad
768 1.2 ad /*
769 1.8.8.3 matt * lockdebug_abort1:
770 1.2 ad *
771 1.8.8.3 matt * An error has been trapped - dump lock info and panic.
772 1.2 ad */
773 1.5 ad static void
774 1.2 ad lockdebug_abort1(lockdebug_t *ld, lockdebuglk_t *lk, const char *func,
775 1.8.8.1 matt const char *msg, bool dopanic)
776 1.2 ad {
777 1.2 ad
778 1.8.8.3 matt /*
779 1.8.8.3 matt * Don't make the situation wose if the system is already going
780 1.8.8.3 matt * down in flames. Once a panic is triggered, lockdebug state
781 1.8.8.3 matt * becomes stale and cannot be trusted.
782 1.8.8.3 matt */
783 1.8.8.3 matt if (atomic_inc_uint_nv(&ld_panic) != 1) {
784 1.8.8.3 matt lockdebug_unlock(lk);
785 1.8.8.3 matt return;
786 1.8.8.3 matt }
787 1.8.8.3 matt
788 1.2 ad printf_nolog("%s error: %s: %s\n\n", ld->ld_lockops->lo_name,
789 1.2 ad func, msg);
790 1.2 ad lockdebug_dump(ld, printf_nolog);
791 1.2 ad lockdebug_unlock(lk);
792 1.2 ad printf_nolog("\n");
793 1.8.8.1 matt if (dopanic)
794 1.8.8.1 matt panic("LOCKDEBUG");
795 1.2 ad }
796 1.2 ad
797 1.2 ad #endif /* LOCKDEBUG */
798 1.2 ad
799 1.2 ad /*
800 1.2 ad * lockdebug_lock_print:
801 1.2 ad *
802 1.2 ad * Handle the DDB 'show lock' command.
803 1.2 ad */
804 1.2 ad #ifdef DDB
805 1.2 ad void
806 1.2 ad lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
807 1.2 ad {
808 1.2 ad #ifdef LOCKDEBUG
809 1.2 ad lockdebug_t *ld;
810 1.2 ad
811 1.2 ad TAILQ_FOREACH(ld, &ld_all, ld_achain) {
812 1.2 ad if (ld->ld_lock == addr) {
813 1.2 ad lockdebug_dump(ld, pr);
814 1.2 ad return;
815 1.2 ad }
816 1.2 ad }
817 1.2 ad (*pr)("Sorry, no record of a lock with address %p found.\n", addr);
818 1.2 ad #else
819 1.2 ad (*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
820 1.2 ad #endif /* LOCKDEBUG */
821 1.2 ad }
822 1.2 ad #endif /* DDB */
823 1.2 ad
824 1.2 ad /*
825 1.2 ad * lockdebug_abort:
826 1.2 ad *
827 1.2 ad * An error has been trapped - dump lock info and call panic().
828 1.2 ad */
829 1.2 ad void
830 1.8.8.2 matt lockdebug_abort(volatile void *lock, lockops_t *ops, const char *func,
831 1.8.8.2 matt const char *msg)
832 1.2 ad {
833 1.2 ad #ifdef LOCKDEBUG
834 1.2 ad lockdebug_t *ld;
835 1.2 ad lockdebuglk_t *lk;
836 1.2 ad
837 1.8.8.2 matt if ((ld = lockdebug_lookup(lock, &lk)) != NULL) {
838 1.8.8.1 matt lockdebug_abort1(ld, lk, func, msg, true);
839 1.2 ad /* NOTREACHED */
840 1.2 ad }
841 1.2 ad #endif /* LOCKDEBUG */
842 1.2 ad
843 1.8.8.3 matt /*
844 1.8.8.3 matt * Complain first on the occurrance only. Otherwise proceeed to
845 1.8.8.3 matt * panic where we will `rendezvous' with other CPUs if the machine
846 1.8.8.3 matt * is going down in flames.
847 1.8.8.3 matt */
848 1.8.8.3 matt if (atomic_inc_uint_nv(&ld_panic) == 1) {
849 1.8.8.3 matt printf_nolog("%s error: %s: %s\n\n"
850 1.8.8.3 matt "lock address : %#018lx\n"
851 1.8.8.3 matt "current cpu : %18d\n"
852 1.8.8.3 matt "current lwp : %#018lx\n",
853 1.8.8.3 matt ops->lo_name, func, msg, (long)lock, (int)cpu_number(),
854 1.8.8.3 matt (long)curlwp);
855 1.8.8.3 matt (*ops->lo_dump)(lock);
856 1.8.8.3 matt printf_nolog("\n");
857 1.8.8.3 matt }
858 1.2 ad
859 1.2 ad panic("lock error");
860 1.2 ad }
861