From patchwork Thu Jan 25 15:27:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Nyman X-Patchwork-Id: 13531102 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6586C63411 for ; Thu, 25 Jan 2024 15:26:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196372; cv=none; b=uGjuL8R1hw/7HC7nxPVxIYGXsIAp1kGObqsFp+qR+4ctPyYb319yc7mtOrK1MiwLr0rf2003pB7yrcvnOWtO5wGf5SiLf0P+xed3ur2ocNNF0zYC86wA31CGc1ZGxPuGcMJ/QveiPeA49Q4eS2LaqisO9sYCT5KdNfPHqD2xRDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196372; c=relaxed/simple; bh=7zpiG3WM9+M2C982iPJzrxRyKJXcVSLtl4XnxF98gVU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q+3Defjd1YnNLEYfjdUn4/SYgWcNTjKV71FzstV5l5uSlaB/deyW8tgX6gFIrD16QzenyNGFtXS3Eg7e8RdHFHNN6OhIVwVAVqaDH1Qf5p2WYm8RF0jZRA0A6iQMrSRgiJuVrSz5L/RFyFiUQ5VSLY5lOT0AotPlxxncZOcfe3s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=PjK3HFNU; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="PjK3HFNU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706196371; x=1737732371; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7zpiG3WM9+M2C982iPJzrxRyKJXcVSLtl4XnxF98gVU=; b=PjK3HFNUl4GyNcKMGPm8gOlxH/Oa2KyOrZNcpNh3MFNgbXZizrRNFtWx Oh4fiPadVh5hnSpgLMx+uSclHC5H9it3KTJC+/N8CV20zGWBfkGAQDVOT goiY7acG9I00AUV7MOPEYSIn1MJAUr4MqSe3Ig1zqSOG0+z23XHpdwdpq WpHUQRqlhVKIxSvKK7ek0VHZA+Y4hwo5ZYGff4W7PTbLwQ7qfi0OJIwJH PqyEC966Drl7zQlzoCeLwQOBR91Slr5//hOE66etQU5xhY9hqhI3YXHKe UNp1p3FfnjyUWctjEqrEZgnXrXUlbBp+IxR8My+eDo18o9NjEwzfqGkrD Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="23651340" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="23651340" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 07:26:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="857099925" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="857099925" Received: from mattu-haswell.fi.intel.com ([10.237.72.199]) by fmsmga004.fm.intel.com with ESMTP; 25 Jan 2024 07:26:09 -0800 From: Mathias Nyman To: Cc: linux-usb@vger.kernel.org, Mathias Nyman , Dan Carpenter Subject: [PATCH 1/4] xhci: fix possible null pointer dereference at secondary interrupter removal Date: Thu, 25 Jan 2024 17:27:34 +0200 Message-Id: <20240125152737.2983959-2-mathias.nyman@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> References: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Don't try to remove a secondary interrupter that is known to be invalid. Also check if the interrupter is valid inside the spinlock that protects the array of interrupters. Found by smatch static checker Reported-by: Dan Carpenter Closes: https://lore.kernel.org/linux-usb/ffaa0a1b-5984-4a1f-bfd3-9184630a97b9@moroto.mountain/ Fixes: c99b38c41234 ("xhci: add support to allocate several interrupters") Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-mem.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c index 4460fa7e9fab..d00d4d937236 100644 --- a/drivers/usb/host/xhci-mem.c +++ b/drivers/usb/host/xhci-mem.c @@ -1861,14 +1861,14 @@ void xhci_remove_secondary_interrupter(struct usb_hcd *hcd, struct xhci_interrup struct xhci_hcd *xhci = hcd_to_xhci(hcd); unsigned int intr_num; + spin_lock_irq(&xhci->lock); + /* interrupter 0 is primary interrupter, don't touch it */ - if (!ir || !ir->intr_num || ir->intr_num >= xhci->max_interrupters) + if (!ir || !ir->intr_num || ir->intr_num >= xhci->max_interrupters) { xhci_dbg(xhci, "Invalid secondary interrupter, can't remove\n"); - - /* fixme, should we check xhci->interrupter[intr_num] == ir */ - /* fixme locking */ - - spin_lock_irq(&xhci->lock); + spin_unlock_irq(&xhci->lock); + return; + } intr_num = ir->intr_num; From patchwork Thu Jan 25 15:27:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Nyman X-Patchwork-Id: 13531103 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71BEB6EB6D for ; Thu, 25 Jan 2024 15:26:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196374; cv=none; b=a9LG7KnhXyIImGe2x/6smTLTkI1oxA5ADIecOZpTL9aLxPiIbyjdzKJi+4B80ed0G99JGMAxTIdaRplvSX73gFiwrG5sPZj/uaZy27HeyYxss53lhx+WL5U4NqP27Y+P138si/aMaxnPB6CT55QgbzEdmQLPqWwOhc0y/kV60Wk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196374; c=relaxed/simple; bh=7+2tR3u66bv4GLjuFdnhuZS81nyqklScPyEApxlpV9Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZAy4Ku9aYsoGw7QwXCPCaLM5PuA9kqiyHLXiY1hEP9wjt5LMOTsD7EDbqWh+2Hl1F9/CEgL0t8u/uYdHWkspTJ/3KLu6lNPl1B26YBF8A03+bQLT2TK8Nki7xLe0I4VvYpUtdf9+Unjsg3cw87UBuAMdpo99f8/ABWJBybfNrKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=NEl0ptql; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NEl0ptql" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706196372; x=1737732372; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7+2tR3u66bv4GLjuFdnhuZS81nyqklScPyEApxlpV9Y=; b=NEl0ptqlv3LVMadVLcp8eKM7XQCgehh/S8IJKA4pfOyBoYauP5wHe5qv L/q+yU6v9CvUVnHqQmrEHJGN1WIM+6M8j2m8XWlHdaQkodyKIrQznxIy4 Xf0NjdWF5B3HdUzjzR2xwPsVYtI+wnrXa13oEyzGg3Urf0BFrAXjTsxM4 dlBUoWOy8yAtJgFEAcYUI0QkI8P7qcZnYc14mmAz8tmPb3OkddOJPqvvn banKtMFgbo0DYKFfwyEDrzxz3Vn3KJQq95eNI2XmkunfmSFCtYk2PAOkk zGcoT7VqTjyFTUPygDlS3KVEhB4UxKGYg2qNxz/oSeSbcecrbhJBifltF Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="23651353" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="23651353" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 07:26:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="857099928" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="857099928" Received: from mattu-haswell.fi.intel.com ([10.237.72.199]) by fmsmga004.fm.intel.com with ESMTP; 25 Jan 2024 07:26:10 -0800 From: Mathias Nyman To: Cc: linux-usb@vger.kernel.org, Mathias Nyman , Dan Carpenter Subject: [PATCH 2/4] xhci: fix off by one check when adding a secondary interrupter. Date: Thu, 25 Jan 2024 17:27:35 +0200 Message-Id: <20240125152737.2983959-3-mathias.nyman@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> References: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The sanity check of interrupter index when adding a new interrupter is off by one. intr_num needs to be smaller than xhci->max_interrupter to fit the array of interrupters. Luckily this doesn't cause any real word harm as xhci_add_interrupter() is always called with a intr_num value smaller than xhci->max_interrupters in any current kernel. Should not be needed for stable as 6.7 kernel and older only supports one interrupter, with intr_num always being zero. Reported-by: Dan Carpenter Closes: https://lore.kernel.org/linux-usb/e9771296-586d-456a-ac24-a82de79bb2e6@moroto.mountain/ Fixes: 4bf398e15aa4 ("xhci: split allocate interrupter into separate alloacte and add parts") Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c index d00d4d937236..a7716202a8dd 100644 --- a/drivers/usb/host/xhci-mem.c +++ b/drivers/usb/host/xhci-mem.c @@ -2322,7 +2322,7 @@ xhci_add_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir, u64 erst_base; u32 erst_size; - if (intr_num > xhci->max_interrupters) { + if (intr_num >= xhci->max_interrupters) { xhci_warn(xhci, "Can't add interrupter %d, max interrupters %d\n", intr_num, xhci->max_interrupters); return -EINVAL; From patchwork Thu Jan 25 15:27:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mathias Nyman X-Patchwork-Id: 13531104 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D72B073171; Thu, 25 Jan 2024 15:26:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196377; cv=none; b=ofbhXsrq7GAFBaikyRZauH1V9/NonH6F3xeRZcOFDY9wkDBf847+KRdJBHBWCjX+ePJBCJGHPi20Sxz+EXrFJOvq9ZGV94t9BVdjc6KeWEoYQebWttO2FF9xnhjXOkfcWvHkvIRhE/+fuoh2Q1F5ZAkmHSWNI526Fn8kECy8pTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196377; c=relaxed/simple; bh=T6uBCKYEgzrQLc6KKBjDCx5r0prG5DgNIGrjEuNhl0o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=TGSAtiPt+atrx6dzkZOLOtVFW2cT+Ar5lXaX1IHDX6WfSkzRUKuMzsKyEfaHSNz5MrzlGkJxReiGPIpVcrAN4KFIkGycMF/nQIJ+66elVFdKs8hana2IEJrmsgC8t7QWLfHTWB2UZy4s825n38p77SS65LiT6m+/eaJ5FeXGmOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UQq4lMVl; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UQq4lMVl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706196375; x=1737732375; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=T6uBCKYEgzrQLc6KKBjDCx5r0prG5DgNIGrjEuNhl0o=; b=UQq4lMVl+piCi0layQJJELIW6MrqfHSeGsKDa8lg6T9YQbRdWYYj0QrE CJkAz11l2JnUlGWi3CrITnYABsWc1SchJ0vtgjVYinb8UAtG9jpnS1Wuc /l9eTPj+xiaYBI/8h7tUjkjddWe8/j1r3o2bFpTQP3XjlBuLY/zbMwt1H pJSRqu9sjkHZ2VzJ7wYshBLA5tdebcU1gjYYNjhWRDHQyvc+UiwGqLeea RY5C14xoAHm7HwpvEOpmPVOscU9I6/5I2+BS7mejVxRRAJMQI1pG7pgZb mjEKStfIRkRXdD7SufsuNMEjTrk+H1jSKkWYbZaLCjtYJU0TK/Fa7YRqS w==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="23651358" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="23651358" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 07:26:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="857099929" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="857099929" Received: from mattu-haswell.fi.intel.com ([10.237.72.199]) by fmsmga004.fm.intel.com with ESMTP; 25 Jan 2024 07:26:12 -0800 From: Mathias Nyman To: Cc: linux-usb@vger.kernel.org, Mathias Nyman , =?utf-8?q?Micha=C5=82_Pecio?= , stable@vger.kernel.org Subject: [PATCH 3/4] xhci: process isoc TD properly when there was a transaction error mid TD. Date: Thu, 25 Jan 2024 17:27:36 +0200 Message-Id: <20240125152737.2983959-4-mathias.nyman@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> References: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The last TRB of a isoc TD might not trigger an event if there was an error event for a TRB mid TD. This is seen on a NEC Corporation uPD720200 USB 3.0 Host After an error mid a multi-TRB TD the xHC should according to xhci 4.9.1 generate events for passed TRBs with IOC flag set if it proceeds to the next TD. This event is either a copy of the original error, or a "success" transfer event. If that event is missing then the driver and xHC host get out of sync as the driver is still expecting a transfer event for that first TD, while xHC host is already sending events for the next TD in the list. This leads to "Transfer event TRB DMA ptr not part of current TD" messages. As a solution we tag the isoc TDs that get error events mid TD. If an event doesn't match the first TD, then check if the tag is set, and event points to the next TD. In that case give back the fist TD and process the next TD normally Make sure TD status and transferred length stay valid in both cases with and without final TD completion event. Reported-by: Michał Pecio Closes: https://lore.kernel.org/linux-usb/20240112235205.1259f60c@foxbook/ Tested-by: Michał Pecio Cc: stable@vger.kernel.org Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-ring.c | 74 +++++++++++++++++++++++++++++------- drivers/usb/host/xhci.h | 1 + 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index 33806ae966f9..41be7d31a36e 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -2376,6 +2376,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, /* handle completion code */ switch (trb_comp_code) { case COMP_SUCCESS: + /* Don't overwrite status if TD had an error, see xHCI 4.9.1 */ + if (td->error_mid_td) + break; if (remaining) { frame->status = short_framestatus; if (xhci->quirks & XHCI_TRUST_TX_LENGTH) @@ -2401,8 +2404,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, break; case COMP_USB_TRANSACTION_ERROR: frame->status = -EPROTO; + sum_trbs_for_length = true; if (ep_trb != td->last_trb) - return 0; + td->error_mid_td = true; break; case COMP_STOPPED: sum_trbs_for_length = true; @@ -2422,6 +2426,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, break; } + if (td->urb_length_set) + goto finish_td; + if (sum_trbs_for_length) frame->actual_length = sum_trb_lengths(xhci, ep->ring, ep_trb) + ep_trb_len - remaining; @@ -2430,6 +2437,14 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, td->urb->actual_length += frame->actual_length; +finish_td: + /* Don't give back TD yet if we encountered an error mid TD */ + if (td->error_mid_td && ep_trb != td->last_trb) { + xhci_dbg(xhci, "Error mid isoc TD, wait for final completion event\n"); + td->urb_length_set = true; + return 0; + } + return finish_td(xhci, ep, ep_ring, td, trb_comp_code); } @@ -2808,17 +2823,51 @@ static int handle_tx_event(struct xhci_hcd *xhci, } if (!ep_seg) { - if (!ep->skip || - !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { - /* Some host controllers give a spurious - * successful event after a short transfer. - * Ignore it. - */ - if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && - ep_ring->last_td_was_short) { - ep_ring->last_td_was_short = false; - goto cleanup; + + if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { + skip_isoc_td(xhci, td, ep, status); + goto cleanup; + } + + /* + * Some hosts give a spurious success event after a short + * transfer. Ignore it. + */ + if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && + ep_ring->last_td_was_short) { + ep_ring->last_td_was_short = false; + goto cleanup; + } + + /* + * xhci 4.10.2 states isoc endpoints should continue + * processing the next TD if there was an error mid TD. + * So host like NEC don't generate an event for the last + * isoc TRB even if the IOC flag is set. + * xhci 4.9.1 states that if there are errors in mult-TRB + * TDs xHC should generate an error for that TRB, and if xHC + * proceeds to the next TD it should genete an event for + * any TRB with IOC flag on the way. Other host follow this. + * So this event might be for the next TD. + */ + if (td->error_mid_td && + !list_is_last(&td->td_list, &ep_ring->td_list)) { + struct xhci_td *td_next = list_next_entry(td, td_list); + + ep_seg = trb_in_td(xhci, td_next->start_seg, td_next->first_trb, + td_next->last_trb, ep_trb_dma, false); + if (ep_seg) { + /* give back previous TD, start handling new */ + xhci_dbg(xhci, "Missing TD completion event after mid TD error\n"); + ep_ring->dequeue = td->last_trb; + ep_ring->deq_seg = td->last_trb_seg; + inc_deq(xhci, ep_ring); + xhci_td_cleanup(xhci, td, ep_ring, td->status); + td = td_next; } + } + + if (!ep_seg) { /* HC is busted, give up! */ xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not " @@ -2830,9 +2879,6 @@ static int handle_tx_event(struct xhci_hcd *xhci, ep_trb_dma, true); return -ESHUTDOWN; } - - skip_isoc_td(xhci, td, ep, status); - goto cleanup; } if (trb_comp_code == COMP_SHORT_PACKET) ep_ring->last_td_was_short = true; diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index a5c72a634e6a..6f82d404883f 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -1549,6 +1549,7 @@ struct xhci_td { struct xhci_segment *bounce_seg; /* actual_length of the URB has already been set */ bool urb_length_set; + bool error_mid_td; unsigned int num_trbs; }; From patchwork Thu Jan 25 15:27:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Nyman X-Patchwork-Id: 13531105 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64FD26EB7B; Thu, 25 Jan 2024 15:26:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196377; cv=none; b=rVMFY6H8/oHL3HdCa7hOgqVEgiPJp7SItWokgu/HR82mipab3Qq59cj7qhtIt+SEHYMgS4o43wksDtqkAnCLhU5gJ45Q6VcfcAtW/AdnXEM8F09tyw3WcEWGES40wrVOvtRm2TWmu/GFA4W/yk/JS5ZoXq59NQSdH8Tpcn6kjOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196377; c=relaxed/simple; bh=tJN0S6rY1/CKU1dDI8IfHzyyHIeGZm28jyMc1dSzveM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZxIpghVA+esk/Wq0ChoUWYi4HKjgeH9vjEDvrRdCCIFEPoIkMHQQ+SwqMVl8oVbDbx+JitD0BSzPY/QQldpBe0TW/Wm/gyzb6q2RuwhOP/nVmrM0qnuSM0GM1XJjr4422cYs4mjR5SL+b1Iy60KIbRXavpAUObgEuHfWb+pLPAo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=jxuWOugE; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jxuWOugE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706196376; x=1737732376; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tJN0S6rY1/CKU1dDI8IfHzyyHIeGZm28jyMc1dSzveM=; b=jxuWOugEr3u57w4WFJBifkE3iQnzJbIBfvm64/vI8GL53gdNny+V2Rgy KDGTWKIzBaoqQFEquakSCyEUzdFzsc8E996CQBSnMhdEChuaSC++FAf+y RP8Q3wgSZATuLAVkzETZ+SbjjYrZb3RgHQaB5l4wplf0Lay/MfBFs9JD1 fijkCwCZQN3ryoUj5Hrk6xYA+/1Q9gXlDUkwrkuWIwVCNE8aO7pIIRcw9 HW3bmzkK7DW4OCGn93+xUnrSsMI7TWPGDv1FA9BvL1cpsH/plZfq85Zyn O6Y4KPeGd+bsbsQYh8LNOh3FzLyXg2o4itCPUa2cuyLc2Y1hQiuEuoXaS A==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="23651369" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="23651369" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 07:26:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="857099933" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="857099933" Received: from mattu-haswell.fi.intel.com ([10.237.72.199]) by fmsmga004.fm.intel.com with ESMTP; 25 Jan 2024 07:26:14 -0800 From: Mathias Nyman To: Cc: linux-usb@vger.kernel.org, Michal Pecio , stable@vger.kernel.org, Mathias Nyman Subject: [PATCH 4/4] xhci: handle isoc Babble and Buffer Overrun events properly Date: Thu, 25 Jan 2024 17:27:37 +0200 Message-Id: <20240125152737.2983959-5-mathias.nyman@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> References: <20240125152737.2983959-1-mathias.nyman@linux.intel.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Michal Pecio xHCI 4.9 explicitly forbids assuming that the xHC has released its ownership of a multi-TRB TD when it reports an error on one of the early TRBs. Yet the driver makes such assumption and releases the TD, allowing the remaining TRBs to be freed or overwritten by new TDs. The xHC should also report completion of the final TRB due to its IOC flag being set by us, regardless of prior errors. This event cannot be recognized if the TD has already been freed earlier, resulting in "Transfer event TRB DMA ptr not part of current TD" error message. Fix this by reusing the logic for processing isoc Transaction Errors. This also handles hosts which fail to report the final completion. Fix transfer length reporting on Babble errors. They may be caused by device malfunction, no guarantee that the buffer has been filled. Signed-off-by: Michal Pecio Cc: stable@vger.kernel.org Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-ring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index 41be7d31a36e..f0d8a607ff21 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -2394,9 +2394,13 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, case COMP_BANDWIDTH_OVERRUN_ERROR: frame->status = -ECOMM; break; - case COMP_ISOCH_BUFFER_OVERRUN: case COMP_BABBLE_DETECTED_ERROR: + sum_trbs_for_length = true; + fallthrough; + case COMP_ISOCH_BUFFER_OVERRUN: frame->status = -EOVERFLOW; + if (ep_trb != td->last_trb) + td->error_mid_td = true; break; case COMP_INCOMPATIBLE_DEVICE_ERROR: case COMP_STALL_ERROR: