CVS commit: [netbsd-9] src/sys/kern

2024-03-11 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Mar 11 18:01:48 UTC 2024

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1814):

sys/kern/sysv_shm.c: revision 1.142

Avoid overflow when computing kern.ipc.shmmax. Keep shmmax (bytes) and
shmall (pages) values aligned and use arithmetic everywhere instead
of shifts.

Should fix PR 57979


To generate a diff of this commit:
cvs rdiff -u -r1.135.2.4 -r1.135.2.5 src/sys/kern/sysv_shm.c

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



CVS commit: [netbsd-9] src/sys/kern

2024-03-11 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Mar 11 18:01:48 UTC 2024

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1814):

sys/kern/sysv_shm.c: revision 1.142

Avoid overflow when computing kern.ipc.shmmax. Keep shmmax (bytes) and
shmall (pages) values aligned and use arithmetic everywhere instead
of shifts.

Should fix PR 57979


To generate a diff of this commit:
cvs rdiff -u -r1.135.2.4 -r1.135.2.5 src/sys/kern/sysv_shm.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/sysv_shm.c
diff -u src/sys/kern/sysv_shm.c:1.135.2.4 src/sys/kern/sysv_shm.c:1.135.2.5
--- src/sys/kern/sysv_shm.c:1.135.2.4	Thu Oct 10 17:23:45 2019
+++ src/sys/kern/sysv_shm.c	Mon Mar 11 18:01:48 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysv_shm.c,v 1.135.2.4 2019/10/10 17:23:45 martin Exp $	*/
+/*	$NetBSD: sysv_shm.c,v 1.135.2.5 2024/03/11 18:01:48 martin Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135.2.4 2019/10/10 17:23:45 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135.2.5 2024/03/11 18:01:48 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_sysv.h"
@@ -961,10 +961,10 @@ shminit(struct sysctllog **clog)
 	ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)));
 
 	if (shminfo.shmmax == 0)
-		shminfo.shmmax = uimax(physmem / 4, 1024) * PAGE_SIZE;
+		shminfo.shmall = uimax(physmem / 4, 1024);
 	else
-		shminfo.shmmax *= PAGE_SIZE;
-	shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
+		shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
+	shminfo.shmmax = (uint64_t)shminfo.shmall * PAGE_SIZE;
 
 	for (i = 0; i < shminfo.shmmni; i++) {
 		cv_init(_cv[i], "shmwait");
@@ -1087,7 +1087,7 @@ sysctl_ipc_shmmax(SYSCTLFN_ARGS)
 		return EINVAL;
 
 	shminfo.shmmax = round_page(newsize);
-	shminfo.shmall = shminfo.shmmax >> PAGE_SHIFT;
+	shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
 
 	return 0;
 }



CVS commit: [netbsd-9] src/sys/kern

2023-11-29 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Nov 29 12:27:05 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: exec_subr.c

Log Message:
Additionally pull up the following for ticket #1769:

sys/kern/exec_subr.c1.87

Fix build for kernels w/o PAX_MPROTECT.


To generate a diff of this commit:
cvs rdiff -u -r1.82.10.1 -r1.82.10.2 src/sys/kern/exec_subr.c

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

Modified files:

Index: src/sys/kern/exec_subr.c
diff -u src/sys/kern/exec_subr.c:1.82.10.1 src/sys/kern/exec_subr.c:1.82.10.2
--- src/sys/kern/exec_subr.c:1.82.10.1	Tue Nov 28 12:59:21 2023
+++ src/sys/kern/exec_subr.c	Wed Nov 29 12:27:05 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_subr.c,v 1.82.10.1 2023/11/28 12:59:21 martin Exp $	*/
+/*	$NetBSD: exec_subr.c,v 1.82.10.2 2023/11/29 12:27:05 martin Exp $	*/
 
 /*
  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: exec_subr.c,v 1.82.10.1 2023/11/28 12:59:21 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: exec_subr.c,v 1.82.10.2 2023/11/29 12:27:05 martin Exp $");
 
 #include "opt_pax.h"
 
@@ -162,7 +162,7 @@ static int
 vmcmd_get_prot(struct lwp *l, const struct exec_vmcmd *cmd, vm_prot_t *prot,
 vm_prot_t *maxprot)
 {
-	vm_prot_t extraprot = PROT_MPROTECT_EXTRACT(cmd->ev_prot);
+	vm_prot_t extraprot __unused = PROT_MPROTECT_EXTRACT(cmd->ev_prot);
 
 	*prot = cmd->ev_prot & UVM_PROT_ALL;
 	*maxprot = PAX_MPROTECT_MAXPROTECT(l, *prot, extraprot, UVM_PROT_ALL);



CVS commit: [netbsd-9] src/sys/kern

2023-11-29 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Nov 29 12:27:05 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: exec_subr.c

Log Message:
Additionally pull up the following for ticket #1769:

sys/kern/exec_subr.c1.87

Fix build for kernels w/o PAX_MPROTECT.


To generate a diff of this commit:
cvs rdiff -u -r1.82.10.1 -r1.82.10.2 src/sys/kern/exec_subr.c

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



CVS commit: [netbsd-9] src/sys/kern

2023-11-27 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Nov 27 20:01:40 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: uipc_mbuf.c

Log Message:
Pull up following revision(s) (requested by ozaki-r in ticket #1768):
sys/kern/uipc_mbuf.c: revision 1.252

mbuf: avoid assertion failure when splitting mbuf cluster

>From OpenBSD:
commit 7b4d35e0a60ba1dd4daf4b1c2932020a22463a89
Author: bluhm 
Date:   Fri Oct 20 16:25:15 2023 +
Avoid assertion failure when splitting mbuf cluster.
m_split() calls m_align() to initialize the data pointer of newly
allocated mbuf.  If the new mbuf will be converted to a cluster,
this is not necessary.  If additionally the new mbuf is larger than
MLEN, this can lead to a panic.
Only call m_align() when a valid m_data is needed.  This is the
case if we do not refecence the existing cluster, but memcpy() the
data into the new mbuf.
Reported-by: syzbot+0e6817f5877926f0e...@syzkaller.appspotmail.com
OK claudio@ deraadt@

The issue is harmless if DIAGNOSTIC is not enabled.


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



CVS commit: [netbsd-9] src/sys/kern

2023-11-27 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Nov 27 20:01:40 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: uipc_mbuf.c

Log Message:
Pull up following revision(s) (requested by ozaki-r in ticket #1768):
sys/kern/uipc_mbuf.c: revision 1.252

mbuf: avoid assertion failure when splitting mbuf cluster

>From OpenBSD:
commit 7b4d35e0a60ba1dd4daf4b1c2932020a22463a89
Author: bluhm 
Date:   Fri Oct 20 16:25:15 2023 +
Avoid assertion failure when splitting mbuf cluster.
m_split() calls m_align() to initialize the data pointer of newly
allocated mbuf.  If the new mbuf will be converted to a cluster,
this is not necessary.  If additionally the new mbuf is larger than
MLEN, this can lead to a panic.
Only call m_align() when a valid m_data is needed.  This is the
case if we do not refecence the existing cluster, but memcpy() the
data into the new mbuf.
Reported-by: syzbot+0e6817f5877926f0e...@syzkaller.appspotmail.com
OK claudio@ deraadt@

The issue is harmless if DIAGNOSTIC is not enabled.


To generate a diff of this commit:
cvs rdiff -u -r1.232.4.2 -r1.232.4.3 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.232.4.2 src/sys/kern/uipc_mbuf.c:1.232.4.3
--- src/sys/kern/uipc_mbuf.c:1.232.4.2	Mon Oct 25 15:47:50 2021
+++ src/sys/kern/uipc_mbuf.c	Mon Nov 27 20:01:40 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_mbuf.c,v 1.232.4.2 2021/10/25 15:47:50 martin Exp $	*/
+/*	$NetBSD: uipc_mbuf.c,v 1.232.4.3 2023/11/27 20:01:40 martin Exp $	*/
 
 /*
  * Copyright (c) 1999, 2001, 2018 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.232.4.2 2021/10/25 15:47:50 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.232.4.3 2023/11/27 20:01:40 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_mbuftrace.h"
@@ -1273,10 +1273,7 @@ m_split_internal(struct mbuf *m0, int le
 		len_save = m0->m_pkthdr.len;
 		m0->m_pkthdr.len = len0;
 
-		if (m->m_flags & M_EXT)
-			goto extpacket;
-
-		if (remain > MHLEN) {
+		if ((m->m_flags & M_EXT) == 0 && remain > MHLEN) {
 			/* m can't be the lead packet */
 			m_align(n, 0);
 			n->m_len = 0;
@@ -1287,8 +1284,6 @@ m_split_internal(struct mbuf *m0, int le
 return NULL;
 			}
 			return n;
-		} else {
-			m_align(n, remain);
 		}
 	} else if (remain == 0) {
 		n = m->m_next;
@@ -1299,14 +1294,13 @@ m_split_internal(struct mbuf *m0, int le
 		if (n == NULL)
 			return NULL;
 		MCLAIM(n, m->m_owner);
-		m_align(n, remain);
 	}
 
-extpacket:
 	if (m->m_flags & M_EXT) {
 		n->m_data = m->m_data + len;
 		MCLADDREFERENCE(m, n);
 	} else {
+		m_align(n, remain);
 		memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
 	}
 



CVS commit: [netbsd-9] src/sys/kern

2023-10-18 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Oct 18 15:07:06 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: subr_thmap.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1755):

sys/kern/subr_thmap.c: revision 1.14
sys/kern/subr_thmap.c: revision 1.15

thmap(9): Test alloc failure, not THMAP_GETPTR failure.
THMAP_GETPTR may return nonnull even though alloc returned zero.

Note that this failure branch is not actually appropriate;
thmap_create should not fail.  We really need to pass KM_SLEEP
through in this call site even though there are other call sites for
which KM_NOSLEEP is appropriate.

Adapted from: https://github.com/rmind/thmap/pull/14
PR kern/57666
https://github.com/rmind/thmap/issues/13

thmap(9): Preallocate GC list storage for thmap_del.
thmap_del can't fail, and it is used in places in npf where sleeping
is forbidden, so it can't rely on allocating memory either.
Instead of having thmap_del allocate memory on the fly for each
object to defer freeing until thmap_gc, arrange to have thmap(9)
preallocate the same storage when allocating all the objects in the
first place, with a GC header.

This is suboptimal for memory usage, especially on insertion- and
lookup-heavy but deletion-light workloads, but it's not clear rmind's
alternative (https://github.com/rmind/thmap/tree/thmap_del_mem_fail)
is ready to use yet, so we'll go with this for correctness.
PR kern/57208

https://github.com/rmind/npf/issues/129


To generate a diff of this commit:
cvs rdiff -u -r1.5.6.1 -r1.5.6.2 src/sys/kern/subr_thmap.c

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



CVS commit: [netbsd-9] src/sys/kern

2023-10-18 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Oct 18 15:07:06 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: subr_thmap.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1755):

sys/kern/subr_thmap.c: revision 1.14
sys/kern/subr_thmap.c: revision 1.15

thmap(9): Test alloc failure, not THMAP_GETPTR failure.
THMAP_GETPTR may return nonnull even though alloc returned zero.

Note that this failure branch is not actually appropriate;
thmap_create should not fail.  We really need to pass KM_SLEEP
through in this call site even though there are other call sites for
which KM_NOSLEEP is appropriate.

Adapted from: https://github.com/rmind/thmap/pull/14
PR kern/57666
https://github.com/rmind/thmap/issues/13

thmap(9): Preallocate GC list storage for thmap_del.
thmap_del can't fail, and it is used in places in npf where sleeping
is forbidden, so it can't rely on allocating memory either.
Instead of having thmap_del allocate memory on the fly for each
object to defer freeing until thmap_gc, arrange to have thmap(9)
preallocate the same storage when allocating all the objects in the
first place, with a GC header.

