CVS commit: [pgoyette-localcount] src/share/man/man9

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 05:48:28 UTC 2016

Modified Files:
src/share/man/man9 [pgoyette-localcount]: devsw_attach.9

Log Message:
Update for new functions {b,c}devsw_{acquire,release}


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.2.2.1 src/share/man/man9/devsw_attach.9

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

Modified files:

Index: src/share/man/man9/devsw_attach.9
diff -u src/share/man/man9/devsw_attach.9:1.2 src/share/man/man9/devsw_attach.9:1.2.2.1
--- src/share/man/man9/devsw_attach.9:1.2	Thu May 14 13:59:15 2015
+++ src/share/man/man9/devsw_attach.9	Sun Jul 17 05:48:28 2016
@@ -1,4 +1,4 @@
-.\"	$NetBSD: devsw_attach.9,v 1.2 2015/05/14 13:59:15 wiz Exp $
+.\"	$NetBSD: devsw_attach.9,v 1.2.2.1 2016/07/17 05:48:28 pgoyette Exp $
 .\"
 .\" Copyright (c) 2015 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,16 +27,20 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd May 13, 2015
+.Dd July 17, 2016
 .Dt DEVSW_ATTACH 9
 .Os
 .Sh NAME
 .Nm devsw_attach ,
 .Nm devsw_detach ,
 .Nm bdevsw_lookup ,
+.Nm bdevsw_lookup_acquire ,
 .Nm cdevsw_lookup ,
+.Nm cdevsw_lookup_acquire ,
 .Nm bdevsw_lookup_major ,
-.Nm cdevsw_lookup_major
+.Nm cdevsw_lookup_major,
+.Nm bdevsw_release ,
+.Nm cdevsw_release
 .Nd character and block device switch functions
 .Sh SYNOPSIS
 .In sys/conf.h
@@ -57,10 +61,26 @@
 .Fo bdevsw_lookup
 .Fa "dev_t dev"
 .Fc
+.Ft "const struct bdevsw *"
+.Fo bdevsw_lookup_acquire
+.Fa "dev_t dev"
+.Fc
+.Ft "void"
+.Fo bdevsw_release
+.Fa "const struct bdevsw *bdev"
+.Fc
 .Ft "const struct cdevsw *"
 .Fo cdevsw_lookup
 .Fa "dev_t dev"
 .Fc
+.Ft "const struct cdevsw *"
+.Fo cdevsw_lookup_acquire
+.Fa "dev_t dev"
+.Fc
+.Ft "void"
+.Fo cdevsw_release
+.Fa "const struct cdevsw *cdev"
+.Fc
 .Ft devmajor_t
 .Fo bdevsw_lookup_major
 .Fa "const struct bdevsw *bdev"
@@ -129,17 +149,38 @@ and
 structures.
 .Fn devsw_detach
 should be called before a loaded device driver is unloaded.
+For loadable drivers,
+.Fn devsw_detach
+will wait until the device's reference count is zero (see
+.Fn bdevsw_release
+and
+.Fn cdevsw_release
+below) before returning.
 .Pp
 The
-.Fn bdevsw_lookup
+.Fn bdevsw_lookup ,
+.Fn bdevsw_lookup_acquire ,
+.Fn cdevsw_lookup ,
 and
-.Fn cdevsw_lookup
+.Fn cdevsw_lookup_acquire
 functions return
 .Em "const struct bdevsw *"
 and
 .Em "const struct cdevsw *"
 for the given
 .Em dev .
+The
+.Fn bdevsw_lookup_acquire
+and
+.Fn cdevsw_lookup_acquire
+functions will additionally increment a reference count for loadable
+drivers.
+Callers must invoke
+.Fn bdevsw_release
+or
+.Fn cdevsw_release
+to decrement the reference counter when access to the device is no longer
+needed.
 .Pp
 The
 .Fn bdevsw_lookup_major
@@ -160,9 +201,11 @@ return 0.
 Otherwise they return an error value.
 .Pp
 In case of failure,
-.Fn bdevsw_lookup
+.Fn bdevsw_lookup ,
+.Fn bdevsw_lookup_acquire ,
+.Fn cdevsw_lookup ,
 and
-.Fn cdevsw_lookup
+.Fn cdevsw_lookup_acquire
 return the
 .Dv NULL
 value.



CVS commit: [pgoyette-localcount] src/external/cddl/osnet

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 05:18:12 UTC 2016

Modified Files:
src/external/cddl/osnet/dev/fbt [pgoyette-localcount]: fbt.c
src/external/cddl/osnet/dev/sdt [pgoyette-localcount]: sdt.c
src/external/cddl/osnet/dist/uts/common/dtrace [pgoyette-localcount]:
dtrace.c

Log Message:
Add the d_localcount member to the various {b,c}devsw structures.

No need for these to be conditional on _MODULE as dtrace is always
a module.


To generate a diff of this commit:
cvs rdiff -u -r1.18.2.1 -r1.18.2.2 src/external/cddl/osnet/dev/fbt/fbt.c
cvs rdiff -u -r1.16.2.1 -r1.16.2.2 src/external/cddl/osnet/dev/sdt/sdt.c
cvs rdiff -u -r1.32.2.1 -r1.32.2.2 \
src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c

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

Modified files:

Index: src/external/cddl/osnet/dev/fbt/fbt.c
diff -u src/external/cddl/osnet/dev/fbt/fbt.c:1.18.2.1 src/external/cddl/osnet/dev/fbt/fbt.c:1.18.2.2
--- src/external/cddl/osnet/dev/fbt/fbt.c:1.18.2.1	Sat Jul 16 11:27:12 2016
+++ src/external/cddl/osnet/dev/fbt/fbt.c	Sun Jul 17 05:18:11 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: fbt.c,v 1.18.2.1 2016/07/16 11:27:12 pgoyette Exp $	*/
+/*	$NetBSD: fbt.c,v 1.18.2.2 2016/07/17 05:18:11 pgoyette Exp $	*/
 
 /*
  * CDDL HEADER START
@@ -56,6 +56,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #if defined(__i386__) || defined(__amd64__)
@@ -151,10 +152,22 @@ static void	fbt_resume(void *, dtrace_id
 #define	FBT_ADDR2NDX(addr)	uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
 #define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
 
+struct localcount fbt_localcount;
+
 static const struct cdevsw fbt_cdevsw = {
-	fbt_open, noclose, noread, nowrite, noioctl,
-	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER, NULL
+	.d_open		= fbt_open,
+	.d_close	= noclose,
+	.d_read		= noread,
+	.d_write	= nowrite,
+	.d_ioctl	= noioctl,
+	.d_stop		= nostop,
+	.d_tty		= notty,
+	.d_poll		= nopoll,
+	.d_mmap		= nommap,
+	.d_kqfilter	= nokqfilter,
+	.d_discard	= nodiscard,
+	.d_localcount	= fbt_localcount,
+	.d_flag		= D_OTHER
 };
 
 static dtrace_pattr_t fbt_attr = {

Index: src/external/cddl/osnet/dev/sdt/sdt.c
diff -u src/external/cddl/osnet/dev/sdt/sdt.c:1.16.2.1 src/external/cddl/osnet/dev/sdt/sdt.c:1.16.2.2
--- src/external/cddl/osnet/dev/sdt/sdt.c:1.16.2.1	Sat Jul 16 11:27:12 2016
+++ src/external/cddl/osnet/dev/sdt/sdt.c	Sun Jul 17 05:18:11 2016
@@ -39,7 +39,7 @@
  * unloaded; in particular, probes may not span multiple kernel modules.
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.16.2.1 2016/07/16 11:27:12 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.16.2.2 2016/07/17 05:18:11 pgoyette Exp $");
 
 #include 
 #include 
@@ -64,6 +64,7 @@ __KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.16
 #include 
 #include 
 #include 
+#include 
 #define KDTRACE_HOOKS
 #include 
 
@@ -118,13 +119,25 @@ sdt_open(dev_t dev, int flags, int mode,
 	return (0);
 }
 
+struct localcount sdt_localcount;
+
 static const struct cdevsw sdt_cdevsw = {
-	sdt_open, noclose, noread, nowrite, noioctl,
-	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER, NULL
+	.d_open		= sdt_open,
+	.d_close	= noclose,
+	.d_read		= noread,
+	.d_write	= nowrite,
+	.d_ioctl	= noioctl,
+	.d_stop		= nostop,
+	.d_tty		= notty,
+	.d_poll		= nopoll,
+	.d_mmap		= nommap,
+	.d_kqfilter	= nokqfilter,
+	.d_discard	= nodiscard,
+	.d_localcount	= sdt_localcount,
+	.d_flag		= D_OTHER
 };
 #endif
-
+ 
 static TAILQ_HEAD(, sdt_provider) sdt_prov_list;
 
 #ifdef __FreeBSD__

Index: src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c
diff -u src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32.2.1 src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32.2.2
--- src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32.2.1	Sat Jul 16 11:27:12 2016
+++ src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c	Sun Jul 17 05:18:11 2016
@@ -134,6 +134,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include "dtrace_cddl.h"
 #include "dtrace_debug.c"
@@ -15337,10 +15338,23 @@ static dev_type_open(dtrace_open);
 
 /* Pseudo Device Entry points */
 /* Just opens, clones to the fileops below */
+
+struct localcount dtrace_localcount;
+
 const struct cdevsw dtrace_cdevsw = {
-	dtrace_open, noclose, noread, nowrite, noioctl,
-	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER | D_MPSAFE, NULL
+	.d_open		= dtrace_open,
+	.d_close	= noclose,
+	.d_read		= noread,
+	.d_write	= nowrite,
+	.d_ioctl	= noioctl,
+	.d_stop		= nostop,
+	.d_tty		= notty,
+	.d_poll		= nopoll,
+	.d_mmap		= nommap,
+	.d_kqfilter	= nokqfilter,
+	.d_discard	= nodiscard,
+	.d_localcount	= dtrace_localcount,
+	.d_flag		= D_OTHER | D_MPSAFE
 };
 
 static int dtrace_ioctl(struct file *fp, u_long cmd, void *data);



CVS commit: [pgoyette-localcount] src/sys

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 05:05:11 UTC 2016

Modified Files:
src/sys/dev/raidframe [pgoyette-localcount]: rf_netbsdkintf.c
src/sys/external/bsd/ipf/netinet [pgoyette-localcount]: ip_fil_netbsd.c
src/sys/net [pgoyette-localcount]: bpf.c
src/sys/netsmb [pgoyette-localcount]: smb_dev.c

Log Message:
Adapt some modular drivers to the localcount(9) world.  We're still
not actually using the localcount stuff, but we need to differentiate
between built-in vs loaded drivers and allocate a "struct localcount"
only for loaded drivers.


