CVS commit: src/libexec/httpd

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Mar 14 05:58:36 UTC 2022

Modified Files:
src/libexec/httpd: bozohttpd.c

Log Message:
in bozo_init_prefs(), default to returning 1 (success) and if a
bozo_set_pref() fails, return 0 instead.  fixes PR#54785 but with
a different patch.


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 src/libexec/httpd/bozohttpd.c

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



CVS commit: src/libexec/httpd

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Mar 14 05:58:36 UTC 2022

Modified Files:
src/libexec/httpd: bozohttpd.c

Log Message:
in bozo_init_prefs(), default to returning 1 (success) and if a
bozo_set_pref() fails, return 0 instead.  fixes PR#54785 but with
a different patch.


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 src/libexec/httpd/bozohttpd.c

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

Modified files:

Index: src/libexec/httpd/bozohttpd.c
diff -u src/libexec/httpd/bozohttpd.c:1.138 src/libexec/httpd/bozohttpd.c:1.139
--- src/libexec/httpd/bozohttpd.c:1.138	Tue Jan  4 06:08:14 2022
+++ src/libexec/httpd/bozohttpd.c	Mon Mar 14 05:58:36 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: bozohttpd.c,v 1.138 2022/01/04 06:08:14 kim Exp $	*/
+/*	$NetBSD: bozohttpd.c,v 1.139 2022/03/14 05:58:36 mrg Exp $	*/
 
 /*	$eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $	*/
 
@@ -2520,26 +2520,26 @@ bozo_init_httpd(bozohttpd_t *httpd)
 int
 bozo_init_prefs(bozohttpd_t *httpd, bozoprefs_t *prefs)
 {
-	int rv = 0;
+	int rv = 1;
 
 	/* make sure everything is clean */
 	(void) memset(prefs, 0x0, sizeof(*prefs));
 
 	/* set up default values */
 	if (!bozo_set_pref(httpd, prefs, "server software", SERVER_SOFTWARE))
-		rv = 1;
+		rv = 0;
 	if (!bozo_set_pref(httpd, prefs, "index.html", INDEX_HTML))
-		rv = 1;
+		rv = 0;
 	if (!bozo_set_pref(httpd, prefs, "public_html", PUBLIC_HTML))
-		rv = 1;
+		rv = 0;
 	if (!bozo_set_pref(httpd, prefs, "ssl timeout", SSL_TIMEOUT))
-		rv = 1;
+		rv = 0;
 	if (!bozo_set_pref(httpd, prefs, "initial timeout", INITIAL_TIMEOUT))
-		rv = 1;
+		rv = 0;
 	if (!bozo_set_pref(httpd, prefs, "header timeout", HEADER_WAIT_TIME))
-		rv = 1;
+		rv = 0;
 	if (!bozo_set_pref(httpd, prefs, "request timeout", TOTAL_MAX_REQ_TIME))
-		rv = 1;
+		rv = 0;
 
 	return rv;
 }



CVS commit: src/external/gpl2/gmake/dist/doc

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Mar 14 05:50:08 UTC 2022

Modified Files:
src/external/gpl2/gmake/dist/doc: Makefile.in

Log Message:
apply some NetBSD_Disabled_make.info: love to avoid a source tree
timestamp issue with read-only sources.  fixes PR#42497.


To generate a diff of this commit:
cvs rdiff -u -r1.1.1.1 -r1.2 src/external/gpl2/gmake/dist/doc/Makefile.in

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

Modified files:

Index: src/external/gpl2/gmake/dist/doc/Makefile.in
diff -u src/external/gpl2/gmake/dist/doc/Makefile.in:1.1.1.1 src/external/gpl2/gmake/dist/doc/Makefile.in:1.2
--- src/external/gpl2/gmake/dist/doc/Makefile.in:1.1.1.1	Mon Aug 18 06:47:41 2014
+++ src/external/gpl2/gmake/dist/doc/Makefile.in	Mon Mar 14 05:50:08 2022
@@ -281,6 +281,8 @@ $(ACLOCAL_M4):  $(am__aclocal_m4_deps)
 	  exit 1; \
 	fi
 $(srcdir)/make.info: make.texi $(srcdir)/version.texi $(make_TEXINFOS)
+	@echo "NOT REBUILDING $@"
+NetBSD_DISABLED_make.info:
 make.dvi: make.texi $(srcdir)/version.texi $(make_TEXINFOS)
 make.pdf: make.texi $(srcdir)/version.texi $(make_TEXINFOS)
 make.html: make.texi $(srcdir)/version.texi $(make_TEXINFOS)



CVS commit: src/external/gpl2/gmake/dist/doc

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Mar 14 05:50:08 UTC 2022

Modified Files:
src/external/gpl2/gmake/dist/doc: Makefile.in

Log Message:
apply some NetBSD_Disabled_make.info: love to avoid a source tree
timestamp issue with read-only sources.  fixes PR#42497.


To generate a diff of this commit:
cvs rdiff -u -r1.1.1.1 -r1.2 src/external/gpl2/gmake/dist/doc/Makefile.in

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



CVS commit: src/libexec/httpd

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Mar 14 05:06:59 UTC 2022

Modified Files:
src/libexec/httpd: dir-index-bozo.c

Log Message:
check for scandir() returning -1.  should handle PR#56358.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/libexec/httpd/dir-index-bozo.c

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

Modified files:

Index: src/libexec/httpd/dir-index-bozo.c
diff -u src/libexec/httpd/dir-index-bozo.c:1.34 src/libexec/httpd/dir-index-bozo.c:1.35
--- src/libexec/httpd/dir-index-bozo.c:1.34	Thu Oct 15 02:19:23 2020
+++ src/libexec/httpd/dir-index-bozo.c	Mon Mar 14 05:06:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir-index-bozo.c,v 1.34 2020/10/15 02:19:23 mrg Exp $	*/
+/*	$NetBSD: dir-index-bozo.c,v 1.35 2022/03/14 05:06:59 mrg Exp $	*/
 
 /*	$eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $	*/
 
