git: d412c07617eb - main - Check for errors when detaching children first, not last

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 05 Nov 2024 01:33:43 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=d412c07617eb35435668b024bc2cecda05f57f1f

commit d412c07617eb35435668b024bc2cecda05f57f1f
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2024-11-05 01:30:33 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-11-05 01:30:33 +0000

    Check for errors when detaching children first, not last
    
    These detach routines in these drivers all ended with 'return
    (bus_generic_detach())' meaning that if any child device failed to
    detach, the parent driver was left in a mostly destroyed state, but
    still marked attached.  Instead, bus drivers should detach child
    drivers first and return errors before destroying driver state in the
    parent.
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D47387
---
 sys/arm/nvidia/as3722.c                  | 7 ++++++-
 sys/arm/nvidia/drm2/tegra_dc.c           | 7 ++++++-
 sys/arm/nvidia/drm2/tegra_hdmi.c         | 8 +++++++-
 sys/arm/nvidia/drm2/tegra_host1x.c       | 7 ++++++-
 sys/arm/nvidia/tegra_abpmisc.c           | 7 ++++++-
 sys/arm/nvidia/tegra_efuse.c             | 7 ++++++-
 sys/arm/nvidia/tegra_i2c.c               | 7 ++++++-
 sys/arm/nvidia/tegra_mc.c                | 7 ++++++-
 sys/arm/nvidia/tegra_rtc.c               | 7 ++++++-
 sys/arm/ti/ti_adc.c                      | 7 ++++++-
 sys/arm64/nvidia/tegra210/max77620.c     | 7 ++++++-
 sys/arm64/nvidia/tegra210/max77620_rtc.c | 7 ++++++-
 sys/dev/dwwdt/dwwdt.c                    | 7 ++++++-
 sys/dev/ena/ena.c                        | 6 +++++-
 sys/dev/fdt/simplebus.c                  | 7 ++++++-
 sys/dev/gve/gve_main.c                   | 7 ++++++-
 sys/dev/iicbus/pmic/act8846.c            | 7 ++++++-
 sys/dev/mana/gdma_main.c                 | 7 ++++++-
 sys/powerpc/mpc85xx/pci_mpc85xx.c        | 7 ++++++-
 sys/powerpc/powermac/cuda.c              | 7 ++++++-
 sys/powerpc/powermac/pmu.c               | 7 ++++++-
 21 files changed, 126 insertions(+), 21 deletions(-)

