Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size
@ 2026-11-16  9:34 YuanZhe Liu
  2026-11-16  9:39 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: YuanZhe Liu @ 2026-11-16  9:34 UTC (permalink / raw)
  To: Alan Stern, Greg Kroah-Hartman
  Cc: linux-usb, usb-storage, linux-kernel, stable, Bin Yu, Cheng Wen

usbat_flash_read_data() and usbat_flash_write_data() divide the transfer
length by info->ssize to turn it into an ATA sector count.  info->ssize is
only assigned when a READ CAPACITY command has completed successfully, so
it is still zero when a read request arrives earlier.

Such a request is sent during device scan: sd_probe() starts with
media_present set and usb-storage sets read_before_ms for every device, so
sd_revalidate_disk() calls sd_read_block_zero() right after
sd_read_capacity(), even when READ CAPACITY failed, because
read_capacity_error() deliberately keeps media_present set for drives that
fail it with media loaded.  A CompactFlash reader that does not complete
the READ CAPACITY sequence, for example because a register access is
stalled, therefore reaches usbat_flash_read_data() with info->ssize == 0:

  Oops: divide error
  RIP: 0010:usbat_flash_read_data+0x2a4/0x5c0

Reject read and write requests while the sector size is unknown, the same
way the driver already rejects an out of range LBA.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
 drivers/usb/storage/shuttle_usbat.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 7e5424268c73..faea2c77c5aa 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -1130,6 +1130,13 @@ static int usbat_flash_read_data(struct us_data *us,
 	if (result != USB_STOR_TRANSPORT_GOOD)
 		return result;
 
+	/*
+	 * The sector size is only known once a READ CAPACITY command has
+	 * succeeded; a request can arrive before that.
+	 */
+	if (!info->ssize)
+		return USB_STOR_TRANSPORT_ERROR;
+
 	/*
 	 * we're working in LBA mode.  according to the ATA spec,
 	 * we can support up to 28-bit addressing.  I don't know if Jumpshot
@@ -1221,6 +1228,13 @@ static int usbat_flash_write_data(struct us_data *us,
 	if (result != USB_STOR_TRANSPORT_GOOD)
 		return result;
 
+	/*
+	 * The sector size is only known once a READ CAPACITY command has
+	 * succeeded; a request can arrive before that.
+	 */
+	if (!info->ssize)
+		return USB_STOR_TRANSPORT_ERROR;
+
 	/*
 	 * we're working in LBA mode.  according to the ATA spec,
 	 * we can support up to 28-bit addressing.  I don't know if the device
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size
  2026-11-16  9:34 [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size YuanZhe Liu
@ 2026-11-16  9:39 ` sashiko-bot
  2026-11-16 18:48 ` Alan Stern
  2026-11-17  1:27 ` [PATCH v2] usb: storage: shuttle_usbat: set the ATA sector size at device init YuanZhe Liu
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-16  9:39 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-usb

> usbat_flash_read_data() and usbat_flash_write_data() divide the transfer
> length by info->ssize to turn it into an ATA sector count.  info->ssize is
> only assigned when a READ CAPACITY command has completed successfully, so
> it is still zero when a read request arrives earlier.
> 
> Such a request is sent during device scan: sd_probe() starts with
> media_present set and usb-storage sets read_before_ms for every device, so
> sd_revalidate_disk() calls sd_read_block_zero() right after
> sd_read_capacity(), even when READ CAPACITY failed, because
> read_capacity_error() deliberately keeps media_present set for drives that
> fail it with media loaded.  A CompactFlash reader that does not complete
> the READ CAPACITY sequence, for example because a register access is
> stalled, therefore reaches usbat_flash_read_data() with info->ssize == 0:
> 
>   Oops: divide error
>   RIP: 0010:usbat_flash_read_data+0x2a4/0x5c0
> 
> Reject read and write requests while the sector size is unknown, the same
> way the driver already rejects an out of range LBA.
> 
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116093452.15286-1-25031212351@stu.xidian.edu.cn?part=1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size
  2026-11-16  9:34 [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size YuanZhe Liu
  2026-11-16  9:39 ` sashiko-bot
@ 2026-11-16 18:48 ` Alan Stern
  2026-11-17  1:27 ` [PATCH v2] usb: storage: shuttle_usbat: set the ATA sector size at device init YuanZhe Liu
  2 siblings, 0 replies; 5+ messages in thread
From: Alan Stern @ 2026-11-16 18:48 UTC (permalink / raw)
  To: YuanZhe Liu
  Cc: Greg Kroah-Hartman, linux-usb, usb-storage, linux-kernel, stable,
	Bin Yu, Cheng Wen

On Mon, Nov 16, 2026 at 05:34:52PM +0800, YuanZhe Liu wrote:
> usbat_flash_read_data() and usbat_flash_write_data() divide the transfer
> length by info->ssize to turn it into an ATA sector count.  info->ssize is
> only assigned when a READ CAPACITY command has completed successfully, so
> it is still zero when a read request arrives earlier.
> 
> Such a request is sent during device scan: sd_probe() starts with
> media_present set and usb-storage sets read_before_ms for every device, so
> sd_revalidate_disk() calls sd_read_block_zero() right after
> sd_read_capacity(), even when READ CAPACITY failed, because
> read_capacity_error() deliberately keeps media_present set for drives that
> fail it with media loaded.  A CompactFlash reader that does not complete
> the READ CAPACITY sequence, for example because a register access is
> stalled, therefore reaches usbat_flash_read_data() with info->ssize == 0:
> 
>   Oops: divide error
>   RIP: 0010:usbat_flash_read_data+0x2a4/0x5c0
> 
> Reject read and write requests while the sector size is unknown, the same
> way the driver already rejects an out of range LBA.

How about initializing info->ssize to 512 (it's currently hard-coded in 
usbat_flash_transport()) earlier on, so the value is always available?

Alan Stern

> 
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
> ---
>  drivers/usb/storage/shuttle_usbat.c | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
> 
> diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
> index 7e5424268c73..faea2c77c5aa 100644
> --- a/drivers/usb/storage/shuttle_usbat.c
> +++ b/drivers/usb/storage/shuttle_usbat.c
> @@ -1130,6 +1130,13 @@ static int usbat_flash_read_data(struct us_data *us,
>  	if (result != USB_STOR_TRANSPORT_GOOD)
>  		return result;
>  
> +	/*
> +	 * The sector size is only known once a READ CAPACITY command has
> +	 * succeeded; a request can arrive before that.
> +	 */
> +	if (!info->ssize)
> +		return USB_STOR_TRANSPORT_ERROR;
> +
>  	/*
>  	 * we're working in LBA mode.  according to the ATA spec,
>  	 * we can support up to 28-bit addressing.  I don't know if Jumpshot
> @@ -1221,6 +1228,13 @@ static int usbat_flash_write_data(struct us_data *us,
>  	if (result != USB_STOR_TRANSPORT_GOOD)
>  		return result;
>  
> +	/*
> +	 * The sector size is only known once a READ CAPACITY command has
> +	 * succeeded; a request can arrive before that.
> +	 */
> +	if (!info->ssize)
> +		return USB_STOR_TRANSPORT_ERROR;
> +
>  	/*
>  	 * we're working in LBA mode.  according to the ATA spec,
>  	 * we can support up to 28-bit addressing.  I don't know if the device
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] usb: storage: shuttle_usbat: set the ATA sector size at device init
  2026-11-16  9:34 [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size YuanZhe Liu
  2026-11-16  9:39 ` sashiko-bot
  2026-11-16 18:48 ` Alan Stern
@ 2026-11-17  1:27 ` YuanZhe Liu
  2026-11-17  1:32   ` sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: YuanZhe Liu @ 2026-11-17  1:27 UTC (permalink / raw)
  To: Alan Stern, Greg Kroah-Hartman
  Cc: linux-usb, usb-storage, linux-kernel, stable, Bin Yu, Cheng Wen

usbat_flash_read_data() and usbat_flash_write_data() divide the transfer
length by info->ssize to turn it into an ATA sector count.  info->ssize is
only assigned in the READ CAPACITY handler, so it is still zero when a
read request arrives earlier.

Such a request is sent during device scan: sd_probe() starts with
media_present set and usb-storage sets read_before_ms for every device, so
sd_revalidate_disk() calls sd_read_block_zero() right after
sd_read_capacity(), even when READ CAPACITY failed, because
read_capacity_error() deliberately keeps media_present set for drives that
fail it with media loaded.  A CompactFlash reader that does not complete
the READ CAPACITY sequence, for example because a register access is
stalled, therefore reaches usbat_flash_read_data() with info->ssize == 0:

  Oops: divide error
  RIP: 0010:usbat_flash_read_data+0x2a4/0x5c0

The ATA sector size of these readers is fixed at 512 bytes, so record it
when the USBAT flash transport is selected, instead of waiting for a READ
CAPACITY command to succeed.  The datafab and jumpshot drivers, which
share this code, already set it during device identification for the same
reason.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
Changes in v2:
 - Set info->ssize when the flash transport is selected, as suggested by
   Alan Stern, so the value is always available.
 - Drop the assignment from the READ CAPACITY handler, now redundant.
---
 drivers/usb/storage/shuttle_usbat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 7e5424268c73..267c82381465 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -1038,6 +1038,8 @@ static int usbat_set_transport(struct us_data *us,
 		break;
 
 	case USBAT_DEV_FLASH:
+		/* The ATA sector size of these readers is fixed at 512 bytes */
+		info->ssize = 0x200;
 		us->transport = usbat_flash_transport;
 		break;
 	}
