Skip to contentSkip to conversation

usb: core: sysfs: show product, manufacturer and serial without the device lock

PATCH In review Joe Finzel 3 participants 4 messages Oct 14 – Dec 5 USB

lore
Share & tools
lore

Joe Finzel

author
#0 lore raw

PATCH v2 1/1 usb: core: sysfs: show product, manufacturer and serial without the device lock

Reading the product, manufacturer or serial attribute of a USB device takes the device lock. A hub holds its lock in hub_event() while it enumerates all of its ports, including the debounce and reset delays of every child, so a reader of the hub's strings waits until the last port is done -- seconds on a hub with several devices.

udev reads these strings for every USB device (the usb_id builtin, run from 50-udev-default.rules). Its worker blocks SIGTERM, so when systemd stops udevd in the initrd before switching root, it waits for that worker. On a desktop with a few hubs this delays every boot:

i7-13700K, ASUS ROG STRIX Z790-F, four hubs with HID devices behind
them: udevd took 1.7 s to stop, while the hubs were still enumerating.

QEMU, qemu-xhci with 8 usb-hubs of 8 HID devices each, second boot, three runs: udevd stops in 2.55-2.58 s (switch-root at 5.56-5.59 s); with this patch in 11-12 ms (switch-root at 3.02 s). Without usb_id in the udev rules, it stops in 9 ms as well.

-device qemu-xhci,id=xhci,p2=8,p3=8
-device usb-hub,bus=xhci.0,port=N (N = 1..8)
-device usb-kbd,bus=xhci.0,port=N.M (M = 1..8; also mouse, tablet)

The lock is not needed: usb_enumerate_device() sets the strings once, before device_add() makes the attributes visible, and usb_release_dev() frees them only after the attributes are gone. Nothing changes them in
between: authorizing or deauthorizing a device no longer touches them since Wireless USB, for which they could appear and disappear, was removed.

Andy Whitcroft reported the same boot stall in 2010 ("khubd -- switch USB product/manufacturer/serial handling to RCU"); back then the strings could still change under Wireless USB.

---
Found while looking into a 1.7 s stall at the end of the initrd on a desktop with several USB hubs; reproducible in QEMU (see the commit message). Tested on 7.2.9 with the QEMU reproduction; applies to usb-next.

Changes in v2:
- Drop the claim that dev_string_attrs_are_visible() reads the strings unlocked from the commit message and the comment; it only reads the
pointers (Alan Stern). No functional change.
- Add Alan's Reviewed-by.
- Rebase onto current usb-next.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/usb/core/sysfs.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/usb/core/sysfs.c +8 -10
index d22dc78457d..07865eb1f3b 100644
@@ -120,21 +120,19 @@ static ssize_t devspec_show(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR_RO(devspec);
 #endif
 
-/* String fields */
+/*
+ * String fields. usb_enumerate_device() reads them once, before the device
+ * is added, and they are freed only when it is released, so showing them
+ * needs no lock. The device lock would make readers wait while a hub holds
+ * it to enumerate all of its ports.
+ */
 #define usb_string_attr(name)						\
 static ssize_t  name##_show(struct device *dev,				\
 		struct device_attribute *attr, char *buf)		\
 {									\
-	struct usb_device *udev;					\
-	int retval;							\
+	struct usb_device *udev = to_usb_device(dev);			\
 									\
-	udev = to_usb_device(dev);					\
-	retval = usb_lock_device_interruptible(udev);			\
-	if (retval < 0)							\
-		return -EINTR;						\
-	retval = sysfs_emit(buf, "%s\n", udev->name);			\
-	usb_unlock_device(udev);					\
-	return retval;							\
+	return sysfs_emit(buf, "%s\n", udev->name);			\
 }									\
 static DEVICE_ATTR_RO(name)
 

---
base-commit: bbf6b99a57b2479bf51c3c8ad0946f4b8c33ed69
change-id: 20261011-usb-string-attrs-74abcc8b0195

Best regards,

Signature · 1 line
Joe Finzel <[email protected]>
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

From: Joe Finzel <[email protected]>

