From patchwork Fri Jan 30 14:47:50 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 5751101 Return-Path: X-Original-To: patchwork-linux-acpi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B21DFBF440 for ; Fri, 30 Jan 2015 14:52:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B95BD202E5 for ; Fri, 30 Jan 2015 14:52:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 92E7D202BE for ; Fri, 30 Jan 2015 14:52:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1422723AbbA3Os4 (ORCPT ); Fri, 30 Jan 2015 09:48:56 -0500 Received: from mailout4.w1.samsung.com ([210.118.77.14]:50429 "EHLO mailout4.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1422636AbbA3Osn (ORCPT ); Fri, 30 Jan 2015 09:48:43 -0500 Received: from eucpsbgm2.samsung.com (unknown [203.254.199.245]) 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 <0NIZ00HLEVZQF680@mailout4.w1.samsung.com>; Fri, 30 Jan 2015 14:52:38 +0000 (GMT) X-AuditID: cbfec7f5-b7fc86d0000066b7-63-54cb9933e823 Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm2.samsung.com (EUCPMTA) with SMTP id 8F.78.26295.3399BC45; Fri, 30 Jan 2015 14:46:12 +0000 (GMT) Received: from AMDC1943.digital.local ([106.116.151.171]) by eusync2.samsung.com (Oracle Communications Messaging Server 7u4-23.01(7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0NIZ00EV0VSES470@eusync2.samsung.com>; Fri, 30 Jan 2015 14:48:37 +0000 (GMT) From: Krzysztof Kozlowski To: Sebastian Reichel , Dmitry Eremin-Solenikov , David Woodhouse , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, "Rafael J. Wysocki" , Len Brown , Jiri Kosina , David Herrmann , Cezary Jackiewicz , Darren Hart , Support Opensource , Milo Kim , Julian Andres Klode , Marc Dietrich , Greg Kroah-Hartman , 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, 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 , Russell King Cc: Thomas Gleixner , Pavel Machek , Kyungmin Park , Bartlomiej Zolnierkiewicz , Krzysztof Kozlowski Subject: [PATCH v3 12/20] power_supply: Add power_supply_put for decrementing device reference counter Date: Fri, 30 Jan 2015 15:47:50 +0100 Message-id: <1422629278-12202-13-git-send-email-k.kozlowski@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1422629278-12202-1-git-send-email-k.kozlowski@samsung.com> References: <1422629278-12202-1-git-send-email-k.kozlowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAAyWRa0iTcRSH+++97FUcvUzTtwl+kCII8pbkQSUCC9+CQEEo+mAtnRdy0zYn 2pdEk3Dq1M3KRE3xMi8xdRZkznnJmTovy6UYOqW0nKbmJaUktU2/PefH8zvnw6Ewfg8uoBIl qSKpRJjkTTrjpoOPny8EvjRF+TWqOKAc3eBAW2kLAbWDeThk7a/goFr8hYF+ZI8LNRZ3UFT7 QXGjGoPsmhYSzNogeN7mAfWbr0joLK/B4afNH0azVrkwv2NE0PF1jYSSfxoOaAyFCHQLUwR8 2dolwPK+nITtgn4Eub/LSJix8CB3vBqBZk+Lw0RPlb2wdkiCddhIQrPehsGIaYKAslVXMOUR YOpMha6cTxi0655h8Kd1gbhyjn2SnU+yDVu+7JzhA84WarcJtqPMymU3NqPZ70VqLtvecJ5d XNEQrK4pl2Rnp/Qkuz42xmUrhyLZ7orXdqH2MWvMn8UjBHecQ2NFSYlpIqnv5XvOCXmHP7gp h17pg+YKPBPlCxSIohg6kNFlBSiQkx3dGfNcC6lAzhSfrkPM36J1/HhQcpgCvRJzWCR9kWnX 1B5ZbvSaEzOg1B9ZGD2DmA59I8ex1pVOYBRd4Cjg9FlG3d+NO5hHs4xhxoaOz3kxQwNqwsFO 9rzV9u2I+XQ4UzhZShYhXhU60YROieQxKbL78eIAH5lQLJNL4n1iksU6dPz1nXeobiC4D9EU 8nbhTQtNUXxCmCbLEPchhsK83XglKnvEixVmPBJJk+9K5UkiWR/iUE6CTKRpvR0X5jOrRBWF D2tE1oM3cf0zSfIXXddzAgMku00n4zpPey41LO+Hzd8scAuxDHus9QpCDCPTZrJ23KTf1w7d UtXfsBhRhLWCjug1PFD3SJbnJqnVqy5a/8qg4unwM8GRoZea0ybTn0YPRwVvG+eveU5oJW+X BvDB3p3mSm9cliD0P49JZcL/KUTSj9MCAAA= Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 0dcf5ca0092e..95aeae44c34a 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 54ebac5e3fe3..7000a4927ac8 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);