From patchwork Sun Dec 26 20:30:43 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesper Juhl X-Patchwork-Id: 76723 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 6B0C91007D2 for ; Mon, 27 Dec 2010 07:40:58 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752713Ab0LZUkW (ORCPT ); Sun, 26 Dec 2010 15:40:22 -0500 Received: from swampdragon.chaosbits.net ([90.184.90.115]:18421 "EHLO swampdragon.chaosbits.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752621Ab0LZUkV (ORCPT ); Sun, 26 Dec 2010 15:40:21 -0500 Received: by swampdragon.chaosbits.net (Postfix, from userid 1000) id 875C89403F; Sun, 26 Dec 2010 21:30:43 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by swampdragon.chaosbits.net (Postfix) with ESMTP id 802019403B; Sun, 26 Dec 2010 21:30:43 +0100 (CET) Date: Sun, 26 Dec 2010 21:30:43 +0100 (CET) From: Jesper Juhl To: netdev@vger.kernel.org cc: linux-kernel@vger.kernel.org, "John(Zongxi) Chen" , Michael Chan Subject: [PATCH] Broadcom CNIC core network driver: fix mem leak on allocation failures in cnic_alloc_uio_rings() Message-ID: User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 --- 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. 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);