From patchwork Tue Nov 4 15:01:34 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 5228401 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.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 891649F3EE for ; Tue, 4 Nov 2014 15:04:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2596B20166 for ; Tue, 4 Nov 2014 15:04: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 E52C820122 for ; Tue, 4 Nov 2014 15:04:50 +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 1Xlfcf-0005gP-0m; Tue, 04 Nov 2014 15:02:17 +0000 Received: from mailout3.w1.samsung.com ([210.118.77.13]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Xlfca-0005Pm-0Z for linux-arm-kernel@lists.infradead.org; Tue, 04 Nov 2014 15:02:13 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout3.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NEI00B3RSJRKW20@mailout3.w1.samsung.com> for linux-arm-kernel@lists.infradead.org; Tue, 04 Nov 2014 15:04:40 +0000 (GMT) X-AuditID: cbfec7f4-b7f6c6d00000120b-18-5458ea5bea62 Received: from eusync1.samsung.com ( [203.254.199.211]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 9F.65.04619.B5AE8545; Tue, 04 Nov 2014 15:01:47 +0000 (GMT) Received: from AMDC1943.digital.local ([106.116.151.171]) by eusync1.samsung.com (Oracle Communications Messaging Server 7u4-23.01(7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0NEI00F9USEVQU60@eusync1.samsung.com>; Tue, 04 Nov 2014 15:01:47 +0000 (GMT) From: Krzysztof Kozlowski To: Linus Walleij , Samuel Ortiz , Lee Jones , Sebastian Reichel , Dmitry Eremin-Solenikov , David Woodhouse , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Subject: [PATCH RESEND v2 1/8] power_supply: Add API for safe access of power supply function attrs Date: Tue, 04 Nov 2014 16:01:34 +0100 Message-id: <1415113301-25661-2-git-send-email-k.kozlowski@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1415113301-25661-1-git-send-email-k.kozlowski@samsung.com> References: <1415113301-25661-1-git-send-email-k.kozlowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrCLMWRmVeSWpSXmKPExsVy+t/xy7rRryJCDObd0rE4uFXTYuOM9awW k568Z7aYuHIys0Xn2SfMFq9fGFqcbXrDbnH/61FGiyl/ljNZbHp8jdXi8q45bBafe48wWjxZ eIbJYu2Ru+wWtxtXsFncPXWUzeJ0N6vF6d0lDkIeE/o/MXrsnHWX3WPzCi2PTas62TzuXNvD 5jHvZKDH5iX1Hju/N7B79G1ZxeixYvV3do/Pm+QCuKO4bFJSczLLUov07RK4Mi7fsClY4l7x 5sJDpgbGOzZdjJwcEgImEk+3XmSBsMUkLtxbzwZiCwksZZQ4PEexi5ELyO5jkjh5dj9YEZuA scTm5UvYQBIiAteZJDpXTGABcZgFGpkl3jU0M4FUCQukSvyZ1sUOYrMIqEqsnvEYrJtXwF1i zaduVoh1chInj00GszkFPCQarv5hhVjtLrGit51tAiPvAkaGVYyiqaXJBcVJ6bmGesWJucWl eel6yfm5mxghIf9lB+PiY1aHGAU4GJV4eDPUI0KEWBPLiitzDzFKcDArifCqPwYK8aYkVlal FuXHF5XmpBYfYmTi4JRqYOTMmso9a2H+shBmNg7xCe/eCtwu+Sj9wt5ye+w1pTkzRZuO/Ge8 yLfJzTZD/9fxuQ22E9OnrfMIeKY6d/mDnnWqvGvXG39ZuGTW7aj4wyq3qsql7J6x71rXkxaj dSDnmXwh82+vkx0FXa+5NmR9UTVwXR6bMuup4H5pfpnd2z7eXPNE/+ikVC0lluKMREMt5qLi RACz1RxfVwIAAA== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141104_070212_209796_9988844B X-CRM114-Status: GOOD ( 15.14 ) X-Spam-Score: -5.6 (-----) Cc: Jonghwa Lee , Bartlomiej Zolnierkiewicz , Anton Vorontsov , Krzysztof Kozlowski , Kyungmin Park , Myungjoo Ham , Pavel Machek , Guenter Roeck , 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=-2.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, 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 Add simple wrappers for accessing power supply's function attributes: - get_property -> power_supply_get_property - set_property -> power_supply_set_property - property_is_writeable -> power_supply_property_is_writeable - external_power_changed -> power_supply_external_power_changed - set_charged -> power_supply_set_charged This API adds a safe way of accessing a power supply from another driver. Particularly it solves invalid memory references (and lockup) in following race condition scenario: Thread 1: charger manager Thread 2: power supply driver, used by charger manager THREAD 1 (charger manager) THREAD 2 (power supply driver) ========================== ============================== psy = power_supply_get_by_name() Driver unbind, .remove power_supply_unregister Device fully removed psy->get_property() The 'get_property' callback is still valid and leads to actual calling of Thread2->get_property() but now in invalid context because the driver was unbound. This could be observed easily with charger manager driver (here compiled with max17042 fuel gauge): $ echo "12-0036" > /sys/bus/i2c/drivers/max17042/unbind $ cat /sys/devices/virtual/power_supply/battery/temp [ 240.505998] INFO: task cat:1394 blocked for more than 120 seconds. [ 240.510762] Not tainted 3.17.0-next-20141007-00028-ge60b6dd79570-dirty #183 [ 240.518208] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 240.526025] cat D c0469548 0 1394 1 0x00000000 [ 240.532384] [] (__schedule) from [] (schedule_preempt_disabled+0x14/0x20) [ 240.540885] [] (schedule_preempt_disabled) from [] (mutex_lock_nested+0x1bc/0x458) [ 240.550171] [] (mutex_lock_nested) from [] (regmap_read+0x30/0x60) [ 240.558079] [] (regmap_read) from [] (max17042_get_property+0x2e8/0x350) [ 240.566490] [] (max17042_get_property) from [] (charger_get_property+0x238/0x31c) [ 240.575688] [] (charger_get_property) from [] (power_supply_show_property+0x48/0x1e0) [ 240.585241] [] (power_supply_show_property) from [] (dev_attr_show+0x1c/0x48) [ 240.594100] [] (dev_attr_show) from [] (sysfs_kf_seq_show+0x84/0x104) [ 240.602251] [] (sysfs_kf_seq_show) from [] (kernfs_seq_show+0x24/0x28) [ 240.610497] [] (kernfs_seq_show) from [] (seq_read+0x1b0/0x484) [ 240.618138] [] (seq_read) from [] (vfs_read+0x88/0x144) [ 240.625127] [] (vfs_read) from [] (SyS_read+0x40/0x8c) [ 240.631941] [] (SyS_read) from [] (ret_fast_syscall+0x0/0x48) Or: [ 17.277605] Division by zero in kernel. [ 17.280043] CPU: 2 PID: 1384 Comm: cat Not tainted 3.17.0-next-20141007-00028-ge60b6dd79570-dirty #181 [ 17.289348] [] (unwind_backtrace) from [] (show_stack+0x10/0x14) [ 17.297055] [] (show_stack) from [] (dump_stack+0x70/0xbc) [ 17.304264] [] (dump_stack) from [] (Ldiv0+0x8/0x10) [ 17.310933] [] (Ldiv0) from [] (__aeabi_uidivmod+0x8/0x18) [ 17.318132] [] (__aeabi_uidivmod) from [] (regmap_read+0x1c/0x60) [ 17.325956] [] (regmap_read) from [] (max17042_get_property+0x1bc/0x350) [ 17.334361] [] (max17042_get_property) from [] (charger_get_property+0x198/0x328) [ 17.343560] [] (charger_get_property) from [] (power_supply_show_property+0x48/0x1e0) [ 17.353108] [] (power_supply_show_property) from [] (power_supply_uevent+0x9c/0x1c4) [ 17.362575] [] (power_supply_uevent) from [] (dev_uevent+0xb8/0x1c8) [ 17.370639] [] (dev_uevent) from [] (uevent_show+0xa8/0x104) [ 17.378015] [] (uevent_show) from [] (dev_attr_show+0x1c/0x48) [ 17.385579] [] (dev_attr_show) from [] (sysfs_kf_seq_show+0x84/0x104) [ 17.393731] [] (sysfs_kf_seq_show) from [] (kernfs_seq_show+0x24/0x28) [ 17.401979] [] (kernfs_seq_show) from [] (seq_read+0x1b0/0x484) [ 17.409619] [] (seq_read) from [] (vfs_read+0x88/0x144) [ 17.416557] [] (vfs_read) from [] (SyS_read+0x40/0x8c) [ 17.423416] [] (SyS_read) from [] (ret_fast_syscall+0x0/0x48) [ 17.430880] power_supply battery: driver failed to report `voltage_now' property: -22 Signed-off-by: Krzysztof Kozlowski Acked-by: Jonghwa Lee Acked-by: Pavel Machek --- drivers/power/power_supply_core.c | 57 +++++++++++++++++++++++++++++++++++++-- include/linux/power_supply.h | 16 +++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c index 694e8cddd5c1..bd8f5be0d976 100644 --- a/drivers/power/power_supply_core.c +++ b/drivers/power/power_supply_core.c @@ -366,6 +366,56 @@ struct power_supply *power_supply_get_by_phandle(struct device_node *np, EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); #endif /* CONFIG_OF */ +int power_supply_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + if (!psy->dev) + return -ENODEV; + + return psy->get_property(psy, psp, val); +} +EXPORT_SYMBOL_GPL(power_supply_get_property); + +int power_supply_set_property(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + if (!psy->dev || !psy->set_property) + return -ENODEV; + + return psy->set_property(psy, psp, val); +} +EXPORT_SYMBOL_GPL(power_supply_set_property); + +int power_supply_property_is_writeable(struct power_supply *psy, + enum power_supply_property psp) +{ + if (!psy->dev || !psy->property_is_writeable) + return -ENODEV; + + return psy->property_is_writeable(psy, psp); +} +EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); + +void power_supply_external_power_changed(struct power_supply *psy) +{ + if (!psy->dev || !psy->external_power_changed) + return; + + psy->external_power_changed(psy); +} +EXPORT_SYMBOL_GPL(power_supply_external_power_changed); + +void power_supply_set_charged(struct power_supply *psy) +{ + if (!psy->dev || !psy->set_charged) + return; + + psy->set_charged(psy); +} +EXPORT_SYMBOL_GPL(power_supply_set_charged); + int power_supply_powers(struct power_supply *psy, struct device *dev) { return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers"); @@ -619,13 +669,16 @@ EXPORT_SYMBOL_GPL(power_supply_register_no_ws); void power_supply_unregister(struct power_supply *psy) { + struct device *dev = psy->dev; + cancel_work_sync(&psy->changed_work); sysfs_remove_link(&psy->dev->kobj, "powers"); + psy->dev = NULL; power_supply_remove_triggers(psy); psy_unregister_cooler(psy); psy_unregister_thermal(psy); - device_init_wakeup(psy->dev, false); - device_unregister(psy->dev); + device_init_wakeup(dev, false); + device_unregister(dev); } EXPORT_SYMBOL_GPL(power_supply_unregister); diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 096dbced02ac..0ef515301aaf 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -189,6 +189,12 @@ struct power_supply { size_t num_supplies; struct device_node *of_node; + /* + * Functions for drivers implementing power supply class. + * These shouldn't be called directly by other drivers for accessing + * this power supply. Instead use power_supply_*() functions (for + * example power_supply_get_property()). + */ int (*get_property)(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val); @@ -274,6 +280,16 @@ extern int power_supply_is_system_supplied(void); static inline int power_supply_is_system_supplied(void) { return -ENOSYS; } #endif +extern int power_supply_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val); +extern int power_supply_set_property(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val); +extern int power_supply_property_is_writeable(struct power_supply *psy, + enum power_supply_property psp); +extern void power_supply_external_power_changed(struct power_supply *psy); +extern void power_supply_set_charged(struct power_supply *psy); extern int power_supply_register(struct device *parent, struct power_supply *psy); extern int power_supply_register_no_ws(struct device *parent,