1 From stable+bounces-198030-greg=kroah.com@vger.kernel.org Tue Dec 2 02:48:56 2025
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Mon, 1 Dec 2025 20:48:40 -0500
4 Subject: usb: uas: fix urb unmapping issue when the uas device is remove during ongoing data transfer
5 To: stable@vger.kernel.org
6 Cc: Owen Gu <guhuinan@xiaomi.com>, stable <stable@kernel.org>, Yu Chen <chenyu45@xiaomi.com>, Oliver Neukum <oneukum@suse.com>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Sasha Levin <sashal@kernel.org>
7 Message-ID: <20251202014840.1603338-1-sashal@kernel.org>
9 From: Owen Gu <guhuinan@xiaomi.com>
11 [ Upstream commit 26d56a9fcb2014b99e654127960aa0a48a391e3c ]
13 When a UAS device is unplugged during data transfer, there is
14 a probability of a system panic occurring. The root cause is
15 an access to an invalid memory address during URB callback handling.
16 Specifically, this happens when the dma_direct_unmap_sg() function
17 is called within the usb_hcd_unmap_urb_for_dma() interface, but the
18 sg->dma_address field is 0 and the sg data structure has already been
21 The SCSI driver sends transfer commands by invoking uas_queuecommand_lck()
22 in uas.c, using the uas_submit_urbs() function to submit requests to USB.
23 Within the uas_submit_urbs() implementation, three URBs (sense_urb,
24 data_urb, and cmd_urb) are sequentially submitted. Device removal may
25 occur at any point during uas_submit_urbs execution, which may result
26 in URB submission failure. However, some URBs might have been successfully
27 submitted before the failure, and uas_submit_urbs will return the -ENODEV
28 error code in this case. The current error handling directly calls
29 scsi_done(). In the SCSI driver, this eventually triggers scsi_complete()
30 to invoke scsi_end_request() for releasing the sgtable. The successfully
31 submitted URBs, when being unlinked to giveback, call
32 usb_hcd_unmap_urb_for_dma() in hcd.c, leading to exceptions during sg
33 unmapping operations since the sg data structure has already been freed.
35 This patch modifies the error condition check in the uas_submit_urbs()
36 function. When a UAS device is removed but one or more URBs have already
37 been successfully submitted to USB, it avoids immediately invoking
38 scsi_done() and save the cmnd to devinfo->cmnd array. If the successfully
39 submitted URBs is completed before devinfo->resetting being set, then
40 the scsi_done() function will be called within uas_try_complete() after
41 all pending URB operations are finalized. Otherwise, the scsi_done()
42 function will be called within uas_zap_pending(), which is executed after
43 usb_kill_anchored_urbs().
45 The error handling only takes effect when uas_queuecommand_lck() calls
46 uas_submit_urbs() and returns the error value -ENODEV . In this case,
47 the device is disconnected, and the flow proceeds to uas_disconnect(),
48 where uas_zap_pending() is invoked to call uas_try_complete().
50 Fixes: eb2a86ae8c54 ("USB: UAS: fix disconnect by unplugging a hub")
51 Cc: stable <stable@kernel.org>
52 Signed-off-by: Yu Chen <chenyu45@xiaomi.com>
53 Signed-off-by: Owen Gu <guhuinan@xiaomi.com>
54 Acked-by: Oliver Neukum <oneukum@suse.com>
55 Link: https://patch.msgid.link/20251120123336.3328-1-guhuinan@xiaomi.com
56 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
57 [ adapted scsi_done(cmnd) helper to older cmnd->scsi_done(cmnd) callback API ]
58 Signed-off-by: Sasha Levin <sashal@kernel.org>
59 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
61 drivers/usb/storage/uas.c | 7 ++++++-
62 1 file changed, 6 insertions(+), 1 deletion(-)
64 --- a/drivers/usb/storage/uas.c
65 +++ b/drivers/usb/storage/uas.c
66 @@ -705,7 +705,11 @@ static int uas_queuecommand_lck(struct s
67 * of queueing, no matter how fatal the error
70 - set_host_byte(cmnd, DID_ERROR);
71 + if (cmdinfo->state & (COMMAND_INFLIGHT | DATA_IN_URB_INFLIGHT |
72 + DATA_OUT_URB_INFLIGHT))
75 + set_host_byte(cmnd, DID_NO_CONNECT);
76 cmnd->scsi_done(cmnd);
79 @@ -718,6 +722,7 @@ static int uas_queuecommand_lck(struct s
80 uas_add_work(cmdinfo);
84 devinfo->cmnd[idx] = cmnd;
86 spin_unlock_irqrestore(&devinfo->lock, flags);