git: d2070e5fa983 - main - cxgbe: Don't leak memory resource if t4iov attach fails.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Wed, 15 Feb 2023 21:41:55 UTC
The branch main has been updated by jhb:

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

commit d2070e5fa983281e94a338d6e87eb29c7dd28505
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-02-15 21:34:00 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-02-15 21:34:00 +0000

    cxgbe: Don't leak memory resource if t4iov attach fails.
    
    Co-authored by: np
    Reviewed by:    np
    Sponsored by:   Chelsio Communications
    Differential Revision:  https://reviews.freebsd.org/D38580
---
 sys/dev/cxgbe/t4_iov.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/sys/dev/cxgbe/t4_iov.c b/sys/dev/cxgbe/t4_iov.c
index d3796d300947..ce9a97c69ed4 100644
--- a/sys/dev/cxgbe/t4_iov.c
+++ b/sys/dev/cxgbe/t4_iov.c
@@ -194,6 +194,7 @@ t4iov_attach(device_t dev)
 {
 	struct t4iov_softc *sc;
 	uint32_t pl_rev, whoami;
+	int error;
 
 	sc = device_get_softc(dev);
 	sc->sc_dev = dev;
@@ -217,10 +218,18 @@ t4iov_attach(device_t dev)
 
 	sc->sc_main = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
 	    pci_get_slot(dev), 4);
-	if (sc->sc_main == NULL)
+	if (sc->sc_main == NULL) {
+		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
+		    sc->regs_res);
 		return (ENXIO);
-	if (T4_IS_MAIN_READY(sc->sc_main) == 0)
-		return (t4iov_attach_child(dev));
+	}
+	if (T4_IS_MAIN_READY(sc->sc_main) == 0) {
+		error = t4iov_attach_child(dev);
+		if (error != 0)
+			bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
+			    sc->regs_res);
+		return (error);
+	}
 	return (0);
 }