USB: UAS: don't unbind and rebind the driver during usb_reset_device
Subsystems (3)
PATCH 1/1 USB: UAS: don't unbind and rebind the driver during usb_reset_device
Once pre_reset() or post_reset() returns non-zero, the disconnect() and probe() of the usb_driver will be called. In the disconnect(), the scsi_host will be removed and be freed after scsi_host_put(), in the probe(), the new scsi_host and uas_dev_info will be created.
If the usb_reset_device() is triggered by eh_device_reset_handler(), and pre_reset()/post_reset() returns non-zero, the disconnect() and probe() will be called, then returns to the eh_device_reset_handler(), it still accesses old scsi related variables and uas_dev_info, and so do its caller functions.
Here change the pre_reset() and post_reset() to let them only return 0, after this change, the usb_reset_device() will only reset this usb devcie from its hub port, will not execute unbind and rebind usb_driver during reset.
--- drivers/usb/storage/uas.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/storage/uas.c +1 -2
@@ -1121,7 +1121,6 @@ static int uas_pre_reset(struct usb_interface *intf)if (uas_wait_for_pending_cmnds (devinfo) !=0 ) {shost_printk (KERN_ERR , shost,"%s: timed out\n" , __func__);scsi_unblock_requests (shost); -return 1 ; }uas_free_streams (devinfo);
@@ -1152,7 +1151,7 @@ static int uas_post_reset(struct usb_interface *intf)scsi_unblock_requests (shost); -return err ?1 :0 ; +return 0 ; }static int uas_suspend (struct usb_interface *intf,pm_message_t message)
Signature · 1 line
2.25.1
Hi,
Quoted 11 lines · Hui Wang
Once
pre_reset()orpost_reset()returns non-zero, thedisconnect()andprobe()of theusb_driverwill be called. In thedisconnect(), thescsi_hostwill be removed and be freed afterscsi_host_put(), in theprobe(), the newscsi_hostanduas_dev_infowill be created.If the
usb_reset_device()is triggered byeh_device_reset_handler(), and pre_reset()/post_reset() returns non-zero, thedisconnect()andprobe()will be called, then returns to theeh_device_reset_handler(), it still accesses old scsi related variables anduas_dev_info, and so do its caller functions.
Quoted 4 lines
Here change the
pre_reset()andpost_reset()to let them only return 0, after this change, theusb_reset_device()will only reset this usb devcie from its hub port, will not execute unbind and rebindusb_driverduring reset.
We only return non 0 from the pre/post reset handles if we failed to ensure the device is in a known state.
E.g. in uas_post_reset() we only fail if we failed to switch the device from the good old usb-storage protocol back to the UAS mode which it was in before.
Continuing with the UAS driver bound, as if everything is fine, while the device is actually in longer in UAS mode is a bad idea.
Summarizing this patch is wrong: NACK.
###
I assume that you wrote this patch because of some bug report ?
In such a case please always include a link to the bug (or forum discussion) in the commit message.
UAS problems typically are caused by people shoving a 2.5 inch or m2 SSD in an USB enclosure which is powered from the USB bus only. SSD-s can cause pretty hefty power-consumption peaks when high queue depts are used; and these bus powered devices typically cannot handle these peaks. Either because the port they are plugged in does not deliver enough current; and/or because they don't have enough buffering (capacitors) on the enclosure's PCB to deal with short but quite large consumption peaks.
Regards,
Hans
Quoted 28 lines
Signed-off-by Hui Wang <[email protected]>--- drivers/usb/storage/uas.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/usb/storage/uas.c +1
-2@@ -1121,7 +1121,6 @@ static int uas_pre_reset(struct usb_interface *intf)if (uas_wait_for_pending_cmnds (devinfo) !=0 ) {shost_printk (KERN_ERR , shost,"%s: timed out\n" , __func__);scsi_unblock_requests (shost); -return 1 ; }uas_free_streams (devinfo);@@ -1152,7 +1151,7 @@ static int uas_post_reset(struct usb_interface *intf)scsi_unblock_requests (shost); -return err ?1 :0 ; +return 0 ; }static int uas_suspend (struct usb_interface *intf,pm_message_t message)
Quoted 28 lines · Hans de Goede
Hi,
Quoted 11 lines · Hui Wang
Once
pre_reset()orpost_reset()returns non-zero, thedisconnect()andprobe()of theusb_driverwill be called. In thedisconnect(), thescsi_hostwill be removed and be freed afterscsi_host_put(), in theprobe(), the newscsi_hostanduas_dev_infowill be created.If the
usb_reset_device()is triggered byeh_device_reset_handler(), and pre_reset()/post_reset() returns non-zero, thedisconnect()andprobe()will be called, then returns to theeh_device_reset_handler(), it still accesses old scsi related variables anduas_dev_info, and so do its caller functions.Quoted 4 lines
Here change the
pre_reset()andpost_reset()to let them only return 0, after this change, theusb_reset_device()will only reset this usb devcie from its hub port, will not execute unbind and rebindusb_driverduring reset.We only return non 0 from the pre/post reset handles if we failed to ensure the device is in a known state.
E.g. in
uas_post_reset()we only fail if we failed to switch the device from the good old usb-storage protocol back to the UAS mode which it was in before.Continuing with the UAS driver bound, as if everything is fine, while the device is actually in longer in UAS mode is a bad idea.
Quoted 8 lines
Summarizing this patch is wrong: NACK.
###
I assume that you wrote this patch because of some bug report ?
In such a case please always include a link to the bug (or forum discussion) in the commit message.
OK, got it, so far there is no bug about it. I just wrote some code and called usb_reset_device(), found it will trigger to call disconnect() and probe() in some cases. I found uas.c could trigger to call disconnect() and probe() in eh_device_reset_handler(), and could possibility introduce use-after-free issue. I thought resetting the device from the hub port is enough to let it back to normal work mode, if it doesn't, calling disconnect() and probe() will not help either. And I checked some eh_device_reset_handler callback in other drivers, looks like they don't trigger unbind and rebind, then I wrote this patch.
Thanks,
Hui.
Quoted 48 lines
UAS problems typically are caused by people shoving a 2.5 inch or m2 SSD in an USB enclosure which is powered from the USB bus only. SSD-s can cause pretty hefty power-consumption peaks when high queue depts are used; and these bus powered devices typically cannot handle these peaks. Either because the port they are plugged in does not deliver enough current; and/or because they don't have enough buffering (capacitors) on the enclosure's PCB to deal with short but quite large consumption peaks.
Regards,
Hans
Quoted 27 lines
Signed-off-by Hui Wang <[email protected]>--- drivers/usb/storage/uas.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/usb/storage/uas.c +1
-2@@ -1121,7 +1121,6 @@ static int uas_pre_reset(struct usb_interface *intf)if (uas_wait_for_pending_cmnds (devinfo) !=0 ) {shost_printk (KERN_ERR , shost,"%s: timed out\n" , __func__);scsi_unblock_requests (shost); -return 1 ; }uas_free_streams (devinfo);@@ -1152,7 +1151,7 @@ static int uas_post_reset(struct usb_interface *intf)scsi_unblock_requests (shost); -return err ?1 :0 ; +return 0 ; }static int uas_suspend (struct usb_interface *intf,pm_message_t message)
Oliver Neukum
maintainerAm Sonntag, den 21.02.2021, 11:20 +0100 schrieb Hans de Goede:
Quoted 1 line
Hi,
Hi,
Quoted 13 lines · Hui Wang
Quoted 11 lines
Once
pre_reset()orpost_reset()returns non-zero, thedisconnect()andprobe()of theusb_driverwill be called. In thedisconnect(), thescsi_hostwill be removed and be freed afterscsi_host_put(), in theprobe(), the newscsi_hostanduas_dev_infowill be created.If the
usb_reset_device()is triggered byeh_device_reset_handler(), and pre_reset()/post_reset() returns non-zero, thedisconnect()andprobe()will be called, then returns to theeh_device_reset_handler(), it still accesses old scsi related variables anduas_dev_info, and so do its caller functions.
Quoted 7 lines
Quoted 4 lines
Here change the
pre_reset()andpost_reset()to let them only return 0, after this change, theusb_reset_device()will only reset this usb devcie from its hub port, will not execute unbind and rebindusb_driverduring reset.We only return non 0 from the pre/post reset handles if we failed to ensure the device is in a known state.
correct. Technically it is a bit unfortunate that UAS devices react a bit different to other SCSI devices, but we definitely cannot hide a failure. Arguably we should go into OFFLINE state.
But that needs a
good reason beyond theoretical considerations.
Regards
Oliver
Quoted 22 lines · Oliver Neukum
Am Sonntag, den 21.02.2021, 11:20 +0100 schrieb Hans de Goede:
Quoted 1 line
Hi,
Hi,
Quoted 12 lines · Hui Wang
Quoted 11 lines
Once
pre_reset()orpost_reset()returns non-zero, thedisconnect()andprobe()of theusb_driverwill be called. In thedisconnect(), thescsi_hostwill be removed and be freed afterscsi_host_put(), in theprobe(), the newscsi_hostanduas_dev_infowill be created.If the
usb_reset_device()is triggered byeh_device_reset_handler(), and pre_reset()/post_reset() returns non-zero, thedisconnect()andprobe()will be called, then returns to theeh_device_reset_handler(), it still accesses old scsi related variables anduas_dev_info, and so do its caller functions.Quoted 6 lines
Quoted 4 lines
Here change the
pre_reset()andpost_reset()to let them only return 0, after this change, theusb_reset_device()will only reset this usb devcie from its hub port, will not execute unbind and rebindusb_driverduring reset.We only return non 0 from the pre/post reset handles if we failed to ensure the device is in a known state.
Quoted 10 lines
correct. Technically it is a bit unfortunate that UAS devices react a bit different to other SCSI devices, but we definitely cannot hide a failure. Arguably we should go into OFFLINE state.
But that needs a
good reason beyond theoretical considerations.Regards
Oliver
OK, will find a UAS device to do the test.
thanks,
Hui.
Oliver Neukum
maintainerAm Montag, den 22.02.2021, 20:40 +0800 schrieb Hui Wang:
Quoted 3 lines · Oliver Neukum
Quoted 1 line
OK, will find a UAS device to do the test.
Hi,
do you have a design at all?
Regards
Oliver
Quoted 6 lines · Oliver Neukum
Am Montag, den 22.02.2021, 20:40 +0800 schrieb Hui Wang:
Quoted 2 lines · Oliver Neukum
OK, will find a UAS device to do the test.
Hi,
do you have a design at all?
No, so far what I could find is all driven by usb-storage, I tested a couple of usb-sdcard-readers and usb-scsi/ata disk adapters, they all belong to USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) instead of USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS). I plan to go to the office to find some usb storage devices to test.
Regards,
Hui.
Quoted 5 lines
Regards
Oliver
Oliver Neukum
maintainerAm Montag, den 22.02.2021, 21:02 +0800 schrieb Hui Wang:
Quoted 6 lines · Oliver Neukum
Quoted 5 lines
Am Montag, den 22.02.2021, 20:40 +0800 schrieb Hui Wang:
Quoted 2 lines · Oliver Neukum
OK, will find a UAS device to do the test.
Hi,
Quoted 8 lines
Quoted 1 line
do you have a design at all?
No, so far what I could find is all driven by usb-storage, I tested a couple of usb-sdcard-readers and usb-scsi/ata disk adapters, they all belong to
USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK)instead ofUSB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS). I plan to go to the office to find some usb storage devices to test.
Hi,
please wait. First of all, you are making the assumption that all resets originate from the SCSI layer. You cannot make that assumption.
Secondly, yes, ideally we should not pretend that a disconnect has happened, when it hasn't happened, but what is your alternative. What exactly do you want to test? You have not even defined the desirable behavior and the problem you are seeing with the current behavior.
Regards
Oliver
Quoted 16 lines · Oliver Neukum
Am Montag, den 22.02.2021, 21:02 +0800 schrieb Hui Wang:
Quoted 6 lines · Oliver Neukum
Quoted 5 lines
Am Montag, den 22.02.2021, 20:40 +0800 schrieb Hui Wang:
Quoted 2 lines · Oliver Neukum
OK, will find a UAS device to do the test.
Hi,
Quoted 7 lines
Quoted 1 line
do you have a design at all?
No, so far what I could find is all driven by usb-storage, I tested a couple of usb-sdcard-readers and usb-scsi/ata disk adapters, they all belong to
USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK)instead ofUSB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS). I plan to go to the office to find some usb storage devices to test.Hi,
Quoted 8 lines
please wait. First of all, you are making the assumption that all resets originate from the SCSI layer. You cannot make that assumption.
Secondly, yes, ideally we should not pretend that a disconnect has happened, when it hasn't happened, but what is your alternative. What exactly do you want to test? You have not even defined the desirable behavior and the problem you are seeing with the current behavior.
I planed to forcibly (simulate) trigger calling eh_device_reset_handler() from scsi layer and let pre_reset() or post_reset() return a non-zero, and test if there is use-after-free issue in the rest part of eh_device_reset_handler() and its callers. But after thinking of your comment, looks like I was wrong. Thanks for your instructions on this issue.
Thanks,
Hui.
Quoted 4 lines
Regards
Oliver