subr_lockdebug.c revision 1.1.2.5 1 /* $NetBSD: subr_lockdebug.c,v 1.1.2.5 2006/12/29 20:27:44 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Basic lock debugging code shared among lock primatives.
41 */
42
43 #include "opt_multiprocessor.h"
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.1.2.5 2006/12/29 20:27:44 ad Exp $");
47
48 #include <sys/param.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/kmem.h>
52 #include <sys/lock.h>
53 #include <sys/lockdebug.h>
54
55 #include <machine/cpu.h>
56
57 #ifdef LOCKDEBUG
58
59 #define LD_BATCH_SHIFT 9
60 #define LD_BATCH (1 << LD_BATCH_SHIFT)
61 #define LD_BATCH_MASK (LD_BATCH - 1)
62 #define LD_MAX_LOCKS 1048576
63 #define LD_SLOP 16
64
65 #define LD_LOCKED 0x01
66 #define LD_SLEEPER 0x02
67
68 #define LD_NOID LD_MAX_LOCKS
69
70 typedef struct lockdebuglk {
71 __cpu_simple_lock_t lk_lock;
72 int lk_oldspl;
73 } volatile lockdebuglk_t;
74
75 typedef struct lockdebug {
76 _TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
77 volatile void *ld_lock;
78 lockops_t *ld_lockops;
79 struct lwp *ld_lwp;
80 uintptr_t ld_locked;
81 uintptr_t ld_unlocked;
82 u_int ld_id;
83 u_short ld_cpu;
84 u_short ld_shares;
85 u_char ld_flags;
86 } volatile lockdebug_t;
87
88 typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
89
90 lockdebuglk_t ld_sleeper_lk;
91 lockdebuglk_t ld_spinner_lk;
92 lockdebuglk_t ld_free_lk;
93
94 lockdebuglist_t ld_sleepers;
95 lockdebuglist_t ld_spinners;
96 lockdebuglist_t ld_free;
97 int ld_nfree;
98 int ld_freeptr;
99 int ld_recurse;
100 lockdebug_t *ld_table[LD_MAX_LOCKS / LD_BATCH];
101
102 lockdebug_t ld_prime[LD_BATCH];
103
104 void lockdebug_abort1(lockdebug_t *, lockdebuglk_t *lk, const char *,
105 const char *);
106 void lockdebug_more(void);
107
108 static inline void
109 lockdebug_lock(lockdebuglk_t *lk)
110 {
111 int s;
112
113 s = spllock();
114 __cpu_simple_lock(&lk->lk_lock);
115 lk->lk_oldspl = s;
116 }
117
118 static inline void
119 lockdebug_unlock(lockdebuglk_t *lk)
120 {
121 int s;
122
123 s = lk->lk_oldspl;
124 __cpu_simple_unlock(&lk->lk_lock);
125 splx(s);
126 }
127
128 /*
129 * lockdebug_lookup:
130 *
131 * Find a lockdebug structure by ID and return it locked.
132 */
133 static inline lockdebug_t *
134 lockdebug_lookup(u_int id, lockdebuglk_t **lk)
135 {
136 lockdebug_t *base, *ld;
137
138 if (id == LD_NOID)
139 return NULL;
140
141 if (id == 0 || id >= LD_MAX_LOCKS)
142 panic("lockdebug_lookup: uninitialized lock (id=%d)", id);
143
144 base = ld_table[id >> LD_BATCH_SHIFT];
145 ld = base + (id & LD_BATCH_MASK);
146
147 if (base == NULL || ld->ld_lock == NULL || ld->ld_id != id)
148 panic("lockdebug_lookup: uninitialized lock (id=%d)", id);
149
150 if ((ld->ld_flags & LD_SLEEPER) != 0)
151 *lk = &ld_sleeper_lk;
152 else
153 *lk = &ld_spinner_lk;
154
155 lockdebug_lock(*lk);
156 return ld;
157 }
158
159 /*
160 * lockdebug_init:
161 *
162 * Initialize the lockdebug system. Allocate an initial pool of
163 * lockdebug structures before the VM system is up and running.
164 */
165 void
166 lockdebug_init(void)
167 {
168 lockdebug_t *ld;
169 int i;
170
171 __cpu_simple_lock_init(&ld_sleeper_lk.lk_lock);
172 __cpu_simple_lock_init(&ld_spinner_lk.lk_lock);
173 __cpu_simple_lock_init(&ld_free_lk.lk_lock);
174
175 TAILQ_INIT(&ld_free);
176 TAILQ_INIT(&ld_sleepers);
177 TAILQ_INIT(&ld_spinners);
178
179 ld = ld_prime;
180 ld_table[0] = ld;
181 for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
182 ld->ld_id = i;
183 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
184 }
185 ld_freeptr = 1;
186 ld_nfree = LD_BATCH - 1;
187 }
188
189 /*
190 * lockdebug_alloc:
191 *
192 * A lock is being initialized, so allocate an associated debug
193 * structure.
194 */
195 u_int
196 lockdebug_alloc(volatile void *lock, lockops_t *lo)
197 {
198 struct cpu_info *ci;
199 lockdebug_t *ld;
200
201 if (panicstr != NULL)
202 return 0;
203
204 if (ld_freeptr == 0)
205 panic("lockdebug_alloc: not initialized");
206
207 ci = curcpu();
208
209 /*
210 * Pinch a new debug structure. We may recurse because we call
211 * kmem_alloc(), which may need to initialize new locks somewhere
212 * down the path. If not recursing, we try to maintain at keep
213 * LD_SLOP structures free, which should hopefully be enough to
214 * satisfy kmem_alloc(). If we can't provide a structure, not to
215 * worry: we'll just mark the lock as not having an ID.
216 */
217 lockdebug_lock(&ld_free_lk);
218 ci->ci_lkdebug_recurse++;
219
220 if (TAILQ_EMPTY(&ld_free)) {
221 if (ci->ci_lkdebug_recurse > 1) {
222 ci->ci_lkdebug_recurse--;
223 lockdebug_unlock(&ld_free_lk);
224 return LD_NOID;
225 }
226 lockdebug_more();
227 } else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP)
228 lockdebug_more();
229
230 if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
231 lockdebug_unlock(&ld_free_lk);
232 return LD_NOID;
233 }
234
235 TAILQ_REMOVE(&ld_free, ld, ld_chain);
236 ld_nfree--;
237
238 ci->ci_lkdebug_recurse--;
239 lockdebug_unlock(&ld_free_lk);
240
241 if (ld->ld_lock != NULL)
242 panic("lockdebug_alloc: corrupt table");
243
244 if (lo->lo_sleeplock)
245 lockdebug_lock(&ld_sleeper_lk);
246 else
247 lockdebug_lock(&ld_spinner_lk);
248
249 /* Initialise the structure. */
250 ld->ld_lock = lock;
251 ld->ld_lockops = lo;
252 ld->ld_locked = 0;
253 ld->ld_unlocked = 0;
254 ld->ld_lwp = NULL;
255
256 if (lo->lo_sleeplock) {
257 ld->ld_flags = LD_SLEEPER;
258 lockdebug_unlock(&ld_sleeper_lk);
259 } else {
260 ld->ld_flags = 0;
261 lockdebug_unlock(&ld_spinner_lk);
262 }
263
264 return ld->ld_id;
265 }
266
267 /*
268 * lockdebug_free:
269 *
270 * A lock is being destroyed, so release debugging resources.
271 */
272 void
273 lockdebug_free(volatile void *lock, u_int id)
274 {
275 lockdebug_t *ld;
276 lockdebuglk_t *lk;
277
278 if (panicstr != NULL)
279 return;
280
281 if ((ld = lockdebug_lookup(id, &lk)) == NULL)
282 return;
283
284 if (ld->ld_lock != lock) {
285 panic("lockdebug_free: destroying uninitialized lock %p"
286 "(ld_id=%d ld_lock=%p)", lock, id, ld->ld_lock);
287 lockdebug_abort1(ld, lk, __FUNCTION__, "lock record follows");
288 }
289 if ((ld->ld_flags & LD_LOCKED) != 0)
290 lockdebug_abort1(ld, lk, __FUNCTION__, "is locked");
291
292 ld->ld_lock = NULL;
293
294 lockdebug_unlock(lk);
295
296 lockdebug_lock(&ld_free_lk);
297 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
298 ld_nfree++;
299 lockdebug_unlock(&ld_free_lk);
300 }
301
302 /*
303 * lockdebug_more:
304 *
305 * Allocate a batch of debug structures and add to the free list. Must
306 * be called with ld_free_lk held.
307 */
308 void
309 lockdebug_more(void)
310 {
311 lockdebug_t *ld;
312 void *block;
313 int i, base;
314
315 while (ld_nfree < LD_SLOP) {
316 lockdebug_unlock(&ld_free_lk);
317 block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
318 lockdebug_lock(&ld_free_lk);
319
320 if (block == NULL)
321 return;
322
323 if (ld_nfree > LD_SLOP) {
324 /* Somebody beat us to it. */
325 lockdebug_unlock(&ld_free_lk);
326 kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
327 lockdebug_lock(&ld_free_lk);
328 continue;
329 }
330
331 base = ld_freeptr;
332 ld_nfree += LD_BATCH;
333 ld = block;
334 base <<= LD_BATCH_SHIFT;
335
336 for (i = 0; i < LD_BATCH; i++, ld++) {
337 ld->ld_id = i + base;
338 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
339 }
340
341 mb_write();
342 ld_table[ld_freeptr++] = block;
343 }
344 }
345
346 /*
347 * lockdebug_locked:
348 *
349 * Process a lock acquire operation.
350 */
351 void
352 lockdebug_locked(u_int id, uintptr_t where, int shared)
353 {
354 struct lwp *l = curlwp;
355 lockdebuglk_t *lk;
356 lockdebug_t *ld;
357
358 if (panicstr != NULL)
359 return;
360
361 if ((ld = lockdebug_lookup(id, &lk)) == NULL)
362 return;
363
364 if ((ld->ld_flags & LD_LOCKED) != 0)
365 lockdebug_abort1(ld, lk, __FUNCTION__, "already locked");
366
367 if (shared) {
368 if (l == NULL)
369 lockdebug_abort1(ld, lk, __FUNCTION__, "releasing "
370 "shared lock from idle context");
371
372 l->l_shlocks++;
373 ld->ld_shares++;
374 } else {
375 ld->ld_flags |= LD_LOCKED;
376 ld->ld_locked = where;
377 ld->ld_cpu = (u_short)cpu_number();
378 ld->ld_lwp = l;
379
380 if ((ld->ld_flags & LD_SLEEPER) != 0) {
381 l->l_exlocks++;
382 TAILQ_INSERT_TAIL(&ld_sleepers, ld, ld_chain);
383 } else {
384 curcpu()->ci_spin_locks2++;
385 TAILQ_INSERT_TAIL(&ld_spinners, ld, ld_chain);
386 }
387 }
388
389 lockdebug_unlock(lk);
390 }
391
392 /*
393 * lockdebug_unlocked:
394 *
395 * Process a lock release operation.
396 */
397 void
398 lockdebug_unlocked(u_int id, uintptr_t where, int shared)
399 {
400 struct lwp *l = curlwp;
401 lockdebuglk_t *lk;
402 lockdebug_t *ld;
403
404 if (panicstr != NULL)
405 return;
406
407 if ((ld = lockdebug_lookup(id, &lk)) == NULL)
408 return;
409
410 if (shared) {
411 if (l == NULL)
412 lockdebug_abort1(ld, lk, __FUNCTION__, "acquiring "
413 "shared lock from idle context");
414 if (l->l_shlocks == 0)
415 lockdebug_abort1(ld, lk, __FUNCTION__, "no shared "
416 "locks held by LWP");
417 if (ld->ld_shares == 0)
418 lockdebug_abort1(ld, lk, __FUNCTION__, "no shared "
419 "holds on this lock");
420 l->l_shlocks--;
421 ld->ld_shares--;
422 } else {
423 if ((ld->ld_flags & LD_LOCKED) == 0)
424 lockdebug_abort1(ld, lk, __FUNCTION__, "not locked");
425
426 if ((ld->ld_flags & LD_SLEEPER) != 0) {
427 if (ld->ld_lwp != curlwp)
428 lockdebug_abort1(ld, lk, __FUNCTION__,
429 "not held by current LWP");
430 ld->ld_flags &= ~LD_LOCKED;
431 ld->ld_unlocked = where;
432 ld->ld_lwp = NULL;
433 curlwp->l_exlocks--;
434 TAILQ_REMOVE(&ld_sleepers, ld, ld_chain);
435 } else {
436 if (ld->ld_cpu != (u_short)cpu_number())
437 lockdebug_abort1(ld, lk, __FUNCTION__,
438 "not held by current CPU");
439 ld->ld_flags &= ~LD_LOCKED;
440 ld->ld_unlocked = where;
441 ld->ld_lwp = NULL;
442 curcpu()->ci_spin_locks2--;
443 TAILQ_REMOVE(&ld_spinners, ld, ld_chain);
444 }
445 }
446
447 lockdebug_unlock(lk);
448 }
449
450 /*
451 * lockdebug_barrier:
452 *
453 * Panic if we hold more than one specified spin lock, and optionally,
454 * if we hold sleep locks.
455 */
456 void
457 lockdebug_barrier(volatile void *spinlock, int slplocks)
458 {
459 struct lwp *l = curlwp;
460 lockdebug_t *ld;
461 u_short cpuno;
462
463 if (panicstr != NULL)
464 return;
465
466 if (curcpu()->ci_spin_locks2 != 0) {
467 cpuno = (u_short)cpu_number();
468
469 lockdebug_lock(&ld_spinner_lk);
470 TAILQ_FOREACH(ld, &ld_spinners, ld_chain) {
471 if (ld->ld_lock == spinlock) {
472 if (ld->ld_cpu != cpuno)
473 lockdebug_abort1(ld, &ld_spinner_lk,
474 __FUNCTION__,
475 "not held by current CPU");
476 continue;
477 }
478 if (ld->ld_cpu == cpuno)
479 lockdebug_abort1(ld, &ld_spinner_lk,
480 __FUNCTION__, "spin lock held");
481 }
482 lockdebug_unlock(&ld_spinner_lk);
483 }
484
485 if (!slplocks) {
486 if (l->l_exlocks != 0) {
487 lockdebug_lock(&ld_sleeper_lk);
488 TAILQ_FOREACH(ld, &ld_sleepers, ld_chain) {
489 if (ld->ld_lwp == l)
490 lockdebug_abort1(ld, &ld_sleeper_lk,
491 __FUNCTION__, "sleep lock held");
492 }
493 lockdebug_unlock(&ld_sleeper_lk);
494 }
495 if (l->l_shlocks != 0)
496 panic("lockdebug_barrier: holding %d shared locks",
497 l->l_shlocks);
498 }
499 }
500
501 void
502 lockdebug_abort1(lockdebug_t *ld, lockdebuglk_t *lk, const char *func,
503 const char *msg)
504 {
505
506 lockdebug_unlock(lk);
507
508 printf_nolog("%s error: %s: %s\n\n"
509 "lock address : %#018lx type : %18s\n"
510 "shared holds : %18d exclusive: %12slocked\n"
511 "last locked : %#018lx unlocked : %#018lx\n"
512 "current cpu : %18d last held: %18d\n"
513 "current lwp : %#018lx last held: %#018lx\n",
514 ld->ld_lockops->lo_name, func, msg, (long)ld->ld_lock,
515 ((ld->ld_flags & LD_SLEEPER) == 0 ? "spin" : "sleep"),
516 ld->ld_shares, ((ld->ld_flags & LD_LOCKED) == 0 ? "un" : " "),
517 (long)ld->ld_locked, (long)ld->ld_unlocked,
518 (int)cpu_number(), (int)ld->ld_cpu,
519 (long)curlwp, (long)ld->ld_lwp);
520
521 if (ld->ld_lockops->lo_dump != NULL)
522 (*ld->ld_lockops->lo_dump)(ld->ld_lock);
523
524 printf_nolog("\n");
525 panic("LOCKDEBUG");
526 }
527
528 #endif /* LOCKDEBUG */
529
530 /*
531 * lockdebug_abort:
532 *
533 * An error has been trapped - dump lock info and call panic().
534 */
535 void
536 lockdebug_abort(int id, volatile void *lock, lockops_t *ops,
537 const char *func, const char *msg)
538 {
539 #ifdef LOCKDEBUG
540 lockdebug_t *ld;
541 lockdebuglk_t *lk;
542
543 if ((ld = lockdebug_lookup(id, &lk)) != NULL) {
544 lockdebug_abort1(ld, lk, func, msg);
545 /* NOTREACHED */
546 }
547 #endif /* LOCKDEBUG */
548
549 printf_nolog("%s error: %s: %s\n\n"
550 "lock address : %#018lx\n"
551 "current cpu : %18d\n"
552 "current lwp : %#018lx\n",
553 ops->lo_name, func, msg, (long)lock, (int)cpu_number(),
554 (long)curlwp);
555
556 (*ops->lo_dump)(lock);
557
558 printf_nolog("\n");
559 panic("lock error");
560 }
561