From: Krzysztof Kozlowski <k.kozlowski@samsung.com>
To: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
x86@kernel.org, Linus Walleij <linus.walleij@linaro.org>,
Samuel Ortiz <sameo@linux.intel.com>,
Lee Jones <lee.jones@linaro.org>,
Sebastian Reichel <sre@kernel.org>,
Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>,
David Woodhouse <dwmw2@infradead.org>,
Krzysztof Kozlowski <k.kozlowski@samsung.com>,
Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
"Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
Lv Zheng <lv.zheng@intel.com>,
Bjorn Helgaas <bhelgaas@google.com>,
linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org
Cc: Anton Vorontsov <anton@enomsg.org>, Pavel Machek <pavel@ucw.cz>,
Kyungmin Park <kyungmin.park@samsung.com>,
Marek Szyprowski <m.szyprowski@samsung.com>,
Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
Subject: [PATCH v2 1/7] power_supply: Add power_supply_put for decrementing device reference counter
Date: Thu, 16 Oct 2014 09:42:01 +0200 [thread overview]
Message-ID: <1413445327-24862-2-git-send-email-k.kozlowski@samsung.com> (raw)
In-Reply-To: <1413445327-24862-1-git-send-email-k.kozlowski@samsung.com>
The power_supply_get_by_phandle() and power_supply_get_by_name() use
function class_find_device() for obtaining the reference to power
supply. Each use of class_find_device() increases the power supply's
device reference counter.
However the reference counter was not decreased by users of this API.
Thus final device_unregister() call from power_supply_unregister() could
not release the device and clean up its resources. This lead to memory
leak if at least once power_supply_get_by_*() was called between
registering and unregistering the power supply.
Add and document new API power_supply_put() for decrementing the
reference counter.
Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Acked-by: Pavel Machek <pavel@ucw.cz>
---
drivers/power/power_supply_core.c | 38 ++++++++++++++++++++++++++++++++++++++
include/linux/power_supply.h | 1 +
2 files changed, 39 insertions(+)
diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 94f278cf2cbb..db368527c65b 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -331,6 +331,17 @@ static int power_supply_match_device_by_name(struct device *dev, const void *dat
return strcmp(psy->name, name) == 0;
}
+/**
+ * power_supply_get_by_name() - Search for a power supply and returns its ref
+ * @name: Power supply name to fetch
+ *
+ * If power supply was found, it increases reference count for the
+ * internal power supply's device. The user should power_supply_put()
+ * after usage.
+ *
+ * Return: On success returns a reference to a power supply with
+ * matching name equals to @name, a NULL otherwise.
+ */
struct power_supply *power_supply_get_by_name(const char *name)
{
struct device *dev = class_find_device(power_supply_class, NULL, name,
@@ -340,12 +351,39 @@ struct power_supply *power_supply_get_by_name(const char *name)
}
EXPORT_SYMBOL_GPL(power_supply_get_by_name);
+/**
+ * power_supply_put() - Drop reference obtained with power_supply_get_by_name
+ * @psy: Reference to put
+ *
+ * The reference to power supply should be put before unregistering
+ * the power supply.
+ */
+void power_supply_put(struct power_supply *psy)
+{
+ might_sleep();
+
+ put_device(psy->dev);
+}
+EXPORT_SYMBOL_GPL(power_supply_put);
+
#ifdef CONFIG_OF
static int power_supply_match_device_node(struct device *dev, const void *data)
{
return dev->parent && dev->parent->of_node == data;
}
+/**
+ * power_supply_get_by_phandle() - Search for a power supply and returns its ref
+ * @np: Pointer to device node holding phandle property
+ * @phandle_name: Name of property holding a power supply name
+ *
+ * If power supply was found, it increases reference count for the
+ * internal power supply's device. The user should power_supply_put()
+ * after usage.
+ *
+ * Return: On success returns a reference to a power supply with
+ * matching name equals to value under @property, NULL or ERR_PTR otherwise.
+ */
struct power_supply *power_supply_get_by_phandle(struct device_node *np,
const char *property)
{
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 44e749c65505..1b6db883e090 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -256,6 +256,7 @@ extern struct atomic_notifier_head power_supply_notifier;
extern int power_supply_reg_notifier(struct notifier_block *nb);
extern void power_supply_unreg_notifier(struct notifier_block *nb);
extern struct power_supply *power_supply_get_by_name(const char *name);
+extern void power_supply_put(struct power_supply *psy);
#ifdef CONFIG_OF
extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
const char *property);
--
1.9.1
next prev parent reply other threads:[~2014-10-16 7:42 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-16 7:42 [PATCH v2 0/7] power_supply: Decrement the " Krzysztof Kozlowski
2014-10-16 7:42 ` Krzysztof Kozlowski [this message]
2014-10-16 7:42 ` [PATCH v2 2/7] power: charger-manager: Decrement the power supply's " Krzysztof Kozlowski
2014-10-16 7:42 ` [PATCH v2 3/7] x86/olpc/xo1/sci: Use newly added power_supply_put API Krzysztof Kozlowski
2014-10-16 7:42 ` [PATCH v2 4/7] x86/olpc/xo15/sci: " Krzysztof Kozlowski
2014-10-16 7:42 ` [PATCH v2 5/7] power: 88pm860x_charger: Decrement the power supply's device reference counter Krzysztof Kozlowski
2014-10-16 7:42 ` [PATCH v2 6/7] power: bq2415x_charger: " Krzysztof Kozlowski
2014-10-16 7:42 ` [PATCH v2 7/7] mfd: ab8500: " Krzysztof Kozlowski
2014-10-16 12:29 ` Lee Jones
2014-10-16 12:41 ` Krzysztof Kozlowski
2014-10-17 7:50 ` Lee Jones
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=1413445327-24862-2-git-send-email-k.kozlowski@samsung.com \
--to=k.kozlowski@samsung.com \
--cc=anton@enomsg.org \
--cc=b.zolnierkie@samsung.com \
--cc=bhelgaas@google.com \
--cc=dbaryshkov@gmail.com \
--cc=dwmw2@infradead.org \
--cc=hpa@zytor.com \
--cc=konrad.wilk@oracle.com \
--cc=kyungmin.park@samsung.com \
--cc=lee.jones@linaro.org \
--cc=linus.walleij@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=lv.zheng@intel.com \
--cc=m.szyprowski@samsung.com \
--cc=mingo@redhat.com \
--cc=pavel@ucw.cz \
--cc=rafael.j.wysocki@intel.com \
--cc=sameo@linux.intel.com \
--cc=sre@kernel.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.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
all inboxes | Powered by JetHome®