Missing uevents after successful USBDEVFS_RESET
Hi,
I am investigating an issue where userspace processes using usbfs (such as ADB) cannot reliably track device state across a USBDEVFS_RESET ioctl by monitoring netlink uevents.
Repro steps: (Lenovo P620, kernel 7.1.6)
1. Plug an ADB device into your computer.
2. Open a console to monitor netlink events: udevadm monitor -p -u.
3. Send USBDEVFS_RESET a couple of times (i used adb_usbreset).
4. 50% of the time the event sequence is UNBIND/REMOVE/ADD/BIND. 50% of the time, the event sequence is just UNBIND.
When USBDEVFS_RESET is issued via drivers/usb/core/devio.c, it calls usb_reset_device() -> usb_reset_and_verify_device() in drivers/usb/core/hub.c. Depending on the reset outcome, userspace sees inconsistent uevents:
Case 1: Full re-enumeration (descriptors changed or reset/verify failed) The device is torn down and reprobed. Userspace receives:
UNBIND -> REMOVE -> ADD -> BIND
Userspace processes monitoring uevents immediately detect that the device is available again and re-open it.
Case 2: Reset succeeds without re-enumeration (descriptors unchanged) The kernel unbinds usbfs/interface drivers before reset, generating a KOBJ_UNBIND uevent. Because the usb_device structure is preserved and usbfs does not re-probe, no subsequent KOBJ_BIND, KOBJ_ADD, or KOBJ_CHANGE event is ever emitted.
As a result of Case 2, userspace receives an UNBIND ("disconnect") signal and assumes the device went away, but never receives an event indicating that the reset finished and the device is ready for reuse. This forces daemons like adb to fall back to polling /dev/bus/usb instead of relying on netlink/udev events.
Is the omission of a completion uevent after a successful reset without re-enumeration intentional?
If not, would a patch emitting KOBJ_CHANGE (or re-binding/notifying userspace) upon completion of usb_reset_device() be acceptable here?
Thanks,
Fab
Quoted 29 lines · Fabien Sanglard
Hi,
I am investigating an issue where userspace processes using usbfs (such as ADB) cannot reliably track device state across a
USBDEVFS_RESETioctl by monitoring netlink uevents.Repro steps: (Lenovo P620, kernel 7.1.6)
1. Plug an ADB device into your computer.
2. Open a console to monitor netlink events: udevadm monitor -p -u.
3. SendUSBDEVFS_RESETa couple of times (i usedadb_usbreset).
4. 50% of the time the event sequence is UNBIND/REMOVE/ADD/BIND. 50% of the time, the event sequence is just UNBIND.When
USBDEVFS_RESETis issued viadrivers/usb/core/devio.c, it callsusb_reset_device()->usb_reset_and_verify_device()indrivers/usb/core/hub.c. Depending on the reset outcome, userspace sees inconsistent uevents:Case 1: Full re-enumeration (descriptors changed or reset/verify failed) The device is torn down and reprobed. Userspace receives:
UNBIND -> REMOVE -> ADD -> BIND
Userspace processes monitoring uevents immediately detect that the device is available again and re-open it.Case 2: Reset succeeds without re-enumeration (descriptors unchanged) The kernel unbinds usbfs/interface drivers before reset, generating a
KOBJ_UNBINDuevent. Because theusb_devicestructure is preserved and usbfs does not re-probe, no subsequentKOBJ_BIND,KOBJ_ADD, orKOBJ_CHANGEevent is ever emitted.
Quoted 8 lines
As a result of Case 2, userspace receives an UNBIND ("disconnect") signal and assumes the device went away, but never receives an event indicating that the reset finished and the device is ready for reuse. This forces daemons like adb to fall back to polling
/dev/bus/usbinstead of relying on netlink/udev events.Is the omission of a completion uevent after a successful reset without re-enumeration intentional?
It's more or less unintentional.
A normal driver would rebind automatically. But usbfs isn't normal, in the sense that it won't bind to a device without a special user request (primarily the USBDEVFS_CLAIMINTERFACE ioctl but also some others).
Quoted 2 lines
If not, would a patch emitting
KOBJ_CHANGE(or re-binding/notifying userspace) upon completion ofusb_reset_device()be acceptable here?
Yes. But there are some things to bear in mind:
Adding pre_reset and post_reset handlers to the usbfs driver would prevent unbinding in case 2. The problem is that there is no agreed-upon way to then let userspace clients know that the device has been reset, or to let them know beforehand that the device is going to be reset. In other words, the two handlers wouldn't be able to do anything useful.
Emitting a KOBJ_CHANGE uevent instead would be more straightforward. But you would have to think carefully about the conditions under which it should be sent.
Alan Stern
Hi Alan,
Quoted 2 lines
You would have to think carefully about the conditions under which it should be sent.
We only want to emit KOBJ_CHANGE on an interface when:
1. A driver was bound before reset and forcibly unbound.
2. The reset succeeded in-place.
3. No driver matched on re-probe, which happens for usbfs.
It looks like these three conditions are all met in usb_rebind_intf().
I tested it with an Android device/ adb_reset/udevadm monitor --kernel --subsystem-match=usb. When the in-place reset succeeds, I now see UNBIND followed by CHANGE.
WDYT?
---
drivers/usb/core/driver.c +16 -1
@@ -1182,8 +1182,23 @@ static void usb_rebind_intf(struct usb_interface *intf)if (!intf->dev.power.is_prepared) { intf->needs_binding =0 ; rc =device_attach (&intf->dev); -if (rc <0 && rc != -EPROBE_DEFER ) +if (rc <0 && rc != -EPROBE_DEFER ) {dev_warn (&intf->dev,"rebind failed: %d\n" , rc); + }else if (rc ==0 ) { +/* +* Emit KOBJ_CHANGE to notify user space that the +* interface without driver is ready to be re-claimed. +* 1. A driver was bound and forcibly unbound (this +* function usb_rebind_intf() is called in this +* case only). +* 2. The reset succeeded in-place (this function is +* called from +* usb_unbind_and_rebind_marked_interfaces()). +* 3. No kernel driver matched on re-probe +* (device_attach() returned 0). +*/ +kobject_uevent (&intf->dev.kobj,KOBJ_CHANGE ); + } } }
Quoted 63 lines · Alan Stern
Quoted 29 lines · Fabien Sanglard
Hi,
I am investigating an issue where userspace processes using usbfs (such as ADB) cannot reliably track device state across a
USBDEVFS_RESETioctl by monitoring netlink uevents.Repro steps: (Lenovo P620, kernel 7.1.6)
1. Plug an ADB device into your computer.
2. Open a console to monitor netlink events: udevadm monitor -p -u.
3. SendUSBDEVFS_RESETa couple of times (i usedadb_usbreset).
4. 50% of the time the event sequence is UNBIND/REMOVE/ADD/BIND. 50% of the time, the event sequence is just UNBIND.When
USBDEVFS_RESETis issued viadrivers/usb/core/devio.c, it callsusb_reset_device()->usb_reset_and_verify_device()indrivers/usb/core/hub.c. Depending on the reset outcome, userspace sees inconsistent uevents:Case 1: Full re-enumeration (descriptors changed or reset/verify failed) The device is torn down and reprobed. Userspace receives:
UNBIND -> REMOVE -> ADD -> BIND
Userspace processes monitoring uevents immediately detect that the device is available again and re-open it.Case 2: Reset succeeds without re-enumeration (descriptors unchanged) The kernel unbinds usbfs/interface drivers before reset, generating a
KOBJ_UNBINDuevent. Because theusb_devicestructure is preserved and usbfs does not re-probe, no subsequentKOBJ_BIND,KOBJ_ADD, orKOBJ_CHANGEevent is ever emitted.Quoted 8 lines
As a result of Case 2, userspace receives an UNBIND ("disconnect") signal and assumes the device went away, but never receives an event indicating that the reset finished and the device is ready for reuse. This forces daemons like adb to fall back to polling
/dev/bus/usbinstead of relying on netlink/udev events.Is the omission of a completion uevent after a successful reset without re-enumeration intentional?
It's more or less unintentional.
A normal driver would rebind automatically. But usbfs isn't normal, in the sense that it won't bind to a device without a special user request (primarily the
USBDEVFS_CLAIMINTERFACEioctl but also some others).Quoted 2 lines
If not, would a patch emitting
KOBJ_CHANGE(or re-binding/notifying userspace) upon completion ofusb_reset_device()be acceptable here?Yes. But there are some things to bear in mind:
Adding
pre_resetandpost_resethandlers to the usbfs driver would prevent unbinding in case 2. The problem is that there is no agreed-upon way to then let userspace clients know that the device has been reset, or to let them know beforehand that the device is going to be reset. In other words, the two handlers wouldn't be able to do anything useful.Emitting a
KOBJ_CHANGEuevent instead would be more straightforward. But you would have to think carefully about the conditions under which it should be sent.Alan Stern
Quoted 10 lines · Fabien Sanglard
Hi Alan,
Quoted 2 lines
You would have to think carefully about the conditions under which it should be sent.
We only want to emit
KOBJ_CHANGEon an interface when:
1. A driver was bound before reset and forcibly unbound.
2. The reset succeeded in-place.
3. No driver matched on re-probe, which happens forusbfs.
Quoted 5 lines
It looks like these three conditions are all met in
usb_rebind_intf().I tested it with an Android device/
adb_reset/udevadm monitor --kernel --subsystem-match=usb. When the in-place reset succeeds, I now seeUNBINDfollowed byCHANGE.
All right.
Quoted 1 line
WDYT?
The patch was corrupted by your email client. Please fix this.
Quoted 15 lines
---
drivers/usb/core/driver.c +4
-1@@ -1182,8 +1182,23 @@ static void usb_rebind_intf(struct usb_interface *intf)if (!intf->dev.power.is_prepared) { intf->needs_binding =0 ; rc =device_attach (&intf->dev); -if (rc <0 && rc != -EPROBE_DEFER ) +if (rc <0 && rc != -EPROBE_DEFER ) {dev_warn (&intf->dev,"rebind failed: %d\n" , rc); + }else if (rc ==0 ) { +/* +* Emit KOBJ_CHANGE to notify user space that the
drivers/usb/core/driver.c static void usb_rebind_intf(struct usb_interface *intf) · line 1199
Quoted 10 lines
drivers/usb/core/driver.c +10
-0+* interface without driver is ready to be re-claimed. +* 1. A driver was bound and forcibly unbound (this +* function usb_rebind_intf() is called in this +* case only). +* 2. The reset succeeded in-place (this function is +* called from +* usb_unbind_and_rebind_marked_interfaces()). +* 3. No kernel driver matched on re-probe +* (device_attach() returned 0). +*/
Much too verbose. (Are people learning how to write patches by imitating what LLMs do?) Something like that would be appropriate for the patch description, but not for a code comment. It should be more like this:
/* * Emit KOBJ_CHANGED uevent to notify userspace that the * interface could not be rebound. */
drivers/usb/core/driver.c static void usb_rebind_intf(struct usb_interface *intf) · line 1203
Quoted 4 lines
drivers/usb/core/driver.c +2
-0+kobject_uevent (&intf->dev.kobj,KOBJ_CHANGE ); + } } }
Otherwise this seems okay.
Alan Stern
PATCH 1/1 usb: core: Emit KOBJ_CHANGE on unbound interface
When an interface driver is forcibly unbound before a device reset, usb_rebind_intf() attempts to re-attach a driver once the reset completes in-place. Normal kernel drivers rebind and emit UNBIND followed by BIND. However, usbfs only binds on explicit userspace requests (e.g., USBDEVFS_CLAIMINTERFACE), so device_attach() returns 0 and no uevent is emitted.
As a result, userspace clients like adb only see UNBIND and must poll /dev/bus/usb to detect when the interface is ready again. Emit a KOBJ_CHANGE uevent when device_attach() finds no matching driver so userspace is notified that the interface can be reclaimed.
--- drivers/usb/core/driver.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/usb/core/driver.c +8 -1
@@ -1182,8 +1182,15 @@ static void usb_rebind_intf(struct usb_interface *intf)if (!intf->dev.power.is_prepared) { intf->needs_binding =0 ; rc =device_attach (&intf->dev); -if (rc <0 && rc != -EPROBE_DEFER ) +if (rc <0 && rc != -EPROBE_DEFER ) {dev_warn (&intf->dev,"rebind failed: %d\n" , rc); + }else if (rc ==0 ) { +/* +* Emit a KOBJ_CHANGE uevent to notify userspace +* that the interface could not be rebound. +*/ +kobject_uevent (&intf->dev.kobj,KOBJ_CHANGE ); + } } }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH] usb: core: Emit KOBJ_CHANGE on unbound interface
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
When an interface driver is forcibly unbound before a device reset,
usb_rebind_intf()attempts to re-attach a driver once the reset completes in-place. Normal kernel drivers rebind and emit UNBIND followed by BIND. However, usbfs only binds on explicit userspace requests (e.g.,USBDEVFS_CLAIMINTERFACE), sodevice_attach()returns 0 and no uevent is emitted.
Quoted 7 lines
As a result, userspace clients like adb only see UNBIND and must poll
/dev/bus/usbto detect when the interface is ready again. Emit aKOBJ_CHANGEuevent whendevice_attach()finds no matching driver so userspace is notified that the interface can be reclaimed.
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH] usb: core: Emit KOBJ_CHANGE on unbound interface
In reply to Fabien SanglardQuoted 4 lines · Fabien Sanglard
As a result, userspace clients like adb only see UNBIND and must poll
/dev/bus/usbto detect when the interface is ready again. Emit aKOBJ_CHANGEuevent whendevice_attach()finds no matching driver so userspace is notified that the interface can be reclaimed.
Hi,
why do you want to limit this to that specific case? Why not not always tell user space tat a device has been reset?
Regards
Oliver
Re: [PATCH] usb: core: Emit KOBJ_CHANGE on unbound interface
My original patch focuses on fixing the silent failure for usbfs drivers (like ADB). Since other drivers survive a reset without issues from a user space perspective, I wanted to avoid being disruptive to unaffected systems.
That said, unifying the behavior upon reset would be elegant. My main concern is whether some existing udev rules or netlink listeners in user space might break if we generalize this behavior and start emitting unexpected CHANGE events for interfaces that successfully rebound (which already emit UNBIND -> BIND).
But it might be worth "trying". I am unsure what the kernel team philosophy is in this regard. If you all agree, we could trigger the KOBJ_CHANGE event unconditionally inside rebind_marked_interfaces() instead of conditionally inside usb_rebind_intf().
Fab
Quoted 17 lines · Oliver Neukum
Quoted 4 lines · Fabien Sanglard
As a result, userspace clients like adb only see UNBIND and must poll
/dev/bus/usbto detect when the interface is ready again. Emit aKOBJ_CHANGEuevent whendevice_attach()finds no matching driver so userspace is notified that the interface can be reclaimed.Hi,
why do you want to limit this to that specific case? Why not not always tell user space tat a device has been reset?
Regards
Oliver
Greg Kroah-Hartman
maintainerRe: [PATCH] usb: core: Emit KOBJ_CHANGE on unbound interface
A: http://en.wikipedia.org/wiki/Top_post
Q: Were do I find info about this thing called top-posting?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
A: No.
Q: Should I include quotations after my reply?
http://daringfireball.net/2007/07/on_top
Quoted 11 lines · Fabien Sanglard
My original patch focuses on fixing the silent failure for usbfs drivers (like ADB). Since other drivers survive a reset without issues from a user space perspective, I wanted to avoid being disruptive to unaffected systems.
That said, unifying the behavior upon reset would be elegant. My main concern is whether some existing udev rules or netlink listeners in user space might break if we generalize this behavior and start emitting unexpected CHANGE events for interfaces that successfully rebound (which already emit UNBIND -> BIND).
Quoted 4 lines
But it might be worth "trying". I am unsure what the kernel team philosophy is in this regard. If you all agree, we could trigger the
KOBJ_CHANGEevent unconditionally insiderebind_marked_interfaces()instead of conditionally insideusb_rebind_intf().
Please try and test to verify.
thanks,
greg k-h
Re: [PATCH] usb: core: Emit KOBJ_CHANGE on unbound interface
In reply to Fabien SanglardGood morning,
Quoted 5 lines · Fabien Sanglard
That said, unifying the behavior upon reset would be elegant. My main concern is whether some existing udev rules or netlink listeners in user space might break if we generalize this behavior and start emitting unexpected CHANGE events for interfaces that successfully rebound (which already emit UNBIND -> BIND).
On a generic note, it seems to me that you are concerned about an unavoidable case. You can check whether an interface is unclaimed. Yet you have no way to ensure that the result of your check is still current and thus valid, at time you act upon it.
In other words, you were about to design an interface that is inherently racy. That is not a good idea.
If you really, really need to avoid it, you'd have to define a new type of event.
Regards
Oliver
Re: [PATCH] usb: core: Emit KOBJ_CHANGE on unbound interface
Quoted 7 lines · Greg Kroah-Hartman
Quoted 4 lines · Fabien Sanglard
But it might be worth "trying". I am unsure what the kernel team philosophy is in this regard. If you all agree, we could trigger the
KOBJ_CHANGEevent unconditionally insiderebind_marked_interfaces()instead of conditionally insideusb_rebind_intf().Please try and test to verify.
Quoted 8 lines · Oliver Neukum
On a generic note, it seems to me that you are concerned about an unavoidable case. You can check whether an interface is unclaimed. Yet you have no way to ensure that the result of your check is still current and thus valid, at time you act upon it.
In other words, you were about to design an interface that is inherently racy. That is not a good idea.
Emitting KOBJ_CHANGE unconditionally on the usb_device (&udev->dev.kobj) in usb_reset_device() when ret == 0, right after usb_unbind_and_rebind_marked_interfaces(udev), avoids the racy unclaimed-interface check and is in line with the earlier feedback ("Why not always tell user space that a device has been reset?"). Instead of asserting that an interface is unclaimed, the event simply tells userspace that the device has changed and, if tracked, should be re-inspected.
I tested this across devices with snd-usb-audio (no pre/post_reset), usbhid (with pre/post_reset), and usbfs (adb) using udevadm monitor:
Interface driver Uevent sequence on reset (ret == 0)
--------------------------------------------------------------------------
Kernel (no pre/post_reset) UNBIND(intf) -> BIND(intf) -> CHANGE(dev) Kernel (with pre/post_reset) CHANGE(dev) Userspace (usbfs, e.g. adb) UNBIND(intf) -> CHANGE(dev)
In all cases, CHANGE(dev) is emitted after in-kernel interface rebinding has finished, notifying userspace that the reset is complete. Because it is a usb_device uevent, it also includes DEVNAME (which usb_interface uevents lack), making it trivial for usbfs clients to re-inspect and re-claim the device without polling.
Thanks,
Fabien
-- >8 --
Subject: [PATCH v2] usb: core: Emit KOBJ_CHANGE when usb_reset_device() succeeds
When usb_reset_device() succeeds without re-enumerating the device, userspace receives no device-level uevent indicating that the reset has completed. For usbfs clients such as adb, usb_forced_unbind_intf() unbinds the interface before the port reset, and device_attach() leaves it unbound afterward because no kernel driver matches. Since usbfs suppresses uevents on interface claim/release, userspace receives UNBIND(intf) before the reset and no event when the device is ready to be re-claimed, forcing it to fall back to polling /dev/bus/usb.
Emit KOBJ_CHANGE on &udev->dev.kobj in usb_reset_device() when the reset succeeds, after usb_unbind_and_rebind_marked_interfaces(udev), so userspace is notified (with DEVNAME) once the reset and all kernel interface rebinding are complete.
---
Changes in v2:
- Emit KOBJ_CHANGE on &udev->dev.kobj in usb_reset_device() after usb_unbind_and_rebind_marked_interfaces() when ret == 0, instead of on the interface in usb_rebind_intf().
- v1: https://lore.kernel.org/linux-usb/[email protected]/
drivers/usb/core/hub.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/core/hub.c +3 -1
@@ -6447,8 +6447,10 @@ int usb_reset_device(struct usb_device *udev) }/* If the reset failed, hub_wq will unbind drivers later */ -if (ret ==0 ) +if (ret ==0 ) {usb_unbind_and_rebind_marked_interfaces (udev); +kobject_uevent (&udev->dev.kobj,KOBJ_CHANGE ); + } }usb_autosuspend_device (udev);
Signature · 1 line
2.55.0