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