Module Name:    src
Committed By:   riastradh
Date:           Tue Sep  7 10:43:51 UTC 2021

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

Log Message:
ugen(4): Use cv_wait loop for draining reference count on detach.

- Should be no need to use cv_timedwait because all users have now
  been given a wakeup (previously writers were not, so we relied on
  the timeouts to work out).

- Need to run this in a loop or else a spurious wakeup could cause us
  to free data structures before the users have actually drained.


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

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

Modified files:

Index: src/sys/dev/usb/ugen.c
diff -u src/sys/dev/usb/ugen.c:1.165 src/sys/dev/usb/ugen.c:1.166
--- src/sys/dev/usb/ugen.c:1.165	Tue Sep  7 10:43:34 2021
+++ src/sys/dev/usb/ugen.c	Tue Sep  7 10:43:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ugen.c,v 1.165 2021/09/07 10:43:34 riastradh Exp $	*/
+/*	$NetBSD: ugen.c,v 1.166 2021/09/07 10:43:51 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.165 2021/09/07 10:43:34 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.166 2021/09/07 10:43:51 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -1215,6 +1215,12 @@ ugen_detach(device_t self, int flags)
 		}
 	}
 
+	/*
+	 * Wait for users to drain.  Before this point there can be no
+	 * more I/O operations started because we set sc_dying; after
+	 * this, there can be no more I/O operations in progress, so it
+	 * will be safe to free things.
+	 */
 	mutex_enter(&sc->sc_lock);
 	if (--sc->sc_refcnt >= 0) {
 		/* Wake everyone */
@@ -1223,8 +1229,9 @@ ugen_detach(device_t self, int flags)
 				cv_broadcast(&sc->sc_endpoints[i][dir].cv);
 		}
 		/* Wait for processes to go away. */
-		if (cv_timedwait(&sc->sc_detach_cv, &sc->sc_lock, hz * 60))
-			aprint_error_dev(self, ": didn't detach\n");
+		do {
+			cv_wait(&sc->sc_detach_cv, &sc->sc_lock);
+		} while (sc->sc_refcnt >= 0);
 	}
 	mutex_exit(&sc->sc_lock);
 

Reply via email to