Module Name:    src
Committed By:   pgoyette
Date:           Fri Apr 28 06:00:33 UTC 2017

Modified Files:
        src/distrib/sets/lists/comp [prg-localcount2]: mi
        src/share/man/man9 [prg-localcount2]: Makefile autoconf.9
        src/sys/dev [prg-localcount2]: cgd.c fss.c md.c vnd.c
        src/sys/kern [prg-localcount2]: subr_autoconf.c
        src/sys/sys [prg-localcount2]: device.h

Log Message:
Introduce config_detach_release() which does all the work from the
former config_detach().  Now, config_detach() simply acquires a
reference to the device, which config_detach_release() can release!

This is needed because some drivers call config_detach() with a
reference, while other drivers have not been updated to use the
localcount reference mechanism.  So we provide a shim to make
everyone equal.


To generate a diff of this commit:
cvs rdiff -u -r1.2125.2.1 -r1.2125.2.2 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.409.2.1 -r1.409.2.2 src/share/man/man9/Makefile
cvs rdiff -u -r1.30 -r1.30.20.1 src/share/man/man9/autoconf.9
cvs rdiff -u -r1.114.4.3 -r1.114.4.4 src/sys/dev/cgd.c
cvs rdiff -u -r1.97.4.1 -r1.97.4.2 src/sys/dev/fss.c
cvs rdiff -u -r1.78.6.2 -r1.78.6.3 src/sys/dev/md.c
cvs rdiff -u -r1.259.4.1 -r1.259.4.2 src/sys/dev/vnd.c
cvs rdiff -u -r1.252.4.1 -r1.252.4.2 src/sys/kern/subr_autoconf.c
cvs rdiff -u -r1.149.8.1 -r1.149.8.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/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.2125.2.1 src/distrib/sets/lists/comp/mi:1.2125.2.2
--- src/distrib/sets/lists/comp/mi:1.2125.2.1	Thu Apr 27 05:36:30 2017
+++ src/distrib/sets/lists/comp/mi	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: mi,v 1.2125.2.1 2017/04/27 05:36:30 pgoyette Exp $
+#	$NetBSD: mi,v 1.2125.2.2 2017/04/28 06:00:33 pgoyette Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp				comp-sys-root
@@ -10313,6 +10313,7 @@
 ./usr/share/man/cat9/config_defer.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/config_detach.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/config_detach_children.0	comp-sys-catman		.cat
+./usr/share/man/cat9/config_detach_release.0	comp-sys-catman		.cat
 ./usr/share/man/cat9/config_finalize_register.0 comp-sys-catman		.cat
 ./usr/share/man/cat9/config_found.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/config_found_ia.0		comp-sys-catman		.cat
@@ -17776,6 +17777,7 @@
 ./usr/share/man/html9/config_defer.html		comp-sys-htmlman	html
 ./usr/share/man/html9/config_detach.html	comp-sys-htmlman	html
 ./usr/share/man/html9/config_detach_children.html	comp-sys-htmlman	html
+./usr/share/man/html9/config_detach_release.html	comp-sys-htmlman	html
 ./usr/share/man/html9/config_finalize_register.html	comp-sys-htmlman	html
 ./usr/share/man/html9/config_found.html		comp-sys-htmlman	html
 ./usr/share/man/html9/config_found_ia.html	comp-sys-htmlman	html
@@ -25351,6 +25353,7 @@
 ./usr/share/man/man9/config_defer.9		comp-sys-man		.man
 ./usr/share/man/man9/config_detach.9		comp-sys-man		.man
 ./usr/share/man/man9/config_detach_children.9	comp-sys-man		.man
+./usr/share/man/man9/config_detach_release.9	comp-sys-man		.man
 ./usr/share/man/man9/config_finalize_register.9 comp-sys-man		.man
 ./usr/share/man/man9/config_found.9		comp-sys-man		.man
 ./usr/share/man/man9/config_found_ia.9		comp-sys-man		.man

