Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1483768pxb; Fri, 22 Jan 2021 17:59:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJwmYMSeN8VRHL3Y+CKNdnbjlgB2wGiZ9388r1JU62T/+XEo3g7UENdaRAvYHmoZoPxugl1w X-Received: by 2002:a17:906:f85b:: with SMTP id ks27mr604891ejb.20.1611367153323; Fri, 22 Jan 2021 17:59:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611367153; cv=none; d=google.com; s=arc-20160816; b=j4QOAYmZD/ZbfNqLBW6O79KoA2EKJdyUJSPBQd1Cen/EEHXeP4LrqpqLRhgrJFiCIq jboAkbC3jo/041qcJbbF+a0DUO2CBlF3N31hr69R5ajFatyQMvpJS7ddJHpKYqFtsI57 uUnCBfEDUBw/yP/lwRwAn52biN+p0VjGxHmDiPU99Q+6KFWnpg3P28fb/uTrNge5P6GF NgCNhwZJe5DdwuplArHN/ft/QszuemTO/QkwFpe3di/Dd/IjIDQH9GDyNULLmbK12Xuy roin3vG7h6aQs021WuDQcKwVRjd9I3uEvjECHFzIeVIdEuHtkR6EgLlmUpUevT+MIw7Q sV4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dmarc-filter:sender:dkim-signature; bh=PSuBZhuZUbMXWczL8TbyXTEbtdPcTovEfVwdjNUqKUs=; b=oqwmVGkCPUQ9/qcwCZuQ01EDnYgpu4FOAPxwvZZ7C/kHpb4y3gsW0XXr+Rc8s297PG 7tdk/U4wuSQtTkzzuC0nfUYBBjIu5FZfPCzhHMn7Lypan71qAgDDjRJm8hZdimav7h/A Uz0FdjpzUf5CJz6pgnm39EHfKW27QoM1wILOyNiREpAJd3JwAsN6SxEPxw/n0XbJ9pAv 5AwtVtsrVL/lOCKSETP/xmJ7fs4BqMbOPCrJu5ZenoSb3SRBGaU1S7Qfv6SPFHrfVqN3 t0y+iuwYM1XSmfL93WBDGLaWW8RGQRuUnT4vVk20FrLO+YGploVzbOD7LhUJgn+P4zH9 0GhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=bsaC8au5; 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 f25si2661308edr.468.2021.01.22.17.58.49; Fri, 22 Jan 2021 17:59:13 -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; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=bsaC8au5; 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 S1726147AbhAWBzy (ORCPT + 99 others); Fri, 22 Jan 2021 20:55:54 -0500 Received: from m42-8.mailgun.net ([69.72.42.8]:31663 "EHLO m42-8.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726024AbhAWBzx (ORCPT ); Fri, 22 Jan 2021 20:55:53 -0500 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1611366933; h=Content-Transfer-Encoding: Content-Type: In-Reply-To: MIME-Version: Date: Message-ID: From: References: Cc: To: Subject: Sender; bh=PSuBZhuZUbMXWczL8TbyXTEbtdPcTovEfVwdjNUqKUs=; b=bsaC8au5ekPN9mLu5qhPViufuhwrOrpTYFMkEybfJRtIRsCDix7YPxiTbaILFDFgTFYPi0IC PQNwONqIoGn6hSyzeunvwhNbUrbWPJxCLoE5cHlnm2pfgzAuV0SmdNaTXdhdnqMH0n12iMIR 3u3np0yiWPoJeh/Xdym3WzDCUr0= X-Mailgun-Sending-Ip: 69.72.42.8 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-n05.prod.us-east-1.postgun.com with SMTP id 600b81f8beacd1a2525bd85f (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Sat, 23 Jan 2021 01:55:04 GMT Sender: collinsd=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 4671BC43461; Sat, 23 Jan 2021 01:55:03 +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=-3.1 required=2.0 tests=ALL_TRUSTED,BAYES_00, NICE_REPLY_A,SPF_FAIL,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from [10.46.160.165] (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: collinsd) by smtp.codeaurora.org (Postfix) with ESMTPSA id 4F7ECC433CA; Sat, 23 Jan 2021 01:54:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 4F7ECC433CA Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=fail smtp.mailfrom=collinsd@codeaurora.org Subject: Re: [PATCH] regulator: core: avoid regulator_resolve_supply() race condition To: Mark Brown Cc: Marek Szyprowski , Liam Girdwood , Krzysztof Kozlowski , Bartlomiej Zolnierkiewicz , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, 'Linux Samsung SOC' References: <1610068562-4410-1-git-send-email-collinsd@codeaurora.org> <20210118204958.GS4455@sirena.org.uk> <5f37ae96-c5f9-6619-d88f-21c5e483ff8e@samsung.com> <20210121154418.GE4588@sirena.org.uk> From: David Collins Message-ID: <5d02de1f-a6f4-c564-6738-2ca46b5d5cc0@codeaurora.org> Date: Fri, 22 Jan 2021 17:54:56 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Mark, On 1/21/21 12:30 PM, Marek Szyprowski wrote: > Hi Mark, > > On 21.01.2021 16:44, Mark Brown wrote: >> On Thu, Jan 21, 2021 at 10:41:59AM +0100, Marek Szyprowski wrote: >>> On 18.01.2021 21:49, Mark Brown wrote: >>>> Does this help (completely untested): >>> Sadly nope. I get same warning: >> Try this instead: >> >> diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c >> index 3ae5ccd9277d..31503776dbd7 100644 >> --- a/drivers/regulator/core.c >> +++ b/drivers/regulator/core.c >> @@ -1823,17 +1823,6 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) >> if (rdev->supply) >> return 0; >> >> - /* >> - * Recheck rdev->supply with rdev->mutex lock held to avoid a race >> - * between rdev->supply null check and setting rdev->supply in >> - * set_supply() from concurrent tasks. >> - */ >> - regulator_lock(rdev); >> - >> - /* Supply just resolved by a concurrent task? */ >> - if (rdev->supply) >> - goto out; >> - >> r = regulator_dev_lookup(dev, rdev->supply_name); >> if (IS_ERR(r)) { >> ret = PTR_ERR(r); >> @@ -1885,12 +1874,29 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) >> goto out; >> } >> >> + /* >> + * Recheck rdev->supply with rdev->mutex lock held to avoid a race >> + * between rdev->supply null check and setting rdev->supply in >> + * set_supply() from concurrent tasks. >> + */ >> + regulator_lock(rdev); >> + >> + /* Supply just resolved by a concurrent task? */ >> + if (rdev->supply) { >> + regulator_unlock(rdev); >> + put_device(&r->dev); >> + return ret; >> + } >> + >> ret = set_supply(rdev, r); >> if (ret < 0) { >> + regulator_unlock(rdev); >> put_device(&r->dev); >> - goto out; >> + return ret; >> } >> >> + regulator_unlock(rdev); >> + >> /* >> * In set_machine_constraints() we may have turned this regulator on >> * but we couldn't propagate to the supply if it hadn't been resolved >> @@ -1901,12 +1907,11 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) >> if (ret < 0) { >> _regulator_put(rdev->supply); >> rdev->supply = NULL; >> - goto out; >> + goto out_rdev_lock; > > drivers/regulator/core.c:1910:4: error: label ‘out_rdev_lock’ used but > not defined > >> } >> } >> >> out: >> - regulator_unlock(rdev); >> return ret; >> } >> > > It looks that it finally fixes the locking issue, with the above goto > removed completely to fix build. Feel free to add: > > Reported-by: Marek Szyprowski > > Tested-by: Marek Szyprowski Thank you for making this fix. I'm sorry that I missed the potential deadlock issue resulting from the regulator_enable() call inside regulator_resolve_supply() with rdev->mutex locked. Your fix avoids deadlock while still ensuring that the there isn't a set supply race condition. Take care, David -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project