From patchwork Wed Nov 2 02:24:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xinming Hu X-Patchwork-Id: 9408393 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 1FDE060721 for ; Wed, 2 Nov 2016 02:25:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0957629D22 for ; Wed, 2 Nov 2016 02:25:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EEA7629D25; Wed, 2 Nov 2016 02:25:17 +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 83A6429D22 for ; Wed, 2 Nov 2016 02:25:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755175AbcKBCZN (ORCPT ); Tue, 1 Nov 2016 22:25:13 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:36804 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753888AbcKBCZM (ORCPT ); Tue, 1 Nov 2016 22:25:12 -0400 Received: by mail-pf0-f194.google.com with SMTP id n85so261032pfi.3 for ; Tue, 01 Nov 2016 19:25:11 -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; bh=3CsrPXZSZnuNKCnpNhMSC4btB1qNNAwsekK8/Y9HsCQ=; b=ii2H+dGDo2sllrj8YFP80rLqvp+t39exYhyzm1cSez5V0U4Dcjugyg596HJNO587cw TGGH7gZiAfhv7QYiV0yva0PjLcVymbFj0QdAapKt+kPbASmSDbaUD42PR8nU/ZQbO0H5 XsZmT+7RINJYumVFmgvOwKSeNhpSUufI13nX5kTF5R1yeAaBNJQCwJRRoL4jk+rBd4+X MTFEOdVY5Eaz/hxkgrhjri8VPJrKr1GpapWaFapVGSSagv4UzTVDgJexd65rnvETiiwt FqJwam9LI+RHJfPZMnxv8OqGiX8XN3GQpvEaB3BXUTTU4ltF3o5gIpxk7H1l5B0Alvw4 7xfQ== 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=3CsrPXZSZnuNKCnpNhMSC4btB1qNNAwsekK8/Y9HsCQ=; b=Li/s0W3GkdBKgcKbJLlze4cLKW4ncq5pXxUHE9HYUSqYyty29r2rw+X6xfqo3HLQqq WWQhAfulKVGIhdSZ87LnXXm6J+nR3RAC3lnLG3NoAfAa9njzRZC73z+55WJhAylbpuyb Im2zmjaNED2uDmu6KRbPyMO2gpVNHek3rQLdMFIiemcbYN6pMkhbnd54asLvot2K6eSv 5YLymypNBoMqNfcHNPi0E8RMABtP4sML1fyc5m6DTWrz5fkOoVkcqrSqmpI1wLJU6/uQ CQ+sJ7OtXnxxTWqSy9sveZUbEtdIyWhGJW2kGn5snOotIqqi0vqWpGOhBw1Jvd2I7cVx /HqQ== X-Gm-Message-State: ABUngvd2TESXsTyjBy2yylv0fsRc8ZhLRNGC271BP5TGlZdAtn4guI9N4St/HJN2fBeTyQ== X-Received: by 10.99.177.69 with SMTP id g5mr1948753pgp.144.1478053511264; Tue, 01 Nov 2016 19:25:11 -0700 (PDT) Received: from localhost.localdomain ([216.207.47.6]) by smtp.gmail.com with ESMTPSA id tx10sm36409076pab.40.2016.11.01.19.25.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 01 Nov 2016 19:25:10 -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 v2 03/12] mwifiex: don't do unbalanced free()'ing in cleanup_if() Date: Wed, 2 Nov 2016 10:24:39 +0800 Message-Id: <1478053488-16042-1-git-send-email-huxinming820@marvell.com> X-Mailer: git-send-email 1.9.1 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. --- v2: Same as v1 --- 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 = {