CVS commit: src/sys/kern

2022-09-24 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Sep 24 16:29:27 UTC 2022

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

Log Message:
Propagate the open flags to the master pty (Anthony Mallet)


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/sys/kern/tty_ptm.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/tty_ptm.c
diff -u src/sys/kern/tty_ptm.c:1.43 src/sys/kern/tty_ptm.c:1.44
--- src/sys/kern/tty_ptm.c:1.43	Tue Jun 29 18:40:53 2021
+++ src/sys/kern/tty_ptm.c	Sat Sep 24 12:29:27 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: tty_ptm.c,v 1.43 2021/06/29 22:40:53 dholland Exp $	*/
+/*	$NetBSD: tty_ptm.c,v 1.44 2022/09/24 16:29:27 christos Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.43 2021/06/29 22:40:53 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.44 2022/09/24 16:29:27 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -87,7 +87,7 @@ const struct cdevsw ptm_cdevsw = {
 int pts_major, ptc_major;
 
 static dev_t pty_getfree(void);
-static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *);
+static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *, int);
 static int pty_alloc_slave(struct lwp *, int *, dev_t, struct mount *);
 static int pty_vn_open(struct vnode *, struct lwp *);
 
@@ -155,7 +155,8 @@ pty_vn_open(struct vnode *vp, struct lwp
 }
 
 static int
-pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp)
+pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp,
+int flags)
 {
 	int error;
 	struct file *fp;
@@ -199,7 +200,7 @@ retry:
 		else
 			goto bad;
 	}
-	fp->f_flag = FREAD|FWRITE;
+	fp->f_flag = FREAD|FWRITE|(flags);
 	fp->f_type = DTYPE_VNODE;
 	fp->f_ops = 
 	fp->f_vnode = vp;
@@ -321,9 +322,9 @@ ptmattach(int n)
 	extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
 	/* find the major and minor of the pty devices */
 	if ((pts_major = cdevsw_lookup_major(_cdevsw)) == -1)
-		panic("ptmattach: Can't find pty slave in cdevsw");
+		panic("%s: Can't find pty slave in cdevsw", __func__);
 	if ((ptc_major = cdevsw_lookup_major(_cdevsw)) == -1)
-		panic("ptmattach: Can't find pty master in cdevsw");
+		panic("%s: Can't find pty master in cdevsw", __func__);
 #ifdef COMPAT_BSDPTY
 	ptm = _bsdpty;
 #endif
@@ -343,7 +344,7 @@ ptmopen(dev_t dev, int flag, int mode, s
 	case 2:		/* /emul/linux/dev/ptmx */
 		if ((error = pty_getmp(l, )) != 0)
 			return error;
-		if ((error = pty_alloc_master(l, , , mp)) != 0)
+		if ((error = pty_alloc_master(l, , , mp, flag)) != 0)
 			return error;
 		if (minor(dev) == 2) {
 			/*
@@ -392,7 +393,7 @@ ptmioctl(dev_t dev, u_long cmd, void *da
 		if ((error = pty_getmp(l, )) != 0)
 			return error;
 
-		if ((error = pty_alloc_master(l, , , mp)) != 0)
+		if ((error = pty_alloc_master(l, , , mp, 0)) != 0)
 			return error;
 
 		if ((error = pty_grant_slave(l, newdev, mp)) != 0)



CVS commit: src/sys/kern

2022-09-24 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Sep 24 16:29:27 UTC 2022

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

Log Message:
Propagate the open flags to the master pty (Anthony Mallet)


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/sys/kern/tty_ptm.c

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



CVS commit: src/sys/kern

2022-09-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Sep 22 14:39:24 UTC 2022

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

Log Message:
crashme(9): Clarify description of kernel_lock_spinout.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/kern/kern_crashme.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_crashme.c
diff -u src/sys/kern/kern_crashme.c:1.9 src/sys/kern/kern_crashme.c:1.10
--- src/sys/kern/kern_crashme.c:1.9	Wed Sep 21 10:50:29 2022
+++ src/sys/kern/kern_crashme.c	Thu Sep 22 14:39:24 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_crashme.c,v 1.9 2022/09/21 10:50:29 riastradh Exp $	*/
+/*	$NetBSD: kern_crashme.c,v 1.10 2022/09/22 14:39:24 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2018, 2019 Matthew R. Green
@@ -83,7 +83,7 @@ static crashme_node nodes[] = {
 CMNODE("ddb", "enter ddb directly", crashme_ddb),
 #endif
 #ifdef LOCKDEBUG
-CMNODE("kernel_lock_spinout", "infinite kernel lock",
+CMNODE("kernel_lock_spinout", "infinite loop under kernel lock",
 	crashme_kernel_lock_spinout),
 #endif
 CMNODE("mutex_recursion", "enter the same mutex twice",



CVS commit: src/sys/kern

2022-09-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Sep 22 14:39:24 UTC 2022

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

Log Message:
crashme(9): Clarify description of kernel_lock_spinout.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/kern/kern_crashme.c

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



CVS commit: src/sys/kern

2022-09-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Sep 21 10:50:29 UTC 2022

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

Log Message:
crashme(9): Fix crashme_add return value to match comment.

XXX Why do this and crashme_remove return -1 instead of an error code
like essentially everything else in the kernel?


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/kern/kern_crashme.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_crashme.c
diff -u src/sys/kern/kern_crashme.c:1.8 src/sys/kern/kern_crashme.c:1.9
--- src/sys/kern/kern_crashme.c:1.8	Wed Sep 21 10:50:11 2022
+++ src/sys/kern/kern_crashme.c	Wed Sep 21 10:50:29 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_crashme.c,v 1.8 2022/09/21 10:50:11 riastradh Exp $	*/
+/*	$NetBSD: kern_crashme.c,v 1.9 2022/09/21 10:50:29 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2018, 2019 Matthew R. Green
@@ -134,7 +134,7 @@ crashme_add(crashme_node *ncn)
 	}
 	mutex_exit(_lock);
 
-	return rv;
+	return rv == 0 ? 0 : -1;
 }
 
 /*



CVS commit: src/sys/kern

2022-09-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Sep 21 10:50:29 UTC 2022

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

Log Message:
crashme(9): Fix crashme_add return value to match comment.

XXX Why do this and crashme_remove return -1 instead of an error code
like essentially everything else in the kernel?


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/kern/kern_crashme.c

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



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:40:38 UTC 2022

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

Log Message:
autoconf(9): Improve diagnostics for config_detach_enter/commit/exit.


To generate a diff of this commit:
cvs rdiff -u -r1.304 -r1.305 src/sys/kern/subr_autoconf.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/subr_autoconf.c
diff -u src/sys/kern/subr_autoconf.c:1.304 src/sys/kern/subr_autoconf.c:1.305
--- src/sys/kern/subr_autoconf.c:1.304	Wed Aug 24 11:19:25 2022
+++ src/sys/kern/subr_autoconf.c	Tue Sep 13 09:40:38 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.304 2022/08/24 11:19:25 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.305 2022/09/13 09:40:38 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.304 2022/08/24 11:19:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.305 2022/09/13 09:40:38 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1949,11 +1949,13 @@ config_detach_enter(device_t dev)
 	 * all new attempts to acquire references to block.
 	 */
 	KASSERTMSG((l = dev->dv_attaching) == NULL,
-	"lwp %ld [%s] @ %p attaching",
-	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
+	"lwp %ld [%s] @ %p attaching %s",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
+	device_xname(dev));
 	KASSERTMSG((l = dev->dv_detaching) == NULL,
-	"lwp %ld [%s] @ %p detaching",
-	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
+	"lwp %ld [%s] @ %p detaching %s",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
+	device_xname(dev));
 	dev->dv_detaching = curlwp;
 
 out:	mutex_exit(_misc_lock);
@@ -1966,9 +1968,12 @@ config_detach_exit(device_t dev)
 	struct lwp *l __diagused;
 
 	mutex_enter(_misc_lock);
+	KASSERTMSG(dev->dv_detaching != NULL, "not detaching %s",
+	device_xname(dev));
 	KASSERTMSG((l = dev->dv_detaching) == curlwp,
-	"lwp %ld [%s] @ %p detaching",
-	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
+	"lwp %ld [%s] @ %p detaching %s",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
+	device_xname(dev));
 	dev->dv_detaching = NULL;
 	cv_broadcast(_misc_cv);
 	mutex_exit(_misc_lock);
@@ -2182,9 +2187,12 @@ config_detach_commit(device_t dev)
 	struct lwp *l __diagused;
 
 	mutex_enter(_misc_lock);
+	KASSERTMSG(dev->dv_detaching != NULL, "not detaching %s",
+	device_xname(dev));
 	KASSERTMSG((l = dev->dv_detaching) == curlwp,
-	"lwp %ld [%s] @ %p detaching",
-	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
+	"lwp %ld [%s] @ %p detaching %s",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
+	device_xname(dev));
 	dev->dv_detached = true;
 	cv_broadcast(_misc_cv);
 	mutex_exit(_misc_lock);



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:40:38 UTC 2022

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

Log Message:
autoconf(9): Improve diagnostics for config_detach_enter/commit/exit.


To generate a diff of this commit:
cvs rdiff -u -r1.304 -r1.305 src/sys/kern/subr_autoconf.c

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



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:37:49 UTC 2022

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

Log Message:
kthread(9): Attribute uarea to caller for kmsan.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/kern/kern_kthread.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_kthread.c
diff -u src/sys/kern/kern_kthread.c:1.46 src/sys/kern/kern_kthread.c:1.47
--- src/sys/kern/kern_kthread.c:1.46	Sat Aug  1 02:04:55 2020
+++ src/sys/kern/kern_kthread.c	Tue Sep 13 09:37:49 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_kthread.c,v 1.46 2020/08/01 02:04:55 riastradh Exp $	*/
+/*	$NetBSD: kern_kthread.c,v 1.47 2022/09/13 09:37:49 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1998, 1999, 2007, 2009, 2019 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.46 2020/08/01 02:04:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.47 2022/09/13 09:37:49 riastradh Exp $");
 
 #include 
 #include 
@@ -41,6 +41,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_kthread
 #include 
 #include 
 #include 
+#include 
 
 #include 
 
@@ -76,6 +77,7 @@ kthread_create(pri_t pri, int flag, stru
 	if (uaddr == 0) {
 		return ENOMEM;
 	}
+	kmsan_orig((void *)uaddr, USPACE, KMSAN_TYPE_POOL, __RET_ADDR);
 	if ((flag & KTHREAD_TS) != 0) {
 		lc = SCHED_OTHER;
 	} else {



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:37:49 UTC 2022

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

Log Message:
kthread(9): Attribute uarea to caller for kmsan.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/kern/kern_kthread.c

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



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:35:32 UTC 2022

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

Log Message:
vflush(9): Insert `involuntary' preemption point at each vnode.

Currently there is a voluntary yield every 100ms, but that's a long
time.  Should help to avoid hogging the CPU while flushing lots of
data to big disks on systems without kpreemption.


To generate a diff of this commit:
cvs rdiff -u -r1.96 -r1.97 src/sys/kern/vfs_mount.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/vfs_mount.c
diff -u src/sys/kern/vfs_mount.c:1.96 src/sys/kern/vfs_mount.c:1.97
--- src/sys/kern/vfs_mount.c:1.96	Fri Aug 26 11:03:53 2022
+++ src/sys/kern/vfs_mount.c	Tue Sep 13 09:35:31 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_mount.c,v 1.96 2022/08/26 11:03:53 hannken Exp $	*/
+/*	$NetBSD: vfs_mount.c,v 1.97 2022/09/13 09:35:31 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1997-2020 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.96 2022/08/26 11:03:53 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.97 2022/09/13 09:35:31 riastradh Exp $");
 
 #include 
 #include 
@@ -549,6 +549,7 @@ vflushnext(struct vnode_iterator *marker
 		yield();
 		*when = getticks() + hz / 10;
 	}
+	preempt_point();
 	return vfs_vnode_iterator_next1(marker, NULL, NULL, true);
 }
 



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:35:32 UTC 2022

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

Log Message:
vflush(9): Insert `involuntary' preemption point at each vnode.

Currently there is a voluntary yield every 100ms, but that's a long
time.  Should help to avoid hogging the CPU while flushing lots of
data to big disks on systems without kpreemption.


To generate a diff of this commit:
cvs rdiff -u -r1.96 -r1.97 src/sys/kern/vfs_mount.c

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



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:28:06 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Avoid spinning out until 10sec have passed.

This means we'll never spin out if the hardclock timer is stuck.  But
the hardclock timer never runs with the kernel lock held itself, so
it's not immediately clear that's important.


To generate a diff of this commit:
cvs rdiff -u -r1.179 -r1.180 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.179 src/sys/kern/kern_lock.c:1.180
--- src/sys/kern/kern_lock.c:1.179	Tue Sep 13 09:14:26 2022
+++ src/sys/kern/kern_lock.c	Tue Sep 13 09:28:05 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.179 2022/09/13 09:14:26 riastradh Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.180 2022/09/13 09:28:05 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.179 2022/09/13 09:14:26 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.180 2022/09/13 09:28:05 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_lockdebug.h"
@@ -195,6 +195,7 @@ _kernel_lock(int nlocks)
 #ifdef LOCKDEBUG
 	static struct cpu_info *kernel_lock_holder;
 	u_int spins = 0;
+	u_int starttime = getticks();
 #endif
 	int s;
 	struct lwp *l = curlwp;
@@ -254,7 +255,8 @@ _kernel_lock(int nlocks)
 		while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
 #ifdef LOCKDEBUG
 			extern int start_init_exec;
-			if (SPINLOCK_SPINOUT(spins) && start_init_exec) {
+			if (SPINLOCK_SPINOUT(spins) && start_init_exec &&
+			(getticks() - starttime) > 10*hz) {
 ipi_msg_t msg = {
 	.func = kernel_lock_trace_ipi,
 };



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:28:06 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Avoid spinning out until 10sec have passed.

This means we'll never spin out if the hardclock timer is stuck.  But
the hardclock timer never runs with the kernel lock held itself, so
it's not immediately clear that's important.


To generate a diff of this commit:
cvs rdiff -u -r1.179 -r1.180 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.



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:14:26 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Restore backoff while spinning in !LOCKDEBUG case.

When the spinout logic was put under LOCKDEBUG among a series of
other changes that got reverted, the backoff was inadvertently made
LOCKDEBUG-only too.


To generate a diff of this commit:
cvs rdiff -u -r1.178 -r1.179 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.



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:14:26 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Restore backoff while spinning in !LOCKDEBUG case.

When the spinout logic was put under LOCKDEBUG among a series of
other changes that got reverted, the backoff was inadvertently made
LOCKDEBUG-only too.


To generate a diff of this commit:
cvs rdiff -u -r1.178 -r1.179 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.178 src/sys/kern/kern_lock.c:1.179
--- src/sys/kern/kern_lock.c:1.178	Sat Aug 20 23:37:12 2022
+++ src/sys/kern/kern_lock.c	Tue Sep 13 09:14:26 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.178 2022/08/20 23:37:12 riastradh Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.179 2022/09/13 09:14:26 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.178 2022/08/20 23:37:12 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.179 2022/09/13 09:14:26 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_lockdebug.h"
@@ -264,9 +264,9 @@ _kernel_lock(int nlocks)
 kpreempt_enable();
 _KERNEL_LOCK_ABORT("spinout");
 			}
+#endif
 			SPINLOCK_BACKOFF_HOOK;
 			SPINLOCK_SPIN_HOOK;
-#endif
 		}
 		s = splvm();
 	} while (!__cpu_simple_lock_try(kernel_lock));



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:13:20 UTC 2022

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

Log Message:
vfs(9): For MP-safe mounts, don't kernel lock in mount/unmount.


To generate a diff of this commit:
cvs rdiff -u -r1.494 -r1.495 src/sys/kern/vfs_subr.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/vfs_subr.c
diff -u src/sys/kern/vfs_subr.c:1.494 src/sys/kern/vfs_subr.c:1.495
--- src/sys/kern/vfs_subr.c:1.494	Tue Sep 13 08:48:20 2022
+++ src/sys/kern/vfs_subr.c	Tue Sep 13 09:13:19 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_subr.c,v 1.494 2022/09/13 08:48:20 riastradh Exp $	*/
+/*	$NetBSD: vfs_subr.c,v 1.495 2022/09/13 09:13:19 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2004, 2005, 2007, 2008, 2019, 2020
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.494 2022/09/13 08:48:20 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.495 2022/09/13 09:13:19 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1385,11 +1385,24 @@ vtype2dt(enum vtype vt)
 int
 VFS_MOUNT(struct mount *mp, const char *a, void *b, size_t *c)
 {
+	int mpsafe = mp->mnt_iflag & IMNT_MPSAFE;
 	int error;
 
-	KERNEL_LOCK(1, NULL);
+	/*
+	 * Note: The first time through, the vfs_mount function may set
+	 * IMNT_MPSAFE, so we have to cache it on entry in order to
+	 * avoid leaking a kernel lock.
+	 *
+	 * XXX Maybe the MPSAFE bit should be set in struct vfsops and
+	 * not in struct mount.
+	 */
+	if (mpsafe) {
+		KERNEL_LOCK(1, NULL);
+	}
 	error = (*(mp->mnt_op->vfs_mount))(mp, a, b, c);
-	KERNEL_UNLOCK_ONE(NULL);
+	if (mpsafe) {
+		KERNEL_UNLOCK_ONE(NULL);
+	}
 
 	return error;
 }
@@ -1415,9 +1428,13 @@ VFS_UNMOUNT(struct mount *mp, int a)
 {
 	int error;
 
-	KERNEL_LOCK(1, NULL);
+	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
+		KERNEL_LOCK(1, NULL);
+	}
 	error = (*(mp->mnt_op->vfs_unmount))(mp, a);
-	KERNEL_UNLOCK_ONE(NULL);
+	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
+		KERNEL_UNLOCK_ONE(NULL);
+	}
 
 	return error;
 }



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 09:13:20 UTC 2022

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

