git: dd47685dbdc8 - stable/13 - gpiobus: Use a bus_child_deleted method to free ivars for children

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Sun, 01 Dec 2024 04:58:03 UTC
The branch stable/13 has been updated by jhb:

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

commit dd47685dbdc89e33632d1e7da25e112849c324f7
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2024-11-01 14:09:39 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-11-30 21:43:55 +0000

    gpiobus: Use a bus_child_deleted method to free ivars for children
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D47369
    
    (cherry picked from commit c9e880c0ceef9541625c18add3f5a8499137e016)
---
 sys/dev/gpio/gpiobus.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/sys/dev/gpio/gpiobus.c b/sys/dev/gpio/gpiobus.c
index 4dceea15b00d..8e4b8be7e931 100644
--- a/sys/dev/gpio/gpiobus.c
+++ b/sys/dev/gpio/gpiobus.c
@@ -587,9 +587,7 @@ static int
 gpiobus_detach(device_t dev)
 {
 	struct gpiobus_softc *sc;
-	struct gpiobus_ivar *devi;
-	device_t *devlist;
-	int i, err, ndevs;
+	int i, err;
 
 	sc = GPIOBUS_SOFTC(dev);
 	KASSERT(mtx_initialized(&sc->sc_mtx),
@@ -598,17 +596,9 @@ gpiobus_detach(device_t dev)
 
 	if ((err = bus_generic_detach(dev)) != 0)
 		return (err);
-
-	if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
+	if ((err = device_delete_children(dev)) != 0)
 		return (err);
-	for (i = 0; i < ndevs; i++) {
-		devi = GPIOBUS_IVAR(devlist[i]);
-		gpiobus_free_ivars(devi);
-		resource_list_free(&devi->rl);
-		free(devi, M_DEVBUF);
-		device_delete_child(dev, devlist[i]);
-	}
-	free(devlist, M_TEMP);
+
 	rman_fini(&sc->sc_intr_rman);
 	if (sc->sc_pins) {
 		for (i = 0; i < sc->sc_npins; i++) {
@@ -723,6 +713,19 @@ gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
 	return (child);
 }
 
+static void
+gpiobus_child_deleted(device_t dev, device_t child)
+{
+	struct gpiobus_ivar *devi;
+
+	devi = GPIOBUS_IVAR(child);
+	if (devi == NULL)
+		return;
+	gpiobus_free_ivars(devi);
+	resource_list_free(&devi->rl);
+	free(devi, M_DEVBUF);
+}
+
 static int
 gpiobus_rescan(device_t dev)
 {
@@ -742,7 +745,6 @@ static void
 gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
 {
 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
-	struct gpiobus_ivar *devi;
 	device_t child;
 	const char *pins;
 	int irq, pinmask;
@@ -752,19 +754,14 @@ gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
 	}
 
 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
-	devi = GPIOBUS_IVAR(child);
 	if (resource_int_value(dname, dunit, "pins", &pinmask) == 0) {
 		if (gpiobus_parse_pins(sc, child, pinmask)) {
-			resource_list_free(&devi->rl);
-			free(devi, M_DEVBUF);
 			device_delete_child(bus, child);
 			return;
 		}
 	}
 	else if (resource_string_value(dname, dunit, "pin_list", &pins) == 0) {
 		if (gpiobus_parse_pin_list(sc, child, pins)) {
-			resource_list_free(&devi->rl);
-			free(devi, M_DEVBUF);
 			device_delete_child(bus, child);
 			return;
 		}
@@ -1103,6 +1100,7 @@ static device_method_t gpiobus_methods[] = {
 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
 	DEVMETHOD(bus_get_resource_list,	gpiobus_get_resource_list),
 	DEVMETHOD(bus_add_child,	gpiobus_add_child),
+	DEVMETHOD(bus_child_deleted,	gpiobus_child_deleted),
 	DEVMETHOD(bus_rescan,		gpiobus_rescan),
 	DEVMETHOD(bus_probe_nomatch,	gpiobus_probe_nomatch),
 	DEVMETHOD(bus_print_child,	gpiobus_print_child),