public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Viresh Kumar <viresh.kumar@linaro.org>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linaro-kernel@lists.linaro.org, Alex Elder <elder@linaro.org>,
	Johan Hovold <johan@hovoldconsulting.com>,
	"#4 . 4+" <stable@vger.kernel.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: hub: Fix unbalanced reference count and memory leak
Date: Thu, 28 Jul 2016 07:15:38 -0700	[thread overview]
Message-ID: <20160728141538.GT6869@ubuntu> (raw)
In-Reply-To: <Pine.LNX.4.44L0.1607281005310.1801-100000@iolanthe.rowland.org>

On 28-07-16, 10:13, Alan Stern wrote:
> On Wed, 27 Jul 2016, Viresh Kumar wrote:
> 
> > If the hub gets disconnected while the core is still activating it, this
> > can result in leaking memory of few USB structures.
> > 
> > This will happen if we have done a kref_get() from hub_activate() and
> > scheduled a delayed work item for HUB_INIT2/3. Now if hub_disconnect()
> > gets called before the delayed work expires, then we will cancel the
> > work from hub_quiesce(), but wouldn't do a kref_put(). And so the
> > unbalance.
> > 
> > kmemleak reports this as (with the commit e50293ef9775 backported to
> > 3.10 kernel with other changes, though the same is true for mainline as
> > well):
> 
> ...
> 
> > Fix this by putting the reference in hub_quiesce() if we canceled a
> > pending work.
> > 
> > CC: <stable@vger.kernel.org> #4.4+
> > Fixes: e50293ef9775 ("USB: fix invalid memory access in hub_activate()")
> > Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> > ---
> > Greg,
> > 
> > This is tested over 3.10 with backported patches only, sorry didn't had
> > a mainline setup to test this out. :(
> 
> Arg.  This is exactly the sort of thing I should have foreseen when 
> writing the earlier commit.
> 
> >  drivers/usb/core/hub.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> > index bee13517676f..3173693fa8e3 100644
> > --- a/drivers/usb/core/hub.c
> > +++ b/drivers/usb/core/hub.c
> > @@ -1315,7 +1315,8 @@ static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
> >  	struct usb_device *hdev = hub->hdev;
> >  	int i;
> >  
> > -	cancel_delayed_work_sync(&hub->init_work);
> > +	if (cancel_delayed_work_sync(&hub->init_work))
> > +		kref_put(&hub->kref, hub_release);
> >  
> >  	/* hub_wq and related activity won't re-trigger */
> >  	hub->quiescing = 1;
> 
> Another possibility is to remove the cancel_delayed_work_sync call 
> entirely.  Either way, you can add
> 
> Acked-by: Alan Stern <stern@rowland.harvard.edu>

Thanks Alan, I thought about that as well but didn't like it much as
that is unnecessary overhead (timer+work). I mean, we know that the
work is of no use anymore, why keep it around and let it fire? Also,
in the worst case it may end up waking up an idle CPU, which isn't
good as well.

-- 
viresh

      reply	other threads:[~2016-07-28 14:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-27 22:24 [PATCH] usb: hub: Fix unbalanced reference count and memory leak Viresh Kumar
2016-07-28 14:13 ` Alan Stern
2016-07-28 14:15   ` Viresh Kumar [this message]

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=20160728141538.GT6869@ubuntu \
    --to=viresh.kumar@linaro.org \
    --cc=elder@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@hovoldconsulting.com \
    --cc=linaro-kernel@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stable@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