To generate a diff of this commit:
cvs rdiff -u -r1.345.2.1 -r1.345.2.2 src/sys/dev/raidframe/rf_netbsdkintf.c
cvs rdiff -u -r1.16 -r1.16.2.1 \
src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c
cvs rdiff -u -r1.199 -r1.199.2.1 src/sys/net/bpf.c
cvs rdiff -u -r1.44 -r1.44.2.1 src/sys/netsmb/smb_dev.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/raidframe/rf_netbsdkintf.c
diff -u src/sys/dev/raidframe/rf_netbsdkintf.c:1.345.2.1 src/sys/dev/raidframe/rf_netbsdkintf.c:1.345.2.2
--- src/sys/dev/raidframe/rf_netbsdkintf.c:1.345.2.1	Sun Jul 17 02:44:41 2016
+++ src/sys/dev/raidframe/rf_netbsdkintf.c	Sun Jul 17 05:05:10 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: rf_netbsdkintf.c,v 1.345.2.1 2016/07/17 02:44:41 pgoyette Exp $	*/
+/*	$NetBSD: rf_netbsdkintf.c,v 1.345.2.2 2016/07/17 05:05:10 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2008-2011 The NetBSD Foundation, Inc.
@@ -101,7 +101,7 @@
  ***/
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: rf_netbsdkintf.c,v 1.345.2.1 2016/07/17 02:44:41 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rf_netbsdkintf.c,v 1.345.2.2 2016/07/17 05:05:10 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -127,6 +127,7 @@ __KERNEL_RCSID(0, "$NetBSD: rf_netbsdkin
 #include 
 #include 
 #include 
+#include 
 
 #include 
 
@@ -216,6 +217,10 @@ static dev_type_strategy(raidstrategy);
 static dev_type_dump(raiddump);
 static dev_type_size(raidsize);
 
+#ifdef _MODULE
+struct localcount raid_localcount_bdev, raid_localcount_cdev;
+#endif
+
 const struct bdevsw raid_bdevsw = {
 	.d_open = raidopen,
 	.d_close = raidclose,
@@ -224,6 +229,9 @@ const struct bdevsw raid_bdevsw = {
 	.d_dump = raiddump,
 	.d_psize = raidsize,
 	.d_discard = nodiscard,
+#ifdef _MODULE
+	.d_localcount = _localcount_bdev,
+#endif
 	.d_flag = D_DISK
 };
 
@@ -239,6 +247,9 @@ const struct cdevsw raid_cdevsw = {
 	.d_mmap = nommap,
 	.d_kqfilter = nokqfilter,
 	.d_discard = nodiscard,
+#ifdef _MODULE
+	.d_localcount = _localcount_bdev,
+#endif
 	.d_flag = D_DISK
 };
 

Index: src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c
diff -u src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c:1.16 src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c:1.16.2.1
--- src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c:1.16	Thu Jul  7 09:32:02 2016
+++ src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c	Sun Jul 17 05:05:10 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_fil_netbsd.c,v 1.16 2016/07/07 09:32:02 ozaki-r Exp $	*/
+/*	$NetBSD: ip_fil_netbsd.c,v 1.16.2.1 2016/07/17 05:05:10 pgoyette Exp $	*/
 
 /*
  * Copyright (C) 2012 by Darren Reed.
@@ -8,7 +8,7 @@
 #if !defined(lint)
 #if defined(__NetBSD__)
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ip_fil_netbsd.c,v 1.16 2016/07/07 09:32:02 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_fil_netbsd.c,v 1.16.2.1 2016/07/17 05:05:10 pgoyette Exp $");
 #else
 static const char sccsid[] = "@(#)ip_fil.c	2.41 6/5/96 (C) 1993-2000 Darren Reed";
 static const char rcsid[] = "@(#)Id: ip_fil_netbsd.c,v 1.1.1.2 2012/07/22 13:45:17 darrenr Exp";
@@ -57,6 +57,9 @@ static const char rcsid[] = "@(#)Id: ip_
 #include 
 #include 
 #endif
+#if (__NetBSD_Version__ >= 799003300)
+#include 
+#endif
 
 #include 
 #include 
@@ -137,6 +140,10 @@ static  int ipfwrite(dev_t, struct u
 static  int ipfpoll(dev_t, int events, PROC_T *);
 static	void	ipf_timer_func(void *ptr);
 
+#if	defined(_MODULE) && (__NetBSD_Version__ >= 799003300)
+struct localcount ipl_localcount;
+#endif
+
 const struct cdevsw ipl_cdevsw = {
 	.d_open = ipfopen,
 	.d_close = ipfclose,
@@ -150,6 +157,10 @@ const struct cdevsw ipl_cdevsw = {
 #if  (__NetBSD_Version__ >= 2)
 	.d_kqfilter = nokqfilter,
 #endif
+#ifdef _MODULE
+	.d_localcount = _localcount,
+#endif
+
 	.d_discard = nodiscard,
 #ifdef D_OTHER
 	.d_flag = D_OTHER
@@ -2162,7 +2173,9 @@ static int ipl_init(void *);
 static int ipl_fini(void *);
 static int ipl_modcmd(modcmd_t, void *);
 
+#ifdef _MODULE
 static devmajor_t ipl_cmaj = -1, ipl_bmaj = -1;
+#endif
 
 static int
 ipl_modcmd(modcmd_t cmd, void *opaque)
@@ -2198,6 +2211,7 @@ ipl_init(void *opaque)
 	mutex_init(_ref_mutex, MUTEX_DEFAULT, IPL_NONE);
 	ipf_active = 0;
 
+#ifdef _MODULE
 	/*
 	 * Insert ourself into the cdevsw list.  It's OK if we are

CVS commit: [pgoyette-localcount] src/sys/kern

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 05:02:20 UTC 2016

Modified Files:
src/sys/kern [pgoyette-localcount]: subr_devsw.c

Log Message:
Defer initialization of the pserialize_t device_psz" until just before
we need it.  We can't initialize within devsw_init() because init_main()
has not yet initialized the pserialize stuff.

Restore a "return bdev" in bdevsw_lookup_acquire() - accidental deletion

Update cdevsw_lookup_acquire() to match bdev_lookup_acquire()


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

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

Modified files:

Index: src/sys/kern/subr_devsw.c
diff -u src/sys/kern/subr_devsw.c:1.34.2.3 src/sys/kern/subr_devsw.c:1.34.2.4
--- src/sys/kern/subr_devsw.c:1.34.2.3	Sun Jul 17 02:37:54 2016
+++ src/sys/kern/subr_devsw.c	Sun Jul 17 05:02:19 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_devsw.c,v 1.34.2.3 2016/07/17 02:37:54 pgoyette Exp $	*/
+/*	$NetBSD: subr_devsw.c,v 1.34.2.4 2016/07/17 05:02:19 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.3 2016/07/17 02:37:54 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.4 2016/07/17 05:02:19 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -113,7 +113,7 @@ static void devsw_detach_locked(const st
 
 kmutex_t	device_lock;
 kcondvar_t	device_cv;
-pserialize_t	device_psz;
+pserialize_t	device_psz = NULL;
 
 void (*biodone_vfs)(buf_t *) = (void *)nullop;
 
@@ -125,7 +125,6 @@ devsw_init(void)
 	KASSERT(sys_cdevsws < MAXDEVSW - 1);
 	mutex_init(_lock, MUTEX_DEFAULT, IPL_NONE);
 	cv_init(_cv, "devsw");
-	device_psz = pserialize_init();
 }
 
 int
@@ -392,7 +391,12 @@ devsw_detach_locked(const struct bdevsw 
 	if (j < max_cdevsws )
 		cdevsw[j] = NULL;
 
-	/* We need to wait for all current readers to finish. */
+	/*
+	 * If we haven't already done so, create the serialization
+	 * stucture.  Then wait for all current readers to finish.
+	 */
+	if(__predict_false(device_psz == NULL))
+		device_psz = pserialize_create();
 	pserialize_perform(device_psz);
 
 	/*
@@ -469,6 +473,8 @@ bdevsw_lookup_acquire(dev_t dev)
 		localcount_acquire(bdevsw[bmajor]->d_localcount);
 
 out:	pserialize_read_exit(s);
+
+	return bdev;
 }
 
 void
@@ -512,9 +518,6 @@ cdevsw_lookup_acquire(dev_t dev)
 	if (cmajor < 0 || cmajor >= max_cdevsws)
 		return (NULL);
 
-	/* Prevent any concurrent attempts to detach the device */
-	mutex_enter(_lock);
-
 	/* Start a read transaction to block localcount_drain() */
 	s = pserialize_read_enter();
 
@@ -523,7 +526,7 @@ cdevsw_lookup_acquire(dev_t dev)
 	if (cdev == NULL)
 		goto out;
 
-	/* Wait for the content of the struct bdevsw to become visible */
+	/* Wait for the content of the struct cdevsw to become visible */
 	membar_datadep_consumer();
 
 	/* If the devsw is not statically linked, acquire a reference */



CVS commit: src/sys/net

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 02:49:52 UTC 2016

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

Log Message:
Now that we're only calling devsw_attach() in the modular driver, it
is not ok for the driver/module to already exist.  So don't ignore
EEXIST.


To generate a diff of this commit:
cvs rdiff -u -r1.201 -r1.202 src/sys/net/bpf.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/bpf.c
diff -u src/sys/net/bpf.c:1.201 src/sys/net/bpf.c:1.202
--- src/sys/net/bpf.c:1.201	Sun Jul 17 01:16:30 2016
+++ src/sys/net/bpf.c	Sun Jul 17 02:49:52 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: bpf.c,v 1.201 2016/07/17 01:16:30 pgoyette Exp $	*/
+/*	$NetBSD: bpf.c,v 1.202 2016/07/17 02:49:52 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 1990, 1991, 1993
@@ -39,7 +39,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.201 2016/07/17 01:16:30 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.202 2016/07/17 02:49:52 pgoyette Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_bpf.h"
@@ -2115,7 +2115,7 @@ bpf_modcmd(modcmd_t cmd, void *arg)
 #ifdef _MODULE
 	devmajor_t bmajor, cmajor;
 #endif
-	int error;
+	int error = 0;
 
 
 	switch (cmd) {
@@ -2125,8 +2125,6 @@ bpf_modcmd(modcmd_t cmd, void *arg)
 		bmajor = cmajor = NODEVMAJOR;
 		error = devsw_attach("bpf", NULL, ,
 		_cdevsw, );
-		if (error == EEXIST)
-			error = 0; /* maybe built-in ... improve eventually */
 #endif
 		if (error)
 			break;



CVS commit: src/sys/netsmb

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 02:48:07 UTC 2016

Modified Files:
src/sys/netsmb: smb_dev.c

Log Message:
Now that we're only calling devsw_attach() in the modular driver, it
is not ok for the driver/module to already exist.  So don't ignore
EEXIST.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/netsmb/smb_dev.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/netsmb/smb_dev.c
diff -u src/sys/netsmb/smb_dev.c:1.47 src/sys/netsmb/smb_dev.c:1.48
--- src/sys/netsmb/smb_dev.c:1.47	Sun Jul 17 00:33:28 2016
+++ src/sys/netsmb/smb_dev.c	Sun Jul 17 02:48:07 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: smb_dev.c,v 1.47 2016/07/17 00:33:28 pgoyette Exp $	*/
+/*	$NetBSD: smb_dev.c,v 1.48 2016/07/17 02:48:07 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2000-2001 Boris Popov
@@ -35,7 +35,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.47 2016/07/17 00:33:28 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.48 2016/07/17 02:48:07 pgoyette Exp $");
 
 #include 
 #include 
@@ -383,8 +383,6 @@ nsmb_modcmd(modcmd_t cmd, void *arg)
 #ifdef _MODULE
 		error =
 		devsw_attach("nsmb", NULL, , _cdevsw, );
-		if (error == EEXIST) /* builtin */
-			error = 0;
 #endif
 		if (error) {
 			nsmbdetach();



CVS commit: [pgoyette-localcount] src/sys/dev/raidframe

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 02:44:41 UTC 2016

Modified Files:
src/sys/dev/raidframe [pgoyette-localcount]: rf_netbsdkintf.c

Log Message:
Don't call devsw_attach() and devsw_detach() for built-in modules.


To generate a diff of this commit:
cvs rdiff -u -r1.345 -r1.345.2.1 src/sys/dev/raidframe/rf_netbsdkintf.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/raidframe/rf_netbsdkintf.c
diff -u src/sys/dev/raidframe/rf_netbsdkintf.c:1.345 src/sys/dev/raidframe/rf_netbsdkintf.c:1.345.2.1
--- src/sys/dev/raidframe/rf_netbsdkintf.c:1.345	Wed Apr 27 02:47:39 2016
+++ src/sys/dev/raidframe/rf_netbsdkintf.c	Sun Jul 17 02:44:41 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: rf_netbsdkintf.c,v 1.345 2016/04/27 02:47:39 christos Exp $	*/
+/*	$NetBSD: rf_netbsdkintf.c,v 1.345.2.1 2016/07/17 02:44:41 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2008-2011 The NetBSD Foundation, Inc.
@@ -101,7 +101,7 @@
  ***/
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: rf_netbsdkintf.c,v 1.345 2016/04/27 02:47:39 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rf_netbsdkintf.c,v 1.345.2.1 2016/07/17 02:44:41 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -3766,7 +3766,9 @@ static int
 raid_modcmd_init(void)
 {
 	int error;
+#ifdef _MODULE
 	int bmajor, cmajor;
+#endif
 
 	mutex_init(_lock, MUTEX_DEFAULT, IPL_NONE);
 	mutex_enter(_lock);
@@ -3778,15 +3780,15 @@ raid_modcmd_init(void)
 	rf_sparet_wait_queue = rf_sparet_resp_queue = NULL;
 #endif
 
+#ifdef _MODULE
 	bmajor = cmajor = -1;
 	error = devsw_attach("raid", _bdevsw, ,
 	_cdevsw, );
-	if (error != 0 && error != EEXIST) {
+	if (error != 0) {
 		aprint_error("%s: devsw_attach failed %d\n", __func__, error);
 		mutex_exit(_lock);
 		return error;
 	}
-#ifdef _MODULE
 	error = config_cfdriver_attach(_cd);
 	if (error != 0) {
 		aprint_error("%s: config_cfdriver_attach failed %d\n",
@@ -3860,17 +3862,15 @@ raid_modcmd_fini(void)
 		mutex_exit(_lock);
 		return error;
 	}
-#endif
 	error = devsw_detach(_bdevsw, _cdevsw);
 	if (error != 0) {
 		aprint_error("%s: cannot detach devsw\n",__func__);
-#ifdef _MODULE
 		config_cfdriver_attach(_cd);
-#endif
 		config_cfattach_attach(raid_cd.cd_name, _ca);
 		mutex_exit(_lock);
 		return error;
 	}
+#endif
 	rf_BootRaidframe(false);
 #if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
 	rf_destroy_mutex2(rf_sparet_wait_mutex);



CVS commit: [pgoyette-localcount] src/sys/kern

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 02:37:54 UTC 2016

Modified Files:
src/sys/kern [pgoyette-localcount]: subr_devsw.c

Log Message:
More locking fixes from riastradh@


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

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

Modified files:

Index: src/sys/kern/subr_devsw.c
diff -u src/sys/kern/subr_devsw.c:1.34.2.2 src/sys/kern/subr_devsw.c:1.34.2.3
--- src/sys/kern/subr_devsw.c:1.34.2.2	Sat Jul 16 22:35:34 2016
+++ src/sys/kern/subr_devsw.c	Sun Jul 17 02:37:54 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_devsw.c,v 1.34.2.2 2016/07/16 22:35:34 pgoyette Exp $	*/
+/*	$NetBSD: subr_devsw.c,v 1.34.2.3 2016/07/17 02:37:54 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.2 2016/07/16 22:35:34 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.3 2016/07/17 02:37:54 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -113,6 +113,7 @@ static void devsw_detach_locked(const st
 
 kmutex_t	device_lock;
 kcondvar_t	device_cv;
+pserialize_t	device_psz;
 
 void (*biodone_vfs)(buf_t *) = (void *)nullop;
 
@@ -124,6 +125,7 @@ devsw_init(void)
 	KASSERT(sys_cdevsws < MAXDEVSW - 1);
 	mutex_init(_lock, MUTEX_DEFAULT, IPL_NONE);
 	cv_init(_cv, "devsw");
+	device_psz = pserialize_init();
 }
 
 int
@@ -359,7 +361,7 @@ cdevsw_attach(const struct cdevsw *devsw
 static void
 devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
 {
-	int i, j, s;
+	int i, j;
 
 	KASSERT(mutex_owned(_lock));
 
@@ -390,7 +392,15 @@ devsw_detach_locked(const struct bdevsw 
 	if (j < max_cdevsws )
 		cdevsw[j] = NULL;
 
-	s = pserialize_read_enter();
+	/* We need to wait for all current readers to finish. */
+	pserialize_perform(device_psz);
+
+	/*
+	 * Here, no new readers can reach the bdev and cdev via the
+	 * {b,c}devsw[] arrays.  Wait for existing references to
+	 * drain, and then destroy.
+	 */
+
 	if (i < max_bdevsws && bdev->d_localcount != NULL) {
 		localcount_drain(bdev->d_localcount, _cv, _lock);
 		localcount_fini(bdev->d_localcount);
@@ -399,7 +409,6 @@ devsw_detach_locked(const struct bdevsw 
 		localcount_drain(cdev->d_localcount, _cv, _lock);
 		localcount_fini(cdev->d_localcount);
 	}
-	pserialize_read_exit(s);
 }
 
 int
@@ -444,9 +453,6 @@ bdevsw_lookup_acquire(dev_t dev)
 	if (bmajor < 0 || bmajor >= max_bdevsws)
 		return (NULL);
 
-	/* Prevent any concurrent attempts to detach the device */
-	mutex_enter(_lock);
-
 	/* Start a read transaction to block localcount_drain() */
 	s = pserialize_read_enter();
 
@@ -463,9 +469,6 @@ bdevsw_lookup_acquire(dev_t dev)
 		localcount_acquire(bdevsw[bmajor]->d_localcount);
 
 out:	pserialize_read_exit(s);
-	mutex_exit(_lock);
-
-	return bdev;
 }
 
 void



CVS commit: src/external/cddl/osnet

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 02:09:10 UTC 2016

Modified Files:
src/external/cddl/osnet/dev/fbt: fbt.c
src/external/cddl/osnet/dev/sdt: sdt.c
src/external/cddl/osnet/dist/uts/common/dtrace: dtrace.c

Log Message:
Use C99 initializers for the cdevsw

No functional change intended or expected.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/external/cddl/osnet/dev/fbt/fbt.c
cvs rdiff -u -r1.16 -r1.17 src/external/cddl/osnet/dev/sdt/sdt.c
cvs rdiff -u -r1.32 -r1.33 \
src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c

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

Modified files:

Index: src/external/cddl/osnet/dev/fbt/fbt.c
diff -u src/external/cddl/osnet/dev/fbt/fbt.c:1.18 src/external/cddl/osnet/dev/fbt/fbt.c:1.19
--- src/external/cddl/osnet/dev/fbt/fbt.c:1.18	Thu Feb 26 10:31:52 2015
+++ src/external/cddl/osnet/dev/fbt/fbt.c	Sun Jul 17 02:09:10 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: fbt.c,v 1.18 2015/02/26 10:31:52 ozaki-r Exp $	*/
+/*	$NetBSD: fbt.c,v 1.19 2016/07/17 02:09:10 pgoyette Exp $	*/
 
 /*
  * CDDL HEADER START
@@ -152,9 +152,18 @@ static void	fbt_resume(void *, dtrace_id
 #define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
 
 static const struct cdevsw fbt_cdevsw = {
-	fbt_open, noclose, noread, nowrite, noioctl,
-	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER
+	.d_open		= fbt_open,
+	.d_close	= noclose,
+	.d_read		= noread,
+	.d_write	= nowrite,
+	.d_ioctl	= noioctl,
+	.d_stop		= nostop,
+	.d_tty		= notty,
+	.d_poll		= nopoll,
+	.d_mmap		= nommap,
+	.d_kqfilter	= nokqfilter,
+	.d_discard	= nodiscard,
+	.d_flag		= D_OTHER
 };
 
 static dtrace_pattr_t fbt_attr = {

Index: src/external/cddl/osnet/dev/sdt/sdt.c
diff -u src/external/cddl/osnet/dev/sdt/sdt.c:1.16 src/external/cddl/osnet/dev/sdt/sdt.c:1.17
--- src/external/cddl/osnet/dev/sdt/sdt.c:1.16	Thu Jun 16 06:08:06 2016
+++ src/external/cddl/osnet/dev/sdt/sdt.c	Sun Jul 17 02:09:10 2016
@@ -39,7 +39,7 @@
  * unloaded; in particular, probes may not span multiple kernel modules.
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.16 2016/06/16 06:08:06 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.17 2016/07/17 02:09:10 pgoyette Exp $");
 
 #include 
 #include 
@@ -119,9 +119,18 @@ sdt_open(dev_t dev, int flags, int mode,
 }
 
 static const struct cdevsw sdt_cdevsw = {
-	sdt_open, noclose, noread, nowrite, noioctl,
-	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER
+	.d_open		= sdt_open,
+	.d_close	= noclose,
+	.d_read		= noread,
+	.d_write	= nowrite,
+	.d_ioctl	= noioctl,
+	.d_stop		= nostop,
+	.d_tty		= notty,
+	.d_poll		= nopoll,
+	.d_mmap		= nommap,
+	.d_kqfilter	= nokqfilter,
+	.d_discard	= nodiscard,
+	.d_flag		= D_OTHER
 };
 #endif
 

Index: src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c
diff -u src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32 src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.33
--- src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32	Thu Jun 23 04:35:35 2016
+++ src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c	Sun Jul 17 02:09:10 2016
@@ -15338,9 +15338,18 @@ static dev_type_open(dtrace_open);
 /* Pseudo Device Entry points */
 /* Just opens, clones to the fileops below */
 const struct cdevsw dtrace_cdevsw = {
-	dtrace_open, noclose, noread, nowrite, noioctl,
-	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER | D_MPSAFE
+	.d_open		= dtrace_open,
+	.d_close	= noclose,
+	.d_read		= noread,
+	.d_write	= nowrite,
+	.d_ioctl	= noioctl,
+	.d_stop		= nostop,
+	.d_tty		= notty,
+	.d_poll		= nopoll,
+	.d_mmap		= nommap,
+	.d_kqfilter	= nokqfilter,
+	.d_discard	= nodiscard,
+	.d_flag		= D_OTHER | D_MPSAFE
 };
 
 static int dtrace_ioctl(struct file *fp, u_long cmd, void *data);



CVS commit: src/sys/external/bsd/ipf/netinet

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 02:02:01 UTC 2016

Modified Files:
src/sys/external/bsd/ipf/netinet: ip_fil_netbsd.c

Log Message:
Another case of not calling devsw_attach() for built-in device modules


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.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/external/bsd/ipf/netinet/ip_fil_netbsd.c
diff -u src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c:1.16 src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c:1.17
--- src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c:1.16	Thu Jul  7 09:32:02 2016
+++ src/sys/external/bsd/ipf/netinet/ip_fil_netbsd.c	Sun Jul 17 02:02:01 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_fil_netbsd.c,v 1.16 2016/07/07 09:32:02 ozaki-r Exp $	*/
+/*	$NetBSD: ip_fil_netbsd.c,v 1.17 2016/07/17 02:02:01 pgoyette Exp $	*/
 
 /*
  * Copyright (C) 2012 by Darren Reed.
@@ -8,7 +8,7 @@
 #if !defined(lint)
 #if defined(__NetBSD__)
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ip_fil_netbsd.c,v 1.16 2016/07/07 09:32:02 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_fil_netbsd.c,v 1.17 2016/07/17 02:02:01 pgoyette Exp $");
 #else
 static const char sccsid[] = "@(#)ip_fil.c	2.41 6/5/96 (C) 1993-2000 Darren Reed";
 static const char rcsid[] = "@(#)Id: ip_fil_netbsd.c,v 1.1.1.2 2012/07/22 13:45:17 darrenr Exp";
@@ -2162,7 +2162,9 @@ static int ipl_init(void *);
 static int ipl_fini(void *);
 static int ipl_modcmd(modcmd_t, void *);
 
+#ifdef _MODULE
 static devmajor_t ipl_cmaj = -1, ipl_bmaj = -1;
+#endif
 
 static int
 ipl_modcmd(modcmd_t cmd, void *opaque)
@@ -2198,6 +2200,7 @@ ipl_init(void *opaque)
 	mutex_init(_ref_mutex, MUTEX_DEFAULT, IPL_NONE);
 	ipf_active = 0;
 
+#ifdef _MODULE
 	/*
 	 * Insert ourself into the cdevsw list.  It's OK if we are
 	 * already there, since this will happen when our module is
@@ -2209,6 +2212,7 @@ ipl_init(void *opaque)
 	error = devsw_attach("ipl", NULL, _bmaj, _cdevsw, _cmaj);
 	if (error == EEXIST)
 		error = 0;
+#endif
 
 	if (error)
 		ipl_fini(opaque);
@@ -2220,7 +2224,9 @@ static int
 ipl_fini(void *opaque)
 {
 
+#ifdef _MODULE
 	(void)devsw_detach(NULL, _cdevsw);
+#endif
 
 	/*
 	 * Grab the mutex, verify that there are no references
@@ -2230,8 +2236,10 @@ ipl_fini(void *opaque)
 	 */
 	mutex_enter(_ref_mutex);
 	if (ipf_active != 0 || ipfmain.ipf_running > 0) {
+#ifdef _MODULE
 		(void)devsw_attach("ipl", NULL, _bmaj,
 		_cdevsw, _cmaj);
+#endif
 		mutex_exit(_ref_mutex);
 		return EBUSY;
 	}



CVS commit: src/sys/net

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 01:16:30 UTC 2016

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

Log Message:
Don't initialize variables that no longer exist in built-in module.


To generate a diff of this commit:
cvs rdiff -u -r1.200 -r1.201 src/sys/net/bpf.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/bpf.c
diff -u src/sys/net/bpf.c:1.200 src/sys/net/bpf.c:1.201
--- src/sys/net/bpf.c:1.200	Sun Jul 17 01:03:46 2016
+++ src/sys/net/bpf.c	Sun Jul 17 01:16:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: bpf.c,v 1.200 2016/07/17 01:03:46 pgoyette Exp $	*/
+/*	$NetBSD: bpf.c,v 1.201 2016/07/17 01:16:30 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 1990, 1991, 1993
@@ -39,7 +39,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.200 2016/07/17 01:03:46 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.201 2016/07/17 01:16:30 pgoyette Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_bpf.h"
@@ -2117,12 +2117,12 @@ bpf_modcmd(modcmd_t cmd, void *arg)
 #endif
 	int error;
 
-	bmajor = cmajor = NODEVMAJOR;
 
 	switch (cmd) {
 	case MODULE_CMD_INIT:
 		bpfilterattach(0);
 #ifdef _MODULE
+		bmajor = cmajor = NODEVMAJOR;
 		error = devsw_attach("bpf", NULL, ,
 		_cdevsw, );
 		if (error == EEXIST)



CVS commit: src/sys/net

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 01:03:46 UTC 2016

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

Log Message:
Don't try to call devsw_attach() for built-in driver code.


To generate a diff of this commit:
cvs rdiff -u -r1.199 -r1.200 src/sys/net/bpf.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/bpf.c
diff -u src/sys/net/bpf.c:1.199 src/sys/net/bpf.c:1.200
--- src/sys/net/bpf.c:1.199	Mon Jun 20 06:46:37 2016
+++ src/sys/net/bpf.c	Sun Jul 17 01:03:46 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: bpf.c,v 1.199 2016/06/20 06:46:37 knakahara Exp $	*/
+/*	$NetBSD: bpf.c,v 1.200 2016/07/17 01:03:46 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 1990, 1991, 1993
@@ -39,7 +39,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.199 2016/06/20 06:46:37 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.200 2016/07/17 01:03:46 pgoyette Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_bpf.h"
@@ -2112,7 +2112,9 @@ MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_fi
 static int
 bpf_modcmd(modcmd_t cmd, void *arg)
 {
+#ifdef _MODULE
 	devmajor_t bmajor, cmajor;
+#endif
 	int error;
 
 	bmajor = cmajor = NODEVMAJOR;
@@ -2120,10 +2122,12 @@ bpf_modcmd(modcmd_t cmd, void *arg)
 	switch (cmd) {
 	case MODULE_CMD_INIT:
 		bpfilterattach(0);
+#ifdef _MODULE
 		error = devsw_attach("bpf", NULL, ,
 		_cdevsw, );
 		if (error == EEXIST)
 			error = 0; /* maybe built-in ... improve eventually */
+#endif
 		if (error)
 			break;
 



CVS commit: src/sys/netsmb

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 00:33:28 UTC 2016

Modified Files:
src/sys/netsmb: smb_dev.c

Log Message:
Ooops - previous change was over-zealous.  We still need the cdevsw for
the built-in case.  Restore it.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/netsmb/smb_dev.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/netsmb/smb_dev.c
diff -u src/sys/netsmb/smb_dev.c:1.46 src/sys/netsmb/smb_dev.c:1.47
--- src/sys/netsmb/smb_dev.c:1.46	Sun Jul 17 00:27:03 2016
+++ src/sys/netsmb/smb_dev.c	Sun Jul 17 00:33:28 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: smb_dev.c,v 1.46 2016/07/17 00:27:03 pgoyette Exp $	*/
+/*	$NetBSD: smb_dev.c,v 1.47 2016/07/17 00:33:28 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2000-2001 Boris Popov
@@ -35,7 +35,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.46 2016/07/17 00:27:03 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.47 2016/07/17 00:33:28 pgoyette Exp $");
 
 #include 
 #include 
@@ -86,7 +86,6 @@ dev_type_open(nsmb_dev_open);
 dev_type_close(nsmb_dev_close);
 dev_type_ioctl(nsmb_dev_ioctl);
 
-#ifdef _MODULE
 const struct cdevsw nsmb_cdevsw = {
 	.d_open = nsmb_dev_open,
 	.d_close = nsmb_dev_close,
@@ -101,7 +100,7 @@ const struct cdevsw nsmb_cdevsw = {
 	.d_discard = nodiscard,
 	.d_flag = D_OTHER,
 };
-#endif
+
 
 static bool nsmb_inited = false;
 



CVS commit: src/sys/netsmb

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 00:27:03 UTC 2016

Modified Files:
src/sys/netsmb: smb_dev.c

Log Message:
Finish previous - now that we're not calling devsw_attach unnecessarily,
we can enclose some additional variables in #ifdef _MODULE / #endif


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/sys/netsmb/smb_dev.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/netsmb/smb_dev.c
diff -u src/sys/netsmb/smb_dev.c:1.45 src/sys/netsmb/smb_dev.c:1.46
--- src/sys/netsmb/smb_dev.c:1.45	Sun Jul 17 00:17:14 2016
+++ src/sys/netsmb/smb_dev.c	Sun Jul 17 00:27:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: smb_dev.c,v 1.45 2016/07/17 00:17:14 pgoyette Exp $	*/
+/*	$NetBSD: smb_dev.c,v 1.46 2016/07/17 00:27:03 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2000-2001 Boris Popov
@@ -35,7 +35,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.45 2016/07/17 00:17:14 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.46 2016/07/17 00:27:03 pgoyette Exp $");
 
 #include 
 #include 
@@ -86,6 +86,7 @@ dev_type_open(nsmb_dev_open);
 dev_type_close(nsmb_dev_close);
 dev_type_ioctl(nsmb_dev_ioctl);
 
+#ifdef _MODULE
 const struct cdevsw nsmb_cdevsw = {
 	.d_open = nsmb_dev_open,
 	.d_close = nsmb_dev_close,
@@ -100,7 +101,7 @@ const struct cdevsw nsmb_cdevsw = {
 	.d_discard = nodiscard,
 	.d_flag = D_OTHER,
 };
-
+#endif
 
 static bool nsmb_inited = false;
 
@@ -372,7 +373,9 @@ MODULE(MODULE_CLASS_DRIVER, nsmb, NULL);
 static int
 nsmb_modcmd(modcmd_t cmd, void *arg)
 {
+#ifdef _MODULE
 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
+#endif
 	int error = 0;
 
 	switch (cmd) {



CVS commit: src/sys/netsmb

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Jul 17 00:17:14 UTC 2016

Modified Files:
src/sys/netsmb: smb_dev.c

Log Message:
Rather than calling devsw_attach() and ignoring the EEXIST error when
the driver is already built-in to the kernel, don't bother calling.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/sys/netsmb/smb_dev.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/netsmb/smb_dev.c
diff -u src/sys/netsmb/smb_dev.c:1.44 src/sys/netsmb/smb_dev.c:1.45
--- src/sys/netsmb/smb_dev.c:1.44	Thu Aug 20 14:40:19 2015
+++ src/sys/netsmb/smb_dev.c	Sun Jul 17 00:17:14 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: smb_dev.c,v 1.44 2015/08/20 14:40:19 christos Exp $	*/
+/*	$NetBSD: smb_dev.c,v 1.45 2016/07/17 00:17:14 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2000-2001 Boris Popov
@@ -35,7 +35,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.44 2015/08/20 14:40:19 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.45 2016/07/17 00:17:14 pgoyette Exp $");
 
 #include 
 #include 
@@ -378,19 +378,23 @@ nsmb_modcmd(modcmd_t cmd, void *arg)
 	switch (cmd) {
 	case MODULE_CMD_INIT:
 		nsmbattach(1);
+#ifdef _MODULE
 		error =
 		devsw_attach("nsmb", NULL, , _cdevsw, );
 		if (error == EEXIST) /* builtin */
 			error = 0;
+#endif
 		if (error) {
 			nsmbdetach();
 		}
 
 		break;
 	case MODULE_CMD_FINI:
+#ifdef _MODULE
 		error = devsw_detach(NULL, _cdevsw);
 		if (error)
 			break;
+#endif
 		nsmbdetach();
 		break;
 	default:



CVS commit: [pgoyette-localcount] src/share/man/man9

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 23:42:35 UTC 2016

Modified Files:
src/share/man/man9 [pgoyette-localcount]: localcount.9

Log Message:
Add myself as author/compiler of the man page.


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/share/man/man9/localcount.9

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

Modified files:

Index: src/share/man/man9/localcount.9
diff -u src/share/man/man9/localcount.9:1.1.2.3 src/share/man/man9/localcount.9:1.1.2.4
--- src/share/man/man9/localcount.9:1.1.2.3	Sat Jul 16 23:28:37 2016
+++ src/share/man/man9/localcount.9	Sat Jul 16 23:42:35 2016
@@ -1,4 +1,4 @@
-.\"	$NetBSD: localcount.9,v 1.1.2.3 2016/07/16 23:28:37 pgoyette Exp $
+.\"	$NetBSD: localcount.9,v 1.1.2.4 2016/07/16 23:42:35 pgoyette Exp $
 .\"
 .\" Copyright (c) 2016
 .\" All rights reserved.
@@ -169,4 +169,7 @@ The localcount primitives first appeared
 This implementation of
 .Nm
 was written by
+.An -nosplit
 .An Taylor R. Campbell .
+This manual page was compiled by
+.An Paul Goyette .



CVS commit: [pgoyette-localcount] src/share/man/man9

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 23:28:37 UTC 2016

Modified Files:
src/share/man/man9 [pgoyette-localcount]: localcount.9

Log Message:
Improve description of localcount_release


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/share/man/man9/localcount.9

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

Modified files:

Index: src/share/man/man9/localcount.9
diff -u src/share/man/man9/localcount.9:1.1.2.2 src/share/man/man9/localcount.9:1.1.2.3
--- src/share/man/man9/localcount.9:1.1.2.2	Sat Jul 16 23:13:59 2016
+++ src/share/man/man9/localcount.9	Sat Jul 16 23:28:37 2016
@@ -1,4 +1,4 @@
-.\"	$NetBSD: localcount.9,v 1.1.2.2 2016/07/16 23:13:59 pgoyette Exp $
+.\"	$NetBSD: localcount.9,v 1.1.2.3 2016/07/16 23:28:37 pgoyette Exp $
 .\"
 .\" Copyright (c) 2016
 .\" All rights reserved.
@@ -86,36 +86,42 @@ xyzioctl(IOC_READ_A_CPU_REG).
 .Bl -tag -width abcd
 .It Fn localcount_init "lc"
 .Pp
-Dynamically initialize a localcount for use.
+Dynamically initialize localcount
+.Ar lc
+for use.
 .Pp
 No other operations can be performed on a localcount until it has been
 initialized.
 .It Fn localcount_fini "lc"
 .Pp
-Release resources used by a localcount.
+Release resources used by localcount
+.Ar lc .
 The caller must have already called
-.fn localcount_drain .
+.Fn localcount_drain .
 The localcount may not be used after
 .Fn localcount_fini
 has been called until it has been re-initialized by
 .Fn localcount_init .
 .It Fn localcount_acquire "lc"
 .Pp
-Acquire a reference to the localcount.
+Acquire a reference to the localcount
+.Ar lc .
 .It Fn localcount_release "lc" "cv" "mtx"
 .Pp
-Release a reference to the localcount.
-Must be called with the mutex
+Release a reference to the localcount
+.Ar lc .
+If the localcount is currently being drained, the mutex
 .Ar mtx
-locked.
-If the localcount is being drained, and the reference count goes to zero,
-.Fn localcout_release
+will be used to synchronize updates to the global reference count (i.e.
+the total across all CPUs).
+If the reference count goes to zero,
+.Fn localcount_release
 will broadcast availability of the condvar
 .Ar cv .
 .It Fn localcount_drain "lc" "cv" "mtx"
 .Pp
-Wait for all references to the
-.Nm
+Wait for all references to the localcount
+.Ar lc
 to be released.
 The caller must hold the mutex
 .Ar mtx ;



CVS commit: [pgoyette-localcount] src/share/man/man9

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 23:13:59 UTC 2016

Modified Files:
src/share/man/man9 [pgoyette-localcount]: localcount.9

Log Message:
Add a bit more about appropriate use-cases.


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/share/man/man9/localcount.9

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

Modified files:

Index: src/share/man/man9/localcount.9
diff -u src/share/man/man9/localcount.9:1.1.2.1 src/share/man/man9/localcount.9:1.1.2.2
--- src/share/man/man9/localcount.9:1.1.2.1	Sat Jul 16 09:36:57 2016
+++ src/share/man/man9/localcount.9	Sat Jul 16 23:13:59 2016
@@ -1,4 +1,4 @@
-.\"	$NetBSD: localcount.9,v 1.1.2.1 2016/07/16 09:36:57 pgoyette Exp $
+.\"	$NetBSD: localcount.9,v 1.1.2.2 2016/07/16 23:13:59 pgoyette Exp $
 .\"
 .\" Copyright (c) 2016
 .\" All rights reserved.
@@ -74,6 +74,14 @@ more than
 .Xr atomic_ops 3
 and almost always more than
 .Xr psref 9 .
+.Pp
+As a rough heuristic,
+.Nm
+should be used for classes of objects of which there are maybe a few dozen
+instances but not a few thousand instances (e.g. autoconf devices, but not
+network flows) and on which there may be a mixture of long-term I/O waits,
+such as xyzread for a device xyz(4), and short-term fast operations, such as
+xyzioctl(IOC_READ_A_CPU_REG).
 .Sh FUNCTIONS
 .Bl -tag -width abcd
 .It Fn localcount_init "lc"



CVS commit: [pgoyette-localcount] src/sys/kern

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 22:35:34 UTC 2016

Modified Files:
src/sys/kern [pgoyette-localcount]: subr_devsw.c

Log Message:
Fix some locking sequences.  Thanks to ristradh@ for the review!


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

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

Modified files:

Index: src/sys/kern/subr_devsw.c
diff -u src/sys/kern/subr_devsw.c:1.34.2.1 src/sys/kern/subr_devsw.c:1.34.2.2
--- src/sys/kern/subr_devsw.c:1.34.2.1	Sat Jul 16 07:54:13 2016
+++ src/sys/kern/subr_devsw.c	Sat Jul 16 22:35:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_devsw.c,v 1.34.2.1 2016/07/16 07:54:13 pgoyette Exp $	*/
+/*	$NetBSD: subr_devsw.c,v 1.34.2.2 2016/07/16 22:35:34 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.1 2016/07/16 07:54:13 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.2 2016/07/16 22:35:34 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -85,8 +85,10 @@ __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
+#include 
 
 #ifdef DEVSW_DEBUG
 #define	DPRINTF(x)	printf x
@@ -139,6 +141,13 @@ devsw_attach(const char *devname,
 
 	mutex_enter(_lock);
 
+	if (bdev != NULL) {
+		KASSERT(bdev->d_localcount != NULL);
+		KASSERT(bdev->d_localcount != cdev->d_localcount);
+	}
+	if (cdev != NULL)
+		KASSERT(cdev->d_localcount != NULL);
+
 	for (i = 0 ; i < max_devsw_convs ; i++) {
 		conv = _conv[i];
 		if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
@@ -164,13 +173,14 @@ devsw_attach(const char *devname,
 			goto fail;
 		}
 
+		/* use membar_producer() to ensure visibility of the xdevsw */
 		if (bdev != NULL) {
-			KASSERT(bdev->d_localcount != NULL);
 			localcount_init(bdev->d_localcount);
+			membar_producer();
 			bdevsw[*bmajor] = bdev;
 		}
-		KASSERT(cdev->d_localcount != NULL);
 		localcount_init(cdev->d_localcount);
+		membar_producer();
 		cdevsw[*cmajor] = cdev;
 
 		mutex_exit(_lock);
@@ -278,6 +288,9 @@ bdevsw_attach(const struct bdevsw *devsw
 	if (bdevsw[*devmajor] != NULL)
 		return (EEXIST);
 
+	/* ensure visibility of the bdevsw */
+	membar_producer();
+
 	bdevsw[*devmajor] = devsw;
 	KASSERT(devsw->d_localcount != NULL);
 	localcount_init(devsw->d_localcount);
@@ -327,6 +340,9 @@ cdevsw_attach(const struct cdevsw *devsw
 	if (cdevsw[*devmajor] != NULL)
 		return (EEXIST);
 
+	/* ensure visibility of the bdevsw */
+	membar_producer();
+
 	cdevsw[*devmajor] = devsw;
 	KASSERT(devsw->d_localcount != NULL);
 	localcount_init(devsw->d_localcount);
@@ -335,16 +351,15 @@ cdevsw_attach(const struct cdevsw *devsw
 }
 
 /*
- * First, look up both bdev and cdev indices, and confirm that the
- * localcount pointer(s) exist.  Then drain any existing references,
- * deactivate the localcount(s).  Finally, remove the {b,c}devsw[]
- * entries.
+ * First, look up both bdev and cdev indices, and remove the
+ * {b,c]devsw[] entries so no new references can be taken.  Then
+ * drain any existing references.
  */
 
 static void
 devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
 {
-	int i, j;
+	int i, j, s;
 
 	KASSERT(mutex_owned(_lock));
 
@@ -370,24 +385,21 @@ devsw_detach_locked(const struct bdevsw 
 			break;
 		}
 	}
-	if (i < max_bdevsws) {
+	if (i < max_bdevsws)
+		bdevsw[i] = NULL;
+	if (j < max_cdevsws )
+		cdevsw[j] = NULL;
+
+	s = pserialize_read_enter();
+	if (i < max_bdevsws && bdev->d_localcount != NULL) {
 		localcount_drain(bdev->d_localcount, _cv, _lock);
 		localcount_fini(bdev->d_localcount);
-		bdevsw[i] = NULL;
 	}
-	if (j < max_cdevsws ) {
-		/*
-		 * Take care not to drain/fini the d_localcount if the same
-		 * one was used for both cdev and bdev!
-		 */
-		if (i >= max_bdevsws ||
-		bdev->d_localcount != cdev->d_localcount) {
-			localcount_drain(cdev->d_localcount, _cv,
-			_lock);
-			localcount_fini(cdev->d_localcount);
-		}
-		cdevsw[j] = NULL;
+	if (j < max_cdevsws && cdev->d_localcount != NULL ) {
+		localcount_drain(cdev->d_localcount, _cv, _lock);
+		localcount_fini(cdev->d_localcount);
 	}
+	pserialize_read_exit(s);
 }
 
 int
@@ -423,6 +435,8 @@ const struct bdevsw *
 bdevsw_lookup_acquire(dev_t dev)
 {
 	devmajor_t bmajor;
+	const struct bdevsw *bdev = NULL;
+	int s;
 
 	if (dev == NODEV)
 		return (NULL);
@@ -430,20 +444,35 @@ bdevsw_lookup_acquire(dev_t dev)
 	if (bmajor < 0 || bmajor >= max_bdevsws)
 		return (NULL);
 
+	/* Prevent any concurrent attempts to detach the device */
+	mutex_enter(_lock);
+
+	/* Start a read transaction to block localcount_drain() */
+	s = pserialize_read_enter();
+
+	/* Get the struct bdevsw pointer */
+	bdev = bdevsw[bmajor];
+	if (bdev == NULL)
+		goto out;
+
+	/* Wait for the content of the 

CVS commit: [pgoyette-localcount] src/sys

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 22:06:42 UTC 2016

Modified Files:
src/sys/kern [pgoyette-localcount]: subr_autoconf.c
src/sys/sys [pgoyette-localcount]: device.h

Log Message:
Add new xxx_acquire variants for device_lookup_private() and
device_find_by_driver_unit_acquire rather than blindly making the old
variants call localcount_acquire().

Also fix a couple of locking sequences.

Thanks to Taylor for the review!


To generate a diff of this commit:
cvs rdiff -u -r1.246.2.1 -r1.246.2.2 src/sys/kern/subr_autoconf.c
cvs rdiff -u -r1.149.2.1 -r1.149.2.2 src/sys/sys/device.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/kern/subr_autoconf.c
diff -u src/sys/kern/subr_autoconf.c:1.246.2.1 src/sys/kern/subr_autoconf.c:1.246.2.2
--- src/sys/kern/subr_autoconf.c:1.246.2.1	Sat Jul 16 02:13:07 2016
+++ src/sys/kern/subr_autoconf.c	Sat Jul 16 22:06:42 2016
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.246.2.1 2016/07/16 02:13:07 pgoyette Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.246.2.2 2016/07/16 22:06:42 pgoyette Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.246.2.1 2016/07/16 02:13:07 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.246.2.2 2016/07/16 22:06:42 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1257,9 +1257,6 @@ config_devunlink(device_t dev, struct de
 
 	KASSERT(mutex_owned(_mtx));
 
-	localcount_drain(dev->dv_localcnt, _drain_cv,
-	_mtx);
-
  	/* Unlink from device list.  Link to garbage list. */
 	TAILQ_REMOVE(, dev, dv_list);
 	TAILQ_INSERT_TAIL(garbage, dev, dv_list);
@@ -1267,6 +1264,10 @@ config_devunlink(device_t dev, struct de
 	/* Remove from cfdriver's array. */
 	cd->cd_devs[dev->dv_unit] = NULL;
 
+	/* Now wait for references to drain - no new refs are possible */
+	localcount_drain(dev->dv_localcnt, _drain_cv,
+	_mtx);
+
 	/*
 	 * If the device now has no units in use, unlink its softc array.
 	 */
@@ -2265,9 +2266,15 @@ device_lookup_acquire(cfdriver_t cd, int
 {
 	device_t dv;
 
-	dv = device_lookup(cd, unit);
-	if (dv != NULL)
+	mutex_enter(_mtx);
+	if (unit < 0 || unit >= cd->cd_ndevs)
+		dv = NULL;
+	else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0)
+		dv = NULL;
+	else
 		localcount_acquire(dv->dv_localcnt);
+	mutex_exit(_mtx);
+
 	return dv;
 }
 
@@ -2294,6 +2301,19 @@ void *
 device_lookup_private(cfdriver_t cd, int unit)
 {
 
+	return device_private(device_lookup(cd, unit));
+}
+
+/*
+ * device_lookup_private_acquire:
+ *
+ *	Look up the softc and acquire a reference to the device
+ *	so it won't disappear.
+ */
+void *
+device_lookup_private_acquire(cfdriver_t cd, int unit)
+{
+
 	return device_private(device_lookup_acquire(cd, unit));
 }
 
@@ -2330,6 +2350,23 @@ device_find_by_driver_unit(const char *n
 
 	if ((cd = config_cfdriver_lookup(name)) == NULL)
 		return NULL;
+	return device_lookup(cd, unit);
+}
+
+/*
+ * device_find_by_driver_unit_acquire:
+ *
+ *	Returns the device of the given driver name and unit or
+ *	NULL if it doesn't exist.  If driver is found, it's
+ *	reference count is incremented so it won't go away.
+ */
+device_t
+device_find_by_driver_unit_acquire(const char *name, int unit)
+{
+	struct cfdriver *cd;
+
+	if ((cd = config_cfdriver_lookup(name)) == NULL)
+		return NULL;
 	return device_lookup_acquire(cd, unit);
 }
 

Index: src/sys/sys/device.h
diff -u src/sys/sys/device.h:1.149.2.1 src/sys/sys/device.h:1.149.2.2
--- src/sys/sys/device.h:1.149.2.1	Sat Jul 16 02:13:08 2016
+++ src/sys/sys/device.h	Sat Jul 16 22:06:42 2016
@@ -1,4 +1,4 @@
-/* $NetBSD: device.h,v 1.149.2.1 2016/07/16 02:13:08 pgoyette Exp $ */
+/* $NetBSD: device.h,v 1.149.2.2 2016/07/16 22:06:42 pgoyette Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -493,6 +493,7 @@ device_t	device_lookup(cfdriver_t, int);
 device_t	device_lookup_acquire(cfdriver_t, int);
 void		device_release(device_t);
 void		*device_lookup_private(cfdriver_t, int);
+void		*device_lookup_private_acquire(cfdriver_t, int);
 void		device_register(device_t, void *);
 void		device_register_post_config(device_t, void *);
 
@@ -526,6 +527,7 @@ bool		device_is_a(device_t, const char *
 
 device_t	device_find_by_xname(const char *);
 device_t	device_find_by_driver_unit(const char *, int);
+device_t	device_find_by_driver_unit_acquire(const char *, int);
 
 bool		device_pmf_is_registered(device_t);
 



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

2016-07-16 Thread Matt Thomas
Module Name:src
Committed By:   matt
Date:   Sat Jul 16 21:33:46 UTC 2016

Modified Files:
src/sys/arch/mips/mips: bus_dma.c

Log Message:
When doing dmamap syncs, try to use KSEG0/XKPHYS address if possible.
XXX once hard page coloring is supported XKPHYS could be used all the time.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/sys/arch/mips/mips/bus_dma.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/arch/mips/mips/bus_dma.c
diff -u src/sys/arch/mips/mips/bus_dma.c:1.34 src/sys/arch/mips/mips/bus_dma.c:1.35
--- src/sys/arch/mips/mips/bus_dma.c:1.34	Tue Feb 17 09:58:33 2015
+++ src/sys/arch/mips/mips/bus_dma.c	Sat Jul 16 21:33:46 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.34 2015/02/17 09:58:33 macallan Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.35 2016/07/16 21:33:46 matt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 
 #include 			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.34 2015/02/17 09:58:33 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.35 2016/07/16 21:33:46 matt Exp $");
 
 #define _MIPS_BUS_DMA_PRIVATE
 
@@ -106,18 +106,16 @@ _bus_dmamap_load_buffer(bus_dma_tag_t t,
 void *buf, bus_size_t buflen, struct vmspace *vm, int flags,
 int *segp, bool first)
 {
-	bus_size_t sgsize;
 	paddr_t baddr, curaddr, lastaddr;
 	vaddr_t vaddr = (vaddr_t)buf, lastvaddr;
-	int seg = *segp;
-	bus_dma_segment_t *ds = >dm_segs[seg];
+	bus_dma_segment_t *ds = >dm_segs[*segp];
 	bus_dma_segment_t * const eds = >dm_segs[map->_dm_segcnt];
+	const bus_addr_t bmask = ~(map->_dm_boundary - 1);
 	const bool d_cache_coherent =
 	(mips_options.mips_cpu_flags & CPU_MIPS_D_CACHE_COHERENT) != 0;
 
 	lastaddr = ds->ds_addr + ds->ds_len;
 	lastvaddr = ds->_ds_vaddr + ds->ds_len;
-	const bus_size_t bmask = ~(map->_dm_boundary - 1);
 
 	while (buflen > 0) {
 		/*
@@ -146,7 +144,7 @@ _bus_dmamap_load_buffer(bus_dma_tag_t t,
 		/*
 		 * Compute the segment size, and adjust counts.
 		 */
-		sgsize = PAGE_SIZE - ((uintptr_t)vaddr & PGOFSET);
+		bus_size_t sgsize = PAGE_SIZE - ((uintptr_t)vaddr & PGOFSET);
 		if (sgsize > buflen) {
 			sgsize = buflen;
 		}
@@ -174,8 +172,12 @@ _bus_dmamap_load_buffer(bus_dma_tag_t t,
 			ds->_ds_vaddr = vaddr;
 			first = false;
 		} else if (curaddr == lastaddr
-		&& (d_cache_coherent || lastvaddr == vaddr)
-		&& ds->ds_len + sgsize <= map->dm_maxsegsz
+		&& (d_cache_coherent
+#ifndef __mips_o32
+			|| !MIPS_CACHE_VIRTUAL_ALIAS
+#endif
+			|| vaddr == lastvaddr)
+		&& (ds->ds_len + sgsize) <= map->dm_maxsegsz
 		&& (map->_dm_boundary == 0
 			|| ((ds->ds_addr ^ curaddr) & bmask) == 0)) {
 			ds->ds_len += sgsize;
@@ -185,6 +187,25 @@ _bus_dmamap_load_buffer(bus_dma_tag_t t,
 			ds->ds_addr = curaddr;
 			ds->ds_len = sgsize;
 			ds->_ds_vaddr = vaddr;
+			/*
+			 * If this segment uses the correct color, try to see
+			 * if we can use a direct-mapped VA for the segment.
+			 */
+			if (!mips_cache_badalias(curaddr, vaddr)) {
+#ifdef __mips_o32
+if (MIPS_KSEG0_P(curaddr + sgsize - 1)) {
+	ds->_ds_vaddr =
+	MIPS_PHYS_TO_KSEG0(curaddr);
+}
+#else
+/*
+ * All physical addresses can be accessed
+ * via XKPHYS.
+ */
+				ds->_ds_vaddr =
+MIPS_PHYS_TO_XKPHYS_CACHED(curaddr);
+#endif
+			}
 		}
 
 		lastaddr = curaddr + sgsize;
@@ -496,6 +517,7 @@ _bus_dmamap_load_mbuf(bus_dma_tag_t t, b
 #endif
 		STAT_INCR(unloads);
 	}
+
 	/*
 	 * Make sure that on error condition we return "no valid mappings."
 	 */
@@ -803,6 +825,7 @@ _bus_dmamap_sync(bus_dma_tag_t t, bus_dm
 	|| (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) == 0)
 		goto bounce_it;
 
+#ifdef _mips_o32
 	/*
 	 * If the mapping belongs to the kernel, or it belongs
 	 * to the currently-running process (XXX actually, vmspace),
@@ -812,6 +835,7 @@ _bus_dmamap_sync(bus_dma_tag_t t, bus_dm
 	 */
 	const bool useindex = (!VMSPACE_IS_KERNEL_P(map->_dm_vmspace)
 	&& map->_dm_vmspace != curproc->p_vmspace);
+#endif
 
 	bus_dma_segment_t *seg = map->dm_segs;
 	bus_dma_segment_t * const lastseg = seg + map->dm_nsegs;
@@ -827,7 +851,7 @@ _bus_dmamap_sync(bus_dma_tag_t t, bus_dm
 		 * Now at the first segment to sync; nail each segment until we
 		 * have exhausted the length.
 		 */
-		vaddr_t vaddr = seg->_ds_vaddr + offset;
+		register_t vaddr = (intptr_t)seg->_ds_vaddr + offset;
 		minlen = ulmin(len, seg->ds_len - offset);
 
 #ifdef BUS_DMA_DEBUG
@@ -843,13 +867,15 @@ _bus_dmamap_sync(bus_dma_tag_t t, bus_dm
 		 * If we are forced to use Index ops, it's always a
 		 * Write-back,Invalidate, so just do one test.
 		 */
-		if (__predict_false(useindex)) {
+#ifdef mips_o32
+		if (__predict_false(useindex || vaddr == 0)) {
 			mips_dcache_wbinv_range_index(vaddr, minlen);
 #ifdef BUS_DMA_DEBUG
 			printf("\n");
 #endif
 			continue;
 		

CVS commit: [pgoyette-localcount] src/sys/sys

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 20:40:30 UTC 2016

Modified Files:
src/sys/sys [pgoyette-localcount]: Makefile

Log Message:
Install the localcount.h header file


To generate a diff of this commit:
cvs rdiff -u -r1.159 -r1.159.2.1 src/sys/sys/Makefile

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

Modified files:

Index: src/sys/sys/Makefile
diff -u src/sys/sys/Makefile:1.159 src/sys/sys/Makefile:1.159.2.1
--- src/sys/sys/Makefile:1.159	Sun Apr 24 19:48:29 2016
+++ src/sys/sys/Makefile	Sat Jul 16 20:40:30 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.159 2016/04/24 19:48:29 dholland Exp $
+#	$NetBSD: Makefile,v 1.159.2.1 2016/07/16 20:40:30 pgoyette Exp $
 
 .include 
 
@@ -25,7 +25,7 @@ INCS=	acct.h agpio.h aio.h ansi.h aout_m
 	ioctl_compat.h iostat.h ipc.h \
 	joystick.h \
 	kcore.h kcpuset.h kgdb.h kmem.h ksem.h ksyms.h ktrace.h \
-	localedef.h lock.h lockf.h lua.h lwp.h lwpctl.h \
+	localcount.h localedef.h lock.h lockf.h lua.h lwp.h lwpctl.h \
 	malloc.h mallocvar.h mbuf.h md4.h md5.h midiio.h \
 	mman.h module.h mount.h mqueue.h msg.h msgbuf.h mtio.h mutex.h \
 	namei.h null.h \



CVS commit: src/sys/arch/x86/x86

2016-07-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jul 16 17:13:25 UTC 2016

Modified Files:
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
KNF, and rename.


To generate a diff of this commit:
cvs rdiff -u -r1.72 -r1.73 src/sys/arch/x86/x86/x86_machdep.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/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.72 src/sys/arch/x86/x86/x86_machdep.c:1.73
--- src/sys/arch/x86/x86/x86_machdep.c:1.72	Sat Jul 16 17:02:34 2016
+++ src/sys/arch/x86/x86/x86_machdep.c	Sat Jul 16 17:13:25 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.72 2016/07/16 17:02:34 maxv Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.73 2016/07/16 17:13:25 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.72 2016/07/16 17:02:34 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.73 2016/07/16 17:13:25 maxv Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -459,12 +459,51 @@ x86_cpu_idle_set(void (*func)(void), con
 #define KBTOB(x)	((size_t)(x) * 1024UL)
 #define MBTOB(x)	((size_t)(x) * 1024UL * 1024UL)
 
+static struct {
+	int freelist;
+	uint64_t limit;
+} x86_freelists[VM_NFREELIST] = {
+	{ VM_FREELIST_DEFAULT, 0 },
+#ifdef VM_FREELIST_FIRST1T
+	/* 40-bit addresses needed for modern graphics. */
+	{ VM_FREELIST_FIRST1T,	1ULL * 1024 * 1024 * 1024 * 1024 },
+#endif
+#ifdef VM_FREELIST_FIRST64G
+	/* 36-bit addresses needed for oldish graphics. */
+	{ VM_FREELIST_FIRST64G, 64ULL * 1024 * 1024 * 1024 },
+#endif
+#ifdef VM_FREELIST_FIRST4G
+	/* 32-bit addresses needed for PCI 32-bit DMA and old graphics. */
+	{ VM_FREELIST_FIRST4G,  4ULL * 1024 * 1024 * 1024 },
+#endif
+	/* 30-bit addresses needed for ancient graphics. */
+	{ VM_FREELIST_FIRST1G,	1ULL * 1024 * 1024 * 1024 },
+	/* 24-bit addresses needed for ISA DMA. */
+	{ VM_FREELIST_FIRST16,	16 * 1024 * 1024 },
+};
+
 extern paddr_t avail_start, avail_end;
 
+int
+x86_select_freelist(uint64_t maxaddr)
+{
+	unsigned int i;
+
+	if (avail_end <= maxaddr)
+		return VM_NFREELIST;
+
+	for (i = 0; i < __arraycount(x86_freelists); i++) {
+		if ((x86_freelists[i].limit - 1) <= maxaddr)
+			return x86_freelists[i].freelist;
+	}
+
+	panic("no freelist for maximum address %"PRIx64, maxaddr);
+}
+
 static int
-add_mem_cluster(phys_ram_seg_t *seg_clusters, int seg_cluster_cnt,
-	struct extent *iomem_ex,
-	uint64_t seg_start, uint64_t seg_end, uint32_t type)
+x86_add_cluster(phys_ram_seg_t *seg_clusters, int seg_cluster_cnt,
+struct extent *iomem_ex, uint64_t seg_start, uint64_t seg_end,
+uint32_t type)
 {
 	uint64_t new_physmem = 0;
 	phys_ram_seg_t *cluster;
@@ -483,15 +522,12 @@ add_mem_cluster(phys_ram_seg_t *seg_clus
 	if (seg_end > TOPLIMIT) {
 		aprint_verbose("WARNING: skipping large memory map entry: "
 		"0x%"PRIx64"/0x%"PRIx64"/0x%x\n",
-		seg_start,
-		(seg_end - seg_start),
-		type);
+		seg_start, (seg_end - seg_start), type);
 		return seg_cluster_cnt;
 	}
 
 	/*
-	 * XXX Chop the last page off the size so that
-	 * XXX it can fit in avail_end.
+	 * XXX: Chop the last page off the size so that it can fit in avail_end.
 	 */
 	if (seg_end == TOPLIMIT)
 		seg_end -= PAGE_SIZE;
@@ -501,9 +537,8 @@ add_mem_cluster(phys_ram_seg_t *seg_clus
 
 	for (i = 0; i < seg_cluster_cnt; i++) {
 		cluster = _clusters[i];
-		if ((cluster->start == round_page(seg_start))
-		&& (cluster->size == trunc_page(seg_end) - cluster->start))
-		{
+		if ((cluster->start == round_page(seg_start)) &&
+		(cluster->size == trunc_page(seg_end) - cluster->start)) {
 #ifdef DEBUG_MEMLOAD
 			printf("WARNING: skipping duplicate segment entry\n");
 #endif
@@ -512,9 +547,8 @@ add_mem_cluster(phys_ram_seg_t *seg_clus
 	}
 
 	/*
-	 * Allocate the physical addresses used by RAM
-	 * from the iomem extent map.  This is done before
-	 * the addresses are page rounded just to make
+	 * Allocate the physical addresses used by RAM from the iomem extent
+	 * map. This is done before the addresses are page rounded just to make
 	 * sure we get them all.
 	 */
 	if (seg_start < 0x1ULL) {
@@ -589,7 +623,7 @@ add_mem_cluster(phys_ram_seg_t *seg_clus
 }
 
 static int
-initx86_parse_memmap(struct btinfo_memmap *bim, struct extent *iomem_ex)
+x86_parse_clusters(struct btinfo_memmap *bim, struct extent *iomem_ex)
 {
 	uint64_t seg_start, seg_end;
 	uint64_t addr, size;
@@ -602,6 +636,7 @@ initx86_parse_memmap(struct btinfo_memma
 #ifdef DEBUG_MEMLOAD
 	printf("BIOS MEMORY MAP (%d ENTRIES):\n", bim->num);
 #endif
+
 	for (x = 0; x < bim->num; x++) {
 		addr = bim->entry[x].addr;
 		size = bim->entry[x].size;
@@ -611,9 +646,7 @@ initx86_parse_memmap(struct btinfo_memma
 			addr, size, type);
 #endif
 
-		/*
-		 * If the segment is not memory, skip it.
-		 */
+		/* If the segment is not memory, skip 

CVS commit: src/sys/arch

2016-07-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jul 16 17:02:35 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: machdep.c
src/sys/arch/i386/i386: machdep.c
src/sys/arch/x86/include: machdep.h
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
Simplify the way physical pages are internalized into the VM system on x86.
Only two functions are called now: init_x86_clusters, which initializes the
memory clusters from the bootinfo, and init_x86_vm, which inserts the pages
from the clusters into VM.


To generate a diff of this commit:
cvs rdiff -u -r1.223 -r1.224 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.758 -r1.759 src/sys/arch/i386/i386/machdep.c
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/x86/include/machdep.h
cvs rdiff -u -r1.71 -r1.72 src/sys/arch/x86/x86/x86_machdep.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/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.223 src/sys/arch/amd64/amd64/machdep.c:1.224
--- src/sys/arch/amd64/amd64/machdep.c:1.223	Wed Jul 13 15:53:26 2016
+++ src/sys/arch/amd64/amd64/machdep.c	Sat Jul 16 17:02:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.223 2016/07/13 15:53:26 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.224 2016/07/16 17:02:34 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -111,7 +111,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.223 2016/07/13 15:53:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.224 2016/07/16 17:02:34 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1537,10 +1537,6 @@ init_x86_64(paddr_t first_avail)
 	int x;
 #ifndef XEN
 	int ist;
-	extern struct extent *iomem_ex;
-#if !defined(REALEXTMEM) && !defined(REALBASEMEM)
-	struct btinfo_memmap *bim;
-#endif
 #endif /* !XEN */
 
 #ifdef XEN
@@ -1589,23 +1585,8 @@ init_x86_64(paddr_t first_avail)
 	 */
 	avail_start = 8 * PAGE_SIZE;
 
-#if !defined(REALBASEMEM) && !defined(REALEXTMEM)
-	/*
-	 * Check to see if we have a memory map from the BIOS (passed to us by
-	 * the boot program).
-	 */
-	bim = lookup_bootinfo(BTINFO_MEMMAP);
-	if (bim != NULL && bim->num > 0)
-		initx86_parse_memmap(bim, iomem_ex);
-#endif	/* ! REALBASEMEM && ! REALEXTMEM */
-
-	/*
-	 * If initx86_parse_memmap didn't find any valid segment, fall back to
-	 * former code.
-	 */
-	if (mem_cluster_cnt == 0)
-		initx86_fake_memmap(iomem_ex);
-
+	/* Initialize the memory clusters (needed in pmap_boostrap). */
+	init_x86_clusters();
 #else	/* XEN */
 	/* Parse Xen command line (replace bootinfo) */
 	xen_parse_cmdline(XEN_PARSE_BOOTFLAGS, NULL);
@@ -1629,7 +1610,8 @@ init_x86_64(paddr_t first_avail)
 		pmap_prealloc_lowmem_ptps();
 
 #ifndef XEN
-	initx86_load_memmap(first_avail);
+	/* Internalize the physical pages into the VM system. */
+	init_x86_vm(first_avail);
 #else	/* XEN */
 	kern_end = KERNBASE + first_avail;
 	physmem = xen_start_info.nr_pages;

Index: src/sys/arch/i386/i386/machdep.c
diff -u src/sys/arch/i386/i386/machdep.c:1.758 src/sys/arch/i386/i386/machdep.c:1.759
--- src/sys/arch/i386/i386/machdep.c:1.758	Wed Jul 13 15:53:27 2016
+++ src/sys/arch/i386/i386/machdep.c	Sat Jul 16 17:02:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.758 2016/07/13 15:53:27 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.759 2016/07/16 17:02:34 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009
@@ -67,7 +67,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.758 2016/07/13 15:53:27 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.759 2016/07/16 17:02:34 maxv Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_ibcs2.h"
@@ -1129,9 +1129,7 @@ init386(paddr_t first_avail)
 	int x;
 #ifndef XEN
 	union descriptor *tgdt;
-	extern struct extent *iomem_ex;
 	struct region_descriptor region;
-	struct btinfo_memmap *bim;
 #endif
 #if NBIOSCALL > 0
 	extern int biostramp_image_size;
@@ -1246,24 +1244,11 @@ init386(paddr_t first_avail)
 	pmap_bootstrap((vaddr_t)atdevbase + IOM_SIZE);
 
 #ifndef XEN
-	/*
-	 * Check to see if we have a memory map from the BIOS (passed to us by
-	 * the boot program).
-	 */
-	bim = lookup_bootinfo(BTINFO_MEMMAP);
-	if ((biosmem_implicit || (biosbasemem == 0 && biosextmem == 0)) &&
-	bim != NULL && bim->num > 0)
-		initx86_parse_memmap(bim, iomem_ex);
-
-	/*
-	 * If initx86_parse_memmap didn't find any valid segment, fall back to
-	 * former code.
-	 */
-	if (mem_cluster_cnt == 0)
-		initx86_fake_memmap(iomem_ex);
-
-	initx86_load_memmap(first_avail);
+	/* Initialize the memory clusters. */
+	init_x86_clusters();
 
+	/* Internalize the physical pages into the VM system. */
+	init_x86_vm(first_avail);
 #else /* !XEN */
 	XENPRINTK(("load the memory cluster 0x%" PRIx64 " (%" PRId64 ") - "
 	"0x%" PRIx64 " (%" PRId64 ")\n",

Index: src/sys/arch/x86/include/machdep.h
diff -u src/sys/arch/x86/include/machdep.h:1.7 

CVS commit: src/sys/arch/x86/x86

2016-07-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jul 16 14:51:45 UTC 2016

Modified Files:
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
Introduce x86_load_region(), and explain a little what we are doing.


To generate a diff of this commit:
cvs rdiff -u -r1.70 -r1.71 src/sys/arch/x86/x86/x86_machdep.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/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.70 src/sys/arch/x86/x86/x86_machdep.c:1.71
--- src/sys/arch/x86/x86/x86_machdep.c:1.70	Thu Jan 28 06:52:55 2016
+++ src/sys/arch/x86/x86/x86_machdep.c	Sat Jul 16 14:51:45 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.70 2016/01/28 06:52:55 jnemeth Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.71 2016/07/16 14:51:45 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.70 2016/01/28 06:52:55 jnemeth Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.71 2016/07/16 14:51:45 maxv Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -773,6 +773,49 @@ x86_select_freelist(uint64_t maxaddr)
 	panic("no freelist for maximum address %"PRIx64, maxaddr);
 }
 
+/*
+ * Load the physical memory region from seg_start to seg_end into the VM
+ * system.
+ */
+static void
+x86_load_region(uint64_t seg_start, uint64_t seg_end)
+{
+	unsigned int i;
+	uint64_t tmp;
+
+	i = __arraycount(x86_freelists);
+	while (i--) {
+		if (x86_freelists[i].limit <= seg_start)
+			continue;
+		if (x86_freelists[i].freelist == VM_FREELIST_DEFAULT)
+			continue;
+		tmp = MIN(x86_freelists[i].limit, seg_end);
+		if (tmp == seg_start)
+			continue;
+
+#ifdef DEBUG_MEMLOAD
+		printf("loading freelist %d 0x%"PRIx64"-0x%"PRIx64
+		" (0x%"PRIx64"-0x%"PRIx64")\n", x86_freelists[i].freelist,
+		seg_start, tmp, (uint64_t)atop(seg_start),
+		(uint64_t)atop(tmp));
+#endif
+
+		uvm_page_physload(atop(seg_start), atop(tmp), atop(seg_start),
+		atop(tmp), x86_freelists[i].freelist);
+		seg_start = tmp;
+	}
+
+	if (seg_start != seg_end) {
+#ifdef DEBUG_MEMLOAD
+		printf("loading default 0x%"PRIx64"-0x%"PRIx64
+		" (0x%"PRIx64"-0x%"PRIx64")\n", seg_start, seg_end,
+		(uint64_t)atop(seg_start), (uint64_t)atop(seg_end));
+#endif
+		uvm_page_physload(atop(seg_start), atop(seg_end),
+		atop(seg_start), atop(seg_end), VM_FREELIST_DEFAULT);
+	}
+}
+
 int
 initx86_load_memmap(paddr_t first_avail)
 {
@@ -796,11 +839,11 @@ initx86_load_memmap(paddr_t first_avail)
 #endif
 
 	/*
-	 * Now, load the memory clusters (which have already been
-	 * rounded and truncated) into the VM system.
+	 * Now, load the memory clusters (which have already been rounded and
+	 * truncated) into the VM system.
 	 *
-	 * NOTE: WE ASSUME THAT MEMORY STARTS AT 0 AND THAT THE KERNEL
-	 * IS LOADED AT IOM_END (1M).
+	 * NOTE: we assume that memory starts at 0 and that the kernel is
+	 * loaded at IOM_END (1MB).
 	 */
 	for (x = 0; x < mem_cluster_cnt; x++) {
 		const phys_ram_seg_t *cluster = _clusters[x];
@@ -810,13 +853,11 @@ initx86_load_memmap(paddr_t first_avail)
 		seg_start1 = 0;
 		seg_end1 = 0;
 
-		/*
-		 * Skip memory before our available starting point.
-		 */
+		/* Skip memory before our available starting point. */
 		if (seg_end <= avail_start)
 			continue;
 
-		if (avail_start >= seg_start && avail_start < seg_end) {
+		if (seg_start <= avail_start && avail_start < seg_end) {
 			if (seg_start != 0)
 panic("init_x86_64: memory doesn't start at 0");
 			seg_start = avail_start;
@@ -825,8 +866,8 @@ initx86_load_memmap(paddr_t first_avail)
 		}
 
 		/*
-		 * If this segment contains the kernel, split it
-		 * in two, around the kernel.
+		 * If this segment contains the kernel, split it in two, around
+		 * the kernel.
 		 */
 		if (seg_start <= IOM_END && first_avail <= seg_end) {
 			seg_start1 = first_avail;
@@ -837,92 +878,19 @@ initx86_load_memmap(paddr_t first_avail)
 
 		/* First hunk */
 		if (seg_start != seg_end) {
-			i = __arraycount(x86_freelists);
-			while (i--) {
-uint64_t tmp;
-
-if (x86_freelists[i].limit <= seg_start)
-	continue;
-if (x86_freelists[i].freelist ==
-VM_FREELIST_DEFAULT)
-	continue;
-tmp = MIN(x86_freelists[i].limit, seg_end);
-if (tmp == seg_start)
-	continue;
-#ifdef DEBUG_MEMLOAD
-printf("loading freelist %d"
-" 0x%"PRIx64"-0x%"PRIx64
-" (0x%"PRIx64"-0x%"PRIx64")\n",
-x86_freelists[i].freelist, seg_start, tmp,
-(uint64_t)atop(seg_start),
-(uint64_t)atop(tmp));
-#endif
-uvm_page_physload(atop(seg_start), atop(tmp),
-atop(seg_start), atop(tmp),
-x86_freelists[i].freelist);
-seg_start = tmp;
-			}
-
-			if (seg_start != seg_end) {
-#ifdef DEBUG_MEMLOAD
-printf("loading default 0x%"PRIx64"-0x%"PRIx64
-" (0x%"PRIx64"-0x%"PRIx64")\n",
-

CVS commit: src/sys/arch/x86/include

2016-07-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jul 16 13:47:01 UTC 2016

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Add the cr4 flags for PKE and UMIP.


To generate a diff of this commit:
cvs rdiff -u -r1.87 -r1.88 src/sys/arch/x86/include/specialreg.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/arch/x86/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.87 src/sys/arch/x86/include/specialreg.h:1.88
--- src/sys/arch/x86/include/specialreg.h:1.87	Wed Apr 27 08:51:32 2016
+++ src/sys/arch/x86/include/specialreg.h	Sat Jul 16 13:47:01 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.87 2016/04/27 08:51:32 msaitoh Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.88 2016/07/16 13:47:01 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1991 The Regents of the University of California.
@@ -81,6 +81,7 @@
 #define CR4_PCE		0x0100 /* enable RDPMC instruction for all cpls */
 #define CR4_OSFXSR	0x0200 /* enable fxsave/fxrestor and SSE */
 #define CR4_OSXMMEXCPT	0x0400 /* enable unmasked SSE exceptions */
+#define CR4_UMIP	0x0800 /* user-mode instruction prevention */
 #define CR4_VMXE	0x2000 /* enable VMX operations */
 #define CR4_SMXE	0x4000 /* enable SMX operations */
 #define CR4_FSGSBASE	0x0001 /* enable *FSBASE and *GSBASE instructions */
@@ -88,6 +89,7 @@
 #define CR4_OSXSAVE	0x0004 /* enable xsave and xrestore */
 #define CR4_SMEP	0x0010 /* enable SMEP support */
 #define CR4_SMAP	0x0020 /* enable SMAP support */
+#define CR4_PKE		0x0040 /* protection key enable */
 
 /*
  * Extended Control Register XCR0



CVS commit: src/lib/libpthread

2016-07-16 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Sat Jul 16 12:58:12 UTC 2016

Modified Files:
src/lib/libpthread: pthread_mutex.c

Log Message:
KNF


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 src/lib/libpthread/pthread_mutex.c

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

Modified files:

Index: src/lib/libpthread/pthread_mutex.c
diff -u src/lib/libpthread/pthread_mutex.c:1.60 src/lib/libpthread/pthread_mutex.c:1.61
--- src/lib/libpthread/pthread_mutex.c:1.60	Sun Jul  3 14:24:58 2016
+++ src/lib/libpthread/pthread_mutex.c	Sat Jul 16 12:58:11 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_mutex.c,v 1.60 2016/07/03 14:24:58 christos Exp $	*/
+/*	$NetBSD: pthread_mutex.c,v 1.61 2016/07/16 12:58:11 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -47,7 +47,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread_mutex.c,v 1.60 2016/07/03 14:24:58 christos Exp $");
+__RCSID("$NetBSD: pthread_mutex.c,v 1.61 2016/07/16 12:58:11 skrll Exp $");
 
 #include 
 #include 
@@ -794,7 +794,7 @@ pthread__mutex_deferwake(pthread_t self,
 }
 
 int
-pthread_mutex_getprioceiling(const pthread_mutex_t *ptm, int*ceil) 
+pthread_mutex_getprioceiling(const pthread_mutex_t *ptm, int *ceil) 
 {
 	*ceil = (unsigned int)ptm->ptm_ceiling;
 	return 0;



CVS commit: [pgoyette-localcount] src/external/cddl/osnet

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 11:27:12 UTC 2016

Modified Files:
src/external/cddl/osnet/dev/fbt [pgoyette-localcount]: fbt.c
src/external/cddl/osnet/dev/sdt [pgoyette-localcount]: sdt.c
src/external/cddl/osnet/dist/uts/common/dtrace [pgoyette-localcount]:
dtrace.c

Log Message:
Add an initializer for the cdevsw's localcount.

XXX These should maybe be converted to C99-style structure initialization?


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.18.2.1 src/external/cddl/osnet/dev/fbt/fbt.c
cvs rdiff -u -r1.16 -r1.16.2.1 src/external/cddl/osnet/dev/sdt/sdt.c
cvs rdiff -u -r1.32 -r1.32.2.1 \
src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c

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

Modified files:

Index: src/external/cddl/osnet/dev/fbt/fbt.c
diff -u src/external/cddl/osnet/dev/fbt/fbt.c:1.18 src/external/cddl/osnet/dev/fbt/fbt.c:1.18.2.1
--- src/external/cddl/osnet/dev/fbt/fbt.c:1.18	Thu Feb 26 10:31:52 2015
+++ src/external/cddl/osnet/dev/fbt/fbt.c	Sat Jul 16 11:27:12 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: fbt.c,v 1.18 2015/02/26 10:31:52 ozaki-r Exp $	*/
+/*	$NetBSD: fbt.c,v 1.18.2.1 2016/07/16 11:27:12 pgoyette Exp $	*/
 
 /*
  * CDDL HEADER START
@@ -154,7 +154,7 @@ static void	fbt_resume(void *, dtrace_id
 static const struct cdevsw fbt_cdevsw = {
 	fbt_open, noclose, noread, nowrite, noioctl,
 	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER
+	D_OTHER, NULL
 };
 
 static dtrace_pattr_t fbt_attr = {

Index: src/external/cddl/osnet/dev/sdt/sdt.c
diff -u src/external/cddl/osnet/dev/sdt/sdt.c:1.16 src/external/cddl/osnet/dev/sdt/sdt.c:1.16.2.1
--- src/external/cddl/osnet/dev/sdt/sdt.c:1.16	Thu Jun 16 06:08:06 2016
+++ src/external/cddl/osnet/dev/sdt/sdt.c	Sat Jul 16 11:27:12 2016
@@ -39,7 +39,7 @@
  * unloaded; in particular, probes may not span multiple kernel modules.
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.16 2016/06/16 06:08:06 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.16.2.1 2016/07/16 11:27:12 pgoyette Exp $");
 
 #include 
 #include 
@@ -121,7 +121,7 @@ sdt_open(dev_t dev, int flags, int mode,
 static const struct cdevsw sdt_cdevsw = {
 	sdt_open, noclose, noread, nowrite, noioctl,
 	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER
+	D_OTHER, NULL
 };
 #endif
 

Index: src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c
diff -u src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32 src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32.2.1
--- src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c:1.32	Thu Jun 23 04:35:35 2016
+++ src/external/cddl/osnet/dist/uts/common/dtrace/dtrace.c	Sat Jul 16 11:27:12 2016
@@ -15340,7 +15340,7 @@ static dev_type_open(dtrace_open);
 const struct cdevsw dtrace_cdevsw = {
 	dtrace_open, noclose, noread, nowrite, noioctl,
 	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
-	D_OTHER | D_MPSAFE
+	D_OTHER | D_MPSAFE, NULL
 };
 
 static int dtrace_ioctl(struct file *fp, u_long cmd, void *data);



CVS commit: [pgoyette-localcount] src/sys/rump/librump/rumpkern

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 11:07:00 UTC 2016

Modified Files:
src/sys/rump/librump/rumpkern [pgoyette-localcount]: Makefile.rumpkern

Log Message:
Make sure we include the localcount routines in the rump libraries


To generate a diff of this commit:
cvs rdiff -u -r1.167 -r1.167.2.1 \
src/sys/rump/librump/rumpkern/Makefile.rumpkern

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

Modified files:

Index: src/sys/rump/librump/rumpkern/Makefile.rumpkern
diff -u src/sys/rump/librump/rumpkern/Makefile.rumpkern:1.167 src/sys/rump/librump/rumpkern/Makefile.rumpkern:1.167.2.1
--- src/sys/rump/librump/rumpkern/Makefile.rumpkern:1.167	Mon Apr 11 06:49:11 2016
+++ src/sys/rump/librump/rumpkern/Makefile.rumpkern	Sat Jul 16 11:06:59 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.rumpkern,v 1.167 2016/04/11 06:49:11 ozaki-r Exp $
+#	$NetBSD: Makefile.rumpkern,v 1.167.2.1 2016/07/16 11:06:59 pgoyette Exp $
 #
 
 .include "${RUMPTOP}/Makefile.rump"
@@ -108,6 +108,7 @@ SRCS+=	init_sysctl_base.c	\
 	subr_kcpuset.c		\
 	subr_kmem.c		\
 	subr_kobj.c		\
+	subr_localcount.c	\
 	subr_log.c		\
 	subr_lwp_specificdata.c	\
 	subr_once.c		\



CVS commit: src/lib/libc/arch/hppa/gen

2016-07-16 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Sat Jul 16 09:51:13 UTC 2016

Modified Files:
src/lib/libc/arch/hppa/gen: __setjmp14.S

Log Message:
No need to define PSW_MBS as machine/psl.h provides it for us


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/lib/libc/arch/hppa/gen/__setjmp14.S

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/arch/hppa/gen/__setjmp14.S
diff -u src/lib/libc/arch/hppa/gen/__setjmp14.S:1.5 src/lib/libc/arch/hppa/gen/__setjmp14.S:1.6
--- src/lib/libc/arch/hppa/gen/__setjmp14.S:1.5	Mon Apr 28 20:22:55 2008
+++ src/lib/libc/arch/hppa/gen/__setjmp14.S	Sat Jul 16 09:51:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: __setjmp14.S,v 1.5 2008/04/28 20:22:55 martin Exp $	*/
+/*	$NetBSD: __setjmp14.S,v 1.6 2016/07/16 09:51:13 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 #include 
 
 #if defined(LIBC_SCCS) && !defined(lint)
-	RCSID("$NetBSD: __setjmp14.S,v 1.5 2008/04/28 20:22:55 martin Exp $")
+	RCSID("$NetBSD: __setjmp14.S,v 1.6 2016/07/16 09:51:13 skrll Exp $")
 #endif /* LIBC_SCCS and not lint */
 
 /*
@@ -66,7 +66,6 @@ ENTRY(__setjmp14,0)
 	stw	%r1, 28(%arg0)		; sc.sc_pcsqh = %sr0
 	ldo	4(%rp), %r1
 	stw	%r1, 32(%arg0)		; sc.sc_pcoqt = %rp + 4
-#define PSW_MBS (PSW_C|PSW_Q|PSW_P|PSW_D|PSW_I)
 	ldil	L%PSW_MBS, %r1
 	ldo	R%PSW_MBS(%r1), %r1
 	stw	%r1, 36(%arg0)		; set sc.sc_ps



CVS commit: [pgoyette-localcount] src

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 09:49:24 UTC 2016

Modified Files:
src/distrib/sets/lists/comp [pgoyette-localcount]: mi
src/share/man/man9 [pgoyette-localcount]: Makefile

Log Message:
Add MLINKS for localcount(9)


To generate a diff of this commit:
cvs rdiff -u -r1.2051.2.1 -r1.2051.2.2 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.396.2.1 -r1.396.2.2 src/share/man/man9/Makefile

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

Modified files:

Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.2051.2.1 src/distrib/sets/lists/comp/mi:1.2051.2.2
--- src/distrib/sets/lists/comp/mi:1.2051.2.1	Sat Jul 16 09:41:07 2016
+++ src/distrib/sets/lists/comp/mi	Sat Jul 16 09:49:24 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: mi,v 1.2051.2.1 2016/07/16 09:41:07 pgoyette Exp $
+#	$NetBSD: mi,v 1.2051.2.2 2016/07/16 09:49:24 pgoyette Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.compcomp-sys-root
@@ -10462,6 +10462,11 @@
 ./usr/share/man/cat9/le64toh.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/linedisc.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/localcount.0		comp-sys-catman		.cat
+./usr/share/man/cat9/localcount_init.0		comp-sys-catman		.cat
+./usr/share/man/cat9/localcount_fini.0		comp-sys-catman		.cat
+./usr/share/man/cat9/localcount_acquire.0	comp-sys-catman		.cat
+./usr/share/man/cat9/localcount_release.0	comp-sys-catman		.cat
+./usr/share/man/cat9/localcount_drain.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/lock.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/lockinit.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/lockmgr.0			comp-sys-catman		.cat
@@ -17616,6 +17621,11 @@
 ./usr/share/man/html9/le64toh.html		comp-sys-htmlman	html
 ./usr/share/man/html9/linedisc.html		comp-sys-htmlman	html
 ./usr/share/man/html9/localcount.html		comp-sys-htmlman	html
+./usr/share/man/html9/localcount_init.html	comp-sys-htmlman	html
+./usr/share/man/html9/localcount_fini.html	comp-sys-htmlman	html
+./usr/share/man/html9/localcount_acquire.html	comp-sys-htmlman	html
+./usr/share/man/html9/localcount_release.html	comp-sys-htmlman	html
+./usr/share/man/html9/localcount_drain.html	comp-sys-htmlman	html
 ./usr/share/man/html9/lock.html			comp-sys-htmlman	html
 ./usr/share/man/html9/lockinit.html		comp-sys-htmlman	html
 ./usr/share/man/html9/lockmgr.html		comp-sys-htmlman	html
@@ -24924,6 +24934,11 @@
 ./usr/share/man/man9/le64toh.9			comp-sys-man		.man
 ./usr/share/man/man9/linedisc.9			comp-sys-man		.man
 ./usr/share/man/man9/localcount.9		comp-sys-man		.man
+./usr/share/man/man9/localcount_init.9		comp-sys-man		.man
+./usr/share/man/man9/localcount_fini.9		comp-sys-man		.man
+./usr/share/man/man9/localcount_acquire.9	comp-sys-man		.man
+./usr/share/man/man9/localcount_release.9	comp-sys-man		.man
+./usr/share/man/man9/localcount_drain.9		comp-sys-man		.man
 ./usr/share/man/man9/lock.9			comp-sys-man		.man
 ./usr/share/man/man9/lockinit.9			comp-sys-man		.man
 ./usr/share/man/man9/lockmgr.9			comp-sys-man		.man

Index: src/share/man/man9/Makefile
diff -u src/share/man/man9/Makefile:1.396.2.1 src/share/man/man9/Makefile:1.396.2.2
--- src/share/man/man9/Makefile:1.396.2.1	Sat Jul 16 09:36:57 2016
+++ src/share/man/man9/Makefile	Sat Jul 16 09:49:24 2016
@@ -1,4 +1,4 @@
-#   $NetBSD: Makefile,v 1.396.2.1 2016/07/16 09:36:57 pgoyette Exp $
+#   $NetBSD: Makefile,v 1.396.2.2 2016/07/16 09:49:24 pgoyette Exp $
 
 #	Makefile for section 9 (kernel function and variable) manual pages.
 
@@ -455,6 +455,11 @@ MLINKS+=kthread.9 kthread_exit.9 \
 MLINKS+=linedisc.9 ttyldisc_add.9 \
 	linedisc.9 ttyldisc_lookup.9 \
 	linedisc.9 ttyldisc_remove.9
+MLINKS+=localcount.9 localcount_init.9 \
+	localcount.9 localcount_fini.9 \
+	localcount.9 localcount_acquire.9 \
+	localcount.9 localcount_release.9 \
+	localcount.9 localcount_drain.9
 MLINKS+=lock.9 simple_lock_init.9 lock.9 simple_lock.9 \
 	lock.9 simple_lock_try.9 lock.9 simple_unlock.9 \
 	lock.9 simple_lock_freecheck.9 lock.9 simple_lock_dump.9  \



CVS commit: [pgoyette-localcount] src/distrib/sets/lists/comp

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 09:41:08 UTC 2016

Modified Files:
src/distrib/sets/lists/comp [pgoyette-localcount]: mi

Log Message:
Add localcount(9) man pages and header file

XXX Man-page links not yet added!


To generate a diff of this commit:
cvs rdiff -u -r1.2051 -r1.2051.2.1 src/distrib/sets/lists/comp/mi

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

Modified files:

Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.2051 src/distrib/sets/lists/comp/mi:1.2051.2.1
--- src/distrib/sets/lists/comp/mi:1.2051	Tue Jul 12 03:39:55 2016
+++ src/distrib/sets/lists/comp/mi	Sat Jul 16 09:41:07 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: mi,v 1.2051 2016/07/12 03:39:55 knakahara Exp $
+#	$NetBSD: mi,v 1.2051.2.1 2016/07/16 09:41:07 pgoyette Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.compcomp-sys-root
@@ -2860,6 +2860,7 @@
 ./usr/include/sys/ksyms.h			comp-c-include
 ./usr/include/sys/ktrace.h			comp-c-include
 ./usr/include/sys/lkm.hcomp-obsolete		obsolete
+./usr/include/sys/localcount.h			comp-c-include
 ./usr/include/sys/localedef.h			comp-c-include
 ./usr/include/sys/lock.h			comp-c-include
 ./usr/include/sys/lockf.h			comp-c-include
@@ -10460,6 +10461,7 @@
 ./usr/share/man/cat9/le64enc.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/le64toh.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/linedisc.0			comp-sys-catman		.cat
+./usr/share/man/cat9/localcount.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/lock.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/lockinit.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/lockmgr.0			comp-sys-catman		.cat
@@ -17613,6 +17615,7 @@
 ./usr/share/man/html9/le64enc.html		comp-sys-htmlman	html
 ./usr/share/man/html9/le64toh.html		comp-sys-htmlman	html
 ./usr/share/man/html9/linedisc.html		comp-sys-htmlman	html
+./usr/share/man/html9/localcount.html		comp-sys-htmlman	html
 ./usr/share/man/html9/lock.html			comp-sys-htmlman	html
 ./usr/share/man/html9/lockinit.html		comp-sys-htmlman	html
 ./usr/share/man/html9/lockmgr.html		comp-sys-htmlman	html
@@ -24920,6 +24923,7 @@
 ./usr/share/man/man9/le64enc.9			comp-sys-man		.man
 ./usr/share/man/man9/le64toh.9			comp-sys-man		.man
 ./usr/share/man/man9/linedisc.9			comp-sys-man		.man
+./usr/share/man/man9/localcount.9		comp-sys-man		.man
 ./usr/share/man/man9/lock.9			comp-sys-man		.man
 ./usr/share/man/man9/lockinit.9			comp-sys-man		.man
 ./usr/share/man/man9/lockmgr.9			comp-sys-man		.man



CVS commit: [pgoyette-localcount] src/share/man/man9

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 09:36:57 UTC 2016

Modified Files:
src/share/man/man9 [pgoyette-localcount]: Makefile
Added Files:
src/share/man/man9 [pgoyette-localcount]: localcount.9

Log Message:
Initial man page for localcount(9).


To generate a diff of this commit:
cvs rdiff -u -r1.396 -r1.396.2.1 src/share/man/man9/Makefile
cvs rdiff -u -r0 -r1.1.2.1 src/share/man/man9/localcount.9

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

Modified files:

Index: src/share/man/man9/Makefile
diff -u src/share/man/man9/Makefile:1.396 src/share/man/man9/Makefile:1.396.2.1
--- src/share/man/man9/Makefile:1.396	Tue Jul 12 03:39:55 2016
+++ src/share/man/man9/Makefile	Sat Jul 16 09:36:57 2016
@@ -1,4 +1,4 @@
-#   $NetBSD: Makefile,v 1.396 2016/07/12 03:39:55 knakahara Exp $
+#   $NetBSD: Makefile,v 1.396.2.1 2016/07/16 09:36:57 pgoyette Exp $
 
 #	Makefile for section 9 (kernel function and variable) manual pages.
 
@@ -31,7 +31,7 @@ MAN=	accept_filter.9 accf_data.9 accf_ht
 	ioctl.9 ipkdb.9 ipi.9 isa.9 isapnp.9 itimerfix.9 kauth.9 kcopy.9 \
 	kcpuset.9 kernhist.9 kmem.9 kpause.9 \
 	kfilter_register.9 knote.9 \
-	kprintf.9 kthread.9 linedisc.9 lock.9 log.9 ltsleep.9 \
+	kprintf.9 kthread.9 linedisc.9 localcount.9 lock.9 log.9 ltsleep.9 \
 	LWP_CACHE_CREDS.9 \
 	makeiplcookie.9 \
 	malloc.9 mb.9 mbuf.9 mca.9 memcmp.9 memcpy.9 memoryallocators.9 \

Added files:

Index: src/share/man/man9/localcount.9
diff -u /dev/null src/share/man/man9/localcount.9:1.1.2.1
--- /dev/null	Sat Jul 16 09:36:57 2016
+++ src/share/man/man9/localcount.9	Sat Jul 16 09:36:57 2016
@@ -0,0 +1,158 @@
+.\"	$NetBSD: localcount.9,v 1.1.2.1 2016/07/16 09:36:57 pgoyette Exp $
+.\"
+.\" Copyright (c) 2016
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Taylor R. Campbell.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"notice, this list of conditions and the following disclaimer in the
+.\"documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd July xx, 2016
+.Dt LOCALCOUNT 9
+.Os
+.Sh NAME
+.Nm localcount ,
+.Nm localcount_init ,
+.Nm localcount_fini ,
+.Nm localcount_acquire ,
+.Nm localcount_release ,
+.Nm localcount_drain ,
+.Sh SYNOPSIS
+.In sys/localcount.h
+.Ft int
+.Fn localcount_init "struct localcount *lc"
+.Ft void
+.Fn localcount_fini "struct localcount *lc"
+.Ft void
+.Fn localcount_acquire "struct localcount *lc"
+.Ft void
+.Fn localcount_release "struct localcount *lc" "struct kcondvar *cv" \
+"struct kmutex *mtx"
+.Ft void
+.Fn localcount_drain "struct localcount *lc" "struct kcondvar *cv" \
+"struct kmutex *mtx"
+.Sh DESCRIPTION
+Localcounts are used in the kernel to implement a medium-weight reference
+counting mechanism.
+During normal operations, localcounts do not need the interprocessor
+synchronization associated with
+.Xr atomic_ops 3
+atomic memory operations, and (unlike
+.Xr psref 9 )
+.Nm
+references can be held across sleeps and can migrate between CPUs.
+Draining a
+.Nm localcount
+requires more expensive interprocessor synchronization than
+.Xr atomic_ops 3
+(similar to
+.Xr psref 9 ) .
+And
+.Nm
+references require eight bytes of memory per object per-CPU, significantly
+more than
+.Xr atomic_ops 3
+and almost always more than
+.Xr psref 9 .
+.Sh FUNCTIONS
+.Bl -tag -width abcd
+.It Fn localcount_init "lc"
+.Pp
+Dynamically initialize a localcount for use.
+.Pp
+No other operations can be performed on a localcount until it has been
+initialized.
+.It Fn localcount_fini "lc"
+.Pp
+Release resources used by a localcount.
+The caller must have already called
+.fn localcount_drain .
+The localcount may not be used after
+.Fn localcount_fini
+has been called until it 

CVS commit: [pgoyette-localcount] src/sys

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 07:54:14 UTC 2016

Modified Files:
src/sys/kern [pgoyette-localcount]: subr_devsw.c
src/sys/sys [pgoyette-localcount]: conf.h

Log Message:
First pass of adding localcount(9) support for devsw.  As with the
earlier autoconf changes, nothing currently uses this new feature.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.34.2.1 src/sys/kern/subr_devsw.c
cvs rdiff -u -r1.146 -r1.146.2.1 src/sys/sys/conf.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/kern/subr_devsw.c
diff -u src/sys/kern/subr_devsw.c:1.34 src/sys/kern/subr_devsw.c:1.34.2.1
--- src/sys/kern/subr_devsw.c:1.34	Mon Feb  1 05:05:43 2016
+++ src/sys/kern/subr_devsw.c	Sat Jul 16 07:54:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_devsw.c,v 1.34 2016/02/01 05:05:43 riz Exp $	*/
+/*	$NetBSD: subr_devsw.c,v 1.34.2.1 2016/07/16 07:54:13 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34 2016/02/01 05:05:43 riz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.1 2016/07/16 07:54:13 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dtrace.h"
@@ -85,6 +85,8 @@ __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #ifdef DEVSW_DEBUG
 #define	DPRINTF(x)	printf x
@@ -107,7 +109,8 @@ static int bdevsw_attach(const struct bd
 static int cdevsw_attach(const struct cdevsw *, devmajor_t *);
 static void devsw_detach_locked(const struct bdevsw *, const struct cdevsw *);
 
-kmutex_t device_lock;
+kmutex_t	device_lock;
+kcondvar_t	device_cv;
 
 void (*biodone_vfs)(buf_t *) = (void *)nullop;
 
@@ -118,6 +121,7 @@ devsw_init(void)
 	KASSERT(sys_bdevsws < MAXDEVSW - 1);
 	KASSERT(sys_cdevsws < MAXDEVSW - 1);
 	mutex_init(_lock, MUTEX_DEFAULT, IPL_NONE);
+	cv_init(_cv, "devsw");
 }
 
 int
@@ -160,8 +164,13 @@ devsw_attach(const char *devname,
 			goto fail;
 		}
 
-		if (bdev != NULL)
+		if (bdev != NULL) {
+			KASSERT(bdev->d_localcount != NULL);
+			localcount_init(bdev->d_localcount);
 			bdevsw[*bmajor] = bdev;
+		}
+		KASSERT(cdev->d_localcount != NULL);
+		localcount_init(cdev->d_localcount);
 		cdevsw[*cmajor] = cdev;
 
 		mutex_exit(_lock);
@@ -270,6 +279,8 @@ bdevsw_attach(const struct bdevsw *devsw
 		return (EEXIST);
 
 	bdevsw[*devmajor] = devsw;
+	KASSERT(devsw->d_localcount != NULL);
+	localcount_init(devsw->d_localcount);
 
 	return (0);
 }
@@ -317,33 +328,66 @@ cdevsw_attach(const struct cdevsw *devsw
 		return (EEXIST);
 
 	cdevsw[*devmajor] = devsw;
+	KASSERT(devsw->d_localcount != NULL);
+	localcount_init(devsw->d_localcount);
 
 	return (0);
 }
 
+/*
+ * First, look up both bdev and cdev indices, and confirm that the
+ * localcount pointer(s) exist.  Then drain any existing references,
+ * deactivate the localcount(s).  Finally, remove the {b,c}devsw[]
+ * entries.
+ */
+
 static void
 devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
 {
-	int i;
+	int i, j;
 
 	KASSERT(mutex_owned(_lock));
 
+	i = max_bdevsws;
 	if (bdev != NULL) {
 		for (i = 0 ; i < max_bdevsws ; i++) {
 			if (bdevsw[i] != bdev)
 continue;
-			bdevsw[i] = NULL;
+
+			KASSERTMSG(bdev->d_localcount != NULL,
+			"%s: no bdev localcount", __func__);
 			break;
 		}
 	}
+	j = max_cdevsws;
 	if (cdev != NULL) {
-		for (i = 0 ; i < max_cdevsws ; i++) {
-			if (cdevsw[i] != cdev)
+		for (j = 0 ; j < max_cdevsws ; j++) {
+			if (cdevsw[j] != cdev)
 continue;
-			cdevsw[i] = NULL;
+
+			KASSERTMSG(cdev->d_localcount != NULL,
+			"%s: no cdev localcount", __func__);
 			break;
 		}
 	}
+	if (i < max_bdevsws) {
+		localcount_drain(bdev->d_localcount, _cv, _lock);
+		localcount_fini(bdev->d_localcount);
+		bdevsw[i] = NULL;
+	}
+	if (j < max_cdevsws ) {
+		/*
+		 * Take care not to drain/fini the d_localcount if the same
+		 * one was used for both cdev and bdev!
+		 */
+		if (i >= max_bdevsws ||
+		bdev->d_localcount != cdev->d_localcount) {
+			localcount_drain(cdev->d_localcount, _cv,
+			_lock);
+			localcount_fini(cdev->d_localcount);
+		}
+		cdevsw[j] = NULL;
+	}
 }
 
 int
@@ -375,6 +419,35 @@ bdevsw_lookup(dev_t dev)
 	return (bdevsw[bmajor]);
 }
 
+const struct bdevsw *
+bdevsw_lookup_acquire(dev_t dev)
+{
+	devmajor_t bmajor;
+
+	if (dev == NODEV)
+		return (NULL);
+	bmajor = major(dev);
+	if (bmajor < 0 || bmajor >= max_bdevsws)
+		return (NULL);
+
+	if (bdevsw[bmajor]->d_localcount != NULL)
+		localcount_acquire(bdevsw[bmajor]->d_localcount);
+
+	return (bdevsw[bmajor]);
+}
+
+void
+bdevsw_release(const struct bdevsw *bd)
+{
+	devmajor_t bmaj;
+
+	bmaj = bdevsw_lookup_major(bd);
+
+	KASSERTMSG(bmaj != NODEVMAJOR, "%s: no bmajor to release!", __func__);
+	if (bd->d_localcount != NULL)
+		localcount_release(bd->d_localcount, _cv, _lock);
+}
+
 /*
  * Look up 

CVS commit: src/usr.bin/config

2016-07-16 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jul 16 07:13:26 UTC 2016

Modified Files:
src/usr.bin/config: mkdevsw.c

Log Message:
Only emit bdevsw externs for entries in the bdev table, rather than for
entries in the cdev table.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/usr.bin/config/mkdevsw.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/config/mkdevsw.c
diff -u src/usr.bin/config/mkdevsw.c:1.14 src/usr.bin/config/mkdevsw.c:1.15
--- src/usr.bin/config/mkdevsw.c:1.14	Thu Sep  3 13:53:36 2015
+++ src/usr.bin/config/mkdevsw.c	Sat Jul 16 07:13:26 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: mkdevsw.c,v 1.14 2015/09/03 13:53:36 uebayasi Exp $	*/
+/*	$NetBSD: mkdevsw.c,v 1.15 2016/07/16 07:13:26 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
 #endif
 
 #include 
-__RCSID("$NetBSD: mkdevsw.c,v 1.14 2015/09/03 13:53:36 uebayasi Exp $");
+__RCSID("$NetBSD: mkdevsw.c,v 1.15 2016/07/16 07:13:26 pgoyette Exp $");
 
 #include 
 #include 
@@ -131,7 +131,7 @@ emitdevm(FILE *fp)
 	fputs("\n/* device switch table for block device */\n", fp);
 
 	for (i = 0; i <= maxbdevm ; i++)
-		dentry(fp, cdevmtab, i, 'b');
+		dentry(fp, bdevmtab, i, 'b');
 
 	fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp);