Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/usb ugen(4): Use cv_wait loop for draining reference...



details:   https://anonhg.NetBSD.org/src/rev/010214ff1359
branches:  trunk
changeset: 1023433:010214ff1359
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Tue Sep 07 10:43:51 2021 +0000

description:
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.

diffstat:

 sys/dev/usb/ugen.c |  15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)

diffs (43 lines):

diff -r 6c745cdb6909 -r 010214ff1359 sys/dev/usb/ugen.c
--- a/sys/dev/usb/ugen.c        Tue Sep 07 10:43:34 2021 +0000
+++ b/sys/dev/usb/ugen.c        Tue Sep 07 10:43:51 2021 +0000
@@ -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 @@
                }
        }
 
+       /*
+        * 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 @@
                                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);
 



Home | Main Index | Thread Index | Old Index