From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796354638; b=e2l2rCspjb+v4IvL1ZTxA7i06/CI8z26rxDg6YQUAFJMH5zYU4IYmfI61CA1DOQ51XHk iMcg2ci8llLkY6ohEY/O3WbB8qZqZR9l4FeV4NEwhfbgJIy2izWMxssc/jRPJzkRvqpEO M+VjHDPbRt0FCAnLAkcAvnQYBIo+DRNnralfVePDkWug75dOdKpDz+KGGLeVgH8TvdI8f nrCVn0tMcJHQwfLv7w7+LP4ZJY4Zt2STqdhnMLvsScZF29hWot2diMBA9zNAaBRuz4EVS 79/JTbg1aHpCFaMBGhZKIGPB5z0l41xWoHshf3Vem6nZbsSZf1etfXaNxSGBbIlYZzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796354638; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=lZBMsgGxE+z2C4I8K/azr1b4vVbT7U9RIF4jjVrxno4=; b=AQ6pvc+n/UG8kBB5kTRyYFoZC8Xa6dAnLUDWLNr3H82xxx1KXaiY8O92TvnoNBxzq7sZ xz5zvz4jwPYR7h6qzjrnPO2xNpqvp05dMO5yNnQCwOZjF0uD9oBbU0CAYBBnyY/h2cOxV 0qZowpEWGw15MqcoblzeGJM05BcJPCxpCkFwii1FFCy22qLEmISkz/4dfwNT3CVIWw437 luMLCSkUf1V3G9sD9iSLBgkxKvJ47RXgUEHZbsb6RwevM6AuSShploPkUGibiuD82VeXz RYpsPYPGljLpy7Lmii4gZXdErnfRJfPNDo1eyZ3HVRmRlVJh4m9L1mSyq4fGWojqb4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=kfYft0HU; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E630606A322; Fri, 4 Dec 2026 03:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=kfYft0HU Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19D971F00897; Fri, 4 Dec 2026 03:23:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796354638; bh=lZBMsgGxE+z2C4I8K/azr1b4vVbT7U9RIF4jjVrxno4=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=kfYft0HUnxw7DQeBqjy0/IsNRs6bXm3brbnen5ZDuln47v5MbZeArzTvyBsVmtSXI 8jRvkb7Xtd4VTS/aXeZGknVBnyJyxQx93DP5RY+HFHcSM/pqhYz+eFN2w2Qo9uDnZ7 5ORSQ1JG0zBGvO1gp62deMAIf/VtU35EV7Pg3B9s= Date: Thu, 3 Dec 2026 19:06:11 +0100 From: Greg Kroah-Hartman To: joe@sudmix.de Cc: Alan Stern , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] usb: core: sysfs: show product, manufacturer and serial without the device lock Message-ID: <2026120355-dropkick-owl-c480@gregkh> References: <20261014-usb-string-attrs-v2-1-c5cacdad7094@sudmix.de> X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261014-usb-string-attrs-v2-1-c5cacdad7094@sudmix.de> On Wed, Oct 14, 2026 at 11:01:51PM +0200, Joe Finzel via B4 Relay wrote: > From: Joe Finzel > > 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. > 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. > 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. > -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. 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...) > Reviewed-by: Alan Stern > Signed-off-by: Joe Finzel > --- > 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/20261011-usb-string-attrs-v1-1-81c01b035237@sudmix.de > --- > drivers/usb/core/sysfs.c | 18 ++++++++---------- > 1 file changed, 8 insertions(+), 10 deletions(-) > > diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c > index d22dc78457d..07865eb1f3b 100644 > --- a/drivers/usb/core/sysfs.c > +++ b/drivers/usb/core/sysfs.c > @@ -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. > + */ > #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); \ 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