diff --git a/sys/arm/nvidia/as3722.c b/sys/arm/nvidia/as3722.c
index ca5f7372aed1..ed5f47363b01 100644
--- a/sys/arm/nvidia/as3722.c
+++ b/sys/arm/nvidia/as3722.c
@@ -343,6 +343,11 @@ static int
 as3722_detach(device_t dev)
 {
 	struct as3722_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	if (sc->irq_h != NULL)
@@ -351,7 +356,7 @@ as3722_detach(device_t dev)
 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
 	LOCK_DESTROY(sc);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static phandle_t
diff --git a/sys/arm/nvidia/drm2/tegra_dc.c b/sys/arm/nvidia/drm2/tegra_dc.c
index f4168f161f5e..a8a0e3f4abf8 100644
--- a/sys/arm/nvidia/drm2/tegra_dc.c
+++ b/sys/arm/nvidia/drm2/tegra_dc.c
@@ -1393,6 +1393,11 @@ static int
 dc_detach(device_t dev)
 {
 	struct dc_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
@@ -1412,7 +1417,7 @@ dc_detach(device_t dev)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
 	LOCK_DESTROY(sc);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t tegra_dc_methods[] = {
diff --git a/sys/arm/nvidia/drm2/tegra_hdmi.c b/sys/arm/nvidia/drm2/tegra_hdmi.c
index e5ce30ac2eb3..067df21c889c 100644
--- a/sys/arm/nvidia/drm2/tegra_hdmi.c
+++ b/sys/arm/nvidia/drm2/tegra_hdmi.c
@@ -1272,6 +1272,12 @@ static int
 hdmi_detach(device_t dev)
 {
 	struct hdmi_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
+
 	sc = device_get_softc(dev);
 
 	TEGRA_DRM_DEREGISTER_CLIENT(device_get_parent(sc->dev), sc->dev);
@@ -1294,7 +1300,7 @@ hdmi_detach(device_t dev)
 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
 	if (sc->mem_res != NULL)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t tegra_hdmi_methods[] = {
diff --git a/sys/arm/nvidia/drm2/tegra_host1x.c b/sys/arm/nvidia/drm2/tegra_host1x.c
index e04a50e4c003..ee43efba5ba7 100644
--- a/sys/arm/nvidia/drm2/tegra_host1x.c
+++ b/sys/arm/nvidia/drm2/tegra_host1x.c
@@ -586,6 +586,11 @@ static int
 host1x_detach(device_t dev)
 {
 	struct host1x_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
@@ -608,7 +613,7 @@ host1x_detach(device_t dev)
 	if (sc->mem_res != NULL)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
 	LOCK_DESTROY(sc);
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t host1x_methods[] = {
diff --git a/sys/arm/nvidia/tegra_abpmisc.c b/sys/arm/nvidia/tegra_abpmisc.c
index 88f9ecde3f31..1f54a918f63b 100644
--- a/sys/arm/nvidia/tegra_abpmisc.c
+++ b/sys/arm/nvidia/tegra_abpmisc.c
@@ -166,13 +166,18 @@ static int
 tegra_abpmisc_detach(device_t dev)
 {
 	struct tegra_abpmisc_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	if (sc->abp_misc_res != NULL)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->abp_misc_res);
 	if (sc->strap_opt_res != NULL)
 		bus_release_resource(dev, SYS_RES_MEMORY, 1, sc->strap_opt_res);
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t tegra_abpmisc_methods[] = {
diff --git a/sys/arm/nvidia/tegra_efuse.c b/sys/arm/nvidia/tegra_efuse.c
index 35d9380a18a5..dc637e6d0bec 100644
--- a/sys/arm/nvidia/tegra_efuse.c
+++ b/sys/arm/nvidia/tegra_efuse.c
@@ -499,6 +499,11 @@ static int
 tegra_efuse_detach(device_t dev)
 {
 	struct tegra_efuse_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	dev_sc = NULL;
@@ -509,7 +514,7 @@ tegra_efuse_detach(device_t dev)
 	if (sc->mem_res != NULL)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t tegra_efuse_methods[] = {
diff --git a/sys/arm/nvidia/tegra_i2c.c b/sys/arm/nvidia/tegra_i2c.c
index 0fe1aa7ba48e..b01f1a1fdce4 100644
--- a/sys/arm/nvidia/tegra_i2c.c
+++ b/sys/arm/nvidia/tegra_i2c.c
@@ -740,6 +740,11 @@ static int
 tegra_i2c_detach(device_t dev)
 {
 	struct tegra_i2c_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	tegra_i2c_hw_init(sc);
@@ -753,7 +758,7 @@ tegra_i2c_detach(device_t dev)
 	LOCK_DESTROY(sc);
 	if (sc->iicbus)
 		device_delete_child(dev, sc->iicbus);
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static phandle_t
diff --git a/sys/arm/nvidia/tegra_mc.c b/sys/arm/nvidia/tegra_mc.c
index 020c9617b453..0f05c32117e9 100644
--- a/sys/arm/nvidia/tegra_mc.c
+++ b/sys/arm/nvidia/tegra_mc.c
@@ -280,6 +280,11 @@ static int
 tegra_mc_detach(device_t dev)
 {
 	struct tegra_mc_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	if (sc->irq_h != NULL)
@@ -290,7 +295,7 @@ tegra_mc_detach(device_t dev)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
 
 	LOCK_DESTROY(sc);
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t tegra_mc_methods[] = {
diff --git a/sys/arm/nvidia/tegra_rtc.c b/sys/arm/nvidia/tegra_rtc.c
index 07bf51628665..09bd42bc64e3 100644
--- a/sys/arm/nvidia/tegra_rtc.c
+++ b/sys/arm/nvidia/tegra_rtc.c
@@ -266,6 +266,11 @@ static int
 tegra_rtc_detach(device_t dev)
 {
 	struct tegra_rtc_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	if (sc->irq_h != NULL)
@@ -276,7 +281,7 @@ tegra_rtc_detach(device_t dev)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
 
 	LOCK_DESTROY(sc);
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t tegra_rtc_methods[] = {
diff --git a/sys/arm/ti/ti_adc.c b/sys/arm/ti/ti_adc.c
index 5a3a337afff5..d13dd87001de 100644
--- a/sys/arm/ti/ti_adc.c
+++ b/sys/arm/ti/ti_adc.c
@@ -915,6 +915,11 @@ static int
 ti_adc_detach(device_t dev)
 {
 	struct ti_adc_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
@@ -938,7 +943,7 @@ ti_adc_detach(device_t dev)
 	if (sc->sc_mem_res)
 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t ti_adc_methods[] = {
diff --git a/sys/arm64/nvidia/tegra210/max77620.c b/sys/arm64/nvidia/tegra210/max77620.c
index b33d73e71f90..a7d938cb8123 100644
--- a/sys/arm64/nvidia/tegra210/max77620.c
+++ b/sys/arm64/nvidia/tegra210/max77620.c
@@ -450,6 +450,11 @@ static int
 max77620_detach(device_t dev)
 {
 	struct max77620_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	if (sc->irq_h != NULL)
@@ -458,7 +463,7 @@ max77620_detach(device_t dev)
 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
 	LOCK_DESTROY(sc);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static phandle_t
diff --git a/sys/arm64/nvidia/tegra210/max77620_rtc.c b/sys/arm64/nvidia/tegra210/max77620_rtc.c
index 29808af27819..dca90549443c 100644
--- a/sys/arm64/nvidia/tegra210/max77620_rtc.c
+++ b/sys/arm64/nvidia/tegra210/max77620_rtc.c
@@ -366,11 +366,16 @@ static int
 max77620_rtc_detach(device_t dev)
 {
 	struct max77620_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	LOCK_DESTROY(sc);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 /*
diff --git a/sys/dev/dwwdt/dwwdt.c b/sys/dev/dwwdt/dwwdt.c
index 89f94fff9bad..c433317a8d87 100644
--- a/sys/dev/dwwdt/dwwdt.c
+++ b/sys/dev/dwwdt/dwwdt.c
@@ -309,6 +309,7 @@ static int
 dwwdt_detach(device_t dev)
 {
 	struct dwwdt_softc *sc = device_get_softc(dev);
+	int error;
 
 	if (dwwdt_started(sc)) {
 		/*
@@ -318,6 +319,10 @@ dwwdt_detach(device_t dev)
 		return (EBUSY);
 	}
 
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
+
 	EVENTHANDLER_DEREGISTER(watchdog_list, sc->sc_evtag);
 	sc->sc_evtag = NULL;
 
@@ -337,7 +342,7 @@ dwwdt_detach(device_t dev)
 		    sc->sc_mem_res);
 	}
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static int
diff --git a/sys/dev/ena/ena.c b/sys/dev/ena/ena.c
index e9d4530e9085..f0b6cec1bb61 100644
--- a/sys/dev/ena/ena.c
+++ b/sys/dev/ena/ena.c
@@ -4030,6 +4030,10 @@ ena_detach(device_t pdev)
 		return (EBUSY);
 	}
 
+	rc = bus_generic_detach(pdev);
+	if (rc != 0)
+		return (rc);
+
 	ether_ifdetach(adapter->ifp);
 
 	ifmedia_removeall(&adapter->media);
@@ -4096,7 +4100,7 @@ ena_detach(device_t pdev)
 
 	free(ena_dev, M_DEVBUF);
 
-	return (bus_generic_detach(pdev));
+	return (0);
 }
 
 /******************************************************************************
diff --git a/sys/dev/fdt/simplebus.c b/sys/dev/fdt/simplebus.c
index 37db238f2108..0f41d3433d75 100644
--- a/sys/dev/fdt/simplebus.c
+++ b/sys/dev/fdt/simplebus.c
@@ -189,12 +189,17 @@ int
 simplebus_detach(device_t dev)
 {
 	struct		simplebus_softc *sc;
+	int	rv;
+
+	rv = bus_generic_detach(dev);
+	if (rv != 0)
+		return (rv);
 
 	sc = device_get_softc(dev);
 	if (sc->ranges != NULL)
 		free(sc->ranges, M_DEVBUF);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 void
diff --git a/sys/dev/gve/gve_main.c b/sys/dev/gve/gve_main.c
index f8a37b9f37a9..15d57ed0f2ac 100644
--- a/sys/dev/gve/gve_main.c
+++ b/sys/dev/gve/gve_main.c
@@ -820,6 +820,11 @@ gve_detach(device_t dev)
 {
 	struct gve_priv *priv = device_get_softc(dev);
 	if_t ifp = priv->ifp;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	ether_ifdetach(ifp);
 
@@ -836,7 +841,7 @@ gve_detach(device_t dev)
 	taskqueue_free(priv->service_tq);
 
 	if_free(ifp);
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t gve_methods[] = {
diff --git a/sys/dev/iicbus/pmic/act8846.c b/sys/dev/iicbus/pmic/act8846.c
index 5e166247f79b..e55fc70986a8 100644
--- a/sys/dev/iicbus/pmic/act8846.c
+++ b/sys/dev/iicbus/pmic/act8846.c
@@ -226,11 +226,16 @@ static int
 act8846_detach(device_t dev)
 {
 	struct act8846_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	LOCK_DESTROY(sc);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static device_method_t act8846_methods[] = {
diff --git a/sys/dev/mana/gdma_main.c b/sys/dev/mana/gdma_main.c
index 59ac6911c784..b339badad925 100644
--- a/sys/dev/mana/gdma_main.c
+++ b/sys/dev/mana/gdma_main.c
@@ -1879,6 +1879,11 @@ static int
 mana_gd_detach(device_t dev)
 {
 	struct gdma_context *gc = device_get_softc(dev);
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	mana_remove(&gc->mana);
 
@@ -1890,7 +1895,7 @@ mana_gd_detach(device_t dev)
 
 	pci_disable_busmaster(dev);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 
diff --git a/sys/powerpc/mpc85xx/pci_mpc85xx.c b/sys/powerpc/mpc85xx/pci_mpc85xx.c
index f4080ce3b75c..8e349df03a51 100644
--- a/sys/powerpc/mpc85xx/pci_mpc85xx.c
+++ b/sys/powerpc/mpc85xx/pci_mpc85xx.c
@@ -673,12 +673,17 @@ static int
 fsl_pcib_detach(device_t dev)
 {
 	struct fsl_pcib_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
 	mtx_destroy(&sc->sc_cfg_mtx);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static int
diff --git a/sys/powerpc/powermac/cuda.c b/sys/powerpc/powermac/cuda.c
index 4d120747e0f0..367eb7a059c2 100644
--- a/sys/powerpc/powermac/cuda.c
+++ b/sys/powerpc/powermac/cuda.c
@@ -259,6 +259,11 @@ cuda_attach(device_t dev)
 
 static int cuda_detach(device_t dev) {
 	struct cuda_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
@@ -267,7 +272,7 @@ static int cuda_detach(device_t dev) {
 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
 	mtx_destroy(&sc->sc_mutex);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static uint8_t
diff --git a/sys/powerpc/powermac/pmu.c b/sys/powerpc/powermac/pmu.c
index 2d38545a7046..e0358c6f9887 100644
--- a/sys/powerpc/powermac/pmu.c
+++ b/sys/powerpc/powermac/pmu.c
@@ -524,6 +524,11 @@ static int
 pmu_detach(device_t dev) 
 {
 	struct pmu_softc *sc;
+	int error;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
@@ -535,7 +540,7 @@ pmu_detach(device_t dev)
 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
 	mtx_destroy(&sc->sc_mutex);
 
-	return (bus_generic_detach(dev));
+	return (0);
 }
 
 static uint8_t