Index: src/share/man/man9/Makefile
diff -u src/share/man/man9/Makefile:1.409.2.1 src/share/man/man9/Makefile:1.409.2.2
--- src/share/man/man9/Makefile:1.409.2.1	Thu Apr 27 05:36:31 2017
+++ src/share/man/man9/Makefile	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-#       $NetBSD: Makefile,v 1.409.2.1 2017/04/27 05:36:31 pgoyette Exp $
+#       $NetBSD: Makefile,v 1.409.2.2 2017/04/28 06:00:33 pgoyette Exp $
 
 #	Makefile for section 9 (kernel function and variable) manual pages.
 
@@ -119,6 +119,7 @@ MLINKS+=autoconf.9 config_search_loc.9 \
 	autoconf.9 config_attach_pseudo.9 \
 	autoconf.9 config_detach.9 \
 	autoconf.9 config_detach_children.9 \
+	autoconf.9 config_detach_release.9 \
 	autoconf.9 config_deactivate.9 \
 	autoconf.9 config_defer.9 \
 	autoconf.9 config_interrupts.9 \

Index: src/share/man/man9/autoconf.9
diff -u src/share/man/man9/autoconf.9:1.30 src/share/man/man9/autoconf.9:1.30.20.1
--- src/share/man/man9/autoconf.9:1.30	Wed Mar 13 00:09:21 2013
+++ src/share/man/man9/autoconf.9	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-.\"     $NetBSD: autoconf.9,v 1.30 2013/03/13 00:09:21 riastradh Exp $
+.\"     $NetBSD: autoconf.9,v 1.30.20.1 2017/04/28 06:00:33 pgoyette Exp $
 .\"
 .\" Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd February 17, 2012
+.Dd April 28, 2017
 .Dt AUTOCONF 9
 .Os
 .Sh NAME
@@ -43,6 +43,7 @@
 .Nm config_attach_pseudo ,
 .Nm config_detach ,
 .Nm config_detach_children ,
+.Nm config_detach_release ,
 .Nm config_deactivate ,
 .Nm config_defer ,
 .Nm config_interrupts ,
@@ -84,6 +85,8 @@
 .Ft int
 .Fn config_detach_children "device_t dev" "int flags"
 .Ft int
+.Fn config_detach_release "device_t dev" "int flags"
+.Ft int
 .Fn config_deactivate "device_t dev"
 .Ft int
 .Fn config_defer "device_t dev" "void (*func)(device_t)"
@@ -340,7 +343,13 @@ The content of that object is similar to
 .Fn config_search_loc
 for regular devices.
 .It Fn config_detach "dev" "flags"
-Called by the parent to detach the child device.
+.It Fn config_detach_release "dev" "flags"
+Called by the parent to acquire a reference to and detach the child device.
+If the parent has already acquired a reference to the child via
+.Fn device_acquire ,
+it should call
+.Fn config_detach_release .
+In either case, the reference is released.
 The second argument
 .Em flags
 contains detachment flags.

Index: src/sys/dev/cgd.c
diff -u src/sys/dev/cgd.c:1.114.4.3 src/sys/dev/cgd.c:1.114.4.4
--- src/sys/dev/cgd.c:1.114.4.3	Thu Apr 27 12:07:23 2017
+++ src/sys/dev/cgd.c	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: cgd.c,v 1.114.4.3 2017/04/27 12:07:23 pgoyette Exp $ */
+/* $NetBSD: cgd.c,v 1.114.4.4 2017/04/28 06:00:33 pgoyette Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.114.4.3 2017/04/27 12:07:23 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.114.4.4 2017/04/28 06:00:33 pgoyette Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -337,8 +337,7 @@ cgd_attach(device_t parent, device_t sel
 
 
 /*
- * The caller must hold a reference to the device's localcount.  the
- * reference is released if the device is available for detach.
+ * The caller must hold a reference to the device's localcount.
  */
 static int
 cgd_detach(device_t self, int flags)
@@ -358,7 +357,6 @@ cgd_detach(device_t self, int flags)
 	disk_destroy(&dksc->sc_dkdev);
 	mutex_destroy(&sc->sc_lock);
 
-	device_release(self);
 	return 0;
 }
 
@@ -407,7 +405,7 @@ cgd_destroy(device_t dev)
 	cfdata_t cf;
 
 	cf = device_cfdata(dev);
