[PATCH v5 2/2] usb: storage: set surprise_removal flag on physical removal

From: 胡连勤

Date: Fri Oct 09 2026 - 07:33:54 EST


Set the surprise_removal flag in struct Scsi_Host when the USB device
state is USB_STATE_NOTATTACHED, indicating the device has been
physically removed and cannot handle I/O.

This flag is checked by sd_remove() (added in the previous patch) to
skip sync_filesystem() during surprise removal, avoiding a hang waiting
for I/O that can never complete.

For orderly removal (e.g. rmmod, sysfs unbind) the USB device state is
not NOTATTACHED, so the flag is not set and sync proceeds normally,
preserving dirty data writeback.

Signed-off-by: Lianqin Hu <hulianqin@xxxxxxxx>
---
drivers/usb/storage/usb.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index 064c7fc8e368..f6c81f7ff9f1 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -930,6 +930,9 @@ static void quiesce_and_remove_host(struct us_data *us)
if (us->pusb_dev->state == USB_STATE_NOTATTACHED) {
set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
wake_up(&us->delay_wait);
+
+ /* Device is physically gone, cannot handle I/O */
+ host->surprise_removal = 1;
}

/*
--
2.48.1