From patchwork Wed Oct 26 23:29:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Norris X-Patchwork-Id: 9398689 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 3FE1060236 for ; Wed, 26 Oct 2016 23:30:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 070E229E3D for ; Wed, 26 Oct 2016 23:30:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED08329E3E; Wed, 26 Oct 2016 23:30:32 +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,RCVD_IN_DNSWL_HI,RCVD_IN_SORBS_SPAM autolearn=unavailable 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 5DB7529E38 for ; Wed, 26 Oct 2016 23:30:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933301AbcJZXaH (ORCPT ); Wed, 26 Oct 2016 19:30:07 -0400 Received: from mail-pf0-f181.google.com ([209.85.192.181]:35248 "EHLO mail-pf0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754619AbcJZXaF (ORCPT ); Wed, 26 Oct 2016 19:30:05 -0400 Received: by mail-pf0-f181.google.com with SMTP id s8so4865348pfj.2 for ; Wed, 26 Oct 2016 16:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id; bh=CUGVWIM9sQcspjMg0VH37x7s4GTp7DOBIP1JbTOV80k=; b=W6pie7QPx7HK8q00rg+S2ESOVDrflxZ1BgHbE6tnYdV8QH/aMI/rxGQCbE0ss/Mh+1 WesdnEuRt+UNDUZVhA9JX7uUozCIyDhLbVW3kU8ZbSSNb68N347cHP9SVOA6E2Kg/12/ hMWqXQQFcdSas1I7awjDrsnwO5TkrqYA9s+c0= 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; bh=CUGVWIM9sQcspjMg0VH37x7s4GTp7DOBIP1JbTOV80k=; b=L9IjOyb76kUYiZx7l36UXlGJ37EWbAz+/EhrEgnXGvcFydPkQLsT70DsxDG/nL1B1m 22RvxK2W2Tnd13ZNuRT54aKX+CizM/5YVDYhxMTZHoRcKQFAqE12gjnBoV0eAMUuU62a 99IqncflIkjQYgenE3qyRUMMyIndqSHR9lGBjW6fQTSuR6lCy7apnaFqSiyc2ys0RRfb RE9GMybh+zCmoTPGI22WcBnj27cmuzdSXnWbwt0BBEeFG1YuUjp9kuYR44bCM/KLSCHE sX7HsC8qSP+8P1CRGpDpWq+Fz/6TzkELDrebY1Z8qAdOcMl9Tf6DZzUTJu40s7VqzCqc GSgw== X-Gm-Message-State: ABUngvePAlIlIwk+LpkAtY5eF1k/hfFGv+CpStHJDrkfgzxuWf8IUOk38PG0BBvhK7DNNj99 X-Received: by 10.99.202.72 with SMTP id o8mr6928743pgi.93.1477524595843; Wed, 26 Oct 2016 16:29:55 -0700 (PDT) Received: from ban.mtv.corp.google.com ([172.22.64.120]) by smtp.gmail.com with ESMTPSA id zh13sm6600392pab.4.2016.10.26.16.29.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 26 Oct 2016 16:29:55 -0700 (PDT) From: Brian Norris To: Amitkumar Karwar , Nishant Sarmukadam Cc: , Kalle Valo , linux-wireless@vger.kernel.org, Cathy Luo , Rajat Jain , Dmitry Torokhov , Brian Norris Subject: [PATCH] mwifiex: don't do unbalanced free()'ing in cleanup_if() Date: Wed, 26 Oct 2016 16:29:20 -0700 Message-Id: <1477524560-49226-1-git-send-email-briannorris@chromium.org> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 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 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." 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 063c707844d3..3047c1ab944a 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; @@ -2815,7 +2815,6 @@ static int mwifiex_pcie_init(struct mwifiex_adapter *adapter) err_set_dma_mask: pci_disable_device(pdev); err_enable_dev: - pci_set_drvdata(pdev, NULL); return ret; } @@ -2849,9 +2848,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 8718950004f3..f04cf5a551b3 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 @@ mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id) 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 73eb0846db21..57ed834ba296 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 = {