Log Message:
vfs(9): For MP-safe mounts, don't kernel lock in mount/unmount.


To generate a diff of this commit:
cvs rdiff -u -r1.494 -r1.495 src/sys/kern/vfs_subr.c

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



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 08:48:21 UTC 2022

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

Log Message:
vfs_subr.c: Nix trailing whitespace.


To generate a diff of this commit:
cvs rdiff -u -r1.493 -r1.494 src/sys/kern/vfs_subr.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/vfs_subr.c
diff -u src/sys/kern/vfs_subr.c:1.493 src/sys/kern/vfs_subr.c:1.494
--- src/sys/kern/vfs_subr.c:1.493	Mon Mar 28 12:38:33 2022
+++ src/sys/kern/vfs_subr.c	Tue Sep 13 08:48:20 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_subr.c,v 1.493 2022/03/28 12:38:33 riastradh Exp $	*/
+/*	$NetBSD: vfs_subr.c,v 1.494 2022/09/13 08:48:20 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2004, 2005, 2007, 2008, 2019, 2020
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.493 2022/03/28 12:38:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.494 2022/09/13 08:48:20 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -827,11 +827,11 @@ sched_sync(void *arg)
  *
  * If we locked it yet arrive here, it's
  * likely that lazy sync is in progress and
- * so the vnode still has dirty metadata. 
+ * so the vnode still has dirty metadata.
  * syncdelay is mainly to get this vnode out
  * of the way so we do not consider it again
  * "soon" in this loop, so the delay time is
- * not critical as long as it is not "soon". 
+ * not critical as long as it is not "soon".
  * While write-back strategy is the file
  * system's domain, we expect write-back to
  * occur no later than syncdelay seconds
@@ -1393,7 +1393,7 @@ VFS_MOUNT(struct mount *mp, const char *
 
 	return error;
 }
-	
+
 int
 VFS_START(struct mount *mp, int a)
 {
@@ -1409,7 +1409,7 @@ VFS_START(struct mount *mp, int a)
 
 	return error;
 }
-	
+
 int
 VFS_UNMOUNT(struct mount *mp, int a)
 {



CVS commit: src/sys/kern

2022-09-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Sep 13 08:48:21 UTC 2022

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

Log Message:
vfs_subr.c: Nix trailing whitespace.


To generate a diff of this commit:
cvs rdiff -u -r1.493 -r1.494 src/sys/kern/vfs_subr.c

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



CVS commit: src/sys/kern

2022-09-10 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Sat Sep 10 07:30:41 UTC 2022

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

Log Message:
avoid a GCC warning (happens on -current, -9, and -8.)


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/sys/kern/kern_core.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_core.c
diff -u src/sys/kern/kern_core.c:1.36 src/sys/kern/kern_core.c:1.37
--- src/sys/kern/kern_core.c:1.36	Fri Sep  9 14:30:17 2022
+++ src/sys/kern/kern_core.c	Sat Sep 10 07:30:41 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_core.c,v 1.36 2022/09/09 14:30:17 christos Exp $	*/
+/*	$NetBSD: kern_core.c,v 1.37 2022/09/10 07:30:41 mrg Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.36 2022/09/09 14:30:17 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.37 2022/09/10 07:30:41 mrg Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_execfmt.h"
@@ -127,7 +127,7 @@ coredump(struct lwp *l, const char *patt
 	struct coredump_iostate	io;
 	struct plimit		*lim;
 	int			error, error1;
-	char			*name, *lastslash;
+	char			*name, *lastslash = NULL /* XXXgcc */;
 
 	name = PNBUF_GET();
 



CVS commit: src/sys/kern

2022-09-10 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Sat Sep 10 07:30:41 UTC 2022

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

Log Message:
avoid a GCC warning (happens on -current, -9, and -8.)


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/sys/kern/kern_core.c

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



CVS commit: src/sys/kern

2022-09-09 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Sep  9 14:30:17 UTC 2022

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

Log Message:
Don't forget to free the cred we just held.
Thanks to Chris J-D (chris at accessvector dot net)
While here, de-duplicate the mutex exit sequence.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/kern/kern_core.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_core.c
diff -u src/sys/kern/kern_core.c:1.35 src/sys/kern/kern_core.c:1.36
--- src/sys/kern/kern_core.c:1.35	Tue Jun 29 18:40:53 2021
+++ src/sys/kern/kern_core.c	Fri Sep  9 10:30:17 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_core.c,v 1.35 2021/06/29 22:40:53 dholland Exp $	*/
+/*	$NetBSD: kern_core.c,v 1.36 2022/09/09 14:30:17 christos Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.35 2021/06/29 22:40:53 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.36 2022/09/09 14:30:17 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_execfmt.h"
@@ -121,7 +121,7 @@ coredump(struct lwp *l, const char *patt
 	struct vnode		*vp;
 	struct proc		*p;
 	struct vmspace		*vm;
-	kauth_cred_t		cred;
+	kauth_cred_t		cred = NULL;
 	struct pathbuf		*pb;
 	struct vattr		vattr;
 	struct coredump_iostate	io;
@@ -145,9 +145,7 @@ coredump(struct lwp *l, const char *patt
 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
 	p->p_rlimit[RLIMIT_CORE].rlim_cur) {
 		error = EFBIG;		/* better error code? */