This is suboptimal for memory usage, especially on insertion- and
lookup-heavy but deletion-light workloads, but it's not clear rmind's
alternative (https://github.com/rmind/thmap/tree/thmap_del_mem_fail)
is ready to use yet, so we'll go with this for correctness.
PR kern/57208

https://github.com/rmind/npf/issues/129


To generate a diff of this commit:
cvs rdiff -u -r1.5.6.1 -r1.5.6.2 src/sys/kern/subr_thmap.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_thmap.c
diff -u src/sys/kern/subr_thmap.c:1.5.6.1 src/sys/kern/subr_thmap.c:1.5.6.2
--- src/sys/kern/subr_thmap.c:1.5.6.1	Mon May 25 17:19:37 2020
+++ src/sys/kern/subr_thmap.c	Wed Oct 18 15:07:06 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_thmap.c,v 1.5.6.1 2020/05/25 17:19:37 martin Exp $	*/
+/*	$NetBSD: subr_thmap.c,v 1.5.6.2 2023/10/18 15:07:06 martin Exp $	*/
 
 /*-
  * Copyright (c) 2018 Mindaugas Rasiukevicius 
@@ -111,7 +111,7 @@
 #include "utils.h"
 #endif
 
-THMAP_RCSID("$NetBSD: subr_thmap.c,v 1.5.6.1 2020/05/25 17:19:37 martin Exp $");
+THMAP_RCSID("$NetBSD: subr_thmap.c,v 1.5.6.2 2023/10/18 15:07:06 martin Exp $");
 
 /*
  * NetBSD kernel wrappers
@@ -207,11 +207,17 @@ typedef struct {
 	uint32_t	hashval;	// current hash value
 } thmap_query_t;
 
-typedef struct {
-	uintptr_t	addr;
+union thmap_align {
+	void *		p;
+	uint64_t	v;
+};
+
+typedef struct thmap_gc thmap_gc_t;
+struct thmap_gc {
 	size_t		len;
-	void *		next;
-} thmap_gc_t;
+	thmap_gc_t *	next;
+	char		data[] __aligned(sizeof(union thmap_align));
+};
 
 #define	THMAP_ROOT_LEN	(sizeof(thmap_ptr_t) * ROOT_SIZE)
 
@@ -246,6 +252,34 @@ static const thmap_ops_t thmap_default_o
 	.free = free_wrapper
 };
 
+static uintptr_t
+gc_alloc(const thmap_t *thmap, size_t len)
+{
+	const size_t alloclen = offsetof(struct thmap_gc, data[len]);
+	const uintptr_t gcaddr = thmap->ops->alloc(alloclen);
+
+	if (!gcaddr)
+		return 0;
+
+	thmap_gc_t *const gc = THMAP_GETPTR(thmap, gcaddr);
+	gc->len = len;
+	return THMAP_GETOFF(thmap, >data[0]);
+}
+
+static void
+gc_free(const thmap_t *thmap, uintptr_t addr, size_t len)
+{
+	const size_t alloclen = offsetof(struct thmap_gc, data[len]);
+	char *const ptr = THMAP_GETPTR(thmap, addr);
+	thmap_gc_t *const gc = container_of(ptr, struct thmap_gc, data[0]);
+	const uintptr_t gcaddr = THMAP_GETOFF(thmap, gc);
+
+	KASSERTMSG(gc->len == len, "thmap=%p ops=%p addr=%p len=%zu"
+	" gc=%p gc->len=%zu",
+	thmap, thmap->ops, (void *)addr, len, gc, gc->len);
+	thmap->ops->free(gcaddr, alloclen);
+}
+
 /*
  * NODE LOCKING.
  */
@@ -361,7 +395,7 @@ node_create(thmap_t *thmap, thmap_inode_
 	thmap_inode_t *node;
 	uintptr_t p;
 
-	p = thmap->ops->alloc(THMAP_INODE_LEN);
+	p = gc_alloc(thmap, THMAP_INODE_LEN);
 	if (!p) {
 		return NULL;
 	}
@@ -422,7 +456,7 @@ leaf_create(const thmap_t *thmap, const 
 	thmap_leaf_t *leaf;
 	uintptr_t leaf_off, key_off;
 
-	leaf_off = thmap->ops->alloc(sizeof(thmap_leaf_t));
+	leaf_off = gc_alloc(thmap, sizeof(thmap_leaf_t));
 	if (!leaf_off) {
 		return NULL;
 	}
@@ -433,9 +467,9 @@ leaf_create(const thmap_t *thmap, const 
 		/*
 		 * Copy the key.
 		 */
-		key_off = thmap->ops->alloc(len);
+		key_off = gc_alloc(thmap, len);
 		if (!key_off) {
-			thmap->ops->free(leaf_off, sizeof(thmap_leaf_t));
+			gc_free(thmap, leaf_off, sizeof(thmap_leaf_t));
 			return NULL;
 		}
 		memcpy(THMAP_GETPTR(thmap, key_off), key, len);
@@ -453,9 +487,9 @@ static void
 leaf_free(const thmap_t *thmap, thmap_leaf_t *leaf)
 {
 	if ((thmap->flags & THMAP_NOCOPY) == 0) {
-		thmap->ops->free(leaf->key, leaf->len);
+		gc_free(thmap, leaf->key, leaf->len);
 	}
-	thmap->ops->free(THMAP_GETOFF(thmap, leaf), sizeof(thmap_leaf_t));
+	gc_free(thmap, THMAP_GETOFF(thmap, leaf), 

CVS commit: [netbsd-9] src/sys/kern

2023-08-01 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Aug  1 14:54:58 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: subr_autoconf.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1688):

sys/kern/subr_autoconf.c: revision 1.308

autoconf(9): Avoid potential ABA bug in config_makeroom.

When we unlock alldevs_lock to allocate a new cd_devs array nsp,
other threads may have:
1. freed the old one (osp),
2. done some other memory allocation,
3. allocated a new _larger_ array whose address happens to concide
   with osp (e.g., in (2) the page was recycled for a different pool
   cache), and
4. updated cd_devs back to osp but increased cd_ndevs.

In that case, the memory may be corrupted: we try to copy the wrong
number of device_t pointers into nsp and we free osp with the wrong
(stale) length.

Avoid this by checking whether cd_ndevs has changed too -- if not,
osp might have been recycled but at least the lengths we're about to
copy and free are still correct so there's no harm in an ABA
situation.


To generate a diff of this commit:
cvs rdiff -u -r1.265 -r1.265.4.1 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.265 src/sys/kern/subr_autoconf.c:1.265.4.1
--- src/sys/kern/subr_autoconf.c:1.265	Sat Dec  1 02:08:16 2018
+++ src/sys/kern/subr_autoconf.c	Tue Aug  1 14:54:58 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.265 2018/12/01 02:08:16 msaitoh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.265.4.1 2023/08/01 14:54:58 martin Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.265 2018/12/01 02:08:16 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.265.4.1 2023/08/01 14:54:58 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1195,7 +1195,7 @@ config_makeroom(int n, struct cfdriver *
 		 * If another thread moved the array while we did
 		 * not hold alldevs_lock, try again.
 		 */
-		if (cd->cd_devs != osp) {
+		if (cd->cd_devs != osp || cd->cd_ndevs != ondevs) {
 			mutex_exit(_lock);
 			kmem_free(nsp, sizeof(device_t[nndevs]));
 			mutex_enter(_lock);



CVS commit: [netbsd-9] src/sys/kern

2023-08-01 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Aug  1 14:54:58 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: subr_autoconf.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1688):

sys/kern/subr_autoconf.c: revision 1.308

autoconf(9): Avoid potential ABA bug in config_makeroom.

When we unlock alldevs_lock to allocate a new cd_devs array nsp,
other threads may have:
1. freed the old one (osp),
2. done some other memory allocation,
3. allocated a new _larger_ array whose address happens to concide
   with osp (e.g., in (2) the page was recycled for a different pool
   cache), and
4. updated cd_devs back to osp but increased cd_ndevs.

In that case, the memory may be corrupted: we try to copy the wrong
number of device_t pointers into nsp and we free osp with the wrong
(stale) length.

Avoid this by checking whether cd_ndevs has changed too -- if not,
osp might have been recycled but at least the lengths we're about to
copy and free are still correct so there's no harm in an ABA
situation.


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

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



CVS commit: [netbsd-9] src/sys/kern

2023-07-31 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Jul 31 16:21:46 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: uipc_domain.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1685):

sys/kern/uipc_domain.c: revision 1.109

sockaddr_alloc(9): Avoid uninitialized buffer in sockaddr_checklen.

Manifests only under DIAGNOSTIC because the DIAGNOSTIC check itself
uses an uninitialized buffer.


To generate a diff of this commit:
cvs rdiff -u -r1.106 -r1.106.4.1 src/sys/kern/uipc_domain.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_domain.c
diff -u src/sys/kern/uipc_domain.c:1.106 src/sys/kern/uipc_domain.c:1.106.4.1
--- src/sys/kern/uipc_domain.c:1.106	Thu Dec 27 07:56:43 2018
+++ src/sys/kern/uipc_domain.c	Mon Jul 31 16:21:46 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_domain.c,v 1.106 2018/12/27 07:56:43 maxv Exp $	*/
+/*	$NetBSD: uipc_domain.c,v 1.106.4.1 2023/07/31 16:21:46 martin Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.106 2018/12/27 07:56:43 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.106.4.1 2023/07/31 16:21:46 martin Exp $");
 
 #include 
 #include 
@@ -324,6 +324,15 @@ sockaddr_alloc(sa_family_t af, socklen_t
 	struct sockaddr *sa;
 	socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
 
+#ifdef DIAGNOSTIC
+	/*
+	 * sockaddr_checklen passes sa to sockaddr_format which
+	 * requires it to be fully initialized.
+	 *
+	 * XXX This should be factored better.
+	 */
+	flags |= M_ZERO;
+#endif
 	if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
 		return NULL;
 



CVS commit: [netbsd-9] src/sys/kern

2023-07-31 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Jul 31 16:21:46 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: uipc_domain.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1685):

sys/kern/uipc_domain.c: revision 1.109

sockaddr_alloc(9): Avoid uninitialized buffer in sockaddr_checklen.

Manifests only under DIAGNOSTIC because the DIAGNOSTIC check itself
uses an uninitialized buffer.


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

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



CVS commit: [netbsd-9] src/sys/kern

2023-07-31 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Jul 31 14:48:07 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: kern_rwlock.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1678):

sys/kern/kern_rwlock.c: revision 1.68 (patch)

rwlock(9): Fix membars.

rw_downgrade must be a release operation, and rw_tryupgrade must be
an acquire operation.  membar_producer is not enough -- need to use
membar_release and membar_acquire.

Discussed on tech-kern:
https://mail-index.netbsd.org/tech-kern/2023/02/22/msg028726.html


To generate a diff of this commit:
cvs rdiff -u -r1.54.2.1 -r1.54.2.2 src/sys/kern/kern_rwlock.c

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



CVS commit: [netbsd-9] src/sys/kern

2023-07-31 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Jul 31 14:48:07 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: kern_rwlock.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1678):

sys/kern/kern_rwlock.c: revision 1.68 (patch)

rwlock(9): Fix membars.

rw_downgrade must be a release operation, and rw_tryupgrade must be
an acquire operation.  membar_producer is not enough -- need to use
membar_release and membar_acquire.

Discussed on tech-kern:
https://mail-index.netbsd.org/tech-kern/2023/02/22/msg028726.html


To generate a diff of this commit:
cvs rdiff -u -r1.54.2.1 -r1.54.2.2 src/sys/kern/kern_rwlock.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_rwlock.c
diff -u src/sys/kern/kern_rwlock.c:1.54.2.1 src/sys/kern/kern_rwlock.c:1.54.2.2
--- src/sys/kern/kern_rwlock.c:1.54.2.1	Mon Jul 31 14:40:04 2023
+++ src/sys/kern/kern_rwlock.c	Mon Jul 31 14:48:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_rwlock.c,v 1.54.2.1 2023/07/31 14:40:04 martin Exp $	*/
+/*	$NetBSD: kern_rwlock.c,v 1.54.2.2 2023/07/31 14:48:07 martin Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.54.2.1 2023/07/31 14:40:04 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.54.2.2 2023/07/31 14:48:07 martin Exp $");
 
 #define	__RWLOCK_PRIVATE
 
@@ -594,8 +594,7 @@ rw_downgrade(krwlock_t *rw)
 	__USE(curthread);
 #endif
 
-
-	membar_producer();
+	membar_exit();
 	owner = rw->rw_owner;
 	if ((owner & RW_HAS_WAITERS) == 0) {
 		/*
@@ -691,7 +690,7 @@ rw_tryupgrade(krwlock_t *rw)
 		newown = curthread | RW_WRITE_LOCKED | (owner & ~RW_THREAD);
 		next = rw_cas(rw, owner, newown);
 		if (__predict_true(next == owner)) {
-			membar_producer();
+			membar_enter(); /* XXX membar_acquire */
 			break;
 		}
 	}



CVS commit: [netbsd-9] src/sys/kern

2023-07-31 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Jul 31 14:40:04 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: kern_lock.c kern_mutex.c kern_rwlock.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1677):

sys/kern/kern_rwlock.c: revision 1.67
sys/kern/kern_lock.c: revision 1.182
sys/kern/kern_mutex.c: revision 1.102

Sprinkle __predict_{true,false} for panicstr checks


To generate a diff of this commit:
cvs rdiff -u -r1.163 -r1.163.2.1 src/sys/kern/kern_lock.c
cvs rdiff -u -r1.79.2.1 -r1.79.2.2 src/sys/kern/kern_mutex.c
cvs rdiff -u -r1.54 -r1.54.2.1 src/sys/kern/kern_rwlock.c

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



CVS commit: [netbsd-9] src/sys/kern

2023-07-31 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Jul 31 14:40:04 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: kern_lock.c kern_mutex.c kern_rwlock.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1677):

sys/kern/kern_rwlock.c: revision 1.67
sys/kern/kern_lock.c: revision 1.182
sys/kern/kern_mutex.c: revision 1.102

Sprinkle __predict_{true,false} for panicstr checks


To generate a diff of this commit:
cvs rdiff -u -r1.163 -r1.163.2.1 src/sys/kern/kern_lock.c
cvs rdiff -u -r1.79.2.1 -r1.79.2.2 src/sys/kern/kern_mutex.c
cvs rdiff -u -r1.54 -r1.54.2.1 src/sys/kern/kern_rwlock.c

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

Modified files:

Index: src/sys/kern/kern_lock.c
diff -u src/sys/kern/kern_lock.c:1.163 src/sys/kern/kern_lock.c:1.163.2.1
--- src/sys/kern/kern_lock.c:1.163	Thu May  9 05:00:31 2019
+++ src/sys/kern/kern_lock.c	Mon Jul 31 14:40:04 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lock.c,v 1.163 2019/05/09 05:00:31 ozaki-r Exp $	*/
+/*	$NetBSD: kern_lock.c,v 1.163.2.1 2023/07/31 14:40:04 martin Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.163 2019/05/09 05:00:31 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.163.2.1 2023/07/31 14:40:04 martin Exp $");
 
 #include 
 #include 
@@ -63,7 +63,7 @@ assert_sleepable(void)
 	uint64_t pctr;
 	bool idle;
 
-	if (panicstr != NULL) {
+	if (__predict_false(panicstr != NULL)) {
 		return;
 	}
 

Index: src/sys/kern/kern_mutex.c
diff -u src/sys/kern/kern_mutex.c:1.79.2.1 src/sys/kern/kern_mutex.c:1.79.2.2
--- src/sys/kern/kern_mutex.c:1.79.2.1	Sun Mar  8 11:21:29 2020
+++ src/sys/kern/kern_mutex.c	Mon Jul 31 14:40:04 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_mutex.c,v 1.79.2.1 2020/03/08 11:21:29 martin Exp $	*/
+/*	$NetBSD: kern_mutex.c,v 1.79.2.2 2023/07/31 14:40:04 martin Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 #define	__MUTEX_PRIVATE
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.79.2.1 2020/03/08 11:21:29 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.79.2.2 2023/07/31 14:40:04 martin Exp $");
 
 #include 
 #include 
@@ -525,7 +525,7 @@ mutex_vector_enter(kmutex_t *mtx)
 	MUTEX_ASSERT(mtx, !cpu_intr_p());
 	MUTEX_WANTLOCK(mtx);
 
-	if (panicstr == NULL) {
+	if (__predict_true(panicstr == NULL)) {
 		KDASSERT(pserialize_not_in_read_section());
 		LOCKDEBUG_BARRIER(_lock, 1);
 	}

Index: src/sys/kern/kern_rwlock.c
diff -u src/sys/kern/kern_rwlock.c:1.54 src/sys/kern/kern_rwlock.c:1.54.2.1
--- src/sys/kern/kern_rwlock.c:1.54	Thu May  9 05:00:31 2019
+++ src/sys/kern/kern_rwlock.c	Mon Jul 31 14:40:04 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_rwlock.c,v 1.54 2019/05/09 05:00:31 ozaki-r Exp $	*/
+/*	$NetBSD: kern_rwlock.c,v 1.54.2.1 2023/07/31 14:40:04 martin Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.54 2019/05/09 05:00:31 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.54.2.1 2023/07/31 14:40:04 martin Exp $");
 
 #define	__RWLOCK_PRIVATE
 
@@ -187,7 +187,7 @@ static void __noinline
 rw_abort(const char *func, size_t line, krwlock_t *rw, const char *msg)
 {
 
-	if (panicstr != NULL)
+	if (__predict_false(panicstr != NULL))
 		return;
 
 	LOCKDEBUG_ABORT(func, line, rw, _lockops, msg);
@@ -291,7 +291,7 @@ rw_vector_enter(krwlock_t *rw, const krw
 	RW_ASSERT(rw, curthread != 0);
 	RW_WANTLOCK(rw, op);
 
-	if (panicstr == NULL) {
+	if (__predict_true(panicstr == NULL)) {
 		KDASSERT(pserialize_not_in_read_section());
 		LOCKDEBUG_BARRIER(_lock, 1);
 	}



CVS commit: [netbsd-9] src/sys/kern

2023-03-07 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Mar  7 20:01:07 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: vfs_syscalls.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1610):

sys/kern/vfs_syscalls.c: revision 1.557

open(2): Don't map ERESTART to EINTR.

If a file or device's open function returns ERESTART, respect that --
restart the syscall; don't pretend a signal has been delivered when
it was not.  If an SA_RESTART signal was delivered, POSIX does not
allow it to fail with EINTR:

SA_RESTART
This flag affects the behavior of interruptible functions;
that is, those specified to fail with errno set to [EINTR].
If set, and a function specified as interruptible is
interrupted by this signal, the function shall restart and
shall not fail with [EINTR] unless otherwise specified.  If
an interruptible function which uses a timeout is restarted,
the duration of the timeout following the restart is set to
an unspecified value that does not exceed the original
timeout value.  If the flag is not set, interruptible
functions interrupted by this signal shall fail with errno
set to [EINTR].

https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html

Nothing in the POSIX definition of open specifies otherwise.

In 1990, Kirk McKusick added these lines with a mysterious commit
message:
Author: Kirk McKusick 
Date:   Tue Apr 10 19:36:33 1990 -0800
eliminate longjmp from the kernel (for karels)
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index 7bc7b39bbf..d572d3a32d 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -14,7 +14,7 @@
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
- * @(#)vfs_syscalls.c  7.42 (Berkeley) 3/26/90
+ * @(#)vfs_syscalls.c  7.43 (Berkeley) 4/10/90
  */
 #include "param.h"
@@ -530,8 +530,10 @@ copen(scp, fmode, cmode, ndp, resultfd)
if (error = vn_open(ndp, fmode, (cmode & 0) &~ S_ISVTX)) {
crfree(fp->f_cred);
fp->f_count--;
-   if (error == -1)/* XXX from fdopen */
-   return (0); /* XXX from fdopen */
+   if (error == EJUSTRETURN)   /* XXX from fdopen */
+   return (0); /* XXX from fdopen */
+   if (error == ERESTART)
+   error = EINTR;
scp->sc_ofile[indx] = NULL;
return (error);
}

(found via this git import of the CSRG history:
https://github.com/robohack/ucb-csrg-bsd/commit/cce2869b7ae5d360921eb411005b328a29c4a3fe

This change appears to have served two related purposes:
1. The fdopen function (the erstwhile open routine for /dev/fd/N)
   used to return -1 as a hack to mean it had just duplicated the fd;
   it was recently changed by Mike Karels, in kern_descrip.c 7.9, to
   return EJUSTRETURN, now defined to be -2, presumably to avoid a
   conflict with ERESTART, defined to be -1.  So this change finished
   part of the change by Mike Karels to use a different magic return
   code from fdopen.
   Of course, today we use still another disgusting hack, EDUPFD, for
   the same purpose, so none of this is relevant any more.
2. Prior to April 1990, the kernel handled signals during tsleep(9)
   by longjmping out to the system call entry point or similar.  In
   April 1990, Mike Karels worked to convert all of that into
   explicit unwind logic by passing through EINTR or ERESTART as
   appropriate, instead of setjmp at each entry point.

However, it's not clear to me why this setjmp/longjmp and
fdopen/-1/EJUSTRETURN renovation justifies unconditional logic to map
ERESTART to EINTR in open(2).  I suspect it was a mistake.

In 2013, the corresponding logic to map ERESTART to EINTR in open(2)
was removed from FreeBSD:

   r246472 | kib | 2013-02-07 14:53:33 + (Thu, 07 Feb 2013) | 11 lines
   Stop translating the ERESTART error from the open(2) into EINTR.
   Posix requires that open(2) is restartable for SA_RESTART.
   For non-posix objects, in particular, devfs nodes, still disable
   automatic restart of the opens. The open call to a driver could have
   significant side effects for the hardware.
   Noted and reviewed by:  jilles
   Discussed with: bde
   MFC after:  2 weeks

Index: vfs_syscalls.c
===
--- vfs_syscalls.c  (revision 246471)
+++ vfs_syscalls.c  (revision 246472)
@@ -1106,8 +1106,6 @@
goto success;
}
-   if (error == ERESTART)
-   error = EINTR;
goto bad;
}
td->td_dupfd = 0;


CVS commit: [netbsd-9] src/sys/kern

