Module Name:    src
Committed By:   mlelstv
Date:           Tue Nov 29 03:23:01 UTC 2016

Modified Files:
        src/sys/dev/scsipi: atapiconf.c scsiconf.c scsipi_base.c scsipiconf.h

Log Message:
reference count adapter mutex possibly shared by multiple channels.

fix error in atapibusdetach, when a child device cannot be detached,
keep atapibus instance alive.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/sys/dev/scsipi/atapiconf.c
cvs rdiff -u -r1.276 -r1.277 src/sys/dev/scsipi/scsiconf.c
cvs rdiff -u -r1.168 -r1.169 src/sys/dev/scsipi/scsipi_base.c
cvs rdiff -u -r1.125 -r1.126 src/sys/dev/scsipi/scsipiconf.h

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

Modified files:

Index: src/sys/dev/scsipi/atapiconf.c
diff -u src/sys/dev/scsipi/atapiconf.c:1.89 src/sys/dev/scsipi/atapiconf.c:1.90
--- src/sys/dev/scsipi/atapiconf.c:1.89	Sun Nov 20 15:37:19 2016
+++ src/sys/dev/scsipi/atapiconf.c	Tue Nov 29 03:23:00 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: atapiconf.c,v 1.89 2016/11/20 15:37:19 mlelstv Exp $	*/
+/*	$NetBSD: atapiconf.c,v 1.90 2016/11/29 03:23:00 mlelstv Exp $	*/
 
 /*
  * Copyright (c) 1996, 2001 Manuel Bouyer.  All rights reserved.
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.89 2016/11/20 15:37:19 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.90 2016/11/29 03:23:00 mlelstv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -34,6 +34,7 @@ __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,
 #include <sys/buf.h>
 #include <sys/proc.h>
 #include <sys/kthread.h>
+#include <sys/atomic.h>
 
 #include <dev/scsipi/scsipi_all.h>
 #include <dev/scsipi/scsipiconf.h>
@@ -152,7 +153,9 @@ atapibusattach(device_t parent, device_t
 	aprint_naive("\n");
 	aprint_normal(": %d targets\n", chan->chan_ntargets);
 
-	mutex_init(&chan->chan_adapter->adapt_mtx, MUTEX_DEFAULT, IPL_BIO);
+	if (atomic_inc_uint_nv(&chan_running(chan)) == 1)
+		mutex_init(chan_mtx(chan), MUTEX_DEFAULT, IPL_BIO);
+
 	cv_init(&chan->chan_cv_thr, "scshut");
 	cv_init(&chan->chan_cv_comp, "sccomp");
 	cv_init(&chan->chan_cv_xs, "xscmd");
@@ -222,12 +225,14 @@ atapibusdetach(device_t self, int flags)
 	}
 	mutex_exit(chan_mtx(chan));
 
-out:
 	cv_destroy(&chan->chan_cv_xs);
 	cv_destroy(&chan->chan_cv_comp);
 	cv_destroy(&chan->chan_cv_thr);
-	mutex_destroy(chan_mtx(chan));
 
+	if (atomic_dec_uint_nv(&chan_running(chan)) == 0)
+		mutex_destroy(chan_mtx(chan));
+
+out:
 	/* XXXSMP scsipi */
 	KERNEL_UNLOCK_ONE(curlwp);
 	return error;

