From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.7 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60E5DC432C0 for ; Thu, 21 Nov 2019 22:09:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3210F206D8 for ; Thu, 21 Nov 2019 22:09:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726655AbfKUWJN (ORCPT ); Thu, 21 Nov 2019 17:09:13 -0500 Received: from mail-pj1-f68.google.com ([209.85.216.68]:39012 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726297AbfKUWJN (ORCPT ); Thu, 21 Nov 2019 17:09:13 -0500 Received: by mail-pj1-f68.google.com with SMTP id t103so2131588pjb.6 for ; Thu, 21 Nov 2019 14:09:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=krgVqfN08dI1zDlTZBYuPK/bUPMTZCdq3wSHyVYHpKs=; b=FOaWuEyjuol0tpVWz9ClsKfDfm8H/y4YS+lhDJ/J4owY92iabu2n5p6OQIvM2JsWqX GkfQo9We3ECa5VS+gpGxNv/FXNizbsQy98BaSq/hWwzF5gyKOhHWPLngPjJmblFFy6C0 kQHB5uUuD2ACNwOGtaD/ElVxlHb5SwrQ8ekuWeXx4cthS5WTsmsCQNprZ0Qz49SiDnfl h2nUZICfOJk+LYciahqwzwOFjRULCIb+BuJkFAsp0MG/SxNxIBnJDnKTp8kaWEhj6hjK OsMzoR6rj/bUr9jy73TV0kfEn14i1dupbTlAXzqqdlE4n46EiZTI8q8OzJVfVVXkhSH6 UaEA== X-Gm-Message-State: APjAAAX+O9hPadOThZQybZ3sxelFcfk+DVuKgF6zip8w/O3MEb6NAa7B OWpnod7UNX4sOUmwlFUjI54= X-Google-Smtp-Source: APXvYqzqh9c6ZKeNwtwvX5HFI8xPjKfVO69ylpzXFP4FvMI994mJGQV9uOXJ2TPw/DaP5R/m0ni24g== X-Received: by 2002:a17:902:8692:: with SMTP id g18mr11257394plo.39.1574374152471; Thu, 21 Nov 2019 14:09:12 -0800 (PST) Received: from desktop-bart.svl.corp.google.com ([2620:15c:2cd:202:4308:52a3:24b6:2c60]) by smtp.gmail.com with ESMTPSA id m15sm4617714pfh.19.2019.11.21.14.09.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2019 14:09:11 -0800 (PST) From: Bart Van Assche To: "Martin K . Petersen" , "James E . J . Bottomley" Cc: Bean Huo , Avri Altman , Asutosh Das , Vignesh Raghavendra , Can Guo , linux-scsi@vger.kernel.org, Bart Van Assche , Stanley Chu , Tomas Winkler Subject: [PATCH v6 4/4] ufs: Simplify the clock scaling mechanism implementation Date: Thu, 21 Nov 2019 14:08:50 -0800 Message-Id: <20191121220850.16195-5-bvanassche@acm.org> X-Mailer: git-send-email 2.24.0.432.g9d3f5f5b63-goog In-Reply-To: <20191121220850.16195-1-bvanassche@acm.org> References: <20191121220850.16195-1-bvanassche@acm.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Scaling the clock is only safe while no commands are in progress. The current clock scaling implementation uses hba->clk_scaling_lock to serialize clock scaling against the following three functions: * ufshcd_queuecommand() (uses sdev->request_queue) * ufshcd_exec_dev_cmd() (uses hba->cmd_queue) * ufshcd_issue_devman_upiu_cmd() (uses hba->cmd_queue) __ufshcd_issue_tm_cmd(), which uses hba->tmf_queue, is not yet serialized against clock scaling. Use blk_mq_{un,}freeze_queue() to block submission of new commands and to wait for ongoing commands to complete. This patch removes a semaphore down and up operation pair from the hot path. This patch fixes a bug by disallowing that TMFs are submitted as follows from user space while clock scaling is in progress: submit UPIU_TRANSACTION_TASK_REQ on an UFS BSG queue -> ufs_bsg_request() -> ufshcd_exec_raw_upiu_cmd(msgcode = UPIU_TRANSACTION_TASK_REQ) -> ufshcd_exec_raw_upiu_cmd() -> __ufshcd_issue_tm_cmd() Cc: Can Guo Cc: Bean Huo Cc: Avri Altman Cc: Stanley Chu Cc: Tomas Winkler Signed-off-by: Bart Van Assche --- drivers/scsi/ufs/ufshcd.c | 124 ++++++++++++-------------------------- drivers/scsi/ufs/ufshcd.h | 1 - 2 files changed, 40 insertions(+), 85 deletions(-) diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index 99337e0b54f6..472d164f6ae6 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -971,65 +971,6 @@ static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, return false; } -static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, - u64 wait_timeout_us) -{ - unsigned long flags; - int ret = 0; - u32 tm_doorbell; - u32 tr_doorbell; - bool timeout = false, do_last_check = false; - ktime_t start; - - ufshcd_hold(hba, false); - spin_lock_irqsave(hba->host->host_lock, flags); - /* - * Wait for all the outstanding tasks/transfer requests. - * Verify by checking the doorbell registers are clear. - */ - start = ktime_get(); - do { - if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { - ret = -EBUSY; - goto out; - } - - tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); - tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); - if (!tm_doorbell && !tr_doorbell) { - timeout = false; - break; - } else if (do_last_check) { - break; - } - - spin_unlock_irqrestore(hba->host->host_lock, flags); - schedule(); - if (ktime_to_us(ktime_sub(ktime_get(), start)) > - wait_timeout_us) { - timeout = true; - /* - * We might have scheduled out for long time so make - * sure to check if doorbells are cleared by this time - * or not. - */ - do_last_check = true; - } - spin_lock_irqsave(hba->host->host_lock, flags); - } while (tm_doorbell || tr_doorbell); - - if (timeout) { - dev_err(hba->dev, - "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", - __func__, tm_doorbell, tr_doorbell); - ret = -EBUSY; - } -out: - spin_unlock_irqrestore(hba->host->host_lock, flags); - ufshcd_release(hba); - return ret; -} - /** * ufshcd_scale_gear - scale up/down UFS gear * @hba: per adapter instance @@ -1079,27 +1020,54 @@ static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) { - #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ - int ret = 0; + unsigned long deadline = jiffies + HZ; + struct scsi_device *sdev; + int res = 0; + /* - * make sure that there are no outstanding requests when - * clock scaling is in progress + * Make sure that no requests are outstanding while clock scaling is in + * progress. If SCSI error recovery would start while this function is + * in progress then + * blk_mq_freeze_queue_wait_timeout(sdev->request_queue) will either + * wait until error handling has finished or will time out. */ - 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); - ufshcd_scsi_unblock_requests(hba); + shost_for_each_device(sdev, hba->host) + blk_freeze_queue_start(sdev->request_queue); + blk_freeze_queue_start(hba->cmd_queue); + blk_freeze_queue_start(hba->tmf_queue); + shost_for_each_device(sdev, hba->host) { + if (blk_mq_freeze_queue_wait_timeout(sdev->request_queue, + max_t(long, 0, deadline - jiffies)) <= 0) { + goto err; + } } + if (blk_mq_freeze_queue_wait_timeout(hba->cmd_queue, + max_t(long, 0, deadline - jiffies)) <= 0) + goto err; + if (blk_mq_freeze_queue_wait_timeout(hba->tmf_queue, + max_t(long, 0, deadline - jiffies)) <= 0) + goto err; - return ret; +out: + return res; + +err: + blk_mq_unfreeze_queue(hba->tmf_queue); + blk_mq_unfreeze_queue(hba->cmd_queue); + shost_for_each_device(sdev, hba->host) + blk_mq_unfreeze_queue(sdev->request_queue); + res = -ETIMEDOUT; + goto out; } static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) { - up_write(&hba->clk_scaling_lock); - ufshcd_scsi_unblock_requests(hba); + struct scsi_device *sdev; + + blk_mq_unfreeze_queue(hba->tmf_queue); + blk_mq_unfreeze_queue(hba->cmd_queue); + shost_for_each_device(sdev, hba->host) + blk_mq_unfreeze_queue(sdev->request_queue); } /** @@ -2394,9 +2362,6 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) BUG(); } - if (!down_read_trylock(&hba->clk_scaling_lock)) - return SCSI_MLQUEUE_HOST_BUSY; - spin_lock_irqsave(hba->host->host_lock, flags); switch (hba->ufshcd_state) { case UFSHCD_STATE_OPERATIONAL: @@ -2462,7 +2427,6 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) out_unlock: spin_unlock_irqrestore(hba->host->host_lock, flags); out: - up_read(&hba->clk_scaling_lock); return err; } @@ -2616,8 +2580,6 @@ static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, struct completion wait; unsigned long flags; - down_read(&hba->clk_scaling_lock); - /* * Get free slot, sleep if slots are unavailable. * Even though we use wait_event() which sleeps indefinitely, @@ -2653,7 +2615,6 @@ static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, out_put_tag: blk_put_request(req); - up_read(&hba->clk_scaling_lock); return err; } @@ -5845,8 +5806,6 @@ static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, unsigned long flags; u32 upiu_flags; - down_read(&hba->clk_scaling_lock); - req = blk_get_request(q, REQ_OP_DRV_OUT, 0); if (IS_ERR(req)) return PTR_ERR(req); @@ -5928,7 +5887,6 @@ static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, } blk_put_request(req); - up_read(&hba->clk_scaling_lock); return err; } @@ -8397,8 +8355,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) /* Initialize mutex for device management commands */ mutex_init(&hba->dev_cmd.lock); - init_rwsem(&hba->clk_scaling_lock); - ufshcd_init_clk_gating(hba); ufshcd_init_clk_scaling(hba); diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h index 53bfe175342c..bbdef1153257 100644 --- a/drivers/scsi/ufs/ufshcd.h +++ b/drivers/scsi/ufs/ufshcd.h @@ -724,7 +724,6 @@ struct ufs_hba { enum bkops_status urgent_bkops_lvl; bool is_urgent_bkops_lvl_checked; - struct rw_semaphore clk_scaling_lock; struct ufs_desc_size desc_size; atomic_t scsi_block_reqs_cnt;