Reading the product, manufacturer or serial attribute of a USB device takes the device lock. A hub holds its lock in hub_event() while it enumerates all of its ports, including the debounce and reset delays of every child, so a reader of the hub's strings waits until the last port is done -- seconds on a hub with several devices.

udev reads these strings for every USB device (the usb_id builtin, run from 50-udev-default.rules). Its worker blocks SIGTERM, so when systemd stops udevd in the initrd before switching root, it waits for that worker. On a desktop with a few hubs this delays every boot:

Quoted 8 lines

i7-13700K, ASUS ROG STRIX Z790-F, four hubs with HID devices behind
them: udevd took 1.7 s to stop, while the hubs were still enumerating.
[ ... ]
USB product/manufacturer/serial handling to RCU"); back then the strings could still change under Wireless USB.

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
#2 lore raw
In reply to Joe Finzel
Quoted 7 lines · On Wed, Oct 14, 2026 at 11:01:51PM +0200, Joe Finzel via B4 Relay wrote:

From: Joe Finzel <[email protected]>

Reading the product, manufacturer or serial attribute of a USB device takes the device lock. A hub holds its lock in hub_event() while it enumerates all of its ports, including the debounce and reset delays of every child, so a reader of the hub's strings waits until the last port is done -- seconds on a hub with several devices.

"seconds"? USB isn't that slow.

Quoted 7 lines

udev reads these strings for every USB device (the usb_id builtin, run from 50-udev-default.rules). Its worker blocks SIGTERM, so when systemd stops udevd in the initrd before switching root, it waits for that worker. On a desktop with a few hubs this delays every boot:

i7-13700K, ASUS ROG STRIX Z790-F, four hubs with HID devices behind
them: udevd took 1.7 s to stop, while the hubs were still enumerating.

If this is annoying, don't read all of the strings from userspace.

Quoted 4 lines

QEMU, qemu-xhci with 8 usb-hubs of 8 HID devices each, second boot, three runs: udevd stops in 2.55-2.58 s (switch-root at 5.56-5.59 s); with this patch in 11-12 ms (switch-root at 3.02 s). Without usb_id in the udev rules, it stops in 9 ms as well.

qemu is "fake", so it can't provide real numbers, sorry. Use real hardware for testing this type of thing.

Quoted 11 lines

-device qemu-xhci,id=xhci,p2=8,p3=8
-device usb-hub,bus=xhci.0,port=N (N = 1..8)
-device usb-kbd,bus=xhci.0,port=N.M (M = 1..8; also mouse, tablet)

The lock is not needed: usb_enumerate_device() sets the strings once, before device_add() makes the attributes visible, and usb_release_dev() frees them only after the attributes are gone. Nothing changes them in
between: authorizing or deauthorizing a device no longer touches them since Wireless USB, for which they could appear and disappear, was removed.

Quoted 3 lines

Andy Whitcroft reported the same boot stall in 2010 ("khubd -- switch USB product/manufacturer/serial handling to RCU"); back then the strings could still change under Wireless USB.

They can still change. I used to have a device somewhere that did just that. Don't know why, guess USB firmware authors are "odd" (hint, that's what I used to do before I did Linux work...)

Quoted 24 lines

---
Found while looking into a 1.7 s stall at the end of the initrd on a desktop with several USB hubs; reproducible in QEMU (see the commit message). Tested on 7.2.9 with the QEMU reproduction; applies to usb-next.

Changes in v2:
- Drop the claim that dev_string_attrs_are_visible() reads the strings unlocked from the commit message and the comment; it only reads the
pointers (Alan Stern). No functional change.
- Add Alan's Reviewed-by.
- Rebase onto current usb-next.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/usb/core/sysfs.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/usb/core/sysfs.c +0 -0
index d22dc78457d..07865eb1f3b 100644

