Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3315720pxu; Sat, 19 Dec 2020 21:45:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJzysiimk/bJMKs2YmD15QF8GYNlYBIC2Tn3ADzLISFC+/B0U7meTXHGso+WLy7eQi4GsO3/ X-Received: by 2002:a05:6402:1386:: with SMTP id b6mr11250677edv.42.1608443126476; Sat, 19 Dec 2020 21:45:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608443126; cv=none; d=google.com; s=arc-20160816; b=j9B1Wg+j/iGbCbW/3roZ1yGmI1Vxabe879BJapHNJzLMNUz7ERWq13Qa8Q5lIPJPWE suYnzl21zvJheGGOcp0dEtruIdA44XuxPoPah67UAecZurz6+wVvBabXvTE3Zz3bRGSR 9SjzNGFdrUrMb50lqFRirwz0g098Xvwl41ca7hxX4QFv3aqycZqYRUdVgrJEYpHefRy3 TZOlxgUCaVHoNLZAQI6tQgYK8hahhQ285ZvGOmKBAsIZMWWqOx1kYvLnE+ud4sb2bfCX 1v7ojL9xKafZFzIbkCbA8wc1R2C7JPIKLX1WMHBhJyVi5okJpgdhTS5/d8dNc52hz4Mb SA0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:ironport-sdr; bh=Zx2pg4LosJh0WEVXEaSDzyYSj5I9Gdo3RMNPGb8r9KI=; b=pEXENlt8wl/viPw5yWNaxhc12OkTtnPR+9OQ9hVvGB4fe2cHRF/9ncFbQNsjIuc0M5 jLl/weJpYf236G+AyEo/C3rilWTtljvmJLuqv+35w7y7m0scGj3+XqICdyrfWUKA1nQL vC2R+nb38/+XbnWVSG/Ds3vMjTiBOK9Zgy8r3I4WXPdOIDLth3Pe7EqavsK6U9moragX uBujtiiE1kxiphZRySrvSRte5vECjhsASbM+9nmP6dE0fW/goQahuNwr6gE15qKPcAgb LNNwg4bkDliwFIyd+kPtx5w1fl+olfKc+ISS3ryNjXmg1GZqoocpOtNAdg8qq+UKepKj JuNw== ARC-Authentication-Results: i=1; mx.google.com; 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 b6si8871108edn.50.2020.12.19.21.45.02; Sat, 19 Dec 2020 21:45:26 -0800 (PST) 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; 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 S1727161AbgLTFns (ORCPT + 99 others); Sun, 20 Dec 2020 00:43:48 -0500 Received: from labrats.qualcomm.com ([199.106.110.90]:29195 "EHLO labrats.qualcomm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726099AbgLTFnr (ORCPT ); Sun, 20 Dec 2020 00:43:47 -0500 IronPort-SDR: 5Eg2bAC1RJMt6sL3Y6O14yK/uwBUP3cvmjPsi8U/442NN/hMHMEcjQURX6NLsv1E7u9u+4P9OJ hFrf3fZkFORVX4vWnaF2dgfbqxsIbl4B92xbeyu8VJssky18+4DbRHFZ2E3Yxh082skbS3inbr QrU45Z9HTdzRRb7RP9GcvYI13kXBDua82ERxVnRYQdeAP7KDBQCiRXNkOmYT1/SaodP8Oacr9C /IS9Cm+Dd5dRj/l7N8oSRU1zWvC4EjE7xY5D4lw4jI441JAq0f5nK8yPIvuymc4B+mDjiZ/otP dno= X-IronPort-AV: E=Sophos;i="5.78,434,1599548400"; d="scan'208";a="29398169" Received: from unknown (HELO ironmsg02-sd.qualcomm.com) ([10.53.140.142]) by labrats.qualcomm.com with ESMTP; 19 Dec 2020 21:42:57 -0800 X-QCInternal: smtphost Received: from wsp769891wss.qualcomm.com (HELO stor-presley.qualcomm.com) ([192.168.140.85]) by ironmsg02-sd.qualcomm.com with ESMTP; 19 Dec 2020 21:42:56 -0800 Received: by stor-presley.qualcomm.com (Postfix, from userid 359480) id E0F35212A8; Sat, 19 Dec 2020 21:42:55 -0800 (PST) From: Can Guo To: asutoshd@codeaurora.org, nguyenb@codeaurora.org, hongwus@codeaurora.org, rnayak@codeaurora.org, linux-scsi@vger.kernel.org, kernel-team@android.com, saravanak@google.com, salyzyn@google.com, cang@codeaurora.org Cc: Alim Akhtar , Avri Altman , "James E.J. Bottomley" , "Martin K. Petersen" , Matthias Brugger , Stanley Chu , Bean Huo , Bart Van Assche , Satya Tangirala , linux-kernel@vger.kernel.org (open list), linux-arm-kernel@lists.infradead.org (moderated list:ARM/Mediatek SoC support), linux-mediatek@lists.infradead.org (moderated list:ARM/Mediatek SoC support) Subject: [PATCH v6 1/3] scsi: ufs: Protect some contexts from unexpected clock scaling Date: Sat, 19 Dec 2020 21:42:45 -0800 Message-Id: <1608442968-38560-2-git-send-email-cang@codeaurora.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1608442968-38560-1-git-send-email-cang@codeaurora.org> References: <1608442968-38560-1-git-send-email-cang@codeaurora.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In contexts like suspend, shutdown and error handling, we need to suspend devfreq to make sure these contexts won't be disturbed by clock scaling. However, suspending devfreq is not enough since users can still trigger a clock scaling by manipulating the sysfs node clkscale_enable and devfreq sysfs nodes like min/max_freq and governor. Add one more flag in struct clk_scaling such that these contexts can prevent clock scaling from being invoked through above sysfs nodes. Reviewed-by: Stanley Chu Reviewed-by: Bean Huo Signed-off-by: Can Guo --- drivers/scsi/ufs/ufshcd.c | 81 +++++++++++++++++++++++++++++++---------------- drivers/scsi/ufs/ufshcd.h | 6 +++- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index 0c148fc..79752a7 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -1147,12 +1147,22 @@ static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) */ ufshcd_scsi_block_requests(hba); down_write(&hba->clk_scaling_lock); - if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { + + if (!hba->clk_scaling.is_allowed) + ret = -EAGAIN; + else if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) ret = -EBUSY; + + if (ret) { up_write(&hba->clk_scaling_lock); ufshcd_scsi_unblock_requests(hba); + goto out; } + /* let's not get into low power until clock scaling is completed */ + ufshcd_hold(hba, false); + +out: return ret; } @@ -1160,6 +1170,7 @@ static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) { up_write(&hba->clk_scaling_lock); ufshcd_scsi_unblock_requests(hba); + ufshcd_release(hba); } /** @@ -1175,12 +1186,9 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) { int ret = 0; - /* let's not get into low power until clock scaling is completed */ - ufshcd_hold(hba, false); - ret = ufshcd_clock_scaling_prepare(hba); if (ret) - goto out; + return ret; /* scale down the gear before scaling down clocks */ if (!scale_up) { @@ -1212,8 +1220,6 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) out_unprepare: ufshcd_clock_scaling_unprepare(hba); -out: - ufshcd_release(hba); return ret; } @@ -1487,7 +1493,7 @@ static ssize_t ufshcd_clkscale_enable_show(struct device *dev, { struct ufs_hba *hba = dev_get_drvdata(dev); - return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); + return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_enabled); } static ssize_t ufshcd_clkscale_enable_store(struct device *dev, @@ -1496,12 +1502,20 @@ static ssize_t ufshcd_clkscale_enable_store(struct device *dev, struct ufs_hba *hba = dev_get_drvdata(dev); u32 value; int err; + unsigned long flags; + bool update = true; if (kstrtou32(buf, 0, &value)) return -EINVAL; value = !!value; - if (value == hba->clk_scaling.is_allowed) + spin_lock_irqsave(hba->host->host_lock, flags); + if (value == hba->clk_scaling.is_enabled) + update = false; + else + hba->clk_scaling.is_enabled = value; + spin_unlock_irqrestore(hba->host->host_lock, flags); + if (!update) goto out; pm_runtime_get_sync(hba->dev); @@ -1510,8 +1524,6 @@ static ssize_t ufshcd_clkscale_enable_store(struct device *dev, cancel_work_sync(&hba->clk_scaling.suspend_work); cancel_work_sync(&hba->clk_scaling.resume_work); - hba->clk_scaling.is_allowed = value; - if (value) { ufshcd_resume_clkscaling(hba); } else { @@ -1845,8 +1857,6 @@ static void ufshcd_init_clk_scaling(struct ufs_hba *hba) snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d", hba->host->host_no); hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); - - ufshcd_clkscaling_init_sysfs(hba); } static void ufshcd_exit_clk_scaling(struct ufs_hba *hba) @@ -1854,6 +1864,8 @@ static void ufshcd_exit_clk_scaling(struct ufs_hba *hba) if (!ufshcd_is_clkscaling_supported(hba)) return; + if (hba->devfreq) + device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); destroy_workqueue(hba->clk_scaling.workq); ufshcd_devfreq_remove(hba); } @@ -1918,7 +1930,7 @@ static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) if (!hba->clk_scaling.active_reqs++) queue_resume_work = true; - if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress) + if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress) return; if (queue_resume_work) @@ -4987,7 +4999,8 @@ static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, complete(hba->dev_cmd.complete); } } - if (ufshcd_is_clkscaling_supported(hba)) + if (ufshcd_is_clkscaling_supported(hba) && + hba->clk_scaling.active_reqs > 0) hba->clk_scaling.active_reqs--; } @@ -5650,18 +5663,25 @@ static void ufshcd_err_handling_prepare(struct ufs_hba *hba) ufshcd_vops_resume(hba, UFS_RUNTIME_PM); } else { ufshcd_hold(hba, false); - if (hba->clk_scaling.is_allowed) { + if (hba->clk_scaling.is_enabled) { cancel_work_sync(&hba->clk_scaling.suspend_work); cancel_work_sync(&hba->clk_scaling.resume_work); ufshcd_suspend_clkscaling(hba); } } + down_write(&hba->clk_scaling_lock); + hba->clk_scaling.is_allowed = false; + up_write(&hba->clk_scaling_lock); } static void ufshcd_err_handling_unprepare(struct ufs_hba *hba) { ufshcd_release(hba); - if (hba->clk_scaling.is_allowed) + + down_write(&hba->clk_scaling_lock); + hba->clk_scaling.is_allowed = true; + up_write(&hba->clk_scaling_lock); + if (hba->clk_scaling.is_enabled) ufshcd_resume_clkscaling(hba); pm_runtime_put(hba->dev); } @@ -7620,12 +7640,14 @@ static int ufshcd_add_lus(struct ufs_hba *hba) sizeof(struct ufs_pa_layer_attr)); hba->clk_scaling.saved_pwr_info.is_valid = true; if (!hba->devfreq) { + hba->clk_scaling.is_allowed = true; ret = ufshcd_devfreq_init(hba); if (ret) goto out; - } - hba->clk_scaling.is_allowed = true; + hba->clk_scaling.is_enabled = true; + ufshcd_clkscaling_init_sysfs(hba); + } } ufs_bsg_probe(hba); @@ -8491,11 +8513,14 @@ static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) ufshcd_hold(hba, false); hba->clk_gating.is_suspended = true; - if (hba->clk_scaling.is_allowed) { + if (hba->clk_scaling.is_enabled) { cancel_work_sync(&hba->clk_scaling.suspend_work); cancel_work_sync(&hba->clk_scaling.resume_work); ufshcd_suspend_clkscaling(hba); } + down_write(&hba->clk_scaling_lock); + hba->clk_scaling.is_allowed = false; + up_write(&hba->clk_scaling_lock); if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && req_link_state == UIC_LINK_ACTIVE_STATE) { @@ -8592,8 +8617,6 @@ static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) goto out; set_link_active: - if (hba->clk_scaling.is_allowed) - ufshcd_resume_clkscaling(hba); ufshcd_vreg_set_hpm(hba); if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) ufshcd_set_link_active(hba); @@ -8603,7 +8626,10 @@ static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) ufshcd_disable_auto_bkops(hba); enable_gating: - if (hba->clk_scaling.is_allowed) + down_write(&hba->clk_scaling_lock); + hba->clk_scaling.is_allowed = true; + up_write(&hba->clk_scaling_lock); + if (hba->clk_scaling.is_enabled) ufshcd_resume_clkscaling(hba); hba->clk_gating.is_suspended = false; hba->dev_info.b_rpm_dev_flush_capable = false; @@ -8701,7 +8727,10 @@ static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) hba->clk_gating.is_suspended = false; - if (hba->clk_scaling.is_allowed) + down_write(&hba->clk_scaling_lock); + hba->clk_scaling.is_allowed = true; + up_write(&hba->clk_scaling_lock); + if (hba->clk_scaling.is_enabled) ufshcd_resume_clkscaling(hba); /* Enable Auto-Hibernate if configured */ @@ -8725,8 +8754,6 @@ static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) ufshcd_vreg_set_lpm(hba); disable_irq_and_vops_clks: ufshcd_disable_irq(hba); - if (hba->clk_scaling.is_allowed) - ufshcd_suspend_clkscaling(hba); ufshcd_setup_clocks(hba, false); if (ufshcd_is_clkgating_allowed(hba)) { hba->clk_gating.state = CLKS_OFF; @@ -8944,8 +8971,6 @@ void ufshcd_remove(struct ufs_hba *hba) ufshcd_exit_clk_scaling(hba); ufshcd_exit_clk_gating(hba); - if (ufshcd_is_clkscaling_supported(hba)) - device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); ufshcd_hba_exit(hba); } EXPORT_SYMBOL_GPL(ufshcd_remove); diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h index e0f00a4..5737679 100644 --- a/drivers/scsi/ufs/ufshcd.h +++ b/drivers/scsi/ufs/ufshcd.h @@ -382,7 +382,10 @@ struct ufs_saved_pwr_info { * @workq: workqueue to schedule devfreq suspend/resume work * @suspend_work: worker to suspend devfreq * @resume_work: worker to resume devfreq - * @is_allowed: tracks if scaling is currently allowed or not + * @is_enabled: tracks if scaling is currently enabled or not, controlled by + clkscale_enable sysfs node + * @is_allowed: tracks if scaling is currently allowed or not, used to block + clock scaling which is not invoked from devfreq governor * @is_busy_started: tracks if busy period has started or not * @is_suspended: tracks if devfreq is suspended or not */ @@ -396,6 +399,7 @@ struct ufs_clk_scaling { struct workqueue_struct *workq; struct work_struct suspend_work; struct work_struct resume_work; + bool is_enabled; bool is_allowed; bool is_busy_started; bool is_suspended; -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.