CVS commit: src/sys/kern

2021-08-14 Thread Ryo Shimizu
Module Name:src
Committed By:   ryo
Date:   Sat Aug 14 21:17:11 UTC 2021

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

Log Message:
fix rumpkernel build failure


To generate a diff of this commit:
cvs rdiff -u -r1.145 -r1.146 src/sys/kern/kern_clock.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_clock.c
diff -u src/sys/kern/kern_clock.c:1.145 src/sys/kern/kern_clock.c:1.146
--- src/sys/kern/kern_clock.c:1.145	Sat Aug 14 17:51:20 2021
+++ src/sys/kern/kern_clock.c	Sat Aug 14 21:17:11 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_clock.c,v 1.145 2021/08/14 17:51:20 ryo Exp $	*/
+/*	$NetBSD: kern_clock.c,v 1.146 2021/08/14 21:17:11 ryo Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.145 2021/08/14 17:51:20 ryo Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.146 2021/08/14 21:17:11 ryo Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -457,7 +457,7 @@ statclock(struct clockframe *frame)
 		/*
 		 * Kernel statistics are just like addupc_intr, only easier.
 		 */
-#ifdef MULTIPROCESSOR
+#if defined(MULTIPROCESSOR) && !defined(_RUMPKERNEL)
 		g = curcpu()->ci_gmon;
 		if (g != NULL &&
 		profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {



CVS commit: src/sys/kern

2021-08-12 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Thu Aug 12 19:15:15 UTC 2021

Modified Files:
src/sys/kern: vnode_if.sh

Log Message:
s/beween/between/


To generate a diff of this commit:
cvs rdiff -u -r1.70 -r1.71 src/sys/kern/vnode_if.sh

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/vnode_if.sh
diff -u src/sys/kern/vnode_if.sh:1.70 src/sys/kern/vnode_if.sh:1.71
--- src/sys/kern/vnode_if.sh:1.70	Sat May 16 18:31:50 2020
+++ src/sys/kern/vnode_if.sh	Thu Aug 12 19:15:15 2021
@@ -29,7 +29,7 @@ copyright="\
  * SUCH DAMAGE.
  */
 "
-SCRIPT_ID='$NetBSD: vnode_if.sh,v 1.70 2020/05/16 18:31:50 christos Exp $'
+SCRIPT_ID='$NetBSD: vnode_if.sh,v 1.71 2021/08/12 19:15:15 andvar Exp $'
 
 # Script to produce VFS front-end sugar.
 #
@@ -86,7 +86,7 @@ fi
 # Input to this parser is pre-processed slightly by sed
 # so this awk parser doesn't have to work so hard.  The
 # changes done by the sed pre-processing step are:
-#	insert a space beween * and pointer name
+#	insert a space between * and pointer name
 #	replace semicolons with spaces
 #
 sed_prep='s:\*\([^\*/]\):\* \1:g



CVS commit: src/sys/kern

2021-08-03 Thread Chuck Silvers
Module Name:src
Committed By:   chs
Date:   Tue Aug  3 20:27:08 UTC 2021

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

Log Message:
in sbsavetimestamp(), initialize struct timeval to 0 with memset() so that
the implicit padding is initialized.  this avoids later copying uninitialized
memory out to user space.  detected by KMSAN.


To generate a diff of this commit:
cvs rdiff -u -r1.294 -r1.295 src/sys/kern/uipc_socket.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/uipc_socket.c
diff -u src/sys/kern/uipc_socket.c:1.294 src/sys/kern/uipc_socket.c:1.295
--- src/sys/kern/uipc_socket.c:1.294	Fri Dec 11 03:00:09 2020
+++ src/sys/kern/uipc_socket.c	Tue Aug  3 20:27:08 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_socket.c,v 1.294 2020/12/11 03:00:09 thorpej Exp $	*/
+/*	$NetBSD: uipc_socket.c,v 1.295 2021/08/03 20:27:08 chs Exp $	*/
 
 /*
  * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -71,7 +71,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.294 2020/12/11 03:00:09 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.295 2021/08/03 20:27:08 chs Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -2432,6 +2432,7 @@ sbsavetimestamp(int opt, struct mbuf **m
 	struct timeval tv;
 	int error;
 
+	memset(, 0, sizeof(tv));
 	microtime();
 
 	MODULE_HOOK_CALL(uipc_socket_50_sbts_hook, (opt, ), enosys(), error);



CVS commit: src/sys/kern

2021-08-03 Thread Chuck Silvers
Module Name:src
Committed By:   chs
Date:   Tue Aug  3 20:25:43 UTC 2021

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

Log Message:
initialize wc_unused to 0, to avoid writing uninitialized memory to disk.
detected by KMSAN.


To generate a diff of this commit:
cvs rdiff -u -r1.108 -r1.109 src/sys/kern/vfs_wapbl.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_wapbl.c
diff -u src/sys/kern/vfs_wapbl.c:1.108 src/sys/kern/vfs_wapbl.c:1.109
--- src/sys/kern/vfs_wapbl.c:1.108	Sun Apr 12 17:02:52 2020
+++ src/sys/kern/vfs_wapbl.c	Tue Aug  3 20:25:43 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_wapbl.c,v 1.108 2020/04/12 17:02:52 jdolecek Exp $	*/
+/*	$NetBSD: vfs_wapbl.c,v 1.109 2021/08/03 20:25:43 chs Exp $	*/
 
 /*-
  * Copyright (c) 2003, 2008, 2009 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
 #define WAPBL_INTERNAL
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_wapbl.c,v 1.108 2020/04/12 17:02:52 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_wapbl.c,v 1.109 2021/08/03 20:25:43 chs Exp $");
 
 #include 
 #include 
@@ -2559,6 +2559,7 @@ wapbl_write_blocks(struct wapbl *wl, off
 		wc->wc_type = WAPBL_WC_BLOCKS;
 		wc->wc_len = blocklen;
 		wc->wc_blkcount = 0;
+		wc->wc_unused = 0;
 		while (bp && (wc->wc_blkcount < wl->wl_brperjblock)) {
 			/*
 			 * Make sure all the physical block numbers are up to
@@ -2647,6 +2648,7 @@ wapbl_write_revocations(struct wapbl *wl
 		wc->wc_type = WAPBL_WC_REVOCATIONS;
 		wc->wc_len = blocklen;
 		wc->wc_blkcount = 0;
+		wc->wc_unused = 0;
 		while (wd && (wc->wc_blkcount < wl->wl_brperjblock)) {
 			wc->wc_blocks[wc->wc_blkcount].wc_daddr =
 			wd->wd_blkno;



CVS commit: src/sys/kern

2021-07-25 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sun Jul 25 06:06:40 UTC 2021

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

Log Message:
If we're only doing a count-only kern.buf sysctl, just return the number
of active members in the pool cache (plus some slop) instead of looking
in all the free buffer list.  Should reduce CPU usage for "systat vm"
to << 1% especially for machines with a larger number of buffers.


To generate a diff of this commit:
cvs rdiff -u -r1.300 -r1.301 src/sys/kern/vfs_bio.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_bio.c
diff -u src/sys/kern/vfs_bio.c:1.300 src/sys/kern/vfs_bio.c:1.301
--- src/sys/kern/vfs_bio.c:1.300	Sat Jul 24 13:28:14 2021
+++ src/sys/kern/vfs_bio.c	Sun Jul 25 06:06:40 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_bio.c,v 1.300 2021/07/24 13:28:14 simonb Exp $	*/
+/*	$NetBSD: vfs_bio.c,v 1.301 2021/07/25 06:06:40 simonb Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2008, 2009, 2019, 2020 The NetBSD Foundation, Inc.
@@ -123,7 +123,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.300 2021/07/24 13:28:14 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.301 2021/07/25 06:06:40 simonb Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_bufcache.h"
@@ -1804,6 +1804,14 @@ sysctl_dobuf(SYSCTLFN_ARGS)
 	elem_size < 1 || elem_count < 0)
 		return (EINVAL);
 
+	if (oldp == NULL) {
+		/* count only, don't run through the buffer queues */
+		needed = pool_cache_nget(buf_cache) - pool_cache_nput(buf_cache);
+		*oldlenp = (needed + KERN_BUFSLOP) * elem_size;
+
+		return 0;
+	}
+
 	error = 0;
 	needed = 0;
 	sysctl_unlock();
@@ -1848,8 +1856,6 @@ sysctl_dobuf(SYSCTLFN_ARGS)
 	sysctl_relock();
 
 	*oldlenp = needed;
-	if (oldp == NULL)
-		*oldlenp += KERN_BUFSLOP * elem_size;
 
 	return (error);
 }



CVS commit: src/sys/kern

2021-07-24 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sat Jul 24 13:27:39 UTC 2021

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

Log Message:
Pad out the slop for kern.buf based on the passed in element size,
rather than a size of an unrelated struct.


