From patchwork Thu Dec 6 12:43:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 153012 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp10479080ljp; Thu, 6 Dec 2018 04:44:06 -0800 (PST) X-Google-Smtp-Source: AFSGD/Xi+J05SJgdtdIX0toh/ldJbkdooDa+7CSLSr/nkj04jJP9q0vlv+RKLIgV+9oQG10zwymH X-Received: by 2002:a17:902:b68d:: with SMTP id c13mr28124500pls.102.1544100246601; Thu, 06 Dec 2018 04:44:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544100246; cv=none; d=google.com; s=arc-20160816; b=UMojzGCJeHYhjoVxpepcEAK448ClbqcaxFQ4wkae4tvQCkRi220JTDs0C0ZPQemiwn Aho4w3OSugoQniSOjG3sdf/9ocd7rGgHNQvs4ruVVwK6uaPYhGRB66nkx6Syku62BKnh zcYoWAAYa9etJHheBA5aIyQOdsuov7EC4QEweUimuvNgvU2VPMV8Apy+ZXT1AwkGRj8y jOSe/bo2/aH170+tZiI9bdQHmao6A6slTOKpdrq402GvW7aun211kLcrCXUTDrKPYu+4 1QUiAMM9D4uLFZUUjmQJev2Uaiz9WYIj/dzkiSRbqLVtt5rBKN+7RS5/fDDNypEvKsuc TwUg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tOxZO3nUb+U86qEGSI5iNJyT/exUrePPUOkDnR1qhVY=; b=MlsYc+MmjwDjvw/Frorr3LOXS4k0t3zPqE7ukcNYsm1JYuuwzvuZ9IW8QlZsNT/Izn V7h7TVZkKVo3amfXiiPUYYNfXcmbdj06tKn3dou/2YAhQ0CUTA2kk3MGuIl/PJ6RVIVR RxtfvqWi5UPP9iPTxRqECV65mqjpDNQq3fMwCeoj3TstEE0NU0mcqUdRYNtqTZ4wCVMZ 6In63PRzU/JfKAVBVj0pEzfIoDR8vqT85fEOms0tmPuS5qS1IUr72pc4lA3kv/fF3HcH N8Gtcqa7mjXaAVeTupib0fNhGGSI1qmklJKQYnifm6UV7tNBuijw37NavDESxQ3LNwYb EOXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=T3X5npWw; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x18si230714pfm.39.2018.12.06.04.44.06; Thu, 06 Dec 2018 04:44:06 -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=@linaro.org header.s=google header.b=T3X5npWw; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729588AbeLFMoF (ORCPT + 31 others); Thu, 6 Dec 2018 07:44:05 -0500 Received: from mail-lj1-f193.google.com ([209.85.208.193]:40014 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727832AbeLFMoE (ORCPT ); Thu, 6 Dec 2018 07:44:04 -0500 Received: by mail-lj1-f193.google.com with SMTP id n18-v6so276346lji.7 for ; Thu, 06 Dec 2018 04:44:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tOxZO3nUb+U86qEGSI5iNJyT/exUrePPUOkDnR1qhVY=; b=T3X5npWw2s5gDwjGiLO+z4UcfTzTL+V9W6JjAq/wlgfmrZibqqOvRt/UPDTVaB69XZ WyO4zfZDSLcoELOUhCR1CMbxCwQheZiPalM0O75Q1ayPsBgA16ZCic7T/GRUncqXco9Z nvZMwx3wmO/9AQ0rbSHRkPsXafK90QOTFWcJs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tOxZO3nUb+U86qEGSI5iNJyT/exUrePPUOkDnR1qhVY=; b=AWaCml/AypbzpMxCQ+ion08yijUYPTbmfsokL5n+9gPwETLpG+3+V7yYliiKD4UESR dl2Ebh2mF7XFLyJCiDPFay5Km0NcgGb7JEC1kd8GtB1ViKA6VTCjxBWSRjGQC8ucdSuF Cf9f7EaLO3NIGUurk8uUCLW6S7nQixZx7bBotBdOkGfTrLyIXwlwuAMnnfbx6GgNbYP0 MpOgASERIq5FdWH3Tf0AviY4VkRTC/h6QxJ3wfOy+0abTb6FFjTMLVRp76ziZ7rx+Aa2 HLkLmnC0lFDSbX2FhGXLcJuNeS0Vd3Q3gjl06QOvsnz3y7gbq5VcBzSOl4ZxdGKzuIyN CBpQ== X-Gm-Message-State: AA+aEWZmlW07gKfeSqUP0Pmbp0JRSCE0mHOAQAnGPsGCOFaiPFGJkJNk xc1F0v67MsD60A1FgtmvbgWW7w== X-Received: by 2002:a2e:1f01:: with SMTP id f1-v6mr17921486ljf.129.1544100241274; Thu, 06 Dec 2018 04:44:01 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id j25-v6sm44071lji.77.2018.12.06.04.43.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 06 Dec 2018 04:44:00 -0800 (PST) From: Linus Walleij To: Liam Girdwood , Mark Brown Cc: linux-kernel@vger.kernel.org, Charles Keepax , Bartosz Golaszewski , Marek Szyprowski , Linus Walleij Subject: [PATCH 01/15 v4] regulator: core: Track dangling GPIO descriptors Date: Thu, 6 Dec 2018 13:43:37 +0100 Message-Id: <20181206124351.10155-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181206124351.10155-1-linus.walleij@linaro.org> References: <20181206124351.10155-1-linus.walleij@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a GPIO descriptor is passed to the regulator_register() function inside the config->ena_gpiod callers must be sure that once they call this API the regulator core owns that descriptor and will make sure to issue gpiod_put() on it, no matter whether the call is successful or not. For device tree regulators, the regulator core will automatically set up regulator init data from the device tree when registering a regulator by calling regulator_of_get_init_data() which in turn calls down to the regulator driver's .of_parse_cb() callback. This callback (in drivers such as for max77686) may also choose to fill in the config->ena_gpiod field with a GPIO descriptor. Harden the errorpath of regulator_register() to properly gpiod_put() any passed in cfg->ena_gpiod or any gpiod coming from the device tree on any type of error. Signed-off-by: Linus Walleij --- ChangeLog v3->v4: - Resending. ChangeLog v2->v3: - Resending. ChangeLog v1->v2: - New patch, just numbering v2 to keep it in line with the patch set it came out of. --- drivers/regulator/core.c | 55 ++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 10 deletions(-) -- 2.19.2 diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 757619878068..79cb090ff22f 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -4868,21 +4868,33 @@ regulator_register(const struct regulator_desc *regulator_desc, struct regulator_config *config = NULL; static atomic_t regulator_no = ATOMIC_INIT(-1); struct regulator_dev *rdev; + bool dangling_cfg_gpiod = false; + bool dangling_of_gpiod = false; struct device *dev; int ret, i; - if (regulator_desc == NULL || cfg == NULL) + if (cfg == NULL) return ERR_PTR(-EINVAL); + if (cfg->ena_gpiod) + dangling_cfg_gpiod = true; + if (regulator_desc == NULL) { + ret = -EINVAL; + goto rinse; + } dev = cfg->dev; WARN_ON(!dev); - if (regulator_desc->name == NULL || regulator_desc->ops == NULL) - return ERR_PTR(-EINVAL); + if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { + ret = -EINVAL; + goto rinse; + } if (regulator_desc->type != REGULATOR_VOLTAGE && - regulator_desc->type != REGULATOR_CURRENT) - return ERR_PTR(-EINVAL); + regulator_desc->type != REGULATOR_CURRENT) { + ret = -EINVAL; + goto rinse; + } /* Only one of each should be implemented */ WARN_ON(regulator_desc->ops->get_voltage && @@ -4893,16 +4905,20 @@ regulator_register(const struct regulator_desc *regulator_desc, /* If we're using selectors we must implement list_voltage. */ if (regulator_desc->ops->get_voltage_sel && !regulator_desc->ops->list_voltage) { - return ERR_PTR(-EINVAL); + ret = -EINVAL; + goto rinse; } if (regulator_desc->ops->set_voltage_sel && !regulator_desc->ops->list_voltage) { - return ERR_PTR(-EINVAL); + ret = -EINVAL; + goto rinse; } rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); - if (rdev == NULL) - return ERR_PTR(-ENOMEM); + if (rdev == NULL) { + ret = -ENOMEM; + goto rinse; + } /* * Duplicate the config so the driver could override it after @@ -4911,11 +4927,22 @@ regulator_register(const struct regulator_desc *regulator_desc, config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); if (config == NULL) { kfree(rdev); - return ERR_PTR(-ENOMEM); + ret = -ENOMEM; + goto rinse; } init_data = regulator_of_get_init_data(dev, regulator_desc, config, &rdev->dev.of_node); + /* + * We need to keep track of any GPIO descriptor coming from the + * device tree until we have handled it over to the core. If the + * config that was passed in to this function DOES NOT contain + * a descriptor, and the config after this call DOES contain + * a descriptor, we definately got one from parsing the device + * tree. + */ + if (!cfg->ena_gpiod && config->ena_gpiod) + dangling_of_gpiod = true; if (!init_data) { init_data = config->init_data; rdev->dev.of_node = of_node_get(config->of_node); @@ -4954,6 +4981,9 @@ regulator_register(const struct regulator_desc *regulator_desc, config->ena_gpio, ret); goto clean; } + /* The regulator core took over the GPIO descriptor */ + dangling_cfg_gpiod = false; + dangling_of_gpiod = false; } /* register with sysfs */ @@ -5039,8 +5069,13 @@ regulator_register(const struct regulator_desc *regulator_desc, regulator_ena_gpio_free(rdev); mutex_unlock(®ulator_list_mutex); clean: + if (dangling_of_gpiod) + gpiod_put(config->ena_gpiod); kfree(rdev); kfree(config); +rinse: + if (dangling_cfg_gpiod) + gpiod_put(cfg->ena_gpiod); return ERR_PTR(ret); } EXPORT_SYMBOL_GPL(regulator_register);