linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Chen <peter.chen@nxp.com>
To: balbi@kernel.org
Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com,
	gregkh@linuxfoundation.org, stern@rowland.harvard.edu,
	Peter Chen <peter.chen@nxp.com>
Subject: [PATCH v2 4/6] usb: cdns3: gadget: fix possible memory leak
Date: Mon, 10 Aug 2020 10:25:08 +0800	[thread overview]
Message-ID: <20200810022510.6516-5-peter.chen@nxp.com> (raw)
In-Reply-To: <20200810022510.6516-1-peter.chen@nxp.com>

If cdns3_gadget_start is failed, it never frees cdns3_device structure.
Meanwhile, there is no release function for gadget device, it causes
there is no sync with driver core.

To fix this, we add release function for gadget device, and free
cdns3_device structure at there. Meanwhile, With the new UDC core
APIs, we could work with driver core better to handle memory leak
issue.

Signed-off-by: Peter Chen <peter.chen@nxp.com>
---
Changes from v1:
- Simplify the method of get private data pointer at .release function

 drivers/usb/cdns3/gadget.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/cdns3/gadget.c b/drivers/usb/cdns3/gadget.c
index 814338476a2d..e2925a29bee5 100644
--- a/drivers/usb/cdns3/gadget.c
+++ b/drivers/usb/cdns3/gadget.c
@@ -3043,6 +3043,14 @@ static int cdns3_init_eps(struct cdns3_device *priv_dev)
 	return -ENOMEM;
 }
 
+static void cdns3_gadget_release(struct device *dev)
+{
+	struct cdns3_device *priv_dev = container_of(dev,
+			struct cdns3_device, gadget.dev);
+
+	kfree(priv_dev);
+}
+
 void cdns3_gadget_exit(struct cdns3 *cdns)
 {
 	struct cdns3_device *priv_dev;
@@ -3054,7 +3062,7 @@ void cdns3_gadget_exit(struct cdns3 *cdns)
 	pm_runtime_mark_last_busy(cdns->dev);
 	pm_runtime_put_autosuspend(cdns->dev);
 
-	usb_del_gadget_udc(&priv_dev->gadget);
+	usb_del_gadget(&priv_dev->gadget);
 
 	cdns3_free_all_eps(priv_dev);
 
@@ -3074,7 +3082,7 @@ void cdns3_gadget_exit(struct cdns3 *cdns)
 			  priv_dev->setup_dma);
 
 	kfree(priv_dev->zlp_buf);
-	kfree(priv_dev);
+	usb_put_gadget(&priv_dev->gadget);
 	cdns->gadget_dev = NULL;
 	cdns3_drd_switch_gadget(cdns, 0);
 }
@@ -3089,6 +3097,8 @@ static int cdns3_gadget_start(struct cdns3 *cdns)
 	if (!priv_dev)
 		return -ENOMEM;
 
+	usb_initialize_gadget(cdns->dev, &priv_dev->gadget,
+			cdns3_gadget_release);
 	cdns->gadget_dev = priv_dev;
 	priv_dev->sysdev = cdns->dev;
 	priv_dev->dev = cdns->dev;
@@ -3176,10 +3186,9 @@ static int cdns3_gadget_start(struct cdns3 *cdns)
 	}
 
 	/* add USB gadget device */
-	ret = usb_add_gadget_udc(priv_dev->dev, &priv_dev->gadget);
+	ret = usb_add_gadget(&priv_dev->gadget);
 	if (ret < 0) {
-		dev_err(priv_dev->dev,
-			"Failed to register USB device controller\n");
+		dev_err(priv_dev->dev, "Failed to add gadget\n");
 		goto err4;
 	}
 
@@ -3192,6 +3201,7 @@ static int cdns3_gadget_start(struct cdns3 *cdns)
 err2:
 	cdns3_free_all_eps(priv_dev);
 err1:
+	usb_put_gadget(&priv_dev->gadget);
 	cdns->gadget_dev = NULL;
 	return ret;
 }
-- 
2.17.1


  parent reply	other threads:[~2020-08-10  2:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-10  2:25 [PATCH v2 0/6] USB: UDC: Fix memory leaks by expanding the API Peter Chen
2020-08-10  2:25 ` [PATCH v2 1/6] USB: UDC: Expand device model API interface Peter Chen
2020-08-10  2:25 ` [PATCH v2 2/6] USB: UDC: net2280: Fix memory leaks Peter Chen
2020-08-10  2:25 ` [PATCH v2 3/6] USB: UDC: net2272: " Peter Chen
2020-08-10  2:25 ` Peter Chen [this message]
2020-08-10  2:25 ` [PATCH v2 5/6] usb: dwc3: allocate gadget structure dynamically Peter Chen
2020-08-10  2:25 ` [PATCH v2 6/6] Revert "usb: udc: allow adding and removing the same gadget device" Peter Chen
2020-08-18  9:33   ` Greg KH
2020-08-18 10:05     ` Peter Chen
2020-08-18 14:46       ` stern
2020-08-19  1:31         ` Peter Chen
2020-08-19 14:52           ` stern
2020-08-20  3:40             ` Peter Chen
2020-08-20 14:25               ` stern
2020-08-18  9:33 ` [PATCH v2 0/6] USB: UDC: Fix memory leaks by expanding the API Greg KH
2020-08-18 10:07   ` Peter Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200810022510.6516-5-peter.chen@nxp.com \
    --to=peter.chen@nxp.com \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).