-		mutex_exit(p->p_lock);
-		mutex_exit(_lock);
-		goto done;
+		goto release;
 	}
 
 	/*
@@ -164,9 +162,7 @@ coredump(struct lwp *l, const char *patt
 	if (p->p_flag & PK_SUGID) {
 		if (!security_setidcore_dump) {
 			error = EPERM;
-			mutex_exit(p->p_lock);
-			mutex_exit(_lock);
-			goto done;
+			goto release;
 		}
 		pattern = security_setidcore_path;
 	}
@@ -180,11 +176,8 @@ coredump(struct lwp *l, const char *patt
 	error = coredump_buildname(p, name, pattern, MAXPATHLEN);
 	mutex_exit(>pl_lock);
 
-	if (error) {
-		mutex_exit(p->p_lock);
-		mutex_exit(_lock);
-		goto done;
-	}
+	if (error)
+		goto release;
 
 	/*
 	 * On a simple filename, see if the filesystem allow us to write
@@ -198,6 +191,7 @@ coredump(struct lwp *l, const char *patt
 			error = EPERM;
 	}
 
+release:
 	mutex_exit(p->p_lock);
 	mutex_exit(_lock);
 	if (error)
@@ -284,6 +278,8 @@ coredump(struct lwp *l, const char *patt
 	if (error == 0)
 		error = error1;
 done:
+	if (cred != NULL)
+		kauth_cred_free(cred);
 	if (name != NULL)
 		PNBUF_PUT(name);
 	return error;



CVS commit: src/sys/kern

2022-09-09 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Sep  9 14:30:17 UTC 2022

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

Log Message:
Don't forget to free the cred we just held.
Thanks to Chris J-D (chris at accessvector dot net)
While here, de-duplicate the mutex exit sequence.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/kern/kern_core.c

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



CVS commit: src/sys/kern

2022-09-06 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Tue Sep  6 13:31:09 UTC 2022

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

Log Message:
Resequence the sysctl_setup and evcnt stuff so that they always occur
_before_ the module's MODULE_CMD_INIT call.  Also update the unload
code to invoke the sysctl_setup and evcnt stuff _after_ the module's
MODULE_CMD_FINI call.  This makes the behaviour and order consistent
whether the module is built-in or loaded at run-time.

As reported by riastradh@


To generate a diff of this commit:
cvs rdiff -u -r1.158 -r1.159 src/sys/kern/kern_module.c

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



CVS commit: src/sys/kern

2022-09-06 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Tue Sep  6 13:31:09 UTC 2022

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

Log Message:
Resequence the sysctl_setup and evcnt stuff so that they always occur
_before_ the module's MODULE_CMD_INIT call.  Also update the unload
code to invoke the sysctl_setup and evcnt stuff _after_ the module's
MODULE_CMD_FINI call.  This makes the behaviour and order consistent
whether the module is built-in or loaded at run-time.

As reported by riastradh@


To generate a diff of this commit:
cvs rdiff -u -r1.158 -r1.159 src/sys/kern/kern_module.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_module.c
diff -u src/sys/kern/kern_module.c:1.158 src/sys/kern/kern_module.c:1.159
--- src/sys/kern/kern_module.c:1.158	Fri Aug 12 15:17:10 2022
+++ src/sys/kern/kern_module.c	Tue Sep  6 13:31:09 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_module.c,v 1.158 2022/08/12 15:17:10 riastradh Exp $	*/
+/*	$NetBSD: kern_module.c,v 1.159 2022/09/06 13:31:09 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.158 2022/08/12 15:17:10 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.159 2022/09/06 13:31:09 pgoyette Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -1371,6 +1371,17 @@ module_do_load(const char *name, bool is
 
 	prev_active = module_active;
 	module_active = mod;
+
+	/*
+	 * Note that we handle sysctl and evcnt setup _before_ we
+	 * initialize the module itself.  This maintains a consistent
+	 * order between built-in and run-time-loaded modules.  If
+	 * initialization then fails, we'll need to undo these, too.
+	 */
+	module_load_sysctl(mod);	/* Set-up module's sysctl if any */
+	module_load_evcnt(mod);		/* Attach any static evcnt needed */
+
+
 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props);
 	module_active = prev_active;
 	if (filedict) {
@@ -1380,7 +1391,7 @@ module_do_load(const char *name, bool is
 	if (error != 0) {
 		module_error("modcmd(CMD_INIT) failed for `%s', error %d",
 		mi->mi_name, error);
-		goto fail;
+		goto fail3;
 	}
 
 	/*
@@ -1395,9 +1406,6 @@ module_do_load(const char *name, bool is
 		goto fail1;
 	}
 
-	module_load_sysctl(mod);	/* Set-up module's sysctl if any */
-	module_load_evcnt(mod);		/* Attach any static evcnt needed */
-
 	/*
 	 * Good, the module loaded successfully.  Put it onto the
 	 * list and add references to its requisite modules.
@@ -1423,6 +1431,16 @@ module_do_load(const char *name, bool is
 
  fail1:
 	(*mi->mi_modcmd)(MODULE_CMD_FINI, NULL);
+ fail3:
+	/*
+	 * If there were any registered SYSCTL_SETUP funcs, make sure
+	 * we release the sysctl entries
+	 */
+	if (mod->mod_sysctllog) {
+		sysctl_teardown(>mod_sysctllog);
+	}
+	/* Also detach any static evcnt's */
+	module_unload_evcnt(mod);
  fail:
 	kobj_unload(mod->mod_kobj);
  fail2:
@@ -1478,20 +1496,22 @@ module_do_unload(const char *name, bool 
 	module_active = mod;
 	module_callback_unload(mod);
 
+	/* let the module clean up after itself */
+	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
+
 	/*
 	 * If there were any registered SYSCTL_SETUP funcs, make sure
-	 * we release the sysctl entries
+	 * we release the sysctl entries.  Same for static evcnt.
 	 */
-	if (mod->mod_sysctllog) {
-		sysctl_teardown(>mod_sysctllog);
+	if (error == 0) {
+		if (mod->mod_sysctllog) {
+			sysctl_teardown(>mod_sysctllog);
+		}
+		module_unload_evcnt(mod);
 	}
-	module_unload_evcnt(mod);
-	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
 	module_active = prev_active;
 	if (error != 0) {
-		module_load_sysctl(mod);	/* re-enable sysctl stuff */
-		module_load_evcnt(mod);		/* and reenable evcnts */
-		module_print("cannot unload module `%s' error=%d", name,
+		module_print("could not unload module `%s' error=%d", name,
 		error);
 		return error;
 	}



CVS commit: src/sys/kern

2022-09-02 Thread Takeshi Nakayama
Module Name:src
Committed By:   nakayama
Date:   Fri Sep  2 06:01:38 UTC 2022

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

Log Message:
Fix "error: cast from pointer to integer of different size" on
sparc build.


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 src/sys/kern/subr_lockdebug.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/subr_lockdebug.c
diff -u src/sys/kern/subr_lockdebug.c:1.82 src/sys/kern/subr_lockdebug.c:1.83
--- src/sys/kern/subr_lockdebug.c:1.82	Wed Aug 31 05:24:41 2022
+++ src/sys/kern/subr_lockdebug.c	Fri Sep  2 06:01:38 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_lockdebug.c,v 1.82 2022/08/31 05:24:41 msaitoh Exp $	*/
+/*	$NetBSD: subr_lockdebug.c,v 1.83 2022/09/02 06:01:38 nakayama Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.82 2022/08/31 05:24:41 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.83 2022/09/02 06:01:38 nakayama Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -729,7 +729,7 @@ lockdebug_dump(lwp_t *l, lockdebug_t *ld
 	char locksym[128], initsym[128], lockedsym[128], unlockedsym[128];
 
 #ifdef DDB
-	db_symstr(locksym, sizeof(locksym), (db_expr_t)ld->ld_lock,
+	db_symstr(locksym, sizeof(locksym), (db_expr_t)(intptr_t)ld->ld_lock,
 	DB_STGY_ANY);
 	db_symstr(initsym, sizeof(initsym), (db_expr_t)ld->ld_initaddr,
 	DB_STGY_PROC);
@@ -855,7 +855,7 @@ lockdebug_lock_print(void *addr,
 	uintptr_t word;
 
 	(*pr)("WARNING: lock print is unreliable without LOCKDEBUG\n");
-	db_symstr(sym, sizeof(sym), (db_expr_t)addr, DB_STGY_ANY);
+	db_symstr(sym, sizeof(sym), (db_expr_t)(intptr_t)addr, DB_STGY_ANY);
 	db_read_bytes((db_addr_t)addr, sizeof(word), (char *));
 	(*pr)("%s: possible owner: %p, bits: 0x%" PRIxPTR "\n", sym,
 	(void *)(word & ~(uintptr_t)ALIGNBYTES), word & ALIGNBYTES);
@@ -1075,7 +1075,8 @@ lockdebug_abort(const char *func, size_t
 	char locksym[128];
 
 #ifdef DDB
-	db_symstr(locksym, sizeof(locksym), (db_expr_t)lock, DB_STGY_ANY);
+	db_symstr(locksym, sizeof(locksym), (db_expr_t)(intptr_t)lock,
+	DB_STGY_ANY);
 #else
 	snprintf(locksym, sizeof(locksym), "%#018lx", (unsigned long)lock);
 #endif



CVS commit: src/sys/kern

2022-09-02 Thread Takeshi Nakayama
Module Name:src
Committed By:   nakayama
Date:   Fri Sep  2 06:01:38 UTC 2022

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

Log Message:
Fix "error: cast from pointer to integer of different size" on
sparc build.


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 src/sys/kern/subr_lockdebug.c

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



CVS commit: src/sys/kern

2022-08-30 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Wed Aug 31 05:24:41 UTC 2022

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

Log Message:
Fix previous to make it compile again.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/kern/subr_lockdebug.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/subr_lockdebug.c
diff -u src/sys/kern/subr_lockdebug.c:1.81 src/sys/kern/subr_lockdebug.c:1.82
--- src/sys/kern/subr_lockdebug.c:1.81	Tue Aug 30 22:38:17 2022
+++ src/sys/kern/subr_lockdebug.c	Wed Aug 31 05:24:41 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_lockdebug.c,v 1.81 2022/08/30 22:38:17 riastradh Exp $	*/
+/*	$NetBSD: subr_lockdebug.c,v 1.82 2022/08/31 05:24:41 msaitoh Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.81 2022/08/30 22:38:17 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.82 2022/08/31 05:24:41 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -56,6 +56,13 @@ __KERNEL_RCSID(0, "$NetBSD: subr_lockdeb
 
 #include 
 
+#ifdef DDB
+#include 
+#include 
+#include 
+#include 
+#endif
+
 unsigned int		ld_panic;
 
 #ifdef LOCKDEBUG
@@ -708,13 +715,6 @@ lockdebug_mem_check(const char *func, si
 }
 #endif /* _KERNEL */
 
-#ifdef DDB
-#include 
-#include 
-#include 
-#include 
-#endif
-
 /*
  * lockdebug_dump:
  *
@@ -856,8 +856,8 @@ lockdebug_lock_print(void *addr,
 
 	(*pr)("WARNING: lock print is unreliable without LOCKDEBUG\n");
 	db_symstr(sym, sizeof(sym), (db_expr_t)addr, DB_STGY_ANY);
-	db_read_bytes((db_addr_t)addr, sizeof(word), );
-	(*pr)("%s: possible owner: %p, bits: 0x%x\n", sym,
+	db_read_bytes((db_addr_t)addr, sizeof(word), (char *));
+	(*pr)("%s: possible owner: %p, bits: 0x%" PRIxPTR "\n", sym,
 	(void *)(word & ~(uintptr_t)ALIGNBYTES), word & ALIGNBYTES);
 #endif	/* LOCKDEBUG */
 }



CVS commit: src/sys/kern

2022-08-30 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Wed Aug 31 05:24:41 UTC 2022

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

Log Message:
Fix previous to make it compile again.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/kern/subr_lockdebug.c

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



CVS commit: src/sys/kern

2022-08-30 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 30 22:38:27 UTC 2022

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

Log Message:
crashme(9): New debug.crashme.mutex_recursion method.

Takes a lock twice.  Set it to 1 for adaptive lock, 2 for spin lock.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/kern/kern_crashme.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_crashme.c
diff -u src/sys/kern/kern_crashme.c:1.6 src/sys/kern/kern_crashme.c:1.7
--- src/sys/kern/kern_crashme.c:1.6	Tue Aug 16 10:24:17 2022
+++ src/sys/kern/kern_crashme.c	Tue Aug 30 22:38:26 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_crashme.c,v 1.6 2022/08/16 10:24:17 riastradh Exp $	*/
+/*	$NetBSD: kern_crashme.c,v 1.7 2022/08/30 22:38:26 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2018, 2019 Matthew R. Green
@@ -66,6 +66,7 @@ static int crashme_ddb(int);
 #ifdef LOCKDEBUG
 static int crashme_kernel_lock_spinout(int);
 #endif
+static int crashme_mutex_recursion(int);
 
 #define CMNODE(name, lname, func)	\
 {	\
@@ -85,6 +86,8 @@ static crashme_node nodes[] = {
 CMNODE("kernel_lock_spinout", "infinite kernel lock",
 	crashme_kernel_lock_spinout),
 #endif
+CMNODE("mutex_recursion", "enter the same mutex twice",
+	crashme_mutex_recursion),
 };
 static crashme_node *first_node;
 static kmutex_t crashme_lock;
@@ -290,3 +293,36 @@ crashme_kernel_lock_spinout(int flags)
 	return 0;
 }
 #endif
+
+static int
+crashme_mutex_recursion(int flags)
+{
+	kmutex_t crashme_spinlock;
+
+	switch (flags) {
+	case 0:
+		return 0;
+	case 1:
+	default:
+		/*
+		 * printf makes the return address of the first
+		 * mutex_enter call a little more obvious, so the line
+		 * number of the _return address_ for the first
+		 * mutex_enter doesn't confusingly point at the second
+		 * mutex_enter.
+		 */
+		mutex_enter(_lock);
+		printf("%s: locked once\n", __func__);
+		mutex_enter(_lock);
+		printf("%s: locked twice\n", __func__);
+		return -1;
+	case 2:
+		mutex_init(_spinlock, MUTEX_DEFAULT, IPL_VM);
+		printf("%s: initialized\n", __func__);
+		mutex_enter(_spinlock);
+		printf("%s: locked once\n", __func__);
+		mutex_enter(_spinlock);
+		printf("%s: locked twice\n", __func__);
+		return -1;
+	}
+}



CVS commit: src/sys/kern

2022-08-30 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 30 22:38:27 UTC 2022

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

Log Message:
crashme(9): New debug.crashme.mutex_recursion method.

Takes a lock twice.  Set it to 1 for adaptive lock, 2 for spin lock.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/kern/kern_crashme.c

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



CVS commit: src/sys/kern

2022-08-30 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 30 22:38:17 UTC 2022

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

Log Message:
lockdebug(9): Try to show symbol names if possible.

Also print the possible owner in ddb/crash `show lock' even if the
kernel is built without LOCKDEBUG.

Output may not be as pretty before with two neatly aligned columns,
but that can be changed; for now the value of having the symbols
printed instead of just obscure hex addresses (which one's an lwp
address and which one's a code pointer? can never remember!) should
outweigh the prettiness temporarily lost.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/sys/kern/subr_lockdebug.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/subr_lockdebug.c
diff -u src/sys/kern/subr_lockdebug.c:1.80 src/sys/kern/subr_lockdebug.c:1.81
--- src/sys/kern/subr_lockdebug.c:1.80	Tue Mar  2 01:20:35 2021
+++ src/sys/kern/subr_lockdebug.c	Tue Aug 30 22:38:17 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_lockdebug.c,v 1.80 2021/03/02 01:20:35 rin Exp $	*/
+/*	$NetBSD: subr_lockdebug.c,v 1.81 2022/08/30 22:38:17 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.80 2021/03/02 01:20:35 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.81 2022/08/30 22:38:17 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -712,6 +712,7 @@ lockdebug_mem_check(const char *func, si
 #include 
 #include 
 #include 
+#include 
 #endif
 
 /*
@@ -725,12 +726,34 @@ lockdebug_dump(lwp_t *l, lockdebug_t *ld
 {
 	int sleeper = (ld->ld_flags & LD_SLEEPER);
 	lockops_t *lo = ld->ld_lockops;
+	char locksym[128], initsym[128], lockedsym[128], unlockedsym[128];
+
+#ifdef DDB
+	db_symstr(locksym, sizeof(locksym), (db_expr_t)ld->ld_lock,
+	DB_STGY_ANY);
+	db_symstr(initsym, sizeof(initsym), (db_expr_t)ld->ld_initaddr,
+	DB_STGY_PROC);
+	db_symstr(lockedsym, sizeof(lockedsym), (db_expr_t)ld->ld_locked,
+	DB_STGY_PROC);
+	db_symstr(unlockedsym, sizeof(unlockedsym), (db_expr_t)ld->ld_unlocked,
+	DB_STGY_PROC);
+#else
+	snprintf(locksym, sizeof(locksym), "%#018lx",
+	(unsigned long)ld->ld_lock);
+	snprintf(initsym, sizeof(initsym), "%#018lx",
+	(unsigned long)ld->ld_initaddr);
+	snprintf(lockedsym, sizeof(lockedsym), "%#018lx",
+	(unsigned long)ld->ld_locked);
+	snprintf(unlockedsym, sizeof(unlockedsym), "%#018lx",
+	(unsigned long)ld->ld_unlocked);
+#endif
 
 	(*pr)(
-	"lock address : %#018lx type : %18s\n"
-	"initialized  : %#018lx",
-	(long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
-	(long)ld->ld_initaddr);
+	"lock address : %s\n"
+	"type : %s\n"
+	"initialized  : %s",
+	locksym, (sleeper ? "sleep/adaptive" : "spin"),
+	initsym);
 
 #ifndef _KERNEL
 	lockops_t los;
@@ -742,15 +765,16 @@ lockdebug_dump(lwp_t *l, lockdebug_t *ld
 	"shares wanted: %18u exclusive: %18u\n"
 	"relevant cpu : %18u last held: %18u\n"
 	"relevant lwp : %#018lx last held: %#018lx\n"
-	"last locked%c : %#018lx unlocked%c: %#018lx\n",
+	"last locked%c : %s\n"
+	"unlocked%c: %s\n",
 	(unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
 	(unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
 	(unsigned)cpu_index(l->l_cpu), (unsigned)ld->ld_cpu,
 	(long)l, (long)ld->ld_lwp,
 	((ld->ld_flags & LD_LOCKED) ? '*' : ' '),
-	(long)ld->ld_locked,
+	lockedsym,
 	((ld->ld_flags & LD_LOCKED) ? ' ' : '*'),
-	(long)ld->ld_unlocked);
+	unlockedsym);
 
 #ifdef _KERNEL
 	if (lo->lo_dump != NULL)
@@ -827,7 +851,14 @@ lockdebug_lock_print(void *addr,
 		addr);
 	}
 #else
-	(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
+	char sym[128];
+	uintptr_t word;
+
+	(*pr)("WARNING: lock print is unreliable without LOCKDEBUG\n");
+	db_symstr(sym, sizeof(sym), (db_expr_t)addr, DB_STGY_ANY);
+	db_read_bytes((db_addr_t)addr, sizeof(word), );
+	(*pr)("%s: possible owner: %p, bits: 0x%x\n", sym,
+	(void *)(word & ~(uintptr_t)ALIGNBYTES), word & ALIGNBYTES);
 #endif	/* LOCKDEBUG */
 }
 
@@ -837,11 +868,12 @@ static void
 lockdebug_show_one(lwp_t *l, lockdebug_t *ld, int i,
 void (*pr)(const char *, ...) __printflike(1, 2))
 {
-	const char *sym;
+	char sym[128];
 
-#ifdef _KERNEL
-	ksyms_getname(NULL, , (vaddr_t)ld->ld_initaddr,
-	KSYMS_CLOSEST|KSYMS_PROC|KSYMS_ANY);
+#ifdef DDB
+	db_symstr(sym, sizeof(sym), (db_expr_t)ld->ld_initaddr, DB_STGY_PROC);
+#else
+	snprintf(sym, sizeof(sym), "%p", (void *)ld->ld_initaddr);
 #endif
 	(*pr)("* Lock %d (initialized at %s)\n", i++, sym);
 	lockdebug_dump(l, ld, pr);
@@ -1040,11 +1072,19 @@ lockdebug_abort(const char *func, size_t
 	if (atomic_inc_uint_nv(_panic) > 1)
 		return;
 
+	char locksym[128];
+
+#ifdef DDB
+	

CVS commit: src/sys/kern

2022-08-30 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 30 22:38:17 UTC 2022

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

Log Message:
lockdebug(9): Try to show symbol names if possible.

Also print the possible owner in ddb/crash `show lock' even if the
kernel is built without LOCKDEBUG.

Output may not be as pretty before with two neatly aligned columns,
but that can be changed; for now the value of having the symbols
printed instead of just obscure hex addresses (which one's an lwp
address and which one's a code pointer? can never remember!) should
outweigh the prettiness temporarily lost.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/sys/kern/subr_lockdebug.c

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



CVS commit: src/sys/kern

2022-08-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug 28 12:24:39 UTC 2022

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

Log Message:
devsw(9): Clarify to match loop condition.  NFCI.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/kern/subr_devsw.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/subr_devsw.c
diff -u src/sys/kern/subr_devsw.c:1.47 src/sys/kern/subr_devsw.c:1.48
--- src/sys/kern/subr_devsw.c:1.47	Sun Aug 28 11:17:38 2022
+++ src/sys/kern/subr_devsw.c	Sun Aug 28 12:24:39 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_devsw.c,v 1.47 2022/08/28 11:17:38 riastradh Exp $	*/
+/*	$NetBSD: subr_devsw.c,v 1.48 2022/08/28 12:24:39 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.47 2022/08/28 11:17:38 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.48 2022/08/28 12:24:39 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -194,7 +194,7 @@ devsw_attach(const char *devname,
 	 * If we already found a conv, we're done.  Otherwise, find an
 	 * empty slot or extend the table.
 	 */
-	if (i != max_devsw_convs) {
+	if (i < max_devsw_convs) {
 		error = 0;
 		goto out;
 	}



CVS commit: src/sys/kern

2022-08-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug 28 12:24:39 UTC 2022

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

Log Message:
devsw(9): Clarify to match loop condition.  NFCI.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/kern/subr_devsw.c

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



CVS commit: src/sys/kern

2022-08-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug 28 11:17:38 UTC 2022

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

Log Message:
devsw(9): Fix case of existing conv in devsw_attach.

- Fix sense of conditional: if we found a conv, i < max_devsw_convs.
- Make sure to initialize error on the way out.

PR kern/56962: Incorrect behavior of the devsw_attach function


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/kern/subr_devsw.c

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



CVS commit: src/sys/kern

2022-08-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug 28 11:17:38 UTC 2022

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

Log Message:
devsw(9): Fix case of existing conv in devsw_attach.

- Fix sense of conditional: if we found a conv, i < max_devsw_convs.
- Make sure to initialize error on the way out.

PR kern/56962: Incorrect behavior of the devsw_attach function


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/kern/subr_devsw.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/subr_devsw.c
diff -u src/sys/kern/subr_devsw.c:1.46 src/sys/kern/subr_devsw.c:1.47
--- src/sys/kern/subr_devsw.c:1.46	Sat Jul  9 10:30:27 2022
+++ src/sys/kern/subr_devsw.c	Sun Aug 28 11:17:38 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_devsw.c,v 1.46 2022/07/09 10:30:27 riastradh Exp $	*/
+/*	$NetBSD: subr_devsw.c,v 1.47 2022/08/28 11:17:38 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.46 2022/07/09 10:30:27 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.47 2022/08/28 11:17:38 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -194,8 +194,10 @@ devsw_attach(const char *devname,
 	 * If we already found a conv, we're done.  Otherwise, find an
 	 * empty slot or extend the table.
 	 */
-	if (i == max_devsw_convs)
+	if (i != max_devsw_convs) {
+		error = 0;
 		goto out;
+	}
 
 	for (i = 0; i < max_devsw_convs; i++) {
 		if (devsw_conv[i].d_name == NULL)



CVS commit: src/sys/kern

2022-08-26 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Fri Aug 26 11:03:53 UTC 2022

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

Log Message:
Two defects in vfs_getnewfsid():

- Parallel mounts may get the same fsid.  Always increment "xxxfs_mntid"
  to make it unlikely.

- Directly walk "mountlist" to prevent a rare deadlock where one thread
  holds a vnode locked, calls vfs_getnewfsid() and the iterator has to
  wait for a suspended file system while the thread suspending needs
  this vnode lock.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/kern/vfs_mount.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/vfs_mount.c
diff -u src/sys/kern/vfs_mount.c:1.95 src/sys/kern/vfs_mount.c:1.96
--- src/sys/kern/vfs_mount.c:1.95	Mon Aug 22 09:14:24 2022
+++ src/sys/kern/vfs_mount.c	Fri Aug 26 11:03:53 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_mount.c,v 1.95 2022/08/22 09:14:24 hannken Exp $	*/
+/*	$NetBSD: vfs_mount.c,v 1.96 2022/08/26 11:03:53 hannken Exp $	*/
 
 /*-
  * Copyright (c) 1997-2020 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.95 2022/08/22 09:14:24 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.96 2022/08/26 11:03:53 hannken Exp $");
 
 #include 
 #include 
@@ -220,23 +220,41 @@ void
 vfs_getnewfsid(struct mount *mp)
 {
 	static u_short xxxfs_mntid;
+	struct mountlist_entry *me;
 	fsid_t tfsid;
 	int mtype;
 
 	mutex_enter(_lock);
-	mtype = makefstype(mp->mnt_op->vfs_name);
-	mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0);
-	mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype;
-	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
 	if (xxxfs_mntid == 0)
 		++xxxfs_mntid;
+	mtype = makefstype(mp->mnt_op->vfs_name);
 	tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
 	tfsid.__fsid_val[1] = mtype;
-	while (vfs_getvfs()) {
-		tfsid.__fsid_val[0]++;
-		xxxfs_mntid++;
+	/* Always increment to not return the same fsid to parallel mounts. */
+	xxxfs_mntid++;
+
+	/*
+	 * Directly walk mountlist to prevent deadlock through
+	 * mountlist_iterator_next() -> vfs_busy().
+	 */
+	mutex_enter(_lock);
+	for (me = TAILQ_FIRST(); me != TAILQ_END(); ) {
+		if (me->me_type == ME_MOUNT &&
+		me->me_mount->mnt_stat.f_fsidx.__fsid_val[0] ==
+		tfsid.__fsid_val[0] &&
+		me->me_mount->mnt_stat.f_fsidx.__fsid_val[1] ==
+		tfsid.__fsid_val[1]) {
+			tfsid.__fsid_val[0]++;
+			xxxfs_mntid++;
+			me = TAILQ_FIRST();
+		} else {
+			me = TAILQ_NEXT(me, me_list);
+		}
 	}
+	mutex_exit(_lock);
+
 	mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
+	mp->mnt_stat.f_fsidx.__fsid_val[1] = tfsid.__fsid_val[1];
 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
 	mutex_exit(_lock);
 }



CVS commit: src/sys/kern

2022-08-26 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Fri Aug 26 11:03:53 UTC 2022

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

Log Message:
Two defects in vfs_getnewfsid():

- Parallel mounts may get the same fsid.  Always increment "xxxfs_mntid"
  to make it unlikely.

- Directly walk "mountlist" to prevent a rare deadlock where one thread
  holds a vnode locked, calls vfs_getnewfsid() and the iterator has to
  wait for a suspended file system while the thread suspending needs
  this vnode lock.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/kern/vfs_mount.c

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



CVS commit: src/sys/kern

2022-08-24 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Aug 24 11:41:39 UTC 2022

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

Log Message:
kern_pmf.c: Fix whitespace, KNF, static.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/sys/kern/kern_pmf.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_pmf.c
diff -u src/sys/kern/kern_pmf.c:1.50 src/sys/kern/kern_pmf.c:1.51
--- src/sys/kern/kern_pmf.c:1.50	Wed Aug 24 11:19:25 2022
+++ src/sys/kern/kern_pmf.c	Wed Aug 24 11:41:39 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_pmf.c,v 1.50 2022/08/24 11:19:25 riastradh Exp $ */
+/* $NetBSD: kern_pmf.c,v 1.51 2022/08/24 11:41:39 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill 
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.50 2022/08/24 11:19:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.51 2022/08/24 11:41:39 riastradh Exp $");
 
 #include 
 #include 
@@ -112,8 +112,8 @@ static struct pool pew_pl;
 static pmf_event_workitem_t *pmf_event_workitem_get(void);
 static void pmf_event_workitem_put(pmf_event_workitem_t *);
 
-bool pmf_device_resume_locked(device_t, const pmf_qual_t *);
-bool pmf_device_suspend_locked(device_t, const pmf_qual_t *);
+static bool pmf_device_resume_locked(device_t, const pmf_qual_t *);
+static bool pmf_device_suspend_locked(device_t, const pmf_qual_t *);
 static bool device_pmf_any_suspensor(device_t, devact_level_t);
 
 static bool
@@ -163,17 +163,17 @@ pmf_suspend_worker(struct work *wk, void
 	switch (pmf_qual_depth(>psw_qual)) {
 	case DEVACT_LEVEL_FULL:
 		if (!complete_suspension(dev, dev->dv_class_suspensors,
-		>psw_qual))
+			>psw_qual))
 			break;
 		/*FALLTHROUGH*/
 	case DEVACT_LEVEL_DRIVER:
 		if (!complete_suspension(dev, dev->dv_driver_suspensors,
-		>psw_qual))
+			>psw_qual))
 			break;
 		/*FALLTHROUGH*/
 	case DEVACT_LEVEL_BUS:
 		if (!complete_suspension(dev, dev->dv_bus_suspensors,
-		>psw_qual))
+			>psw_qual))
 			break;
 	}
 	device_pmf_unlock(dev);