To generate a diff of this commit:
cvs rdiff -u -r1.298 -r1.299 src/sys/kern/vfs_bio.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_bio.c
diff -u src/sys/kern/vfs_bio.c:1.298 src/sys/kern/vfs_bio.c:1.299
--- src/sys/kern/vfs_bio.c:1.298	Thu Apr  1 06:25:59 2021
+++ src/sys/kern/vfs_bio.c	Sat Jul 24 13:27:39 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_bio.c,v 1.298 2021/04/01 06:25:59 simonb Exp $	*/
+/*	$NetBSD: vfs_bio.c,v 1.299 2021/07/24 13:27:39 simonb Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2008, 2009, 2019, 2020 The NetBSD Foundation, Inc.
@@ -123,7 +123,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.298 2021/04/01 06:25:59 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.299 2021/07/24 13:27:39 simonb Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_bufcache.h"
@@ -1850,7 +1850,7 @@ sysctl_dobuf(SYSCTLFN_ARGS)
 
 	*oldlenp = needed;
 	if (oldp == NULL)
-		*oldlenp += KERN_BUFSLOP * sizeof(buf_t);
+		*oldlenp += KERN_BUFSLOP * elem_size;
 
 	return (error);
 }



CVS commit: src/sys/kern

2021-07-18 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Sun Jul 18 09:30:36 UTC 2021

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

Log Message:
Fix confusion arising from whether FOLLOW or NOFOLLOW is 0.

In vn_open, don't set and then throw away FOLLOW, and clarify the
comment about requesting FOLLOW/NOFOLLOW behavior.

Related to PR 56316.


To generate a diff of this commit:
cvs rdiff -u -r1.220 -r1.221 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.220 src/sys/kern/vfs_vnops.c:1.221
--- src/sys/kern/vfs_vnops.c:1.220	Thu Jul  1 15:53:20 2021
+++ src/sys/kern/vfs_vnops.c	Sun Jul 18 09:30:36 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.220 2021/07/01 15:53:20 martin Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.221 2021/07/18 09:30:36 dholland Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.220 2021/07/01 15:53:20 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.221 2021/07/18 09:30:36 dholland Exp $");
 
 #include "veriexec.h"
 
@@ -155,8 +155,9 @@ const struct fileops vnops = {
  * EOPNOTSUPP will be produced in the cases that would otherwise return
  * a file descriptor.
  *
- * Note that callers that want NOFOLLOW should pass O_NOFOLLOW in fmode,
- * not NOFOLLOW in nmode.
+ * Note that callers that want no-follow behavior should pass
+ * O_NOFOLLOW in fmode. Neither FOLLOW nor NOFOLLOW in nmode is
+ * honored.
  */
 int
 vn_open(struct vnode *at_dvp, struct pathbuf *pb,
@@ -179,7 +180,7 @@ vn_open(struct vnode *at_dvp, struct pat
 	if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
 		return EINVAL;
 
-	NDINIT(, LOOKUP, FOLLOW | nmode, pb);
+	NDINIT(, LOOKUP, nmode, pb);
 	if (at_dvp != NULL)
 		NDAT(, at_dvp);
 



CVS commit: src/sys/kern

2021-07-18 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Jul 18 06:57:28 UTC 2021

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

Log Message:
skip symbol tables that were unloaded again to avoid EFAULT when reading
ksyms.

also restore TAILQ_FOREACH idiom.


To generate a diff of this commit:
cvs rdiff -u -r1.97 -r1.98 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.97 src/sys/kern/kern_ksyms.c:1.98
--- src/sys/kern/kern_ksyms.c:1.97	Thu Jun  3 09:22:47 2021
+++ src/sys/kern/kern_ksyms.c	Sun Jul 18 06:57:28 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.97 2021/06/03 09:22:47 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.98 2021/07/18 06:57:28 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.97 2021/06/03 09:22:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.98 2021/07/18 06:57:28 mlelstv Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -1084,9 +1084,9 @@ ksymsread(dev_t dev, struct uio *uio, in
 	 * Copy out the symbol table.
 	 */
 	filepos = sizeof(struct ksyms_hdr);
-	for (st = TAILQ_FIRST(_symtabs);
-	 ;
-	 st = TAILQ_NEXT(st, sd_queue)) {
+	TAILQ_FOREACH(st, _symtabs, sd_queue) {
+		if (__predict_false(st->sd_gone))
+			continue;
 		if (uio->uio_resid == 0)
 			return 0;
 		if (uio->uio_offset <= st->sd_symsize + filepos) {



CVS commit: src/sys/kern

2021-07-03 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sat Jul  3 09:39:26 UTC 2021

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

Log Message:
Return error from fd_dupopen.


To generate a diff of this commit:
cvs rdiff -u -r1.550 -r1.551 src/sys/kern/vfs_syscalls.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_syscalls.c
diff -u src/sys/kern/vfs_syscalls.c:1.550 src/sys/kern/vfs_syscalls.c:1.551
--- src/sys/kern/vfs_syscalls.c:1.550	Tue Jun 29 22:40:53 2021
+++ src/sys/kern/vfs_syscalls.c	Sat Jul  3 09:39:26 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_syscalls.c,v 1.550 2021/06/29 22:40:53 dholland Exp $	*/
+/*	$NetBSD: vfs_syscalls.c,v 1.551 2021/07/03 09:39:26 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2019, 2020 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.550 2021/06/29 22:40:53 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.551 2021/07/03 09:39:26 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_fileassoc.h"
@@ -1674,10 +1674,9 @@ do_open(lwp_t *l, struct vnode *dvp, str
 	if (vp == NULL) {
 		fd_abort(p, fp, indx);
 		error = fd_dupopen(dupfd, dupfd_move, flags, );
-		if (error == 0) {
-			*fd = indx;
-			return 0;
-		}
+		if (error)
+			return error;
+		*fd = indx;
 	} else {
 		error = open_setfp(l, fp, vp, indx, flags);
 		if (error)



CVS commit: src/sys/kern

2021-07-02 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Fri Jul  2 16:56:22 UTC 2021

Modified Files:
src/sys/kern: vnode_if.src

Log Message:
Turn off fstrans for VOP_PARSEPATH. Fixes t_umountstress deadlock.

Diagnosis by hannken@.


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

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/vnode_if.src
diff -u src/sys/kern/vnode_if.src:1.81 src/sys/kern/vnode_if.src:1.82
--- src/sys/kern/vnode_if.src:1.81	Tue Jun 29 22:34:08 2021
+++ src/sys/kern/vnode_if.src	Fri Jul  2 16:56:22 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: vnode_if.src,v 1.81 2021/06/29 22:34:08 dholland Exp $
+#	$NetBSD: vnode_if.src,v 1.82 2021/07/02 16:56:22 dholland Exp $
 #
 # Copyright (c) 1992, 1993
 #	The Regents of the University of California.  All rights reserved.
@@ -60,9 +60,10 @@ vop_bwrite {
 };
 
 #
-#% parsepath  dvp L L L
+#% parsepath  dvp = = =
 #
 vop_parsepath {
+	FSTRANS=NO
 	IN struct vnode *dvp;
 	IN const char *name;
 	OUT size_t *retval;



CVS commit: src/sys/kern

2021-07-01 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Thu Jul  1 15:53:20 UTC 2021

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

Log Message:
gcc (with some options) eroneously claims we would use "vp" uninitialized,
so initialize it as NULL.


To generate a diff of this commit:
cvs rdiff -u -r1.219 -r1.220 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.219 src/sys/kern/vfs_vnops.c:1.220
--- src/sys/kern/vfs_vnops.c:1.219	Thu Jul  1 04:25:51 2021
+++ src/sys/kern/vfs_vnops.c	Thu Jul  1 15:53:20 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.219 2021/07/01 04:25:51 christos Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.220 2021/07/01 15:53:20 martin Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.219 2021/07/01 04:25:51 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.220 2021/07/01 15:53:20 martin Exp $");
 
 #include "veriexec.h"
 
@@ -164,7 +164,7 @@ vn_open(struct vnode *at_dvp, struct pat
 	struct vnode **ret_vp, bool *ret_domove, int *ret_fd)
 {
 	struct nameidata nd;
-	struct vnode *vp;
+	struct vnode *vp = NULL;
 	struct lwp *l = curlwp;
 	kauth_cred_t cred = l->l_cred;
 	struct vattr va;



CVS commit: src/sys/kern

2021-06-30 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Jul  1 04:25:51 UTC 2021

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

Log Message:
don't clear the error before we use it to determine if we are moving or duping.


To generate a diff of this commit:
cvs rdiff -u -r1.218 -r1.219 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.218 src/sys/kern/vfs_vnops.c:1.219
--- src/sys/kern/vfs_vnops.c:1.218	Wed Jun 30 13:51:49 2021
+++ src/sys/kern/vfs_vnops.c	Thu Jul  1 00:25:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.218 2021/06/30 17:51:49 dholland Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.219 2021/07/01 04:25:51 christos Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.218 2021/06/30 17:51:49 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.219 2021/07/01 04:25:51 christos Exp $");
 
 #include "veriexec.h"
 
@@ -339,10 +339,10 @@ out:
 			error = EOPNOTSUPP;
 			break;
 		}
-		error = 0;
 		*ret_vp = NULL;
 		*ret_domove = error == EMOVEFD;
 		*ret_fd = l->l_dupfd;
+		error = 0;
 		break;
 	case 0:
 		*ret_vp = vp;



CVS commit: src/sys/kern

2021-06-30 Thread Brett Lymn
Module Name:src
Committed By:   blymn
Date:   Wed Jun 30 21:52:16 UTC 2021

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

Log Message:
Prevent kernel panic on sleep if network interface driver does not
have if_stop defined.  This is a common problem with usb adaptors.
This is a temporary fix, the printf needs to be converted to an
assert once the drivers are cleaned up.


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 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.45 src/sys/kern/kern_pmf.c:1.46
--- src/sys/kern/kern_pmf.c:1.45	Thu Jun 11 02:30:21 2020
+++ src/sys/kern/kern_pmf.c	Wed Jun 30 21:52:16 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_pmf.c,v 1.45 2020/06/11 02:30:21 thorpej Exp $ */
+/* $NetBSD: kern_pmf.c,v 1.46 2021/06/30 21:52:16 blymn Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill 
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.45 2020/06/11 02:30:21 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.46 2021/06/30 21:52:16 blymn Exp $");
 
 #include 
 #include 
@@ -892,11 +892,18 @@ pmf_class_network_suspend(device_t dev, 
 	struct ifnet *ifp = device_pmf_class_private(dev);
 	int s;
 
-	s = splnet();
-	IFNET_LOCK(ifp);
-	(*ifp->if_stop)(ifp, 0);
-	IFNET_UNLOCK(ifp);
-	splx(s);
+	if (ifp == NULL)
+		return true;
+
+	if ((*ifp->if_stop) == NULL)
+		printf("device %s has no if_stop\n", ifp->if_xname);
+	else {
+		s = splnet();
+		IFNET_LOCK(ifp);
+		(*ifp->if_stop)(ifp, 0);
+		IFNET_UNLOCK(ifp);
+		splx(s);
+	}
 
 	return true;
 }



CVS commit: src/sys/kern

2021-06-30 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Wed Jun 30 17:51:49 UTC 2021

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

Log Message:
Improve Christos's vn_open fix.

- assert about api misuse up front (suggested by riastradh)
- restore the behavior of returning EOPNOTSUPP if ret_fd is NULL and we
  get a fd back (otherwise things like ktruss -o /dev/stderr panic)
- clear error to 0 for the EDUPFD and EMOVEFD cases so opening a
  cloner succeeds


To generate a diff of this commit:
cvs rdiff -u -r1.217 -r1.218 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.217 src/sys/kern/vfs_vnops.c:1.218
--- src/sys/kern/vfs_vnops.c:1.217	Wed Jun 30 11:20:32 2021
+++ src/sys/kern/vfs_vnops.c	Wed Jun 30 17:51:49 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.217 2021/06/30 11:20:32 christos Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.218 2021/06/30 17:51:49 dholland Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.217 2021/06/30 11:20:32 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.218 2021/06/30 17:51:49 dholland Exp $");
 
 #include "veriexec.h"
 
@@ -173,6 +173,9 @@ vn_open(struct vnode *at_dvp, struct pat
 
 	KASSERT((nmode & (TRYEMULROOT | NOCHROOT)) == nmode);
 
+	KASSERT(ret_vp != NULL);
+	KASSERT((ret_domove == NULL) == (ret_fd == NULL));
+
 	if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
 		return EINVAL;
 
@@ -332,9 +335,11 @@ out:
 	case EDUPFD:
 	case EMOVEFD:
 		/* if the caller isn't prepared to handle fds, fail for them */
-		KASSERTMSG(ret_domove && ret_fd,
-		"caller did not supply ret_domove and ret_fd for %d",
-		error);
+		if (ret_fd == NULL) {
+			error = EOPNOTSUPP;
+			break;
+		}
+		error = 0;
 		*ret_vp = NULL;
 		*ret_domove = error == EMOVEFD;
 		*ret_fd = l->l_dupfd;



CVS commit: src/sys/kern

2021-06-30 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Wed Jun 30 11:20:33 UTC 2021

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

Log Message:
PR/56286: Martin Husemann: Fix NULL deref on kmod load.
- No need to set ret_domove and ret_fd in the regular case, they are meaningless
- KASSERT instead of setting errno and then doing the NULL deref.


To generate a diff of this commit:
cvs rdiff -u -r1.216 -r1.217 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.216 src/sys/kern/vfs_vnops.c:1.217
--- src/sys/kern/vfs_vnops.c:1.216	Tue Jun 29 18:40:53 2021
+++ src/sys/kern/vfs_vnops.c	Wed Jun 30 07:20:32 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.216 2021/06/29 22:40:53 dholland Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.217 2021/06/30 11:20:32 christos Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.216 2021/06/29 22:40:53 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.217 2021/06/30 11:20:32 christos Exp $");
 
 #include "veriexec.h"
 
@@ -328,35 +328,20 @@ bad:
 out:
 	pathbuf_stringcopy_put(nd.ni_pathbuf, pathstring);
 
-	/* if the caller isn't prepared to handle fds, fail for them */
-	if (ret_fd == NULL && (error == EDUPFD || error == EMOVEFD)) {
-		/*
-		 * XXX: for EMOVEFD (cloning devices) this leaks the
-		 * device's file descriptor. That's not good, but
-		 * fixing it here would still be a layer violation and
-		 * callers not currently prepared to deal weren't
-		 * prepared before I rearranged things either and
-		 * would still have leaked the fd, so it's at least
-		 * not a regression.
-		 *-- dholland 20210627
-		 */
-		error = EOPNOTSUPP;
-	}
-
-	if (error == EDUPFD) {
+	switch (error) {
+	case EDUPFD:
+	case EMOVEFD:
+		/* if the caller isn't prepared to handle fds, fail for them */
+		KASSERTMSG(ret_domove && ret_fd,
+		"caller did not supply ret_domove and ret_fd for %d",
+		error);
 		*ret_vp = NULL;
-		*ret_domove = false;
+		*ret_domove = error == EMOVEFD;
 		*ret_fd = l->l_dupfd;
-		error = 0;
-	} else if (error == EMOVEFD) {
-		*ret_vp = NULL;
-		*ret_domove = true;
-		*ret_fd = l->l_dupfd;
-		error = 0;
-	} else if (error == 0) {
+		break;
+	case 0:
 		*ret_vp = vp;
-		*ret_domove = false;
-		*ret_fd = -1;
+		break;
 	}
 	l->l_dupfd = 0;
 	return error;



CVS commit: src/sys/kern

2021-06-29 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Tue Jun 29 22:29:59 UTC 2021

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

Log Message:
Adjust namei internals to be able to make an external call to parse
the pathname. (Basically, this means change the signature of
namei_getcomponent(), and thus lookup_parsepath(), to pass in the
directory vnode and to allow failures.)


To generate a diff of this commit:
cvs rdiff -u -r1.226 -r1.227 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.226 src/sys/kern/vfs_lookup.c:1.227
--- src/sys/kern/vfs_lookup.c:1.226	Wed Jun 16 01:51:57 2021
+++ src/sys/kern/vfs_lookup.c	Tue Jun 29 22:29:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_lookup.c,v 1.226 2021/06/16 01:51:57 dholland Exp $	*/
+/*	$NetBSD: vfs_lookup.c,v 1.227 2021/06/29 22:29:59 dholland Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.226 2021/06/16 01:51:57 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.227 2021/06/29 22:29:59 dholland Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_magiclinks.h"
@@ -229,16 +229,19 @@ namei_hash(const char *name, const char 
  * Find the end of the first path component in NAME and return its
  * length.
  */
-static size_t
-namei_getcomponent(const char *name)
+static int
+namei_getcomponent(struct vnode *dvp, const char *name, size_t *ret)
 {
 	size_t pos;
 
+	(void)dvp;
+
 	pos = 0;
 	while (name[pos] != '\0' && name[pos] != '/') {
 		pos++;
 	}
-	return pos;
+	*ret = pos;
+	return 0;
 }
 
 
@@ -840,9 +843,10 @@ namei_follow(struct namei_state *state, 
  * Inspect the leading path component and update the state accordingly.
  */
 static int
-lookup_parsepath(struct namei_state *state)
+lookup_parsepath(struct namei_state *state, struct vnode *searchdir)
 {
 	const char *cp;			/* pointer into pathname argument */
+	int error;
 
 	struct componentname *cnp = state->cnp;
 	struct nameidata *ndp = state->ndp;
@@ -861,7 +865,11 @@ lookup_parsepath(struct namei_state *sta
 	 * is held.
 	 */
 	cnp->cn_consume = 0;
-	cnp->cn_namelen = namei_getcomponent(cnp->cn_nameptr);
+	error = namei_getcomponent(searchdir, cnp->cn_nameptr,
+   >cn_namelen);
+	if (error) {
+		return error;
+	}
 	cp = cnp->cn_nameptr + cnp->cn_namelen;
 	if (cnp->cn_namelen > KERNEL_NAME_MAX) {
 		return ENAMETOOLONG;
@@ -1325,7 +1333,7 @@ lookup_fastforward(struct namei_state *s
 		 */
 		KASSERT(cnp->cn_nameptr[0] != '/');
 		KASSERT(cnp->cn_nameptr[0] != '\0');
-		if ((error = lookup_parsepath(state)) != 0) {
+		if ((error = lookup_parsepath(state, searchdir)) != 0) {
 			break;
 		}
 
@@ -1500,9 +1508,13 @@ lookup_fastforward(struct namei_state *s
 			}
 			cnp->cn_nameptr = oldnameptr;
 			ndp->ni_pathlen = oldpathlen;
-			error = lookup_parsepath(state);
-			if (error == 0) {
+			if (searchdir == NULL) {
 error = EOPNOTSUPP;
+			} else {
+error = lookup_parsepath(state, searchdir);
+if (error == 0) {
+	error = EOPNOTSUPP;
+}
 			}
 		}
 	} else if (plock != NULL) {
@@ -2049,7 +2061,7 @@ lookup_for_nfsd(struct nameidata *ndp, s
 static int
 do_lookup_for_nfsd_index(struct namei_state *state)
 {
-	int error = 0;
+	int error;
 
 	struct componentname *cnp = state->cnp;
 	struct nameidata *ndp = state->ndp;
@@ -2068,7 +2080,12 @@ do_lookup_for_nfsd_index(struct namei_st
 	ndp->ni_dvp = NULL;
 
 	cnp->cn_consume = 0;
-	cnp->cn_namelen = namei_getcomponent(cnp->cn_nameptr);
+	error = namei_getcomponent(startdir, cnp->cn_nameptr,
+   >cn_namelen);
+	if (error) {
+		return error;
+	}
+
 	cp = cnp->cn_nameptr + cnp->cn_namelen;
 	KASSERT(cnp->cn_namelen <= KERNEL_NAME_MAX);
 	ndp->ni_pathlen -= cnp->cn_namelen;
@@ -2199,7 +2216,10 @@ relookup(struct vnode *dvp, struct vnode
 	if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
 		panic("relookup: bad hash");
 #endif
-	newlen = namei_getcomponent(cnp->cn_nameptr);
+	error = namei_getcomponent(dvp, cnp->cn_nameptr, );
+	if (error) {
+		panic("relookup: parsepath failed with error %d", error);
+	}
 	if (cnp->cn_namelen != newlen)
 		panic("relookup: bad len");
 	cp = cnp->cn_nameptr + cnp->cn_namelen;



CVS commit: src/sys/kern

2021-06-27 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Jun 27 09:13:08 UTC 2021

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

Log Message:
factor out the ktrace printing into their own functions.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/kern/vfs_xattr.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_xattr.c
diff -u src/sys/kern/vfs_xattr.c:1.35 src/sys/kern/vfs_xattr.c:1.36
--- src/sys/kern/vfs_xattr.c:1.35	Sat May 16 14:31:50 2020
+++ src/sys/kern/vfs_xattr.c	Sun Jun 27 05:13:08 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_xattr.c,v 1.35 2020/05/16 18:31:50 christos Exp $	*/
+/*	$NetBSD: vfs_xattr.c,v 1.36 2021/06/27 09:13:08 christos Exp $	*/
 
 /*-
  * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,v 1.35 2020/05/16 18:31:50 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,v 1.36 2021/06/27 09:13:08 christos Exp $");
 
 #include 
 #include 
@@ -89,6 +89,18 @@ __KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,
 
 #include 
 
+static void
+ktr_xattr_name(const char *str)
+{
+	ktrkuser("xattr-name", (void *)__UNCONST(str), strlen(str));
+}
+
+static void
+ktr_xattr_val(const void *data, size_t cnt)
+{
+	ktruser("xattr-val", __UNCONST(data), cnt, 0);
+}
+
 /*
  * Credential check based on process requesting service, and per-attribute
  * permissions.
@@ -250,8 +262,8 @@ extattr_set_vp(struct vnode *vp, int att
 	auio.uio_vmspace = l->l_proc->p_vmspace;
 	cnt = nbytes;
 
-	ktrkuser("xattr-name", (void *)__UNCONST(attrname), strlen(attrname));
-	ktruser("xattr-val", __UNCONST(data), nbytes, 0);
+	ktr_xattr_name(attrname);
+	ktr_xattr_val(data, nbytes);
 
 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, , l->l_cred);
 	cnt -= auio.uio_resid;
@@ -305,7 +317,7 @@ extattr_get_vp(struct vnode *vp, int att
 	} else
 		sizep = 
 
-	ktrkuser("xattr-name", (void *)__UNCONST(attrname), strlen(attrname));
+	ktr_xattr_name(attrname);
 
 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
 	l->l_cred);
@@ -314,7 +326,7 @@ extattr_get_vp(struct vnode *vp, int att
 		cnt -= auio.uio_resid;
 		retval[0] = cnt;
 
-		ktruser("xattr-val", data, cnt, 0);
+		ktr_xattr_val(data, cnt);
 	} else
 		retval[0] = size;
 
@@ -336,7 +348,7 @@ extattr_delete_vp(struct vnode *vp, int 
 
 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 
-	ktrkuser("xattr-name", (void *)__UNCONST(attrname), strlen(attrname));
+	ktr_xattr_name(attrname);
 
 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, l->l_cred);
 	if (error == EOPNOTSUPP)



CVS commit: src/sys/kern

2021-06-15 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jun 16 00:19:46 UTC 2021

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

Log Message:
drvctl(4): If no ifattr is specified on rescan, rescan all of them.

No longer must you remember the magic `-a ata_hl' incantation when
you deign to `drvctl -r atabusN'.  It didn't actually do anything
anyway: atabus_rescan always rescans all ifattrs; same with
uhub_rescan and many others.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/sys/kern/kern_drvctl.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_drvctl.c
diff -u src/sys/kern/kern_drvctl.c:1.48 src/sys/kern/kern_drvctl.c:1.49
--- src/sys/kern/kern_drvctl.c:1.48	Sat Jun 12 12:14:03 2021
+++ src/sys/kern/kern_drvctl.c	Wed Jun 16 00:19:46 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_drvctl.c,v 1.48 2021/06/12 12:14:03 riastradh Exp $ */
+/* $NetBSD: kern_drvctl.c,v 1.49 2021/06/16 00:19:46 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.48 2021/06/12 12:14:03 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.49 2021/06/16 00:19:46 riastradh Exp $");
 
 #include 
 #include 
@@ -318,11 +318,15 @@ rescanbus(const char *busname, const cha
 	!d->dv_cfdriver->cd_attrs)
 		return ENODEV;
 
-	/* allow to omit attribute if there is exactly one */
+	/* rescan all ifattrs if none is specified */
 	if (!ifattr) {
-		if (d->dv_cfdriver->cd_attrs[1])
-			return EINVAL;
-		ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
+		rc = 0;
+		for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++) {
+			rc = (*d->dv_cfattach->ca_rescan)(d, (*ap)->ci_name,
+			locs);
+			if (rc)
+break;
+		}
 	} else {
 		/* check for valid attribute passed */
 		for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
@@ -330,9 +334,9 @@ rescanbus(const char *busname, const cha
 break;
 		if (!*ap)
 			return EINVAL;
+		rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
 	}
 
-	rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
 	config_deferred(NULL);
 	return rc;
 }



CVS commit: src/sys/kern

2021-06-14 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Mon Jun 14 08:55:49 UTC 2021

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

Log Message:
Fix UP build


To generate a diff of this commit:
cvs rdiff -u -r1.287 -r1.288 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.287 src/sys/kern/subr_autoconf.c:1.288
--- src/sys/kern/subr_autoconf.c:1.287	Sun Jun 13 09:30:48 2021
+++ src/sys/kern/subr_autoconf.c	Mon Jun 14 08:55:49 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.287 2021/06/13 09:30:48 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.288 2021/06/14 08:55:49 skrll Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.287 2021/06/13 09:30:48 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.288 2021/06/14 08:55:49 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -963,7 +963,7 @@ config_cfdata_attach(cfdata_t cf, int sc
 	if (scannow)
 		rescan_with_cfdata(cf);
 
-	KERNEL_UNLOCK_ONE();
+	KERNEL_UNLOCK_ONE(NULL);
 
 	return 0;
 }



CVS commit: src/sys/kern

2021-06-13 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sun Jun 13 14:58:50 UTC 2021

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

Log Message:
Return ENOENT if the hashstat sysctl was called to query a specific hash
name and that hash name doesn't exist.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/kern/subr_hash.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_hash.c
diff -u src/sys/kern/subr_hash.c:1.11 src/sys/kern/subr_hash.c:1.12
--- src/sys/kern/subr_hash.c:1.11	Sun Jun 13 14:02:46 2021
+++ src/sys/kern/subr_hash.c	Sun Jun 13 14:58:49 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_hash.c,v 1.11 2021/06/13 14:02:46 christos Exp $	*/
+/*	$NetBSD: subr_hash.c,v 1.12 2021/06/13 14:58:49 simonb Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.11 2021/06/13 14:02:46 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.12 2021/06/13 14:58:49 simonb Exp $");
 
 #include 
 #include 
@@ -243,6 +243,9 @@ hashstat_sysctl(SYSCTLFN_ARGS)
 	rw_exit(_lock);
 	sysctl_relock();
 
+	if (query && written == 0)	/* query not found? */
+		error = ENOENT;
+
 	*oldlenp = written;
 	return error;
 }



CVS commit: src/sys/kern

2021-06-13 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Jun 13 14:02:46 UTC 2021

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

Log Message:
remove unnecessary double init (fron paulg)


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/kern/subr_hash.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_hash.c
diff -u src/sys/kern/subr_hash.c:1.10 src/sys/kern/subr_hash.c:1.11
--- src/sys/kern/subr_hash.c:1.10	Sat Jun 12 23:09:20 2021
+++ src/sys/kern/subr_hash.c	Sun Jun 13 10:02:46 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_hash.c,v 1.10 2021/06/13 03:09:20 christos Exp $	*/
+/*	$NetBSD: subr_hash.c,v 1.11 2021/06/13 14:02:46 christos Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.10 2021/06/13 03:09:20 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.11 2021/06/13 14:02:46 christos Exp $");
 
 #include 
 #include 
@@ -214,7 +214,6 @@ hashstat_sysctl(SYSCTLFN_ARGS)
 			return EINVAL;
 		}
 		query = true;
-		h = newp;
 		error = sysctl_copyinstr(l, h->hash_name, queryname, 
 		sizeof(queryname), );
 		if (error)



CVS commit: src/sys/kern

2021-06-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jun 13 09:30:48 UTC 2021

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

Log Message:
autoconf(9): Take kernel lock in config_detach.

config_detach is used in too many places to audit for now -- so
although I'm quite sure it is racy (e.g., with cloning devices and
drvctl: drvctl -d a newly opened fss0 before sc_state has
transitioned from FSS_IDLE), this will mitigate the immediate fallout
until we can properly fix autoconf's notions of device pointers.


To generate a diff of this commit:
cvs rdiff -u -r1.286 -r1.287 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.286 src/sys/kern/subr_autoconf.c:1.287
--- src/sys/kern/subr_autoconf.c:1.286	Sun Jun 13 00:11:46 2021
+++ src/sys/kern/subr_autoconf.c	Sun Jun 13 09:30:48 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.286 2021/06/13 00:11:46 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.287 2021/06/13 09:30:48 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.286 2021/06/13 00:11:46 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.287 2021/06/13 09:30:48 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1969,7 +1969,7 @@ config_detach(device_t dev, int flags)
 	device_t d __diagused;
 	int rv = 0;
 
-	KASSERT(KERNEL_LOCKED_P());
+	KERNEL_LOCK(1, NULL);
 
 	cf = dev->dv_cfdata;
 	KASSERTMSG((cf == NULL || cf->cf_fstate == FSTATE_FOUND ||
@@ -1988,8 +1988,10 @@ config_detach(device_t dev, int flags)
 	 * attached.
 	 */
 	rv = config_detach_enter(dev);
-	if (rv)
+	if (rv) {
+		KERNEL_UNLOCK_ONE(NULL);
 		return rv;
+	}
 
 	mutex_enter(_lock);
 	if (dev->dv_del_gen != 0) {
@@ -1999,6 +2001,7 @@ config_detach(device_t dev, int flags)
 		device_xname(dev));
 #endif /* DIAGNOSTIC */
 		config_detach_exit(dev);
+		KERNEL_UNLOCK_ONE(NULL);
 		return ENOENT;
 	}
 	alldevs_nwrite++;
@@ -2095,6 +2098,8 @@ out:
 	}
 	config_alldevs_exit();
 
+	KERNEL_UNLOCK_ONE(NULL);
+
 	return rv;
 }
 



CVS commit: src/sys/kern

2021-06-12 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Jun 13 03:09:20 UTC 2021

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

Log Message:
need to copyin when reading the hash name from userland


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/kern/subr_hash.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_hash.c
diff -u src/sys/kern/subr_hash.c:1.9 src/sys/kern/subr_hash.c:1.10
--- src/sys/kern/subr_hash.c:1.9	Mon Apr  5 01:47:31 2021
+++ src/sys/kern/subr_hash.c	Sat Jun 12 23:09:20 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_hash.c,v 1.9 2021/04/05 05:47:31 simonb Exp $	*/
+/*	$NetBSD: subr_hash.c,v 1.10 2021/06/13 03:09:20 christos Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.9 2021/04/05 05:47:31 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.10 2021/06/13 03:09:20 christos Exp $");
 
 #include 
 #include 
@@ -207,6 +207,7 @@ hashstat_sysctl(SYSCTLFN_ARGS)
 
 	if (namelen > 0 && name[0] == CTL_QUERY) {
 		const struct hashstat_sysctl *h = newp;
+		size_t s;
 
 		if (h == NULL) {
 			/* Can't QUERY one hash without supplying the hash name. */
@@ -214,7 +215,10 @@ hashstat_sysctl(SYSCTLFN_ARGS)
 		}
 		query = true;
 		h = newp;
-		strlcpy(queryname, h->hash_name, sizeof(queryname));
+		error = sysctl_copyinstr(l, h->hash_name, queryname, 
+		sizeof(queryname), );
+		if (error)
+			return error;
 	} else {
 		query = false;
 	}



CVS commit: src/sys/kern

2021-06-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jun 13 00:11:46 UTC 2021

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

Log Message:
autoconf(9): Sprinkle KASSERT(dev->dv_pending == 0) in dealloc paths.

This would have made uhub's config_pending_incr leak more obvious by
crashing in KASSERT(dev->dv_pending == 0) early on, rather than
crashing in a tailq panic later on when the config_pending list gets
corrupted with use-after-free because nothing took the device off
dv_pending_list when attached.

(This is slightly academic now because config_detach blocks until
dev->dv_pending == 0, but it doesn't hurt and makes the intent
clearer.)


To generate a diff of this commit:
cvs rdiff -u -r1.285 -r1.286 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.285 src/sys/kern/subr_autoconf.c:1.286
--- src/sys/kern/subr_autoconf.c:1.285	Sun Jun 13 00:11:17 2021
+++ src/sys/kern/subr_autoconf.c	Sun Jun 13 00:11:46 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.285 2021/06/13 00:11:17 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.286 2021/06/13 00:11:46 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.285 2021/06/13 00:11:17 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.286 2021/06/13 00:11:46 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1421,7 +1421,9 @@ config_devlink(device_t dev)
 static void
 config_devfree(device_t dev)
 {
+
 	KASSERT(dev->dv_flags & DVF_PRIV_ALLOC);
+	KASSERTMSG(dev->dv_pending == 0, "%d", dev->dv_pending);
 
 	if (dev->dv_cfattach->ca_devsize > 0)
 		kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize);
@@ -1439,6 +1441,7 @@ config_devunlink(device_t dev, struct de
 	int i;
 
 	KASSERT(mutex_owned(_lock));
+	KASSERTMSG(dev->dv_pending == 0, "%d", dev->dv_pending);
 
  	/* Unlink from device list.  Link to garbage list. */
 	TAILQ_REMOVE(, dev, dv_list);
@@ -1469,6 +1472,8 @@ config_devdelete(device_t dev)
 	struct device_garbage *dg = >dv_garbage;
 	device_lock_t dvl = device_getlock(dev);
 
+	KASSERTMSG(dev->dv_pending == 0, "%d", dev->dv_pending);
+
 	if (dg->dg_devs != NULL)
 		kmem_free(dg->dg_devs, sizeof(device_t) * dg->dg_ndevs);
 



CVS commit: src/sys/kern

2021-06-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jun 13 00:11:17 UTC 2021

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

Log Message:
autoconf(9): Take kernel lock in a few entry points.

The arguments to config_attach_pseudo, config_init/fini_component,
and config_cfdata_attach/detach are generally statically allocated
objects in a module or the main kernel and as such are stable, and
there are no data structure invariants they assume the kernel lock
will covers from call to call.  So there should be no need for the
caller to hold the kernel lock.

Should fix panic on modload of, e.g., nvmm.


To generate a diff of this commit:
cvs rdiff -u -r1.284 -r1.285 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.284 src/sys/kern/subr_autoconf.c:1.285
--- src/sys/kern/subr_autoconf.c:1.284	Sat Jun 12 12:14:13 2021
+++ src/sys/kern/subr_autoconf.c	Sun Jun 13 00:11:17 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.284 2021/06/12 12:14:13 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.285 2021/06/13 00:11:17 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.284 2021/06/12 12:14:13 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.285 2021/06/13 00:11:17 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -377,27 +377,31 @@ config_init_component(struct cfdriver * 
 {
 	int error;
 
-	KASSERT(KERNEL_LOCKED_P());
+	KERNEL_LOCK(1, NULL);
 
 	if ((error = frob_cfdrivervec(cfdriverv,
 	config_cfdriver_attach, config_cfdriver_detach, "init", false))!= 0)
-		return error;
+		goto out;
 	if ((error = frob_cfattachvec(cfattachv,
 	config_cfattach_attach, config_cfattach_detach,
 	"init", false)) != 0) {
 		frob_cfdrivervec(cfdriverv,
 	config_cfdriver_detach, NULL, "init rollback", true);
-		return error;
+		goto out;
 	}
 	if ((error = config_cfdata_attach(cfdatav, 1)) != 0) {
 		frob_cfattachvec(cfattachv,
 		config_cfattach_detach, NULL, "init rollback", true);
 		frob_cfdrivervec(cfdriverv,
 	config_cfdriver_detach, NULL, "init rollback", true);
-		return error;
+		goto out;
 	}
 
-	return 0;
+	/* Success!  */
+	error = 0;
+
+out:	KERNEL_UNLOCK_ONE(NULL);
+	return error;
 }
 
 int
@@ -406,16 +410,16 @@ config_fini_component(struct cfdriver * 
 {
 	int error;
 
-	KASSERT(KERNEL_LOCKED_P());
+	KERNEL_LOCK(1, NULL);
 
 	if ((error = config_cfdata_detach(cfdatav)) != 0)
-		return error;
+		goto out;
 	if ((error = frob_cfattachvec(cfattachv,
 	config_cfattach_detach, config_cfattach_attach,
 	"fini", false)) != 0) {
 		if (config_cfdata_attach(cfdatav, 0) != 0)
 			panic("config_cfdata fini rollback failed");
-		return error;
+		goto out;
 	}
 	if ((error = frob_cfdrivervec(cfdriverv,
 	config_cfdriver_detach, config_cfdriver_attach,
@@ -424,10 +428,14 @@ config_fini_component(struct cfdriver * 
 	config_cfattach_attach, NULL, "fini rollback", true);
 		if (config_cfdata_attach(cfdatav, 0) != 0)
 			panic("config_cfdata fini rollback failed");
-		return error;
+		goto out;
 	}
 
-	return 0;
+	/* Success!  */
+	error = 0;
+
+out:	KERNEL_UNLOCK_ONE(NULL);
+	return error;
 }
 
 void
@@ -946,7 +954,7 @@ config_cfdata_attach(cfdata_t cf, int sc
 {
 	struct cftable *ct;
 
-	KASSERT(KERNEL_LOCKED_P());
+	KERNEL_LOCK(1, NULL);
 
 	ct = kmem_alloc(sizeof(*ct), KM_SLEEP);
 	ct->ct_cfdata = cf;
@@ -955,6 +963,8 @@ config_cfdata_attach(cfdata_t cf, int sc
 	if (scannow)
 		rescan_with_cfdata(cf);
 
+	KERNEL_UNLOCK_ONE();
+
 	return 0;
 }
 
@@ -986,6 +996,8 @@ config_cfdata_detach(cfdata_t cf)
 	struct cftable *ct;
 	deviter_t di;
 
+	KERNEL_LOCK(1, NULL);
+
 	for (d = deviter_first(, DEVITER_F_RW); d != NULL;
 	 d = deviter_next()) {
 		if (!dev_in_cfdata(d, cf))
@@ -996,19 +1008,23 @@ config_cfdata_detach(cfdata_t cf)
 	deviter_release();
 	if (error) {
 		aprint_error_dev(d, "unable to detach instance\n");
-		return error;
+		goto out;
 	}
 
 	TAILQ_FOREACH(ct, , ct_list) {
 		if (ct->ct_cfdata == cf) {
 			TAILQ_REMOVE(, ct, ct_list);
 			kmem_free(ct, sizeof(*ct));
-			return 0;
+			error = 0;
+			goto out;
 		}
 	}
 
 	/* not found -- shouldn't happen */
-	return EINVAL;
+	error = EINVAL;
+
+out:	KERNEL_UNLOCK_ONE(NULL);
+	return error;
 }
 
 /*
@@ -1824,11 +1840,11 @@ config_attach_pseudo(cfdata_t cf)
 {
 	device_t dev;
 
-	KASSERT(KERNEL_LOCKED_P());
+	KERNEL_LOCK(1, NULL);
 
 	dev = config_devalloc(ROOT, cf, CFARG_EOL);
 	if (!dev)
-		return NULL;
+		goto out;
 
 	/* XXX mark busy in cfdata */
 
@@ -1851,6 +1867,8 @@ config_attach_pseudo(cfdata_t cf)
 	config_pending_decr(dev);
 
 	config_process_deferred(_config_queue, dev);
+
+out:	KERNEL_UNLOCK_ONE(NULL);
 	

CVS commit: src/sys/kern

2021-06-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jun 12 12:14:13 UTC 2021

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

Log Message:
autoconf(9): Must hold alldevs_lock to iterate dv_list on alldevs.


To generate a diff of this commit:
cvs rdiff -u -r1.283 -r1.284 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.283 src/sys/kern/subr_autoconf.c:1.284
--- src/sys/kern/subr_autoconf.c:1.283	Sat Jun 12 12:13:51 2021
+++ src/sys/kern/subr_autoconf.c	Sat Jun 12 12:14:13 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.283 2021/06/12 12:13:51 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.284 2021/06/12 12:14:13 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.283 2021/06/12 12:13:51 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.284 2021/06/12 12:14:13 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -2020,6 +2020,7 @@ config_detach(device_t dev, int flags)
 	 * after parents, we only need to search the latter part of
 	 * the list.)
 	 */
+	mutex_enter(_lock);
 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
 	d = TAILQ_NEXT(d, dv_list)) {
 		if (d->dv_parent == dev && d->dv_del_gen == 0) {
@@ -2029,6 +2030,7 @@ config_detach(device_t dev, int flags)
 			panic("config_detach");
 		}
 	}
+	mutex_exit(_lock);
 #endif
 
 	/* notify the parent that the child is gone */



CVS commit: src/sys/kern

2021-06-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jun 12 12:14:03 UTC 2021

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

Log Message:
drvctl(4): Hold a deviter while issuing config_detach.

Otherwise another concurrent detach -- e.g., from concurrent drvctl
or from USB port disconnection -- can pull the device_t out from
under us.

XXX Need to do this for _every_ operation config_* operation on a
device_t; device_find_by_xname is just fundamentally broken because a
concurrent detach can pull the device_t rug out from under you.

We really need another mechanism for holding a weak reference to a
device, and temporarily getting a strong reference to it; abusing
deviter is a bit of a kludge, and doesn't work very well because it
doesn't properly trigger garbage collection of devices detached while
other concurrent deviters are pending.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/kern/kern_drvctl.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_drvctl.c
diff -u src/sys/kern/kern_drvctl.c:1.47 src/sys/kern/kern_drvctl.c:1.48
--- src/sys/kern/kern_drvctl.c:1.47	Sat Jun 12 12:12:11 2021
+++ src/sys/kern/kern_drvctl.c	Sat Jun 12 12:14:03 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_drvctl.c,v 1.47 2021/06/12 12:12:11 riastradh Exp $ */
+/* $NetBSD: kern_drvctl.c,v 1.48 2021/06/12 12:14:03 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.47 2021/06/12 12:12:11 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.48 2021/06/12 12:14:03 riastradh Exp $");
 
 #include 
 #include 
@@ -254,11 +254,21 @@ static int
 detachdevbyname(const char *devname)
 {
 	device_t d;
+	deviter_t di;
+	int error;
 
 	KASSERT(KERNEL_LOCKED_P());
 
-	if ((d = device_find_by_xname(devname)) == NULL)
-		return ENXIO;
+	for (d = deviter_first(, DEVITER_F_RW);
+	 d != NULL;
+	 d = deviter_next()) {
+		if (strcmp(device_xname(d), devname) == 0)
+			break;
+	}
+	if (d == NULL) {
+		error = ENXIO;
+		goto out;
+	}
 
 #ifndef XXXFULLRISK
 	/*
@@ -267,10 +277,15 @@ detachdevbyname(const char *devname)
 	 * There might be a private notification mechanism,
 	 * but better play it safe here.
 	 */
-	if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached)
-		return ENOTSUP;
+	if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached) {
+		error = ENOTSUP;
+		goto out;
+	}
 #endif
-	return config_detach(d, 0);
+
+	error = config_detach(d, 0);
+out:	deviter_release();
+	return error;
 }
 
 static int



CVS commit: src/sys/kern

2021-06-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jun 12 12:12:11 UTC 2021

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

Log Message:
autoconf(9), drvctl(4): Sprinkle kernel lock assertions.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/kern/kern_drvctl.c
cvs rdiff -u -r1.281 -r1.282 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/kern_drvctl.c
diff -u src/sys/kern/kern_drvctl.c:1.46 src/sys/kern/kern_drvctl.c:1.47
--- src/sys/kern/kern_drvctl.c:1.46	Sat Jun 12 12:11:59 2021
+++ src/sys/kern/kern_drvctl.c	Sat Jun 12 12:12:11 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_drvctl.c,v 1.46 2021/06/12 12:11:59 riastradh Exp $ */
+/* $NetBSD: kern_drvctl.c,v 1.47 2021/06/12 12:12:11 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.46 2021/06/12 12:11:59 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.47 2021/06/12 12:12:11 riastradh Exp $");
 
 #include 
 #include 
@@ -195,6 +195,8 @@ pmdevbyname(u_long cmd, struct devpmargs
 {
 	device_t d;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	if ((d = device_find_by_xname(a->devname)) == NULL)
 		return ENXIO;
 
@@ -221,6 +223,8 @@ listdevbyname(struct devlistargs *l)
 	deviter_t di;
 	int cnt = 0, idx, error = 0;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	if (*l->l_devname == '\0')
 		d = NULL;
 	else if (memchr(l->l_devname, 0, sizeof(l->l_devname)) == NULL)
@@ -251,6 +255,8 @@ detachdevbyname(const char *devname)
 {
 	device_t d;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	if ((d = device_find_by_xname(devname)) == NULL)
 		return ENXIO;
 
@@ -275,6 +281,8 @@ rescanbus(const char *busname, const cha
 	device_t d;
 	const struct cfiattrdata * const *ap;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	/* XXX there should be a way to get limits and defaults (per device)
 	   from config generated data */
 	int locs[MAXLOCATORS];

Index: src/sys/kern/subr_autoconf.c
diff -u src/sys/kern/subr_autoconf.c:1.281 src/sys/kern/subr_autoconf.c:1.282
--- src/sys/kern/subr_autoconf.c:1.281	Sat Jun 12 12:11:49 2021
+++ src/sys/kern/subr_autoconf.c	Sat Jun 12 12:12:11 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.281 2021/06/12 12:11:49 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.282 2021/06/12 12:12:11 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.281 2021/06/12 12:11:49 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.282 2021/06/12 12:12:11 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -377,6 +377,8 @@ config_init_component(struct cfdriver * 
 {
 	int error;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	if ((error = frob_cfdrivervec(cfdriverv,
 	config_cfdriver_attach, config_cfdriver_detach, "init", false))!= 0)
 		return error;
@@ -404,6 +406,8 @@ config_fini_component(struct cfdriver * 
 {
 	int error;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	if ((error = config_cfdata_detach(cfdatav)) != 0)
 		return error;
 	if ((error = frob_cfattachvec(cfattachv,
@@ -439,6 +443,9 @@ config_init_mi(void)
 void
 config_deferred(device_t dev)
 {
+
+	KASSERT(KERNEL_LOCKED_P());
+
 	config_process_deferred(_config_queue, dev);
 	config_process_deferred(_config_queue, dev);
 	config_process_deferred(_config_queue, dev);
@@ -906,6 +913,7 @@ rescan_with_cfdata(const struct cfdata *
 	const struct cfdata *cf1;
 	deviter_t di;
 
+	KASSERT(KERNEL_LOCKED_P());
 
 	/*
 	 * "alldevs" is likely longer than a modules's cfdata, so make it
@@ -939,6 +947,8 @@ config_cfdata_attach(cfdata_t cf, int sc
 {
 	struct cftable *ct;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	ct = kmem_alloc(sizeof(*ct), KM_SLEEP);
 	ct->ct_cfdata = cf;
 	TAILQ_INSERT_TAIL(, ct, ct_list);
@@ -1011,6 +1021,8 @@ config_match(device_t parent, cfdata_t c
 {
 	struct cfattach *ca;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
 	if (ca == NULL) {
 		/* No attachment for this entry, oh well. */
@@ -1710,6 +1722,8 @@ config_vattach(device_t parent, cfdata_t
 	struct cftable *ct;
 	const char *drvname;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	dev = config_vdevalloc(parent, cf, tag, ap);
 	if (!dev)
 		panic("config_attach: allocation of device softc failed");
@@ -1782,6 +1796,8 @@ config_attach(device_t parent, cfdata_t 
 	device_t dev;
 	va_list ap;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	va_start(ap, tag);
 	dev = config_vattach(parent, cf, aux, print, tag, ap);
 	va_end(ap);
@@ -1803,6 +1819,8 @@ config_attach_pseudo(cfdata_t cf)
 {
 	device_t dev;
 
+	KASSERT(KERNEL_LOCKED_P());
+
 	dev = config_devalloc(ROOT, cf, CFARG_EOL);
 	if (!dev)
 		return NULL;
@@ -1886,6 +1904,8 @@ config_detach(device_t dev, int flags)
 	device_t d __diagused;
 	int rv = 0;
 
+	

CVS commit: src/sys/kern

2021-06-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jun 12 12:11:59 UTC 2021

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

Log Message:
drvctl(4): Take the kernel lock around entry into autoconf(9).

Can make this finer-grained once the rest of autoconf(9) is in good
enough shape to support it.


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/sys/kern/kern_drvctl.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_drvctl.c
diff -u src/sys/kern/kern_drvctl.c:1.45 src/sys/kern/kern_drvctl.c:1.46
--- src/sys/kern/kern_drvctl.c:1.45	Thu Jun 11 02:28:01 2020
+++ src/sys/kern/kern_drvctl.c	Sat Jun 12 12:11:59 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_drvctl.c,v 1.45 2020/06/11 02:28:01 thorpej Exp $ */
+/* $NetBSD: kern_drvctl.c,v 1.46 2021/06/12 12:11:59 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.45 2020/06/11 02:28:01 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.46 2021/06/12 12:11:59 riastradh Exp $");
 
 #include 
 #include 
@@ -336,6 +336,7 @@ drvctl_ioctl(struct file *fp, u_long cmd
 	int *locs;
 	size_t locs_sz = 0; /* XXXgcc */
 
+	KERNEL_LOCK(1, NULL);
 	switch (cmd) {
 	case DRVSUSPENDDEV:
 	case DRVRESUMEDEV:
@@ -363,14 +364,16 @@ drvctl_ioctl(struct file *fp, u_long cmd
 			ifattr = 0;
 
 		if (d->numlocators) {
-			if (d->numlocators > MAXLOCATORS)
-return EINVAL;
+			if (d->numlocators > MAXLOCATORS) {
+res = EINVAL;
+goto out;
+			}
 			locs_sz = d->numlocators * sizeof(int);
 			locs = kmem_alloc(locs_sz, KM_SLEEP);
 			res = copyin(d->locators, locs, locs_sz);
 			if (res) {
 kmem_free(locs, locs_sz);
-return res;
+goto out;
 			}
 		} else
 			locs = NULL;
@@ -388,8 +391,10 @@ drvctl_ioctl(struct file *fp, u_long cmd
 		fp->f_flag);
 		break;
 	default:
-		return EPASSTHROUGH;
+		res = EPASSTHROUGH;
+		break;
 	}
+out:	KERNEL_UNLOCK_ONE(NULL);
 	return res;
 }
 



CVS commit: src/sys/kern

2021-06-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jun 12 12:11:49 UTC 2021

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

Log Message:
autoconf(9): Take kernel lock on various entries into autoconf.

Most of autoconf still tacitly assumes the kernel lock is held.


To generate a diff of this commit:
cvs rdiff -u -r1.280 -r1.281 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.280 src/sys/kern/subr_autoconf.c:1.281
--- src/sys/kern/subr_autoconf.c:1.280	Wed Apr 28 03:21:57 2021
+++ src/sys/kern/subr_autoconf.c	Sat Jun 12 12:11:49 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.280 2021/04/28 03:21:57 thorpej Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.281 2021/06/12 12:11:49 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.280 2021/04/28 03:21:57 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.281 2021/06/12 12:11:49 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1285,11 +1285,15 @@ device_t
 config_rootfound(const char *rootname, void *aux)
 {
 	cfdata_t cf;
+	device_t dev = NULL;
 
+	KERNEL_LOCK(1, NULL);
 	if ((cf = config_rootsearch(NULL, rootname, aux)) != NULL)
-		return config_attach(ROOT, cf, aux, NULL, CFARG_EOL);
-	aprint_error("root device %s not configured\n", rootname);
-	return NULL;
+		dev = config_attach(ROOT, cf, aux, NULL, CFARG_EOL);
+	else
+		aprint_error("root device %s not configured\n", rootname);
+	KERNEL_UNLOCK_ONE(NULL);
+	return dev;
 }
 
 /* just like sprintf(buf, "%d") except that it works from the end */
@@ -2046,8 +2050,10 @@ config_detach_all(int how)
 	bool progress = false;
 	int flags;
 
+	KERNEL_LOCK(1, NULL);
+
 	if ((how & (RB_NOSYNC|RB_DUMP)) != 0)
-		return false;
+		goto out;
 
 	if ((how & RB_POWERDOWN) == RB_POWERDOWN)
 		flags = DETACH_SHUTDOWN | DETACH_POWEROFF;
@@ -2063,6 +2069,8 @@ config_detach_all(int how)
 		} else
 			aprint_debug("failed.");
 	}
+
+out:	KERNEL_UNLOCK_ONE(NULL);
 	return progress;
 }
 
@@ -2286,6 +2294,9 @@ int
 config_finalize_register(device_t dev, int (*fn)(device_t))
 {
 	struct finalize_hook *f;
+	int error = 0;
+
+	KERNEL_LOCK(1, NULL);
 
 	/*
 	 * If finalization has already been done, invoke the
@@ -2294,13 +2305,15 @@ config_finalize_register(device_t dev, i
 	if (config_finalize_done) {
 		while ((*fn)(dev) != 0)
 			/* loop */ ;
-		return 0;
+		goto out;
 	}
 
 	/* Ensure this isn't already on the list. */
 	TAILQ_FOREACH(f, _finalize_list, f_list) {
-		if (f->f_func == fn && f->f_dev == dev)
-			return EEXIST;
+		if (f->f_func == fn && f->f_dev == dev) {
+			error = EEXIST;
+			goto out;
+		}
 	}
 
 	f = kmem_alloc(sizeof(*f), KM_SLEEP);
@@ -2308,7 +2321,11 @@ config_finalize_register(device_t dev, i
 	f->f_dev = dev;
 	TAILQ_INSERT_TAIL(_finalize_list, f, f_list);
 
-	return 0;
+	/* Success!  */
+	error = 0;
+
+out:	KERNEL_UNLOCK_ONE(NULL);
+	return error;
 }
 
 void



CVS commit: src/sys/kern

2021-06-11 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Jun 11 23:41:47 UTC 2021

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

Log Message:
Remove extraneous '\n' from debug message.


To generate a diff of this commit:
cvs rdiff -u -r1.151 -r1.152 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.151 src/sys/kern/kern_module.c:1.152
--- src/sys/kern/kern_module.c:1.151	Thu Jun 11 19:20:46 2020
+++ src/sys/kern/kern_module.c	Fri Jun 11 23:41:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_module.c,v 1.151 2020/06/11 19:20:46 ad Exp $	*/
+/*	$NetBSD: kern_module.c,v 1.152 2021/06/11 23:41:47 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.151 2020/06/11 19:20:46 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.152 2021/06/11 23:41:47 pgoyette Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -717,7 +717,7 @@ module_autoload(const char *filename, mo
 		error = module_do_load(filename, false, 0, NULL, NULL, modclass,
 		true);
 
-	module_print("Autoload for `%s' requested by pid %d (%s), status %d\n",
+	module_print("Autoload for `%s' requested by pid %d (%s), status %d",
 	filename, p->p_pid, p->p_comm, error);
 	kernconfig_unlock();
 	return error;



CVS commit: src/sys/kern

2021-06-11 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Fri Jun 11 12:54:23 UTC 2021

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

Log Message:
Fix the order of handling of posix_spawn attributes and file actions.
The standard is explicit about it and it matters if e.g. RESETIDS is
used as an attribute and file actions depend on the group rights for
opening a file.


To generate a diff of this commit:
cvs rdiff -u -r1.505 -r1.506 src/sys/kern/kern_exec.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_exec.c
diff -u src/sys/kern/kern_exec.c:1.505 src/sys/kern/kern_exec.c:1.506
--- src/sys/kern/kern_exec.c:1.505	Sun May  2 10:23:55 2021
+++ src/sys/kern/kern_exec.c	Fri Jun 11 12:54:22 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_exec.c,v 1.505 2021/05/02 10:23:55 martin Exp $	*/
+/*	$NetBSD: kern_exec.c,v 1.506 2021/06/11 12:54:22 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2019, 2020 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.505 2021/05/02 10:23:55 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.506 2021/06/11 12:54:22 martin Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -2260,13 +2260,13 @@ spawn_return(void *arg)
 	rw_enter(>p_reflock, RW_WRITER);
 	have_reflock = true;
 
-	/* handle posix_spawn_file_actions */
-	error = handle_posix_spawn_file_actions(spawn_data->sed_actions);
+	/* handle posix_spawnattr */
+	error = handle_posix_spawn_attrs(attrs, spawn_data->sed_parent);
 	if (error)
 		goto report_error;
 
-	/* handle posix_spawnattr */
-	error = handle_posix_spawn_attrs(attrs, spawn_data->sed_parent);
+	/* handle posix_spawn_file_actions */
+	error = handle_posix_spawn_file_actions(spawn_data->sed_actions);
 	if (error)
 		goto report_error;
 



CVS commit: src/sys/kern

2021-06-09 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Wed Jun  9 15:15:35 UTC 2021

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

Log Message:
Work around abs rela relocations issue (for now):

$ readelf -r compat_linux
...
Relocation section '.rela.data' at offset 0x37270 contains 537 entries:
Offset  Info   Type   Sym. ValueSym. Name + Addend
...
0040  0001 R_X86_64_64  0
0048  0001 R_X86_64_64  0
...

$ objdump -r compat_linux
...
RELOCATION RECORDS FOR [.data]:
OFFSET   TYPE  VALUE
...
0040 R_X86_64_64   *ABS*
0048 R_X86_64_64   *ABS*
...

Since those have symidx == 0, and the 0 symbol table entry is special,
treat them as SHN_ABS.

Change ENOENT -> ENOEXEC to avoid confusion (like other linking errors),
and add some debugging when that happens.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/sys/kern/subr_kobj.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_kobj.c
diff -u src/sys/kern/subr_kobj.c:1.67 src/sys/kern/subr_kobj.c:1.68
--- src/sys/kern/subr_kobj.c:1.67	Sat Jun 27 13:27:59 2020
+++ src/sys/kern/subr_kobj.c	Wed Jun  9 11:15:35 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_kobj.c,v 1.67 2020/06/27 17:27:59 christos Exp $	*/
+/*	$NetBSD: subr_kobj.c,v 1.68 2021/06/09 15:15:35 christos Exp $	*/
 
 /*
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.67 2020/06/27 17:27:59 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.68 2021/06/09 15:15:35 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_modular.h"
@@ -881,7 +881,7 @@ kobj_sym_lookup(kobj_t ko, uintptr_t sym
 
 	sym = ko->ko_symtab + symidx;
 
-	if (symidx == SHN_ABS) {
+	if (symidx == SHN_ABS || symidx == 0) {
 		*val = (uintptr_t)sym->st_value;
 		return 0;
 	} else if (symidx >= ko->ko_symcnt) {
@@ -1074,7 +1074,12 @@ kobj_relocate(kobj_t ko, bool local)
 			}
 			error = kobj_reloc(ko, base, rel, false, local);
 			if (error != 0) {
-return ENOENT;
+kobj_error(ko, "unresolved rel relocation "
+"@%#jx type=%d symidx=%d",
+(intmax_t)rel->r_offset,
+(int)ELF_R_TYPE(rel->r_info),
+(int)ELF_R_SYM(rel->r_info));
+return ENOEXEC;
 			}
 		}
 	}
@@ -1105,7 +1110,12 @@ kobj_relocate(kobj_t ko, bool local)
 			}
 			error = kobj_reloc(ko, base, rela, true, local);
 			if (error != 0) {
-return ENOENT;
+kobj_error(ko, "unresolved rela relocation "
+"@%#jx type=%d symidx=%d",
+(intmax_t)rela->r_offset,
+(int)ELF_R_TYPE(rela->r_info),
+(int)ELF_R_SYM(rela->r_info));
+return ENOEXEC;
 			}
 		}
 	}



CVS commit: src/sys/kern

2021-06-03 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Jun  3 09:22:47 UTC 2021

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

Log Message:
ksyms(4): Allow multiple concurrent opens of /dev/ksyms.

First one takes a snapshot; others all agree with the snapshot.

Previously this code path was just broken (could fail horribly if
modules were unloaded after one of the opens is closed), so I just
blocked it off in an earlier commit, but that broke crash(8).  So
let's continue allowing multiple opens seeing the same snapshot, but
without the horrible bugs.


To generate a diff of this commit:
cvs rdiff -u -r1.96 -r1.97 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.96 src/sys/kern/kern_ksyms.c:1.97
--- src/sys/kern/kern_ksyms.c:1.96	Thu Jun  3 01:00:24 2021
+++ src/sys/kern/kern_ksyms.c	Thu Jun  3 09:22:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.96 2021/06/03 01:00:24 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.97 2021/06/03 09:22:47 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.96 2021/06/03 01:00:24 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.97 2021/06/03 09:22:47 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -112,7 +112,7 @@ static uint32_t *ksyms_nmap = NULL;
 #endif
 
 static int ksyms_maxlen;
-static bool ksyms_isopen;
+static uint64_t ksyms_opencnt;
 static struct ksyms_symtab *ksyms_last_snapshot;
 static bool ksyms_initted;
 static bool ksyms_loaded;
@@ -791,7 +791,7 @@ ksyms_modunload(const char *name)
 			continue;
 		st->sd_gone = true;
 		ksyms_sizes_calc();
-		if (!ksyms_isopen) {
+		if (ksyms_opencnt == 0) {
 			/*
 			 * Ensure ddb never witnesses an inconsistent
 			 * state of the queue, unless memory is so
@@ -1004,14 +1004,12 @@ ksymsopen(dev_t dev, int oflags, int dev
 		return ENXIO;
 
 	/*
-	 * Create a "snapshot" of the kernel symbol table.  Setting
-	 * ksyms_isopen will prevent symbol tables from being freed.
+	 * Create a "snapshot" of the kernel symbol table.  Bumping
+	 * ksyms_opencnt will prevent symbol tables from being freed.
 	 */
 	mutex_enter(_lock);
-	if (ksyms_isopen) {
-		mutex_exit(_lock);
-		return EBUSY;
-	}
+	if (ksyms_opencnt++)
+		goto out;
 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
@@ -1020,9 +1018,8 @@ ksymsopen(dev_t dev, int oflags, int dev
 	ksyms_hdr.kh_shdr[SHCTF].sh_offset = ksyms_strsz +
 	ksyms_hdr.kh_shdr[STRTAB].sh_offset;
 	ksyms_hdr.kh_shdr[SHCTF].sh_size = ksyms_ctfsz;
-	ksyms_isopen = true;
 	ksyms_last_snapshot = TAILQ_LAST(_symtabs, ksyms_symtab_queue);
-	mutex_exit(_lock);
+out:	mutex_exit(_lock);
 
 	return 0;
 }
@@ -1036,7 +1033,8 @@ ksymsclose(dev_t dev, int oflags, int de
 
 	/* Discard references to symbol tables. */
 	mutex_enter(_lock);
-	ksyms_isopen = false;
+	if (--ksyms_opencnt)
+		goto out;
 	ksyms_last_snapshot = NULL;
 	TAILQ_FOREACH_SAFE(st, _symtabs, sd_queue, next) {
 		if (st->sd_gone) {
@@ -1053,7 +1051,7 @@ ksymsclose(dev_t dev, int oflags, int de
 	}
 	if (!TAILQ_EMPTY(_free))
 		ksyms_sizes_calc();
-	mutex_exit(_lock);
+out:	mutex_exit(_lock);
 
 	TAILQ_FOREACH_SAFE(st, _free, sd_queue, next) {
 		kmem_free(st->sd_nmap, st->sd_nmapsize * sizeof(uint32_t));



CVS commit: src/sys/kern

2021-06-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Jun  3 01:00:24 UTC 2021

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

Log Message:
ksyms(4): Don't skip symbol tables that are soon to be freed, take 2.

They will not actually be freed until /dev/ksyms is closed, so
continued access to them remains kosher.

The previous change was busted because of an off-by-one error in a
previous previous change's iteration over the symtabs; that error has
since been corrected.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 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.95 src/sys/kern/kern_ksyms.c:1.96
--- src/sys/kern/kern_ksyms.c:1.95	Thu Jun  3 01:00:15 2021
+++ src/sys/kern/kern_ksyms.c	Thu Jun  3 01:00:24 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.95 2021/06/03 01:00:15 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.96 2021/06/03 01:00:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.95 2021/06/03 01:00:15 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.96 2021/06/03 01:00:24 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -1089,8 +1089,6 @@ ksymsread(dev_t dev, struct uio *uio, in
 	for (st = TAILQ_FIRST(_symtabs);
 	 ;
 	 st = TAILQ_NEXT(st, sd_queue)) {
-		if (__predict_false(st->sd_gone))
-			continue;
 		if (uio->uio_resid == 0)
 			return 0;
 		if (uio->uio_offset <= st->sd_symsize + filepos) {
@@ -1108,13 +1106,11 @@ ksymsread(dev_t dev, struct uio *uio, in
 	/*
 	 * Copy out the string table
 	 */
-	KASSERT(filepos <= sizeof(struct ksyms_hdr) +
+	KASSERT(filepos == sizeof(struct ksyms_hdr) +
 	ksyms_hdr.kh_shdr[SYMTAB].sh_size);
 	for (st = TAILQ_FIRST(_symtabs);
 	 ;
 	 st = TAILQ_NEXT(st, sd_queue)) {
-		if (__predict_false(st->sd_gone))
-			continue;
 		if (uio->uio_resid == 0)
 			return 0;
 		if (uio->uio_offset <= st->sd_strsize + filepos) {



CVS commit: src/sys/kern

2021-06-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Jun  3 01:00:15 UTC 2021

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

Log Message:
ksyms(4): Fix race in ksymsread iteration.

TAILQ_NEXT(ksyms_last_snapshot) might change while we are iterating,
but ksyms_last_snapshot itself cannot, so invert the loop structure.

Discussed with rin@.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 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.94 src/sys/kern/kern_ksyms.c:1.95
--- src/sys/kern/kern_ksyms.c:1.94	Wed Jun  2 15:43:33 2021
+++ src/sys/kern/kern_ksyms.c	Thu Jun  3 01:00:15 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.94 2021/06/02 15:43:33 rin Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.95 2021/06/03 01:00:15 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.94 2021/06/02 15:43:33 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.95 2021/06/03 01:00:15 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -1087,7 +1087,7 @@ ksymsread(dev_t dev, struct uio *uio, in
 	 */
 	filepos = sizeof(struct ksyms_hdr);
 	for (st = TAILQ_FIRST(_symtabs);
-	 st != TAILQ_NEXT(ksyms_last_snapshot, sd_queue);
+	 ;
 	 st = TAILQ_NEXT(st, sd_queue)) {
 		if (__predict_false(st->sd_gone))
 			continue;
@@ -1101,6 +1101,8 @@ ksymsread(dev_t dev, struct uio *uio, in
 return error;
 		}
 		filepos += st->sd_symsize;
+		if (st == ksyms_last_snapshot)
+			break;
 	}
 
 	/*
@@ -1109,7 +,7 @@ ksymsread(dev_t dev, struct uio *uio, in
 	KASSERT(filepos <= sizeof(struct ksyms_hdr) +
 	ksyms_hdr.kh_shdr[SYMTAB].sh_size);
 	for (st = TAILQ_FIRST(_symtabs);
-	 st != TAILQ_NEXT(ksyms_last_snapshot, sd_queue);
+	 ;
 	 st = TAILQ_NEXT(st, sd_queue)) {
 		if (__predict_false(st->sd_gone))
 			continue;
@@ -1123,6 +1125,8 @@ ksymsread(dev_t dev, struct uio *uio, in
 return error;
 		}
 		filepos += st->sd_strsize;
+		if (st == ksyms_last_snapshot)
+			break;
 	}
 
 	/*



CVS commit: src/sys/kern

2021-06-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jun  2 21:34:58 UTC 2021

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

Log Message:
timecounter(9): Zero the next pointer after tc_detach.

This reverts it to a state where tc_init works again.


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/sys/kern/kern_tc.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_tc.c
diff -u src/sys/kern/kern_tc.c:1.61 src/sys/kern/kern_tc.c:1.62
--- src/sys/kern/kern_tc.c:1.61	Thu Apr  8 06:20:47 2021
+++ src/sys/kern/kern_tc.c	Wed Jun  2 21:34:58 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_tc.c,v 1.61 2021/04/08 06:20:47 simonb Exp $ */
+/* $NetBSD: kern_tc.c,v 1.62 2021/06/02 21:34:58 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 
 #include 
 /* __FBSDID("$FreeBSD: src/sys/kern/kern_tc.c,v 1.166 2005/09/19 22:16:31 andre Exp $"); */
-__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.61 2021/04/08 06:20:47 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.62 2021/06/02 21:34:58 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ntp.h"
@@ -698,10 +698,13 @@ tc_detach(struct timecounter *target)
 		 * before retrying.
 		 */
 		if (l == NULL) {
-			return 0;
+			break;
 		}
 		(void)kpause("tcdetach", false, mstohz(10), NULL);
 	}
+
+	tc->tc_next = NULL;
+	return 0;
 }
 
 /* Report the frequency of the current timecounter. */



CVS commit: src/sys/kern

2021-06-02 Thread Rin Okuyama
Module Name:src
Committed By:   rin
Date:   Wed Jun  2 15:43:33 UTC 2021

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

Log Message:
Fix regression introduced in rev 1.90:

http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/kern/kern_ksyms.c#rev1.90

in which the last element of ksyms_symtabs is skipped by mistake.


To generate a diff of this commit:
cvs rdiff -u -r1.93 -r1.94 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.93 src/sys/kern/kern_ksyms.c:1.94
--- src/sys/kern/kern_ksyms.c:1.93	Wed Jun  2 08:46:16 2021
+++ src/sys/kern/kern_ksyms.c	Wed Jun  2 15:43:33 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.93 2021/06/02 08:46:16 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.94 2021/06/02 15:43:33 rin Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.93 2021/06/02 08:46:16 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.94 2021/06/02 15:43:33 rin Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -1087,7 +1087,7 @@ ksymsread(dev_t dev, struct uio *uio, in
 	 */
 	filepos = sizeof(struct ksyms_hdr);
 	for (st = TAILQ_FIRST(_symtabs);
-	 st != ksyms_last_snapshot;
+	 st != TAILQ_NEXT(ksyms_last_snapshot, sd_queue);
 	 st = TAILQ_NEXT(st, sd_queue)) {
 		if (__predict_false(st->sd_gone))
 			continue;
@@ -1109,7 +1109,7 @@ ksymsread(dev_t dev, struct uio *uio, in
 	KASSERT(filepos <= sizeof(struct ksyms_hdr) +
 	ksyms_hdr.kh_shdr[SYMTAB].sh_size);
 	for (st = TAILQ_FIRST(_symtabs);
-	 st != ksyms_last_snapshot;
+	 st != TAILQ_NEXT(ksyms_last_snapshot, sd_queue);
 	 st = TAILQ_NEXT(st, sd_queue)) {
 		if (__predict_false(st->sd_gone))
 			continue;



CVS commit: src/sys/kern

2021-06-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jun  2 09:23:32 UTC 2021

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

Log Message:
psref(9): Make use-after-free panic message more obvious.

Previously it would almost always manifest as

mismatched psref target class: 0x0 (ref) != 0x... (expected)

and now it will manifest as

psref target already destroyed: 0x...


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/kern/subr_psref.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_psref.c
diff -u src/sys/kern/subr_psref.c:1.13 src/sys/kern/subr_psref.c:1.14
--- src/sys/kern/subr_psref.c:1.13	Fri May 17 03:34:26 2019
+++ src/sys/kern/subr_psref.c	Wed Jun  2 09:23:32 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_psref.c,v 1.13 2019/05/17 03:34:26 ozaki-r Exp $	*/
+/*	$NetBSD: subr_psref.c,v 1.14 2021/06/02 09:23:32 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_psref.c,v 1.13 2019/05/17 03:34:26 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_psref.c,v 1.14 2021/06/02 09:23:32 riastradh Exp $");
 
 #include 
 #include 
@@ -293,11 +293,11 @@ psref_acquire(struct psref *psref, const
 	"passive references are CPU-local,"
 	" but preemption is enabled and the caller is not"
 	" in a softint or CPU-bound LWP");
+	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
+	target);
 	KASSERTMSG((target->prt_class == class),
 	"mismatched psref target class: %p (ref) != %p (expected)",
 	target->prt_class, class);
-	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
-	target);
 
 	/* Block interrupts and acquire the current CPU's reference list.  */
 	s = splraiseipl(class->prc_iplcookie);
@@ -516,13 +516,13 @@ psref_target_destroy(struct psref_target
 
 	ASSERT_SLEEPABLE();
 
+	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
+	target);
 	KASSERTMSG((target->prt_class == class),
 	"mismatched psref target class: %p (ref) != %p (expected)",
 	target->prt_class, class);
 
 	/* Request psref_release to notify us when done.  */
-	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
-	target);
 	target->prt_draining = true;
 
 	/* Wait until there are no more references on any CPU.  */



CVS commit: src/sys/kern

2021-06-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jun  2 08:46:17 UTC 2021

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

Log Message:
Revert "ksyms(4): Don't skip symbol tables that are soon to be freed."

Apparently the equality kassert this restored doesn't work; to be
analyzed.


To generate a diff of this commit:
cvs rdiff -u -r1.92 -r1.93 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.92 src/sys/kern/kern_ksyms.c:1.93
--- src/sys/kern/kern_ksyms.c:1.92	Tue Jun  1 21:11:52 2021
+++ src/sys/kern/kern_ksyms.c	Wed Jun  2 08:46:16 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.92 2021/06/01 21:11:52 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.93 2021/06/02 08:46:16 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.92 2021/06/01 21:11:52 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.93 2021/06/02 08:46:16 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -1089,6 +1089,8 @@ ksymsread(dev_t dev, struct uio *uio, in
 	for (st = TAILQ_FIRST(_symtabs);
 	 st != ksyms_last_snapshot;
 	 st = TAILQ_NEXT(st, sd_queue)) {
+		if (__predict_false(st->sd_gone))
+			continue;
 		if (uio->uio_resid == 0)
 			return 0;
 		if (uio->uio_offset <= st->sd_symsize + filepos) {
@@ -1104,11 +1106,13 @@ ksymsread(dev_t dev, struct uio *uio, in
 	/*
 	 * Copy out the string table
 	 */
-	KASSERT(filepos == sizeof(struct ksyms_hdr) +
+	KASSERT(filepos <= sizeof(struct ksyms_hdr) +
 	ksyms_hdr.kh_shdr[SYMTAB].sh_size);
 	for (st = TAILQ_FIRST(_symtabs);
 	 st != ksyms_last_snapshot;
 	 st = TAILQ_NEXT(st, sd_queue)) {
+		if (__predict_false(st->sd_gone))
+			continue;
 		if (uio->uio_resid == 0)
 			return 0;
 		if (uio->uio_offset <= st->sd_strsize + filepos) {



CVS commit: src/sys/kern

2021-06-01 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jun  1 21:11:52 UTC 2021

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

Log Message:
ksyms(4): Don't skip symbol tables that are soon to be freed.

They will not actually be freed until /dev/ksyms is closed, so
continued access to them remains kosher.


To generate a diff of this commit:
cvs rdiff -u -r1.91 -r1.92 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.91 src/sys/kern/kern_ksyms.c:1.92
--- src/sys/kern/kern_ksyms.c:1.91	Tue Jun  1 21:11:07 2021
+++ src/sys/kern/kern_ksyms.c	Tue Jun  1 21:11:52 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.91 2021/06/01 21:11:07 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.92 2021/06/01 21:11:52 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.91 2021/06/01 21:11:07 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.92 2021/06/01 21:11:52 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -1089,8 +1089,6 @@ ksymsread(dev_t dev, struct uio *uio, in
 	for (st = TAILQ_FIRST(_symtabs);
 	 st != ksyms_last_snapshot;
 	 st = TAILQ_NEXT(st, sd_queue)) {
-		if (__predict_false(st->sd_gone))
-			continue;
 		if (uio->uio_resid == 0)
 			return 0;
 		if (uio->uio_offset <= st->sd_symsize + filepos) {
@@ -1106,13 +1104,11 @@ ksymsread(dev_t dev, struct uio *uio, in
 	/*
 	 * Copy out the string table
 	 */
-	KASSERT(filepos <= sizeof(struct ksyms_hdr) +
+	KASSERT(filepos == sizeof(struct ksyms_hdr) +
 	ksyms_hdr.kh_shdr[SYMTAB].sh_size);
 	for (st = TAILQ_FIRST(_symtabs);
 	 st != ksyms_last_snapshot;
 	 st = TAILQ_NEXT(st, sd_queue)) {
-		if (__predict_false(st->sd_gone))
-			continue;
 		if (uio->uio_resid == 0)
 			return 0;
 		if (uio->uio_offset <= st->sd_strsize + filepos) {



CVS commit: src/sys/kern

2021-06-01 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jun  1 21:11:07 UTC 2021

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

Log Message:
ksyms(4): Modify ksyms_symtabs only at IPL_HIGH.

This limits the opportunities for ddb to witness an inconsistent
state of the symbol table list.


To generate a diff of this commit:
cvs rdiff -u -r1.90 -r1.91 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.90 src/sys/kern/kern_ksyms.c:1.91
--- src/sys/kern/kern_ksyms.c:1.90	Tue Jun  1 21:10:23 2021
+++ src/sys/kern/kern_ksyms.c	Tue Jun  1 21:11:07 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.90 2021/06/01 21:10:23 riastradh Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.91 2021/06/01 21:11:07 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.90 2021/06/01 21:10:23 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.91 2021/06/01 21:11:07 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -93,6 +93,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c
 #include 
 #include 
 #include 
+#include 
 
 #ifdef DDB
 #include 
@@ -308,6 +309,7 @@ addsymtab(const char *name, void *symsta
 	int i, j, n, nglob;
 	char *str;
 	int nsyms = symsize / sizeof(Elf_Sym);
+	int s;
 
 	/* Sanity check for pre-allocated map table used during startup. */
 	if ((nmap == ksyms_nmap) && (nsyms >= KSYMS_MAX_ID)) {
@@ -443,7 +445,14 @@ addsymtab(const char *name, void *symsta
 	KASSERT(strcmp(name, "netbsd") == 0 || mutex_owned(_lock));
 	KASSERT(cold || mutex_owned(_lock));
 
+	/*
+	 * Ensure ddb never witnesses an inconsistent state of the
+	 * queue, unless memory is so corrupt that we crash in
+	 * TAILQ_INSERT_TAIL.
+	 */
+	s = splhigh();
 	TAILQ_INSERT_TAIL(_symtabs, tab, sd_queue);
+	splx(s);
 
 	ksyms_sizes_calc();
 	ksyms_loaded = true;
@@ -772,6 +781,7 @@ ksyms_modunload(const char *name)
 {
 	struct ksyms_symtab *st;
 	bool do_free = false;
+	int s;
 
 	mutex_enter(_lock);
 	TAILQ_FOREACH(st, _symtabs, sd_queue) {
@@ -782,7 +792,14 @@ ksyms_modunload(const char *name)
 		st->sd_gone = true;
 		ksyms_sizes_calc();
 		if (!ksyms_isopen) {
+			/*
+			 * Ensure ddb never witnesses an inconsistent
+			 * state of the queue, unless memory is so
+			 * corrupt that we crash in TAILQ_REMOVE.
+			 */
+			s = splhigh();
 			TAILQ_REMOVE(_symtabs, st, sd_queue);
+			splx(s);
 			do_free = true;
 		}
 		break;
@@ -1015,6 +1032,7 @@ ksymsclose(dev_t dev, int oflags, int de
 {
 	struct ksyms_symtab *st, *next;
 	TAILQ_HEAD(, ksyms_symtab) to_free = TAILQ_HEAD_INITIALIZER(to_free);
+	int s;
 
 	/* Discard references to symbol tables. */
 	mutex_enter(_lock);
@@ -1022,7 +1040,14 @@ ksymsclose(dev_t dev, int oflags, int de
 	ksyms_last_snapshot = NULL;
 	TAILQ_FOREACH_SAFE(st, _symtabs, sd_queue, next) {
 		if (st->sd_gone) {
+			/*
+			 * Ensure ddb never witnesses an inconsistent
+			 * state of the queue, unless memory is so
+			 * corrupt that we crash in TAILQ_REMOVE.
+			 */
+			s = splhigh();
 			TAILQ_REMOVE(_symtabs, st, sd_queue);
+			splx(s);
 			TAILQ_INSERT_TAIL(_free, st, sd_queue);
 		}
 	}



CVS commit: src/sys/kern

2021-06-01 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jun  1 21:10:23 UTC 2021

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

Log Message:
ksyms(4): Fix ksymsread synchronization.

Fixes crash on concurrent update and read of /dev/ksyms.

XXX Unclear why we have to skip sd_gone entries here -- it seems like
they should be preserved until ksymsclose.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 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.89 src/sys/kern/kern_ksyms.c:1.90
--- src/sys/kern/kern_ksyms.c:1.89	Wed Sep 23 09:52:02 2020
+++ src/sys/kern/kern_ksyms.c	Tue Jun  1 21:10:23 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.89 2020/09/23 09:52:02 simonb Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.90 2021/06/01 21:10:23 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.89 2020/09/23 09:52:02 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.90 2021/06/01 21:10:23 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -92,6 +92,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c
 #include 
 #include 
 #include 
+#include 
 
 #ifdef DDB
 #include 
@@ -111,6 +112,7 @@ static uint32_t *ksyms_nmap = NULL;
 
 static int ksyms_maxlen;
 static bool ksyms_isopen;
+static struct ksyms_symtab *ksyms_last_snapshot;
 static bool ksyms_initted;
 static bool ksyms_loaded;
 static kmutex_t ksyms_lock __cacheline_aligned;
@@ -140,7 +142,7 @@ struct ksyms_hdr ksyms_hdr;
 int ksyms_symsz;
 int ksyms_strsz;
 int ksyms_ctfsz;	/* this is not currently used by savecore(8) */
-TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
+TAILQ_HEAD(ksyms_symtab_queue, ksyms_symtab) ksyms_symtabs =
 TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
 
 static int
@@ -429,7 +431,7 @@ addsymtab(const char *name, void *symsta
 		for (new = 0; new < n; new++) {
 			uint32_t orig = nsym[new].st_size - 1;
 			uint32_t size = nmap[orig];
-	
+
 			nmap[orig] = new + 1;
 
 			/* restore the size */
@@ -438,9 +440,11 @@ addsymtab(const char *name, void *symsta
 	}
 #endif
 
-	/* ksymsread() is unlocked, so membar. */
-	membar_producer();
+	KASSERT(strcmp(name, "netbsd") == 0 || mutex_owned(_lock));
+	KASSERT(cold || mutex_owned(_lock));
+
 	TAILQ_INSERT_TAIL(_symtabs, tab, sd_queue);
+
 	ksyms_sizes_calc();
 	ksyms_loaded = true;
 }
@@ -767,6 +771,7 @@ void
 ksyms_modunload(const char *name)
 {
 	struct ksyms_symtab *st;
+	bool do_free = false;
 
 	mutex_enter(_lock);
 	TAILQ_FOREACH(st, _symtabs, sd_queue) {
@@ -778,14 +783,17 @@ ksyms_modunload(const char *name)
 		ksyms_sizes_calc();
 		if (!ksyms_isopen) {
 			TAILQ_REMOVE(_symtabs, st, sd_queue);
-			kmem_free(st->sd_nmap,
-  st->sd_nmapsize * sizeof(uint32_t));
-			kmem_free(st, sizeof(*st));
+			do_free = true;
 		}
 		break;
 	}
 	mutex_exit(_lock);
 	KASSERT(st != NULL);
+
+	if (do_free) {
+		kmem_free(st->sd_nmap, st->sd_nmapsize * sizeof(uint32_t));
+		kmem_free(st, sizeof(*st));
+	}
 }
 
 #ifdef DDB
@@ -864,6 +872,8 @@ ksyms_sizes_calc(void)
 	struct ksyms_symtab *st;
 	int i, delta;
 
+	KASSERT(cold || mutex_owned(_lock));
+
 	ksyms_symsz = ksyms_strsz = 0;
 	TAILQ_FOREACH(st, _symtabs, sd_queue) {
 		if (__predict_false(st->sd_gone))
@@ -981,6 +991,10 @@ ksymsopen(dev_t dev, int oflags, int dev
 	 * ksyms_isopen will prevent symbol tables from being freed.
 	 */
 	mutex_enter(_lock);
+	if (ksyms_isopen) {
+		mutex_exit(_lock);
+		return EBUSY;
+	}
 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
@@ -990,6 +1004,7 @@ ksymsopen(dev_t dev, int oflags, int dev
 	ksyms_hdr.kh_shdr[STRTAB].sh_offset;
 	ksyms_hdr.kh_shdr[SHCTF].sh_size = ksyms_ctfsz;
 	ksyms_isopen = true;
+	ksyms_last_snapshot = TAILQ_LAST(_symtabs, ksyms_symtab_queue);
 	mutex_exit(_lock);
 
 	return 0;
@@ -999,26 +1014,27 @@ static int
 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
 {
 	struct ksyms_symtab *st, *next;
-	bool resize;
+	TAILQ_HEAD(, ksyms_symtab) to_free = TAILQ_HEAD_INITIALIZER(to_free);
 
 	/* Discard references to symbol tables. */
 	mutex_enter(_lock);
 	ksyms_isopen = false;
-	resize = false;
-	for (st = TAILQ_FIRST(_symtabs); st != NULL; st = next) {
-		next = TAILQ_NEXT(st, sd_queue);
+	ksyms_last_snapshot = NULL;
+	TAILQ_FOREACH_SAFE(st, _symtabs, sd_queue, next) {
 		if (st->sd_gone) {
 			TAILQ_REMOVE(_symtabs, st, sd_queue);
-			kmem_free(st->sd_nmap,
-  st->sd_nmapsize * sizeof(uint32_t));
-			kmem_free(st, sizeof(*st));
-			resize = true;
+			TAILQ_INSERT_TAIL(_free, st, sd_queue);
 		}
 	}
-	if (resize)
+	if (!TAILQ_EMPTY(_free))
 		ksyms_sizes_calc();
 	mutex_exit(_lock);
 
+	

CVS commit: src/sys/kern

2021-05-18 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Tue May 18 08:59:44 UTC 2021

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

Log Message:
Remove a superfluous VOP_GETATTR() from elf_load_interp() and replace
the LK_EXCLUSIVE lock with a LK_SHARED lock.

The attributes requested were not used since Rev 1.25 of exec_elf32.c
from 1997/05/08 when mycroft GCd the va_mode check.


To generate a diff of this commit:
cvs rdiff -u -r1.101 -r1.102 src/sys/kern/exec_elf.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/exec_elf.c
diff -u src/sys/kern/exec_elf.c:1.101 src/sys/kern/exec_elf.c:1.102
--- src/sys/kern/exec_elf.c:1.101	Sun Jan 12 18:30:58 2020
+++ src/sys/kern/exec_elf.c	Tue May 18 08:59:44 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_elf.c,v 1.101 2020/01/12 18:30:58 ad Exp $	*/
+/*	$NetBSD: exec_elf.c,v 1.102 2021/05/18 08:59:44 hannken Exp $	*/
 
 /*-
  * Copyright (c) 1994, 2000, 2005, 2015, 2020 The NetBSD Foundation, Inc.
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.101 2020/01/12 18:30:58 ad Exp $");
+__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.102 2021/05/18 08:59:44 hannken Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pax.h"
@@ -421,7 +421,6 @@ elf_load_interp(struct lwp *l, struct ex
 {
 	int error, i;
 	struct vnode *vp;
-	struct vattr attr;
 	Elf_Ehdr eh;
 	Elf_Phdr *ph = NULL;
 	const Elf_Phdr *base_ph;
@@ -456,7 +455,7 @@ elf_load_interp(struct lwp *l, struct ex
 	}
 	/* We'll tidy this ourselves - otherwise we have locking issues */
 	epp->ep_interp = NULL;
-	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
+	vn_lock(vp, LK_SHARED | LK_RETRY);
 
 	/*
 	 * Similarly, if it's not marked as executable, or it's not a regular
@@ -469,11 +468,6 @@ elf_load_interp(struct lwp *l, struct ex
 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
 		goto bad;
 
-	/* get attributes */
-	/* XXX VOP_GETATTR() is the only thing that needs LK_EXCLUSIVE ^ */
-	if ((error = VOP_GETATTR(vp, , l->l_cred)) != 0)
-		goto bad;
-
 	/*
 	 * Check mount point.  Though we're not trying to exec this binary,
 	 * we will be executing code from it, so if the mount point



CVS commit: src/sys/kern

2021-05-17 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Tue May 18 05:16:10 UTC 2021

Modified Files:
src/sys/kern: files.kern

Log Message:
s/defopt/defflag/.  2021 called to remind me it's still 2021.


To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/sys/kern/files.kern

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/files.kern
diff -u src/sys/kern/files.kern:1.55 src/sys/kern/files.kern:1.56
--- src/sys/kern/files.kern:1.55	Mon May 17 08:50:36 2021
+++ src/sys/kern/files.kern	Tue May 18 05:16:09 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: files.kern,v 1.55 2021/05/17 08:50:36 mrg Exp $
+#	$NetBSD: files.kern,v 1.56 2021/05/18 05:16:09 mrg Exp $
 
 #
 # kernel sources
@@ -115,7 +115,7 @@ file	kern/subr_device.c		kern
 file	kern/subr_devsw.c		kern
 file	kern/subr_disk.c		kern
 file	kern/subr_disklabel.c		kern
-defopt opt_disklabel.h			DISKLABEL_EI
+defflag opt_disklabel.h			DISKLABEL_EI
 file	kern/subr_disk_open.c		kern
 file	kern/subr_emul.c		kern
 file	kern/subr_evcnt.c		kern



CVS commit: src/sys/kern

2021-05-03 Thread Frederic Cambus
Module Name:src
Committed By:   fcambus
Date:   Mon May  3 10:25:14 UTC 2021

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

Log Message:
Remove duplicate #ifdef block.


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 src/sys/kern/exec_script.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/exec_script.c
diff -u src/sys/kern/exec_script.c:1.82 src/sys/kern/exec_script.c:1.83
--- src/sys/kern/exec_script.c:1.82	Mon Mar 23 02:44:22 2020
+++ src/sys/kern/exec_script.c	Mon May  3 10:25:14 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_script.c,v 1.82 2020/03/23 02:44:22 pgoyette Exp $	*/
+/*	$NetBSD: exec_script.c,v 1.83 2021/05/03 10:25:14 fcambus Exp $	*/
 
 /*
  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
@@ -31,11 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.82 2020/03/23 02:44:22 pgoyette Exp $");
-
-#ifdef _KERNEL_OPT
-#include "opt_script.h"
-#endif
+__KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.83 2021/05/03 10:25:14 fcambus Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_script.h"



CVS commit: src/sys/kern

2021-05-02 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun May  2 10:23:56 UTC 2021

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

Log Message:
Fix copy in handling of POSIX_SPAWN_RESETIDS in posix_spawn(3)


To generate a diff of this commit:
cvs rdiff -u -r1.504 -r1.505 src/sys/kern/kern_exec.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_exec.c
diff -u src/sys/kern/kern_exec.c:1.504 src/sys/kern/kern_exec.c:1.505
--- src/sys/kern/kern_exec.c:1.504	Sat Dec  5 18:17:01 2020
+++ src/sys/kern/kern_exec.c	Sun May  2 10:23:55 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_exec.c,v 1.504 2020/12/05 18:17:01 thorpej Exp $	*/
+/*	$NetBSD: kern_exec.c,v 1.505 2021/05/02 10:23:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2019, 2020 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.504 2020/12/05 18:17:01 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.505 2021/05/02 10:23:55 martin Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -2172,7 +2172,7 @@ handle_posix_spawn_attrs(struct posix_sp
 
 	/* Reset user ID's */
 	if (attrs->sa_flags & POSIX_SPAWN_RESETIDS) {
-		error = do_setresuid(l, -1, kauth_cred_getgid(l->l_cred), -1,
+		error = do_setresgid(l, -1, kauth_cred_getgid(l->l_cred), -1,
 		 ID_E_EQ_R | ID_E_EQ_S);
 		if (error)
 			return error;



CVS commit: src/sys/kern

2021-04-27 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Wed Apr 28 03:21:57 UTC 2021

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

Log Message:
Validate the return value of cfprint functions before using it to
index the msgs[] array.  Use designated initializers to initialize
msgs[].


To generate a diff of this commit:
cvs rdiff -u -r1.279 -r1.280 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.279 src/sys/kern/subr_autoconf.c:1.280
--- src/sys/kern/subr_autoconf.c:1.279	Tue Apr 27 14:48:28 2021
+++ src/sys/kern/subr_autoconf.c	Wed Apr 28 03:21:57 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.279 2021/04/27 14:48:28 thorpej Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.280 2021/04/28 03:21:57 thorpej Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -79,7 +79,7 @@
 #define	__SUBR_AUTOCONF_PRIVATE	/* see  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.279 2021/04/27 14:48:28 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.280 2021/04/28 03:21:57 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1214,7 +1214,11 @@ config_rootsearch(cfsubmatch_t fn, const
 	return m.match;
 }
 
-static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
+static const char * const msgs[] = {
+[QUIET]		=	"",
+[UNCONF]	=	" not configured\n",
+[UNSUPP]	=	" unsupported\n",
+};
 
 /*
  * The given `aux' argument describes a device that has been found
@@ -1242,7 +1246,12 @@ config_vfound(device_t parent, void *aux
 	if (print) {
 		if (config_do_twiddle && cold)
 			twiddle();
-		aprint_normal("%s", msgs[(*print)(aux, device_xname(parent))]);
+
+		const int pret = (*print)(aux, device_xname(parent));
+		KASSERT(pret >= 0);
+		KASSERT(pret < __arraycount(msgs));
+		KASSERT(msgs[pret] != NULL);
+		aprint_normal("%s", msgs[pret]);
 	}
 
 	/*



CVS commit: src/sys/kern

2021-04-16 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Sat Apr 17 00:05:31 UTC 2021

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

Log Message:
avoid an error in the recent sys/evcnt.h change to drop the 0
array size from evcnt_sysctl:ev_strings that clang does not like.

the previous "struct xevcnt_sysctl" was well defined as the
evcnt_sysctl with array length of 0 could be placed as the
anywhere in other structures, but with [] it must remain last
and xevcnt_sysctl then places a char array afterwards.

avoid this entirely by removing struct xevcnt_sysctl, combining
the string array + struct evcnt_sysctl size manually for the
allocation, and moving all "ev_string" accesses to be via the
new [] array.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/kern/subr_evcnt.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_evcnt.c
diff -u src/sys/kern/subr_evcnt.c:1.16 src/sys/kern/subr_evcnt.c:1.17
--- src/sys/kern/subr_evcnt.c:1.16	Thu Apr 15 00:37:31 2021
+++ src/sys/kern/subr_evcnt.c	Sat Apr 17 00:05:31 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_evcnt.c,v 1.16 2021/04/15 00:37:31 rin Exp $ */
+/* $NetBSD: subr_evcnt.c,v 1.17 2021/04/17 00:05:31 mrg Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.16 2021/04/15 00:37:31 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.17 2021/04/17 00:05:31 mrg Exp $");
 
 #include 
 #include 
@@ -214,13 +214,10 @@ evcnt_detach(struct evcnt *ev)
 	mutex_exit(_lock);
 }
 
-struct xevcnt_sysctl {
-	struct evcnt_sysctl evs;
-	char ev_strings[2*EVCNT_STRING_MAX];
-};
+typedef char ev_strings[2*EVCNT_STRING_MAX];
 
 static size_t
-sysctl_fillevcnt(const struct evcnt *ev, struct xevcnt_sysctl *xevs,
+sysctl_fillevcnt(const struct evcnt *ev, struct evcnt_sysctl *evs,
 	size_t *copylenp)
 {
 	const bool allowaddr = get_expose_address(curproc);
@@ -228,17 +225,17 @@ sysctl_fillevcnt(const struct evcnt *ev,
 	+ ev->ev_grouplen + 1 + ev->ev_namelen + 1;
 	const size_t len = roundup2(copylen, sizeof(uint64_t));
 
-	if (xevs != NULL) {
-		xevs->evs.ev_count = ev->ev_count;
-		COND_SET_VALUE(xevs->evs.ev_addr, PTRTOUINT64(ev), allowaddr);
-		COND_SET_VALUE(xevs->evs.ev_parent, PTRTOUINT64(ev->ev_parent),
+	if (evs != NULL) {
+		evs->ev_count = ev->ev_count;
+		COND_SET_VALUE(evs->ev_addr, PTRTOUINT64(ev), allowaddr);
+		COND_SET_VALUE(evs->ev_parent, PTRTOUINT64(ev->ev_parent),
 		allowaddr);
-		xevs->evs.ev_type = ev->ev_type;
-		xevs->evs.ev_grouplen = ev->ev_grouplen;
-		xevs->evs.ev_namelen = ev->ev_namelen;
-		xevs->evs.ev_len = len / sizeof(uint64_t);
-		strcpy(xevs->evs.ev_strings, ev->ev_group);
-		strcpy(xevs->evs.ev_strings + ev->ev_grouplen + 1, ev->ev_name);
+		evs->ev_type = ev->ev_type;
+		evs->ev_grouplen = ev->ev_grouplen;
+		evs->ev_namelen = ev->ev_namelen;
+		evs->ev_len = len / sizeof(uint64_t);
+		strcpy(evs->ev_strings, ev->ev_group);
+		strcpy(evs->ev_strings + ev->ev_grouplen + 1, ev->ev_name);
 	}
 
 	*copylenp = copylen;
@@ -248,7 +245,8 @@ sysctl_fillevcnt(const struct evcnt *ev,
 static int
 sysctl_doevcnt(SYSCTLFN_ARGS)
 {   
-	struct xevcnt_sysctl *xevs0 = NULL, *xevs;
+	struct evcnt_sysctl *evs0 = NULL, *evs;
+	const size_t xevcnt_size = sizeof(*evs0) + sizeof(ev_strings);
 	const struct evcnt *ev;
 	int error;
 	int retries;
@@ -278,14 +276,14 @@ sysctl_doevcnt(SYSCTLFN_ARGS)
 
 	sysctl_unlock();
 
-	if (oldp != NULL && xevs0 == NULL)
-		xevs0 = kmem_zalloc(sizeof(*xevs0), KM_SLEEP);
+	if (oldp != NULL)
+		evs0 = kmem_zalloc(xevcnt_size, KM_SLEEP);
 
 	retries = 100;
  retry:
 	dp = oldp;
 	len = (oldp != NULL) ? *oldlenp : 0;
-	xevs = xevs0;
+	evs = evs0;
 	error = 0;
 	needed = 0;
 
@@ -300,22 +298,22 @@ sysctl_doevcnt(SYSCTLFN_ARGS)
 			continue;
 
 		/*
-		 * Prepare to copy.  If xevs is NULL, fillevcnt will just
+		 * Prepare to copy.  If evs is NULL, fillevcnt will just
 		 * how big the item is.
 		 */
 		size_t copylen;
-		const size_t elem_size = sysctl_fillevcnt(ev, xevs, );
+		const size_t elem_size = sysctl_fillevcnt(ev, evs, );
 		needed += elem_size;
 
 		if (len < elem_size) {
-			xevs = NULL;
+			evs = NULL;
 			continue;
 		}
 
-		KASSERT(xevs != NULL);
-		KASSERT(xevs->evs.ev_grouplen != 0);
-		KASSERT(xevs->evs.ev_namelen != 0);
-		KASSERT(xevs->evs.ev_strings[0] != 0);
+		KASSERT(evs != NULL);
+		KASSERT(evs->ev_grouplen != 0);
+		KASSERT(evs->ev_namelen != 0);
+		KASSERT(evs->ev_strings[0] != 0);
 
 		const uint32_t last_generation = evcnt_generation;
 		mutex_exit(_lock);
@@ -325,7 +323,7 @@ sysctl_doevcnt(SYSCTLFN_ARGS)
 		 * number.  If we did the latter we'd have to zero them
 		 * first or we'd leak random kernel memory.
 		 */
-		error = copyout(xevs, dp, copylen);
+		error = copyout(evs, dp, copylen);
 
 		mutex_enter(_lock);
 		if (error)
@@ -354,8 +352,8 @@ 

CVS commit: src/sys/kern

2021-04-14 Thread Rin Okuyama
Module Name:src
Committed By:   rin
Date:   Thu Apr 15 00:37:31 UTC 2021

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

Log Message:
intrcnt[] is changed from long to u_int. Use sizeof(var) instead of
sizeof(type) to catch up with this change.

No binary changes as all ports with __HAVE_LEGACY_INTRCNT are ILP32, IIUC.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/kern/subr_evcnt.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_evcnt.c
diff -u src/sys/kern/subr_evcnt.c:1.15 src/sys/kern/subr_evcnt.c:1.16
--- src/sys/kern/subr_evcnt.c:1.15	Fri Apr  2 10:39:22 2021
+++ src/sys/kern/subr_evcnt.c	Thu Apr 15 00:37:31 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_evcnt.c,v 1.15 2021/04/02 10:39:22 simonb Exp $ */
+/* $NetBSD: subr_evcnt.c,v 1.16 2021/04/15 00:37:31 rin Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.15 2021/04/02 10:39:22 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.16 2021/04/15 00:37:31 rin Exp $");
 
 #include 
 #include 
@@ -393,7 +393,7 @@ evcnt_attach_legacy_intrcnt(void)
 	size_t i;
 	const char *cp;
 
-	nintr = ((intptr_t)eintrcnt - (intptr_t)intrcnt) / sizeof(long);
+	nintr = ((intptr_t)eintrcnt - (intptr_t)intrcnt) / sizeof(intrcnt[0]);
 	intr_evcnts = kmem_alloc(sizeof(struct evcnt) * nintr, KM_SLEEP);
 	for (cp = intrnames, i = 0; i < nintr; i++) {
 		evcnt_attach_dynamic(_evcnts[i], EVCNT_TYPE_INTR,



CVS commit: src/sys/kern

2021-04-13 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Wed Apr 14 02:45:58 UTC 2021

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

Log Message:
regen


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/kern/systrace_args.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/systrace_args.c
diff -u src/sys/kern/systrace_args.c:1.46 src/sys/kern/systrace_args.c:1.47
--- src/sys/kern/systrace_args.c:1.46	Tue Apr 13 18:46:13 2021
+++ src/sys/kern/systrace_args.c	Tue Apr 13 22:45:58 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: systrace_args.c,v 1.46 2021/04/13 22:46:13 christos Exp $ */
+/* $NetBSD: systrace_args.c,v 1.47 2021/04/14 02:45:58 christos Exp $ */
 
 /*
  * System call argument to DTrace register array conversion.
@@ -16,7 +16,7 @@ systrace_args(register_t sysnum, const v
 	case 0: {
 		const struct sys_syscall_args *p = params;
 		iarg[0] = SCARG(p, code); /* int */
-		uarg[1] = (intptr_t) SCARG(p, args); /* register_t */
+		iarg[1] = SCARG(p, args[0]); /* register_t */
 		*n_args = 2;
 		break;
 	}
@@ -1460,7 +1460,7 @@ systrace_args(register_t sysnum, const v
 	case 198: {
 		const struct sys___syscall_args *p = params;
 		iarg[0] = SCARG(p, code); /* quad_t */
-		uarg[1] = (intptr_t) SCARG(p, args); /* register_t */
+		iarg[1] = SCARG(p, args[0]); /* register_t */
 		*n_args = 2;
 		break;
 	}



CVS commit: src/sys/kern

2021-04-13 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Wed Apr 14 02:45:40 UTC 2021

Modified Files:
src/sys/kern: makesyscalls.sh

Log Message:
use the first element of the array instead


To generate a diff of this commit:
cvs rdiff -u -r1.184 -r1.185 src/sys/kern/makesyscalls.sh

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/makesyscalls.sh
diff -u src/sys/kern/makesyscalls.sh:1.184 src/sys/kern/makesyscalls.sh:1.185
--- src/sys/kern/makesyscalls.sh:1.184	Tue Apr 13 18:45:32 2021
+++ src/sys/kern/makesyscalls.sh	Tue Apr 13 22:45:40 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: makesyscalls.sh,v 1.184 2021/04/13 22:45:32 christos Exp $
+#	$NetBSD: makesyscalls.sh,v 1.185 2021/04/14 02:45:40 christos Exp $
 #
 # Copyright (c) 1994, 1996, 2000 Christopher G. Demetriou
 # All rights reserved.
@@ -808,9 +808,9 @@ function printrumpsysmap(syscall, wfn, f
 function fixarray(arg) {
 	iii = index(arg, "[")
 	if (iii == 0) {
-		return arg
+		return arg 
 	} else {
-		return substr(arg, 1, iii - 1)
+		return substr(arg, 1, iii - 1) "[0]"
 	}
 }
 
@@ -831,7 +831,7 @@ function putsystrace(type, compatwrap_) 
  i - 1, \
  argname[i], arg) > systrace
 			else if (index(arg, "*") > 0 || arg == "caddr_t" ||
-			arg ~ /.*_handler_t$/ || index(argname[i], "[") > 0)
+			arg ~ /.*_handler_t$/)
 printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s); /* %s */\n", \
  i - 1, \
  fixarray(argname[i]), arg) > systrace
@@ -842,7 +842,7 @@ function putsystrace(type, compatwrap_) 
 			else
 printf("\t\tiarg[%d] = SCARG(p, %s); /* %s */\n", \
  i - 1, \
- argname[i], arg) > systrace
+ fixarray(argname[i]), arg) > systrace
 		}
 		printf("\t\tdefault:\n\t\t\tbreak;\n\t\t};\n") > systracetmp
 



CVS commit: src/sys/kern

2021-04-13 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Apr 13 22:46:13 UTC 2021

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

Log Message:
regen


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/sys/kern/systrace_args.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/systrace_args.c
diff -u src/sys/kern/systrace_args.c:1.45 src/sys/kern/systrace_args.c:1.46
--- src/sys/kern/systrace_args.c:1.45	Wed Feb 17 01:25:10 2021
+++ src/sys/kern/systrace_args.c	Tue Apr 13 18:46:13 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: systrace_args.c,v 1.45 2021/02/17 06:25:10 rillig Exp $ */
+/* $NetBSD: systrace_args.c,v 1.46 2021/04/13 22:46:13 christos Exp $ */
 
 /*
  * System call argument to DTrace register array conversion.
@@ -16,7 +16,7 @@ systrace_args(register_t sysnum, const v
 	case 0: {
 		const struct sys_syscall_args *p = params;
 		iarg[0] = SCARG(p, code); /* int */
-		iarg[1] = SCARG(p, args[SYS_MAXSYSARGS]); /* register_t */
+		uarg[1] = (intptr_t) SCARG(p, args); /* register_t */
 		*n_args = 2;
 		break;
 	}
@@ -1460,7 +1460,7 @@ systrace_args(register_t sysnum, const v
 	case 198: {
 		const struct sys___syscall_args *p = params;
 		iarg[0] = SCARG(p, code); /* quad_t */
-		iarg[1] = SCARG(p, args[SYS_MAXSYSARGS]); /* register_t */
+		uarg[1] = (intptr_t) SCARG(p, args); /* register_t */
 		*n_args = 2;
 		break;
 	}



CVS commit: src/sys/kern

2021-04-13 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Apr 13 22:45:32 UTC 2021

Modified Files:
src/sys/kern: makesyscalls.sh

Log Message:
Elide [NUM] in arguments for systrace, use the first address of the array
and cast to intptr_t instead.


To generate a diff of this commit:
cvs rdiff -u -r1.183 -r1.184 src/sys/kern/makesyscalls.sh

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/makesyscalls.sh
diff -u src/sys/kern/makesyscalls.sh:1.183 src/sys/kern/makesyscalls.sh:1.184
--- src/sys/kern/makesyscalls.sh:1.183	Wed Feb 17 01:25:10 2021
+++ src/sys/kern/makesyscalls.sh	Tue Apr 13 18:45:32 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: makesyscalls.sh,v 1.183 2021/02/17 06:25:10 rillig Exp $
+#	$NetBSD: makesyscalls.sh,v 1.184 2021/04/13 22:45:32 christos Exp $
 #
 # Copyright (c) 1994, 1996, 2000 Christopher G. Demetriou
 # All rights reserved.
@@ -805,6 +805,15 @@ function printrumpsysmap(syscall, wfn, f
 	syscall, wfn, funcalias, rumpentry) > rumpsysmap
 }
 
+function fixarray(arg) {
+	iii = index(arg, "[")
+	if (iii == 0) {
+		return arg
+	} else {
+		return substr(arg, 1, iii - 1)
+	}
+}
+
 function putsystrace(type, compatwrap_) {
 	printf("\t/* %s */\n\tcase %d: {\n", funcname, syscall) > systrace
 	printf("\t/* %s */\n\tcase %d:\n", funcname, syscall) > systracetmp
@@ -822,10 +831,10 @@ function putsystrace(type, compatwrap_) 
  i - 1, \
  argname[i], arg) > systrace
 			else if (index(arg, "*") > 0 || arg == "caddr_t" ||
-			arg ~ /.*_handler_t$/)
+			arg ~ /.*_handler_t$/ || index(argname[i], "[") > 0)
 printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s); /* %s */\n", \
  i - 1, \
- argname[i], arg) > systrace
+ fixarray(argname[i]), arg) > systrace
 			else if (substr(arg, 1, 1) == "u" || arg == "size_t")
 printf("\t\tuarg[%d] = SCARG(p, %s); /* %s */\n", \
  i - 1, \



CVS commit: src/sys/kern

2021-04-08 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Thu Apr  8 06:20:47 UTC 2021

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

Log Message:
Sort includes.


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 src/sys/kern/kern_tc.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_tc.c
diff -u src/sys/kern/kern_tc.c:1.60 src/sys/kern/kern_tc.c:1.61
--- src/sys/kern/kern_tc.c:1.60	Thu Apr  8 06:06:24 2021
+++ src/sys/kern/kern_tc.c	Thu Apr  8 06:20:47 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_tc.c,v 1.60 2021/04/08 06:06:24 simonb Exp $ */
+/* $NetBSD: kern_tc.c,v 1.61 2021/04/08 06:20:47 simonb Exp $ */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -40,14 +40,18 @@
 
 #include 
 /* __FBSDID("$FreeBSD: src/sys/kern/kern_tc.c,v 1.166 2005/09/19 22:16:31 andre Exp $"); */
-__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.60 2021/04/08 06:06:24 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.61 2021/04/08 06:20:47 simonb Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ntp.h"
 #endif
 
 #include 
+#include 
+#include 
+#include 
 #include 
+#include 
 #include 	/* XXX just to get AB_VERBOSE */
 #include 
 #include 
@@ -55,10 +59,6 @@ __KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 
 #include 
 #include 
 #include 
-#include 
-#include 
-#include 
-#include 
 #include 
 
 /*



CVS commit: src/sys/kern

2021-04-08 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Thu Apr  8 06:06:24 UTC 2021

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

Log Message:
tc_init(): KASSERT that the timecounter hasn't previously been
initialised by checking the tc_next field.  Prevents nastly endless
loops in the timecounter code.


To generate a diff of this commit:
cvs rdiff -u -r1.59 -r1.60 src/sys/kern/kern_tc.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_tc.c
diff -u src/sys/kern/kern_tc.c:1.59 src/sys/kern/kern_tc.c:1.60
--- src/sys/kern/kern_tc.c:1.59	Wed May 27 09:09:50 2020
+++ src/sys/kern/kern_tc.c	Thu Apr  8 06:06:24 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_tc.c,v 1.59 2020/05/27 09:09:50 rin Exp $ */
+/* $NetBSD: kern_tc.c,v 1.60 2021/04/08 06:06:24 simonb Exp $ */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 
 #include 
 /* __FBSDID("$FreeBSD: src/sys/kern/kern_tc.c,v 1.166 2005/09/19 22:16:31 andre Exp $"); */
-__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.59 2020/05/27 09:09:50 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.60 2021/04/08 06:06:24 simonb Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ntp.h"
@@ -551,6 +551,9 @@ tc_init(struct timecounter *tc)
 {
 	u_int u;
 
+	KASSERTMSG(tc->tc_next == NULL, "timecounter %s already initialised",
+	tc->tc_name);
+
 	u = tc->tc_frequency / tc->tc_counter_mask;
 	/* XXX: We need some margin here, 10% is a guess */
 	u *= 11;



CVS commit: src/sys/kern

2021-04-06 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Tue Apr  6 07:57:03 UTC 2021

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

Log Message:
Check the CTF magic number in an endian friendly way (and don't use
magic numbers to check the magic number!).


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/kern/kern_ctf.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_ctf.c
diff -u src/sys/kern/kern_ctf.c:1.7 src/sys/kern/kern_ctf.c:1.8
--- src/sys/kern/kern_ctf.c:1.7	Mon May 28 21:05:00 2018
+++ src/sys/kern/kern_ctf.c	Tue Apr  6 07:57:03 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ctf.c,v 1.7 2018/05/28 21:05:00 chs Exp $	*/
+/*	$NetBSD: kern_ctf.c,v 1.8 2021/04/06 07:57:03 simonb Exp $	*/
 /*-
  * Copyright (c) 2008 John Birrell 
  * All rights reserved.
@@ -81,6 +81,7 @@ mod_ctf_get(struct module *mod, mod_ctf_
 
 	void *ctfbuf = NULL;
 	uint8_t *ctfaddr;
+	uint16_t ctfmagic;
 	size_t ctfsize;
 
 	/*
@@ -137,8 +138,9 @@ mod_ctf_get(struct module *mod, mod_ctf_
 		goto out;
 	}
 
-	/* Check the CTF magic number. (XXX check for big endian!) */
-	if (ctfaddr[0] != 0xf1 || ctfaddr[1] != 0xcf) {
+	/* Check the CTF magic number. */
+	memcpy(, ctfaddr, sizeof ctfmagic);
+	if (ctfmagic != CTF_MAGIC) {
 		error = EINVAL;
 		goto out;
 	}



CVS commit: src/sys/kern

2021-04-04 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Mon Apr  5 05:47:32 UTC 2021

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

Log Message:
Don't call strncmp() where the length arg is the size of a pointer
instead of the max string length.  Switch to strcmp() - both strings
are known nul-terminated.

Thanks tnn@ (and clang!).


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/kern/subr_hash.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_hash.c
diff -u src/sys/kern/subr_hash.c:1.8 src/sys/kern/subr_hash.c:1.9
--- src/sys/kern/subr_hash.c:1.8	Thu Apr  1 06:22:09 2021
+++ src/sys/kern/subr_hash.c	Mon Apr  5 05:47:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_hash.c,v 1.8 2021/04/01 06:22:09 simonb Exp $	*/
+/*	$NetBSD: subr_hash.c,v 1.9 2021/04/05 05:47:31 simonb Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.8 2021/04/01 06:22:09 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.9 2021/04/05 05:47:31 simonb Exp $");
 
 #include 
 #include 
@@ -222,8 +222,7 @@ hashstat_sysctl(SYSCTLFN_ARGS)
 	sysctl_unlock();
 	rw_enter(_lock, RW_READER);
 	TAILQ_FOREACH(hash, _list, hs_next) {
-		if (query &&
-		(strncmp(hash->hs_name, queryname, sizeof(hash->hs_name)) != 0)) {
+		if (query && (strcmp(hash->hs_name, queryname) != 0)) {
 			continue;
 		}
 



CVS commit: src/sys/kern

2021-04-03 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sat Apr  3 12:06:53 UTC 2021

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

Log Message:
Print the "preposterous TOD clock time" message only if the RTC is
before very late 2020 (1/1/2021 minus a few leap days) instead of
late 1994.  Someone(TM) should remember to adjust this in somewhat
less than 26 years.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/kern/kern_todr.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_todr.c
diff -u src/sys/kern/kern_todr.c:1.46 src/sys/kern/kern_todr.c:1.47
--- src/sys/kern/kern_todr.c:1.46	Fri Jan  3 01:24:48 2020
+++ src/sys/kern/kern_todr.c	Sat Apr  3 12:06:53 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_todr.c,v 1.46 2020/01/03 01:24:48 thorpej Exp $	*/
+/*	$NetBSD: kern_todr.c,v 1.47 2021/04/03 12:06:53 simonb Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
 #include "opt_todr.h"
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.46 2020/01/03 01:24:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.47 2021/04/03 12:06:53 simonb Exp $");
 
 #include 
 #include 
@@ -90,6 +90,9 @@ static kmutex_t todr_mutex;
 static todr_chip_handle_t todr_handle;
 static bool todr_initialized;
 
+/* The minimum reasonable RTC date before preposterousness */
+#define	PREPOSTEROUS_YEARS	(2021 - POSIX_BASE_YEAR)
+
 /*
  * todr_init:
  *	Initialize TOD clock data.
@@ -213,7 +216,7 @@ todr_set_systime(time_t base)
 
 	if ((todr_handle == NULL) ||
 	(todr_gettime(todr_handle, ) != 0) ||
-	(tv.tv_sec < (25 * SECS_PER_COMMON_YEAR))) {
+	(tv.tv_sec < (PREPOSTEROUS_YEARS * SECS_PER_COMMON_YEAR))) {
 
 		if (todr_handle != NULL)
 			printf("WARNING: preposterous TOD clock time\n");



CVS commit: src/sys/kern

2021-04-03 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sat Apr  3 11:19:11 UTC 2021

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

Log Message:
CTASSERT that NSIG <= 128.  There are many hard-coded assumptions that
there are <= 4 x 32bit signal mask bits.


To generate a diff of this commit:
cvs rdiff -u -r1.396 -r1.397 src/sys/kern/kern_sig.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_sig.c
diff -u src/sys/kern/kern_sig.c:1.396 src/sys/kern/kern_sig.c:1.397
--- src/sys/kern/kern_sig.c:1.396	Mon Jan 11 17:18:51 2021
+++ src/sys/kern/kern_sig.c	Sat Apr  3 11:19:11 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.396 2021/01/11 17:18:51 skrll Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.397 2021/04/03 11:19:11 simonb Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.396 2021/01/11 17:18:51 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.397 2021/04/03 11:19:11 simonb Exp $");
 
 #include "opt_execfmt.h"
 #include "opt_ptrace.h"
@@ -110,6 +110,9 @@ __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v
 
 #include 
 
+/* Many hard-coded assumptions that there are <= 4 x 32bit signal mask bits */
+__CTASSERT(NSIG <= 128);
+
 #define	SIGQUEUE_MAX	32
 static pool_cache_t	sigacts_cache	__read_mostly;
 static pool_cache_t	ksiginfo_cache	__read_mostly;



CVS commit: src/sys/kern

2021-04-02 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Fri Apr  2 10:39:22 UTC 2021

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

Log Message:
Make extern declaration of intrcnt a u_int instead of a long.  Matches
some declartions in some ports.  Fixes build on news68k (and probably
newsmips).


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/kern/subr_evcnt.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_evcnt.c
diff -u src/sys/kern/subr_evcnt.c:1.14 src/sys/kern/subr_evcnt.c:1.15
--- src/sys/kern/subr_evcnt.c:1.14	Thu Apr  1 04:41:38 2021
+++ src/sys/kern/subr_evcnt.c	Fri Apr  2 10:39:22 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_evcnt.c,v 1.14 2021/04/01 04:41:38 simonb Exp $ */
+/* $NetBSD: subr_evcnt.c,v 1.15 2021/04/02 10:39:22 simonb Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.14 2021/04/01 04:41:38 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.15 2021/04/02 10:39:22 simonb Exp $");
 
 #include 
 #include 
@@ -380,7 +380,7 @@ SYSCTL_SETUP(sysctl_evcnt_setup, "sysctl
 }
 
 #ifdef __HAVE_LEGACY_INTRCNT
-extern long intrcnt[], eintrcnt[];
+extern u_int intrcnt[], eintrcnt[];
 extern char intrnames[];
 static size_t nintr;
 struct evcnt *intr_evcnts;



CVS commit: src/sys/kern

2021-04-01 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Thu Apr  1 06:26:14 UTC 2021

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

Log Message:
Add a sysctl hashstat collector for vcache.


To generate a diff of this commit:
cvs rdiff -u -r1.126 -r1.127 src/sys/kern/vfs_vnode.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_vnode.c
diff -u src/sys/kern/vfs_vnode.c:1.126 src/sys/kern/vfs_vnode.c:1.127
--- src/sys/kern/vfs_vnode.c:1.126	Tue Aug  4 03:00:10 2020
+++ src/sys/kern/vfs_vnode.c	Thu Apr  1 06:26:14 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnode.c,v 1.126 2020/08/04 03:00:10 riastradh Exp $	*/
+/*	$NetBSD: vfs_vnode.c,v 1.127 2021/04/01 06:26:14 simonb Exp $	*/
 
 /*-
  * Copyright (c) 1997-2011, 2019, 2020 The NetBSD Foundation, Inc.
@@ -148,7 +148,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.126 2020/08/04 03:00:10 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.127 2021/04/01 06:26:14 simonb Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pax.h"
@@ -1194,6 +1194,38 @@ vcache_hash(const struct vcache_key *key
 	return hash;
 }
 
+static int
+vcache_stats(struct hashstat_sysctl *hs, bool fill)
+{
+	vnode_impl_t *vip;
+	uint64_t chain;
+
+	strlcpy(hs->hash_name, "vcache", sizeof(hs->hash_name));
+	strlcpy(hs->hash_desc, "vnode cache hash", sizeof(hs->hash_desc));
+	if (!fill)
+		return 0;
+
+	hs->hash_size = vcache_hashmask + 1;
+
+	for (size_t i = 0; i < hs->hash_size; i++) {
+		chain = 0;
+		mutex_enter(_lock);
+		SLIST_FOREACH(vip, _hashtab[i], vi_hash) {
+			chain++;
+		}
+		mutex_exit(_lock);
+		if (chain > 0) {
+			hs->hash_used++;
+			hs->hash_items += chain;
+			if (chain > hs->hash_maxchain)
+hs->hash_maxchain = chain;
+		}
+		preempt_point();
+	}
+
+	return 0;
+}
+
 static void
 vcache_init(void)
 {
@@ -1206,6 +1238,7 @@ vcache_init(void)
 	vcache_hashsize = desiredvnodes;
 	vcache_hashtab = hashinit(desiredvnodes, HASH_SLIST, true,
 	_hashmask);
+	hashstat_register("vcache", vcache_stats);
 }
 
 static void



CVS commit: src/sys/kern

2021-04-01 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Thu Apr  1 06:26:00 UTC 2021

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

Log Message:
Add a sysctl hashstat collector for bufhash.


To generate a diff of this commit:
cvs rdiff -u -r1.297 -r1.298 src/sys/kern/vfs_bio.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_bio.c
diff -u src/sys/kern/vfs_bio.c:1.297 src/sys/kern/vfs_bio.c:1.298
--- src/sys/kern/vfs_bio.c:1.297	Fri Jul 31 04:07:30 2020
+++ src/sys/kern/vfs_bio.c	Thu Apr  1 06:25:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_bio.c,v 1.297 2020/07/31 04:07:30 chs Exp $	*/
+/*	$NetBSD: vfs_bio.c,v 1.298 2021/04/01 06:25:59 simonb Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2008, 2009, 2019, 2020 The NetBSD Foundation, Inc.
@@ -123,7 +123,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.297 2020/07/31 04:07:30 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.298 2021/04/01 06:25:59 simonb Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_bufcache.h"
@@ -252,6 +252,8 @@ biohist_init(void)
 LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
 u_long	bufhash;
 
+static int bufhash_stats(struct hashstat_sysctl *, bool);
+
 static kcondvar_t needbuffer_cv;
 
 /*
@@ -536,6 +538,7 @@ bufinit(void)
 
 	sysctl_kern_buf_setup();
 	sysctl_vm_buf_setup();
+	hashstat_register("bufhash", bufhash_stats);
 }
 
 void
@@ -1953,6 +1956,40 @@ sysctl_vm_buf_setup(void)
 		   CTL_VM, CTL_CREATE, CTL_EOL);
 }
 
+static int
+bufhash_stats(struct hashstat_sysctl *hs, bool fill)
+{
+	buf_t *bp;
+	uint64_t chain;
+
+	strlcpy(hs->hash_name, "bufhash", sizeof(hs->hash_name));
+	strlcpy(hs->hash_desc, "buffer hash", sizeof(hs->hash_desc));
+	if (!fill)
+		return 0;
+
+	hs->hash_size = bufhash + 1;
+
+	for (size_t i = 0; i < hs->hash_size; i++) {
+		chain = 0;
+
+		mutex_enter(_lock);
+		LIST_FOREACH(bp, [i], b_hash) {
+			chain++;
+		}
+		mutex_exit(_lock);
+
+		if (chain > 0) {
+			hs->hash_used++;
+			hs->hash_items += chain;
+			if (chain > hs->hash_maxchain)
+hs->hash_maxchain = chain;
+		}
+		preempt_point();
+	}
+
+	return 0;
+}
+
 #ifdef DEBUG
 /*
  * Print out statistics on the current allocation of the buffer pool.



CVS commit: src/sys/kern

2021-04-01 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Thu Apr  1 06:25:46 UTC 2021

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

Log Message:
Add a sysctl hashstat collector for uihash.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/kern/kern_uidinfo.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_uidinfo.c
diff -u src/sys/kern/kern_uidinfo.c:1.11 src/sys/kern/kern_uidinfo.c:1.12
--- src/sys/kern/kern_uidinfo.c:1.11	Fri Mar  1 03:03:19 2019
+++ src/sys/kern/kern_uidinfo.c	Thu Apr  1 06:25:45 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_uidinfo.c,v 1.11 2019/03/01 03:03:19 christos Exp $	*/
+/*	$NetBSD: kern_uidinfo.c,v 1.12 2021/04/01 06:25:45 simonb Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -35,7 +35,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.11 2019/03/01 03:03:19 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.12 2021/04/01 06:25:45 simonb Exp $");
 
 #include 
 #include 
@@ -132,6 +132,37 @@ sysctl_kern_uidinfo_setup(void)
 		   CTL_CREATE, CTL_EOL);
 }
 
+static int
+uid_stats(struct hashstat_sysctl *hs, bool fill)
+{
+	struct uidinfo *uip;
+	uint64_t chain;
+
+	strlcpy(hs->hash_name, "uihash", sizeof(hs->hash_name));
+	strlcpy(hs->hash_desc, "user info (uid->used proc) hash",
+	sizeof(hs->hash_desc));
+	if (!fill)
+		return 0;
+
+	hs->hash_size = uihash + 1;
+
+	for (size_t i = 0; i < hs->hash_size; i++) {
+		chain = 0;
+		SLIST_FOREACH(uip, [i], ui_hash) {
+			membar_datadep_consumer();
+			chain++;
+		}
+		if (chain > 0) {
+			hs->hash_used++;
+			hs->hash_items += chain;
+			if (chain > hs->hash_maxchain)
+hs->hash_maxchain = chain;
+		}
+	}
+
+	return 0;
+}
+
 void
 uid_init(void)
 {
@@ -151,6 +182,7 @@ uid_init(void)
 	 */
 	(void)uid_find(0);
 	sysctl_kern_uidinfo_setup();
+	hashstat_register("uihash", uid_stats);
 }
 
 struct uidinfo *



CVS commit: src/sys/kern

2021-03-18 Thread Nia Alarie
Module Name:src
Committed By:   nia
Date:   Thu Mar 18 14:05:38 UTC 2021

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

Log Message:
restore flags-as-bitmask, just in case another function is passing its
flags here.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/sys/kern/subr_time.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_time.c
diff -u src/sys/kern/subr_time.c:1.29 src/sys/kern/subr_time.c:1.30
--- src/sys/kern/subr_time.c:1.29	Thu Mar 18 14:01:18 2021
+++ src/sys/kern/subr_time.c	Thu Mar 18 14:05:37 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.29 2021/03/18 14:01:18 nia Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.30 2021/03/18 14:05:37 nia Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.29 2021/03/18 14:01:18 nia Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.30 2021/03/18 14:05:37 nia Exp $");
 
 #include 
 #include 
@@ -332,7 +332,7 @@ ts2timo(clockid_t clock_id, int flags, s
 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 10L)
 		return EINVAL;
 
-	if (flags == TIMER_ABSTIME || start != NULL) {
+	if ((flags & TIMER_ABSTIME) != 0 || start != NULL) {
 		error = clock_gettime1(clock_id, );
 		if (error != 0)
 			return error;
@@ -340,7 +340,7 @@ ts2timo(clockid_t clock_id, int flags, s
 			*start = tsd;
 	}
 
-	if (flags == TIMER_ABSTIME) {
+	if ((flags & TIMER_ABSTIME) != 0) {
 		if ((tsd.tv_sec > 0 && ts->tv_sec < LLONG_MIN + tsd.tv_sec) ||
 		(tsd.tv_sec < 0 && ts->tv_sec > LLONG_MAX + tsd.tv_sec))
 			return EINVAL;



CVS commit: src/sys/kern

2021-03-18 Thread Nia Alarie
Module Name:src
Committed By:   nia
Date:   Thu Mar 18 14:01:18 UTC 2021

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

Log Message:
ts2timo(9): refactor TIMER_ABSTIME handling

- only use *start for output of the original time.
  for clarity purposes, use the temporary variable for everything else.
- add a check for integer underflow

Reported-by: syzbot+17b5072d5ed262a96...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/kern/subr_time.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_time.c
diff -u src/sys/kern/subr_time.c:1.28 src/sys/kern/subr_time.c:1.29
--- src/sys/kern/subr_time.c:1.28	Thu Mar 18 13:45:15 2021
+++ src/sys/kern/subr_time.c	Thu Mar 18 14:01:18 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.28 2021/03/18 13:45:15 nia Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.29 2021/03/18 14:01:18 nia Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.28 2021/03/18 13:45:15 nia Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.29 2021/03/18 14:01:18 nia Exp $");
 
 #include 
 #include 
@@ -332,17 +332,20 @@ ts2timo(clockid_t clock_id, int flags, s
 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 10L)
 		return EINVAL;
 
-	if (start == NULL)
-		start = 
-
-	if (flags != TIMER_RELTIME || start != ) {
-		error = clock_gettime1(clock_id, start);
+	if (flags == TIMER_ABSTIME || start != NULL) {
+		error = clock_gettime1(clock_id, );
 		if (error != 0)
 			return error;
+		if (start != NULL)
+			*start = tsd;
 	}
 
-	if (flags != TIMER_RELTIME)
-		timespecsub(ts, start, ts);
+	if (flags == TIMER_ABSTIME) {
+		if ((tsd.tv_sec > 0 && ts->tv_sec < LLONG_MIN + tsd.tv_sec) ||
+		(tsd.tv_sec < 0 && ts->tv_sec > LLONG_MAX + tsd.tv_sec))
+			return EINVAL;
+		timespecsub(ts, , ts);
+	}
 
 	error = itimespecfix(ts);
 	if (error != 0)



CVS commit: src/sys/kern

2021-03-18 Thread Nia Alarie
Module Name:src
Committed By:   nia
Date:   Thu Mar 18 13:45:15 UTC 2021

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

Log Message:
revert previous


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/kern/subr_time.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_time.c
diff -u src/sys/kern/subr_time.c:1.27 src/sys/kern/subr_time.c:1.28
--- src/sys/kern/subr_time.c:1.27	Thu Mar 18 12:37:51 2021
+++ src/sys/kern/subr_time.c	Thu Mar 18 13:45:15 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.27 2021/03/18 12:37:51 nia Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.28 2021/03/18 13:45:15 nia Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.27 2021/03/18 12:37:51 nia Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.28 2021/03/18 13:45:15 nia Exp $");
 
 #include 
 #include 
@@ -327,11 +327,15 @@ ts2timo(clockid_t clock_id, int flags, s
 int *timo, struct timespec *start)
 {
 	int error;
+	struct timespec tsd;
 
 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 10L)
 		return EINVAL;
 
-	if (flags != TIMER_RELTIME || start != NULL) {
+	if (start == NULL)
+		start = 
+
+	if (flags != TIMER_RELTIME || start != ) {
 		error = clock_gettime1(clock_id, start);
 		if (error != 0)
 			return error;



CVS commit: src/sys/kern

2021-03-18 Thread Nia Alarie
Module Name:src
Committed By:   nia
Date:   Thu Mar 18 12:37:51 UTC 2021

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

Log Message:
ts2timo(9): further deobfuscation.

we want to check against NULL, so use it, rather than some random value on
the stack


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/kern/subr_time.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_time.c
diff -u src/sys/kern/subr_time.c:1.26 src/sys/kern/subr_time.c:1.27
--- src/sys/kern/subr_time.c:1.26	Thu Mar 18 11:53:16 2021
+++ src/sys/kern/subr_time.c	Thu Mar 18 12:37:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.26 2021/03/18 11:53:16 nia Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.27 2021/03/18 12:37:51 nia Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.26 2021/03/18 11:53:16 nia Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.27 2021/03/18 12:37:51 nia Exp $");
 
 #include 
 #include 
@@ -327,15 +327,11 @@ ts2timo(clockid_t clock_id, int flags, s
 int *timo, struct timespec *start)
 {
 	int error;
-	struct timespec tsd;
 
 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 10L)
 		return EINVAL;
 
-	if (start == NULL)
-		start = 
-
-	if (flags != TIMER_RELTIME || start != ) {
+	if (flags != TIMER_RELTIME || start != NULL) {
 		error = clock_gettime1(clock_id, start);
 		if (error != 0)
 			return error;



CVS commit: src/sys/kern

2021-03-18 Thread Nia Alarie
Module Name:src
Committed By:   nia
Date:   Thu Mar 18 11:53:16 UTC 2021

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

Log Message:
ts2timo(9): refactor for clarity

- 'flags' is not a boolean...
- actually, it is, but it should simply be named "absolute".
- convert tests for if (flags) to if (flags != TIMER_RELTIME)
- hoist function calls out of if expressions (requested by uwe)

still needs fixing:

- need to check for overflow before timespecsub.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/sys/kern/subr_time.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_time.c
diff -u src/sys/kern/subr_time.c:1.25 src/sys/kern/subr_time.c:1.26
--- src/sys/kern/subr_time.c:1.25	Sat May 23 23:42:43 2020
+++ src/sys/kern/subr_time.c	Thu Mar 18 11:53:16 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.25 2020/05/23 23:42:43 ad Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.26 2021/03/18 11:53:16 nia Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.25 2020/05/23 23:42:43 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.26 2021/03/18 11:53:16 nia Exp $");
 
 #include 
 #include 
@@ -332,18 +332,20 @@ ts2timo(clockid_t clock_id, int flags, s
 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 10L)
 		return EINVAL;
 
-	flags &= TIMER_ABSTIME;
 	if (start == NULL)
 		start = 
 
-	if (flags || start != )
-		if ((error = clock_gettime1(clock_id, start)) != 0)
+	if (flags != TIMER_RELTIME || start != ) {
+		error = clock_gettime1(clock_id, start);
+		if (error != 0)
 			return error;
+	}
 
-	if (flags)
+	if (flags != TIMER_RELTIME)
 		timespecsub(ts, start, ts);
 
-	if ((error = itimespecfix(ts)) != 0)
+	error = itimespecfix(ts);
+	if (error != 0)
 		return error;
 
 	if (ts->tv_sec == 0 && ts->tv_nsec == 0)



CVS commit: src/sys/kern

2021-03-03 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Thu Mar  4 01:37:42 UTC 2021

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

Log Message:
 Revert accidentally committed debug code. Sorry.


To generate a diff of this commit:
cvs rdiff -u -r1.242 -r1.243 src/sys/kern/uipc_mbuf.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/uipc_mbuf.c
diff -u src/sys/kern/uipc_mbuf.c:1.242 src/sys/kern/uipc_mbuf.c:1.243
--- src/sys/kern/uipc_mbuf.c:1.242	Thu Mar  4 01:35:31 2021
+++ src/sys/kern/uipc_mbuf.c	Thu Mar  4 01:37:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_mbuf.c,v 1.242 2021/03/04 01:35:31 msaitoh Exp $	*/
+/*	$NetBSD: uipc_mbuf.c,v 1.243 2021/03/04 01:37:42 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 1999, 2001, 2018 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.242 2021/03/04 01:35:31 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.243 2021/03/04 01:37:42 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_mbuftrace.h"
@@ -120,7 +120,6 @@ static const char mclpool_warnmsg[] =
 
 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
 
-void *watchpoint = (void *)0xdeadbeefdeadbeef;
 static percpu_t *mbstat_percpu;
 
 #ifdef MBUFTRACE
@@ -1993,7 +1992,6 @@ m_free(struct mbuf *m)
 	return n;
 }
 
-#if 0
 void
 m_freem(struct mbuf *m)
 {
@@ -2003,21 +2001,6 @@ m_freem(struct mbuf *m)
 		m = m_free(m);
 	} while (m);
 }
-#else
-void
-M_FREEM(struct mbuf *m, const char *func, int line)
-{
-	if (m == NULL)
-		return;
-	do {
-		if (((m->m_flags & M_EXT) != 0) &&
-		(m->m_ext.ext_arg == watchpoint))
-			printf("catch %p (%s line %d)\n", watchpoint,
-			func, line);
-		m = m_free(m);
-	} while (m);
-}
-#endif
 
 #if defined(DDB)
 void



CVS commit: src/sys/kern

2021-03-03 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Thu Mar  4 01:35:32 UTC 2021

Modified Files:
src/sys/kern: uipc_mbuf.c uipc_socket2.c

Log Message:
Add missing opt_inet.h.


To generate a diff of this commit:
cvs rdiff -u -r1.241 -r1.242 src/sys/kern/uipc_mbuf.c
cvs rdiff -u -r1.138 -r1.139 src/sys/kern/uipc_socket2.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/uipc_mbuf.c
diff -u src/sys/kern/uipc_mbuf.c:1.241 src/sys/kern/uipc_mbuf.c:1.242
--- src/sys/kern/uipc_mbuf.c:1.241	Tue May  5 20:36:48 2020
+++ src/sys/kern/uipc_mbuf.c	Thu Mar  4 01:35:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_mbuf.c,v 1.241 2020/05/05 20:36:48 jdolecek Exp $	*/
+/*	$NetBSD: uipc_mbuf.c,v 1.242 2021/03/04 01:35:31 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 1999, 2001, 2018 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.241 2020/05/05 20:36:48 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.242 2021/03/04 01:35:31 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_mbuftrace.h"
@@ -120,6 +120,7 @@ static const char mclpool_warnmsg[] =
 
 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
 
+void *watchpoint = (void *)0xdeadbeefdeadbeef;
 static percpu_t *mbstat_percpu;
 
 #ifdef MBUFTRACE
@@ -1992,6 +1993,7 @@ m_free(struct mbuf *m)
 	return n;
 }
 
+#if 0
 void
 m_freem(struct mbuf *m)
 {
@@ -2001,6 +2003,21 @@ m_freem(struct mbuf *m)
 		m = m_free(m);
 	} while (m);
 }
+#else
+void
+M_FREEM(struct mbuf *m, const char *func, int line)
+{
+	if (m == NULL)
+		return;
+	do {
+		if (((m->m_flags & M_EXT) != 0) &&
+		(m->m_ext.ext_arg == watchpoint))
+			printf("catch %p (%s line %d)\n", watchpoint,
+			func, line);
+		m = m_free(m);
+	} while (m);
+}
+#endif
 
 #if defined(DDB)
 void

Index: src/sys/kern/uipc_socket2.c
diff -u src/sys/kern/uipc_socket2.c:1.138 src/sys/kern/uipc_socket2.c:1.139
--- src/sys/kern/uipc_socket2.c:1.138	Wed Aug 26 22:54:30 2020
+++ src/sys/kern/uipc_socket2.c	Thu Mar  4 01:35:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_socket2.c,v 1.138 2020/08/26 22:54:30 christos Exp $	*/
+/*	$NetBSD: uipc_socket2.c,v 1.139 2021/03/04 01:35:31 msaitoh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -58,10 +58,11 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.138 2020/08/26 22:54:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.139 2021/03/04 01:35:31 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
+#include "opt_inet.h"
 #include "opt_mbuftrace.h"
 #include "opt_sb_max.h"
 #endif



CVS commit: src/sys/kern

2021-03-01 Thread Rin Okuyama
Module Name:src
Committed By:   rin
Date:   Tue Mar  2 01:20:35 UTC 2021

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

Log Message:
KNF. No binary changes.


To generate a diff of this commit:
cvs rdiff -u -r1.79 -r1.80 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.79 src/sys/kern/subr_lockdebug.c:1.80
--- src/sys/kern/subr_lockdebug.c:1.79	Fri Jan  1 14:08:33 2021
+++ src/sys/kern/subr_lockdebug.c	Tue Mar  2 01:20:35 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_lockdebug.c,v 1.79 2021/01/01 14:08:33 riastradh Exp $	*/
+/*	$NetBSD: subr_lockdebug.c,v 1.80 2021/03/02 01:20:35 rin 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.79 2021/01/01 14:08:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.80 2021/03/02 01:20:35 rin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -851,7 +851,8 @@ static void
 lockdebug_show_trace(const void *ptr,
 void (*pr)(const char *, ...) __printflike(1, 2))
 {
-db_stack_trace_print((db_expr_t)(intptr_t)ptr, true, 32, "a", pr);
+
+	db_stack_trace_print((db_expr_t)(intptr_t)ptr, true, 32, "a", pr);
 }
 
 static void



CVS commit: src/sys/kern

2021-03-01 Thread Rin Okuyama
Module Name:src
Committed By:   rin
Date:   Tue Mar  2 01:15:15 UTC 2021

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

Log Message:
Consistently right-justify backslash in macro definition.
No binary changes.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/kern/kern_mutex.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_mutex.c
diff -u src/sys/kern/kern_mutex.c:1.95 src/sys/kern/kern_mutex.c:1.96
--- src/sys/kern/kern_mutex.c:1.95	Tue Dec 15 08:35:52 2020
+++ src/sys/kern/kern_mutex.c	Tue Mar  2 01:15:15 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_mutex.c,v 1.95 2020/12/15 08:35:52 skrll Exp $	*/
+/*	$NetBSD: kern_mutex.c,v 1.96 2021/03/02 01:15:15 rin Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 #define	__MUTEX_PRIVATE
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.95 2020/12/15 08:35:52 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.96 2021/03/02 01:15:15 rin Exp $");
 
 #include 
 #include 
@@ -160,7 +160,7 @@ do {	\
 	struct cpu_info *x__ci = curcpu();\
 	int s = x__ci->ci_mtx_oldspl;	\
 	__insn_barrier();		\
-	if (++(x__ci->ci_mtx_count) == 0)			\
+	if (++(x__ci->ci_mtx_count) == 0)\
 		splx(s);		\
 } while (/* CONSTCOND */ 0)
 



CVS commit: src/sys/kern

2021-02-27 Thread Simon Burge
Module Name:src
Committed By:   simonb
Date:   Sat Feb 27 13:02:42 UTC 2021

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

Log Message:
Use "static" in the function intro if the function is static.


To generate a diff of this commit:
cvs rdiff -u -r1.177 -r1.178 src/sys/kern/kern_ktrace.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_ktrace.c
diff -u src/sys/kern/kern_ktrace.c:1.177 src/sys/kern/kern_ktrace.c:1.178
--- src/sys/kern/kern_ktrace.c:1.177	Sat May 23 23:42:43 2020
+++ src/sys/kern/kern_ktrace.c	Sat Feb 27 13:02:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ktrace.c,v 1.177 2020/05/23 23:42:43 ad Exp $	*/
+/*	$NetBSD: kern_ktrace.c,v 1.178 2021/02/27 13:02:42 simonb Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.177 2020/05/23 23:42:43 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.178 2021/02/27 13:02:42 simonb Exp $");
 
 #include 
 #include 
@@ -262,7 +262,7 @@ ktrinit(void)
 /*
  * Release a reference.  Called with ktrace_lock held.
  */
-void
+static void
 ktdrel(struct ktr_desc *ktd)
 {
 
@@ -278,7 +278,7 @@ ktdrel(struct ktr_desc *ktd)
 	}
 }
 
-void
+static void
 ktdref(struct ktr_desc *ktd)
 {
 
@@ -288,7 +288,7 @@ ktdref(struct ktr_desc *ktd)
 	ktrace_on++;
 }
 
-struct ktr_desc *
+static struct ktr_desc *
 ktd_lookup(file_t *fp)
 {
 	struct ktr_desc *ktd;
@@ -411,7 +411,7 @@ freekte:
 	ktrexit(l);
 }
 
-void
+static void
 ktefree(struct ktrace_entry *kte)
 {
 
@@ -426,7 +426,7 @@ ktefree(struct ktrace_entry *kte)
  * same underlying vnode/socket.
  */
 
-int
+static int
 ktrsamefile(file_t *f1, file_t *f2)
 {
 
@@ -462,7 +462,7 @@ ktradref(struct proc *p)
 	ktdref(ktd);
 }
 
-int
+static int
 ktrderefall(struct ktr_desc *ktd, int auth)
 {
 	lwp_t *curl = curlwp;
@@ -1165,7 +1165,7 @@ sys_fktrace(struct lwp *l, const struct 
 	return error;
 }
 
-int
+static int
 ktrops(lwp_t *curl, struct proc *p, int ops, int facs,
 struct ktr_desc *ktd)
 {
@@ -1231,7 +1231,7 @@ ktrops(lwp_t *curl, struct proc *p, int 
 	return error ? 0 : 1;
 }
 
-int
+static int
 ktrsetchildren(lwp_t *curl, struct proc *top, int ops, int facs,
 struct ktr_desc *ktd)
 {
@@ -1265,7 +1265,7 @@ ktrsetchildren(lwp_t *curl, struct proc 
 	/*NOTREACHED*/
 }
 
-void
+static void
 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte)
 {
 	size_t hlen;
@@ -1359,7 +1359,7 @@ again:
 	}
 }
 
-void
+static void
 ktrace_thread(void *arg)
 {
 	struct ktr_desc *ktd = arg;
@@ -1427,7 +1427,7 @@ ktrace_thread(void *arg)
  *
  * TODO: check groups.  use caller effective gid.
  */
-int
+static int
 ktrcanset(lwp_t *calll, struct proc *targetp)
 {
 	KASSERT(mutex_owned(targetp->p_lock));



CVS commit: src/sys/kern

2021-02-23 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Wed Feb 24 05:36:02 UTC 2021

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

Log Message:
skip redzone on pools with the allocation (including all overhead)
on anything greater than half the pool pagesize.

this stops 4KiB being used per allocation from the kmem-02048 pool,
and 64KiB per allocation from the buf32k pool.

we're still wasting 1/4 of space for overhead on eg, the buf1k or
kmem-01024 pools.  however, including overhead costs, the amount of
useless space (not used by consumer or overhead) reduces from 47%
to 18%, so this is far less bad overall.

there are a couple of ideas on solving this less ugly:

- pool redzones are enabled with DIAGNOSTIC kernels, which is
  defined as being "fast, cheap".  this is not cheap (though it
  is relatively fast if you don't run out of memory) so it does
  not really belong here as is, but DEBUG or a special option
  would work for it.

- if we increase the "pool page" size for these pools, such that
  the overhead over pool page is reduced to 5% or less, we can
  have redzones for more allocations without using more space.

also, see this thread:

https://mail-index.netbsd.org/tech-kern/2021/02/23/msg027130.html


To generate a diff of this commit:
cvs rdiff -u -r1.275 -r1.276 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.275 src/sys/kern/subr_pool.c:1.276
--- src/sys/kern/subr_pool.c:1.275	Sat Dec 19 23:38:21 2020
+++ src/sys/kern/subr_pool.c	Wed Feb 24 05:36:02 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_pool.c,v 1.275 2020/12/19 23:38:21 mrg Exp $	*/
+/*	$NetBSD: subr_pool.c,v 1.276 2021/02/24 05:36:02 mrg 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.275 2020/12/19 23:38:21 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.276 2021/02/24 05:36:02 mrg Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -3096,9 +3096,14 @@ pool_redzone_init(struct pool *pp, size_
 	/*
 	 * No space in the natural padding; check if we can extend a
 	 * bit the size of the pool.
+	 *
+	 * Avoid using redzone for allocations half of a page or larger.
+	 * For pagesize items, we'd waste a whole new page (could be
+	 * unmapped?), and for half pagesize items, approximately half
+	 * the space is lost (eg, 4K pages, you get one 2K allocation.)
 	 */
 	nsz = roundup(pp->pr_size + redzsz, pp->pr_align);
-	if (nsz <= pp->pr_alloc->pa_pagesz) {
+	if (nsz <= (pp->pr_alloc->pa_pagesz / 2)) {
 		/* Ok, we can */
 		pp->pr_size = nsz;
 		pp->pr_reqsize_with_redzone = requested_size + redzsz;



CVS commit: src/sys/kern

2021-02-17 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Wed Feb 17 17:39:08 UTC 2021

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

Log Message:
Don't allow callers of fsync_range() to trigger UB in the kernel.

(also prohibit syncing ranges at start offsets less than zero)


To generate a diff of this commit:
cvs rdiff -u -r1.548 -r1.549 src/sys/kern/vfs_syscalls.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_syscalls.c
diff -u src/sys/kern/vfs_syscalls.c:1.548 src/sys/kern/vfs_syscalls.c:1.549
--- src/sys/kern/vfs_syscalls.c:1.548	Sat May 16 18:31:50 2020
+++ src/sys/kern/vfs_syscalls.c	Wed Feb 17 17:39:08 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_syscalls.c,v 1.548 2020/05/16 18:31:50 christos Exp $	*/
+/*	$NetBSD: vfs_syscalls.c,v 1.549 2021/02/17 17:39:08 dholland Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2019, 2020 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.548 2020/05/16 18:31:50 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.549 2021/02/17 17:39:08 dholland Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_fileassoc.h"
@@ -4198,11 +4198,12 @@ sys_fsync_range(struct lwp *l, const str
 	/* If length == 0, we do the whole file, and s = e = 0 will do that */
 	if (len) {
 		s = SCARG(uap, start);
-		e = s + len;
-		if (e < s) {
+		if (s < 0 || len < 0 || len > OFF_T_MAX - s) {
 			error = EINVAL;
 			goto out;
 		}
+		e = s + len;
+		KASSERT(s <= e);
 	} else {
 		e = 0;
 		s = 0;



CVS commit: src/sys/kern

2021-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Feb 17 06:25:11 UTC 2021

Modified Files:
src/sys/kern: makesyscalls.sh systrace_args.c

Log Message:
makesyscalls.sh: fix typo in comment


To generate a diff of this commit:
cvs rdiff -u -r1.182 -r1.183 src/sys/kern/makesyscalls.sh
cvs rdiff -u -r1.44 -r1.45 src/sys/kern/systrace_args.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/makesyscalls.sh
diff -u src/sys/kern/makesyscalls.sh:1.182 src/sys/kern/makesyscalls.sh:1.183
--- src/sys/kern/makesyscalls.sh:1.182	Tue Sep 29 02:58:53 2020
+++ src/sys/kern/makesyscalls.sh	Wed Feb 17 06:25:10 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: makesyscalls.sh,v 1.182 2020/09/29 02:58:53 msaitoh Exp $
+#	$NetBSD: makesyscalls.sh,v 1.183 2021/02/17 06:25:10 rillig Exp $
 #
 # Copyright (c) 1994, 1996, 2000 Christopher G. Demetriou
 # All rights reserved.
@@ -274,7 +274,7 @@ BEGIN {
 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcallshdr
 
 	printf "/* %s */\n\n", tag > systrace
-	printf "/*\n * System call argument to DTrace register array converstion.\n *\n" > systrace
+	printf "/*\n * System call argument to DTrace register array conversion.\n *\n" > systrace
 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > systrace
 }
 NR == 1 {

Index: src/sys/kern/systrace_args.c
diff -u src/sys/kern/systrace_args.c:1.44 src/sys/kern/systrace_args.c:1.45
--- src/sys/kern/systrace_args.c:1.44	Mon Nov  2 18:56:16 2020
+++ src/sys/kern/systrace_args.c	Wed Feb 17 06:25:10 2021
@@ -1,7 +1,7 @@
-/* $NetBSD: systrace_args.c,v 1.44 2020/11/02 18:56:16 christos Exp $ */
+/* $NetBSD: systrace_args.c,v 1.45 2021/02/17 06:25:10 rillig Exp $ */
 
 /*
- * System call argument to DTrace register array converstion.
+ * System call argument to DTrace register array conversion.
  *
  * DO NOT EDIT-- this file is automatically generated.
  * This file is part of the DTrace syscall provider.



CVS commit: src/sys/kern

2021-02-12 Thread Jared D. McNeill
Module Name:src
Committed By:   jmcneill
Date:   Fri Feb 12 19:48:26 UTC 2021

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

Log Message:
entropy: Only print consolidation warning of AB_DEBUG.

The previous fix for PR kern/55458 changed printf to log(LOG_DEBUG, ...) with
the intent of hiding the message unless 'boot -x'. But this did not actually
suppress the message to console as log(LOG_DEBUG, ...) will print to console
if syslogd is not running yet.

So instead, just check for AB_DEBUG flag directly in boothowto, and only
printf the message if it is set.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 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.29 src/sys/kern/kern_entropy.c:1.30
--- src/sys/kern/kern_entropy.c:1.29	Thu Jan 21 17:33:55 2021
+++ src/sys/kern/kern_entropy.c	Fri Feb 12 19:48:26 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_entropy.c,v 1.29 2021/01/21 17:33:55 riastradh Exp $	*/
+/*	$NetBSD: kern_entropy.c,v 1.30 2021/02/12 19:48:26 jmcneill Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.29 2021/01/21 17:33:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.30 2021/02/12 19:48:26 jmcneill Exp $");
 
 #include 
 #include 
@@ -98,6 +98,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_entropy
 #include 
 #include 
 #include 
+#include 
 #include 		/* legacy kernel API */
 #include 		/* userland ioctl interface */
 #include 	/* kernel rndsource driver API */
@@ -1031,9 +1032,11 @@ entropy_do_consolidate(void)
 	atomic_store_relaxed(>needed, E->needed - diff);
 	E->pending -= diff;
 	if (__predict_false(E->needed > 0)) {
-		if (ratecheck(, ))
-			log(LOG_DEBUG, "entropy: WARNING:"
+		if (ratecheck(, ) &&
+		(boothowto & AB_DEBUG) != 0) {
+			printf("entropy: WARNING:"
 			" consolidating less than full entropy\n");
+		}
 	}
 
 	/* Advance the epoch and notify waiters.  */



CVS commit: src/sys/kern

2021-02-08 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Mon Feb  8 09:31:05 UTC 2021

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

Log Message:
Fix typo in comment.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/kern/subr_pcq.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_pcq.c
diff -u src/sys/kern/subr_pcq.c:1.12 src/sys/kern/subr_pcq.c:1.13
--- src/sys/kern/subr_pcq.c:1.12	Tue Jan 12 19:36:39 2021
+++ src/sys/kern/subr_pcq.c	Mon Feb  8 09:31:05 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_pcq.c,v 1.12 2021/01/12 19:36:39 skrll Exp $	*/
+/*	$NetBSD: subr_pcq.c,v 1.13 2021/02/08 09:31:05 wiz Exp $	*/
 
 /*-
  * Copyright (c) 2009, 2019 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_pcq.c,v 1.12 2021/01/12 19:36:39 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pcq.c,v 1.13 2021/02/08 09:31:05 wiz Exp $");
 
 #include 
 #include 
@@ -124,7 +124,7 @@ pcq_put(pcq_t *pcq, void *item)
 	/*
 	 * Synchronization activity to wake up the consumer will ensure
 	 * that the update to pcq_items[] is visible before the wakeup
-	 * arrives.  So, we do not need an additonal memory barrier here.
+	 * arrives.  So, we do not need an additional memory barrier here.
 	 */
 	return true;
 }



CVS commit: src/sys/kern

2021-02-06 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Sat Feb  6 13:54:48 UTC 2021

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

Log Message:
Do not cast memcpy arguments when the intention is unaligned access.
The standard is pretty explicit that misaligned pointers is UB and LLVM
does exploit the promised alignment on SPARC, resulting in kernel
crashes during early boot.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/kern/subr_kmem.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_kmem.c
diff -u src/sys/kern/subr_kmem.c:1.81 src/sys/kern/subr_kmem.c:1.82
--- src/sys/kern/subr_kmem.c:1.81	Sun Jan 24 17:29:11 2021
+++ src/sys/kern/subr_kmem.c	Sat Feb  6 13:54:48 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_kmem.c,v 1.81 2021/01/24 17:29:11 thorpej Exp $	*/
+/*	$NetBSD: subr_kmem.c,v 1.82 2021/02/06 13:54:48 joerg Exp $	*/
 
 /*
  * Copyright (c) 2009-2020 The NetBSD Foundation, Inc.
@@ -78,7 +78,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.81 2021/01/24 17:29:11 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.82 2021/02/06 13:54:48 joerg Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kmem.h"
@@ -505,7 +505,7 @@ kmem_tmpbuf_free(void *buf, size_t size,
 static void
 kmem_size_set(void *p, size_t sz)
 {
-	memcpy((size_t *)((uintptr_t)p + sz), , sizeof(size_t));
+	memcpy((char *)p + sz, , sizeof(size_t));
 }
 
 static void
@@ -513,13 +513,13 @@ kmem_size_check(void *p, size_t sz)
 {
 	size_t hsz;
 
-	memcpy(, (size_t *)((uintptr_t)p + sz), sizeof(size_t));
+	memcpy(, (char *)p + sz, sizeof(size_t));
 
 	if (hsz != sz) {
 		panic("kmem_free(%p, %zu) != allocated size %zu; overwrote?",
 		p, sz, hsz);
 	}
 
-	memset((size_t *)((uintptr_t)p + sz), 0xff, sizeof(size_t));
+	memset((char *)p + sz, 0xff, sizeof(size_t));
 }
 #endif /* defined(KMEM_SIZE) */



CVS commit: src/sys/kern

2021-02-05 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat Feb  6 05:33:20 UTC 2021

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

Log Message:
Fix a comment.  Thanks rillig@ for pointing it out!


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/kern/subr_device.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_device.c
diff -u src/sys/kern/subr_device.c:1.6 src/sys/kern/subr_device.c:1.7
--- src/sys/kern/subr_device.c:1.6	Fri Feb  5 17:03:35 2021
+++ src/sys/kern/subr_device.c	Sat Feb  6 05:33:20 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_device.c,v 1.6 2021/02/05 17:03:35 thorpej Exp $	*/
+/*	$NetBSD: subr_device.c,v 1.7 2021/02/06 05:33:20 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2006, 2021 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.6 2021/02/05 17:03:35 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.7 2021/02/06 05:33:20 thorpej Exp $");
 
 #include 
 #include 
@@ -37,7 +37,7 @@ __KERNEL_RCSID(0, "$NetBSD: subr_device.
 device_t			root_device;
 
 /*
- * device_handle_t accessors / mutators.
+ * devhandle_t accessors / mutators.
  */
 
 static bool



CVS commit: src/sys/kern

2021-01-28 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Thu Jan 28 15:53:46 UTC 2021

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

Log Message:
In device_is_a(), handle dev or dev->dv_cfdriver being NULL.  This
makes the calling pattern:

device_is_a(device_parent(dev), "whatever")

safe.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/kern/subr_device.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_device.c
diff -u src/sys/kern/subr_device.c:1.3 src/sys/kern/subr_device.c:1.4
--- src/sys/kern/subr_device.c:1.3	Mon Mar  9 15:35:11 2015
+++ src/sys/kern/subr_device.c	Thu Jan 28 15:53:46 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_device.c,v 1.3 2015/03/09 15:35:11 pooka Exp $	*/
+/*	$NetBSD: subr_device.c,v 1.4 2021/01/28 15:53:46 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.3 2015/03/09 15:35:11 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.4 2021/01/28 15:53:46 thorpej Exp $");
 
 #include 
 #include 
@@ -176,6 +176,9 @@ device_properties(device_t dev)
 bool
 device_is_a(device_t dev, const char *dname)
 {
+	if (dev == NULL || dev->dv_cfdriver == NULL) {
+		return false;
+	}
 
 	return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
 }



CVS commit: src/sys/kern

2021-01-26 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Wed Jan 27 06:59:08 UTC 2021

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

Log Message:
Fix non-DIAGNOSTIC build


To generate a diff of this commit:
cvs rdiff -u -r1.116 -r1.117 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.116 src/sys/kern/kern_event.c:1.117
--- src/sys/kern/kern_event.c:1.116	Tue Jan 26 19:09:18 2021
+++ src/sys/kern/kern_event.c	Wed Jan 27 06:59:08 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.116 2021/01/26 19:09:18 jdolecek Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.117 2021/01/27 06:59:08 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.116 2021/01/26 19:09:18 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.117 2021/01/27 06:59:08 skrll Exp $");
 
 #include 
 #include 
@@ -835,10 +835,9 @@ filt_user(struct knote *kn, long hint)
 static void
 filt_usertouch(struct knote *kn, struct kevent *kev, long type)
 {
-	struct kqueue *kq = kn->kn_kq;
 	int ffctrl;
 
-	KASSERT(mutex_owned(>kq_lock));
+	KASSERT(mutex_owned(>kn_kq->kq_lock));
 
 	switch (type) {
 	case EVENT_REGISTER:



CVS commit: src/sys/kern

2021-01-26 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Tue Jan 26 19:09:18 UTC 2021

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

Log Message:
call f_touch with kq_lock held, and without KERNEL_LOCK() - for this
adjust EVFILT_USER, which is the only filter actually using that hook

kqueue_scan() now doesn't need to exit/enter the kq_lock when calling
f_touch, which removes another possible race

part of PR kern/50094


To generate a diff of this commit:
cvs rdiff -u -r1.115 -r1.116 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.115 src/sys/kern/kern_event.c:1.116
--- src/sys/kern/kern_event.c:1.115	Mon Jan 25 19:57:05 2021
+++ src/sys/kern/kern_event.c	Tue Jan 26 19:09:18 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.115 2021/01/25 19:57:05 jdolecek Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.116 2021/01/26 19:09:18 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.115 2021/01/25 19:57:05 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.116 2021/01/26 19:09:18 jdolecek Exp $");
 
 #include 
 #include 
@@ -838,7 +838,8 @@ filt_usertouch(struct knote *kn, struct 
 	struct kqueue *kq = kn->kn_kq;
 	int ffctrl;
 
-	mutex_spin_enter(>kq_lock);
+	KASSERT(mutex_owned(>kq_lock));
+
 	switch (type) {
 	case EVENT_REGISTER:
 		if (kev->fflags & NOTE_TRIGGER)
@@ -889,7 +890,6 @@ filt_usertouch(struct knote *kn, struct 
 		panic("filt_usertouch() - invalid type (%ld)", type);
 		break;
 	}
-	mutex_spin_exit(>kq_lock);
 }
 
 /*
@@ -925,7 +925,6 @@ const struct filterops seltrue_filtops =
 	.f_attach = NULL,
 	.f_detach = filt_seltruedetach,
 	.f_event = filt_seltrue,
-	.f_touch = NULL,
 };
 
 int
@@ -1276,9 +1275,9 @@ kqueue_register(struct kqueue *kq, struc
 	kn->kn_kevent.udata = kev->udata;
 	KASSERT(kn->kn_fop != NULL);
 	if (!kn->kn_fop->f_isfd && kn->kn_fop->f_touch != NULL) {
-		KERNEL_LOCK(1, NULL);			/* XXXSMP */
+		mutex_spin_enter(>kq_lock);
 		(*kn->kn_fop->f_touch)(kn, kev, EVENT_REGISTER);
-		KERNEL_UNLOCK_ONE(NULL);		/* XXXSMP */
+		mutex_spin_exit(>kq_lock);
 	} else {
 		kn->kn_sfflags = kev->fflags;
 		kn->kn_sdata = kev->data;
@@ -1529,11 +1528,7 @@ relock:
 kn->kn_fop->f_touch != NULL);
 		/* XXXAD should be got from f_event if !oneshot. */
 		if (touch) {
-			mutex_spin_exit(>kq_lock);
-			KERNEL_LOCK(1, NULL);		/* XXXSMP */
 			(*kn->kn_fop->f_touch)(kn, kevp, EVENT_PROCESS);
-			KERNEL_UNLOCK_ONE(NULL);	/* XXXSMP */
-			mutex_spin_enter(>kq_lock);
 		} else {
 			*kevp = kn->kn_kevent;
 		}



CVS commit: src/sys/kern

2021-01-25 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Mon Jan 25 19:57:05 UTC 2021

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

Log Message:
put back clearing of KN_QUEUED and check for re-queue - as rev. 1.53 notes,
it's necessary for correct function

fixes PR kern/55946, thanks to Paul Goyette for testing

part of PR kern/50094 fix


To generate a diff of this commit:
cvs rdiff -u -r1.114 -r1.115 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.114 src/sys/kern/kern_event.c:1.115
--- src/sys/kern/kern_event.c:1.114	Sun Jan 24 11:31:47 2021
+++ src/sys/kern/kern_event.c	Mon Jan 25 19:57:05 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.114 2021/01/24 11:31:47 jdolecek Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.115 2021/01/25 19:57:05 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.114 2021/01/24 11:31:47 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.115 2021/01/25 19:57:05 jdolecek Exp $");
 
 #include 
 #include 
@@ -1487,11 +1487,12 @@ relock:
 		KASSERT((kn->kn_status & KN_BUSY) == 0);
 
 		kq_check(kq);
+		kn->kn_status &= ~KN_QUEUED;
 		kn->kn_status |= KN_BUSY;
 		kq_check(kq);
 		if (kn->kn_status & KN_DISABLED) {
+			kn->kn_status &= ~KN_BUSY;
 			kq->kq_count--;
-			kn->kn_status &= ~(KN_QUEUED|KN_BUSY);
 			/* don't want disabled events */
 			continue;
 		}
@@ -1504,12 +1505,20 @@ relock:
 			rv = (*kn->kn_fop->f_event)(kn, 0);
 			KERNEL_UNLOCK_ONE(NULL);	/* XXXSMP */
 			mutex_spin_enter(>kq_lock);
+			/* Re-poll if note was re-enqueued. */
+			if ((kn->kn_status & KN_QUEUED) != 0) {
+kn->kn_status &= ~KN_BUSY;
+/* Re-enqueue raised kq_count, lower it again */
+kq->kq_count--;
+influx = 1;
+continue;
+			}
 			if (rv == 0) {
 /*
  * non-ONESHOT event that hasn't
  * triggered again, so de-queue.
  */
-kn->kn_status &= ~(KN_QUEUED|KN_ACTIVE|KN_BUSY);
+kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
 kq->kq_count--;
 influx = 1;
 continue;
@@ -1533,7 +1542,7 @@ relock:
 		influx = 1;
 		if (kn->kn_flags & EV_ONESHOT) {
 			/* delete ONESHOT events after retrieval */
-			kn->kn_status &= ~(KN_QUEUED|KN_BUSY);
+			kn->kn_status &= ~KN_BUSY;
 			kq->kq_count--;
 			mutex_spin_exit(>kq_lock);
 			knote_detach(kn, fdp, true);
@@ -1551,15 +1560,16 @@ relock:
 kn->kn_data = 0;
 kn->kn_fflags = 0;
 			}
-			kn->kn_status &= ~(KN_QUEUED|KN_ACTIVE|KN_BUSY);
+			kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
 			kq->kq_count--;
 		} else if (kn->kn_flags & EV_DISPATCH) {
 			kn->kn_status |= KN_DISABLED;
-			kn->kn_status &= ~(KN_QUEUED|KN_ACTIVE|KN_BUSY);
+			kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
 			kq->kq_count--;
 		} else {
 			/* add event back on list */
 			kq_check(kq);
+			kn->kn_status |= KN_QUEUED;
 			kn->kn_status &= ~KN_BUSY;
 			TAILQ_INSERT_TAIL(>kq_head, kn, kn_tqe);
 			kq_check(kq);



CVS commit: src/sys/kern

2021-01-24 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Sun Jan 24 11:31:47 UTC 2021

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

Log Message:
don't check signals while waiting for other kqueue scans to finish

reportedly somewhat improves behaviour for PR kern/55946

part of PR kern/50094 fix


To generate a diff of this commit:
cvs rdiff -u -r1.113 -r1.114 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.113 src/sys/kern/kern_event.c:1.114
--- src/sys/kern/kern_event.c:1.113	Thu Jan 21 19:37:23 2021
+++ src/sys/kern/kern_event.c	Sun Jan 24 11:31:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.113 2021/01/21 19:37:23 jdolecek Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.114 2021/01/24 11:31:47 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.113 2021/01/21 19:37:23 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.114 2021/01/24 11:31:47 jdolecek Exp $");
 
 #include 
 #include 
@@ -1470,7 +1470,7 @@ relock:
 KQ_FLUX_WAKEUP(kq);
 			}
 			mutex_exit(>fd_lock);
-			(void)cv_wait_sig(>kq_cv, >kq_lock);
+			(void)cv_wait(>kq_cv, >kq_lock);
 			goto relock;
 		}
 



CVS commit: src/sys/kern

2021-01-23 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jan 23 16:33:49 UTC 2021

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

Log Message:
threadpool(9): Fix synchronization between cancel and dispatch.

- threadpool_cancel_job_async tried to prevent
  threadpool_dispatcher_thread from taking the job by setting
  job->job_thread = NULL and then removing the job from the queue.

- But threadpool_cancel_job_async didn't notice job->job_thread is
  null until after it also removes the job from the queue =>
  double-remove, *boom*.

The solution is to teach threadpool_dispatcher_thread to wait until
it has acquired the job lock to test whether job->job_thread is still
valid before it decides to remove the job from the queue.

Fixes PR kern/55948.

XXX pullup-9


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/sys/kern/kern_threadpool.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_threadpool.c
diff -u src/sys/kern/kern_threadpool.c:1.22 src/sys/kern/kern_threadpool.c:1.23
--- src/sys/kern/kern_threadpool.c:1.22	Wed Jan 13 07:34:37 2021
+++ src/sys/kern/kern_threadpool.c	Sat Jan 23 16:33:49 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_threadpool.c,v 1.22 2021/01/13 07:34:37 skrll Exp $	*/
+/*	$NetBSD: kern_threadpool.c,v 1.23 2021/01/23 16:33:49 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2014, 2018 The NetBSD Foundation, Inc.
@@ -81,7 +81,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.22 2021/01/13 07:34:37 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.23 2021/01/23 16:33:49 riastradh Exp $");
 
 #include 
 #include 
@@ -1041,7 +1041,7 @@ threadpool_dispatcher_thread(void *arg)
 
 		/* There are idle threads, so try giving one a job.  */
 		struct threadpool_job *const job = TAILQ_FIRST(>tp_jobs);
-		TAILQ_REMOVE(>tp_jobs, job, job_entry);
+
 		/*
 		 * Take an extra reference on the job temporarily so that
 		 * it won't disappear on us while we have both locks dropped.
@@ -1053,6 +1053,7 @@ threadpool_dispatcher_thread(void *arg)
 		/* If the job was cancelled, we'll no longer be its thread.  */
 		if (__predict_true(job->job_thread == dispatcher)) {
 			mutex_spin_enter(>tp_lock);
+			TAILQ_REMOVE(>tp_jobs, job, job_entry);
 			if (__predict_false(
 TAILQ_EMPTY(>tp_idle_threads))) {
 /*



CVS commit: src/sys/kern

2021-01-21 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Thu Jan 21 19:37:23 UTC 2021

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

Log Message:
remove stray debug #define DEBUG


To generate a diff of this commit:
cvs rdiff -u -r1.112 -r1.113 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.112 src/sys/kern/kern_event.c:1.113
--- src/sys/kern/kern_event.c:1.112	Thu Jan 21 18:09:23 2021
+++ src/sys/kern/kern_event.c	Thu Jan 21 19:37:23 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.112 2021/01/21 18:09:23 jdolecek Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.113 2021/01/21 19:37:23 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.112 2021/01/21 18:09:23 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.113 2021/01/21 19:37:23 jdolecek Exp $");
 
 #include 
 #include 
@@ -1322,7 +1322,6 @@ doneunlock:
 	return (error);
 }
 
-#define DEBUG
 #if defined(DEBUG)
 #define KN_FMT(buf, kn) \
 (snprintb((buf), sizeof(buf), __KN_FLAG_BITS, (kn)->kn_status), buf)



CVS commit: src/sys/kern

2021-01-21 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Thu Jan 21 18:09:23 UTC 2021

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

Log Message:
adjust kq_check() (enabled with DEBUG) to new reality - it's now perfectly
normal to have kq_count bigger than number of the linked entries
on the kqueue

PR kern/50094, problem pointed out by Chuck Silvers


To generate a diff of this commit:
cvs rdiff -u -r1.111 -r1.112 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.111 src/sys/kern/kern_event.c:1.112
--- src/sys/kern/kern_event.c:1.111	Wed Jan 20 21:39:09 2021
+++ src/sys/kern/kern_event.c	Thu Jan 21 18:09:23 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.111 2021/01/20 21:39:09 jdolecek Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.112 2021/01/21 18:09:23 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.111 2021/01/20 21:39:09 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.112 2021/01/21 18:09:23 jdolecek Exp $");
 
 #include 
 #include 
@@ -1322,6 +1322,7 @@ doneunlock:
 	return (error);
 }
 
+#define DEBUG
 #if defined(DEBUG)
 #define KN_FMT(buf, kn) \
 (snprintb((buf), sizeof(buf), __KN_FLAG_BITS, (kn)->kn_status), buf)
@@ -1356,7 +1357,10 @@ kqueue_check(const char *func, size_t li
 			}
 			count++;
 			if (count > kq->kq_count) {
-goto bad;
+panic("%s,%zu: kq=%p kq->kq_count(%d) != "
+"count(%d), nmarker=%d",
+				func, line, kq, kq->kq_count, count,
+nmarker);
 			}
 		} else {
 			nmarker++;
@@ -1370,11 +1374,6 @@ kqueue_check(const char *func, size_t li
 #endif
 		}
 	}
-	if (kq->kq_count != count) {
-bad:
-		panic("%s,%zu: kq=%p kq->kq_count(%d) != count(%d), nmarker=%d",
-		func, line, kq, kq->kq_count, count, nmarker);
-	}
 }
 #define kq_check(a) kqueue_check(__func__, __LINE__, (a))
 #else /* defined(DEBUG) */



CVS commit: src/sys/kern

2021-01-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Jan 21 17:33:55 UTC 2021

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

Log Message:
entropy: Reduce `no seed from bootloader' message to debug level.

This does not necessarily indicate a problem -- only x86 and arm pass
a seed from the bootloader anyway -- so it makes for an always-on
warning on some platforms, including all rump kernels, which is not
helpful.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 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.28 src/sys/kern/kern_entropy.c:1.29
--- src/sys/kern/kern_entropy.c:1.28	Sat Jan 16 02:21:26 2021
+++ src/sys/kern/kern_entropy.c	Thu Jan 21 17:33:55 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_entropy.c,v 1.28 2021/01/16 02:21:26 riastradh Exp $	*/
+/*	$NetBSD: kern_entropy.c,v 1.29 2021/01/21 17:33:55 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.28 2021/01/16 02:21:26 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.29 2021/01/21 17:33:55 riastradh Exp $");
 
 #include 
 #include 
@@ -378,7 +378,7 @@ entropy_init(void)
 
 	/* Note if the bootloader didn't provide a seed.  */
 	if (!E->seeded)
-		printf("entropy: no seed from bootloader\n");
+		aprint_debug("entropy: no seed from bootloader\n");
 
 	/* Allocate the per-CPU records for all early entropy sources.  */
 	LIST_FOREACH(rs, >sources, list)



CVS commit: src/sys/kern

2021-01-20 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Wed Jan 20 21:39:09 UTC 2021

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

Log Message:
fix a race in kqueue_scan() - when multiple threads check the same
kqueue, it could happen other thread seen empty kqueue while kevent
was being checked for re-firing and re-queued

make sure to keep retrying if there are outstanding kevents even
if no kevent is found on first pass through the queue, and only
drop the KN_QUEUED flag and kq_count when actually completely done
with the kevent

change is inspired by the FreeBSD in-flux handling, but without
introducing the reference counting

PR kern/50094 by Christof Meerwald


To generate a diff of this commit:
cvs rdiff -u -r1.110 -r1.111 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.110 src/sys/kern/kern_event.c:1.111
--- src/sys/kern/kern_event.c:1.110	Sun Dec 27 12:45:33 2020
+++ src/sys/kern/kern_event.c	Wed Jan 20 21:39:09 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.110 2020/12/27 12:45:33 jdolecek Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.111 2021/01/20 21:39:09 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.110 2020/12/27 12:45:33 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.111 2021/01/20 21:39:09 jdolecek Exp $");
 
 #include 
 #include 
@@ -179,6 +179,8 @@ static int	kq_calloutmax = (4 * 1024);
 
 extern const struct filterops sig_filtops;
 
+#define KQ_FLUX_WAKEUP(kq)	cv_broadcast(>kq_cv)
+
 /*
  * Table for for all system-defined filters.
  * These should be listed in the numeric order of the EVFILT_* defines.
@@ -1396,7 +1398,7 @@ kqueue_scan(file_t *fp, size_t maxevents
 	struct timespec	ats, sleepts;
 	struct knote	*kn, *marker, morker;
 	size_t		count, nkev, nevents;
-	int		timeout, error, touch, rv;
+	int		timeout, error, touch, rv, influx;
 	filedesc_t	*fdp;
 
 	fdp = curlwp->l_fd;
@@ -1450,39 +1452,48 @@ kqueue_scan(file_t *fp, size_t maxevents
 
 	/* mark end of knote list */
 	TAILQ_INSERT_TAIL(>kq_head, marker, kn_tqe);
+	influx = 0;
 
 	/*
 	 * Acquire the fdp->fd_lock interlock to avoid races with
 	 * file creation/destruction from other threads.
 	 */
+relock:
 	mutex_spin_exit(>kq_lock);
 	mutex_enter(>fd_lock);
 	mutex_spin_enter(>kq_lock);
 
 	while (count != 0) {
 		kn = TAILQ_FIRST(>kq_head);	/* get next knote */
-		while ((kn->kn_status & KN_MARKER) != 0) {
-			if (kn == marker) {
-/* it's our marker, stop */
-TAILQ_REMOVE(>kq_head, kn, kn_tqe);
-if (count < maxevents || (tsp != NULL &&
-(timeout = gettimeleft(,
-)) <= 0))
-	goto queue_processed;
+
+		if ((kn->kn_status & KN_MARKER) != 0 && kn != marker) {
+			if (influx) {
+influx = 0;
+KQ_FLUX_WAKEUP(kq);
+			}
+			mutex_exit(>fd_lock);
+			(void)cv_wait_sig(>kq_cv, >kq_lock);
+			goto relock;
+		}
+
+		TAILQ_REMOVE(>kq_head, kn, kn_tqe);
+		if (kn == marker) {
+			/* it's our marker, stop */
+			KQ_FLUX_WAKEUP(kq);
+			if (count == maxevents) {
 mutex_exit(>fd_lock);
 goto retry;
 			}
-			/* someone else's marker. */
-			kn = TAILQ_NEXT(kn, kn_tqe);
+			break;
 		}
+		KASSERT((kn->kn_status & KN_BUSY) == 0);
+
 		kq_check(kq);
-		kq->kq_count--;
-		TAILQ_REMOVE(>kq_head, kn, kn_tqe);
-		kn->kn_status &= ~KN_QUEUED;
 		kn->kn_status |= KN_BUSY;
 		kq_check(kq);
 		if (kn->kn_status & KN_DISABLED) {
-			kn->kn_status &= ~KN_BUSY;
+			kq->kq_count--;
+			kn->kn_status &= ~(KN_QUEUED|KN_BUSY);
 			/* don't want disabled events */
 			continue;
 		}
@@ -1495,17 +1506,14 @@ kqueue_scan(file_t *fp, size_t maxevents
 			rv = (*kn->kn_fop->f_event)(kn, 0);
 			KERNEL_UNLOCK_ONE(NULL);	/* XXXSMP */
 			mutex_spin_enter(>kq_lock);
-			/* Re-poll if note was re-enqueued. */
-			if ((kn->kn_status & KN_QUEUED) != 0) {
-kn->kn_status &= ~KN_BUSY;
-continue;
-			}
 			if (rv == 0) {
 /*
  * non-ONESHOT event that hasn't
  * triggered again, so de-queue.
  */
-kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
+kn->kn_status &= ~(KN_QUEUED|KN_ACTIVE|KN_BUSY);
+kq->kq_count--;
+influx = 1;
 continue;
 			}
 		}
@@ -1524,9 +1532,11 @@ kqueue_scan(file_t *fp, size_t maxevents
 		}
 		kevp++;
 		nkev++;
+		influx = 1;
 		if (kn->kn_flags & EV_ONESHOT) {
 			/* delete ONESHOT events after retrieval */
-			kn->kn_status &= ~KN_BUSY;
+			kn->kn_status &= ~(KN_QUEUED|KN_BUSY);
+			kq->kq_count--;
 			mutex_spin_exit(>kq_lock);
 			knote_detach(kn, fdp, true);
 			mutex_enter(>fd_lock);
@@ -1544,20 +1554,23 @@ kqueue_scan(file_t *fp, size_t maxevents
 kn->kn_fflags = 0;
 			}
 			kn->kn_status &= ~(KN_QUEUED|KN_ACTIVE|KN_BUSY);
+			kq->kq_count--;
 		} else if (kn->kn_flags & EV_DISPATCH) {
 			kn->kn_status |= KN_DISABLED;
 			kn->kn_status &= 

CVS commit: src/sys/kern

2021-01-15 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jan 16 02:21:26 UTC 2021

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

Log Message:
entropy: Record number of time and data samples for userland.

This more or less follows the semantics of the RNDGETESTNUM and
RNDGETESTNAME ioctls to restore useful `rndctl -lv' output.

Specifically: We count the number of time or data samples entered
with rnd_add_*.  Previously it would count the total number of 32-bit
words in the data, rather than the number of rnd_add_* calls that
enter data, but I think the number of calls makes more sense here.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 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.27 src/sys/kern/kern_entropy.c:1.28
--- src/sys/kern/kern_entropy.c:1.27	Wed Jan 13 23:53:23 2021
+++ src/sys/kern/kern_entropy.c	Sat Jan 16 02:21:26 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_entropy.c,v 1.27 2021/01/13 23:53:23 riastradh Exp $	*/
+/*	$NetBSD: kern_entropy.c,v 1.28 2021/01/16 02:21:26 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.27 2021/01/13 23:53:23 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.28 2021/01/16 02:21:26 riastradh Exp $");
 
 #include 
 #include 
@@ -141,7 +141,9 @@ struct entropy_cpu {
  *	Per-CPU rndsource state.
  */
 struct rndsource_cpu {
-	unsigned		rc_nbits; /* bits of entropy added */
+	unsigned		rc_entropybits;
+	unsigned		rc_timesamples;
+	unsigned		rc_datasamples;
 };
 
 /*
@@ -250,11 +252,12 @@ static void	filt_entropy_read_detach(str
 static int	filt_entropy_read_event(struct knote *, long);
 static void	entropy_request(size_t);
 static void	rnd_add_data_1(struct krndsource *, const void *, uint32_t,
-		uint32_t);
+		uint32_t, uint32_t);
 static unsigned	rndsource_entropybits(struct krndsource *);
 static void	rndsource_entropybits_cpu(void *, void *, struct cpu_info *);
 static void	rndsource_to_user(struct krndsource *, rndsource_t *);
 static void	rndsource_to_user_est(struct krndsource *, rndsource_est_t *);
+static void	rndsource_to_user_est_cpu(void *, void *, struct cpu_info *);
 
 /*
  * entropy_timer()
@@ -1545,6 +1548,8 @@ rnd_attach_source(struct krndsource *rs,
 	/* Initialize the random source.  */
 	memset(rs->name, 0, sizeof(rs->name)); /* paranoia */
 	strlcpy(rs->name, name, sizeof(rs->name));
+	memset(>time_delta, 0, sizeof(rs->time_delta));
+	memset(>value_delta, 0, sizeof(rs->value_delta));
 	rs->total = 0;
 	rs->type = type;
 	rs->flags = flags;
@@ -1817,17 +1822,27 @@ rnd_add_data(struct krndsource *rs, cons
 
 	/* If we are collecting data, enter them.  */
 	if (ISSET(flags, RND_FLAG_COLLECT_VALUE))
-		rnd_add_data_1(rs, buf, len, entropybits);
+		rnd_add_data_1(rs, buf, len, entropybits,
+		RND_FLAG_COLLECT_VALUE);
 
 	/* If we are collecting timings, enter one.  */
 	if (ISSET(flags, RND_FLAG_COLLECT_TIME)) {
 		extra = entropy_timer();
-		rnd_add_data_1(rs, , sizeof extra, 0);
+		rnd_add_data_1(rs, , sizeof extra, 0,
+		RND_FLAG_COLLECT_TIME);
 	}
 }
 
+static unsigned
+add_sat(unsigned a, unsigned b)
+{
+	unsigned c = a + b;
+
+	return (c < a ? UINT_MAX : c);
+}
+
 /*
- * rnd_add_data_1(rs, buf, len, entropybits)
+ * rnd_add_data_1(rs, buf, len, entropybits, flag)
  *
  *	Internal subroutine to call either entropy_enter_intr, if we're
  *	in interrupt context, or entropy_enter if not, and to count the
@@ -1835,7 +1850,7 @@ rnd_add_data(struct krndsource *rs, cons
  */
 static void
 rnd_add_data_1(struct krndsource *rs, const void *buf, uint32_t len,
-uint32_t entropybits)
+uint32_t entropybits, uint32_t flag)
 {
 	bool fullyused;
 
@@ -1859,15 +1874,34 @@ rnd_add_data_1(struct krndsource *rs, co
 		if (E->stage < ENTROPY_HOT) {
 			if (E->stage >= ENTROPY_WARM)
 mutex_enter(>lock);
-			rs->total += MIN(UINT_MAX - rs->total, entropybits);
+			rs->total = add_sat(rs->total, entropybits);
+			switch (flag) {
+			case RND_FLAG_COLLECT_TIME:
+rs->time_delta.insamples =
+add_sat(rs->time_delta.insamples, 1);
+break;
+			case RND_FLAG_COLLECT_VALUE:
+rs->value_delta.insamples =
+add_sat(rs->value_delta.insamples, 1);
+break;
+			}
 			if (E->stage >= ENTROPY_WARM)
 mutex_exit(>lock);
 		} else {
 			struct rndsource_cpu *rc = percpu_getref(rs->state);
-			unsigned nbits = rc->rc_nbits;
 
-			nbits += MIN(UINT_MAX - nbits, entropybits);
-			atomic_store_relaxed(>rc_nbits, nbits);
+			atomic_store_relaxed(>rc_entropybits,
+			add_sat(rc->rc_entropybits, entropybits));
+			switch (flag) {
+			case RND_FLAG_COLLECT_TIME:
+atomic_store_relaxed(>rc_timesamples,
+add_sat(rc->rc_timesamples, 1));
+break;
+			case RND_FLAG_COLLECT_VALUE:
+

CVS commit: src/sys/kern

2021-01-15 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jan 16 02:20:00 UTC 2021

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

Log Message:
entropy: Sample cycle counter or timecounter in hardclock.

Only do so when we're short on entropy, in order to minimize
performance impact.

The sampling should stay close to the time of the actual hardclock
timer interrupt, so that the oscillator driving it determines when we
sample the cycle counter or timecounter, which we hope is driven by
an independent oscillator.

If we used a callout, there might be many other influences -- such as
spin lock delays possibly synchronized with this core's cycle counter
-- that could get between the timer interrupt and the sample.

In the glorious tickless future, this should instead be wired up to
the timer interrupt handler, however that manifests in the future
tickless API.


To generate a diff of this commit:
cvs rdiff -u -r1.143 -r1.144 src/sys/kern/kern_clock.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_clock.c
diff -u src/sys/kern/kern_clock.c:1.143 src/sys/kern/kern_clock.c:1.144
--- src/sys/kern/kern_clock.c:1.143	Sat Dec  5 18:17:01 2020
+++ src/sys/kern/kern_clock.c	Sat Jan 16 02:20:00 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_clock.c,v 1.143 2020/12/05 18:17:01 thorpej Exp $	*/
+/*	$NetBSD: kern_clock.c,v 1.144 2021/01/16 02:20:00 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.143 2020/12/05 18:17:01 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.144 2021/01/16 02:20:00 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -90,6 +90,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_clock.c
 #include 
 #include 
 #include 
+#include 
 
 #ifdef GPROF
 #include 
@@ -138,6 +139,61 @@ static int hardscheddiv; /* hard => sche
 static int psdiv;			/* prof => stat divider */
 int	psratio;			/* ratio: prof / stat */
 
+struct clockrnd {
+	struct krndsource source;
+	unsigned needed;
+};
+
+static struct clockrnd hardclockrnd __aligned(COHERENCY_UNIT);
+static struct clockrnd statclockrnd __aligned(COHERENCY_UNIT);
+
+static void
+clockrnd_get(size_t needed, void *cookie)
+{
+	struct clockrnd *C = cookie;
+
+	/* Start sampling.  */
+	atomic_store_relaxed(>needed, 2*NBBY*needed);
+}
+
+static void
+clockrnd_sample(struct clockrnd *C)
+{
+	struct cpu_info *ci = curcpu();
+
+	/* If there's nothing needed right now, stop here.  */
+	if (__predict_true(C->needed == 0))
+		return;
+
+	/*
+	 * If we're not the primary core of a package, we're probably
+	 * driven by the same clock as the primary core, so don't
+	 * bother.
+	 */
+	if (ci != ci->ci_package1st)
+		return;
+
+	/* Take a sample and enter it into the pool.  */
+	rnd_add_uint32(>source, 0);
+
+	/*
+	 * On the primary CPU, count down.  Using an atomic decrement
+	 * here isn't really necessary -- on every platform we care
+	 * about, stores to unsigned int are atomic, and the only other
+	 * memory operation that could happen here is for another CPU
+	 * to store a higher value for needed.  But using an atomic
+	 * decrement avoids giving the impression of data races, and is
+	 * unlikely to hurt because only one CPU will ever be writing
+	 * to the location.
+	 */
+	if (CPU_IS_PRIMARY(curcpu())) {
+		unsigned needed __diagused;
+
+		needed = atomic_dec_uint_nv(>needed);
+		KASSERT(needed != UINT_MAX);
+	}
+}
+
 static u_int get_intr_timecount(struct timecounter *);
 
 static struct timecounter intr_timecounter = {
@@ -224,6 +280,16 @@ initclocks(void)
 		   SYSCTL_DESCR("Number of hardclock ticks"),
 		   NULL, 0, _ticks, sizeof(hardclock_ticks),
 		   CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
+
+	rndsource_setcb(, clockrnd_get, );
+	rnd_attach_source(, "hardclock", RND_TYPE_SKEW,
+	RND_FLAG_COLLECT_TIME|RND_FLAG_HASCB);
+	if (stathz) {
+		rndsource_setcb(, clockrnd_get,
+		);
+		rnd_attach_source(, "statclock",
+		RND_TYPE_SKEW, RND_FLAG_COLLECT_TIME|RND_FLAG_HASCB);
+	}
 }
 
 /*
@@ -235,6 +301,8 @@ hardclock(struct clockframe *frame)
 	struct lwp *l;
 	struct cpu_info *ci;
 
+	clockrnd_sample();
+
 	ci = curcpu();
 	l = ci->ci_onproc;
 
@@ -338,6 +406,9 @@ statclock(struct clockframe *frame)
 	struct proc *p;
 	struct lwp *l;
 
+	if (stathz)
+		clockrnd_sample();
+
 	/*
 	 * Notice changes in divisor frequency, and adjust clock
 	 * frequency accordingly.



CVS commit: src/sys/kern

2021-01-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jan 13 23:53:23 UTC 2021

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

Log Message:
entropy: Use a separate condvar for rndsource list lock.

Otherwise, two processes both waiting for entropy will dance around
waking each other up (by releasing the rndsource list lock) and going
back to sleep (waiting for entropy).

Witnessed on the armv7 testbed when /etc/security presumably ran
twice over a >day-long test, until the metaphorical plug got pulled:

net/if_ipsec/t_ipsec_natt (509/888): 2 test cases
ipsecif_natt_transport_null: [ 37123.2631856] entropy: pid 1005 (dd) 
blocking due to lack of entropy
[256.523317s] Failed: atf-check failed; see the output of the test for details
ipsecif_natt_transport_rijndaelcbc: [274.370791s] Failed: atf-check failed; 
see the output of the test for details
[532.486697s]
...
puffs_lstat_symlink: [ 123442.1606517] entropy: pid 9499 (dd) blocking due 
to lack of entropy
[ 123442.1835067] entropy: pid 1005 (dd) blocking due to lack of entropy
[ 123442.1944600] entropy: pid 9499 (dd) blocking due to lack of entropy
[ 123442.1944600] entropy: pid 1005 (dd) blocking due to lack of entropy
...


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 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.26 src/sys/kern/kern_entropy.c:1.27
--- src/sys/kern/kern_entropy.c:1.26	Mon Jan 11 02:18:40 2021
+++ src/sys/kern/kern_entropy.c	Wed Jan 13 23:53:23 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_entropy.c,v 1.26 2021/01/11 02:18:40 riastradh Exp $	*/
+/*	$NetBSD: kern_entropy.c,v 1.27 2021/01/13 23:53:23 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.26 2021/01/11 02:18:40 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.27 2021/01/13 23:53:23 riastradh Exp $");
 
 #include 
 #include 
@@ -161,6 +161,7 @@ struct {
 	kcondvar_t	cv;		/* notifies state changes */
 	struct selinfo	selq;		/* notifies needed -> 0 */
 	struct lwp	*sourcelock;	/* lock on list of sources */
+	kcondvar_t	sourcelock_cv;	/* notifies sourcelock release */
 	LIST_HEAD(,krndsource) sources;	/* list of entropy sources */
 	enum entropy_stage {
 		ENTROPY_COLD = 0, /* single-threaded */
@@ -367,6 +368,7 @@ entropy_init(void)
 	mutex_init(>lock, MUTEX_DEFAULT, IPL_VM);
 	cv_init(>cv, "entropy");
 	selinit(>selq);
+	cv_init(>sourcelock_cv, "entsrclock");
 
 	/* Make sure the seed source is attached.  */
 	attach_seed_rndsource();
@@ -1594,7 +1596,7 @@ rnd_detach_source(struct krndsource *rs)
 	/* Wait until the source list is not in use, and remove it.  */
 	mutex_enter(>lock);
 	while (E->sourcelock)
-		cv_wait(>cv, >lock);
+		cv_wait(>sourcelock_cv, >lock);
 	LIST_REMOVE(rs, list);
 	mutex_exit(>lock);
 
@@ -1618,7 +1620,7 @@ rnd_lock_sources(void)
 	KASSERT(mutex_owned(>lock));
 
 	while (E->sourcelock) {
-		error = cv_wait_sig(>cv, >lock);
+		error = cv_wait_sig(>sourcelock_cv, >lock);
 		if (error)
 			return error;
 	}
@@ -1663,7 +1665,7 @@ rnd_unlock_sources(void)
 	curlwp, E->sourcelock);
 	E->sourcelock = NULL;
 	if (E->stage >= ENTROPY_WARM)
-		cv_broadcast(>cv);
+		cv_signal(>sourcelock_cv);
 }
 
 /*



CVS commit: src/sys/kern

2021-01-12 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Wed Jan 13 07:36:56 UTC 2021

Modified Files:
src/sys/kern: kern_lwp.c kgdb_stub.c

Log Message:
Improve English in comments


To generate a diff of this commit:
cvs rdiff -u -r1.242 -r1.243 src/sys/kern/kern_lwp.c
cvs rdiff -u -r1.29 -r1.30 src/sys/kern/kgdb_stub.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_lwp.c
diff -u src/sys/kern/kern_lwp.c:1.242 src/sys/kern/kern_lwp.c:1.243
--- src/sys/kern/kern_lwp.c:1.242	Mon Jun 22 16:21:29 2020
+++ src/sys/kern/kern_lwp.c	Wed Jan 13 07:36:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lwp.c,v 1.242 2020/06/22 16:21:29 maxv Exp $	*/
+/*	$NetBSD: kern_lwp.c,v 1.243 2021/01/13 07:36:56 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008, 2009, 2019, 2020
@@ -196,7 +196,7 @@
  *
  *		sleepq -> turnstile -> spc_lwplock -> spc_mutex
  *
- *	Each process has an scheduler state lock (proc::p_lock), and a
+ *	Each process has a scheduler state lock (proc::p_lock), and a
  *	number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
  *	so on.  When an LWP is to be entered into or removed from one of the
  *	following states, p_lock must be held and the process wide counters
@@ -217,7 +217,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.242 2020/06/22 16:21:29 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.243 2021/01/13 07:36:56 skrll Exp $");
 
 #include "opt_ddb.h"
 #include "opt_lockdebug.h"

Index: src/sys/kern/kgdb_stub.c
diff -u src/sys/kern/kgdb_stub.c:1.29 src/sys/kern/kgdb_stub.c:1.30
--- src/sys/kern/kgdb_stub.c:1.29	Fri Jun 26 14:26:38 2015
+++ src/sys/kern/kgdb_stub.c	Wed Jan 13 07:36:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kgdb_stub.c,v 1.29 2015/06/26 14:26:38 christos Exp $	*/
+/*	$NetBSD: kgdb_stub.c,v 1.30 2021/01/13 07:36:56 skrll Exp $	*/
 
 /*
  * Copyright (c) 1990, 1993
@@ -45,7 +45,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kgdb_stub.c,v 1.29 2015/06/26 14:26:38 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kgdb_stub.c,v 1.30 2021/01/13 07:36:56 skrll Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -164,7 +164,7 @@ digit2i(u_char c)
 
 /*
  * Convert the low 4 bits of an integer into
- * an hex digit.
+ * a hex digit.
  */
 static u_char
 i2digit(int n)
@@ -173,7 +173,7 @@ i2digit(int n)
 }
 
 /*
- * Convert a byte array into an hex string.
+ * Convert a byte array into a hex string.
  */
 static void
 mem2hex(void *vdst, void *vsrc, int len)
@@ -189,7 +189,7 @@ mem2hex(void *vdst, void *vsrc, int len)
 }
 
 /*
- * Convert an hex string into a byte array.
+ * Convert a hex string into a byte array.
  * This returns a pointer to the character following
  * the last valid hex digit. If the string ends in
  * the middle of a byte, NULL is returned.
@@ -213,7 +213,7 @@ hex2mem(void *vdst, u_char *src, int max
 }
 
 /*
- * Convert an hex string into an integer.
+ * Convert a hex string into an integer.
  * This returns a pointer to the character following
  * the last valid hex digit.
  */



CVS commit: src/sys/kern

2021-01-12 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Wed Jan 13 07:34:37 UTC 2021

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

Log Message:
Improve english in a comment


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/kern/kern_threadpool.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_threadpool.c
diff -u src/sys/kern/kern_threadpool.c:1.21 src/sys/kern/kern_threadpool.c:1.22
--- src/sys/kern/kern_threadpool.c:1.21	Wed Jan 13 02:20:15 2021
+++ src/sys/kern/kern_threadpool.c	Wed Jan 13 07:34:37 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_threadpool.c,v 1.21 2021/01/13 02:20:15 riastradh Exp $	*/
+/*	$NetBSD: kern_threadpool.c,v 1.22 2021/01/13 07:34:37 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2014, 2018 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
  * Thread pools.
  *
  * A thread pool is a collection of worker threads idle or running
- * jobs, together with an dispatcher thread that does not run jobs but
+ * jobs, together with a dispatcher thread that does not run jobs but
  * can be given jobs to assign to a worker thread.  Scheduling a job in
  * a thread pool does not allocate or even sleep at all, except perhaps
  * on an adaptive lock, unlike kthread_create.  Jobs reuse threads, so
@@ -81,7 +81,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.21 2021/01/13 02:20:15 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.22 2021/01/13 07:34:37 skrll Exp $");
 
 #include 
 #include 



CVS commit: src/sys/kern

2021-01-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jan 13 02:20:15 UTC 2021

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

Log Message:
threadpool(9): Tidy up thread naming.

- `dispatcher', not `overseer' -- much more appropriate metaphor.
- Just omit `/-1' from unbound thread names.
- Just omit `@-1' from dynamic-priority (PRI_NONE) thread names.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/kern/kern_threadpool.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_threadpool.c
diff -u src/sys/kern/kern_threadpool.c:1.20 src/sys/kern/kern_threadpool.c:1.21
--- src/sys/kern/kern_threadpool.c:1.20	Wed Jan 13 02:19:08 2021
+++ src/sys/kern/kern_threadpool.c	Wed Jan 13 02:20:15 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_threadpool.c,v 1.20 2021/01/13 02:19:08 riastradh Exp $	*/
+/*	$NetBSD: kern_threadpool.c,v 1.21 2021/01/13 02:20:15 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2014, 2018 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
  * Thread pools.
  *
  * A thread pool is a collection of worker threads idle or running
- * jobs, together with an overseer thread that does not run jobs but
+ * jobs, together with an dispatcher thread that does not run jobs but
  * can be given jobs to assign to a worker thread.  Scheduling a job in
  * a thread pool does not allocate or even sleep at all, except perhaps
  * on an adaptive lock, unlike kthread_create.  Jobs reuse threads, so
@@ -56,32 +56,32 @@
  * CPU.  When you're done, call threadpool_percpu_put(pool_percpu,
  * pri).
  *
- * +--MACHINE---+
- * | +--CPU 0---+ +--CPU 1---+ +--CPU n---+ |
- * | |  | |  | ... |  | |
- * | | | |  | ... | | |
- * | |  | |  | ... | | |
- * | | .| | .| ... | .| |
- * | | .| | .| ... | .| |
- * | | .| | .| ... | .| |
- * | +--+ +--+ +--+ |
- * |+--unbound-+|
- * ||||
- * || ||
- * ||  ||
- * |+--+|
- * ++
+ * +--MACHINE-+
+ * | +--CPU 0-+ +--CPU 1-+ +--CPU n-+ |
+ * | |  | |  | ... |  | |
+ * | |   | || ... |   | |
+ * | || || ... |   | |
+ * | | .  | | .  | ... | .  | |
+ * | | .  | | .  | ... | .  | |
+ * | | .  | | .  | ... | .  | |
+ * | ++ ++ ++ |
+ * |+--unbound---+|
+ * ||||
+ * ||   ||
+ * ||||
+ * |++|
+ * +--+
  *
- * XXX Why one overseer per CPU?  I did that originally to avoid
+ * XXX Why one dispatcher per CPU?  I did that originally to avoid
  * touching remote CPUs' memory when scheduling a job, but that still
  * requires interprocessor synchronization.  Perhaps we could get by
- * with a single overseer thread, at the expense of another pointer in
- * struct threadpool_job to identify the CPU on which it must run
- * in order for the overseer to schedule it correctly.
+ * with a single dispatcher thread, at the expense of another pointer
+ * in struct threadpool_job to identify the CPU on which it must run in
+ * order for the dispatcher to schedule it correctly.
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.20 2021/01/13 02:19:08 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.21 2021/01/13 02:20:15 riastradh Exp $");
 
 #include 
 #include 
@@ -141,27 +141,27 @@ SDT_PROBE_DEFINE2(sdt, kernel, threadpoo
 "struct threadpool *"/*pool*/, "struct threadpool_job *"/*job*/);
 SDT_PROBE_DEFINE2(sdt, kernel, threadpool, schedule__job__running,
 "struct threadpool *"/*pool*/, "struct threadpool_job *"/*job*/);
-SDT_PROBE_DEFINE2(sdt, kernel, threadpool, schedule__job__overseer,
+SDT_PROBE_DEFINE2(sdt, kernel, threadpool, schedule__job__dispatcher,
 "struct threadpool *"/*pool*/, "struct threadpool_job *"/*job*/);
 SDT_PROBE_DEFINE3(sdt, kernel, threadpool, schedule__job__thread,
 "struct threadpool *"/*pool*/,
 "struct threadpool_job *"/*job*/,
 "struct lwp *"/*thread*/);
 
-SDT_PROBE_DEFINE1(sdt, kernel, threadpool, overseer__start,
+SDT_PROBE_DEFINE1(sdt, kernel, 

CVS commit: src/sys/kern

2021-01-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jan 13 02:19:09 UTC 2021

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

Log Message:
threadpool(9): Make threadpool_percpu_ref_remote non-sleepable.

Needed for threadpool-based workqueue_enqueue to run in interrupt
context.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/kern/kern_threadpool.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_threadpool.c
diff -u src/sys/kern/kern_threadpool.c:1.19 src/sys/kern/kern_threadpool.c:1.20
--- src/sys/kern/kern_threadpool.c:1.19	Mon Sep  7 01:08:27 2020
+++ src/sys/kern/kern_threadpool.c	Wed Jan 13 02:19:08 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_threadpool.c,v 1.19 2020/09/07 01:08:27 riastradh Exp $	*/
+/*	$NetBSD: kern_threadpool.c,v 1.20 2021/01/13 02:19:08 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2014, 2018 The NetBSD Foundation, Inc.
@@ -81,7 +81,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.19 2020/09/07 01:08:27 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.20 2021/01/13 02:19:08 riastradh Exp $");
 
 #include 
 #include 
@@ -644,10 +644,16 @@ threadpool_percpu_ref_remote(struct thre
 {
 	struct threadpool **poolp, *pool;
 
-	percpu_traverse_enter();
+	/*
+	 * As long as xcalls are blocked -- e.g., by kpreempt_disable
+	 * -- the percpu object will not be swapped and destroyed.  We
+	 * can't write to it, because the data may have already been
+	 * moved to a new buffer, but we can safely read from it.
+	 */
+	kpreempt_disable();
 	poolp = percpu_getptr_remote(pool_percpu->tpp_percpu, ci);
 	pool = *poolp;
-	percpu_traverse_exit();
+	kpreempt_enable();
 
 	return pool;
 }



CVS commit: src/sys/kern

2021-01-12 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Tue Jan 12 19:36:39 UTC 2021

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

Log Message:
Typo in comment


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/kern/subr_pcq.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_pcq.c
diff -u src/sys/kern/subr_pcq.c:1.11 src/sys/kern/subr_pcq.c:1.12
--- src/sys/kern/subr_pcq.c:1.11	Mon Dec 16 18:50:44 2019
+++ src/sys/kern/subr_pcq.c	Tue Jan 12 19:36:39 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_pcq.c,v 1.11 2019/12/16 18:50:44 ad Exp $	*/
+/*	$NetBSD: subr_pcq.c,v 1.12 2021/01/12 19:36:39 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2009, 2019 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_pcq.c,v 1.11 2019/12/16 18:50:44 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pcq.c,v 1.12 2021/01/12 19:36:39 skrll Exp $");
 
 #include 
 #include 
@@ -180,7 +180,7 @@ pcq_get(pcq_t *pcq)
 
 	/*
 	 * Ensure that update to pcq_items[] becomes globally visible
-	 * before the update to pcq_pc.  If it were reodered to occur
+	 * before the update to pcq_pc.  If it were reordered to occur
 	 * after it, we could in theory wipe out a modification made
 	 * to pcq_items[] by pcq_put().
 	 */



CVS commit: src/sys/kern

2021-01-11 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Mon Jan 11 17:18:51 UTC 2021

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

Log Message:
Trailing whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.395 -r1.396 src/sys/kern/kern_sig.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_sig.c
diff -u src/sys/kern/kern_sig.c:1.395 src/sys/kern/kern_sig.c:1.396
--- src/sys/kern/kern_sig.c:1.395	Sun Nov  1 18:51:02 2020
+++ src/sys/kern/kern_sig.c	Mon Jan 11 17:18:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.395 2020/11/01 18:51:02 pgoyette Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.396 2021/01/11 17:18:51 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.395 2020/11/01 18:51:02 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.396 2021/01/11 17:18:51 skrll Exp $");
 
 #include "opt_execfmt.h"
 #include "opt_ptrace.h"
@@ -548,7 +548,7 @@ siggetinfo(sigpend_t *sp, ksiginfo_t *ou
 		if (ksi->ksi_signo != signo)
 			continue;
 		if (count++ > 0) /* Only remove the first, count all of them */
-			continue; 
+			continue;
 		TAILQ_REMOVE(>sp_info, ksi, ksi_list);
 		KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
 		KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
@@ -578,7 +578,7 @@ out:
  *	Fetch the first pending signal from a set.  Optionally, also fetch
  *	or manufacture a ksiginfo element.  Returns the number of the first
  *	pending signal, or zero.
- */ 
+ */
 int
 sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask)
 {
@@ -646,7 +646,7 @@ sigput(sigpend_t *sp, struct proc *p, ks
 			return 0;
 		}
 	}
-	
+
 	if (count >= SIGQUEUE_MAX) {
 #ifdef DIAGNOSTIC
 		printf("%s(%d): Signal queue is full signal=%d\n",
@@ -656,7 +656,7 @@ sigput(sigpend_t *sp, struct proc *p, ks
 	}
 	ksi->ksi_flags |= KSI_QUEUED;
 	TAILQ_INSERT_TAIL(>sp_info, ksi, ksi_list);
-	
+
 	return 0;
 }
 
@@ -715,7 +715,7 @@ sigclearall(struct proc *p, const sigset
  *
  *	This should only ever be called with (l == curlwp), unless the
  *	result does not matter (procfs, sysctl).
- */ 
+ */
 int
 sigispending(struct lwp *l, int signo)
 {
@@ -990,7 +990,7 @@ repeat:
 	const bool ignored = action == SIG_IGN;
 	if (masked || ignored) {
 		mutex_enter(>sa_mutex);
-		sigdelset(mask, signo);	
+		sigdelset(mask, signo);
 		sigdelset(>p_sigctx.ps_sigcatch, signo);
 		sigdelset(>p_sigctx.ps_sigignore, signo);
 		sigdelset(_PS(ps, signo).sa_mask, signo);
@@ -2004,7 +2004,7 @@ issignal(struct lwp *l)
 
 			/*
 			 * If there is a pending stop signal to process with
-			 * default action, stop here, then clear the signal. 
+			 * default action, stop here, then clear the signal.
 			 * However, if process is member of an orphaned
 			 * process group, ignore tty stop signals.
 			 */
@@ -2231,7 +2231,7 @@ killproc(struct proc *p, const char *why
  * if appropriate.  We bypass the normal tests for masked and caught
  * signals, allowing unrecoverable failures to terminate the process without
  * changing signal state.  Mark the accounting record with the signal
- * termination.  If dumping core, save the signal number for the debugger. 
+ * termination.  If dumping core, save the signal number for the debugger.
  * Calls exit and does not return.
  */
 void
@@ -2443,7 +2443,7 @@ proc_stop(struct proc *p, int signo)
  * interruptably into the LSSTOP state.
  *
  * Note that we are not concerned about keeping all LWPs stopped while the
- * process is stopped: stopped LWPs can awaken briefly to handle signals. 
+ * process is stopped: stopped LWPs can awaken briefly to handle signals.
  * What we do need to ensure is that all LWPs in a stopping process have
  * stopped at least once, so that notification can be sent to the parent
  * process.



  1   2   3   4   5   6   7   8   9   10   >