Date: Mon, 7 Nov 2016 09:19:04 +0000 (UTC) From: Hans Petter Selasky <hselasky@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r308402 - in stable/10/sys: arm/allwinner arm/cavium/cns11xx arm/ti/usb arm/xilinx dev/puc dev/usb dev/usb/controller kern mips/atheros mips/cavium/usb mips/rmi mips/rt305x Message-ID: <201611070919.uA79J49D047502@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: hselasky Date: Mon Nov 7 09:19:04 2016 New Revision: 308402 URL: https://svnweb.freebsd.org/changeset/base/308402 Log: MFC r307518: Fix device delete child function. When detaching device trees parent devices must be detached prior to detaching its children. This is because parent devices can have pointers to the child devices in their softcs which are not invalidated by device_delete_child(). This can cause use after free issues and panic(). Device drivers implementing trees, must ensure its detach function detaches or deletes all its children before returning. While at it remove now redundant device_detach() calls before device_delete_child() and device_delete_children(), mostly in the USB controller drivers. Tested by: Jan Henrik Sylvester <me@janh.de> Reviewed by: jhb Differential Revision: https://reviews.freebsd.org/D8070 Modified: stable/10/sys/arm/allwinner/a10_ehci.c stable/10/sys/arm/cavium/cns11xx/ehci_ebus.c stable/10/sys/arm/cavium/cns11xx/ohci_ec.c stable/10/sys/arm/ti/usb/omap_ehci.c stable/10/sys/arm/xilinx/zy7_ehci.c stable/10/sys/dev/puc/puc.c stable/10/sys/dev/usb/controller/at91dci_atmelarm.c stable/10/sys/dev/usb/controller/at91dci_fdt.c stable/10/sys/dev/usb/controller/atmegadci_atmelarm.c stable/10/sys/dev/usb/controller/dwc_otg_fdt.c stable/10/sys/dev/usb/controller/ehci_ixp4xx.c stable/10/sys/dev/usb/controller/ehci_mv.c stable/10/sys/dev/usb/controller/ehci_pci.c stable/10/sys/dev/usb/controller/musb_otg_atmelarm.c stable/10/sys/dev/usb/controller/ohci_pci.c stable/10/sys/dev/usb/controller/ohci_s3c24x0.c stable/10/sys/dev/usb/controller/uhci_pci.c stable/10/sys/dev/usb/controller/uss820dci_atmelarm.c stable/10/sys/dev/usb/controller/xhci_pci.c stable/10/sys/dev/usb/usb_device.c stable/10/sys/kern/subr_bus.c stable/10/sys/mips/atheros/ar71xx_ehci.c stable/10/sys/mips/atheros/ar71xx_ohci.c stable/10/sys/mips/cavium/usb/octusb_octeon.c stable/10/sys/mips/rmi/xls_ehci.c stable/10/sys/mips/rt305x/rt305x_dotg.c Directory Properties: stable/10/ (props changed) Modified: stable/10/sys/arm/allwinner/a10_ehci.c ============================================================================== --- stable/10/sys/arm/allwinner/a10_ehci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/arm/allwinner/a10_ehci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -224,15 +224,9 @@ static int a10_ehci_detach(device_t self) { ehci_softc_t *sc = device_get_softc(self); - device_t bdev; int err; uint32_t reg_value = 0; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/arm/cavium/cns11xx/ehci_ebus.c ============================================================================== --- stable/10/sys/arm/cavium/cns11xx/ehci_ebus.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/arm/cavium/cns11xx/ehci_ebus.c Mon Nov 7 09:19:04 2016 (r308402) @@ -184,14 +184,8 @@ static int ehci_ebus_detach(device_t self) { ehci_softc_t *sc = device_get_softc(self); - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/arm/cavium/cns11xx/ohci_ec.c ============================================================================== --- stable/10/sys/arm/cavium/cns11xx/ohci_ec.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/arm/cavium/cns11xx/ohci_ec.c Mon Nov 7 09:19:04 2016 (r308402) @@ -177,14 +177,8 @@ static int ohci_ec_detach(device_t dev) { struct ec_ohci_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_ohci.sc_bus.bdev) { - bdev = sc->sc_ohci.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/arm/ti/usb/omap_ehci.c ============================================================================== --- stable/10/sys/arm/ti/usb/omap_ehci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/arm/ti/usb/omap_ehci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -939,15 +939,8 @@ omap_ehci_detach(device_t dev) { struct omap_ehci_softc *isc = device_get_softc(dev); ehci_softc_t *sc = &isc->base; - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } - /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/arm/xilinx/zy7_ehci.c ============================================================================== --- stable/10/sys/arm/xilinx/zy7_ehci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/arm/xilinx/zy7_ehci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -310,20 +310,17 @@ zy7_ehci_detach(device_t dev) { ehci_softc_t *sc = device_get_softc(dev); + /* during module unload there are lots of children leftover */ + device_delete_children(dev); + sc->sc_flags &= ~EHCI_SCFLG_DONEINIT; - if (device_is_attached(dev)) - bus_generic_detach(dev); - if (sc->sc_irq_res && sc->sc_intr_hdl) /* call ehci_detach() after ehci_init() called after * successful bus_setup_intr(). */ ehci_detach(sc); - if (sc->sc_bus.bdev) { - device_detach(sc->sc_bus.bdev); - device_delete_child(dev, sc->sc_bus.bdev); - } + if (sc->sc_irq_res) { if (sc->sc_intr_hdl != NULL) bus_teardown_intr(dev, sc->sc_irq_res, Modified: stable/10/sys/dev/puc/puc.c ============================================================================== --- stable/10/sys/dev/puc/puc.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/puc/puc.c Mon Nov 7 09:19:04 2016 (r308402) @@ -414,8 +414,7 @@ puc_bfe_detach(device_t dev) port = &sc->sc_port[idx]; if (port->p_dev == NULL) continue; - if (device_detach(port->p_dev) == 0) { - device_delete_child(dev, port->p_dev); + if (device_delete_child(dev, port->p_dev) == 0) { if (port->p_rres != NULL) rman_release_resource(port->p_rres); if (port->p_ires != NULL) Modified: stable/10/sys/dev/usb/controller/at91dci_atmelarm.c ============================================================================== --- stable/10/sys/dev/usb/controller/at91dci_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/at91dci_atmelarm.c Mon Nov 7 09:19:04 2016 (r308402) @@ -243,14 +243,8 @@ static int at91_udp_detach(device_t dev) { struct at91_udp_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_dci.sc_bus.bdev) { - bdev = sc->sc_dci.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/dev/usb/controller/at91dci_fdt.c ============================================================================== --- stable/10/sys/dev/usb/controller/at91dci_fdt.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/at91dci_fdt.c Mon Nov 7 09:19:04 2016 (r308402) @@ -249,14 +249,8 @@ static int at91_udp_detach(device_t dev) { struct at91_udp_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_dci.sc_bus.bdev) { - bdev = sc->sc_dci.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/dev/usb/controller/atmegadci_atmelarm.c ============================================================================== --- stable/10/sys/dev/usb/controller/atmegadci_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/atmegadci_atmelarm.c Mon Nov 7 09:19:04 2016 (r308402) @@ -155,14 +155,8 @@ static int atmegadci_detach(device_t dev) { struct atmegadci_super_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_otg.sc_bus.bdev) { - bdev = sc->sc_otg.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/dev/usb/controller/dwc_otg_fdt.c ============================================================================== --- stable/10/sys/dev/usb/controller/dwc_otg_fdt.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/dwc_otg_fdt.c Mon Nov 7 09:19:04 2016 (r308402) @@ -154,14 +154,8 @@ static int dwc_otg_detach(device_t dev) { struct dwc_otg_super_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_otg.sc_bus.bdev) { - bdev = sc->sc_otg.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/dev/usb/controller/ehci_ixp4xx.c ============================================================================== --- stable/10/sys/dev/usb/controller/ehci_ixp4xx.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/ehci_ixp4xx.c Mon Nov 7 09:19:04 2016 (r308402) @@ -207,14 +207,8 @@ ehci_ixp_detach(device_t self) { struct ixp_ehci_softc *isc = device_get_softc(self); ehci_softc_t *sc = &isc->base; - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/dev/usb/controller/ehci_mv.c ============================================================================== --- stable/10/sys/dev/usb/controller/ehci_mv.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/ehci_mv.c Mon Nov 7 09:19:04 2016 (r308402) @@ -242,14 +242,8 @@ static int mv_ehci_detach(device_t self) { ehci_softc_t *sc = device_get_softc(self); - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/dev/usb/controller/ehci_pci.c ============================================================================== --- stable/10/sys/dev/usb/controller/ehci_pci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/ehci_pci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -477,13 +477,7 @@ static int ehci_pci_detach(device_t self) { ehci_softc_t *sc = device_get_softc(self); - device_t bdev; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/dev/usb/controller/musb_otg_atmelarm.c ============================================================================== --- stable/10/sys/dev/usb/controller/musb_otg_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/musb_otg_atmelarm.c Mon Nov 7 09:19:04 2016 (r308402) @@ -204,14 +204,8 @@ static int musbotg_detach(device_t dev) { struct musbotg_super_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_otg.sc_bus.bdev) { - bdev = sc->sc_otg.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/dev/usb/controller/ohci_pci.c ============================================================================== --- stable/10/sys/dev/usb/controller/ohci_pci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/ohci_pci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -333,13 +333,7 @@ static int ohci_pci_detach(device_t self) { ohci_softc_t *sc = device_get_softc(self); - device_t bdev; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/dev/usb/controller/ohci_s3c24x0.c ============================================================================== --- stable/10/sys/dev/usb/controller/ohci_s3c24x0.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/ohci_s3c24x0.c Mon Nov 7 09:19:04 2016 (r308402) @@ -148,14 +148,8 @@ static int ohci_s3c24x0_detach(device_t dev) { struct ohci_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/dev/usb/controller/uhci_pci.c ============================================================================== --- stable/10/sys/dev/usb/controller/uhci_pci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/uhci_pci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -393,13 +393,7 @@ int uhci_pci_detach(device_t self) { uhci_softc_t *sc = device_get_softc(self); - device_t bdev; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/dev/usb/controller/uss820dci_atmelarm.c ============================================================================== --- stable/10/sys/dev/usb/controller/uss820dci_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/uss820dci_atmelarm.c Mon Nov 7 09:19:04 2016 (r308402) @@ -169,14 +169,8 @@ static int uss820_atmelarm_detach(device_t dev) { struct uss820dci_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/dev/usb/controller/xhci_pci.c ============================================================================== --- stable/10/sys/dev/usb/controller/xhci_pci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/controller/xhci_pci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -318,13 +318,7 @@ static int xhci_pci_detach(device_t self) { struct xhci_softc *sc = device_get_softc(self); - device_t bdev; - if (sc->sc_bus.bdev != NULL) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/dev/usb/usb_device.c ============================================================================== --- stable/10/sys/dev/usb/usb_device.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/dev/usb/usb_device.c Mon Nov 7 09:19:04 2016 (r308402) @@ -1106,10 +1106,8 @@ usb_detach_device_sub(struct usb_device device_printf(dev, "Resume failed\n"); } } - if (device_detach(dev)) { - goto error; - } } + /* detach and delete child */ if (device_delete_child(udev->parent_dev, dev)) { goto error; } Modified: stable/10/sys/kern/subr_bus.c ============================================================================== --- stable/10/sys/kern/subr_bus.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/kern/subr_bus.c Mon Nov 7 09:19:04 2016 (r308402) @@ -1923,15 +1923,17 @@ device_delete_child(device_t dev, device PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); - /* remove children first */ + /* detach parent before deleting children, if any */ + if ((error = device_detach(child)) != 0) + return (error); + + /* remove children second */ while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { error = device_delete_child(child, grandchild); if (error) return (error); } - if ((error = device_detach(child)) != 0) - return (error); if (child->devclass) devclass_delete_device(child->devclass, child); if (child->parent) Modified: stable/10/sys/mips/atheros/ar71xx_ehci.c ============================================================================== --- stable/10/sys/mips/atheros/ar71xx_ehci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/mips/atheros/ar71xx_ehci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -196,14 +196,8 @@ ar71xx_ehci_detach(device_t self) { struct ar71xx_ehci_softc *isc = device_get_softc(self); ehci_softc_t *sc = &isc->base; - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/mips/atheros/ar71xx_ohci.c ============================================================================== --- stable/10/sys/mips/atheros/ar71xx_ohci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/mips/atheros/ar71xx_ohci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -143,13 +143,7 @@ static int ar71xx_ohci_detach(device_t dev) { struct ar71xx_ohci_softc *sc = device_get_softc(dev); - device_t bdev; - if (sc->sc_ohci.sc_bus.bdev) { - bdev = sc->sc_ohci.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/mips/cavium/usb/octusb_octeon.c ============================================================================== --- stable/10/sys/mips/cavium/usb/octusb_octeon.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/mips/cavium/usb/octusb_octeon.c Mon Nov 7 09:19:04 2016 (r308402) @@ -160,16 +160,10 @@ static int octusb_octeon_detach(device_t dev) { struct octusb_octeon_softc *sc = device_get_softc(dev); - device_t bdev; int err; int nports; int i; - if (sc->sc_dci.sc_bus.bdev) { - bdev = sc->sc_dci.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev); Modified: stable/10/sys/mips/rmi/xls_ehci.c ============================================================================== --- stable/10/sys/mips/rmi/xls_ehci.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/mips/rmi/xls_ehci.c Mon Nov 7 09:19:04 2016 (r308402) @@ -164,14 +164,8 @@ static int ehci_xls_detach(device_t self) { ehci_softc_t *sc = device_get_softc(self); - device_t bdev; int err; - if (sc->sc_bus.bdev) { - bdev = sc->sc_bus.bdev; - device_detach(bdev); - device_delete_child(self, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(self); Modified: stable/10/sys/mips/rt305x/rt305x_dotg.c ============================================================================== --- stable/10/sys/mips/rt305x/rt305x_dotg.c Mon Nov 7 08:36:06 2016 (r308401) +++ stable/10/sys/mips/rt305x/rt305x_dotg.c Mon Nov 7 09:19:04 2016 (r308402) @@ -167,14 +167,8 @@ static int dotg_obio_detach(device_t dev) { struct dotg_obio_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_dci.sc_bus.bdev) { - bdev = sc->sc_dci.sc_bus.bdev; - device_detach(bdev); - device_delete_child(dev, bdev); - } /* during module unload there are lots of children leftover */ device_delete_children(dev);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201611070919.uA79J49D047502>