The driver uses a single out urb for commands and tx data. A command is completed by a response on the in endpoint, but that response does not guarantee that the out urb has been given back by the host controller. The in endpoint handler also submits tx chunks directly. So when the device delays an out transfer, for example by NAKing it, the driver can submit the out urb while it is still active. usb_submit_urb() rejects this with a warning and -EBUSY; the transmission then fails and the driver tries to reset an already confused device. Track whether the out urb is in flight and serialize it with the state machine, since a completion can run in interrupt context. A tx chunk requested while the urb is busy is submitted from the out completion instead, and a command waits for the urb to be idle before reusing it. Fixes: 261463dbc34f ("media: rc: add support for Infrared Toy and IR Droid devices") Cc: stable@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- This was found with syzkaller against a virtual USB IR Toy device. The warning was observed on 7.3.5, and the code is unchanged in v7.4-rc5 and media-committers next. The change was compile-tested with CONFIG_IR_TOY=y only. Changes in v2: Do not use urb->hcpriv to decide whether the out urb is busy: it is a host controller private field, some controllers never set it and dwc2 clears it before the completion runs. Track the state in the driver and serialize the in handler, the out completion and the command path with a spinlock; only submit a deferred chunk after a successful completion, and abort the transfer on an out urb error. drivers/media/rc/ir_toy.c | 95 +++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 23 deletions(-) diff --git a/drivers/media/rc/ir_toy.c b/drivers/media/rc/ir_toy.c index 58b23d08a897..8b75dc206318 100644 --- a/drivers/media/rc/ir_toy.c +++ b/drivers/media/rc/ir_toy.c @@ -68,6 +68,9 @@ struct irtoy { void *tx_buf; uint tx_len; + uint tx_pending; /* chunk to send once urb_out is idle */ + spinlock_t lock; /* protects state, tx_* and urb_out submission */ + bool urb_out_busy; uint emitted; uint hw_version; @@ -77,6 +80,29 @@ struct irtoy { char phys[64]; }; +/* Caller must hold irtoy->lock. */ +static void irtoy_tx_submit(struct irtoy *irtoy, uint len) +{ + int err; + + memcpy(irtoy->out, irtoy->tx_buf, len); + irtoy->urb_out->transfer_buffer_length = len; + irtoy->urb_out_busy = true; + + err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC); + if (err) { + dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n", err); + irtoy->urb_out_busy = false; + irtoy->tx_pending = 0; + irtoy->state = STATE_IRDATA; + complete(&irtoy->command_done); + return; + } + + irtoy->tx_buf += len; + irtoy->tx_len -= len; +} + static void irtoy_response(struct irtoy *irtoy, u32 len) { switch (irtoy->state) { @@ -164,8 +190,8 @@ static void irtoy_response(struct irtoy *irtoy, u32 len) } else { // send next part of tx buffer uint space = irtoy->in[0]; + unsigned long flags; uint buf_len; - int err; if (len != 1 || space > MAX_PACKET || space == 0) { dev_dbg(irtoy->dev, "packet length expected: %*phN\n", @@ -175,22 +201,14 @@ static void irtoy_response(struct irtoy *irtoy, u32 len) buf_len = min(space, irtoy->tx_len); - dev_dbg(irtoy->dev, "remaining:%u sending:%u\n", - irtoy->tx_len, buf_len); - - memcpy(irtoy->out, irtoy->tx_buf, buf_len); - irtoy->urb_out->transfer_buffer_length = buf_len; - err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC); - if (err != 0) { - dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n", - err); - irtoy->state = STATE_IRDATA; - complete(&irtoy->command_done); - break; + spin_lock_irqsave(&irtoy->lock, flags); + if (irtoy->state == STATE_TX) { + if (irtoy->urb_out_busy) + irtoy->tx_pending = buf_len; + else + irtoy_tx_submit(irtoy, buf_len); } - - irtoy->tx_buf += buf_len; - irtoy->tx_len -= buf_len; + spin_unlock_irqrestore(&irtoy->lock, flags); } break; } @@ -199,13 +217,27 @@ static void irtoy_response(struct irtoy *irtoy, u32 len) static void irtoy_out_callback(struct urb *urb) { struct irtoy *irtoy = urb->context; + unsigned long flags; - if (urb->status == 0) { - if (irtoy->state == STATE_COMMAND_NO_RESP) - complete(&irtoy->command_done); - } else { + if (urb->status != 0) dev_warn(irtoy->dev, "out urb status: %d\n", urb->status); + + spin_lock_irqsave(&irtoy->lock, flags); + irtoy->urb_out_busy = false; + + if (urb->status != 0) { + irtoy->tx_pending = 0; + if (irtoy->state == STATE_TX) + irtoy->state = STATE_IRDATA; + } else if (irtoy->state == STATE_COMMAND_NO_RESP) { + complete(&irtoy->command_done); + } else if (irtoy->tx_pending && irtoy->state == STATE_TX) { + uint len = irtoy->tx_pending; + + irtoy->tx_pending = 0; + irtoy_tx_submit(irtoy, len); } + spin_unlock_irqrestore(&irtoy->lock, flags); } static void irtoy_in_callback(struct urb *urb) @@ -236,22 +268,37 @@ static void irtoy_in_callback(struct urb *urb) static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len, enum state state) { + unsigned long flags; int err; + /* the in endpoint can complete the command before urb_out is idle */ + usb_kill_urb(irtoy->urb_out); + init_completion(&irtoy->command_done); + spin_lock_irqsave(&irtoy->lock, flags); + irtoy->tx_pending = 0; irtoy->state = state; - memcpy(irtoy->out, cmd, cmd_len); irtoy->urb_out->transfer_buffer_length = cmd_len; + irtoy->urb_out_busy = true; + err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC); + if (err) { + irtoy->urb_out_busy = false; + irtoy->state = STATE_IRDATA; + } + spin_unlock_irqrestore(&irtoy->lock, flags); - err = usb_submit_urb(irtoy->urb_out, GFP_KERNEL); - if (err != 0) + if (err) return err; if (!wait_for_completion_timeout(&irtoy->command_done, msecs_to_jiffies(TIMEOUT))) { usb_kill_urb(irtoy->urb_out); + spin_lock_irqsave(&irtoy->lock, flags); + irtoy->tx_pending = 0; + irtoy->state = STATE_IRDATA; + spin_unlock_irqrestore(&irtoy->lock, flags); return -ETIMEDOUT; } @@ -409,6 +456,8 @@ static int irtoy_probe(struct usb_interface *intf, if (!irtoy) return -ENOMEM; + spin_lock_init(&irtoy->lock); + err = -ENOMEM; irtoy->in = kmalloc(MAX_PACKET, GFP_KERNEL); if (!irtoy->in) -- 2.34.1