drivers/usb/core/sysfs.c static ssize_t devspec_show(struct device *dev, struct device_attribute *attr, · line 127

Quoted 9 lines
drivers/usb/core/sysfs.c +5 -1
@@ -120,21 +120,19 @@ static ssize_t devspec_show(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR_RO(devspec);
 #endif
 
-/* String fields */
+/*
+ * String fields. usb_enumerate_device() reads them once, before the device
+ * is added, and they are freed only when it is released, so showing them
+ * needs no lock. The device lock would make readers wait while a hub holds
+ * it to enumerate all of its ports.

Again, no, we should read them when asked for. And a lock is needed.

Quoted 7 lines
drivers/usb/core/sysfs.c +1 -2
+ */
 #define usb_string_attr(name)						\
 static ssize_t  name##_show(struct device *dev,				\
 		struct device_attribute *attr, char *buf)		\
 {									\
-	struct usb_device *udev;					\
-	int retval;							\

drivers/usb/core/sysfs.c static ssize_t devspec_show(struct device *dev, struct device_attribute *attr, · line 135

Quoted 10 lines
drivers/usb/core/sysfs.c +2 -7
+	struct usb_device *udev = to_usb_device(dev);			\
 									\
-	udev = to_usb_device(dev);					\
-	retval = usb_lock_device_interruptible(udev);			\
-	if (retval < 0)							\
-		return -EINTR;						\
-	retval = sysfs_emit(buf, "%s\n", udev->name);			\
-	usb_unlock_device(udev);					\
-	return retval;							\
+	return sysfs_emit(buf, "%s\n", udev->name);			\

Wait, this is not changing when they are read from the device, it's just not grabbing a lock at all. What happens if the device goes away when this is called?

How is usb_lock_device() so slow? Is it because you are racing and trying to read the same device all at once and grabbing the lock and hitting the sleep in usb_lock_device() somehow? Why not turn that value down if that's the real issue?

thanks,

greg k-h

author
#3 lore raw
Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · On Wed, Oct 14, 2026 at 11:01:51PM +0200, Joe Finzel via B4 Relay wrote:

is done -- seconds on a hub with several devices.

"seconds"?  USB isn't that slow.

Not per device, but it adds up because hub_event() keeps the hub locked for the whole port loop. This is the root hub of the Z790 board from the commit message, real hardware, 7.3.6:

  [2.419] usb 1-2: new high-speed USB device number 2 using xhci_hcd
  [4.295] hid-generic 0003:0B05:1A52.0001: ... [Generic USB Audio]
  [4.408] usb 1-3: new full-speed USB device number 3 using xhci_hcd
  [4.704] usb 1-4: new full-speed USB device number 4 using xhci_hcd
  [4.950] usb 1-10: new high-speed USB device number 5 using xhci_hcd
  [5.194] usb 1-11: new high-speed USB device number 6 using xhci_hcd
  [5.600] usb 1-13: new high-speed USB device number 8 using xhci_hcd
  [6.009] usb 1-14: new full-speed USB device number 10 using xhci_hcd

Most of it is the on-board audio device on port 2, which takes 1.9 s from getting its address to its HID interface being bound (I haven't looked into why yet); the other ports take 0.1-0.3 s each.

systemd stops udevd at 4.14 s and it exits at 6.15 s. In the last 40 boots of this machine the stop took 1.6-2.3 s, and every time it ended 141-152 ms after the "usb 1-14: new" line, so udevd waits for the root hub's port loop to finish.

Quoted 2 lines

qemu is "fake", so it can't provide real numbers, sorry.  Use real hardware for testing this type of thing.

Fair enough. The numbers above are from the real machine; I only used QEMU to show the effect of the patch reproducibly. I can run the same board with the patch applied and send before/after numbers from it.

Quoted 1 line

They can still change.

The device can change them, but the kernel reads them only once:
usb_enumerate_device() sets udev->product, ->manufacturer and ->serial, and nothing writes them again until usb_release_dev() frees them. If a device comes back from a reset with different descriptors, usb_reset_and_verify_device() re-enumerates it as a new device.

Quoted 1 line

What happens if the device goes away when this is called?

device_del() removes the attribute, and kernfs waits for any show() that is already running (kernfs_drain()). The strings are only freed in usb_release_dev(), after that.

Quoted 4 lines

How is usb_lock_device() so slow?  Is it because you are racing and trying to read the same device all at once and grabbing the lock and hitting the sleep in usb_lock_device() somehow?  Why not turn that value down if that's the real issue?

There is no sleep in it, usb_lock_device() is device_lock(). It only waits because hub_event() holds the same lock while it enumerates every port, as above.

If you still prefer to keep the lock, I can drop the patch.

thanks,
Joe