From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18BD43A4F5E for ; Mon, 24 Aug 2026 07:59:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787558398; cv=none; b=abOVXYi1rOGa8WXh6mD35fl2fS7aHzkOcjdHodfhZNXQhjkS2CF9szgkFD3Cv5cAqkPyZKtmpkWfO7csCP+6mv00uUh757SJL87Y8lmDa48mgmQtYAiu2y6dnHdEtqQH5YfNSKShm0ePCG5ViyIl53HVOG+QuuYfRF1tybnRIC4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787558398; c=relaxed/simple; bh=+aUQRaCJEiOlOXPCqhnqzr2GzBTvKrS5uoneUccI4pc=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=HKumVxqH1aVPR71ag1ZxCmE6gKsQhgDy5B/jDS2MqmvLOrq9qD39perKt6qxloiQF0saeAkhIr4lW4eDYgcZO7gbO1Tm5D7YQexbKYG5I+7aEN+H/pxnio9VU6WjV+dycH4RYkcbkI/DgUAfWDZt8KwWjhgeNn1dAAGpGacIPBQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AulkzfBx; arc=none smtp.client-ip=209.85.218.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AulkzfBx" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-c1c26d7e951so462707966b.0 for ; Mon, 24 Aug 2026 00:59:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787558389; x=1788163189; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=WttKNsbulNL5/elFBjAMNMa3KXVDrXB8+BjVRa3s9Y4=; b=AulkzfBx7W3CRKwgjgfhSTAqtZOVxbWsgTisYfefgHo12nPOuGQfk0e3xOcgDYRlVM HyBgGGR8SmRd5npnByxerOiACw25aGR06upTFR36TYXE0FArId8+Ld54zWeqeWTbSjkV 3snNezq6+pj9qQNjl/n6UgIIIUFacPVUH1Uge6gz+z/KNyOSiE68bYccfzwlhr3QVtMq nLMMPmfeXiZhdRYAKIxhVtzQJLlePE28bMRBC4OcV2uCaZ5RmNJii2HTLSQxrHWttkqZ U/ipenfVxtqZtCNvqQJUJp5qur1jxAMqCDJdeogfVWtwl97F0Dh3tl13Izhad18bD4iQ iZZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787558389; x=1788163189; h=content-transfer-encoding:content-type:mime-version:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=WttKNsbulNL5/elFBjAMNMa3KXVDrXB8+BjVRa3s9Y4=; b=Jxjon7R+eak9DcKutZRLwd7iLbziJdcdzZfRbfTGUTSMXaTzwgIIkEox8HvWF7LCNz BQrt9qfS9QK/SzXo1f3/9YDXqLunwcSeioMQxO4+4B16Li12HvP/EsHPGkDzZmjyxMjz glQDOLpBn258au264Be9vFhK6QkXH2FfY9z5McqeeKvTp+LCu2qms3NhQk7qr4+/hAWg qX2TViDZ2nbHT+Tta9NB8has7zOy7cLbh/ayVEBTRKoI+iq+LA7GXAsXUAkyTcAUgvgH 0muBeTOgzwqzwqgXlLN4BuaQkAlHjSQyutdbsh8JNdx3K0Ez6jkuaRa+ika2WAJ4z91x yd+Q== X-Forwarded-Encrypted: i=1; AHgh+RqOz4lGN/bFW2MOG+1jmaO0iJ68r6w9M5vORqrQX3HeM3lJSVSu6cv5lpZ+CQaFKDqFyW44NIlCfOyQrR8=@vger.kernel.org X-Gm-Message-State: AFuF++k9SVLPe/DIGiIFSobS/CSQhjpqKYfdw92EdOsLBRi6v4iVZF22 vAbl3/ixqPr0KSwUSxWrAlZN4cD1tKmt98iP32vha4nc6zcpXnUbl9NP X-Gm-Gg: AR+sD12zQfqIzj0ES6YXa5Eo2ed9vUQoJqoRvAmPMZRJc9pAVpYqlLNxa5hVqUssENb R6fuZ/0SG9dvZ/4lMBQY0FKarVZWkL7Y1duh3eHEZD7HGbBnhEsimBwmu54tDU9QNz6khOm/PaJ Hs5MyOUrz5B/hMf2y9QjUgToms/wVMX4Fz3eT7Y6O1WxuS/SNUp+I54yAUC3LjeVDsHm9JhdjGY 1rXpo2HbtPd4pbCdqDJZ5rOV5K/91t5FmxUdwuiieGsQkb5ZUopYPewUxCmLBb3dtixHBjLOSvq Fwd1ogqx80XzM20Evn0pm+0fZTq2naIrSO7ge2i93anrrTcboEAEo3BtLCw9QGX1lQoNgX1ii7Q K0czDbWpLLKfgr/gazEOnxpW7YyxOt/qLYeugMyDC+gyDeQkAz+U7p80hh5U1HnvUSSMI2pMZiV M+vx9W7uXCuN/imTQCz4HajQj8HwSxhcv2wzyWM/+p9j9OHeWQpQcFE22I+720YefbwLg= X-Received: by 2002:a17:907:8b8a:b0:c05:b9db:68cc with SMTP id a640c23a62f3a-c2469f7b668mr2755707566b.0.1787558388709; Mon, 24 Aug 2026 00:59:48 -0700 (PDT) Received: from foxbook (bfk5.neoplus.adsl.tpnet.pl. [83.28.48.5]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c249629a901sm1262228266b.25.2026.08.24.00.59.47 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Mon, 24 Aug 2026 00:59:48 -0700 (PDT) Date: Mon, 24 Aug 2026 09:59:44 +0200 From: Michal Pecio To: Mathias Nyman , Greg Kroah-Hartman , Pedro Fonseca Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] usb: xhci: Unlock for command abort polling Message-ID: <20260824095944.1c8335fa.michal.pecio@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit xhci_abort_cmd_ring() requests abort, waits for the CRR bit to clear, drops xhci->lock and waits for the Command Ring Stopped event. The CRR wait timeout is 5 seconds as suggested by xHCI 4.6.1.2, which means that if the xHC fails to complete the operation at all, we poll with the lock held and IRQs disabled for several seconds. If any other CPU tries to acquire the lock, it will spin likewise. IRQs get delays, drivers log errors, tasks freeze, it's a mess. So drop the lock earlier, before waiting for the CRR bit. It should be safe - the sole caller sets cmd_ring_state to CMD_RING_STATE_ABORTED before calling us, which will prevent others from ringing the command doorbell and interfering with the abort. Queuing new commands during this time poses no danger, and if the command we try to abort actually completes concurrently, existing code already needs to deal with this. And in my testing it does - it's trivial to trigger this on ASM1042, where Address Device can't be aborted, but it completes as soon as the offending device is unplugged, including during abort attempt. Note that the lock still covers reinit_completion(), so it won't race with complete() being called by the event handler. And works are not reentrant, so another timeout can't expire while the lock is dropped. We will configure timeout anew when restarting the ring. One other difference is that now we also drop the lock if abort fails. This too should be harmless. Commands queued during this time will be released like any other pending commands. If the aborted command does complete before we regain the lock, it's a waste, but not regression. Reported-by: Pedro Fonseca Link: https://lore.kernel.org/linux-usb/16f65081-5a3c-4c30-9811-9017796a3373@fonseca.com.pt/ Signed-off-by: Michal Pecio --- This has been annoying me and various others for years, only the latest incident is listed above. I expected a nightmare of race conditions, but after finally taking a serious look I think it really is quite simple. It helps that the lock was already being dropped and existing code seems to handle it fine. drivers/usb/host/xhci-ring.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index 8b0c27d6f12d..44a0f89110a1 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -494,7 +494,7 @@ static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags) struct xhci_segment *new_seg = xhci->cmd_ring->deq_seg; union xhci_trb *new_deq = xhci->cmd_ring->dequeue; u64 crcr; - int ret; + int ret, completed; xhci_dbg(xhci, "Abort command ring\n"); @@ -521,25 +521,27 @@ static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags) * In the future we should distinguish between -ENODEV and -ETIMEDOUT * and try to recover a -ETIMEDOUT with a host controller reset. */ + spin_unlock_irqrestore(&xhci->lock, flags); ret = xhci_handshake(&xhci->op_regs->cmd_ring, CMD_RING_RUNNING, 0, 5 * 1000 * 1000); - if (ret < 0) { - xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret); - xhci_halt(xhci); - xhci_hc_died(xhci); - return ret; - } /* * Writing the CMD_RING_ABORT bit should cause a cmd completion event, * however on some host hw the CMD_RING_RUNNING bit is correctly cleared * but the completion event in never sent. Wait 2 secs (arbitrary * number) to handle those cases after negation of CMD_RING_RUNNING. */ - spin_unlock_irqrestore(&xhci->lock, flags); - ret = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion, + if (ret >= 0) + completed = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion, msecs_to_jiffies(2000)); spin_lock_irqsave(&xhci->lock, flags); - if (!ret) { + + if (ret < 0) { + xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret); + xhci_halt(xhci); + xhci_hc_died(xhci); + return ret; + } + if (!completed) { xhci_dbg(xhci, "No stop event for abort, ring start fail?\n"); xhci_cleanup_command_queue(xhci); } else { -- 2.48.1