Re: [usb-storage] [PATCH v4] usb-storage: ene_ub6250: don't let the card-type probe hang forever

From: Alan Stern

Date: Wed Nov 11 2026 - 11:38:37 EST


On Mon, Nov 09, 2026 at 04:54:49PM +0800, Hongyu Xie wrote:
> ene_ub6250_probe() reads the card type with ene_get_card_type() right
> after usb_stor_probe2() has added the SCSI host. The query is a
> bulk-only transaction (CBW, one-byte data-in, CSW) sent through
> ene_send_scsi_cmd(), which uses usb_stor_bulk_transfer_buf() and
> therefore waits for URB completion without any timeout. That is safe
> only for transfers belonging to a SCSI command, where the command's
> timeout and abort handling terminate the wait. A card-type query has
> no SCSI command behind it, so a device which enumerates but never
> services bulk transfers blocks the probe forever.
>
> commit 445fc368c6bc ("usb-storage: ene_ub6250: fix race between scan
> work and probe") added us->dev_mutex around the query to keep it from
> racing with the scan work for us->current_urb. usb_stor_probe2()
> queues that work with a one-second delay, so it now waits for a
> transfer which may never finish. The query itself has been unbounded
> since commit 41e568d14ec0 ("Staging: Merge ENE UB6250 SD card codes
> from keucr to drivers/usb/storage"), but only the latter turns the
> hang into the lockup reported here, hence the Fixes tag.
> syzbot reports:
>
> INFO: task kworker/0:2:53 blocked for more than 143 seconds.
> ...
> __mutex_lock
> usb_stor_scan_dwork
> ...
> INFO: task syz-executor339:5675 blocked for more than 143 seconds.
> ...
> device_lock
> usbdev_open
>
> with the hub worker holding both locks stalled in
> ene_ub6250_probe() -> ene_get_card_type() -> usb_stor_msg_common().
>
> Run the query from ->initFunction instead. usb_stor_acquire_resources()
> calls it before the control thread is started and before the scan work
> is queued, so nothing else can be using us->current_urb and the
> dev_mutex from that commit can go away. A failed query now unwinds
> through usb_stor_probe2() before the SCSI host is registered, instead
> of going through usb_stor_disconnect() and cancel_delayed_work_sync()
> on a scan work which is itself stuck on the dead device.

Makes sense.

> Also bound the whole query with a 10-second budget covering the CBW,
> the data stage and the CSW. Per-transfer timeouts would not be
> enough, since a device can stretch the query over the data stage and
> the two CSW retries; each transfer gets the time remaining in the
> budget.

I don't think this matters. A timeout at any stage should cause the
entire transfer to fail immediately. As for the possibility of slow
stages completing just before the timeout expires... How often do you
expect that case to come up? Does we care if the initialization
sometimes is a little slow, so long as the total time is still limited?

However, it's not a big deal one way or the other.

> diff --git a/drivers/usb/storage/ene_ub6250.c b/drivers/usb/storage/ene_ub6250.c
> index fea2614e767c7..2b4fab31e900d 100644
> --- a/drivers/usb/storage/ene_ub6250.c
> +++ b/drivers/usb/storage/ene_ub6250.c
> @@ -24,6 +24,15 @@
>
> #define DRV_NAME "ums_eneub6250"
>
> +/*
> + * Time budget for the card-type query done while probing, in jiffies. It
> + * is a bulk transfer with no SCSI command behind it, so nothing else would
> + * terminate the wait if the device stopped responding.
> + */

Detailed explanation not needed here; you explain the reasoning in
ene_ub6250_init() below.

> +#define ENE_PROBE_TIMEOUT (10 * HZ)
> +
> +static int ene_ub6250_init(struct us_data *us);

Unnecessary forward declaration.

> +
> MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
> MODULE_LICENSE("GPL");
> MODULE_IMPORT_NS("USB_STORAGE");
> @@ -487,39 +496,76 @@ static void ene_ub6250_info_destructor(void *extra)
> kfree(info->bbuf);
> }
>
> -static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
> +/*
> + * Return what is left of a budget of @timeout jiffies which started at
> + * @start, in jiffies, or -ETIMEDOUT if the budget has been used up. A zero
> + * @timeout means no limit.
> + */
> +static int ene_time_left(unsigned long start, unsigned int timeout)
> +{
> + unsigned long elapsed;
> +
> + if (!timeout)
> + return 0;
> +
> + elapsed = jiffies - start;
> + if (elapsed >= timeout)
> + return -ETIMEDOUT;
> +
> + return min_t(unsigned long, timeout - elapsed, INT_MAX);

You're mixing unsigned long and unsigned int in a confusing way. Why
can't elapsed be an unsigned int? You don't expect ever to encounter
delays longer than 4 billion jiffies, do you?

Also, if you don't care about an overall timeout, this routine isn't
needed.

> +}
> +
> +/*
> + * Send an ENE SCSI command. @timeout is the time budget for the whole
> + * transaction (CBW, data and CSW), in jiffies; zero means no limit.
> + */
> +static int ene_send_scsi_cmd_timeout(struct us_data *us, u8 fdir, void *buf,
> + int use_sg, unsigned int timeout)
> {
> struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
> struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
> -
> - int result;
> + unsigned long start = jiffies;
> + int result, left;
> unsigned int residue;
> unsigned int cswlen = 0, partial = 0;
> unsigned int transfer_length = bcb->DataTransferLength;
>
> + left = ene_time_left(start, timeout);
> + if (left < 0)
> + return USB_STOR_TRANSPORT_ERROR;

What point is there in checking for a timeout before anything has
happened?

> +
> /* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */
> /* send cmd to out endpoint */
> - result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
> - bcb, US_BULK_CB_WRAP_LEN, NULL);
> + result = usb_stor_bulk_transfer_buf_timeout(us, us->send_bulk_pipe,
> + bcb, US_BULK_CB_WRAP_LEN,
> + NULL, left);
> if (result != USB_STOR_XFER_GOOD) {
> usb_stor_dbg(us, "send cmd to out endpoint fail ---\n");
> return USB_STOR_TRANSPORT_ERROR;
> }
>
> if (buf) {
> - unsigned int pipe = fDir;
> + unsigned int pipe = fdir;

Unnecessary initialization.

>
> - if (fDir == FDIR_READ)
> + if (fdir == FDIR_READ)
> pipe = us->recv_bulk_pipe;
> else
> pipe = us->send_bulk_pipe;
>
> /* Bulk */
> if (use_sg) {
> + /*
> + * Scatter-gather transfers belong to SCSI commands,
> + * whose timeout and abort machinery bound the wait.
> + */
> result = usb_stor_bulk_srb(us, pipe, us->srb);
> } else {
> - result = usb_stor_bulk_transfer_sg(us, pipe, buf,
> - transfer_length, 0, &partial);
> + left = ene_time_left(start, timeout);
> + if (left < 0)
> + return USB_STOR_TRANSPORT_ERROR;
> + result = usb_stor_bulk_transfer_buf_timeout(us, pipe,
> + buf, transfer_length, &partial,

Things like this are why I maintain that the policy of aligning
continuation lines with the preceding open paren is a mistake. Grumble.

The rest of the material looks pretty much okay. However, I would
prefer this to be made into a series of two patches, one with the
modifications to transport.[ch] and the other with the modifications to
ene_ub6250.

Alan Stern