-	error = config_detach(dev, DETACH_QUIET);
+	error = config_detach_release(dev, DETACH_QUIET);
 	if (error == 0)
 		free(cf, M_DEVBUF);
 

Index: src/sys/dev/fss.c
diff -u src/sys/dev/fss.c:1.97.4.1 src/sys/dev/fss.c:1.97.4.2
--- src/sys/dev/fss.c:1.97.4.1	Thu Apr 27 05:36:35 2017
+++ src/sys/dev/fss.c	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: fss.c,v 1.97.4.1 2017/04/27 05:36:35 pgoyette Exp $	*/
+/*	$NetBSD: fss.c,v 1.97.4.2 2017/04/28 06:00:33 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fss.c,v 1.97.4.1 2017/04/27 05:36:35 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fss.c,v 1.97.4.2 2017/04/28 06:00:33 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -173,8 +173,7 @@ fss_attach(device_t parent, device_t sel
 }
 
 /*
- * Caller must hold a reference to the device's localcount.  The
- * reference is released upon successful exit.
+ * Caller must hold a reference to the device's localcount.
  */
 static int
 fss_detach(device_t self, int flags)
@@ -197,7 +196,6 @@ fss_detach(device_t self, int flags)
 	disk_destroy(sc->sc_dkdev);
 	free(sc->sc_dkdev, M_DEVBUF);
 
-	device_release(self);
 	return 0;
 }
 
@@ -285,12 +283,11 @@ restart:
 	KASSERT((sc->sc_flags & (FSS_CDEV_OPEN|FSS_BDEV_OPEN)) == mflag);
 	mutex_exit(&sc->sc_slock);
 	cf = device_cfdata(sc->sc_dev);
-	error = config_detach(sc->sc_dev, DETACH_QUIET);
+	error = config_detach_release(sc->sc_dev, DETACH_QUIET);
 	if (! error)
 		free(cf, M_DEVBUF);
 	mutex_exit(&fss_device_lock);
 
-	/* device_release() was called by fss_detach() from config_detach() */
 	return error;
 }
 

Index: src/sys/dev/md.c
diff -u src/sys/dev/md.c:1.78.6.2 src/sys/dev/md.c:1.78.6.3
--- src/sys/dev/md.c:1.78.6.2	Thu Apr 27 23:18:21 2017
+++ src/sys/dev/md.c	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: md.c,v 1.78.6.2 2017/04/27 23:18:21 pgoyette Exp $	*/
+/*	$NetBSD: md.c,v 1.78.6.3 2017/04/28 06:00:33 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: md.c,v 1.78.6.2 2017/04/27 23:18:21 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: md.c,v 1.78.6.3 2017/04/28 06:00:33 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_md.h"
@@ -216,7 +216,6 @@ md_detach(device_t self, int flags)
 	if (rc != 0)
 		return rc;
 
-	device_release(self);
 	pmf_device_deregister(self);
 	disk_detach(&sc->sc_dkdev);
 	disk_destroy(&sc->sc_dkdev);
@@ -384,12 +383,11 @@ mdclose(dev_t dev, int flag, int fmt, st
 
 	mutex_enter(&md_device_lock);
 	cf = device_cfdata(sc->sc_dev);
-	error = config_detach(sc->sc_dev, DETACH_QUIET);
+	error = config_detach_release(sc->sc_dev, DETACH_QUIET);
 	if (! error)
 		free(cf, M_DEVBUF);
 	mutex_exit(&md_device_lock);
-	if (error)
-		device_release(sc->sc_dev);
+
 	return error;
 }
 

Index: src/sys/dev/vnd.c
diff -u src/sys/dev/vnd.c:1.259.4.1 src/sys/dev/vnd.c:1.259.4.2
--- src/sys/dev/vnd.c:1.259.4.1	Thu Apr 27 05:36:35 2017
+++ src/sys/dev/vnd.c	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: vnd.c,v 1.259.4.1 2017/04/27 05:36:35 pgoyette Exp $	*/
+/*	$NetBSD: vnd.c,v 1.259.4.2 2017/04/28 06:00:33 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2008 The NetBSD Foundation, Inc.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.259.4.1 2017/04/27 05:36:35 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.259.4.2 2017/04/28 06:00:33 pgoyette Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_vnd.h"
@@ -334,7 +334,7 @@ vnd_destroy(device_t dev)
 	cfdata_t cf;
 
 	cf = device_cfdata(dev);
-	error = config_detach(dev, DETACH_QUIET);
+	error = config_detach_release(dev, DETACH_QUIET);
 	if (error)
 		return error;
 	free(cf, M_DEVBUF);
@@ -499,12 +499,11 @@ vndclose(dev_t dev, int flags, int mode,
 		if ((error = vnd_destroy(sc->sc_dev)) != 0) {
 			aprint_error_dev(sc->sc_dev,
 			    "unable to detach instance\n");
-			device_release(self);
 		}
 		return error;
-	}
+	} else
+		device_release(self);
 
-	device_release(self);
 	return 0;
 }
 

Index: src/sys/kern/subr_autoconf.c
diff -u src/sys/kern/subr_autoconf.c:1.252.4.1 src/sys/kern/subr_autoconf.c:1.252.4.2
--- src/sys/kern/subr_autoconf.c:1.252.4.1	Thu Apr 27 05:36:37 2017
+++ src/sys/kern/subr_autoconf.c	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.252.4.1 2017/04/27 05:36:37 pgoyette Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.252.4.2 2017/04/28 06:00:33 pgoyette Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.252.4.1 2017/04/27 05:36:37 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.252.4.2 2017/04/28 06:00:33 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1275,6 +1275,12 @@ config_devunlink(device_t dev, struct de
 	/* Remove from cfdriver's array. */
 	cd->cd_devs[dev->dv_unit] = NULL;
 
