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