From owner-svn-src-stable@freebsd.org Mon Nov 7 08:36:11 2016 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E2D14C34AF1; Mon, 7 Nov 2016 08:36:11 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id AE94F951; Mon, 7 Nov 2016 08:36:11 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uA78aB83032045; Mon, 7 Nov 2016 08:36:11 GMT (envelope-from hselasky@FreeBSD.org) Received: (from hselasky@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uA78a6OM032001; Mon, 7 Nov 2016 08:36:06 GMT (envelope-from hselasky@FreeBSD.org) Message-Id: <201611070836.uA78a6OM032001@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: hselasky set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky Date: Mon, 7 Nov 2016 08:36:06 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r308401 - in stable/11/sys: arm/allwinner arm/at91 arm/cavium/cns11xx arm/samsung/exynos arm/ti/am335x arm/ti/usb arm/xilinx boot/kshim dev/puc dev/usb dev/usb/controller dev/usb/video ... X-SVN-Group: stable-11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Nov 2016 08:36:12 -0000 Author: hselasky Date: Mon Nov 7 08:36:06 2016 New Revision: 308401 URL: https://svnweb.freebsd.org/changeset/base/308401 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 Reviewed by: jhb Differential Revision: https://reviews.freebsd.org/D8070 Modified: stable/11/sys/arm/allwinner/a10_ehci.c stable/11/sys/arm/at91/at91_ohci.c stable/11/sys/arm/at91/at91_ohci_fdt.c stable/11/sys/arm/cavium/cns11xx/ehci_ebus.c stable/11/sys/arm/cavium/cns11xx/ohci_ec.c stable/11/sys/arm/samsung/exynos/exynos5_xhci.c stable/11/sys/arm/ti/am335x/am335x_musb.c stable/11/sys/arm/ti/usb/omap_ehci.c stable/11/sys/arm/xilinx/zy7_ehci.c stable/11/sys/boot/kshim/bsd_kernel.c stable/11/sys/dev/puc/puc.c stable/11/sys/dev/usb/controller/at91dci_atmelarm.c stable/11/sys/dev/usb/controller/at91dci_fdt.c stable/11/sys/dev/usb/controller/atmegadci_atmelarm.c stable/11/sys/dev/usb/controller/dwc_otg_fdt.c stable/11/sys/dev/usb/controller/ehci_ixp4xx.c stable/11/sys/dev/usb/controller/ehci_mv.c stable/11/sys/dev/usb/controller/ehci_pci.c stable/11/sys/dev/usb/controller/generic_ehci.c stable/11/sys/dev/usb/controller/generic_ohci.c stable/11/sys/dev/usb/controller/musb_otg_atmelarm.c stable/11/sys/dev/usb/controller/ohci_pci.c stable/11/sys/dev/usb/controller/ohci_s3c24x0.c stable/11/sys/dev/usb/controller/saf1761_otg_boot.c stable/11/sys/dev/usb/controller/saf1761_otg_fdt.c stable/11/sys/dev/usb/controller/uhci_pci.c stable/11/sys/dev/usb/controller/uss820dci_atmelarm.c stable/11/sys/dev/usb/controller/xhci_mv.c stable/11/sys/dev/usb/controller/xhci_pci.c stable/11/sys/dev/usb/usb_device.c stable/11/sys/dev/usb/video/udl.c stable/11/sys/kern/subr_bus.c stable/11/sys/mips/atheros/ar71xx_ehci.c stable/11/sys/mips/atheros/ar71xx_ohci.c stable/11/sys/mips/cavium/usb/octusb_octeon.c stable/11/sys/mips/mediatek/mtk_dotg.c stable/11/sys/mips/mediatek/mtk_ehci.c stable/11/sys/mips/mediatek/mtk_ohci.c stable/11/sys/mips/mediatek/mtk_xhci.c stable/11/sys/mips/rmi/xls_ehci.c stable/11/sys/mips/rt305x/rt305x_dotg.c stable/11/sys/mips/rt305x/rt305x_ehci.c stable/11/sys/mips/rt305x/rt305x_ohci.c Directory Properties: stable/11/ (props changed) Modified: stable/11/sys/arm/allwinner/a10_ehci.c ============================================================================== --- stable/11/sys/arm/allwinner/a10_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/allwinner/a10_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -278,17 +278,11 @@ a10_ehci_detach(device_t self) struct aw_ehci_softc *aw_sc = device_get_softc(self); ehci_softc_t *sc = &aw_sc->sc; const struct aw_ehci_conf *conf; - device_t bdev; int err; uint32_t reg_value = 0; conf = USB_CONF(self); - 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/11/sys/arm/at91/at91_ohci.c ============================================================================== --- stable/11/sys/arm/at91/at91_ohci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/at91/at91_ohci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -165,14 +165,8 @@ static int ohci_atmelarm_detach(device_t dev) { struct at91_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/11/sys/arm/at91/at91_ohci_fdt.c ============================================================================== --- stable/11/sys/arm/at91/at91_ohci_fdt.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/at91/at91_ohci_fdt.c Mon Nov 7 08:36:06 2016 (r308401) @@ -171,14 +171,8 @@ static int ohci_at91_fdt_detach(device_t dev) { struct at91_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/11/sys/arm/cavium/cns11xx/ehci_ebus.c ============================================================================== --- stable/11/sys/arm/cavium/cns11xx/ehci_ebus.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/cavium/cns11xx/ehci_ebus.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/arm/cavium/cns11xx/ohci_ec.c ============================================================================== --- stable/11/sys/arm/cavium/cns11xx/ohci_ec.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/cavium/cns11xx/ohci_ec.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/arm/samsung/exynos/exynos5_xhci.c ============================================================================== --- stable/11/sys/arm/samsung/exynos/exynos5_xhci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/samsung/exynos/exynos5_xhci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -288,14 +288,8 @@ static int exynos_xhci_detach(device_t dev) { struct exynos_xhci_softc *esc = device_get_softc(dev); - device_t bdev; int err; - if (esc->base.sc_bus.bdev != NULL) { - bdev = esc->base.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/11/sys/arm/ti/am335x/am335x_musb.c ============================================================================== --- stable/11/sys/arm/ti/am335x/am335x_musb.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/ti/am335x/am335x_musb.c Mon Nov 7 08:36:06 2016 (r308401) @@ -366,14 +366,10 @@ 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); if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) { /* @@ -397,9 +393,6 @@ musbotg_detach(device_t dev) bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, sc->sc_otg.sc_irq_res); - /* during module unload there are lots of children leftover */ - device_delete_children(dev); - return (0); } Modified: stable/11/sys/arm/ti/usb/omap_ehci.c ============================================================================== --- stable/11/sys/arm/ti/usb/omap_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/ti/usb/omap_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -392,15 +392,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/11/sys/arm/xilinx/zy7_ehci.c ============================================================================== --- stable/11/sys/arm/xilinx/zy7_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/arm/xilinx/zy7_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -323,20 +323,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/11/sys/boot/kshim/bsd_kernel.c ============================================================================== --- stable/11/sys/boot/kshim/bsd_kernel.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/boot/kshim/bsd_kernel.c Mon Nov 7 08:36:06 2016 (r308401) @@ -817,8 +817,12 @@ device_delete_child(device_t dev, device int error = 0; device_t grandchild; - /* remove children first */ + /* detach parent before deleting children, if any */ + error = device_detach(child); + if (error) + goto done; + /* remove children second */ while ((grandchild = TAILQ_FIRST(&child->dev_children))) { error = device_delete_child(child, grandchild); if (error) { @@ -827,11 +831,6 @@ device_delete_child(device_t dev, device } } - error = device_detach(child); - - if (error) - goto done; - devclass_delete_device(child->dev_module, child); if (dev != NULL) { Modified: stable/11/sys/dev/puc/puc.c ============================================================================== --- stable/11/sys/dev/puc/puc.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/puc/puc.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/at91dci_atmelarm.c ============================================================================== --- stable/11/sys/dev/usb/controller/at91dci_atmelarm.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/at91dci_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/at91dci_fdt.c ============================================================================== --- stable/11/sys/dev/usb/controller/at91dci_fdt.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/at91dci_fdt.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/atmegadci_atmelarm.c ============================================================================== --- stable/11/sys/dev/usb/controller/atmegadci_atmelarm.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/atmegadci_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/dwc_otg_fdt.c ============================================================================== --- stable/11/sys/dev/usb/controller/dwc_otg_fdt.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/dwc_otg_fdt.c Mon Nov 7 08:36:06 2016 (r308401) @@ -163,14 +163,8 @@ static int dwc_otg_detach(device_t dev) { struct dwc_otg_fdt_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/11/sys/dev/usb/controller/ehci_ixp4xx.c ============================================================================== --- stable/11/sys/dev/usb/controller/ehci_ixp4xx.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/ehci_ixp4xx.c Mon Nov 7 08:36:06 2016 (r308401) @@ -221,14 +221,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/11/sys/dev/usb/controller/ehci_mv.c ============================================================================== --- stable/11/sys/dev/usb/controller/ehci_mv.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/ehci_mv.c Mon Nov 7 08:36:06 2016 (r308401) @@ -264,14 +264,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/11/sys/dev/usb/controller/ehci_pci.c ============================================================================== --- stable/11/sys/dev/usb/controller/ehci_pci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/ehci_pci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/generic_ehci.c ============================================================================== --- stable/11/sys/dev/usb/controller/generic_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/generic_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -156,14 +156,8 @@ static int generic_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/11/sys/dev/usb/controller/generic_ohci.c ============================================================================== --- stable/11/sys/dev/usb/controller/generic_ohci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/generic_ohci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -214,18 +214,11 @@ static int generic_ohci_detach(device_t dev) { struct generic_ohci_softc *sc = device_get_softc(dev); - device_t bdev; int err; #ifdef EXT_RESOURCES struct clk_list *clk, *clk_tmp; #endif - if (sc->ohci_sc.sc_bus.bdev) { - bdev = sc->ohci_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/11/sys/dev/usb/controller/musb_otg_atmelarm.c ============================================================================== --- stable/11/sys/dev/usb/controller/musb_otg_atmelarm.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/musb_otg_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/ohci_pci.c ============================================================================== --- stable/11/sys/dev/usb/controller/ohci_pci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/ohci_pci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -335,13 +335,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/11/sys/dev/usb/controller/ohci_s3c24x0.c ============================================================================== --- stable/11/sys/dev/usb/controller/ohci_s3c24x0.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/ohci_s3c24x0.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/saf1761_otg_boot.c ============================================================================== --- stable/11/sys/dev/usb/controller/saf1761_otg_boot.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/saf1761_otg_boot.c Mon Nov 7 08:36:06 2016 (r308401) @@ -124,13 +124,6 @@ static int saf1761_otg_fdt_detach(device_t dev) { struct saf1761_otg_softc *sc = device_get_softc(dev); - device_t bdev; - - 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/11/sys/dev/usb/controller/saf1761_otg_fdt.c ============================================================================== --- stable/11/sys/dev/usb/controller/saf1761_otg_fdt.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/saf1761_otg_fdt.c Mon Nov 7 08:36:06 2016 (r308401) @@ -238,14 +238,8 @@ static int saf1761_otg_fdt_detach(device_t dev) { struct saf1761_otg_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/11/sys/dev/usb/controller/uhci_pci.c ============================================================================== --- stable/11/sys/dev/usb/controller/uhci_pci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/uhci_pci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/dev/usb/controller/uss820dci_atmelarm.c ============================================================================== --- stable/11/sys/dev/usb/controller/uss820dci_atmelarm.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/uss820dci_atmelarm.c Mon Nov 7 08:36:06 2016 (r308401) @@ -164,14 +164,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/11/sys/dev/usb/controller/xhci_mv.c ============================================================================== --- stable/11/sys/dev/usb/controller/xhci_mv.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/xhci_mv.c Mon Nov 7 08:36:06 2016 (r308401) @@ -171,15 +171,8 @@ static int xhci_detach(device_t dev) { struct xhci_softc *sc = device_get_softc(dev); - device_t bdev; int err; - if (sc->sc_bus.bdev != NULL) { - 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/11/sys/dev/usb/controller/xhci_pci.c ============================================================================== --- stable/11/sys/dev/usb/controller/xhci_pci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/controller/xhci_pci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -340,13 +340,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/11/sys/dev/usb/usb_device.c ============================================================================== --- stable/11/sys/dev/usb/usb_device.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/usb_device.c Mon Nov 7 08:36:06 2016 (r308401) @@ -1103,10 +1103,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/11/sys/dev/usb/video/udl.c ============================================================================== --- stable/11/sys/dev/usb/video/udl.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/dev/usb/video/udl.c Mon Nov 7 08:36:06 2016 (r308401) @@ -443,14 +443,9 @@ udl_detach(device_t dev) { struct udl_softc *sc = device_get_softc(dev); - if (sc->sc_fbdev != NULL) { - device_t bdev; + /* delete all child devices */ + device_delete_children(dev); - bdev = sc->sc_fbdev; - sc->sc_fbdev = NULL; - device_detach(bdev); - device_delete_child(dev, bdev); - } UDL_LOCK(sc); sc->sc_gone = 1; callout_stop(&sc->sc_callout); Modified: stable/11/sys/kern/subr_bus.c ============================================================================== --- stable/11/sys/kern/subr_bus.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/kern/subr_bus.c Mon Nov 7 08:36:06 2016 (r308401) @@ -1949,15 +1949,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/11/sys/mips/atheros/ar71xx_ehci.c ============================================================================== --- stable/11/sys/mips/atheros/ar71xx_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/atheros/ar71xx_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -231,14 +231,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/11/sys/mips/atheros/ar71xx_ohci.c ============================================================================== --- stable/11/sys/mips/atheros/ar71xx_ohci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/atheros/ar71xx_ohci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -156,13 +156,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/11/sys/mips/cavium/usb/octusb_octeon.c ============================================================================== --- stable/11/sys/mips/cavium/usb/octusb_octeon.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/cavium/usb/octusb_octeon.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/mips/mediatek/mtk_dotg.c ============================================================================== --- stable/11/sys/mips/mediatek/mtk_dotg.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/mediatek/mtk_dotg.c Mon Nov 7 08:36:06 2016 (r308401) @@ -161,14 +161,8 @@ static int dotg_fdt_detach(device_t dev) { struct dwc_otg_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/11/sys/mips/mediatek/mtk_ehci.c ============================================================================== --- stable/11/sys/mips/mediatek/mtk_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/mediatek/mtk_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -162,14 +162,8 @@ static int ehci_fdt_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/11/sys/mips/mediatek/mtk_ohci.c ============================================================================== --- stable/11/sys/mips/mediatek/mtk_ohci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/mediatek/mtk_ohci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -162,14 +162,8 @@ static int ohci_fdt_detach(device_t self) { ohci_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/11/sys/mips/mediatek/mtk_xhci.c ============================================================================== --- stable/11/sys/mips/mediatek/mtk_xhci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/mediatek/mtk_xhci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -161,14 +161,8 @@ static int mtk_xhci_fdt_detach(device_t self) { struct xhci_softc *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/11/sys/mips/rmi/xls_ehci.c ============================================================================== --- stable/11/sys/mips/rmi/xls_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/rmi/xls_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -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/11/sys/mips/rt305x/rt305x_dotg.c ============================================================================== --- stable/11/sys/mips/rt305x/rt305x_dotg.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/rt305x/rt305x_dotg.c Mon Nov 7 08:36:06 2016 (r308401) @@ -173,14 +173,8 @@ static int dotg_obio_detach(device_t dev) { struct dwc_otg_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/11/sys/mips/rt305x/rt305x_ehci.c ============================================================================== --- stable/11/sys/mips/rt305x/rt305x_ehci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/rt305x/rt305x_ehci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -175,14 +175,8 @@ static int ehci_obio_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/11/sys/mips/rt305x/rt305x_ohci.c ============================================================================== --- stable/11/sys/mips/rt305x/rt305x_ohci.c Mon Nov 7 08:26:19 2016 (r308400) +++ stable/11/sys/mips/rt305x/rt305x_ohci.c Mon Nov 7 08:36:06 2016 (r308401) @@ -175,14 +175,8 @@ static int ohci_obio_detach(device_t self) { ohci_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);