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