From patchwork Tue Mar 10 08:27:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 5975351 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 C9D5D9F318 for ; Tue, 10 Mar 2015 08:36:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E1ADC20220 for ; Tue, 10 Mar 2015 08:36:52 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A63F2200EC for ; Tue, 10 Mar 2015 08:36:51 +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 1YVFbi-00006X-U7; Tue, 10 Mar 2015 08:33:42 +0000 Received: from mailout4.w1.samsung.com ([210.118.77.14]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YVFWZ-0004PL-Nb for linux-arm-kernel@lists.infradead.org; Tue, 10 Mar 2015 08:28:30 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NKZ00248MDE6V10@mailout4.w1.samsung.com> for linux-arm-kernel@lists.infradead.org; Tue, 10 Mar 2015 08:32:02 +0000 (GMT) X-AuditID: cbfec7f4-b7f126d000001e9a-38-54feaa824e84 Received: from eusync4.samsung.com ( [203.254.199.214]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 2B.DC.07834.28AAEF45; Tue, 10 Mar 2015 08:25:38 +0000 (GMT) Received: from AMDC1943.digital.local ([106.116.151.171]) by eusync4.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0NKZ0033KM64IB70@eusync4.samsung.com>; Tue, 10 Mar 2015 08:28:03 +0000 (GMT) From: Krzysztof Kozlowski To: Sebastian Reichel , Dmitry Eremin-Solenikov , David Woodhouse , "Rafael J. Wysocki" , Len Brown , Jiri Kosina , David Herrmann , Cezary Jackiewicz , Darren Hart , Support Opensource , Milo Kim , Linus Walleij , Samuel Ortiz , Lee Jones , linux-arm-kernel@lists.infradead.org, Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Daniel Mack , Haojian Zhuang , Robert Jarzmik , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-input@vger.kernel.org, platform-driver-x86@vger.kernel.org, patches@opensource.wolfsonmicro.com, ac100@lists.launchpad.net, linux-tegra@vger.kernel.org, devel@driverdev.osuosl.org Subject: [PATCH v6 14/22] power_supply: Add power_supply_put for decrementing device reference counter Date: Tue, 10 Mar 2015 09:27:18 +0100 Message-id: <1425976046-20973-15-git-send-email-k.kozlowski@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1425976046-20973-1-git-send-email-k.kozlowski@samsung.com> References: <1425976046-20973-1-git-send-email-k.kozlowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAAy3RfSzUcRwHcN/7Pbq59Qvply1/aNam5WG0PrVm1j9912rLhlmt8YvjlEN3 TmmtZJpwDqE23UrOc566UyMPMznSIQ+jrqEMm4qIU406cfrv/Xnv9dnnjw9LODaSrmxsfJJU ES/EudNi0mTtHT+cVmMN9tGU+oNmYFkEZW+ySUj7+5WE+7NLBLT1rzOgG3WBrKc+kF9dQMBQ /VF48HwvtGp1JHyb94WBtAUGPq0ZEbRML9JQ+KdSBJUduQj0M+MUmFd+UjD6SkvDak43gkxL MQ113ZMMVK7XkzDSWbKFFzdpmHxrpOFZ2zwB/aYRCooXnMCUTYGpNQna7w4TYNAXEfC7cYYK PICrVrzxVMdrEufWr1K4pXiSwXN5BQw2VHlifU0mjSfG22j8fXCQwY/7grCh7DY2qidI/CV/ g8ZLHWM0bhrLILGmqQadczkvPhEljYtNliq8AyLEsvyGaiZx0+36Qnk7k4rUrlnInuU5f/6z Jl20k134oakGOguJWUeuHPEmwxNmZ9CI+DFtL7WtaM6PN1SW2ZQzZ2T59aV5myK4EcTPzPYx 28qJk/EPKzS2DZLz4NsLPhDbWcJhvrlp+v89N76vp8Bm7Ld6i+UjuZ0duVP85MgPlIckJciu Bu2RqiITlZdi5L5eSkGuVMXHeEUmyPVo58uWZqTrOd6FOBa5O0jMF6zBjpSQrEyRdyGeJdyd JcbcrUoSJaTckCoSwhWqOKmyC4lYe9dUZB+w4bAhW6kIm0kPj10zxWYH5oY07O8UdKYM8Gi6 8k4c0aMXbvoFand7ylTqWvlJFyGELjoo07wIqk1+aRWdsWuf01Ex1ZaUnOXozF2hrffoW3Wd x9R2Vem6vGllWeFZc+niofenV6IvIvPwVYdHYaOh147ku12+M/TLkj60z51UygRfT0KhFP4B rYhHasMCAAA= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150310_012824_005122_36FD652A X-CRM114-Status: GOOD ( 10.95 ) X-Spam-Score: -5.0 (-----) Cc: Krzysztof Kozlowski , Thomas Gleixner , Kyungmin Park , Pavel Machek , Marek Szyprowski X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 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 Acked-by: Pavel Machek Reviewed-by: Bartlomiej Zolnierkiewicz Reviewed-by: Sebastian Reichel --- 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 e51405b176c8..b4ec14683b7f 100644 --- a/drivers/power/power_supply_core.c +++ b/drivers/power/power_supply_core.c @@ -336,6 +336,17 @@ static int power_supply_match_device_by_name(struct device *dev, const void *dat return strcmp(psy->desc->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, @@ -345,12 +356,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 ea15eb68f609..75a1dd8dc56e 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -282,6 +282,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);