Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1515521pxb; Fri, 27 Aug 2021 10:32:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5dOxGRQWfmPXhiihimoHu+Qa4seEHGo/pAZJLyYv6XdDoMrOiIQfzlEiRF+Jryj1tKkEI X-Received: by 2002:a17:906:c054:: with SMTP id bm20mr11038705ejb.203.1630085542413; Fri, 27 Aug 2021 10:32:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630085542; cv=none; d=google.com; s=arc-20160816; b=oUpPRxDp78ULHYOaq45hOlVCefviJHWL1G/WxPlO7GuBMfJKpKij3uhjCLSgaQAPB7 9wXxZ8PH2QRvX5LS+b9bZ/ZAHIRqvCZhw0hk4/KCj8FqYT+79hWfoBffo3CAHxRsdo/r m0z/IdXAEEzbzY2sJOYl4jk3GNZUoXtn3U2Gk10LYb0cpoUP3SkDvv3Ly0ThEfcklEt3 jtU7vToi9AQvFiqKHHiRabjhgIpD7/fJKa/CBYLjSF1l9QThFyJNfmqW1LyjyHKfOi9m GUVencvi5M8jjXvlQBFLu0D18+QMfOSglUdtRtEM9aLULkQ/eJyuthFny7XjI9/XVWr8 V7hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:message-id:references:in-reply-to :mail-reply-to:reply-to:organization:subject:cc:to:from:date :content-transfer-encoding:mime-version:sender:dkim-signature; bh=f1K7tpYyGaEgTI2CpLNbGPsvPdr213WjVf/5LadNQ9c=; b=Uk9eMpeb2KFmgwfdJjymdeChHR+IUW04TIN1ZZ79OI6+C0+jL0HFUymuy5DhLZMdZW +CPuujw83A6iv7mfCJEwUvoKXlUuYJVBehpyZNFcfZbqOGJK92/HHiUlGhDlaLCof4Ts YW/5N7iN5vEIq53YetPci9GVvD1brzubTh2yKLylHgczPMpVkAW8aOCqZRKuVnv7sYvB cagAGgRMZUmj4a0BJdo385rH2g8rfWetI7Xbmht+6TAVE23u1+BV8DFAB7vslBoumnVa 6KuLlWZYZhkvGHboUCz3OGYcvq1MLpbNy+9z4AFdxvVd/JSPfS04tYlTsxZSuJSTr3Cg gg8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=hGUncBRt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a24si6651331edy.136.2021.08.27.10.31.47; Fri, 27 Aug 2021 10:32:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=hGUncBRt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234095AbhH0Rav (ORCPT + 99 others); Fri, 27 Aug 2021 13:30:51 -0400 Received: from m43-7.mailgun.net ([69.72.43.7]:26837 "EHLO m43-7.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230367AbhH0Rav (ORCPT ); Fri, 27 Aug 2021 13:30:51 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1630085402; h=Message-ID: References: In-Reply-To: Reply-To: Subject: Cc: To: From: Date: Content-Transfer-Encoding: Content-Type: MIME-Version: Sender; bh=f1K7tpYyGaEgTI2CpLNbGPsvPdr213WjVf/5LadNQ9c=; b=hGUncBRtlPiNgPcEfTyN2ZjbOukyBwYiYT8ExIkRwq8XvMyAUyq2rHQ8V3NIxQqLAsHiDLei Nw0B3V45d2CLEHI+t04KYfrioAE0xd2VYh7XlMEkLdnCqr8CRFwumYEHC6l6Gn+d+Tf1/439 CZFM42B0UmlWoHRD9WGr86K3bdg= X-Mailgun-Sending-Ip: 69.72.43.7 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n06.prod.us-east-1.postgun.com with SMTP id 61292119825e13c54a2e97f2 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Fri, 27 Aug 2021 17:30:01 GMT Sender: bbhatt=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 888F1C4360D; Fri, 27 Aug 2021 17:30:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.codeaurora.org (localhost.localdomain [127.0.0.1]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: bbhatt) by smtp.codeaurora.org (Postfix) with ESMTPSA id 9AEF6C4338F; Fri, 27 Aug 2021 17:29:59 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 27 Aug 2021 10:29:59 -0700 From: Bhaumik Bhatt To: Jeffrey Hugo Cc: mani@kernel.org, hemantk@codeaurora.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] bus: mhi: core: Use cached values for calculating the shared write pointer Organization: Qualcomm Innovation Center, Inc. Reply-To: bbhatt@codeaurora.org Mail-Reply-To: bbhatt@codeaurora.org In-Reply-To: <1630073606-13671-1-git-send-email-quic_jhugo@quicinc.com> References: <1630073606-13671-1-git-send-email-quic_jhugo@quicinc.com> Message-ID: X-Sender: bbhatt@codeaurora.org User-Agent: Roundcube Webmail/1.3.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-08-27 07:13 AM, Jeffrey Hugo wrote: > mhi_recycle_ev_ring() computes the shared write pointer for the ring > (ctxt_wp) using a read/modify/write pattern where the ctxt_wp value in > the > shared memory is read, incremented, and written back. There are no > checks > on the read value, it is assumed that it is kept in sync with the > locally > cached value. Per the MHI spec, this is correct. The device should > only > read ctxt_wp, never write it. > > However, there are devices in the wild that violate the spec, and can > update the ctxt_wp in a specific scenario. This can cause corruption, > and > violate the above assumption that the ctxt_wp is in sync with the > cached > value. > > This can occur when the device has loaded firmware from the host, and > is > transitioning from the SBL EE to the AMSS EE. As part of shutting down > SBL, the SBL flushes it's local MHI context to the shared memory since > the local context will not persist across an EE change. In the case of > the event ring, SBL will flush its entire context, not just the parts > that > it is allowed to update. This means SBL will write to ctxt_wp, and > possibly corrupt it. > > An example: > > Host Device > ---- --- > Update ctxt_wp to 0x1f0 > SBL observes 0x1f0 > Update ctxt_wp to 0x0 > Starts transition to AMSS EE > Context flush, writes 0x1f0 to ctxt_wp > Update ctxt_wp to 0x200 > Update ctxt_wp to 0x210 > AMSS observes 0x210 > 0x210 exceeds ring size > AMSS signals syserr > > The reason the ctxt_wp goes off the end of the ring is that the > rollover > check is only performed on the cached wp, which is out of sync with > ctxt_wp. > > Since the host is the authority of the value of ctxt_wp per the MHI > spec, > we can fix this issue by not reading ctxt_wp from the shared memory, > and > instead compute it based on the cached value. If SBL corrupts ctxt_wp, > the host won't observe it, and will correct the value at some point > later. > > Signed-off-by: Jeffrey Hugo > Reviewed-by: Hemant Kumar > --- > > v2: > Fix typo on the ring base > > drivers/bus/mhi/core/main.c | 9 ++------- > 1 file changed, 2 insertions(+), 7 deletions(-) > > diff --git a/drivers/bus/mhi/core/main.c b/drivers/bus/mhi/core/main.c > index c01ec2f..dc86fdb3 100644 > --- a/drivers/bus/mhi/core/main.c > +++ b/drivers/bus/mhi/core/main.c > @@ -533,18 +533,13 @@ irqreturn_t mhi_intvec_handler(int irq_number, > void *dev) > static void mhi_recycle_ev_ring_element(struct mhi_controller > *mhi_cntrl, > struct mhi_ring *ring) > { > - dma_addr_t ctxt_wp; > - > /* Update the WP */ > ring->wp += ring->el_size; > - ctxt_wp = *ring->ctxt_wp + ring->el_size; > > - if (ring->wp >= (ring->base + ring->len)) { > + if (ring->wp >= (ring->base + ring->len)) > ring->wp = ring->base; > - ctxt_wp = ring->iommu_base; > - } > > - *ring->ctxt_wp = ctxt_wp; > + *ring->ctxt_wp = ring->iommu_base + (ring->wp - ring->base); > > /* Update the RP */ > ring->rp += ring->el_size; Reviewed-by: Bhaumik Bhatt Thanks, Bhaumik --- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project