From e9ddeea551a2805e5eb0b887a13a2f9f5910047e Mon Sep 17 00:00:00 2001 From: msaitoh Date: Tue, 21 Jan 2020 14:55:55 +0000 Subject: [PATCH] Fix the freeing code for some error paths. Found and tested by Patrick Welche. --- sys/dev/pci/ixgbe/ix_txrx.c | 23 ++++++++++++++++++++++- sys/dev/pci/ixgbe/ixgbe.c | 18 ++++-------------- sys/dev/pci/ixgbe/ixgbe.h | 3 ++- sys/dev/pci/ixgbe/ixgbe_netbsd.c | 8 ++++---- sys/dev/pci/ixgbe/ixv.c | 14 +++----------- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/sys/dev/pci/ixgbe/ix_txrx.c b/sys/dev/pci/ixgbe/ix_txrx.c index 1004fe047b96..d40791bee164 100644 --- a/sys/dev/pci/ixgbe/ix_txrx.c +++ b/sys/dev/pci/ixgbe/ix_txrx.c @@ -1,4 +1,4 @@ -/* $NetBSD: ix_txrx.c,v 1.59 2020/01/20 07:19:04 msaitoh Exp $ */ +/* $NetBSD: ix_txrx.c,v 1.60 2020/01/21 14:55:55 msaitoh Exp $ */ /****************************************************************************** @@ -2353,3 +2353,24 @@ err_tx_desc: free(adapter->queues, M_DEVBUF); return (error); } /* ixgbe_allocate_queues */ + +/************************************************************************ + * ixgbe_free_queues + * + * Free descriptors for the transmit and receive rings, and then + * the memory associated with each. + ************************************************************************/ +void +ixgbe_free_queues(struct adapter *adapter) +{ + struct ix_queue *que; + int i; + + ixgbe_free_transmit_structures(adapter); + ixgbe_free_receive_structures(adapter); + for (i = 0; i < adapter->num_queues; i++) { + que = &adapter->queues[i]; + mutex_destroy(&que->dc_mtx); + } + free(adapter->queues, M_DEVBUF); +} /* ixgbe_free_queues */ diff --git a/sys/dev/pci/ixgbe/ixgbe.c b/sys/dev/pci/ixgbe/ixgbe.c index 1d83b3221bca..3221640eeee3 100644 --- a/sys/dev/pci/ixgbe/ixgbe.c +++ b/sys/dev/pci/ixgbe/ixgbe.c @@ -1,4 +1,4 @@ -/* $NetBSD: ixgbe.c,v 1.220 2020/01/03 12:59:46 pgoyette Exp $ */ +/* $NetBSD: ixgbe.c,v 1.221 2020/01/21 14:55:55 msaitoh Exp $ */ /****************************************************************************** @@ -1059,9 +1059,7 @@ ixgbe_attach(device_t parent, device_t dev, void *aux) error = ixgbe_allocate_msix(adapter, pa); if (error) { /* Free allocated queue structures first */ - ixgbe_free_transmit_structures(adapter); - ixgbe_free_receive_structures(adapter); - free(adapter->queues, M_DEVBUF); + ixgbe_free_queues(adapter); /* Fallback to legacy interrupt */ adapter->feat_en &= ~IXGBE_FEATURE_MSIX; @@ -1236,9 +1234,7 @@ ixgbe_attach(device_t parent, device_t dev, void *aux) return; err_late: - ixgbe_free_transmit_structures(adapter); - ixgbe_free_receive_structures(adapter); - free(adapter->queues, M_DEVBUF); + ixgbe_free_queues(adapter); err_out: ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT); ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD; @@ -3712,13 +3708,7 @@ ixgbe_detach(device_t dev, int flags) evcnt_detach(&stats->ptc1023); evcnt_detach(&stats->ptc1522); - ixgbe_free_transmit_structures(adapter); - ixgbe_free_receive_structures(adapter); - for (i = 0; i < adapter->num_queues; i++) { - struct ix_queue * que = &adapter->queues[i]; - mutex_destroy(&que->dc_mtx); - } - free(adapter->queues, M_DEVBUF); + ixgbe_free_queues(adapter); free(adapter->mta, M_DEVBUF); IXGBE_CORE_LOCK_DESTROY(adapter); diff --git a/sys/dev/pci/ixgbe/ixgbe.h b/sys/dev/pci/ixgbe/ixgbe.h index a465c9598fb1..aea552436aa6 100644 --- a/sys/dev/pci/ixgbe/ixgbe.h +++ b/sys/dev/pci/ixgbe/ixgbe.h @@ -1,4 +1,4 @@ -/* $NetBSD: ixgbe.h,v 1.61 2020/01/20 07:19:04 msaitoh Exp $ */ +/* $NetBSD: ixgbe.h,v 1.62 2020/01/21 14:55:55 msaitoh Exp $ */ /****************************************************************************** SPDX-License-Identifier: BSD-3-Clause @@ -768,6 +768,7 @@ void ixgbe_deferred_mq_start_work(struct work *, void *); void ixgbe_drain_all(struct adapter *); int ixgbe_allocate_queues(struct adapter *); +void ixgbe_free_queues(struct adapter *); int ixgbe_setup_transmit_structures(struct adapter *); void ixgbe_free_transmit_structures(struct adapter *); int ixgbe_setup_receive_structures(struct adapter *); diff --git a/sys/dev/pci/ixgbe/ixgbe_netbsd.c b/sys/dev/pci/ixgbe/ixgbe_netbsd.c index a4126b01f6a7..729573cf5e97 100644 --- a/sys/dev/pci/ixgbe/ixgbe_netbsd.c +++ b/sys/dev/pci/ixgbe/ixgbe_netbsd.c @@ -1,4 +1,4 @@ -/* $NetBSD: ixgbe_netbsd.c,v 1.11 2020/01/20 07:19:04 msaitoh Exp $ */ +/* $NetBSD: ixgbe_netbsd.c,v 1.12 2020/01/21 14:55:55 msaitoh Exp $ */ /* * Copyright (c) 2011 The NetBSD Foundation, Inc. * All rights reserved. @@ -225,10 +225,10 @@ ixgbe_jcl_destroy(struct adapter *adapter, struct rx_ring *rxr) { ixgbe_extmem_head_t *eh = &rxr->jcl_head; - /* Free all dmamem */ - ixgbe_jcl_freeall(adapter, rxr); - if (eh->eh_initialized) { + /* Free all dmamem */ + ixgbe_jcl_freeall(adapter, rxr); + mutex_destroy(&eh->eh_mtx); eh->eh_initialized = false; } diff --git a/sys/dev/pci/ixgbe/ixv.c b/sys/dev/pci/ixgbe/ixv.c index 74f44286e491..c11ceee62410 100644 --- a/sys/dev/pci/ixgbe/ixv.c +++ b/sys/dev/pci/ixgbe/ixv.c @@ -1,4 +1,4 @@ -/*$NetBSD: ixv.c,v 1.143 2019/12/17 05:49:01 msaitoh Exp $*/ +/*$NetBSD: ixv.c,v 1.144 2020/01/21 14:55:55 msaitoh Exp $*/ /****************************************************************************** @@ -547,9 +547,7 @@ ixv_attach(device_t parent, device_t dev, void *aux) return; err_late: - ixgbe_free_transmit_structures(adapter); - ixgbe_free_receive_structures(adapter); - free(adapter->queues, M_DEVBUF); + ixgbe_free_queues(adapter); err_out: ixv_free_pci_resources(adapter); IXGBE_CORE_LOCK_DESTROY(adapter); @@ -674,13 +672,7 @@ ixv_detach(device_t dev, int flags) evcnt_detach(&hw->mbx.stats.reqs); evcnt_detach(&hw->mbx.stats.rsts); - ixgbe_free_transmit_structures(adapter); - ixgbe_free_receive_structures(adapter); - for (int i = 0; i < adapter->num_queues; i++) { - struct ix_queue *lque = &adapter->queues[i]; - mutex_destroy(&lque->dc_mtx); - } - free(adapter->queues, M_DEVBUF); + ixgbe_free_queues(adapter); IXGBE_CORE_LOCK_DESTROY(adapter);