Received: by 10.192.165.148 with SMTP id m20csp1711582imm; Thu, 3 May 2018 04:17:43 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpIK3wLUqpfaZZBSPHO1IPjkzJpKUNDJRKHx7rbLfNovQK0zwu3cKfdkxMen0KWAUj3Oj42 X-Received: by 2002:a63:9e12:: with SMTP id s18-v6mr8466098pgd.207.1525346263829; Thu, 03 May 2018 04:17:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525346263; cv=none; d=google.com; s=arc-20160816; b=F6Kek/FA/R6l2qXHHywFnhT4tt0ic04UMKXslN+O30Bcu8cCcX4dNe0I3SsCy6etNk 3GvvSnF8wPIWELx8rpyXYaIs/Lg3698CYTHYI440SjD9b54nnDEKavN+OsI5rX3CQEQa d1mqQbHMXBnRaatpkDda8YH3kEsaijfHRr/EwHD3lkVGXffFcOmxUhKHgKLNdCh5jFf+ 6KrSqi05gOasK3+LYStBlWKGKDbDYALJZgVBNrVlqMLkQxuyzeYg5QCxBBy2Sl3v+ryE unkCQi6V8cMhmUBo+H+Ihe2+g+yfjJrFZ6MfJMGQiObCW6hEzomleTyAjRaRHo9CAqqA hw1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=gf0apNFqOXW2BJBcgHlcJqNVv5rwPS8gZbY6HG6JpBI=; b=fl5/zDj6KA1/CJ033IzFpE2zu1r0vSbsXX1OJnoYb3271TwSVPFAarJqdlfpwpXxJq mrUBhXkTn8cCz5sNPCQUEqhE2SFg8uAHVb98Mh/0GkTalfNjFdwEWSUDbOsLkBzeDpcE eNfG65AtjaxSP6MAt6awEpYANwcrY+mbj1FjqqGd/HzENtVwLx/4Xi/E31g/hTVjWDFN hYGFsg12nMyI4uFOA7oVQi2eQuI+nJi59xSPCpEmr2Irorj//BMSZXx2EHIkV+jPIjrp C/KIcui4S1Y686eETwBuA1e4JpDVkEUkOe2ojyzJ2KEfSoDCaFRfVoqa5q8eHJjPHQh7 K4iA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o1-v6si11139951pgp.273.2018.05.03.04.17.29; Thu, 03 May 2018 04:17:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751889AbeECLPU (ORCPT + 99 others); Thu, 3 May 2018 07:15:20 -0400 Received: from alexa-out-tai-02.qualcomm.com ([103.229.16.227]:53487 "EHLO alexa-out-tai-02.qualcomm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751492AbeECLOT (ORCPT ); Thu, 3 May 2018 07:14:19 -0400 X-Greylist: delayed 375 seconds by postgrey-1.27 at vger.kernel.org; Thu, 03 May 2018 07:14:07 EDT X-IronPort-AV: E=Sophos;i="5.49,358,1520870400"; d="scan'208";a="447820" Received: from ironmsg03-tai.qualcomm.com ([10.249.140.8]) by alexa-out-tai-02.qualcomm.com with ESMTP; 03 May 2018 19:07:54 +0800 X-IronPort-AV: E=McAfee;i="5900,7806,8881"; a="7258976" Received: from asutoshd-linux.qualcomm.com ([10.206.24.163]) by ironmsg03-tai.qualcomm.com with ESMTP; 03 May 2018 19:07:41 +0800 Received: by asutoshd-linux.qualcomm.com (Postfix, from userid 92687) id 80E0E2E8C; Thu, 3 May 2018 16:37:38 +0530 (IST) From: Asutosh Das To: subhashj@codeaurora.org, cang@codeaurora.org, vivek.gautam@codeaurora.org, rnayak@codeaurora.org, vinholikatti@gmail.com, jejb@linux.vnet.ibm.com, martin.petersen@oracle.com, asutoshd@codeaurora.org, linux-mmc@vger.kernel.org Cc: linux-scsi@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org (open list) Subject: [PATCH v2 06/10] scsi: ufs: add reference counting for scsi block requests Date: Thu, 3 May 2018 16:37:18 +0530 Message-Id: <3d524952ffe2c0a14c92f5e322aed5d2c24c2832.1525343531.git.asutoshd@codeaurora.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Subhash Jadavani Currently we call the scsi_block_requests()/scsi_unblock_requests() whenever we want to block/unblock scsi requests but as there is no reference counting, nesting of these calls could leave us in undesired state sometime. Consider following call flow sequence: 1. func1() calls scsi_block_requests() but calls func2() before calling scsi_unblock_requests() 2. func2() calls scsi_block_requests() 3. func2() calls scsi_unblock_requests() 4. func1() calls scsi_unblock_requests() As there is no reference counting, we will have scsi requests unblocked after #3 instead of it to be unblocked only after #4. Though we may not have failures seen with this, we might run into some failures in future. Better solution would be to fix this by adding reference counting. Signed-off-by: Subhash Jadavani Signed-off-by: Can Guo Signed-off-by: Asutosh Das --- drivers/scsi/ufs/ufshcd.c | 28 ++++++++++++++++++++-------- drivers/scsi/ufs/ufshcd.h | 2 ++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index dfeb194..c35a076 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -264,6 +264,18 @@ static inline void ufshcd_disable_irq(struct ufs_hba *hba) } } +static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) +{ + if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt)) + scsi_unblock_requests(hba->host); +} + +static void ufshcd_scsi_block_requests(struct ufs_hba *hba) +{ + if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1) + scsi_block_requests(hba->host); +} + /* replace non-printable or non-ASCII characters with spaces */ static inline void ufshcd_remove_non_printable(char *val) { @@ -1077,12 +1089,12 @@ static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) * make sure that there are no outstanding requests when * clock scaling is in progress */ - scsi_block_requests(hba->host); + ufshcd_scsi_block_requests(hba); down_write(&hba->clk_scaling_lock); if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { ret = -EBUSY; up_write(&hba->clk_scaling_lock); - scsi_unblock_requests(hba->host); + ufshcd_scsi_unblock_requests(hba); } return ret; @@ -1091,7 +1103,7 @@ static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) { up_write(&hba->clk_scaling_lock); - scsi_unblock_requests(hba->host); + ufshcd_scsi_unblock_requests(hba); } /** @@ -1411,7 +1423,7 @@ static void ufshcd_ungate_work(struct work_struct *work) hba->clk_gating.is_suspended = false; } unblock_reqs: - scsi_unblock_requests(hba->host); + ufshcd_scsi_unblock_requests(hba); } /** @@ -1467,7 +1479,7 @@ int ufshcd_hold(struct ufs_hba *hba, bool async) * work and to enable clocks. */ case CLKS_OFF: - scsi_block_requests(hba->host); + ufshcd_scsi_block_requests(hba); hba->clk_gating.state = REQ_CLKS_ON; trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); @@ -5192,7 +5204,7 @@ static void ufshcd_err_handler(struct work_struct *work) out: spin_unlock_irqrestore(hba->host->host_lock, flags); - scsi_unblock_requests(hba->host); + ufshcd_scsi_unblock_requests(hba); ufshcd_release(hba); pm_runtime_put_sync(hba->dev); } @@ -5294,7 +5306,7 @@ static void ufshcd_check_errors(struct ufs_hba *hba) /* handle fatal errors only when link is functional */ if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { /* block commands from scsi mid-layer */ - scsi_block_requests(hba->host); + ufshcd_scsi_block_requests(hba); hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED; @@ -8017,7 +8029,7 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) /* Hold auto suspend until async scan completes */ pm_runtime_get_sync(dev); - + atomic_set(&hba->scsi_block_reqs_cnt, 0); /* * We are assuming that device wasn't put in sleep/power-down * state exclusively during the boot stage before kernel. diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h index 0417c42..76c31d5 100644 --- a/drivers/scsi/ufs/ufshcd.h +++ b/drivers/scsi/ufs/ufshcd.h @@ -498,6 +498,7 @@ struct ufs_stats { * @urgent_bkops_lvl: keeps track of urgent bkops level for device * @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for * device is known or not. + * @scsi_block_reqs_cnt: reference counting for scsi block requests */ struct ufs_hba { void __iomem *mmio_base; @@ -698,6 +699,7 @@ struct ufs_hba { struct rw_semaphore clk_scaling_lock; struct ufs_desc_size desc_size; + atomic_t scsi_block_reqs_cnt; }; /* Returns true if clocks can be gated. Otherwise false */ -- Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.