Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp56924pxb; Wed, 29 Sep 2021 21:03:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVOwfgseNOCKSz5wH3fS81OtfgUaXUohDDKX0qzzOiCkAvEBxBvn21EFu2RXenTQwK6buV X-Received: by 2002:a17:902:f703:b029:12c:982:c9ae with SMTP id h3-20020a170902f703b029012c0982c9aemr2070840plo.20.1632974596682; Wed, 29 Sep 2021 21:03:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632974596; cv=none; d=google.com; s=arc-20160816; b=lZbZvJ4FJvjR7zungiXJwFwa48a1cMTCnHVcEcOTxbUrkLrs/UP8jvidWGQ97aH5Am UJWxomzVP4wpyWsKiXoGdTr/CrbS9vio8DYDiovpxTARvNT7ABk2HTp/Ouu/21A/p8DA jtpXTkS6zMppjgUqZz+LOcAGxV4FlixxtT7C8GQb5Z1RUD8cCZGkWLL+hFsXwTqMpOcD 59A/75Wb0zLQvrgCeUID+I12wqq2Xbu8UOx5E9tE7Hg9r1L0T4r0PeJpbrlXVqbyAOm8 p9gDQAdCZNJQCCVC97Z+MwaL2au6LEF4ZRE5lRR/aZ0PRPQtqrJjVKSrv9f0OC0ZBYZX +cHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:message-id:references:in-reply-to :subject:cc:to:from:date:content-transfer-encoding:mime-version :sender:dkim-signature; bh=Lk3z6zTSdTFGpMOmC+/3+SLOaVyJ9DMt6rB0tK9eu9w=; b=mUdk/6l+XJcNURvH/h+wKO7G5R585XiIivVkM1crCf6nD09qFID+9HqV6Qe0K63Uu0 ZbpPBHC25l4+stfzXlFCo8XnpK1QPdSNoliuPEYfrBu+XM2I4VXe3qmB05FdZGjbtBj2 CpILt9abvHhlPu2xzQdhNKJ29i4kk2s2FzS6nIn3jc5fgsxCvgf3a5pYSGwsGaqPp1lJ ZV7zUcdqKcIMPZ2c306qOLCWeIkPq/plO7KH+2g98lp9cCtlfWX69g92yMEHOILrfVNO KhyOwiktK+M/zs9y3lWxFrxpW2blN9QORUUZMFPHTOb1rHrSwkbon0hsm8dND+JJ2lpH fdaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b="Q58s/HOG"; 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 m67si2254929pfb.368.2021.09.29.21.03.03; Wed, 29 Sep 2021 21:03:16 -0700 (PDT) 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; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b="Q58s/HOG"; 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 S1348018AbhI3D7k (ORCPT + 99 others); Wed, 29 Sep 2021 23:59:40 -0400 Received: from m43-7.mailgun.net ([69.72.43.7]:59644 "EHLO m43-7.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347963AbhI3D7j (ORCPT ); Wed, 29 Sep 2021 23:59:39 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1632974277; h=Message-ID: References: In-Reply-To: Subject: Cc: To: From: Date: Content-Transfer-Encoding: Content-Type: MIME-Version: Sender; bh=Lk3z6zTSdTFGpMOmC+/3+SLOaVyJ9DMt6rB0tK9eu9w=; b=Q58s/HOGfasX+7VbqJYbMM6GOEK4aMZJf0cl/7e2KDgqnFwzhFAkE9RwVWWE7JOz4PipEwim oL1O6BIuHReByh50lQwPh2JBWbobb1w8B9yVQW3jH4uuWJt5uomOSLWs530ojN+0J0ZkzYvt wlx+dgWk2MFBh/w9qKYHbL1/vPw= X-Mailgun-Sending-Ip: 69.72.43.7 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n06.prod.us-west-2.postgun.com with SMTP id 615535b4fc6e34f8cd02e1a0 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Thu, 30 Sep 2021 03:57:40 GMT Sender: cang=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 2AE76C4361C; Thu, 30 Sep 2021 03:57:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.codeaurora.org (localhost.localdomain [127.0.0.1]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: cang) by smtp.codeaurora.org (Postfix) with ESMTPSA id 288F8C4338F; Thu, 30 Sep 2021 03:57:38 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 30 Sep 2021 11:57:38 +0800 From: Can Guo To: Bart Van Assche Cc: asutoshd@codeaurora.org, nguyenb@codeaurora.org, hongwus@codeaurora.org, ziqichen@codeaurora.org, linux-scsi@vger.kernel.org, kernel-team@android.com, Alim Akhtar , Avri Altman , "James E.J. Bottomley" , "Martin K. Petersen" , Bean Huo , Stanley Chu , Jaegeuk Kim , Adrian Hunter , open list Subject: Re: [PATCH] scsi: ufs: Fix a possible dead lock in clock scaling In-Reply-To: <644dcd92-25ae-e951-d9f3-607306a02370@acm.org> References: <1631843521-2863-1-git-send-email-cang@codeaurora.org> <644dcd92-25ae-e951-d9f3-607306a02370@acm.org> Message-ID: <27e9265371e96d0bcc06139ce5f0e026@codeaurora.org> X-Sender: cang@codeaurora.org User-Agent: Roundcube Webmail/1.3.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-09-30 02:15, Bart Van Assche wrote: > On 9/28/21 8:31 PM, Can Guo wrote: >> On 2021-09-18 01:27, Bart Van Assche wrote: >>> On 9/16/21 6:51 PM, Can Guo wrote: >>>> Assume a scenario where task A and B call ufshcd_devfreq_scale() >>>> simultaneously. After task B calls downgrade_write() [1], but before >>>> it >>>> calls down_read() [3], if task A calls down_write() [2], when task B >>>> calls >>>> down_read() [3], it will lead to dead lock. >>> >>> Something is wrong with the above description. The downgrade_write() >>> call is >>> not followed by down_read() but by up_read(). Additionally, I don't >>> see how >>> concurrent calls of ufshcd_devfreq_scale() could lead to a deadlock. >> >> As mentioned in the commit msg, the down_read() [3] is from >> ufshcd_wb_ctrl(). >> >> Task A - >> down_write [2] >> ufshcd_clock_scaling_prepare >> ufshcd_devfreq_scale >> ufshcd_clkscale_enable_store >> >> Task B - >> down_read [3] >> ufshcd_exec_dev_cmd >> ufshcd_query_flag >> ufshcd_wb_ctrl >> downgrade_write [1] >> ufshcd_devfreq_scale >> ufshcd_devfreq_target >> devfreq_set_target >> update_devfreq >> devfreq_performance_handler >> governor_store >> >> >>> If one thread calls downgrade_write() and another thread calls >>> down_write() >>> immediately, that down_write() call will block until the other thread >>> has called up_read() >>> without triggering a deadlock. >> >> Since the down_write() caller is blocked, the down_read() caller, >> which comes after >> down_write(), is blocked too, no? downgrade_write() keeps lock owner >> as it is, but >> it does not change the fact that readers and writers can be blocked by >> each other. > > Please use the upstream function names when posting upstream patches. > I think that > ufshcd_wb_ctrl() has been renamed into ufshcd_wb_toggle(). > > So the deadlock is caused by nested locking - one task holding a > reader lock, another > task calling down_write() and next the first task grabbing the reader > lock recursively? > I prefer one of the following two solutions above the patch that has > been posted since > I expect that both alternatives will result in easier to maintain UFS > code: > - Fix the down_read() implementation. Making down_read() wait in case > of nested locking > seems wrong to me. > - Modify the UFS driver such that it does not lock > hba->clk_scaling_lock recursively. My current change is the 2nd solution - drop the hba->clk_scaling_lock before calls ufshcd_wb_toggle() to avoid recursive lock. Thanks, Can Guo. > > Thanks, > > Bart.