xref: /freebsd/sys/kern/subr_lock.c (revision d101cdd6edd782f6ec56eef63ed91abd77a8b317)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * This module holds the global variables and functions used to maintain
30  * lock_object structures.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_ddb.h"
37 #include "opt_mprof.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/ktr.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/lock_profile.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/pcpu.h>
49 #include <sys/proc.h>
50 #include <sys/sbuf.h>
51 #include <sys/sched.h>
52 #include <sys/smp.h>
53 #include <sys/sysctl.h>
54 
55 #ifdef DDB
56 #include <ddb/ddb.h>
57 #endif
58 
59 #include <machine/cpufunc.h>
60 
61 /*
62  * Uncomment to validate that spin argument to acquire/release routines matches
63  * the flag in the lock
64  */
65 //#define	LOCK_PROFILING_DEBUG_SPIN
66 
67 CTASSERT(LOCK_CLASS_MAX == 15);
68 
69 struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = {
70 	&lock_class_mtx_spin,
71 	&lock_class_mtx_sleep,
72 	&lock_class_sx,
73 	&lock_class_rm,
74 	&lock_class_rm_sleepable,
75 	&lock_class_rw,
76 	&lock_class_lockmgr,
77 };
78 
79 void
80 lock_init(struct lock_object *lock, struct lock_class *class, const char *name,
81     const char *type, int flags)
82 {
83 	int i;
84 
85 	/* Check for double-init and zero object. */
86 	KASSERT(flags & LO_NEW || !lock_initialized(lock),
87 	    ("lock \"%s\" %p already initialized", name, lock));
88 
89 	/* Look up lock class to find its index. */
90 	for (i = 0; i < LOCK_CLASS_MAX; i++)
91 		if (lock_classes[i] == class) {
92 			lock->lo_flags = i << LO_CLASSSHIFT;
93 			break;
94 		}
95 	KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));
96 
97 	/* Initialize the lock object. */
98 	lock->lo_name = name;
99 	lock->lo_flags |= flags | LO_INITIALIZED;
100 	LOCK_LOG_INIT(lock, 0);
101 	WITNESS_INIT(lock, (type != NULL) ? type : name);
102 }
103 
104 void
105 lock_destroy(struct lock_object *lock)
106 {
107 
108 	KASSERT(lock_initialized(lock), ("lock %p is not initialized", lock));
109 	WITNESS_DESTROY(lock);
110 	LOCK_LOG_DESTROY(lock, 0);
111 	lock->lo_flags &= ~LO_INITIALIZED;
112 }
113 
114 static SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
115     "lock debugging");
116 static SYSCTL_NODE(_debug_lock, OID_AUTO, delay,
117     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
118     "lock delay");
119 
120 void
121 lock_delay(struct lock_delay_arg *la)
122 {
123 	struct lock_delay_config *lc = la->config;
124 	u_short i;
125 
126 	for (i = la->delay; i > 0; i--)
127 		cpu_spinwait();
128 	la->spin_cnt += la->delay;
129 
130 	la->delay <<= 1;
131 	if (__predict_false(la->delay > lc->max))
132 		la->delay = lc->max;
133 }
134 
135 static u_int
136 lock_roundup_2(u_int val)
137 {
138 	u_int res;
139 
140 	for (res = 1; res <= val; res <<= 1)
141 		continue;
142 
143 	return (res);
144 }
145 
146 void
147 lock_delay_default_init(struct lock_delay_config *lc)
148 {
149 
150 	lc->base = 1;
151 	lc->max = lock_roundup_2(mp_ncpus) * 256;
152 	if (lc->max > 32678)
153 		lc->max = 32678;
154 }
155 
156 struct lock_delay_config __read_frequently locks_delay;
157 u_short __read_frequently locks_delay_retries;
158 u_short __read_frequently locks_delay_loops;
159 
160 SYSCTL_U16(_debug_lock, OID_AUTO, delay_base, CTLFLAG_RW, &locks_delay.base,
161     0, "");
162 SYSCTL_U16(_debug_lock, OID_AUTO, delay_max, CTLFLAG_RW, &locks_delay.max,
163     0, "");
164 SYSCTL_U16(_debug_lock, OID_AUTO, delay_retries, CTLFLAG_RW, &locks_delay_retries,
165     0, "");
166 SYSCTL_U16(_debug_lock, OID_AUTO, delay_loops, CTLFLAG_RW, &locks_delay_loops,
167     0, "");
168 
169 static void
170 locks_delay_init(void *arg __unused)
171 {
172 
173 	lock_delay_default_init(&locks_delay);
174 	locks_delay_retries = 10;
175 	locks_delay_loops = max(10000, locks_delay.max);
176 }
177 LOCK_DELAY_SYSINIT(locks_delay_init);
178 
179 #ifdef DDB
180 DB_SHOW_COMMAND(lock, db_show_lock)
181 {
182 	struct lock_object *lock;
183 	struct lock_class *class;
184 
185 	if (!have_addr)
186 		return;
187 	lock = (struct lock_object *)addr;
188 	if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) {
189 		db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock));
190 		return;
191 	}
192 	class = LOCK_CLASS(lock);
193 	db_printf(" class: %s\n", class->lc_name);
194 	db_printf(" name: %s\n", lock->lo_name);
195 	class->lc_ddb_show(lock);
196 }
197 #endif
198 
199 #ifdef LOCK_PROFILING
200 
201 /*
202  * One object per-thread for each lock the thread owns.  Tracks individual
203  * lock instances.
204  */
205 struct lock_profile_object {
206 	LIST_ENTRY(lock_profile_object) lpo_link;
207 	struct lock_object *lpo_obj;
208 	const char	*lpo_file;
209 	int		lpo_line;
210 	uint16_t	lpo_ref;
211 	uint16_t	lpo_cnt;
212 	uint64_t	lpo_acqtime;
213 	uint64_t	lpo_waittime;
214 	u_int		lpo_contest_locking;
215 };
216 
217 /*
218  * One lock_prof for each (file, line, lock object) triple.
219  */
220 struct lock_prof {
221 	SLIST_ENTRY(lock_prof) link;
222 	struct lock_class *class;
223 	const char	*file;
224 	const char	*name;
225 	int		line;
226 	int		ticks;
227 	uintmax_t	cnt_wait_max;
228 	uintmax_t	cnt_max;
229 	uintmax_t	cnt_tot;
230 	uintmax_t	cnt_wait;
231 	uintmax_t	cnt_cur;
232 	uintmax_t	cnt_contest_locking;
233 };
234 
235 SLIST_HEAD(lphead, lock_prof);
236 
237 #define	LPROF_HASH_SIZE		4096
238 #define	LPROF_HASH_MASK		(LPROF_HASH_SIZE - 1)
239 #define	LPROF_CACHE_SIZE	4096
240 
241 /*
242  * Array of objects and profs for each type of object for each cpu.  Spinlocks
243  * are handled separately because a thread may be preempted and acquire a
244  * spinlock while in the lock profiling code of a non-spinlock.  In this way
245  * we only need a critical section to protect the per-cpu lists.
246  */
247 struct lock_prof_type {
248 	struct lphead		lpt_lpalloc;
249 	struct lpohead		lpt_lpoalloc;
250 	struct lphead		lpt_hash[LPROF_HASH_SIZE];
251 	struct lock_prof	lpt_prof[LPROF_CACHE_SIZE];
252 	struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE];
253 };
254 
255 struct lock_prof_cpu {
256 	struct lock_prof_type	lpc_types[2]; /* One for spin one for other. */
257 };
258 
259 DPCPU_DEFINE_STATIC(struct lock_prof_cpu, lp);
260 #define	LP_CPU_SELF	(DPCPU_PTR(lp))
261 #define	LP_CPU(cpu)	(DPCPU_ID_PTR((cpu), lp))
262 
263 volatile int __read_mostly lock_prof_enable;
264 int __read_mostly lock_contested_only;
265 static volatile int lock_prof_resetting;
266 
267 #define LPROF_SBUF_SIZE		256
268 
269 static int lock_prof_rejected;
270 static int lock_prof_skipspin;
271 
272 #ifndef USE_CPU_NANOSECONDS
273 uint64_t
274 nanoseconds(void)
275 {
276 	struct bintime bt;
277 	uint64_t ns;
278 
279 	binuptime(&bt);
280 	/* From bintime2timespec */
281 	ns = bt.sec * (uint64_t)1000000000;
282 	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
283 	return (ns);
284 }
285 #endif
286 
287 static void
288 lock_prof_init_type(struct lock_prof_type *type)
289 {
290 	int i;
291 
292 	SLIST_INIT(&type->lpt_lpalloc);
293 	LIST_INIT(&type->lpt_lpoalloc);
294 	for (i = 0; i < LPROF_CACHE_SIZE; i++) {
295 		SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i],
296 		    link);
297 		LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i],
298 		    lpo_link);
299 	}
300 }
301 
302 static void
303 lock_prof_init(void *arg)
304 {
305 	int cpu;
306 
307 	CPU_FOREACH(cpu) {
308 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[0]);
309 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[1]);
310 	}
311 }
312 SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL);
313 
314 static void
315 lock_prof_reset_wait(void)
316 {
317 
318 	/*
319 	 * Spin relinquishing our cpu so that quiesce_all_cpus may
320 	 * complete.
321 	 */
322 	while (lock_prof_resetting)
323 		sched_relinquish(curthread);
324 }
325 
326 static void
327 lock_prof_reset(void)
328 {
329 	struct lock_prof_cpu *lpc;
330 	int enabled, i, cpu;
331 
332 	/*
333 	 * We not only race with acquiring and releasing locks but also
334 	 * thread exit.  To be certain that threads exit without valid head
335 	 * pointers they must see resetting set before enabled is cleared.
336 	 * Otherwise a lock may not be removed from a per-thread list due
337 	 * to disabled being set but not wait for reset() to remove it below.
338 	 */
339 	atomic_store_rel_int(&lock_prof_resetting, 1);
340 	enabled = lock_prof_enable;
341 	lock_prof_enable = 0;
342 	/*
343 	 * This both publishes lock_prof_enable as disabled and makes sure
344 	 * everyone else reads it if they are not far enough. We wait for the
345 	 * rest down below.
346 	 */
347 	cpus_fence_seq_cst();
348 	quiesce_all_critical();
349 	/*
350 	 * Some objects may have migrated between CPUs.  Clear all links
351 	 * before we zero the structures.  Some items may still be linked
352 	 * into per-thread lists as well.
353 	 */
354 	CPU_FOREACH(cpu) {
355 		lpc = LP_CPU(cpu);
356 		for (i = 0; i < LPROF_CACHE_SIZE; i++) {
357 			LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link);
358 			LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link);
359 		}
360 	}
361 	CPU_FOREACH(cpu) {
362 		lpc = LP_CPU(cpu);
363 		bzero(lpc, sizeof(*lpc));
364 		lock_prof_init_type(&lpc->lpc_types[0]);
365 		lock_prof_init_type(&lpc->lpc_types[1]);
366 	}
367 	/*
368 	 * Paired with the fence from cpus_fence_seq_cst()
369 	 */
370 	atomic_store_rel_int(&lock_prof_resetting, 0);
371 	lock_prof_enable = enabled;
372 }
373 
374 static void
375 lock_prof_output(struct lock_prof *lp, struct sbuf *sb)
376 {
377 	const char *p;
378 
379 	for (p = lp->file; p != NULL && strncmp(p, "../", 3) == 0; p += 3);
380 	sbuf_printf(sb,
381 	    "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n",
382 	    lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000,
383 	    lp->cnt_wait / 1000, lp->cnt_cur,
384 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
385 	    lp->cnt_tot / (lp->cnt_cur * 1000),
386 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
387 	    lp->cnt_wait / (lp->cnt_cur * 1000),
388 	    (uintmax_t)0, lp->cnt_contest_locking,
389 	    p, lp->line, lp->class->lc_name, lp->name);
390 }
391 
392 static void
393 lock_prof_sum(struct lock_prof *match, struct lock_prof *dst, int hash,
394     int spin, int t)
395 {
396 	struct lock_prof_type *type;
397 	struct lock_prof *l;
398 	int cpu;
399 
400 	dst->file = match->file;
401 	dst->line = match->line;
402 	dst->class = match->class;
403 	dst->name = match->name;
404 
405 	CPU_FOREACH(cpu) {
406 		type = &LP_CPU(cpu)->lpc_types[spin];
407 		SLIST_FOREACH(l, &type->lpt_hash[hash], link) {
408 			if (l->ticks == t)
409 				continue;
410 			if (l->file != match->file || l->line != match->line ||
411 			    l->name != match->name)
412 				continue;
413 			l->ticks = t;
414 			if (l->cnt_max > dst->cnt_max)
415 				dst->cnt_max = l->cnt_max;
416 			if (l->cnt_wait_max > dst->cnt_wait_max)
417 				dst->cnt_wait_max = l->cnt_wait_max;
418 			dst->cnt_tot += l->cnt_tot;
419 			dst->cnt_wait += l->cnt_wait;
420 			dst->cnt_cur += l->cnt_cur;
421 			dst->cnt_contest_locking += l->cnt_contest_locking;
422 		}
423 	}
424 }
425 
426 static void
427 lock_prof_type_stats(struct lock_prof_type *type, struct sbuf *sb, int spin,
428     int t)
429 {
430 	struct lock_prof *l;
431 	int i;
432 
433 	for (i = 0; i < LPROF_HASH_SIZE; ++i) {
434 		SLIST_FOREACH(l, &type->lpt_hash[i], link) {
435 			struct lock_prof lp = {};
436 
437 			if (l->ticks == t)
438 				continue;
439 			lock_prof_sum(l, &lp, i, spin, t);
440 			lock_prof_output(&lp, sb);
441 		}
442 	}
443 }
444 
445 static int
446 dump_lock_prof_stats(SYSCTL_HANDLER_ARGS)
447 {
448 	struct sbuf *sb;
449 	int error, cpu, t;
450 	int enabled;
451 
452 	error = sysctl_wire_old_buffer(req, 0);
453 	if (error != 0)
454 		return (error);
455 	sb = sbuf_new_for_sysctl(NULL, NULL, LPROF_SBUF_SIZE, req);
456 	sbuf_printf(sb, "\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n",
457 	    "max", "wait_max", "total", "wait_total", "count", "avg", "wait_avg", "cnt_hold", "cnt_lock", "name");
458 	enabled = lock_prof_enable;
459 	lock_prof_enable = 0;
460 	/*
461 	 * See the comment in lock_prof_reset
462 	 */
463 	cpus_fence_seq_cst();
464 	quiesce_all_critical();
465 	t = ticks;
466 	CPU_FOREACH(cpu) {
467 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[0], sb, 0, t);
468 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[1], sb, 1, t);
469 	}
470 	atomic_thread_fence_rel();
471 	lock_prof_enable = enabled;
472 
473 	error = sbuf_finish(sb);
474 	/* Output a trailing NUL. */
475 	if (error == 0)
476 		error = SYSCTL_OUT(req, "", 1);
477 	sbuf_delete(sb);
478 	return (error);
479 }
480 
481 static int
482 enable_lock_prof(SYSCTL_HANDLER_ARGS)
483 {
484 	int error, v;
485 
486 	v = lock_prof_enable;
487 	error = sysctl_handle_int(oidp, &v, v, req);
488 	if (error)
489 		return (error);
490 	if (req->newptr == NULL)
491 		return (error);
492 	if (v == lock_prof_enable)
493 		return (0);
494 	if (v == 1)
495 		lock_prof_reset();
496 	lock_prof_enable = !!v;
497 
498 	return (0);
499 }
500 
501 static int
502 reset_lock_prof_stats(SYSCTL_HANDLER_ARGS)
503 {
504 	int error, v;
505 
506 	v = 0;
507 	error = sysctl_handle_int(oidp, &v, 0, req);
508 	if (error)
509 		return (error);
510 	if (req->newptr == NULL)
511 		return (error);
512 	if (v == 0)
513 		return (0);
514 	lock_prof_reset();
515 
516 	return (0);
517 }
518 
519 static struct lock_prof *
520 lock_profile_lookup(struct lock_object *lo, int spin, const char *file,
521     int line)
522 {
523 	const char *unknown = "(unknown)";
524 	struct lock_prof_type *type;
525 	struct lock_prof *lp;
526 	struct lphead *head;
527 	const char *p;
528 	u_int hash;
529 
530 	p = file;
531 	if (p == NULL || *p == '\0')
532 		p = unknown;
533 	hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line;
534 	hash &= LPROF_HASH_MASK;
535 	type = &LP_CPU_SELF->lpc_types[spin];
536 	head = &type->lpt_hash[hash];
537 	SLIST_FOREACH(lp, head, link) {
538 		if (lp->line == line && lp->file == p &&
539 		    lp->name == lo->lo_name)
540 			return (lp);
541 	}
542 	lp = SLIST_FIRST(&type->lpt_lpalloc);
543 	if (lp == NULL) {
544 		lock_prof_rejected++;
545 		return (lp);
546 	}
547 	SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link);
548 	lp->file = p;
549 	lp->line = line;
550 	lp->class = LOCK_CLASS(lo);
551 	lp->name = lo->lo_name;
552 	SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link);
553 	return (lp);
554 }
555 
556 static struct lock_profile_object *
557 lock_profile_object_lookup(struct lock_object *lo, int spin, const char *file,
558     int line)
559 {
560 	struct lock_profile_object *l;
561 	struct lock_prof_type *type;
562 	struct lpohead *head;
563 
564 	head = &curthread->td_lprof[spin];
565 	LIST_FOREACH(l, head, lpo_link)
566 		if (l->lpo_obj == lo && l->lpo_file == file &&
567 		    l->lpo_line == line)
568 			return (l);
569 	type = &LP_CPU_SELF->lpc_types[spin];
570 	l = LIST_FIRST(&type->lpt_lpoalloc);
571 	if (l == NULL) {
572 		lock_prof_rejected++;
573 		return (NULL);
574 	}
575 	LIST_REMOVE(l, lpo_link);
576 	l->lpo_obj = lo;
577 	l->lpo_file = file;
578 	l->lpo_line = line;
579 	l->lpo_cnt = 0;
580 	LIST_INSERT_HEAD(head, l, lpo_link);
581 
582 	return (l);
583 }
584 
585 void
586 lock_profile_obtain_lock_success(struct lock_object *lo, bool spin,
587     int contested, uint64_t waittime, const char *file, int line)
588 {
589 	struct lock_profile_object *l;
590 
591 #ifdef LOCK_PROFILING_DEBUG_SPIN
592 	bool is_spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK);
593 	if ((spin && !is_spin) || (!spin && is_spin))
594 		printf("%s: lock %s spin mismatch (arg %d, flag %d)\n", __func__,
595 		    lo->lo_name, spin, is_spin);
596 #endif
597 
598 	/* don't reset the timer when/if recursing */
599 	if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE))
600 		return;
601 	if (lock_contested_only && !contested)
602 		return;
603 	if (spin && lock_prof_skipspin == 1)
604 		return;
605 
606 	if (SCHEDULER_STOPPED())
607 		return;
608 
609 	critical_enter();
610 	/* Recheck enabled now that we're in a critical section. */
611 	if (lock_prof_enable == 0)
612 		goto out;
613 	l = lock_profile_object_lookup(lo, spin, file, line);
614 	if (l == NULL)
615 		goto out;
616 	l->lpo_cnt++;
617 	if (++l->lpo_ref > 1)
618 		goto out;
619 	l->lpo_contest_locking = contested;
620 	l->lpo_acqtime = nanoseconds();
621 	if (waittime && (l->lpo_acqtime > waittime))
622 		l->lpo_waittime = l->lpo_acqtime - waittime;
623 	else
624 		l->lpo_waittime = 0;
625 out:
626 	/*
627 	 * Paired with cpus_fence_seq_cst().
628 	 */
629 	atomic_thread_fence_rel();
630 	critical_exit();
631 }
632 
633 void
634 lock_profile_thread_exit(struct thread *td)
635 {
636 #ifdef INVARIANTS
637 	struct lock_profile_object *l;
638 
639 	MPASS(curthread->td_critnest == 0);
640 #endif
641 	/*
642 	 * If lock profiling was disabled we have to wait for reset to
643 	 * clear our pointers before we can exit safely.
644 	 */
645 	lock_prof_reset_wait();
646 #ifdef INVARIANTS
647 	LIST_FOREACH(l, &td->td_lprof[0], lpo_link)
648 		printf("thread still holds lock acquired at %s:%d\n",
649 		    l->lpo_file, l->lpo_line);
650 	LIST_FOREACH(l, &td->td_lprof[1], lpo_link)
651 		printf("thread still holds lock acquired at %s:%d\n",
652 		    l->lpo_file, l->lpo_line);
653 #endif
654 	MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL);
655 	MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL);
656 }
657 
658 void
659 lock_profile_release_lock(struct lock_object *lo, bool spin)
660 {
661 	struct lock_profile_object *l;
662 	struct lock_prof_type *type;
663 	struct lock_prof *lp;
664 	uint64_t curtime, holdtime;
665 	struct lpohead *head;
666 
667 #ifdef LOCK_PROFILING_DEBUG_SPIN
668 	bool is_spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK);
669 	if ((spin && !is_spin) || (!spin && is_spin))
670 		printf("%s: lock %s spin mismatch (arg %d, flag %d)\n", __func__,
671 		    lo->lo_name, spin, is_spin);
672 #endif
673 
674 	if (lo->lo_flags & LO_NOPROFILE)
675 		return;
676 	head = &curthread->td_lprof[spin];
677 	if (LIST_FIRST(head) == NULL)
678 		return;
679 	if (SCHEDULER_STOPPED())
680 		return;
681 	critical_enter();
682 	/* Recheck enabled now that we're in a critical section. */
683 	if (lock_prof_enable == 0 && lock_prof_resetting == 1)
684 		goto out;
685 	/*
686 	 * If lock profiling is not enabled we still want to remove the
687 	 * lpo from our queue.
688 	 */
689 	LIST_FOREACH(l, head, lpo_link)
690 		if (l->lpo_obj == lo)
691 			break;
692 	if (l == NULL)
693 		goto out;
694 	if (--l->lpo_ref > 0)
695 		goto out;
696 	lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line);
697 	if (lp == NULL)
698 		goto release;
699 	curtime = nanoseconds();
700 	if (curtime < l->lpo_acqtime)
701 		goto release;
702 	holdtime = curtime - l->lpo_acqtime;
703 
704 	/*
705 	 * Record if the lock has been held longer now than ever
706 	 * before.
707 	 */
708 	if (holdtime > lp->cnt_max)
709 		lp->cnt_max = holdtime;
710 	if (l->lpo_waittime > lp->cnt_wait_max)
711 		lp->cnt_wait_max = l->lpo_waittime;
712 	lp->cnt_tot += holdtime;
713 	lp->cnt_wait += l->lpo_waittime;
714 	lp->cnt_contest_locking += l->lpo_contest_locking;
715 	lp->cnt_cur += l->lpo_cnt;
716 release:
717 	LIST_REMOVE(l, lpo_link);
718 	type = &LP_CPU_SELF->lpc_types[spin];
719 	LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link);
720 out:
721 	/*
722 	 * Paired with cpus_fence_seq_cst().
723 	 */
724 	atomic_thread_fence_rel();
725 	critical_exit();
726 }
727 
728 static SYSCTL_NODE(_debug_lock, OID_AUTO, prof,
729     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
730     "lock profiling");
731 SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW,
732     &lock_prof_skipspin, 0, "Skip profiling on spinlocks.");
733 SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD,
734     &lock_prof_rejected, 0, "Number of rejected profiling records");
735 SYSCTL_INT(_debug_lock_prof, OID_AUTO, contested_only, CTLFLAG_RW,
736     &lock_contested_only, 0, "Only profile contested acquires");
737 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats,
738     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
739     dump_lock_prof_stats, "A",
740     "Lock profiling statistics");
741 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset,
742     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
743     reset_lock_prof_stats, "I",
744     "Reset lock profiling statistics");
745 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable,
746     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
747     enable_lock_prof, "I",
748     "Enable lock profiling");
749 
750 #endif
751