Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2518221imu; Thu, 29 Nov 2018 06:18:52 -0800 (PST) X-Google-Smtp-Source: AFSGD/U4OVfYtzTordaRa6EbGplyD8dnqRR24jJG+f6G/eqjmWzfT4Fiqt5vI3KXCaG8kKrEjHHQ X-Received: by 2002:a65:6094:: with SMTP id t20mr1361743pgu.285.1543501132610; Thu, 29 Nov 2018 06:18:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543501132; cv=none; d=google.com; s=arc-20160816; b=Jn3VIrK0vfVvJyKevw5iypvbfE0miNi/R/xNNTHh8Ln3VtBCI25HQ1THOn6Nxtev/f Rmv13jck7R9PQtd+T5Wr/9R91rh95U61iPntNF8NDGkrKNAbda702VSm3ReevGmcAmaq QknGpbwEHcjGdISGMExR1UOIJv/fqspUMMcu+DyyI0t+zjsx9tY+cCBuQ5eqPn9GRP0w YaRgj9lETUsBL8rQE2H/PC5VR52wH48C/1+d0Z+Yps0Jkd+aeHcV0vK8L9XWBzlnCQdw dP3sqbA3rMjbH0ITUopfzxnwQtjI1qA6iA3rD+JYH0D8iAgQ72Ogx5y8nM3cYvkzPYsU VzqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fnOp/Pg4jw/pZMJMVtQnWCY4LhbKYPde37kp3ekh0RA=; b=Oo+Q70kHwWH9zLMX+86ife+UCwdxk+J6mq/+QDOlv7dCVa3t8Iu6STwEoQyBGR2/nu /odhQnsBgC+QjDwepDvFjmTGhXE++kqFdhw3o5BTw9Pn3rN2lt+XLnYb1CjoiC9iO3v3 B15qPUznzRSnhCP4/Exi3ODwhyXOw3eahhAaDtUXJ9biHrxghdcdNPBiJWOiGeMcribw xo4CFWOyjXDu7bQKt/C2PmxhGeVw91LX8suj1sibhH2+4sg2xdV6GBJKHV2RumuIkBGA CQogBp4yOrZvlBhXe8juz1BObJLRAJa2I+VyrehMj8sefsQNdy7OqH7hBxc0IREXKACm gUCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="WQ3PsNr/"; 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 e35si1852509pgb.548.2018.11.29.06.18.37; Thu, 29 Nov 2018 06:18:52 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b="WQ3PsNr/"; 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 S1730165AbeK3BWV (ORCPT + 99 others); Thu, 29 Nov 2018 20:22:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:44004 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728190AbeK3BWU (ORCPT ); Thu, 29 Nov 2018 20:22:20 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CADDF205C9; Thu, 29 Nov 2018 14:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543501010; bh=S+RZSTma1Kh0rEEoX7ERESvEzOCDNJ1Z5aX5rwzOF7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WQ3PsNr/QC6N9X+6AizV0kIebTHl/vhAsr9sCuFQvkofq3pje3wia4INOWGUKt6FN sU/kThb3Admc4YT3z4AVEgHuCkbS3E/WIvcmDdR4NcoVovXoDM9s5L7PlJq5vrifkm CnZyreFihCPOJnWN8c4PAOl6ODfjg8mwA5MqyM5w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sahitya Tummala , Subhash Jadavani , "Martin K. Petersen" , Amit Pundir Subject: [PATCH 3.18 78/83] scsi: ufs: fix race between clock gating and devfreq scaling work Date: Thu, 29 Nov 2018 15:12:36 +0100 Message-Id: <20181129140142.468437043@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181129140138.002176596@linuxfoundation.org> References: <20181129140138.002176596@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Subhash Jadavani commit 30fc33f1ef475480dc5bea4fe1bda84b003b992c upstream. UFS devfreq clock scaling work may require clocks to be ON if it need to execute some UFS commands hence it may request for clock hold before issuing the command. But if UFS clock gating work is already running in parallel, ungate work would end up waiting for the clock gating work to finish and as clock gating work would also wait for the clock scaling work to finish, we would enter in deadlock state. Here is the call trace during this deadlock state: Workqueue: devfreq_wq devfreq_monitor __switch_to __schedule schedule schedule_timeout wait_for_common wait_for_completion flush_work ufshcd_hold ufshcd_send_uic_cmd ufshcd_dme_get_attr ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div ufs_qcom_clk_scale_notify ufshcd_scale_clks ufshcd_devfreq_target update_devfreq devfreq_monitor process_one_work worker_thread kthread ret_from_fork Workqueue: events ufshcd_gate_work __switch_to __schedule schedule schedule_preempt_disabled __mutex_lock_slowpath mutex_lock devfreq_monitor_suspend devfreq_simple_ondemand_handler devfreq_suspend_device ufshcd_gate_work process_one_work worker_thread kthread ret_from_fork Workqueue: events ufshcd_ungate_work __switch_to __schedule schedule schedule_timeout wait_for_common wait_for_completion flush_work __cancel_work_timer cancel_delayed_work_sync ufshcd_ungate_work process_one_work worker_thread kthread ret_from_fork This change fixes this deadlock by doing this in devfreq work (devfreq_wq): Try cancelling clock gating work. If we are able to cancel gating work or it wasn't scheduled, hold the clock reference count until scaling is in progress. If gate work is already running in parallel, let's skip the frequecy scaling at this time and it will be retried once next scaling window expires. Reviewed-by: Sahitya Tummala Signed-off-by: Subhash Jadavani Signed-off-by: Martin K. Petersen Signed-off-by: Amit Pundir Signed-off-by: Greg Kroah-Hartman --- drivers/scsi/ufs/ufshcd.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -5406,15 +5406,47 @@ static int ufshcd_devfreq_target(struct { int err = 0; struct ufs_hba *hba = dev_get_drvdata(dev); + bool release_clk_hold = false; + unsigned long irq_flags; if (!ufshcd_is_clkscaling_enabled(hba)) return -EINVAL; + spin_lock_irqsave(hba->host->host_lock, irq_flags); + if (ufshcd_eh_in_progress(hba)) { + spin_unlock_irqrestore(hba->host->host_lock, irq_flags); + return 0; + } + + if (ufshcd_is_clkgating_allowed(hba) && + (hba->clk_gating.state != CLKS_ON)) { + if (cancel_delayed_work(&hba->clk_gating.gate_work)) { + /* hold the vote until the scaling work is completed */ + hba->clk_gating.active_reqs++; + release_clk_hold = true; + hba->clk_gating.state = CLKS_ON; + } else { + /* + * Clock gating work seems to be running in parallel + * hence skip scaling work to avoid deadlock between + * current scaling work and gating work. + */ + spin_unlock_irqrestore(hba->host->host_lock, irq_flags); + return 0; + } + } + spin_unlock_irqrestore(hba->host->host_lock, irq_flags); + if (*freq == UINT_MAX) err = ufshcd_scale_clks(hba, true); else if (*freq == 0) err = ufshcd_scale_clks(hba, false); + spin_lock_irqsave(hba->host->host_lock, irq_flags); + if (release_clk_hold) + __ufshcd_release(hba); + spin_unlock_irqrestore(hba->host->host_lock, irq_flags); + return err; }