* FAILED: patch "[PATCH] power: supply: lp8788-charger: fix use-after-free on remove" failed to apply to 5.10-stable tree
@ 2026-09-03 14:38 gregkh
2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin
0 siblings, 1 reply; 9+ messages in thread
From: gregkh @ 2026-09-03 14:38 UTC (permalink / raw)
To: fanwu01, sebastian.reichel; +Cc: stable
The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm
tree, then please email the backport, including the original git commit
id to <stable@vger.kernel.org>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 831c29a12d560f8a3225f43050b3fbb5dfd79c66
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<stable@vger.kernel.org>' --in-reply-to '2026090353-unadorned-buffed-09ad@gregkh' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 831c29a12d560f8a3225f43050b3fbb5dfd79c66 Mon Sep 17 00:00:00 2001
From: Fan Wu <fanwu01@zju.edu.cn>
Date: Sun, 2 Aug 2026 03:54:42 +0000
Subject: [PATCH] power: supply: lp8788-charger: fix use-after-free on remove
lp8788_charger_remove() flushes charger_work before unregistering the
IRQs. An IRQ thread can queue charger_work after flush_work() has
returned. The work can then run after devres frees pchg and dereference
it in lp8788_charger_event().
Unregister the IRQs first. free_irq() waits for any running threaded
handler, so no handler can queue more work afterwards. Then use
cancel_work_sync() to cancel pending work or wait for running work to
finish.
This issue was found by an in-house static analysis tool.
Fixes: 98a276649358 ("power_supply: Add new lp8788 charger driver")
Cc: stable@vger.kernel.org
Assisted-by: Codex:gpt-5.6
Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
Link: https://patch.msgid.link/20260802035442.421697-1-fanwu01@zju.edu.cn
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
diff --git a/drivers/power/supply/lp8788-charger.c b/drivers/power/supply/lp8788-charger.c
index f0a680c155c4..1bc2c150fb91 100644
--- a/drivers/power/supply/lp8788-charger.c
+++ b/drivers/power/supply/lp8788-charger.c
@@ -710,8 +710,8 @@ static void lp8788_charger_remove(struct platform_device *pdev)
{
struct lp8788_charger *pchg = platform_get_drvdata(pdev);
- flush_work(&pchg->charger_work);
lp8788_irq_unregister(pdev, pchg);
+ cancel_work_sync(&pchg->charger_work);
}
static struct platform_driver lp8788_charger_driver = {
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 5.10.y 1/8] driver core: platform: reorder functions 2026-09-03 14:38 FAILED: patch "[PATCH] power: supply: lp8788-charger: fix use-after-free on remove" failed to apply to 5.10-stable tree gregkh @ 2026-09-09 11:21 ` Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 2/8] driver core: platform: change logic implementing platform_driver_probe Sasha Levin ` (6 more replies) 0 siblings, 7 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Uwe Kleine-König, Greg Kroah-Hartman, Sasha Levin From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> [ Upstream commit e21d740a3fe5ad2db7b5f5c2331fe2b713b1edba ] This way all callbacks and structures used to initialize platform_bus_type are defined just before platform_bus_type and in the same order. Also move platform_drv_probe_fail just before it's only user. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Link: https://lore.kernel.org/r/20201119124611.2573057-1-u.kleine-koenig@pengutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Stable-dep-of: 831c29a12d56 ("power: supply: lp8788-charger: fix use-after-free on remove") Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/base/platform.c | 293 ++++++++++++++++++++-------------------- 1 file changed, 147 insertions(+), 146 deletions(-) diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 647066229fec3..fa023cf80dc48 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -772,11 +772,6 @@ static int platform_drv_probe(struct device *_dev) return ret; } -static int platform_drv_probe_fail(struct device *_dev) -{ - return -ENXIO; -} - static int platform_drv_remove(struct device *_dev) { struct platform_driver *drv = to_platform_driver(_dev->driver); @@ -827,6 +822,11 @@ void platform_driver_unregister(struct platform_driver *drv) } EXPORT_SYMBOL_GPL(platform_driver_unregister); +static int platform_drv_probe_fail(struct device *_dev) +{ + return -ENXIO; +} + /** * __platform_driver_probe - register driver for non-hotpluggable device * @drv: platform driver structure @@ -1017,109 +1017,6 @@ void platform_unregister_drivers(struct platform_driver * const *drivers, } EXPORT_SYMBOL_GPL(platform_unregister_drivers); -/* modalias support enables more hands-off userspace setup: - * (a) environment variable lets new-style hotplug events work once system is - * fully running: "modprobe $MODALIAS" - * (b) sysfs attribute lets new-style coldplug recover from hotplug events - * mishandled before system is fully running: "modprobe $(cat modalias)" - */ -static ssize_t modalias_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - int len; - - len = of_device_modalias(dev, buf, PAGE_SIZE); - if (len != -ENODEV) - return len; - - len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); - if (len != -ENODEV) - return len; - - return sysfs_emit(buf, "platform:%s\n", pdev->name); -} -static DEVICE_ATTR_RO(modalias); - -static ssize_t driver_override_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - int ret; - - ret = driver_set_override(dev, &pdev->driver_override, buf, count); - if (ret) - return ret; - - return count; -} - -static ssize_t driver_override_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - ssize_t len; - - device_lock(dev); - len = sysfs_emit(buf, "%s\n", pdev->driver_override); - device_unlock(dev); - - return len; -} -static DEVICE_ATTR_RW(driver_override); - -static ssize_t numa_node_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return sysfs_emit(buf, "%d\n", dev_to_node(dev)); -} -static DEVICE_ATTR_RO(numa_node); - -static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a, - int n) -{ - struct device *dev = container_of(kobj, typeof(*dev), kobj); - - if (a == &dev_attr_numa_node.attr && - dev_to_node(dev) == NUMA_NO_NODE) - return 0; - - return a->mode; -} - -static struct attribute *platform_dev_attrs[] = { - &dev_attr_modalias.attr, - &dev_attr_numa_node.attr, - &dev_attr_driver_override.attr, - NULL, -}; - -static struct attribute_group platform_dev_group = { - .attrs = platform_dev_attrs, - .is_visible = platform_dev_attrs_visible, -}; -__ATTRIBUTE_GROUPS(platform_dev); - -static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) -{ - struct platform_device *pdev = to_platform_device(dev); - int rc; - - /* Some devices have extra OF data and an OF-style MODALIAS */ - rc = of_device_uevent_modalias(dev, env); - if (rc != -ENODEV) - return rc; - - rc = acpi_device_uevent_modalias(dev, env); - if (rc != -ENODEV) - return rc; - - add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, - pdev->name); - return 0; -} - static const struct platform_device_id *platform_match_id( const struct platform_device_id *id, struct platform_device *pdev) @@ -1134,44 +1031,6 @@ static const struct platform_device_id *platform_match_id( return NULL; } -/** - * platform_match - bind platform device to platform driver. - * @dev: device. - * @drv: driver. - * - * Platform device IDs are assumed to be encoded like this: - * "<name><instance>", where <name> is a short description of the type of - * device, like "pci" or "floppy", and <instance> is the enumerated - * instance of the device, like '0' or '42'. Driver IDs are simply - * "<name>". So, extract the <name> from the platform_device structure, - * and compare it against the name of the driver. Return whether they match - * or not. - */ -static int platform_match(struct device *dev, struct device_driver *drv) -{ - struct platform_device *pdev = to_platform_device(dev); - struct platform_driver *pdrv = to_platform_driver(drv); - - /* When driver_override is set, only bind to the matching driver */ - if (pdev->driver_override) - return !strcmp(pdev->driver_override, drv->name); - - /* Attempt an OF style match first */ - if (of_driver_match_device(dev, drv)) - return 1; - - /* Then try ACPI style match */ - if (acpi_driver_match_device(dev, drv)) - return 1; - - /* Then try to match against the id table */ - if (pdrv->id_table) - return platform_match_id(pdrv->id_table, pdev) != NULL; - - /* fall-back to driver name match */ - return (strcmp(pdev->name, drv->name) == 0); -} - #ifdef CONFIG_PM_SLEEP static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) @@ -1316,6 +1175,148 @@ int platform_pm_restore(struct device *dev) #endif /* CONFIG_HIBERNATE_CALLBACKS */ +/* modalias support enables more hands-off userspace setup: + * (a) environment variable lets new-style hotplug events work once system is + * fully running: "modprobe $MODALIAS" + * (b) sysfs attribute lets new-style coldplug recover from hotplug events + * mishandled before system is fully running: "modprobe $(cat modalias)" + */ +static ssize_t modalias_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct platform_device *pdev = to_platform_device(dev); + int len; + + len = of_device_modalias(dev, buf, PAGE_SIZE); + if (len != -ENODEV) + return len; + + len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); + if (len != -ENODEV) + return len; + + return sysfs_emit(buf, "platform:%s\n", pdev->name); +} +static DEVICE_ATTR_RO(modalias); + +static ssize_t numa_node_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%d\n", dev_to_node(dev)); +} +static DEVICE_ATTR_RO(numa_node); + +static ssize_t driver_override_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct platform_device *pdev = to_platform_device(dev); + ssize_t len; + + device_lock(dev); + len = sysfs_emit(buf, "%s\n", pdev->driver_override); + device_unlock(dev); + + return len; +} + +static ssize_t driver_override_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct platform_device *pdev = to_platform_device(dev); + int ret; + + ret = driver_set_override(dev, &pdev->driver_override, buf, count); + if (ret) + return ret; + + return count; +} +static DEVICE_ATTR_RW(driver_override); + +static struct attribute *platform_dev_attrs[] = { + &dev_attr_modalias.attr, + &dev_attr_numa_node.attr, + &dev_attr_driver_override.attr, + NULL, +}; + +static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a, + int n) +{ + struct device *dev = container_of(kobj, typeof(*dev), kobj); + + if (a == &dev_attr_numa_node.attr && + dev_to_node(dev) == NUMA_NO_NODE) + return 0; + + return a->mode; +} + +static struct attribute_group platform_dev_group = { + .attrs = platform_dev_attrs, + .is_visible = platform_dev_attrs_visible, +}; +__ATTRIBUTE_GROUPS(platform_dev); + + +/** + * platform_match - bind platform device to platform driver. + * @dev: device. + * @drv: driver. + * + * Platform device IDs are assumed to be encoded like this: + * "<name><instance>", where <name> is a short description of the type of + * device, like "pci" or "floppy", and <instance> is the enumerated + * instance of the device, like '0' or '42'. Driver IDs are simply + * "<name>". So, extract the <name> from the platform_device structure, + * and compare it against the name of the driver. Return whether they match + * or not. + */ +static int platform_match(struct device *dev, struct device_driver *drv) +{ + struct platform_device *pdev = to_platform_device(dev); + struct platform_driver *pdrv = to_platform_driver(drv); + + /* When driver_override is set, only bind to the matching driver */ + if (pdev->driver_override) + return !strcmp(pdev->driver_override, drv->name); + + /* Attempt an OF style match first */ + if (of_driver_match_device(dev, drv)) + return 1; + + /* Then try ACPI style match */ + if (acpi_driver_match_device(dev, drv)) + return 1; + + /* Then try to match against the id table */ + if (pdrv->id_table) + return platform_match_id(pdrv->id_table, pdev) != NULL; + + /* fall-back to driver name match */ + return (strcmp(pdev->name, drv->name) == 0); +} + +static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) +{ + struct platform_device *pdev = to_platform_device(dev); + int rc; + + /* Some devices have extra OF data and an OF-style MODALIAS */ + rc = of_device_uevent_modalias(dev, env); + if (rc != -ENODEV) + return rc; + + rc = acpi_device_uevent_modalias(dev, env); + if (rc != -ENODEV) + return rc; + + add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, + pdev->name); + return 0; +} + int platform_dma_configure(struct device *dev) { enum dev_dma_attr attr; -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y 2/8] driver core: platform: change logic implementing platform_driver_probe 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin @ 2026-09-09 11:21 ` Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 3/8] driver core: platform: use bus_type functions Sasha Levin ` (5 subsequent siblings) 6 siblings, 0 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Uwe Kleine-König, Greg Kroah-Hartman, Sasha Levin From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> [ Upstream commit 16085668eacdc56c46652d0f3bfef81ecace57de ] Instead of overwriting the core driver's probe function handle probing devices for drivers loaded by platform_driver_probe() in the platform driver probe function. The intended goal is to not have to change the probe function to simplify converting the platform bus to use bus functions. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Link: https://lore.kernel.org/r/20201119124611.2573057-2-u.kleine-koenig@pengutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Stable-dep-of: 831c29a12d56 ("power: supply: lp8788-charger: fix use-after-free on remove") Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/base/platform.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/base/platform.c b/drivers/base/platform.c index fa023cf80dc48..16426eb934632 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -743,12 +743,25 @@ struct platform_device *platform_device_register_full( } EXPORT_SYMBOL_GPL(platform_device_register_full); +static int platform_probe_fail(struct platform_device *pdev); + static int platform_drv_probe(struct device *_dev) { struct platform_driver *drv = to_platform_driver(_dev->driver); struct platform_device *dev = to_platform_device(_dev); int ret; + /* + * A driver registered using platform_driver_probe() cannot be bound + * again later because the probe function usually lives in __init code + * and so is gone. For these drivers .probe is set to + * platform_probe_fail in __platform_driver_probe(). Don't even + * prepare clocks and PM domains for these to match the traditional + * behaviour. + */ + if (unlikely(drv->probe == platform_probe_fail)) + return -ENXIO; + ret = of_clk_set_defaults(_dev->of_node, false); if (ret < 0) return ret; @@ -822,7 +835,7 @@ void platform_driver_unregister(struct platform_driver *drv) } EXPORT_SYMBOL_GPL(platform_driver_unregister); -static int platform_drv_probe_fail(struct device *_dev) +static int platform_probe_fail(struct platform_device *pdev) { return -ENXIO; } @@ -887,10 +900,9 @@ int __init_or_module __platform_driver_probe(struct platform_driver *drv, * new devices fail. */ spin_lock(&drv->driver.bus->p->klist_drivers.k_lock); - drv->probe = NULL; + drv->probe = platform_probe_fail; if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) retval = -ENODEV; - drv->driver.probe = platform_drv_probe_fail; spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock); if (code != retval) -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y 3/8] driver core: platform: use bus_type functions 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 2/8] driver core: platform: change logic implementing platform_driver_probe Sasha Levin @ 2026-09-09 11:21 ` Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 4/8] driver core: platform: Emit a warning if a remove callback returned non-zero Sasha Levin ` (4 subsequent siblings) 6 siblings, 0 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Uwe Kleine-König, Greg Kroah-Hartman, Sasha Levin From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> [ Upstream commit 9c30921fe7994907e0b3e0637b2c8c0fc4b5171f ] This works towards the goal mentioned in 2006 in commit 594c8281f905 ("[PATCH] Add bus_type probe, remove, shutdown methods."). The functions are moved to where the other bus_type functions are defined and renamed to match the already established naming scheme. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Link: https://lore.kernel.org/r/20201119124611.2573057-3-u.kleine-koenig@pengutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Stable-dep-of: 831c29a12d56 ("power: supply: lp8788-charger: fix use-after-free on remove") Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/base/platform.c | 132 ++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 67 deletions(-) diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 16426eb934632..90166535a5c05 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -743,70 +743,6 @@ struct platform_device *platform_device_register_full( } EXPORT_SYMBOL_GPL(platform_device_register_full); -static int platform_probe_fail(struct platform_device *pdev); - -static int platform_drv_probe(struct device *_dev) -{ - struct platform_driver *drv = to_platform_driver(_dev->driver); - struct platform_device *dev = to_platform_device(_dev); - int ret; - - /* - * A driver registered using platform_driver_probe() cannot be bound - * again later because the probe function usually lives in __init code - * and so is gone. For these drivers .probe is set to - * platform_probe_fail in __platform_driver_probe(). Don't even - * prepare clocks and PM domains for these to match the traditional - * behaviour. - */ - if (unlikely(drv->probe == platform_probe_fail)) - return -ENXIO; - - ret = of_clk_set_defaults(_dev->of_node, false); - if (ret < 0) - return ret; - - ret = dev_pm_domain_attach(_dev, true); - if (ret) - goto out; - - if (drv->probe) { - ret = drv->probe(dev); - if (ret) - dev_pm_domain_detach(_dev, true); - } - -out: - if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) { - dev_warn(_dev, "probe deferral not supported\n"); - ret = -ENXIO; - } - - return ret; -} - -static int platform_drv_remove(struct device *_dev) -{ - struct platform_driver *drv = to_platform_driver(_dev->driver); - struct platform_device *dev = to_platform_device(_dev); - int ret = 0; - - if (drv->remove) - ret = drv->remove(dev); - dev_pm_domain_detach(_dev, true); - - return ret; -} - -static void platform_drv_shutdown(struct device *_dev) -{ - struct platform_driver *drv = to_platform_driver(_dev->driver); - struct platform_device *dev = to_platform_device(_dev); - - if (drv->shutdown) - drv->shutdown(dev); -} - /** * __platform_driver_register - register a driver for platform-level devices * @drv: platform driver structure @@ -817,9 +753,6 @@ int __platform_driver_register(struct platform_driver *drv, { drv->driver.owner = owner; drv->driver.bus = &platform_bus_type; - drv->driver.probe = platform_drv_probe; - drv->driver.remove = platform_drv_remove; - drv->driver.shutdown = platform_drv_shutdown; return driver_register(&drv->driver); } @@ -1329,6 +1262,68 @@ static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) return 0; } +static int platform_probe(struct device *_dev) +{ + struct platform_driver *drv = to_platform_driver(_dev->driver); + struct platform_device *dev = to_platform_device(_dev); + int ret; + + /* + * A driver registered using platform_driver_probe() cannot be bound + * again later because the probe function usually lives in __init code + * and so is gone. For these drivers .probe is set to + * platform_probe_fail in __platform_driver_probe(). Don't even prepare + * clocks and PM domains for these to match the traditional behaviour. + */ + if (unlikely(drv->probe == platform_probe_fail)) + return -ENXIO; + + ret = of_clk_set_defaults(_dev->of_node, false); + if (ret < 0) + return ret; + + ret = dev_pm_domain_attach(_dev, true); + if (ret) + goto out; + + if (drv->probe) { + ret = drv->probe(dev); + if (ret) + dev_pm_domain_detach(_dev, true); + } + +out: + if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) { + dev_warn(_dev, "probe deferral not supported\n"); + ret = -ENXIO; + } + + return ret; +} + +static int platform_remove(struct device *_dev) +{ + struct platform_driver *drv = to_platform_driver(_dev->driver); + struct platform_device *dev = to_platform_device(_dev); + int ret = 0; + + if (drv->remove) + ret = drv->remove(dev); + dev_pm_domain_detach(_dev, true); + + return ret; +} + +static void platform_shutdown(struct device *_dev) +{ + struct platform_driver *drv = to_platform_driver(_dev->driver); + struct platform_device *dev = to_platform_device(_dev); + + if (drv->shutdown) + drv->shutdown(dev); +} + + int platform_dma_configure(struct device *dev) { enum dev_dma_attr attr; @@ -1355,6 +1350,9 @@ struct bus_type platform_bus_type = { .dev_groups = platform_dev_groups, .match = platform_match, .uevent = platform_uevent, + .probe = platform_probe, + .remove = platform_remove, + .shutdown = platform_shutdown, .dma_configure = platform_dma_configure, .pm = &platform_dev_pm_ops, }; -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y 4/8] driver core: platform: Emit a warning if a remove callback returned non-zero 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 2/8] driver core: platform: change logic implementing platform_driver_probe Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 3/8] driver core: platform: use bus_type functions Sasha Levin @ 2026-09-09 11:21 ` Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 5/8] platform: Provide a remove callback that returns no value Sasha Levin ` (3 subsequent siblings) 6 siblings, 0 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Uwe Kleine-König, Greg Kroah-Hartman, Sasha Levin From: Uwe Kleine-König <uwe@kleine-koenig.org> [ Upstream commit e5e1c209788138f33ca6558bf9f572f6904f486d ] The driver core ignores the return value of a bus' remove callback. However a driver returning an error code is a hint that there is a problem, probably a driver author who expects that returning e.g. -EBUSY has any effect. The right thing to do would be to make struct platform_driver::remove() return void. With the immense number of platform drivers this is however a big quest and I hope to prevent at least a few new drivers that return an error code here. Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org> Link: https://lore.kernel.org/r/20210207211537.19992-1-uwe@kleine-koenig.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Stable-dep-of: 831c29a12d56 ("power: supply: lp8788-charger: fix use-after-free on remove") Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/base/platform.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 90166535a5c05..d0b15cbab0ff0 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -1305,13 +1305,16 @@ static int platform_remove(struct device *_dev) { struct platform_driver *drv = to_platform_driver(_dev->driver); struct platform_device *dev = to_platform_device(_dev); - int ret = 0; - if (drv->remove) - ret = drv->remove(dev); + if (drv->remove) { + int ret = drv->remove(dev); + + if (ret) + dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n"); + } dev_pm_domain_detach(_dev, true); - return ret; + return 0; } static void platform_shutdown(struct device *_dev) -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y 5/8] platform: Provide a remove callback that returns no value 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin ` (2 preceding siblings ...) 2026-09-09 11:21 ` [PATCH 5.10.y 4/8] driver core: platform: Emit a warning if a remove callback returned non-zero Sasha Levin @ 2026-09-09 11:21 ` Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 6/8] power: supply: lp8788-charger: Convert to platform remove callback returning void Sasha Levin ` (2 subsequent siblings) 6 siblings, 0 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Uwe Kleine-König, Greg Kroah-Hartman, Sasha Levin From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> [ Upstream commit 5c5a7680e67ba6fbbb5f4d79fa41485450c1985c ] struct platform_driver::remove returning an integer made driver authors expect that returning an error code was proper error handling. However the driver core ignores the error and continues to remove the device because there is nothing the core could do anyhow and reentering the remove callback again is only calling for trouble. So this is an source for errors typically yielding resource leaks in the error path. As there are too many platform drivers to neatly convert them all to return void in a single go, do it in several steps after this patch: a) Convert all drivers to implement .remove_new() returning void instead of .remove() returning int; b) Change struct platform_driver::remove() to return void and so make it identical to .remove_new(); c) Change all drivers back to .remove() now with the better prototype; d) drop struct platform_driver::remove_new(). While this touches all drivers eventually twice, steps a) and c) can be done one driver after another and so reduces coordination efforts immensely and simplifies review. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Link: https://lore.kernel.org/r/20221209150914.3557650-1-u.kleine-koenig@pengutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Stable-dep-of: 831c29a12d56 ("power: supply: lp8788-charger: fix use-after-free on remove") Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/base/platform.c | 4 +++- include/linux/platform_device.h | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/base/platform.c b/drivers/base/platform.c index d0b15cbab0ff0..e07043d85c65c 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -1306,7 +1306,9 @@ static int platform_remove(struct device *_dev) struct platform_driver *drv = to_platform_driver(_dev->driver); struct platform_device *dev = to_platform_device(_dev); - if (drv->remove) { + if (drv->remove_new) { + drv->remove_new(dev); + } else if (drv->remove) { int ret = drv->remove(dev); if (ret) diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h index e7a83b0218077..870a918aa251c 100644 --- a/include/linux/platform_device.h +++ b/include/linux/platform_device.h @@ -203,7 +203,18 @@ extern void platform_device_put(struct platform_device *pdev); struct platform_driver { int (*probe)(struct platform_device *); + + /* + * Traditionally the remove callback returned an int which however is + * ignored by the driver core. This led to wrong expectations by driver + * authors who thought returning an error code was a valid error + * handling strategy. To convert to a callback returning void, new + * drivers should implement .remove_new() until the conversion it done + * that eventually makes .remove() return void. + */ int (*remove)(struct platform_device *); + void (*remove_new)(struct platform_device *); + void (*shutdown)(struct platform_device *); int (*suspend)(struct platform_device *, pm_message_t state); int (*resume)(struct platform_device *); -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y 6/8] power: supply: lp8788-charger: Convert to platform remove callback returning void 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin ` (3 preceding siblings ...) 2026-09-09 11:21 ` [PATCH 5.10.y 5/8] platform: Provide a remove callback that returns no value Sasha Levin @ 2026-09-09 11:21 ` Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 7/8] power: supply: lp8788: Use devm_power_supply_register() helper Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 8/8] power: supply: lp8788-charger: fix use-after-free on remove Sasha Levin 6 siblings, 0 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Uwe Kleine-König, Sebastian Reichel, Sasha Levin From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> [ Upstream commit 7810ba3c5110ce66652ec6bd2abb92da5d726c3b ] The .remove() callback for a platform driver returns an int which makes many driver authors wrongly assume it's possible to do error handling by returning an error code. However the value returned is ignored (apart from emitting a warning) and this typically results in resource leaks. To improve here there is a quest to make the remove callback return void. In the first step of this quest all drivers are converted to .remove_new() which already returns void. Eventually after all drivers are converted, .remove_new() is renamed to .remove(). Trivially convert this driver from always returning zero in the remove callback to the void returning variant. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Link: https://lore.kernel.org/r/20230918133700.1254499-17-u.kleine-koenig@pengutronix.de Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com> Stable-dep-of: 831c29a12d56 ("power: supply: lp8788-charger: fix use-after-free on remove") Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/power/supply/lp8788-charger.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/power/supply/lp8788-charger.c b/drivers/power/supply/lp8788-charger.c index 397e5a03b7d9a..20694deb05275 100644 --- a/drivers/power/supply/lp8788-charger.c +++ b/drivers/power/supply/lp8788-charger.c @@ -715,20 +715,18 @@ static int lp8788_charger_probe(struct platform_device *pdev) return 0; } -static int lp8788_charger_remove(struct platform_device *pdev) +static void lp8788_charger_remove(struct platform_device *pdev) { struct lp8788_charger *pchg = platform_get_drvdata(pdev); flush_work(&pchg->charger_work); lp8788_irq_unregister(pdev, pchg); lp8788_psy_unregister(pchg); - - return 0; } static struct platform_driver lp8788_charger_driver = { .probe = lp8788_charger_probe, - .remove = lp8788_charger_remove, + .remove_new = lp8788_charger_remove, .driver = { .name = LP8788_DEV_CHARGER, }, -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y 7/8] power: supply: lp8788: Use devm_power_supply_register() helper 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin ` (4 preceding siblings ...) 2026-09-09 11:21 ` [PATCH 5.10.y 6/8] power: supply: lp8788-charger: Convert to platform remove callback returning void Sasha Levin @ 2026-09-09 11:21 ` Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 8/8] power: supply: lp8788-charger: fix use-after-free on remove Sasha Levin 6 siblings, 0 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Andrew Davis, Sebastian Reichel, Sasha Levin From: Andrew Davis <afd@ti.com> [ Upstream commit 2abb571143c39f581df9951d263948726db42d86 ] Use the device lifecycle managed register function. This helps prevent mistakes like unregistering out of order in cleanup functions and forgetting to unregister on error paths. Signed-off-by: Andrew Davis <afd@ti.com> Link: https://lore.kernel.org/r/20240123163653.384385-7-afd@ti.com Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com> Stable-dep-of: 831c29a12d56 ("power: supply: lp8788-charger: fix use-after-free on remove") Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/power/supply/lp8788-charger.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/drivers/power/supply/lp8788-charger.c b/drivers/power/supply/lp8788-charger.c index 20694deb05275..54f663ba95700 100644 --- a/drivers/power/supply/lp8788-charger.c +++ b/drivers/power/supply/lp8788-charger.c @@ -406,12 +406,6 @@ static const struct power_supply_desc lp8788_psy_battery_desc = { .get_property = lp8788_battery_get_property, }; -static void lp8788_psy_unregister(struct lp8788_charger *pchg) -{ - power_supply_unregister(pchg->battery); - power_supply_unregister(pchg->charger); -} - static void lp8788_charger_event(struct work_struct *work) { struct lp8788_charger *pchg = @@ -667,18 +661,16 @@ static int lp8788_psy_register(struct platform_device *pdev, charger_cfg.supplied_to = battery_supplied_to; charger_cfg.num_supplicants = ARRAY_SIZE(battery_supplied_to); - pchg->charger = power_supply_register(&pdev->dev, - &lp8788_psy_charger_desc, - &charger_cfg); + pchg->charger = devm_power_supply_register(&pdev->dev, + &lp8788_psy_charger_desc, + &charger_cfg); if (IS_ERR(pchg->charger)) return -EPERM; - pchg->battery = power_supply_register(&pdev->dev, - &lp8788_psy_battery_desc, NULL); - if (IS_ERR(pchg->battery)) { - power_supply_unregister(pchg->charger); + pchg->battery = devm_power_supply_register(&pdev->dev, + &lp8788_psy_battery_desc, NULL); + if (IS_ERR(pchg->battery)) return -EPERM; - } return 0; } @@ -721,7 +713,6 @@ static void lp8788_charger_remove(struct platform_device *pdev) flush_work(&pchg->charger_work); lp8788_irq_unregister(pdev, pchg); - lp8788_psy_unregister(pchg); } static struct platform_driver lp8788_charger_driver = { -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y 8/8] power: supply: lp8788-charger: fix use-after-free on remove 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin ` (5 preceding siblings ...) 2026-09-09 11:21 ` [PATCH 5.10.y 7/8] power: supply: lp8788: Use devm_power_supply_register() helper Sasha Levin @ 2026-09-09 11:21 ` Sasha Levin 6 siblings, 0 replies; 9+ messages in thread From: Sasha Levin @ 2026-09-09 11:21 UTC (permalink / raw) To: stable; +Cc: Fan Wu, Sebastian Reichel, Sasha Levin From: Fan Wu <fanwu01@zju.edu.cn> [ Upstream commit 831c29a12d560f8a3225f43050b3fbb5dfd79c66 ] lp8788_charger_remove() flushes charger_work before unregistering the IRQs. An IRQ thread can queue charger_work after flush_work() has returned. The work can then run after devres frees pchg and dereference it in lp8788_charger_event(). Unregister the IRQs first. free_irq() waits for any running threaded handler, so no handler can queue more work afterwards. Then use cancel_work_sync() to cancel pending work or wait for running work to finish. This issue was found by an in-house static analysis tool. Fixes: 98a276649358 ("power_supply: Add new lp8788 charger driver") Cc: stable@vger.kernel.org Assisted-by: Codex:gpt-5.6 Signed-off-by: Fan Wu <fanwu01@zju.edu.cn> Link: https://patch.msgid.link/20260802035442.421697-1-fanwu01@zju.edu.cn Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com> Signed-off-by: Sasha Levin <sashal@kernel.org> --- drivers/power/supply/lp8788-charger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/power/supply/lp8788-charger.c b/drivers/power/supply/lp8788-charger.c index 54f663ba95700..3cd2238fc5d8d 100644 --- a/drivers/power/supply/lp8788-charger.c +++ b/drivers/power/supply/lp8788-charger.c @@ -711,8 +711,8 @@ static void lp8788_charger_remove(struct platform_device *pdev) { struct lp8788_charger *pchg = platform_get_drvdata(pdev); - flush_work(&pchg->charger_work); lp8788_irq_unregister(pdev, pchg); + cancel_work_sync(&pchg->charger_work); } static struct platform_driver lp8788_charger_driver = { -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-09-09 11:22 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-09-03 14:38 FAILED: patch "[PATCH] power: supply: lp8788-charger: fix use-after-free on remove" failed to apply to 5.10-stable tree gregkh 2026-09-09 11:21 ` [PATCH 5.10.y 1/8] driver core: platform: reorder functions Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 2/8] driver core: platform: change logic implementing platform_driver_probe Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 3/8] driver core: platform: use bus_type functions Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 4/8] driver core: platform: Emit a warning if a remove callback returned non-zero Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 5/8] platform: Provide a remove callback that returns no value Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 6/8] power: supply: lp8788-charger: Convert to platform remove callback returning void Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 7/8] power: supply: lp8788: Use devm_power_supply_register() helper Sasha Levin 2026-09-09 11:21 ` [PATCH 5.10.y 8/8] power: supply: lp8788-charger: fix use-after-free on remove Sasha Levin
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox