From: Alan Stern <stern@rowland.harvard.edu>
To: Peter Chen <peter.chen@nxp.com>
Cc: balbi@kernel.org, linux-usb@vger.kernel.org, linux-imx@nxp.com,
gregkh@linuxfoundation.org,
Benjamin Herrenschmidt <benh@kernel.crashing.org>
Subject: Re: [PATCH v3 2/6] USB: UDC: net2280: Fix memory leaks
Date: Thu, 13 Aug 2020 09:57:13 -0400 [thread overview]
Message-ID: <20200813135713.GA405784@rowland.harvard.edu> (raw)
In-Reply-To: <20200813031953.13676-3-peter.chen@nxp.com>
On Thu, Aug 13, 2020 at 11:19:49AM +0800, Peter Chen wrote:
> From: Alan Stern <stern@rowland.harvard.edu>
>
> As Anton and Evgeny have noted, the net2280 UDC driver has a problem
> with leaking memory along some of its failure pathways. It also has
> another problem, not previously noted, in that some of the failure
> pathways will call usb_del_gadget_udc() without first calling
> usb_add_gadget_udc_release(). And it leaks memory by calling kfree()
> when it should call put_device().
>
> Previous attempts to fix the problems have failed because of lack of
> support in the UDC core for separately initializing and adding
> gadgets, or for separately deleting and freeing gadgets. The previous
> patch in this series adds the necessary support, making it possible to
> fix the outstanding problems properly.
>
> This patch adds an "added" flag to the net2280 structure to indicate
> whether or not the gadget has been registered (and thus whether or not
> to call usb_del_gadget()), and it fixes the deallocation issues by
> calling usb_put_gadget() at the appropriate point.
>
> A similar memory leak issue, apparently never before recognized, stems
> from the fact that the driver never initializes the drvdata field in
> the gadget's embedded struct device! Evidently this wasn't noticed
> because the pointer is only ever used as an argument to kfree(), which
> doesn't mind getting called with a NULL pointer. In fact, the drvdata
> for gadget device will be written by usb_composite_dev structure if
> any gadget class is loaded, so it needs to use usb_gadget structure
> to get net2280 private data.
>
> CC: Benjamin Herrenschmidt <benh@kernel.crashing.org>
> Reported-by: Anton Vasilyev <vasilyev@ispras.ru>
> Reported-by: Evgeny Novikov <novikov@ispras.ru>
> Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
> ---
> drivers/usb/gadget/udc/net2280.c | 13 +++++++++----
> drivers/usb/gadget/udc/net2280.h | 1 +
> 2 files changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c
> index 7530bd9a08c4..31e49cc34316 100644
> --- a/drivers/usb/gadget/udc/net2280.c
> +++ b/drivers/usb/gadget/udc/net2280.c
> @@ -3561,7 +3561,9 @@ static irqreturn_t net2280_irq(int irq, void *_dev)
>
> static void gadget_release(struct device *_dev)
> {
> - struct net2280 *dev = dev_get_drvdata(_dev);
> + struct usb_gadget *gadget = container_of(_dev,
> + struct usb_gadget, dev);
> + struct net2280 *dev = container_of(gadget, struct net2280, gadget);
Please change this to
struct net2280 *dev = container_of(_dev, struct net2280, gadget,dev);
And do the same for the net2272 patch.
Alan Stern
next prev parent reply other threads:[~2020-08-13 13:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-13 3:19 [PATCH v3 0/6] USB: UDC: Fix memory leaks by expanding the API Peter Chen
2020-08-13 3:19 ` [PATCH v3 1/6] USB: UDC: Expand device model API interface Peter Chen
2020-08-13 3:19 ` [PATCH v3 2/6] USB: UDC: net2280: Fix memory leaks Peter Chen
2020-08-13 13:57 ` Alan Stern [this message]
2020-08-14 3:44 ` Peter Chen
2020-08-13 3:19 ` [PATCH v3 3/6] USB: UDC: net2272: " Peter Chen
2020-08-13 3:19 ` [PATCH v3 4/6] usb: cdns3: gadget: fix possible memory leak Peter Chen
2020-08-13 3:19 ` [PATCH v3 5/6] usb: dwc3: allocate gadget structure dynamically Peter Chen
2020-08-13 3:19 ` [PATCH v3 6/6] Revert "usb: udc: allow adding and removing the same gadget device" 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=20200813135713.GA405784@rowland.harvard.edu \
--to=stern@rowland.harvard.edu \
--cc=balbi@kernel.org \
--cc=benh@kernel.crashing.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-imx@nxp.com \
--cc=linux-usb@vger.kernel.org \
--cc=peter.chen@nxp.com \
/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