Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/usb Fix error handling, to prevent kernel crashes wh...



details:   https://anonhg.NetBSD.org/src/rev/21418b138c29
branches:  trunk
changeset: 965369:21418b138c29
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sat Sep 14 12:48:51 2019 +0000

description:
Fix error handling, to prevent kernel crashes when detaching an ugensa0
device. Also move usbd_add_drv_event() down, after we are sure the attach
didn't fail.

Found with vHCI.

diffstat:

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

diffs (68 lines):

diff -r 4f38171dc8d1 -r 21418b138c29 sys/dev/usb/ugensa.c
--- a/sys/dev/usb/ugensa.c      Sat Sep 14 12:46:00 2019 +0000
+++ b/sys/dev/usb/ugensa.c      Sat Sep 14 12:48:51 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ugensa.c,v 1.39 2019/05/09 02:43:35 mrg Exp $  */
+/*     $NetBSD: ugensa.c,v 1.40 2019/09/14 12:48:51 maxv Exp $ */
 
 /*
  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ugensa.c,v 1.39 2019/05/09 02:43:35 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ugensa.c,v 1.40 2019/09/14 12:48:51 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -62,6 +62,12 @@
 
 struct ugensa_softc {
        device_t                sc_dev;         /* base device */
+
+       enum {
+               UGENSA_INIT_NONE,
+               UGENSA_INIT_INITED
+       } sc_init_state;
+
        struct usbd_device *    sc_udev;        /* device */
        struct usbd_interface * sc_iface;       /* interface */
 
@@ -144,6 +150,7 @@
 
        sc->sc_dev = self;
        sc->sc_dying = false;
+       sc->sc_init_state = UGENSA_INIT_NONE;
 
        aprint_naive("\n");
        aprint_normal("\n");
@@ -187,8 +194,6 @@
        ucaa.ucaa_methods = &ugensa_methods;
        ucaa.ucaa_arg = sc;
 
-       usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
-
        ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
        for (i = 0; i < id->bNumEndpoints; i++) {
                int addr, dir, attr;
@@ -227,6 +232,9 @@
                goto bad;
        }
 
+       sc->sc_init_state = UGENSA_INIT_INITED;
+       usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
+
        DPRINTF(("ugensa: in=0x%x out=0x%x\n", ucaa.ucaa_bulkin,
            ucaa.ucaa_bulkout));
        sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
@@ -261,6 +269,9 @@
 
        sc->sc_dying = true;
 
+       if (sc->sc_init_state < UGENSA_INIT_INITED)
+               return 0;
+
        if (sc->sc_subdev != NULL) {
                rv = config_detach(sc->sc_subdev, flags);
                sc->sc_subdev = NULL;



Home | Main Index | Thread Index | Old Index