From patchwork Tue Dec 18 22:47:26 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesper Juhl X-Patchwork-Id: 1893921 Return-Path: X-Original-To: patchwork-linux-mmc@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id ADDCF3FC64 for ; Tue, 18 Dec 2012 22:47:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752992Ab2LRWr3 (ORCPT ); Tue, 18 Dec 2012 17:47:29 -0500 Received: from swampdragon.chaosbits.net ([90.184.90.115]:45366 "EHLO swampdragon.chaosbits.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751367Ab2LRWr2 (ORCPT ); Tue, 18 Dec 2012 17:47:28 -0500 Received: by swampdragon.chaosbits.net (Postfix, from userid 1000) id 06EA19403D; Tue, 18 Dec 2012 23:47:27 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by swampdragon.chaosbits.net (Postfix) with ESMTP id F27B29403B; Tue, 18 Dec 2012 23:47:26 +0100 (CET) Date: Tue, 18 Dec 2012 23:47:26 +0100 (CET) From: Jesper Juhl To: linux-kernel@vger.kernel.org cc: linux-usb@vger.kernel.org, linux-mmc@vger.kernel.org, Chris Ball , Tony Olech , Greg Kroah-Hartman Subject: [PATCH][resend] MMC, vub300: Resolve mem leak in vub300_probe() and simplify the code a bit Message-ID: User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org In drivers/mmc/host/vub300.c::vub300_probe() we need both 'command_out_urb' and 'command_res_urb'. Currently we fail to free the former if allocating the latter fails. Fix that and simplify the code a bit at the same time by just doing both allocations and if either fails then free both - usb_free_urb() deals gracefully with NULL pointers, so this is safe. We also initialize 'retval' to '-ENOMEM' when we declare the variable, so there's no reason to re-set it to '-ENOMEM' before jumping to 'error0:' when one of the initial usb_alloc_urb() calls fail(). Also rename the 'error[0145]:' labels to be just 'error0' and 'error1'. Signed-off-by: Jesper Juhl --- drivers/mmc/host/vub300.c | 29 +++++++++++------------------ 1 files changed, 11 insertions(+), 18 deletions(-) This is a resend. I originally submitted this back in January: http://lkml.indiana.edu/hypermail/linux/kernel/1201.3/00305.html but got no response. Note: I have no real way to actually test this patch, so it is compile tested only. Please review carefully before applying. Also please keep me on Cc: when replying. diff --git a/drivers/mmc/host/vub300.c b/drivers/mmc/host/vub300.c index cb9f361..3f59fe1 100644 --- a/drivers/mmc/host/vub300.c +++ b/drivers/mmc/host/vub300.c @@ -2116,23 +2116,18 @@ static int vub300_probe(struct usb_interface *interface, udev->descriptor.idVendor, udev->descriptor.idProduct, manufacturer, product, serial_number); command_out_urb = usb_alloc_urb(0, GFP_KERNEL); - if (!command_out_urb) { - retval = -ENOMEM; - dev_err(&udev->dev, "not enough memory for command_out_urb\n"); - goto error0; - } command_res_urb = usb_alloc_urb(0, GFP_KERNEL); - if (!command_res_urb) { + if (!command_res_urb || !command_out_urb) { retval = -ENOMEM; - dev_err(&udev->dev, "not enough memory for command_res_urb\n"); - goto error1; + dev_err(&udev->dev, "not enough memory for command urbs\n"); + goto error0; } /* this also allocates memory for our VUB300 mmc host device */ mmc = mmc_alloc_host(sizeof(struct vub300_mmc_host), &udev->dev); if (!mmc) { retval = -ENOMEM; dev_err(&udev->dev, "not enough memory for the mmc_host\n"); - goto error4; + goto error0; } /* MMC core transfer sizes tunable parameters */ mmc->caps = 0; @@ -2285,7 +2280,7 @@ static int vub300_probe(struct usb_interface *interface, dev_err(&vub300->udev->dev, "Could not find two sets of bulk-in/out endpoint pairs\n"); retval = -EINVAL; - goto error5; + goto error1; } retval = usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0), @@ -2294,14 +2289,14 @@ static int vub300_probe(struct usb_interface *interface, 0x0000, 0x0000, &vub300->hc_info, sizeof(vub300->hc_info), HZ); if (retval < 0) - goto error5; + goto error1; retval = usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0), SET_ROM_WAIT_STATES, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, firmware_rom_wait_states, 0x0000, NULL, 0, HZ); if (retval < 0) - goto error5; + goto error1; dev_info(&vub300->udev->dev, "operating_mode = %s %s %d MHz %s %d byte USB packets\n", (mmc->caps & MMC_CAP_SDIO_IRQ) ? "IRQs" : "POLL", @@ -2316,14 +2311,14 @@ static int vub300_probe(struct usb_interface *interface, 0x0000, 0x0000, &vub300->system_port_status, sizeof(vub300->system_port_status), HZ); if (retval < 0) { - goto error4; + goto error0; } else if (sizeof(vub300->system_port_status) == retval) { vub300->card_present = (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0; vub300->read_only = (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0; } else { - goto error4; + goto error0; } usb_set_intfdata(interface, vub300); INIT_DELAYED_WORK(&vub300->pollwork, vub300_pollwork_thread); @@ -2351,17 +2346,15 @@ static int vub300_probe(struct usb_interface *interface, interface_to_InterfaceNumber(interface)); mmc_add_host(mmc); return 0; -error5: +error1: mmc_free_host(mmc); /* * and hence also frees vub300 * which is contained at the end of struct mmc */ -error4: +error0: usb_free_urb(command_res_urb); -error1: usb_free_urb(command_out_urb); -error0: usb_put_dev(udev); return retval; }