From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 12/13] usb: detect unplugged devices on transfer errors
Date: Mon, 31 Aug 2026 15:20:19 +0200 [thread overview]
Message-ID: <20260831-usb-device-lifetime-v1-12-6adf4054b909@pengutronix.de> (raw)
In-Reply-To: <20260831-usb-device-lifetime-v1-0-6adf4054b909@pengutronix.de>
Removing a device is only done when somebody runs "usb". Until then I/O
to a device that has been unplugged in the meantime keeps failing, and
neither the USB core nor the drivers realise why. Reading from a
filesystem that was mounted off a stick that is gone results in an
endless stream of
usb-storage usb1-0-1: Resetting EP 1...
ERROR: xHCI xHCI0: halted endpoint, not queueing URB.
as the storage driver keeps resetting endpoints of a device that is not
there and retries the command.
When a transfer fails, ask the hub the device is attached to whether the
port still reports a connection. That is cheap - the hub is still there -
and it answers the question the transfer error cannot: is this a flaky
device or no device at all. Remember the answer, so the bus is asked once
and every further transfer fails with -ENODEV right away.
Only a hub that positively reports an empty port counts. If the hub
cannot be asked we have learned nothing, and marking a device gone
because some unrelated transfer failed would be worse than leaving it
alone. A device behind a hub that turns out to be gone is marked as well,
along with everything below it.
Removal itself is left to the next scan. Tearing the device down from
inside a transfer would pull the block device and the cdevs out from
under the filesystem that is asking for the data.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Assisted-by: Claude:claude-opus-5
---
drivers/usb/core/hub.c | 22 ++++++++++++
drivers/usb/core/usb.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++--
drivers/usb/core/usb.h | 1 +
include/linux/usb/usb.h | 16 +++++++++
4 files changed, 126 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index f897740cbf..6520cd43d5 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -260,6 +260,28 @@ static int hub_port_reset(struct usb_device *hub, int port,
}
+/**
+ * usb_hub_port_connected - ask a hub whether a port still has a device
+ * @hub: the hub to ask
+ * @port: 1-based port number
+ *
+ * Return: 1 if the port reports a device, 0 if it doesn't and a negative
+ * error code when the hub could not be asked. Do not treat the latter as
+ * "no device": a hub that doesn't answer tells us nothing about what is
+ * plugged into it.
+ */
+int usb_hub_port_connected(struct usb_device *hub, int port)
+{
+ struct usb_port_status portsts;
+ int ret;
+
+ ret = usb_get_port_status(hub, port, &portsts);
+ if (ret < 0)
+ return ret;
+
+ return !!(le16_to_cpu(portsts.wPortStatus) & USB_PORT_STAT_CONNECTION);
+}
+
/**
* usb_hub_cancel_scans - drop pending port scans of a hub that goes away
* @dev: the USB device that is about to be removed
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 55c57e6334..71b72c3efb 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -751,6 +751,73 @@ int usb_rescan(void)
*
*/
+/**
+ * usb_mark_disconnected - remember that a device and its children are gone
+ * @dev: the device that has been unplugged
+ */
+static void usb_mark_disconnected(struct usb_device *dev)
+{
+ int i;
+
+ if (usb_device_disconnected(dev))
+ return;
+
+ dev->disconnected = true;
+ dev_info(&dev->dev, "disconnected\n");
+
+ /* everything behind an unplugged hub is gone as well */
+ for (i = 0; i < dev->maxchild; i++)
+ if (dev->children[i])
+ usb_mark_disconnected(dev->children[i]);
+}
+
+/**
+ * usb_device_check_gone - find out whether a failed transfer means "unplugged"
+ * @dev: the device a transfer just failed on
+ *
+ * A transfer can fail for all kinds of reasons, so ask the hub the device
+ * is attached to whether the port still sees a device. Without this the
+ * only thing telling a stale device from a flaky one is a scan, and until
+ * somebody runs one we keep resetting endpoints of a device that is not
+ * there anymore.
+ *
+ * The result is remembered, so this asks the bus once and all further
+ * transfers fail right away.
+ */
+static bool usb_device_check_gone(struct usb_device *dev)
+{
+ struct usb_device *parent = dev->parent;
+
+ if (usb_device_disconnected(dev))
+ return true;
+
+ /*
+ * A root hub has no hub to ask. It goes away with its controller,
+ * which is not something we can detect here.
+ */
+ if (!parent)
+ return false;
+
+ /* If the hub itself is gone, so is everything below it */
+ if (usb_device_check_gone(parent)) {
+ usb_mark_disconnected(dev);
+ return true;
+ }
+
+ /*
+ * Only believe a hub that positively says the port is empty. If it
+ * doesn't answer we have learned nothing, and declaring the device
+ * gone on a transfer that failed for some other reason would be
+ * worse than leaving it alone.
+ */
+ if (usb_hub_port_connected(parent, dev->portnr) != 0)
+ return false;
+
+ usb_mark_disconnected(dev);
+
+ return true;
+}
+
/*
* submits an Interrupt Message
*/
@@ -760,6 +827,9 @@ int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
struct usb_host *host = dev->host;
int ret;
+ if (usb_device_disconnected(dev))
+ return -ENODEV;
+
ret = usb_host_acquire(host);
if (ret)
return ret;
@@ -768,6 +838,9 @@ int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
usb_host_release(host);
+ if (ret && usb_device_check_gone(dev))
+ return -ENODEV;
+
return ret;
}
@@ -788,6 +861,9 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
int ret;
struct devrequest *setup_packet = dev->setup_packet;
+ if (usb_device_disconnected(dev))
+ return -ENODEV;
+
ret = usb_host_acquire(host);
if (ret)
return ret;
@@ -808,8 +884,11 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
usb_host_release(host);
- if (ret)
+ if (ret) {
+ if (usb_device_check_gone(dev))
+ return -ENODEV;
return ret;
+ }
return dev->act_len;
}
@@ -828,6 +907,9 @@ int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
if (len < 0)
return -1;
+ if (usb_device_disconnected(dev))
+ return -ENODEV;
+
ret = usb_host_acquire(host);
if (ret)
return ret;
@@ -837,8 +919,11 @@ int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
usb_host_release(host);
- if (ret)
+ if (ret) {
+ if (usb_device_check_gone(dev))
+ return -ENODEV;
return ret;
+ }
*actual_length = dev->act_len;
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index b3c224d88f..2529bcfec1 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -7,5 +7,6 @@ void usb_free_device(struct usb_device *dev);
int usb_new_device(struct usb_device *dev);
void usb_remove_device(struct usb_device *dev);
void usb_hub_cancel_scans(struct usb_device *dev);
+int usb_hub_port_connected(struct usb_device *hub, int port);
#endif /* __CORE_USB_H */
diff --git a/include/linux/usb/usb.h b/include/linux/usb/usb.h
index c25f3d73c3..784786dad5 100644
--- a/include/linux/usb/usb.h
+++ b/include/linux/usb/usb.h
@@ -126,8 +126,24 @@ struct usb_device {
/* slot_id - for xHCI enabled devices */
unsigned int slot_id;
+
+ /* device is physically gone, don't talk to it anymore */
+ bool disconnected;
};
+/**
+ * usb_device_disconnected - has the device been unplugged?
+ * @dev: the USB device
+ *
+ * Set once a transfer to the device failed and the hub it is attached to
+ * confirmed that the port has no connection anymore. Transfers to such a
+ * device fail with -ENODEV without touching the bus.
+ */
+static inline bool usb_device_disconnected(struct usb_device *dev)
+{
+ return dev->disconnected;
+}
+
struct usb_device_id;
struct usb_driver {
--
2.47.3
next prev parent reply other threads:[~2026-08-31 13:37 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-31 13:20 [PATCH 00/13] USB: Make USB devices removable Sascha Hauer
2026-08-31 13:20 ` [PATCH 01/13] usb: don't report device removal after the device name is gone Sascha Hauer
2026-08-31 13:20 ` [PATCH 02/13] fs: devfs: count an open partition as an open device Sascha Hauer
2026-08-31 13:20 ` [PATCH 03/13] block: propagate errors from blockdevice_unregister() Sascha Hauer
2026-08-31 13:20 ` [PATCH 04/13] fs: add cdev_umount_all() Sascha Hauer
2026-08-31 13:20 ` [PATCH 05/13] block: add blockdevice_unregister_removed() Sascha Hauer
2026-08-31 13:20 ` [PATCH 06/13] usb: storage: tear the disk down properly on disconnect Sascha Hauer
2026-08-31 13:20 ` [PATCH 07/13] usb: hub: cancel pending port scans of a removed device Sascha Hauer
2026-08-31 13:20 ` [PATCH 08/13] usb: reuse the addresses of removed devices Sascha Hauer
2026-08-31 13:20 ` [PATCH 09/13] usb: hub: detect disconnected devices Sascha Hauer
2026-08-31 13:20 ` [PATCH 10/13] usb: don't keep a dangling root device on enumeration failure Sascha Hauer
2026-08-31 13:20 ` [PATCH 11/13] usb: hub: limit the number of ports to USB_MAXCHILDREN Sascha Hauer
2026-08-31 13:20 ` Sascha Hauer [this message]
2026-08-31 13:20 ` [PATCH 13/13] usb: storage: stop talking to a device that is gone Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260831-usb-device-lifetime-v1-12-6adf4054b909@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox