Skip to contentSkip to conversation

USB hotplug race

PATCH In review Petr Baudis 3 participants 4 messages Mar 17, 2004 – Mar 27, 2004

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB hotplug race

Hello,

so I finally upgraded from 2.4 to 2.6 (2.6.4), and one of the few
problems I've hit is hotplug. Basically, when I start up hotplug while my Zaurus is plugged, everything works fine and usbnet gets loaded, however when I plug Zaurus after hotplug was loaded, it trashes the event.

After populating the hotplug scripts with plethora of debug prints,
I've tracked it down to usb_convert_vars() - basically, immediatelly after the USB device is plugged and the signal is already delivered to hotplug, the sysfs tree isn't yet populated by all the relevant data and only detach_state is present there - no bDeviceClass or so.

Looking at drivers/usb/core/message.c:usb_set_configuration(),
device_add() is called first (which I suppose calls /sbin/hotplug through platform_notify ?), and _then_ this set of files is created. It doesn't look like there would be a lot of time to trigger the race, but I personally get a _very_ high reproducibility rate (actually I'm not sure if I ever managed not to trigger it).

I don't know how to fix it in the kernel since we would probably need to
first add kobject, then pop out and create the sysfs nodes, and _then_ call the userspace helper. So I just created a trivial bandaid for /etc/hotplug/usb.agent, which fixes this for me:

etc/hotplug/usb.agent +7 -2
@@ -354,9 +354,14 @@
     # they happen to issue requests at the same time ... it happens easily
     # with slow HID devices and "usbmodules".
     # starting with 2.5 (DEVPATH set), all hcds must queue control traffic.
+
+    # However by the time of 2.6.4, the kernel has an easy-to-trigger (at
+    # least for me) race condition when /sbin/hotplug already got called but
+    # the sysfs device directory is not yet populated with all the neccessary
+    # files. --pasky
-    if [ "$DEVPATH" = "" ]; then
+    # if [ "$DEVPATH" = "" ]; then
 	sleep 3
-    fi
+    # fi
 
     usb_convert_vars
 

Please consider applying.

PS: By the way, usb.agent.(orig|rej) slipped into the 2004_03_11
distribution.

Kind regards,

Signature · 16 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man. -- George Bernard Shaw


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#1 lore raw
Quoted 8 lines · Petr Baudis

Hello,

so I finally upgraded from 2.4 to 2.6 (2.6.4), and one of the few
problems I've hit is hotplug. Basically, when I start up hotplug while my Zaurus is plugged, everything works fine and usbnet gets loaded, however when I plug Zaurus after hotplug was loaded, it trashes the event.

Quoted 5 lines

After populating the hotplug scripts with plethora of debug prints,
I've tracked it down to usb_convert_vars() - basically, immediatelly after the USB device is plugged and the signal is already delivered to hotplug, the sysfs tree isn't yet populated by all the relevant data and only detach_state is present there - no bDeviceClass or so.

Yes, it's quite easy to race the kernel with this. But how about putting the proper patch in by sitting and spinning until we see the file we are looking for (or erroring out after a time period.)

That's much cleaner than always sleeping. On a SMP box, you usually never need to sleep :)

thanks,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Petr Baudis
Quoted 7 lines
Quoted 6 lines

After populating the hotplug scripts with plethora of debug prints,
I've tracked it down to usb_convert_vars() - basically, immediatelly after the USB device is plugged and the signal is already delivered to hotplug, the sysfs tree isn't yet populated by all the relevant data and only detach_state is present there - no bDeviceClass or so.

Quoted 6 lines

Yes, it's quite easy to race the kernel with this. But how about putting the proper patch in by sitting and spinning until we see the file we are looking for (or erroring out after a time period.)

That's much cleaner than always sleeping. On a SMP box, you usually never need to sleep :)

It seems to me the proper patch would be for the kernel to wait for the creation of any files it refers to when generating a hotplug invocation.

Regards
Oliver

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw
In reply to Petr Baudis
Quoted 11 lines · Oliver Neukum
Quoted 11 lines
Quoted 6 lines

After populating the hotplug scripts with plethora of debug prints,
I've tracked it down to usb_convert_vars() - basically, immediatelly after the USB device is plugged and the signal is already delivered to hotplug, the sysfs tree isn't yet populated by all the relevant data and only detach_state is present there - no bDeviceClass or so.

Yes, it's quite easy to race the kernel with this. But how about putting the proper patch in by sitting and spinning until we see the file we are looking for (or erroring out after a time period.)

Quoted 5 lines
Quoted 2 lines

That's much cleaner than always sleeping. On a SMP box, you usually never need to sleep :)

It seems to me the proper patch would be for the kernel to wait for the creation of any files it refers to when generating a hotplug invocation.

Heh, yeah, that would be "nice" to have, but unfortunatly due to the way the stupid stupid stupid programmer [1] who wrote that code, it's quite difficult (if not impossible) to do...

It's a 2.7 thing...

thanks,

greg k-h

[1] yeah, it was me, go ahead, blame me, who knew hotplug would be so

    fast...

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel