Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1541953pxb; Tue, 8 Feb 2022 21:41:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJyR7SDBD1mE/Ut3vbG9eY5/+L5VTZSuOMr1AJjUNS9bqVpV9/+LFYZjfrRGslBU3EiOTRPj X-Received: by 2002:a17:902:ce8b:: with SMTP id f11mr573057plg.40.1644385262800; Tue, 08 Feb 2022 21:41:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644385262; cv=none; d=google.com; s=arc-20160816; b=a8UGR7J73hdhM4KP/Pt+LEOxA5RE5oU4TvRS7Qatmc6gc/ox5cQ4AO0+ERQIGJSP/R G7Z6bVT/sPWU6dPGOcGcRoiXlmRkgUe9FqqHaoYSFjkcwZe7lHolpUdRvZjRYtU7g882 oQOuOgPJa4MhydEosKEaxcDZa4QRYHwp7HZ4VNAGRJ2vM17PZGVwn10P+E2SvvIxc8Ci hLWpwfnwTk6JUsvGCW/NaSjeQ9zAnROf/2FE3pdRslfB5dzosoJmZfbjgiXnnrpgHNW8 n3GHNXBlJtwWKIu41csSO/0WGHYBkMOTbD7HAq9qvHr0IoedODQQ4D2hI4nlb91ns5mv n03w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from :dkim-signature; bh=fderG4Gz8z+WaznEGhLQ9NMtlL+AgXDhS7nYzmK/IC8=; b=0qcATrJGMNZ6gvquuQwJg42WDKJMb4Hzte+gwrm0O83uNljvsoVUWUEcUsChj+/tYD xhp5+x1aNg18jrrY0RRjLuqelMrxiI7PUCsYTG9l/YZ+O67VDPM87NCOQgU7/sfpkjXu aKujoZuSMWVirt8lArKnti+KgRfNqlUaLQK+022NiYJjsNcZiwnsA5imoMmx17giakaa RY72XSSnewdnHCm9yA7ZRHuzwvUQDmllXqguQsSdBTjPEAmUzzeEetH7hovXVP/dnhiz 8o+0wy7X0OmMSSsI+4GVBKj+XGgzdijI5ocp8RAjBZI/qeJ/5vjXcnvUZT9oH+nceG+4 /jnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b=GI1IwDBc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k8si17911270pgp.91.2022.02.08.21.41.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 21:41:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b=GI1IwDBc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4A7E1C008641; Tue, 8 Feb 2022 21:38:02 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350524AbiBGFwo (ORCPT + 99 others); Mon, 7 Feb 2022 00:52:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239559AbiBGE2S (ORCPT ); Sun, 6 Feb 2022 23:28:18 -0500 X-Greylist: delayed 122 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sun, 06 Feb 2022 20:28:17 PST Received: from alexa-out-sd-02.qualcomm.com (alexa-out-sd-02.qualcomm.com [199.106.114.39]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 489A9C043181; Sun, 6 Feb 2022 20:28:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; i=@quicinc.com; q=dns/txt; s=qcdkim; t=1644208097; x=1675744097; h=from:to:cc:subject:date:message-id:mime-version; bh=fderG4Gz8z+WaznEGhLQ9NMtlL+AgXDhS7nYzmK/IC8=; b=GI1IwDBcDB/T/wFTAbD3moQWyNUK4MgJ4LeQ2coHlliDaQW1vOZ+Idwd cNkHNy6RaKe+Sft1eyw2stKts4Q3YIFKK14jP9Td/xFYEA++dgKPyRRIg GeZvk8jKxAKYM/WnUOd4enFkKZ519/hCRvQKL/9JlnQhQiFnHwVY69ZP0 w=; Received: from unknown (HELO ironmsg04-sd.qualcomm.com) ([10.53.140.144]) by alexa-out-sd-02.qualcomm.com with ESMTP; 06 Feb 2022 20:26:14 -0800 X-QCInternal: smtphost Received: from nasanex01c.na.qualcomm.com ([10.47.97.222]) by ironmsg04-sd.qualcomm.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2022 20:26:13 -0800 Received: from nalasex01a.na.qualcomm.com (10.47.209.196) by nasanex01c.na.qualcomm.com (10.47.97.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.922.19; Sun, 6 Feb 2022 20:26:13 -0800 Received: from ugoswami-linux.qualcomm.com (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.922.19; Sun, 6 Feb 2022 20:26:09 -0800 From: Udipto Goswami To: Felipe Balbi , Greg Kroah-Hartman , , CC: Pratham Pratap , Pavankumar Kondeti , Jack Pham , Wesley Cheng , Udipto Goswami Subject: [PATCH] usb: dwc3: gadget: Prevent core from processing stale TRBs Date: Mon, 7 Feb 2022 09:55:58 +0530 Message-ID: <1644207958-18287-1-git-send-email-quic_ugoswami@quicinc.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01b.na.qualcomm.com (10.46.141.250) To nalasex01a.na.qualcomm.com (10.47.209.196) X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With CPU re-ordering on write instructions, there might be a chance that the HWO is set before the TRB is updated with the new mapped buffer address. And in the case where core is processing a list of TRBs it is possible that it fetched the TRBs when the HWO is set but before the buffer address is updated. Prevent this by adding a memory barrier before the HWO is updated to ensure that the core always process the updated TRBs. Fixes: f6bafc6a1c9 ("usb: dwc3: convert TRBs into bitshifts") Signed-off-by: Udipto Goswami --- v1: For an ep the trbs can be reused, and if cpu re-ordering also takes place, there is a change that the HWO will get set even before the trb bpl/bph are updated which will lead controller to access a stale buffer address from the previous transactions. drivers/usb/dwc3/gadget.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 520031b..183b909 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1291,6 +1291,19 @@ static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); + /* + * As per data book 4.2.3.2TRB Control Bit Rules section + * + * The controller autonomously checks the HWO field of a TRB to determine if the + * entire TRB is valid. Therefore, software must ensure that the rest of the TRB + * is valid before setting the HWO field to '1'. In most systems, this means that + * software must update the fourth DWORD of a TRB last. + * + * However there is a possibility of CPU re-ordering here which can cause + * controller to observe the HWO bit set prematurely. + * Add a write memory barrier to prevent CPU re-ordering. + */ + wmb(); trb->ctrl |= DWC3_TRB_CTRL_HWO; dwc3_ep_inc_enq(dep); -- 2.7.4