@@ -189,7 +189,7 @@ pmf_event_worker(struct work *wk, void *
 	pew = (void *)wk;
 	KASSERT(wk == >pew_work);
 	KASSERT(pew != NULL);
-	
+
 	TAILQ_FOREACH(event, _all_events, pmf_link) {
 		if (event->pmf_event != pew->pew_event)
 			continue;
@@ -427,31 +427,32 @@ pmf_device_register1(device_t dev,
 void
 pmf_device_deregister(device_t dev)
 {
+
 	device_pmf_class_deregister(dev);
 	device_pmf_bus_deregister(dev);
 	device_pmf_driver_deregister(dev);
 }
 
 static const device_suspensor_t _device_suspensor_drvctl = {
-	  .ds_delegator = NULL
-	, .ds_name = "drvctl"
+	.ds_delegator = NULL,
+	.ds_name = "drvctl",
 };
 
 static const device_suspensor_t _device_suspensor_self = {
-	  .ds_delegator = NULL
-	, .ds_name = "self"
+	.ds_delegator = NULL,
+	.ds_name = "self",
 };
 
 #if 0
 static const device_suspensor_t _device_suspensor_self_delegate = {
-	  .ds_delegator = &_device_suspensor_self
-	, .ds_name = "self delegate"
+	.ds_delegator = &_device_suspensor_self,
+	.ds_name = "self delegate",
 };
 #endif
 
 static const device_suspensor_t _device_suspensor_system = {
-	  .ds_delegator = NULL
-	, .ds_name = "system"
+	.ds_delegator = NULL,
+	.ds_name = "system",
 };
 
 const device_suspensor_t
@@ -463,18 +464,18 @@ const device_suspensor_t
 * const device_suspensor_drvctl = &_device_suspensor_drvctl;
 
 static const pmf_qual_t _pmf_qual_system = {
-	  .pq_actlvl = DEVACT_LEVEL_FULL
-	, .pq_suspensor = &_device_suspensor_system
+	.pq_actlvl = DEVACT_LEVEL_FULL,
+	.pq_suspensor = &_device_suspensor_system,
 };
 
 static const pmf_qual_t _pmf_qual_drvctl = {
-	  .pq_actlvl = DEVACT_LEVEL_FULL
-	, .pq_suspensor = &_device_suspensor_drvctl
+	.pq_actlvl = DEVACT_LEVEL_FULL,
+	.pq_suspensor = &_device_suspensor_drvctl,
 };
 
 static const pmf_qual_t _pmf_qual_self = {
-	  .pq_actlvl = DEVACT_LEVEL_DRIVER
-	, .pq_suspensor = &_device_suspensor_self
+	.pq_actlvl = DEVACT_LEVEL_DRIVER,
+	.pq_suspensor = &_device_suspensor_self,
 };
 
 const pmf_qual_t
@@ -667,6 +668,7 @@ void
 pmf_self_suspensor_init(device_t dev, device_suspensor_t *ds,
 pmf_qual_t *pq)
 {
+
 	ds->ds_delegator = device_suspensor_self;
 	snprintf(ds->ds_name, sizeof(ds->ds_name), "%s-self",
 	device_xname(dev));
@@ -697,6 +699,7 @@ pmf_device_suspend(device_t dev, const p
 bool
 pmf_device_suspend_locked(device_t dev, const pmf_qual_t *qual)
 {
+
 	if (!device_pmf_add_suspensor(dev, qual))
 		return false;
 
@@ -738,6 +741,7 @@ pmf_device_resume(device_t dev, const pm
 bool
 pmf_device_resume_locked(device_t dev, const pmf_qual_t *qual)
 {
+
 	device_pmf_remove_suspensor(dev, qual);
 
 	if (device_pmf_any_suspensor(dev, DEVACT_LEVEL_FULL))
@@ -785,6 +789,7 @@ pmf_device_recursive_suspend(device_t dv
 void
 pmf_qual_recursive_copy(pmf_qual_t *dst, const pmf_qual_t *src)
 {
+
 	*dst = *src;
 	dst->pq_actlvl = 

CVS commit: src/sys/kern

2022-08-24 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Aug 24 11:41:39 UTC 2022

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

Log Message:
kern_pmf.c: Fix whitespace, KNF, static.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/sys/kern/kern_pmf.c

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



CVS commit: src/sys/kern

2022-08-22 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Mon Aug 22 09:14:59 UTC 2022

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

Log Message:
Use fstrans_start()/fstrans_done() to cross the mount in lookup_crossmount().
It is sufficient here as it prevents the file system from unmount and
makes it safe to use VFS_ROOT() here.

Removes a rare deadlock where one thread has "foundobj" locked and waits
for "foundobj->v_mountedhere" to resume while the thread holding the file
system suspended tries to lookup a node and needs a lock on "foundobj".


To generate a diff of this commit:
cvs rdiff -u -r1.231 -r1.232 src/sys/kern/vfs_lookup.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/vfs_lookup.c
diff -u src/sys/kern/vfs_lookup.c:1.231 src/sys/kern/vfs_lookup.c:1.232
--- src/sys/kern/vfs_lookup.c:1.231	Thu Feb 10 10:59:12 2022
+++ src/sys/kern/vfs_lookup.c	Mon Aug 22 09:14:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_lookup.c,v 1.231 2022/02/10 10:59:12 hannken Exp $	*/
+/*	$NetBSD: vfs_lookup.c,v 1.232 2022/08/22 09:14:59 hannken Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.231 2022/02/10 10:59:12 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.232 2022/08/22 09:14:59 hannken Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_magiclinks.h"
@@ -51,6 +51,7 @@ __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -948,31 +949,16 @@ lookup_crossmount(struct namei_state *st
 			vrele(foundobj);
 			foundobj = vp;
 		} else {
-			/* First get the vnode stable. */
-			error = vn_lock(foundobj, LK_SHARED);
-			if (error != 0) {
-vrele(foundobj);
-foundobj = NULL;
-break;
-			}
-
-			/*
-			 * Check to see if something is still mounted on it.
-			 */
-			if ((mp = foundobj->v_mountedhere) == NULL) {
-VOP_UNLOCK(foundobj);
-break;
+			/* First get the vnodes mount stable. */
+			while ((mp = foundobj->v_mountedhere) != NULL) {
+fstrans_start(mp);
+if (fstrans_held(mp) &&
+mp == foundobj->v_mountedhere) {
+	break;
+}
+fstrans_done(mp);
 			}
-
-			/*
-			 * Get a reference to the mountpoint, and unlock
-			 * foundobj.
-			 */
-			error = vfs_busy(mp);
-			VOP_UNLOCK(foundobj);
-			if (error != 0) {
-vrele(foundobj);
-foundobj = NULL;
+			if (mp == NULL) {
 break;
 			}
 
@@ -992,7 +978,7 @@ lookup_crossmount(struct namei_state *st
 
 			/* Finally, drop references to foundobj & mountpoint. */
 			vrele(foundobj);
-			vfs_unbusy(mp);
+			fstrans_done(mp);
 			if (error) {
 foundobj = NULL;
 break;



CVS commit: src/sys/kern

2022-08-22 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Mon Aug 22 09:14:59 UTC 2022

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

Log Message:
Use fstrans_start()/fstrans_done() to cross the mount in lookup_crossmount().
It is sufficient here as it prevents the file system from unmount and
makes it safe to use VFS_ROOT() here.

Removes a rare deadlock where one thread has "foundobj" locked and waits
for "foundobj->v_mountedhere" to resume while the thread holding the file
system suspended tries to lookup a node and needs a lock on "foundobj".


To generate a diff of this commit:
cvs rdiff -u -r1.231 -r1.232 src/sys/kern/vfs_lookup.c

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



CVS commit: src/sys/kern

2022-08-22 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Mon Aug 22 09:14:24 UTC 2022

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

Log Message:
Protect changing "v_mountedhere" with file system suspension instead
of vnode lock.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/kern/vfs_mount.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/vfs_mount.c
diff -u src/sys/kern/vfs_mount.c:1.94 src/sys/kern/vfs_mount.c:1.95
--- src/sys/kern/vfs_mount.c:1.94	Fri Jul  8 07:43:19 2022
+++ src/sys/kern/vfs_mount.c	Mon Aug 22 09:14:24 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_mount.c,v 1.94 2022/07/08 07:43:19 hannken Exp $	*/
+/*	$NetBSD: vfs_mount.c,v 1.95 2022/08/22 09:14:24 hannken Exp $	*/
 
 /*-
  * Copyright (c) 1997-2020 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.94 2022/07/08 07:43:19 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.95 2022/08/22 09:14:24 hannken Exp $");
 
 #include 
 #include 
@@ -929,14 +929,12 @@ dounmount(struct mount *mp, int flags, s
 	 * vfs_busy() from succeeding.
 	 */
 	mp->mnt_iflag |= IMNT_GONE;
-	if (!was_suspended)
-		vfs_resume(mp);
-
 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
-		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
 		coveredvp->v_mountedhere = NULL;
-		VOP_UNLOCK(coveredvp);
 	}
+	if (!was_suspended)
+		vfs_resume(mp);
+
 	mountlist_remove(mp);
 	if (TAILQ_FIRST(>mnt_vnodelist) != NULL)
 		panic("unmount: dangling vnode");



CVS commit: src/sys/kern

2022-08-22 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Mon Aug 22 09:14:24 UTC 2022

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

Log Message:
Protect changing "v_mountedhere" with file system suspension instead
of vnode lock.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/kern/vfs_mount.c

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



CVS commit: src/sys/kern

2022-08-22 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Mon Aug 22 09:13:08 UTC 2022

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

Log Message:
Don't allocate lwp info for fstrans_held() and fstrans_is_owner().
If it doesn't exist we cannot hold a transaction or suspension.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/sys/kern/vfs_trans.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/vfs_trans.c
diff -u src/sys/kern/vfs_trans.c:1.67 src/sys/kern/vfs_trans.c:1.68
--- src/sys/kern/vfs_trans.c:1.67	Thu Aug 11 10:17:44 2022
+++ src/sys/kern/vfs_trans.c	Mon Aug 22 09:13:08 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_trans.c,v 1.67 2022/08/11 10:17:44 hannken Exp $	*/
+/*	$NetBSD: vfs_trans.c,v 1.68 2022/08/22 09:13:08 hannken Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.67 2022/08/11 10:17:44 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.68 2022/08/22 09:13:08 hannken Exp $");
 
 /*
  * File system transaction operations.
@@ -631,7 +631,7 @@ fstrans_held(struct mount *mp)
 
 	KASSERT(mp != dead_rootmount);
 
-	fli = fstrans_get_lwp_info(mp, true);
+	fli = fstrans_get_lwp_info(mp, false);
 	if (fli == NULL)
 		return 0;
 	fmi = fli->fli_mountinfo;
@@ -650,7 +650,7 @@ fstrans_is_owner(struct mount *mp)
 
 	KASSERT(mp != dead_rootmount);
 
-	fli = fstrans_get_lwp_info(mp, true);
+	fli = fstrans_get_lwp_info(mp, false);
 	if (fli == NULL)
 		return 0;
 	fmi = fli->fli_mountinfo;



CVS commit: src/sys/kern

2022-08-22 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Mon Aug 22 09:13:08 UTC 2022

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

Log Message:
Don't allocate lwp info for fstrans_held() and fstrans_is_owner().
If it doesn't exist we cannot hold a transaction or suspension.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/sys/kern/vfs_trans.c

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



CVS commit: src/sys/kern

2022-08-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Aug 20 23:37:12 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Limit ipi trace diagnostic to after init has started.


To generate a diff of this commit:
cvs rdiff -u -r1.177 -r1.178 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.



CVS commit: src/sys/kern

2022-08-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Aug 20 23:37:12 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Limit ipi trace diagnostic to after init has started.


To generate a diff of this commit:
cvs rdiff -u -r1.177 -r1.178 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.177 src/sys/kern/kern_lock.c:1.178
--- src/sys/kern/kern_lock.c:1.177	Tue Aug 16 21:23:37 2022
+++ src/sys/kern/kern_lock.c	Sat Aug 20 23:37:12 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.177 2022/08/16 21:23:37 riastradh Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.178 2022/08/20 23:37:12 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.177 2022/08/16 21:23:37 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.178 2022/08/20 23:37:12 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_lockdebug.h"
@@ -253,8 +253,8 @@ _kernel_lock(int nlocks)
 		splx(s);
 		while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
 #ifdef LOCKDEBUG
-			if (SPINLOCK_SPINOUT(spins)) {
-extern int start_init_exec;
+			extern int start_init_exec;
+			if (SPINLOCK_SPINOUT(spins) && start_init_exec) {
 ipi_msg_t msg = {
 	.func = kernel_lock_trace_ipi,
 };
@@ -262,8 +262,7 @@ _kernel_lock(int nlocks)
 ipi_unicast(, kernel_lock_holder);
 ipi_wait();
 kpreempt_enable();
-if (start_init_exec)
-	_KERNEL_LOCK_ABORT("spinout");
+_KERNEL_LOCK_ABORT("spinout");
 			}
 			SPINLOCK_BACKOFF_HOOK;
 			SPINLOCK_SPIN_HOOK;



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 21:23:37 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Fix previous for non-LOCKDEBUG builds.


To generate a diff of this commit:
cvs rdiff -u -r1.176 -r1.177 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.176 src/sys/kern/kern_lock.c:1.177
--- src/sys/kern/kern_lock.c:1.176	Tue Aug 16 18:13:09 2022
+++ src/sys/kern/kern_lock.c	Tue Aug 16 21:23:37 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.176 2022/08/16 18:13:09 riastradh Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.177 2022/08/16 21:23:37 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.176 2022/08/16 18:13:09 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.177 2022/08/16 21:23:37 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_lockdebug.h"
@@ -216,7 +216,9 @@ _kernel_lock(int nlocks)
 	0);
 
 	if (__predict_true(__cpu_simple_lock_try(kernel_lock))) {
+#ifdef LOCKDEBUG
 		kernel_lock_holder = curcpu();
+#endif
 		ci->ci_biglock_count = nlocks;
 		l->l_blcnt = nlocks;
 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 21:23:37 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Fix previous for non-LOCKDEBUG builds.


To generate a diff of this commit:
cvs rdiff -u -r1.176 -r1.177 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.



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 18:13:09 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Record kernel lock holder in fast path too.


To generate a diff of this commit:
cvs rdiff -u -r1.175 -r1.176 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.175 src/sys/kern/kern_lock.c:1.176
--- src/sys/kern/kern_lock.c:1.175	Tue Aug 16 16:25:16 2022
+++ src/sys/kern/kern_lock.c	Tue Aug 16 18:13:09 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.175 2022/08/16 16:25:16 riastradh Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.176 2022/08/16 18:13:09 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.175 2022/08/16 16:25:16 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.176 2022/08/16 18:13:09 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_lockdebug.h"
@@ -216,6 +216,7 @@ _kernel_lock(int nlocks)
 	0);
 
 	if (__predict_true(__cpu_simple_lock_try(kernel_lock))) {
+		kernel_lock_holder = curcpu();
 		ci->ci_biglock_count = nlocks;
 		l->l_blcnt = nlocks;
 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 18:13:09 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Record kernel lock holder in fast path too.


To generate a diff of this commit:
cvs rdiff -u -r1.175 -r1.176 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.



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 16:25:17 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Need kpreempt_disable to ipi_send, oops.


To generate a diff of this commit:
cvs rdiff -u -r1.174 -r1.175 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.174 src/sys/kern/kern_lock.c:1.175
--- src/sys/kern/kern_lock.c:1.174	Tue Aug 16 00:26:39 2022
+++ src/sys/kern/kern_lock.c	Tue Aug 16 16:25:16 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.174 2022/08/16 00:26:39 riastradh Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.175 2022/08/16 16:25:16 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.174 2022/08/16 00:26:39 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.175 2022/08/16 16:25:16 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_lockdebug.h"
@@ -255,8 +255,10 @@ _kernel_lock(int nlocks)
 ipi_msg_t msg = {
 	.func = kernel_lock_trace_ipi,
 };
+kpreempt_disable();
 ipi_unicast(, kernel_lock_holder);
 ipi_wait();
+kpreempt_enable();
 if (start_init_exec)
 	_KERNEL_LOCK_ABORT("spinout");
 			}



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 16:25:17 UTC 2022

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

Log Message:
KERNEL_LOCK(9): Need kpreempt_disable to ipi_send, oops.


To generate a diff of this commit:
cvs rdiff -u -r1.174 -r1.175 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.



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 10:24:17 UTC 2022

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

Log Message:
crashme(9): New kernel_lock_spinout crasher.

This assumes that something will eventually try to take the kernel
lock and, after spinning for a while, spin out and crash -- so for
now it's only enabled under LOCKDEBUG, without which the kernel lock
spins forever.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/kern/kern_crashme.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_crashme.c
diff -u src/sys/kern/kern_crashme.c:1.5 src/sys/kern/kern_crashme.c:1.6
--- src/sys/kern/kern_crashme.c:1.5	Sat Nov 27 14:11:14 2021
+++ src/sys/kern/kern_crashme.c	Tue Aug 16 10:24:17 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_crashme.c,v 1.5 2021/11/27 14:11:14 riastradh Exp $	*/
+/*	$NetBSD: kern_crashme.c,v 1.6 2022/08/16 10:24:17 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2018, 2019 Matthew R. Green
@@ -63,6 +63,9 @@ static int crashme_null_jump(int);
 #ifdef DDB
 static int crashme_ddb(int);
 #endif
+#ifdef LOCKDEBUG
+static int crashme_kernel_lock_spinout(int);
+#endif
 
 #define CMNODE(name, lname, func)	\
 {	\
@@ -78,6 +81,10 @@ static crashme_node nodes[] = {
 #ifdef DDB
 CMNODE("ddb", "enter ddb directly", crashme_ddb),
 #endif
+#ifdef LOCKDEBUG
+CMNODE("kernel_lock_spinout", "infinite kernel lock",
+	crashme_kernel_lock_spinout),
+#endif
 };
 static crashme_node *first_node;
 static kmutex_t crashme_lock;
@@ -270,3 +277,16 @@ crashme_ddb(int flags)
 	return 0;
 }
 #endif
+
+#ifdef LOCKDEBUG
+static int
+crashme_kernel_lock_spinout(int flags)
+{
+
+	KERNEL_LOCK(1, NULL);
+	for (;;)
+		__insn_barrier();
+	KERNEL_UNLOCK_ONE(NULL);
+	return 0;
+}
+#endif



CVS commit: src/sys/kern

2022-08-16 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Aug 16 10:24:17 UTC 2022

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

Log Message:
crashme(9): New kernel_lock_spinout crasher.

This assumes that something will eventually try to take the kernel
lock and, after spinning for a while, spin out and crash -- so for
now it's only enabled under LOCKDEBUG, without which the kernel lock
spins forever.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/kern/kern_crashme.c

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



CVS commit: src/sys/kern

2022-08-15 Thread Taylor R Campbell
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 
-__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 
+
+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(, kernel_lock_holder);
+ipi_wait();
 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
 }
 
 /*



CVS commit: src/sys/kern

2022-08-15 Thread Taylor R Campbell
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.



CVS commit: src/sys/kern

2022-08-15 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Aug 15 11:43:56 UTC 2022

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

Log Message:
workqueue(9): workqueue_wait and workqueue_destroy may sleep.

But might not, so assert sleepable up front.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/sys/kern/subr_workqueue.c

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



CVS commit: src/sys/kern

2022-08-15 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Aug 15 11:43:56 UTC 2022

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

Log Message:
workqueue(9): workqueue_wait and workqueue_destroy may sleep.

But might not, so assert sleepable up front.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/sys/kern/subr_workqueue.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/subr_workqueue.c
diff -u src/sys/kern/subr_workqueue.c:1.39 src/sys/kern/subr_workqueue.c:1.40
--- src/sys/kern/subr_workqueue.c:1.39	Tue Sep  8 17:02:18 2020
+++ src/sys/kern/subr_workqueue.c	Mon Aug 15 11:43:56 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_workqueue.c,v 1.39 2020/09/08 17:02:18 riastradh Exp $	*/
+/*	$NetBSD: subr_workqueue.c,v 1.40 2022/08/15 11:43:56 riastradh Exp $	*/
 
 /*-
  * Copyright (c)2002, 2005, 2006, 2007 YAMAMOTO Takashi,
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_workqueue.c,v 1.39 2020/09/08 17:02:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_workqueue.c,v 1.40 2022/08/15 11:43:56 riastradh Exp $");
 
 #include 
 #include 
@@ -324,6 +324,8 @@ workqueue_wait(struct workqueue *wq, str
 	struct workqueue_queue *q;
 	bool found;
 
+	ASSERT_SLEEPABLE();
+
 	if (ISSET(wq->wq_flags, WQ_PERCPU)) {
 		struct cpu_info *ci;
 		CPU_INFO_ITERATOR cii;
@@ -346,6 +348,8 @@ workqueue_destroy(struct workqueue *wq)
 	struct cpu_info *ci;
 	CPU_INFO_ITERATOR cii;
 
+	ASSERT_SLEEPABLE();
+
 	wq->wq_func = workqueue_exit;
 	for (CPU_INFO_FOREACH(cii, ci)) {
 		q = workqueue_queue_lookup(wq, ci);



CVS commit: src/sys/kern

2022-08-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Aug 12 16:16:12 UTC 2022

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

Log Message:
autoconf(9): Provide diagnostics for config_detach_* misuse.


To generate a diff of this commit:
cvs rdiff -u -r1.301 -r1.302 src/sys/kern/subr_autoconf.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/subr_autoconf.c
diff -u src/sys/kern/subr_autoconf.c:1.301 src/sys/kern/subr_autoconf.c:1.302
--- src/sys/kern/subr_autoconf.c:1.301	Mon Mar 28 12:38:59 2022
+++ src/sys/kern/subr_autoconf.c	Fri Aug 12 16:16:12 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.301 2022/03/28 12:38:59 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.302 2022/08/12 16:16:12 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.301 2022/03/28 12:38:59 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.302 2022/08/12 16:16:12 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1919,6 +1919,7 @@ config_dump_garbage(struct devicelist *g
 static int
 config_detach_enter(device_t dev)
 {
+	struct lwp *l __diagused;
 	int error = 0;
 
 	mutex_enter(_misc_lock);
@@ -1947,8 +1948,12 @@ config_detach_enter(device_t dev)
 	 * running.  Claim the device for detaching.  This will cause
 	 * all new attempts to acquire references to block.
 	 */
-	KASSERT(dev->dv_attaching == NULL);
-	KASSERT(dev->dv_detaching == NULL);
+	KASSERTMSG((l = dev->dv_attaching) == NULL,
+	"lwp %ld [%s] @ %p attaching",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
+	KASSERTMSG((l = dev->dv_detaching) == NULL,
+	"lwp %ld [%s] @ %p detaching",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
 	dev->dv_detaching = curlwp;
 
 out:	mutex_exit(_misc_lock);
@@ -1958,9 +1963,12 @@ out:	mutex_exit(_misc_lock);
 static void
 config_detach_exit(device_t dev)
 {
+	struct lwp *l __diagused;
 
 	mutex_enter(_misc_lock);
-	KASSERT(dev->dv_detaching == curlwp);
+	KASSERTMSG((l = dev->dv_detaching) == curlwp,
+	"lwp %ld [%s] @ %p detaching",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
 	dev->dv_detaching = NULL;
 	cv_broadcast(_misc_cv);
 	mutex_exit(_misc_lock);
@@ -2171,9 +2179,12 @@ out:
 void
 config_detach_commit(device_t dev)
 {
+	struct lwp *l __diagused;
 
 	mutex_enter(_misc_lock);
-	KASSERT(dev->dv_detaching == curlwp);
+	KASSERTMSG((l = dev->dv_detaching) == curlwp,
+	"lwp %ld [%s] @ %p detaching",
+	(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l);
 	dev->dv_detached = true;
 	cv_broadcast(_misc_cv);
 	mutex_exit(_misc_lock);



CVS commit: src/sys/kern

2022-08-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Aug 12 16:16:12 UTC 2022

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

Log Message:
autoconf(9): Provide diagnostics for config_detach_* misuse.


To generate a diff of this commit:
cvs rdiff -u -r1.301 -r1.302 src/sys/kern/subr_autoconf.c

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



CVS commit: src/sys/kern

2022-08-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Aug 12 15:17:10 UTC 2022

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

Log Message:
module(9): Don't autounload unaudited modules by default.

Modules that have not been audited for autounload safety don't
recognize the command MODULE_CMD_AUTOUNLOAD and return ENOTTY.  These
modules are not safe to autounload, so don't autounload them.  Since
unload is risky business (if not careful, can lead to use-after-free,
kernel memory corruption, ), it needs to be opt-in by default, not
opt-out.

Modules that have been audited can return 0 or EBUSY to explicitly
allow or deny autounload.  Users who want to live on the edge to try
to exercise module autounload even for unaudited modules -- and are
willing to accept the consequences, and maybe contribute to auditing!
-- can set the new sysctl knob kern.module.autounload_unsafe=1.

Discussed on tech-kern:
https://mail-index.netbsd.org/tech-kern/2022/08/08/msg028282.html


To generate a diff of this commit:
cvs rdiff -u -r1.157 -r1.158 src/sys/kern/kern_module.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_module.c
diff -u src/sys/kern/kern_module.c:1.157 src/sys/kern/kern_module.c:1.158
--- src/sys/kern/kern_module.c:1.157	Sun Aug  7 23:03:59 2022
+++ src/sys/kern/kern_module.c	Fri Aug 12 15:17:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_module.c,v 1.157 2022/08/07 23:03:59 riastradh Exp $	*/
+/*	$NetBSD: kern_module.c,v 1.158 2022/08/12 15:17:10 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.157 2022/08/07 23:03:59 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.158 2022/08/12 15:17:10 riastradh Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -94,6 +94,7 @@ bool		module_autoload_on = true;
 #else
 bool		module_autoload_on = false;
 #endif
+bool		module_autounload_unsafe = 0;
 u_int		module_count;
 u_int		module_builtinlist;
 u_int		module_autotime = 10;
@@ -546,6 +547,12 @@ sysctl_module_setup(void)
 		CTL_CREATE, CTL_EOL);
 	sysctl_createv(_sysctllog, 0, , NULL,
 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
+		CTLTYPE_BOOL, "autounload_unsafe",
+		SYSCTL_DESCR("Enable automatic unload of unaudited modules"),
+		NULL, 0, _autounload_unsafe, 0,
+		CTL_CREATE, CTL_EOL);
+	sysctl_createv(_sysctllog, 0, , NULL,
+		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
 		CTLTYPE_BOOL, "verbose",
 		SYSCTL_DESCR("Enable verbose output"),
 		NULL, 0, _verbose_on, 0,
@@ -1672,14 +1679,28 @@ module_thread(void *cookie)
 			}
 
 			/*
-			 * If this module wants to avoid autounload then
-			 * skip it.  Some modules can ping-pong in and out
-			 * because their use is transient but often. 
-			 * Example: exec_script.
+			 * Ask the module if it can be safely unloaded.
+			 *
+			 * - Modules which have been audited to be OK
+			 *   with that will return 0.
+			 *
+			 * - Modules which have not been audited for
+			 *   safe autounload will return ENOTTY.
+			 *
+			 *   => With kern.module.autounload_unsafe=1,
+			 *  we treat ENOTTY as acceptance.
+			 *
+			 * - Some modules would ping-ping in and out
+			 *   because their use is transient but often.
+			 *   Example: exec_script.  Other modules may
+			 *   still be in use.  These modules can
+			 *   prevent autounload in all cases by
+			 *   returning EBUSY or some other error code.
 			 */
 			mi = mod->mod_info;
 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
-			if (error == 0 || error == ENOTTY) {
+			if (error == 0 ||
+			(error == ENOTTY && module_autounload_unsafe)) {
 (void)module_do_unload(mi->mi_name, false);
 			} else
 module_print("module `%s' declined to be "



CVS commit: src/sys/kern

2022-08-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Aug 12 15:17:10 UTC 2022

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

Log Message:
module(9): Don't autounload unaudited modules by default.

Modules that have not been audited for autounload safety don't
recognize the command MODULE_CMD_AUTOUNLOAD and return ENOTTY.  These
modules are not safe to autounload, so don't autounload them.  Since
unload is risky business (if not careful, can lead to use-after-free,
kernel memory corruption, ), it needs to be opt-in by default, not
opt-out.

Modules that have been audited can return 0 or EBUSY to explicitly
allow or deny autounload.  Users who want to live on the edge to try
to exercise module autounload even for unaudited modules -- and are
willing to accept the consequences, and maybe contribute to auditing!
-- can set the new sysctl knob kern.module.autounload_unsafe=1.

Discussed on tech-kern:
https://mail-index.netbsd.org/tech-kern/2022/08/08/msg028282.html


To generate a diff of this commit:
cvs rdiff -u -r1.157 -r1.158 src/sys/kern/kern_module.c

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



CVS commit: src/sys/kern

2022-08-11 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Thu Aug 11 10:17:44 UTC 2022

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

Log Message:
Finish previous, evaluate the lowest mount on first access
to "struct mount_info" and store it here so we no longer
derefence the "struct mount" from fstrans_alloc_lwp_info().

Reported-by: syzbot+5a79214d043395b55...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.67 src/sys/kern/vfs_trans.c

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



CVS commit: src/sys/kern

2022-08-11 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Thu Aug 11 10:17:44 UTC 2022

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

Log Message:
Finish previous, evaluate the lowest mount on first access
to "struct mount_info" and store it here so we no longer
derefence the "struct mount" from fstrans_alloc_lwp_info().

Reported-by: syzbot+5a79214d043395b55...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.67 src/sys/kern/vfs_trans.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/vfs_trans.c
diff -u src/sys/kern/vfs_trans.c:1.66 src/sys/kern/vfs_trans.c:1.67
--- src/sys/kern/vfs_trans.c:1.66	Fri Jul  8 07:42:47 2022
+++ src/sys/kern/vfs_trans.c	Thu Aug 11 10:17:44 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_trans.c,v 1.66 2022/07/08 07:42:47 hannken Exp $	*/
+/*	$NetBSD: vfs_trans.c,v 1.67 2022/08/11 10:17:44 hannken Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.66 2022/07/08 07:42:47 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.67 2022/08/11 10:17:44 hannken Exp $");
 
 /*
  * File system transaction operations.
@@ -87,6 +87,7 @@ struct fstrans_mount_info {
 	SLIST_ENTRY(fstrans_mount_info) fmi_hash;
 	LIST_HEAD(, fscow_handler) fmi_cow_handler;
 	struct mount *fmi_mount;
+	struct fstrans_mount_info *fmi_lower_info;
 	struct lwp *fmi_owner;
 };
 SLIST_HEAD(fstrans_mount_hashhead, fstrans_mount_info);
@@ -229,13 +230,29 @@ static inline struct fstrans_mount_info 
 fstrans_mount_get(struct mount *mp)
 {
 	uint32_t indx;
-	struct fstrans_mount_info *fmi;
+	struct fstrans_mount_info *fmi, *fmi_lower;
 
 	KASSERT(mutex_owned(_lock));
 
 	indx = fstrans_mount_hash(mp);
 	SLIST_FOREACH(fmi, _mount_hashtab[indx], fmi_hash) {
 		if (fmi->fmi_mount == mp) {
+			if (__predict_false(mp->mnt_lower != NULL &&
+			fmi->fmi_lower_info == NULL)) {
+/*
+ * Intern the lower/lowest mount into
+ * this mount info on first lookup.
+ */
+KASSERT(fmi->fmi_ref_cnt == 1);
+
+fmi_lower = fstrans_mount_get(mp->mnt_lower);
+if (fmi_lower && fmi_lower->fmi_lower_info)
+	fmi_lower = fmi_lower->fmi_lower_info;
+if (fmi_lower == NULL)
+	return NULL;
+fmi->fmi_lower_info = fmi_lower;
+fmi->fmi_lower_info->fmi_ref_cnt += 1;
+			}
 			return fmi;
 		}
 	}
@@ -262,6 +279,9 @@ fstrans_mount_dtor(struct fstrans_mount_
 	KASSERT(LIST_FIRST(>fmi_cow_handler) == NULL);
 	KASSERT(fmi->fmi_owner == NULL);
 
+	if (fmi->fmi_lower_info)
+		fstrans_mount_dtor(fmi->fmi_lower_info);
+
 	KASSERT(fstrans_gone_count > 0);
 	fstrans_gone_count -= 1;
 
@@ -287,6 +307,7 @@ fstrans_mount(struct mount *mp)
 	LIST_INIT(>fmi_cow_handler);
 	newfmi->fmi_cow_change = false;
 	newfmi->fmi_mount = mp;
+	newfmi->fmi_lower_info = NULL;
 	newfmi->fmi_owner = NULL;
 
 	mutex_enter(_lock);
@@ -374,7 +395,7 @@ fstrans_clear_lwp_info(void)
 static struct fstrans_lwp_info *
 fstrans_alloc_lwp_info(struct mount *mp)
 {
-	struct fstrans_lwp_info *fli;
+	struct fstrans_lwp_info *fli, *fli_lower;
 	struct fstrans_mount_info *fmi;
 
 	for (fli = curlwp->l_fstrans; fli; fli = fli->fli_succ) {
@@ -383,6 +404,32 @@ fstrans_alloc_lwp_info(struct mount *mp)
 	}
 
 	/*
+	 * Lookup mount info and get lower mount per lwp info.
+	 */
+	mutex_enter(_lock);
+	fmi = fstrans_mount_get(mp);
+	if (fmi == NULL) {
+		mutex_exit(_lock);
+		return NULL;
+	}
+	fmi->fmi_ref_cnt += 1;
+	mutex_exit(_lock);
+
+	if (fmi->fmi_lower_info) {
+		fli_lower =
+		fstrans_alloc_lwp_info(fmi->fmi_lower_info->fmi_mount);
+		if (fli_lower == NULL) {
+			mutex_enter(_lock);
+			fstrans_mount_dtor(fmi);
+			mutex_exit(_lock);
+
+			return NULL;
+		}
+	} else {
+		fli_lower = NULL;
+	}
+
+	/*
 	 * Allocate a new entry.
 	 */
 	fli = pool_cache_get(fstrans_lwp_cache, PR_WAITOK);
@@ -395,30 +442,18 @@ fstrans_alloc_lwp_info(struct mount *mp)
 	KASSERT(fli->fli_self == NULL);
 
 	/*
-	 * Attach the mount info if it is valid.
+	 * Attach the mount info and alias.
 	 */
 
-	mutex_enter(_lock);
-	fmi = fstrans_mount_get(mp);
-	if (fmi == NULL) {
-		mutex_exit(_lock);
-		pool_cache_put(fstrans_lwp_cache, fli);
-		return NULL;
-	}
 	fli->fli_self = curlwp;
 	fli->fli_mount = mp;
 	fli->fli_mountinfo = fmi;
-	fmi->fmi_ref_cnt += 1;
-	do {
-		mp = mp->mnt_lower;
-	} while (mp && mp->mnt_lower);
-	mutex_exit(_lock);
 
 	fli->fli_succ = curlwp->l_fstrans;
 	curlwp->l_fstrans = fli;
 
-	if (mp) {
-		fli->fli_alias = fstrans_alloc_lwp_info(mp);
+	if (fli_lower) {
+		fli->fli_alias = fli_lower;
 		fli->fli_alias->fli_alias_cnt++;
 		fli = fli->fli_alias;
 	}
@@ -499,9 +534,6 @@ _fstrans_start(struct mount *mp, enum fs
 
 	s = pserialize_read_enter();
 	if (__predict_true(grant_lock(fmi, lock_type))) {
-
-		KASSERT(!fmi->fmi_gone);
-
 		fli->fli_trans_cnt = 1;
 		fli->fli_lock_type = lock_type;
 		

CVS commit: src/sys/kern

2022-08-08 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Aug  8 22:31:45 UTC 2022

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

Log Message:
rasctl(2): Avoid arithmetic overflow.

Reported-by: syzbot+40b6085d020ae47a6...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=8fb9b5dee9b056e4f8ad3b937dc5be1296608c5f


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/sys/kern/kern_ras.c

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



CVS commit: src/sys/kern

2022-08-08 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Aug  8 22:31:45 UTC 2022

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

Log Message:
rasctl(2): Avoid arithmetic overflow.

Reported-by: syzbot+40b6085d020ae47a6...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=8fb9b5dee9b056e4f8ad3b937dc5be1296608c5f


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/sys/kern/kern_ras.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_ras.c
diff -u src/sys/kern/kern_ras.c:1.41 src/sys/kern/kern_ras.c:1.42
--- src/sys/kern/kern_ras.c:1.41	Wed Aug  3 09:40:25 2022
+++ src/sys/kern/kern_ras.c	Mon Aug  8 22:31:45 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ras.c,v 1.41 2022/08/03 09:40:25 riastradh Exp $	*/
+/*	$NetBSD: kern_ras.c,v 1.42 2022/08/08 22:31:45 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.41 2022/08/03 09:40:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.42 2022/08/08 22:31:45 riastradh Exp $");
 
 #include 
 #include 
@@ -218,16 +218,15 @@ static int
 ras_purge(void *addr, size_t len)
 {
 	struct ras *rp, **link;
-	void *endaddr;
 	proc_t *p;
 
-	endaddr = (char *)addr + len;
 	p = curproc;
 
 	mutex_enter(>p_auxlock);
 	link = >p_raslist;
 	for (rp = *link; rp != NULL; link = >ras_next, rp = *link) {
-		if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr)
+		if (addr == rp->ras_startaddr &&
+		(char *)rp->ras_endaddr - (char *)rp->ras_startaddr == len)
 			break;
 	}
 	if (rp != NULL) {



Re: CVS commit: src/sys/kern

2022-08-07 Thread Taylor R Campbell
Reverted and alternative proposed on tech-kern!  Sorry for the
unilateral toe-stomping.


CVS commit: src/sys/kern

2022-08-07 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 23:03:59 UTC 2022

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

Log Message:
Revert "module(9): Disable module autounload by default."

Needs more discussion.  Maybe better with an opt-in mechanism to
replace the opt-out mechanism than by disabling it altogether by
default.


To generate a diff of this commit:
cvs rdiff -u -r1.156 -r1.157 src/sys/kern/kern_module.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_module.c
diff -u src/sys/kern/kern_module.c:1.156 src/sys/kern/kern_module.c:1.157
--- src/sys/kern/kern_module.c:1.156	Sun Aug  7 21:17:18 2022
+++ src/sys/kern/kern_module.c	Sun Aug  7 23:03:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_module.c,v 1.156 2022/08/07 21:17:18 riastradh Exp $	*/
+/*	$NetBSD: kern_module.c,v 1.157 2022/08/07 23:03:59 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.156 2022/08/07 21:17:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.157 2022/08/07 23:03:59 riastradh Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -96,7 +96,7 @@ bool		module_autoload_on = false;
 #endif
 u_int		module_count;
 u_int		module_builtinlist;
-u_int		module_autotime = 0;
+u_int		module_autotime = 10;
 u_int		module_gen = 1;
 static kcondvar_t module_thread_cv;
 static kmutex_t module_thread_lock;



CVS commit: src/sys/kern

2022-08-07 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 23:03:59 UTC 2022

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

Log Message:
Revert "module(9): Disable module autounload by default."

Needs more discussion.  Maybe better with an opt-in mechanism to
replace the opt-out mechanism than by disabling it altogether by
default.


To generate a diff of this commit:
cvs rdiff -u -r1.156 -r1.157 src/sys/kern/kern_module.c

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



Re: CVS commit: src/sys/kern

2022-08-07 Thread Paul Goyette

On Sun, 7 Aug 2022, Paul Goyette wrote:


On Sun, 7 Aug 2022, Taylor R Campbell wrote:


Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 21:17:18 UTC 2022

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

Log Message:
module(9): Disable module autounload by default.

I don't know why this was ever enabled by default; many modules are
still not safe to unload, let alone autounload.  If any autounload is
to happen by default, it should only be for modules that have opted
into it in some way after audit.


One reason for the current behavior involves the modules used for
various emulations.  When a file is executed, and none of the
currently-loaded modules can "deal" with it, we load _all_ of the
available emulation modules with the hope that one of them will
"deal with" the new executable, and with the expectation that the
remaining emulation modules will just "go away".

Modules that are known to be unsafe to unload should declare that
in their modcmd() unload (by returning EBUSY).  After all, one
might well expect that the module itself is the most likely place
that the unloadable status would be known.

Making no-autounload as the default seems like using a 20-pound
sledge hammer on a carpet tack.


I might also note that making such a fundamental behavior change
when we're so close to the -10 release, without actually providing
the suggested "opt-in" mechanism to retain current behavior, is
not very user friendly.   :-)

At least this should be discussed (on tech-kern@, perhaps) before
being unilaterally decided and committed.  IIRC, we had this
discussion some time ago, and the decision at that time was to
retain current behavior.  Unfortunately, I didn't save any pointer
to that old discussion.  :-(




++--+--+
| Paul Goyette   | PGP Key fingerprint: | E-mail addresses:|
| (Retired)  | FA29 0E3B 35AF E8AE 6651 | p...@whooppee.com|
| Software Developer | 0786 F758 55DE 53BA 7731 | pgoye...@netbsd.org  |
| & Network Engineer |  | pgoyett...@gmail.com |
++--+--+

!DSPAM:62f0321a29502088639869!




++--+--+
| Paul Goyette   | PGP Key fingerprint: | E-mail addresses:|
| (Retired)  | FA29 0E3B 35AF E8AE 6651 | p...@whooppee.com|
| Software Developer | 0786 F758 55DE 53BA 7731 | pgoye...@netbsd.org  |
| & Network Engineer |  | pgoyett...@gmail.com |
++--+--+


Re: CVS commit: src/sys/kern

2022-08-07 Thread Paul Goyette

On Sun, 7 Aug 2022, Taylor R Campbell wrote:


Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 21:17:18 UTC 2022

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

Log Message:
module(9): Disable module autounload by default.

I don't know why this was ever enabled by default; many modules are
still not safe to unload, let alone autounload.  If any autounload is
to happen by default, it should only be for modules that have opted
into it in some way after audit.


One reason for the current behavior involves the modules used for
various emulations.  When a file is executed, and none of the
currently-loaded modules can "deal" with it, we load _all_ of the
available emulation modules with the hope that one of them will
"deal with" the new executable, and with the expectation that the
remaining emulation modules will just "go away".

Modules that are known to be unsafe to unload should declare that
in their modcmd() unload (by returning EBUSY).  After all, one
might well expect that the module itself is the most likely place
that the unloadable status would be known.

Making no-autounload as the default seems like using a 20-pound
sledge hammer on a carpet tack.


++--+--+
| Paul Goyette   | PGP Key fingerprint: | E-mail addresses:|
| (Retired)  | FA29 0E3B 35AF E8AE 6651 | p...@whooppee.com|
| Software Developer | 0786 F758 55DE 53BA 7731 | pgoye...@netbsd.org  |
| & Network Engineer |  | pgoyett...@gmail.com |
++--+--+


CVS commit: src/sys/kern

2022-08-07 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 21:17:18 UTC 2022

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

Log Message:
module(9): Disable module autounload by default.

I don't know why this was ever enabled by default; many modules are
still not safe to unload, let alone autounload.  If any autounload is
to happen by default, it should only be for modules that have opted
into it in some way after audit.


To generate a diff of this commit:
cvs rdiff -u -r1.155 -r1.156 src/sys/kern/kern_module.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_module.c
diff -u src/sys/kern/kern_module.c:1.155 src/sys/kern/kern_module.c:1.156
--- src/sys/kern/kern_module.c:1.155	Sun Aug  7 20:44:39 2022
+++ src/sys/kern/kern_module.c	Sun Aug  7 21:17:18 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_module.c,v 1.155 2022/08/07 20:44:39 riastradh Exp $	*/
+/*	$NetBSD: kern_module.c,v 1.156 2022/08/07 21:17:18 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.155 2022/08/07 20:44:39 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.156 2022/08/07 21:17:18 riastradh Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -96,7 +96,7 @@ bool		module_autoload_on = false;
 #endif
 u_int		module_count;
 u_int		module_builtinlist;
-u_int		module_autotime = 10;
+u_int		module_autotime = 0;
 u_int		module_gen = 1;
 static kcondvar_t module_thread_cv;
 static kmutex_t module_thread_lock;



CVS commit: src/sys/kern

2022-08-07 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 21:17:18 UTC 2022

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

Log Message:
module(9): Disable module autounload by default.

I don't know why this was ever enabled by default; many modules are
still not safe to unload, let alone autounload.  If any autounload is
to happen by default, it should only be for modules that have opted
into it in some way after audit.


To generate a diff of this commit:
cvs rdiff -u -r1.155 -r1.156 src/sys/kern/kern_module.c

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



CVS commit: src/sys/kern

2022-08-07 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 20:44:39 UTC 2022

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

Log Message:
module(9): Call callbacks in topological order on load.

They are called in reverse topological order on unload.

dtrace_sdt will rely on this soon to ensure provider definitions are
processed before their uses.

ok chs@


To generate a diff of this commit:
cvs rdiff -u -r1.154 -r1.155 src/sys/kern/kern_module.c

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



CVS commit: src/sys/kern

2022-08-07 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug  7 20:44:39 UTC 2022

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

Log Message:
module(9): Call callbacks in topological order on load.

They are called in reverse topological order on unload.

dtrace_sdt will rely on this soon to ensure provider definitions are
processed before their uses.

ok chs@


To generate a diff of this commit:
cvs rdiff -u -r1.154 -r1.155 src/sys/kern/kern_module.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_module.c
diff -u src/sys/kern/kern_module.c:1.154 src/sys/kern/kern_module.c:1.155
--- src/sys/kern/kern_module.c:1.154	Sat May  7 04:30:41 2022
+++ src/sys/kern/kern_module.c	Sun Aug  7 20:44:39 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_module.c,v 1.154 2022/05/07 04:30:41 rin Exp $	*/
+/*	$NetBSD: kern_module.c,v 1.155 2022/08/07 20:44:39 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.154 2022/05/07 04:30:41 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.155 2022/08/07 20:44:39 riastradh Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -1889,7 +1889,7 @@ module_register_callbacks(void (*load)(s
 
 	kernconfig_lock();
 	TAILQ_INSERT_TAIL(, modcb, modcb_list);
-	TAILQ_FOREACH(mod, _list, mod_chain)
+	TAILQ_FOREACH_REVERSE(mod, _list, modlist, mod_chain)
 		load(mod);
 	kernconfig_unlock();
 



CVS commit: src/sys/kern

2022-08-06 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Aug  6 21:21:10 UTC 2022

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

Log Message:
vnodeops(9): Take exclusive lock in read/seek for f_offset update.

Otherwise concurrent readers/seekers might clobber it.


To generate a diff of this commit:
cvs rdiff -u -r1.234 -r1.235 src/sys/kern/vfs_vnops.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/vfs_vnops.c
diff -u src/sys/kern/vfs_vnops.c:1.234 src/sys/kern/vfs_vnops.c:1.235
--- src/sys/kern/vfs_vnops.c:1.234	Mon Jul 18 04:30:30 2022
+++ src/sys/kern/vfs_vnops.c	Sat Aug  6 21:21:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.234 2022/07/18 04:30:30 thorpej Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.235 2022/08/06 21:21:10 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.234 2022/07/18 04:30:30 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.235 2022/08/06 21:21:10 riastradh Exp $");
 
 #include "veriexec.h"
 
@@ -647,7 +647,10 @@ vn_read(file_t *fp, off_t *offset, struc
 		ioflag |= IO_ALTSEMANTICS;
 	if (fflag & FDIRECT)
 		ioflag |= IO_DIRECT;
-	vn_lock(vp, LK_SHARED | LK_RETRY);
+	if (offset == >f_offset && (flags & FOF_UPDATE_OFFSET) != 0)
+		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
+	else
+		vn_lock(vp, LK_SHARED | LK_RETRY);
 	uio->uio_offset = *offset;
 	count = uio->uio_resid;
 	error = VOP_READ(vp, uio, ioflag, cred);
@@ -1135,7 +1138,10 @@ vn_seek(struct file *fp, off_t delta, in
 	if (vp->v_type == VFIFO)
 		return ESPIPE;
 
-	vn_lock(vp, LK_SHARED | LK_RETRY);
+	if (flags & FOF_UPDATE_OFFSET)
+		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
+	else
+		vn_lock(vp, LK_SHARED | LK_RETRY);
 
 	/* Compute the old and new offsets.  */
 	oldoff = fp->f_offset;



CVS commit: src/sys/kern

2022-08-06 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Aug  6 21:21:10 UTC 2022

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

Log Message:
vnodeops(9): Take exclusive lock in read/seek for f_offset update.

Otherwise concurrent readers/seekers might clobber it.


To generate a diff of this commit:
cvs rdiff -u -r1.234 -r1.235 src/sys/kern/vfs_vnops.c

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



CVS commit: src/sys/kern

2022-08-05 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Aug  5 23:43:46 UTC 2022

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

Log Message:
entropy: Don't disclose stack garbage in kern.entropy sysctls.

kern.entropy.consolidate and kern.entropy.gather are supposed to be
write-only -- it doesn't make any sense to read from them, but I
suppose it's better to read-as-zero than read-as-stack-secrets!


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/sys/kern/kern_entropy.c

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



CVS commit: src/sys/kern

2022-08-05 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Aug  5 23:43:46 UTC 2022

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

Log Message:
entropy: Don't disclose stack garbage in kern.entropy sysctls.

kern.entropy.consolidate and kern.entropy.gather are supposed to be
write-only -- it doesn't make any sense to read from them, but I
suppose it's better to read-as-zero than read-as-stack-secrets!


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/sys/kern/kern_entropy.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_entropy.c
diff -u src/sys/kern/kern_entropy.c:1.56 src/sys/kern/kern_entropy.c:1.57
--- src/sys/kern/kern_entropy.c:1.56	Fri May 13 09:40:02 2022
+++ src/sys/kern/kern_entropy.c	Fri Aug  5 23:43:46 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_entropy.c,v 1.56 2022/05/13 09:40:02 riastradh Exp $	*/
+/*	$NetBSD: kern_entropy.c,v 1.57 2022/08/05 23:43:46 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.56 2022/05/13 09:40:02 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.57 2022/08/05 23:43:46 riastradh Exp $");
 
 #include 
 #include 
@@ -1291,7 +1291,7 @@ static int
 sysctl_entropy_consolidate(SYSCTLFN_ARGS)
 {
 	struct sysctlnode node = *rnode;
-	int arg;
+	int arg = 0;
 	int error;
 
 	KASSERT(E->stage == ENTROPY_HOT);
@@ -1317,7 +1317,7 @@ static int
 sysctl_entropy_gather(SYSCTLFN_ARGS)
 {
 	struct sysctlnode node = *rnode;
-	int arg;
+	int arg = 0;
 	int error;
 
 	KASSERT(E->stage == ENTROPY_HOT);



CVS commit: src/sys/kern

2022-08-03 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Aug  3 11:09:13 UTC 2022

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

Log Message:
kern/vfs_lockf.c: Fix overflow in overflow detection.

Reported-by: syzbot+cda9440741a151674...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=030eb71324790093d467799263cd0789e5097229


To generate a diff of this commit:
cvs rdiff -u -r1.76 -r1.77 src/sys/kern/vfs_lockf.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/vfs_lockf.c
diff -u src/sys/kern/vfs_lockf.c:1.76 src/sys/kern/vfs_lockf.c:1.77
--- src/sys/kern/vfs_lockf.c:1.76	Fri Jul  1 01:04:01 2022
+++ src/sys/kern/vfs_lockf.c	Wed Aug  3 11:09:13 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_lockf.c,v 1.76 2022/07/01 01:04:01 riastradh Exp $	*/
+/*	$NetBSD: vfs_lockf.c,v 1.77 2022/08/03 11:09:13 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -35,7 +35,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_lockf.c,v 1.76 2022/07/01 01:04:01 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_lockf.c,v 1.77 2022/08/03 11:09:13 riastradh Exp $");
 
 #include 
 #include 
@@ -844,7 +844,8 @@ lf_advlock(struct vop_advlock_args *ap, 
 		end = -1;
 	else {
 		if (fl->l_len >= 0) {
-			if (fl->l_len - 1 > __type_max(off_t) - start)
+			if (start >= 0 &&
+			fl->l_len - 1 > __type_max(off_t) - start)
 return EINVAL;
 			end = start + fl->l_len - 1;
 		} else {



CVS commit: src/sys/kern

2022-08-03 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Aug  3 11:09:13 UTC 2022

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

Log Message:
kern/vfs_lockf.c: Fix overflow in overflow detection.

Reported-by: syzbot+cda9440741a151674...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=030eb71324790093d467799263cd0789e5097229


To generate a diff of this commit:
cvs rdiff -u -r1.76 -r1.77 src/sys/kern/vfs_lockf.c

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



CVS commit: src/sys/kern

2022-08-03 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Aug  3 09:40:25 UTC 2022

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

Log Message:
rasctl(2): Avoid overflow in address range arithmetic.

Remove various contortions to suppress warnings.  Rely on
-Wno-type-limits instead.

Reported-by: syzbot+8b0f1ced3fce82031...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=e9055200701cffd653d5b13491d85c34e07f06a3


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/sys/kern/kern_ras.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_ras.c
diff -u src/sys/kern/kern_ras.c:1.40 src/sys/kern/kern_ras.c:1.41
--- src/sys/kern/kern_ras.c:1.40	Sat Dec 14 16:58:25 2019
+++ src/sys/kern/kern_ras.c	Wed Aug  3 09:40:25 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ras.c,v 1.40 2019/12/14 16:58:25 riastradh Exp $	*/
+/*	$NetBSD: kern_ras.c,v 1.41 2022/08/03 09:40:25 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.40 2019/12/14 16:58:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.41 2022/08/03 09:40:25 riastradh Exp $");
 
 #include 
 #include 
@@ -156,14 +156,6 @@ ras_purgeall(void)
 
 #if defined(__HAVE_RAS)
 
-#if __GNUC_PREREQ__(4, 8)
-#define	__WARNING_PUSH_LESS_NULL_PTR	_Pragma("GCC diagnostic push") 	_Pragma("GCC diagnostic ignored \"-Wextra\"")
-#define	__WARNING_POP_LESS_NULL_PTR	_Pragma("GCC diagnostic pop")
-#else
-#define	__WARNING_PUSH_LESS_NULL_PTR
-#define	__WARNING_POP_LESS_NULL_PTR
-#endif
-
 /*
  * Install the new sequence.  If it already exists, return
  * an error.
@@ -180,17 +172,12 @@ ras_install(void *addr, size_t len)
 	if (len == 0)
 		return EINVAL;
 
-	endaddr = (char *)addr + len;
-
-	/* Do not warn about < NULL pointer comparison */
-	__WARNING_PUSH_LESS_NULL_PTR
-	if (addr < (void *)VM_MIN_ADDRESS || addr > (void *)VM_MAXUSER_ADDRESS)
-		return EINVAL;
-	if (endaddr > (void *)VM_MAXUSER_ADDRESS)
+	if ((uintptr_t)addr < VM_MIN_ADDRESS ||
+	(uintptr_t)addr > VM_MAXUSER_ADDRESS)
 		return EINVAL;
-	if (endaddr < addr)
+	if (len > VM_MAXUSER_ADDRESS - (uintptr_t)addr)
 		return EINVAL;
-	__WARNING_POP_LESS_NULL_PTR
+	endaddr = (char *)addr + len;
 
 	newrp = kmem_alloc(sizeof(*newrp), KM_SLEEP);
 	newrp->ras_startaddr = addr;



CVS commit: src/sys/kern

2022-08-03 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Aug  3 09:40:25 UTC 2022

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

Log Message:
rasctl(2): Avoid overflow in address range arithmetic.

Remove various contortions to suppress warnings.  Rely on
-Wno-type-limits instead.

Reported-by: syzbot+8b0f1ced3fce82031...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=e9055200701cffd653d5b13491d85c34e07f06a3


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/sys/kern/kern_ras.c

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



CVS commit: src/sys/kern

2022-07-24 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jul 24 19:23:44 UTC 2022

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

Log Message:
kern_event.c: Mark KASSERT-only static function as __diagused.

Otherwise clang objects with -Wunneeded-internal-declaration.


To generate a diff of this commit:
cvs rdiff -u -r1.145 -r1.146 src/sys/kern/kern_event.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_event.c
diff -u src/sys/kern/kern_event.c:1.145 src/sys/kern/kern_event.c:1.146
--- src/sys/kern/kern_event.c:1.145	Tue Jul 19 01:03:05 2022
+++ src/sys/kern/kern_event.c	Sun Jul 24 19:23:44 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.145 2022/07/19 01:03:05 thorpej Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.146 2022/07/24 19:23:44 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #endif /* _KERNEL_OPT */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.145 2022/07/19 01:03:05 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.146 2022/07/24 19:23:44 riastradh Exp $");
 
 #include 
 #include 
@@ -169,7 +169,7 @@ knote_foplock_exit(struct knote *kn)
 	mutex_exit(_TO_KIMPL(kn)->ki_foplock);
 }
 
-static inline bool
+static inline bool __diagused
 knote_foplock_owned(struct knote *kn)
 {
 	return mutex_owned(_TO_KIMPL(kn)->ki_foplock);



CVS commit: src/sys/kern

2022-07-24 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jul 24 19:23:44 UTC 2022

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

Log Message:
kern_event.c: Mark KASSERT-only static function as __diagused.

Otherwise clang objects with -Wunneeded-internal-declaration.


To generate a diff of this commit:
cvs rdiff -u -r1.145 -r1.146 src/sys/kern/kern_event.c

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



CVS commit: src/sys/kern

2022-07-21 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Thu Jul 21 14:07:16 UTC 2022

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

Log Message:
Removed unused opt_wapbl.h include.


To generate a diff of this commit:
cvs rdiff -u -r1.539 -r1.540 src/sys/kern/init_main.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/init_main.c
diff -u src/sys/kern/init_main.c:1.539 src/sys/kern/init_main.c:1.540
--- src/sys/kern/init_main.c:1.539	Sat Jun 18 22:11:01 2022
+++ src/sys/kern/init_main.c	Thu Jul 21 14:07:16 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: init_main.c,v 1.539 2022/06/18 22:11:01 andvar Exp $	*/
+/*	$NetBSD: init_main.c,v 1.540 2022/07/21 14:07:16 simonb Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -97,7 +97,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.539 2022/06/18 22:11:01 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.540 2022/07/21 14:07:16 simonb Exp $");
 
 #include "opt_cnmagic.h"
 #include "opt_ddb.h"
@@ -112,7 +112,6 @@ __KERNEL_RCSID(0, "$NetBSD: init_main.c,
 #include "opt_ktrace.h"
 #include "opt_pax.h"
 #include "opt_compat_netbsd.h"
-#include "opt_wapbl.h"
 #include "opt_ptrace.h"
 #include "opt_splash.h"
 #include "opt_kernhist.h"



CVS commit: src/sys/kern

2022-07-21 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Thu Jul 21 14:07:16 UTC 2022

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

Log Message:
Removed unused opt_wapbl.h include.


To generate a diff of this commit:
cvs rdiff -u -r1.539 -r1.540 src/sys/kern/init_main.c

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



CVS commit: src/sys/kern

2022-07-18 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Tue Jul 19 01:03:05 UTC 2022

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

Log Message:
Fix a problem whereby detaching a device that has open kevent
registrations can result in a UAF: When a device detaches, it
calls seldestroy(), which calls knote_fini(), and when that
returns, the softc that contained the selinfo and klist are freed.
However, any knotes that were registered still linger on with the
kq descriptor they're were associated with, and when the file
descriptors close, those knotes will be f_detach'd, which will
call into the driver instance that no longer exists.

Address this problem by adding a "foplock" mutex to the knote.
This foplock must be held when calling into filter_attach(),
filter_detach(), and filter_event() (XXX not filter_touch();
see code for details).  Now, in klist_fini(), for each knote
that is on the klist that's about to be blown away, acquire
the foplock, replace the knote's filterops with a do-nothing
stub, and release the foplock.

The end result is that:
==> The foplock ensures that calls into filter_*() will get EITHER
the real backing object's filterops OR the nop stubs.
==> Holing the foplock across the filter_*() calls ensures that
klist_fini() will not complete until there are no callers inside
the filterops that are about to be blown away.


To generate a diff of this commit:
cvs rdiff -u -r1.144 -r1.145 src/sys/kern/kern_event.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_event.c
diff -u src/sys/kern/kern_event.c:1.144 src/sys/kern/kern_event.c:1.145
--- src/sys/kern/kern_event.c:1.144	Tue Jul 19 00:46:00 2022
+++ src/sys/kern/kern_event.c	Tue Jul 19 01:03:05 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.144 2022/07/19 00:46:00 thorpej Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.145 2022/07/19 01:03:05 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #endif /* _KERNEL_OPT */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.144 2022/07/19 00:46:00 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.145 2022/07/19 01:03:05 thorpej Exp $");
 
 #include 
 #include 
@@ -131,6 +131,7 @@ static int	filt_usertouch(struct knote *
 struct knote_impl {
 	struct knote	ki_knote;
 	unsigned int	ki_influx;	/* q: in-flux counter */
+	kmutex_t	ki_foplock;	/* for kn_filterops */
 };
 
 #define	KIMPL_TO_KNOTE(kip)	(&(kip)->ki_knote)
@@ -142,6 +143,7 @@ knote_alloc(bool sleepok)
 	struct knote_impl *ki;
 
 	ki = kmem_zalloc(sizeof(*ki), sleepok ? KM_SLEEP : KM_NOSLEEP);
+	mutex_init(>ki_foplock, MUTEX_DEFAULT, IPL_NONE);
 
 	return KIMPL_TO_KNOTE(ki);
 }
@@ -151,9 +153,28 @@ knote_free(struct knote *kn)
 {
 	struct knote_impl *ki = KNOTE_TO_KIMPL(kn);
 
+	mutex_destroy(>ki_foplock);
 	kmem_free(ki, sizeof(*ki));
 }
 
+static inline void
+knote_foplock_enter(struct knote *kn)
+{
+	mutex_enter(_TO_KIMPL(kn)->ki_foplock);
+}
+
+static inline void
+knote_foplock_exit(struct knote *kn)
+{
+	mutex_exit(_TO_KIMPL(kn)->ki_foplock);
+}
+
+static inline bool
+knote_foplock_owned(struct knote *kn)
+{
+	return mutex_owned(_TO_KIMPL(kn)->ki_foplock);
+}
+
 static const struct fileops kqueueops = {
 	.fo_name = "kqueue",
 	.fo_read = (void *)enxio,
@@ -167,6 +188,31 @@ static const struct fileops kqueueops = 
 	.fo_restart = kqueue_restart,
 };
 
+static void
+filt_nopdetach(struct knote *kn __unused)
+{
+}
+
+static int
+filt_nopevent(struct knote *kn __unused, long hint __unused)
+{
+	return 0;
+}
+
+static const struct filterops nop_fd_filtops = {
+	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
+	.f_attach = NULL,
+	.f_detach = filt_nopdetach,
+	.f_event = filt_nopevent,
+};
+
+static const struct filterops nop_filtops = {
+	.f_flags = FILTEROP_MPSAFE,
+	.f_attach = NULL,
+	.f_detach = filt_nopdetach,
+	.f_event = filt_nopevent,
+};
+
 static const struct filterops kqread_filtops = {
 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
 	.f_attach = NULL,
@@ -263,18 +309,45 @@ static size_t		user_kfiltersz;		/* size 
  *
  *	kqueue_filter_lock
  *	-> kn_kq->kq_fdp->fd_lock
+ *	-> knote foplock (if taken)
  *	-> object lock (e.g., device driver lock, )
  *	-> kn_kq->kq_lock
  *
- * Locking rules:
+ * Locking rules.  ==> indicates the lock is acquired by the backing
+ * object, locks prior are acquired before calling filter ops:
+ *
+ *	f_attach: fdp->fd_lock -> knote foplock ->
+ *	  (maybe) KERNEL_LOCK ==> backing object lock
  *
- *	f_attach: fdp->fd_lock, KERNEL_LOCK
- *	f_detach: fdp->fd_lock, KERNEL_LOCK
- *	f_event(!NOTE_SUBMIT) via kevent: fdp->fd_lock, _no_ object lock
- *	f_event via knote: whatever caller guarantees
- *		Typically,	f_event(NOTE_SUBMIT) via knote: object lock
- *f_event(!NOTE_SUBMIT) via knote: nothing,
- *	acquires/releases object lock inside.
+ *	f_detach: fdp->fd_lock -> knote foplock ->
+ *	   (maybe) 

CVS commit: src/sys/kern

2022-07-18 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Tue Jul 19 01:03:05 UTC 2022

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

Log Message:
Fix a problem whereby detaching a device that has open kevent
registrations can result in a UAF: When a device detaches, it
calls seldestroy(), which calls knote_fini(), and when that
returns, the softc that contained the selinfo and klist are freed.
However, any knotes that were registered still linger on with the
kq descriptor they're were associated with, and when the file
descriptors close, those knotes will be f_detach'd, which will
call into the driver instance that no longer exists.

Address this problem by adding a "foplock" mutex to the knote.
This foplock must be held when calling into filter_attach(),
filter_detach(), and filter_event() (XXX not filter_touch();
see code for details).  Now, in klist_fini(), for each knote
that is on the klist that's about to be blown away, acquire
the foplock, replace the knote's filterops with a do-nothing
stub, and release the foplock.

The end result is that:
==> The foplock ensures that calls into filter_*() will get EITHER
the real backing object's filterops OR the nop stubs.
==> Holing the foplock across the filter_*() calls ensures that
klist_fini() will not complete until there are no callers inside
the filterops that are about to be blown away.


To generate a diff of this commit:
cvs rdiff -u -r1.144 -r1.145 src/sys/kern/kern_event.c

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



CVS commit: src/sys/kern

2022-07-16 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sat Jul 16 10:20:21 UTC 2022

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

Log Message:
Use 64-bit math to calculate pool sizes.  Fixes overflow errors for
pools larger than 4GB and gives the correct output for kernel pool pages
in "vmstat -s" output.


To generate a diff of this commit:
cvs rdiff -u -r1.284 -r1.285 src/sys/kern/subr_pool.c

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



CVS commit: src/sys/kern

2022-07-16 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sat Jul 16 10:20:21 UTC 2022

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

Log Message:
Use 64-bit math to calculate pool sizes.  Fixes overflow errors for
pools larger than 4GB and gives the correct output for kernel pool pages
in "vmstat -s" output.


To generate a diff of this commit:
cvs rdiff -u -r1.284 -r1.285 src/sys/kern/subr_pool.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/subr_pool.c
diff -u src/sys/kern/subr_pool.c:1.284 src/sys/kern/subr_pool.c:1.285
--- src/sys/kern/subr_pool.c:1.284	Sun May 29 10:47:40 2022
+++ src/sys/kern/subr_pool.c	Sat Jul 16 10:20:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_pool.c,v 1.284 2022/05/29 10:47:40 andvar Exp $	*/
+/*	$NetBSD: subr_pool.c,v 1.285 2022/07/16 10:20:21 simonb Exp $	*/
 
 /*
  * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008, 2010, 2014, 2015, 2018,
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.284 2022/05/29 10:47:40 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.285 2022/07/16 10:20:21 simonb Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1795,10 +1795,11 @@ pool_totalpages_locked(void)
 	uint64_t total = 0;
 
 	TAILQ_FOREACH(pp, _head, pr_poollist) {
-		uint64_t bytes = pp->pr_npages * pp->pr_alloc->pa_pagesz;
+		uint64_t bytes =
+		(uint64_t)pp->pr_npages * pp->pr_alloc->pa_pagesz;
 
 		if ((pp->pr_roflags & PR_RECURSIVE) != 0)
-			bytes -= (pp->pr_nout * pp->pr_size);
+			bytes -= ((uint64_t)pp->pr_nout * pp->pr_size);
 		total += bytes;
 	}
 



CVS commit: src/sys/kern

2022-07-15 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Fri Jul 15 06:40:24 UTC 2022

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

Log Message:
convert "ksyms: checking " into KSYMS_DEBUG over normal DEBUG.


To generate a diff of this commit:
cvs rdiff -u -r1.106 -r1.107 src/sys/kern/kern_ksyms.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_ksyms.c
diff -u src/sys/kern/kern_ksyms.c:1.106 src/sys/kern/kern_ksyms.c:1.107
--- src/sys/kern/kern_ksyms.c:1.106	Wed Jul  6 01:12:46 2022
+++ src/sys/kern/kern_ksyms.c	Fri Jul 15 06:40:24 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.106 2022/07/06 01:12:46 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.107 2022/07/15 06:40:24 mrg Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.106 2022/07/06 01:12:46 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.107 2022/07/15 06:40:24 mrg Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -556,7 +556,7 @@ ksyms_addsyms_elf(int symsize, void *sta
 		char *shstr = (uint8_t *)start +
 		shdr[ehdr->e_shstrndx].sh_offset;
 		for (i = 1; i < ehdr->e_shnum; i++) {
-#ifdef DEBUG
+#ifdef KSYMS_DEBUG
 			printf("ksyms: checking %s\n", [shdr[i].sh_name]);
 #endif
 			if (shdr[i].sh_type != SHT_PROGBITS)



CVS commit: src/sys/kern

2022-07-15 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Fri Jul 15 06:40:24 UTC 2022

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

Log Message:
convert "ksyms: checking " into KSYMS_DEBUG over normal DEBUG.


To generate a diff of this commit:
cvs rdiff -u -r1.106 -r1.107 src/sys/kern/kern_ksyms.c

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



CVS commit: src/sys/kern

2022-07-12 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Wed Jul 13 03:23:07 UTC 2022

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

Log Message:
Funnel knote alloc/free into a single pair of functions.  NFCI.


To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 src/sys/kern/kern_event.c

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



CVS commit: src/sys/kern

2022-07-12 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Wed Jul 13 03:23:07 UTC 2022

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

Log Message:
Funnel knote alloc/free into a single pair of functions.  NFCI.


To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 src/sys/kern/kern_event.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_event.c
diff -u src/sys/kern/kern_event.c:1.141 src/sys/kern/kern_event.c:1.142
--- src/sys/kern/kern_event.c:1.141	Tue May 24 20:50:19 2022
+++ src/sys/kern/kern_event.c	Wed Jul 13 03:23:07 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.141 2022/05/24 20:50:19 andvar Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.142 2022/07/13 03:23:07 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #endif /* _KERNEL_OPT */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.141 2022/05/24 20:50:19 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.142 2022/07/13 03:23:07 thorpej Exp $");
 
 #include 
 #include 
@@ -423,6 +423,22 @@ knote_detach_quiesce(struct knote *kn)
 	return false;
 }
 
+static inline struct knote *
+knote_alloc(bool sleepok)
+{
+	struct knote *kn;
+
+	kn = kmem_zalloc(sizeof(*kn), sleepok ? KM_SLEEP : KM_NOSLEEP);
+
+	return kn;
+}
+
+static inline void
+knote_free(struct knote *kn)
+{
+	kmem_free(kn, sizeof(*kn));
+}
+
 static int
 filter_attach(struct knote *kn)
 {
@@ -952,8 +968,8 @@ knote_proc_fork_track(struct proc *p1, s
 	struct knote *knchild, *kntrack;
 	int error = 0;
 
-	knchild = kmem_zalloc(sizeof(*knchild), KM_NOSLEEP);
-	kntrack = kmem_zalloc(sizeof(*knchild), KM_NOSLEEP);
+	knchild = knote_alloc(false);
+	kntrack = knote_alloc(false);
 	if (__predict_false(knchild == NULL || kntrack == NULL)) {
 		error = ENOMEM;
 		goto out;
@@ -1041,10 +1057,10 @@ knote_proc_fork_track(struct proc *p1, s
 
  out:
 	if (__predict_false(knchild != NULL)) {
-		kmem_free(knchild, sizeof(*knchild));
+		knote_free(knchild);
 	}
 	if (__predict_false(kntrack != NULL)) {
-		kmem_free(kntrack, sizeof(*kntrack));
+		knote_free(kntrack);
 	}
 	mutex_enter(p1->p_lock);
 	mutex_spin_enter(>kq_lock);
@@ -1756,14 +1772,14 @@ kqueue_register(struct kqueue *kq, struc
 	error = 0;
 	fd = 0;
 
-	newkn = kmem_zalloc(sizeof(*newkn), KM_SLEEP);
+	newkn = knote_alloc(true);
 
 	rw_enter(_filter_lock, RW_READER);
 	kfilter = kfilter_byfilter(kev->filter);
 	if (kfilter == NULL || kfilter->filtops == NULL) {
 		/* filter not found nor implemented */
 		rw_exit(_filter_lock);
-		kmem_free(newkn, sizeof(*newkn));
+		knote_free(newkn);
 		return (EINVAL);
 	}
 
@@ -1774,7 +1790,7 @@ kqueue_register(struct kqueue *kq, struc
 		if (kev->ident > INT_MAX
 		|| (fp = fd_getfile(fd = kev->ident)) == NULL) {
 			rw_exit(_filter_lock);
-			kmem_free(newkn, sizeof(*newkn));
+			knote_free(newkn);
 			return EBADF;
 		}
 		mutex_enter(>fd_lock);
@@ -1986,7 +2002,7 @@ kqueue_register(struct kqueue *kq, struc
  done:
 	rw_exit(_filter_lock);
 	if (newkn != NULL)
-		kmem_free(newkn, sizeof(*newkn));
+		knote_free(newkn);
 	if (fp != NULL)
 		fd_putfile(fd);
 	return (error);
@@ -2680,7 +2696,7 @@ again:
 	if (kn->kn_fop->f_flags & FILTEROP_ISFD)
 		fd_putfile(kn->kn_id);
 	atomic_dec_uint(>kn_kfilter->refcnt);
-	kmem_free(kn, sizeof(*kn));
+	knote_free(kn);
 }
 
 /*



CVS commit: src/sys/kern

2022-07-10 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jul 10 23:12:12 UTC 2022

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

Log Message:
readv(2), writev(2): Avoid arithmetic overflow in bounds check.


To generate a diff of this commit:
cvs rdiff -u -r1.133 -r1.134 src/sys/kern/sys_generic.c

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



  1   2   3   4   5   6   7   8   9   10   >