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