Index: src/sys/dev/scsipi/scsiconf.c
diff -u src/sys/dev/scsipi/scsiconf.c:1.276 src/sys/dev/scsipi/scsiconf.c:1.277
--- src/sys/dev/scsipi/scsiconf.c:1.276	Sun Nov 20 15:37:19 2016
+++ src/sys/dev/scsipi/scsiconf.c	Tue Nov 29 03:23:00 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: scsiconf.c,v 1.276 2016/11/20 15:37:19 mlelstv Exp $	*/
+/*	$NetBSD: scsiconf.c,v 1.277 2016/11/29 03:23:00 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc.
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: scsiconf.c,v 1.276 2016/11/20 15:37:19 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: scsiconf.c,v 1.277 2016/11/29 03:23:00 mlelstv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -63,6 +63,7 @@ __KERNEL_RCSID(0, "$NetBSD: scsiconf.c,v
 #include <sys/fcntl.h>
 #include <sys/scsiio.h>
 #include <sys/queue.h>
+#include <sys/atomic.h>
 
 #include <dev/scsipi/scsi_all.h>
 #include <dev/scsipi/scsipi_all.h>
@@ -240,11 +241,12 @@ scsibusattach(device_t parent, device_t 
 			chan->chan_adapter->adapt_max_periph = 256;
 	}
 
-	mutex_init(chan_mtx(chan), MUTEX_DEFAULT, IPL_BIO);
+	if (atomic_inc_uint_nv(&chan_running(chan)) == 1)
+		mutex_init(chan_mtx(chan), MUTEX_DEFAULT, IPL_BIO);
+
 	cv_init(&chan->chan_cv_thr, "scshut");
 	cv_init(&chan->chan_cv_comp, "sccomp");
 	cv_init(&chan->chan_cv_xs, "xscmd");
-	chan_running(chan) = true;
 
 	if (scsipi_adapter_addref(chan->chan_adapter))
 		return;
@@ -371,11 +373,12 @@ scsibusdetach(device_t self, int flags)
 	 */
 	scsipi_channel_shutdown(chan);
 
-	chan_running(chan) = false;
 	cv_destroy(&chan->chan_cv_xs);
 	cv_destroy(&chan->chan_cv_comp);
 	cv_destroy(&chan->chan_cv_thr);
-	mutex_destroy(chan_mtx(chan));
+
+	if (atomic_dec_uint_nv(&chan_running(chan)) == 0)
+		mutex_destroy(chan_mtx(chan));
 
 	return 0;
 }

Index: src/sys/dev/scsipi/scsipi_base.c
diff -u src/sys/dev/scsipi/scsipi_base.c:1.168 src/sys/dev/scsipi/scsipi_base.c:1.169
--- src/sys/dev/scsipi/scsipi_base.c:1.168	Mon Nov 21 21:03:22 2016
+++ src/sys/dev/scsipi/scsipi_base.c	Tue Nov 29 03:23:00 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: scsipi_base.c,v 1.168 2016/11/21 21:03:22 mlelstv Exp $	*/
+/*	$NetBSD: scsipi_base.c,v 1.169 2016/11/29 03:23:00 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 1998, 1999, 2000, 2002, 2003, 2004 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: scsipi_base.c,v 1.168 2016/11/21 21:03:22 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: scsipi_base.c,v 1.169 2016/11/29 03:23:00 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_scsi.h"
@@ -571,7 +571,7 @@ scsipi_put_xs(struct scsipi_xfer *xs)
 void
 scsipi_channel_freeze(struct scsipi_channel *chan, int count)
 {
-	bool lock = chan_running(chan);
+	bool lock = chan_running(chan) > 0;
 
 	if (lock)
 		mutex_enter(chan_mtx(chan));
@@ -595,7 +595,7 @@ scsipi_channel_freeze_locked(struct scsi
 void
 scsipi_channel_thaw(struct scsipi_channel *chan, int count)
 {
-	bool lock = chan_running(chan);
+	bool lock = chan_running(chan) > 0;
 
 	if (lock)
 		mutex_enter(chan_mtx(chan));

Index: src/sys/dev/scsipi/scsipiconf.h
diff -u src/sys/dev/scsipi/scsipiconf.h:1.125 src/sys/dev/scsipi/scsipiconf.h:1.126
--- src/sys/dev/scsipi/scsipiconf.h:1.125	Fri Nov 25 02:23:14 2016
+++ src/sys/dev/scsipi/scsipiconf.h	Tue Nov 29 03:23:00 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: scsipiconf.h,v 1.125 2016/11/25 02:23:14 christos Exp $	*/
+/*	$NetBSD: scsipiconf.h,v 1.126 2016/11/29 03:23:00 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 1998, 1999, 2000, 2004 The NetBSD Foundation, Inc.
@@ -206,7 +206,7 @@ struct scsipi_adapter {
 			struct scsipi_inquiry_pattern *);
 
 	kmutex_t adapt_mtx;
-	bool	adapt_running;	/* attachment initialized */
+	volatile int	adapt_running;	/* how many users of mutex */
 };
 
 /* adapt_flags */

Reply via email to