Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp2362pxb; Thu, 7 Jan 2021 17:18:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJytvZBFgpWHIr63EO/3Fv16TXNtYYL4mtiuTMcXw/m31MWqOenbFWTA8+wmMtXNyRuQVtA2 X-Received: by 2002:aa7:cac2:: with SMTP id l2mr3533301edt.141.1610068735142; Thu, 07 Jan 2021 17:18:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610068735; cv=none; d=google.com; s=arc-20160816; b=lAgUp78DZ97i6hn804OTVH6QgWmjtInlTG4d9O3RO+Rv9lb6Lx313/cBYf4nlNneLA bIH3X4Kob/pPjLY+d1fUKJulbJfmjgLkuKVrGigOQT2yqFeDKCTH7PvWUHIS+cQCvxRb h17JrXQ7XTmGFezJ5/DnIpHP0x/678qFO3AO/UbIuA2KV6seZACRQvqUnEb/Utatb3KO Td/0TUJBP1aFM52qxmQRbzn5aWMh/3KpkYvQABzJMIgqT2xkE5HO1YESZIuZXwCtB990 0DuBEHFHOugx4FJ6B5Pv3C7kwmF208lZii6pd3PAW1QQ+mjVPQe8RqxzsQnDl9rOPgRT SCpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from:dmarc-filter :sender:dkim-signature; bh=cqNjlNBBgadimuvCOQhJCtKlZ2t+VTKBlMiwNFmVwME=; b=E0xHPHSbI0hf47VUpRtsjg70IWxywSm5sZHcBkANtsjTYaWgdyhcIzu89PFGRqgyJT HSSPoU/RDQ1AWqU9unXUrqbxZs1P2bXlTplbvdL9wMmsyqxvrMb7+Sm47YLOgL5vhdDt FecvN3jaKEcygZcyi3eextMOg3VWOWQZOdDQ1uudolCRUInn73HPV/CGHJ/382t9a5UH UpTHWQ+eBl3tuLYMYYKmvf+CX9CjVEJ94G9rQtIFKxgO7pd0dzbtQEw5vJvxmBRafz8w 94WPnElcUg67y+esqEmL+nY5mR4chN2HwLEC6aQyxtZgLLCfuFNA771m4RrcYmGlxrwb SYzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=IshdKj2Z; 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 t12si572499eju.194.2021.01.07.17.18.29; Thu, 07 Jan 2021 17:18:55 -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=IshdKj2Z; 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 S1729719AbhAHBRX (ORCPT + 99 others); Thu, 7 Jan 2021 20:17:23 -0500 Received: from so254-31.mailgun.net ([198.61.254.31]:40819 "EHLO so254-31.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729564AbhAHBRX (ORCPT ); Thu, 7 Jan 2021 20:17:23 -0500 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1610068624; h=Message-Id: Date: Subject: Cc: To: From: Sender; bh=cqNjlNBBgadimuvCOQhJCtKlZ2t+VTKBlMiwNFmVwME=; b=IshdKj2ZEiNE4k4F5qe/x48XnPuUzhg8ZZVgJ9wsP5BEGGB0HreIRXoZEopWKjF01tORVr6H YBEiTzsvRqVus76Gj/Ga6xhlITbJDbBZA0UM8Pq+73B+mIpSi0EvUa5E2AYNQD90j5Hv5cDA scLuEQQflaGifgO0GmbMW7we+NY= X-Mailgun-Sending-Ip: 198.61.254.31 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-n08.prod.us-west-2.postgun.com with SMTP id 5ff7b25bb95fc593268fd052 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Fri, 08 Jan 2021 01:16:11 GMT Sender: collinsd=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id E09F8C433C6; Fri, 8 Jan 2021 01:16:10 +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,SPF_FAIL, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from codeaurora.org (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: collinsd) by smtp.codeaurora.org (Postfix) with ESMTPSA id B061AC433CA; Fri, 8 Jan 2021 01:16:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org B061AC433CA 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 From: David Collins To: Mark Brown , Liam Girdwood Cc: David Collins , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] regulator: core: avoid regulator_resolve_supply() race condition Date: Thu, 7 Jan 2021 17:16:02 -0800 Message-Id: <1610068562-4410-1-git-send-email-collinsd@codeaurora.org> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The final step in regulator_register() is to call regulator_resolve_supply() for each registered regulator (including the one in the process of being registered). The regulator_resolve_supply() function first checks if rdev->supply is NULL, then it performs various steps to try to find the supply. If successful, rdev->supply is set inside of set_supply(). This procedure can encounter a race condition if two concurrent tasks call regulator_register() near to each other on separate CPUs and one of the regulators has rdev->supply_name specified. There is currently nothing guaranteeing atomicity between the rdev->supply check and set steps. Thus, both tasks can observe rdev->supply==NULL in their regulator_resolve_supply() calls. This then results in both creating a struct regulator for the supply. One ends up actually stored in rdev->supply and the other is lost (though still present in the supply's consumer_list). Here is a kernel log snippet showing the issue: [ 12.421768] gpu_cc_gx_gdsc: supplied by pm8350_s5_level [ 12.425854] gpu_cc_gx_gdsc: supplied by pm8350_s5_level [ 12.429064] debugfs: Directory 'regulator.4-SUPPLY' with parent '17a00000.rsc:rpmh-regulator-gfxlvl-pm8350_s5_level' already present! Avoid this race condition by holding the rdev->mutex lock inside of regulator_resolve_supply() while checking and setting rdev->supply. Signed-off-by: David Collins --- drivers/regulator/core.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index fee9241..3ae5ccd 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -1813,23 +1813,34 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) { struct regulator_dev *r; struct device *dev = rdev->dev.parent; - int ret; + int ret = 0; /* No supply to resolve? */ if (!rdev->supply_name) return 0; - /* Supply already resolved? */ + /* Supply already resolved? (fast-path without locking contention) */ 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); /* Did the lookup explicitly defer for us? */ if (ret == -EPROBE_DEFER) - return ret; + goto out; if (have_full_constraints()) { r = dummy_regulator_rdev; @@ -1837,15 +1848,18 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) } else { dev_err(dev, "Failed to resolve %s-supply for %s\n", rdev->supply_name, rdev->desc->name); - return -EPROBE_DEFER; + ret = -EPROBE_DEFER; + goto out; } } if (r == rdev) { dev_err(dev, "Supply for %s (%s) resolved to itself\n", rdev->desc->name, rdev->supply_name); - if (!have_full_constraints()) - return -EINVAL; + if (!have_full_constraints()) { + ret = -EINVAL; + goto out; + } r = dummy_regulator_rdev; get_device(&r->dev); } @@ -1859,7 +1873,8 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) if (r->dev.parent && r->dev.parent != rdev->dev.parent) { if (!device_is_bound(r->dev.parent)) { put_device(&r->dev); - return -EPROBE_DEFER; + ret = -EPROBE_DEFER; + goto out; } } @@ -1867,13 +1882,13 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) ret = regulator_resolve_supply(r); if (ret < 0) { put_device(&r->dev); - return ret; + goto out; } ret = set_supply(rdev, r); if (ret < 0) { put_device(&r->dev); - return ret; + goto out; } /* @@ -1886,11 +1901,13 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) if (ret < 0) { _regulator_put(rdev->supply); rdev->supply = NULL; - return ret; + goto out; } } - return 0; +out: + regulator_unlock(rdev); + return ret; } /* Internal regulator request function */ -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project