mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Chung-Geol Kim" <chunggeol.kim@samsung.com>
To: "Alan Stern" <stern@rowland.harvard.edu>
Cc: "gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
	"mathias.nyman@linux.intel.com" <mathias.nyman@linux.intel.com>,
	"stefan.koch10@gmail.com" <stefan.koch10@gmail.com>,
	"hkallweit1@gmail.com" <hkallweit1@gmail.com>,
	"sergei.shtylyov@cogentembedded.com"
	<sergei.shtylyov@cogentembedded.com>,
	"dan.j.williams@intel.com" <dan.j.williams@intel.com>,
	"chris.bainbridge@gmail.com" <chris.bainbridge@gmail.com>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: Re: Re: Re: Re: [PATCH] usb: core: fix a double free in the usb driver
Date: Wed, 08 Jun 2016 07:51:01 +0000	[thread overview]
Message-ID: <973472732.60398.1465372261231.JavaMail.weblogic@ep1ml103c> (raw)
In-Reply-To: <CGME20160607145414epcas1p29ace7d6cfbcc6342f3a245bbfb9137c5@epcas1p2.samsung.com>

[-- Attachment #1: Type: text/plain, Size: 6197 bytes --]

>On Tue, 7 Jun 2016, Chung-Geol Kim wrote:
>
>> =================================================
>>      At *remove USB(3.0) Storage
>>      sequence <1> --> <5> ((Problem Case))
>> =================================================
>>                                   VOLD
>> ------------------------------------|------------
>>                                  (uevent)
>>                             ________|_________
>>                            |<1>               |
>>                            |dwc3_otg_sm_work  |
>>                            |usb_put_hcd       |
>>                            |shared_hcd(kref=2)|
>>                            |__________________|
>>                             ________|_________ 
>>                            |<2>               |
>>                            |New USB BUS #2    |
>>                            |                  |
>>                            |shared_hcd(kref=1)|
>>                            |                  |
>>                          --(Link)-bandXX_mutex|
>>                          | |__________________|
>>                          |
>>     ___________________  |
>>    |<3>                | |
>>    |dwc3_otg_sm_work   | |
>>    |usb_put_hcd        | |
>>    |primary_hcd(kref=1)| |
>>    |___________________| |
>>     _________|_________  |
>>    |<4>                | |
>>    |New USB BUS #1     | |
>>    |hcd_release        | |
>>    |primary_hcd(kref=0)| |
>>    |                   | |
>>    |bandXX_mutex(free) |<-
>>    |___________________|
>>                                (( VOLD ))
>>                             ______|___________
>>                            |<5>               |
>>                            |      SCSI        |
>>                            |usb_put_hcd       |
>>                            |shared_hcd(kref=0)|
>>                            |*hcd_release      |
>>                            |bandXX_mutex(free*)|<- double free
>>                            |__________________|
>> 
>> =================================================
>
>Okay, now I understand the problem you want to solve.  What we need to
>do is make sure the mutex is deallocated when the _last_ hcd is
>released, which is not necessarily the same as when the _primary_ hcd
>is released.
>
>Can you please test the patch below?
>
>By the way, a good change (if you want to do it) would be to rename the
>"shared_hcd" field to "other_hcd" or "peer_hcd".  This is because it
>always points to the other hcd in the peer set: In the primary
>structure it points to the secondary, and in the secondary structure it
>points to the primary.
>
>Alan Stern
>

Thank you for clear understanding the problem that I faced.
When I tested with your below patch, it also works well.
The description has been modified as follows as you suggested.

=================================================
     At *remove USB(3.0) Storage
     sequence <1> --> <5> ((Problem Case))
=================================================
                                  VOLD
------------------------------------|------------
                                 (uevent)
                            ________|_________
                           |<1>               |
                           |dwc3_otg_sm_work  |
                           |usb_put_hcd       |
                           |peer_hcd(kref=2)|
                           |__________________|
                            ________|_________ 
                           |<2>               |
                           |New USB BUS #2    |
                           |                  |
                           |peer_hcd(kref=1)|
                           |                  |
                         --(Link)-bandXX_mutex|
                         | |__________________|
                         |
    ___________________  |
   |<3>                | |
   |dwc3_otg_sm_work   | |
   |usb_put_hcd        | |
   |primary_hcd(kref=1)| |
   |___________________| |
    _________|_________  |
   |<4>                | |
   |New USB BUS #1     | |
   |hcd_release        | |
   |primary_hcd(kref=0)| |
   |                   | |
   |bandXX_mutex(free) |<-
   |___________________|
                               (( VOLD ))
                            ______|___________
                           |<5>               |
                           |      SCSI        |
                           |usb_put_hcd       |
                           |peer_hcd(kref=0)|
                           |*hcd_release      |
                           |bandXX_mutex(free*)|<- double free
                           |__________________|

=================================================

>
>
>Index: usb-4.x/drivers/usb/core/hcd.c
>===================================================================
>--- usb-4.x.orig/drivers/usb/core/hcd.c
>+++ usb-4.x/drivers/usb/core/hcd.c
>@@ -2588,24 +2588,22 @@ EXPORT_SYMBOL_GPL(usb_create_hcd);
>  * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is
>  * deallocated.
>  *
>- * Make sure to only deallocate the bandwidth_mutex when the primary HCD is
>- * freed.  When hcd_release() is called for either hcd in a peer set
>- * invalidate the peer's ->shared_hcd and ->primary_hcd pointers to
>- * block new peering attempts
>+ * Make sure to deallocate the bandwidth_mutex only when the last HCD is
>+ * freed.  When hcd_release() is called for either hcd in a peer set,
>+ * invalidate the peer's ->shared_hcd and ->primary_hcd pointers.
>  */
> static void hcd_release(struct kref *kref)
> {
> 	struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
> 
> 	mutex_lock(&usb_port_peer_mutex);
>-	if (usb_hcd_is_primary_hcd(hcd))
>-		kfree(hcd->bandwidth_mutex);
> 	if (hcd->shared_hcd) {
> 		struct usb_hcd *peer = hcd->shared_hcd;
> 
> 		peer->shared_hcd = NULL;
>-		if (peer->primary_hcd == hcd)
>-			peer->primary_hcd = NULL;
>+		peer->primary_hcd = NULL;
>+	} else {
>+		kfree(hcd->bandwidth_mutex);
> 	}
> 	mutex_unlock(&usb_port_peer_mutex);
> 	kfree(hcd);
>

           reply	other threads:[~2016-06-08  7:51 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <CGME20160607145414epcas1p29ace7d6cfbcc6342f3a245bbfb9137c5@epcas1p2.samsung.com>]

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=973472732.60398.1465372261231.JavaMail.weblogic@ep1ml103c \
    --to=chunggeol.kim@samsung.com \
    --cc=chris.bainbridge@gmail.com \
    --cc=dan.j.williams@intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hkallweit1@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@linux.intel.com \
    --cc=sergei.shtylyov@cogentembedded.com \
    --cc=stefan.koch10@gmail.com \
    --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

all inboxes | Powered by JetHome®