xref: /freebsd/sys/kern/subr_lock.c (revision a0842e69aa5f86d61c072544b540ef21b2015211)
183a81bcbSJohn Baldwin /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
483a81bcbSJohn Baldwin  * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
583a81bcbSJohn Baldwin  *
683a81bcbSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
783a81bcbSJohn Baldwin  * modification, are permitted provided that the following conditions
883a81bcbSJohn Baldwin  * are met:
983a81bcbSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
1083a81bcbSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
1183a81bcbSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
1283a81bcbSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
1383a81bcbSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
1483a81bcbSJohn Baldwin  *
1583a81bcbSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1683a81bcbSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1783a81bcbSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1883a81bcbSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1983a81bcbSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2083a81bcbSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2183a81bcbSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2283a81bcbSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2383a81bcbSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2483a81bcbSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2583a81bcbSJohn Baldwin  * SUCH DAMAGE.
2683a81bcbSJohn Baldwin  */
2783a81bcbSJohn Baldwin 
2883a81bcbSJohn Baldwin /*
2983a81bcbSJohn Baldwin  * This module holds the global variables and functions used to maintain
3083a81bcbSJohn Baldwin  * lock_object structures.
3183a81bcbSJohn Baldwin  */
3283a81bcbSJohn Baldwin 
3383a81bcbSJohn Baldwin #include <sys/cdefs.h>
3483a81bcbSJohn Baldwin __FBSDID("$FreeBSD$");
3583a81bcbSJohn Baldwin 
366ef970a9SJohn Baldwin #include "opt_ddb.h"
377c0435b9SKip Macy #include "opt_mprof.h"
386ef970a9SJohn Baldwin 
3983a81bcbSJohn Baldwin #include <sys/param.h>
4083a81bcbSJohn Baldwin #include <sys/systm.h>
41eea4f254SJeff Roberson #include <sys/kernel.h>
4283a81bcbSJohn Baldwin #include <sys/ktr.h>
43eac22dd4SMateusz Guzik #include <sys/limits.h>
4483a81bcbSJohn Baldwin #include <sys/lock.h>
457c0435b9SKip Macy #include <sys/lock_profile.h>
46eea4f254SJeff Roberson #include <sys/malloc.h>
472e6b8de4SJeff Roberson #include <sys/mutex.h>
48eea4f254SJeff Roberson #include <sys/pcpu.h>
49eea4f254SJeff Roberson #include <sys/proc.h>
50eea4f254SJeff Roberson #include <sys/sbuf.h>
512e6b8de4SJeff Roberson #include <sys/sched.h>
52eea4f254SJeff Roberson #include <sys/smp.h>
53eea4f254SJeff Roberson #include <sys/sysctl.h>
5483a81bcbSJohn Baldwin 
5583a81bcbSJohn Baldwin #ifdef DDB
5683a81bcbSJohn Baldwin #include <ddb/ddb.h>
5783a81bcbSJohn Baldwin #endif
5883a81bcbSJohn Baldwin 
59eea4f254SJeff Roberson #include <machine/cpufunc.h>
60eea4f254SJeff Roberson 
618e5a3e9aSMateusz Guzik SDT_PROVIDER_DEFINE(lock);
628e5a3e9aSMateusz Guzik SDT_PROBE_DEFINE1(lock, , , starvation, "u_int");
638e5a3e9aSMateusz Guzik 
6483a81bcbSJohn Baldwin CTASSERT(LOCK_CLASS_MAX == 15);
6583a81bcbSJohn Baldwin 
6683a81bcbSJohn Baldwin struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = {
6783a81bcbSJohn Baldwin 	&lock_class_mtx_spin,
6883a81bcbSJohn Baldwin 	&lock_class_mtx_sleep,
6983a81bcbSJohn Baldwin 	&lock_class_sx,
70f53d15feSStephan Uphoff 	&lock_class_rm,
71cd32bd7aSJohn Baldwin 	&lock_class_rm_sleepable,
723f08bd8bSJohn Baldwin 	&lock_class_rw,
7361bd5e21SKip Macy 	&lock_class_lockmgr,
7483a81bcbSJohn Baldwin };
7583a81bcbSJohn Baldwin 
7683a81bcbSJohn Baldwin void
7783a81bcbSJohn Baldwin lock_init(struct lock_object *lock, struct lock_class *class, const char *name,
7883a81bcbSJohn Baldwin     const char *type, int flags)
7983a81bcbSJohn Baldwin {
8083a81bcbSJohn Baldwin 	int i;
8183a81bcbSJohn Baldwin 
8283a81bcbSJohn Baldwin 	/* Check for double-init and zero object. */
83fd07ddcfSDmitry Chagin 	KASSERT(flags & LO_NEW || !lock_initialized(lock),
84fd07ddcfSDmitry Chagin 	    ("lock \"%s\" %p already initialized", name, lock));
8583a81bcbSJohn Baldwin 
8683a81bcbSJohn Baldwin 	/* Look up lock class to find its index. */
8783a81bcbSJohn Baldwin 	for (i = 0; i < LOCK_CLASS_MAX; i++)
8883a81bcbSJohn Baldwin 		if (lock_classes[i] == class) {
8983a81bcbSJohn Baldwin 			lock->lo_flags = i << LO_CLASSSHIFT;
9083a81bcbSJohn Baldwin 			break;
9183a81bcbSJohn Baldwin 		}
9283a81bcbSJohn Baldwin 	KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));
9383a81bcbSJohn Baldwin 
9483a81bcbSJohn Baldwin 	/* Initialize the lock object. */
9583a81bcbSJohn Baldwin 	lock->lo_name = name;
9683a81bcbSJohn Baldwin 	lock->lo_flags |= flags | LO_INITIALIZED;
9783a81bcbSJohn Baldwin 	LOCK_LOG_INIT(lock, 0);
9890356491SAttilio Rao 	WITNESS_INIT(lock, (type != NULL) ? type : name);
9983a81bcbSJohn Baldwin }
10083a81bcbSJohn Baldwin 
10183a81bcbSJohn Baldwin void
10283a81bcbSJohn Baldwin lock_destroy(struct lock_object *lock)
10383a81bcbSJohn Baldwin {
10483a81bcbSJohn Baldwin 
1053a6cdc4eSJohn-Mark Gurney 	KASSERT(lock_initialized(lock), ("lock %p is not initialized", lock));
10683a81bcbSJohn Baldwin 	WITNESS_DESTROY(lock);
10783a81bcbSJohn Baldwin 	LOCK_LOG_DESTROY(lock, 0);
10883a81bcbSJohn Baldwin 	lock->lo_flags &= ~LO_INITIALIZED;
10983a81bcbSJohn Baldwin }
11083a81bcbSJohn Baldwin 
1117029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1127029da5cSPawel Biernacki     "lock debugging");
1137029da5cSPawel Biernacki static SYSCTL_NODE(_debug_lock, OID_AUTO, delay,
1147029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1158e5a3e9aSMateusz Guzik     "lock delay");
1168e5a3e9aSMateusz Guzik 
1178e5a3e9aSMateusz Guzik static u_int __read_mostly starvation_limit = 131072;
1188e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_lock_delay, OID_AUTO, starvation_limit, CTLFLAG_RW,
1198e5a3e9aSMateusz Guzik     &starvation_limit, 0, "");
1208e5a3e9aSMateusz Guzik 
1218e5a3e9aSMateusz Guzik static u_int __read_mostly restrict_starvation = 0;
1228e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_lock_delay, OID_AUTO, restrict_starvation, CTLFLAG_RW,
1238e5a3e9aSMateusz Guzik     &restrict_starvation, 0, "");
1248e5a3e9aSMateusz Guzik 
1251ada9041SMateusz Guzik void
1261ada9041SMateusz Guzik lock_delay(struct lock_delay_arg *la)
1271ada9041SMateusz Guzik {
1281ada9041SMateusz Guzik 	struct lock_delay_config *lc = la->config;
1296b8dd26eSMateusz Guzik 	u_short i;
1301ada9041SMateusz Guzik 
1313c798b2bSMateusz Guzik 	for (i = la->delay; i > 0; i--)
1321ada9041SMateusz Guzik 		cpu_spinwait();
1338e5a3e9aSMateusz Guzik 	la->spin_cnt += la->delay;
1347f6157f7SEdward Tomasz Napierala 
1357f6157f7SEdward Tomasz Napierala 	la->delay <<= 1;
1367f6157f7SEdward Tomasz Napierala 	if (__predict_false(la->delay > lc->max))
1377f6157f7SEdward Tomasz Napierala 		la->delay = lc->max;
1387f6157f7SEdward Tomasz Napierala 
1398e5a3e9aSMateusz Guzik 	if (__predict_false(la->spin_cnt > starvation_limit)) {
1408e5a3e9aSMateusz Guzik 		SDT_PROBE1(lock, , , starvation, la->delay);
1418e5a3e9aSMateusz Guzik 		if (restrict_starvation)
1428e5a3e9aSMateusz Guzik 			la->delay = lc->base;
1438e5a3e9aSMateusz Guzik 	}
1448e5a3e9aSMateusz Guzik }
1458e5a3e9aSMateusz Guzik 
1468e5a3e9aSMateusz Guzik static u_int
1478e5a3e9aSMateusz Guzik lock_roundup_2(u_int val)
1488e5a3e9aSMateusz Guzik {
1498e5a3e9aSMateusz Guzik 	u_int res;
1508e5a3e9aSMateusz Guzik 
1518e5a3e9aSMateusz Guzik 	for (res = 1; res <= val; res <<= 1)
1528e5a3e9aSMateusz Guzik 		continue;
1538e5a3e9aSMateusz Guzik 
1548e5a3e9aSMateusz Guzik 	return (res);
1558e5a3e9aSMateusz Guzik }
1568e5a3e9aSMateusz Guzik 
1578e5a3e9aSMateusz Guzik void
1588e5a3e9aSMateusz Guzik lock_delay_default_init(struct lock_delay_config *lc)
1598e5a3e9aSMateusz Guzik {
1608e5a3e9aSMateusz Guzik 
161a045941bSMateusz Guzik 	lc->base = 1;
162a045941bSMateusz Guzik 	lc->max = lock_roundup_2(mp_ncpus) * 256;
163a045941bSMateusz Guzik 	if (lc->max > 32678)
164a045941bSMateusz Guzik 		lc->max = 32678;
1651ada9041SMateusz Guzik }
1661ada9041SMateusz Guzik 
1672e77cad1SMateusz Guzik struct lock_delay_config __read_frequently locks_delay;
1682e77cad1SMateusz Guzik u_short __read_frequently locks_delay_retries;
1692e77cad1SMateusz Guzik u_short __read_frequently locks_delay_loops;
1702e77cad1SMateusz Guzik 
1712e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_base, CTLFLAG_RW, &locks_delay.base,
1722e77cad1SMateusz Guzik     0, "");
1732e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_max, CTLFLAG_RW, &locks_delay.max,
1742e77cad1SMateusz Guzik     0, "");
1752e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_retries, CTLFLAG_RW, &locks_delay_retries,
1762e77cad1SMateusz Guzik     0, "");
1772e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_loops, CTLFLAG_RW, &locks_delay_loops,
1782e77cad1SMateusz Guzik     0, "");
1792e77cad1SMateusz Guzik 
1802e77cad1SMateusz Guzik static void
1812e77cad1SMateusz Guzik locks_delay_init(void *arg __unused)
1822e77cad1SMateusz Guzik {
1832e77cad1SMateusz Guzik 
1842e77cad1SMateusz Guzik 	lock_delay_default_init(&locks_delay);
1852e77cad1SMateusz Guzik 	locks_delay_retries = 10;
1862e77cad1SMateusz Guzik 	locks_delay_loops = max(10000, locks_delay.max);
1872e77cad1SMateusz Guzik }
1882e77cad1SMateusz Guzik LOCK_DELAY_SYSINIT(locks_delay_init);
1892e77cad1SMateusz Guzik 
19083a81bcbSJohn Baldwin #ifdef DDB
19183a81bcbSJohn Baldwin DB_SHOW_COMMAND(lock, db_show_lock)
19283a81bcbSJohn Baldwin {
19383a81bcbSJohn Baldwin 	struct lock_object *lock;
19483a81bcbSJohn Baldwin 	struct lock_class *class;
19583a81bcbSJohn Baldwin 
19683a81bcbSJohn Baldwin 	if (!have_addr)
19783a81bcbSJohn Baldwin 		return;
19883a81bcbSJohn Baldwin 	lock = (struct lock_object *)addr;
19983a81bcbSJohn Baldwin 	if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) {
20083a81bcbSJohn Baldwin 		db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock));
20183a81bcbSJohn Baldwin 		return;
20283a81bcbSJohn Baldwin 	}
20383a81bcbSJohn Baldwin 	class = LOCK_CLASS(lock);
20483a81bcbSJohn Baldwin 	db_printf(" class: %s\n", class->lc_name);
20583a81bcbSJohn Baldwin 	db_printf(" name: %s\n", lock->lo_name);
20683a81bcbSJohn Baldwin 	class->lc_ddb_show(lock);
20783a81bcbSJohn Baldwin }
20883a81bcbSJohn Baldwin #endif
2097c0435b9SKip Macy 
2107c0435b9SKip Macy #ifdef LOCK_PROFILING
211eea4f254SJeff Roberson 
212eea4f254SJeff Roberson /*
213eea4f254SJeff Roberson  * One object per-thread for each lock the thread owns.  Tracks individual
214eea4f254SJeff Roberson  * lock instances.
215eea4f254SJeff Roberson  */
216eea4f254SJeff Roberson struct lock_profile_object {
217eea4f254SJeff Roberson 	LIST_ENTRY(lock_profile_object) lpo_link;
218eea4f254SJeff Roberson 	struct lock_object *lpo_obj;
219eea4f254SJeff Roberson 	const char	*lpo_file;
220eea4f254SJeff Roberson 	int		lpo_line;
221eea4f254SJeff Roberson 	uint16_t	lpo_ref;
222eea4f254SJeff Roberson 	uint16_t	lpo_cnt;
22360ae52f7SEd Schouten 	uint64_t	lpo_acqtime;
22460ae52f7SEd Schouten 	uint64_t	lpo_waittime;
225eea4f254SJeff Roberson 	u_int		lpo_contest_locking;
226eea4f254SJeff Roberson };
227eea4f254SJeff Roberson 
228eea4f254SJeff Roberson /*
229eea4f254SJeff Roberson  * One lock_prof for each (file, line, lock object) triple.
230eea4f254SJeff Roberson  */
231eea4f254SJeff Roberson struct lock_prof {
232eea4f254SJeff Roberson 	SLIST_ENTRY(lock_prof) link;
2330c66dc67SJeff Roberson 	struct lock_class *class;
234eea4f254SJeff Roberson 	const char	*file;
235eea4f254SJeff Roberson 	const char	*name;
236eea4f254SJeff Roberson 	int		line;
237eea4f254SJeff Roberson 	int		ticks;
238947265b6SKip Macy 	uintmax_t	cnt_wait_max;
239eea4f254SJeff Roberson 	uintmax_t	cnt_max;
240eea4f254SJeff Roberson 	uintmax_t	cnt_tot;
241eea4f254SJeff Roberson 	uintmax_t	cnt_wait;
242eea4f254SJeff Roberson 	uintmax_t	cnt_cur;
243eea4f254SJeff Roberson 	uintmax_t	cnt_contest_locking;
244eea4f254SJeff Roberson };
245eea4f254SJeff Roberson 
246eea4f254SJeff Roberson SLIST_HEAD(lphead, lock_prof);
247eea4f254SJeff Roberson 
248eea4f254SJeff Roberson #define	LPROF_HASH_SIZE		4096
249eea4f254SJeff Roberson #define	LPROF_HASH_MASK		(LPROF_HASH_SIZE - 1)
250eea4f254SJeff Roberson #define	LPROF_CACHE_SIZE	4096
251eea4f254SJeff Roberson 
252eea4f254SJeff Roberson /*
253eea4f254SJeff Roberson  * Array of objects and profs for each type of object for each cpu.  Spinlocks
254b1ce21c6SRebecca Cran  * are handled separately because a thread may be preempted and acquire a
255eea4f254SJeff Roberson  * spinlock while in the lock profiling code of a non-spinlock.  In this way
256eea4f254SJeff Roberson  * we only need a critical section to protect the per-cpu lists.
257eea4f254SJeff Roberson  */
258eea4f254SJeff Roberson struct lock_prof_type {
259eea4f254SJeff Roberson 	struct lphead		lpt_lpalloc;
260eea4f254SJeff Roberson 	struct lpohead		lpt_lpoalloc;
261eea4f254SJeff Roberson 	struct lphead		lpt_hash[LPROF_HASH_SIZE];
262eea4f254SJeff Roberson 	struct lock_prof	lpt_prof[LPROF_CACHE_SIZE];
263eea4f254SJeff Roberson 	struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE];
264eea4f254SJeff Roberson };
265eea4f254SJeff Roberson 
266eea4f254SJeff Roberson struct lock_prof_cpu {
267eea4f254SJeff Roberson 	struct lock_prof_type	lpc_types[2]; /* One for spin one for other. */
268eea4f254SJeff Roberson };
269eea4f254SJeff Roberson 
270d2be3ef0SMateusz Guzik DPCPU_DEFINE_STATIC(struct lock_prof_cpu, lp);
271d2be3ef0SMateusz Guzik #define	LP_CPU_SELF	(DPCPU_PTR(lp))
272d2be3ef0SMateusz Guzik #define	LP_CPU(cpu)	(DPCPU_ID_PTR((cpu), lp))
273eea4f254SJeff Roberson 
27429051116SMateusz Guzik volatile int __read_mostly lock_prof_enable;
275*a0842e69SMateusz Guzik int __read_mostly lock_contested_only;
2762e6b8de4SJeff Roberson static volatile int lock_prof_resetting;
277eea4f254SJeff Roberson 
2784e657159SMatthew D Fleming #define LPROF_SBUF_SIZE		256
279eea4f254SJeff Roberson 
280eea4f254SJeff Roberson static int lock_prof_rejected;
281eea4f254SJeff Roberson static int lock_prof_skipspin;
282eea4f254SJeff Roberson 
283eea4f254SJeff Roberson #ifndef USE_CPU_NANOSECONDS
28460ae52f7SEd Schouten uint64_t
285eea4f254SJeff Roberson nanoseconds(void)
2867c0435b9SKip Macy {
287eea4f254SJeff Roberson 	struct bintime bt;
28860ae52f7SEd Schouten 	uint64_t ns;
2897c0435b9SKip Macy 
290eea4f254SJeff Roberson 	binuptime(&bt);
291eea4f254SJeff Roberson 	/* From bintime2timespec */
29260ae52f7SEd Schouten 	ns = bt.sec * (uint64_t)1000000000;
293eea4f254SJeff Roberson 	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
294eea4f254SJeff Roberson 	return (ns);
295eea4f254SJeff Roberson }
296eea4f254SJeff Roberson #endif
297fe68a916SKip Macy 
298eea4f254SJeff Roberson static void
299eea4f254SJeff Roberson lock_prof_init_type(struct lock_prof_type *type)
300eea4f254SJeff Roberson {
301eea4f254SJeff Roberson 	int i;
302fe68a916SKip Macy 
303eea4f254SJeff Roberson 	SLIST_INIT(&type->lpt_lpalloc);
304eea4f254SJeff Roberson 	LIST_INIT(&type->lpt_lpoalloc);
305eea4f254SJeff Roberson 	for (i = 0; i < LPROF_CACHE_SIZE; i++) {
306eea4f254SJeff Roberson 		SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i],
307eea4f254SJeff Roberson 		    link);
308eea4f254SJeff Roberson 		LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i],
309eea4f254SJeff Roberson 		    lpo_link);
310eea4f254SJeff Roberson 	}
311eea4f254SJeff Roberson }
312eea4f254SJeff Roberson 
313eea4f254SJeff Roberson static void
314eea4f254SJeff Roberson lock_prof_init(void *arg)
315eea4f254SJeff Roberson {
316eea4f254SJeff Roberson 	int cpu;
317eea4f254SJeff Roberson 
318cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
319d2be3ef0SMateusz Guzik 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[0]);
320d2be3ef0SMateusz Guzik 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[1]);
321eea4f254SJeff Roberson 	}
322eea4f254SJeff Roberson }
323eea4f254SJeff Roberson SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL);
324eea4f254SJeff Roberson 
3252e6b8de4SJeff Roberson static void
3262e6b8de4SJeff Roberson lock_prof_reset_wait(void)
3272e6b8de4SJeff Roberson {
3282e6b8de4SJeff Roberson 
3292e6b8de4SJeff Roberson 	/*
33028d91af3SJeff Roberson 	 * Spin relinquishing our cpu so that quiesce_all_cpus may
33128d91af3SJeff Roberson 	 * complete.
3322e6b8de4SJeff Roberson 	 */
3332e6b8de4SJeff Roberson 	while (lock_prof_resetting)
3342e6b8de4SJeff Roberson 		sched_relinquish(curthread);
3352e6b8de4SJeff Roberson }
3362e6b8de4SJeff Roberson 
337eea4f254SJeff Roberson static void
338eea4f254SJeff Roberson lock_prof_reset(void)
339eea4f254SJeff Roberson {
340eea4f254SJeff Roberson 	struct lock_prof_cpu *lpc;
341eea4f254SJeff Roberson 	int enabled, i, cpu;
342eea4f254SJeff Roberson 
3432e6b8de4SJeff Roberson 	/*
3442e6b8de4SJeff Roberson 	 * We not only race with acquiring and releasing locks but also
3452e6b8de4SJeff Roberson 	 * thread exit.  To be certain that threads exit without valid head
3462e6b8de4SJeff Roberson 	 * pointers they must see resetting set before enabled is cleared.
3472e6b8de4SJeff Roberson 	 * Otherwise a lock may not be removed from a per-thread list due
3482e6b8de4SJeff Roberson 	 * to disabled being set but not wait for reset() to remove it below.
3492e6b8de4SJeff Roberson 	 */
3502e6b8de4SJeff Roberson 	atomic_store_rel_int(&lock_prof_resetting, 1);
351eea4f254SJeff Roberson 	enabled = lock_prof_enable;
352eea4f254SJeff Roberson 	lock_prof_enable = 0;
3533ac2ac2eSMateusz Guzik 	/*
3543ac2ac2eSMateusz Guzik 	 * This both publishes lock_prof_enable as disabled and makes sure
3553ac2ac2eSMateusz Guzik 	 * everyone else reads it if they are not far enough. We wait for the
3563ac2ac2eSMateusz Guzik 	 * rest down below.
3573ac2ac2eSMateusz Guzik 	 */
3583ac2ac2eSMateusz Guzik 	cpus_fence_seq_cst();
3593ac2ac2eSMateusz Guzik 	quiesce_all_critical();
3602e6b8de4SJeff Roberson 	/*
3612e6b8de4SJeff Roberson 	 * Some objects may have migrated between CPUs.  Clear all links
3622e6b8de4SJeff Roberson 	 * before we zero the structures.  Some items may still be linked
3632e6b8de4SJeff Roberson 	 * into per-thread lists as well.
3642e6b8de4SJeff Roberson 	 */
365cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
366d2be3ef0SMateusz Guzik 		lpc = LP_CPU(cpu);
367eea4f254SJeff Roberson 		for (i = 0; i < LPROF_CACHE_SIZE; i++) {
368eea4f254SJeff Roberson 			LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link);
369eea4f254SJeff Roberson 			LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link);
370eea4f254SJeff Roberson 		}
3712e6b8de4SJeff Roberson 	}
372cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
373d2be3ef0SMateusz Guzik 		lpc = LP_CPU(cpu);
374eea4f254SJeff Roberson 		bzero(lpc, sizeof(*lpc));
375eea4f254SJeff Roberson 		lock_prof_init_type(&lpc->lpc_types[0]);
376eea4f254SJeff Roberson 		lock_prof_init_type(&lpc->lpc_types[1]);
377eea4f254SJeff Roberson 	}
3783ac2ac2eSMateusz Guzik 	/*
3793ac2ac2eSMateusz Guzik 	 * Paired with the fence from cpus_fence_seq_cst()
3803ac2ac2eSMateusz Guzik 	 */
3812e6b8de4SJeff Roberson 	atomic_store_rel_int(&lock_prof_resetting, 0);
382eea4f254SJeff Roberson 	lock_prof_enable = enabled;
383eea4f254SJeff Roberson }
384eea4f254SJeff Roberson 
385eea4f254SJeff Roberson static void
386eea4f254SJeff Roberson lock_prof_output(struct lock_prof *lp, struct sbuf *sb)
387eea4f254SJeff Roberson {
388eea4f254SJeff Roberson 	const char *p;
389eea4f254SJeff Roberson 
390eea4f254SJeff Roberson 	for (p = lp->file; p != NULL && strncmp(p, "../", 3) == 0; p += 3);
391eea4f254SJeff Roberson 	sbuf_printf(sb,
392947265b6SKip Macy 	    "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n",
393947265b6SKip Macy 	    lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000,
394eea4f254SJeff Roberson 	    lp->cnt_wait / 1000, lp->cnt_cur,
395eea4f254SJeff Roberson 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
396eea4f254SJeff Roberson 	    lp->cnt_tot / (lp->cnt_cur * 1000),
397eea4f254SJeff Roberson 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
398eea4f254SJeff Roberson 	    lp->cnt_wait / (lp->cnt_cur * 1000),
399eea4f254SJeff Roberson 	    (uintmax_t)0, lp->cnt_contest_locking,
4000c66dc67SJeff Roberson 	    p, lp->line, lp->class->lc_name, lp->name);
401eea4f254SJeff Roberson }
402eea4f254SJeff Roberson 
403eea4f254SJeff Roberson static void
404eea4f254SJeff Roberson lock_prof_sum(struct lock_prof *match, struct lock_prof *dst, int hash,
405eea4f254SJeff Roberson     int spin, int t)
406eea4f254SJeff Roberson {
407eea4f254SJeff Roberson 	struct lock_prof_type *type;
408eea4f254SJeff Roberson 	struct lock_prof *l;
409eea4f254SJeff Roberson 	int cpu;
410eea4f254SJeff Roberson 
411eea4f254SJeff Roberson 	dst->file = match->file;
412eea4f254SJeff Roberson 	dst->line = match->line;
4130c66dc67SJeff Roberson 	dst->class = match->class;
414eea4f254SJeff Roberson 	dst->name = match->name;
415eea4f254SJeff Roberson 
416cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
417d2be3ef0SMateusz Guzik 		type = &LP_CPU(cpu)->lpc_types[spin];
418eea4f254SJeff Roberson 		SLIST_FOREACH(l, &type->lpt_hash[hash], link) {
419eea4f254SJeff Roberson 			if (l->ticks == t)
420eea4f254SJeff Roberson 				continue;
421eea4f254SJeff Roberson 			if (l->file != match->file || l->line != match->line ||
4220c66dc67SJeff Roberson 			    l->name != match->name)
423eea4f254SJeff Roberson 				continue;
424eea4f254SJeff Roberson 			l->ticks = t;
425eea4f254SJeff Roberson 			if (l->cnt_max > dst->cnt_max)
426eea4f254SJeff Roberson 				dst->cnt_max = l->cnt_max;
427947265b6SKip Macy 			if (l->cnt_wait_max > dst->cnt_wait_max)
428947265b6SKip Macy 				dst->cnt_wait_max = l->cnt_wait_max;
429eea4f254SJeff Roberson 			dst->cnt_tot += l->cnt_tot;
430eea4f254SJeff Roberson 			dst->cnt_wait += l->cnt_wait;
431eea4f254SJeff Roberson 			dst->cnt_cur += l->cnt_cur;
432eea4f254SJeff Roberson 			dst->cnt_contest_locking += l->cnt_contest_locking;
433eea4f254SJeff Roberson 		}
434eea4f254SJeff Roberson 	}
435eea4f254SJeff Roberson }
436eea4f254SJeff Roberson 
437eea4f254SJeff Roberson static void
438eea4f254SJeff Roberson lock_prof_type_stats(struct lock_prof_type *type, struct sbuf *sb, int spin,
439eea4f254SJeff Roberson     int t)
440eea4f254SJeff Roberson {
441eea4f254SJeff Roberson 	struct lock_prof *l;
442eea4f254SJeff Roberson 	int i;
443eea4f254SJeff Roberson 
444eea4f254SJeff Roberson 	for (i = 0; i < LPROF_HASH_SIZE; ++i) {
445eea4f254SJeff Roberson 		SLIST_FOREACH(l, &type->lpt_hash[i], link) {
446eea4f254SJeff Roberson 			struct lock_prof lp = {};
447eea4f254SJeff Roberson 
448eea4f254SJeff Roberson 			if (l->ticks == t)
449eea4f254SJeff Roberson 				continue;
450eea4f254SJeff Roberson 			lock_prof_sum(l, &lp, i, spin, t);
451eea4f254SJeff Roberson 			lock_prof_output(&lp, sb);
452eea4f254SJeff Roberson 		}
453eea4f254SJeff Roberson 	}
454eea4f254SJeff Roberson }
455eea4f254SJeff Roberson 
456eea4f254SJeff Roberson static int
457eea4f254SJeff Roberson dump_lock_prof_stats(SYSCTL_HANDLER_ARGS)
458eea4f254SJeff Roberson {
459eea4f254SJeff Roberson 	struct sbuf *sb;
460eea4f254SJeff Roberson 	int error, cpu, t;
4610c66dc67SJeff Roberson 	int enabled;
462eea4f254SJeff Roberson 
46300f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
46400f0e671SMatthew D Fleming 	if (error != 0)
46500f0e671SMatthew D Fleming 		return (error);
4664e657159SMatthew D Fleming 	sb = sbuf_new_for_sysctl(NULL, NULL, LPROF_SBUF_SIZE, req);
467947265b6SKip Macy 	sbuf_printf(sb, "\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n",
468947265b6SKip Macy 	    "max", "wait_max", "total", "wait_total", "count", "avg", "wait_avg", "cnt_hold", "cnt_lock", "name");
4690c66dc67SJeff Roberson 	enabled = lock_prof_enable;
4700c66dc67SJeff Roberson 	lock_prof_enable = 0;
4713ac2ac2eSMateusz Guzik 	/*
4723ac2ac2eSMateusz Guzik 	 * See the comment in lock_prof_reset
4733ac2ac2eSMateusz Guzik 	 */
4743ac2ac2eSMateusz Guzik 	cpus_fence_seq_cst();
4753ac2ac2eSMateusz Guzik 	quiesce_all_critical();
476eea4f254SJeff Roberson 	t = ticks;
477cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
478d2be3ef0SMateusz Guzik 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[0], sb, 0, t);
479d2be3ef0SMateusz Guzik 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[1], sb, 1, t);
480eea4f254SJeff Roberson 	}
4813ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
4820c66dc67SJeff Roberson 	lock_prof_enable = enabled;
483eea4f254SJeff Roberson 
4844e657159SMatthew D Fleming 	error = sbuf_finish(sb);
4854e657159SMatthew D Fleming 	/* Output a trailing NUL. */
4864e657159SMatthew D Fleming 	if (error == 0)
4874e657159SMatthew D Fleming 		error = SYSCTL_OUT(req, "", 1);
488eea4f254SJeff Roberson 	sbuf_delete(sb);
489eea4f254SJeff Roberson 	return (error);
490eea4f254SJeff Roberson }
491eea4f254SJeff Roberson 
492eea4f254SJeff Roberson static int
493eea4f254SJeff Roberson enable_lock_prof(SYSCTL_HANDLER_ARGS)
494eea4f254SJeff Roberson {
495eea4f254SJeff Roberson 	int error, v;
496eea4f254SJeff Roberson 
497eea4f254SJeff Roberson 	v = lock_prof_enable;
498eea4f254SJeff Roberson 	error = sysctl_handle_int(oidp, &v, v, req);
499eea4f254SJeff Roberson 	if (error)
500eea4f254SJeff Roberson 		return (error);
501eea4f254SJeff Roberson 	if (req->newptr == NULL)
502eea4f254SJeff Roberson 		return (error);
503eea4f254SJeff Roberson 	if (v == lock_prof_enable)
504eea4f254SJeff Roberson 		return (0);
505eea4f254SJeff Roberson 	if (v == 1)
506eea4f254SJeff Roberson 		lock_prof_reset();
507eea4f254SJeff Roberson 	lock_prof_enable = !!v;
508eea4f254SJeff Roberson 
509eea4f254SJeff Roberson 	return (0);
510eea4f254SJeff Roberson }
511eea4f254SJeff Roberson 
512eea4f254SJeff Roberson static int
513eea4f254SJeff Roberson reset_lock_prof_stats(SYSCTL_HANDLER_ARGS)
514eea4f254SJeff Roberson {
515eea4f254SJeff Roberson 	int error, v;
516eea4f254SJeff Roberson 
517eea4f254SJeff Roberson 	v = 0;
518eea4f254SJeff Roberson 	error = sysctl_handle_int(oidp, &v, 0, req);
519eea4f254SJeff Roberson 	if (error)
520eea4f254SJeff Roberson 		return (error);
521eea4f254SJeff Roberson 	if (req->newptr == NULL)
522eea4f254SJeff Roberson 		return (error);
523eea4f254SJeff Roberson 	if (v == 0)
524eea4f254SJeff Roberson 		return (0);
525eea4f254SJeff Roberson 	lock_prof_reset();
526eea4f254SJeff Roberson 
527eea4f254SJeff Roberson 	return (0);
528eea4f254SJeff Roberson }
529eea4f254SJeff Roberson 
530eea4f254SJeff Roberson static struct lock_prof *
531eea4f254SJeff Roberson lock_profile_lookup(struct lock_object *lo, int spin, const char *file,
532eea4f254SJeff Roberson     int line)
533eea4f254SJeff Roberson {
534eea4f254SJeff Roberson 	const char *unknown = "(unknown)";
535eea4f254SJeff Roberson 	struct lock_prof_type *type;
536eea4f254SJeff Roberson 	struct lock_prof *lp;
537eea4f254SJeff Roberson 	struct lphead *head;
538eea4f254SJeff Roberson 	const char *p;
539eea4f254SJeff Roberson 	u_int hash;
540eea4f254SJeff Roberson 
541eea4f254SJeff Roberson 	p = file;
542eea4f254SJeff Roberson 	if (p == NULL || *p == '\0')
543eea4f254SJeff Roberson 		p = unknown;
544eea4f254SJeff Roberson 	hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line;
545eea4f254SJeff Roberson 	hash &= LPROF_HASH_MASK;
546d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
547eea4f254SJeff Roberson 	head = &type->lpt_hash[hash];
548eea4f254SJeff Roberson 	SLIST_FOREACH(lp, head, link) {
549eea4f254SJeff Roberson 		if (lp->line == line && lp->file == p &&
550eea4f254SJeff Roberson 		    lp->name == lo->lo_name)
551eea4f254SJeff Roberson 			return (lp);
552eea4f254SJeff Roberson 	}
553eea4f254SJeff Roberson 	lp = SLIST_FIRST(&type->lpt_lpalloc);
554eea4f254SJeff Roberson 	if (lp == NULL) {
555eea4f254SJeff Roberson 		lock_prof_rejected++;
556eea4f254SJeff Roberson 		return (lp);
557eea4f254SJeff Roberson 	}
558eea4f254SJeff Roberson 	SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link);
559eea4f254SJeff Roberson 	lp->file = p;
560eea4f254SJeff Roberson 	lp->line = line;
5610c66dc67SJeff Roberson 	lp->class = LOCK_CLASS(lo);
562eea4f254SJeff Roberson 	lp->name = lo->lo_name;
563eea4f254SJeff Roberson 	SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link);
564eea4f254SJeff Roberson 	return (lp);
565eea4f254SJeff Roberson }
566eea4f254SJeff Roberson 
567eea4f254SJeff Roberson static struct lock_profile_object *
568eea4f254SJeff Roberson lock_profile_object_lookup(struct lock_object *lo, int spin, const char *file,
569eea4f254SJeff Roberson     int line)
570eea4f254SJeff Roberson {
571eea4f254SJeff Roberson 	struct lock_profile_object *l;
572eea4f254SJeff Roberson 	struct lock_prof_type *type;
573eea4f254SJeff Roberson 	struct lpohead *head;
574eea4f254SJeff Roberson 
575eea4f254SJeff Roberson 	head = &curthread->td_lprof[spin];
576eea4f254SJeff Roberson 	LIST_FOREACH(l, head, lpo_link)
577eea4f254SJeff Roberson 		if (l->lpo_obj == lo && l->lpo_file == file &&
578eea4f254SJeff Roberson 		    l->lpo_line == line)
579eea4f254SJeff Roberson 			return (l);
580d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
581eea4f254SJeff Roberson 	l = LIST_FIRST(&type->lpt_lpoalloc);
582eea4f254SJeff Roberson 	if (l == NULL) {
583eea4f254SJeff Roberson 		lock_prof_rejected++;
584eea4f254SJeff Roberson 		return (NULL);
585eea4f254SJeff Roberson 	}
586eea4f254SJeff Roberson 	LIST_REMOVE(l, lpo_link);
587eea4f254SJeff Roberson 	l->lpo_obj = lo;
588eea4f254SJeff Roberson 	l->lpo_file = file;
589eea4f254SJeff Roberson 	l->lpo_line = line;
590eea4f254SJeff Roberson 	l->lpo_cnt = 0;
591eea4f254SJeff Roberson 	LIST_INSERT_HEAD(head, l, lpo_link);
592eea4f254SJeff Roberson 
593eea4f254SJeff Roberson 	return (l);
594eea4f254SJeff Roberson }
595eea4f254SJeff Roberson 
596eea4f254SJeff Roberson void
597eea4f254SJeff Roberson lock_profile_obtain_lock_success(struct lock_object *lo, int contested,
598eea4f254SJeff Roberson     uint64_t waittime, const char *file, int line)
599eea4f254SJeff Roberson {
600eea4f254SJeff Roberson 	struct lock_profile_object *l;
601eea4f254SJeff Roberson 	int spin;
602eea4f254SJeff Roberson 
60335370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
60435370593SAndriy Gapon 		return;
60535370593SAndriy Gapon 
606eea4f254SJeff Roberson 	/* don't reset the timer when/if recursing */
607eea4f254SJeff Roberson 	if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE))
608eea4f254SJeff Roberson 		return;
609*a0842e69SMateusz Guzik 	if (lock_contested_only && !contested)
610*a0842e69SMateusz Guzik 		return;
61113ddf72dSAttilio Rao 	spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
612eea4f254SJeff Roberson 	if (spin && lock_prof_skipspin == 1)
613eea4f254SJeff Roberson 		return;
6142e6b8de4SJeff Roberson 	critical_enter();
6152e6b8de4SJeff Roberson 	/* Recheck enabled now that we're in a critical section. */
6162e6b8de4SJeff Roberson 	if (lock_prof_enable == 0)
6172e6b8de4SJeff Roberson 		goto out;
618eea4f254SJeff Roberson 	l = lock_profile_object_lookup(lo, spin, file, line);
619eea4f254SJeff Roberson 	if (l == NULL)
6202e6b8de4SJeff Roberson 		goto out;
621eea4f254SJeff Roberson 	l->lpo_cnt++;
622eea4f254SJeff Roberson 	if (++l->lpo_ref > 1)
6232e6b8de4SJeff Roberson 		goto out;
624eea4f254SJeff Roberson 	l->lpo_contest_locking = contested;
6257c0435b9SKip Macy 	l->lpo_acqtime = nanoseconds();
626aa077979SKip Macy 	if (waittime && (l->lpo_acqtime > waittime))
6277c0435b9SKip Macy 		l->lpo_waittime = l->lpo_acqtime - waittime;
628aa077979SKip Macy 	else
629aa077979SKip Macy 		l->lpo_waittime = 0;
6302e6b8de4SJeff Roberson out:
6313ac2ac2eSMateusz Guzik 	/*
6323ac2ac2eSMateusz Guzik 	 * Paired with cpus_fence_seq_cst().
6333ac2ac2eSMateusz Guzik 	 */
6343ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
6352e6b8de4SJeff Roberson 	critical_exit();
6362e6b8de4SJeff Roberson }
6372e6b8de4SJeff Roberson 
6382e6b8de4SJeff Roberson void
6392e6b8de4SJeff Roberson lock_profile_thread_exit(struct thread *td)
6402e6b8de4SJeff Roberson {
6412e6b8de4SJeff Roberson #ifdef INVARIANTS
6422e6b8de4SJeff Roberson 	struct lock_profile_object *l;
6432e6b8de4SJeff Roberson 
6442e6b8de4SJeff Roberson 	MPASS(curthread->td_critnest == 0);
6452e6b8de4SJeff Roberson #endif
6462e6b8de4SJeff Roberson 	/*
6472e6b8de4SJeff Roberson 	 * If lock profiling was disabled we have to wait for reset to
6482e6b8de4SJeff Roberson 	 * clear our pointers before we can exit safely.
6492e6b8de4SJeff Roberson 	 */
6502e6b8de4SJeff Roberson 	lock_prof_reset_wait();
6512e6b8de4SJeff Roberson #ifdef INVARIANTS
6522e6b8de4SJeff Roberson 	LIST_FOREACH(l, &td->td_lprof[0], lpo_link)
6532e6b8de4SJeff Roberson 		printf("thread still holds lock acquired at %s:%d\n",
6542e6b8de4SJeff Roberson 		    l->lpo_file, l->lpo_line);
6552e6b8de4SJeff Roberson 	LIST_FOREACH(l, &td->td_lprof[1], lpo_link)
6562e6b8de4SJeff Roberson 		printf("thread still holds lock acquired at %s:%d\n",
6572e6b8de4SJeff Roberson 		    l->lpo_file, l->lpo_line);
6582e6b8de4SJeff Roberson #endif
6592e6b8de4SJeff Roberson 	MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL);
6602e6b8de4SJeff Roberson 	MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL);
6617c0435b9SKip Macy }
6627c0435b9SKip Macy 
663eea4f254SJeff Roberson void
664eea4f254SJeff Roberson lock_profile_release_lock(struct lock_object *lo)
6657c0435b9SKip Macy {
666eea4f254SJeff Roberson 	struct lock_profile_object *l;
667eea4f254SJeff Roberson 	struct lock_prof_type *type;
668eea4f254SJeff Roberson 	struct lock_prof *lp;
66960ae52f7SEd Schouten 	uint64_t curtime, holdtime;
670eea4f254SJeff Roberson 	struct lpohead *head;
671eea4f254SJeff Roberson 	int spin;
6727c0435b9SKip Macy 
67335370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
67435370593SAndriy Gapon 		return;
6752e6b8de4SJeff Roberson 	if (lo->lo_flags & LO_NOPROFILE)
6767c0435b9SKip Macy 		return;
67713ddf72dSAttilio Rao 	spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
678eea4f254SJeff Roberson 	head = &curthread->td_lprof[spin];
6792e6b8de4SJeff Roberson 	if (LIST_FIRST(head) == NULL)
6802e6b8de4SJeff Roberson 		return;
681eea4f254SJeff Roberson 	critical_enter();
6822e6b8de4SJeff Roberson 	/* Recheck enabled now that we're in a critical section. */
6832e6b8de4SJeff Roberson 	if (lock_prof_enable == 0 && lock_prof_resetting == 1)
6842e6b8de4SJeff Roberson 		goto out;
6852e6b8de4SJeff Roberson 	/*
6862e6b8de4SJeff Roberson 	 * If lock profiling is not enabled we still want to remove the
6872e6b8de4SJeff Roberson 	 * lpo from our queue.
6882e6b8de4SJeff Roberson 	 */
689eea4f254SJeff Roberson 	LIST_FOREACH(l, head, lpo_link)
690eea4f254SJeff Roberson 		if (l->lpo_obj == lo)
6917c0435b9SKip Macy 			break;
692eea4f254SJeff Roberson 	if (l == NULL)
693eea4f254SJeff Roberson 		goto out;
694eea4f254SJeff Roberson 	if (--l->lpo_ref > 0)
695eea4f254SJeff Roberson 		goto out;
696eea4f254SJeff Roberson 	lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line);
697eea4f254SJeff Roberson 	if (lp == NULL)
698eea4f254SJeff Roberson 		goto release;
699e7154e7eSAndriy Gapon 	curtime = nanoseconds();
700e7154e7eSAndriy Gapon 	if (curtime < l->lpo_acqtime)
701eea4f254SJeff Roberson 		goto release;
702e7154e7eSAndriy Gapon 	holdtime = curtime - l->lpo_acqtime;
703e7154e7eSAndriy Gapon 
7047c0435b9SKip Macy 	/*
70583b72e3eSKip Macy 	 * Record if the lock has been held longer now than ever
7067c0435b9SKip Macy 	 * before.
7077c0435b9SKip Macy 	 */
708eea4f254SJeff Roberson 	if (holdtime > lp->cnt_max)
709eea4f254SJeff Roberson 		lp->cnt_max = holdtime;
710947265b6SKip Macy 	if (l->lpo_waittime > lp->cnt_wait_max)
711947265b6SKip Macy 		lp->cnt_wait_max = l->lpo_waittime;
712eea4f254SJeff Roberson 	lp->cnt_tot += holdtime;
713eea4f254SJeff Roberson 	lp->cnt_wait += l->lpo_waittime;
714eea4f254SJeff Roberson 	lp->cnt_contest_locking += l->lpo_contest_locking;
715eea4f254SJeff Roberson 	lp->cnt_cur += l->lpo_cnt;
716eea4f254SJeff Roberson release:
717eea4f254SJeff Roberson 	LIST_REMOVE(l, lpo_link);
718d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
719eea4f254SJeff Roberson 	LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link);
720eea4f254SJeff Roberson out:
7213ac2ac2eSMateusz Guzik 	/*
7223ac2ac2eSMateusz Guzik 	 * Paired with cpus_fence_seq_cst().
7233ac2ac2eSMateusz Guzik 	 */
7243ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
725eea4f254SJeff Roberson 	critical_exit();
726eea4f254SJeff Roberson }
7277c0435b9SKip Macy 
7287029da5cSPawel Biernacki static SYSCTL_NODE(_debug_lock, OID_AUTO, prof,
7297029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7306472ac3dSEd Schouten     "lock profiling");
731eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW,
732eea4f254SJeff Roberson     &lock_prof_skipspin, 0, "Skip profiling on spinlocks.");
733eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD,
734eea4f254SJeff Roberson     &lock_prof_rejected, 0, "Number of rejected profiling records");
735*a0842e69SMateusz Guzik SYSCTL_INT(_debug_lock_prof, OID_AUTO, contested_only, CTLFLAG_RW,
736*a0842e69SMateusz Guzik     &lock_contested_only, 0, "Only profile contested acquires");
7377029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats,
7387029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
7397029da5cSPawel Biernacki     dump_lock_prof_stats, "A",
7407029da5cSPawel Biernacki     "Lock profiling statistics");
7417029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset,
7427029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
7437029da5cSPawel Biernacki     reset_lock_prof_stats, "I",
7447029da5cSPawel Biernacki     "Reset lock profiling statistics");
7457029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable,
7467029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
7477029da5cSPawel Biernacki     enable_lock_prof, "I",
7487029da5cSPawel Biernacki     "Enable lock profiling");
749eea4f254SJeff Roberson 
7507c0435b9SKip Macy #endif
751