From: ulf.hansson@linaro.org (Ulf Hansson)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 6/8] PM / Domains: Free pm_subsys_data in error path in __pm_genpd_add_device()
Date: Tue, 27 Jan 2015 21:13:43 +0100	[thread overview]
Message-ID: <1422389625-11730-7-git-send-email-ulf.hansson@linaro.org> (raw)
In-Reply-To: <1422389625-11730-1-git-send-email-ulf.hansson@linaro.org>
The error path in __pm_genpd_add_device() didn't decrease the reference
to the struct pm_subsys_data.
Let's move the calls to dev_pm_get|put_subsys_data() into
genpd_alloc|free_dev_data() to fix this issue and thus prevent a
potential memory leakage.
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
---
 drivers/base/power/domain.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index 1f026c1..3bd342f 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -1380,18 +1380,30 @@ EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev)
 {
 	struct generic_pm_domain_data *gpd_data;
+	int ret;
+
+	ret = dev_pm_get_subsys_data(dev);
+	if (ret)
+		return ERR_PTR(ret);
 
 	gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
-	if (!gpd_data)
-		return NULL;
+	if (!gpd_data) {
+		ret = -ENOMEM;
+		goto err_put;
+	}
 
 	return gpd_data;
+
+ err_put:
+	dev_pm_put_subsys_data(dev);
+	return ERR_PTR(ret);
 }
 
 static void genpd_free_dev_data(struct device *dev,
 				struct generic_pm_domain_data *gpd_data)
 {
 	kfree(gpd_data);
+	dev_pm_put_subsys_data(dev);
 }
 
 /**
@@ -1412,8 +1424,8 @@ int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
 		return -EINVAL;
 
 	gpd_data = genpd_alloc_dev_data(dev);
-	if (!gpd_data)
-		return -ENOMEM;
+	if (IS_ERR(gpd_data))
+		return PTR_ERR(gpd_data);
 
 	genpd_acquire_lock(genpd);
 
@@ -1422,10 +1434,6 @@ int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
 		goto out;
 	}
 
-	ret = dev_pm_get_subsys_data(dev);
-	if (ret)
-		goto out;
-
 	spin_lock_irq(&dev->power.lock);
 
 	if (dev->power.subsys_data->domain_data) {
@@ -1528,7 +1536,6 @@ int pm_genpd_remove_device(struct generic_pm_domain *genpd,
 
 	genpd_release_lock(genpd);
 
-	dev_pm_put_subsys_data(dev);
 	genpd_free_dev_data(dev, gpd_data);
 
 	return 0;
-- 
1.9.1
next prev parent reply	other threads:[~2015-01-27 20:13 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-27 20:13 [PATCH 0/8] PM / Domains: Re-structure code for adding/removing devices to genpd Ulf Hansson
2015-01-27 20:13 ` [PATCH 1/8] PM / Domains: Rename __pm_genpd_alloc|free_dev_data() Ulf Hansson
2015-01-28 10:06   ` Pavel Machek
2015-01-27 20:13 ` [PATCH 2/8] PM / Domains: Remove reference counting for the generic_pm_domain_data Ulf Hansson
2015-01-28 10:07   ` Pavel Machek
2015-01-27 20:13 ` [PATCH 3/8] PM / Domains: Don't allow an existing generic_pm_domain_data Ulf Hansson
2015-01-27 20:13 ` [PATCH 4/8] PM / Domains: Don't check for an existing device when adding a new Ulf Hansson
2015-01-27 20:13 ` [PATCH 5/8] PM / Domains: Eliminate the mutex for the generic_pm_domain_data Ulf Hansson
2015-01-27 20:13 ` Ulf Hansson [this message]
2015-01-27 20:13 ` [PATCH 7/8] PM / Domains: Re-order initialization of generic_pm_domain_data Ulf Hansson
2015-01-27 20:13 ` [PATCH 8/8] PM / Domains: Handle errors from genpd's ->attach_dev() callback Ulf Hansson
2015-01-28  9:35   ` Geert Uytterhoeven
2015-01-29 10:32 ` [PATCH 0/8] PM / Domains: Re-structure code for adding/removing devices to genpd Geert Uytterhoeven
2015-02-04 14:39 ` Rafael J. Wysocki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=1422389625-11730-7-git-send-email-ulf.hansson@linaro.org \
    --to=ulf.hansson@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
  Be sure your reply has a Subject: header at the top and a blank line
  before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).