From patchwork Mon Oct 31 08:02:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xinming Hu X-Patchwork-Id: 9405121 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B0B586022E for ; Mon, 31 Oct 2016 08:02:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A16DD29217 for ; Mon, 31 Oct 2016 08:02:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95B1C2922F; Mon, 31 Oct 2016 08:02:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E9CCC29217 for ; Mon, 31 Oct 2016 08:02:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761960AbcJaICo (ORCPT ); Mon, 31 Oct 2016 04:02:44 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:33888 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761975AbcJaICi (ORCPT ); Mon, 31 Oct 2016 04:02:38 -0400 Received: by mail-pf0-f196.google.com with SMTP id y68so2243235pfb.1 for ; Mon, 31 Oct 2016 01:02:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eCUnG1zOOBk1lbPpiJjcO+NWHu1CDLlxAklfDZ0OK7c=; b=KmlGtHMkstubVGawAXYf1qN4XWFM483Oxh60ca8FumAPct4iJeb9nbUqVfZr1w/PL8 4a630O27ASFHFJ0HhxLUDX8S5EGnae8mCufYsx/3nVhqHTzhA56QXPFwNEyZy5D2sTF4 YatgJzD7sAgldBMuqV8qwEwG8G/Pc1lobHockqpw8P2Wu+HUzcdQN48seO5R8Zqds8Cd J/RRbiu6oow9qltfNbNacWLmEbEgX9ps5NlJ8O5/fp/L1dmwc8DAGNw4n2ye4J7VFc2n Pb+wHVEB0x01oYC2cU67C75CfvJ0k5Q4rhgg8NA4pNXQw7Y1hF4M7eipUaA/KmhXuS0f D/Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=eCUnG1zOOBk1lbPpiJjcO+NWHu1CDLlxAklfDZ0OK7c=; b=RUwbDvaeFGAi8wkQQSkNZZwTcIN5p/+L79hlbkBXoSmXzSU+YOaURyT+JwswFvVuEI cSbnvkZTxTCYn7qu5o0kWbwi7oayPypFKf3eHj+kaQlsebaVZqc097ukRoE/tOHjolv7 +ZB1OTfU8CoA6vRWDGDcCkk4a25y3je4CD2QARB6L67nikp07e5YR2b0Qe5UoQJ+Y1jL XUlFqJh38SNugh18h1YDWIg7u5etbboYRcfgGsz+NDzB2YQGq9CnK6xrC87AvnQAEfy8 yxfDH2/1TgEwCogK7cE788MiNvlWuI0ELbxsh4LjcoIsLDYoiT++pxMYXam0vTxmh5o9 IrNA== X-Gm-Message-State: ABUngvcsC7ZGT1Ocng32scDNemy4Wzub5kWw6iCKzE4puv5hzCl1n1c4w6tLrqVGEQTOAw== X-Received: by 10.99.161.2 with SMTP id b2mr38361879pgf.5.1477900957744; Mon, 31 Oct 2016 01:02:37 -0700 (PDT) Received: from localhost.localdomain ([216.207.47.6]) by smtp.gmail.com with ESMTPSA id k7sm34121196pan.8.2016.10.31.01.02.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 31 Oct 2016 01:02:37 -0700 (PDT) From: Xinming Hu X-Google-Original-From: Xinming Hu To: Linux Wireless Cc: Kalle Valo , Brian Norris , Dmitry Torokhov , Amitkumar Karwar , Cathy Luo , Brian Norris Subject: [PATCH 03/12] mwifiex: don't do unbalanced free()'ing in cleanup_if() Date: Mon, 31 Oct 2016 16:02:11 +0800 Message-Id: <1477900940-10549-3-git-send-email-huxinming820@marvell.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1477900940-10549-1-git-send-email-huxinming820@marvell.com> References: <1477900940-10549-1-git-send-email-huxinming820@marvell.com> Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Brian Norris The cleanup_if() callback is the inverse of init_if(). We allocate our 'card' interface structure in the probe() function, but we free it in cleanup_if(). That gives a few problems: (a) we leak this memory if probe() fails before we reach init_if() (b) we can't safely utilize 'card' after cleanup_if() -- namely, in remove() or suspend(), both of which might race with the cleanup paths in our asynchronous FW initialization path Solution: just use devm_kzalloc(), which will free this structure properly when the device is removed -- and drop the set_drvdata(..., NULL), since the driver core does this for us. This also removes the temptation to use drvdata == NULL as a hack for checking if the device has been "cleaned up." This is a preparatory step for adding a card-level completion structure to handle our FW init vs. remove/suspend races, in addition to fixing memory leaks. I *do* leave the set_drvdata(..., NULL) for the hacky SDIO mwifiex_recreate_adapter(), since the device core won't be able to clear that one for us. Signed-off-by: Brian Norris --- drivers/net/wireless/marvell/mwifiex/pcie.c | 5 +---- drivers/net/wireless/marvell/mwifiex/sdio.c | 16 ++++++++++------ drivers/net/wireless/marvell/mwifiex/usb.c | 7 +------ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/drivers/net/wireless/marvell/mwifiex/pcie.c b/drivers/net/wireless/marvell/mwifiex/pcie.c index 4aa5d91..e6bea02 100644 --- a/drivers/net/wireless/marvell/mwifiex/pcie.c +++ b/drivers/net/wireless/marvell/mwifiex/pcie.c @@ -189,7 +189,7 @@ static int mwifiex_pcie_probe(struct pci_dev *pdev, pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n", pdev->vendor, pdev->device, pdev->revision); - card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL); + card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL); if (!card) return -ENOMEM; @@ -2820,7 +2820,6 @@ err_req_region0: err_set_dma_mask: pci_disable_device(pdev); err_enable_dev: - pci_set_drvdata(pdev, NULL); return ret; } @@ -2854,9 +2853,7 @@ static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter) pci_disable_device(pdev); pci_release_region(pdev, 2); pci_release_region(pdev, 0); - pci_set_drvdata(pdev, NULL); } - kfree(card); } static int mwifiex_pcie_request_irq(struct mwifiex_adapter *adapter) diff --git a/drivers/net/wireless/marvell/mwifiex/sdio.c b/drivers/net/wireless/marvell/mwifiex/sdio.c index 8718950..f04cf5a 100644 --- a/drivers/net/wireless/marvell/mwifiex/sdio.c +++ b/drivers/net/wireless/marvell/mwifiex/sdio.c @@ -152,7 +152,7 @@ mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id) pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n", func->vendor, func->device, func->class, func->num); - card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL); + card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL); if (!card) return -ENOMEM; @@ -185,7 +185,7 @@ mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id) if (ret) { dev_err(&func->dev, "failed to enable function\n"); - goto err_free; + return ret; } /* device tree node parsing and platform specific configuration*/ @@ -210,8 +210,6 @@ err_disable: sdio_claim_host(func); sdio_disable_func(func); sdio_release_host(func); -err_free: - kfree(card); return ret; } @@ -2240,8 +2238,6 @@ static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter) kfree(card->mpa_rx.len_arr); kfree(card->mpa_tx.buf); kfree(card->mpa_rx.buf); - sdio_set_drvdata(card->func, NULL); - kfree(card); } /* @@ -2291,6 +2287,14 @@ static void mwifiex_recreate_adapter(struct sdio_mmc_card *card) mwifiex_sdio_remove(func); + /* + * Normally, we would let the driver core take care of releasing these. + * But we're not letting the driver core handle this one. See above + * TODO. + */ + sdio_set_drvdata(func, NULL); + devm_kfree(&func->dev, card); + /* power cycle the adapter */ sdio_claim_host(func); mmc_hw_reset(func->card->host); diff --git a/drivers/net/wireless/marvell/mwifiex/usb.c b/drivers/net/wireless/marvell/mwifiex/usb.c index 73eb084..57ed834 100644 --- a/drivers/net/wireless/marvell/mwifiex/usb.c +++ b/drivers/net/wireless/marvell/mwifiex/usb.c @@ -382,7 +382,7 @@ static int mwifiex_usb_probe(struct usb_interface *intf, struct usb_card_rec *card; u16 id_vendor, id_product, bcd_device, bcd_usb; - card = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL); + card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL); if (!card) return -ENOMEM; @@ -480,7 +480,6 @@ static int mwifiex_usb_probe(struct usb_interface *intf, if (ret) { pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret); usb_reset_device(udev); - kfree(card); return ret; } @@ -630,11 +629,7 @@ static void mwifiex_usb_disconnect(struct usb_interface *intf) "%s: removing card\n", __func__); mwifiex_remove_card(adapter, &add_remove_card_sem); - usb_set_intfdata(intf, NULL); usb_put_dev(interface_to_usbdev(intf)); - kfree(card); - - return; } static struct usb_driver mwifiex_usb_driver = {