2023-03-07 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Mar  7 20:01:07 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: vfs_syscalls.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1610):

sys/kern/vfs_syscalls.c: revision 1.557

open(2): Don't map ERESTART to EINTR.

If a file or device's open function returns ERESTART, respect that --
restart the syscall; don't pretend a signal has been delivered when
it was not.  If an SA_RESTART signal was delivered, POSIX does not
allow it to fail with EINTR:

SA_RESTART
This flag affects the behavior of interruptible functions;
that is, those specified to fail with errno set to [EINTR].
If set, and a function specified as interruptible is
interrupted by this signal, the function shall restart and
shall not fail with [EINTR] unless otherwise specified.  If
an interruptible function which uses a timeout is restarted,
the duration of the timeout following the restart is set to
an unspecified value that does not exceed the original
timeout value.  If the flag is not set, interruptible
functions interrupted by this signal shall fail with errno
set to [EINTR].

https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html

Nothing in the POSIX definition of open specifies otherwise.

In 1990, Kirk McKusick added these lines with a mysterious commit
message:
Author: Kirk McKusick 
Date:   Tue Apr 10 19:36:33 1990 -0800
eliminate longjmp from the kernel (for karels)
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index 7bc7b39bbf..d572d3a32d 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -14,7 +14,7 @@
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
- * @(#)vfs_syscalls.c  7.42 (Berkeley) 3/26/90
+ * @(#)vfs_syscalls.c  7.43 (Berkeley) 4/10/90
  */
 #include "param.h"
@@ -530,8 +530,10 @@ copen(scp, fmode, cmode, ndp, resultfd)
if (error = vn_open(ndp, fmode, (cmode & 0) &~ S_ISVTX)) {
crfree(fp->f_cred);
fp->f_count--;
-   if (error == -1)/* XXX from fdopen */
-   return (0); /* XXX from fdopen */
+   if (error == EJUSTRETURN)   /* XXX from fdopen */
+   return (0); /* XXX from fdopen */
+   if (error == ERESTART)
+   error = EINTR;
scp->sc_ofile[indx] = NULL;
return (error);
}

(found via this git import of the CSRG history:
https://github.com/robohack/ucb-csrg-bsd/commit/cce2869b7ae5d360921eb411005b328a29c4a3fe

This change appears to have served two related purposes:
1. The fdopen function (the erstwhile open routine for /dev/fd/N)
   used to return -1 as a hack to mean it had just duplicated the fd;
   it was recently changed by Mike Karels, in kern_descrip.c 7.9, to
   return EJUSTRETURN, now defined to be -2, presumably to avoid a
   conflict with ERESTART, defined to be -1.  So this change finished
   part of the change by Mike Karels to use a different magic return
   code from fdopen.
   Of course, today we use still another disgusting hack, EDUPFD, for
   the same purpose, so none of this is relevant any more.
2. Prior to April 1990, the kernel handled signals during tsleep(9)
   by longjmping out to the system call entry point or similar.  In
   April 1990, Mike Karels worked to convert all of that into
   explicit unwind logic by passing through EINTR or ERESTART as
   appropriate, instead of setjmp at each entry point.

However, it's not clear to me why this setjmp/longjmp and
fdopen/-1/EJUSTRETURN renovation justifies unconditional logic to map
ERESTART to EINTR in open(2).  I suspect it was a mistake.

In 2013, the corresponding logic to map ERESTART to EINTR in open(2)
was removed from FreeBSD:

   r246472 | kib | 2013-02-07 14:53:33 + (Thu, 07 Feb 2013) | 11 lines
   Stop translating the ERESTART error from the open(2) into EINTR.
   Posix requires that open(2) is restartable for SA_RESTART.
   For non-posix objects, in particular, devfs nodes, still disable
   automatic restart of the opens. The open call to a driver could have
   significant side effects for the hardware.
   Noted and reviewed by:  jilles
   Discussed with: bde
   MFC after:  2 weeks

Index: vfs_syscalls.c
===
--- vfs_syscalls.c  (revision 246471)
+++ vfs_syscalls.c  (revision 246472)
@@ -1106,8 +1106,6 @@
goto success;
}
-   if (error == ERESTART)
-   error = EINTR;
goto bad;
}
td->td_dupfd = 0;


CVS commit: [netbsd-9] src/sys/kern

2023-02-14 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Feb 14 16:19:00 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: subr_devsw.c

Log Message:
Pull up following revision(s) (requested by buhrow in ticket #1595):

sys/kern/subr_devsw.c: revision 1.50

When a device driver calls devsw_attach() it has the option of 
attaching a block device
structure and a character device structure, or, just the character device 
structure.
With the existing code, if a driver elects not to attach a block device 
structure and if it
asks for a major number to be dynamically assigned to its character interface,
that driver will not be able to detach and reattach its character driver 
interface.  This is a very
long standing bug which didn't come to light until we began using loadable 
kernel modules more
heavily.  this patch fixes this problem.  With this patch in place, drivers 
that implement only
a character device interface may detach and reattach that character interface 
as often as they
need to.

Fixes PR kern/57229


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

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



CVS commit: [netbsd-9] src/sys/kern

2023-02-14 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Feb 14 16:19:00 UTC 2023

Modified Files:
src/sys/kern [netbsd-9]: subr_devsw.c

Log Message:
Pull up following revision(s) (requested by buhrow in ticket #1595):

sys/kern/subr_devsw.c: revision 1.50

When a device driver calls devsw_attach() it has the option of 
attaching a block device
structure and a character device structure, or, just the character device 
structure.
With the existing code, if a driver elects not to attach a block device 
structure and if it
asks for a major number to be dynamically assigned to its character interface,
that driver will not be able to detach and reattach its character driver 
interface.  This is a very
long standing bug which didn't come to light until we began using loadable 
kernel modules more
heavily.  this patch fixes this problem.  With this patch in place, drivers 
that implement only
a character device interface may detach and reattach that character interface 
as often as they
need to.

Fixes PR kern/57229


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

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

Modified files:

Index: src/sys/kern/subr_devsw.c
diff -u src/sys/kern/subr_devsw.c:1.38 src/sys/kern/subr_devsw.c:1.38.8.1
--- src/sys/kern/subr_devsw.c:1.38	Tue Nov  7 18:35:57 2017
+++ src/sys/kern/subr_devsw.c	Tue Feb 14 16:19:00 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_devsw.c,v 1.38 2017/11/07 18:35:57 christos Exp $	*/
+/*	$NetBSD: subr_devsw.c,v 1.38.8.1 2023/02/14 16:19:00 martin Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.38 2017/11/07 18:35:57 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.38.8.1 2023/02/14 16:19:00 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -139,7 +139,7 @@ devsw_attach(const char *devname,
 		if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
 			continue;
 
-		if (*bmajor < 0)
+		if ((bdev != NULL) && (*bmajor < 0)) 
 			*bmajor = conv->d_bmajor;
 		if (*cmajor < 0)
 			*cmajor = conv->d_cmajor;



CVS commit: [netbsd-9] src/sys/kern

2022-09-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sat Sep 10 08:19:35 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: kern_core.c

Log Message:
Pull up following revision(s) (requested by mrg in ticket #1517):

sys/kern/kern_core.c: revision 1.37

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


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

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

Modified files:

Index: src/sys/kern/kern_core.c
diff -u src/sys/kern/kern_core.c:1.24.22.2 src/sys/kern/kern_core.c:1.24.22.3
--- src/sys/kern/kern_core.c:1.24.22.2	Fri Sep  9 18:20:51 2022
+++ src/sys/kern/kern_core.c	Sat Sep 10 08:19:34 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_core.c,v 1.24.22.2 2022/09/09 18:20:51 martin Exp $	*/
+/*	$NetBSD: kern_core.c,v 1.24.22.3 2022/09/10 08:19:34 martin Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.24.22.2 2022/09/09 18:20:51 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.24.22.3 2022/09/10 08:19:34 martin Exp $");
 
 #include 
 #include 
@@ -104,7 +104,7 @@ coredump(struct lwp *l, const char *patt
 	struct coredump_iostate	io;
 	struct plimit		*lim;
 	int			error, error1;
-	char			*name, *lastslash;
+	char			*name, *lastslash = NULL /* XXXgcc */;
 
 	name = PNBUF_GET();
 



CVS commit: [netbsd-9] src/sys/kern

2022-09-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sat Sep 10 08:19:35 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: kern_core.c

Log Message:
Pull up following revision(s) (requested by mrg in ticket #1517):

sys/kern/kern_core.c: revision 1.37

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


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

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



CVS commit: [netbsd-9] src/sys/kern

2022-09-09 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Fri Sep  9 18:20:51 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: kern_core.c

Log Message:
Pull up following revision(s) (requested by christos in ticket #1516):

sys/kern/kern_core.c: revision 1.36

Don't forget to free the cred we just held.
Thanks to Chris J-D (chris at accessvector dot net)

While here, de-duplicate the mutex exit sequence.


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

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



CVS commit: [netbsd-9] src/sys/kern

2022-09-09 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Fri Sep  9 18:20:51 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: kern_core.c

Log Message:
Pull up following revision(s) (requested by christos in ticket #1516):

sys/kern/kern_core.c: revision 1.36

Don't forget to free the cred we just held.
Thanks to Chris J-D (chris at accessvector dot net)

While here, de-duplicate the mutex exit sequence.


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

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

Modified files:

Index: src/sys/kern/kern_core.c
diff -u src/sys/kern/kern_core.c:1.24.22.1 src/sys/kern/kern_core.c:1.24.22.2
--- src/sys/kern/kern_core.c:1.24.22.1	Mon Nov 11 17:11:07 2019
+++ src/sys/kern/kern_core.c	Fri Sep  9 18:20:51 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_core.c,v 1.24.22.1 2019/11/11 17:11:07 martin Exp $	*/
+/*	$NetBSD: kern_core.c,v 1.24.22.2 2022/09/09 18:20:51 martin Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.24.22.1 2019/11/11 17:11:07 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.24.22.2 2022/09/09 18:20:51 martin Exp $");
 
 #include 
 #include 
@@ -97,7 +97,7 @@ coredump(struct lwp *l, const char *patt
 	struct vnode		*vp;
 	struct proc		*p;
 	struct vmspace		*vm;
-	kauth_cred_t		cred;
+	kauth_cred_t		cred = NULL;
 	struct pathbuf		*pb;
 	struct nameidata	nd;
 	struct vattr		vattr;
@@ -122,9 +122,7 @@ coredump(struct lwp *l, const char *patt
 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
 	p->p_rlimit[RLIMIT_CORE].rlim_cur) {
 		error = EFBIG;		/* better error code? */
-		mutex_exit(p->p_lock);
-		mutex_exit(proc_lock);
-		goto done;
+		goto release;
 	}
 
 	/*
@@ -141,9 +139,7 @@ coredump(struct lwp *l, const char *patt
 	if (p->p_flag & PK_SUGID) {
 		if (!security_setidcore_dump) {
 			error = EPERM;
-			mutex_exit(p->p_lock);
-			mutex_exit(proc_lock);
-			goto done;
+			goto release;
 		}
 		pattern = security_setidcore_path;
 	}
@@ -157,11 +153,8 @@ coredump(struct lwp *l, const char *patt
 	error = coredump_buildname(p, name, pattern, MAXPATHLEN);
 	mutex_exit(>pl_lock);
 
-	if (error) {
-		mutex_exit(p->p_lock);
-		mutex_exit(proc_lock);
-		goto done;
-	}
+	if (error)
+		goto release;
 
 	/*
 	 * On a simple filename, see if the filesystem allow us to write
@@ -175,6 +168,7 @@ coredump(struct lwp *l, const char *patt
 			error = EPERM;
 	}
 
+release:
 	mutex_exit(p->p_lock);
 	mutex_exit(proc_lock);
 	if (error)
@@ -262,6 +256,8 @@ coredump(struct lwp *l, const char *patt
 	if (error == 0)
 		error = error1;
 done:
+	if (cred != NULL)
+		kauth_cred_free(cred);
 	if (name != NULL)
 		PNBUF_PUT(name);
 	return error;



CVS commit: [netbsd-9] src/sys/kern

2022-08-17 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Aug 17 16:43:01 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: uipc_sem.c

Log Message:
Pull up following revision(s) (requested by chs in ticket #1501):

sys/kern/uipc_sem.c: revision 1.60

when updating the per-uid "semcnt", decrement the counter for the uid
that created the ksem, not the uid of the process freeing the ksem.
fixes PR 55509.


To generate a diff of this commit:
cvs rdiff -u -r1.55.4.2 -r1.55.4.3 src/sys/kern/uipc_sem.c

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



CVS commit: [netbsd-9] src/sys/kern

2022-08-17 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Aug 17 16:43:01 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: uipc_sem.c

Log Message:
Pull up following revision(s) (requested by chs in ticket #1501):

sys/kern/uipc_sem.c: revision 1.60

when updating the per-uid "semcnt", decrement the counter for the uid
that created the ksem, not the uid of the process freeing the ksem.
fixes PR 55509.


To generate a diff of this commit:
cvs rdiff -u -r1.55.4.2 -r1.55.4.3 src/sys/kern/uipc_sem.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_sem.c
diff -u src/sys/kern/uipc_sem.c:1.55.4.2 src/sys/kern/uipc_sem.c:1.55.4.3
--- src/sys/kern/uipc_sem.c:1.55.4.2	Tue May  5 20:12:37 2020
+++ src/sys/kern/uipc_sem.c	Wed Aug 17 16:43:01 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_sem.c,v 1.55.4.2 2020/05/05 20:12:37 martin Exp $	*/
+/*	$NetBSD: uipc_sem.c,v 1.55.4.3 2022/08/17 16:43:01 martin Exp $	*/
 
 /*-
  * Copyright (c) 2011, 2019 The NetBSD Foundation, Inc.
@@ -60,7 +60,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.55.4.2 2020/05/05 20:12:37 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.55.4.3 2022/08/17 16:43:01 martin Exp $");
 
 #include 
 #include 
@@ -470,8 +470,6 @@ ksem_create(lwp_t *l, const char *name, 
 		len = 0;
 	}
 
-	chgsemcnt(kauth_cred_getuid(l->l_cred), 1);
-
 	ks = kmem_zalloc(sizeof(ksem_t), KM_SLEEP);
 	mutex_init(>ks_lock, MUTEX_DEFAULT, IPL_NONE);
 	cv_init(>ks_cv, "psem");
@@ -484,8 +482,9 @@ ksem_create(lwp_t *l, const char *name, 
 	uc = l->l_cred;
 	ks->ks_uid = kauth_cred_geteuid(uc);
 	ks->ks_gid = kauth_cred_getegid(uc);
-
+	chgsemcnt(ks->ks_uid, 1);
 	atomic_inc_uint(_total);
+
 	*ksret = ks;
 	return 0;
 }
@@ -496,6 +495,9 @@ ksem_free(ksem_t *ks)
 
 	KASSERT(!cv_has_waiters(>ks_cv));
 
+	chgsemcnt(ks->ks_uid, -1);
+	atomic_dec_uint(_total);
+
 	if (ks->ks_pshared_id) {
 		KASSERT(ks->ks_pshared_proc == NULL);
 		ksem_remove_pshared(ks);
@@ -507,9 +509,6 @@ ksem_free(ksem_t *ks)
 	mutex_destroy(>ks_lock);
 	cv_destroy(>ks_cv);
 	kmem_free(ks, sizeof(ksem_t));
-
-	atomic_dec_uint(_total);
-	chgsemcnt(kauth_cred_getuid(curproc->p_cred), -1);
 }
 
 #define	KSEM_ID_IS_PSHARED(id)		\



CVS commit: [netbsd-9] src/sys/kern

2022-07-17 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Jul 17 10:34:10 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: subr_pool.c

Log Message:
Pull up following revision(s) (requested by simonb in ticket #1479):

sys/kern/subr_pool.c: revision 1.285

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


To generate a diff of this commit:
cvs rdiff -u -r1.252.2.3 -r1.252.2.4 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.252.2.3 src/sys/kern/subr_pool.c:1.252.2.4
--- src/sys/kern/subr_pool.c:1.252.2.3	Sun Mar  8 11:04:43 2020
+++ src/sys/kern/subr_pool.c	Sun Jul 17 10:34:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_pool.c,v 1.252.2.3 2020/03/08 11:04:43 martin Exp $	*/
+/*	$NetBSD: subr_pool.c,v 1.252.2.4 2022/07/17 10:34:10 martin 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.252.2.3 2020/03/08 11:04:43 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.252.2.4 2022/07/17 10:34:10 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1678,10 +1678,11 @@ pool_totalpages_locked(void)
 	uint64_t total = 0;
 
 	TAILQ_FOREACH(pp, _head, pr_poollist) {
-		uint64_t bytes = pp->pr_npages * pp->pr_alloc->pa_pagesz;
+		uint64_t bytes =
+		(uint64_t)pp->pr_npages * pp->pr_alloc->pa_pagesz;
 
 		if ((pp->pr_roflags & PR_RECURSIVE) != 0)
-			bytes -= (pp->pr_nout * pp->pr_size);
+			bytes -= ((uint64_t)pp->pr_nout * pp->pr_size);
 		total += bytes;
 	}
 



CVS commit: [netbsd-9] src/sys/kern

2022-07-17 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Jul 17 10:34:10 UTC 2022

Modified Files:
src/sys/kern [netbsd-9]: subr_pool.c

Log Message:
Pull up following revision(s) (requested by simonb in ticket #1479):

sys/kern/subr_pool.c: revision 1.285

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


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

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



CVS commit: [netbsd-9] src/sys/kern

2021-10-08 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Fri Oct  8 14:58:08 UTC 2021

Modified Files:
src/sys/kern [netbsd-9]: sys_pipe.c

Log Message:
Pull up following revision(s) (requested by hannken in ticket #1357):

sys/kern/sys_pipe.c: revision 1.157

Fix a deadlock where one thread writes to a pipe, has more data
and no space in the pipe and waits on "pipe_wcv" while the reader
is closing the pipe and waits on "pipe_draincv".

Swap the test for "PIPE_EOF" and the "cv_wait_sig()" in "pipe_write()".

PR bin/56422 "zgrep -l sometimes hangs"


To generate a diff of this commit:
cvs rdiff -u -r1.148 -r1.148.2.1 src/sys/kern/sys_pipe.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/sys_pipe.c
diff -u src/sys/kern/sys_pipe.c:1.148 src/sys/kern/sys_pipe.c:1.148.2.1
--- src/sys/kern/sys_pipe.c:1.148	Fri Apr 26 17:24:23 2019
+++ src/sys/kern/sys_pipe.c	Fri Oct  8 14:58:08 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_pipe.c,v 1.148 2019/04/26 17:24:23 mlelstv Exp $	*/
+/*	$NetBSD: sys_pipe.c,v 1.148.2.1 2021/10/08 14:58:08 martin Exp $	*/
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.148 2019/04/26 17:24:23 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.148.2.1 2021/10/08 14:58:08 martin Exp $");
 
 #include 
 #include 
@@ -1006,11 +1006,6 @@ pipe_write(file_t *fp, off_t *offset, st
 break;
 			}
 
-			pipeunlock(wpipe);
-			error = cv_wait_sig(>pipe_wcv, lock);
-			(void)pipelock(wpipe, false);
-			if (error != 0)
-break;
 			/*
 			 * If read side wants to go away, we just issue a signal
 			 * to ourselves.
@@ -1019,6 +1014,12 @@ pipe_write(file_t *fp, off_t *offset, st
 error = EPIPE;
 break;
 			}
+
+			pipeunlock(wpipe);
+			error = cv_wait_sig(>pipe_wcv, lock);
+			(void)pipelock(wpipe, false);
+			if (error != 0)
+break;
 			wakeup_state = wpipe->pipe_state;
 		}
 	}



CVS commit: [netbsd-9] src/sys/kern

2021-10-08 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Fri Oct  8 14:58:08 UTC 2021

Modified Files:
src/sys/kern [netbsd-9]: sys_pipe.c

Log Message:
Pull up following revision(s) (requested by hannken in ticket #1357):

sys/kern/sys_pipe.c: revision 1.157

Fix a deadlock where one thread writes to a pipe, has more data
and no space in the pipe and waits on "pipe_wcv" while the reader
is closing the pipe and waits on "pipe_draincv".

Swap the test for "PIPE_EOF" and the "cv_wait_sig()" in "pipe_write()".

PR bin/56422 "zgrep -l sometimes hangs"


To generate a diff of this commit:
cvs rdiff -u -r1.148 -r1.148.2.1 src/sys/kern/sys_pipe.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-11-25 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Nov 25 17:00:22 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_cprng.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #481):

sys/kern/subr_cprng.c: revision 1.33

Use cprng_strong, not cprng_fast, for sysctl kern.arnd.


To generate a diff of this commit:
cvs rdiff -u -r1.30.2.1 -r1.30.2.2 src/sys/kern/subr_cprng.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_cprng.c
diff -u src/sys/kern/subr_cprng.c:1.30.2.1 src/sys/kern/subr_cprng.c:1.30.2.2
--- src/sys/kern/subr_cprng.c:1.30.2.1	Tue Sep  3 07:48:00 2019
+++ src/sys/kern/subr_cprng.c	Mon Nov 25 17:00:22 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_cprng.c,v 1.30.2.1 2019/09/03 07:48:00 martin Exp $ */
+/*	$NetBSD: subr_cprng.c,v 1.30.2.2 2019/11/25 17:00:22 martin Exp $ */
 
 /*-
  * Copyright (c) 2011-2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.30.2.1 2019/09/03 07:48:00 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.30.2.2 2019/11/25 17:00:22 martin Exp $");
 
 #include 
 #include 
@@ -539,6 +539,7 @@ cprng_strong_rndsink_callback(void *cont
 	mutex_exit(>cs_lock);
 }
 
+static ONCE_DECL(sysctl_prng_once);
 static cprng_strong_t *sysctl_prng;
 
 static int
@@ -558,10 +559,9 @@ makeprng(void)
 static int
 sysctl_kern_urnd(SYSCTLFN_ARGS)
 {
-	static ONCE_DECL(control);
 	int v, rv;
 
-	RUN_ONCE(, makeprng);
+	RUN_ONCE(_prng_once, makeprng);
 	rv = cprng_strong(sysctl_prng, , sizeof(v), 0);
 	if (rv == sizeof(v)) {
 		struct sysctlnode node = *rnode;
@@ -590,6 +590,7 @@ sysctl_kern_arnd(SYSCTLFN_ARGS)
 	int error;
 	void *v;
 	struct sysctlnode node = *rnode;
+	size_t n __diagused;
 
 	switch (*oldlenp) {
 	case 0:
@@ -598,8 +599,10 @@ sysctl_kern_arnd(SYSCTLFN_ARGS)
 		if (*oldlenp > 256) {
 			return E2BIG;
 		}
+		RUN_ONCE(_prng_once, makeprng);
 		v = kmem_alloc(*oldlenp, KM_SLEEP);
-		cprng_fast(v, *oldlenp);
+		n = cprng_strong(sysctl_prng, v, *oldlenp, 0);
+		KASSERT(n == *oldlenp);
 		node.sysctl_data = v;
 		node.sysctl_size = *oldlenp;
 		error = sysctl_lookup(SYSCTLFN_CALL());



CVS commit: [netbsd-9] src/sys/kern

2019-11-25 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Nov 25 17:00:22 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_cprng.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #481):

sys/kern/subr_cprng.c: revision 1.33

Use cprng_strong, not cprng_fast, for sysctl kern.arnd.


To generate a diff of this commit:
cvs rdiff -u -r1.30.2.1 -r1.30.2.2 src/sys/kern/subr_cprng.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-11-24 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Nov 24 08:16:53 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by rin in ticket #465):

sys/kern/sys_ptrace_common.c: revision 1.73

Fix regression introduced to ptrace_regs() in rev 1.27:
http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/kern/sys_ptrace_common.c#rev1.27

Size of registers should be determined from tracer, NOT tracee.

Now, 64-bit tracer can manipulate registers of 32-bit tracee again.
gdb for amd64 works for i386 binaries to some extent.

XXX
pullup to netbsd-9 and -8.


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.11 -r1.58.2.12 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-11-24 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Nov 24 08:16:53 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by rin in ticket #465):

sys/kern/sys_ptrace_common.c: revision 1.73

Fix regression introduced to ptrace_regs() in rev 1.27:
http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/kern/sys_ptrace_common.c#rev1.27

Size of registers should be determined from tracer, NOT tracee.

Now, 64-bit tracer can manipulate registers of 32-bit tracee again.
gdb for amd64 works for i386 binaries to some extent.

XXX
pullup to netbsd-9 and -8.


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.11 -r1.58.2.12 src/sys/kern/sys_ptrace_common.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/sys_ptrace_common.c
diff -u src/sys/kern/sys_ptrace_common.c:1.58.2.11 src/sys/kern/sys_ptrace_common.c:1.58.2.12
--- src/sys/kern/sys_ptrace_common.c:1.58.2.11	Tue Nov 19 13:22:01 2019
+++ src/sys/kern/sys_ptrace_common.c	Sun Nov 24 08:16:52 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.11 2019/11/19 13:22:01 martin Exp $	*/
+/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.12 2019/11/24 08:16:52 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.11 2019/11/19 13:22:01 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.12 2019/11/24 08:16:52 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -831,9 +831,12 @@ ptrace_regs(struct lwp *l, struct lwp **
 void *addr, size_t data)
 {
 	int error;
-	struct proc *t = (*lt)->l_proc;
+	struct proc *p, *t;
 	struct vmspace *vm;
 
+	p = l->l_proc;		/* tracer */
+	t = (*lt)->l_proc;	/* traced */
+
 	if ((error = ptrace_update_lwp(t, lt, data)) != 0)
 		return error;
 
@@ -849,7 +852,7 @@ ptrace_regs(struct lwp *l, struct lwp **
 	case_PT_SETREGS
 		if (!process_validregs(*lt))
 			return EINVAL;
-		size = PROC_REGSZ(t);
+		size = PROC_REGSZ(p);
 		func = ptm->ptm_doregs;
 		break;
 #endif
@@ -858,7 +861,7 @@ ptrace_regs(struct lwp *l, struct lwp **
 	case_PT_SETFPREGS
 		if (!process_validfpregs(*lt))
 			return EINVAL;
-		size = PROC_FPREGSZ(t);
+		size = PROC_FPREGSZ(p);
 		func = ptm->ptm_dofpregs;
 		break;
 #endif
@@ -867,7 +870,7 @@ ptrace_regs(struct lwp *l, struct lwp **
 	case_PT_SETDBREGS
 		if (!process_validdbregs(*lt))
 			return EINVAL;
-		size = PROC_DBREGSZ(t);
+		size = PROC_DBREGSZ(p);
 		func = ptm->ptm_dodbregs;
 		break;
 #endif



CVS commit: [netbsd-9] src/sys/kern

2019-11-19 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Nov 19 13:22:01 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by rin in ticket #455):

sys/kern/sys_ptrace_common.c: revision 1.72

Fix pointer arithmetic for 32-bit process on LP64 kernel in
process_auxv_offset().

Now, PIOD_READ_AUXV works fine with COMPAT_NETBSD32.

XXX
pullup to netbsd-9 and netbsd-8


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.10 -r1.58.2.11 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-11-19 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Nov 19 13:22:01 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by rin in ticket #455):

sys/kern/sys_ptrace_common.c: revision 1.72

Fix pointer arithmetic for 32-bit process on LP64 kernel in
process_auxv_offset().

Now, PIOD_READ_AUXV works fine with COMPAT_NETBSD32.

XXX
pullup to netbsd-9 and netbsd-8


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.10 -r1.58.2.11 src/sys/kern/sys_ptrace_common.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/sys_ptrace_common.c
diff -u src/sys/kern/sys_ptrace_common.c:1.58.2.10 src/sys/kern/sys_ptrace_common.c:1.58.2.11
--- src/sys/kern/sys_ptrace_common.c:1.58.2.10	Mon Nov 11 17:11:07 2019
+++ src/sys/kern/sys_ptrace_common.c	Tue Nov 19 13:22:01 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.10 2019/11/11 17:11:07 martin Exp $	*/
+/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.11 2019/11/19 13:22:01 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.10 2019/11/11 17:11:07 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.11 2019/11/19 13:22:01 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -1635,7 +1635,15 @@ process_auxv_offset(struct proc *p, stru
 	if (pss.ps_envstr == NULL)
 		return EIO;
 
-	uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
+#ifdef COMPAT_NETBSD32
+	if (p->p_flag & PK_32)
+		uio->uio_offset += (off_t)((vaddr_t)pss.ps_envstr +
+		sizeof(uint32_t) * (pss.ps_nenvstr + 1));
+	else
+#endif
+		uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr +
+		pss.ps_nenvstr + 1);
+
 #ifdef __MACHINE_STACK_GROWS_UP
 	if (uio->uio_offset < off)
 		return EIO;



CVS commit: [netbsd-9] src/sys/kern

2019-11-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Nov 10 13:24:50 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_sig.c

Log Message:
Pull up following revision(s) (requested by pgoyette in ticket #412):

sys/kern/sys_sig.c: revision 1.49

If we need to handle old-version signals, load the version-specific
"compat_16" module.  There is no longer a monolithic "compat" module!
XXX pullup-9 needed


To generate a diff of this commit:
cvs rdiff -u -r1.47.4.1 -r1.47.4.2 src/sys/kern/sys_sig.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-11-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Nov 10 13:24:50 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_sig.c

Log Message:
Pull up following revision(s) (requested by pgoyette in ticket #412):

sys/kern/sys_sig.c: revision 1.49

If we need to handle old-version signals, load the version-specific
"compat_16" module.  There is no longer a monolithic "compat" module!
XXX pullup-9 needed


To generate a diff of this commit:
cvs rdiff -u -r1.47.4.1 -r1.47.4.2 src/sys/kern/sys_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/sys_sig.c
diff -u src/sys/kern/sys_sig.c:1.47.4.1 src/sys/kern/sys_sig.c:1.47.4.2
--- src/sys/kern/sys_sig.c:1.47.4.1	Mon Oct 21 20:13:09 2019
+++ src/sys/kern/sys_sig.c	Sun Nov 10 13:24:50 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_sig.c,v 1.47.4.1 2019/10/21 20:13:09 martin Exp $	*/
+/*	$NetBSD: sys_sig.c,v 1.47.4.2 2019/11/10 13:24:50 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.47.4.1 2019/10/21 20:13:09 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.47.4.2 2019/11/10 13:24:50 martin Exp $");
 
 #include "opt_dtrace.h"
 
@@ -414,7 +414,7 @@ sigaction1(struct lwp *l, int signum, co
 			else if ((p->p_lflag & PL_SIGCOMPAT) == 0) {
 kernconfig_lock();
 if (sendsig_sigcontext_vec == NULL) {
-	(void)module_autoload("compat",
+	(void)module_autoload("compat_16",
 	MODULE_CLASS_ANY);
 }
 if (sendsig_sigcontext_vec != NULL) {



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:28:17 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #330):

sys/kern/kern_sig.c: revision 1.372

Avoid double lwp_exit() in eventswitch()

For the PTRACE_LWP_EXIT event, the eventswitch() call is triggered from
lwp_exit(). In the case of setting the program status to PS_WEXIT, do not
try to demise in place, by calling lwp_exit() as it causes panic.

In this scenario bail out from the function and resume the lwp_exit()
procedure.


To generate a diff of this commit:
cvs rdiff -u -r1.364.2.6 -r1.364.2.7 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.364.2.6 src/sys/kern/kern_sig.c:1.364.2.7
--- src/sys/kern/kern_sig.c:1.364.2.6	Tue Oct 15 19:27:04 2019
+++ src/sys/kern/kern_sig.c	Tue Oct 15 19:28:16 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.364.2.6 2019/10/15 19:27:04 martin Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.364.2.7 2019/10/15 19:28:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.6 2019/10/15 19:27:04 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.7 2019/10/15 19:28:16 martin Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -1612,6 +1612,12 @@ repeat:
 	if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
 		mutex_exit(p->p_lock);
 		mutex_exit(proc_lock);
+
+		if (pe_report_event == PTRACE_LWP_EXIT) {
+			/* Avoid double lwp_exit() and panic. */
+			return;
+		}
+
 		lwp_exit(l);
 		panic("eventswitch");
 		/* NOTREACHED */



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:28:17 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #330):

sys/kern/kern_sig.c: revision 1.372

Avoid double lwp_exit() in eventswitch()

For the PTRACE_LWP_EXIT event, the eventswitch() call is triggered from
lwp_exit(). In the case of setting the program status to PS_WEXIT, do not
try to demise in place, by calling lwp_exit() as it causes panic.

In this scenario bail out from the function and resume the lwp_exit()
procedure.


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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:27:04 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #329):

sys/kern/kern_sig.c: revision 1.371

Fix one the the root causes of unreliability of the ptrace(2)ed threads

In case of sigswitchin away in issignal() and continuing the execution on
PT_CONTINUE (or equivalent call), there is a time window when another
thread could cause the process state to be changed to PS_STOPPING.

In the current logic, a thread would receive signal 0 (no-signal) and exit
from issignal(), returning to userland and never finishing the process of
stopping all LWPs. This causes hangs waitpid() waiting for SIGCHLD and
the callout polling for the state of the process in an infinite loop.

Instead of prompting for a returned signal from a debugger, repeat the
issignal() loop, this will cause checking the PS_STOPPING flag again and
sigswitching away in the scenario of stopping the process.


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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:27:04 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #329):

sys/kern/kern_sig.c: revision 1.371

Fix one the the root causes of unreliability of the ptrace(2)ed threads

In case of sigswitchin away in issignal() and continuing the execution on
PT_CONTINUE (or equivalent call), there is a time window when another
thread could cause the process state to be changed to PS_STOPPING.

In the current logic, a thread would receive signal 0 (no-signal) and exit
from issignal(), returning to userland and never finishing the process of
stopping all LWPs. This causes hangs waitpid() waiting for SIGCHLD and
the callout polling for the state of the process in an infinite loop.

Instead of prompting for a returned signal from a debugger, repeat the
issignal() loop, this will cause checking the PS_STOPPING flag again and
sigswitching away in the scenario of stopping the process.


To generate a diff of this commit:
cvs rdiff -u -r1.364.2.5 -r1.364.2.6 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.364.2.5 src/sys/kern/kern_sig.c:1.364.2.6
--- src/sys/kern/kern_sig.c:1.364.2.5	Tue Oct 15 19:25:11 2019
+++ src/sys/kern/kern_sig.c	Tue Oct 15 19:27:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.364.2.5 2019/10/15 19:25:11 martin Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.364.2.6 2019/10/15 19:27:04 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.5 2019/10/15 19:25:11 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.6 2019/10/15 19:27:04 martin Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -1863,7 +1863,7 @@ issignal(struct lwp *l)
 		if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
 			sigswitch_unlock_and_switch_away(l);
 			mutex_enter(p->p_lock);
-			signo = sigchecktrace();
+			continue;
 		} else if (p->p_stat == SACTIVE)
 			signo = sigchecktrace();
 		else



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:25:12 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #327):

sys/kern/kern_sig.c: revision 1.370

Add sigswitch_unlock_and_switch_away(), extracted from sigswitch()

Use sigswitch_unlock_and_switch_away() whenever there is no need for
sigswitch().


To generate a diff of this commit:
cvs rdiff -u -r1.364.2.4 -r1.364.2.5 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.364.2.4 src/sys/kern/kern_sig.c:1.364.2.5
--- src/sys/kern/kern_sig.c:1.364.2.4	Tue Oct 15 19:23:09 2019
+++ src/sys/kern/kern_sig.c	Tue Oct 15 19:25:11 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.364.2.4 2019/10/15 19:23:09 martin Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.364.2.5 2019/10/15 19:25:11 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.4 2019/10/15 19:23:09 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.5 2019/10/15 19:25:11 martin Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -126,6 +126,7 @@ static int	sigpost(struct lwp *, sig_t, 
 static int	sigput(sigpend_t *, struct proc *, ksiginfo_t *);
 static int	sigunwait(struct proc *, const ksiginfo_t *);
 static void	sigswitch(int, int, bool);
+static void	sigswitch_unlock_and_switch_away(struct lwp *);
 
 static void	sigacts_poolpage_free(struct pool *, void *);
 static void	*sigacts_poolpage_alloc(struct pool *, int);
@@ -932,10 +933,11 @@ repeat:
 	 * The process is already stopping.
 	 */
 	if ((p->p_sflag & PS_STOPPING) != 0) {
-		sigswitch(0, p->p_xsig, true);
+		mutex_exit(proc_lock);
+		sigswitch_unlock_and_switch_away(l);
 		mutex_enter(proc_lock);
 		mutex_enter(p->p_lock);
-		goto repeat; /* XXX */
+		goto repeat;
 	}
 
 	mask = >l_sigmask;
@@ -1640,10 +1642,11 @@ repeat:
 	 * The process is already stopping.
 	 */
 	if ((p->p_sflag & PS_STOPPING) != 0) {
-		sigswitch(0, p->p_xsig, true);
+		mutex_exit(proc_lock);
+		sigswitch_unlock_and_switch_away(l);
 		mutex_enter(proc_lock);
 		mutex_enter(p->p_lock);
-		goto repeat; /* XXX */
+		goto repeat;
 	}
 
 	KSI_INIT_TRAP();
@@ -1690,7 +1693,6 @@ sigswitch(int ppmask, int signo, bool pr
 {
 	struct lwp *l = curlwp;
 	struct proc *p = l->l_proc;
-	int biglocks;
 
 	KASSERT(mutex_owned(p->p_lock));
 	KASSERT(l->l_stat == LSONPROC);
@@ -1750,10 +1752,26 @@ sigswitch(int ppmask, int signo, bool pr
 		mutex_exit(proc_lock);
 	}
 
-	/*
-	 * Unlock and switch away.
-	 */
+	sigswitch_unlock_and_switch_away(l);
+}
+
+/*
+ * Unlock and switch away.
+ */
+static void
+sigswitch_unlock_and_switch_away(struct lwp *l)
+{
+	struct proc *p;
+	int biglocks;
+
+	p = l->l_proc;
+
+	KASSERT(mutex_owned(p->p_lock));
 	KASSERT(!mutex_owned(proc_lock));
+
+	KASSERT(l->l_stat == LSONPROC);
+	KASSERT(p->p_nrlwps > 0);
+
 	KERNEL_UNLOCK_ALL(l, );
 	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
 		p->p_nrlwps--;
@@ -1843,7 +1861,7 @@ issignal(struct lwp *l)
 		 * we awaken, check for a signal from the debugger.
 		 */
 		if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
-			sigswitch(PS_NOCLDSTOP, 0, false);
+			sigswitch_unlock_and_switch_away(l);
 			mutex_enter(p->p_lock);
 			signo = sigchecktrace();
 		} else if (p->p_stat == SACTIVE)
@@ -2525,9 +2543,9 @@ repeat:
 	 * The process is already stopping.
 	 */
 	if ((p->p_sflag & PS_STOPPING) != 0) {
-		sigswitch(0, p->p_xsig, false);
+		sigswitch_unlock_and_switch_away(l);
 		mutex_enter(p->p_lock);
-		goto repeat; /* XXX */
+		goto repeat;
 	}
 
 	/* Needed for ktrace */



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:25:12 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #327):

sys/kern/kern_sig.c: revision 1.370

Add sigswitch_unlock_and_switch_away(), extracted from sigswitch()

Use sigswitch_unlock_and_switch_away() whenever there is no need for
sigswitch().


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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:08:46 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #324):

sys/kern/kern_sig.c: revision 1.367

Enhance reliability of ptrace(2) in a debuggee with multiple LWPs

Stop competing between threads which one emits event signal quicker and
overwriting the signal from another thread.

This fixes missed in action signals.

NetBSD truss can now report reliably all TRAP_SCE/SCX/etc events without
reports of missed ones.
his was one of the reasons why debuggee with multiple threads misbehaved
under a debugger.

This change is v.2 of the previously reverted commit for the same fix.

This version contains recovery path that stopps triggering event SIGTRAP
for a detached debugger.


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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:08:46 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_sig.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #324):

sys/kern/kern_sig.c: revision 1.367

Enhance reliability of ptrace(2) in a debuggee with multiple LWPs

Stop competing between threads which one emits event signal quicker and
overwriting the signal from another thread.

This fixes missed in action signals.

NetBSD truss can now report reliably all TRAP_SCE/SCX/etc events without
reports of missed ones.
his was one of the reasons why debuggee with multiple threads misbehaved
under a debugger.

This change is v.2 of the previously reverted commit for the same fix.

This version contains recovery path that stopps triggering event SIGTRAP
for a detached debugger.


To generate a diff of this commit:
cvs rdiff -u -r1.364.2.2 -r1.364.2.3 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.364.2.2 src/sys/kern/kern_sig.c:1.364.2.3
--- src/sys/kern/kern_sig.c:1.364.2.2	Tue Oct 15 19:01:06 2019
+++ src/sys/kern/kern_sig.c	Tue Oct 15 19:08:46 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.364.2.2 2019/10/15 19:01:06 martin Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.364.2.3 2019/10/15 19:08:46 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.2 2019/10/15 19:01:06 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.3 2019/10/15 19:08:46 martin Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -913,6 +913,7 @@ trapsignal(struct lwp *l, ksiginfo_t *ks
 	mutex_enter(proc_lock);
 	mutex_enter(p->p_lock);
 
+repeat:
 	/*
 	 * If we are exiting, demise now.
 	 *
@@ -926,6 +927,16 @@ trapsignal(struct lwp *l, ksiginfo_t *ks
 		/* NOTREACHED */
 	}
 
+	/*
+	 * The process is already stopping.
+	 */
+	if ((p->p_sflag & PS_STOPPING) != 0) {
+		sigswitch(0, p->p_xsig, false);
+		mutex_enter(proc_lock);
+		mutex_enter(p->p_lock);
+		goto repeat; /* XXX */
+	}
+
 	mask = >l_sigmask;
 	ps = p->p_sigacts;
 	action = SIGACTION_PS(ps, signo).sa_handler;
@@ -1589,11 +1600,12 @@ eventswitch(int code, int pe_report_even
 	KASSERT((code == TRAP_CHLD) || (code == TRAP_LWP) ||
 	(code == TRAP_EXEC));
 
+repeat:
 	/*
 	 * If we are exiting, demise now.
 	 *
 	 * This avoids notifying tracer and deadlocking.
-	*/
+	 */
 	if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
 		mutex_exit(p->p_lock);
 		mutex_exit(proc_lock);
@@ -1603,6 +1615,17 @@ eventswitch(int code, int pe_report_even
 	}
 
 	/*
+	 * If we are no longer traced, abandon this event signal.
+	 *
+	 * This avoids killing a process after detaching the debugger.
+	 */
+	if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
+		mutex_exit(p->p_lock);
+		mutex_exit(proc_lock);
+		return;
+	}
+
+	/*
 	 * If there's a pending SIGKILL process it immediately.
 	 */
 	if (p->p_xsig == SIGKILL ||
@@ -1612,6 +1635,16 @@ eventswitch(int code, int pe_report_even
 		return;
 	}
 
+	/*
+	 * The process is already stopping.
+	 */
+	if ((p->p_sflag & PS_STOPPING) != 0) {
+		sigswitch(0, p->p_xsig, false);
+		mutex_enter(proc_lock);
+		mutex_enter(p->p_lock);
+		goto repeat; /* XXX */
+	}
+
 	KSI_INIT_TRAP();
 	ksi.ksi_lid = l->l_lid;
 	ksi.ksi_signo = signo;
@@ -2448,6 +2481,7 @@ proc_stoptrace(int trapno, int sysnum, c
 
 	mutex_enter(p->p_lock);
 
+repeat:
 	/*
 	 * If we are exiting, demise now.
 	 *
@@ -2469,6 +2503,25 @@ proc_stoptrace(int trapno, int sysnum, c
 		return;
 	}
 
+	/*
+	 * If we are no longer traced, abandon this event signal.
+	 *
+	 * This avoids killing a process after detaching the debugger.
+	 */
+	if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
+		mutex_exit(p->p_lock);
+		return;
+	}
+
+	/*
+	 * The process is already stopping.
+	 */
+	if ((p->p_sflag & PS_STOPPING) != 0) {
+		sigswitch(0, p->p_xsig, true);
+		mutex_enter(p->p_lock);
+		goto repeat; /* XXX */
+	}
+
 	/* Needed for ktrace */
 	ps = p->p_sigacts;
 	action = SIGACTION_PS(ps, signo).sa_handler;



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:07:14 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #323):

sys/kern/sys_ptrace_common.c: revision 1.65

Correct the same expression on both sides of |
PR sw-bug/54610 by David Binderman


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.6 -r1.58.2.7 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:07:14 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #323):

sys/kern/sys_ptrace_common.c: revision 1.65

Correct the same expression on both sides of |
PR sw-bug/54610 by David Binderman


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.6 -r1.58.2.7 src/sys/kern/sys_ptrace_common.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/sys_ptrace_common.c
diff -u src/sys/kern/sys_ptrace_common.c:1.58.2.6 src/sys/kern/sys_ptrace_common.c:1.58.2.7
--- src/sys/kern/sys_ptrace_common.c:1.58.2.6	Tue Oct 15 19:03:20 2019
+++ src/sys/kern/sys_ptrace_common.c	Tue Oct 15 19:07:14 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.6 2019/10/15 19:03:20 martin Exp $	*/
+/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.7 2019/10/15 19:07:14 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.6 2019/10/15 19:03:20 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.7 2019/10/15 19:07:14 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -1265,7 +1265,7 @@ do_ptrace(struct ptrace_methods *ptm, st
 	break;
 			}
 		} else {
-			if (lt->l_flag & (LW_WSUSPEND | LW_WSUSPEND)) {
+			if (lt->l_flag & (LW_WSUSPEND | LW_DBGSUSPEND)) {
 error = EDEADLK;
 break;
 			}



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:05:38 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_msg.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #322):

sys/kern/sysv_msg.c: revision 1.76

Avoid -LONG_MIN msgtyp in msgrcv(2) and treat it as LONG_MAX

This logic (found in Linux) avoids undefined behavior.


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.74.4.1 src/sys/kern/sysv_msg.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/sysv_msg.c
diff -u src/sys/kern/sysv_msg.c:1.74 src/sys/kern/sysv_msg.c:1.74.4.1
--- src/sys/kern/sysv_msg.c:1.74	Wed Apr 10 10:03:50 2019
+++ src/sys/kern/sysv_msg.c	Tue Oct 15 19:05:38 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysv_msg.c,v 1.74 2019/04/10 10:03:50 pgoyette Exp $	*/
+/*	$NetBSD: sysv_msg.c,v 1.74.4.1 2019/10/15 19:05:38 martin Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2006, 2007 The NetBSD Foundation, Inc.
@@ -50,7 +50,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.74 2019/04/10 10:03:50 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.74.4.1 2019/10/15 19:05:38 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_sysv.h"
@@ -1104,6 +1104,7 @@ restart:
  */
 
 if (msgtyp != msghdr->msg_type &&
+msgtyp != LONG_MIN &&
 msghdr->msg_type > -msgtyp)
 	continue;
 



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:05:38 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_msg.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #322):

sys/kern/sysv_msg.c: revision 1.76

Avoid -LONG_MIN msgtyp in msgrcv(2) and treat it as LONG_MAX

This logic (found in Linux) avoids undefined behavior.


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.74.4.1 src/sys/kern/sysv_msg.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:03:20 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #321):

sys/kern/sys_ptrace_common.c: revision 1.63

Add two KASSERTS in the ptrace(2) kernel code

Verify that we will never return empty ptrace_state for CHILD/LWP event.


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.5 -r1.58.2.6 src/sys/kern/sys_ptrace_common.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/sys_ptrace_common.c
diff -u src/sys/kern/sys_ptrace_common.c:1.58.2.5 src/sys/kern/sys_ptrace_common.c:1.58.2.6
--- src/sys/kern/sys_ptrace_common.c:1.58.2.5	Tue Oct 15 19:01:06 2019
+++ src/sys/kern/sys_ptrace_common.c	Tue Oct 15 19:03:20 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.5 2019/10/15 19:01:06 martin Exp $	*/
+/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.6 2019/10/15 19:03:20 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.5 2019/10/15 19:01:06 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.6 2019/10/15 19:03:20 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -702,6 +702,10 @@ ptrace_get_process_state(struct proc *t,
 		memset(, 0, sizeof(ps));
 	} else {
 		si = >p_sigctx.ps_info;
+
+		KASSERT(si->_reason._ptrace_state._pe_report_event > 0);
+		KASSERT(si->_reason._ptrace_state._option._pe_other_pid > 0);
+
 		ps.pe_report_event = si->_reason._ptrace_state._pe_report_event;
 
 		CTASSERT(sizeof(ps.pe_other_pid) == sizeof(ps.pe_lwp));



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:03:20 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #321):

sys/kern/sys_ptrace_common.c: revision 1.63

Add two KASSERTS in the ptrace(2) kernel code

Verify that we will never return empty ptrace_state for CHILD/LWP event.


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.5 -r1.58.2.6 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:01:06 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_exit.c kern_lwp.c kern_sig.c kern_synch.c
sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #320):

sys/kern/kern_synch.c: revision 1.324
sys/kern/kern_sig.c: revision 1.366
sys/kern/kern_exit.c: revision 1.277
sys/kern/kern_lwp.c: revision 1.204
sys/kern/sys_ptrace_common.c: revision 1.62

Separate flag for suspended by _lwp_suspend and suspended by a debugger

Once a thread was stopped with ptrace(2), userland process must not
be able to unstop it deliberately or by an accident.

This was a Windows-style behavior that makes threading tracing fragile.


To generate a diff of this commit:
cvs rdiff -u -r1.276 -r1.276.2.1 src/sys/kern/kern_exit.c
cvs rdiff -u -r1.202.2.1 -r1.202.2.2 src/sys/kern/kern_lwp.c
cvs rdiff -u -r1.364.2.1 -r1.364.2.2 src/sys/kern/kern_sig.c
cvs rdiff -u -r1.323 -r1.323.4.1 src/sys/kern/kern_synch.c
cvs rdiff -u -r1.58.2.4 -r1.58.2.5 src/sys/kern/sys_ptrace_common.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_exit.c
diff -u src/sys/kern/kern_exit.c:1.276 src/sys/kern/kern_exit.c:1.276.2.1
--- src/sys/kern/kern_exit.c:1.276	Thu Jun 13 20:20:18 2019
+++ src/sys/kern/kern_exit.c	Tue Oct 15 19:01:06 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_exit.c,v 1.276 2019/06/13 20:20:18 kamil Exp $	*/
+/*	$NetBSD: kern_exit.c,v 1.276.2.1 2019/10/15 19:01:06 martin Exp $	*/
 
 /*-
  * Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.276 2019/06/13 20:20:18 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.276.2.1 2019/10/15 19:01:06 martin Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_dtrace.h"
@@ -617,6 +617,7 @@ retry:
 		l2->l_flag |= LW_WEXIT;
 		if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
 		l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
+			l2->l_flag &= ~LW_DBGSUSPEND;
 			/* setrunnable() will release the lock. */
 			setrunnable(l2);
 			continue;

Index: src/sys/kern/kern_lwp.c
diff -u src/sys/kern/kern_lwp.c:1.202.2.1 src/sys/kern/kern_lwp.c:1.202.2.2
--- src/sys/kern/kern_lwp.c:1.202.2.1	Tue Oct 15 18:32:13 2019
+++ src/sys/kern/kern_lwp.c	Tue Oct 15 19:01:06 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lwp.c,v 1.202.2.1 2019/10/15 18:32:13 martin Exp $	*/
+/*	$NetBSD: kern_lwp.c,v 1.202.2.2 2019/10/15 19:01:06 martin Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -211,7 +211,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.202.2.1 2019/10/15 18:32:13 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.202.2.2 2019/10/15 19:01:06 martin Exp $");
 
 #include "opt_ddb.h"
 #include "opt_lockdebug.h"
@@ -408,6 +408,11 @@ lwp_suspend(struct lwp *curl, struct lwp
 		return (EDEADLK);
 	}
 
+	if ((t->l_flag & LW_DBGSUSPEND) != 0) {
+		lwp_unlock(t);
+		return 0;
+	}
+
 	error = 0;
 
 	switch (t->l_stat) {
@@ -472,7 +477,7 @@ lwp_continue(struct lwp *l)
 
 	l->l_flag &= ~LW_WSUSPEND;
 
-	if (l->l_stat != LSSUSPENDED) {
+	if (l->l_stat != LSSUSPENDED || (l->l_flag & LW_DBGSUSPEND) != 0) {
 		lwp_unlock(l);
 		return;
 	}
@@ -497,6 +502,8 @@ lwp_unstop(struct lwp *l)
 
 	lwp_lock(l);
 
+	KASSERT((l->l_flag & LW_DBGSUSPEND) == 0);
+
 	/* If not stopped, then just bail out. */
 	if (l->l_stat != LSSTOP) {
 		lwp_unlock(l);

Index: src/sys/kern/kern_sig.c
diff -u src/sys/kern/kern_sig.c:1.364.2.1 src/sys/kern/kern_sig.c:1.364.2.2
--- src/sys/kern/kern_sig.c:1.364.2.1	Tue Oct 15 18:32:13 2019
+++ src/sys/kern/kern_sig.c	Tue Oct 15 19:01:06 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.364.2.1 2019/10/15 18:32:13 martin Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.364.2.2 2019/10/15 19:01:06 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.1 2019/10/15 18:32:13 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.2 2019/10/15 19:01:06 martin Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -1108,11 +1108,20 @@ sigpost(struct lwp *l, sig_t action, int
 
 	SDT_PROBE(proc, kernel, , signal__send, l, p, sig, 0, 0);
 
+	lwp_lock(l);
+	if (__predict_false((l->l_flag & LW_DBGSUSPEND) != 0)) {
+		if ((prop & SA_KILL) != 0)
+			l->l_flag &= ~LW_DBGSUSPEND;
+		else {
+			lwp_unlock(l);
+			return 0;
+		}
+	}
+
 	/*
 	 * Have the LWP check for signals.  This ensures that even if no LWP
 	 * is found to take the signal immediately, it should be taken soon.
 	 */
-	lwp_lock(l);
 	l->l_flag |= LW_PENDSIG;
 
 	/*
@@ -2179,7 +2188,8 @@ sigexit(struct lwp *l, int signo)
 			LIST_FOREACH(t, >p_lwps, l_sibling) {
 

CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 19:01:06 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_exit.c kern_lwp.c kern_sig.c kern_synch.c
sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #320):

sys/kern/kern_synch.c: revision 1.324
sys/kern/kern_sig.c: revision 1.366
sys/kern/kern_exit.c: revision 1.277
sys/kern/kern_lwp.c: revision 1.204
sys/kern/sys_ptrace_common.c: revision 1.62

Separate flag for suspended by _lwp_suspend and suspended by a debugger

Once a thread was stopped with ptrace(2), userland process must not
be able to unstop it deliberately or by an accident.

This was a Windows-style behavior that makes threading tracing fragile.


To generate a diff of this commit:
cvs rdiff -u -r1.276 -r1.276.2.1 src/sys/kern/kern_exit.c
cvs rdiff -u -r1.202.2.1 -r1.202.2.2 src/sys/kern/kern_lwp.c
cvs rdiff -u -r1.364.2.1 -r1.364.2.2 src/sys/kern/kern_sig.c
cvs rdiff -u -r1.323 -r1.323.4.1 src/sys/kern/kern_synch.c
cvs rdiff -u -r1.58.2.4 -r1.58.2.5 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:50:44 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #319):

sys/kern/sys_ptrace_common.c: revision 1.61

Remove 2 static asserts from the kernel ptrace code
sizeof(pid) and sizeof(lwp) will unlikely ever change and the check can
confuse.

The assert has been moved to ATF t_ptrace_wait.c r.1.132.

Requested by 


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.3 -r1.58.2.4 src/sys/kern/sys_ptrace_common.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/sys_ptrace_common.c
diff -u src/sys/kern/sys_ptrace_common.c:1.58.2.3 src/sys/kern/sys_ptrace_common.c:1.58.2.4
--- src/sys/kern/sys_ptrace_common.c:1.58.2.3	Tue Oct 15 18:38:39 2019
+++ src/sys/kern/sys_ptrace_common.c	Tue Oct 15 18:50:44 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.3 2019/10/15 18:38:39 martin Exp $	*/
+/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.4 2019/10/15 18:50:44 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.3 2019/10/15 18:38:39 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.4 2019/10/15 18:50:44 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -704,12 +704,7 @@ ptrace_get_process_state(struct proc *t,
 		si = >p_sigctx.ps_info;
 		ps.pe_report_event = si->_reason._ptrace_state._pe_report_event;
 
-		CTASSERT(sizeof(ps.pe_other_pid) ==
-		sizeof(si->_reason._ptrace_state._option._pe_other_pid));
-		CTASSERT(sizeof(ps.pe_lwp) ==
-		sizeof(si->_reason._ptrace_state._option._pe_other_pid));
 		CTASSERT(sizeof(ps.pe_other_pid) == sizeof(ps.pe_lwp));
-
 		ps.pe_other_pid =
 			si->_reason._ptrace_state._option._pe_other_pid;
 	}



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:50:44 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #319):

sys/kern/sys_ptrace_common.c: revision 1.61

Remove 2 static asserts from the kernel ptrace code
sizeof(pid) and sizeof(lwp) will unlikely ever change and the check can
confuse.

The assert has been moved to ATF t_ptrace_wait.c r.1.132.

Requested by 


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.3 -r1.58.2.4 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:38:40 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #313):

sys/kern/sys_ptrace_common.c: revision 1.60

Restore the old behavior in PT_GET_PROCESS_STATE
For !child and !lwp events return zeroed struct ptrace_state.

There is code that depends on it (GDB).

Fixes PR toolchain/54590 by martin@


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.2 -r1.58.2.3 src/sys/kern/sys_ptrace_common.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/sys_ptrace_common.c
diff -u src/sys/kern/sys_ptrace_common.c:1.58.2.2 src/sys/kern/sys_ptrace_common.c:1.58.2.3
--- src/sys/kern/sys_ptrace_common.c:1.58.2.2	Tue Oct 15 18:32:13 2019
+++ src/sys/kern/sys_ptrace_common.c	Tue Oct 15 18:38:39 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.2 2019/10/15 18:32:13 martin Exp $	*/
+/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.3 2019/10/15 18:38:39 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.2 2019/10/15 18:32:13 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.3 2019/10/15 18:38:39 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -688,6 +688,7 @@ ptrace_set_event_mask(struct proc *t, vo
 static int
 ptrace_get_process_state(struct proc *t, void *addr, size_t data)
 {
+	struct _ksiginfo *si;
 	struct ptrace_state ps;
 
 	if (data != sizeof(ps)) {
@@ -698,20 +699,20 @@ ptrace_get_process_state(struct proc *t,
 	if (t->p_sigctx.ps_info._signo != SIGTRAP ||
 	(t->p_sigctx.ps_info._code != TRAP_CHLD &&
 	t->p_sigctx.ps_info._code != TRAP_LWP)) {
-		return EINVAL;
-	}
-
-	ps.pe_report_event =
-		t->p_sigctx.ps_info._reason._ptrace_state._pe_report_event;
+		memset(, 0, sizeof(ps));
+	} else {
+		si = >p_sigctx.ps_info;
+		ps.pe_report_event = si->_reason._ptrace_state._pe_report_event;
 
-	CTASSERT(sizeof(ps.pe_other_pid) ==
-	sizeof(t->p_sigctx.ps_info._reason._ptrace_state._option._pe_other_pid));
-	CTASSERT(sizeof(ps.pe_lwp) ==
-	sizeof(t->p_sigctx.ps_info._reason._ptrace_state._option._pe_other_pid));
-	CTASSERT(sizeof(ps.pe_other_pid) == sizeof(ps.pe_lwp));
+		CTASSERT(sizeof(ps.pe_other_pid) ==
+		sizeof(si->_reason._ptrace_state._option._pe_other_pid));
+		CTASSERT(sizeof(ps.pe_lwp) ==
+		sizeof(si->_reason._ptrace_state._option._pe_other_pid));
+		CTASSERT(sizeof(ps.pe_other_pid) == sizeof(ps.pe_lwp));
 
-	ps.pe_other_pid =
-		t->p_sigctx.ps_info._reason._ptrace_state._option._pe_other_pid;
+		ps.pe_other_pid =
+			si->_reason._ptrace_state._option._pe_other_pid;
+	}
 
 	DPRINTF(("%s: lwp=%d event=%#x pid=%d lwp=%d\n", __func__,
 	t->p_sigctx.ps_lwp, ps.pe_report_event,



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:38:40 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #313):

sys/kern/sys_ptrace_common.c: revision 1.60

Restore the old behavior in PT_GET_PROCESS_STATE
For !child and !lwp events return zeroed struct ptrace_state.

There is code that depends on it (GDB).

Fixes PR toolchain/54590 by martin@


To generate a diff of this commit:
cvs rdiff -u -r1.58.2.2 -r1.58.2.3 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:21:06 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #309):

sys/kern/sys_ptrace_common.c: revision 1.67

Avoid signed integer overflow for -lwp where lwp is INT_MIN


To generate a diff of this commit:
cvs rdiff -u -r1.58 -r1.58.2.1 src/sys/kern/sys_ptrace_common.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:21:06 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sys_ptrace_common.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #309):

sys/kern/sys_ptrace_common.c: revision 1.67

Avoid signed integer overflow for -lwp where lwp is INT_MIN


To generate a diff of this commit:
cvs rdiff -u -r1.58 -r1.58.2.1 src/sys/kern/sys_ptrace_common.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/sys_ptrace_common.c
diff -u src/sys/kern/sys_ptrace_common.c:1.58 src/sys/kern/sys_ptrace_common.c:1.58.2.1
--- src/sys/kern/sys_ptrace_common.c:1.58	Thu Jul 18 20:10:46 2019
+++ src/sys/kern/sys_ptrace_common.c	Tue Oct 15 18:21:06 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_ptrace_common.c,v 1.58 2019/07/18 20:10:46 kamil Exp $	*/
+/*	$NetBSD: sys_ptrace_common.c,v 1.58.2.1 2019/10/15 18:21:06 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58 2019/07/18 20:10:46 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.1 2019/10/15 18:21:06 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -1205,8 +1205,12 @@ do_ptrace(struct ptrace_methods *ptm, st
 signo = tmp;
 tmp = 0;	/* don't search for LWP */
 			}
-		} else
+		} else if (tmp == INT_MIN) {
+			error = ESRCH;
+			break;
+		} else {
 			tmp = -tmp;
+		}
 
 		if (tmp > 0) {
 			if (req == PT_DETACH) {



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:13:56 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: vnode_if.c

Log Message:
Regen (for ticket #307)


To generate a diff of this commit:
cvs rdiff -u -r1.107 -r1.107.10.1 src/sys/kern/vnode_if.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/vnode_if.c
diff -u src/sys/kern/vnode_if.c:1.107 src/sys/kern/vnode_if.c:1.107.10.1
--- src/sys/kern/vnode_if.c:1.107	Wed Jul 12 09:31:59 2017
+++ src/sys/kern/vnode_if.c	Tue Oct 15 18:13:55 2019
@@ -1,13 +1,13 @@
-/*	$NetBSD: vnode_if.c,v 1.107 2017/07/12 09:31:59 hannken Exp $	*/
+/*	$NetBSD: vnode_if.c,v 1.107.10.1 2019/10/15 18:13:55 martin Exp $	*/
 
 /*
  * Warning: DO NOT EDIT! This file is automatically generated!
  * (Modifications made here may easily be lost!)
  *
  * Created from the file:
- *	NetBSD: vnode_if.src,v 1.77 2017/07/12 09:31:07 hannken Exp
+ *	NetBSD: vnode_if.src,v 1.77.10.1 2019/10/15 18:12:25 martin Exp
  * by the script:
- *	NetBSD: vnode_if.sh,v 1.66 2017/06/04 08:03:26 hannken Exp
+ *	NetBSD: vnode_if.sh,v 1.66.10.1 2019/10/15 18:12:25 martin Exp
  */
 
 /*
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vnode_if.c,v 1.107 2017/07/12 09:31:59 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vnode_if.c,v 1.107.10.1 2019/10/15 18:13:55 martin Exp $");
 
 #include 
 #include 
@@ -49,7 +49,7 @@ __KERNEL_RCSID(0, "$NetBSD: vnode_if.c,v
 #include 
 #include 
 
-enum fst_op { FST_NO, FST_YES, FST_TRY };
+enum fst_op { FST_NO, FST_YES, FST_LAZY, FST_TRY };
 
 static inline int
 vop_pre(vnode_t *vp, struct mount **mp, bool *mpsafe, enum fst_op op)
@@ -62,7 +62,7 @@ vop_pre(vnode_t *vp, struct mount **mp, 
 		KERNEL_LOCK(1, curlwp);
 	}
 
-	if (op == FST_YES || op == FST_TRY) {
+	if (op == FST_YES || op == FST_LAZY || op == FST_TRY) {
 		for (;;) {
 			*mp = vp->v_mount;
 			if (op == FST_TRY) {
@@ -73,6 +73,8 @@ vop_pre(vnode_t *vp, struct mount **mp, 
 	}
 	return error;
 }
+			} else if (op == FST_LAZY) {
+fstrans_start_lazy(*mp);
 			} else {
 fstrans_start(*mp);
 			}
@@ -91,7 +93,7 @@ static inline void
 vop_post(vnode_t *vp, struct mount *mp, bool mpsafe, enum fst_op op)
 {
 
-	if (op == FST_YES) {
+	if (op == FST_YES || op == FST_LAZY) {
 		fstrans_done(mp);
 	}
 
@@ -1378,11 +1380,11 @@ VOP_STRATEGY(struct vnode *vp,
 	a.a_desc = VDESC(vop_strategy);
 	a.a_vp = vp;
 	a.a_bp = bp;
-	error = vop_pre(vp, , , FST_YES);
+	error = vop_pre(vp, , , FST_LAZY);
 	if (error)
 		return error;
 	error = (VCALL(vp, VOFFSET(vop_strategy), ));
-	vop_post(vp, mp, mpsafe, FST_YES);
+	vop_post(vp, mp, mpsafe, FST_LAZY);
 	return error;
 }
 



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:13:56 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: vnode_if.c

Log Message:
Regen (for ticket #307)


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

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:12:25 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: vnode_if.sh vnode_if.src

Log Message:
Pull up following revision(s) (requested by hannken in ticket #307):

sys/kern/vnode_if.sh: revision 1.67
sys/kern/vnode_if.src: revision 1.78

As VOP_STRATEGY() usually calls itself on the file system holding "/dev"
it may deadlock on suspension of this file system.

Add fstrans type LAZY and use it for VOP_STRATEGY().

Adress PR kern/53624 (dom0 freeze on domU exit) is still there


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.66.10.1 src/sys/kern/vnode_if.sh
cvs rdiff -u -r1.77 -r1.77.10.1 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.sh
diff -u src/sys/kern/vnode_if.sh:1.66 src/sys/kern/vnode_if.sh:1.66.10.1
--- src/sys/kern/vnode_if.sh:1.66	Sun Jun  4 08:03:26 2017
+++ src/sys/kern/vnode_if.sh	Tue Oct 15 18:12:25 2019
@@ -29,7 +29,7 @@ copyright="\
  * SUCH DAMAGE.
  */
 "
-SCRIPT_ID='$NetBSD: vnode_if.sh,v 1.66 2017/06/04 08:03:26 hannken Exp $'
+SCRIPT_ID='$NetBSD: vnode_if.sh,v 1.66.10.1 2019/10/15 18:12:25 martin Exp $'
 
 # Script to produce VFS front-end sugar.
 #
@@ -318,7 +318,7 @@ echo '
 
 if [ -z "${rump}" ] ; then
 	echo "
-enum fst_op { FST_NO, FST_YES, FST_TRY };
+enum fst_op { FST_NO, FST_YES, FST_LAZY, FST_TRY };
 
 static inline int
 vop_pre(vnode_t *vp, struct mount **mp, bool *mpsafe, enum fst_op op)
@@ -331,7 +331,7 @@ vop_pre(vnode_t *vp, struct mount **mp, 
 		KERNEL_LOCK(1, curlwp);
 	}
 
-	if (op == FST_YES || op == FST_TRY) {
+	if (op == FST_YES || op == FST_LAZY || op == FST_TRY) {
 		for (;;) {
 			*mp = vp->v_mount;
 			if (op == FST_TRY) {
@@ -342,6 +342,8 @@ vop_pre(vnode_t *vp, struct mount **mp, 
 	}
 	return error;
 }
+			} else if (op == FST_LAZY) {
+fstrans_start_lazy(*mp);
 			} else {
 fstrans_start(*mp);
 			}
@@ -360,7 +362,7 @@ static inline void
 vop_post(vnode_t *vp, struct mount *mp, bool mpsafe, enum fst_op op)
 {
 
-	if (op == FST_YES) {
+	if (op == FST_YES || op == FST_LAZY) {
 		fstrans_done(mp);
 	}
 

Index: src/sys/kern/vnode_if.src
diff -u src/sys/kern/vnode_if.src:1.77 src/sys/kern/vnode_if.src:1.77.10.1
--- src/sys/kern/vnode_if.src:1.77	Wed Jul 12 09:31:07 2017
+++ src/sys/kern/vnode_if.src	Tue Oct 15 18:12:25 2019
@@ -1,4 +1,4 @@
-#	$NetBSD: vnode_if.src,v 1.77 2017/07/12 09:31:07 hannken Exp $
+#	$NetBSD: vnode_if.src,v 1.77.10.1 2019/10/15 18:12:25 martin Exp $
 #
 # Copyright (c) 1992, 1993
 #	The Regents of the University of California.  All rights reserved.
@@ -436,6 +436,7 @@ vop_bmap {
 #% strategy   vp  = = =
 #
 vop_strategy {
+	FSTRANS=LAZY
 	IN struct vnode *vp;
 	IN struct buf *bp;
 };



CVS commit: [netbsd-9] src/sys/kern

2019-10-15 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Oct 15 18:12:25 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: vnode_if.sh vnode_if.src

Log Message:
Pull up following revision(s) (requested by hannken in ticket #307):

sys/kern/vnode_if.sh: revision 1.67
sys/kern/vnode_if.src: revision 1.78

As VOP_STRATEGY() usually calls itself on the file system holding "/dev"
it may deadlock on suspension of this file system.

Add fstrans type LAZY and use it for VOP_STRATEGY().

Adress PR kern/53624 (dom0 freeze on domU exit) is still there


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.66.10.1 src/sys/kern/vnode_if.sh
cvs rdiff -u -r1.77 -r1.77.10.1 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.



CVS commit: [netbsd-9] src/sys/kern

2019-10-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Thu Oct 10 17:23:45 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by chs in ticket #294):

sys/kern/sysv_shm.c: revision 1.140,1.141

revert rev 1.139 (fixing a race between shmat() and shmdt())
that approach turned out to be too complicated.

 -

simpler fix for the race between shmat() and shmdt():
change shmat() to hold shm_lock until it is completely done.


To generate a diff of this commit:
cvs rdiff -u -r1.135.2.3 -r1.135.2.4 src/sys/kern/sysv_shm.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-10-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Thu Oct 10 17:23:45 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by chs in ticket #294):

sys/kern/sysv_shm.c: revision 1.140,1.141

revert rev 1.139 (fixing a race between shmat() and shmdt())
that approach turned out to be too complicated.

 -

simpler fix for the race between shmat() and shmdt():
change shmat() to hold shm_lock until it is completely done.


To generate a diff of this commit:
cvs rdiff -u -r1.135.2.3 -r1.135.2.4 src/sys/kern/sysv_shm.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/sysv_shm.c
diff -u src/sys/kern/sysv_shm.c:1.135.2.3 src/sys/kern/sysv_shm.c:1.135.2.4
--- src/sys/kern/sysv_shm.c:1.135.2.3	Thu Oct  3 17:20:33 2019
+++ src/sys/kern/sysv_shm.c	Thu Oct 10 17:23:45 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysv_shm.c,v 1.135.2.3 2019/10/03 17:20:33 martin Exp $	*/
+/*	$NetBSD: sysv_shm.c,v 1.135.2.4 2019/10/10 17:23:45 martin Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135.2.3 2019/10/03 17:20:33 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135.2.4 2019/10/10 17:23:45 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_sysv.h"
@@ -87,7 +87,6 @@ struct shmmap_entry {
 	SLIST_ENTRY(shmmap_entry) next;
 	vaddr_t va;
 	int shmid;
-	bool busy;
 };
 
 int			shm_nused		__cacheline_aligned;
@@ -314,10 +313,8 @@ sys_shmdt(struct lwp *l, const struct sy
 	struct uvm_object *uobj;
 	struct shmid_ds *shmseg;
 	size_t size;
-	int segnum;
 
 	mutex_enter(_lock);
-restart:
 	/* In case of reallocation, we will wait for completion */
 	while (__predict_false(shm_realloc_state))
 		cv_wait(_realloc_cv, _lock);
@@ -349,18 +346,12 @@ restart:
 		}
 	}
 
-	segnum = IPCID_TO_IX(shmmap_se->shmid);
-	if (shmmap_se->busy) {
-		cv_wait(_cv[segnum], _lock);
-		goto restart;
-	}
-
 	SHMPRINTF(("shmdt: vm %p: remove %d @%lx\n",
 	p->p_vmspace, shmmap_se->shmid, shmmap_se->va));
 
 	/* Delete the entry from shm map */
 	uobj = shm_delete_mapping(shmmap_s, shmmap_se);
-	shmseg = [segnum];
+	shmseg = [IPCID_TO_IX(shmmap_se->shmid)];
 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
 	mutex_exit(_lock);
 
@@ -395,13 +386,10 @@ sys_shmat(struct lwp *l, const struct sy
 	vaddr_t attach_va;
 	vm_prot_t prot;
 	vsize_t size;
-	int segnum;
 
 	/* Allocate a new map entry and set it */
 	shmmap_se = kmem_alloc(sizeof(struct shmmap_entry), KM_SLEEP);
 	shmmap_se->shmid = SCARG(uap, shmid);
-	shmmap_se->busy = true;
-	segnum = IPCID_TO_IX(shmmap_se->shmid);
 
 	mutex_enter(_lock);
 	/* In case of reallocation, we will wait for completion */
@@ -449,44 +437,30 @@ sys_shmat(struct lwp *l, const struct sy
 
 	/*
 	 * Create a map entry, add it to the list and increase the counters.
-	 * The lock will be dropped before the mapping, disable reallocation.
 	 */
 	shmmap_s = shmmap_getprivate(p);
 	SLIST_INSERT_HEAD(_s->entries, shmmap_se, next);
 	shmmap_s->nitems++;
 	shmseg->shm_lpid = p->p_pid;
 	shmseg->shm_nattch++;
-	shm_realloc_disable++;
 
 	/*
-	 * Add a reference to the uvm object while we hold the
-	 * shm_lock.
+	 * Map the segment into the address space.
 	 */
 	uobj = shmseg->_shm_internal;
 	uao_reference(uobj);
-	mutex_exit(_lock);
-
-	/*
-	 * Drop the shm_lock to map it into the address space, and lock
-	 * the memory, if needed (XXX where does this lock memory?).
-	 */
 	error = uvm_map(>vm_map, _va, size, uobj, 0, 0,
 	UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, flags));
 	if (error)
 		goto err_detach;
 
 	/* Set the new address, and update the time */
-	mutex_enter(_lock);
 	shmmap_se->va = attach_va;
-	shmmap_se->busy = false;
 	shmseg->shm_atime = time_second;
-	shm_realloc_disable--;
 	retval[0] = attach_va;
 	SHMPRINTF(("shmat: vm %p: add %d @%lx\n",
 	p->p_vmspace, shmmap_se->shmid, attach_va));
-	cv_broadcast(_cv[segnum]);
 err:
-	cv_broadcast(_realloc_cv);
 	mutex_exit(_lock);
 	if (error && shmmap_se) {
 		kmem_free(shmmap_se, sizeof(struct shmmap_entry));
@@ -495,11 +469,7 @@ err:
 
 err_detach:
 	uao_detach(uobj);
-	mutex_enter(_lock);
 	uobj = shm_delete_mapping(shmmap_s, shmmap_se);
-	shm_realloc_disable--;
-	cv_broadcast(_cv[segnum]);
-	cv_broadcast(_realloc_cv);
 	mutex_exit(_lock);
 	if (uobj != NULL) {
 		uao_detach(uobj);



CVS commit: [netbsd-9] src/sys/kern

2019-10-03 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Thu Oct  3 17:20:33 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by chs in ticket #275):

sys/kern/sysv_shm.c: revision 1.139

in shmdt(), wait until shmat() completes before detaching.


To generate a diff of this commit:
cvs rdiff -u -r1.135.2.2 -r1.135.2.3 src/sys/kern/sysv_shm.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/sysv_shm.c
diff -u src/sys/kern/sysv_shm.c:1.135.2.2 src/sys/kern/sysv_shm.c:1.135.2.3
--- src/sys/kern/sysv_shm.c:1.135.2.2	Fri Sep 13 06:25:26 2019
+++ src/sys/kern/sysv_shm.c	Thu Oct  3 17:20:33 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysv_shm.c,v 1.135.2.2 2019/09/13 06:25:26 martin Exp $	*/
+/*	$NetBSD: sysv_shm.c,v 1.135.2.3 2019/10/03 17:20:33 martin Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135.2.2 2019/09/13 06:25:26 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135.2.3 2019/10/03 17:20:33 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_sysv.h"
@@ -87,6 +87,7 @@ struct shmmap_entry {
 	SLIST_ENTRY(shmmap_entry) next;
 	vaddr_t va;
 	int shmid;
+	bool busy;
 };
 
 int			shm_nused		__cacheline_aligned;
@@ -313,8 +314,10 @@ sys_shmdt(struct lwp *l, const struct sy
 	struct uvm_object *uobj;
 	struct shmid_ds *shmseg;
 	size_t size;
+	int segnum;
 
 	mutex_enter(_lock);
+restart:
 	/* In case of reallocation, we will wait for completion */
 	while (__predict_false(shm_realloc_state))
 		cv_wait(_realloc_cv, _lock);
@@ -346,12 +349,18 @@ sys_shmdt(struct lwp *l, const struct sy
 		}
 	}
 
+	segnum = IPCID_TO_IX(shmmap_se->shmid);
+	if (shmmap_se->busy) {
+		cv_wait(_cv[segnum], _lock);
+		goto restart;
+	}
+
 	SHMPRINTF(("shmdt: vm %p: remove %d @%lx\n",
 	p->p_vmspace, shmmap_se->shmid, shmmap_se->va));
 
 	/* Delete the entry from shm map */
 	uobj = shm_delete_mapping(shmmap_s, shmmap_se);
-	shmseg = [IPCID_TO_IX(shmmap_se->shmid)];
+	shmseg = [segnum];
 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
 	mutex_exit(_lock);
 
@@ -386,10 +395,13 @@ sys_shmat(struct lwp *l, const struct sy
 	vaddr_t attach_va;
 	vm_prot_t prot;
 	vsize_t size;
+	int segnum;
 
 	/* Allocate a new map entry and set it */
 	shmmap_se = kmem_alloc(sizeof(struct shmmap_entry), KM_SLEEP);
 	shmmap_se->shmid = SCARG(uap, shmid);
+	shmmap_se->busy = true;
+	segnum = IPCID_TO_IX(shmmap_se->shmid);
 
 	mutex_enter(_lock);
 	/* In case of reallocation, we will wait for completion */
@@ -466,11 +478,13 @@ sys_shmat(struct lwp *l, const struct sy
 	/* Set the new address, and update the time */
 	mutex_enter(_lock);
 	shmmap_se->va = attach_va;
+	shmmap_se->busy = false;
 	shmseg->shm_atime = time_second;
 	shm_realloc_disable--;
 	retval[0] = attach_va;
 	SHMPRINTF(("shmat: vm %p: add %d @%lx\n",
 	p->p_vmspace, shmmap_se->shmid, attach_va));
+	cv_broadcast(_cv[segnum]);
 err:
 	cv_broadcast(_realloc_cv);
 	mutex_exit(_lock);
@@ -484,6 +498,7 @@ err_detach:
 	mutex_enter(_lock);
 	uobj = shm_delete_mapping(shmmap_s, shmmap_se);
 	shm_realloc_disable--;
+	cv_broadcast(_cv[segnum]);
 	cv_broadcast(_realloc_cv);
 	mutex_exit(_lock);
 	if (uobj != NULL) {



CVS commit: [netbsd-9] src/sys/kern

2019-10-03 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Thu Oct  3 17:20:33 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by chs in ticket #275):

sys/kern/sysv_shm.c: revision 1.139

in shmdt(), wait until shmat() completes before detaching.


To generate a diff of this commit:
cvs rdiff -u -r1.135.2.2 -r1.135.2.3 src/sys/kern/sysv_shm.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-09-11 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Sep 11 16:36:13 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_time.c

Log Message:
Additionally pull up the following revision for ticket #192, to fix the build:

src/sys/kern/kern_time.c1.199

mark a variable __diagused to fix this problem affecting many builds:

kern/kern_time.c:1413:6: error: variable 'error' set but not used
[-Werror=unused-but-set-variable]


To generate a diff of this commit:
cvs rdiff -u -r1.197.4.1 -r1.197.4.2 src/sys/kern/kern_time.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-09-11 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Wed Sep 11 16:36:13 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_time.c

Log Message:
Additionally pull up the following revision for ticket #192, to fix the build:

src/sys/kern/kern_time.c1.199

mark a variable __diagused to fix this problem affecting many builds:

kern/kern_time.c:1413:6: error: variable 'error' set but not used
[-Werror=unused-but-set-variable]


To generate a diff of this commit:
cvs rdiff -u -r1.197.4.1 -r1.197.4.2 src/sys/kern/kern_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/kern_time.c
diff -u src/sys/kern/kern_time.c:1.197.4.1 src/sys/kern/kern_time.c:1.197.4.2
--- src/sys/kern/kern_time.c:1.197.4.1	Tue Sep 10 16:16:46 2019
+++ src/sys/kern/kern_time.c	Wed Sep 11 16:36:13 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_time.c,v 1.197.4.1 2019/09/10 16:16:46 martin Exp $	*/
+/*	$NetBSD: kern_time.c,v 1.197.4.2 2019/09/11 16:36:13 martin Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.197.4.1 2019/09/10 16:16:46 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.197.4.2 2019/09/11 16:36:13 martin Exp $");
 
 #include 
 #include 
@@ -1410,7 +1410,7 @@ static int
 itimerdecr(struct ptimer *pt, int nsec)
 {
 	struct itimerspec *itp;
-	int error;
+	int error __diagused;
 
 	KASSERT(mutex_owned(_lock));
 	KASSERT(CLOCK_VIRTUAL_P(pt->pt_type));



CVS commit: [netbsd-9] src/sys/kern

2019-09-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Sep 10 16:14:53 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #191):

sys/kern/sysv_shm.c: revision 1.136

Acquire shmseg uobj reference while we hold shm_lock.

Otherwise nothing prevents it from being detached under our feet when
we drop shm_lock.


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.135.2.1 src/sys/kern/sysv_shm.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/sysv_shm.c
diff -u src/sys/kern/sysv_shm.c:1.135 src/sys/kern/sysv_shm.c:1.135.2.1
--- src/sys/kern/sysv_shm.c:1.135	Mon Jun 10 00:35:47 2019
+++ src/sys/kern/sysv_shm.c	Tue Sep 10 16:14:53 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysv_shm.c,v 1.135 2019/06/10 00:35:47 chs Exp $	*/
+/*	$NetBSD: sysv_shm.c,v 1.135.2.1 2019/09/10 16:14:53 martin Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135 2019/06/10 00:35:47 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.135.2.1 2019/09/10 16:14:53 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_sysv.h"
@@ -425,14 +425,19 @@ sys_shmat(struct lwp *l, const struct sy
 	shmseg->shm_lpid = p->p_pid;
 	shmseg->shm_nattch++;
 	shm_realloc_disable++;
-	mutex_exit(_lock);
 
 	/*
-	 * Add a reference to the memory object, map it to the
-	 * address space, and lock the memory, if needed.
+	 * Add a reference to the uvm object while we hold the
+	 * shm_lock.
 	 */
 	uobj = shmseg->_shm_internal;
 	uao_reference(uobj);
+	mutex_exit(_lock);
+
+	/*
+	 * Drop the shm_lock to map it into the address space, and lock
+	 * the memory, if needed (XXX where does this lock memory?).
+	 */
 	error = uvm_map(>vm_map, _va, size, uobj, 0, 0,
 	UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, flags));
 	if (error)



CVS commit: [netbsd-9] src/sys/kern

2019-09-10 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Sep 10 16:14:53 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: sysv_shm.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #191):

sys/kern/sysv_shm.c: revision 1.136

Acquire shmseg uobj reference while we hold shm_lock.

Otherwise nothing prevents it from being detached under our feet when
we drop shm_lock.


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.135.2.1 src/sys/kern/sysv_shm.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-09-06 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Fri Sep  6 19:37:51 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_ipi.c

Log Message:
Pull up following revision(s) (requested by ryo in ticket #181):

sys/kern/subr_ipi.c: revision 1.5

Requires memory barrier before IPI ack.
Problem was seen on the aarch64 cpus.
Fixes PR/54009


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.4.4.1 src/sys/kern/subr_ipi.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_ipi.c
diff -u src/sys/kern/subr_ipi.c:1.4 src/sys/kern/subr_ipi.c:1.4.4.1
--- src/sys/kern/subr_ipi.c:1.4	Sat Apr  6 02:59:05 2019
+++ src/sys/kern/subr_ipi.c	Fri Sep  6 19:37:51 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_ipi.c,v 1.4 2019/04/06 02:59:05 thorpej Exp $	*/
+/*	$NetBSD: subr_ipi.c,v 1.4.4.1 2019/09/06 19:37:51 martin Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_ipi.c,v 1.4 2019/04/06 02:59:05 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_ipi.c,v 1.4.4.1 2019/09/06 19:37:51 martin Exp $");
 
 #include 
 #include 
@@ -331,6 +331,9 @@ ipi_msg_cpu_handler(void *arg __unused)
 		msg->func(msg->arg);
 
 		/* Ack the request. */
+#ifndef __HAVE_ATOMIC_AS_MEMBAR
+		membar_producer();
+#endif
 		atomic_dec_uint(>_pending);
 	}
 }



CVS commit: [netbsd-9] src/sys/kern

2019-09-06 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Fri Sep  6 19:37:51 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_ipi.c

Log Message:
Pull up following revision(s) (requested by ryo in ticket #181):

sys/kern/subr_ipi.c: revision 1.5

Requires memory barrier before IPI ack.
Problem was seen on the aarch64 cpus.
Fixes PR/54009


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.4.4.1 src/sys/kern/subr_ipi.c

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



CVS commit: [netbsd-9] src/sys/kern

2019-09-01 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Sep  1 10:56:00 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_pool.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #129):

sys/kern/subr_pool.c: revision 1.256
sys/kern/subr_pool.c: revision 1.257

Kernel Heap Hardening: use bitmaps on all off-page pools. This migrates 29
MI pools on amd64 from linked lists to bitmaps, which have higher security
properties.

Then, change the computation of the size of the PH pools: take into account
the bitmap area available by default in the ph_u2 union, and don't go with
[>0] if [0] already has enough space to embed a bitmap.

The pools that are migrated in this change all use bitmaps small enough to
fit in [0], therefore there is no increase in memory consumption.

 -

Revert r1.254, put back || for KASAN, some destructors like lwp_dtor()
caused false positives. Needs more work.


To generate a diff of this commit:
cvs rdiff -u -r1.252.2.1 -r1.252.2.2 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.252.2.1 src/sys/kern/subr_pool.c:1.252.2.2
--- src/sys/kern/subr_pool.c:1.252.2.1	Sun Aug 18 09:52:12 2019
+++ src/sys/kern/subr_pool.c	Sun Sep  1 10:56:00 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_pool.c,v 1.252.2.1 2019/08/18 09:52:12 martin Exp $	*/
+/*	$NetBSD: subr_pool.c,v 1.252.2.2 2019/09/01 10:56:00 martin 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.252.2.1 2019/08/18 09:52:12 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.252.2.2 2019/09/01 10:56:00 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -81,7 +81,7 @@ TAILQ_HEAD(, pool) pool_head = TAILQ_HEA
 #define	PHPOOL_MAX	8
 static struct pool phpool[PHPOOL_MAX];
 #define	PHPOOL_FREELIST_NELEM(idx) \
-	(((idx) == 0) ? 0 : BITMAP_SIZE * (1 << (idx)))
+	(((idx) == 0) ? BITMAP_MIN_SIZE : BITMAP_SIZE * (1 << (idx)))
 
 #if defined(KASAN)
 #define POOL_REDZONE
@@ -162,6 +162,7 @@ static unsigned int poolid_counter = 0;
 typedef uint32_t pool_item_bitmap_t;
 #define	BITMAP_SIZE	(CHAR_BIT * sizeof(pool_item_bitmap_t))
 #define	BITMAP_MASK	(BITMAP_SIZE - 1)
+#define	BITMAP_MIN_SIZE	(CHAR_BIT * sizeof(((struct pool_item_header *)NULL)->ph_u2))
 
 struct pool_item_header {
 	/* Page headers */
@@ -201,6 +202,9 @@ struct pool_item_header {
 
 #define PHSIZE	ALIGN(sizeof(struct pool_item_header))
 
+CTASSERT(offsetof(struct pool_item_header, ph_u2) +
+BITMAP_MIN_SIZE / CHAR_BIT == sizeof(struct pool_item_header));
+
 #if defined(DIAGNOSTIC) && !defined(KASAN)
 #define POOL_CHECK_MAGIC
 #endif
@@ -588,13 +592,11 @@ pool_subsystem_init(void)
 		size_t sz;
 
 		nelem = PHPOOL_FREELIST_NELEM(idx);
+		KASSERT(nelem != 0);
 		snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
 		"phpool-%d", nelem);
-		sz = sizeof(struct pool_item_header);
-		if (nelem) {
-			sz = offsetof(struct pool_item_header,
-			ph_bitmap[howmany(nelem, BITMAP_SIZE)]);
-		}
+		sz = offsetof(struct pool_item_header,
+		ph_bitmap[howmany(nelem, BITMAP_SIZE)]);
 		pool_init([idx], sz, 0, 0, 0,
 		phpool_names[idx], _allocator_meta, IPL_VM);
 	}
@@ -657,12 +659,16 @@ pool_init_is_usebmap(const struct pool *
 	}
 
 	/*
-	 * If we're on-page, and the page header can already contain a bitmap
-	 * big enough to cover all the items of the page, go with a bitmap.
+	 * If we're off-page, go with a bitmap.
 	 */
 	if (!(pp->pr_roflags & PR_PHINPAGE)) {
-		return false;
+		return true;
 	}
+
+	/*
+	 * If we're on-page, and the page header can already contain a bitmap
+	 * big enough to cover all the items of the page, go with a bitmap.
+	 */
 	bmapsize = roundup(PHSIZE, pp->pr_align) -
 	offsetof(struct pool_item_header, ph_bitmap[0]);
 	KASSERT(bmapsize % sizeof(pool_item_bitmap_t) == 0);
@@ -801,14 +807,15 @@ pool_init(struct pool *pp, size_t size, 
 	}
 
 	/*
-	 * If we're off-page and use a bitmap, choose the appropriate pool to
-	 * allocate page headers, whose size varies depending on the bitmap. If
-	 * we're just off-page, take the first pool, no extra size. If we're
-	 * on-page, nothing to do.
+	 * If we're off-page, then we're using a bitmap; choose the appropriate
+	 * pool to allocate page headers, whose size varies depending on the
+	 * bitmap. If we're on-page, nothing to do.
 	 */
-	if (!(pp->pr_roflags & PR_PHINPAGE) && (pp->pr_roflags & PR_USEBMAP)) {
+	if (!(pp->pr_roflags & PR_PHINPAGE)) {
 		int idx;
 
+		KASSERT(pp->pr_roflags & PR_USEBMAP);
+
 		for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
 		idx++) {
 			/* nothing */
@@ -823,8 +830,6 @@ pool_init(struct pool *pp, size_t size, 
 			pp->pr_wchan, pp->pr_itemsperpage);
 		}
 		pp->pr_phpool = [idx];
-	} 

CVS commit: [netbsd-9] src/sys/kern

2019-09-01 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Sep  1 10:56:00 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_pool.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #129):

sys/kern/subr_pool.c: revision 1.256
sys/kern/subr_pool.c: revision 1.257

Kernel Heap Hardening: use bitmaps on all off-page pools. This migrates 29
MI pools on amd64 from linked lists to bitmaps, which have higher security
properties.

Then, change the computation of the size of the PH pools: take into account
the bitmap area available by default in the ph_u2 union, and don't go with
[>0] if [0] already has enough space to embed a bitmap.

The pools that are migrated in this change all use bitmaps small enough to
fit in [0], therefore there is no increase in memory consumption.

 -

Revert r1.254, put back || for KASAN, some destructors like lwp_dtor()
caused false positives. Needs more work.


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

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



CVS commit: [netbsd-9] src/sys/kern

2019-08-18 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Aug 18 09:52:12 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_pool.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #81):

sys/kern/subr_pool.c: revision 1.253
sys/kern/subr_pool.c: revision 1.254
sys/kern/subr_pool.c: revision 1.255

Kernel Heap Hardening: perform certain sanity checks on the pool caches
directly, to immediately detect certain bugs that would otherwise have
been detected only later on the pool layer, if the buffer ever reached
the pool layer.

 -

Replace || by && in KASAN, to increase the pool coverage.
Strictly speaking, what we want to avoid is poisoning buffers that were
referenced in a global list as part of the ctor. But, if a buffer indeed
got referenced as part of the ctor, it necessarily has to be unreferenced
in the dtor; which implies it has to have a dtor. So we want both a ctor
and a dtor, and not just one of them.

Note that POOL_QUARANTINE already implicitly provides this increased
coverage.

 -

Initialize pp->pr_redzone to false. For some reason with KUBSAN GCC does
not eliminate the unused branch in pr_item_linkedlist_put(), and this
leads to a unused uninitialized access which triggers KUBSAN messages.


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

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



CVS commit: [netbsd-9] src/sys/kern

2019-08-18 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Aug 18 09:52:12 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: subr_pool.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #81):

sys/kern/subr_pool.c: revision 1.253
sys/kern/subr_pool.c: revision 1.254
sys/kern/subr_pool.c: revision 1.255

Kernel Heap Hardening: perform certain sanity checks on the pool caches
directly, to immediately detect certain bugs that would otherwise have
been detected only later on the pool layer, if the buffer ever reached
the pool layer.

 -

Replace || by && in KASAN, to increase the pool coverage.
Strictly speaking, what we want to avoid is poisoning buffers that were
referenced in a global list as part of the ctor. But, if a buffer indeed
got referenced as part of the ctor, it necessarily has to be unreferenced
in the dtor; which implies it has to have a dtor. So we want both a ctor
and a dtor, and not just one of them.

Note that POOL_QUARANTINE already implicitly provides this increased
coverage.

 -

Initialize pp->pr_redzone to false. For some reason with KUBSAN GCC does
not eliminate the unused branch in pr_item_linkedlist_put(), and this
leads to a unused uninitialized access which triggers KUBSAN messages.


To generate a diff of this commit:
cvs rdiff -u -r1.252 -r1.252.2.1 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.252 src/sys/kern/subr_pool.c:1.252.2.1
--- src/sys/kern/subr_pool.c:1.252	Sat Jun 29 11:13:23 2019
+++ src/sys/kern/subr_pool.c	Sun Aug 18 09:52:12 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_pool.c,v 1.252 2019/06/29 11:13:23 maxv Exp $	*/
+/*	$NetBSD: subr_pool.c,v 1.252.2.1 2019/08/18 09:52:12 martin 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.252 2019/06/29 11:13:23 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.252.2.1 2019/08/18 09:52:12 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -216,6 +216,8 @@ struct pool_item {
 
 #define	POOL_NEEDS_CATCHUP(pp)		\
 	((pp)->pr_nitems < (pp)->pr_minitems)
+#define	POOL_OBJ_TO_PAGE(pp, v)		\
+	(void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask)
 
 /*
  * Pool cache management.
@@ -408,6 +410,40 @@ pr_item_linkedlist_get(struct pool *pp, 
 
 /* -- */
 
+static inline void
+pr_phinpage_check(struct pool *pp, struct pool_item_header *ph, void *page,
+void *object)
+{
+	if (__predict_false((void *)ph->ph_page != page)) {
+		panic("%s: [%s] item %p not part of pool", __func__,
+		pp->pr_wchan, object);
+	}
+	if (__predict_false((char *)object < (char *)page + ph->ph_off)) {
+		panic("%s: [%s] item %p below item space", __func__,
+		pp->pr_wchan, object);
+	}
+	if (__predict_false(ph->ph_poolid != pp->pr_poolid)) {
+		panic("%s: [%s] item %p poolid %u != %u", __func__,
+		pp->pr_wchan, object, ph->ph_poolid, pp->pr_poolid);
+	}
+}
+
+static inline void
+pc_phinpage_check(pool_cache_t pc, void *object)
+{
+	struct pool_item_header *ph;
+	struct pool *pp;
+	void *page;
+
+	pp = >pc_pool;
+	page = POOL_OBJ_TO_PAGE(pp, object);
+	ph = (struct pool_item_header *)page;
+
+	pr_phinpage_check(pp, ph, page, object);
+}
+
+/* -- */
+
 static inline int
 phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
 {
@@ -456,25 +492,10 @@ pr_find_pagehead(struct pool *pp, void *
 	if ((pp->pr_roflags & PR_NOALIGN) != 0) {
 		ph = pr_find_pagehead_noalign(pp, v);
 	} else {
-		void *page =
-		(void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
-
+		void *page = POOL_OBJ_TO_PAGE(pp, v);
 		if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
 			ph = (struct pool_item_header *)page;
-			if (__predict_false((void *)ph->ph_page != page)) {
-panic("%s: [%s] item %p not part of pool",
-__func__, pp->pr_wchan, v);
-			}
-			if (__predict_false((char *)v < (char *)page +
-			ph->ph_off)) {
-panic("%s: [%s] item %p below item space",
-__func__, pp->pr_wchan, v);
-			}
-			if (__predict_false(ph->ph_poolid != pp->pr_poolid)) {
-panic("%s: [%s] item %p poolid %u != %u",
-__func__, pp->pr_wchan, v, ph->ph_poolid,
-pp->pr_poolid);
-			}
+			pr_phinpage_check(pp, ph, page, v);
 		} else {
 			tmp.ph_page = page;
 			ph = SPLAY_FIND(phtree, >pr_phtree, );
@@ -746,6 +767,7 @@ pool_init(struct pool *pp, size_t size, 
 	pp->pr_drain_hook = NULL;
 	pp->pr_drain_hook_arg = NULL;
 	pp->pr_freecheck = NULL;
+	pp->pr_redzone = false;
 	pool_redzone_init(pp, size);
 	pool_quarantine_init(pp);
 
@@ -1832,7 +1854,7 @@ pool_chk_page(struct pool *pp, const cha
 	int n;
 
 	if ((pp->pr_roflags 

CVS commit: [netbsd-9] src/sys/kern

2019-08-06 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Aug  6 16:16:55 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_proc.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #20):

sys/kern/kern_proc.c: revision 1.234

Update our vm resource use for sysctl(3) call reading kinfo_proc*

Without this change RSS properties are zeroed unless a process exits or
calls getrusage(2).


To generate a diff of this commit:
cvs rdiff -u -r1.233 -r1.233.2.1 src/sys/kern/kern_proc.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_proc.c
diff -u src/sys/kern/kern_proc.c:1.233 src/sys/kern/kern_proc.c:1.233.2.1
--- src/sys/kern/kern_proc.c:1.233	Tue Jun 11 23:18:55 2019
+++ src/sys/kern/kern_proc.c	Tue Aug  6 16:16:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_proc.c,v 1.233 2019/06/11 23:18:55 kamil Exp $	*/
+/*	$NetBSD: kern_proc.c,v 1.233.2.1 2019/08/06 16:16:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.233 2019/06/11 23:18:55 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.233.2.1 2019/08/06 16:16:55 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kstack.h"
@@ -1819,6 +1819,8 @@ sysctl_doeproc(SYSCTLFN_ARGS)
 
 		if (buflen >= elem_size &&
 		(type == KERN_PROC || elem_count > 0)) {
+			ruspace(p);	/* Update process vm resource use */
+
 			if (type == KERN_PROC) {
 fill_proc(p, >kproc.kp_proc, allowaddr);
 fill_eproc(p, >kproc.kp_eproc, zombie,



CVS commit: [netbsd-9] src/sys/kern

2019-08-06 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Tue Aug  6 16:16:55 UTC 2019

Modified Files:
src/sys/kern [netbsd-9]: kern_proc.c

Log Message:
Pull up following revision(s) (requested by kamil in ticket #20):

sys/kern/kern_proc.c: revision 1.234

Update our vm resource use for sysctl(3) call reading kinfo_proc*

Without this change RSS properties are zeroed unless a process exits or
calls getrusage(2).


To generate a diff of this commit:
cvs rdiff -u -r1.233 -r1.233.2.1 src/sys/kern/kern_proc.c

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