Module Name:    src
Committed By:   ad
Date:           Wed Jan 22 13:19:33 UTC 2020

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

Log Message:
- DIAGNOSTIC: check for leaked kernel_lock in mi_switch().

- Now that ci_biglock_wanted is set later, explicitly disable preemption
  while acquiring kernel_lock.  It was blocked in a roundabout way
  previously.

Reported-by: syzbot+43111d810160fb4b9...@syzkaller.appspotmail.com
Reported-by: syzbot+f5b871bd00089bf97...@syzkaller.appspotmail.com
Reported-by: syzbot+cd1f15eee5b1b6d20...@syzkaller.appspotmail.com
Reported-by: syzbot+fb945a331dabd0b6b...@syzkaller.appspotmail.com
Reported-by: syzbot+53a0c2342b361db25...@syzkaller.appspotmail.com
Reported-by: syzbot+552222a952814dede...@syzkaller.appspotmail.com
Reported-by: syzbot+c7104a72172b0f909...@syzkaller.appspotmail.com
Reported-by: syzbot+efbd30c6ca0f7d844...@syzkaller.appspotmail.com
Reported-by: syzbot+330a421bd46794d8b...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.165 -r1.166 src/sys/kern/kern_lock.c
cvs rdiff -u -r1.336 -r1.337 src/sys/kern/kern_synch.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.165 src/sys/kern/kern_lock.c:1.166
--- src/sys/kern/kern_lock.c:1.165	Fri Jan 17 20:26:22 2020
+++ src/sys/kern/kern_lock.c	Wed Jan 22 13:19:33 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.165 2020/01/17 20:26:22 ad Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.166 2020/01/22 13:19:33 ad 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.165 2020/01/17 20:26:22 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.166 2020/01/22 13:19:33 ad Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -212,9 +212,10 @@ _kernel_lock(int nlocks)
 	owant = ci->ci_biglock_wanted;
 
 	/*
-	 * Spin until we acquire the lock.  Once we have it, record the
-	 * time spent with lockstat.
+	 * Stay pinned to the CPU and spin until we acquire the lock.  Once
+	 * we have it, record the time spent with lockstat.
 	 */
+	l->l_nopreempt++;
 	LOCKSTAT_ENTER(lsflag);
 	LOCKSTAT_START_TIMER(lsflag, spintime);
 
@@ -238,6 +239,11 @@ _kernel_lock(int nlocks)
 		s = splvm();
 	} while (!__cpu_simple_lock_try(kernel_lock));
 
+	/*
+	 * Got it; not re-enable preemption, although we now can't do a
+	 * preemption as kernel_lock is held!
+	 */
+	l->l_nopreempt--;
 	ci->ci_biglock_count = nlocks;
 	l->l_blcnt = nlocks;
 	splx(s);

Index: src/sys/kern/kern_synch.c
diff -u src/sys/kern/kern_synch.c:1.336 src/sys/kern/kern_synch.c:1.337
--- src/sys/kern/kern_synch.c:1.336	Thu Jan  9 16:35:03 2020
+++ src/sys/kern/kern_synch.c	Wed Jan 22 13:19:33 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_synch.c,v 1.336 2020/01/09 16:35:03 ad Exp $	*/
+/*	$NetBSD: kern_synch.c,v 1.337 2020/01/22 13:19:33 ad Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2004, 2006, 2007, 2008, 2009, 2019
@@ -69,7 +69,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.336 2020/01/09 16:35:03 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.337 2020/01/22 13:19:33 ad Exp $");
 
 #include "opt_kstack.h"
 #include "opt_dtrace.h"
@@ -353,8 +353,7 @@ kpreempt(uintptr_t where)
 			break;
 		}
 		s = splsched();
-		if (__predict_false(l->l_blcnt != 0 ||
-		    curcpu()->ci_biglock_wanted != NULL)) {
+		if (__predict_false(l->l_blcnt != 0)) {
 			/* Hold or want kernel_lock, code is not MT safe. */
 			splx(s);
 			if ((dop & DOPREEMPT_COUNTED) == 0) {
@@ -531,6 +530,7 @@ mi_switch(lwp_t *l)
 	KASSERT(lwp_locked(l, NULL));
 	KASSERT(kpreempt_disabled());
 	KASSERT(mutex_owned(curcpu()->ci_schedstate.spc_mutex));
+	KASSERTMSG(l->l_blcnt == 0, "kernel_lock leaked");
 
 	kstack_check_magic(l);
 

Reply via email to