Fix various resource leaks that can occur in the error paths of

iflib_device_register() and iflib_pseudo_register().

Reviewed by:	shurd
MFC after:	1 week
Sponsored by:	RG Nets
Differential Revision:	https://reviews.freebsd.org/D18760
This commit is contained in:
Patrick Kelsey 2019-01-22 00:56:44 +00:00
parent 1d34c9dac8
commit 7f3eb9dab3

View File

@ -4368,11 +4368,8 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
ctx->ifc_softc = sc;
if ((err = iflib_register(ctx)) != 0) {
if (ctx->ifc_flags & IFC_SC_ALLOCATED)
free(sc, M_IFLIB);
free(ctx, M_IFLIB);
device_printf(dev, "iflib_register failed %d\n", err);
return (err);
goto fail_ctx_free;
}
iflib_add_device_sysctl_pre(ctx);
@ -4382,9 +4379,8 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
iflib_reset_qvalues(ctx);
CTX_LOCK(ctx);
if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
CTX_UNLOCK(ctx);
device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
return (err);
goto fail_unlock;
}
_iflib_pre_assert(scctx);
ctx->ifc_txrx = *scctx->isc_txrx;
@ -4414,7 +4410,7 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
/* round down instead? */
device_printf(dev, "# rx descriptors must be a power of 2\n");
err = EINVAL;
goto fail;
goto fail_iflib_detach;
}
}
for (i = 0; i < sctx->isc_ntxqs; i++) {
@ -4422,7 +4418,7 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
device_printf(dev,
"# tx descriptors must be a power of 2");
err = EINVAL;
goto fail;
goto fail_iflib_detach;
}
}
@ -4492,7 +4488,7 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
/* Get memory for the station queues */
if ((err = iflib_queues_alloc(ctx))) {
device_printf(dev, "Unable to allocate queue memory\n");
goto fail;
goto fail_intr_free;
}
if ((err = iflib_qset_structures_setup(ctx)))
@ -4511,7 +4507,7 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
IFDI_INTR_DISABLE(ctx);
if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) {
device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err);
goto fail_intr_free;
goto fail_queues;
}
if (msix <= 1) {
rid = 0;
@ -4521,7 +4517,7 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
}
if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
device_printf(dev, "iflib_legacy_setup failed %d\n", err);
goto fail_intr_free;
goto fail_queues;
}
}
@ -4557,14 +4553,18 @@ iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ct
fail_detach:
ether_ifdetach(ctx->ifc_ifp);
fail_intr_free:
iflib_free_intr_mem(ctx);
fail_queues:
iflib_tx_structures_free(ctx);
iflib_rx_structures_free(ctx);
fail:
iflib_free_intr_mem(ctx);
fail_iflib_detach:
IFDI_DETACH(ctx);
fail_unlock:
CTX_UNLOCK(ctx);
fail_ctx_free:
if (ctx->ifc_flags & IFC_SC_ALLOCATED)
free(ctx->ifc_softc, M_IFLIB);
free(ctx, M_IFLIB);
return (err);
}
@ -4593,9 +4593,7 @@ iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
if ((err = iflib_register(ctx)) != 0) {
device_printf(dev, "%s: iflib_register failed %d\n", __func__, err);
free(sc, M_IFLIB);
free(ctx, M_IFLIB);
return (err);
goto fail_ctx_free;
}
iflib_add_device_sysctl_pre(ctx);
@ -4609,14 +4607,14 @@ iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
return (err);
goto fail_ctx_free;
}
if (sctx->isc_flags & IFLIB_GEN_MAC)
iflib_gen_mac(ctx);
if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name,
clctx->cc_params)) != 0) {
device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err);
return (err);
goto fail_ctx_free;
}
ifmedia_add(&ctx->ifc_media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
ifmedia_add(&ctx->ifc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
@ -4674,7 +4672,7 @@ iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
/* round down instead? */
device_printf(dev, "# rx descriptors must be a power of 2\n");
err = EINVAL;
goto fail;
goto fail_iflib_detach;
}
}
for (i = 0; i < sctx->isc_ntxqs; i++) {
@ -4682,7 +4680,7 @@ iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
device_printf(dev,
"# tx descriptors must be a power of 2");
err = EINVAL;
goto fail;
goto fail_iflib_detach;
}
}
@ -4728,7 +4726,7 @@ iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
/* Get memory for the station queues */
if ((err = iflib_queues_alloc(ctx))) {
device_printf(dev, "Unable to allocate queue memory\n");
goto fail;
goto fail_iflib_detach;
}
if ((err = iflib_qset_structures_setup(ctx))) {
@ -4768,8 +4766,11 @@ iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
fail_queues:
iflib_tx_structures_free(ctx);
iflib_rx_structures_free(ctx);
fail:
fail_iflib_detach:
IFDI_DETACH(ctx);
fail_ctx_free:
free(ctx->ifc_softc, M_IFLIB);
free(ctx, M_IFLIB);
return (err);
}