@@ -1703,8 +1705,6 @@ static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
 		if (rc != USB_STOR_TRANSPORT_GOOD)
 			return rc;
 
-		/* hard coded 512 byte sectors as per ATA spec */
-		info->ssize = 0x200;
 		usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
 			     info->sectors, info->ssize);
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] usb: storage: shuttle_usbat: set the ATA sector size at device init
  2026-11-17  1:27 ` [PATCH v2] usb: storage: shuttle_usbat: set the ATA sector size at device init YuanZhe Liu
@ 2026-11-17  1:32   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-17  1:32 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-usb

> usbat_flash_read_data() and usbat_flash_write_data() divide the transfer
> length by info->ssize to turn it into an ATA sector count.  info->ssize is
> only assigned in the READ CAPACITY handler, so it is still zero when a
> read request arrives earlier.
> 
> Such a request is sent during device scan: sd_probe() starts with
> media_present set and usb-storage sets read_before_ms for every device, so
> sd_revalidate_disk() calls sd_read_block_zero() right after
> sd_read_capacity(), even when READ CAPACITY failed, because
> read_capacity_error() deliberately keeps media_present set for drives that
> fail it with media loaded.  A CompactFlash reader that does not complete
> the READ CAPACITY sequence, for example because a register access is
> stalled, therefore reaches usbat_flash_read_data() with info->ssize == 0:
> 
>   Oops: divide error
>   RIP: 0010:usbat_flash_read_data+0x2a4/0x5c0
> 
> The ATA sector size of these readers is fixed at 512 bytes, so record it
> when the USBAT flash transport is selected, instead of waiting for a READ
> CAPACITY command to succeed.  The datafab and jumpshot drivers, which
> share this code, already set it during device identification for the same
> reason.
> 
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117012707.5858-1-25031212351@stu.xidian.edu.cn?part=1


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-17  1:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  9:34 [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size YuanZhe Liu
2026-11-16  9:39 ` sashiko-bot
2026-11-16 18:48 ` Alan Stern
2026-11-17  1:27 ` [PATCH v2] usb: storage: shuttle_usbat: set the ATA sector size at device init YuanZhe Liu
2026-11-17  1:32   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox