Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2247163rwd; Fri, 9 Jun 2023 08:33:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5xQpiw2TBWLmjNNaGXL8Tv09Ryr6ekL6fgZZeoMEhnFpiQDsdJ0gJYRCsBBLupWRFIRW+r X-Received: by 2002:a17:902:d4cc:b0:1ac:4735:45d7 with SMTP id o12-20020a170902d4cc00b001ac473545d7mr1883204plg.6.1686324809270; Fri, 09 Jun 2023 08:33:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686324809; cv=none; d=google.com; s=arc-20160816; b=EXl55K+QQFahC6kosw1wB/49MRvgLw3JmCsRUv/PFOrYYvpqt9AqmBjkE2ppk9J1Js Fq3VcyEDN4lPwRzW42A+38jWsrK/PTqXvnUiIE7py1+gG/7SDqfQ0cr8fg7uaTV9mEey X3gCV+PM/79A/IPfvGkB2Mgs69jlhFbRf28xGJf5KGBpQj+kVEPZhwhFYEqdd3WXOQVU XFmMnxYtcYajDYcwL4OJlgPyYw0GmBtTDMgdRkAR9gaI8XGyBknkYkp3NfCWYEWm3fcT n6aAQ+251O0Uw4lOh2lqPu/aEWCCHL+GfN+YHrZWwABWpbxvYbnFsR231DeZdBLwqzGu ZZ9w== 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=pf6wOR3QwBpqRrbCCzQI3m8dQ1fomGKSQzhmJ3BU2hk=; b=pRbJRXIg1LGIWBCXlhGyh2yBvFlxNYg2Fycwsv+Al4fJIG6oU3RIFeJy78grm8LIX/ 0kDjGBZhx44YcEgXt3Hfebu6fuecetKPkSf2IZYVz2ICbE7YlhOd65e9DatPsekHowli 1ZpitLHS8A+Hpqj6j69u357xjh/kI21P17KfHefhAGi3W15CYsjIrkqJ4XZ7k+5EIExp Eh1jcAhOVkhYc7SbmtTJPaHFZlwP3U0MYnszEbqZgTl7CqcnCKhiQYUnwMEVnqY7V908 2jCMAkCchkDablE/AaxUySBuC5DwwCbr1OXDpJw7t+fV7aKPTJ8ARdWxiwcRi+aYbkx3 v6Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=mrI3zVwV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j14-20020a170903024e00b001ac6e1fd203si2858061plh.180.2023.06.09.08.33.13; Fri, 09 Jun 2023 08:33:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=mrI3zVwV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241416AbjFIPSl (ORCPT + 99 others); Fri, 9 Jun 2023 11:18:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239661AbjFIPSe (ORCPT ); Fri, 9 Jun 2023 11:18:34 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D66B269A; Fri, 9 Jun 2023 08:18:32 -0700 (PDT) X-UUID: e16bf54806d811eeb20a276fd37b9834-20230609 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=pf6wOR3QwBpqRrbCCzQI3m8dQ1fomGKSQzhmJ3BU2hk=; b=mrI3zVwVOVNsgBTgQ3o3VX5BNjtPLo/nEZQFnQ5R9cTRVMEOyf6o5rKv+jiv6WBkvEUlPEpFxMvbABx2zwzlz2aW0m/c1qb7lsn1ddOj+zpt9weDoCwg06U7aA05oZIzURYM/uClGDggzK+IK1Q5Rkuw+NQNRMwS5vm81iaI4nI=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.26,REQID:80259494-7073-472d-ac81-e5dcc8e7f378,IP:0,U RL:0,TC:0,Content:-5,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:90 X-CID-INFO: VERSION:1.1.26,REQID:80259494-7073-472d-ac81-e5dcc8e7f378,IP:0,URL :0,TC:0,Content:-5,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTIO N:quarantine,TS:90 X-CID-META: VersionHash:cb9a4e1,CLOUDID:77fd966e-2f20-4998-991c-3b78627e4938,B ulkID:230609231827BZA3AU36,BulkQuantity:0,Recheck:0,SF:19|48|38|29|28|17,T C:nil,Content:0,EDM:-3,IP:nil,URL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0 ,OSI:0,OSA:0,AV:0,LES:1,SPR:NO X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_SDM,TF_CID_SPAM_ASC,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD, TF_CID_SPAM_SNR X-UUID: e16bf54806d811eeb20a276fd37b9834-20230609 Received: from mtkmbs13n1.mediatek.inc [(172.21.101.193)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 509786790; Fri, 09 Jun 2023 23:18:25 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.108) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Fri, 9 Jun 2023 23:18:24 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Fri, 9 Jun 2023 23:18:24 +0800 From: Po-Wen Kao To: , , , , Alim Akhtar , Avri Altman , Bart Van Assche , "James E.J. Bottomley" , "Martin K. Petersen" , Matthias Brugger , AngeloGioacchino Del Regno CC: , , , , , , , , Subject: [PATCH v1 1/2] scsi: ufs: mcq: Fix the incorrect OCS value for the device command Date: Fri, 9 Jun 2023 23:17:55 +0800 Message-ID: <20230609151758.19070-1-powen.kao@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 Content-Type: text/plain X-MTK: N X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY,URIBL_BLOCKED autolearn=ham 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 From: Stanley Chu In MCQ mode, when a device command uses a hardware queue shared with other commands, a race condition may occur in the following scenario: 1. A device command is completed in CQx with CQE entry "e". 2. The interrupt handler copies the "cqe" pointer to "hba->dev_cmd.cqe" and completes "hba->dev_cmd.complete". 3. The "ufshcd_wait_for_dev_cmd()" function is awakened and retrieves the OCS value from "hba->dev_cmd.cqe". However, there is a possibility that the CQE entry "e" will be overwritten by newly completed commands in CQx, resulting in an incorrect OCS value being received by "ufshcd_wait_for_dev_cmd()". To avoid this race condition, the OCS value should be immediately copied to the struct "lrb" of the device command. Then "ufshcd_wait_for_dev_cmd()" can retrieve the OCS value from the struct "lrb". Fixes: b5167638ec82 ("scsi: ufs: core: mcq: Add support to allocate multiple queues") Suggested-by: Can Guo Signed-off-by: Stanley Chu --- drivers/ufs/core/ufshcd.c | 11 ++++++++--- include/ufs/ufshcd.h | 1 - 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index 5da62248ebc4..df8782981d8c 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -3086,7 +3086,7 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, * not trigger any race conditions. */ hba->dev_cmd.complete = NULL; - err = ufshcd_get_tr_ocs(lrbp, hba->dev_cmd.cqe); + err = ufshcd_get_tr_ocs(lrbp, NULL); if (!err) err = ufshcd_dev_cmd_completion(hba, lrbp); } else { @@ -3182,7 +3182,6 @@ static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, goto out; hba->dev_cmd.complete = &wait; - hba->dev_cmd.cqe = NULL; ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr); @@ -5431,6 +5430,7 @@ void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag, { struct ufshcd_lrb *lrbp; struct scsi_cmnd *cmd; + enum utp_ocs ocs; lrbp = &hba->lrb[task_tag]; lrbp->compl_time_stamp = ktime_get(); @@ -5446,7 +5446,12 @@ void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag, } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { if (hba->dev_cmd.complete) { - hba->dev_cmd.cqe = cqe; + if (cqe) { + ocs = le32_to_cpu(cqe->status) & MASK_OCS; + lrbp->utr_descriptor_ptr->header.dword_2 = + cpu_to_le32(ocs); + } + ufshcd_add_command_trace(hba, task_tag, UFS_DEV_COMP); complete(hba->dev_cmd.complete); ufshcd_clk_scaling_update_busy(hba); } diff --git a/include/ufs/ufshcd.h b/include/ufs/ufshcd.h index 9b2d1859f885..602615e6d1bf 100644 --- a/include/ufs/ufshcd.h +++ b/include/ufs/ufshcd.h @@ -225,7 +225,6 @@ struct ufs_dev_cmd { struct mutex lock; struct completion *complete; struct ufs_query query; - struct cq_entry *cqe; }; /** -- 2.18.0