Re: Subject: [PATCH v4] USB:Core: BugFix: Proper handling of Race Condition when two USB class drivers try to call init_usb_class simultaneously

From: gregkh@xxxxxxxxxxxxxxxxxxx
Date: Wed Mar 08 2017 - 13:06:22 EST


On Tue, Mar 07, 2017 at 04:35:37AM +0000, Ajay Kaher wrote:
> Â
> Â
> Â
> > OnÂFri,Â3ÂMarÂ2017,ÂAjayÂKaherÂwrote:
>
> > >Â>Âusb_class->krefÂisÂnotÂaccessibleÂoutsideÂtheÂfile.c
> > >Â>ÂasÂusb_classÂisÂ_static_ÂinsideÂtheÂfile.cÂand
> > >Â>ÂpointerÂofÂusb_class->krefÂisÂnotÂpassedÂanywhere.
> > >Â>Â
> > >Â>ÂHenceÂasÂyouÂwanted,ÂthereÂareÂnoÂreferencesÂofÂusb_class->kref
> > >Â>ÂotherÂthanÂtakenÂbyÂinit_usb_class()ÂandÂreleasedÂbyÂdestroy_usb_class().
> > >Â
> > >ÂVerifiedÂtheÂcodeÂagain,ÂIÂhopeÂmyÂlastÂcommentsÂclarifedÂtheÂthings
> > >ÂwhichÂcameÂinÂyourÂmindÂandÂhelpsÂyouÂtoÂacceptÂtheÂpatchÂ:)
> > Â
> > YourÂmainÂpointÂisÂthatÂusb_class->krefÂisÂaccessedÂfromÂonlyÂtwo
> > points,ÂbothÂofÂwhichÂareÂprotectedÂbyÂtheÂnewÂmutex.ÂÂThisÂmeansÂthere
> > isÂnoÂreasonÂforÂtheÂvalueÂtoÂbeÂaÂstructÂkrefÂatÂall.ÂÂYouÂshould
> > changeÂitÂtoÂanÂintÂ(andÂchangeÂitsÂname).ÂÂLeavingÂitÂasÂaÂkrefÂwill
> > makeÂreadersÂwonderÂwhyÂitÂneedsÂtoÂbeÂupdatedÂatomically.
>
> At many places in Linux kernel, instances of Kref have been used within
> Mutex, SpinLock and donât have any side effect.
>
> Making to int and handle (i.e. get/put) it within file.c seems
> not good as we have Kref. Instead, we can have non_atomic version of kref.
> We can discuss about non_atomic kref in another thread, if you are interested.
>
> > Also,ÂwhyÂdoesÂdestroy_usb_class()ÂhaveÂthatÂ"ifÂ(usb_class)Â"test?Â
> > Isn'tÂitÂtrueÂthatÂusb_classÂcanÂneverÂbeÂNULLÂthere?
>
> Removed in Patch v4.
>
> thanks,
> ajayÂkaher
> Â
> ÂÂ
> Signed-off-by:ÂAjayÂKaher
> Â

Can you resend this in a format that I can apply it in? I suggest
reading Documentation/SubmittingPatches. If you have any questions
about the correct format, please let me know.

Also add Alan's ack to it.

thanks,

greg k-h