[PATCH v4 20/33] USB: Drain usbfs when a device is unregistered

From: Leon Romanovsky

Date: Tue Nov 24 2026 - 04:48:20 EST


From: Leon Romanovsky <leonro@xxxxxxxxxx>

Only the generic USB device driver tells usbfs that a device is gone.
A device without that driver bound, such as one unbound through sysfs,
is unplugged without any notification. usbdev_open() does not check for
a driver, so a process can open such a device and sleep in poll() on its
idle descriptor. The unplug never wakes it or sends its disconnect
signal.

Drain usbfs from usb_disconnect() before device_del(). The device is
already marked as not attached, so no new user can open it. The later
drain from the generic driver finds nothing left.

Fixes: 782da727b0d59 ("usbcore: make usb_generic a usb_device_driver")
Signed-off-by: Leon Romanovsky <leonro@xxxxxxxxxx>
---
drivers/usb/core/devio.c | 4 ++--
drivers/usb/core/hub.c | 3 +++
drivers/usb/core/usb.h | 1 +
3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 1491ba015bcd8..a881f06361bfa 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -2862,7 +2862,7 @@ const struct file_operations usbdev_file_operations = {
.release = usbdev_release,
};

-static void usbdev_remove(struct usb_device *udev)
+void usbfs_notify_remove(struct usb_device *udev)
{
struct usb_dev_state *ps;

@@ -2889,7 +2889,7 @@ static int usbdev_notify(struct notifier_block *self,
case USB_DEVICE_ADD:
break;
case USB_DEVICE_REMOVE:
- usbdev_remove(dev);
+ usbfs_notify_remove(dev);
break;
}
return NOTIFY_OK;
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 199d3f535a891..372bd6368cc94 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -2379,6 +2379,9 @@ void usb_disconnect(struct usb_device **pdev)
if (udev->usb4_link)
device_link_del(udev->usb4_link);

+ /* usbfs is otherwise notified only by a bound generic driver */
+ usbfs_notify_remove(udev);
+
/* Unregister the device. The device driver is responsible
* for de-configuring the device and invoking the remove-device
* notifier chain (used by usbfs and possibly others).
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 1b2de58fd6cef..cbee731837471 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -195,6 +195,7 @@ extern const struct file_operations usbdev_file_operations;

extern int usb_devio_init(void);
extern void usb_devio_cleanup(void);
+void usbfs_notify_remove(struct usb_device *udev);

/*
* Firmware specific cookie identifying a port's location. '0' == no location

--
2.56.1