Module Name:    src
Committed By:   riastradh
Date:           Tue Aug 16 00:26:40 UTC 2022

Modified Files:
        src/sys/kern: kern_lock.c

Log Message:
KERNEL_LOCK(9): Send an IPI to print holder's stack trace on spinout.


To generate a diff of this commit:
cvs rdiff -u -r1.173 -r1.174 src/sys/kern/kern_lock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_lock.c
diff -u src/sys/kern/kern_lock.c:1.173 src/sys/kern/kern_lock.c:1.174
--- src/sys/kern/kern_lock.c:1.173	Sun Oct 31 16:26:26 2021
+++ src/sys/kern/kern_lock.c	Tue Aug 16 00:26:39 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.173 2021/10/31 16:26:26 skrll Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.174 2022/08/16 00:26:39 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.173 2021/10/31 16:26:26 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.174 2022/08/16 00:26:39 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_lockdebug.h"
@@ -134,6 +134,22 @@ lockops_t _kernel_lock_ops = {
 	.lo_dump = _kernel_lock_dump,
 };
 
+#ifdef LOCKDEBUG
+
+#include <ddb/ddb.h>
+
+static void
+kernel_lock_trace_ipi(void *cookie)
+{
+
+	printf("%s[%d %s]: hogging kernel lock\n", cpu_name(curcpu()),
+	    curlwp->l_lid,
+	    curlwp->l_name ? curlwp->l_name : curproc->p_comm);
+	db_stacktrace();
+}
+
+#endif
+
 /*
  * Initialize the kernel lock.
  */
@@ -177,6 +193,7 @@ _kernel_lock(int nlocks)
 	LOCKSTAT_FLAG(lsflag);
 	struct lwp *owant;
 #ifdef LOCKDEBUG
+	static struct cpu_info *kernel_lock_holder;
 	u_int spins = 0;
 #endif
 	int s;
@@ -235,6 +252,11 @@ _kernel_lock(int nlocks)
 #ifdef LOCKDEBUG
 			if (SPINLOCK_SPINOUT(spins)) {
 				extern int start_init_exec;
+				ipi_msg_t msg = {
+					.func = kernel_lock_trace_ipi,
+				};
+				ipi_unicast(&msg, kernel_lock_holder);
+				ipi_wait(&msg);
 				if (start_init_exec)
 					_KERNEL_LOCK_ABORT("spinout");
 			}
@@ -278,6 +300,10 @@ _kernel_lock(int nlocks)
 #ifndef __HAVE_ATOMIC_AS_MEMBAR
 	membar_enter();
 #endif
+
+#ifdef LOCKDEBUG
+	kernel_lock_holder = curcpu();
+#endif
 }
 
 /*

Reply via email to