@@ -141,7 +141,7 @@ bozo_dir_index(bozo_httpreq_t *request, 
 		"\r\n");
 
 	for (j = k = scandir(dirpath, , NULL, alphasort), deo = de;
-	j--; de++) {
+	j-- > 0; de++) {
 		int nostat = 0;
 		char *name = (*de)->d_name;
 		char *urlname, *htmlname;



CVS commit: src/libexec/httpd

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Mar 14 05:06:59 UTC 2022

Modified Files:
src/libexec/httpd: dir-index-bozo.c

Log Message:
check for scandir() returning -1.  should handle PR#56358.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/libexec/httpd/dir-index-bozo.c

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



CVS commit: src/sys/arch/mips/mips

2022-03-13 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Sun Mar 13 22:18:56 UTC 2022

Modified Files:
src/sys/arch/mips/mips: locore_mips1.S

Log Message:
s/entreed/entered/


To generate a diff of this commit:
cvs rdiff -u -r1.97 -r1.98 src/sys/arch/mips/mips/locore_mips1.S

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

Modified files:

Index: src/sys/arch/mips/mips/locore_mips1.S
diff -u src/sys/arch/mips/mips/locore_mips1.S:1.97 src/sys/arch/mips/mips/locore_mips1.S:1.98
--- src/sys/arch/mips/mips/locore_mips1.S:1.97	Sun Mar 13 17:50:55 2022
+++ src/sys/arch/mips/mips/locore_mips1.S	Sun Mar 13 22:18:56 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore_mips1.S,v 1.97 2022/03/13 17:50:55 andvar Exp $	*/
+/*	$NetBSD: locore_mips1.S,v 1.98 2022/03/13 22:18:56 andvar Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -57,7 +57,7 @@
 #include 
 #include 
 
-RCSID("$NetBSD: locore_mips1.S,v 1.97 2022/03/13 17:50:55 andvar Exp $")
+RCSID("$NetBSD: locore_mips1.S,v 1.98 2022/03/13 22:18:56 andvar Exp $")
 
 #include "assym.h"
 
@@ -1086,7 +1086,7 @@ END(MIPSX(tlb_mod_exception))
 #endif
 
 /*
- * Mark where code entreed from exception handler jumptable
+ * Mark where code entered from exception handler jumptable
  * ends, for stack traceback code.
  */
 



CVS commit: src/sys/arch/mips/mips

2022-03-13 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Sun Mar 13 22:18:56 UTC 2022

Modified Files:
src/sys/arch/mips/mips: locore_mips1.S

Log Message:
s/entreed/entered/


To generate a diff of this commit:
cvs rdiff -u -r1.97 -r1.98 src/sys/arch/mips/mips/locore_mips1.S

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:42:39 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Fix some error branches in tunwrite.


To generate a diff of this commit:
cvs rdiff -u -r1.170 -r1.171 src/sys/net/if_tun.c

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:42:39 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Fix some error branches in tunwrite.


To generate a diff of this commit:
cvs rdiff -u -r1.170 -r1.171 src/sys/net/if_tun.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/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.170 src/sys/net/if_tun.c:1.171
--- src/sys/net/if_tun.c:1.170	Sun Mar 13 21:32:43 2022
+++ src/sys/net/if_tun.c	Sun Mar 13 21:42:39 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.170 2022/03/13 21:32:43 riastradh Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.171 2022/03/13 21:42:39 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -19,7 +19,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.170 2022/03/13 21:32:43 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.171 2022/03/13 21:42:39 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -887,6 +887,8 @@ tunwrite(dev_t dev, struct uio *uio, int
 			goto out0;
 		}
 		error = uiomove((void *), sizeof(dst), uio);
+		if (error)
+			goto out0;
 		if (dst.sa_len > sizeof(dst)) {
 			/* Duh.. */
 			int n = dst.sa_len - sizeof(dst);
@@ -904,6 +906,8 @@ tunwrite(dev_t dev, struct uio *uio, int
 			goto out0;
 		}
 		error = uiomove((void *), sizeof(family), uio);
+		if (error)
+			goto out0;
 		dst.sa_family = ntohl(family);
 	} else {
 #ifdef INET
@@ -939,7 +943,8 @@ tunwrite(dev_t dev, struct uio *uio, int
 	/* get a header mbuf */
 	MGETHDR(m, M_DONTWAIT, MT_DATA);
 	if (m == NULL) {
-		return ENOBUFS;
+		error = ENOBUFS;
+		goto out0;
 	}
 	mlen = MHLEN;
 



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:43 UTC 2022

Modified Files:
src/sys/net: if_tun.c if_tun.h

Log Message:
tun(4): Omit TUN_RWAIT micro-optimization.

cv_broadcast aleady has a fast path for no-waiters.


To generate a diff of this commit:
cvs rdiff -u -r1.169 -r1.170 src/sys/net/if_tun.c
cvs rdiff -u -r1.21 -r1.22 src/sys/net/if_tun.h

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

Modified files:

Index: src/sys/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.169 src/sys/net/if_tun.c:1.170
--- src/sys/net/if_tun.c:1.169	Sun Mar 13 21:32:35 2022
+++ src/sys/net/if_tun.c	Sun Mar 13 21:32:43 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.169 2022/03/13 21:32:35 riastradh Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.170 2022/03/13 21:32:43 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -19,7 +19,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.169 2022/03/13 21:32:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.170 2022/03/13 21:32:43 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -330,10 +330,7 @@ tun_clone_destroy(struct ifnet *ifp)
 	}
 	mutex_exit(_softc_lock);
 
-	if (tp->tun_flags & TUN_RWAIT) {
-		tp->tun_flags &= ~TUN_RWAIT;
-		cv_broadcast(>tun_cv);
-	}
+	cv_broadcast(>tun_cv);
 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
 		fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
 	selnotify(>tun_rsel, 0, NOTE_SUBMIT);
@@ -654,15 +651,10 @@ tun_output(struct ifnet *ifp, struct mbu
 	}
 
 	mutex_enter(>tun_lock);
-	if (tp->tun_flags & TUN_RWAIT) {
-		tp->tun_flags &= ~TUN_RWAIT;
-		cv_broadcast(>tun_cv);
-	}
+	cv_broadcast(>tun_cv);
 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
 		softint_schedule(tp->tun_isih);
-
 	selnotify(>tun_rsel, 0, NOTE_SUBMIT);
-
 	mutex_exit(>tun_lock);
 out:
 	if (error && m0)
@@ -823,8 +815,6 @@ tunread(dev_t dev, struct uio *uio, int 
 		goto out;
 	}
 
-	tp->tun_flags &= ~TUN_RWAIT;
-
 	do {
 		IFQ_DEQUEUE(>if_snd, m0);
 		if (m0 == 0) {
@@ -832,7 +822,6 @@ tunread(dev_t dev, struct uio *uio, int 
 error = EWOULDBLOCK;
 goto out;
 			}
-			tp->tun_flags |= TUN_RWAIT;
 			if (cv_wait_sig(>tun_cv, >tun_lock)) {
 error = EINTR;
 goto out;
@@ -1024,10 +1013,7 @@ tunstart(struct ifnet *ifp)
 
 	mutex_enter(>tun_lock);
 	if (!IF_IS_EMPTY(>if_snd)) {
-		if (tp->tun_flags & TUN_RWAIT) {
-			tp->tun_flags &= ~TUN_RWAIT;
-			cv_broadcast(>tun_cv);
-		}
+		cv_broadcast(>tun_cv);
 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
 			softint_schedule(tp->tun_osih);
 

Index: src/sys/net/if_tun.h
diff -u src/sys/net/if_tun.h:1.21 src/sys/net/if_tun.h:1.22
--- src/sys/net/if_tun.h:1.21	Sun Mar 13 21:31:58 2022
+++ src/sys/net/if_tun.h	Sun Mar 13 21:32:43 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.h,v 1.21 2022/03/13 21:31:58 riastradh Exp $	*/
+/*	$NetBSD: if_tun.h,v 1.22 2022/03/13 21:32:43 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -41,7 +41,7 @@ struct tun_softc {
 #define	TUN_RCOLL	0x0004
 #define	TUN_IASET	0x0008
 #define	TUN_DSTADDR	0x0010
-#define	TUN_RWAIT	0x0040
+#define	TUN_UNUSED0	0x0040	/* was TUN_RWAIT */
 #define	TUN_ASYNC	0x0080
 #define	TUN_NBIO	0x0100
 #define	TUN_PREPADDR	0x0200



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:43 UTC 2022

Modified Files:
src/sys/net: if_tun.c if_tun.h

Log Message:
tun(4): Omit TUN_RWAIT micro-optimization.

cv_broadcast aleady has a fast path for no-waiters.


To generate a diff of this commit:
cvs rdiff -u -r1.169 -r1.170 src/sys/net/if_tun.c
cvs rdiff -u -r1.21 -r1.22 src/sys/net/if_tun.h

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:35 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Deliver SIGIO for hangup under tun_lock.

Otherwise, tp->tun_pgid is not stable.


To generate a diff of this commit:
cvs rdiff -u -r1.168 -r1.169 src/sys/net/if_tun.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/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.168 src/sys/net/if_tun.c:1.169
--- src/sys/net/if_tun.c:1.168	Sun Mar 13 21:32:27 2022
+++ src/sys/net/if_tun.c	Sun Mar 13 21:32:35 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.168 2022/03/13 21:32:27 riastradh Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.169 2022/03/13 21:32:35 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -19,7 +19,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.168 2022/03/13 21:32:27 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.169 2022/03/13 21:32:35 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -334,12 +334,10 @@ tun_clone_destroy(struct ifnet *ifp)
 		tp->tun_flags &= ~TUN_RWAIT;
 		cv_broadcast(>tun_cv);
 	}
-	selnotify(>tun_rsel, 0, NOTE_SUBMIT);
-
-	mutex_exit(>tun_lock);
-
 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
 		fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
+	selnotify(>tun_rsel, 0, NOTE_SUBMIT);
+	mutex_exit(>tun_lock);
 
 	bpf_detach(ifp);
 	if_detach(ifp);



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:35 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Deliver SIGIO for hangup under tun_lock.

Otherwise, tp->tun_pgid is not stable.


To generate a diff of this commit:
cvs rdiff -u -r1.168 -r1.169 src/sys/net/if_tun.c

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:27 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Reduce lock from IPL_NET to IPL_SOFTNET.

This is never taken from hardware interrupt handlers any more, as far
as I can tell -- only SOFTINT_NET soft interrupt handlers.

This avoids trying to take an adaptive lock, proc_lock, in fownsignal
while holding a spin lock.  Unfortunately, it doesn't entirely fix the
problem -- proc_lock is at IPL_NONE, and is held across some not
entirely trivial computations like allocating a new pid table.  So it
would really be better if we had some way to deliver SIGIO without
taking proc_lock.

Reported-by: syzbot+3dd54993d3e92e697...@syzkaller.appspotmail.com
Reported-by: syzbot+aca29415f2f0bf23f...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.167 -r1.168 src/sys/net/if_tun.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/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.167 src/sys/net/if_tun.c:1.168
--- src/sys/net/if_tun.c:1.167	Sun Mar 13 21:32:15 2022
+++ src/sys/net/if_tun.c	Sun Mar 13 21:32:27 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.167 2022/03/13 21:32:15 riastradh Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.168 2022/03/13 21:32:27 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -19,7 +19,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.167 2022/03/13 21:32:15 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.168 2022/03/13 21:32:27 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -216,7 +216,7 @@ tun_init(struct tun_softc *tp, int unit)
 {
 
 	tp->tun_unit = unit;
-	mutex_init(>tun_lock, MUTEX_DEFAULT, IPL_NET);
+	mutex_init(>tun_lock, MUTEX_DEFAULT, IPL_SOFTNET);
 	cv_init(>tun_cv, "tunread");
 	selinit(>tun_rsel);
 	selinit(>tun_wsel);



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:27 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Reduce lock from IPL_NET to IPL_SOFTNET.

This is never taken from hardware interrupt handlers any more, as far
as I can tell -- only SOFTINT_NET soft interrupt handlers.

This avoids trying to take an adaptive lock, proc_lock, in fownsignal
while holding a spin lock.  Unfortunately, it doesn't entirely fix the
problem -- proc_lock is at IPL_NONE, and is held across some not
entirely trivial computations like allocating a new pid table.  So it
would really be better if we had some way to deliver SIGIO without
taking proc_lock.

Reported-by: syzbot+3dd54993d3e92e697...@syzkaller.appspotmail.com
Reported-by: syzbot+aca29415f2f0bf23f...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.167 -r1.168 src/sys/net/if_tun.c

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:15 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Reduce tun_softc_lock from IPL_NET to IPL_NONE.

This is always taken in process/thread context, never in interrupt
context, hard or soft.


To generate a diff of this commit:
cvs rdiff -u -r1.166 -r1.167 src/sys/net/if_tun.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/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.166 src/sys/net/if_tun.c:1.167
--- src/sys/net/if_tun.c:1.166	Sun Mar 13 21:32:07 2022
+++ src/sys/net/if_tun.c	Sun Mar 13 21:32:15 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.166 2022/03/13 21:32:07 riastradh Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.167 2022/03/13 21:32:15 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -19,7 +19,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.166 2022/03/13 21:32:07 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.167 2022/03/13 21:32:15 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -130,7 +130,7 @@ static void
 tuninit(void)
 {
 
-	mutex_init(_softc_lock, MUTEX_DEFAULT, IPL_NET);
+	mutex_init(_softc_lock, MUTEX_DEFAULT, IPL_NONE);
 	LIST_INIT(_softc_list);
 	LIST_INIT(_softc_list);
 	if_clone_attach(_cloner);



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:15 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Reduce tun_softc_lock from IPL_NET to IPL_NONE.

This is always taken in process/thread context, never in interrupt
context, hard or soft.


To generate a diff of this commit:
cvs rdiff -u -r1.166 -r1.167 src/sys/net/if_tun.c

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:07 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Factor out setup/teardown into separate routines.

- Reduce duplication.
- Plug softint leak on recycling tun.

(This recycling business seems kinda sketchy...)


To generate a diff of this commit:
cvs rdiff -u -r1.165 -r1.166 src/sys/net/if_tun.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/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.165 src/sys/net/if_tun.c:1.166
--- src/sys/net/if_tun.c:1.165	Sun Mar 13 21:31:47 2022
+++ src/sys/net/if_tun.c	Sun Mar 13 21:32:07 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.165 2022/03/13 21:31:47 riastradh Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.166 2022/03/13 21:32:07 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -19,7 +19,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.165 2022/03/13 21:31:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.166 2022/03/13 21:32:07 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -211,29 +211,73 @@ tun_find_zunit(int unit)
 	return tp;
 }
 
+static void
+tun_init(struct tun_softc *tp, int unit)
+{
+
+	tp->tun_unit = unit;
+	mutex_init(>tun_lock, MUTEX_DEFAULT, IPL_NET);
+	cv_init(>tun_cv, "tunread");
+	selinit(>tun_rsel);
+	selinit(>tun_wsel);
+
+	tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp);
+	tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp);
+}
+
+static void
+tun_fini(struct tun_softc *tp)
+{
+
+	softint_disestablish(tp->tun_isih);
+	softint_disestablish(tp->tun_osih);
+
+	seldestroy(>tun_wsel);
+	seldestroy(>tun_rsel);
+	mutex_destroy(>tun_lock);
+	cv_destroy(>tun_cv);
+}
+
+static struct tun_softc *
+tun_alloc(int unit)
+{
+	struct tun_softc *tp;
+
+	tp = kmem_zalloc(sizeof(*tp), KM_SLEEP);
+	tun_init(tp, unit);
+
+	return tp;
+}
+
+static void
+tun_recycle(struct tun_softc *tp)
+{
+
+	memset(>tun_if, 0, sizeof(struct ifnet)); /* XXX ??? */
+}
+
+static void
+tun_free(struct tun_softc *tp)
+{
+
+	tun_fini(tp);
+	kmem_free(tp, sizeof(*tp));
+}
+
 static int
 tun_clone_create(struct if_clone *ifc, int unit)
 {
 	struct tun_softc *tp;
 
 	if ((tp = tun_find_zunit(unit)) == NULL) {
-		tp = kmem_zalloc(sizeof(*tp), KM_SLEEP);
-
-		tp->tun_unit = unit;
-		mutex_init(>tun_lock, MUTEX_DEFAULT, IPL_NET);
-		cv_init(>tun_cv, "tunread");
-		selinit(>tun_rsel);
-		selinit(>tun_wsel);
+		tp = tun_alloc(unit);
 	} else {
-		/* Revive tunnel instance; clear ifp part */
-		(void)memset(>tun_if, 0, sizeof(struct ifnet));
+		tun_recycle(tp);
 	}
 
 	if_initname(>tun_if, ifc->ifc_name, unit);
 	tunattach0(tp);
 	tp->tun_flags |= TUN_INITED;
-	tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp);
-	tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp);
 
 	mutex_enter(_softc_lock);
 	LIST_INSERT_HEAD(_softc_list, tp, tun_list);
@@ -301,13 +345,7 @@ tun_clone_destroy(struct ifnet *ifp)
 	if_detach(ifp);
 
 	if (!zombie) {
-		seldestroy(>tun_rsel);
-		seldestroy(>tun_wsel);
-		softint_disestablish(tp->tun_osih);
-		softint_disestablish(tp->tun_isih);
-		mutex_destroy(>tun_lock);
-		cv_destroy(>tun_cv);
-		kmem_free(tp, sizeof(*tp));
+		tun_free(tp);
 	}
 
 	return 0;
@@ -367,13 +405,7 @@ tunclose(dev_t dev, int flag, int mode,
 
 	if ((tp = tun_find_zunit(minor(dev))) != NULL) {
 		/* interface was "destroyed" before the close */
-		seldestroy(>tun_rsel);
-		seldestroy(>tun_wsel);
-		softint_disestablish(tp->tun_osih);
-		softint_disestablish(tp->tun_isih);
-		mutex_destroy(>tun_lock);
-		cv_destroy(>tun_cv);
-		kmem_free(tp, sizeof(*tp));
+		tun_free(tp);
 		return 0;
 	}
 



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:32:07 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Factor out setup/teardown into separate routines.

- Reduce duplication.
- Plug softint leak on recycling tun.

(This recycling business seems kinda sketchy...)


To generate a diff of this commit:
cvs rdiff -u -r1.165 -r1.166 src/sys/net/if_tun.c

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:31:58 UTC 2022

Modified Files:
src/sys/net: if_tun.h

Log Message:
tun(4): Add missing includes in if_tun.h.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/net/if_tun.h

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

Modified files:

Index: src/sys/net/if_tun.h
diff -u src/sys/net/if_tun.h:1.20 src/sys/net/if_tun.h:1.21
--- src/sys/net/if_tun.h:1.20	Thu Jan 26 21:13:19 2017
+++ src/sys/net/if_tun.h	Sun Mar 13 21:31:58 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.h,v 1.20 2017/01/26 21:13:19 skrll Exp $	*/
+/*	$NetBSD: if_tun.h,v 1.21 2022/03/13 21:31:58 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -22,6 +22,16 @@
 #include 
 
 #ifdef _KERNEL
+
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
 struct tun_softc {
 	struct	ifnet tun_if;		/* the interface */
 



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:31:58 UTC 2022

Modified Files:
src/sys/net: if_tun.h

Log Message:
tun(4): Add missing includes in if_tun.h.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/net/if_tun.h

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



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:31:47 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Add missing cv_destroy in tunclose.


To generate a diff of this commit:
cvs rdiff -u -r1.164 -r1.165 src/sys/net/if_tun.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/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.164 src/sys/net/if_tun.c:1.165
--- src/sys/net/if_tun.c:1.164	Sun Sep 26 15:58:33 2021
+++ src/sys/net/if_tun.c	Sun Mar 13 21:31:47 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.164 2021/09/26 15:58:33 thorpej Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.165 2022/03/13 21:31:47 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions 
@@ -19,7 +19,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.164 2021/09/26 15:58:33 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.165 2022/03/13 21:31:47 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -372,6 +372,7 @@ tunclose(dev_t dev, int flag, int mode,
 		softint_disestablish(tp->tun_osih);
 		softint_disestablish(tp->tun_isih);
 		mutex_destroy(>tun_lock);
+		cv_destroy(>tun_cv);
 		kmem_free(tp, sizeof(*tp));
 		return 0;
 	}



CVS commit: src/sys/net

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 21:31:47 UTC 2022

Modified Files:
src/sys/net: if_tun.c

Log Message:
tun(4): Add missing cv_destroy in tunclose.


To generate a diff of this commit:
cvs rdiff -u -r1.164 -r1.165 src/sys/net/if_tun.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 20:44:06 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c

Log Message:
usb: Fix debug build.


To generate a diff of this commit:
cvs rdiff -u -r1.273 -r1.274 src/sys/dev/usb/usb_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/dev/usb/usb_subr.c
diff -u src/sys/dev/usb/usb_subr.c:1.273 src/sys/dev/usb/usb_subr.c:1.274
--- src/sys/dev/usb/usb_subr.c:1.273	Sun Mar 13 13:18:22 2022
+++ src/sys/dev/usb/usb_subr.c	Sun Mar 13 20:44:06 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb_subr.c,v 1.273 2022/03/13 13:18:22 riastradh Exp $	*/
+/*	$NetBSD: usb_subr.c,v 1.274 2022/03/13 20:44:06 riastradh Exp $	*/
 /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.273 2022/03/13 13:18:22 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.274 2022/03/13 20:44:06 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -561,10 +561,10 @@ usbd_fill_iface_data(struct usbd_device 
 	for (endpt = 0; endpt < nendpt; endpt++) {
 		DPRINTFN(10, "endpt=%jd", endpt, 0, 0, 0);
 		for (; end - p >= sizeof(*desc); p += desc->bLength) {
+			desc = (usb_descriptor_t *)p;
 			DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd",
 			(uintptr_t)p, (uintptr_t)end, desc->bLength,
 			desc->bDescriptorType);
-			desc = (usb_descriptor_t *)p;
 			if (desc->bLength < sizeof(*desc)) {
 printf("%s: bad descriptor: too short\n",
 __func__);



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 20:44:06 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c

Log Message:
usb: Fix debug build.


To generate a diff of this commit:
cvs rdiff -u -r1.273 -r1.274 src/sys/dev/usb/usb_subr.c

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



Re: CVS commit: src/etc

2022-03-13 Thread Brad Spencer
Greg Troxel  writes:

[snip]

>
>   I am opposed to deciding that all zfs filesystems should be treated as
>   critical (in a world where we have not abolished the notion).
>
>   We could have a discussion about why we even have the concept of
>   critical filesystems, but if so that should not be about zfs and
>   should be in a new thread on tech-userlevel.  And, I think it really
>   isn't strongly releated to this discussion.

[snip]

Ya, I suspect that the notion of precedence needs to be developed
somehow and not abuse the notion of critical too much.  That is,
filesystem mounting by type needs to happen in a particular order.  The
default would have to choose something, either making not ZFS happen
first followed by normal and usual ZFS pools, or the other way around.
I suspect that someone loses out as no default can handle every way that
ZFS may be used when other filesystem types are present.  For example,
in the cases I have, a FFS is the root filesystem along with other FFS
file systems.  Under these are ZFS normal pools, so the order I would
prefer in this case is mount FFS first and then import the pools, as the
places that the pools would mount under would not exist the other way
around.  However, I very much understand that the other way around could
exist where ZFS normal pools are imported before anything else.  I
honestly don't remember what Solaris did, but I can't see any ordering
default with NetBSD handling every case in the correct manor.  Even if
it were possible to know the root filesystem type in a simple manor like
one can know the root filesystem device and partition, I don't think
that it would be possible to guess all of the cases correctly.


-- 
Brad Spencer - b...@anduin.eldar.org - KC8VKS - http://anduin.eldar.org


CVS commit: src/sys/arch/arm/dts

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Sun Mar 13 19:32:07 UTC 2022

Removed Files:
src/sys/arch/arm/dts: rk3399-pinebook-pro.dts

Log Message:
don't disable the spi any more.  it works fine (and i'm booting
from it on one pbp directly to nvme.)


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r0 src/sys/arch/arm/dts/rk3399-pinebook-pro.dts

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



CVS commit: src/sys/arch/arm/dts

2022-03-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Sun Mar 13 19:32:07 UTC 2022

Removed Files:
src/sys/arch/arm/dts: rk3399-pinebook-pro.dts

Log Message:
don't disable the spi any more.  it works fine (and i'm booting
from it on one pbp directly to nvme.)


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r0 src/sys/arch/arm/dts/rk3399-pinebook-pro.dts

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 18:37:10 UTC 2022

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

Log Message:
kern: Use harmless, not harmful, integer truncation in physio.

Reported-by: syzbot+13abd9bc700f7ceac...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.99 -r1.100 src/sys/kern/kern_physio.c

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 18:37:10 UTC 2022

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

Log Message:
kern: Use harmless, not harmful, integer truncation in physio.

Reported-by: syzbot+13abd9bc700f7ceac...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.99 -r1.100 src/sys/kern/kern_physio.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_physio.c
diff -u src/sys/kern/kern_physio.c:1.99 src/sys/kern/kern_physio.c:1.100
--- src/sys/kern/kern_physio.c:1.99	Thu Sep 16 22:19:11 2021
+++ src/sys/kern/kern_physio.c	Sun Mar 13 18:37:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_physio.c,v 1.99 2021/09/16 22:19:11 andvar Exp $	*/
+/*	$NetBSD: kern_physio.c,v 1.100 2022/03/13 18:37:10 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1990, 1993
@@ -71,7 +71,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.99 2021/09/16 22:19:11 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.100 2022/03/13 18:37:10 riastradh Exp $");
 
 #include 
 #include 
@@ -317,7 +317,7 @@ physio(void (*strategy)(struct buf *), s
  */
 bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
 			} else {
-bp->b_bcount = iovp->iov_len;
+bp->b_bcount = MIN(INT_MAX, iovp->iov_len);
 			}
 			bp->b_data = iovp->iov_base;
 



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 17:52:46 UTC 2022

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

Log Message:
kern: Fix fencepost error in ts2timo overflow checks.

Triggered by

clock_settime({.tv_sec=0, .tv_nsec=0})
clock_nanosleep({.tv_sec=LLONG_MIN, .tv_nsec=0})

so that, by the time we enter ts2timo (after a few nanoseconds have
passed), we end up with

tsd = {.tv_sec=0, .tv_nsec=nonzero}
ts = {.tv_sec=LLONG_MIN, .tv_nsec=0}

and the subtraction ts - tsd leads to a borrow from tv_sec.

Reported-by: syzbot+14818113e9d0b45bc...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/sys/kern/subr_time.c

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 17:52:46 UTC 2022

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

Log Message:
kern: Fix fencepost error in ts2timo overflow checks.

Triggered by

clock_settime({.tv_sec=0, .tv_nsec=0})
clock_nanosleep({.tv_sec=LLONG_MIN, .tv_nsec=0})

so that, by the time we enter ts2timo (after a few nanoseconds have
passed), we end up with

tsd = {.tv_sec=0, .tv_nsec=nonzero}
ts = {.tv_sec=LLONG_MIN, .tv_nsec=0}

and the subtraction ts - tsd leads to a borrow from tv_sec.

Reported-by: syzbot+14818113e9d0b45bc...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/sys/kern/subr_time.c

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

Modified files:

Index: src/sys/kern/subr_time.c
diff -u src/sys/kern/subr_time.c:1.31 src/sys/kern/subr_time.c:1.32
--- src/sys/kern/subr_time.c:1.31	Tue Sep 21 14:55:14 2021
+++ src/sys/kern/subr_time.c	Sun Mar 13 17:52:45 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.31 2021/09/21 14:55:14 christos Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.32 2022/03/13 17:52:45 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.31 2021/09/21 14:55:14 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.32 2022/03/13 17:52:45 riastradh Exp $");
 
 #include 
 #include 
@@ -341,8 +341,13 @@ ts2timo(clockid_t clock_id, int flags, s
 	}
 
 	if ((flags & TIMER_ABSTIME) != 0) {
-		if ((tsd.tv_sec > 0 && ts->tv_sec < LLONG_MIN + tsd.tv_sec) ||
-		(tsd.tv_sec < 0 && ts->tv_sec > LLONG_MAX + tsd.tv_sec))
+		/*
+		 * Add one to the bound to account for possible carry
+		 * from tv_nsec in timespecsub.
+		 */
+		if (tsd.tv_sec > 0 && ts->tv_sec < LLONG_MIN + tsd.tv_sec + 1)
+			return EINVAL;
+		if (tsd.tv_sec < 0 && ts->tv_sec > LLONG_MAX + tsd.tv_sec - 1)
 			return EINVAL;
 		timespecsub(ts, , ts);
 	}



CVS commit: src

2022-03-13 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Sun Mar 13 17:50:55 UTC 2022

Modified Files:
src/lib/libc/sys: sa_enable.2 sa_yield.2
src/sys/arch/atari/atari: machdep.c
src/sys/arch/mips/mips: cache.c locore_mips1.S mipsX_subr.S trap.c
src/sys/arch/powerpc/powerpc: compat_13_machdep.c compat_16_machdep.c
src/sys/dev/marvell: if_mvxpevar.h mvxpbmvar.h
src/sys/netbt: l2cap_signal.c

Log Message:
s/hander/handler/ and s/hader/header/ in comments and documentation.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/lib/libc/sys/sa_enable.2 \
src/lib/libc/sys/sa_yield.2
cvs rdiff -u -r1.185 -r1.186 src/sys/arch/atari/atari/machdep.c
cvs rdiff -u -r1.68 -r1.69 src/sys/arch/mips/mips/cache.c
cvs rdiff -u -r1.96 -r1.97 src/sys/arch/mips/mips/locore_mips1.S
cvs rdiff -u -r1.113 -r1.114 src/sys/arch/mips/mips/mipsX_subr.S
cvs rdiff -u -r1.261 -r1.262 src/sys/arch/mips/mips/trap.c
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/powerpc/powerpc/compat_13_machdep.c
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/powerpc/powerpc/compat_16_machdep.c
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/marvell/if_mvxpevar.h
cvs rdiff -u -r1.3 -r1.4 src/sys/dev/marvell/mvxpbmvar.h
cvs rdiff -u -r1.20 -r1.21 src/sys/netbt/l2cap_signal.c

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

Modified files:

Index: src/lib/libc/sys/sa_enable.2
diff -u src/lib/libc/sys/sa_enable.2:1.4 src/lib/libc/sys/sa_enable.2:1.5
--- src/lib/libc/sys/sa_enable.2:1.4	Sun Mar 14 23:05:09 2004
+++ src/lib/libc/sys/sa_enable.2	Sun Mar 13 17:50:54 2022
@@ -1,4 +1,4 @@
-.\" $NetBSD: sa_enable.2,v 1.4 2004/03/14 23:05:09 yamt Exp $
+.\" $NetBSD: sa_enable.2,v 1.5 2022/03/13 17:50:54 andvar Exp $
 .\"
 .\" Copyright (c)2004 YAMAMOTO Takashi,
 .\" All rights reserved.
@@ -54,7 +54,7 @@ beforehand.
 On success,
 .Fn sa_enable
 will not return to userland in the normal way.
-It returns into the upcall hander with an
+It returns into the upcall handler with an
 .Dv SA_UPCALL_NEWPROC
 upcall.
 Otherwise, a value of \-1 is returned and
Index: src/lib/libc/sys/sa_yield.2
diff -u src/lib/libc/sys/sa_yield.2:1.4 src/lib/libc/sys/sa_yield.2:1.5
--- src/lib/libc/sys/sa_yield.2:1.4	Sun May  9 11:28:58 2004
+++ src/lib/libc/sys/sa_yield.2	Sun Mar 13 17:50:54 2022
@@ -1,4 +1,4 @@
-.\" $NetBSD: sa_yield.2,v 1.4 2004/05/09 11:28:58 yamt Exp $
+.\" $NetBSD: sa_yield.2,v 1.5 2022/03/13 17:50:54 andvar Exp $
 .\"
 .\" Copyright (c)2004 YAMAMOTO Takashi,
 .\" All rights reserved.
@@ -54,7 +54,7 @@ beforehand.
 On success,
 .Fn sa_yield
 will not return to userland in the normal way.
-It returns into an upcall hander with an upcall.
+It returns into an upcall handler with an upcall.
 Otherwise, a value of \-1 is returned and
 .Va errno
 is set to indicate the error.

Index: src/sys/arch/atari/atari/machdep.c
diff -u src/sys/arch/atari/atari/machdep.c:1.185 src/sys/arch/atari/atari/machdep.c:1.186
--- src/sys/arch/atari/atari/machdep.c:1.185	Sat Oct  9 20:00:41 2021
+++ src/sys/arch/atari/atari/machdep.c	Sun Mar 13 17:50:55 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.185 2021/10/09 20:00:41 tsutsui Exp $	*/
+/*	$NetBSD: machdep.c,v 1.186 2022/03/13 17:50:55 andvar Exp $	*/
 
 /*
  * Copyright (c) 1988 University of Utah.
@@ -39,7 +39,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.185 2021/10/09 20:00:41 tsutsui Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.186 2022/03/13 17:50:55 andvar Exp $");
 
 #include "opt_ddb.h"
 #include "opt_compat_netbsd.h"
@@ -656,7 +656,7 @@ add_sicallback(void (*function)(void *, 
 	 * happen immediately, or after returning to a safe enough level.
 	 *
 	 * XXX:
-	 * According to  and lev1intr() hander in locore.s,
+	 * According to  and lev1intr() handler in locore.s,
 	 * at least _ATARIHW_ machines (ATARITT and HADES?) seem to have
 	 * some hardware support which can initiate real hardware interrupt
 	 * at ipl 1 for software interrupt. But as per ,

Index: src/sys/arch/mips/mips/cache.c
diff -u src/sys/arch/mips/mips/cache.c:1.68 src/sys/arch/mips/mips/cache.c:1.69
--- src/sys/arch/mips/mips/cache.c:1.68	Sun Aug  2 06:51:47 2020
+++ src/sys/arch/mips/mips/cache.c	Sun Mar 13 17:50:55 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: cache.c,v 1.68 2020/08/02 06:51:47 skrll Exp $	*/
+/*	$NetBSD: cache.c,v 1.69 2022/03/13 17:50:55 andvar Exp $	*/
 
 /*
  * Copyright 2001, 2002 Wasabi Systems, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cache.c,v 1.68 2020/08/02 06:51:47 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cache.c,v 1.69 2022/03/13 17:50:55 andvar Exp $");
 
 #include "opt_cputype.h"
 #include "opt_mips_cache.h"
@@ -711,7 +711,7 @@ primary_cache_is_2way:
 		 * virtual alias on its 8KB or 16KB primary cache
 		 * which has only 1 or 2 bit valid PIdx in 4KB page.
 		 * Actual primary cache size is ignored wrt VCE
-		 * and virtual aliases are resolved by the VCE hander,
+		

CVS commit: src

2022-03-13 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Sun Mar 13 17:50:55 UTC 2022

Modified Files:
src/lib/libc/sys: sa_enable.2 sa_yield.2
src/sys/arch/atari/atari: machdep.c
src/sys/arch/mips/mips: cache.c locore_mips1.S mipsX_subr.S trap.c
src/sys/arch/powerpc/powerpc: compat_13_machdep.c compat_16_machdep.c
src/sys/dev/marvell: if_mvxpevar.h mvxpbmvar.h
src/sys/netbt: l2cap_signal.c

Log Message:
s/hander/handler/ and s/hader/header/ in comments and documentation.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/lib/libc/sys/sa_enable.2 \
src/lib/libc/sys/sa_yield.2
cvs rdiff -u -r1.185 -r1.186 src/sys/arch/atari/atari/machdep.c
cvs rdiff -u -r1.68 -r1.69 src/sys/arch/mips/mips/cache.c
cvs rdiff -u -r1.96 -r1.97 src/sys/arch/mips/mips/locore_mips1.S
cvs rdiff -u -r1.113 -r1.114 src/sys/arch/mips/mips/mipsX_subr.S
cvs rdiff -u -r1.261 -r1.262 src/sys/arch/mips/mips/trap.c
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/powerpc/powerpc/compat_13_machdep.c
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/powerpc/powerpc/compat_16_machdep.c
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/marvell/if_mvxpevar.h
cvs rdiff -u -r1.3 -r1.4 src/sys/dev/marvell/mvxpbmvar.h
cvs rdiff -u -r1.20 -r1.21 src/sys/netbt/l2cap_signal.c

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 17:21:29 UTC 2022

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

Log Message:
kern: Fix ordering of loads for pid_table and pid_tbl_mask.

This introduces a load-acquire where there was none before.  This is
a simple correctness change.  We could avoid the load-acquire, and
use only load-consume, if we used a pointer indirection for _both_
pid_table and pid_tbl_mask.  Takes a little more work, and probably
costs an additional cache line of memory traffic, but might be worth
it to avoid the load-acquire for pid lookup.

Reported-by: syzbot+c49e405d0b977aeed...@syzkaller.appspotmail.com
Reported-by: syzbot+1c88ee7086f93607c...@syzkaller.appspotmail.com
Reported-by: syzbot+da4e9ed1319b75fe2...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.264 -r1.265 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.264 src/sys/kern/kern_proc.c:1.265
--- src/sys/kern/kern_proc.c:1.264	Thu Mar 10 12:21:35 2022
+++ src/sys/kern/kern_proc.c	Sun Mar 13 17:21:29 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_proc.c,v 1.264 2022/03/10 12:21:35 riastradh Exp $	*/
+/*	$NetBSD: kern_proc.c,v 1.265 2022/03/13 17:21:29 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.264 2022/03/10 12:21:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.265 2022/03/13 17:21:29 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kstack.h"
@@ -682,6 +682,7 @@ struct lwp *
 proc_find_lwp(proc_t *p, pid_t pid)
 {
 	struct pid_table *pt;
+	unsigned pt_mask;
 	struct lwp *l = NULL;
 	uintptr_t slot;
 	int s;
@@ -689,13 +690,22 @@ proc_find_lwp(proc_t *p, pid_t pid)
 	KASSERT(mutex_owned(p->p_lock));
 
 	/*
-	 * Look in the pid_table.  This is done unlocked inside a pserialize
-	 * read section covering pid_table's memory allocation only, so take
-	 * care to read the slot atomically and only once.  This issues a
-	 * memory barrier for dependent loads on alpha.
+	 * Look in the pid_table.  This is done unlocked inside a
+	 * pserialize read section covering pid_table's memory
+	 * allocation only, so take care to read things in the correct
+	 * order:
+	 *
+	 * 1. First read the table mask -- this only ever increases, in
+	 *expand_pid_table, so a stale value is safely
+	 *conservative.
+	 *
+	 * 2. Next read the pid table -- this is always set _before_
+	 *the mask increases, so if we see a new table and stale
+	 *mask, the mask is still valid for the table.
 	 */
 	s = pserialize_read_enter();
-	pt = _load_consume(_table)[pid & pid_tbl_mask];
+	pt_mask = atomic_load_acquire(_tbl_mask);
+	pt = _load_consume(_table)[pid & pt_mask];
 	slot = atomic_load_consume(>pt_slot);
 	if (__predict_false(!PT_IS_LWP(slot))) {
 		pserialize_read_exit(s);
@@ -742,18 +752,28 @@ struct lwp *
 proc_find_lwp_unlocked(proc_t *p, pid_t pid)
 {
 	struct pid_table *pt;
+	unsigned pt_mask;
 	struct lwp *l = NULL;
 	uintptr_t slot;
 
 	KASSERT(pserialize_in_read_section());
 
 	/*
-	 * Look in the pid_table.  This is done unlocked inside a pserialize
-	 * read section covering pid_table's memory allocation only, so take
-	 * care to read the slot atomically and only once.  This issues a
-	 * memory barrier for dependent loads on alpha.
+	 * Look in the pid_table.  This is done unlocked inside a
+	 * pserialize read section covering pid_table's memory
+	 * allocation only, so take care to read things in the correct
+	 * order:
+	 *
+	 * 1. First read the table mask -- this only ever increases, in
+	 *expand_pid_table, so a stale value is safely
+	 *conservative.
+	 *
+	 * 2. Next read the pid table -- this is always set _before_
+	 *the mask increases, so if we see a new table and stale
+	 *mask, the mask is still valid for the table.
 	 */
-	pt = _load_consume(_table)[pid & pid_tbl_mask];
+	pt_mask = atomic_load_acquire(_tbl_mask);
+	pt = _load_consume(_table)[pid & pt_mask];
 	slot = atomic_load_consume(>pt_slot);
 	if (__predict_false(!PT_IS_LWP(slot))) {
 		return NULL;
@@ -1004,7 +1024,8 @@ expand_pid_table(void)
 	tsz = pt_size * sizeof(struct pid_table);
 	n_pt = pid_table;
 	atomic_store_release(_table, new_pt);
-	pid_tbl_mask = new_pt_mask;
+	KASSERT(new_pt_mask >= pid_tbl_mask);
+	atomic_store_release(_tbl_mask, new_pt_mask);
 
 	/*
 	 * pid_max starts as PID_MAX (= 3), once we have 16384
@@ -1183,6 +1204,8 @@ proc_free_pid_internal(pid_t pid, uintpt
 {
 	struct pid_table *pt;
 
+	KASSERT(mutex_owned(_lock));
+
 	pt = _table[pid & pid_tbl_mask];
 
 	KASSERT(PT_GET_TYPE(pt->pt_slot) == type);



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 17:21:29 UTC 2022

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

Log Message:
kern: Fix ordering of loads for pid_table and pid_tbl_mask.

This introduces a load-acquire where there was none before.  This is
a simple correctness change.  We could avoid the load-acquire, and
use only load-consume, if we used a pointer indirection for _both_
pid_table and pid_tbl_mask.  Takes a little more work, and probably
costs an additional cache line of memory traffic, but might be worth
it to avoid the load-acquire for pid lookup.

Reported-by: syzbot+c49e405d0b977aeed...@syzkaller.appspotmail.com
Reported-by: syzbot+1c88ee7086f93607c...@syzkaller.appspotmail.com
Reported-by: syzbot+da4e9ed1319b75fe2...@syzkaller.appspotmail.com


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



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:26:54 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: tree.c

Log Message:
lint: add braces around multi-line loop body

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.410 -r1.411 src/usr.bin/xlint/lint1/tree.c

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

Modified files:

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.410 src/usr.bin/xlint/lint1/tree.c:1.411
--- src/usr.bin/xlint/lint1/tree.c:1.410	Sun Feb 27 18:29:14 2022
+++ src/usr.bin/xlint/lint1/tree.c	Sun Mar 13 15:26:54 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.410 2022/02/27 18:29:14 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.411 2022/03/13 15:26:54 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.410 2022/02/27 18:29:14 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.411 2022/03/13 15:26:54 rillig Exp $");
 #endif
 
 #include 
@@ -491,12 +491,13 @@ build_generic_selection(const tnode_t *e
 {
 	tnode_t *default_result = NULL;
 
-	for (; sel != NULL; sel = sel->ga_prev)
+	for (; sel != NULL; sel = sel->ga_prev) {
 		if (expr != NULL &&
 		eqtype(sel->ga_arg, expr->tn_type, false, false, NULL))
 			return sel->ga_result;
 		else if (sel->ga_arg == NULL)
 			default_result = sel->ga_result;
+	}
 	return default_result;
 }
 



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:26:54 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: tree.c

Log Message:
lint: add braces around multi-line loop body

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.410 -r1.411 src/usr.bin/xlint/lint1/tree.c

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



CVS commit: src/doc

2022-03-13 Thread Takahiro Kambe
Module Name:src
Committed By:   taca
Date:   Sun Mar 13 15:23:39 UTC 2022

Modified Files:
src/doc: 3RDPARTY

Log Message:
ISC DHCP 4.4.3 released.


To generate a diff of this commit:
cvs rdiff -u -r1.1842 -r1.1843 src/doc/3RDPARTY

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

Modified files:

Index: src/doc/3RDPARTY
diff -u src/doc/3RDPARTY:1.1842 src/doc/3RDPARTY:1.1843
--- src/doc/3RDPARTY:1.1842	Sat Mar  5 08:53:58 2022
+++ src/doc/3RDPARTY	Sun Mar 13 15:23:38 2022
@@ -1,4 +1,4 @@
-#	$NetBSD: 3RDPARTY,v 1.1842 2022/03/05 08:53:58 wiz Exp $
+#	$NetBSD: 3RDPARTY,v 1.1843 2022/03/13 15:23:38 taca Exp $
 #
 # This file contains a list of the software that has been integrated into
 # NetBSD where we are not the primary maintainer.
@@ -336,7 +336,7 @@ src/lib/libc/db/db2netbsd.
 
 Package:	dhcp
 Version:	4.4.2-P1
-Current Vers:	4.4.2-P1
+Current Vers:	4.4.3
 Maintainer:	mellon
 Archive Site:	ftp://ftp.isc.org/isc/dhcp/
 Home Page:	http://www.isc.org/software/dhcp/



CVS commit: src/doc

2022-03-13 Thread Takahiro Kambe
Module Name:src
Committed By:   taca
Date:   Sun Mar 13 15:23:39 UTC 2022

Modified Files:
src/doc: 3RDPARTY

Log Message:
ISC DHCP 4.4.3 released.


To generate a diff of this commit:
cvs rdiff -u -r1.1842 -r1.1843 src/doc/3RDPARTY

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



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:20:50 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: clean up comments, explain assertion in inssym


To generate a diff of this commit:
cvs rdiff -u -r1.112 -r1.113 src/usr.bin/xlint/lint1/lex.c

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

Modified files:

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.112 src/usr.bin/xlint/lint1/lex.c:1.113
--- src/usr.bin/xlint/lint1/lex.c:1.112	Sun Mar 13 15:17:08 2022
+++ src/usr.bin/xlint/lint1/lex.c	Sun Mar 13 15:20:50 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.112 2022/03/13 15:17:08 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.113 2022/03/13 15:20:50 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.112 2022/03/13 15:17:08 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.113 2022/03/13 15:20:50 rillig Exp $");
 #endif
 
 #include 
@@ -775,9 +775,7 @@ lex_operator(int t, op_t o)
 	return t;
 }
 
-/*
- * Called if lex found a leading \'.
- */
+/* Called if lex found a leading "'". */
 int
 lex_character_constant(void)
 {
@@ -1368,17 +1366,16 @@ lex_unknown_character(int c)
 }
 
 /*
- * As noted above, the scanner does not create new symbol table entries
- * for symbols it cannot find in the symbol table. This is to avoid
- * putting undeclared symbols into the symbol table if a syntax error
- * occurs.
+ * The scanner does not create new symbol table entries for symbols it cannot
+ * find in the symbol table. This is to avoid putting undeclared symbols into
+ * the symbol table if a syntax error occurs.
  *
- * getsym() is called as soon as it is probably ok to put the symbol in the
+ * getsym is called as soon as it is probably ok to put the symbol in the
  * symbol table. It is still possible that symbols are put in the symbol
  * table that are not completely declared due to syntax errors. To avoid too
- * many problems in this case, symbols get type 'int' in getsym().
+ * many problems in this case, symbols get type 'int' in getsym.
  *
- * XXX calls to getsym() should be delayed until decl1*() is called.
+ * XXX calls to getsym should be delayed until declare_1_* is called.
  */
 sym_t *
 getsym(sbuf_t *sb)
@@ -1490,14 +1487,15 @@ rmsym(sym_t *sym)
 }
 
 /*
- * Remove a list of symbols declared at one level from the symbol
- * table.
+ * Remove all symbols from the symbol table that have the same level as the
+ * given symbol.
  */
 void
 rmsyms(sym_t *syms)
 {
 	sym_t	*sym;
 
+	/* Note the use of s_level_next instead of s_symtab_next. */
 	for (sym = syms; sym != NULL; sym = sym->s_level_next) {
 		if (sym->s_block_level != -1) {
 			debug_step("rmsyms '%s' %s '%s'",
@@ -1513,15 +1511,22 @@ rmsyms(sym_t *syms)
  * Put a symbol into the symbol table.
  */
 void
-inssym(int bl, sym_t *sym)
+inssym(int level, sym_t *sym)
 {
 
 	debug_step("inssym '%s' %s '%s'",
 	sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
 	symtab_add(sym);
-	sym->s_block_level = bl;
-	lint_assert(sym->s_symtab_next == NULL ||
-		sym->s_block_level >= sym->s_symtab_next->s_block_level);
+	sym->s_block_level = level;
+
+	/*
+	 * Placing the inner symbols to the beginning of the list ensures
+	 * that these symbols are preferred over symbols from the outer
+	 * blocks that happen to have the same name.
+	 */
+	lint_assert(sym->s_symtab_next != NULL
+	? sym->s_block_level >= sym->s_symtab_next->s_block_level
+	: true);
 }
 
 /*
@@ -1541,9 +1546,7 @@ clean_up_after_error(void)
 		level_free_all(i);
 }
 
-/*
- * Create a new symbol with the name of an existing symbol.
- */
+/* Create a new symbol with the same name as an existing symbol. */
 sym_t *
 pushdown(const sym_t *sym)
 {



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:20:50 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: clean up comments, explain assertion in inssym


To generate a diff of this commit:
cvs rdiff -u -r1.112 -r1.113 src/usr.bin/xlint/lint1/lex.c

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



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:17:08 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: use an unsigned counter for temporary variable names

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.111 -r1.112 src/usr.bin/xlint/lint1/lex.c

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

Modified files:

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.111 src/usr.bin/xlint/lint1/lex.c:1.112
--- src/usr.bin/xlint/lint1/lex.c:1.111	Sun Mar 13 15:08:41 2022
+++ src/usr.bin/xlint/lint1/lex.c	Sun Mar 13 15:17:08 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.111 2022/03/13 15:08:41 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.112 2022/03/13 15:17:08 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.111 2022/03/13 15:08:41 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.112 2022/03/13 15:17:08 rillig Exp $");
 #endif
 
 #include 
@@ -1447,21 +1447,21 @@ getsym(sbuf_t *sb)
  * the name illegal.
  */
 sym_t *
-mktempsym(type_t *t)
+mktempsym(type_t *tp)
 {
-	static int n = 0;
+	static unsigned n = 0;
 	char *s = level_zero_alloc(block_level, 64);
 	sym_t *sym = block_zero_alloc(sizeof(*sym));
 	scl_t scl;
 
-	(void)snprintf(s, 64, "%.8d_tmp", n++);
+	(void)snprintf(s, 64, "%.8u_tmp", n++);
 
 	scl = dcs->d_scl;
 	if (scl == NOSCL)
 		scl = block_level > 0 ? AUTO : EXTERN;
 
 	sym->s_name = s;
-	sym->s_type = t;
+	sym->s_type = tp;
 	sym->s_block_level = block_level;
 	sym->s_scl = scl;
 	sym->s_kind = FVFT;



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:17:08 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: use an unsigned counter for temporary variable names

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.111 -r1.112 src/usr.bin/xlint/lint1/lex.c

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



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:08:41 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: improve debug logging for the symbol table

Only print the heading for the symbol table level if there is actually a
symbol on that level.

Ensure that no symbol with block level -1 is ever in the symbol table
(see rmsym).


To generate a diff of this commit:
cvs rdiff -u -r1.110 -r1.111 src/usr.bin/xlint/lint1/lex.c

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

Modified files:

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.110 src/usr.bin/xlint/lint1/lex.c:1.111
--- src/usr.bin/xlint/lint1/lex.c:1.110	Sun Mar 13 14:49:18 2022
+++ src/usr.bin/xlint/lint1/lex.c	Sun Mar 13 15:08:41 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.110 2022/03/13 14:49:18 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.111 2022/03/13 15:08:41 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.110 2022/03/13 14:49:18 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.111 2022/03/13 15:08:41 rillig Exp $");
 #endif
 
 #include 
@@ -339,9 +339,7 @@ debug_symtab(void)
 {
 	struct syms syms = { xcalloc(64, sizeof(syms.items[0])), 0, 64 };
 
-	for (int level = 0;; level++) {
-		debug_printf("symbol table level %d\n", level);
-
+	for (int level = -1;; level++) {
 		bool more = false;
 		size_t n = sizeof(symtab) / sizeof(symtab[0]);
 
@@ -357,12 +355,17 @@ debug_symtab(void)
 			}
 		}
 
-		debug_indent_inc();
-		qsort(syms.items, syms.len, sizeof(syms.items[0]),
-		sym_by_name);
-		for (size_t i = 0; i < syms.len; i++)
-			debug_sym(syms.items[i]);
-		debug_indent_dec();
+		if (syms.len > 0) {
+			debug_printf("symbol table level %d\n", level);
+			debug_indent_inc();
+			qsort(syms.items, syms.len, sizeof(syms.items[0]),
+			sym_by_name);
+			for (size_t i = 0; i < syms.len; i++)
+debug_sym(syms.items[i]);
+			debug_indent_dec();
+
+			lint_assert(level != -1);
+		}
 
 		if (!more)
 			break;



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 15:08:41 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: improve debug logging for the symbol table

Only print the heading for the symbol table level if there is actually a
symbol on that level.

Ensure that no symbol with block level -1 is ever in the symbol table
(see rmsym).


To generate a diff of this commit:
cvs rdiff -u -r1.110 -r1.111 src/usr.bin/xlint/lint1/lex.c

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



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 14:49:18 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: fix off-by-one error in symbol table

No functional change since the error was in the "safe" direction.


To generate a diff of this commit:
cvs rdiff -u -r1.109 -r1.110 src/usr.bin/xlint/lint1/lex.c

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

Modified files:

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.109 src/usr.bin/xlint/lint1/lex.c:1.110
--- src/usr.bin/xlint/lint1/lex.c:1.109	Sun Mar 13 14:40:36 2022
+++ src/usr.bin/xlint/lint1/lex.c	Sun Mar 13 14:49:18 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.109 2022/03/13 14:40:36 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.110 2022/03/13 14:49:18 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.109 2022/03/13 14:40:36 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.110 2022/03/13 14:49:18 rillig Exp $");
 #endif
 
 #include 
@@ -326,7 +326,7 @@ struct syms {
 static void
 syms_add(struct syms *syms, const sym_t *sym)
 {
-	while (syms->len + 1 >= syms->cap) {
+	while (syms->len >= syms->cap) {
 		syms->cap *= 2;
 		syms->items = xrealloc(syms->items,
 		syms->cap * sizeof(syms->items[0]));



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 14:49:18 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: fix off-by-one error in symbol table

No functional change since the error was in the "safe" direction.


To generate a diff of this commit:
cvs rdiff -u -r1.109 -r1.110 src/usr.bin/xlint/lint1/lex.c

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



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 14:40:36 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: decl.c externs1.h lex.c

Log Message:
lint: rename function 'cleanup' to be more expressive

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.252 -r1.253 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.149 -r1.150 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.108 -r1.109 src/usr.bin/xlint/lint1/lex.c

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



CVS commit: src/usr.bin/xlint/lint1

2022-03-13 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Mar 13 14:40:36 UTC 2022

Modified Files:
src/usr.bin/xlint/lint1: decl.c externs1.h lex.c

Log Message:
lint: rename function 'cleanup' to be more expressive

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.252 -r1.253 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.149 -r1.150 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.108 -r1.109 src/usr.bin/xlint/lint1/lex.c

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

Modified files:

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.252 src/usr.bin/xlint/lint1/decl.c:1.253
--- src/usr.bin/xlint/lint1/decl.c:1.252	Tue Mar  1 00:17:12 2022
+++ src/usr.bin/xlint/lint1/decl.c	Sun Mar 13 14:40:36 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.252 2022/03/01 00:17:12 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.253 2022/03/13 14:40:36 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: decl.c,v 1.252 2022/03/01 00:17:12 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.253 2022/03/13 14:40:36 rillig Exp $");
 #endif
 
 #include 
@@ -2882,7 +2882,7 @@ global_clean_up(void)
 	while (dcs->d_next != NULL)
 		end_declaration_level();
 
-	cleanup();
+	clean_up_after_error();
 	block_level = 0;
 	mem_block_level = 0;
 

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.149 src/usr.bin/xlint/lint1/externs1.h:1.150
--- src/usr.bin/xlint/lint1/externs1.h:1.149	Tue Mar  1 00:17:12 2022
+++ src/usr.bin/xlint/lint1/externs1.h	Sun Mar 13 14:40:36 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.149 2022/03/01 00:17:12 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.150 2022/03/13 14:40:36 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -81,7 +81,7 @@ extern	void	initscan(void);
 extern	int64_t	convert_integer(int64_t, tspec_t, unsigned int);
 extern	void	clear_warn_flags(void);
 extern	sym_t	*getsym(sbuf_t *);
-extern	void	cleanup(void);
+extern	void	clean_up_after_error(void);
 extern	sym_t	*pushdown(const sym_t *);
 extern	sym_t	*mktempsym(type_t *);
 extern	void	rmsym(sym_t *);

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.108 src/usr.bin/xlint/lint1/lex.c:1.109
--- src/usr.bin/xlint/lint1/lex.c:1.108	Tue Mar  1 20:00:47 2022
+++ src/usr.bin/xlint/lint1/lex.c	Sun Mar 13 14:40:36 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.108 2022/03/01 20:00:47 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.109 2022/03/13 14:40:36 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.108 2022/03/01 20:00:47 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.109 2022/03/13 14:40:36 rillig Exp $");
 #endif
 
 #include 
@@ -1529,7 +1529,7 @@ inssym(int bl, sym_t *sym)
  * level 0.
  */
 void
-cleanup(void)
+clean_up_after_error(void)
 {
 
 	symtab_remove_locals();



CVS commit: src/distrib/miniroot

2022-03-13 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Sun Mar 13 14:20:24 UTC 2022

Modified Files:
src/distrib/miniroot: install.sub

Log Message:
fix few typos in comments and output message.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/distrib/miniroot/install.sub

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

Modified files:

Index: src/distrib/miniroot/install.sub
diff -u src/distrib/miniroot/install.sub:1.62 src/distrib/miniroot/install.sub:1.63
--- src/distrib/miniroot/install.sub:1.62	Sat Jun 19 00:54:27 2021
+++ src/distrib/miniroot/install.sub	Sun Mar 13 14:20:24 2022
@@ -1,5 +1,5 @@
 #!/bin/sh
-#	$NetBSD: install.sub,v 1.62 2021/06/19 00:54:27 tsutsui Exp $
+#	$NetBSD: install.sub,v 1.63 2022/03/13 14:20:24 andvar Exp $
 #
 # Copyright (c) 1996 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -1298,7 +1298,7 @@ get_timezone() {
 
 	#
 	# If the zoneinfo is not on the installation medium or on the
-	# installed filesystem, set TZ to GMT and return immediatly.
+	# installed filesystem, set TZ to GMT and return immediately.
 	#
 	if [ ! -e /usr/share/zoneinfo ] && [ ! -e /mnt/usr/share/zoneinfo ]; then
 		TZ=GMT
@@ -1517,7 +1517,7 @@ mount_fs()
 		# with an error condition to tell the outer
 		# later to bail.
 		if ! mount -v -t $_fstype -o async -o $_opt $_dev $_mp ; then
-			# error message displated by mount
+			# error message displayed by mount
 			exit 1
 		fi
 	done ) < $_fstab
@@ -1570,7 +1570,7 @@ unmount_fs()
 			_devs="$1 ${_devs}"
 			_mps="$2 ${_mps}"
 		done
-		echo -n "Umounting filesystems... "
+		echo -n "Unmounting filesystems... "
 		for _mp in ${_mps}; do
 			echo -n "${_mp} "
 			umount ${_mp}



CVS commit: src/distrib/miniroot

2022-03-13 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Sun Mar 13 14:20:24 UTC 2022

Modified Files:
src/distrib/miniroot: install.sub

Log Message:
fix few typos in comments and output message.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/distrib/miniroot/install.sub

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 13:52:53 UTC 2022

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

Log Message:
vfs(9): Avoid arithmetic overflow in vn_seek.

Reported-by: syzbot+b9f9a02148a40675c...@syzkaller.appspotmail.com


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

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

Modified files:

Index: src/sys/kern/vfs_vnops.c
diff -u src/sys/kern/vfs_vnops.c:1.224 src/sys/kern/vfs_vnops.c:1.225
--- src/sys/kern/vfs_vnops.c:1.224	Wed Oct 20 03:08:18 2021
+++ src/sys/kern/vfs_vnops.c	Sun Mar 13 13:52:53 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.224 2021/10/20 03:08:18 thorpej Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.225 2022/03/13 13:52:53 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.224 2021/10/20 03:08:18 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.225 2022/03/13 13:52:53 riastradh Exp $");
 
 #include "veriexec.h"
 
@@ -1117,6 +1117,8 @@ static int
 vn_seek(struct file *fp, off_t delta, int whence, off_t *newoffp,
 int flags)
 {
+	const off_t OFF_MIN = __type_min(off_t);
+	const off_t OFF_MAX = __type_max(off_t);
 	kauth_cred_t cred = fp->f_cred;
 	off_t oldoff, newoff;
 	struct vnode *vp = fp->f_vnode;
@@ -1132,13 +1134,29 @@ vn_seek(struct file *fp, off_t delta, in
 	oldoff = fp->f_offset;
 	switch (whence) {
 	case SEEK_CUR:
-		newoff = oldoff + delta; /* XXX arithmetic overflow */
+		if (delta > 0) {
+			if (oldoff > 0 && delta > OFF_MAX - oldoff) {
+newoff = OFF_MAX;
+break;
+			}
+		} else {
+			if (oldoff < 0 && delta < OFF_MIN - oldoff) {
+newoff = OFF_MIN;
+break;
+			}
+		}
+		newoff = oldoff + delta;
 		break;
 	case SEEK_END:
 		error = VOP_GETATTR(vp, , cred);
 		if (error)
 			goto out;
-		newoff = delta + vattr.va_size; /* XXX arithmetic overflow */
+		if (vattr.va_size > OFF_MAX ||
+		delta > OFF_MAX - (off_t)vattr.va_size) {
+			newoff = OFF_MAX;
+			break;
+		}
+		newoff = delta + vattr.va_size;
 		break;
 	case SEEK_SET:
 		newoff = delta;



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 13:52:53 UTC 2022

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

Log Message:
vfs(9): Avoid arithmetic overflow in vn_seek.

Reported-by: syzbot+b9f9a02148a40675c...@syzkaller.appspotmail.com


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

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 13:18:22 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c

Log Message:
usbdi(9): Fix mistake in previous change to usbd_fill_iface_data.

The previous change stopped and rejected any descriptors smaller than
an endpoint descriptor.  Restore the previous behaviour: just skip
over them (but it will now reject descriptors that are smaller than
_any_ descriptor, which is legitimately a hardware error).


To generate a diff of this commit:
cvs rdiff -u -r1.272 -r1.273 src/sys/dev/usb/usb_subr.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 13:18:22 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c

Log Message:
usbdi(9): Fix mistake in previous change to usbd_fill_iface_data.

The previous change stopped and rejected any descriptors smaller than
an endpoint descriptor.  Restore the previous behaviour: just skip
over them (but it will now reject descriptors that are smaller than
_any_ descriptor, which is legitimately a hardware error).


To generate a diff of this commit:
cvs rdiff -u -r1.272 -r1.273 src/sys/dev/usb/usb_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/dev/usb/usb_subr.c
diff -u src/sys/dev/usb/usb_subr.c:1.272 src/sys/dev/usb/usb_subr.c:1.273
--- src/sys/dev/usb/usb_subr.c:1.272	Sun Mar 13 11:30:12 2022
+++ src/sys/dev/usb/usb_subr.c	Sun Mar 13 13:18:22 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb_subr.c,v 1.272 2022/03/13 11:30:12 riastradh Exp $	*/
+/*	$NetBSD: usb_subr.c,v 1.273 2022/03/13 13:18:22 riastradh Exp $	*/
 /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.272 2022/03/13 11:30:12 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.273 2022/03/13 13:18:22 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -531,7 +531,9 @@ usbd_fill_iface_data(struct usbd_device 
 	USBHIST_CALLARGS(usbdebug, "ifaceidx=%jd altidx=%jd",
 	ifaceidx, altidx, 0, 0);
 	struct usbd_interface *ifc = >ud_ifaces[ifaceidx];
+	usb_descriptor_t *desc;
 	usb_interface_descriptor_t *idesc;
+	usb_endpoint_descriptor_t *ed;
 	struct usbd_endpoint *endpoints;
 	char *p, *end;
 	int endpt, nendpt;
@@ -556,33 +558,33 @@ usbd_fill_iface_data(struct usbd_device 
 	KASSERTMSG((char *)dev->ud_cdesc <= (char *)idesc, "cdesc=%p idesc=%p",
 	dev->ud_cdesc, idesc);
 	KASSERTMSG((char *)idesc < end, "idesc=%p end=%p", idesc, end);
-#define ed ((usb_endpoint_descriptor_t *)p)
 	for (endpt = 0; endpt < nendpt; endpt++) {
 		DPRINTFN(10, "endpt=%jd", endpt, 0, 0, 0);
-		for (; end - p >= sizeof(*ed); p += ed->bLength) {
+		for (; end - p >= sizeof(*desc); p += desc->bLength) {
 			DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd",
-			(uintptr_t)p, (uintptr_t)end, ed->bLength,
-			ed->bDescriptorType);
-			if (ed->bLength < sizeof(*ed) ||
-			ed->bLength > end - p ||
-			ed->bDescriptorType == UDESC_INTERFACE)
-break;
-			if (ed->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE &&
-			ed->bDescriptorType == UDESC_ENDPOINT)
-goto found;
-		}
-		/* passed end, or bad desc */
-		if (end - p >= sizeof(*ed)) {
-			if (ed->bLength == 0) {
-printf("%s: bad descriptor: 0 length\n",
+			(uintptr_t)p, (uintptr_t)end, desc->bLength,
+			desc->bDescriptorType);
+			desc = (usb_descriptor_t *)p;
+			if (desc->bLength < sizeof(*desc)) {
+printf("%s: bad descriptor: too short\n",
+__func__);
+goto bad;
+			} else if (desc->bLength > end - p) {
+printf("%s: bad descriptor: too long\n",
 __func__);
-			} else {
+goto bad;
+			} else if (desc->bDescriptorType == UDESC_INTERFACE) {
 printf("%s: bad descriptor: iface desc\n",
 __func__);
+goto bad;
+			}
+			if (desc->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE &&
+			desc->bDescriptorType == UDESC_ENDPOINT) {
+ed = (usb_endpoint_descriptor_t *)p;
+goto found;
 			}
-		} else {
-			printf("%s: no desc found\n", __func__);
 		}
+		printf("%s: no desc found\n", __func__);
 		goto bad;
 	found:
 		endpoints[endpt].ue_edesc = ed;



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 13:07:39 UTC 2022

Modified Files:
src/sys/dev/usb: usbdi.c

Log Message:
usbdi(9): Fix paste-o in previous change to usbd_ar_pipe.


To generate a diff of this commit:
cvs rdiff -u -r1.237 -r1.238 src/sys/dev/usb/usbdi.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/dev/usb/usbdi.c
diff -u src/sys/dev/usb/usbdi.c:1.237 src/sys/dev/usb/usbdi.c:1.238
--- src/sys/dev/usb/usbdi.c:1.237	Sun Mar 13 11:30:13 2022
+++ src/sys/dev/usb/usbdi.c	Sun Mar 13 13:07:39 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdi.c,v 1.237 2022/03/13 11:30:13 riastradh Exp $	*/
+/*	$NetBSD: usbdi.c,v 1.238 2022/03/13 13:07:39 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.237 2022/03/13 11:30:13 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.238 2022/03/13 13:07:39 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1064,7 +1064,7 @@ usbd_ar_pipe(struct usbd_pipe *pipe)
 	}
 
 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
-	KASSERTMSG(pipe->up_abortlwp == NULL, "pipe->up_abortlwp=%p",
+	KASSERTMSG(pipe->up_abortlwp == curlwp, "pipe->up_abortlwp=%p",
 	pipe->up_abortlwp);
 	pipe->up_abortlwp = NULL;
 



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 13:07:39 UTC 2022

Modified Files:
src/sys/dev/usb: usbdi.c

Log Message:
usbdi(9): Fix paste-o in previous change to usbd_ar_pipe.


To generate a diff of this commit:
cvs rdiff -u -r1.237 -r1.238 src/sys/dev/usb/usbdi.c

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:57:33 UTC 2022

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

Log Message:
ntp(9): Avoid left shift of negative.

Kinda silly that this is UB at all...

Reported-by: syzbot+baf29c7f0756293b8...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/sys/kern/kern_ntptime.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_ntptime.c
diff -u src/sys/kern/kern_ntptime.c:1.62 src/sys/kern/kern_ntptime.c:1.63
--- src/sys/kern/kern_ntptime.c:1.62	Sun Mar 13 12:30:47 2022
+++ src/sys/kern/kern_ntptime.c	Sun Mar 13 12:57:33 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ntptime.c,v 1.62 2022/03/13 12:30:47 riastradh Exp $	*/
+/*	$NetBSD: kern_ntptime.c,v 1.63 2022/03/13 12:57:33 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -60,7 +60,7 @@
 
 #include 
 /* __FBSDID("$FreeBSD: src/sys/kern/kern_ntptime.c,v 1.59 2005/05/28 14:34:41 rwatson Exp $"); */
-__KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.62 2022/03/13 12:30:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.63 2022/03/13 12:57:33 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ntp.h"
@@ -383,7 +383,10 @@ ntp_adjtime1(struct timex *ntv)
 		ntv->offset = L_GINT(time_offset);
 	else
 		ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
-	ntv->freq = L_GINT((time_freq / 1000LL) << 16);
+	if (time_freq < 0)
+		ntv->freq = L_GINT(-((-time_freq / 1000LL) << 16));
+	else
+		ntv->freq = L_GINT((time_freq / 1000LL) << 16);
 	ntv->maxerror = time_maxerror;
 	ntv->esterror = time_esterror;
 	ntv->status = time_status;



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:57:33 UTC 2022

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

Log Message:
ntp(9): Avoid left shift of negative.

Kinda silly that this is UB at all...

Reported-by: syzbot+baf29c7f0756293b8...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/sys/kern/kern_ntptime.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:49:37 UTC 2022

Modified Files:
src/sys/dev/usb: auvitek.c auvitek_audio.c auvitek_dtv.c auvitek_i2c.c
auvitek_video.c auvitekvar.h

Log Message:
auvitek(4): Fix i2c detach if attach failed.

While here, use config_detach_children.

Reported-by: syzbot+bf05898af6a53cb3b...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/dev/usb/auvitek.c
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/usb/auvitek_audio.c
cvs rdiff -u -r1.9 -r1.10 src/sys/dev/usb/auvitek_dtv.c \
src/sys/dev/usb/auvitekvar.h
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/usb/auvitek_i2c.c
cvs rdiff -u -r1.10 -r1.11 src/sys/dev/usb/auvitek_video.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/dev/usb/auvitek.c
diff -u src/sys/dev/usb/auvitek.c:1.12 src/sys/dev/usb/auvitek.c:1.13
--- src/sys/dev/usb/auvitek.c:1.12	Sat Mar 14 02:35:33 2020
+++ src/sys/dev/usb/auvitek.c	Sun Mar 13 12:49:36 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: auvitek.c,v 1.12 2020/03/14 02:35:33 christos Exp $ */
+/* $NetBSD: auvitek.c,v 1.13 2022/03/13 12:49:36 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2010 Jared D. McNeill 
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: auvitek.c,v 1.12 2020/03/14 02:35:33 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: auvitek.c,v 1.13 2022/03/13 12:49:36 riastradh Exp $");
 
 #include 
 #include 
@@ -260,9 +260,21 @@ static int
 auvitek_detach(device_t self, int flags)
 {
 	struct auvitek_softc *sc = device_private(self);
+	int error;
 
 	sc->sc_dying = 1;
 
+	error = config_detach_children(self, flags);
+	if (error) {
+		/*
+		 * XXX Should ask autoconf to block open with
+		 * .d_cfdriver until we're done, instead of setting
+		 * this and then rolling it back.
+		 */
+		sc->sc_dying = 0;
+		return error;
+	}
+
 	pmf_device_deregister(self);
 
 	auvitek_dtv_detach(sc, flags);

Index: src/sys/dev/usb/auvitek_audio.c
diff -u src/sys/dev/usb/auvitek_audio.c:1.5 src/sys/dev/usb/auvitek_audio.c:1.6
--- src/sys/dev/usb/auvitek_audio.c:1.5	Sat Aug  7 16:19:16 2021
+++ src/sys/dev/usb/auvitek_audio.c	Sun Mar 13 12:49:36 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: auvitek_audio.c,v 1.5 2021/08/07 16:19:16 thorpej Exp $ */
+/* $NetBSD: auvitek_audio.c,v 1.6 2022/03/13 12:49:36 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2010 Jared D. McNeill 
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: auvitek_audio.c,v 1.5 2021/08/07 16:19:16 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: auvitek_audio.c,v 1.6 2022/03/13 12:49:36 riastradh Exp $");
 
 #include 
 #include 
@@ -112,10 +112,6 @@ auvitek_audio_attach(struct auvitek_soft
 int
 auvitek_audio_detach(struct auvitek_softc *sc, int flags)
 {
-	if (sc->sc_audiodev != NULL) {
-		config_detach(sc->sc_audiodev, flags);
-		sc->sc_audiodev = NULL;
-	}
 
 	return 0;
 }

Index: src/sys/dev/usb/auvitek_dtv.c
diff -u src/sys/dev/usb/auvitek_dtv.c:1.9 src/sys/dev/usb/auvitek_dtv.c:1.10
--- src/sys/dev/usb/auvitek_dtv.c:1.9	Sat Aug  7 16:19:16 2021
+++ src/sys/dev/usb/auvitek_dtv.c	Sun Mar 13 12:49:36 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: auvitek_dtv.c,v 1.9 2021/08/07 16:19:16 thorpej Exp $ */
+/* $NetBSD: auvitek_dtv.c,v 1.10 2022/03/13 12:49:36 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2011 Jared D. McNeill 
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: auvitek_dtv.c,v 1.9 2021/08/07 16:19:16 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: auvitek_dtv.c,v 1.10 2022/03/13 12:49:36 riastradh Exp $");
 
 #include 
 #include 
@@ -98,10 +98,6 @@ auvitek_dtv_attach(struct auvitek_softc 
 int
 auvitek_dtv_detach(struct auvitek_softc *sc, int flags)
 {
-	if (sc->sc_dtvdev != NULL) {
-		config_detach(sc->sc_dtvdev, flags);
-		sc->sc_dtvdev = NULL;
-	}
 
 	return 0;
 }
Index: src/sys/dev/usb/auvitekvar.h
diff -u src/sys/dev/usb/auvitekvar.h:1.9 src/sys/dev/usb/auvitekvar.h:1.10
--- src/sys/dev/usb/auvitekvar.h:1.9	Sat Apr 23 10:15:31 2016
+++ src/sys/dev/usb/auvitekvar.h	Sun Mar 13 12:49:36 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: auvitekvar.h,v 1.9 2016/04/23 10:15:31 skrll Exp $ */
+/* $NetBSD: auvitekvar.h,v 1.10 2022/03/13 12:49:36 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2010 Jared D. McNeill 
@@ -102,6 +102,7 @@ struct auvitek_softc {
 	device_t		sc_videodev, sc_dtvdev, sc_audiodev, sc_i2cdev;
 	struct i2c_controller	sc_i2c;
 	kmutex_t		sc_i2c_lock;
+	bool			sc_i2c_attached;
 
 	struct usbd_device *sc_udev;
 	int			sc_uport;

Index: src/sys/dev/usb/auvitek_i2c.c
diff -u src/sys/dev/usb/auvitek_i2c.c:1.7 src/sys/dev/usb/auvitek_i2c.c:1.8
--- src/sys/dev/usb/auvitek_i2c.c:1.7	Sat Aug  7 16:19:16 2021
+++ src/sys/dev/usb/auvitek_i2c.c	Sun Mar 13 12:49:36 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: auvitek_i2c.c,v 1.7 2021/08/07 16:19:16 thorpej Exp $ */
+/* $NetBSD: auvitek_i2c.c,v 1.8 2022/03/13 12:49:36 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2010 Jared D. 

CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:49:37 UTC 2022

Modified Files:
src/sys/dev/usb: auvitek.c auvitek_audio.c auvitek_dtv.c auvitek_i2c.c
auvitek_video.c auvitekvar.h

Log Message:
auvitek(4): Fix i2c detach if attach failed.

While here, use config_detach_children.

Reported-by: syzbot+bf05898af6a53cb3b...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/dev/usb/auvitek.c
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/usb/auvitek_audio.c
cvs rdiff -u -r1.9 -r1.10 src/sys/dev/usb/auvitek_dtv.c \
src/sys/dev/usb/auvitekvar.h
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/usb/auvitek_i2c.c
cvs rdiff -u -r1.10 -r1.11 src/sys/dev/usb/auvitek_video.c

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:30:47 UTC 2022

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

Log Message:
ntp(9): Clamp ntv->offset to avoid arithmetic overflow on adjtime.

Reported-by: syzbot+b8406db60db886506...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/sys/kern/kern_ntptime.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_ntptime.c
diff -u src/sys/kern/kern_ntptime.c:1.61 src/sys/kern/kern_ntptime.c:1.62
--- src/sys/kern/kern_ntptime.c:1.61	Sat Feb  5 15:29:50 2022
+++ src/sys/kern/kern_ntptime.c	Sun Mar 13 12:30:47 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ntptime.c,v 1.61 2022/02/05 15:29:50 riastradh Exp $	*/
+/*	$NetBSD: kern_ntptime.c,v 1.62 2022/03/13 12:30:47 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -60,7 +60,7 @@
 
 #include 
 /* __FBSDID("$FreeBSD: src/sys/kern/kern_ntptime.c,v 1.59 2005/05/28 14:34:41 rwatson Exp $"); */
-__KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.61 2022/02/05 15:29:50 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.62 2022/03/13 12:30:47 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ntp.h"
@@ -365,10 +365,14 @@ ntp_adjtime1(struct timex *ntv)
 #endif /* PPS_SYNC */
 	}
 	if (modes & MOD_OFFSET) {
-		if (time_status & STA_NANO)
+		if (time_status & STA_NANO) {
 			hardupdate(ntv->offset);
-		else
-			hardupdate(ntv->offset * 1000);
+		} else {
+			long offset = ntv->offset;
+			offset = MIN(offset, MAXPHASE/1000);
+			offset = MAX(offset, -MAXPHASE/1000);
+			hardupdate(offset * 1000);
+		}
 	}
 
 	/*



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:30:47 UTC 2022

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

Log Message:
ntp(9): Clamp ntv->offset to avoid arithmetic overflow on adjtime.

Reported-by: syzbot+b8406db60db886506...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/sys/kern/kern_ntptime.c

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



CVS commit: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:21:28 UTC 2022

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

Log Message:
kern: Handle clock winding back in nanosleep1 without overflow.

Reported-by: syzbot+3bdd260582424a611...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.212 -r1.213 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: src/sys/kern

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 12:21:28 UTC 2022

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

Log Message:
kern: Handle clock winding back in nanosleep1 without overflow.

Reported-by: syzbot+3bdd260582424a611...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.212 -r1.213 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.212 src/sys/kern/kern_time.c:1.213
--- src/sys/kern/kern_time.c:1.212	Sat Mar 12 16:46:57 2022
+++ src/sys/kern/kern_time.c	Sun Mar 13 12:21:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_time.c,v 1.212 2022/03/12 16:46:57 riastradh Exp $	*/
+/*	$NetBSD: kern_time.c,v 1.213 2022/03/13 12:21:28 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009, 2020
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.212 2022/03/12 16:46:57 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.213 2022/03/13 12:21:28 riastradh Exp $");
 
 #include 
 #include 
@@ -435,8 +435,14 @@ again:
 		if (flags & TIMER_ABSTIME) {
 			timespecsub(rqt, , t);
 		} else {
-			timespecsub(, , t);
-			timespecsub(rqt, t, t);
+			if (timespeccmp(, , <))
+timespecclear(t); /* clock wound back */
+			else
+timespecsub(, , t);
+			if (timespeccmp(rqt, t, <))
+timespecclear(t);
+			else
+timespecsub(rqt, t, t);
 		}
 		if (t->tv_sec < 0)
 			timespecclear(t);



CVS commit: src/sys/dev/hid

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:35:47 UTC 2022

Modified Files:
src/sys/dev/hid: hid.c

Log Message:
hid: Avoid arithmetic overflow by rearranging inequalities.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/hid/hid.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/dev/hid/hid.c
diff -u src/sys/dev/hid/hid.c:1.4 src/sys/dev/hid/hid.c:1.5
--- src/sys/dev/hid/hid.c:1.4	Wed Jan  1 09:40:17 2020
+++ src/sys/dev/hid/hid.c	Sun Mar 13 11:35:47 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: hid.c,v 1.4 2020/01/01 09:40:17 maxv Exp $	*/
+/*	$NetBSD: hid.c,v 1.5 2022/03/13 11:35:47 riastradh Exp $	*/
 /*	$FreeBSD: src/sys/dev/usb/hid.c,v 1.11 1999/11/17 22:33:39 n_hibma Exp $ */
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: hid.c,v 1.4 2020/01/01 09:40:17 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: hid.c,v 1.5 2022/03/13 11:35:47 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -150,7 +150,7 @@ hid_get_item(struct hid_data *s, struct 
 	for (;;) {
 		p = s->p;
 
-		if (p + 1 > s->end)
+		if (s->end - p < 1)
 			return 0;
 		bSize = *p++;
 
@@ -172,7 +172,7 @@ hid_get_item(struct hid_data *s, struct 
 		}
 
 		data = p;
-		if (p + bSize > s->end)
+		if (bSize > s->end - p)
 			return 0;
 		p += bSize;
 



CVS commit: src/sys/dev/hid

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:35:47 UTC 2022

Modified Files:
src/sys/dev/hid: hid.c

Log Message:
hid: Avoid arithmetic overflow by rearranging inequalities.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/hid/hid.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:30:13 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c usbdi.c usbdi_util.c

Log Message:
usb: Parse descriptors a little more robustly.

- Avoid reading past the end in the event of bogus bLength.
- Avoid arithmetic overflow by rearranging inequalities.

Reported-by: syzbot+511227c050a2f164e...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.271 -r1.272 src/sys/dev/usb/usb_subr.c
cvs rdiff -u -r1.236 -r1.237 src/sys/dev/usb/usbdi.c
cvs rdiff -u -r1.84 -r1.85 src/sys/dev/usb/usbdi_util.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/dev/usb/usb_subr.c
diff -u src/sys/dev/usb/usb_subr.c:1.271 src/sys/dev/usb/usb_subr.c:1.272
--- src/sys/dev/usb/usb_subr.c:1.271	Sun Mar 13 11:28:42 2022
+++ src/sys/dev/usb/usb_subr.c	Sun Mar 13 11:30:12 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb_subr.c,v 1.271 2022/03/13 11:28:42 riastradh Exp $	*/
+/*	$NetBSD: usb_subr.c,v 1.272 2022/03/13 11:30:12 riastradh Exp $	*/
 /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.271 2022/03/13 11:28:42 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.272 2022/03/13 11:30:12 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -326,7 +326,7 @@ usbd_find_idesc(usb_config_descriptor_t 
 	usb_interface_descriptor_t *idesc;
 	int curidx, lastidx, curaidx = 0;
 
-	for (curidx = lastidx = -1; p < end; ) {
+	for (curidx = lastidx = -1; end - p >= sizeof(*desc);) {
 		desc = (usb_descriptor_t *)p;
 
 		DPRINTFN(4, "idx=%jd(%jd) altidx=%jd(%jd)", ifaceidx, curidx,
@@ -336,15 +336,15 @@ usbd_find_idesc(usb_config_descriptor_t 
 
 		if (desc->bLength < USB_DESCRIPTOR_SIZE)
 			break;
-		p += desc->bLength;
-		if (p > end)
+		if (desc->bLength > end - p)
 			break;
+		p += desc->bLength;
 
 		if (desc->bDescriptorType != UDESC_INTERFACE)
 			continue;
-		idesc = (usb_interface_descriptor_t *)desc;
-		if (idesc->bLength < USB_INTERFACE_DESCRIPTOR_SIZE)
+		if (desc->bLength < USB_INTERFACE_DESCRIPTOR_SIZE)
 			break;
+		idesc = (usb_interface_descriptor_t *)desc;
 
 		if (idesc->bInterfaceNumber != lastidx) {
 			lastidx = idesc->bInterfaceNumber;
@@ -378,23 +378,23 @@ usbd_find_edesc(usb_config_descriptor_t 
 		return NULL;
 
 	curidx = -1;
-	for (p = (char *)idesc + idesc->bLength; p < end; ) {
+	for (p = (char *)idesc + idesc->bLength; end - p >= sizeof(*edesc);) {
 		desc = (usb_descriptor_t *)p;
 
 		if (desc->bLength < USB_DESCRIPTOR_SIZE)
 			break;
-		p += desc->bLength;
-		if (p > end)
+		if (desc->bLength > end - p)
 			break;
+		p += desc->bLength;
 
 		if (desc->bDescriptorType == UDESC_INTERFACE)
 			break;
 		if (desc->bDescriptorType != UDESC_ENDPOINT)
 			continue;
 
-		edesc = (usb_endpoint_descriptor_t *)desc;
-		if (edesc->bLength < USB_ENDPOINT_DESCRIPTOR_SIZE)
+		if (desc->bLength < USB_ENDPOINT_DESCRIPTOR_SIZE)
 			break;
+		edesc = (usb_endpoint_descriptor_t *)desc;
 
 		curidx++;
 		if (curidx == endptidx)
@@ -553,23 +553,26 @@ usbd_fill_iface_data(struct usbd_device 
 
 	p = (char *)idesc + idesc->bLength;
 	end = (char *)dev->ud_cdesc + UGETW(dev->ud_cdesc->wTotalLength);
+	KASSERTMSG((char *)dev->ud_cdesc <= (char *)idesc, "cdesc=%p idesc=%p",
+	dev->ud_cdesc, idesc);
+	KASSERTMSG((char *)idesc < end, "idesc=%p end=%p", idesc, end);
 #define ed ((usb_endpoint_descriptor_t *)p)
 	for (endpt = 0; endpt < nendpt; endpt++) {
 		DPRINTFN(10, "endpt=%jd", endpt, 0, 0, 0);
-		for (; p < end; p += ed->bLength) {
+		for (; end - p >= sizeof(*ed); p += ed->bLength) {
 			DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd",
 			(uintptr_t)p, (uintptr_t)end, ed->bLength,
 			ed->bDescriptorType);
-			if (p + ed->bLength <= end &&
-			ed->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE &&
-			ed->bDescriptorType == UDESC_ENDPOINT)
-goto found;
-			if (ed->bLength == 0 ||
+			if (ed->bLength < sizeof(*ed) ||
+			ed->bLength > end - p ||
 			ed->bDescriptorType == UDESC_INTERFACE)
 break;
+			if (ed->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE &&
+			ed->bDescriptorType == UDESC_ENDPOINT)
+goto found;
 		}
 		/* passed end, or bad desc */
-		if (p < end) {
+		if (end - p >= sizeof(*ed)) {
 			if (ed->bLength == 0) {
 printf("%s: bad descriptor: 0 length\n",
 __func__);
@@ -607,6 +610,8 @@ usbd_fill_iface_data(struct usbd_device 
 		}
 		endpoints[endpt].ue_refcnt = 0;
 		endpoints[endpt].ue_toggle = 0;
+		KASSERTMSG(end - p >= ed->bLength, "p=%p end=%p length=%u",
+		p, end, ed->bLength);
 		p += ed->bLength;
 	}
 #undef ed

Index: src/sys/dev/usb/usbdi.c
diff -u src/sys/dev/usb/usbdi.c:1.236 src/sys/dev/usb/usbdi.c:1.237
--- src/sys/dev/usb/usbdi.c:1.236	Sun Mar 13 11:29:01 2022
+++ src/sys/dev/usb/usbdi.c	Sun Mar 13 11:30:13 

CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:30:13 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c usbdi.c usbdi_util.c

Log Message:
usb: Parse descriptors a little more robustly.

- Avoid reading past the end in the event of bogus bLength.
- Avoid arithmetic overflow by rearranging inequalities.

Reported-by: syzbot+511227c050a2f164e...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.271 -r1.272 src/sys/dev/usb/usb_subr.c
cvs rdiff -u -r1.236 -r1.237 src/sys/dev/usb/usbdi.c
cvs rdiff -u -r1.84 -r1.85 src/sys/dev/usb/usbdi_util.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:30:04 UTC 2022

Modified Files:
src/sys/dev/usb: xhci.c xhcivar.h

Log Message:
xhci(4): Serialize access to portsc registers.

Both xhci_roothub_ctrl and xhci_suspend/resume do r/m/w on them, so
use a mutex to serialize access to avoid stomping on each other.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/dev/usb/xhci.c
cvs rdiff -u -r1.20 -r1.21 src/sys/dev/usb/xhcivar.h

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

Modified files:

Index: src/sys/dev/usb/xhci.c
diff -u src/sys/dev/usb/xhci.c:1.161 src/sys/dev/usb/xhci.c:1.162
--- src/sys/dev/usb/xhci.c:1.161	Sun Mar 13 11:29:55 2022
+++ src/sys/dev/usb/xhci.c	Sun Mar 13 11:30:04 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhci.c,v 1.161 2022/03/13 11:29:55 riastradh Exp $	*/
+/*	$NetBSD: xhci.c,v 1.162 2022/03/13 11:30:04 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.161 2022/03/13 11:29:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.162 2022/03/13 11:30:04 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -709,6 +709,12 @@ xhci_suspend(device_t self, const pmf_qu
 	mutex_exit(>sc_lock);
 
 	/*
+	 * Block roothub xfers which might touch portsc registers until
+	 * we're done suspending.
+	 */
+	mutex_enter(>sc_rhlock);
+
+	/*
 	 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
 	 * xHCI Power Management, p. 342
 	 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=342
@@ -889,7 +895,8 @@ xhci_suspend(device_t self, const pmf_qu
 	/* Success!  */
 	ok = true;
 
-out:	return ok;
+out:	mutex_exit(>sc_rhlock);
+	return ok;
 }
 
 bool
@@ -906,6 +913,12 @@ xhci_resume(device_t self, const pmf_qua
 	KASSERT(sc->sc_suspender);
 
 	/*
+	 * Block roothub xfers which might touch portsc registers until
+	 * we're done resuming.
+	 */
+	mutex_enter(>sc_rhlock);
+
+	/*
 	 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
 	 * xHCI Power Management, p. 343
 	 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=343
@@ -1089,7 +1102,8 @@ xhci_resume(device_t self, const pmf_qua
 	/* Success!  */
 	ok = true;
 
-out:	return ok;
+out:	mutex_exit(>sc_rhlock);
+	return ok;
 }
 
 bool
@@ -1591,6 +1605,7 @@ xhci_init(struct xhci_softc *sc)
 
 	cv_init(>sc_command_cv, "xhcicmd");
 	cv_init(>sc_cmdbusy_cv, "xhcicmdq");
+	mutex_init(>sc_rhlock, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(>sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
 	mutex_init(>sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
 
@@ -3863,7 +3878,7 @@ xhci_noop(struct usbd_pipe *pipe)
  * Process root hub request.
  */
 static int
-xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
+xhci_roothub_ctrl_locked(struct usbd_bus *bus, usb_device_request_t *req,
 void *buf, int buflen)
 {
 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
@@ -3875,6 +3890,8 @@ xhci_roothub_ctrl(struct usbd_bus *bus, 
 
 	XHCIHIST_FUNC();
 
+	KASSERT(mutex_owned(>sc_rhlock));
+
 	if (sc->sc_dying)
 		return -1;
 
@@ -4127,6 +4144,20 @@ xhci_roothub_ctrl(struct usbd_bus *bus, 
 	return totlen;
 }
 
+static int
+xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
+void *buf, int buflen)
+{
+	struct xhci_softc *sc = XHCI_BUS2SC(bus);
+	int actlen;
+
+	mutex_enter(>sc_rhlock);
+	actlen = xhci_roothub_ctrl_locked(bus, req, buf, buflen);
+	mutex_exit(>sc_rhlock);
+
+	return actlen;
+}
+
 /* root hub interrupt */
 
 static usbd_status

Index: src/sys/dev/usb/xhcivar.h
diff -u src/sys/dev/usb/xhcivar.h:1.20 src/sys/dev/usb/xhcivar.h:1.21
--- src/sys/dev/usb/xhcivar.h:1.20	Thu Mar  3 06:09:03 2022
+++ src/sys/dev/usb/xhcivar.h	Sun Mar 13 11:30:04 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhcivar.h,v 1.20 2022/03/03 06:09:03 riastradh Exp $	*/
+/*	$NetBSD: xhcivar.h,v 1.21 2022/03/13 11:30:04 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -96,6 +96,7 @@ struct xhci_softc {
 	struct usbd_bus sc_bus;		/* USB 3 bus */
 	struct usbd_bus sc_bus2;	/* USB 2 bus */
 
+	kmutex_t sc_rhlock;
 	kmutex_t sc_lock;
 	kmutex_t sc_intr_lock;
 



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:30:04 UTC 2022

Modified Files:
src/sys/dev/usb: xhci.c xhcivar.h

Log Message:
xhci(4): Serialize access to portsc registers.

Both xhci_roothub_ctrl and xhci_suspend/resume do r/m/w on them, so
use a mutex to serialize access to avoid stomping on each other.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/dev/usb/xhci.c
cvs rdiff -u -r1.20 -r1.21 src/sys/dev/usb/xhcivar.h

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:55 UTC 2022

Modified Files:
src/sys/dev/usb: xhci.c

Log Message:
xhci(4): Restore synchronous abort.

In revision 1.155, I made the logic to abort the hardware
asynchronous, under the misapprehension that it is necessary for
ubm_abortx not to release the bus lock.

Not only is this not necessary, but it is harmful to for the logic to
be asynchronous because the caller assumes the hardware won't use any
DMA buffers by the time ubm_abortx has returned so it is safe to
recycle them -- which is false if we don't synchronously wait for the
hardware to stop.


To generate a diff of this commit:
cvs rdiff -u -r1.160 -r1.161 src/sys/dev/usb/xhci.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/dev/usb/xhci.c
diff -u src/sys/dev/usb/xhci.c:1.160 src/sys/dev/usb/xhci.c:1.161
--- src/sys/dev/usb/xhci.c:1.160	Wed Mar  9 22:19:07 2022
+++ src/sys/dev/usb/xhci.c	Sun Mar 13 11:29:55 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhci.c,v 1.160 2022/03/09 22:19:07 riastradh Exp $	*/
+/*	$NetBSD: xhci.c,v 1.161 2022/03/13 11:29:55 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.160 2022/03/09 22:19:07 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.161 2022/03/13 11:29:55 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -154,8 +154,9 @@ static usbd_status xhci_new_device(devic
 static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
 void *, int);
 
-static void xhci_pipe_async_task(void *);
 static void xhci_pipe_restart(struct usbd_pipe *);
+static void xhci_pipe_restart_async_task(void *);
+static void xhci_pipe_restart_async(struct usbd_pipe *);
 
 static usbd_status xhci_configure_endpoint(struct usbd_pipe *);
 //static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *);
@@ -2017,8 +2018,8 @@ xhci_open(struct usbd_pipe *pipe)
 		return USBD_NORMAL_COMPLETION;
 	}
 
-	usb_init_task(>xp_async_task, xhci_pipe_async_task, xpipe,
-	USB_TASKQ_MPSAFE);
+	usb_init_task(>xp_async_task, xhci_pipe_restart_async_task,
+	pipe, USB_TASKQ_MPSAFE);
 
 	switch (xfertype) {
 	case UE_CONTROL:
@@ -2137,8 +2138,8 @@ xhci_close_pipe(struct usbd_pipe *pipe)
 }
 
 /*
- * Abort transfer.
- * Should be called with sc_lock held.  Must not drop sc_lock.
+ * Abort transfer.  Must be called with sc_lock held.  Releases and
+ * reacquires sc_lock to sleep until hardware acknowledges abort.
  */
 static void
 xhci_abortx(struct usbd_xfer *xfer)
@@ -2177,23 +2178,19 @@ xhci_host_dequeue(struct xhci_ring * con
  * xHCI 1.1 sect 4.10.2.1
  * Issue RESET_EP to recover halt condition and SET_TR_DEQUEUE to remove
  * all transfers on transfer ring.
- * These are done in thread context asynchronously.
  */
 static void
-xhci_pipe_async_task(void *cookie)
+xhci_pipe_restart(struct usbd_pipe *pipe)
 {
-	struct xhci_pipe * const xp = cookie;
-	struct usbd_pipe * const pipe = >xp_pipe;
 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
-	struct xhci_ring * const tr = xs->xs_xr[dci];
 
 	XHCIHIST_FUNC();
 	XHCIHIST_CALLARGS("pipe %#jx slot %ju dci %ju",
 	(uintptr_t)pipe, xs->xs_idx, dci, 0);
 
-	mutex_enter(>sc_lock);
+	KASSERT(xhci_polling_p(sc) || mutex_owned(>sc_lock));
 
 	/*
 	 * - If the endpoint is halted, indicating a stall, reset it.
@@ -2213,6 +2210,7 @@ xhci_pipe_async_task(void *cookie)
 		xhci_stop_endpoint(pipe);
 		break;
 	}
+
 	switch (xhci_get_epstate(sc, xs, dci)) {
 	case XHCI_EPSTATE_STOPPED:
 		break;
@@ -2224,34 +,54 @@ xhci_pipe_async_task(void *cookie)
 		device_printf(sc->sc_dev, "endpoint 0x%x failed to stop\n",
 		pipe->up_endpoint->ue_edesc->bEndpointAddress);
 	}
+
 	xhci_set_dequeue(pipe);
 
+	DPRINTFN(4, "ends", 0, 0, 0, 0);
+}
+
+static void
+xhci_pipe_restart_async_task(void *cookie)
+{
+	struct usbd_pipe * const pipe = cookie;
+	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
+	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
+	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
+	struct xhci_ring * const tr = xs->xs_xr[dci];
+	struct usbd_xfer *xfer;
+
+	mutex_enter(>sc_lock);
+
+	xhci_pipe_restart(pipe);
+
 	/*
-	 * If we halted our own queue because it stalled, mark it no
+	 * We halted our own queue because it stalled.  Mark it no
 	 * longer halted and start issuing queued transfers again.
 	 */
-	if (tr->is_halted) {
-		struct usbd_xfer *xfer = SIMPLEQ_FIRST(>up_queue);
-
-		tr->is_halted = false;
-		if (xfer)
-			(*pipe->up_methods->upm_start)(xfer);
-	}
+	tr->is_halted = false;
+	xfer = SIMPLEQ_FIRST(>up_queue);
+	if (xfer)
+		(*pipe->up_methods->upm_start)(xfer);
 
 	mutex_exit(>sc_lock);
-
-	DPRINTFN(4, "ends", 0, 0, 0, 0);
 }
 
 

CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:55 UTC 2022

Modified Files:
src/sys/dev/usb: xhci.c

Log Message:
xhci(4): Restore synchronous abort.

In revision 1.155, I made the logic to abort the hardware
asynchronous, under the misapprehension that it is necessary for
ubm_abortx not to release the bus lock.

Not only is this not necessary, but it is harmful to for the logic to
be asynchronous because the caller assumes the hardware won't use any
DMA buffers by the time ubm_abortx has returned so it is safe to
recycle them -- which is false if we don't synchronously wait for the
hardware to stop.


To generate a diff of this commit:
cvs rdiff -u -r1.160 -r1.161 src/sys/dev/usb/xhci.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:46 UTC 2022

Modified Files:
src/sys/dev/usb: uhci.c

Log Message:
uhci(4): Stop taking the intr lock in uhci_run.

Not needed for anything here.


To generate a diff of this commit:
cvs rdiff -u -r1.314 -r1.315 src/sys/dev/usb/uhci.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/dev/usb/uhci.c
diff -u src/sys/dev/usb/uhci.c:1.314 src/sys/dev/usb/uhci.c:1.315
--- src/sys/dev/usb/uhci.c:1.314	Sun Mar 13 11:29:38 2022
+++ src/sys/dev/usb/uhci.c	Sun Mar 13 11:29:46 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: uhci.c,v 1.314 2022/03/13 11:29:38 riastradh Exp $	*/
+/*	$NetBSD: uhci.c,v 1.315 2022/03/13 11:29:46 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004, 2011, 2012, 2016, 2020 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.314 2022/03/13 11:29:38 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.315 2022/03/13 11:29:46 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1786,7 +1786,6 @@ uhci_run(uhci_softc_t *sc, int run)
 	UHCIHIST_FUNC(); UHCIHIST_CALLED();
 
 	run = run != 0;
-	mutex_spin_enter(>sc_intr_lock);
 
 	DPRINTF("setting run=%jd", run, 0, 0, 0);
 	cmd = UREAD2(sc, UHCI_CMD);
@@ -1799,14 +1798,12 @@ uhci_run(uhci_softc_t *sc, int run)
 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
 		/* return when we've entered the state we want */
 		if (run == running) {
-			mutex_spin_exit(>sc_intr_lock);
 			DPRINTF("done cmd=%#jx sts=%#jx",
 			UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS), 0, 0);
 			return USBD_NORMAL_COMPLETION;
 		}
-		usb_delay_ms_locked(>sc_bus, 1, >sc_intr_lock);
+		usb_delay_ms(>sc_bus, 1);
 	}
-	mutex_spin_exit(>sc_intr_lock);
 	printf("%s: cannot %s\n", device_xname(sc->sc_dev),
 	   run ? "start" : "stop");
 	return USBD_IOERROR;



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:46 UTC 2022

Modified Files:
src/sys/dev/usb: uhci.c

Log Message:
uhci(4): Stop taking the intr lock in uhci_run.

Not needed for anything here.


To generate a diff of this commit:
cvs rdiff -u -r1.314 -r1.315 src/sys/dev/usb/uhci.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:38 UTC 2022

Modified Files:
src/sys/dev/usb: uhci.c

Log Message:
uhci(4): Simplify uhci_run.

`locked' is always 0 now.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.313 -r1.314 src/sys/dev/usb/uhci.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/dev/usb/uhci.c
diff -u src/sys/dev/usb/uhci.c:1.313 src/sys/dev/usb/uhci.c:1.314
--- src/sys/dev/usb/uhci.c:1.313	Sun Mar 13 11:29:31 2022
+++ src/sys/dev/usb/uhci.c	Sun Mar 13 11:29:38 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: uhci.c,v 1.313 2022/03/13 11:29:31 riastradh Exp $	*/
+/*	$NetBSD: uhci.c,v 1.314 2022/03/13 11:29:38 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004, 2011, 2012, 2016, 2020 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.313 2022/03/13 11:29:31 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.314 2022/03/13 11:29:38 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -166,7 +166,7 @@ typedef TAILQ_HEAD(ux_completeq, uhci_xf
 Static void		uhci_globalreset(uhci_softc_t *);
 Static usbd_status	uhci_portreset(uhci_softc_t*, int);
 Static void		uhci_reset(uhci_softc_t *);
-Static usbd_status	uhci_run(uhci_softc_t *, int, int);
+Static usbd_status	uhci_run(uhci_softc_t *, int);
 Static uhci_soft_td_t  *uhci_alloc_std(uhci_softc_t *);
 Static void		uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
 Static void		uhci_free_std_locked(uhci_softc_t *, uhci_soft_td_t *);
@@ -587,7 +587,7 @@ uhci_init(uhci_softc_t *sc)
 
 	DPRINTF("Enabling...", 0, 0, 0, 0);
 
-	err = uhci_run(sc, 1, 0);		/* and here we go... */
+	err = uhci_run(sc, 1);		/* and here we go... */
 	UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
 		UHCI_INTR_IOCE | UHCI_INTR_SPIE);	/* enable interrupts */
 	return err;
@@ -723,7 +723,7 @@ uhci_resume(device_t dv, const pmf_qual_
 	uhci_globalreset(sc);
 	uhci_reset(sc);
 	if (cmd & UHCI_CMD_RS)
-		uhci_run(sc, 0, 0);
+		uhci_run(sc, 0);
 
 	/* restore saved state */
 	UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(>sc_dma, 0));
@@ -736,7 +736,7 @@ uhci_resume(device_t dv, const pmf_qual_
 	UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE |
 	UHCI_INTR_RIE | UHCI_INTR_IOCE | UHCI_INTR_SPIE);
 	UHCICMD(sc, UHCI_CMD_MAXP);
-	uhci_run(sc, 1, 0); /* and start traffic again */
+	uhci_run(sc, 1); /* and start traffic again */
 	usb_delay_ms(>sc_bus, USB_RESUME_RECOVERY);
 #ifdef UHCI_DEBUG
 	if (uhcidebug >= 2)
@@ -771,7 +771,7 @@ uhci_suspend(device_t dv, const pmf_qual
 		uhci_dumpregs(sc);
 #endif
 
-	uhci_run(sc, 0, 0); /* stop the controller */
+	uhci_run(sc, 0); /* stop the controller */
 	cmd &= ~UHCI_CMD_RS;
 
 	/* save some state if BIOS doesn't */
@@ -1778,7 +1778,7 @@ uhci_reset(uhci_softc_t *sc)
 }
 
 usbd_status
-uhci_run(uhci_softc_t *sc, int run, int locked)
+uhci_run(uhci_softc_t *sc, int run)
 {
 	int n, running;
 	uint16_t cmd;
@@ -1786,8 +1786,7 @@ uhci_run(uhci_softc_t *sc, int run, int 
 	UHCIHIST_FUNC(); UHCIHIST_CALLED();
 
 	run = run != 0;
-	if (!locked)
-		mutex_spin_enter(>sc_intr_lock);
+	mutex_spin_enter(>sc_intr_lock);
 
 	DPRINTF("setting run=%jd", run, 0, 0, 0);
 	cmd = UREAD2(sc, UHCI_CMD);
@@ -1800,16 +1799,14 @@ uhci_run(uhci_softc_t *sc, int run, int 
 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
 		/* return when we've entered the state we want */
 		if (run == running) {
-			if (!locked)
-mutex_spin_exit(>sc_intr_lock);
+			mutex_spin_exit(>sc_intr_lock);
 			DPRINTF("done cmd=%#jx sts=%#jx",
 			UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS), 0, 0);
 			return USBD_NORMAL_COMPLETION;
 		}
 		usb_delay_ms_locked(>sc_bus, 1, >sc_intr_lock);
 	}
-	if (!locked)
-		mutex_spin_exit(>sc_intr_lock);
+	mutex_spin_exit(>sc_intr_lock);
 	printf("%s: cannot %s\n", device_xname(sc->sc_dev),
 	   run ? "start" : "stop");
 	return USBD_IOERROR;



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:38 UTC 2022

Modified Files:
src/sys/dev/usb: uhci.c

Log Message:
uhci(4): Simplify uhci_run.

`locked' is always 0 now.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.313 -r1.314 src/sys/dev/usb/uhci.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:31 UTC 2022

Modified Files:
src/sys/dev/usb: uhci.c

Log Message:
uhci(4): Fix synchronization between suspend/resume and poll hub.

- sc_intr_lock is not relevant to anything here -- stop using it.
- Never schedule the callout while suspended.
- Don't futz with usepolling; it makes sense only when all other CPUs
  and threads are quiesced, which is not the case here.


To generate a diff of this commit:
cvs rdiff -u -r1.312 -r1.313 src/sys/dev/usb/uhci.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/dev/usb/uhci.c
diff -u src/sys/dev/usb/uhci.c:1.312 src/sys/dev/usb/uhci.c:1.313
--- src/sys/dev/usb/uhci.c:1.312	Wed Mar  9 22:17:41 2022
+++ src/sys/dev/usb/uhci.c	Sun Mar 13 11:29:31 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: uhci.c,v 1.312 2022/03/09 22:17:41 riastradh Exp $	*/
+/*	$NetBSD: uhci.c,v 1.313 2022/03/13 11:29:31 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004, 2011, 2012, 2016, 2020 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.312 2022/03/09 22:17:41 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.313 2022/03/13 11:29:31 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -638,6 +638,7 @@ uhci_detach(struct uhci_softc *sc, int f
 	if (rv != 0)
 		return rv;
 
+	KASSERT(sc->sc_intr_xfer == NULL);
 	callout_halt(>sc_poll_handle, NULL);
 	callout_destroy(>sc_poll_handle);
 
@@ -717,15 +718,12 @@ uhci_resume(device_t dv, const pmf_qual_
 	uhci_softc_t *sc = device_private(dv);
 	int cmd;
 
-	mutex_spin_enter(>sc_intr_lock);
-
 	cmd = UREAD2(sc, UHCI_CMD);
-	sc->sc_bus.ub_usepolling++;
 	UWRITE2(sc, UHCI_INTR, 0);
 	uhci_globalreset(sc);
 	uhci_reset(sc);
 	if (cmd & UHCI_CMD_RS)
-		uhci_run(sc, 0, 1);
+		uhci_run(sc, 0, 0);
 
 	/* restore saved state */
 	UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(>sc_dma, 0));
@@ -733,23 +731,23 @@ uhci_resume(device_t dv, const pmf_qual_
 	UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
 
 	UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force resume */
-	usb_delay_ms_locked(>sc_bus, USB_RESUME_DELAY, >sc_intr_lock);
+	usb_delay_ms(>sc_bus, USB_RESUME_DELAY);
 	UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
 	UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE |
 	UHCI_INTR_RIE | UHCI_INTR_IOCE | UHCI_INTR_SPIE);
 	UHCICMD(sc, UHCI_CMD_MAXP);
-	uhci_run(sc, 1, 1); /* and start traffic again */
-	usb_delay_ms_locked(>sc_bus, USB_RESUME_RECOVERY, >sc_intr_lock);
-	sc->sc_bus.ub_usepolling--;
-	if (sc->sc_intr_xfer != NULL)
-		callout_schedule(>sc_poll_handle, sc->sc_ival);
+	uhci_run(sc, 1, 0); /* and start traffic again */
+	usb_delay_ms(>sc_bus, USB_RESUME_RECOVERY);
 #ifdef UHCI_DEBUG
 	if (uhcidebug >= 2)
 		uhci_dumpregs(sc);
 #endif
 
+	mutex_enter(>sc_lock);
 	sc->sc_suspend = PWR_RESUME;
-	mutex_spin_exit(>sc_intr_lock);
+	if (sc->sc_intr_xfer != NULL)
+		callout_schedule(>sc_poll_handle, sc->sc_ival);
+	mutex_exit(>sc_lock);
 
 	return true;
 }
@@ -760,7 +758,11 @@ uhci_suspend(device_t dv, const pmf_qual
 	uhci_softc_t *sc = device_private(dv);
 	int cmd;
 
-	mutex_spin_enter(>sc_intr_lock);
+	mutex_enter(>sc_lock);
+	sc->sc_suspend = PWR_SUSPEND;
+	if (sc->sc_intr_xfer != NULL)
+		callout_halt(>sc_poll_handle, >sc_intr_lock);
+	mutex_exit(>sc_lock);
 
 	cmd = UREAD2(sc, UHCI_CMD);
 
@@ -768,12 +770,8 @@ uhci_suspend(device_t dv, const pmf_qual
 	if (uhcidebug >= 2)
 		uhci_dumpregs(sc);
 #endif
-	sc->sc_suspend = PWR_SUSPEND;
-	if (sc->sc_intr_xfer != NULL)
-		callout_halt(>sc_poll_handle, >sc_intr_lock);
-	sc->sc_bus.ub_usepolling++;
 
-	uhci_run(sc, 0, 1); /* stop the controller */
+	uhci_run(sc, 0, 0); /* stop the controller */
 	cmd &= ~UHCI_CMD_RS;
 
 	/* save some state if BIOS doesn't */
@@ -783,10 +781,7 @@ uhci_suspend(device_t dv, const pmf_qual
 	UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
 
 	UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter suspend */
-	usb_delay_ms_locked(>sc_bus, USB_RESUME_WAIT, >sc_intr_lock);
-	sc->sc_bus.ub_usepolling--;
-
-	mutex_spin_exit(>sc_intr_lock);
+	usb_delay_ms(>sc_bus, USB_RESUME_WAIT);
 
 	return true;
 }
@@ -3855,7 +3850,8 @@ uhci_root_intr_start(struct usbd_xfer *x
 	/* XXX temporary variable needed to avoid gcc3 warning */
 	ival = xfer->ux_pipe->up_endpoint->ue_edesc->bInterval;
 	sc->sc_ival = mstohz(ival);
-	callout_schedule(>sc_poll_handle, sc->sc_ival);
+	if (sc->sc_suspend == PWR_RESUME)
+		callout_schedule(>sc_poll_handle, sc->sc_ival);
 	sc->sc_intr_xfer = xfer;
 	xfer->ux_status = USBD_IN_PROGRESS;
 



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:31 UTC 2022

Modified Files:
src/sys/dev/usb: uhci.c

Log Message:
uhci(4): Fix synchronization between suspend/resume and poll hub.

- sc_intr_lock is not relevant to anything here -- stop using it.
- Never schedule the callout while suspended.
- Don't futz with usepolling; it makes sense only when all other CPUs
  and threads are quiesced, which is not the case here.


To generate a diff of this commit:
cvs rdiff -u -r1.312 -r1.313 src/sys/dev/usb/uhci.c

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



CVS commit: src/sys/dev

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:21 UTC 2022

Modified Files:
src/sys/dev/pci: ehci_pci.c
src/sys/dev/usb: ehci.c ehcivar.h

Log Message:
ehci(4): Serialize access to portsc registers.

Both ehci_roothub_ctrl and ehci_suspend/resume do r/m/w on them, so
use a mutex to serialize access to avoid stomping on each other.


To generate a diff of this commit:
cvs rdiff -u -r1.73 -r1.74 src/sys/dev/pci/ehci_pci.c
cvs rdiff -u -r1.308 -r1.309 src/sys/dev/usb/ehci.c
cvs rdiff -u -r1.50 -r1.51 src/sys/dev/usb/ehcivar.h

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

Modified files:

Index: src/sys/dev/pci/ehci_pci.c
diff -u src/sys/dev/pci/ehci_pci.c:1.73 src/sys/dev/pci/ehci_pci.c:1.74
--- src/sys/dev/pci/ehci_pci.c:1.73	Wed Dec 22 21:45:02 2021
+++ src/sys/dev/pci/ehci_pci.c	Sun Mar 13 11:29:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: ehci_pci.c,v 1.73 2021/12/22 21:45:02 skrll Exp $	*/
+/*	$NetBSD: ehci_pci.c,v 1.74 2022/03/13 11:29:21 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.73 2021/12/22 21:45:02 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.74 2022/03/13 11:29:21 riastradh Exp $");
 
 #include 
 #include 
@@ -325,6 +325,7 @@ ehci_pci_detach(device_t self, int flags
 #if 1
 	/* XXX created in ehci.c */
 	if (sc->sc_init_state >= EHCI_INIT_INITED) {
+		mutex_destroy(>sc.sc_rhlock);
 		mutex_destroy(>sc.sc_lock);
 		mutex_destroy(>sc.sc_intr_lock);
 		softint_disestablish(sc->sc.sc_doorbell_si);

Index: src/sys/dev/usb/ehci.c
diff -u src/sys/dev/usb/ehci.c:1.308 src/sys/dev/usb/ehci.c:1.309
--- src/sys/dev/usb/ehci.c:1.308	Sun Mar 13 11:29:10 2022
+++ src/sys/dev/usb/ehci.c	Sun Mar 13 11:29:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: ehci.c,v 1.308 2022/03/13 11:29:10 riastradh Exp $ */
+/*	$NetBSD: ehci.c,v 1.309 2022/03/13 11:29:21 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004-2012,2016,2020 The NetBSD Foundation, Inc.
@@ -54,7 +54,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.308 2022/03/13 11:29:10 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.309 2022/03/13 11:29:21 riastradh Exp $");
 
 #include "ohci.h"
 #include "uhci.h"
@@ -412,6 +412,7 @@ ehci_init(ehci_softc_t *sc)
 	theehci = sc;
 #endif
 
+	mutex_init(>sc_rhlock, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(>sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
 	mutex_init(>sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
 	cv_init(>sc_doorbell, "ehcidb");
@@ -703,6 +704,7 @@ fail2:
 fail1:
 	softint_disestablish(sc->sc_doorbell_si);
 	softint_disestablish(sc->sc_pcd_si);
+	mutex_destroy(>sc_rhlock);
 	mutex_destroy(>sc_lock);
 	mutex_destroy(>sc_intr_lock);
 
@@ -1411,6 +1413,7 @@ ehci_detach(struct ehci_softc *sc, int f
 	/* XXX destroyed in ehci_pci.c as it controls ehci_intr access */
 	softint_disestablish(sc->sc_doorbell_si);
 	softint_disestablish(sc->sc_pcd_si);
+	mutex_destroy(>sc_rhlock);
 	mutex_destroy(>sc_lock);
 	mutex_destroy(>sc_intr_lock);
 #endif
@@ -1441,9 +1444,6 @@ ehci_activate(device_t self, enum devact
  *
  * Note that this power handler isn't to be registered directly; the
  * bus glue needs to call out to it.
- *
- * XXX This should be serialized with ehci_roothub_ctrl's access to the
- * portsc registers.
  */
 bool
 ehci_suspend(device_t dv, const pmf_qual_t *qual)
@@ -1454,6 +1454,8 @@ ehci_suspend(device_t dv, const pmf_qual
 
 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
 
+	mutex_enter(>sc_rhlock);
+
 	for (i = 1; i <= sc->sc_noport; i++) {
 		cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
 		if ((cmd & EHCI_PS_PO) == 0 && (cmd & EHCI_PS_PE) == EHCI_PS_PE)
@@ -1488,6 +1490,8 @@ ehci_suspend(device_t dv, const pmf_qual
 	if (hcr != EHCI_STS_HCH)
 		printf("%s: config timeout\n", device_xname(dv));
 
+	mutex_exit(>sc_rhlock);
+
 	return true;
 }
 
@@ -1500,6 +1504,8 @@ ehci_resume(device_t dv, const pmf_qual_
 
 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
 
+	mutex_enter(>sc_rhlock);
+
 	/* restore things in case the bios sucks */
 	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(>sc_fldma, 0));
@@ -1545,6 +1551,8 @@ ehci_resume(device_t dv, const pmf_qual_
 	if (hcr == EHCI_STS_HCH)
 		printf("%s: config timeout\n", device_xname(dv));
 
+	mutex_exit(>sc_rhlock);
+
 	return true;
 }
 
@@ -2375,8 +2383,8 @@ ehci_free_sitd_chain(ehci_softc_t *sc, s
 
 /***/
 
-Static int
-ehci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
+static int
+ehci_roothub_ctrl_locked(struct usbd_bus *bus, usb_device_request_t *req,
 void *buf, int buflen)
 {
 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
@@ -2389,10 +2397,7 @@ ehci_roothub_ctrl(struct usbd_bus *bus, 
 
 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
 
-	/*
-	 * XXX This should be serialized with ehci_suspend/resume's
-	 * access to the portsc registers.
-	 */
+	KASSERT(mutex_owned(>sc_rhlock));
 
 	if 

CVS commit: src/sys/dev

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:21 UTC 2022

Modified Files:
src/sys/dev/pci: ehci_pci.c
src/sys/dev/usb: ehci.c ehcivar.h

Log Message:
ehci(4): Serialize access to portsc registers.

Both ehci_roothub_ctrl and ehci_suspend/resume do r/m/w on them, so
use a mutex to serialize access to avoid stomping on each other.


To generate a diff of this commit:
cvs rdiff -u -r1.73 -r1.74 src/sys/dev/pci/ehci_pci.c
cvs rdiff -u -r1.308 -r1.309 src/sys/dev/usb/ehci.c
cvs rdiff -u -r1.50 -r1.51 src/sys/dev/usb/ehcivar.h

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:10 UTC 2022

Modified Files:
src/sys/dev/usb: ehci.c ehcivar.h

Log Message:
ehci(4): Fix doorbell synchronization.

ehci_sync_hc was previously subject to spurious wakeup, in which case
the CPU might proceed from aborting and recycle a DMA buffer before
the hardware was done writing to it.  Now the code is not subject to
spurious wakeup -- it waits (up to the 1sec timeout) for the relevant
interrupt to be delivered, not for anything else.


To generate a diff of this commit:
cvs rdiff -u -r1.307 -r1.308 src/sys/dev/usb/ehci.c
cvs rdiff -u -r1.49 -r1.50 src/sys/dev/usb/ehcivar.h

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

Modified files:

Index: src/sys/dev/usb/ehci.c
diff -u src/sys/dev/usb/ehci.c:1.307 src/sys/dev/usb/ehci.c:1.308
--- src/sys/dev/usb/ehci.c:1.307	Wed Mar  9 22:18:13 2022
+++ src/sys/dev/usb/ehci.c	Sun Mar 13 11:29:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: ehci.c,v 1.307 2022/03/09 22:18:13 riastradh Exp $ */
+/*	$NetBSD: ehci.c,v 1.308 2022/03/13 11:29:10 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004-2012,2016,2020 The NetBSD Foundation, Inc.
@@ -54,7 +54,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.307 2022/03/09 22:18:13 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.308 2022/03/13 11:29:10 riastradh Exp $");
 
 #include "ohci.h"
 #include "uhci.h"
@@ -816,7 +816,10 @@ ehci_doorbell(void *addr)
 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
 
 	mutex_enter(>sc_lock);
-	cv_broadcast(>sc_doorbell);
+	if (sc->sc_doorbelllwp == NULL)
+		DPRINTF("spurious doorbell interrupt", 0, 0, 0, 0);
+	sc->sc_doorbelllwp = NULL;
+	cv_signal(>sc_doorbell);
 	mutex_exit(>sc_lock);
 }
 
@@ -2220,11 +2223,16 @@ ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehc
  * by asking for a Async Advance Doorbell interrupt and then we wait for
  * the interrupt.
  * To make this easier we first obtain exclusive use of the doorbell.
+ *
+ * Releases the bus lock to sleep while waiting for interrupt.
  */
 Static void
 ehci_sync_hc(ehci_softc_t *sc)
 {
-	int error __diagused;
+	unsigned delta = hz;
+	unsigned starttime = getticks();
+	unsigned endtime = starttime + delta;
+	unsigned now;
 
 	KASSERT(mutex_owned(>sc_lock));
 
@@ -2235,22 +2243,39 @@ ehci_sync_hc(ehci_softc_t *sc)
 		return;
 	}
 
+	/*
+	 * Wait until any concurrent ehci_sync_hc has completed so we
+	 * have exclusive access to the doorbell.
+	 */
+	while (sc->sc_doorbelllwp)
+		cv_wait(>sc_doorbell, >sc_lock);
+	sc->sc_doorbelllwp = curlwp;
+
 	/* ask for doorbell */
 	EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
 	DPRINTF("cmd = 0x%08jx sts = 0x%08jx",
 	EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
 
-	error = cv_timedwait(>sc_doorbell, >sc_lock, hz); /* bell wait */
+	/*
+	 * Wait for the ehci to ring our doorbell.
+	 */
+	while (sc->sc_doorbelllwp == curlwp) {
+		now = getticks();
+		if (endtime - now > delta) {
+			sc->sc_doorbelllwp = NULL;
+			cv_signal(>sc_doorbell);
+			DPRINTF("doorbell timeout", 0, 0, 0, 0);
+#ifdef DIAGNOSTIC		/* XXX DIAGNOSTIC abuse, do this differently */
+			printf("ehci_sync_hc: timed out\n");
+#endif
+			break;
+		}
+		(void)cv_timedwait(>sc_doorbell, >sc_lock,
+		endtime - now);
+	}
 
 	DPRINTF("cmd = 0x%08jx sts = 0x%08jx ... done",
 	EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
-#ifdef DIAGNOSTIC
-	if (error == EWOULDBLOCK) {
-		printf("ehci_sync_hc: timed out\n");
-	} else if (error) {
-		printf("ehci_sync_hc: cv_timedwait: error %d\n", error);
-	}
-#endif
 }
 
 Static void

Index: src/sys/dev/usb/ehcivar.h
diff -u src/sys/dev/usb/ehcivar.h:1.49 src/sys/dev/usb/ehcivar.h:1.50
--- src/sys/dev/usb/ehcivar.h:1.49	Wed Dec 22 21:45:02 2021
+++ src/sys/dev/usb/ehcivar.h	Sun Mar 13 11:29:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: ehcivar.h,v 1.49 2021/12/22 21:45:02 skrll Exp $ */
+/*	$NetBSD: ehcivar.h,v 1.50 2022/03/13 11:29:10 riastradh Exp $ */
 
 /*
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -166,6 +166,7 @@ typedef struct ehci_softc {
 	kmutex_t sc_intr_lock;
 	kcondvar_t sc_doorbell;
 	void *sc_doorbell_si;
+	struct lwp *sc_doorbelllwp;
 	void *sc_pcd_si;
 	struct usbd_bus sc_bus;
 	bus_space_tag_t iot;



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:10 UTC 2022

Modified Files:
src/sys/dev/usb: ehci.c ehcivar.h

Log Message:
ehci(4): Fix doorbell synchronization.

ehci_sync_hc was previously subject to spurious wakeup, in which case
the CPU might proceed from aborting and recycle a DMA buffer before
the hardware was done writing to it.  Now the code is not subject to
spurious wakeup -- it waits (up to the 1sec timeout) for the relevant
interrupt to be delivered, not for anything else.


To generate a diff of this commit:
cvs rdiff -u -r1.307 -r1.308 src/sys/dev/usb/ehci.c
cvs rdiff -u -r1.49 -r1.50 src/sys/dev/usb/ehcivar.h

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:01 UTC 2022

Modified Files:
src/sys/dev/usb: usbdi.c

Log Message:
usb: Clarify contract of usbd_xfer_trycomplete.

No functional change.  This rule has always been in place since
usbd_xfer_trycomplete was created, just wasn't clearly articulated
anywhere.


To generate a diff of this commit:
cvs rdiff -u -r1.235 -r1.236 src/sys/dev/usb/usbdi.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/dev/usb/usbdi.c
diff -u src/sys/dev/usb/usbdi.c:1.235 src/sys/dev/usb/usbdi.c:1.236
--- src/sys/dev/usb/usbdi.c:1.235	Sun Mar 13 11:28:42 2022
+++ src/sys/dev/usb/usbdi.c	Sun Mar 13 11:29:01 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdi.c,v 1.235 2022/03/13 11:28:42 riastradh Exp $	*/
+/*	$NetBSD: usbdi.c,v 1.236 2022/03/13 11:29:01 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.235 2022/03/13 11:28:42 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.236 2022/03/13 11:29:01 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1506,7 +1506,11 @@ usbd_get_string0(struct usbd_device *dev
  *	in a host controller interrupt handler.
  *
  *	Caller must either hold the bus lock or have the bus in polling
- *	mode.
+ *	mode.  If this succeeds, caller must proceed to call
+ *	usb_complete_transfer under the bus lock or with polling
+ *	enabled -- must not release and reacquire the bus lock in the
+ *	meantime.  Failing to heed this rule may lead to catastrophe
+ *	with abort or timeout.
  */
 bool
 usbd_xfer_trycomplete(struct usbd_xfer *xfer)



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:29:01 UTC 2022

Modified Files:
src/sys/dev/usb: usbdi.c

Log Message:
usb: Clarify contract of usbd_xfer_trycomplete.

No functional change.  This rule has always been in place since
usbd_xfer_trycomplete was created, just wasn't clearly articulated
anywhere.


To generate a diff of this commit:
cvs rdiff -u -r1.235 -r1.236 src/sys/dev/usb/usbdi.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:28:52 UTC 2022

Modified Files:
src/sys/dev/usb: usb.c usbdivar.h usbroothub.c

Log Message:
usb: Fix roothub ctrl xfer aborts.

No mechanism for actually aborting, but at least this now waits for
the xfer to have completed instead of blithely barging ahead whether
it's done or not.


To generate a diff of this commit:
cvs rdiff -u -r1.199 -r1.200 src/sys/dev/usb/usb.c
cvs rdiff -u -r1.136 -r1.137 src/sys/dev/usb/usbdivar.h
cvs rdiff -u -r1.14 -r1.15 src/sys/dev/usb/usbroothub.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/dev/usb/usb.c
diff -u src/sys/dev/usb/usb.c:1.199 src/sys/dev/usb/usb.c:1.200
--- src/sys/dev/usb/usb.c:1.199	Sun Mar  6 09:03:42 2022
+++ src/sys/dev/usb/usb.c	Sun Mar 13 11:28:52 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb.c,v 1.199 2022/03/06 09:03:42 riastradh Exp $	*/
+/*	$NetBSD: usb.c,v 1.200 2022/03/13 11:28:52 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2002, 2008, 2012 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.199 2022/03/06 09:03:42 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.200 2022/03/13 11:28:52 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -296,6 +296,7 @@ usb_attach(device_t parent, device_t sel
 	usbrev = sc->sc_bus->ub_revision;
 
 	cv_init(>sc_bus->ub_needsexplore_cv, "usbevt");
+	cv_init(>sc_bus->ub_rhxfercv, "usbrhxfer");
 	sc->sc_pmf_registered = false;
 
 	aprint_naive("\n");
@@ -1430,6 +1431,7 @@ usb_detach(device_t self, int flags)
 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
 
 	cv_destroy(>sc_bus->ub_needsexplore_cv);
+	cv_destroy(>sc_bus->ub_rhxfercv);
 
 	return 0;
 }

Index: src/sys/dev/usb/usbdivar.h
diff -u src/sys/dev/usb/usbdivar.h:1.136 src/sys/dev/usb/usbdivar.h:1.137
--- src/sys/dev/usb/usbdivar.h:1.136	Sun Mar 13 11:28:42 2022
+++ src/sys/dev/usb/usbdivar.h	Sun Mar 13 11:28:52 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdivar.h,v 1.136 2022/03/13 11:28:42 riastradh Exp $	*/
+/*	$NetBSD: usbdivar.h,v 1.137 2022/03/13 11:28:52 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
@@ -181,6 +181,8 @@ struct usbd_bus {
 	/* Filled by usb driver */
 	kmutex_t	   *ub_lock;
 	struct usbd_device *ub_roothub;
+	struct usbd_xfer   *ub_rhxfer;	/* roothub xfer in progress */
+	kcondvar_t		ub_rhxfercv;
 	uint8_t			ub_rhaddr;	/* roothub address */
 	uint8_t			ub_rhconf;	/* roothub configuration */
 	struct usbd_device *ub_devices[USB_TOTAL_DEVICES];

Index: src/sys/dev/usb/usbroothub.c
diff -u src/sys/dev/usb/usbroothub.c:1.14 src/sys/dev/usb/usbroothub.c:1.15
--- src/sys/dev/usb/usbroothub.c:1.14	Wed Mar  9 22:17:41 2022
+++ src/sys/dev/usb/usbroothub.c	Sun Mar 13 11:28:52 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: usbroothub.c,v 1.14 2022/03/09 22:17:41 riastradh Exp $ */
+/* $NetBSD: usbroothub.c,v 1.15 2022/03/13 11:28:52 riastradh Exp $ */
 
 /*-
  * Copyright (c) 1998, 2004, 2011, 2012 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usbroothub.c,v 1.14 2022/03/09 22:17:41 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbroothub.c,v 1.15 2022/03/13 11:28:52 riastradh Exp $");
 
 #include 
 #include 		/* for ostype */
@@ -368,6 +368,9 @@ roothub_ctrl_start(struct usbd_xfer *xfe
 	 */
 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
 
+	/* Roothub xfers are serialized through the pipe.  */
+	KASSERTMSG(bus->ub_rhxfer == NULL, "rhxfer=%p", bus->ub_rhxfer);
+
 	KASSERT(xfer->ux_rqflags & URQ_REQUEST);
 	req = >ux_request;
 
@@ -549,19 +552,19 @@ roothub_ctrl_start(struct usbd_xfer *xfe
 		break;
 	}
 
-	/*
-	 * XXX This needs some mechanism for concurrent
-	 * roothub_ctrl_abort to wait for ubm_rhctrl to finish.  We
-	 * can't use the bus lock because many ubm_rhctrl methods do
-	 * usb_delay_ms and many bus locks are taken in softint
-	 * context, leading to deadlock in the softclock needed to wake
-	 * up usb_delay_ms.
-	 */
+	KASSERTMSG(bus->ub_rhxfer == NULL, "rhxfer=%p", bus->ub_rhxfer);
+	bus->ub_rhxfer = xfer;
 	if (!bus->ub_usepolling)
 		mutex_exit(bus->ub_lock);
+
 	actlen = bus->ub_methods->ubm_rhctrl(bus, req, buf, buflen);
+
 	if (!bus->ub_usepolling)
 		mutex_enter(bus->ub_lock);
+	KASSERTMSG(bus->ub_rhxfer == xfer, "rhxfer=%p", bus->ub_rhxfer);
+	bus->ub_rhxfer = NULL;
+	cv_signal(>ub_rhxfercv);
+
 	if (actlen < 0)
 		goto fail;
 
@@ -582,8 +585,19 @@ roothub_ctrl_start(struct usbd_xfer *xfe
 Static void
 roothub_ctrl_abort(struct usbd_xfer *xfer)
 {
+	struct usbd_bus *bus = xfer->ux_bus;
+
+	KASSERT(mutex_owned(bus->ub_lock));
+	KASSERTMSG(bus->ub_rhxfer == xfer, "rhxfer=%p", bus->ub_rhxfer);
 
-	/* Nothing to do, all transfers are synchronous. */
+	/*
+	 * No mechanism to abort the xfer (would have to coordinate
+	 * with the bus's ubm_rhctrl to be useful, and usually at most
+	 * 

CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:28:52 UTC 2022

Modified Files:
src/sys/dev/usb: usb.c usbdivar.h usbroothub.c

Log Message:
usb: Fix roothub ctrl xfer aborts.

No mechanism for actually aborting, but at least this now waits for
the xfer to have completed instead of blithely barging ahead whether
it's done or not.


To generate a diff of this commit:
cvs rdiff -u -r1.199 -r1.200 src/sys/dev/usb/usb.c
cvs rdiff -u -r1.136 -r1.137 src/sys/dev/usb/usbdivar.h
cvs rdiff -u -r1.14 -r1.15 src/sys/dev/usb/usbroothub.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:28:42 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c usbdi.c usbdivar.h

Log Message:
usbdi(9): Assert no concurrent aborts on a single pipe.

It is a driver bug to try to abort a pipe at the same time in two
different threads.

HCI drivers may release the bus lock to sleep in upm_abort while
waiting for the hardware to acknowledge an abort, so it won't try to,
e.g., scribble over a DMA buffer in the xfer that we've recycled
after usbd_abort_pipe returns.

If this happens, a concurrent usbd_abort_pipe might try to apply
upm_abort to the same xfer, which HCI drivers are not prepared for
and may wreak havoc.

To avoid this, allow only one usbd_abort_pipe in flight at any given
time.


To generate a diff of this commit:
cvs rdiff -u -r1.270 -r1.271 src/sys/dev/usb/usb_subr.c
cvs rdiff -u -r1.234 -r1.235 src/sys/dev/usb/usbdi.c
cvs rdiff -u -r1.135 -r1.136 src/sys/dev/usb/usbdivar.h

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:28:42 UTC 2022

Modified Files:
src/sys/dev/usb: usb_subr.c usbdi.c usbdivar.h

Log Message:
usbdi(9): Assert no concurrent aborts on a single pipe.

It is a driver bug to try to abort a pipe at the same time in two
different threads.

HCI drivers may release the bus lock to sleep in upm_abort while
waiting for the hardware to acknowledge an abort, so it won't try to,
e.g., scribble over a DMA buffer in the xfer that we've recycled
after usbd_abort_pipe returns.

If this happens, a concurrent usbd_abort_pipe might try to apply
upm_abort to the same xfer, which HCI drivers are not prepared for
and may wreak havoc.

To avoid this, allow only one usbd_abort_pipe in flight at any given
time.


To generate a diff of this commit:
cvs rdiff -u -r1.270 -r1.271 src/sys/dev/usb/usb_subr.c
cvs rdiff -u -r1.234 -r1.235 src/sys/dev/usb/usbdi.c
cvs rdiff -u -r1.135 -r1.136 src/sys/dev/usb/usbdivar.h

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

Modified files:

Index: src/sys/dev/usb/usb_subr.c
diff -u src/sys/dev/usb/usb_subr.c:1.270 src/sys/dev/usb/usb_subr.c:1.271
--- src/sys/dev/usb/usb_subr.c:1.270	Thu Mar  3 06:13:35 2022
+++ src/sys/dev/usb/usb_subr.c	Sun Mar 13 11:28:42 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb_subr.c,v 1.270 2022/03/03 06:13:35 riastradh Exp $	*/
+/*	$NetBSD: usb_subr.c,v 1.271 2022/03/13 11:28:42 riastradh Exp $	*/
 /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.270 2022/03/03 06:13:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.271 2022/03/13 11:28:42 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -948,6 +948,7 @@ usbd_setup_pipe_flags(struct usbd_device
 	SIMPLEQ_INIT(>up_queue);
 	p->up_callingxfer = NULL;
 	cv_init(>up_callingcv, "usbpipecb");
+	p->up_abortlwp = NULL;
 
 	err = dev->ud_bus->ub_methods->ubm_open(p);
 	if (err) {
@@ -967,6 +968,8 @@ usbd_setup_pipe_flags(struct usbd_device
 	err = USBD_NORMAL_COMPLETION;
 
 out:	if (p) {
+		KASSERT(p->up_abortlwp == NULL);
+		KASSERT(p->up_callingxfer == NULL);
 		cv_destroy(>up_callingcv);
 		kmem_free(p, dev->ud_bus->ub_pipesize);
 	}

Index: src/sys/dev/usb/usbdi.c
diff -u src/sys/dev/usb/usbdi.c:1.234 src/sys/dev/usb/usbdi.c:1.235
--- src/sys/dev/usb/usbdi.c:1.234	Sun Mar 13 11:28:33 2022
+++ src/sys/dev/usb/usbdi.c	Sun Mar 13 11:28:42 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdi.c,v 1.234 2022/03/13 11:28:33 riastradh Exp $	*/
+/*	$NetBSD: usbdi.c,v 1.235 2022/03/13 11:28:42 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.234 2022/03/13 11:28:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.235 2022/03/13 11:28:42 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1020,6 +1020,16 @@ usbd_ar_pipe(struct usbd_pipe *pipe)
 	ASSERT_SLEEPABLE();
 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
 
+	/*
+	 * Allow only one thread at a time to abort the pipe, so we
+	 * don't get confused if upm_abort drops the lock in the middle
+	 * of the abort to wait for hardware completion softints to
+	 * stop using the xfer before returning.
+	 */
+	KASSERTMSG(pipe->up_abortlwp == NULL, "pipe->up_abortlwp=%p",
+	pipe->up_abortlwp);
+	pipe->up_abortlwp = curlwp;
+
 #ifdef USB_DEBUG
 	if (usbdebug > 5)
 		usbd_dump_queue(pipe);
@@ -1051,6 +1061,12 @@ usbd_ar_pipe(struct usbd_pipe *pipe)
 			/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
 		}
 	}
+
+	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
+	KASSERTMSG(pipe->up_abortlwp == NULL, "pipe->up_abortlwp=%p",
+	pipe->up_abortlwp);
+	pipe->up_abortlwp = NULL;
+
 	SDT_PROBE1(usb, device, pipe, abort__done,  pipe);
 }
 

Index: src/sys/dev/usb/usbdivar.h
diff -u src/sys/dev/usb/usbdivar.h:1.135 src/sys/dev/usb/usbdivar.h:1.136
--- src/sys/dev/usb/usbdivar.h:1.135	Wed Mar  9 22:17:41 2022
+++ src/sys/dev/usb/usbdivar.h	Sun Mar 13 11:28:42 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdivar.h,v 1.135 2022/03/09 22:17:41 riastradh Exp $	*/
+/*	$NetBSD: usbdivar.h,v 1.136 2022/03/13 11:28:42 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
@@ -257,6 +257,8 @@ struct usbd_pipe {
 	struct usbd_xfer   *up_callingxfer; /* currently in callback */
 	kcondvar_t		up_callingcv;
 
+	struct lwp	   *up_abortlwp;	/* lwp currently aborting */
+
 	/* Filled by HC driver. */
 	const struct usbd_pipe_methods
 			   *up_methods;



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:28:33 UTC 2022

Modified Files:
src/sys/dev/usb: usbdi.c

Log Message:
usbdi(9): Assert sleepable in usbd_ar_pipe.

Caller of usbd_suspend_pipe or usbd_abort_pipe must be prepared to
sleep for hardware to acknowledge abort and for in-flight callback on
another CPU to complete.  Let's catch the mistake early of calling
them in non-sleepable contexts where they might get lucky.


To generate a diff of this commit:
cvs rdiff -u -r1.233 -r1.234 src/sys/dev/usb/usbdi.c

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



CVS commit: src/sys/dev/usb

2022-03-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Mar 13 11:28:33 UTC 2022

Modified Files:
src/sys/dev/usb: usbdi.c

Log Message:
usbdi(9): Assert sleepable in usbd_ar_pipe.

Caller of usbd_suspend_pipe or usbd_abort_pipe must be prepared to
sleep for hardware to acknowledge abort and for in-flight callback on
another CPU to complete.  Let's catch the mistake early of calling
them in non-sleepable contexts where they might get lucky.


To generate a diff of this commit:
cvs rdiff -u -r1.233 -r1.234 src/sys/dev/usb/usbdi.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/dev/usb/usbdi.c
diff -u src/sys/dev/usb/usbdi.c:1.233 src/sys/dev/usb/usbdi.c:1.234
--- src/sys/dev/usb/usbdi.c:1.233	Thu Mar  3 06:13:23 2022
+++ src/sys/dev/usb/usbdi.c	Sun Mar 13 11:28:33 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdi.c,v 1.233 2022/03/03 06:13:23 riastradh Exp $	*/
+/*	$NetBSD: usbdi.c,v 1.234 2022/03/13 11:28:33 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.233 2022/03/03 06:13:23 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.234 2022/03/13 11:28:33 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1017,6 +1017,7 @@ usbd_ar_pipe(struct usbd_pipe *pipe)
 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
 	SDT_PROBE1(usb, device, pipe, abort__start,  pipe);
 
+	ASSERT_SLEEPABLE();
 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
 
 #ifdef USB_DEBUG



CVS commit: [netbsd-8] src/doc

2022-03-13 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Mar 13 09:54:47 UTC 2022

Modified Files:
src/doc [netbsd-8]: CHANGES-8.3

Log Message:
Ticket #1737


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.122 -r1.1.2.123 src/doc/CHANGES-8.3

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

Modified files:

Index: src/doc/CHANGES-8.3
diff -u src/doc/CHANGES-8.3:1.1.2.122 src/doc/CHANGES-8.3:1.1.2.123
--- src/doc/CHANGES-8.3:1.1.2.122	Thu Feb 24 10:08:40 2022
+++ src/doc/CHANGES-8.3	Sun Mar 13 09:54:47 2022
@@ -1,4 +1,4 @@
-# $NetBSD: CHANGES-8.3,v 1.1.2.122 2022/02/24 10:08:40 martin Exp $
+# $NetBSD: CHANGES-8.3,v 1.1.2.123 2022/03/13 09:54:47 martin Exp $
 
 A complete list of changes from the NetBSD 8.2 release to the NetBSD 8.3
 release:
@@ -2548,3 +2548,8 @@ bin/sh/histedit.c1.60
 	sh(1): fix "fc -e".
 	[kre, ticket #1736]
 
+usr.bin/man/man.c1.69,1.70,1.72
+
+	man(1): fix -m option so it works as documented.
+	[gutteridge, ticket #1737]
+



CVS commit: [netbsd-8] src/doc

2022-03-13 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Mar 13 09:54:47 UTC 2022

Modified Files:
src/doc [netbsd-8]: CHANGES-8.3

Log Message:
Ticket #1737


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.122 -r1.1.2.123 src/doc/CHANGES-8.3

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



CVS commit: [netbsd-8] src/usr.bin/man

2022-03-13 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Mar 13 09:54:01 UTC 2022

Modified Files:
src/usr.bin/man [netbsd-8]: man.c

Log Message:
Pull up following revision(s) (requested by gutteridge in ticket #1737):

usr.bin/man/man.c: revision 1.69
usr.bin/man/man.c: revision 1.70
usr.bin/man/man.c: revision 1.72

man.c: fix -m option so it works as documented

Refactoring work in man.c r. 1.40 from twelve years ago introduced a
regression where input from the -m option was appended rather than
prepended to the search paths. Problem reported by C. Chapman on
netbsd-users.

man: fix type mismatch between enum and int (since yesterday)
No binary change.

man: remove unused global variable 'instype' (since yesterday)
No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.66.2.1 src/usr.bin/man/man.c

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

Modified files:

Index: src/usr.bin/man/man.c
diff -u src/usr.bin/man/man.c:1.66 src/usr.bin/man/man.c:1.66.2.1
--- src/usr.bin/man/man.c:1.66	Tue May  2 14:19:23 2017
+++ src/usr.bin/man/man.c	Sun Mar 13 09:54:01 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: man.c,v 1.66 2017/05/02 14:19:23 abhinav Exp $	*/
+/*	$NetBSD: man.c,v 1.66.2.1 2022/03/13 09:54:01 martin Exp $	*/
 
 /*
  * Copyright (c) 1987, 1993, 1994, 1995
@@ -40,7 +40,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 19
 #if 0
 static char sccsid[] = "@(#)man.c	8.17 (Berkeley) 1/31/95";
 #else
-__RCSID("$NetBSD: man.c,v 1.66 2017/05/02 14:19:23 abhinav Exp $");
+__RCSID("$NetBSD: man.c,v 1.66.2.1 2022/03/13 09:54:01 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -69,6 +69,11 @@ __RCSID("$NetBSD: man.c,v 1.66 2017/05/0
 #define MAN_DEBUG 0		/* debug path output */
 #endif
 
+enum inserttype {
+	INS_TAIL,
+	INS_HEAD
+};
+
 /*
  * manstate: structure collecting the current global state so we can 
  * easily identify it and pass it to helper functions in one arg.
@@ -117,7 +122,8 @@ static void	 jump(char **, const char *,
 static int	 manual(char *, struct manstate *, glob_t *);
 static void	 onsig(int) __dead;
 static void	 usage(void) __dead;
-static void	 addpath(struct manstate *, const char *, size_t, const char *);
+static void	 addpath(struct manstate *, const char *, size_t, const char *,
+		 enum inserttype);
 static const char *getclass(const char *);
 static void printmanpath(struct manstate *);
 
@@ -328,7 +334,7 @@ main(int argc, char **argv)
 			if (len < 1)
 continue;
 			TAILQ_FOREACH(esubd, >entrylist, q)
-addpath(, p, len, esubd->s);
+addpath(, p, len, esubd->s, INS_TAIL);
 		}
 
 	} else {
@@ -336,12 +342,12 @@ main(int argc, char **argv)
 		TAILQ_FOREACH(epath, >entrylist, q) {
 			/* handle trailing "/" magic here ... */
 		  	if (abs_section && epath->s[epath->len - 1] != '/') {
-addpath(, "", 1, epath->s);
+addpath(, "", 1, epath->s, INS_TAIL);
 continue;
 			}
 
 			TAILQ_FOREACH(esubd, >entrylist, q)
-addpath(, epath->s, epath->len, esubd->s);
+addpath(, epath->s, epath->len, esubd->s, INS_TAIL);
 		}
 
 	}
@@ -359,7 +365,7 @@ main(int argc, char **argv)
 			if (len < 1)
 continue;
 			TAILQ_FOREACH(esubd, >entrylist, q)
-addpath(, p, len, esubd->s);
+addpath(, p, len, esubd->s, INS_HEAD); /* Add to front */
 		}
 
 	}
@@ -1011,14 +1017,15 @@ getclass(const char *machine)
 }
 
 static void
-addpath(struct manstate *m, const char *dir, size_t len, const char *sub)
+addpath(struct manstate *m, const char *dir, size_t len, const char *sub,
+	enum inserttype ishead)
 {
 	char buf[2 * MAXPATHLEN + 1];
 	(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,%s%s%s}",
 	 dir, (dir[len - 1] == '/') ? "" : "/", sub, m->machine,
 	 m->machclass ? "/" : "", m->machclass ? m->machclass : "",
 	 m->machclass ? "," : "");
-	if (addentry(m->mymanpath, buf, 0) < 0)
+	if (addentry(m->mymanpath, buf, (int)ishead) < 0)
 		errx(EXIT_FAILURE, "malloc failed");
 }
 



CVS commit: [netbsd-8] src/usr.bin/man

2022-03-13 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Mar 13 09:54:01 UTC 2022

Modified Files:
src/usr.bin/man [netbsd-8]: man.c

Log Message:
Pull up following revision(s) (requested by gutteridge in ticket #1737):

usr.bin/man/man.c: revision 1.69
usr.bin/man/man.c: revision 1.70
usr.bin/man/man.c: revision 1.72

man.c: fix -m option so it works as documented

Refactoring work in man.c r. 1.40 from twelve years ago introduced a
regression where input from the -m option was appended rather than
prepended to the search paths. Problem reported by C. Chapman on
netbsd-users.

man: fix type mismatch between enum and int (since yesterday)
No binary change.

man: remove unused global variable 'instype' (since yesterday)
No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.66.2.1 src/usr.bin/man/man.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/doc

2022-03-13 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Mar 13 09:53:12 UTC 2022

Modified Files:
src/doc [netbsd-9]: CHANGES-9.3

Log Message:
Tickets #1431 - #1433


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.79 -r1.1.2.80 src/doc/CHANGES-9.3

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

Modified files:

Index: src/doc/CHANGES-9.3
diff -u src/doc/CHANGES-9.3:1.1.2.79 src/doc/CHANGES-9.3:1.1.2.80
--- src/doc/CHANGES-9.3:1.1.2.79	Mon Feb 28 16:39:22 2022
+++ src/doc/CHANGES-9.3	Sun Mar 13 09:53:11 2022
@@ -1,4 +1,4 @@
-29~# $NetBSD: CHANGES-9.3,v 1.1.2.79 2022/02/28 16:39:22 martin Exp $
+29~# $NetBSD: CHANGES-9.3,v 1.1.2.80 2022/03/13 09:53:11 martin Exp $
 
 A complete list of changes from the NetBSD 9.2 release to the NetBSD 9.3
 release:
@@ -1502,3 +1502,19 @@ include/math.h	1.66
 	Correct C99 / C++11 feature testing.
 	[gdt, ticket #1430]
 
+sys/fs/udf/udf_strat_sequential.c		1.16
+sys/fs/udf/udf_subr.c1.160 and 1.161 via patch, 1.167
+sys/fs/udf/udf_allocation.c			1.45
+sys/fs/udf/udf_vfsops.c1.83
+
+	UDF file system: Fix and enhance interchange with Windows10 on
+	recordable media by being bug compatible.
+	Prevent device lockup on some drives on switching from writing to
+	reading.
+	[reinoud, ticket #1431 and #1432]
+
+usr.bin/man/man.c1.69,1.70,1.72
+
+	man(1): fix -m option so it works as documented.
+	[gutteridge, ticket #1433]
+



CVS commit: [netbsd-9] src/doc

2022-03-13 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Mar 13 09:53:12 UTC 2022

Modified Files:
src/doc [netbsd-9]: CHANGES-9.3

Log Message:
Tickets #1431 - #1433


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.79 -r1.1.2.80 src/doc/CHANGES-9.3

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



CVS commit: [netbsd-9] src/usr.bin/man

2022-03-13 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Mar 13 09:52:20 UTC 2022

Modified Files:
src/usr.bin/man [netbsd-9]: man.c

Log Message:
Pull up following revision(s) (requested by gutteridge in ticket #1433):

usr.bin/man/man.c: revision 1.69
usr.bin/man/man.c: revision 1.70
usr.bin/man/man.c: revision 1.72

man.c: fix -m option so it works as documented

Refactoring work in man.c r. 1.40 from twelve years ago introduced a
regression where input from the -m option was appended rather than
prepended to the search paths. Problem reported by C. Chapman on
netbsd-users.

man: fix type mismatch between enum and int (since yesterday)
No binary change.

man: remove unused global variable 'instype' (since yesterday)
No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.67.4.1 -r1.67.4.2 src/usr.bin/man/man.c

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

Modified files:

Index: src/usr.bin/man/man.c
diff -u src/usr.bin/man/man.c:1.67.4.1 src/usr.bin/man/man.c:1.67.4.2
--- src/usr.bin/man/man.c:1.67.4.1	Thu Apr  9 11:28:32 2020
+++ src/usr.bin/man/man.c	Sun Mar 13 09:52:20 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: man.c,v 1.67.4.1 2020/04/09 11:28:32 martin Exp $	*/
+/*	$NetBSD: man.c,v 1.67.4.2 2022/03/13 09:52:20 martin Exp $	*/
 
 /*
  * Copyright (c) 1987, 1993, 1994, 1995
@@ -40,7 +40,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 19
 #if 0
 static char sccsid[] = "@(#)man.c	8.17 (Berkeley) 1/31/95";
 #else
-__RCSID("$NetBSD: man.c,v 1.67.4.1 2020/04/09 11:28:32 martin Exp $");
+__RCSID("$NetBSD: man.c,v 1.67.4.2 2022/03/13 09:52:20 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -69,6 +69,11 @@ __RCSID("$NetBSD: man.c,v 1.67.4.1 2020/
 #define MAN_DEBUG 0		/* debug path output */
 #endif
 
+enum inserttype {
+	INS_TAIL,
+	INS_HEAD
+};
+
 /*
  * manstate: structure collecting the current global state so we can 
  * easily identify it and pass it to helper functions in one arg.
@@ -117,7 +122,8 @@ static void	 jump(char **, const char *,
 static int	 manual(char *, struct manstate *, glob_t *);
 static void	 onsig(int) __dead;
 static void	 usage(void) __dead;
-static void	 addpath(struct manstate *, const char *, size_t, const char *);
+static void	 addpath(struct manstate *, const char *, size_t, const char *,
+		 enum inserttype);
 static const char *getclass(const char *);
 static void printmanpath(struct manstate *);
 
@@ -327,7 +333,7 @@ main(int argc, char **argv)
 			if (len < 1)
 continue;
 			TAILQ_FOREACH(esubd, >entrylist, q)
-addpath(, p, len, esubd->s);
+addpath(, p, len, esubd->s, INS_TAIL);
 		}
 
 	} else {
@@ -335,12 +341,12 @@ main(int argc, char **argv)
 		TAILQ_FOREACH(epath, >entrylist, q) {
 			/* handle trailing "/" magic here ... */
 		  	if (abs_section && epath->s[epath->len - 1] != '/') {
-addpath(, "", 1, epath->s);
+addpath(, "", 1, epath->s, INS_TAIL);
 continue;
 			}
 
 			TAILQ_FOREACH(esubd, >entrylist, q)
-addpath(, epath->s, epath->len, esubd->s);
+addpath(, epath->s, epath->len, esubd->s, INS_TAIL);
 		}
 
 	}
@@ -358,7 +364,7 @@ main(int argc, char **argv)
 			if (len < 1)
 continue;
 			TAILQ_FOREACH(esubd, >entrylist, q)
-addpath(, p, len, esubd->s);
+addpath(, p, len, esubd->s, INS_HEAD); /* Add to front */
 		}
 
 	}
@@ -1012,14 +1018,15 @@ getclass(const char *machine)
 }
 
 static void
-addpath(struct manstate *m, const char *dir, size_t len, const char *sub)
+addpath(struct manstate *m, const char *dir, size_t len, const char *sub,
+	enum inserttype ishead)
 {
 	char buf[2 * MAXPATHLEN + 1];
 	(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,%s%s%s}",
 	 dir, (dir[len - 1] == '/') ? "" : "/", sub, m->machine,
 	 m->machclass ? "/" : "", m->machclass ? m->machclass : "",
 	 m->machclass ? "," : "");
-	if (addentry(m->mymanpath, buf, 0) < 0)
+	if (addentry(m->mymanpath, buf, (int)ishead) < 0)
 		errx(EXIT_FAILURE, "malloc failed");
 }
 



  1   2   >