From patchwork Thu Sep 17 12:57:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomeu Vizoso X-Patchwork-Id: 7207731 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C2EB69F380 for ; Thu, 17 Sep 2015 13:48:34 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B531420751 for ; Thu, 17 Sep 2015 13:48:33 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A62A320804 for ; Thu, 17 Sep 2015 13:48:32 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZcZWB-0002PK-SO; Thu, 17 Sep 2015 13:46:31 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZcZVt-0002JM-Q3 for linux-arm-kernel@bombadil.infradead.org; Thu, 17 Sep 2015 13:46:13 +0000 Received: from mail-wi0-x232.google.com ([2a00:1450:400c:c05::232]) by merlin.infradead.org with esmtps (Exim 4.85 #2 (Red Hat Linux)) id 1ZcYld-00035H-WF for linux-arm-kernel@lists.infradead.org; Thu, 17 Sep 2015 12:58:26 +0000 Received: by wiclk2 with SMTP id lk2so26211912wic.1 for ; Thu, 17 Sep 2015 05:57:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=W2opL6ym4VivHyKBO/aqA3ZZRXzBh5Ufg0ApS5Q0Q/E=; b=ORhx6ri1p320B7mMaTkbLnQTcJoaa8BnpEcxcfKOvXtQ1SMtl5exHAgqyrppUFl93f rYnov4BCyoH3IRT4MChmPOmhlnFayC5+bOf4zM1ZGn+en2wNKA05csItWgclYqVhBSpa hPXhLq/2CR36ZoBYxa5CKMVOdnJSPLn8f6NnOwhVCsCsatJL9t7SYnMnjCEvMxQSe9Sl h1UJ1Z4L74cYKQt6AkVwChrqaI5pYkB0ONGvhbi3n0GJaMesvaalcZ/NvpVJgtJAFK53 vk9Iock7CnGYnPAyU50ckzT5/ajGSiDeD0aSDUz7jiz0FFp5wBZ5nJG5P15gb8od0CRV Sc4g== X-Received: by 10.180.87.162 with SMTP id az2mr29685711wib.62.1442494677328; Thu, 17 Sep 2015 05:57:57 -0700 (PDT) Received: from cizrna.lan ([109.72.12.203]) by smtp.gmail.com with ESMTPSA id jr5sm2203032wjc.26.2015.09.17.05.57.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Sep 2015 05:57:55 -0700 (PDT) From: Tomeu Vizoso To: linux-kernel@vger.kernel.org Subject: [PATCH v5 08/23] regulator: core: Drop redundant locking Date: Thu, 17 Sep 2015 14:57:02 +0200 Message-Id: <1442494637-3674-9-git-send-email-tomeu.vizoso@collabora.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1442494637-3674-1-git-send-email-tomeu.vizoso@collabora.com> References: <1442494637-3674-1-git-send-email-tomeu.vizoso@collabora.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150917_085826_309379_D0C64199 X-CRM114-Status: GOOD ( 20.94 ) X-Spam-Score: -2.6 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tomeu Vizoso , devicetree@vger.kernel.org, linux-acpi@vger.kernel.org, Arnd Bergmann , Stephen Warren , Greg Kroah-Hartman , Linus Walleij , Dmitry Torokhov , "Rafael J. Wysocki" , Rob Herring , Javier Martinez Canillas , Mark Brown , Thierry Reding , Alan Stern , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Now that we use a klist when looking up regulator devices, we can avoid locking regulator_list_mutex if we take a reference on the regulator device, if one is found. This change would be useful if for example regulator devices could be registered on demand when a driver requests them. regulator_register() could end up being called from within _regulator_get while the lock on regulator_list_mutex is being held, causing a deadlock. This sequence illustrates the situation described above: tegra_hdmi_probe _regulator_get regulator_dev_lookup of_device_probe reg_fixed_voltage_probe regulator_register Signed-off-by: Tomeu Vizoso --- Changes in v5: - Use regulator_class' klist of devices instead of regulator_list to store and lookup regulator devices. Changes in v4: - Take a reference to the regulator's device to prevent dangling pointers Changes in v3: - Avoid unlocking the regulator device's mutex if we don't have a device Changes in v2: - Acquire regulator device lock before returning from regulator_dev_lookup() drivers/regulator/core.c | 49 +++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index b048af9c1ae4..24245c78b58c 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -1328,7 +1328,7 @@ static void regulator_supply_alias(struct device **dev, const char **supply) static int of_node_match(struct device *dev, const void *data) { - return dev->of_node == data; + return dev->of_node == data && get_device(dev); } static struct regulator_dev *of_find_regulator_by_node(struct device_node *np) @@ -1344,7 +1344,7 @@ static int regulator_match(struct device *dev, const void *data) { struct regulator_dev *r = dev_to_rdev(dev); - return strcmp(rdev_get_name(r), data) == 0; + return strcmp(rdev_get_name(r), data) == 0 && get_device(dev); } static struct regulator_dev *regulator_lookup_by_name(const char *name) @@ -1395,16 +1395,20 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev, if (r) return r; + mutex_lock(®ulator_list_mutex); list_for_each_entry(map, ®ulator_map_list, list) { /* If the mapping has a device set up it must match */ if (map->dev_name && (!devname || strcmp(map->dev_name, devname))) continue; - if (strcmp(map->supply, supply) == 0) + if (strcmp(map->supply, supply) == 0 && + get_device(&map->regulator->dev)) { + mutex_unlock(®ulator_list_mutex); return map->regulator; + } } - + mutex_unlock(®ulator_list_mutex); return NULL; } @@ -1435,6 +1439,7 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) if (!r) { if (have_full_constraints()) { r = dummy_regulator_rdev; + get_device(&r->dev); } else { dev_err(dev, "Failed to resolve %s-supply for %s\n", rdev->supply_name, rdev->desc->name); @@ -1444,12 +1449,16 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) /* Recursively resolve the supply of the supply */ ret = regulator_resolve_supply(r); - if (ret < 0) + if (ret < 0) { + put_device(&r->dev); return ret; + } ret = set_supply(rdev, r); - if (ret < 0) + if (ret < 0) { + put_device(&r->dev); return ret; + } /* Cascade always-on state to supply */ if (_regulator_is_enabled(rdev) && rdev->supply) { @@ -1485,8 +1494,6 @@ static struct regulator *_regulator_get(struct device *dev, const char *id, else ret = -EPROBE_DEFER; - mutex_lock(®ulator_list_mutex); - rdev = regulator_dev_lookup(dev, id, &ret); if (rdev) goto found; @@ -1498,7 +1505,7 @@ static struct regulator *_regulator_get(struct device *dev, const char *id, * succeed, so, quit with appropriate error value */ if (ret && ret != -ENODEV) - goto out; + return regulator; if (!devname) devname = "deviceless"; @@ -1512,40 +1519,46 @@ static struct regulator *_regulator_get(struct device *dev, const char *id, devname, id); rdev = dummy_regulator_rdev; + get_device(&rdev->dev); goto found; /* Don't log an error when called from regulator_get_optional() */ } else if (!have_full_constraints() || exclusive) { dev_warn(dev, "dummy supplies not allowed\n"); } - mutex_unlock(®ulator_list_mutex); return regulator; found: if (rdev->exclusive) { regulator = ERR_PTR(-EPERM); - goto out; + put_device(&rdev->dev); + return regulator; } if (exclusive && rdev->open_count) { regulator = ERR_PTR(-EBUSY); - goto out; + put_device(&rdev->dev); + return regulator; } ret = regulator_resolve_supply(rdev); if (ret < 0) { regulator = ERR_PTR(ret); - goto out; + put_device(&rdev->dev); + return regulator; } - if (!try_module_get(rdev->owner)) - goto out; + if (!try_module_get(rdev->owner)) { + put_device(&rdev->dev); + return regulator; + } regulator = create_regulator(rdev, dev, id); if (regulator == NULL) { regulator = ERR_PTR(-ENOMEM); + put_device(&rdev->dev); module_put(rdev->owner); - goto out; + return regulator; } rdev->open_count++; @@ -1559,9 +1572,6 @@ found: rdev->use_count = 0; } -out: - mutex_unlock(®ulator_list_mutex); - return regulator; } @@ -1659,6 +1669,7 @@ static void _regulator_put(struct regulator *regulator) rdev->open_count--; rdev->exclusive = 0; + put_device(&rdev->dev); mutex_unlock(&rdev->mutex); kfree(regulator->supply_name);