Skip to content

Commit dd83dc1

Browse files
matnymangregkh
authored andcommitted
xhci: sideband: don't dereference freed ring when removing sideband endpoint
xhci_sideband_remove_endpoint() incorrecly assumes that the endpoint is running and has a valid transfer ring. Lianqin reported a crash during suspend/wake-up stress testing, and found the cause to be dereferencing a non-existing transfer ring 'ep->ring' during xhci_sideband_remove_endpoint(). The endpoint and its ring may be in unknown state if this function is called after xHCI was reinitialized in resume (lost power), or if device is being re-enumerated, disconnected or endpoint already dropped. Fix this by both removing unnecessary ring access, and by checking ep->ring exists before dereferencing it. Also make sure endpoint is running before attempting to stop it. Remove the xhci_initialize_ring_info() call during sideband endpoint removal as is it only initializes ring structure enqueue, dequeue and cycle state values to their starting values without changing actual hardware enqueue, dequeue and cycle state. Leaving them out of sync is worse than leaving it as it is. The endpoint will get freed in after this in most usecases. If the (audio) class driver want's to reuse the endpoint after offload then it is up to the class driver to ensure endpoint is properly set up. Reported-by: 胡连勤 <hulianqin@vivo.com> Closes: https://lore.kernel.org/linux-usb/TYUPR06MB6217B105B059A7730C4F6EC8D2B9A@TYUPR06MB6217.apcprd06.prod.outlook.com/ Tested-by: 胡连勤 <hulianqin@vivo.com> Fixes: de66754 ("xhci: sideband: add initial api to register a secondary interrupter entity") Cc: stable@vger.kernel.org Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com> Link: https://patch.msgid.link/20260115233758.364097-2-mathias.nyman@linux.intel.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9fa015b commit dd83dc1

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

drivers/usb/host/xhci-sideband.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
210210
return -ENODEV;
211211

212212
__xhci_sideband_remove_endpoint(sb, ep);
213-
xhci_initialize_ring_info(ep->ring);
214213

215214
return 0;
216215
}

drivers/usb/host/xhci.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,16 +2898,25 @@ int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, int
28982898
gfp_t gfp_flags)
28992899
{
29002900
struct xhci_command *command;
2901+
struct xhci_ep_ctx *ep_ctx;
29012902
unsigned long flags;
2902-
int ret;
2903+
int ret = -ENODEV;
29032904

29042905
command = xhci_alloc_command(xhci, true, gfp_flags);
29052906
if (!command)
29062907
return -ENOMEM;
29072908

29082909
spin_lock_irqsave(&xhci->lock, flags);
2909-
ret = xhci_queue_stop_endpoint(xhci, command, ep->vdev->slot_id,
2910-
ep->ep_index, suspend);
2910+
2911+
/* make sure endpoint exists and is running before stopping it */
2912+
if (ep->ring) {
2913+
ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index);
2914+
if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING)
2915+
ret = xhci_queue_stop_endpoint(xhci, command,
2916+
ep->vdev->slot_id,
2917+
ep->ep_index, suspend);
2918+
}
2919+
29112920
if (ret < 0) {
29122921
spin_unlock_irqrestore(&xhci->lock, flags);
29132922
goto out;

0 commit comments

Comments
 (0)