xref: /freebsd/sys/kern/subr_lock.c (revision 3ac2ac2e085ac3be61caddc3ec9982443e641c4b)
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>
4383a81bcbSJohn Baldwin #include <sys/lock.h>
447c0435b9SKip Macy #include <sys/lock_profile.h>
45eea4f254SJeff Roberson #include <sys/malloc.h>
462e6b8de4SJeff Roberson #include <sys/mutex.h>
47eea4f254SJeff Roberson #include <sys/pcpu.h>
48eea4f254SJeff Roberson #include <sys/proc.h>
49eea4f254SJeff Roberson #include <sys/sbuf.h>
502e6b8de4SJeff Roberson #include <sys/sched.h>
51eea4f254SJeff Roberson #include <sys/smp.h>
52eea4f254SJeff Roberson #include <sys/sysctl.h>
5383a81bcbSJohn Baldwin 
5483a81bcbSJohn Baldwin #ifdef DDB
5583a81bcbSJohn Baldwin #include <ddb/ddb.h>
5683a81bcbSJohn Baldwin #endif
5783a81bcbSJohn Baldwin 
58eea4f254SJeff Roberson #include <machine/cpufunc.h>
59eea4f254SJeff Roberson 
608e5a3e9aSMateusz Guzik SDT_PROVIDER_DEFINE(lock);
618e5a3e9aSMateusz Guzik SDT_PROBE_DEFINE1(lock, , , starvation, "u_int");
628e5a3e9aSMateusz Guzik 
6383a81bcbSJohn Baldwin CTASSERT(LOCK_CLASS_MAX == 15);
6483a81bcbSJohn Baldwin 
6583a81bcbSJohn Baldwin struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = {
6683a81bcbSJohn Baldwin 	&lock_class_mtx_spin,
6783a81bcbSJohn Baldwin 	&lock_class_mtx_sleep,
6883a81bcbSJohn Baldwin 	&lock_class_sx,
69f53d15feSStephan Uphoff 	&lock_class_rm,
70cd32bd7aSJohn Baldwin 	&lock_class_rm_sleepable,
713f08bd8bSJohn Baldwin 	&lock_class_rw,
7261bd5e21SKip Macy 	&lock_class_lockmgr,
7383a81bcbSJohn Baldwin };
7483a81bcbSJohn Baldwin 
7583a81bcbSJohn Baldwin void
7683a81bcbSJohn Baldwin lock_init(struct lock_object *lock, struct lock_class *class, const char *name,
7783a81bcbSJohn Baldwin     const char *type, int flags)
7883a81bcbSJohn Baldwin {
7983a81bcbSJohn Baldwin 	int i;
8083a81bcbSJohn Baldwin 
8183a81bcbSJohn Baldwin 	/* Check for double-init and zero object. */
82fd07ddcfSDmitry Chagin 	KASSERT(flags & LO_NEW || !lock_initialized(lock),
83fd07ddcfSDmitry Chagin 	    ("lock \"%s\" %p already initialized", name, lock));
8483a81bcbSJohn Baldwin 
8583a81bcbSJohn Baldwin 	/* Look up lock class to find its index. */
8683a81bcbSJohn Baldwin 	for (i = 0; i < LOCK_CLASS_MAX; i++)
8783a81bcbSJohn Baldwin 		if (lock_classes[i] == class) {
8883a81bcbSJohn Baldwin 			lock->lo_flags = i << LO_CLASSSHIFT;
8983a81bcbSJohn Baldwin 			break;
9083a81bcbSJohn Baldwin 		}
9183a81bcbSJohn Baldwin 	KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));
9283a81bcbSJohn Baldwin 
9383a81bcbSJohn Baldwin 	/* Initialize the lock object. */
9483a81bcbSJohn Baldwin 	lock->lo_name = name;
9583a81bcbSJohn Baldwin 	lock->lo_flags |= flags | LO_INITIALIZED;
9683a81bcbSJohn Baldwin 	LOCK_LOG_INIT(lock, 0);
9790356491SAttilio Rao 	WITNESS_INIT(lock, (type != NULL) ? type : name);
9883a81bcbSJohn Baldwin }
9983a81bcbSJohn Baldwin 
10083a81bcbSJohn Baldwin void
10183a81bcbSJohn Baldwin lock_destroy(struct lock_object *lock)
10283a81bcbSJohn Baldwin {
10383a81bcbSJohn Baldwin 
1043a6cdc4eSJohn-Mark Gurney 	KASSERT(lock_initialized(lock), ("lock %p is not initialized", lock));
10583a81bcbSJohn Baldwin 	WITNESS_DESTROY(lock);
10683a81bcbSJohn Baldwin 	LOCK_LOG_DESTROY(lock, 0);
10783a81bcbSJohn Baldwin 	lock->lo_flags &= ~LO_INITIALIZED;
10883a81bcbSJohn Baldwin }
10983a81bcbSJohn Baldwin 
1108e5a3e9aSMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD, NULL, "lock debugging");
1118e5a3e9aSMateusz Guzik static SYSCTL_NODE(_debug_lock, OID_AUTO, delay, CTLFLAG_RD, NULL,
1128e5a3e9aSMateusz Guzik     "lock delay");
1138e5a3e9aSMateusz Guzik 
1148e5a3e9aSMateusz Guzik static u_int __read_mostly starvation_limit = 131072;
1158e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_lock_delay, OID_AUTO, starvation_limit, CTLFLAG_RW,
1168e5a3e9aSMateusz Guzik     &starvation_limit, 0, "");
1178e5a3e9aSMateusz Guzik 
1188e5a3e9aSMateusz Guzik static u_int __read_mostly restrict_starvation = 0;
1198e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_lock_delay, OID_AUTO, restrict_starvation, CTLFLAG_RW,
1208e5a3e9aSMateusz Guzik     &restrict_starvation, 0, "");
1218e5a3e9aSMateusz Guzik 
1221ada9041SMateusz Guzik void
1231ada9041SMateusz Guzik lock_delay(struct lock_delay_arg *la)
1241ada9041SMateusz Guzik {
1251ada9041SMateusz Guzik 	struct lock_delay_config *lc = la->config;
1268e5a3e9aSMateusz Guzik 	u_int i;
1271ada9041SMateusz Guzik 
1288e5a3e9aSMateusz Guzik 	la->delay <<= 1;
1298e5a3e9aSMateusz Guzik 	if (__predict_false(la->delay > lc->max))
1308e5a3e9aSMateusz Guzik 		la->delay = lc->max;
1311ada9041SMateusz Guzik 
1323c798b2bSMateusz Guzik 	for (i = la->delay; i > 0; i--)
1331ada9041SMateusz Guzik 		cpu_spinwait();
1341ada9041SMateusz Guzik 
1358e5a3e9aSMateusz Guzik 	la->spin_cnt += la->delay;
1368e5a3e9aSMateusz Guzik 	if (__predict_false(la->spin_cnt > starvation_limit)) {
1378e5a3e9aSMateusz Guzik 		SDT_PROBE1(lock, , , starvation, la->delay);
1388e5a3e9aSMateusz Guzik 		if (restrict_starvation)
1398e5a3e9aSMateusz Guzik 			la->delay = lc->base;
1408e5a3e9aSMateusz Guzik 	}
1418e5a3e9aSMateusz Guzik }
1428e5a3e9aSMateusz Guzik 
1438e5a3e9aSMateusz Guzik static u_int
1448e5a3e9aSMateusz Guzik lock_roundup_2(u_int val)
1458e5a3e9aSMateusz Guzik {
1468e5a3e9aSMateusz Guzik 	u_int res;
1478e5a3e9aSMateusz Guzik 
1488e5a3e9aSMateusz Guzik 	for (res = 1; res <= val; res <<= 1)
1498e5a3e9aSMateusz Guzik 		continue;
1508e5a3e9aSMateusz Guzik 
1518e5a3e9aSMateusz Guzik 	return (res);
1528e5a3e9aSMateusz Guzik }
1538e5a3e9aSMateusz Guzik 
1548e5a3e9aSMateusz Guzik void
1558e5a3e9aSMateusz Guzik lock_delay_default_init(struct lock_delay_config *lc)
1568e5a3e9aSMateusz Guzik {
1578e5a3e9aSMateusz Guzik 
158a045941bSMateusz Guzik 	lc->base = 1;
159a045941bSMateusz Guzik 	lc->max = lock_roundup_2(mp_ncpus) * 256;
160a045941bSMateusz Guzik 	if (lc->max > 32678)
161a045941bSMateusz Guzik 		lc->max = 32678;
1621ada9041SMateusz Guzik }
1631ada9041SMateusz Guzik 
16483a81bcbSJohn Baldwin #ifdef DDB
16583a81bcbSJohn Baldwin DB_SHOW_COMMAND(lock, db_show_lock)
16683a81bcbSJohn Baldwin {
16783a81bcbSJohn Baldwin 	struct lock_object *lock;
16883a81bcbSJohn Baldwin 	struct lock_class *class;
16983a81bcbSJohn Baldwin 
17083a81bcbSJohn Baldwin 	if (!have_addr)
17183a81bcbSJohn Baldwin 		return;
17283a81bcbSJohn Baldwin 	lock = (struct lock_object *)addr;
17383a81bcbSJohn Baldwin 	if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) {
17483a81bcbSJohn Baldwin 		db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock));
17583a81bcbSJohn Baldwin 		return;
17683a81bcbSJohn Baldwin 	}
17783a81bcbSJohn Baldwin 	class = LOCK_CLASS(lock);
17883a81bcbSJohn Baldwin 	db_printf(" class: %s\n", class->lc_name);
17983a81bcbSJohn Baldwin 	db_printf(" name: %s\n", lock->lo_name);
18083a81bcbSJohn Baldwin 	class->lc_ddb_show(lock);
18183a81bcbSJohn Baldwin }
18283a81bcbSJohn Baldwin #endif
1837c0435b9SKip Macy 
1847c0435b9SKip Macy #ifdef LOCK_PROFILING
185eea4f254SJeff Roberson 
186eea4f254SJeff Roberson /*
187eea4f254SJeff Roberson  * One object per-thread for each lock the thread owns.  Tracks individual
188eea4f254SJeff Roberson  * lock instances.
189eea4f254SJeff Roberson  */
190eea4f254SJeff Roberson struct lock_profile_object {
191eea4f254SJeff Roberson 	LIST_ENTRY(lock_profile_object) lpo_link;
192eea4f254SJeff Roberson 	struct lock_object *lpo_obj;
193eea4f254SJeff Roberson 	const char	*lpo_file;
194eea4f254SJeff Roberson 	int		lpo_line;
195eea4f254SJeff Roberson 	uint16_t	lpo_ref;
196eea4f254SJeff Roberson 	uint16_t	lpo_cnt;
19760ae52f7SEd Schouten 	uint64_t	lpo_acqtime;
19860ae52f7SEd Schouten 	uint64_t	lpo_waittime;
199eea4f254SJeff Roberson 	u_int		lpo_contest_locking;
200eea4f254SJeff Roberson };
201eea4f254SJeff Roberson 
202eea4f254SJeff Roberson /*
203eea4f254SJeff Roberson  * One lock_prof for each (file, line, lock object) triple.
204eea4f254SJeff Roberson  */
205eea4f254SJeff Roberson struct lock_prof {
206eea4f254SJeff Roberson 	SLIST_ENTRY(lock_prof) link;
2070c66dc67SJeff Roberson 	struct lock_class *class;
208eea4f254SJeff Roberson 	const char	*file;
209eea4f254SJeff Roberson 	const char	*name;
210eea4f254SJeff Roberson 	int		line;
211eea4f254SJeff Roberson 	int		ticks;
212947265b6SKip Macy 	uintmax_t	cnt_wait_max;
213eea4f254SJeff Roberson 	uintmax_t	cnt_max;
214eea4f254SJeff Roberson 	uintmax_t	cnt_tot;
215eea4f254SJeff Roberson 	uintmax_t	cnt_wait;
216eea4f254SJeff Roberson 	uintmax_t	cnt_cur;
217eea4f254SJeff Roberson 	uintmax_t	cnt_contest_locking;
218eea4f254SJeff Roberson };
219eea4f254SJeff Roberson 
220eea4f254SJeff Roberson SLIST_HEAD(lphead, lock_prof);
221eea4f254SJeff Roberson 
222eea4f254SJeff Roberson #define	LPROF_HASH_SIZE		4096
223eea4f254SJeff Roberson #define	LPROF_HASH_MASK		(LPROF_HASH_SIZE - 1)
224eea4f254SJeff Roberson #define	LPROF_CACHE_SIZE	4096
225eea4f254SJeff Roberson 
226eea4f254SJeff Roberson /*
227eea4f254SJeff Roberson  * Array of objects and profs for each type of object for each cpu.  Spinlocks
228b1ce21c6SRebecca Cran  * are handled separately because a thread may be preempted and acquire a
229eea4f254SJeff Roberson  * spinlock while in the lock profiling code of a non-spinlock.  In this way
230eea4f254SJeff Roberson  * we only need a critical section to protect the per-cpu lists.
231eea4f254SJeff Roberson  */
232eea4f254SJeff Roberson struct lock_prof_type {
233eea4f254SJeff Roberson 	struct lphead		lpt_lpalloc;
234eea4f254SJeff Roberson 	struct lpohead		lpt_lpoalloc;
235eea4f254SJeff Roberson 	struct lphead		lpt_hash[LPROF_HASH_SIZE];
236eea4f254SJeff Roberson 	struct lock_prof	lpt_prof[LPROF_CACHE_SIZE];
237eea4f254SJeff Roberson 	struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE];
238eea4f254SJeff Roberson };
239eea4f254SJeff Roberson 
240eea4f254SJeff Roberson struct lock_prof_cpu {
241eea4f254SJeff Roberson 	struct lock_prof_type	lpc_types[2]; /* One for spin one for other. */
242eea4f254SJeff Roberson };
243eea4f254SJeff Roberson 
244d2be3ef0SMateusz Guzik DPCPU_DEFINE_STATIC(struct lock_prof_cpu, lp);
245d2be3ef0SMateusz Guzik #define	LP_CPU_SELF	(DPCPU_PTR(lp))
246d2be3ef0SMateusz Guzik #define	LP_CPU(cpu)	(DPCPU_ID_PTR((cpu), lp))
247eea4f254SJeff Roberson 
24829051116SMateusz Guzik volatile int __read_mostly lock_prof_enable;
2492e6b8de4SJeff Roberson static volatile int lock_prof_resetting;
250eea4f254SJeff Roberson 
2514e657159SMatthew D Fleming #define LPROF_SBUF_SIZE		256
252eea4f254SJeff Roberson 
253eea4f254SJeff Roberson static int lock_prof_rejected;
254eea4f254SJeff Roberson static int lock_prof_skipspin;
255eea4f254SJeff Roberson static int lock_prof_skipcount;
256eea4f254SJeff Roberson 
257eea4f254SJeff Roberson #ifndef USE_CPU_NANOSECONDS
25860ae52f7SEd Schouten uint64_t
259eea4f254SJeff Roberson nanoseconds(void)
2607c0435b9SKip Macy {
261eea4f254SJeff Roberson 	struct bintime bt;
26260ae52f7SEd Schouten 	uint64_t ns;
2637c0435b9SKip Macy 
264eea4f254SJeff Roberson 	binuptime(&bt);
265eea4f254SJeff Roberson 	/* From bintime2timespec */
26660ae52f7SEd Schouten 	ns = bt.sec * (uint64_t)1000000000;
267eea4f254SJeff Roberson 	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
268eea4f254SJeff Roberson 	return (ns);
269eea4f254SJeff Roberson }
270eea4f254SJeff Roberson #endif
271fe68a916SKip Macy 
272eea4f254SJeff Roberson static void
273eea4f254SJeff Roberson lock_prof_init_type(struct lock_prof_type *type)
274eea4f254SJeff Roberson {
275eea4f254SJeff Roberson 	int i;
276fe68a916SKip Macy 
277eea4f254SJeff Roberson 	SLIST_INIT(&type->lpt_lpalloc);
278eea4f254SJeff Roberson 	LIST_INIT(&type->lpt_lpoalloc);
279eea4f254SJeff Roberson 	for (i = 0; i < LPROF_CACHE_SIZE; i++) {
280eea4f254SJeff Roberson 		SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i],
281eea4f254SJeff Roberson 		    link);
282eea4f254SJeff Roberson 		LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i],
283eea4f254SJeff Roberson 		    lpo_link);
284eea4f254SJeff Roberson 	}
285eea4f254SJeff Roberson }
286eea4f254SJeff Roberson 
287eea4f254SJeff Roberson static void
288eea4f254SJeff Roberson lock_prof_init(void *arg)
289eea4f254SJeff Roberson {
290eea4f254SJeff Roberson 	int cpu;
291eea4f254SJeff Roberson 
292cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
293d2be3ef0SMateusz Guzik 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[0]);
294d2be3ef0SMateusz Guzik 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[1]);
295eea4f254SJeff Roberson 	}
296eea4f254SJeff Roberson }
297eea4f254SJeff Roberson SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL);
298eea4f254SJeff Roberson 
2992e6b8de4SJeff Roberson static void
3002e6b8de4SJeff Roberson lock_prof_reset_wait(void)
3012e6b8de4SJeff Roberson {
3022e6b8de4SJeff Roberson 
3032e6b8de4SJeff Roberson 	/*
30428d91af3SJeff Roberson 	 * Spin relinquishing our cpu so that quiesce_all_cpus may
30528d91af3SJeff Roberson 	 * complete.
3062e6b8de4SJeff Roberson 	 */
3072e6b8de4SJeff Roberson 	while (lock_prof_resetting)
3082e6b8de4SJeff Roberson 		sched_relinquish(curthread);
3092e6b8de4SJeff Roberson }
3102e6b8de4SJeff Roberson 
311eea4f254SJeff Roberson static void
312eea4f254SJeff Roberson lock_prof_reset(void)
313eea4f254SJeff Roberson {
314eea4f254SJeff Roberson 	struct lock_prof_cpu *lpc;
315eea4f254SJeff Roberson 	int enabled, i, cpu;
316eea4f254SJeff Roberson 
3172e6b8de4SJeff Roberson 	/*
3182e6b8de4SJeff Roberson 	 * We not only race with acquiring and releasing locks but also
3192e6b8de4SJeff Roberson 	 * thread exit.  To be certain that threads exit without valid head
3202e6b8de4SJeff Roberson 	 * pointers they must see resetting set before enabled is cleared.
3212e6b8de4SJeff Roberson 	 * Otherwise a lock may not be removed from a per-thread list due
3222e6b8de4SJeff Roberson 	 * to disabled being set but not wait for reset() to remove it below.
3232e6b8de4SJeff Roberson 	 */
3242e6b8de4SJeff Roberson 	atomic_store_rel_int(&lock_prof_resetting, 1);
325eea4f254SJeff Roberson 	enabled = lock_prof_enable;
326eea4f254SJeff Roberson 	lock_prof_enable = 0;
327*3ac2ac2eSMateusz Guzik 	/*
328*3ac2ac2eSMateusz Guzik 	 * This both publishes lock_prof_enable as disabled and makes sure
329*3ac2ac2eSMateusz Guzik 	 * everyone else reads it if they are not far enough. We wait for the
330*3ac2ac2eSMateusz Guzik 	 * rest down below.
331*3ac2ac2eSMateusz Guzik 	 */
332*3ac2ac2eSMateusz Guzik 	cpus_fence_seq_cst();
333*3ac2ac2eSMateusz Guzik 	quiesce_all_critical();
3342e6b8de4SJeff Roberson 	/*
3352e6b8de4SJeff Roberson 	 * Some objects may have migrated between CPUs.  Clear all links
3362e6b8de4SJeff Roberson 	 * before we zero the structures.  Some items may still be linked
3372e6b8de4SJeff Roberson 	 * into per-thread lists as well.
3382e6b8de4SJeff Roberson 	 */
339cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
340d2be3ef0SMateusz Guzik 		lpc = LP_CPU(cpu);
341eea4f254SJeff Roberson 		for (i = 0; i < LPROF_CACHE_SIZE; i++) {
342eea4f254SJeff Roberson 			LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link);
343eea4f254SJeff Roberson 			LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link);
344eea4f254SJeff Roberson 		}
3452e6b8de4SJeff Roberson 	}
346cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
347d2be3ef0SMateusz Guzik 		lpc = LP_CPU(cpu);
348eea4f254SJeff Roberson 		bzero(lpc, sizeof(*lpc));
349eea4f254SJeff Roberson 		lock_prof_init_type(&lpc->lpc_types[0]);
350eea4f254SJeff Roberson 		lock_prof_init_type(&lpc->lpc_types[1]);
351eea4f254SJeff Roberson 	}
352*3ac2ac2eSMateusz Guzik 	/*
353*3ac2ac2eSMateusz Guzik 	 * Paired with the fence from cpus_fence_seq_cst()
354*3ac2ac2eSMateusz Guzik 	 */
3552e6b8de4SJeff Roberson 	atomic_store_rel_int(&lock_prof_resetting, 0);
356eea4f254SJeff Roberson 	lock_prof_enable = enabled;
357eea4f254SJeff Roberson }
358eea4f254SJeff Roberson 
359eea4f254SJeff Roberson static void
360eea4f254SJeff Roberson lock_prof_output(struct lock_prof *lp, struct sbuf *sb)
361eea4f254SJeff Roberson {
362eea4f254SJeff Roberson 	const char *p;
363eea4f254SJeff Roberson 
364eea4f254SJeff Roberson 	for (p = lp->file; p != NULL && strncmp(p, "../", 3) == 0; p += 3);
365eea4f254SJeff Roberson 	sbuf_printf(sb,
366947265b6SKip Macy 	    "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n",
367947265b6SKip Macy 	    lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000,
368eea4f254SJeff Roberson 	    lp->cnt_wait / 1000, lp->cnt_cur,
369eea4f254SJeff Roberson 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
370eea4f254SJeff Roberson 	    lp->cnt_tot / (lp->cnt_cur * 1000),
371eea4f254SJeff Roberson 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
372eea4f254SJeff Roberson 	    lp->cnt_wait / (lp->cnt_cur * 1000),
373eea4f254SJeff Roberson 	    (uintmax_t)0, lp->cnt_contest_locking,
3740c66dc67SJeff Roberson 	    p, lp->line, lp->class->lc_name, lp->name);
375eea4f254SJeff Roberson }
376eea4f254SJeff Roberson 
377eea4f254SJeff Roberson static void
378eea4f254SJeff Roberson lock_prof_sum(struct lock_prof *match, struct lock_prof *dst, int hash,
379eea4f254SJeff Roberson     int spin, int t)
380eea4f254SJeff Roberson {
381eea4f254SJeff Roberson 	struct lock_prof_type *type;
382eea4f254SJeff Roberson 	struct lock_prof *l;
383eea4f254SJeff Roberson 	int cpu;
384eea4f254SJeff Roberson 
385eea4f254SJeff Roberson 	dst->file = match->file;
386eea4f254SJeff Roberson 	dst->line = match->line;
3870c66dc67SJeff Roberson 	dst->class = match->class;
388eea4f254SJeff Roberson 	dst->name = match->name;
389eea4f254SJeff Roberson 
390cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
391d2be3ef0SMateusz Guzik 		type = &LP_CPU(cpu)->lpc_types[spin];
392eea4f254SJeff Roberson 		SLIST_FOREACH(l, &type->lpt_hash[hash], link) {
393eea4f254SJeff Roberson 			if (l->ticks == t)
394eea4f254SJeff Roberson 				continue;
395eea4f254SJeff Roberson 			if (l->file != match->file || l->line != match->line ||
3960c66dc67SJeff Roberson 			    l->name != match->name)
397eea4f254SJeff Roberson 				continue;
398eea4f254SJeff Roberson 			l->ticks = t;
399eea4f254SJeff Roberson 			if (l->cnt_max > dst->cnt_max)
400eea4f254SJeff Roberson 				dst->cnt_max = l->cnt_max;
401947265b6SKip Macy 			if (l->cnt_wait_max > dst->cnt_wait_max)
402947265b6SKip Macy 				dst->cnt_wait_max = l->cnt_wait_max;
403eea4f254SJeff Roberson 			dst->cnt_tot += l->cnt_tot;
404eea4f254SJeff Roberson 			dst->cnt_wait += l->cnt_wait;
405eea4f254SJeff Roberson 			dst->cnt_cur += l->cnt_cur;
406eea4f254SJeff Roberson 			dst->cnt_contest_locking += l->cnt_contest_locking;
407eea4f254SJeff Roberson 		}
408eea4f254SJeff Roberson 	}
409eea4f254SJeff Roberson }
410eea4f254SJeff Roberson 
411eea4f254SJeff Roberson static void
412eea4f254SJeff Roberson lock_prof_type_stats(struct lock_prof_type *type, struct sbuf *sb, int spin,
413eea4f254SJeff Roberson     int t)
414eea4f254SJeff Roberson {
415eea4f254SJeff Roberson 	struct lock_prof *l;
416eea4f254SJeff Roberson 	int i;
417eea4f254SJeff Roberson 
418eea4f254SJeff Roberson 	for (i = 0; i < LPROF_HASH_SIZE; ++i) {
419eea4f254SJeff Roberson 		SLIST_FOREACH(l, &type->lpt_hash[i], link) {
420eea4f254SJeff Roberson 			struct lock_prof lp = {};
421eea4f254SJeff Roberson 
422eea4f254SJeff Roberson 			if (l->ticks == t)
423eea4f254SJeff Roberson 				continue;
424eea4f254SJeff Roberson 			lock_prof_sum(l, &lp, i, spin, t);
425eea4f254SJeff Roberson 			lock_prof_output(&lp, sb);
426eea4f254SJeff Roberson 		}
427eea4f254SJeff Roberson 	}
428eea4f254SJeff Roberson }
429eea4f254SJeff Roberson 
430eea4f254SJeff Roberson static int
431eea4f254SJeff Roberson dump_lock_prof_stats(SYSCTL_HANDLER_ARGS)
432eea4f254SJeff Roberson {
433eea4f254SJeff Roberson 	struct sbuf *sb;
434eea4f254SJeff Roberson 	int error, cpu, t;
4350c66dc67SJeff Roberson 	int enabled;
436eea4f254SJeff Roberson 
43700f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
43800f0e671SMatthew D Fleming 	if (error != 0)
43900f0e671SMatthew D Fleming 		return (error);
4404e657159SMatthew D Fleming 	sb = sbuf_new_for_sysctl(NULL, NULL, LPROF_SBUF_SIZE, req);
441947265b6SKip Macy 	sbuf_printf(sb, "\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n",
442947265b6SKip Macy 	    "max", "wait_max", "total", "wait_total", "count", "avg", "wait_avg", "cnt_hold", "cnt_lock", "name");
4430c66dc67SJeff Roberson 	enabled = lock_prof_enable;
4440c66dc67SJeff Roberson 	lock_prof_enable = 0;
445*3ac2ac2eSMateusz Guzik 	/*
446*3ac2ac2eSMateusz Guzik 	 * See the comment in lock_prof_reset
447*3ac2ac2eSMateusz Guzik 	 */
448*3ac2ac2eSMateusz Guzik 	cpus_fence_seq_cst();
449*3ac2ac2eSMateusz Guzik 	quiesce_all_critical();
450eea4f254SJeff Roberson 	t = ticks;
451cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
452d2be3ef0SMateusz Guzik 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[0], sb, 0, t);
453d2be3ef0SMateusz Guzik 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[1], sb, 1, t);
454eea4f254SJeff Roberson 	}
455*3ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
4560c66dc67SJeff Roberson 	lock_prof_enable = enabled;
457eea4f254SJeff Roberson 
4584e657159SMatthew D Fleming 	error = sbuf_finish(sb);
4594e657159SMatthew D Fleming 	/* Output a trailing NUL. */
4604e657159SMatthew D Fleming 	if (error == 0)
4614e657159SMatthew D Fleming 		error = SYSCTL_OUT(req, "", 1);
462eea4f254SJeff Roberson 	sbuf_delete(sb);
463eea4f254SJeff Roberson 	return (error);
464eea4f254SJeff Roberson }
465eea4f254SJeff Roberson 
466eea4f254SJeff Roberson static int
467eea4f254SJeff Roberson enable_lock_prof(SYSCTL_HANDLER_ARGS)
468eea4f254SJeff Roberson {
469eea4f254SJeff Roberson 	int error, v;
470eea4f254SJeff Roberson 
471eea4f254SJeff Roberson 	v = lock_prof_enable;
472eea4f254SJeff Roberson 	error = sysctl_handle_int(oidp, &v, v, req);
473eea4f254SJeff Roberson 	if (error)
474eea4f254SJeff Roberson 		return (error);
475eea4f254SJeff Roberson 	if (req->newptr == NULL)
476eea4f254SJeff Roberson 		return (error);
477eea4f254SJeff Roberson 	if (v == lock_prof_enable)
478eea4f254SJeff Roberson 		return (0);
479eea4f254SJeff Roberson 	if (v == 1)
480eea4f254SJeff Roberson 		lock_prof_reset();
481eea4f254SJeff Roberson 	lock_prof_enable = !!v;
482eea4f254SJeff Roberson 
483eea4f254SJeff Roberson 	return (0);
484eea4f254SJeff Roberson }
485eea4f254SJeff Roberson 
486eea4f254SJeff Roberson static int
487eea4f254SJeff Roberson reset_lock_prof_stats(SYSCTL_HANDLER_ARGS)
488eea4f254SJeff Roberson {
489eea4f254SJeff Roberson 	int error, v;
490eea4f254SJeff Roberson 
491eea4f254SJeff Roberson 	v = 0;
492eea4f254SJeff Roberson 	error = sysctl_handle_int(oidp, &v, 0, req);
493eea4f254SJeff Roberson 	if (error)
494eea4f254SJeff Roberson 		return (error);
495eea4f254SJeff Roberson 	if (req->newptr == NULL)
496eea4f254SJeff Roberson 		return (error);
497eea4f254SJeff Roberson 	if (v == 0)
498eea4f254SJeff Roberson 		return (0);
499eea4f254SJeff Roberson 	lock_prof_reset();
500eea4f254SJeff Roberson 
501eea4f254SJeff Roberson 	return (0);
502eea4f254SJeff Roberson }
503eea4f254SJeff Roberson 
504eea4f254SJeff Roberson static struct lock_prof *
505eea4f254SJeff Roberson lock_profile_lookup(struct lock_object *lo, int spin, const char *file,
506eea4f254SJeff Roberson     int line)
507eea4f254SJeff Roberson {
508eea4f254SJeff Roberson 	const char *unknown = "(unknown)";
509eea4f254SJeff Roberson 	struct lock_prof_type *type;
510eea4f254SJeff Roberson 	struct lock_prof *lp;
511eea4f254SJeff Roberson 	struct lphead *head;
512eea4f254SJeff Roberson 	const char *p;
513eea4f254SJeff Roberson 	u_int hash;
514eea4f254SJeff Roberson 
515eea4f254SJeff Roberson 	p = file;
516eea4f254SJeff Roberson 	if (p == NULL || *p == '\0')
517eea4f254SJeff Roberson 		p = unknown;
518eea4f254SJeff Roberson 	hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line;
519eea4f254SJeff Roberson 	hash &= LPROF_HASH_MASK;
520d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
521eea4f254SJeff Roberson 	head = &type->lpt_hash[hash];
522eea4f254SJeff Roberson 	SLIST_FOREACH(lp, head, link) {
523eea4f254SJeff Roberson 		if (lp->line == line && lp->file == p &&
524eea4f254SJeff Roberson 		    lp->name == lo->lo_name)
525eea4f254SJeff Roberson 			return (lp);
526eea4f254SJeff Roberson 
527eea4f254SJeff Roberson 	}
528eea4f254SJeff Roberson 	lp = SLIST_FIRST(&type->lpt_lpalloc);
529eea4f254SJeff Roberson 	if (lp == NULL) {
530eea4f254SJeff Roberson 		lock_prof_rejected++;
531eea4f254SJeff Roberson 		return (lp);
532eea4f254SJeff Roberson 	}
533eea4f254SJeff Roberson 	SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link);
534eea4f254SJeff Roberson 	lp->file = p;
535eea4f254SJeff Roberson 	lp->line = line;
5360c66dc67SJeff Roberson 	lp->class = LOCK_CLASS(lo);
537eea4f254SJeff Roberson 	lp->name = lo->lo_name;
538eea4f254SJeff Roberson 	SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link);
539eea4f254SJeff Roberson 	return (lp);
540eea4f254SJeff Roberson }
541eea4f254SJeff Roberson 
542eea4f254SJeff Roberson static struct lock_profile_object *
543eea4f254SJeff Roberson lock_profile_object_lookup(struct lock_object *lo, int spin, const char *file,
544eea4f254SJeff Roberson     int line)
545eea4f254SJeff Roberson {
546eea4f254SJeff Roberson 	struct lock_profile_object *l;
547eea4f254SJeff Roberson 	struct lock_prof_type *type;
548eea4f254SJeff Roberson 	struct lpohead *head;
549eea4f254SJeff Roberson 
550eea4f254SJeff Roberson 	head = &curthread->td_lprof[spin];
551eea4f254SJeff Roberson 	LIST_FOREACH(l, head, lpo_link)
552eea4f254SJeff Roberson 		if (l->lpo_obj == lo && l->lpo_file == file &&
553eea4f254SJeff Roberson 		    l->lpo_line == line)
554eea4f254SJeff Roberson 			return (l);
555d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
556eea4f254SJeff Roberson 	l = LIST_FIRST(&type->lpt_lpoalloc);
557eea4f254SJeff Roberson 	if (l == NULL) {
558eea4f254SJeff Roberson 		lock_prof_rejected++;
559eea4f254SJeff Roberson 		return (NULL);
560eea4f254SJeff Roberson 	}
561eea4f254SJeff Roberson 	LIST_REMOVE(l, lpo_link);
562eea4f254SJeff Roberson 	l->lpo_obj = lo;
563eea4f254SJeff Roberson 	l->lpo_file = file;
564eea4f254SJeff Roberson 	l->lpo_line = line;
565eea4f254SJeff Roberson 	l->lpo_cnt = 0;
566eea4f254SJeff Roberson 	LIST_INSERT_HEAD(head, l, lpo_link);
567eea4f254SJeff Roberson 
568eea4f254SJeff Roberson 	return (l);
569eea4f254SJeff Roberson }
570eea4f254SJeff Roberson 
571eea4f254SJeff Roberson void
572eea4f254SJeff Roberson lock_profile_obtain_lock_success(struct lock_object *lo, int contested,
573eea4f254SJeff Roberson     uint64_t waittime, const char *file, int line)
574eea4f254SJeff Roberson {
575eea4f254SJeff Roberson 	static int lock_prof_count;
576eea4f254SJeff Roberson 	struct lock_profile_object *l;
577eea4f254SJeff Roberson 	int spin;
578eea4f254SJeff Roberson 
57935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
58035370593SAndriy Gapon 		return;
58135370593SAndriy Gapon 
582eea4f254SJeff Roberson 	/* don't reset the timer when/if recursing */
583eea4f254SJeff Roberson 	if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE))
584eea4f254SJeff Roberson 		return;
585eea4f254SJeff Roberson 	if (lock_prof_skipcount &&
586357911ceSKris Kennaway 	    (++lock_prof_count % lock_prof_skipcount) != 0)
587eea4f254SJeff Roberson 		return;
58813ddf72dSAttilio Rao 	spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
589eea4f254SJeff Roberson 	if (spin && lock_prof_skipspin == 1)
590eea4f254SJeff Roberson 		return;
5912e6b8de4SJeff Roberson 	critical_enter();
5922e6b8de4SJeff Roberson 	/* Recheck enabled now that we're in a critical section. */
5932e6b8de4SJeff Roberson 	if (lock_prof_enable == 0)
5942e6b8de4SJeff Roberson 		goto out;
595eea4f254SJeff Roberson 	l = lock_profile_object_lookup(lo, spin, file, line);
596eea4f254SJeff Roberson 	if (l == NULL)
5972e6b8de4SJeff Roberson 		goto out;
598eea4f254SJeff Roberson 	l->lpo_cnt++;
599eea4f254SJeff Roberson 	if (++l->lpo_ref > 1)
6002e6b8de4SJeff Roberson 		goto out;
601eea4f254SJeff Roberson 	l->lpo_contest_locking = contested;
6027c0435b9SKip Macy 	l->lpo_acqtime = nanoseconds();
603aa077979SKip Macy 	if (waittime && (l->lpo_acqtime > waittime))
6047c0435b9SKip Macy 		l->lpo_waittime = l->lpo_acqtime - waittime;
605aa077979SKip Macy 	else
606aa077979SKip Macy 		l->lpo_waittime = 0;
6072e6b8de4SJeff Roberson out:
608*3ac2ac2eSMateusz Guzik 	/*
609*3ac2ac2eSMateusz Guzik 	 * Paired with cpus_fence_seq_cst().
610*3ac2ac2eSMateusz Guzik 	 */
611*3ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
6122e6b8de4SJeff Roberson 	critical_exit();
6132e6b8de4SJeff Roberson }
6142e6b8de4SJeff Roberson 
6152e6b8de4SJeff Roberson void
6162e6b8de4SJeff Roberson lock_profile_thread_exit(struct thread *td)
6172e6b8de4SJeff Roberson {
6182e6b8de4SJeff Roberson #ifdef INVARIANTS
6192e6b8de4SJeff Roberson 	struct lock_profile_object *l;
6202e6b8de4SJeff Roberson 
6212e6b8de4SJeff Roberson 	MPASS(curthread->td_critnest == 0);
6222e6b8de4SJeff Roberson #endif
6232e6b8de4SJeff Roberson 	/*
6242e6b8de4SJeff Roberson 	 * If lock profiling was disabled we have to wait for reset to
6252e6b8de4SJeff Roberson 	 * clear our pointers before we can exit safely.
6262e6b8de4SJeff Roberson 	 */
6272e6b8de4SJeff Roberson 	lock_prof_reset_wait();
6282e6b8de4SJeff Roberson #ifdef INVARIANTS
6292e6b8de4SJeff Roberson 	LIST_FOREACH(l, &td->td_lprof[0], lpo_link)
6302e6b8de4SJeff Roberson 		printf("thread still holds lock acquired at %s:%d\n",
6312e6b8de4SJeff Roberson 		    l->lpo_file, l->lpo_line);
6322e6b8de4SJeff Roberson 	LIST_FOREACH(l, &td->td_lprof[1], lpo_link)
6332e6b8de4SJeff Roberson 		printf("thread still holds lock acquired at %s:%d\n",
6342e6b8de4SJeff Roberson 		    l->lpo_file, l->lpo_line);
6352e6b8de4SJeff Roberson #endif
6362e6b8de4SJeff Roberson 	MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL);
6372e6b8de4SJeff Roberson 	MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL);
6387c0435b9SKip Macy }
6397c0435b9SKip Macy 
640eea4f254SJeff Roberson void
641eea4f254SJeff Roberson lock_profile_release_lock(struct lock_object *lo)
6427c0435b9SKip Macy {
643eea4f254SJeff Roberson 	struct lock_profile_object *l;
644eea4f254SJeff Roberson 	struct lock_prof_type *type;
645eea4f254SJeff Roberson 	struct lock_prof *lp;
64660ae52f7SEd Schouten 	uint64_t curtime, holdtime;
647eea4f254SJeff Roberson 	struct lpohead *head;
648eea4f254SJeff Roberson 	int spin;
6497c0435b9SKip Macy 
65035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
65135370593SAndriy Gapon 		return;
6522e6b8de4SJeff Roberson 	if (lo->lo_flags & LO_NOPROFILE)
6537c0435b9SKip Macy 		return;
65413ddf72dSAttilio Rao 	spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
655eea4f254SJeff Roberson 	head = &curthread->td_lprof[spin];
6562e6b8de4SJeff Roberson 	if (LIST_FIRST(head) == NULL)
6572e6b8de4SJeff Roberson 		return;
658eea4f254SJeff Roberson 	critical_enter();
6592e6b8de4SJeff Roberson 	/* Recheck enabled now that we're in a critical section. */
6602e6b8de4SJeff Roberson 	if (lock_prof_enable == 0 && lock_prof_resetting == 1)
6612e6b8de4SJeff Roberson 		goto out;
6622e6b8de4SJeff Roberson 	/*
6632e6b8de4SJeff Roberson 	 * If lock profiling is not enabled we still want to remove the
6642e6b8de4SJeff Roberson 	 * lpo from our queue.
6652e6b8de4SJeff Roberson 	 */
666eea4f254SJeff Roberson 	LIST_FOREACH(l, head, lpo_link)
667eea4f254SJeff Roberson 		if (l->lpo_obj == lo)
6687c0435b9SKip Macy 			break;
669eea4f254SJeff Roberson 	if (l == NULL)
670eea4f254SJeff Roberson 		goto out;
671eea4f254SJeff Roberson 	if (--l->lpo_ref > 0)
672eea4f254SJeff Roberson 		goto out;
673eea4f254SJeff Roberson 	lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line);
674eea4f254SJeff Roberson 	if (lp == NULL)
675eea4f254SJeff Roberson 		goto release;
676e7154e7eSAndriy Gapon 	curtime = nanoseconds();
677e7154e7eSAndriy Gapon 	if (curtime < l->lpo_acqtime)
678eea4f254SJeff Roberson 		goto release;
679e7154e7eSAndriy Gapon 	holdtime = curtime - l->lpo_acqtime;
680e7154e7eSAndriy Gapon 
6817c0435b9SKip Macy 	/*
68283b72e3eSKip Macy 	 * Record if the lock has been held longer now than ever
6837c0435b9SKip Macy 	 * before.
6847c0435b9SKip Macy 	 */
685eea4f254SJeff Roberson 	if (holdtime > lp->cnt_max)
686eea4f254SJeff Roberson 		lp->cnt_max = holdtime;
687947265b6SKip Macy 	if (l->lpo_waittime > lp->cnt_wait_max)
688947265b6SKip Macy 		lp->cnt_wait_max = l->lpo_waittime;
689eea4f254SJeff Roberson 	lp->cnt_tot += holdtime;
690eea4f254SJeff Roberson 	lp->cnt_wait += l->lpo_waittime;
691eea4f254SJeff Roberson 	lp->cnt_contest_locking += l->lpo_contest_locking;
692eea4f254SJeff Roberson 	lp->cnt_cur += l->lpo_cnt;
693eea4f254SJeff Roberson release:
694eea4f254SJeff Roberson 	LIST_REMOVE(l, lpo_link);
695d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
696eea4f254SJeff Roberson 	LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link);
697eea4f254SJeff Roberson out:
698*3ac2ac2eSMateusz Guzik 	/*
699*3ac2ac2eSMateusz Guzik 	 * Paired with cpus_fence_seq_cst().
700*3ac2ac2eSMateusz Guzik 	 */
701*3ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
702eea4f254SJeff Roberson 	critical_exit();
703eea4f254SJeff Roberson }
7047c0435b9SKip Macy 
7056472ac3dSEd Schouten static SYSCTL_NODE(_debug_lock, OID_AUTO, prof, CTLFLAG_RD, NULL,
7066472ac3dSEd Schouten     "lock profiling");
707eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW,
708eea4f254SJeff Roberson     &lock_prof_skipspin, 0, "Skip profiling on spinlocks.");
709eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipcount, CTLFLAG_RW,
710eea4f254SJeff Roberson     &lock_prof_skipcount, 0, "Sample approximately every N lock acquisitions.");
711eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD,
712eea4f254SJeff Roberson     &lock_prof_rejected, 0, "Number of rejected profiling records");
713eea4f254SJeff Roberson SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
714eea4f254SJeff Roberson     NULL, 0, dump_lock_prof_stats, "A", "Lock profiling statistics");
715eea4f254SJeff Roberson SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
716eea4f254SJeff Roberson     NULL, 0, reset_lock_prof_stats, "I", "Reset lock profiling statistics");
717eea4f254SJeff Roberson SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW,
718eea4f254SJeff Roberson     NULL, 0, enable_lock_prof, "I", "Enable lock profiling");
719eea4f254SJeff Roberson 
7207c0435b9SKip Macy #endif
721