+	/*
+	 * Release the reference that was held by (the caller of)
+	 * config_detach_release()
+	 */
+	device_release(dev);
+
 	/* Now wait for references to drain - no new refs are possible */
 	localcount_drain(&dev->dv_localcnt, &config_drain_cv,
 	    &alldevs.lock);
@@ -1714,6 +1720,17 @@ config_dump_garbage(struct devicelist *g
 }
 
 /*
+ * Acquire a reference to a device, and then detach it.
+ */
+int
+config_detach(device_t dev, int flags)
+{
+
+	device_acquire(dev);
+	return config_detach_release(dev, flags);
+}
+
+/*
  * Detach a device.  Optionally forced (e.g. because of hardware
  * removal) and quiet.  Returns zero if successful, non-zero
  * (an error code) otherwise.
@@ -1721,9 +1738,14 @@ config_dump_garbage(struct devicelist *g
  * Note that this code wants to be run from a process context, so
  * that the detach can sleep to allow processes which have a device
  * open to run and unwind their stacks.
+ *
+ * Also note that this code requires that the caller have acquired
+ * a reference to the device; callers that have not been updated
+ * for localcount should instead call config_detach().  The reference
+ * is released in all cases.
  */
 int
-config_detach(device_t dev, int flags)
+config_detach_release(device_t dev, int flags)
 {
 	struct alldevs_foray af;
 	struct cftable *ct;
@@ -1752,6 +1774,7 @@ config_detach(device_t dev, int flags)
 		printf("%s: %s is already detached\n", __func__,
 		    device_xname(dev));
 #endif /* DIAGNOSTIC */
+		device_release(dev);
 		return ENOENT;
 	}
 	alldevs.nwrite++;

Index: src/sys/sys/device.h
diff -u src/sys/sys/device.h:1.149.8.1 src/sys/sys/device.h:1.149.8.2
--- src/sys/sys/device.h:1.149.8.1	Thu Apr 27 05:36:38 2017
+++ src/sys/sys/device.h	Fri Apr 28 06:00:33 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: device.h,v 1.149.8.1 2017/04/27 05:36:38 pgoyette Exp $ */
+/* $NetBSD: device.h,v 1.149.8.2 2017/04/28 06:00:33 pgoyette Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -469,6 +469,7 @@ bool ifattr_match(const char *, const ch
 device_t config_attach_pseudo(cfdata_t);
 
 int	config_detach(device_t, int);
+int	config_detach_release(device_t, int);
 int	config_detach_children(device_t, int flags);
 bool	config_detach_all(int);
 int	config_deactivate(device_t);

Reply via email to