diff mbox

Broadcom CNIC core network driver: fix mem leak on allocation failures in cnic_alloc_uio_rings()

Message ID alpine.LNX.2.00.1012262124320.20797@swampdragon.chaosbits.net
State Superseded, archived
Delegated to: David Miller
Headers show

Commit Message

Jesper Juhl Dec. 26, 2010, 8:30 p.m. UTC
Hi,

We are leaking memory in drivers/net/cnic.c::cnic_alloc_uio_rings() if 
either of the calls to dma_alloc_coherent() fail. This patch fixes it by 
freeing both the memory allocated with kzalloc() and memory allocated with 
previous calls to dma_alloc_coherent() when there's a failure.


Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 cnic.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

  compile tested only since I don't have the hardware to do a proper test.

Comments

Joe Perches Dec. 26, 2010, 8:54 p.m. UTC | #1
On Sun, 2010-12-26 at 21:30 +0100, Jesper Juhl wrote:
> We are leaking memory in drivers/net/cnic.c::cnic_alloc_uio_rings() if 
> either of the calls to dma_alloc_coherent() fail.
> Signed-off-by: Jesper Juhl <jj@chaosbits.net>
>  cnic.c |   10 ++++++++--
> diff --git a/drivers/net/cnic.c b/drivers/net/cnic.c
[]
> -	if (!udev->l2_ring)
> +	if (!udev->l2_ring) {
> +		kfree(udev);
>  		return -ENOMEM;
> +	}
[]
> -	if (!udev->l2_buf)
> +	if (!udev->l2_buf) {
> +		dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
> +				  udev->l2_ring, udev->l2_ring_map);
> +		kfree(udev);
>  		return -ENOMEM;
> +	}

Perhaps this would be more standard with a goto error / exit block

err_dma:
	dma_free_coherent();
err_udev:
	kfree(udev);
	return -ENOMEM;

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/net/cnic.c b/drivers/net/cnic.c
index 92bac19..f094159 100644
--- a/drivers/net/cnic.c
+++ b/drivers/net/cnic.c
@@ -939,16 +939,22 @@  static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
 	udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
 					   &udev->l2_ring_map,
 					   GFP_KERNEL | __GFP_COMP);
-	if (!udev->l2_ring)
+	if (!udev->l2_ring) {
+		kfree(udev);
 		return -ENOMEM;
+	}
 
 	udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
 	udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
 	udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
 					  &udev->l2_buf_map,
 					  GFP_KERNEL | __GFP_COMP);
-	if (!udev->l2_buf)
+	if (!udev->l2_buf) {
+		dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
+				  udev->l2_ring, udev->l2_ring_map);
+		kfree(udev);
 		return -ENOMEM;
+	}
 
 	write_lock(&cnic_dev_lock);
 	list_add(&udev->list, &cnic_udev_list);